capability.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. #ifndef CONFIG_SECURITY_FILE_CAPABILITIES
  102. /*
  103. * Without filesystem capability support, we nominally support one process
  104. * setting the capabilities of another
  105. */
  106. static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
  107. kernel_cap_t *pIp, kernel_cap_t *pPp)
  108. {
  109. struct task_struct *target;
  110. int ret;
  111. spin_lock(&task_capability_lock);
  112. read_lock(&tasklist_lock);
  113. if (pid && pid != task_pid_vnr(current)) {
  114. target = find_task_by_vpid(pid);
  115. if (!target) {
  116. ret = -ESRCH;
  117. goto out;
  118. }
  119. } else
  120. target = current;
  121. ret = security_capget(target, pEp, pIp, pPp);
  122. out:
  123. read_unlock(&tasklist_lock);
  124. spin_unlock(&task_capability_lock);
  125. return ret;
  126. }
  127. /*
  128. * cap_set_pg - set capabilities for all processes in a given process
  129. * group. We call this holding task_capability_lock and tasklist_lock.
  130. */
  131. static inline int cap_set_pg(int pgrp_nr, kernel_cap_t *effective,
  132. kernel_cap_t *inheritable,
  133. kernel_cap_t *permitted)
  134. {
  135. struct task_struct *g, *target;
  136. int ret = -EPERM;
  137. int found = 0;
  138. struct pid *pgrp;
  139. spin_lock(&task_capability_lock);
  140. read_lock(&tasklist_lock);
  141. pgrp = find_vpid(pgrp_nr);
  142. do_each_pid_task(pgrp, PIDTYPE_PGID, g) {
  143. target = g;
  144. while_each_thread(g, target) {
  145. if (!security_capset_check(target, effective,
  146. inheritable, permitted)) {
  147. security_capset_set(target, effective,
  148. inheritable, permitted);
  149. ret = 0;
  150. }
  151. found = 1;
  152. }
  153. } while_each_pid_task(pgrp, PIDTYPE_PGID, g);
  154. read_unlock(&tasklist_lock);
  155. spin_unlock(&task_capability_lock);
  156. if (!found)
  157. ret = 0;
  158. return ret;
  159. }
  160. /*
  161. * cap_set_all - set capabilities for all processes other than init
  162. * and self. We call this holding task_capability_lock and tasklist_lock.
  163. */
  164. static inline int cap_set_all(kernel_cap_t *effective,
  165. kernel_cap_t *inheritable,
  166. kernel_cap_t *permitted)
  167. {
  168. struct task_struct *g, *target;
  169. int ret = -EPERM;
  170. int found = 0;
  171. spin_lock(&task_capability_lock);
  172. read_lock(&tasklist_lock);
  173. do_each_thread(g, target) {
  174. if (target == current
  175. || is_container_init(target->group_leader))
  176. continue;
  177. found = 1;
  178. if (security_capset_check(target, effective, inheritable,
  179. permitted))
  180. continue;
  181. ret = 0;
  182. security_capset_set(target, effective, inheritable, permitted);
  183. } while_each_thread(g, target);
  184. read_unlock(&tasklist_lock);
  185. spin_unlock(&task_capability_lock);
  186. if (!found)
  187. ret = 0;
  188. return ret;
  189. }
  190. /*
  191. * Given the target pid does not refer to the current process we
  192. * need more elaborate support... (This support is not present when
  193. * filesystem capabilities are configured.)
  194. */
  195. static inline int do_sys_capset_other_tasks(pid_t pid, kernel_cap_t *effective,
  196. kernel_cap_t *inheritable,
  197. kernel_cap_t *permitted)
  198. {
  199. struct task_struct *target;
  200. int ret;
  201. if (!capable(CAP_SETPCAP))
  202. return -EPERM;
  203. if (pid == -1) /* all procs other than current and init */
  204. return cap_set_all(effective, inheritable, permitted);
  205. else if (pid < 0) /* all procs in process group */
  206. return cap_set_pg(-pid, effective, inheritable, permitted);
  207. /* target != current */
  208. spin_lock(&task_capability_lock);
  209. read_lock(&tasklist_lock);
  210. target = find_task_by_vpid(pid);
  211. if (!target)
  212. ret = -ESRCH;
  213. else {
  214. ret = security_capset_check(target, effective, inheritable,
  215. permitted);
  216. /* having verified that the proposed changes are legal,
  217. we now put them into effect. */
  218. if (!ret)
  219. security_capset_set(target, effective, inheritable,
  220. permitted);
  221. }
  222. read_unlock(&tasklist_lock);
  223. spin_unlock(&task_capability_lock);
  224. return ret;
  225. }
  226. #else /* ie., def CONFIG_SECURITY_FILE_CAPABILITIES */
  227. /*
  228. * If we have configured with filesystem capability support, then the
  229. * only thing that can change the capabilities of the current process
  230. * is the current process. As such, we can't be in this code at the
  231. * same time as we are in the process of setting capabilities in this
  232. * process. The net result is that we can limit our use of locks to
  233. * when we are reading the caps of another process.
  234. */
  235. static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
  236. kernel_cap_t *pIp, kernel_cap_t *pPp)
  237. {
  238. int ret;
  239. if (pid && (pid != task_pid_vnr(current))) {
  240. struct task_struct *target;
  241. spin_lock(&task_capability_lock);
  242. read_lock(&tasklist_lock);
  243. target = find_task_by_vpid(pid);
  244. if (!target)
  245. ret = -ESRCH;
  246. else
  247. ret = security_capget(target, pEp, pIp, pPp);
  248. read_unlock(&tasklist_lock);
  249. spin_unlock(&task_capability_lock);
  250. } else
  251. ret = security_capget(current, pEp, pIp, pPp);
  252. return ret;
  253. }
  254. /*
  255. * With filesystem capability support configured, the kernel does not
  256. * permit the changing of capabilities in one process by another
  257. * process. (CAP_SETPCAP has much less broad semantics when configured
  258. * this way.)
  259. */
  260. static inline int do_sys_capset_other_tasks(pid_t pid,
  261. kernel_cap_t *effective,
  262. kernel_cap_t *inheritable,
  263. kernel_cap_t *permitted)
  264. {
  265. return -EPERM;
  266. }
  267. #endif /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */
  268. /*
  269. * Atomically modify the effective capabilities returning the original
  270. * value. No permission check is performed here - it is assumed that the
  271. * caller is permitted to set the desired effective capabilities.
  272. */
  273. kernel_cap_t cap_set_effective(const kernel_cap_t pE_new)
  274. {
  275. kernel_cap_t pE_old;
  276. spin_lock(&task_capability_lock);
  277. pE_old = current->cap_effective;
  278. current->cap_effective = pE_new;
  279. spin_unlock(&task_capability_lock);
  280. return pE_old;
  281. }
  282. EXPORT_SYMBOL(cap_set_effective);
  283. /**
  284. * sys_capget - get the capabilities of a given process.
  285. * @header: pointer to struct that contains capability version and
  286. * target pid data
  287. * @dataptr: pointer to struct that contains the effective, permitted,
  288. * and inheritable capabilities that are returned
  289. *
  290. * Returns 0 on success and < 0 on error.
  291. */
  292. asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
  293. {
  294. int ret = 0;
  295. pid_t pid;
  296. unsigned tocopy;
  297. kernel_cap_t pE, pI, pP;
  298. ret = cap_validate_magic(header, &tocopy);
  299. if (ret != 0)
  300. return ret;
  301. if (get_user(pid, &header->pid))
  302. return -EFAULT;
  303. if (pid < 0)
  304. return -EINVAL;
  305. ret = cap_get_target_pid(pid, &pE, &pI, &pP);
  306. if (!ret) {
  307. struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
  308. unsigned i;
  309. for (i = 0; i < tocopy; i++) {
  310. kdata[i].effective = pE.cap[i];
  311. kdata[i].permitted = pP.cap[i];
  312. kdata[i].inheritable = pI.cap[i];
  313. }
  314. /*
  315. * Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,
  316. * we silently drop the upper capabilities here. This
  317. * has the effect of making older libcap
  318. * implementations implicitly drop upper capability
  319. * bits when they perform a: capget/modify/capset
  320. * sequence.
  321. *
  322. * This behavior is considered fail-safe
  323. * behavior. Upgrading the application to a newer
  324. * version of libcap will enable access to the newer
  325. * capabilities.
  326. *
  327. * An alternative would be to return an error here
  328. * (-ERANGE), but that causes legacy applications to
  329. * unexpectidly fail; the capget/modify/capset aborts
  330. * before modification is attempted and the application
  331. * fails.
  332. */
  333. if (copy_to_user(dataptr, kdata, tocopy
  334. * sizeof(struct __user_cap_data_struct))) {
  335. return -EFAULT;
  336. }
  337. }
  338. return ret;
  339. }
  340. /**
  341. * sys_capset - set capabilities for a process or (*) a group of processes
  342. * @header: pointer to struct that contains capability version and
  343. * target pid data
  344. * @data: pointer to struct that contains the effective, permitted,
  345. * and inheritable capabilities
  346. *
  347. * Set capabilities for a given process, all processes, or all
  348. * processes in a given process group.
  349. *
  350. * The restrictions on setting capabilities are specified as:
  351. *
  352. * [pid is for the 'target' task. 'current' is the calling task.]
  353. *
  354. * I: any raised capabilities must be a subset of the (old current) permitted
  355. * P: any raised capabilities must be a subset of the (old current) permitted
  356. * E: must be set to a subset of (new target) permitted
  357. *
  358. * Returns 0 on success and < 0 on error.
  359. */
  360. asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
  361. {
  362. struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
  363. unsigned i, tocopy;
  364. kernel_cap_t inheritable, permitted, effective;
  365. int ret;
  366. pid_t pid;
  367. ret = cap_validate_magic(header, &tocopy);
  368. if (ret != 0)
  369. return ret;
  370. if (get_user(pid, &header->pid))
  371. return -EFAULT;
  372. if (copy_from_user(&kdata, data, tocopy
  373. * sizeof(struct __user_cap_data_struct))) {
  374. return -EFAULT;
  375. }
  376. for (i = 0; i < tocopy; i++) {
  377. effective.cap[i] = kdata[i].effective;
  378. permitted.cap[i] = kdata[i].permitted;
  379. inheritable.cap[i] = kdata[i].inheritable;
  380. }
  381. while (i < _KERNEL_CAPABILITY_U32S) {
  382. effective.cap[i] = 0;
  383. permitted.cap[i] = 0;
  384. inheritable.cap[i] = 0;
  385. i++;
  386. }
  387. if (pid && (pid != task_pid_vnr(current)))
  388. ret = do_sys_capset_other_tasks(pid, &effective, &inheritable,
  389. &permitted);
  390. else {
  391. /*
  392. * This lock is required even when filesystem
  393. * capability support is configured - it protects the
  394. * sys_capget() call from returning incorrect data in
  395. * the case that the targeted process is not the
  396. * current one.
  397. */
  398. spin_lock(&task_capability_lock);
  399. ret = security_capset_check(current, &effective, &inheritable,
  400. &permitted);
  401. /*
  402. * Having verified that the proposed changes are
  403. * legal, we now put them into effect.
  404. */
  405. if (!ret)
  406. security_capset_set(current, &effective, &inheritable,
  407. &permitted);
  408. spin_unlock(&task_capability_lock);
  409. }
  410. return ret;
  411. }
  412. /**
  413. * capable - Determine if the current task has a superior capability in effect
  414. * @cap: The capability to be tested for
  415. *
  416. * Return true if the current task has the given superior capability currently
  417. * available for use, false if not.
  418. *
  419. * This sets PF_SUPERPRIV on the task if the capability is available on the
  420. * assumption that it's about to be used.
  421. */
  422. int capable(int cap)
  423. {
  424. if (has_capability(current, cap)) {
  425. current->flags |= PF_SUPERPRIV;
  426. return 1;
  427. }
  428. return 0;
  429. }
  430. EXPORT_SYMBOL(capable);