commoncap.c 9.4 KB

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