capability.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  31. int file_caps_enabled = 1;
  32. static int __init file_caps_disable(char *str)
  33. {
  34. file_caps_enabled = 0;
  35. return 1;
  36. }
  37. __setup("no_file_caps", file_caps_disable);
  38. #endif
  39. /*
  40. * More recent versions of libcap are available from:
  41. *
  42. * http://www.kernel.org/pub/linux/libs/security/linux-privs/
  43. */
  44. static void warn_legacy_capability_use(void)
  45. {
  46. static int warned;
  47. if (!warned) {
  48. char name[sizeof(current->comm)];
  49. printk(KERN_INFO "warning: `%s' uses 32-bit capabilities"
  50. " (legacy support in use)\n",
  51. get_task_comm(name, current));
  52. warned = 1;
  53. }
  54. }
  55. /*
  56. * Version 2 capabilities worked fine, but the linux/capability.h file
  57. * that accompanied their introduction encouraged their use without
  58. * the necessary user-space source code changes. As such, we have
  59. * created a version 3 with equivalent functionality to version 2, but
  60. * with a header change to protect legacy source code from using
  61. * version 2 when it wanted to use version 1. If your system has code
  62. * that trips the following warning, it is using version 2 specific
  63. * capabilities and may be doing so insecurely.
  64. *
  65. * The remedy is to either upgrade your version of libcap (to 2.10+,
  66. * if the application is linked against it), or recompile your
  67. * application with modern kernel headers and this warning will go
  68. * away.
  69. */
  70. static void warn_deprecated_v2(void)
  71. {
  72. static int warned;
  73. if (!warned) {
  74. char name[sizeof(current->comm)];
  75. printk(KERN_INFO "warning: `%s' uses deprecated v2"
  76. " capabilities in a way that may be insecure.\n",
  77. get_task_comm(name, current));
  78. warned = 1;
  79. }
  80. }
  81. /*
  82. * Version check. Return the number of u32s in each capability flag
  83. * array, or a negative value on error.
  84. */
  85. static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)
  86. {
  87. __u32 version;
  88. if (get_user(version, &header->version))
  89. return -EFAULT;
  90. switch (version) {
  91. case _LINUX_CAPABILITY_VERSION_1:
  92. warn_legacy_capability_use();
  93. *tocopy = _LINUX_CAPABILITY_U32S_1;
  94. break;
  95. case _LINUX_CAPABILITY_VERSION_2:
  96. warn_deprecated_v2();
  97. /*
  98. * fall through - v3 is otherwise equivalent to v2.
  99. */
  100. case _LINUX_CAPABILITY_VERSION_3:
  101. *tocopy = _LINUX_CAPABILITY_U32S_3;
  102. break;
  103. default:
  104. if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version))
  105. return -EFAULT;
  106. return -EINVAL;
  107. }
  108. return 0;
  109. }
  110. #ifndef CONFIG_SECURITY_FILE_CAPABILITIES
  111. /*
  112. * Without filesystem capability support, we nominally support one process
  113. * setting the capabilities of another
  114. */
  115. static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
  116. kernel_cap_t *pIp, kernel_cap_t *pPp)
  117. {
  118. struct task_struct *target;
  119. int ret;
  120. spin_lock(&task_capability_lock);
  121. read_lock(&tasklist_lock);
  122. if (pid && pid != task_pid_vnr(current)) {
  123. target = find_task_by_vpid(pid);
  124. if (!target) {
  125. ret = -ESRCH;
  126. goto out;
  127. }
  128. } else
  129. target = current;
  130. ret = security_capget(target, pEp, pIp, pPp);
  131. out:
  132. read_unlock(&tasklist_lock);
  133. spin_unlock(&task_capability_lock);
  134. return ret;
  135. }
  136. /*
  137. * cap_set_pg - set capabilities for all processes in a given process
  138. * group. We call this holding task_capability_lock and tasklist_lock.
  139. */
  140. static inline int cap_set_pg(int pgrp_nr, kernel_cap_t *effective,
  141. kernel_cap_t *inheritable,
  142. kernel_cap_t *permitted)
  143. {
  144. struct task_struct *g, *target;
  145. int ret = -EPERM;
  146. int found = 0;
  147. struct pid *pgrp;
  148. spin_lock(&task_capability_lock);
  149. read_lock(&tasklist_lock);
  150. pgrp = find_vpid(pgrp_nr);
  151. do_each_pid_task(pgrp, PIDTYPE_PGID, g) {
  152. target = g;
  153. while_each_thread(g, target) {
  154. if (!security_capset_check(target, effective,
  155. inheritable, permitted)) {
  156. security_capset_set(target, effective,
  157. inheritable, permitted);
  158. ret = 0;
  159. }
  160. found = 1;
  161. }
  162. } while_each_pid_task(pgrp, PIDTYPE_PGID, g);
  163. read_unlock(&tasklist_lock);
  164. spin_unlock(&task_capability_lock);
  165. if (!found)
  166. ret = 0;
  167. return ret;
  168. }
  169. /*
  170. * cap_set_all - set capabilities for all processes other than init
  171. * and self. We call this holding task_capability_lock and tasklist_lock.
  172. */
  173. static inline int cap_set_all(kernel_cap_t *effective,
  174. kernel_cap_t *inheritable,
  175. kernel_cap_t *permitted)
  176. {
  177. struct task_struct *g, *target;
  178. int ret = -EPERM;
  179. int found = 0;
  180. spin_lock(&task_capability_lock);
  181. read_lock(&tasklist_lock);
  182. do_each_thread(g, target) {
  183. if (target == current
  184. || is_container_init(target->group_leader))
  185. continue;
  186. found = 1;
  187. if (security_capset_check(target, effective, inheritable,
  188. permitted))
  189. continue;
  190. ret = 0;
  191. security_capset_set(target, effective, inheritable, permitted);
  192. } while_each_thread(g, target);
  193. read_unlock(&tasklist_lock);
  194. spin_unlock(&task_capability_lock);
  195. if (!found)
  196. ret = 0;
  197. return ret;
  198. }
  199. /*
  200. * Given the target pid does not refer to the current process we
  201. * need more elaborate support... (This support is not present when
  202. * filesystem capabilities are configured.)
  203. */
  204. static inline int do_sys_capset_other_tasks(pid_t pid, kernel_cap_t *effective,
  205. kernel_cap_t *inheritable,
  206. kernel_cap_t *permitted)
  207. {
  208. struct task_struct *target;
  209. int ret;
  210. if (!capable(CAP_SETPCAP))
  211. return -EPERM;
  212. if (pid == -1) /* all procs other than current and init */
  213. return cap_set_all(effective, inheritable, permitted);
  214. else if (pid < 0) /* all procs in process group */
  215. return cap_set_pg(-pid, effective, inheritable, permitted);
  216. /* target != current */
  217. spin_lock(&task_capability_lock);
  218. read_lock(&tasklist_lock);
  219. target = find_task_by_vpid(pid);
  220. if (!target)
  221. ret = -ESRCH;
  222. else {
  223. ret = security_capset_check(target, effective, inheritable,
  224. permitted);
  225. /* having verified that the proposed changes are legal,
  226. we now put them into effect. */
  227. if (!ret)
  228. security_capset_set(target, effective, inheritable,
  229. permitted);
  230. }
  231. read_unlock(&tasklist_lock);
  232. spin_unlock(&task_capability_lock);
  233. return ret;
  234. }
  235. #else /* ie., def CONFIG_SECURITY_FILE_CAPABILITIES */
  236. /*
  237. * If we have configured with filesystem capability support, then the
  238. * only thing that can change the capabilities of the current process
  239. * is the current process. As such, we can't be in this code at the
  240. * same time as we are in the process of setting capabilities in this
  241. * process. The net result is that we can limit our use of locks to
  242. * when we are reading the caps of another process.
  243. */
  244. static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
  245. kernel_cap_t *pIp, kernel_cap_t *pPp)
  246. {
  247. int ret;
  248. if (pid && (pid != task_pid_vnr(current))) {
  249. struct task_struct *target;
  250. spin_lock(&task_capability_lock);
  251. read_lock(&tasklist_lock);
  252. target = find_task_by_vpid(pid);
  253. if (!target)
  254. ret = -ESRCH;
  255. else
  256. ret = security_capget(target, pEp, pIp, pPp);
  257. read_unlock(&tasklist_lock);
  258. spin_unlock(&task_capability_lock);
  259. } else
  260. ret = security_capget(current, pEp, pIp, pPp);
  261. return ret;
  262. }
  263. /*
  264. * With filesystem capability support configured, the kernel does not
  265. * permit the changing of capabilities in one process by another
  266. * process. (CAP_SETPCAP has much less broad semantics when configured
  267. * this way.)
  268. */
  269. static inline int do_sys_capset_other_tasks(pid_t pid,
  270. kernel_cap_t *effective,
  271. kernel_cap_t *inheritable,
  272. kernel_cap_t *permitted)
  273. {
  274. return -EPERM;
  275. }
  276. #endif /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */
  277. /*
  278. * Atomically modify the effective capabilities returning the original
  279. * value. No permission check is performed here - it is assumed that the
  280. * caller is permitted to set the desired effective capabilities.
  281. */
  282. kernel_cap_t cap_set_effective(const kernel_cap_t pE_new)
  283. {
  284. kernel_cap_t pE_old;
  285. spin_lock(&task_capability_lock);
  286. pE_old = current->cap_effective;
  287. current->cap_effective = pE_new;
  288. spin_unlock(&task_capability_lock);
  289. return pE_old;
  290. }
  291. EXPORT_SYMBOL(cap_set_effective);
  292. /**
  293. * sys_capget - get the capabilities of a given process.
  294. * @header: pointer to struct that contains capability version and
  295. * target pid data
  296. * @dataptr: pointer to struct that contains the effective, permitted,
  297. * and inheritable capabilities that are returned
  298. *
  299. * Returns 0 on success and < 0 on error.
  300. */
  301. asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
  302. {
  303. int ret = 0;
  304. pid_t pid;
  305. unsigned tocopy;
  306. kernel_cap_t pE, pI, pP;
  307. ret = cap_validate_magic(header, &tocopy);
  308. if (ret != 0)
  309. return ret;
  310. if (get_user(pid, &header->pid))
  311. return -EFAULT;
  312. if (pid < 0)
  313. return -EINVAL;
  314. ret = cap_get_target_pid(pid, &pE, &pI, &pP);
  315. if (!ret) {
  316. struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
  317. unsigned i;
  318. for (i = 0; i < tocopy; i++) {
  319. kdata[i].effective = pE.cap[i];
  320. kdata[i].permitted = pP.cap[i];
  321. kdata[i].inheritable = pI.cap[i];
  322. }
  323. /*
  324. * Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S,
  325. * we silently drop the upper capabilities here. This
  326. * has the effect of making older libcap
  327. * implementations implicitly drop upper capability
  328. * bits when they perform a: capget/modify/capset
  329. * sequence.
  330. *
  331. * This behavior is considered fail-safe
  332. * behavior. Upgrading the application to a newer
  333. * version of libcap will enable access to the newer
  334. * capabilities.
  335. *
  336. * An alternative would be to return an error here
  337. * (-ERANGE), but that causes legacy applications to
  338. * unexpectidly fail; the capget/modify/capset aborts
  339. * before modification is attempted and the application
  340. * fails.
  341. */
  342. if (copy_to_user(dataptr, kdata, tocopy
  343. * sizeof(struct __user_cap_data_struct))) {
  344. return -EFAULT;
  345. }
  346. }
  347. return ret;
  348. }
  349. /**
  350. * sys_capset - set capabilities for a process or (*) a group of processes
  351. * @header: pointer to struct that contains capability version and
  352. * target pid data
  353. * @data: pointer to struct that contains the effective, permitted,
  354. * and inheritable capabilities
  355. *
  356. * Set capabilities for a given process, all processes, or all
  357. * processes in a given process group.
  358. *
  359. * The restrictions on setting capabilities are specified as:
  360. *
  361. * [pid is for the 'target' task. 'current' is the calling task.]
  362. *
  363. * I: any raised capabilities must be a subset of the (old current) permitted
  364. * P: any raised capabilities must be a subset of the (old current) permitted
  365. * E: must be set to a subset of (new target) permitted
  366. *
  367. * Returns 0 on success and < 0 on error.
  368. */
  369. asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
  370. {
  371. struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
  372. unsigned i, tocopy;
  373. kernel_cap_t inheritable, permitted, effective;
  374. int ret;
  375. pid_t pid;
  376. ret = cap_validate_magic(header, &tocopy);
  377. if (ret != 0)
  378. return ret;
  379. if (get_user(pid, &header->pid))
  380. return -EFAULT;
  381. if (copy_from_user(&kdata, data, tocopy
  382. * sizeof(struct __user_cap_data_struct))) {
  383. return -EFAULT;
  384. }
  385. for (i = 0; i < tocopy; i++) {
  386. effective.cap[i] = kdata[i].effective;
  387. permitted.cap[i] = kdata[i].permitted;
  388. inheritable.cap[i] = kdata[i].inheritable;
  389. }
  390. while (i < _KERNEL_CAPABILITY_U32S) {
  391. effective.cap[i] = 0;
  392. permitted.cap[i] = 0;
  393. inheritable.cap[i] = 0;
  394. i++;
  395. }
  396. if (pid && (pid != task_pid_vnr(current)))
  397. ret = do_sys_capset_other_tasks(pid, &effective, &inheritable,
  398. &permitted);
  399. else {
  400. /*
  401. * This lock is required even when filesystem
  402. * capability support is configured - it protects the
  403. * sys_capget() call from returning incorrect data in
  404. * the case that the targeted process is not the
  405. * current one.
  406. */
  407. spin_lock(&task_capability_lock);
  408. ret = security_capset_check(current, &effective, &inheritable,
  409. &permitted);
  410. /*
  411. * Having verified that the proposed changes are
  412. * legal, we now put them into effect.
  413. */
  414. if (!ret)
  415. security_capset_set(current, &effective, &inheritable,
  416. &permitted);
  417. spin_unlock(&task_capability_lock);
  418. }
  419. return ret;
  420. }
  421. /**
  422. * capable - Determine if the current task has a superior capability in effect
  423. * @cap: The capability to be tested for
  424. *
  425. * Return true if the current task has the given superior capability currently
  426. * available for use, false if not.
  427. *
  428. * This sets PF_SUPERPRIV on the task if the capability is available on the
  429. * assumption that it's about to be used.
  430. */
  431. int capable(int cap)
  432. {
  433. if (has_capability(current, cap)) {
  434. current->flags |= PF_SUPERPRIV;
  435. return 1;
  436. }
  437. return 0;
  438. }
  439. EXPORT_SYMBOL(capable);