commoncap.c 9.4 KB

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