capability.c 14 KB

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