capability.c 6.1 KB

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