capability.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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/capability.h>
  10. #include <linux/mm.h>
  11. #include <linux/module.h>
  12. #include <linux/security.h>
  13. #include <linux/syscalls.h>
  14. #include <linux/pid_namespace.h>
  15. #include <asm/uaccess.h>
  16. /*
  17. * This lock protects task->cap_* for all tasks including current.
  18. * Locking rule: acquire this prior to tasklist_lock.
  19. */
  20. static DEFINE_SPINLOCK(task_capability_lock);
  21. /*
  22. * Leveraged for setting/resetting capabilities
  23. */
  24. const kernel_cap_t __cap_empty_set = CAP_EMPTY_SET;
  25. const kernel_cap_t __cap_full_set = CAP_FULL_SET;
  26. const kernel_cap_t __cap_init_eff_set = CAP_INIT_EFF_SET;
  27. EXPORT_SYMBOL(__cap_empty_set);
  28. EXPORT_SYMBOL(__cap_full_set);
  29. EXPORT_SYMBOL(__cap_init_eff_set);
  30. /*
  31. * More recent versions of libcap are available from:
  32. *
  33. * http://www.kernel.org/pub/linux/libs/security/linux-privs/
  34. */
  35. static void warn_legacy_capability_use(void)
  36. {
  37. static int warned;
  38. if (!warned) {
  39. char name[sizeof(current->comm)];
  40. printk(KERN_INFO "warning: `%s' uses 32-bit capabilities"
  41. " (legacy support in use)\n",
  42. get_task_comm(name, current));
  43. warned = 1;
  44. }
  45. }
  46. /*
  47. * Version 2 capabilities worked fine, but the linux/capability.h file
  48. * that accompanied their introduction encouraged their use without
  49. * the necessary user-space source code changes. As such, we have
  50. * created a version 3 with equivalent functionality to version 2, but
  51. * with a header change to protect legacy source code from using
  52. * version 2 when it wanted to use version 1. If your system has code
  53. * that trips the following warning, it is using version 2 specific
  54. * capabilities and may be doing so insecurely.
  55. *
  56. * The remedy is to either upgrade your version of libcap (to 2.10+,
  57. * if the application is linked against it), or recompile your
  58. * application with modern kernel headers and this warning will go
  59. * away.
  60. */
  61. static void warn_deprecated_v2(void)
  62. {
  63. static int warned;
  64. if (!warned) {
  65. char name[sizeof(current->comm)];
  66. printk(KERN_INFO "warning: `%s' uses deprecated v2"
  67. " capabilities in a way that may be insecure.\n",
  68. get_task_comm(name, current));
  69. warned = 1;
  70. }
  71. }
  72. /*
  73. * Version check. Return the number of u32s in each capability flag
  74. * array, or a negative value on error.
  75. */
  76. static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)
  77. {
  78. __u32 version;
  79. if (get_user(version, &header->version))
  80. return -EFAULT;
  81. switch (version) {
  82. case _LINUX_CAPABILITY_VERSION_1:
  83. warn_legacy_capability_use();
  84. *tocopy = _LINUX_CAPABILITY_U32S_1;
  85. break;
  86. case _LINUX_CAPABILITY_VERSION_2:
  87. warn_deprecated_v2();
  88. /*
  89. * fall through - v3 is otherwise equivalent to v2.
  90. */
  91. case _LINUX_CAPABILITY_VERSION_3:
  92. *tocopy = _LINUX_CAPABILITY_U32S_3;
  93. break;
  94. default:
  95. if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version))
  96. return -EFAULT;
  97. return -EINVAL;
  98. }
  99. return 0;
  100. }
  101. /*
  102. * For sys_getproccap() and sys_setproccap(), any of the three
  103. * capability set pointers may be NULL -- indicating that that set is
  104. * uninteresting and/or not to be changed.
  105. */
  106. /*
  107. * Atomically modify the effective capabilities returning the original
  108. * value. No permission check is performed here - it is assumed that the
  109. * caller is permitted to set the desired effective capabilities.
  110. */
  111. kernel_cap_t cap_set_effective(const kernel_cap_t pE_new)
  112. {
  113. kernel_cap_t pE_old;
  114. spin_lock(&task_capability_lock);
  115. pE_old = current->cap_effective;
  116. current->cap_effective = pE_new;
  117. spin_unlock(&task_capability_lock);
  118. return pE_old;
  119. }
  120. EXPORT_SYMBOL(cap_set_effective);
  121. /**
  122. * sys_capget - get the capabilities of a given process.
  123. * @header: pointer to struct that contains capability version and
  124. * target pid data
  125. * @dataptr: pointer to struct that contains the effective, permitted,
  126. * and inheritable capabilities that are returned
  127. *
  128. * Returns 0 on success and < 0 on error.
  129. */
  130. asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
  131. {
  132. int ret = 0;
  133. pid_t pid;
  134. struct task_struct *target;
  135. unsigned tocopy;
  136. kernel_cap_t pE, pI, pP;
  137. ret = cap_validate_magic(header, &tocopy);
  138. if (ret != 0)
  139. return ret;
  140. if (get_user(pid, &header->pid))
  141. return -EFAULT;
  142. if (pid < 0)
  143. return -EINVAL;
  144. spin_lock(&task_capability_lock);
  145. read_lock(&tasklist_lock);
  146. if (pid && pid != task_pid_vnr(current)) {
  147. target = find_task_by_vpid(pid);
  148. if (!target) {
  149. ret = -ESRCH;
  150. goto out;
  151. }
  152. } else
  153. target = current;
  154. ret = security_capget(target, &pE, &pI, &pP);
  155. out:
  156. read_unlock(&tasklist_lock);
  157. spin_unlock(&task_capability_lock);
  158. if (!ret) {
  159. struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
  160. unsigned i;
  161. for (i = 0; i < tocopy; i++) {
  162. kdata[i].effective = pE.cap[i];
  163. kdata[i].permitted = pP.cap[i];
  164. kdata[i].inheritable = pI.cap[i];
  165. }
  166. /*
  167. * Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,
  168. * we silently drop the upper capabilities here. This
  169. * has the effect of making older libcap
  170. * implementations implicitly drop upper capability
  171. * bits when they perform a: capget/modify/capset
  172. * sequence.
  173. *
  174. * This behavior is considered fail-safe
  175. * behavior. Upgrading the application to a newer
  176. * version of libcap will enable access to the newer
  177. * capabilities.
  178. *
  179. * An alternative would be to return an error here
  180. * (-ERANGE), but that causes legacy applications to
  181. * unexpectidly fail; the capget/modify/capset aborts
  182. * before modification is attempted and the application
  183. * fails.
  184. */
  185. if (copy_to_user(dataptr, kdata, tocopy
  186. * sizeof(struct __user_cap_data_struct))) {
  187. return -EFAULT;
  188. }
  189. }
  190. return ret;
  191. }
  192. /*
  193. * cap_set_pg - set capabilities for all processes in a given process
  194. * group. We call this holding task_capability_lock and tasklist_lock.
  195. */
  196. static inline int cap_set_pg(int pgrp_nr, kernel_cap_t *effective,
  197. kernel_cap_t *inheritable,
  198. kernel_cap_t *permitted)
  199. {
  200. struct task_struct *g, *target;
  201. int ret = -EPERM;
  202. int found = 0;
  203. struct pid *pgrp;
  204. pgrp = find_vpid(pgrp_nr);
  205. do_each_pid_task(pgrp, PIDTYPE_PGID, g) {
  206. target = g;
  207. while_each_thread(g, target) {
  208. if (!security_capset_check(target, effective,
  209. inheritable,
  210. permitted)) {
  211. security_capset_set(target, effective,
  212. inheritable,
  213. permitted);
  214. ret = 0;
  215. }
  216. found = 1;
  217. }
  218. } while_each_pid_task(pgrp, PIDTYPE_PGID, g);
  219. if (!found)
  220. ret = 0;
  221. return ret;
  222. }
  223. /*
  224. * cap_set_all - set capabilities for all processes other than init
  225. * and self. We call this holding task_capability_lock and tasklist_lock.
  226. */
  227. static inline int cap_set_all(kernel_cap_t *effective,
  228. kernel_cap_t *inheritable,
  229. kernel_cap_t *permitted)
  230. {
  231. struct task_struct *g, *target;
  232. int ret = -EPERM;
  233. int found = 0;
  234. do_each_thread(g, target) {
  235. if (target == current || is_container_init(target->group_leader))
  236. continue;
  237. found = 1;
  238. if (security_capset_check(target, effective, inheritable,
  239. permitted))
  240. continue;
  241. ret = 0;
  242. security_capset_set(target, effective, inheritable, permitted);
  243. } while_each_thread(g, target);
  244. if (!found)
  245. ret = 0;
  246. return ret;
  247. }
  248. /**
  249. * sys_capset - set capabilities for a process or a group of processes
  250. * @header: pointer to struct that contains capability version and
  251. * target pid data
  252. * @data: pointer to struct that contains the effective, permitted,
  253. * and inheritable capabilities
  254. *
  255. * Set capabilities for a given process, all processes, or all
  256. * processes in a given process group.
  257. *
  258. * The restrictions on setting capabilities are specified as:
  259. *
  260. * [pid is for the 'target' task. 'current' is the calling task.]
  261. *
  262. * I: any raised capabilities must be a subset of the (old current) permitted
  263. * P: any raised capabilities must be a subset of the (old current) permitted
  264. * E: must be set to a subset of (new target) permitted
  265. *
  266. * Returns 0 on success and < 0 on error.
  267. */
  268. asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
  269. {
  270. struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
  271. unsigned i, tocopy;
  272. kernel_cap_t inheritable, permitted, effective;
  273. struct task_struct *target;
  274. int ret;
  275. pid_t pid;
  276. ret = cap_validate_magic(header, &tocopy);
  277. if (ret != 0)
  278. return ret;
  279. if (get_user(pid, &header->pid))
  280. return -EFAULT;
  281. if (pid && pid != task_pid_vnr(current) && !capable(CAP_SETPCAP))
  282. return -EPERM;
  283. if (copy_from_user(&kdata, data, tocopy
  284. * sizeof(struct __user_cap_data_struct))) {
  285. return -EFAULT;
  286. }
  287. for (i = 0; i < tocopy; i++) {
  288. effective.cap[i] = kdata[i].effective;
  289. permitted.cap[i] = kdata[i].permitted;
  290. inheritable.cap[i] = kdata[i].inheritable;
  291. }
  292. while (i < _KERNEL_CAPABILITY_U32S) {
  293. effective.cap[i] = 0;
  294. permitted.cap[i] = 0;
  295. inheritable.cap[i] = 0;
  296. i++;
  297. }
  298. spin_lock(&task_capability_lock);
  299. read_lock(&tasklist_lock);
  300. if (pid > 0 && pid != task_pid_vnr(current)) {
  301. target = find_task_by_vpid(pid);
  302. if (!target) {
  303. ret = -ESRCH;
  304. goto out;
  305. }
  306. } else
  307. target = current;
  308. ret = 0;
  309. /* having verified that the proposed changes are legal,
  310. we now put them into effect. */
  311. if (pid < 0) {
  312. if (pid == -1) /* all procs other than current and init */
  313. ret = cap_set_all(&effective, &inheritable, &permitted);
  314. else /* all procs in process group */
  315. ret = cap_set_pg(-pid, &effective, &inheritable,
  316. &permitted);
  317. } else {
  318. ret = security_capset_check(target, &effective, &inheritable,
  319. &permitted);
  320. if (!ret)
  321. security_capset_set(target, &effective, &inheritable,
  322. &permitted);
  323. }
  324. out:
  325. read_unlock(&tasklist_lock);
  326. spin_unlock(&task_capability_lock);
  327. return ret;
  328. }
  329. int __capable(struct task_struct *t, int cap)
  330. {
  331. if (security_capable(t, cap) == 0) {
  332. t->flags |= PF_SUPERPRIV;
  333. return 1;
  334. }
  335. return 0;
  336. }
  337. int capable(int cap)
  338. {
  339. return __capable(current, cap);
  340. }
  341. EXPORT_SYMBOL(capable);