capability.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. fallthrough; /* v3 is otherwise equivalent to v2 */
  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, CAP_OPT_NONE);
  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. EXPORT_SYMBOL(has_capability);
  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(__task_cred(t), ns, cap, CAP_OPT_NOAUDIT);
  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. static bool ns_capable_common(struct user_namespace *ns,
  313. int cap,
  314. unsigned int opts)
  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 = security_capable(current_cred(), ns, cap, opts);
  322. if (capable == 0) {
  323. current->flags |= PF_SUPERPRIV;
  324. return true;
  325. }
  326. return false;
  327. }
  328. /**
  329. * ns_capable - Determine if the current task has a superior capability in effect
  330. * @ns: The usernamespace we want the capability in
  331. * @cap: The capability to be tested for
  332. *
  333. * Return true if the current task has the given superior capability currently
  334. * available for use, false if not.
  335. *
  336. * This sets PF_SUPERPRIV on the task if the capability is available on the
  337. * assumption that it's about to be used.
  338. */
  339. bool ns_capable(struct user_namespace *ns, int cap)
  340. {
  341. return ns_capable_common(ns, cap, CAP_OPT_NONE);
  342. }
  343. EXPORT_SYMBOL(ns_capable);
  344. /**
  345. * ns_capable_noaudit - Determine if the current task has a superior capability
  346. * (unaudited) in effect
  347. * @ns: The usernamespace we want the capability in
  348. * @cap: The capability to be tested for
  349. *
  350. * Return true if the current task has the given superior capability currently
  351. * available for use, false if not.
  352. *
  353. * This sets PF_SUPERPRIV on the task if the capability is available on the
  354. * assumption that it's about to be used.
  355. */
  356. bool ns_capable_noaudit(struct user_namespace *ns, int cap)
  357. {
  358. return ns_capable_common(ns, cap, CAP_OPT_NOAUDIT);
  359. }
  360. EXPORT_SYMBOL(ns_capable_noaudit);
  361. /**
  362. * ns_capable_setid - Determine if the current task has a superior capability
  363. * in effect, while signalling that this check is being done from within a
  364. * setid or setgroups syscall.
  365. * @ns: The usernamespace we want the capability in
  366. * @cap: The capability to be tested for
  367. *
  368. * Return true if the current task has the given superior capability currently
  369. * available for use, false if not.
  370. *
  371. * This sets PF_SUPERPRIV on the task if the capability is available on the
  372. * assumption that it's about to be used.
  373. */
  374. bool ns_capable_setid(struct user_namespace *ns, int cap)
  375. {
  376. return ns_capable_common(ns, cap, CAP_OPT_INSETID);
  377. }
  378. EXPORT_SYMBOL(ns_capable_setid);
  379. /**
  380. * capable - Determine if the current task has a superior capability in effect
  381. * @cap: The capability to be tested for
  382. *
  383. * Return true if the current task has the given superior capability currently
  384. * available for use, false if not.
  385. *
  386. * This sets PF_SUPERPRIV on the task if the capability is available on the
  387. * assumption that it's about to be used.
  388. */
  389. bool capable(int cap)
  390. {
  391. return ns_capable(&init_user_ns, cap);
  392. }
  393. EXPORT_SYMBOL(capable);
  394. #endif /* CONFIG_MULTIUSER */
  395. /**
  396. * file_ns_capable - Determine if the file's opener had a capability in effect
  397. * @file: The file we want to check
  398. * @ns: The usernamespace we want the capability in
  399. * @cap: The capability to be tested for
  400. *
  401. * Return true if task that opened the file had a capability in effect
  402. * when the file was opened.
  403. *
  404. * This does not set PF_SUPERPRIV because the caller may not
  405. * actually be privileged.
  406. */
  407. bool file_ns_capable(const struct file *file, struct user_namespace *ns,
  408. int cap)
  409. {
  410. if (WARN_ON_ONCE(!cap_valid(cap)))
  411. return false;
  412. if (security_capable(file->f_cred, ns, cap, CAP_OPT_NONE) == 0)
  413. return true;
  414. return false;
  415. }
  416. EXPORT_SYMBOL(file_ns_capable);
  417. /**
  418. * privileged_wrt_inode_uidgid - Do capabilities in the namespace work over the inode?
  419. * @ns: The user namespace in question
  420. * @inode: The inode in question
  421. *
  422. * Return true if the inode uid and gid are within the namespace.
  423. */
  424. bool privileged_wrt_inode_uidgid(struct user_namespace *ns, const struct inode *inode)
  425. {
  426. return kuid_has_mapping(ns, inode->i_uid) &&
  427. kgid_has_mapping(ns, inode->i_gid);
  428. }
  429. /**
  430. * capable_wrt_inode_uidgid - Check nsown_capable and uid and gid mapped
  431. * @inode: The inode in question
  432. * @cap: The capability in question
  433. *
  434. * Return true if the current task has the given capability targeted at
  435. * its own user namespace and that the given inode's uid and gid are
  436. * mapped into the current user namespace.
  437. */
  438. bool capable_wrt_inode_uidgid(const struct inode *inode, int cap)
  439. {
  440. struct user_namespace *ns = current_user_ns();
  441. return ns_capable(ns, cap) && privileged_wrt_inode_uidgid(ns, inode);
  442. }
  443. EXPORT_SYMBOL(capable_wrt_inode_uidgid);
  444. /**
  445. * ptracer_capable - Determine if the ptracer holds CAP_SYS_PTRACE in the namespace
  446. * @tsk: The task that may be ptraced
  447. * @ns: The user namespace to search for CAP_SYS_PTRACE in
  448. *
  449. * Return true if the task that is ptracing the current task had CAP_SYS_PTRACE
  450. * in the specified user namespace.
  451. */
  452. bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns)
  453. {
  454. int ret = 0; /* An absent tracer adds no restrictions */
  455. const struct cred *cred;
  456. rcu_read_lock();
  457. cred = rcu_dereference(tsk->ptracer_cred);
  458. if (cred)
  459. ret = security_capable(cred, ns, CAP_SYS_PTRACE,
  460. CAP_OPT_NOAUDIT);
  461. rcu_read_unlock();
  462. return (ret == 0);
  463. }