commoncap.c 15 KB

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