capability.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 <asm/uaccess.h>
  17. #include "cred-internals.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. const kernel_cap_t __cap_init_eff_set = CAP_INIT_EFF_SET;
  24. EXPORT_SYMBOL(__cap_empty_set);
  25. EXPORT_SYMBOL(__cap_full_set);
  26. EXPORT_SYMBOL(__cap_init_eff_set);
  27. int file_caps_enabled = 1;
  28. static int __init file_caps_disable(char *str)
  29. {
  30. file_caps_enabled = 0;
  31. return 1;
  32. }
  33. __setup("no_file_caps", file_caps_disable);
  34. /*
  35. * More recent versions of libcap are available from:
  36. *
  37. * http://www.kernel.org/pub/linux/libs/security/linux-privs/
  38. */
  39. static void warn_legacy_capability_use(void)
  40. {
  41. static int warned;
  42. if (!warned) {
  43. char name[sizeof(current->comm)];
  44. printk(KERN_INFO "warning: `%s' uses 32-bit capabilities"
  45. " (legacy support in use)\n",
  46. get_task_comm(name, current));
  47. warned = 1;
  48. }
  49. }
  50. /*
  51. * Version 2 capabilities worked fine, but the linux/capability.h file
  52. * that accompanied their introduction encouraged their use without
  53. * the necessary user-space source code changes. As such, we have
  54. * created a version 3 with equivalent functionality to version 2, but
  55. * with a header change to protect legacy source code from using
  56. * version 2 when it wanted to use version 1. If your system has code
  57. * that trips the following warning, it is using version 2 specific
  58. * capabilities and may be doing so insecurely.
  59. *
  60. * The remedy is to either upgrade your version of libcap (to 2.10+,
  61. * if the application is linked against it), or recompile your
  62. * application with modern kernel headers and this warning will go
  63. * away.
  64. */
  65. static void warn_deprecated_v2(void)
  66. {
  67. static int warned;
  68. if (!warned) {
  69. char name[sizeof(current->comm)];
  70. printk(KERN_INFO "warning: `%s' uses deprecated v2"
  71. " capabilities in a way that may be insecure.\n",
  72. get_task_comm(name, current));
  73. warned = 1;
  74. }
  75. }
  76. /*
  77. * Version check. Return the number of u32s in each capability flag
  78. * array, or a negative value on error.
  79. */
  80. static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)
  81. {
  82. __u32 version;
  83. if (get_user(version, &header->version))
  84. return -EFAULT;
  85. switch (version) {
  86. case _LINUX_CAPABILITY_VERSION_1:
  87. warn_legacy_capability_use();
  88. *tocopy = _LINUX_CAPABILITY_U32S_1;
  89. break;
  90. case _LINUX_CAPABILITY_VERSION_2:
  91. warn_deprecated_v2();
  92. /*
  93. * fall through - v3 is otherwise equivalent to v2.
  94. */
  95. case _LINUX_CAPABILITY_VERSION_3:
  96. *tocopy = _LINUX_CAPABILITY_U32S_3;
  97. break;
  98. default:
  99. if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version))
  100. return -EFAULT;
  101. return -EINVAL;
  102. }
  103. return 0;
  104. }
  105. /*
  106. * The only thing that can change the capabilities of the current
  107. * process is the current process. As such, we can't be in this code
  108. * at the same time as we are in the process of setting capabilities
  109. * in this process. The net result is that we can limit our use of
  110. * locks to when we are reading the caps of another process.
  111. */
  112. static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
  113. kernel_cap_t *pIp, kernel_cap_t *pPp)
  114. {
  115. int ret;
  116. if (pid && (pid != task_pid_vnr(current))) {
  117. struct task_struct *target;
  118. rcu_read_lock();
  119. target = find_task_by_vpid(pid);
  120. if (!target)
  121. ret = -ESRCH;
  122. else
  123. ret = security_capget(target, pEp, pIp, pPp);
  124. rcu_read_unlock();
  125. } else
  126. ret = security_capget(current, pEp, pIp, pPp);
  127. return ret;
  128. }
  129. /**
  130. * sys_capget - get the capabilities of a given process.
  131. * @header: pointer to struct that contains capability version and
  132. * target pid data
  133. * @dataptr: pointer to struct that contains the effective, permitted,
  134. * and inheritable capabilities that are returned
  135. *
  136. * Returns 0 on success and < 0 on error.
  137. */
  138. SYSCALL_DEFINE2(capget, cap_user_header_t, header, cap_user_data_t, dataptr)
  139. {
  140. int ret = 0;
  141. pid_t pid;
  142. unsigned tocopy;
  143. kernel_cap_t pE, pI, pP;
  144. ret = cap_validate_magic(header, &tocopy);
  145. if ((dataptr == NULL) || (ret != 0))
  146. return ((dataptr == NULL) && (ret == -EINVAL)) ? 0 : ret;
  147. if (get_user(pid, &header->pid))
  148. return -EFAULT;
  149. if (pid < 0)
  150. return -EINVAL;
  151. ret = cap_get_target_pid(pid, &pE, &pI, &pP);
  152. if (!ret) {
  153. struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
  154. unsigned i;
  155. for (i = 0; i < tocopy; i++) {
  156. kdata[i].effective = pE.cap[i];
  157. kdata[i].permitted = pP.cap[i];
  158. kdata[i].inheritable = pI.cap[i];
  159. }
  160. /*
  161. * Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,
  162. * we silently drop the upper capabilities here. This
  163. * has the effect of making older libcap
  164. * implementations implicitly drop upper capability
  165. * bits when they perform a: capget/modify/capset
  166. * sequence.
  167. *
  168. * This behavior is considered fail-safe
  169. * behavior. Upgrading the application to a newer
  170. * version of libcap will enable access to the newer
  171. * capabilities.
  172. *
  173. * An alternative would be to return an error here
  174. * (-ERANGE), but that causes legacy applications to
  175. * unexpectidly fail; the capget/modify/capset aborts
  176. * before modification is attempted and the application
  177. * fails.
  178. */
  179. if (copy_to_user(dataptr, kdata, tocopy
  180. * sizeof(struct __user_cap_data_struct))) {
  181. return -EFAULT;
  182. }
  183. }
  184. return ret;
  185. }
  186. /**
  187. * sys_capset - set capabilities for a process or (*) a group of processes
  188. * @header: pointer to struct that contains capability version and
  189. * target pid data
  190. * @data: pointer to struct that contains the effective, permitted,
  191. * and inheritable capabilities
  192. *
  193. * Set capabilities for the current process only. The ability to any other
  194. * process(es) has been deprecated and removed.
  195. *
  196. * The restrictions on setting capabilities are specified as:
  197. *
  198. * I: any raised capabilities must be a subset of the old permitted
  199. * P: any raised capabilities must be a subset of the old permitted
  200. * E: must be set to a subset of new permitted
  201. *
  202. * Returns 0 on success and < 0 on error.
  203. */
  204. SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)
  205. {
  206. struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
  207. unsigned i, tocopy, copybytes;
  208. kernel_cap_t inheritable, permitted, effective;
  209. struct cred *new;
  210. int ret;
  211. pid_t pid;
  212. ret = cap_validate_magic(header, &tocopy);
  213. if (ret != 0)
  214. return ret;
  215. if (get_user(pid, &header->pid))
  216. return -EFAULT;
  217. /* may only affect current now */
  218. if (pid != 0 && pid != task_pid_vnr(current))
  219. return -EPERM;
  220. copybytes = tocopy * sizeof(struct __user_cap_data_struct);
  221. if (copybytes > sizeof(kdata))
  222. return -EFAULT;
  223. if (copy_from_user(&kdata, data, copybytes))
  224. return -EFAULT;
  225. for (i = 0; i < tocopy; i++) {
  226. effective.cap[i] = kdata[i].effective;
  227. permitted.cap[i] = kdata[i].permitted;
  228. inheritable.cap[i] = kdata[i].inheritable;
  229. }
  230. while (i < _KERNEL_CAPABILITY_U32S) {
  231. effective.cap[i] = 0;
  232. permitted.cap[i] = 0;
  233. inheritable.cap[i] = 0;
  234. i++;
  235. }
  236. new = prepare_creds();
  237. if (!new)
  238. return -ENOMEM;
  239. ret = security_capset(new, current_cred(),
  240. &effective, &inheritable, &permitted);
  241. if (ret < 0)
  242. goto error;
  243. audit_log_capset(pid, new, current_cred());
  244. return commit_creds(new);
  245. error:
  246. abort_creds(new);
  247. return ret;
  248. }
  249. /**
  250. * capable - Determine if the current task has a superior capability in effect
  251. * @cap: The capability to be tested for
  252. *
  253. * Return true if the current task has the given superior capability currently
  254. * available for use, false if not.
  255. *
  256. * This sets PF_SUPERPRIV on the task if the capability is available on the
  257. * assumption that it's about to be used.
  258. */
  259. int capable(int cap)
  260. {
  261. if (unlikely(!cap_valid(cap))) {
  262. printk(KERN_CRIT "capable() called with invalid cap=%u\n", cap);
  263. BUG();
  264. }
  265. if (security_capable(cap) == 0) {
  266. current->flags |= PF_SUPERPRIV;
  267. return 1;
  268. }
  269. return 0;
  270. }
  271. EXPORT_SYMBOL(capable);