commoncap.c 18 KB

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