commoncap.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* Common capabilities, needed by capability.o and root_plug.o
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. */
  9. #include <linux/capability.h>
  10. #include <linux/config.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/security.h>
  15. #include <linux/file.h>
  16. #include <linux/mm.h>
  17. #include <linux/mman.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/swap.h>
  20. #include <linux/smp_lock.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/netlink.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/xattr.h>
  25. #include <linux/hugetlb.h>
  26. int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
  27. {
  28. NETLINK_CB(skb).eff_cap = current->cap_effective;
  29. return 0;
  30. }
  31. EXPORT_SYMBOL(cap_netlink_send);
  32. int cap_netlink_recv(struct sk_buff *skb)
  33. {
  34. if (!cap_raised(NETLINK_CB(skb).eff_cap, CAP_NET_ADMIN))
  35. return -EPERM;
  36. return 0;
  37. }
  38. EXPORT_SYMBOL(cap_netlink_recv);
  39. int cap_capable (struct task_struct *tsk, int cap)
  40. {
  41. /* Derived from include/linux/sched.h:capable. */
  42. if (cap_raised(tsk->cap_effective, cap))
  43. return 0;
  44. return -EPERM;
  45. }
  46. int cap_settime(struct timespec *ts, struct timezone *tz)
  47. {
  48. if (!capable(CAP_SYS_TIME))
  49. return -EPERM;
  50. return 0;
  51. }
  52. int cap_ptrace (struct task_struct *parent, struct task_struct *child)
  53. {
  54. /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */
  55. if (!cap_issubset(child->cap_permitted, parent->cap_permitted) &&
  56. !__capable(parent, CAP_SYS_PTRACE))
  57. return -EPERM;
  58. return 0;
  59. }
  60. int cap_capget (struct task_struct *target, kernel_cap_t *effective,
  61. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  62. {
  63. /* Derived from kernel/capability.c:sys_capget. */
  64. *effective = cap_t (target->cap_effective);
  65. *inheritable = cap_t (target->cap_inheritable);
  66. *permitted = cap_t (target->cap_permitted);
  67. return 0;
  68. }
  69. int cap_capset_check (struct task_struct *target, kernel_cap_t *effective,
  70. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  71. {
  72. /* Derived from kernel/capability.c:sys_capset. */
  73. /* verify restrictions on target's new Inheritable set */
  74. if (!cap_issubset (*inheritable,
  75. cap_combine (target->cap_inheritable,
  76. current->cap_permitted))) {
  77. return -EPERM;
  78. }
  79. /* verify restrictions on target's new Permitted set */
  80. if (!cap_issubset (*permitted,
  81. cap_combine (target->cap_permitted,
  82. current->cap_permitted))) {
  83. return -EPERM;
  84. }
  85. /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
  86. if (!cap_issubset (*effective, *permitted)) {
  87. return -EPERM;
  88. }
  89. return 0;
  90. }
  91. void cap_capset_set (struct task_struct *target, kernel_cap_t *effective,
  92. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  93. {
  94. target->cap_effective = *effective;
  95. target->cap_inheritable = *inheritable;
  96. target->cap_permitted = *permitted;
  97. }
  98. int cap_bprm_set_security (struct linux_binprm *bprm)
  99. {
  100. /* Copied from fs/exec.c:prepare_binprm. */
  101. /* We don't have VFS support for capabilities yet */
  102. cap_clear (bprm->cap_inheritable);
  103. cap_clear (bprm->cap_permitted);
  104. cap_clear (bprm->cap_effective);
  105. /* To support inheritance of root-permissions and suid-root
  106. * executables under compatibility mode, we raise all three
  107. * capability sets for the file.
  108. *
  109. * If only the real uid is 0, we only raise the inheritable
  110. * and permitted sets of the executable file.
  111. */
  112. if (!issecure (SECURE_NOROOT)) {
  113. if (bprm->e_uid == 0 || current->uid == 0) {
  114. cap_set_full (bprm->cap_inheritable);
  115. cap_set_full (bprm->cap_permitted);
  116. }
  117. if (bprm->e_uid == 0)
  118. cap_set_full (bprm->cap_effective);
  119. }
  120. return 0;
  121. }
  122. void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
  123. {
  124. /* Derived from fs/exec.c:compute_creds. */
  125. kernel_cap_t new_permitted, working;
  126. new_permitted = cap_intersect (bprm->cap_permitted, cap_bset);
  127. working = cap_intersect (bprm->cap_inheritable,
  128. current->cap_inheritable);
  129. new_permitted = cap_combine (new_permitted, working);
  130. if (bprm->e_uid != current->uid || bprm->e_gid != current->gid ||
  131. !cap_issubset (new_permitted, current->cap_permitted)) {
  132. current->mm->dumpable = suid_dumpable;
  133. if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
  134. if (!capable(CAP_SETUID)) {
  135. bprm->e_uid = current->uid;
  136. bprm->e_gid = current->gid;
  137. }
  138. if (!capable (CAP_SETPCAP)) {
  139. new_permitted = cap_intersect (new_permitted,
  140. current->cap_permitted);
  141. }
  142. }
  143. }
  144. current->suid = current->euid = current->fsuid = bprm->e_uid;
  145. current->sgid = current->egid = current->fsgid = bprm->e_gid;
  146. /* For init, we want to retain the capabilities set
  147. * in the init_task struct. Thus we skip the usual
  148. * capability rules */
  149. if (current->pid != 1) {
  150. current->cap_permitted = new_permitted;
  151. current->cap_effective =
  152. cap_intersect (new_permitted, bprm->cap_effective);
  153. }
  154. /* AUD: Audit candidate if current->cap_effective is set */
  155. current->keep_capabilities = 0;
  156. }
  157. int cap_bprm_secureexec (struct linux_binprm *bprm)
  158. {
  159. /* If/when this module is enhanced to incorporate capability
  160. bits on files, the test below should be extended to also perform a
  161. test between the old and new capability sets. For now,
  162. it simply preserves the legacy decision algorithm used by
  163. the old userland. */
  164. return (current->euid != current->uid ||
  165. current->egid != current->gid);
  166. }
  167. int cap_inode_setxattr(struct dentry *dentry, char *name, void *value,
  168. size_t size, int flags)
  169. {
  170. if (!strncmp(name, XATTR_SECURITY_PREFIX,
  171. sizeof(XATTR_SECURITY_PREFIX) - 1) &&
  172. !capable(CAP_SYS_ADMIN))
  173. return -EPERM;
  174. return 0;
  175. }
  176. int cap_inode_removexattr(struct dentry *dentry, char *name)
  177. {
  178. if (!strncmp(name, XATTR_SECURITY_PREFIX,
  179. sizeof(XATTR_SECURITY_PREFIX) - 1) &&
  180. !capable(CAP_SYS_ADMIN))
  181. return -EPERM;
  182. return 0;
  183. }
  184. /* moved from kernel/sys.c. */
  185. /*
  186. * cap_emulate_setxuid() fixes the effective / permitted capabilities of
  187. * a process after a call to setuid, setreuid, or setresuid.
  188. *
  189. * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
  190. * {r,e,s}uid != 0, the permitted and effective capabilities are
  191. * cleared.
  192. *
  193. * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
  194. * capabilities of the process are cleared.
  195. *
  196. * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
  197. * capabilities are set to the permitted capabilities.
  198. *
  199. * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
  200. * never happen.
  201. *
  202. * -astor
  203. *
  204. * cevans - New behaviour, Oct '99
  205. * A process may, via prctl(), elect to keep its capabilities when it
  206. * calls setuid() and switches away from uid==0. Both permitted and
  207. * effective sets will be retained.
  208. * Without this change, it was impossible for a daemon to drop only some
  209. * of its privilege. The call to setuid(!=0) would drop all privileges!
  210. * Keeping uid 0 is not an option because uid 0 owns too many vital
  211. * files..
  212. * Thanks to Olaf Kirch and Peter Benie for spotting this.
  213. */
  214. static inline void cap_emulate_setxuid (int old_ruid, int old_euid,
  215. int old_suid)
  216. {
  217. if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) &&
  218. (current->uid != 0 && current->euid != 0 && current->suid != 0) &&
  219. !current->keep_capabilities) {
  220. cap_clear (current->cap_permitted);
  221. cap_clear (current->cap_effective);
  222. }
  223. if (old_euid == 0 && current->euid != 0) {
  224. cap_clear (current->cap_effective);
  225. }
  226. if (old_euid != 0 && current->euid == 0) {
  227. current->cap_effective = current->cap_permitted;
  228. }
  229. }
  230. int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid,
  231. int flags)
  232. {
  233. switch (flags) {
  234. case LSM_SETID_RE:
  235. case LSM_SETID_ID:
  236. case LSM_SETID_RES:
  237. /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
  238. if (!issecure (SECURE_NO_SETUID_FIXUP)) {
  239. cap_emulate_setxuid (old_ruid, old_euid, old_suid);
  240. }
  241. break;
  242. case LSM_SETID_FS:
  243. {
  244. uid_t old_fsuid = old_ruid;
  245. /* Copied from kernel/sys.c:setfsuid. */
  246. /*
  247. * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
  248. * if not, we might be a bit too harsh here.
  249. */
  250. if (!issecure (SECURE_NO_SETUID_FIXUP)) {
  251. if (old_fsuid == 0 && current->fsuid != 0) {
  252. cap_t (current->cap_effective) &=
  253. ~CAP_FS_MASK;
  254. }
  255. if (old_fsuid != 0 && current->fsuid == 0) {
  256. cap_t (current->cap_effective) |=
  257. (cap_t (current->cap_permitted) &
  258. CAP_FS_MASK);
  259. }
  260. }
  261. break;
  262. }
  263. default:
  264. return -EINVAL;
  265. }
  266. return 0;
  267. }
  268. void cap_task_reparent_to_init (struct task_struct *p)
  269. {
  270. p->cap_effective = CAP_INIT_EFF_SET;
  271. p->cap_inheritable = CAP_INIT_INH_SET;
  272. p->cap_permitted = CAP_FULL_SET;
  273. p->keep_capabilities = 0;
  274. return;
  275. }
  276. int cap_syslog (int type)
  277. {
  278. if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
  279. return -EPERM;
  280. return 0;
  281. }
  282. int cap_vm_enough_memory(long pages)
  283. {
  284. int cap_sys_admin = 0;
  285. if (cap_capable(current, CAP_SYS_ADMIN) == 0)
  286. cap_sys_admin = 1;
  287. return __vm_enough_memory(pages, cap_sys_admin);
  288. }
  289. EXPORT_SYMBOL(cap_capable);
  290. EXPORT_SYMBOL(cap_settime);
  291. EXPORT_SYMBOL(cap_ptrace);
  292. EXPORT_SYMBOL(cap_capget);
  293. EXPORT_SYMBOL(cap_capset_check);
  294. EXPORT_SYMBOL(cap_capset_set);
  295. EXPORT_SYMBOL(cap_bprm_set_security);
  296. EXPORT_SYMBOL(cap_bprm_apply_creds);
  297. EXPORT_SYMBOL(cap_bprm_secureexec);
  298. EXPORT_SYMBOL(cap_inode_setxattr);
  299. EXPORT_SYMBOL(cap_inode_removexattr);
  300. EXPORT_SYMBOL(cap_task_post_setuid);
  301. EXPORT_SYMBOL(cap_task_reparent_to_init);
  302. EXPORT_SYMBOL(cap_syslog);
  303. EXPORT_SYMBOL(cap_vm_enough_memory);
  304. MODULE_DESCRIPTION("Standard Linux Common Capabilities Security Module");
  305. MODULE_LICENSE("GPL");