commoncap.c 15 KB

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