capability.c 6.0 KB

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