capability.c 12 KB

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