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