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