capability.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * linux/kernel/capability.c
  3. *
  4. * Copyright (C) 1997 Andrew Main <zefram@fysh.org>
  5. *
  6. * Integrated into 2.1.97+, Andrew G. Morgan <morgan@transmeta.com>
  7. * 30 May 2002: Cleanup, Robert M. Love <rml@tech9.net>
  8. */
  9. #include <linux/mm.h>
  10. #include <linux/module.h>
  11. #include <linux/security.h>
  12. #include <linux/syscalls.h>
  13. #include <asm/uaccess.h>
  14. unsigned securebits = SECUREBITS_DEFAULT; /* systemwide security settings */
  15. kernel_cap_t cap_bset = CAP_INIT_EFF_SET;
  16. EXPORT_SYMBOL(securebits);
  17. EXPORT_SYMBOL(cap_bset);
  18. /*
  19. * This lock protects task->cap_* for all tasks including current.
  20. * Locking rule: acquire this prior to tasklist_lock.
  21. */
  22. static DEFINE_SPINLOCK(task_capability_lock);
  23. /*
  24. * For sys_getproccap() and sys_setproccap(), any of the three
  25. * capability set pointers may be NULL -- indicating that that set is
  26. * uninteresting and/or not to be changed.
  27. */
  28. /*
  29. * sys_capget - get the capabilities of a given process.
  30. */
  31. asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
  32. {
  33. int ret = 0;
  34. pid_t pid;
  35. __u32 version;
  36. task_t *target;
  37. struct __user_cap_data_struct data;
  38. if (get_user(version, &header->version))
  39. return -EFAULT;
  40. if (version != _LINUX_CAPABILITY_VERSION) {
  41. if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
  42. return -EFAULT;
  43. return -EINVAL;
  44. }
  45. if (get_user(pid, &header->pid))
  46. return -EFAULT;
  47. if (pid < 0)
  48. return -EINVAL;
  49. spin_lock(&task_capability_lock);
  50. read_lock(&tasklist_lock);
  51. if (pid && pid != current->pid) {
  52. target = find_task_by_pid(pid);
  53. if (!target) {
  54. ret = -ESRCH;
  55. goto out;
  56. }
  57. } else
  58. target = current;
  59. ret = security_capget(target, &data.effective, &data.inheritable, &data.permitted);
  60. out:
  61. read_unlock(&tasklist_lock);
  62. spin_unlock(&task_capability_lock);
  63. if (!ret && copy_to_user(dataptr, &data, sizeof data))
  64. return -EFAULT;
  65. return ret;
  66. }
  67. /*
  68. * cap_set_pg - set capabilities for all processes in a given process
  69. * group. We call this holding task_capability_lock and tasklist_lock.
  70. */
  71. static inline int cap_set_pg(int pgrp, kernel_cap_t *effective,
  72. kernel_cap_t *inheritable,
  73. kernel_cap_t *permitted)
  74. {
  75. task_t *g, *target;
  76. int ret = -EPERM;
  77. int found = 0;
  78. do_each_task_pid(pgrp, PIDTYPE_PGID, g) {
  79. target = g;
  80. while_each_thread(g, target) {
  81. if (!security_capset_check(target, effective,
  82. inheritable,
  83. permitted)) {
  84. security_capset_set(target, effective,
  85. inheritable,
  86. permitted);
  87. ret = 0;
  88. }
  89. found = 1;
  90. }
  91. } while_each_task_pid(pgrp, PIDTYPE_PGID, g);
  92. if (!found)
  93. ret = 0;
  94. return ret;
  95. }
  96. /*
  97. * cap_set_all - set capabilities for all processes other than init
  98. * and self. We call this holding task_capability_lock and tasklist_lock.
  99. */
  100. static inline int cap_set_all(kernel_cap_t *effective,
  101. kernel_cap_t *inheritable,
  102. kernel_cap_t *permitted)
  103. {
  104. task_t *g, *target;
  105. int ret = -EPERM;
  106. int found = 0;
  107. do_each_thread(g, target) {
  108. if (target == current || target->pid == 1)
  109. continue;
  110. found = 1;
  111. if (security_capset_check(target, effective, inheritable,
  112. permitted))
  113. continue;
  114. ret = 0;
  115. security_capset_set(target, effective, inheritable, permitted);
  116. } while_each_thread(g, target);
  117. if (!found)
  118. ret = 0;
  119. return ret;
  120. }
  121. /*
  122. * sys_capset - set capabilities for a given process, all processes, or all
  123. * processes in a given process group.
  124. *
  125. * The restrictions on setting capabilities are specified as:
  126. *
  127. * [pid is for the 'target' task. 'current' is the calling task.]
  128. *
  129. * I: any raised capabilities must be a subset of the (old current) permitted
  130. * P: any raised capabilities must be a subset of the (old current) permitted
  131. * E: must be set to a subset of (new target) permitted
  132. */
  133. asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
  134. {
  135. kernel_cap_t inheritable, permitted, effective;
  136. __u32 version;
  137. task_t *target;
  138. int ret;
  139. pid_t pid;
  140. if (get_user(version, &header->version))
  141. return -EFAULT;
  142. if (version != _LINUX_CAPABILITY_VERSION) {
  143. if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
  144. return -EFAULT;
  145. return -EINVAL;
  146. }
  147. if (get_user(pid, &header->pid))
  148. return -EFAULT;
  149. if (pid && pid != current->pid && !capable(CAP_SETPCAP))
  150. return -EPERM;
  151. if (copy_from_user(&effective, &data->effective, sizeof(effective)) ||
  152. copy_from_user(&inheritable, &data->inheritable, sizeof(inheritable)) ||
  153. copy_from_user(&permitted, &data->permitted, sizeof(permitted)))
  154. return -EFAULT;
  155. spin_lock(&task_capability_lock);
  156. read_lock(&tasklist_lock);
  157. if (pid > 0 && pid != current->pid) {
  158. target = find_task_by_pid(pid);
  159. if (!target) {
  160. ret = -ESRCH;
  161. goto out;
  162. }
  163. } else
  164. target = current;
  165. ret = 0;
  166. /* having verified that the proposed changes are legal,
  167. we now put them into effect. */
  168. if (pid < 0) {
  169. if (pid == -1) /* all procs other than current and init */
  170. ret = cap_set_all(&effective, &inheritable, &permitted);
  171. else /* all procs in process group */
  172. ret = cap_set_pg(-pid, &effective, &inheritable,
  173. &permitted);
  174. } else {
  175. ret = security_capset_check(target, &effective, &inheritable,
  176. &permitted);
  177. if (!ret)
  178. security_capset_set(target, &effective, &inheritable,
  179. &permitted);
  180. }
  181. out:
  182. read_unlock(&tasklist_lock);
  183. spin_unlock(&task_capability_lock);
  184. return ret;
  185. }