capability.c 12 KB

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