capability.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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_nr, 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. struct pid *pgrp;
  86. pgrp = find_pid(pgrp_nr);
  87. do_each_pid_task(pgrp, PIDTYPE_PGID, g) {
  88. target = g;
  89. while_each_thread(g, target) {
  90. if (!security_capset_check(target, effective,
  91. inheritable,
  92. permitted)) {
  93. security_capset_set(target, effective,
  94. inheritable,
  95. permitted);
  96. ret = 0;
  97. }
  98. found = 1;
  99. }
  100. } while_each_pid_task(pgrp, PIDTYPE_PGID, g);
  101. if (!found)
  102. ret = 0;
  103. return ret;
  104. }
  105. /*
  106. * cap_set_all - set capabilities for all processes other than init
  107. * and self. We call this holding task_capability_lock and tasklist_lock.
  108. */
  109. static inline int cap_set_all(kernel_cap_t *effective,
  110. kernel_cap_t *inheritable,
  111. kernel_cap_t *permitted)
  112. {
  113. struct task_struct *g, *target;
  114. int ret = -EPERM;
  115. int found = 0;
  116. do_each_thread(g, target) {
  117. if (target == current || is_init(target))
  118. continue;
  119. found = 1;
  120. if (security_capset_check(target, effective, inheritable,
  121. permitted))
  122. continue;
  123. ret = 0;
  124. security_capset_set(target, effective, inheritable, permitted);
  125. } while_each_thread(g, target);
  126. if (!found)
  127. ret = 0;
  128. return ret;
  129. }
  130. /**
  131. * sys_capset - set capabilities for a process or a group of processes
  132. * @header: pointer to struct that contains capability version and
  133. * target pid data
  134. * @data: pointer to struct that contains the effective, permitted,
  135. * and inheritable capabilities
  136. *
  137. * Set capabilities for a given process, all processes, or all
  138. * processes in a given process group.
  139. *
  140. * The restrictions on setting capabilities are specified as:
  141. *
  142. * [pid is for the 'target' task. 'current' is the calling task.]
  143. *
  144. * I: any raised capabilities must be a subset of the (old current) permitted
  145. * P: any raised capabilities must be a subset of the (old current) permitted
  146. * E: must be set to a subset of (new target) permitted
  147. *
  148. * Returns 0 on success and < 0 on error.
  149. */
  150. asmlinkage long sys_capset(cap_user_header_t header, const cap_user_data_t data)
  151. {
  152. kernel_cap_t inheritable, permitted, effective;
  153. __u32 version;
  154. struct task_struct *target;
  155. int ret;
  156. pid_t pid;
  157. if (get_user(version, &header->version))
  158. return -EFAULT;
  159. if (version != _LINUX_CAPABILITY_VERSION) {
  160. if (put_user(_LINUX_CAPABILITY_VERSION, &header->version))
  161. return -EFAULT;
  162. return -EINVAL;
  163. }
  164. if (get_user(pid, &header->pid))
  165. return -EFAULT;
  166. if (pid && pid != current->pid && !capable(CAP_SETPCAP))
  167. return -EPERM;
  168. if (copy_from_user(&effective, &data->effective, sizeof(effective)) ||
  169. copy_from_user(&inheritable, &data->inheritable, sizeof(inheritable)) ||
  170. copy_from_user(&permitted, &data->permitted, sizeof(permitted)))
  171. return -EFAULT;
  172. spin_lock(&task_capability_lock);
  173. read_lock(&tasklist_lock);
  174. if (pid > 0 && pid != current->pid) {
  175. target = find_task_by_pid(pid);
  176. if (!target) {
  177. ret = -ESRCH;
  178. goto out;
  179. }
  180. } else
  181. target = current;
  182. ret = 0;
  183. /* having verified that the proposed changes are legal,
  184. we now put them into effect. */
  185. if (pid < 0) {
  186. if (pid == -1) /* all procs other than current and init */
  187. ret = cap_set_all(&effective, &inheritable, &permitted);
  188. else /* all procs in process group */
  189. ret = cap_set_pg(-pid, &effective, &inheritable,
  190. &permitted);
  191. } else {
  192. ret = security_capset_check(target, &effective, &inheritable,
  193. &permitted);
  194. if (!ret)
  195. security_capset_set(target, &effective, &inheritable,
  196. &permitted);
  197. }
  198. out:
  199. read_unlock(&tasklist_lock);
  200. spin_unlock(&task_capability_lock);
  201. return ret;
  202. }
  203. int __capable(struct task_struct *t, int cap)
  204. {
  205. if (security_capable(t, cap) == 0) {
  206. t->flags |= PF_SUPERPRIV;
  207. return 1;
  208. }
  209. return 0;
  210. }
  211. EXPORT_SYMBOL(__capable);
  212. int capable(int cap)
  213. {
  214. return __capable(current, cap);
  215. }
  216. EXPORT_SYMBOL(capable);