1 /* Common capabilities, needed by capability.o and root_plug.o
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.
10 #include <linux/capability.h>
11 #include <linux/audit.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/security.h>
16 #include <linux/file.h>
18 #include <linux/mman.h>
19 #include <linux/pagemap.h>
20 #include <linux/swap.h>
21 #include <linux/skbuff.h>
22 #include <linux/netlink.h>
23 #include <linux/ptrace.h>
24 #include <linux/xattr.h>
25 #include <linux/hugetlb.h>
26 #include <linux/mount.h>
27 #include <linux/sched.h>
28 #include <linux/prctl.h>
29 #include <linux/securebits.h>
31 int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
33 NETLINK_CB(skb).eff_cap = current_cap();
37 int cap_netlink_recv(struct sk_buff *skb, int cap)
39 if (!cap_raised(NETLINK_CB(skb).eff_cap, cap))
44 EXPORT_SYMBOL(cap_netlink_recv);
47 * NOTE WELL: cap_capable() cannot be used like the kernel's capable()
48 * function. That is, it has the reverse semantics: cap_capable()
49 * returns 0 when a task has a capability, but the kernel's capable()
50 * returns 1 for this case.
52 int cap_capable(struct task_struct *tsk, int cap, int audit)
54 /* Derived from include/linux/sched.h:capable. */
55 if (cap_raised(tsk->cred->cap_effective, cap))
60 int cap_settime(struct timespec *ts, struct timezone *tz)
62 if (!capable(CAP_SYS_TIME))
67 int cap_ptrace_may_access(struct task_struct *child, unsigned int mode)
69 /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */
70 if (cap_issubset(child->cred->cap_permitted,
71 current->cred->cap_permitted))
73 if (capable(CAP_SYS_PTRACE))
78 int cap_ptrace_traceme(struct task_struct *parent)
80 if (cap_issubset(current->cred->cap_permitted,
81 parent->cred->cap_permitted))
83 if (has_capability(parent, CAP_SYS_PTRACE))
88 int cap_capget (struct task_struct *target, kernel_cap_t *effective,
89 kernel_cap_t *inheritable, kernel_cap_t *permitted)
91 struct cred *cred = target->cred;
93 /* Derived from kernel/capability.c:sys_capget. */
94 *effective = cred->cap_effective;
95 *inheritable = cred->cap_inheritable;
96 *permitted = cred->cap_permitted;
100 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
102 static inline int cap_inh_is_capped(void)
105 * Return 1 if changes to the inheritable set are limited
106 * to the old permitted set. That is, if the current task
107 * does *not* possess the CAP_SETPCAP capability.
109 return (cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0);
112 static inline int cap_limit_ptraced_target(void) { return 1; }
114 #else /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */
116 static inline int cap_inh_is_capped(void) { return 1; }
117 static inline int cap_limit_ptraced_target(void)
119 return !capable(CAP_SETPCAP);
122 #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
124 int cap_capset_check(const kernel_cap_t *effective,
125 const kernel_cap_t *inheritable,
126 const kernel_cap_t *permitted)
128 const struct cred *cred = current->cred;
130 if (cap_inh_is_capped()
131 && !cap_issubset(*inheritable,
132 cap_combine(cred->cap_inheritable,
133 cred->cap_permitted))) {
134 /* incapable of using this inheritable set */
137 if (!cap_issubset(*inheritable,
138 cap_combine(cred->cap_inheritable,
140 /* no new pI capabilities outside bounding set */
144 /* verify restrictions on target's new Permitted set */
145 if (!cap_issubset (*permitted,
146 cap_combine (cred->cap_permitted,
147 cred->cap_permitted))) {
151 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
152 if (!cap_issubset (*effective, *permitted)) {
159 void cap_capset_set(const kernel_cap_t *effective,
160 const kernel_cap_t *inheritable,
161 const kernel_cap_t *permitted)
163 struct cred *cred = current->cred;
165 cred->cap_effective = *effective;
166 cred->cap_inheritable = *inheritable;
167 cred->cap_permitted = *permitted;
170 static inline void bprm_clear_caps(struct linux_binprm *bprm)
172 cap_clear(bprm->cap_post_exec_permitted);
173 bprm->cap_effective = false;
176 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
178 int cap_inode_need_killpriv(struct dentry *dentry)
180 struct inode *inode = dentry->d_inode;
183 if (!inode->i_op || !inode->i_op->getxattr)
186 error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
192 int cap_inode_killpriv(struct dentry *dentry)
194 struct inode *inode = dentry->d_inode;
196 if (!inode->i_op || !inode->i_op->removexattr)
199 return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
202 static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
203 struct linux_binprm *bprm)
208 if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
209 bprm->cap_effective = true;
211 bprm->cap_effective = false;
213 CAP_FOR_EACH_U32(i) {
214 __u32 permitted = caps->permitted.cap[i];
215 __u32 inheritable = caps->inheritable.cap[i];
218 * pP' = (X & fP) | (pI & fI)
220 bprm->cap_post_exec_permitted.cap[i] =
221 (current->cred->cap_bset.cap[i] & permitted) |
222 (current->cred->cap_inheritable.cap[i] & inheritable);
224 if (permitted & ~bprm->cap_post_exec_permitted.cap[i]) {
226 * insufficient to execute correctly
233 * For legacy apps, with no internal support for recognizing they
234 * do not have enough capabilities, we return an error if they are
235 * missing some "forced" (aka file-permitted) capabilities.
237 return bprm->cap_effective ? ret : 0;
240 int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
242 struct inode *inode = dentry->d_inode;
246 struct vfs_cap_data caps;
248 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
250 if (!inode || !inode->i_op || !inode->i_op->getxattr)
253 size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
255 if (size == -ENODATA || size == -EOPNOTSUPP) {
256 /* no data, that's ok */
262 if (size < sizeof(magic_etc))
265 cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
267 switch ((magic_etc & VFS_CAP_REVISION_MASK)) {
268 case VFS_CAP_REVISION_1:
269 if (size != XATTR_CAPS_SZ_1)
271 tocopy = VFS_CAP_U32_1;
273 case VFS_CAP_REVISION_2:
274 if (size != XATTR_CAPS_SZ_2)
276 tocopy = VFS_CAP_U32_2;
282 CAP_FOR_EACH_U32(i) {
285 cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
286 cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
291 /* Locate any VFS capabilities: */
292 static int get_file_caps(struct linux_binprm *bprm)
294 struct dentry *dentry;
296 struct cpu_vfs_cap_data vcaps;
298 bprm_clear_caps(bprm);
300 if (!file_caps_enabled)
303 if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
306 dentry = dget(bprm->file->f_dentry);
308 rc = get_vfs_caps_from_disk(dentry, &vcaps);
311 printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
312 __func__, rc, bprm->filename);
313 else if (rc == -ENODATA)
318 rc = bprm_caps_from_vfs_caps(&vcaps, bprm);
323 bprm_clear_caps(bprm);
329 int cap_inode_need_killpriv(struct dentry *dentry)
334 int cap_inode_killpriv(struct dentry *dentry)
339 static inline int get_file_caps(struct linux_binprm *bprm)
341 bprm_clear_caps(bprm);
346 int cap_bprm_set_security (struct linux_binprm *bprm)
350 ret = get_file_caps(bprm);
352 if (!issecure(SECURE_NOROOT)) {
354 * To support inheritance of root-permissions and suid-root
355 * executables under compatibility mode, we override the
356 * capability sets for the file.
358 * If only the real uid is 0, we do not set the effective
361 if (bprm->e_uid == 0 || current_uid() == 0) {
362 /* pP' = (cap_bset & ~0) | (pI & ~0) */
363 bprm->cap_post_exec_permitted = cap_combine(
364 current->cred->cap_bset,
365 current->cred->cap_inheritable);
366 bprm->cap_effective = (bprm->e_uid == 0);
374 void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
376 struct cred *cred = current->cred;
378 if (bprm->e_uid != cred->uid || bprm->e_gid != cred->gid ||
379 !cap_issubset(bprm->cap_post_exec_permitted,
380 cred->cap_permitted)) {
381 set_dumpable(current->mm, suid_dumpable);
382 current->pdeath_signal = 0;
384 if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
385 if (!capable(CAP_SETUID)) {
386 bprm->e_uid = cred->uid;
387 bprm->e_gid = cred->gid;
389 if (cap_limit_ptraced_target()) {
390 bprm->cap_post_exec_permitted = cap_intersect(
391 bprm->cap_post_exec_permitted,
392 cred->cap_permitted);
397 cred->suid = cred->euid = cred->fsuid = bprm->e_uid;
398 cred->sgid = cred->egid = cred->fsgid = bprm->e_gid;
400 /* For init, we want to retain the capabilities set
401 * in the init_task struct. Thus we skip the usual
402 * capability rules */
403 if (!is_global_init(current)) {
404 cred->cap_permitted = bprm->cap_post_exec_permitted;
405 if (bprm->cap_effective)
406 cred->cap_effective = bprm->cap_post_exec_permitted;
408 cap_clear(cred->cap_effective);
412 * Audit candidate if current->cap_effective is set
414 * We do not bother to audit if 3 things are true:
415 * 1) cap_effective has all caps
417 * 3) root is supposed to have all caps (SECURE_NOROOT)
418 * Since this is just a normal root execing a process.
420 * Number 1 above might fail if you don't have a full bset, but I think
421 * that is interesting information to audit.
423 if (!cap_isclear(cred->cap_effective)) {
424 if (!cap_issubset(CAP_FULL_SET, cred->cap_effective) ||
425 (bprm->e_uid != 0) || (cred->uid != 0) ||
426 issecure(SECURE_NOROOT))
427 audit_log_bprm_fcaps(bprm, &cred->cap_permitted,
428 &cred->cap_effective);
431 cred->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
434 int cap_bprm_secureexec (struct linux_binprm *bprm)
436 const struct cred *cred = current->cred;
438 if (cred->uid != 0) {
439 if (bprm->cap_effective)
441 if (!cap_isclear(bprm->cap_post_exec_permitted))
445 return (cred->euid != cred->uid ||
446 cred->egid != cred->gid);
449 int cap_inode_setxattr(struct dentry *dentry, const char *name,
450 const void *value, size_t size, int flags)
452 if (!strcmp(name, XATTR_NAME_CAPS)) {
453 if (!capable(CAP_SETFCAP))
456 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
457 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
458 !capable(CAP_SYS_ADMIN))
463 int cap_inode_removexattr(struct dentry *dentry, const char *name)
465 if (!strcmp(name, XATTR_NAME_CAPS)) {
466 if (!capable(CAP_SETFCAP))
469 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
470 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
471 !capable(CAP_SYS_ADMIN))
476 /* moved from kernel/sys.c. */
478 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
479 * a process after a call to setuid, setreuid, or setresuid.
481 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
482 * {r,e,s}uid != 0, the permitted and effective capabilities are
485 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
486 * capabilities of the process are cleared.
488 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
489 * capabilities are set to the permitted capabilities.
491 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
496 * cevans - New behaviour, Oct '99
497 * A process may, via prctl(), elect to keep its capabilities when it
498 * calls setuid() and switches away from uid==0. Both permitted and
499 * effective sets will be retained.
500 * Without this change, it was impossible for a daemon to drop only some
501 * of its privilege. The call to setuid(!=0) would drop all privileges!
502 * Keeping uid 0 is not an option because uid 0 owns too many vital
504 * Thanks to Olaf Kirch and Peter Benie for spotting this.
506 static inline void cap_emulate_setxuid (int old_ruid, int old_euid,
509 struct cred *cred = current->cred;
511 if ((old_ruid == 0 || old_euid == 0 || old_suid == 0) &&
512 (cred->uid != 0 && cred->euid != 0 && cred->suid != 0) &&
513 !issecure(SECURE_KEEP_CAPS)) {
514 cap_clear (cred->cap_permitted);
515 cap_clear (cred->cap_effective);
517 if (old_euid == 0 && cred->euid != 0) {
518 cap_clear (cred->cap_effective);
520 if (old_euid != 0 && cred->euid == 0) {
521 cred->cap_effective = cred->cap_permitted;
525 int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid,
528 struct cred *cred = current->cred;
534 /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
535 if (!issecure (SECURE_NO_SETUID_FIXUP)) {
536 cap_emulate_setxuid (old_ruid, old_euid, old_suid);
541 uid_t old_fsuid = old_ruid;
543 /* Copied from kernel/sys.c:setfsuid. */
546 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
547 * if not, we might be a bit too harsh here.
550 if (!issecure (SECURE_NO_SETUID_FIXUP)) {
551 if (old_fsuid == 0 && cred->fsuid != 0) {
552 cred->cap_effective =
554 cred->cap_effective);
556 if (old_fsuid != 0 && cred->fsuid == 0) {
557 cred->cap_effective =
560 cred->cap_permitted);
572 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
574 * Rationale: code calling task_setscheduler, task_setioprio, and
575 * task_setnice, assumes that
576 * . if capable(cap_sys_nice), then those actions should be allowed
577 * . if not capable(cap_sys_nice), but acting on your own processes,
578 * then those actions should be allowed
579 * This is insufficient now since you can call code without suid, but
580 * yet with increased caps.
581 * So we check for increased caps on the target process.
583 static int cap_safe_nice(struct task_struct *p)
585 if (!cap_issubset(p->cred->cap_permitted,
586 current->cred->cap_permitted) &&
587 !capable(CAP_SYS_NICE))
592 int cap_task_setscheduler (struct task_struct *p, int policy,
593 struct sched_param *lp)
595 return cap_safe_nice(p);
598 int cap_task_setioprio (struct task_struct *p, int ioprio)
600 return cap_safe_nice(p);
603 int cap_task_setnice (struct task_struct *p, int nice)
605 return cap_safe_nice(p);
609 * called from kernel/sys.c for prctl(PR_CABSET_DROP)
610 * done without task_capability_lock() because it introduces
611 * no new races - i.e. only another task doing capget() on
612 * this task could get inconsistent info. There can be no
613 * racing writer bc a task can only change its own caps.
615 static long cap_prctl_drop(unsigned long cap)
617 if (!capable(CAP_SETPCAP))
621 cap_lower(current->cred->cap_bset, cap);
626 int cap_task_setscheduler (struct task_struct *p, int policy,
627 struct sched_param *lp)
631 int cap_task_setioprio (struct task_struct *p, int ioprio)
635 int cap_task_setnice (struct task_struct *p, int nice)
641 int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
642 unsigned long arg4, unsigned long arg5, long *rc_p)
644 struct cred *cred = current_cred();
648 case PR_CAPBSET_READ:
649 if (!cap_valid(arg2))
652 error = !!cap_raised(cred->cap_bset, arg2);
654 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
655 case PR_CAPBSET_DROP:
656 error = cap_prctl_drop(arg2);
660 * The next four prctl's remain to assist with transitioning a
661 * system from legacy UID=0 based privilege (when filesystem
662 * capabilities are not in use) to a system using filesystem
663 * capabilities only - as the POSIX.1e draft intended.
667 * PR_SET_SECUREBITS =
668 * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
669 * | issecure_mask(SECURE_NOROOT)
670 * | issecure_mask(SECURE_NOROOT_LOCKED)
671 * | issecure_mask(SECURE_NO_SETUID_FIXUP)
672 * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
674 * will ensure that the current process and all of its
675 * children will be locked into a pure
676 * capability-based-privilege environment.
678 case PR_SET_SECUREBITS:
679 if ((((cred->securebits & SECURE_ALL_LOCKS) >> 1)
680 & (cred->securebits ^ arg2)) /*[1]*/
681 || ((cred->securebits & SECURE_ALL_LOCKS
683 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
684 || (cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0)) { /*[4]*/
686 * [1] no changing of bits that are locked
687 * [2] no unlocking of locks
688 * [3] no setting of unsupported bits
689 * [4] doing anything requires privilege (go read about
690 * the "sendmail capabilities bug")
692 error = -EPERM; /* cannot change a locked bit */
694 cred->securebits = arg2;
697 case PR_GET_SECUREBITS:
698 error = cred->securebits;
701 #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
703 case PR_GET_KEEPCAPS:
704 if (issecure(SECURE_KEEP_CAPS))
707 case PR_SET_KEEPCAPS:
708 if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
710 else if (issecure(SECURE_KEEP_CAPS_LOCKED))
713 cred->securebits |= issecure_mask(SECURE_KEEP_CAPS);
715 cred->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
719 /* No functionality available - continue with default */
723 /* Functionality provided */
728 void cap_task_reparent_to_init (struct task_struct *p)
730 struct cred *cred = p->cred;
732 cap_set_init_eff(cred->cap_effective);
733 cap_clear(cred->cap_inheritable);
734 cap_set_full(cred->cap_permitted);
735 p->cred->securebits = SECUREBITS_DEFAULT;
738 int cap_syslog (int type)
740 if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
745 int cap_vm_enough_memory(struct mm_struct *mm, long pages)
747 int cap_sys_admin = 0;
749 if (cap_capable(current, CAP_SYS_ADMIN, SECURITY_CAP_NOAUDIT) == 0)
751 return __vm_enough_memory(mm, pages, cap_sys_admin);