commoncap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. #include <linux/sched.h>
  26. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  27. /*
  28. * Because of the reduced scope of CAP_SETPCAP when filesystem
  29. * capabilities are in effect, it is safe to allow this capability to
  30. * be available in the default configuration.
  31. */
  32. # define CAP_INIT_BSET CAP_FULL_SET
  33. #else /* ie. ndef CONFIG_SECURITY_FILE_CAPABILITIES */
  34. # define CAP_INIT_BSET CAP_INIT_EFF_SET
  35. #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
  36. kernel_cap_t cap_bset = CAP_INIT_BSET; /* systemwide capability bound */
  37. EXPORT_SYMBOL(cap_bset);
  38. /* Global security state */
  39. unsigned securebits = SECUREBITS_DEFAULT; /* systemwide security settings */
  40. EXPORT_SYMBOL(securebits);
  41. int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
  42. {
  43. NETLINK_CB(skb).eff_cap = current->cap_effective;
  44. return 0;
  45. }
  46. int cap_netlink_recv(struct sk_buff *skb, int cap)
  47. {
  48. if (!cap_raised(NETLINK_CB(skb).eff_cap, cap))
  49. return -EPERM;
  50. return 0;
  51. }
  52. EXPORT_SYMBOL(cap_netlink_recv);
  53. int cap_capable (struct task_struct *tsk, int cap)
  54. {
  55. /* Derived from include/linux/sched.h:capable. */
  56. if (cap_raised(tsk->cap_effective, cap))
  57. return 0;
  58. return -EPERM;
  59. }
  60. int cap_settime(struct timespec *ts, struct timezone *tz)
  61. {
  62. if (!capable(CAP_SYS_TIME))
  63. return -EPERM;
  64. return 0;
  65. }
  66. int cap_ptrace (struct task_struct *parent, struct task_struct *child)
  67. {
  68. /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */
  69. if (!cap_issubset(child->cap_permitted, parent->cap_permitted) &&
  70. !__capable(parent, CAP_SYS_PTRACE))
  71. return -EPERM;
  72. return 0;
  73. }
  74. int cap_capget (struct task_struct *target, kernel_cap_t *effective,
  75. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  76. {
  77. /* Derived from kernel/capability.c:sys_capget. */
  78. *effective = cap_t (target->cap_effective);
  79. *inheritable = cap_t (target->cap_inheritable);
  80. *permitted = cap_t (target->cap_permitted);
  81. return 0;
  82. }
  83. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  84. static inline int cap_block_setpcap(struct task_struct *target)
  85. {
  86. /*
  87. * No support for remote process capability manipulation with
  88. * filesystem capability support.
  89. */
  90. return (target != current);
  91. }
  92. static inline int cap_inh_is_capped(void)
  93. {
  94. /*
  95. * return 1 if changes to the inheritable set are limited
  96. * to the old permitted set.
  97. */
  98. return !cap_capable(current, CAP_SETPCAP);
  99. }
  100. #else /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */
  101. static inline int cap_block_setpcap(struct task_struct *t) { return 0; }
  102. static inline int cap_inh_is_capped(void) { return 1; }
  103. #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
  104. int cap_capset_check (struct task_struct *target, kernel_cap_t *effective,
  105. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  106. {
  107. if (cap_block_setpcap(target)) {
  108. return -EPERM;
  109. }
  110. if (cap_inh_is_capped()
  111. && !cap_issubset(*inheritable,
  112. cap_combine(target->cap_inheritable,
  113. current->cap_permitted))) {
  114. /* incapable of using this inheritable set */
  115. return -EPERM;
  116. }
  117. /* verify restrictions on target's new Permitted set */
  118. if (!cap_issubset (*permitted,
  119. cap_combine (target->cap_permitted,
  120. current->cap_permitted))) {
  121. return -EPERM;
  122. }
  123. /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
  124. if (!cap_issubset (*effective, *permitted)) {
  125. return -EPERM;
  126. }
  127. return 0;
  128. }
  129. void cap_capset_set (struct task_struct *target, kernel_cap_t *effective,
  130. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  131. {
  132. target->cap_effective = *effective;
  133. target->cap_inheritable = *inheritable;
  134. target->cap_permitted = *permitted;
  135. }
  136. static inline void bprm_clear_caps(struct linux_binprm *bprm)
  137. {
  138. cap_clear(bprm->cap_inheritable);
  139. cap_clear(bprm->cap_permitted);
  140. bprm->cap_effective = false;
  141. }
  142. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  143. int cap_inode_need_killpriv(struct dentry *dentry)
  144. {
  145. struct inode *inode = dentry->d_inode;
  146. int error;
  147. if (!inode->i_op || !inode->i_op->getxattr)
  148. return 0;
  149. error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
  150. if (error <= 0)
  151. return 0;
  152. return 1;
  153. }
  154. int cap_inode_killpriv(struct dentry *dentry)
  155. {
  156. struct inode *inode = dentry->d_inode;
  157. if (!inode->i_op || !inode->i_op->removexattr)
  158. return 0;
  159. return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
  160. }
  161. static inline int cap_from_disk(struct vfs_cap_data *caps,
  162. struct linux_binprm *bprm,
  163. int size)
  164. {
  165. __u32 magic_etc;
  166. if (size != XATTR_CAPS_SZ)
  167. return -EINVAL;
  168. magic_etc = le32_to_cpu(caps->magic_etc);
  169. switch ((magic_etc & VFS_CAP_REVISION_MASK)) {
  170. case VFS_CAP_REVISION:
  171. if (magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
  172. bprm->cap_effective = true;
  173. else
  174. bprm->cap_effective = false;
  175. bprm->cap_permitted = to_cap_t(le32_to_cpu(caps->permitted));
  176. bprm->cap_inheritable = to_cap_t(le32_to_cpu(caps->inheritable));
  177. return 0;
  178. default:
  179. return -EINVAL;
  180. }
  181. }
  182. /* Locate any VFS capabilities: */
  183. static int get_file_caps(struct linux_binprm *bprm)
  184. {
  185. struct dentry *dentry;
  186. int rc = 0;
  187. struct vfs_cap_data incaps;
  188. struct inode *inode;
  189. if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID) {
  190. bprm_clear_caps(bprm);
  191. return 0;
  192. }
  193. dentry = dget(bprm->file->f_dentry);
  194. inode = dentry->d_inode;
  195. if (!inode->i_op || !inode->i_op->getxattr)
  196. goto out;
  197. rc = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
  198. if (rc > 0) {
  199. if (rc == XATTR_CAPS_SZ)
  200. rc = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS,
  201. &incaps, XATTR_CAPS_SZ);
  202. else
  203. rc = -EINVAL;
  204. }
  205. if (rc == -ENODATA || rc == -EOPNOTSUPP) {
  206. /* no data, that's ok */
  207. rc = 0;
  208. goto out;
  209. }
  210. if (rc < 0)
  211. goto out;
  212. rc = cap_from_disk(&incaps, bprm, rc);
  213. if (rc)
  214. printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
  215. __FUNCTION__, rc, bprm->filename);
  216. out:
  217. dput(dentry);
  218. if (rc)
  219. bprm_clear_caps(bprm);
  220. return rc;
  221. }
  222. #else
  223. int cap_inode_need_killpriv(struct dentry *dentry)
  224. {
  225. return 0;
  226. }
  227. int cap_inode_killpriv(struct dentry *dentry)
  228. {
  229. return 0;
  230. }
  231. static inline int get_file_caps(struct linux_binprm *bprm)
  232. {
  233. bprm_clear_caps(bprm);
  234. return 0;
  235. }
  236. #endif
  237. int cap_bprm_set_security (struct linux_binprm *bprm)
  238. {
  239. int ret;
  240. ret = get_file_caps(bprm);
  241. if (ret)
  242. printk(KERN_NOTICE "%s: get_file_caps returned %d for %s\n",
  243. __FUNCTION__, ret, bprm->filename);
  244. /* To support inheritance of root-permissions and suid-root
  245. * executables under compatibility mode, we raise all three
  246. * capability sets for the file.
  247. *
  248. * If only the real uid is 0, we only raise the inheritable
  249. * and permitted sets of the executable file.
  250. */
  251. if (!issecure (SECURE_NOROOT)) {
  252. if (bprm->e_uid == 0 || current->uid == 0) {
  253. cap_set_full (bprm->cap_inheritable);
  254. cap_set_full (bprm->cap_permitted);
  255. }
  256. if (bprm->e_uid == 0)
  257. bprm->cap_effective = true;
  258. }
  259. return ret;
  260. }
  261. void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
  262. {
  263. /* Derived from fs/exec.c:compute_creds. */
  264. kernel_cap_t new_permitted, working;
  265. new_permitted = cap_intersect (bprm->cap_permitted, cap_bset);
  266. working = cap_intersect (bprm->cap_inheritable,
  267. current->cap_inheritable);
  268. new_permitted = cap_combine (new_permitted, working);
  269. if (bprm->e_uid != current->uid || bprm->e_gid != current->gid ||
  270. !cap_issubset (new_permitted, current->cap_permitted)) {
  271. set_dumpable(current->mm, suid_dumpable);
  272. current->pdeath_signal = 0;
  273. if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
  274. if (!capable(CAP_SETUID)) {
  275. bprm->e_uid = current->uid;
  276. bprm->e_gid = current->gid;
  277. }
  278. if (!capable (CAP_SETPCAP)) {
  279. new_permitted = cap_intersect (new_permitted,
  280. current->cap_permitted);
  281. }
  282. }
  283. }
  284. current->suid = current->euid = current->fsuid = bprm->e_uid;
  285. current->sgid = current->egid = current->fsgid = bprm->e_gid;
  286. /* For init, we want to retain the capabilities set
  287. * in the init_task struct. Thus we skip the usual
  288. * capability rules */
  289. if (!is_global_init(current)) {
  290. current->cap_permitted = new_permitted;
  291. current->cap_effective = bprm->cap_effective ?
  292. new_permitted : 0;
  293. }
  294. /* AUD: Audit candidate if current->cap_effective is set */
  295. current->keep_capabilities = 0;
  296. }
  297. int cap_bprm_secureexec (struct linux_binprm *bprm)
  298. {
  299. if (current->uid != 0) {
  300. if (bprm->cap_effective)
  301. return 1;
  302. if (!cap_isclear(bprm->cap_permitted))
  303. return 1;
  304. if (!cap_isclear(bprm->cap_inheritable))
  305. return 1;
  306. }
  307. return (current->euid != current->uid ||
  308. current->egid != current->gid);
  309. }
  310. int cap_inode_setxattr(struct dentry *dentry, char *name, void *value,
  311. size_t size, int flags)
  312. {
  313. if (!strcmp(name, XATTR_NAME_CAPS)) {
  314. if (!capable(CAP_SETFCAP))
  315. return -EPERM;
  316. return 0;
  317. } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
  318. sizeof(XATTR_SECURITY_PREFIX) - 1) &&
  319. !capable(CAP_SYS_ADMIN))
  320. return -EPERM;
  321. return 0;
  322. }
  323. int cap_inode_removexattr(struct dentry *dentry, char *name)
  324. {
  325. if (!strcmp(name, XATTR_NAME_CAPS)) {
  326. if (!capable(CAP_SETFCAP))
  327. return -EPERM;
  328. return 0;
  329. } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
  330. sizeof(XATTR_SECURITY_PREFIX) - 1) &&
  331. !capable(CAP_SYS_ADMIN))
  332. return -EPERM;
  333. return 0;
  334. }
  335. /* moved from kernel/sys.c. */
  336. /*
  337. * cap_emulate_setxuid() fixes the effective / permitted capabilities of
  338. * a process after a call to setuid, setreuid, or setresuid.
  339. *
  340. * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
  341. * {r,e,s}uid != 0, the permitted and effective capabilities are
  342. * cleared.
  343. *
  344. * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
  345. * capabilities of the process are cleared.
  346. *
  347. * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
  348. * capabilities are set to the permitted capabilities.
  349. *
  350. * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
  351. * never happen.
  352. *
  353. * -astor
  354. *
  355. * cevans - New behaviour, Oct '99
  356. * A process may, via prctl(), elect to keep its capabilities when it
  357. * calls setuid() and switches away from uid==0. Both permitted and
  358. * effective sets will be retained.
  359. * Without this change, it was impossible for a daemon to drop only some
  360. * of its privilege. The call to setuid(!=0) would drop all privileges!
  361. * Keeping uid 0 is not an option because uid 0 owns too many vital
  362. * files..
  363. * Thanks to Olaf Kirch and Peter Benie for spotting this.
  364. */
  365. static inline void cap_emulate_setxuid (int old_ruid, int old_euid,
  366. int old_suid)
  367. {
  368. if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) &&
  369. (current->uid != 0 && current->euid != 0 && current->suid != 0) &&
  370. !current->keep_capabilities) {
  371. cap_clear (current->cap_permitted);
  372. cap_clear (current->cap_effective);
  373. }
  374. if (old_euid == 0 && current->euid != 0) {
  375. cap_clear (current->cap_effective);
  376. }
  377. if (old_euid != 0 && current->euid == 0) {
  378. current->cap_effective = current->cap_permitted;
  379. }
  380. }
  381. int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid,
  382. int flags)
  383. {
  384. switch (flags) {
  385. case LSM_SETID_RE:
  386. case LSM_SETID_ID:
  387. case LSM_SETID_RES:
  388. /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
  389. if (!issecure (SECURE_NO_SETUID_FIXUP)) {
  390. cap_emulate_setxuid (old_ruid, old_euid, old_suid);
  391. }
  392. break;
  393. case LSM_SETID_FS:
  394. {
  395. uid_t old_fsuid = old_ruid;
  396. /* Copied from kernel/sys.c:setfsuid. */
  397. /*
  398. * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
  399. * if not, we might be a bit too harsh here.
  400. */
  401. if (!issecure (SECURE_NO_SETUID_FIXUP)) {
  402. if (old_fsuid == 0 && current->fsuid != 0) {
  403. cap_t (current->cap_effective) &=
  404. ~CAP_FS_MASK;
  405. }
  406. if (old_fsuid != 0 && current->fsuid == 0) {
  407. cap_t (current->cap_effective) |=
  408. (cap_t (current->cap_permitted) &
  409. CAP_FS_MASK);
  410. }
  411. }
  412. break;
  413. }
  414. default:
  415. return -EINVAL;
  416. }
  417. return 0;
  418. }
  419. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  420. /*
  421. * Rationale: code calling task_setscheduler, task_setioprio, and
  422. * task_setnice, assumes that
  423. * . if capable(cap_sys_nice), then those actions should be allowed
  424. * . if not capable(cap_sys_nice), but acting on your own processes,
  425. * then those actions should be allowed
  426. * This is insufficient now since you can call code without suid, but
  427. * yet with increased caps.
  428. * So we check for increased caps on the target process.
  429. */
  430. static inline int cap_safe_nice(struct task_struct *p)
  431. {
  432. if (!cap_issubset(p->cap_permitted, current->cap_permitted) &&
  433. !__capable(current, CAP_SYS_NICE))
  434. return -EPERM;
  435. return 0;
  436. }
  437. int cap_task_setscheduler (struct task_struct *p, int policy,
  438. struct sched_param *lp)
  439. {
  440. return cap_safe_nice(p);
  441. }
  442. int cap_task_setioprio (struct task_struct *p, int ioprio)
  443. {
  444. return cap_safe_nice(p);
  445. }
  446. int cap_task_setnice (struct task_struct *p, int nice)
  447. {
  448. return cap_safe_nice(p);
  449. }
  450. int cap_task_kill(struct task_struct *p, struct siginfo *info,
  451. int sig, u32 secid)
  452. {
  453. if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info)))
  454. return 0;
  455. /*
  456. * Running a setuid root program raises your capabilities.
  457. * Killing your own setuid root processes was previously
  458. * allowed.
  459. * We must preserve legacy signal behavior in this case.
  460. */
  461. if (p->euid == 0 && p->uid == current->uid)
  462. return 0;
  463. /* sigcont is permitted within same session */
  464. if (sig == SIGCONT && (task_session_nr(current) == task_session_nr(p)))
  465. return 0;
  466. if (secid)
  467. /*
  468. * Signal sent as a particular user.
  469. * Capabilities are ignored. May be wrong, but it's the
  470. * only thing we can do at the moment.
  471. * Used only by usb drivers?
  472. */
  473. return 0;
  474. if (cap_issubset(p->cap_permitted, current->cap_permitted))
  475. return 0;
  476. if (capable(CAP_KILL))
  477. return 0;
  478. return -EPERM;
  479. }
  480. #else
  481. int cap_task_setscheduler (struct task_struct *p, int policy,
  482. struct sched_param *lp)
  483. {
  484. return 0;
  485. }
  486. int cap_task_setioprio (struct task_struct *p, int ioprio)
  487. {
  488. return 0;
  489. }
  490. int cap_task_setnice (struct task_struct *p, int nice)
  491. {
  492. return 0;
  493. }
  494. int cap_task_kill(struct task_struct *p, struct siginfo *info,
  495. int sig, u32 secid)
  496. {
  497. return 0;
  498. }
  499. #endif
  500. void cap_task_reparent_to_init (struct task_struct *p)
  501. {
  502. p->cap_effective = CAP_INIT_EFF_SET;
  503. p->cap_inheritable = CAP_INIT_INH_SET;
  504. p->cap_permitted = CAP_FULL_SET;
  505. p->keep_capabilities = 0;
  506. return;
  507. }
  508. int cap_syslog (int type)
  509. {
  510. if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
  511. return -EPERM;
  512. return 0;
  513. }
  514. int cap_vm_enough_memory(struct mm_struct *mm, long pages)
  515. {
  516. int cap_sys_admin = 0;
  517. if (cap_capable(current, CAP_SYS_ADMIN) == 0)
  518. cap_sys_admin = 1;
  519. return __vm_enough_memory(mm, pages, cap_sys_admin);
  520. }