capability.c 9.1 KB

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