commoncap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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_effective;
  32. return 0;
  33. }
  34. int cap_netlink_recv(struct sk_buff *skb, int cap)
  35. {
  36. if (!cap_raised(NETLINK_CB(skb).eff_cap, cap))
  37. return -EPERM;
  38. return 0;
  39. }
  40. EXPORT_SYMBOL(cap_netlink_recv);
  41. /*
  42. * NOTE WELL: cap_capable() cannot be used like the kernel's capable()
  43. * function. That is, it has the reverse semantics: cap_capable()
  44. * returns 0 when a task has a capability, but the kernel's capable()
  45. * returns 1 for this case.
  46. */
  47. int cap_capable(struct task_struct *tsk, int cap, int audit)
  48. {
  49. /* Derived from include/linux/sched.h:capable. */
  50. if (cap_raised(tsk->cap_effective, cap))
  51. return 0;
  52. return -EPERM;
  53. }
  54. int cap_settime(struct timespec *ts, struct timezone *tz)
  55. {
  56. if (!capable(CAP_SYS_TIME))
  57. return -EPERM;
  58. return 0;
  59. }
  60. int cap_ptrace_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->cap_permitted, current->cap_permitted))
  64. return 0;
  65. if (capable(CAP_SYS_PTRACE))
  66. return 0;
  67. return -EPERM;
  68. }
  69. int cap_ptrace_traceme(struct task_struct *parent)
  70. {
  71. /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */
  72. if (cap_issubset(current->cap_permitted, parent->cap_permitted))
  73. return 0;
  74. if (has_capability(parent, CAP_SYS_PTRACE))
  75. return 0;
  76. return -EPERM;
  77. }
  78. int cap_capget (struct task_struct *target, kernel_cap_t *effective,
  79. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  80. {
  81. /* Derived from kernel/capability.c:sys_capget. */
  82. *effective = target->cap_effective;
  83. *inheritable = target->cap_inheritable;
  84. *permitted = target->cap_permitted;
  85. return 0;
  86. }
  87. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  88. static inline int cap_block_setpcap(struct task_struct *target)
  89. {
  90. /*
  91. * No support for remote process capability manipulation with
  92. * filesystem capability support.
  93. */
  94. return (target != current);
  95. }
  96. static inline int cap_inh_is_capped(void)
  97. {
  98. /*
  99. * Return 1 if changes to the inheritable set are limited
  100. * to the old permitted set. That is, if the current task
  101. * does *not* possess the CAP_SETPCAP capability.
  102. */
  103. return (cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0);
  104. }
  105. static inline int cap_limit_ptraced_target(void) { return 1; }
  106. #else /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */
  107. static inline int cap_block_setpcap(struct task_struct *t) { return 0; }
  108. static inline int cap_inh_is_capped(void) { return 1; }
  109. static inline int cap_limit_ptraced_target(void)
  110. {
  111. return !capable(CAP_SETPCAP);
  112. }
  113. #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
  114. int cap_capset_check (struct task_struct *target, kernel_cap_t *effective,
  115. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  116. {
  117. if (cap_block_setpcap(target)) {
  118. return -EPERM;
  119. }
  120. if (cap_inh_is_capped()
  121. && !cap_issubset(*inheritable,
  122. cap_combine(target->cap_inheritable,
  123. current->cap_permitted))) {
  124. /* incapable of using this inheritable set */
  125. return -EPERM;
  126. }
  127. if (!cap_issubset(*inheritable,
  128. cap_combine(target->cap_inheritable,
  129. current->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 (target->cap_permitted,
  136. current->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 (struct task_struct *target, kernel_cap_t *effective,
  146. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  147. {
  148. target->cap_effective = *effective;
  149. target->cap_inheritable = *inheritable;
  150. target->cap_permitted = *permitted;
  151. }
  152. static inline void bprm_clear_caps(struct linux_binprm *bprm)
  153. {
  154. cap_clear(bprm->cap_post_exec_permitted);
  155. bprm->cap_effective = false;
  156. }
  157. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  158. int cap_inode_need_killpriv(struct dentry *dentry)
  159. {
  160. struct inode *inode = dentry->d_inode;
  161. int error;
  162. if (!inode->i_op || !inode->i_op->getxattr)
  163. return 0;
  164. error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
  165. if (error <= 0)
  166. return 0;
  167. return 1;
  168. }
  169. int cap_inode_killpriv(struct dentry *dentry)
  170. {
  171. struct inode *inode = dentry->d_inode;
  172. if (!inode->i_op || !inode->i_op->removexattr)
  173. return 0;
  174. return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
  175. }
  176. static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
  177. struct linux_binprm *bprm)
  178. {
  179. unsigned i;
  180. int ret = 0;
  181. if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
  182. bprm->cap_effective = true;
  183. else
  184. bprm->cap_effective = false;
  185. CAP_FOR_EACH_U32(i) {
  186. __u32 permitted = caps->permitted.cap[i];
  187. __u32 inheritable = caps->inheritable.cap[i];
  188. /*
  189. * pP' = (X & fP) | (pI & fI)
  190. */
  191. bprm->cap_post_exec_permitted.cap[i] =
  192. (current->cap_bset.cap[i] & permitted) |
  193. (current->cap_inheritable.cap[i] & inheritable);
  194. if (permitted & ~bprm->cap_post_exec_permitted.cap[i]) {
  195. /*
  196. * insufficient to execute correctly
  197. */
  198. ret = -EPERM;
  199. }
  200. }
  201. /*
  202. * For legacy apps, with no internal support for recognizing they
  203. * do not have enough capabilities, we return an error if they are
  204. * missing some "forced" (aka file-permitted) capabilities.
  205. */
  206. return bprm->cap_effective ? ret : 0;
  207. }
  208. int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
  209. {
  210. struct inode *inode = dentry->d_inode;
  211. __u32 magic_etc;
  212. unsigned tocopy, i;
  213. int size;
  214. struct vfs_cap_data caps;
  215. memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
  216. if (!inode || !inode->i_op || !inode->i_op->getxattr)
  217. return -ENODATA;
  218. size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
  219. XATTR_CAPS_SZ);
  220. if (size == -ENODATA || size == -EOPNOTSUPP) {
  221. /* no data, that's ok */
  222. return -ENODATA;
  223. }
  224. if (size < 0)
  225. return size;
  226. if (size < sizeof(magic_etc))
  227. return -EINVAL;
  228. cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
  229. switch ((magic_etc & VFS_CAP_REVISION_MASK)) {
  230. case VFS_CAP_REVISION_1:
  231. if (size != XATTR_CAPS_SZ_1)
  232. return -EINVAL;
  233. tocopy = VFS_CAP_U32_1;
  234. break;
  235. case VFS_CAP_REVISION_2:
  236. if (size != XATTR_CAPS_SZ_2)
  237. return -EINVAL;
  238. tocopy = VFS_CAP_U32_2;
  239. break;
  240. default:
  241. return -EINVAL;
  242. }
  243. CAP_FOR_EACH_U32(i) {
  244. if (i >= tocopy)
  245. break;
  246. cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
  247. cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
  248. }
  249. return 0;
  250. }
  251. /* Locate any VFS capabilities: */
  252. static int get_file_caps(struct linux_binprm *bprm)
  253. {
  254. struct dentry *dentry;
  255. int rc = 0;
  256. struct cpu_vfs_cap_data vcaps;
  257. bprm_clear_caps(bprm);
  258. if (!file_caps_enabled)
  259. return 0;
  260. if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
  261. return 0;
  262. dentry = dget(bprm->file->f_dentry);
  263. rc = get_vfs_caps_from_disk(dentry, &vcaps);
  264. if (rc < 0) {
  265. if (rc == -EINVAL)
  266. printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
  267. __func__, rc, bprm->filename);
  268. else if (rc == -ENODATA)
  269. rc = 0;
  270. goto out;
  271. }
  272. rc = bprm_caps_from_vfs_caps(&vcaps, bprm);
  273. out:
  274. dput(dentry);
  275. if (rc)
  276. bprm_clear_caps(bprm);
  277. return rc;
  278. }
  279. #else
  280. int cap_inode_need_killpriv(struct dentry *dentry)
  281. {
  282. return 0;
  283. }
  284. int cap_inode_killpriv(struct dentry *dentry)
  285. {
  286. return 0;
  287. }
  288. static inline int get_file_caps(struct linux_binprm *bprm)
  289. {
  290. bprm_clear_caps(bprm);
  291. return 0;
  292. }
  293. #endif
  294. int cap_bprm_set_security (struct linux_binprm *bprm)
  295. {
  296. int ret;
  297. ret = get_file_caps(bprm);
  298. if (!issecure(SECURE_NOROOT)) {
  299. /*
  300. * To support inheritance of root-permissions and suid-root
  301. * executables under compatibility mode, we override the
  302. * capability sets for the file.
  303. *
  304. * If only the real uid is 0, we do not set the effective
  305. * bit.
  306. */
  307. if (bprm->e_uid == 0 || current_uid() == 0) {
  308. /* pP' = (cap_bset & ~0) | (pI & ~0) */
  309. bprm->cap_post_exec_permitted = cap_combine(
  310. current->cap_bset, current->cap_inheritable
  311. );
  312. bprm->cap_effective = (bprm->e_uid == 0);
  313. ret = 0;
  314. }
  315. }
  316. return ret;
  317. }
  318. void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
  319. {
  320. kernel_cap_t pP = current->cap_permitted;
  321. kernel_cap_t pE = current->cap_effective;
  322. uid_t uid;
  323. gid_t gid;
  324. current_uid_gid(&uid, &gid);
  325. if (bprm->e_uid != uid || bprm->e_gid != gid ||
  326. !cap_issubset(bprm->cap_post_exec_permitted,
  327. current->cap_permitted)) {
  328. set_dumpable(current->mm, suid_dumpable);
  329. current->pdeath_signal = 0;
  330. if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
  331. if (!capable(CAP_SETUID)) {
  332. bprm->e_uid = uid;
  333. bprm->e_gid = gid;
  334. }
  335. if (cap_limit_ptraced_target()) {
  336. bprm->cap_post_exec_permitted = cap_intersect(
  337. bprm->cap_post_exec_permitted,
  338. current->cap_permitted);
  339. }
  340. }
  341. }
  342. current->suid = current->euid = current->fsuid = bprm->e_uid;
  343. current->sgid = current->egid = current->fsgid = bprm->e_gid;
  344. /* For init, we want to retain the capabilities set
  345. * in the init_task struct. Thus we skip the usual
  346. * capability rules */
  347. if (!is_global_init(current)) {
  348. current->cap_permitted = bprm->cap_post_exec_permitted;
  349. if (bprm->cap_effective)
  350. current->cap_effective = bprm->cap_post_exec_permitted;
  351. else
  352. cap_clear(current->cap_effective);
  353. }
  354. /*
  355. * Audit candidate if current->cap_effective is set
  356. *
  357. * We do not bother to audit if 3 things are true:
  358. * 1) cap_effective has all caps
  359. * 2) we are root
  360. * 3) root is supposed to have all caps (SECURE_NOROOT)
  361. * Since this is just a normal root execing a process.
  362. *
  363. * Number 1 above might fail if you don't have a full bset, but I think
  364. * that is interesting information to audit.
  365. */
  366. if (!cap_isclear(current->cap_effective)) {
  367. if (!cap_issubset(CAP_FULL_SET, current->cap_effective) ||
  368. (bprm->e_uid != 0) || (current->uid != 0) ||
  369. issecure(SECURE_NOROOT))
  370. audit_log_bprm_fcaps(bprm, &pP, &pE);
  371. }
  372. current->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
  373. }
  374. int cap_bprm_secureexec (struct linux_binprm *bprm)
  375. {
  376. if (current_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 (current_euid() != current_uid() ||
  383. current_egid() != current_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. uid_t euid = current_euid();
  444. if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) &&
  445. (current_uid() != 0 && euid != 0 && current_suid() != 0) &&
  446. !issecure(SECURE_KEEP_CAPS)) {
  447. cap_clear (current->cap_permitted);
  448. cap_clear (current->cap_effective);
  449. }
  450. if (old_euid == 0 && euid != 0) {
  451. cap_clear (current->cap_effective);
  452. }
  453. if (old_euid != 0 && euid == 0) {
  454. current->cap_effective = current->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. switch (flags) {
  461. case LSM_SETID_RE:
  462. case LSM_SETID_ID:
  463. case LSM_SETID_RES:
  464. /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
  465. if (!issecure (SECURE_NO_SETUID_FIXUP)) {
  466. cap_emulate_setxuid (old_ruid, old_euid, old_suid);
  467. }
  468. break;
  469. case LSM_SETID_FS:
  470. {
  471. uid_t old_fsuid = old_ruid;
  472. /* Copied from kernel/sys.c:setfsuid. */
  473. /*
  474. * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
  475. * if not, we might be a bit too harsh here.
  476. */
  477. if (!issecure (SECURE_NO_SETUID_FIXUP)) {
  478. if (old_fsuid == 0 && current_fsuid() != 0) {
  479. current->cap_effective =
  480. cap_drop_fs_set(
  481. current->cap_effective);
  482. }
  483. if (old_fsuid != 0 && current_fsuid() == 0) {
  484. current->cap_effective =
  485. cap_raise_fs_set(
  486. current->cap_effective,
  487. current->cap_permitted);
  488. }
  489. }
  490. break;
  491. }
  492. default:
  493. return -EINVAL;
  494. }
  495. return 0;
  496. }
  497. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  498. /*
  499. * Rationale: code calling task_setscheduler, task_setioprio, and
  500. * task_setnice, assumes that
  501. * . if capable(cap_sys_nice), then those actions should be allowed
  502. * . if not capable(cap_sys_nice), but acting on your own processes,
  503. * then those actions should be allowed
  504. * This is insufficient now since you can call code without suid, but
  505. * yet with increased caps.
  506. * So we check for increased caps on the target process.
  507. */
  508. static int cap_safe_nice(struct task_struct *p)
  509. {
  510. if (!cap_issubset(p->cap_permitted, current->cap_permitted) &&
  511. !capable(CAP_SYS_NICE))
  512. return -EPERM;
  513. return 0;
  514. }
  515. int cap_task_setscheduler (struct task_struct *p, int policy,
  516. struct sched_param *lp)
  517. {
  518. return cap_safe_nice(p);
  519. }
  520. int cap_task_setioprio (struct task_struct *p, int ioprio)
  521. {
  522. return cap_safe_nice(p);
  523. }
  524. int cap_task_setnice (struct task_struct *p, int nice)
  525. {
  526. return cap_safe_nice(p);
  527. }
  528. /*
  529. * called from kernel/sys.c for prctl(PR_CABSET_DROP)
  530. * done without task_capability_lock() because it introduces
  531. * no new races - i.e. only another task doing capget() on
  532. * this task could get inconsistent info. There can be no
  533. * racing writer bc a task can only change its own caps.
  534. */
  535. static long cap_prctl_drop(unsigned long cap)
  536. {
  537. if (!capable(CAP_SETPCAP))
  538. return -EPERM;
  539. if (!cap_valid(cap))
  540. return -EINVAL;
  541. cap_lower(current->cap_bset, cap);
  542. return 0;
  543. }
  544. #else
  545. int cap_task_setscheduler (struct task_struct *p, int policy,
  546. struct sched_param *lp)
  547. {
  548. return 0;
  549. }
  550. int cap_task_setioprio (struct task_struct *p, int ioprio)
  551. {
  552. return 0;
  553. }
  554. int cap_task_setnice (struct task_struct *p, int nice)
  555. {
  556. return 0;
  557. }
  558. #endif
  559. int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
  560. unsigned long arg4, unsigned long arg5, long *rc_p)
  561. {
  562. long error = 0;
  563. switch (option) {
  564. case PR_CAPBSET_READ:
  565. if (!cap_valid(arg2))
  566. error = -EINVAL;
  567. else
  568. error = !!cap_raised(current->cap_bset, arg2);
  569. break;
  570. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  571. case PR_CAPBSET_DROP:
  572. error = cap_prctl_drop(arg2);
  573. break;
  574. /*
  575. * The next four prctl's remain to assist with transitioning a
  576. * system from legacy UID=0 based privilege (when filesystem
  577. * capabilities are not in use) to a system using filesystem
  578. * capabilities only - as the POSIX.1e draft intended.
  579. *
  580. * Note:
  581. *
  582. * PR_SET_SECUREBITS =
  583. * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
  584. * | issecure_mask(SECURE_NOROOT)
  585. * | issecure_mask(SECURE_NOROOT_LOCKED)
  586. * | issecure_mask(SECURE_NO_SETUID_FIXUP)
  587. * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
  588. *
  589. * will ensure that the current process and all of its
  590. * children will be locked into a pure
  591. * capability-based-privilege environment.
  592. */
  593. case PR_SET_SECUREBITS:
  594. if ((((current->securebits & SECURE_ALL_LOCKS) >> 1)
  595. & (current->securebits ^ arg2)) /*[1]*/
  596. || ((current->securebits & SECURE_ALL_LOCKS
  597. & ~arg2)) /*[2]*/
  598. || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
  599. || (cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0)) { /*[4]*/
  600. /*
  601. * [1] no changing of bits that are locked
  602. * [2] no unlocking of locks
  603. * [3] no setting of unsupported bits
  604. * [4] doing anything requires privilege (go read about
  605. * the "sendmail capabilities bug")
  606. */
  607. error = -EPERM; /* cannot change a locked bit */
  608. } else {
  609. current->securebits = arg2;
  610. }
  611. break;
  612. case PR_GET_SECUREBITS:
  613. error = current->securebits;
  614. break;
  615. #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
  616. case PR_GET_KEEPCAPS:
  617. if (issecure(SECURE_KEEP_CAPS))
  618. error = 1;
  619. break;
  620. case PR_SET_KEEPCAPS:
  621. if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
  622. error = -EINVAL;
  623. else if (issecure(SECURE_KEEP_CAPS_LOCKED))
  624. error = -EPERM;
  625. else if (arg2)
  626. current->securebits |= issecure_mask(SECURE_KEEP_CAPS);
  627. else
  628. current->securebits &=
  629. ~issecure_mask(SECURE_KEEP_CAPS);
  630. break;
  631. default:
  632. /* No functionality available - continue with default */
  633. return 0;
  634. }
  635. /* Functionality provided */
  636. *rc_p = error;
  637. return 1;
  638. }
  639. void cap_task_reparent_to_init (struct task_struct *p)
  640. {
  641. cap_set_init_eff(p->cap_effective);
  642. cap_clear(p->cap_inheritable);
  643. cap_set_full(p->cap_permitted);
  644. p->securebits = SECUREBITS_DEFAULT;
  645. return;
  646. }
  647. int cap_syslog (int type)
  648. {
  649. if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
  650. return -EPERM;
  651. return 0;
  652. }
  653. int cap_vm_enough_memory(struct mm_struct *mm, long pages)
  654. {
  655. int cap_sys_admin = 0;
  656. if (cap_capable(current, CAP_SYS_ADMIN, SECURITY_CAP_NOAUDIT) == 0)
  657. cap_sys_admin = 1;
  658. return __vm_enough_memory(mm, pages, cap_sys_admin);
  659. }