commoncap.c 18 KB

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