capability.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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@transmeta.com>
  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 <asm/uaccess.h>
  15. unsigned securebits = SECUREBITS_DEFAULT; /* systemwide security settings */
  16. kernel_cap_t cap_bset = CAP_INIT_EFF_SET;
  17. EXPORT_SYMBOL(securebits);
  18. EXPORT_SYMBOL(cap_bset);
  19. /*
  20. * This lock protects task->cap_* for all tasks including current.
  21. * Locking rule: acquire this prior to tasklist_lock.
  22. */
  23. static DEFINE_SPINLOCK(task_capability_lock);
  24. /*
  25. * For sys_getproccap() and sys_setproccap(), any of the three
  26. * capability set pointers may be NULL -- indicating that that set is
  27. * uninteresting and/or not to be changed.
  28. */
  29. /**
  30. * sys_capget - get the capabilities of a given process.
  31. * @header: pointer to struct that contains capability version and
  32. * target pid data
  33. * @dataptr: pointer to struct that contains the effective, permitted,
  34. * and inheritable capabilities that are returned
  35. *
  36. * Returns 0 on success and < 0 on error.
  37. */
  38. asmlinkage long sys_capget(cap_user_header_t header, cap_user_data_t dataptr)
  39. {
  40. int ret = 0;
  41. pid_t pid;
  42. __u32 version;
  43. struct task_struct *target;
  44. struct __user_cap_data_struct data;
  45. if (get_user(version, &header->version))
  46. return -EFAULT;
  47. if (version != _LINUX_CAPABILITY_VERSION) {
  48. if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
  49. return -EFAULT;
  50. return -EINVAL;
  51. }
  52. if (get_user(pid, &header->pid))
  53. return -EFAULT;
  54. if (pid < 0)
  55. return -EINVAL;
  56. spin_lock(&task_capability_lock);
  57. read_lock(&tasklist_lock);
  58. if (pid && pid != current->pid) {
  59. target = find_task_by_pid(pid);
  60. if (!target) {
  61. ret = -ESRCH;
  62. goto out;
  63. }
  64. } else
  65. target = current;
  66. ret = security_capget(target, &data.effective, &data.inheritable, &data.permitted);
  67. out:
  68. read_unlock(&tasklist_lock);
  69. spin_unlock(&task_capability_lock);
  70. if (!ret && copy_to_user(dataptr, &data, sizeof data))
  71. return -EFAULT;
  72. return ret;
  73. }
  74. /*
  75. * cap_set_pg - set capabilities for all processes in a given process
  76. * group. We call this holding task_capability_lock and tasklist_lock.
  77. */
  78. static inline int cap_set_pg(int pgrp, kernel_cap_t *effective,
  79. kernel_cap_t *inheritable,
  80. kernel_cap_t *permitted)
  81. {
  82. struct task_struct *g, *target;
  83. int ret = -EPERM;
  84. int found = 0;
  85. do_each_task_pid(pgrp, PIDTYPE_PGID, g) {
  86. target = g;
  87. while_each_thread(g, target) {
  88. if (!security_capset_check(target, effective,
  89. inheritable,
  90. permitted)) {
  91. security_capset_set(target, effective,
  92. inheritable,
  93. permitted);
  94. ret = 0;
  95. }
  96. found = 1;
  97. }
  98. } while_each_task_pid(pgrp, PIDTYPE_PGID, g);
  99. if (!found)
  100. ret = 0;
  101. return ret;
  102. }
  103. /*
  104. * cap_set_all - set capabilities for all processes other than init
  105. * and self. We call this holding task_capability_lock and tasklist_lock.
  106. */
  107. static inline int cap_set_all(kernel_cap_t *effective,
  108. kernel_cap_t *inheritable,
  109. kernel_cap_t *permitted)
  110. {
  111. struct task_struct *g, *target;
  112. int ret = -EPERM;
  113. int found = 0;
  114. do_each_thread(g, target) {
  115. if (target == current || target->pid == 1)
  116. continue;
  117. found = 1;
  118. if (security_capset_check(target, effective, inheritable,
  119. permitted))
  120. continue;
  121. ret = 0;
  122. security_capset_set(target, effective, inheritable, permitted);
  123. } while_each_thread(g, target);
  124. if (!found)
  125. ret = 0;
  126. return ret;
  127. }
  128. /**
  129. * sys_capset - set capabilities for a process or a group of processes
  130. * @header: pointer to struct that contains capability version and
  131. * target pid data
  132. * @data: pointer to struct that contains the effective, permitted,
  133. * and inheritable capabilities
  134. *
  135. * Set capabilities for a given process, all processes, or all
  136. * processes in a given process group.
  137. *
  138. * The restrictions on setting capabilities are specified as:
  139. *
  140. * [pid is for the 'target' task. 'current' is the calling task.]
  141. *
  142. * I: any raised capabilities must be a subset of the (old current) permitted
  143. * P: any raised capabilities must be a subset of the (old current) permitted
  144. * E: must be set to a subset of (new target) permitted
  145. *
  146. * Returns 0 on success and < 0 on error.
  147. */
  148. asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
  149. {
  150. kernel_cap_t inheritable, permitted, effective;
  151. __u32 version;
  152. struct task_struct *target;
  153. int ret;
  154. pid_t pid;
  155. if (get_user(version, &header->version))
  156. return -EFAULT;
  157. if (version != _LINUX_CAPABILITY_VERSION) {
  158. if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
  159. return -EFAULT;
  160. return -EINVAL;
  161. }
  162. if (get_user(pid, &header->pid))
  163. return -EFAULT;
  164. if (pid && pid != current->pid && !capable(CAP_SETPCAP))
  165. return -EPERM;
  166. if (copy_from_user(&effective, &data->effective, sizeof(effective)) ||
  167. copy_from_user(&inheritable, &data->inheritable, sizeof(inheritable)) ||
  168. copy_from_user(&permitted, &data->permitted, sizeof(permitted)))
  169. return -EFAULT;
  170. spin_lock(&task_capability_lock);
  171. read_lock(&tasklist_lock);
  172. if (pid > 0 && pid != current->pid) {
  173. target = find_task_by_pid(pid);
  174. if (!target) {
  175. ret = -ESRCH;
  176. goto out;
  177. }
  178. } else
  179. target = current;
  180. ret = 0;
  181. /* having verified that the proposed changes are legal,
  182. we now put them into effect. */
  183. if (pid < 0) {
  184. if (pid == -1) /* all procs other than current and init */
  185. ret = cap_set_all(&effective, &inheritable, &permitted);
  186. else /* all procs in process group */
  187. ret = cap_set_pg(-pid, &effective, &inheritable,
  188. &permitted);
  189. } else {
  190. ret = security_capset_check(target, &effective, &inheritable,
  191. &permitted);
  192. if (!ret)
  193. security_capset_set(target, &effective, &inheritable,
  194. &permitted);
  195. }
  196. out:
  197. read_unlock(&tasklist_lock);
  198. spin_unlock(&task_capability_lock);
  199. return ret;
  200. }
  201. int __capable(struct task_struct *t, int cap)
  202. {
  203. if (security_capable(t, cap) == 0) {
  204. t->flags |= PF_SUPERPRIV;
  205. return 1;
  206. }
  207. return 0;
  208. }
  209. EXPORT_SYMBOL(__capable);
  210. int capable(int cap)
  211. {
  212. return __capable(current, cap);
  213. }
  214. EXPORT_SYMBOL(capable);