capability.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. * sys_capget - get the capabilities of a given process.
  108. * @header: pointer to struct that contains capability version and
  109. * target pid data
  110. * @dataptr: pointer to struct that contains the effective, permitted,
  111. * and inheritable capabilities that are returned
  112. *
  113. * Returns 0 on success and < 0 on error.
  114. */
  115. asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
  116. {
  117. int ret = 0;
  118. pid_t pid;
  119. struct task_struct *target;
  120. unsigned tocopy;
  121. kernel_cap_t pE, pI, pP;
  122. ret = cap_validate_magic(header, &tocopy);
  123. if (ret != 0)
  124. return ret;
  125. if (get_user(pid, &header->pid))
  126. return -EFAULT;
  127. if (pid < 0)
  128. return -EINVAL;
  129. spin_lock(&task_capability_lock);
  130. read_lock(&tasklist_lock);
  131. if (pid && pid != task_pid_vnr(current)) {
  132. target = find_task_by_vpid(pid);
  133. if (!target) {
  134. ret = -ESRCH;
  135. goto out;
  136. }
  137. } else
  138. target = current;
  139. ret = security_capget(target, &pE, &pI, &pP);
  140. out:
  141. read_unlock(&tasklist_lock);
  142. spin_unlock(&task_capability_lock);
  143. if (!ret) {
  144. struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
  145. unsigned i;
  146. for (i = 0; i < tocopy; i++) {
  147. kdata[i].effective = pE.cap[i];
  148. kdata[i].permitted = pP.cap[i];
  149. kdata[i].inheritable = pI.cap[i];
  150. }
  151. /*
  152. * Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,
  153. * we silently drop the upper capabilities here. This
  154. * has the effect of making older libcap
  155. * implementations implicitly drop upper capability
  156. * bits when they perform a: capget/modify/capset
  157. * sequence.
  158. *
  159. * This behavior is considered fail-safe
  160. * behavior. Upgrading the application to a newer
  161. * version of libcap will enable access to the newer
  162. * capabilities.
  163. *
  164. * An alternative would be to return an error here
  165. * (-ERANGE), but that causes legacy applications to
  166. * unexpectidly fail; the capget/modify/capset aborts
  167. * before modification is attempted and the application
  168. * fails.
  169. */
  170. if (copy_to_user(dataptr, kdata, tocopy
  171. * sizeof(struct __user_cap_data_struct))) {
  172. return -EFAULT;
  173. }
  174. }
  175. return ret;
  176. }
  177. /*
  178. * cap_set_pg - set capabilities for all processes in a given process
  179. * group. We call this holding task_capability_lock and tasklist_lock.
  180. */
  181. static inline int cap_set_pg(int pgrp_nr, kernel_cap_t *effective,
  182. kernel_cap_t *inheritable,
  183. kernel_cap_t *permitted)
  184. {
  185. struct task_struct *g, *target;
  186. int ret = -EPERM;
  187. int found = 0;
  188. struct pid *pgrp;
  189. pgrp = find_vpid(pgrp_nr);
  190. do_each_pid_task(pgrp, PIDTYPE_PGID, g) {
  191. target = g;
  192. while_each_thread(g, target) {
  193. if (!security_capset_check(target, effective,
  194. inheritable,
  195. permitted)) {
  196. security_capset_set(target, effective,
  197. inheritable,
  198. permitted);
  199. ret = 0;
  200. }
  201. found = 1;
  202. }
  203. } while_each_pid_task(pgrp, PIDTYPE_PGID, g);
  204. if (!found)
  205. ret = 0;
  206. return ret;
  207. }
  208. /*
  209. * cap_set_all - set capabilities for all processes other than init
  210. * and self. We call this holding task_capability_lock and tasklist_lock.
  211. */
  212. static inline int cap_set_all(kernel_cap_t *effective,
  213. kernel_cap_t *inheritable,
  214. kernel_cap_t *permitted)
  215. {
  216. struct task_struct *g, *target;
  217. int ret = -EPERM;
  218. int found = 0;
  219. do_each_thread(g, target) {
  220. if (target == current || is_container_init(target->group_leader))
  221. continue;
  222. found = 1;
  223. if (security_capset_check(target, effective, inheritable,
  224. permitted))
  225. continue;
  226. ret = 0;
  227. security_capset_set(target, effective, inheritable, permitted);
  228. } while_each_thread(g, target);
  229. if (!found)
  230. ret = 0;
  231. return ret;
  232. }
  233. /**
  234. * sys_capset - set capabilities for a process or a group of processes
  235. * @header: pointer to struct that contains capability version and
  236. * target pid data
  237. * @data: pointer to struct that contains the effective, permitted,
  238. * and inheritable capabilities
  239. *
  240. * Set capabilities for a given process, all processes, or all
  241. * processes in a given process group.
  242. *
  243. * The restrictions on setting capabilities are specified as:
  244. *
  245. * [pid is for the 'target' task. 'current' is the calling task.]
  246. *
  247. * I: any raised capabilities must be a subset of the (old current) permitted
  248. * P: any raised capabilities must be a subset of the (old current) permitted
  249. * E: must be set to a subset of (new target) permitted
  250. *
  251. * Returns 0 on success and < 0 on error.
  252. */
  253. asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
  254. {
  255. struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
  256. unsigned i, tocopy;
  257. kernel_cap_t inheritable, permitted, effective;
  258. struct task_struct *target;
  259. int ret;
  260. pid_t pid;
  261. ret = cap_validate_magic(header, &tocopy);
  262. if (ret != 0)
  263. return ret;
  264. if (get_user(pid, &header->pid))
  265. return -EFAULT;
  266. if (pid && pid != task_pid_vnr(current) && !capable(CAP_SETPCAP))
  267. return -EPERM;
  268. if (copy_from_user(&kdata, data, tocopy
  269. * sizeof(struct __user_cap_data_struct))) {
  270. return -EFAULT;
  271. }
  272. for (i = 0; i < tocopy; i++) {
  273. effective.cap[i] = kdata[i].effective;
  274. permitted.cap[i] = kdata[i].permitted;
  275. inheritable.cap[i] = kdata[i].inheritable;
  276. }
  277. while (i < _KERNEL_CAPABILITY_U32S) {
  278. effective.cap[i] = 0;
  279. permitted.cap[i] = 0;
  280. inheritable.cap[i] = 0;
  281. i++;
  282. }
  283. spin_lock(&task_capability_lock);
  284. read_lock(&tasklist_lock);
  285. if (pid > 0 && pid != task_pid_vnr(current)) {
  286. target = find_task_by_vpid(pid);
  287. if (!target) {
  288. ret = -ESRCH;
  289. goto out;
  290. }
  291. } else
  292. target = current;
  293. ret = 0;
  294. /* having verified that the proposed changes are legal,
  295. we now put them into effect. */
  296. if (pid < 0) {
  297. if (pid == -1) /* all procs other than current and init */
  298. ret = cap_set_all(&effective, &inheritable, &permitted);
  299. else /* all procs in process group */
  300. ret = cap_set_pg(-pid, &effective, &inheritable,
  301. &permitted);
  302. } else {
  303. ret = security_capset_check(target, &effective, &inheritable,
  304. &permitted);
  305. if (!ret)
  306. security_capset_set(target, &effective, &inheritable,
  307. &permitted);
  308. }
  309. out:
  310. read_unlock(&tasklist_lock);
  311. spin_unlock(&task_capability_lock);
  312. return ret;
  313. }
  314. int __capable(struct task_struct *t, int cap)
  315. {
  316. if (security_capable(t, cap) == 0) {
  317. t->flags |= PF_SUPERPRIV;
  318. return 1;
  319. }
  320. return 0;
  321. }
  322. int capable(int cap)
  323. {
  324. return __capable(current, cap);
  325. }
  326. EXPORT_SYMBOL(capable);