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. * unexpectidly 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. new = prepare_creds();
  224. if (!new)
  225. return -ENOMEM;
  226. ret = security_capset(new, current_cred(),
  227. &effective, &inheritable, &permitted);
  228. if (ret < 0)
  229. goto error;
  230. audit_log_capset(new, current_cred());
  231. return commit_creds(new);
  232. error:
  233. abort_creds(new);
  234. return ret;
  235. }
  236. /**
  237. * has_ns_capability - Does a task have a capability in a specific user ns
  238. * @t: The task in question
  239. * @ns: target user namespace
  240. * @cap: The capability to be tested for
  241. *
  242. * Return true if the specified task has the given superior capability
  243. * currently in effect to the specified user namespace, false if not.
  244. *
  245. * Note that this does not set PF_SUPERPRIV on the task.
  246. */
  247. bool has_ns_capability(struct task_struct *t,
  248. struct user_namespace *ns, int cap)
  249. {
  250. int ret;
  251. rcu_read_lock();
  252. ret = security_capable(__task_cred(t), ns, cap);
  253. rcu_read_unlock();
  254. return (ret == 0);
  255. }
  256. /**
  257. * has_capability - Does a task have a capability in init_user_ns
  258. * @t: The task in question
  259. * @cap: The capability to be tested for
  260. *
  261. * Return true if the specified task has the given superior capability
  262. * currently in effect to the initial user namespace, false if not.
  263. *
  264. * Note that this does not set PF_SUPERPRIV on the task.
  265. */
  266. bool has_capability(struct task_struct *t, int cap)
  267. {
  268. return has_ns_capability(t, &init_user_ns, cap);
  269. }
  270. /**
  271. * has_ns_capability_noaudit - Does a task have a capability (unaudited)
  272. * in a specific user ns.
  273. * @t: The task in question
  274. * @ns: target user namespace
  275. * @cap: The capability to be tested for
  276. *
  277. * Return true if the specified task has the given superior capability
  278. * currently in effect to the specified user namespace, false if not.
  279. * Do not write an audit message for the check.
  280. *
  281. * Note that this does not set PF_SUPERPRIV on the task.
  282. */
  283. bool has_ns_capability_noaudit(struct task_struct *t,
  284. struct user_namespace *ns, int cap)
  285. {
  286. int ret;
  287. rcu_read_lock();
  288. ret = security_capable_noaudit(__task_cred(t), ns, cap);
  289. rcu_read_unlock();
  290. return (ret == 0);
  291. }
  292. /**
  293. * has_capability_noaudit - Does a task have a capability (unaudited) in the
  294. * initial user ns
  295. * @t: The task in question
  296. * @cap: The capability to be tested for
  297. *
  298. * Return true if the specified task has the given superior capability
  299. * currently in effect to init_user_ns, false if not. Don't write an
  300. * audit message for the check.
  301. *
  302. * Note that this does not set PF_SUPERPRIV on the task.
  303. */
  304. bool has_capability_noaudit(struct task_struct *t, int cap)
  305. {
  306. return has_ns_capability_noaudit(t, &init_user_ns, cap);
  307. }
  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. pr_crit("capable() called with invalid cap=%u\n", cap);
  323. BUG();
  324. }
  325. if (security_capable(current_cred(), ns, cap) == 0) {
  326. current->flags |= PF_SUPERPRIV;
  327. return true;
  328. }
  329. return false;
  330. }
  331. EXPORT_SYMBOL(ns_capable);
  332. /**
  333. * file_ns_capable - Determine if the file's opener had a capability in effect
  334. * @file: The file we want to check
  335. * @ns: The usernamespace we want the capability in
  336. * @cap: The capability to be tested for
  337. *
  338. * Return true if task that opened the file had a capability in effect
  339. * when the file was opened.
  340. *
  341. * This does not set PF_SUPERPRIV because the caller may not
  342. * actually be privileged.
  343. */
  344. bool file_ns_capable(const struct file *file, struct user_namespace *ns, int cap)
  345. {
  346. if (WARN_ON_ONCE(!cap_valid(cap)))
  347. return false;
  348. if (security_capable(file->f_cred, ns, cap) == 0)
  349. return true;
  350. return false;
  351. }
  352. EXPORT_SYMBOL(file_ns_capable);
  353. /**
  354. * capable - Determine if the current task has a superior capability in effect
  355. * @cap: The capability to be tested for
  356. *
  357. * Return true if the current task has the given superior capability currently
  358. * available for use, false if not.
  359. *
  360. * This sets PF_SUPERPRIV on the task if the capability is available on the
  361. * assumption that it's about to be used.
  362. */
  363. bool capable(int cap)
  364. {
  365. return ns_capable(&init_user_ns, cap);
  366. }
  367. EXPORT_SYMBOL(capable);
  368. /**
  369. * inode_capable - Check superior capability over inode
  370. * @inode: The inode in question
  371. * @cap: The capability in question
  372. *
  373. * Return true if the current task has the given superior capability
  374. * targeted at it's own user namespace and that the given inode is owned
  375. * by the current user namespace or a child namespace.
  376. *
  377. * Currently we check to see if an inode is owned by the current
  378. * user namespace by seeing if the inode's owner maps into the
  379. * current user namespace.
  380. *
  381. */
  382. bool inode_capable(const struct inode *inode, int cap)
  383. {
  384. struct user_namespace *ns = current_user_ns();
  385. return ns_capable(ns, cap) && kuid_has_mapping(ns, inode->i_uid);
  386. }
  387. EXPORT_SYMBOL(inode_capable);