commoncap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. #include <linux/mount.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. 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. static inline void bprm_clear_caps(struct linux_binprm *bprm)
  97. {
  98. cap_clear(bprm->cap_inheritable);
  99. cap_clear(bprm->cap_permitted);
  100. bprm->cap_effective = false;
  101. }
  102. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  103. int cap_inode_need_killpriv(struct dentry *dentry)
  104. {
  105. struct inode *inode = dentry->d_inode;
  106. int error;
  107. if (!inode->i_op || !inode->i_op->getxattr)
  108. return 0;
  109. error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
  110. if (error <= 0)
  111. return 0;
  112. return 1;
  113. }
  114. int cap_inode_killpriv(struct dentry *dentry)
  115. {
  116. struct inode *inode = dentry->d_inode;
  117. if (!inode->i_op || !inode->i_op->removexattr)
  118. return 0;
  119. return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
  120. }
  121. static inline int cap_from_disk(__le32 *caps, struct linux_binprm *bprm,
  122. int size)
  123. {
  124. __u32 magic_etc;
  125. if (size != XATTR_CAPS_SZ)
  126. return -EINVAL;
  127. magic_etc = le32_to_cpu(caps[0]);
  128. switch ((magic_etc & VFS_CAP_REVISION_MASK)) {
  129. case VFS_CAP_REVISION:
  130. if (magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
  131. bprm->cap_effective = true;
  132. else
  133. bprm->cap_effective = false;
  134. bprm->cap_permitted = to_cap_t( le32_to_cpu(caps[1]) );
  135. bprm->cap_inheritable = to_cap_t( le32_to_cpu(caps[2]) );
  136. return 0;
  137. default:
  138. return -EINVAL;
  139. }
  140. }
  141. /* Locate any VFS capabilities: */
  142. static int get_file_caps(struct linux_binprm *bprm)
  143. {
  144. struct dentry *dentry;
  145. int rc = 0;
  146. __le32 v1caps[XATTR_CAPS_SZ];
  147. struct inode *inode;
  148. if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID) {
  149. bprm_clear_caps(bprm);
  150. return 0;
  151. }
  152. dentry = dget(bprm->file->f_dentry);
  153. inode = dentry->d_inode;
  154. if (!inode->i_op || !inode->i_op->getxattr)
  155. goto out;
  156. rc = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, &v1caps,
  157. XATTR_CAPS_SZ);
  158. if (rc == -ENODATA || rc == -EOPNOTSUPP) {
  159. /* no data, that's ok */
  160. rc = 0;
  161. goto out;
  162. }
  163. if (rc < 0)
  164. goto out;
  165. rc = cap_from_disk(v1caps, bprm, rc);
  166. if (rc)
  167. printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
  168. __FUNCTION__, rc, bprm->filename);
  169. out:
  170. dput(dentry);
  171. if (rc)
  172. bprm_clear_caps(bprm);
  173. return rc;
  174. }
  175. #else
  176. int cap_inode_need_killpriv(struct dentry *dentry)
  177. {
  178. return 0;
  179. }
  180. int cap_inode_killpriv(struct dentry *dentry)
  181. {
  182. return 0;
  183. }
  184. static inline int get_file_caps(struct linux_binprm *bprm)
  185. {
  186. bprm_clear_caps(bprm);
  187. return 0;
  188. }
  189. #endif
  190. int cap_bprm_set_security (struct linux_binprm *bprm)
  191. {
  192. int ret;
  193. ret = get_file_caps(bprm);
  194. if (ret)
  195. printk(KERN_NOTICE "%s: get_file_caps returned %d for %s\n",
  196. __FUNCTION__, ret, bprm->filename);
  197. /* To support inheritance of root-permissions and suid-root
  198. * executables under compatibility mode, we raise all three
  199. * capability sets for the file.
  200. *
  201. * If only the real uid is 0, we only raise the inheritable
  202. * and permitted sets of the executable file.
  203. */
  204. if (!issecure (SECURE_NOROOT)) {
  205. if (bprm->e_uid == 0 || current->uid == 0) {
  206. cap_set_full (bprm->cap_inheritable);
  207. cap_set_full (bprm->cap_permitted);
  208. }
  209. if (bprm->e_uid == 0)
  210. bprm->cap_effective = true;
  211. }
  212. return ret;
  213. }
  214. void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
  215. {
  216. /* Derived from fs/exec.c:compute_creds. */
  217. kernel_cap_t new_permitted, working;
  218. new_permitted = cap_intersect (bprm->cap_permitted, cap_bset);
  219. working = cap_intersect (bprm->cap_inheritable,
  220. current->cap_inheritable);
  221. new_permitted = cap_combine (new_permitted, working);
  222. if (bprm->e_uid != current->uid || bprm->e_gid != current->gid ||
  223. !cap_issubset (new_permitted, current->cap_permitted)) {
  224. set_dumpable(current->mm, suid_dumpable);
  225. current->pdeath_signal = 0;
  226. if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
  227. if (!capable(CAP_SETUID)) {
  228. bprm->e_uid = current->uid;
  229. bprm->e_gid = current->gid;
  230. }
  231. if (!capable (CAP_SETPCAP)) {
  232. new_permitted = cap_intersect (new_permitted,
  233. current->cap_permitted);
  234. }
  235. }
  236. }
  237. current->suid = current->euid = current->fsuid = bprm->e_uid;
  238. current->sgid = current->egid = current->fsgid = bprm->e_gid;
  239. /* For init, we want to retain the capabilities set
  240. * in the init_task struct. Thus we skip the usual
  241. * capability rules */
  242. if (!is_init(current)) {
  243. current->cap_permitted = new_permitted;
  244. current->cap_effective = bprm->cap_effective ?
  245. new_permitted : 0;
  246. }
  247. /* AUD: Audit candidate if current->cap_effective is set */
  248. current->keep_capabilities = 0;
  249. }
  250. int cap_bprm_secureexec (struct linux_binprm *bprm)
  251. {
  252. if (current->uid != 0) {
  253. if (bprm->cap_effective)
  254. return 1;
  255. if (!cap_isclear(bprm->cap_permitted))
  256. return 1;
  257. if (!cap_isclear(bprm->cap_inheritable))
  258. return 1;
  259. }
  260. return (current->euid != current->uid ||
  261. current->egid != current->gid);
  262. }
  263. int cap_inode_setxattr(struct dentry *dentry, char *name, void *value,
  264. size_t size, int flags)
  265. {
  266. if (!strcmp(name, XATTR_NAME_CAPS)) {
  267. if (!capable(CAP_SETFCAP))
  268. return -EPERM;
  269. return 0;
  270. } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
  271. sizeof(XATTR_SECURITY_PREFIX) - 1) &&
  272. !capable(CAP_SYS_ADMIN))
  273. return -EPERM;
  274. return 0;
  275. }
  276. int cap_inode_removexattr(struct dentry *dentry, char *name)
  277. {
  278. if (!strcmp(name, XATTR_NAME_CAPS)) {
  279. if (!capable(CAP_SETFCAP))
  280. return -EPERM;
  281. return 0;
  282. } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
  283. sizeof(XATTR_SECURITY_PREFIX) - 1) &&
  284. !capable(CAP_SYS_ADMIN))
  285. return -EPERM;
  286. return 0;
  287. }
  288. /* moved from kernel/sys.c. */
  289. /*
  290. * cap_emulate_setxuid() fixes the effective / permitted capabilities of
  291. * a process after a call to setuid, setreuid, or setresuid.
  292. *
  293. * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
  294. * {r,e,s}uid != 0, the permitted and effective capabilities are
  295. * cleared.
  296. *
  297. * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
  298. * capabilities of the process are cleared.
  299. *
  300. * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
  301. * capabilities are set to the permitted capabilities.
  302. *
  303. * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
  304. * never happen.
  305. *
  306. * -astor
  307. *
  308. * cevans - New behaviour, Oct '99
  309. * A process may, via prctl(), elect to keep its capabilities when it
  310. * calls setuid() and switches away from uid==0. Both permitted and
  311. * effective sets will be retained.
  312. * Without this change, it was impossible for a daemon to drop only some
  313. * of its privilege. The call to setuid(!=0) would drop all privileges!
  314. * Keeping uid 0 is not an option because uid 0 owns too many vital
  315. * files..
  316. * Thanks to Olaf Kirch and Peter Benie for spotting this.
  317. */
  318. static inline void cap_emulate_setxuid (int old_ruid, int old_euid,
  319. int old_suid)
  320. {
  321. if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) &&
  322. (current->uid != 0 && current->euid != 0 && current->suid != 0) &&
  323. !current->keep_capabilities) {
  324. cap_clear (current->cap_permitted);
  325. cap_clear (current->cap_effective);
  326. }
  327. if (old_euid == 0 && current->euid != 0) {
  328. cap_clear (current->cap_effective);
  329. }
  330. if (old_euid != 0 && current->euid == 0) {
  331. current->cap_effective = current->cap_permitted;
  332. }
  333. }
  334. int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid,
  335. int flags)
  336. {
  337. switch (flags) {
  338. case LSM_SETID_RE:
  339. case LSM_SETID_ID:
  340. case LSM_SETID_RES:
  341. /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
  342. if (!issecure (SECURE_NO_SETUID_FIXUP)) {
  343. cap_emulate_setxuid (old_ruid, old_euid, old_suid);
  344. }
  345. break;
  346. case LSM_SETID_FS:
  347. {
  348. uid_t old_fsuid = old_ruid;
  349. /* Copied from kernel/sys.c:setfsuid. */
  350. /*
  351. * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
  352. * if not, we might be a bit too harsh here.
  353. */
  354. if (!issecure (SECURE_NO_SETUID_FIXUP)) {
  355. if (old_fsuid == 0 && current->fsuid != 0) {
  356. cap_t (current->cap_effective) &=
  357. ~CAP_FS_MASK;
  358. }
  359. if (old_fsuid != 0 && current->fsuid == 0) {
  360. cap_t (current->cap_effective) |=
  361. (cap_t (current->cap_permitted) &
  362. CAP_FS_MASK);
  363. }
  364. }
  365. break;
  366. }
  367. default:
  368. return -EINVAL;
  369. }
  370. return 0;
  371. }
  372. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  373. /*
  374. * Rationale: code calling task_setscheduler, task_setioprio, and
  375. * task_setnice, assumes that
  376. * . if capable(cap_sys_nice), then those actions should be allowed
  377. * . if not capable(cap_sys_nice), but acting on your own processes,
  378. * then those actions should be allowed
  379. * This is insufficient now since you can call code without suid, but
  380. * yet with increased caps.
  381. * So we check for increased caps on the target process.
  382. */
  383. static inline int cap_safe_nice(struct task_struct *p)
  384. {
  385. if (!cap_issubset(p->cap_permitted, current->cap_permitted) &&
  386. !__capable(current, CAP_SYS_NICE))
  387. return -EPERM;
  388. return 0;
  389. }
  390. int cap_task_setscheduler (struct task_struct *p, int policy,
  391. struct sched_param *lp)
  392. {
  393. return cap_safe_nice(p);
  394. }
  395. int cap_task_setioprio (struct task_struct *p, int ioprio)
  396. {
  397. return cap_safe_nice(p);
  398. }
  399. int cap_task_setnice (struct task_struct *p, int nice)
  400. {
  401. return cap_safe_nice(p);
  402. }
  403. int cap_task_kill(struct task_struct *p, struct siginfo *info,
  404. int sig, u32 secid)
  405. {
  406. if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info)))
  407. return 0;
  408. if (secid)
  409. /*
  410. * Signal sent as a particular user.
  411. * Capabilities are ignored. May be wrong, but it's the
  412. * only thing we can do at the moment.
  413. * Used only by usb drivers?
  414. */
  415. return 0;
  416. if (cap_issubset(p->cap_permitted, current->cap_permitted))
  417. return 0;
  418. if (capable(CAP_KILL))
  419. return 0;
  420. return -EPERM;
  421. }
  422. #else
  423. int cap_task_setscheduler (struct task_struct *p, int policy,
  424. struct sched_param *lp)
  425. {
  426. return 0;
  427. }
  428. int cap_task_setioprio (struct task_struct *p, int ioprio)
  429. {
  430. return 0;
  431. }
  432. int cap_task_setnice (struct task_struct *p, int nice)
  433. {
  434. return 0;
  435. }
  436. int cap_task_kill(struct task_struct *p, struct siginfo *info,
  437. int sig, u32 secid)
  438. {
  439. return 0;
  440. }
  441. #endif
  442. void cap_task_reparent_to_init (struct task_struct *p)
  443. {
  444. p->cap_effective = CAP_INIT_EFF_SET;
  445. p->cap_inheritable = CAP_INIT_INH_SET;
  446. p->cap_permitted = CAP_FULL_SET;
  447. p->keep_capabilities = 0;
  448. return;
  449. }
  450. int cap_syslog (int type)
  451. {
  452. if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
  453. return -EPERM;
  454. return 0;
  455. }
  456. int cap_vm_enough_memory(struct mm_struct *mm, long pages)
  457. {
  458. int cap_sys_admin = 0;
  459. if (cap_capable(current, CAP_SYS_ADMIN) == 0)
  460. cap_sys_admin = 1;
  461. return __vm_enough_memory(mm, pages, cap_sys_admin);
  462. }