capability.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. * Leveraged for setting/resetting capabilities
  23. */
  24. const kernel_cap_t __cap_empty_set = CAP_EMPTY_SET;
  25. const kernel_cap_t __cap_full_set = CAP_FULL_SET;
  26. const kernel_cap_t __cap_init_eff_set = CAP_INIT_EFF_SET;
  27. EXPORT_SYMBOL(__cap_empty_set);
  28. EXPORT_SYMBOL(__cap_full_set);
  29. EXPORT_SYMBOL(__cap_init_eff_set);
  30. /*
  31. * More recent versions of libcap are available from:
  32. *
  33. * http://www.kernel.org/pub/linux/libs/security/linux-privs/
  34. */
  35. static void warn_legacy_capability_use(void)
  36. {
  37. static int warned;
  38. if (!warned) {
  39. char name[sizeof(current->comm)];
  40. printk(KERN_INFO "warning: `%s' uses 32-bit capabilities"
  41. " (legacy support in use)\n",
  42. get_task_comm(name, current));
  43. warned = 1;
  44. }
  45. }
  46. /*
  47. * For sys_getproccap() and sys_setproccap(), any of the three
  48. * capability set pointers may be NULL -- indicating that that set is
  49. * uninteresting and/or not to be changed.
  50. */
  51. /**
  52. * sys_capget - get the capabilities of a given process.
  53. * @header: pointer to struct that contains capability version and
  54. * target pid data
  55. * @dataptr: pointer to struct that contains the effective, permitted,
  56. * and inheritable capabilities that are returned
  57. *
  58. * Returns 0 on success and < 0 on error.
  59. */
  60. asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
  61. {
  62. int ret = 0;
  63. pid_t pid;
  64. __u32 version;
  65. struct task_struct *target;
  66. unsigned tocopy;
  67. kernel_cap_t pE, pI, pP;
  68. if (get_user(version, &header->version))
  69. return -EFAULT;
  70. switch (version) {
  71. case _LINUX_CAPABILITY_VERSION_1:
  72. warn_legacy_capability_use();
  73. tocopy = _LINUX_CAPABILITY_U32S_1;
  74. break;
  75. case _LINUX_CAPABILITY_VERSION_2:
  76. tocopy = _LINUX_CAPABILITY_U32S_2;
  77. break;
  78. default:
  79. if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
  80. return -EFAULT;
  81. return -EINVAL;
  82. }
  83. if (get_user(pid, &header->pid))
  84. return -EFAULT;
  85. if (pid < 0)
  86. return -EINVAL;
  87. spin_lock(&task_capability_lock);
  88. read_lock(&tasklist_lock);
  89. if (pid && pid != task_pid_vnr(current)) {
  90. target = find_task_by_vpid(pid);
  91. if (!target) {
  92. ret = -ESRCH;
  93. goto out;
  94. }
  95. } else
  96. target = current;
  97. ret = security_capget(target, &pE, &pI, &pP);
  98. out:
  99. read_unlock(&tasklist_lock);
  100. spin_unlock(&task_capability_lock);
  101. if (!ret) {
  102. struct __user_cap_data_struct kdata[_LINUX_CAPABILITY_U32S];
  103. unsigned i;
  104. for (i = 0; i < tocopy; i++) {
  105. kdata[i].effective = pE.cap[i];
  106. kdata[i].permitted = pP.cap[i];
  107. kdata[i].inheritable = pI.cap[i];
  108. }
  109. /*
  110. * Note, in the case, tocopy < _LINUX_CAPABILITY_U32S,
  111. * we silently drop the upper capabilities here. This
  112. * has the effect of making older libcap
  113. * implementations implicitly drop upper capability
  114. * bits when they perform a: capget/modify/capset
  115. * sequence.
  116. *
  117. * This behavior is considered fail-safe
  118. * behavior. Upgrading the application to a newer
  119. * version of libcap will enable access to the newer
  120. * capabilities.
  121. *
  122. * An alternative would be to return an error here
  123. * (-ERANGE), but that causes legacy applications to
  124. * unexpectidly fail; the capget/modify/capset aborts
  125. * before modification is attempted and the application
  126. * fails.
  127. */
  128. if (copy_to_user(dataptr, kdata, tocopy
  129. * sizeof(struct __user_cap_data_struct))) {
  130. return -EFAULT;
  131. }
  132. }
  133. return ret;
  134. }
  135. /*
  136. * cap_set_pg - set capabilities for all processes in a given process
  137. * group. We call this holding task_capability_lock and tasklist_lock.
  138. */
  139. static inline int cap_set_pg(int pgrp_nr, kernel_cap_t *effective,
  140. kernel_cap_t *inheritable,
  141. kernel_cap_t *permitted)
  142. {
  143. struct task_struct *g, *target;
  144. int ret = -EPERM;
  145. int found = 0;
  146. struct pid *pgrp;
  147. pgrp = find_vpid(pgrp_nr);
  148. do_each_pid_task(pgrp, PIDTYPE_PGID, g) {
  149. target = g;
  150. while_each_thread(g, target) {
  151. if (!security_capset_check(target, effective,
  152. inheritable,
  153. permitted)) {
  154. security_capset_set(target, effective,
  155. inheritable,
  156. permitted);
  157. ret = 0;
  158. }
  159. found = 1;
  160. }
  161. } while_each_pid_task(pgrp, PIDTYPE_PGID, g);
  162. if (!found)
  163. ret = 0;
  164. return ret;
  165. }
  166. /*
  167. * cap_set_all - set capabilities for all processes other than init
  168. * and self. We call this holding task_capability_lock and tasklist_lock.
  169. */
  170. static inline int cap_set_all(kernel_cap_t *effective,
  171. kernel_cap_t *inheritable,
  172. kernel_cap_t *permitted)
  173. {
  174. struct task_struct *g, *target;
  175. int ret = -EPERM;
  176. int found = 0;
  177. do_each_thread(g, target) {
  178. if (target == current || is_container_init(target->group_leader))
  179. continue;
  180. found = 1;
  181. if (security_capset_check(target, effective, inheritable,
  182. permitted))
  183. continue;
  184. ret = 0;
  185. security_capset_set(target, effective, inheritable, permitted);
  186. } while_each_thread(g, target);
  187. if (!found)
  188. ret = 0;
  189. return ret;
  190. }
  191. /**
  192. * sys_capset - set capabilities for a process or a group of processes
  193. * @header: pointer to struct that contains capability version and
  194. * target pid data
  195. * @data: pointer to struct that contains the effective, permitted,
  196. * and inheritable capabilities
  197. *
  198. * Set capabilities for a given process, all processes, or all
  199. * processes in a given process group.
  200. *
  201. * The restrictions on setting capabilities are specified as:
  202. *
  203. * [pid is for the 'target' task. 'current' is the calling task.]
  204. *
  205. * I: any raised capabilities must be a subset of the (old current) permitted
  206. * P: any raised capabilities must be a subset of the (old current) permitted
  207. * E: must be set to a subset of (new target) permitted
  208. *
  209. * Returns 0 on success and < 0 on error.
  210. */
  211. asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
  212. {
  213. struct __user_cap_data_struct kdata[_LINUX_CAPABILITY_U32S];
  214. unsigned i, tocopy;
  215. kernel_cap_t inheritable, permitted, effective;
  216. __u32 version;
  217. struct task_struct *target;
  218. int ret;
  219. pid_t pid;
  220. if (get_user(version, &header->version))
  221. return -EFAULT;
  222. switch (version) {
  223. case _LINUX_CAPABILITY_VERSION_1:
  224. warn_legacy_capability_use();
  225. tocopy = _LINUX_CAPABILITY_U32S_1;
  226. break;
  227. case _LINUX_CAPABILITY_VERSION_2:
  228. tocopy = _LINUX_CAPABILITY_U32S_2;
  229. break;
  230. default:
  231. if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
  232. return -EFAULT;
  233. return -EINVAL;
  234. }
  235. if (get_user(pid, &header->pid))
  236. return -EFAULT;
  237. if (pid && pid != task_pid_vnr(current) && !capable(CAP_SETPCAP))
  238. return -EPERM;
  239. if (copy_from_user(&kdata, data, tocopy
  240. * sizeof(struct __user_cap_data_struct))) {
  241. return -EFAULT;
  242. }
  243. for (i = 0; i < tocopy; i++) {
  244. effective.cap[i] = kdata[i].effective;
  245. permitted.cap[i] = kdata[i].permitted;
  246. inheritable.cap[i] = kdata[i].inheritable;
  247. }
  248. while (i < _LINUX_CAPABILITY_U32S) {
  249. effective.cap[i] = 0;
  250. permitted.cap[i] = 0;
  251. inheritable.cap[i] = 0;
  252. i++;
  253. }
  254. spin_lock(&task_capability_lock);
  255. read_lock(&tasklist_lock);
  256. if (pid > 0 && pid != task_pid_vnr(current)) {
  257. target = find_task_by_vpid(pid);
  258. if (!target) {
  259. ret = -ESRCH;
  260. goto out;
  261. }
  262. } else
  263. target = current;
  264. ret = 0;
  265. /* having verified that the proposed changes are legal,
  266. we now put them into effect. */
  267. if (pid < 0) {
  268. if (pid == -1) /* all procs other than current and init */
  269. ret = cap_set_all(&effective, &inheritable, &permitted);
  270. else /* all procs in process group */
  271. ret = cap_set_pg(-pid, &effective, &inheritable,
  272. &permitted);
  273. } else {
  274. ret = security_capset_check(target, &effective, &inheritable,
  275. &permitted);
  276. if (!ret)
  277. security_capset_set(target, &effective, &inheritable,
  278. &permitted);
  279. }
  280. out:
  281. read_unlock(&tasklist_lock);
  282. spin_unlock(&task_capability_lock);
  283. return ret;
  284. }
  285. int __capable(struct task_struct *t, int cap)
  286. {
  287. if (security_capable(t, cap) == 0) {
  288. t->flags |= PF_SUPERPRIV;
  289. return 1;
  290. }
  291. return 0;
  292. }
  293. int capable(int cap)
  294. {
  295. return __capable(current, cap);
  296. }
  297. EXPORT_SYMBOL(capable);