commoncap.c 19 KB

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