capability.c 15 KB

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