capability.c 10 KB

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