capability.c 6.3 KB

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