commoncap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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/audit.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/security.h>
  15. #include <linux/file.h>
  16. #include <linux/mm.h>
  17. #include <linux/mman.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/swap.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/netlink.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/xattr.h>
  24. #include <linux/hugetlb.h>
  25. #include <linux/mount.h>
  26. #include <linux/sched.h>
  27. #include <linux/prctl.h>
  28. #include <linux/securebits.h>
  29. int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
  30. {
  31. NETLINK_CB(skb).eff_cap = current_cap();
  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, int audit)
  48. {
  49. /* Derived from include/linux/sched.h:capable. */
  50. if (cap_raised(tsk->cred->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_may_access(struct task_struct *child, unsigned int mode)
  61. {
  62. /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */
  63. if (cap_issubset(child->cred->cap_permitted,
  64. current->cred->cap_permitted))
  65. return 0;
  66. if (capable(CAP_SYS_PTRACE))
  67. return 0;
  68. return -EPERM;
  69. }
  70. int cap_ptrace_traceme(struct task_struct *parent)
  71. {
  72. if (cap_issubset(current->cred->cap_permitted,
  73. parent->cred->cap_permitted))
  74. return 0;
  75. if (has_capability(parent, CAP_SYS_PTRACE))
  76. return 0;
  77. return -EPERM;
  78. }
  79. int cap_capget (struct task_struct *target, kernel_cap_t *effective,
  80. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  81. {
  82. struct cred *cred = target->cred;
  83. /* Derived from kernel/capability.c:sys_capget. */
  84. *effective = cred->cap_effective;
  85. *inheritable = cred->cap_inheritable;
  86. *permitted = cred->cap_permitted;
  87. return 0;
  88. }
  89. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  90. static inline int cap_inh_is_capped(void)
  91. {
  92. /*
  93. * Return 1 if changes to the inheritable set are limited
  94. * to the old permitted set. That is, if the current task
  95. * does *not* possess the CAP_SETPCAP capability.
  96. */
  97. return (cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0);
  98. }
  99. static inline int cap_limit_ptraced_target(void) { return 1; }
  100. #else /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */
  101. static inline int cap_inh_is_capped(void) { return 1; }
  102. static inline int cap_limit_ptraced_target(void)
  103. {
  104. return !capable(CAP_SETPCAP);
  105. }
  106. #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
  107. int cap_capset_check(const kernel_cap_t *effective,
  108. const kernel_cap_t *inheritable,
  109. const kernel_cap_t *permitted)
  110. {
  111. const struct cred *cred = current->cred;
  112. if (cap_inh_is_capped()
  113. && !cap_issubset(*inheritable,
  114. cap_combine(cred->cap_inheritable,
  115. cred->cap_permitted))) {
  116. /* incapable of using this inheritable set */
  117. return -EPERM;
  118. }
  119. if (!cap_issubset(*inheritable,
  120. cap_combine(cred->cap_inheritable,
  121. cred->cap_bset))) {
  122. /* no new pI capabilities outside bounding set */
  123. return -EPERM;
  124. }
  125. /* verify restrictions on target's new Permitted set */
  126. if (!cap_issubset (*permitted,
  127. cap_combine (cred->cap_permitted,
  128. cred->cap_permitted))) {
  129. return -EPERM;
  130. }
  131. /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
  132. if (!cap_issubset (*effective, *permitted)) {
  133. return -EPERM;
  134. }
  135. return 0;
  136. }
  137. void cap_capset_set(const kernel_cap_t *effective,
  138. const kernel_cap_t *inheritable,
  139. const kernel_cap_t *permitted)
  140. {
  141. struct cred *cred = current->cred;
  142. cred->cap_effective = *effective;
  143. cred->cap_inheritable = *inheritable;
  144. cred->cap_permitted = *permitted;
  145. }
  146. static inline void bprm_clear_caps(struct linux_binprm *bprm)
  147. {
  148. cap_clear(bprm->cap_post_exec_permitted);
  149. bprm->cap_effective = false;
  150. }
  151. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  152. int cap_inode_need_killpriv(struct dentry *dentry)
  153. {
  154. struct inode *inode = dentry->d_inode;
  155. int error;
  156. if (!inode->i_op || !inode->i_op->getxattr)
  157. return 0;
  158. error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
  159. if (error <= 0)
  160. return 0;
  161. return 1;
  162. }
  163. int cap_inode_killpriv(struct dentry *dentry)
  164. {
  165. struct inode *inode = dentry->d_inode;
  166. if (!inode->i_op || !inode->i_op->removexattr)
  167. return 0;
  168. return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
  169. }
  170. static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
  171. struct linux_binprm *bprm)
  172. {
  173. unsigned i;
  174. int ret = 0;
  175. if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
  176. bprm->cap_effective = true;
  177. else
  178. bprm->cap_effective = false;
  179. CAP_FOR_EACH_U32(i) {
  180. __u32 permitted = caps->permitted.cap[i];
  181. __u32 inheritable = caps->inheritable.cap[i];
  182. /*
  183. * pP' = (X & fP) | (pI & fI)
  184. */
  185. bprm->cap_post_exec_permitted.cap[i] =
  186. (current->cred->cap_bset.cap[i] & permitted) |
  187. (current->cred->cap_inheritable.cap[i] & inheritable);
  188. if (permitted & ~bprm->cap_post_exec_permitted.cap[i]) {
  189. /*
  190. * insufficient to execute correctly
  191. */
  192. ret = -EPERM;
  193. }
  194. }
  195. /*
  196. * For legacy apps, with no internal support for recognizing they
  197. * do not have enough capabilities, we return an error if they are
  198. * missing some "forced" (aka file-permitted) capabilities.
  199. */
  200. return bprm->cap_effective ? ret : 0;
  201. }
  202. int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
  203. {
  204. struct inode *inode = dentry->d_inode;
  205. __u32 magic_etc;
  206. unsigned tocopy, i;
  207. int size;
  208. struct vfs_cap_data caps;
  209. memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
  210. if (!inode || !inode->i_op || !inode->i_op->getxattr)
  211. return -ENODATA;
  212. size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
  213. XATTR_CAPS_SZ);
  214. if (size == -ENODATA || size == -EOPNOTSUPP) {
  215. /* no data, that's ok */
  216. return -ENODATA;
  217. }
  218. if (size < 0)
  219. return size;
  220. if (size < sizeof(magic_etc))
  221. return -EINVAL;
  222. cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
  223. switch ((magic_etc & VFS_CAP_REVISION_MASK)) {
  224. case VFS_CAP_REVISION_1:
  225. if (size != XATTR_CAPS_SZ_1)
  226. return -EINVAL;
  227. tocopy = VFS_CAP_U32_1;
  228. break;
  229. case VFS_CAP_REVISION_2:
  230. if (size != XATTR_CAPS_SZ_2)
  231. return -EINVAL;
  232. tocopy = VFS_CAP_U32_2;
  233. break;
  234. default:
  235. return -EINVAL;
  236. }
  237. CAP_FOR_EACH_U32(i) {
  238. if (i >= tocopy)
  239. break;
  240. cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
  241. cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
  242. }
  243. return 0;
  244. }
  245. /* Locate any VFS capabilities: */
  246. static int get_file_caps(struct linux_binprm *bprm)
  247. {
  248. struct dentry *dentry;
  249. int rc = 0;
  250. struct cpu_vfs_cap_data vcaps;
  251. bprm_clear_caps(bprm);
  252. if (!file_caps_enabled)
  253. return 0;
  254. if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
  255. return 0;
  256. dentry = dget(bprm->file->f_dentry);
  257. rc = get_vfs_caps_from_disk(dentry, &vcaps);
  258. if (rc < 0) {
  259. if (rc == -EINVAL)
  260. printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
  261. __func__, rc, bprm->filename);
  262. else if (rc == -ENODATA)
  263. rc = 0;
  264. goto out;
  265. }
  266. rc = bprm_caps_from_vfs_caps(&vcaps, bprm);
  267. out:
  268. dput(dentry);
  269. if (rc)
  270. bprm_clear_caps(bprm);
  271. return rc;
  272. }
  273. #else
  274. int cap_inode_need_killpriv(struct dentry *dentry)
  275. {
  276. return 0;
  277. }
  278. int cap_inode_killpriv(struct dentry *dentry)
  279. {
  280. return 0;
  281. }
  282. static inline int get_file_caps(struct linux_binprm *bprm)
  283. {
  284. bprm_clear_caps(bprm);
  285. return 0;
  286. }
  287. #endif
  288. int cap_bprm_set_security (struct linux_binprm *bprm)
  289. {
  290. int ret;
  291. ret = get_file_caps(bprm);
  292. if (!issecure(SECURE_NOROOT)) {
  293. /*
  294. * To support inheritance of root-permissions and suid-root
  295. * executables under compatibility mode, we override the
  296. * capability sets for the file.
  297. *
  298. * If only the real uid is 0, we do not set the effective
  299. * bit.
  300. */
  301. if (bprm->e_uid == 0 || current_uid() == 0) {
  302. /* pP' = (cap_bset & ~0) | (pI & ~0) */
  303. bprm->cap_post_exec_permitted = cap_combine(
  304. current->cred->cap_bset,
  305. current->cred->cap_inheritable);
  306. bprm->cap_effective = (bprm->e_uid == 0);
  307. ret = 0;
  308. }
  309. }
  310. return ret;
  311. }
  312. void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
  313. {
  314. struct cred *cred = current->cred;
  315. if (bprm->e_uid != cred->uid || bprm->e_gid != cred->gid ||
  316. !cap_issubset(bprm->cap_post_exec_permitted,
  317. cred->cap_permitted)) {
  318. set_dumpable(current->mm, suid_dumpable);
  319. current->pdeath_signal = 0;
  320. if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
  321. if (!capable(CAP_SETUID)) {
  322. bprm->e_uid = cred->uid;
  323. bprm->e_gid = cred->gid;
  324. }
  325. if (cap_limit_ptraced_target()) {
  326. bprm->cap_post_exec_permitted = cap_intersect(
  327. bprm->cap_post_exec_permitted,
  328. cred->cap_permitted);
  329. }
  330. }
  331. }
  332. cred->suid = cred->euid = cred->fsuid = bprm->e_uid;
  333. cred->sgid = cred->egid = cred->fsgid = bprm->e_gid;
  334. /* For init, we want to retain the capabilities set
  335. * in the init_task struct. Thus we skip the usual
  336. * capability rules */
  337. if (!is_global_init(current)) {
  338. cred->cap_permitted = bprm->cap_post_exec_permitted;
  339. if (bprm->cap_effective)
  340. cred->cap_effective = bprm->cap_post_exec_permitted;
  341. else
  342. cap_clear(cred->cap_effective);
  343. }
  344. /*
  345. * Audit candidate if current->cap_effective is set
  346. *
  347. * We do not bother to audit if 3 things are true:
  348. * 1) cap_effective has all caps
  349. * 2) we are root
  350. * 3) root is supposed to have all caps (SECURE_NOROOT)
  351. * Since this is just a normal root execing a process.
  352. *
  353. * Number 1 above might fail if you don't have a full bset, but I think
  354. * that is interesting information to audit.
  355. */
  356. if (!cap_isclear(cred->cap_effective)) {
  357. if (!cap_issubset(CAP_FULL_SET, cred->cap_effective) ||
  358. (bprm->e_uid != 0) || (cred->uid != 0) ||
  359. issecure(SECURE_NOROOT))
  360. audit_log_bprm_fcaps(bprm, &cred->cap_permitted,
  361. &cred->cap_effective);
  362. }
  363. cred->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
  364. }
  365. int cap_bprm_secureexec (struct linux_binprm *bprm)
  366. {
  367. const struct cred *cred = current->cred;
  368. if (cred->uid != 0) {
  369. if (bprm->cap_effective)
  370. return 1;
  371. if (!cap_isclear(bprm->cap_post_exec_permitted))
  372. return 1;
  373. }
  374. return (cred->euid != cred->uid ||
  375. cred->egid != cred->gid);
  376. }
  377. int cap_inode_setxattr(struct dentry *dentry, const char *name,
  378. const void *value, size_t size, int flags)
  379. {
  380. if (!strcmp(name, XATTR_NAME_CAPS)) {
  381. if (!capable(CAP_SETFCAP))
  382. return -EPERM;
  383. return 0;
  384. } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
  385. sizeof(XATTR_SECURITY_PREFIX) - 1) &&
  386. !capable(CAP_SYS_ADMIN))
  387. return -EPERM;
  388. return 0;
  389. }
  390. int cap_inode_removexattr(struct dentry *dentry, const char *name)
  391. {
  392. if (!strcmp(name, XATTR_NAME_CAPS)) {
  393. if (!capable(CAP_SETFCAP))
  394. return -EPERM;
  395. return 0;
  396. } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
  397. sizeof(XATTR_SECURITY_PREFIX) - 1) &&
  398. !capable(CAP_SYS_ADMIN))
  399. return -EPERM;
  400. return 0;
  401. }
  402. /* moved from kernel/sys.c. */
  403. /*
  404. * cap_emulate_setxuid() fixes the effective / permitted capabilities of
  405. * a process after a call to setuid, setreuid, or setresuid.
  406. *
  407. * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
  408. * {r,e,s}uid != 0, the permitted and effective capabilities are
  409. * cleared.
  410. *
  411. * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
  412. * capabilities of the process are cleared.
  413. *
  414. * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
  415. * capabilities are set to the permitted capabilities.
  416. *
  417. * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
  418. * never happen.
  419. *
  420. * -astor
  421. *
  422. * cevans - New behaviour, Oct '99
  423. * A process may, via prctl(), elect to keep its capabilities when it
  424. * calls setuid() and switches away from uid==0. Both permitted and
  425. * effective sets will be retained.
  426. * Without this change, it was impossible for a daemon to drop only some
  427. * of its privilege. The call to setuid(!=0) would drop all privileges!
  428. * Keeping uid 0 is not an option because uid 0 owns too many vital
  429. * files..
  430. * Thanks to Olaf Kirch and Peter Benie for spotting this.
  431. */
  432. static inline void cap_emulate_setxuid (int old_ruid, int old_euid,
  433. int old_suid)
  434. {
  435. struct cred *cred = current->cred;
  436. if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) &&
  437. (cred->uid != 0 && cred->euid != 0 && cred->suid != 0) &&
  438. !issecure(SECURE_KEEP_CAPS)) {
  439. cap_clear (cred->cap_permitted);
  440. cap_clear (cred->cap_effective);
  441. }
  442. if (old_euid == 0 && cred->euid != 0) {
  443. cap_clear (cred->cap_effective);
  444. }
  445. if (old_euid != 0 && cred->euid == 0) {
  446. cred->cap_effective = cred->cap_permitted;
  447. }
  448. }
  449. int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid,
  450. int flags)
  451. {
  452. struct cred *cred = current->cred;
  453. switch (flags) {
  454. case LSM_SETID_RE:
  455. case LSM_SETID_ID:
  456. case LSM_SETID_RES:
  457. /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
  458. if (!issecure (SECURE_NO_SETUID_FIXUP)) {
  459. cap_emulate_setxuid (old_ruid, old_euid, old_suid);
  460. }
  461. break;
  462. case LSM_SETID_FS:
  463. {
  464. uid_t old_fsuid = old_ruid;
  465. /* Copied from kernel/sys.c:setfsuid. */
  466. /*
  467. * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
  468. * if not, we might be a bit too harsh here.
  469. */
  470. if (!issecure (SECURE_NO_SETUID_FIXUP)) {
  471. if (old_fsuid == 0 && cred->fsuid != 0) {
  472. cred->cap_effective =
  473. cap_drop_fs_set(
  474. cred->cap_effective);
  475. }
  476. if (old_fsuid != 0 && cred->fsuid == 0) {
  477. cred->cap_effective =
  478. cap_raise_fs_set(
  479. cred->cap_effective,
  480. cred->cap_permitted);
  481. }
  482. }
  483. break;
  484. }
  485. default:
  486. return -EINVAL;
  487. }
  488. return 0;
  489. }
  490. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  491. /*
  492. * Rationale: code calling task_setscheduler, task_setioprio, and
  493. * task_setnice, assumes that
  494. * . if capable(cap_sys_nice), then those actions should be allowed
  495. * . if not capable(cap_sys_nice), but acting on your own processes,
  496. * then those actions should be allowed
  497. * This is insufficient now since you can call code without suid, but
  498. * yet with increased caps.
  499. * So we check for increased caps on the target process.
  500. */
  501. static int cap_safe_nice(struct task_struct *p)
  502. {
  503. if (!cap_issubset(p->cred->cap_permitted,
  504. current->cred->cap_permitted) &&
  505. !capable(CAP_SYS_NICE))
  506. return -EPERM;
  507. return 0;
  508. }
  509. int cap_task_setscheduler (struct task_struct *p, int policy,
  510. struct sched_param *lp)
  511. {
  512. return cap_safe_nice(p);
  513. }
  514. int cap_task_setioprio (struct task_struct *p, int ioprio)
  515. {
  516. return cap_safe_nice(p);
  517. }
  518. int cap_task_setnice (struct task_struct *p, int nice)
  519. {
  520. return cap_safe_nice(p);
  521. }
  522. /*
  523. * called from kernel/sys.c for prctl(PR_CABSET_DROP)
  524. * done without task_capability_lock() because it introduces
  525. * no new races - i.e. only another task doing capget() on
  526. * this task could get inconsistent info. There can be no
  527. * racing writer bc a task can only change its own caps.
  528. */
  529. static long cap_prctl_drop(unsigned long cap)
  530. {
  531. if (!capable(CAP_SETPCAP))
  532. return -EPERM;
  533. if (!cap_valid(cap))
  534. return -EINVAL;
  535. cap_lower(current->cred->cap_bset, cap);
  536. return 0;
  537. }
  538. #else
  539. int cap_task_setscheduler (struct task_struct *p, int policy,
  540. struct sched_param *lp)
  541. {
  542. return 0;
  543. }
  544. int cap_task_setioprio (struct task_struct *p, int ioprio)
  545. {
  546. return 0;
  547. }
  548. int cap_task_setnice (struct task_struct *p, int nice)
  549. {
  550. return 0;
  551. }
  552. #endif
  553. int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
  554. unsigned long arg4, unsigned long arg5, long *rc_p)
  555. {
  556. struct cred *cred = current->cred;
  557. long error = 0;
  558. switch (option) {
  559. case PR_CAPBSET_READ:
  560. if (!cap_valid(arg2))
  561. error = -EINVAL;
  562. else
  563. error = !!cap_raised(cred->cap_bset, arg2);
  564. break;
  565. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  566. case PR_CAPBSET_DROP:
  567. error = cap_prctl_drop(arg2);
  568. break;
  569. /*
  570. * The next four prctl's remain to assist with transitioning a
  571. * system from legacy UID=0 based privilege (when filesystem
  572. * capabilities are not in use) to a system using filesystem
  573. * capabilities only - as the POSIX.1e draft intended.
  574. *
  575. * Note:
  576. *
  577. * PR_SET_SECUREBITS =
  578. * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
  579. * | issecure_mask(SECURE_NOROOT)
  580. * | issecure_mask(SECURE_NOROOT_LOCKED)
  581. * | issecure_mask(SECURE_NO_SETUID_FIXUP)
  582. * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
  583. *
  584. * will ensure that the current process and all of its
  585. * children will be locked into a pure
  586. * capability-based-privilege environment.
  587. */
  588. case PR_SET_SECUREBITS:
  589. if ((((cred->securebits & SECURE_ALL_LOCKS) >> 1)
  590. & (cred->securebits ^ arg2)) /*[1]*/
  591. || ((cred->securebits & SECURE_ALL_LOCKS
  592. & ~arg2)) /*[2]*/
  593. || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
  594. || (cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0)) { /*[4]*/
  595. /*
  596. * [1] no changing of bits that are locked
  597. * [2] no unlocking of locks
  598. * [3] no setting of unsupported bits
  599. * [4] doing anything requires privilege (go read about
  600. * the "sendmail capabilities bug")
  601. */
  602. error = -EPERM; /* cannot change a locked bit */
  603. } else {
  604. cred->securebits = arg2;
  605. }
  606. break;
  607. case PR_GET_SECUREBITS:
  608. error = cred->securebits;
  609. break;
  610. #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
  611. case PR_GET_KEEPCAPS:
  612. if (issecure(SECURE_KEEP_CAPS))
  613. error = 1;
  614. break;
  615. case PR_SET_KEEPCAPS:
  616. if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
  617. error = -EINVAL;
  618. else if (issecure(SECURE_KEEP_CAPS_LOCKED))
  619. error = -EPERM;
  620. else if (arg2)
  621. cred->securebits |= issecure_mask(SECURE_KEEP_CAPS);
  622. else
  623. cred->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
  624. break;
  625. default:
  626. /* No functionality available - continue with default */
  627. return 0;
  628. }
  629. /* Functionality provided */
  630. *rc_p = error;
  631. return 1;
  632. }
  633. void cap_task_reparent_to_init (struct task_struct *p)
  634. {
  635. struct cred *cred = p->cred;
  636. cap_set_init_eff(cred->cap_effective);
  637. cap_clear(cred->cap_inheritable);
  638. cap_set_full(cred->cap_permitted);
  639. p->cred->securebits = SECUREBITS_DEFAULT;
  640. }
  641. int cap_syslog (int type)
  642. {
  643. if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
  644. return -EPERM;
  645. return 0;
  646. }
  647. int cap_vm_enough_memory(struct mm_struct *mm, long pages)
  648. {
  649. int cap_sys_admin = 0;
  650. if (cap_capable(current, CAP_SYS_ADMIN, SECURITY_CAP_NOAUDIT) == 0)
  651. cap_sys_admin = 1;
  652. return __vm_enough_memory(mm, pages, cap_sys_admin);
  653. }