capability.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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/audit.h>
  10. #include <linux/capability.h>
  11. #include <linux/mm.h>
  12. #include <linux/module.h>
  13. #include <linux/security.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/pid_namespace.h>
  16. #include <linux/user_namespace.h>
  17. #include <asm/uaccess.h>
  18. /*
  19. * Leveraged for setting/resetting capabilities
  20. */
  21. const kernel_cap_t __cap_empty_set = CAP_EMPTY_SET;
  22. EXPORT_SYMBOL(__cap_empty_set);
  23. int file_caps_enabled = 1;
  24. static int __init file_caps_disable(char *str)
  25. {
  26. file_caps_enabled = 0;
  27. return 1;
  28. }
  29. __setup("no_file_caps", file_caps_disable);
  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. * Version 2 capabilities worked fine, but the linux/capability.h file
  48. * that accompanied their introduction encouraged their use without
  49. * the necessary user-space source code changes. As such, we have
  50. * created a version 3 with equivalent functionality to version 2, but
  51. * with a header change to protect legacy source code from using
  52. * version 2 when it wanted to use version 1. If your system has code
  53. * that trips the following warning, it is using version 2 specific
  54. * capabilities and may be doing so insecurely.
  55. *
  56. * The remedy is to either upgrade your version of libcap (to 2.10+,
  57. * if the application is linked against it), or recompile your
  58. * application with modern kernel headers and this warning will go
  59. * away.
  60. */
  61. static void warn_deprecated_v2(void)
  62. {
  63. static int warned;
  64. if (!warned) {
  65. char name[sizeof(current->comm)];
  66. printk(KERN_INFO "warning: `%s' uses deprecated v2"
  67. " capabilities in a way that may be insecure.\n",
  68. get_task_comm(name, current));
  69. warned = 1;
  70. }
  71. }
  72. /*
  73. * Version check. Return the number of u32s in each capability flag
  74. * array, or a negative value on error.
  75. */
  76. static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)
  77. {
  78. __u32 version;
  79. if (get_user(version, &header->version))
  80. return -EFAULT;
  81. switch (version) {
  82. case _LINUX_CAPABILITY_VERSION_1:
  83. warn_legacy_capability_use();
  84. *tocopy = _LINUX_CAPABILITY_U32S_1;
  85. break;
  86. case _LINUX_CAPABILITY_VERSION_2:
  87. warn_deprecated_v2();
  88. /*
  89. * fall through - v3 is otherwise equivalent to v2.
  90. */
  91. case _LINUX_CAPABILITY_VERSION_3:
  92. *tocopy = _LINUX_CAPABILITY_U32S_3;
  93. break;
  94. default:
  95. if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version))
  96. return -EFAULT;
  97. return -EINVAL;
  98. }
  99. return 0;
  100. }
  101. /*
  102. * The only thing that can change the capabilities of the current
  103. * process is the current process. As such, we can't be in this code
  104. * at the same time as we are in the process of setting capabilities
  105. * in this process. The net result is that we can limit our use of
  106. * locks to when we are reading the caps of another process.
  107. */
  108. static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
  109. kernel_cap_t *pIp, kernel_cap_t *pPp)
  110. {
  111. int ret;
  112. if (pid && (pid != task_pid_vnr(current))) {
  113. struct task_struct *target;
  114. rcu_read_lock();
  115. target = find_task_by_vpid(pid);
  116. if (!target)
  117. ret = -ESRCH;
  118. else
  119. ret = security_capget(target, pEp, pIp, pPp);
  120. rcu_read_unlock();
  121. } else
  122. ret = security_capget(current, pEp, pIp, pPp);
  123. return ret;
  124. }
  125. /**
  126. * sys_capget - get the capabilities of a given process.
  127. * @header: pointer to struct that contains capability version and
  128. * target pid data
  129. * @dataptr: pointer to struct that contains the effective, permitted,
  130. * and inheritable capabilities that are returned
  131. *
  132. * Returns 0 on success and < 0 on error.
  133. */
  134. SYSCALL_DEFINE2(capget, cap_user_header_t, header, cap_user_data_t, dataptr)
  135. {
  136. int ret = 0;
  137. pid_t pid;
  138. unsigned tocopy;
  139. kernel_cap_t pE, pI, pP;
  140. ret = cap_validate_magic(header, &tocopy);
  141. if ((dataptr == NULL) || (ret != 0))
  142. return ((dataptr == NULL) && (ret == -EINVAL)) ? 0 : ret;
  143. if (get_user(pid, &header->pid))
  144. return -EFAULT;
  145. if (pid < 0)
  146. return -EINVAL;
  147. ret = cap_get_target_pid(pid, &pE, &pI, &pP);
  148. if (!ret) {
  149. struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
  150. unsigned i;
  151. for (i = 0; i < tocopy; i++) {
  152. kdata[i].effective = pE.cap[i];
  153. kdata[i].permitted = pP.cap[i];
  154. kdata[i].inheritable = pI.cap[i];
  155. }
  156. /*
  157. * Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,
  158. * we silently drop the upper capabilities here. This
  159. * has the effect of making older libcap
  160. * implementations implicitly drop upper capability
  161. * bits when they perform a: capget/modify/capset
  162. * sequence.
  163. *
  164. * This behavior is considered fail-safe
  165. * behavior. Upgrading the application to a newer
  166. * version of libcap will enable access to the newer
  167. * capabilities.
  168. *
  169. * An alternative would be to return an error here
  170. * (-ERANGE), but that causes legacy applications to
  171. * unexpectidly fail; the capget/modify/capset aborts
  172. * before modification is attempted and the application
  173. * fails.
  174. */
  175. if (copy_to_user(dataptr, kdata, tocopy
  176. * sizeof(struct __user_cap_data_struct))) {
  177. return -EFAULT;
  178. }
  179. }
  180. return ret;
  181. }
  182. /**
  183. * sys_capset - set capabilities for a process or (*) a group of processes
  184. * @header: pointer to struct that contains capability version and
  185. * target pid data
  186. * @data: pointer to struct that contains the effective, permitted,
  187. * and inheritable capabilities
  188. *
  189. * Set capabilities for the current process only. The ability to any other
  190. * process(es) has been deprecated and removed.
  191. *
  192. * The restrictions on setting capabilities are specified as:
  193. *
  194. * I: any raised capabilities must be a subset of the old permitted
  195. * P: any raised capabilities must be a subset of the old permitted
  196. * E: must be set to a subset of new permitted
  197. *
  198. * Returns 0 on success and < 0 on error.
  199. */
  200. SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)
  201. {
  202. struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
  203. unsigned i, tocopy, copybytes;
  204. kernel_cap_t inheritable, permitted, effective;
  205. struct cred *new;
  206. int ret;
  207. pid_t pid;
  208. ret = cap_validate_magic(header, &tocopy);
  209. if (ret != 0)
  210. return ret;
  211. if (get_user(pid, &header->pid))
  212. return -EFAULT;
  213. /* may only affect current now */
  214. if (pid != 0 && pid != task_pid_vnr(current))
  215. return -EPERM;
  216. copybytes = tocopy * sizeof(struct __user_cap_data_struct);
  217. if (copybytes > sizeof(kdata))
  218. return -EFAULT;
  219. if (copy_from_user(&kdata, data, copybytes))
  220. return -EFAULT;
  221. for (i = 0; i < tocopy; i++) {
  222. effective.cap[i] = kdata[i].effective;
  223. permitted.cap[i] = kdata[i].permitted;
  224. inheritable.cap[i] = kdata[i].inheritable;
  225. }
  226. while (i < _KERNEL_CAPABILITY_U32S) {
  227. effective.cap[i] = 0;
  228. permitted.cap[i] = 0;
  229. inheritable.cap[i] = 0;
  230. i++;
  231. }
  232. new = prepare_creds();
  233. if (!new)
  234. return -ENOMEM;
  235. ret = security_capset(new, current_cred(),
  236. &effective, &inheritable, &permitted);
  237. if (ret < 0)
  238. goto error;
  239. audit_log_capset(pid, new, current_cred());
  240. return commit_creds(new);
  241. error:
  242. abort_creds(new);
  243. return ret;
  244. }
  245. /**
  246. * has_capability - Does a task have a capability in init_user_ns
  247. * @t: The task in question
  248. * @cap: The capability to be tested for
  249. *
  250. * Return true if the specified task has the given superior capability
  251. * currently in effect to the initial user namespace, false if not.
  252. *
  253. * Note that this does not set PF_SUPERPRIV on the task.
  254. */
  255. bool has_capability(struct task_struct *t, int cap)
  256. {
  257. int ret = security_real_capable(t, &init_user_ns, cap);
  258. return (ret == 0);
  259. }
  260. /**
  261. * has_capability - Does a task have a capability in a specific user ns
  262. * @t: The task in question
  263. * @ns: target user namespace
  264. * @cap: The capability to be tested for
  265. *
  266. * Return true if the specified task has the given superior capability
  267. * currently in effect to the specified user namespace, false if not.
  268. *
  269. * Note that this does not set PF_SUPERPRIV on the task.
  270. */
  271. bool has_ns_capability(struct task_struct *t,
  272. struct user_namespace *ns, int cap)
  273. {
  274. int ret = security_real_capable(t, ns, cap);
  275. return (ret == 0);
  276. }
  277. /**
  278. * has_capability_noaudit - Does a task have a capability (unaudited)
  279. * @t: The task in question
  280. * @cap: The capability to be tested for
  281. *
  282. * Return true if the specified task has the given superior capability
  283. * currently in effect to init_user_ns, false if not. Don't write an
  284. * audit message for the check.
  285. *
  286. * Note that this does not set PF_SUPERPRIV on the task.
  287. */
  288. bool has_capability_noaudit(struct task_struct *t, int cap)
  289. {
  290. int ret = security_real_capable_noaudit(t, &init_user_ns, cap);
  291. return (ret == 0);
  292. }
  293. /**
  294. * capable - Determine if the current task has a superior capability in effect
  295. * @cap: The capability to be tested for
  296. *
  297. * Return true if the current task has the given superior capability currently
  298. * available for use, false if not.
  299. *
  300. * This sets PF_SUPERPRIV on the task if the capability is available on the
  301. * assumption that it's about to be used.
  302. */
  303. bool capable(int cap)
  304. {
  305. return ns_capable(&init_user_ns, cap);
  306. }
  307. EXPORT_SYMBOL(capable);
  308. /**
  309. * ns_capable - Determine if the current task has a superior capability in effect
  310. * @ns: The usernamespace we want the capability in
  311. * @cap: The capability to be tested for
  312. *
  313. * Return true if the current task has the given superior capability currently
  314. * available for use, false if not.
  315. *
  316. * This sets PF_SUPERPRIV on the task if the capability is available on the
  317. * assumption that it's about to be used.
  318. */
  319. bool ns_capable(struct user_namespace *ns, int cap)
  320. {
  321. if (unlikely(!cap_valid(cap))) {
  322. printk(KERN_CRIT "capable() called with invalid cap=%u\n", cap);
  323. BUG();
  324. }
  325. if (security_capable(ns, current_cred(), cap) == 0) {
  326. current->flags |= PF_SUPERPRIV;
  327. return true;
  328. }
  329. return false;
  330. }
  331. EXPORT_SYMBOL(ns_capable);
  332. /**
  333. * task_ns_capable - Determine whether current task has a superior
  334. * capability targeted at a specific task's user namespace.
  335. * @t: The task whose user namespace is targeted.
  336. * @cap: The capability in question.
  337. *
  338. * Return true if it does, false otherwise.
  339. */
  340. bool task_ns_capable(struct task_struct *t, int cap)
  341. {
  342. return ns_capable(task_cred_xxx(t, user)->user_ns, cap);
  343. }
  344. EXPORT_SYMBOL(task_ns_capable);
  345. /**
  346. * nsown_capable - Check superior capability to one's own user_ns
  347. * @cap: The capability in question
  348. *
  349. * Return true if the current task has the given superior capability
  350. * targeted at its own user namespace.
  351. */
  352. bool nsown_capable(int cap)
  353. {
  354. return ns_capable(current_user_ns(), cap);
  355. }