capability.c 8.1 KB

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