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)
56 /* Derived from include/linux/sched.h:capable. */
58 cap_raised = cap_raised(__task_cred(tsk)->cap_effective, cap);
60 return cap_raised ? 0 : -EPERM;
63 int cap_settime(struct timespec *ts, struct timezone *tz)
65 if (!capable(CAP_SYS_TIME))
70 int cap_ptrace_may_access(struct task_struct *child, unsigned int mode)
75 if (!cap_issubset(__task_cred(child)->cap_permitted,
76 current_cred()->cap_permitted) &&
77 !capable(CAP_SYS_PTRACE))
83 int cap_ptrace_traceme(struct task_struct *parent)
88 if (!cap_issubset(current_cred()->cap_permitted,
89 __task_cred(parent)->cap_permitted) &&
90 !has_capability(parent, CAP_SYS_PTRACE))
96 int cap_capget (struct task_struct *target, kernel_cap_t *effective,
97 kernel_cap_t *inheritable, kernel_cap_t *permitted)
99 const struct cred *cred;
101 /* Derived from kernel/capability.c:sys_capget. */
103 cred = __task_cred(target);
104 *effective = cred->cap_effective;
105 *inheritable = cred->cap_inheritable;
106 *permitted = cred->cap_permitted;
111 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
113 static inline int cap_inh_is_capped(void)
116 * Return 1 if changes to the inheritable set are limited
117 * to the old permitted set. That is, if the current task
118 * does *not* possess the CAP_SETPCAP capability.
120 return cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0;
123 static inline int cap_limit_ptraced_target(void) { return 1; }
125 #else /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */
127 static inline int cap_inh_is_capped(void) { return 1; }
128 static inline int cap_limit_ptraced_target(void)
130 return !capable(CAP_SETPCAP);
133 #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
135 int cap_capset(struct cred *new,
136 const struct cred *old,
137 const kernel_cap_t *effective,
138 const kernel_cap_t *inheritable,
139 const kernel_cap_t *permitted)
141 if (cap_inh_is_capped() &&
142 !cap_issubset(*inheritable,
143 cap_combine(old->cap_inheritable,
144 old->cap_permitted)))
145 /* incapable of using this inheritable set */
148 if (!cap_issubset(*inheritable,
149 cap_combine(old->cap_inheritable,
151 /* no new pI capabilities outside bounding set */
154 /* verify restrictions on target's new Permitted set */
155 if (!cap_issubset(*permitted, old->cap_permitted))
158 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
159 if (!cap_issubset(*effective, *permitted))
162 new->cap_effective = *effective;
163 new->cap_inheritable = *inheritable;
164 new->cap_permitted = *permitted;
168 static inline void bprm_clear_caps(struct linux_binprm *bprm)
170 cap_clear(bprm->cred->cap_permitted);
171 bprm->cap_effective = false;
174 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
176 int cap_inode_need_killpriv(struct dentry *dentry)
178 struct inode *inode = dentry->d_inode;
181 if (!inode->i_op || !inode->i_op->getxattr)
184 error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
190 int cap_inode_killpriv(struct dentry *dentry)
192 struct inode *inode = dentry->d_inode;
194 if (!inode->i_op || !inode->i_op->removexattr)
197 return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
200 static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
201 struct linux_binprm *bprm,
204 struct cred *new = bprm->cred;
208 if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
211 CAP_FOR_EACH_U32(i) {
212 __u32 permitted = caps->permitted.cap[i];
213 __u32 inheritable = caps->inheritable.cap[i];
216 * pP' = (X & fP) | (pI & fI)
218 new->cap_permitted.cap[i] =
219 (new->cap_bset.cap[i] & permitted) |
220 (new->cap_inheritable.cap[i] & inheritable);
222 if (permitted & ~new->cap_permitted.cap[i])
223 /* insufficient to execute correctly */
228 * For legacy apps, with no internal support for recognizing they
229 * do not have enough capabilities, we return an error if they are
230 * missing some "forced" (aka file-permitted) capabilities.
232 return *effective ? ret : 0;
235 int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
237 struct inode *inode = dentry->d_inode;
241 struct vfs_cap_data caps;
243 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
245 if (!inode || !inode->i_op || !inode->i_op->getxattr)
248 size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
250 if (size == -ENODATA || size == -EOPNOTSUPP)
251 /* no data, that's ok */
256 if (size < sizeof(magic_etc))
259 cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
261 switch (magic_etc & VFS_CAP_REVISION_MASK) {
262 case VFS_CAP_REVISION_1:
263 if (size != XATTR_CAPS_SZ_1)
265 tocopy = VFS_CAP_U32_1;
267 case VFS_CAP_REVISION_2:
268 if (size != XATTR_CAPS_SZ_2)
270 tocopy = VFS_CAP_U32_2;
276 CAP_FOR_EACH_U32(i) {
279 cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
280 cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
286 /* Locate any VFS capabilities: */
287 static int get_file_caps(struct linux_binprm *bprm, bool *effective)
289 struct dentry *dentry;
291 struct cpu_vfs_cap_data vcaps;
293 bprm_clear_caps(bprm);
295 if (!file_caps_enabled)
298 if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
301 dentry = dget(bprm->file->f_dentry);
303 rc = get_vfs_caps_from_disk(dentry, &vcaps);
306 printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
307 __func__, rc, bprm->filename);
308 else if (rc == -ENODATA)
313 rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective);
315 printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
316 __func__, rc, bprm->filename);
321 bprm_clear_caps(bprm);
327 int cap_inode_need_killpriv(struct dentry *dentry)
332 int cap_inode_killpriv(struct dentry *dentry)
337 static inline int get_file_caps(struct linux_binprm *bprm, bool *effective)
339 bprm_clear_caps(bprm);
345 * set up the new credentials for an exec'd task
347 int cap_bprm_set_creds(struct linux_binprm *bprm)
349 const struct cred *old = current_cred();
350 struct cred *new = bprm->cred;
355 ret = get_file_caps(bprm, &effective);
359 if (!issecure(SECURE_NOROOT)) {
361 * To support inheritance of root-permissions and suid-root
362 * executables under compatibility mode, we override the
363 * capability sets for the file.
365 * If only the real uid is 0, we do not set the effective bit.
367 if (new->euid == 0 || new->uid == 0) {
368 /* pP' = (cap_bset & ~0) | (pI & ~0) */
369 new->cap_permitted = cap_combine(old->cap_bset,
370 old->cap_inheritable);
376 /* Don't let someone trace a set[ug]id/setpcap binary with the revised
377 * credentials unless they have the appropriate permit
379 if ((new->euid != old->uid ||
380 new->egid != old->gid ||
381 !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
382 bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
383 /* downgrade; they get no more than they had, and maybe less */
384 if (!capable(CAP_SETUID)) {
385 new->euid = new->uid;
386 new->egid = new->gid;
388 if (cap_limit_ptraced_target())
389 new->cap_permitted = cap_intersect(new->cap_permitted,
393 new->suid = new->fsuid = new->euid;
394 new->sgid = new->fsgid = new->egid;
396 /* For init, we want to retain the capabilities set in the initial
397 * task. Thus we skip the usual capability rules
399 if (!is_global_init(current)) {
401 new->cap_effective = new->cap_permitted;
403 cap_clear(new->cap_effective);
405 bprm->cap_effective = effective;
408 * Audit candidate if current->cap_effective is set
410 * We do not bother to audit if 3 things are true:
411 * 1) cap_effective has all caps
413 * 3) root is supposed to have all caps (SECURE_NOROOT)
414 * Since this is just a normal root execing a process.
416 * Number 1 above might fail if you don't have a full bset, but I think
417 * that is interesting information to audit.
419 if (!cap_isclear(new->cap_effective)) {
420 if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
421 new->euid != 0 || new->uid != 0 ||
422 issecure(SECURE_NOROOT)) {
423 ret = audit_log_bprm_fcaps(bprm, new, old);
429 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
434 * determine whether a secure execution is required
435 * - the creds have been committed at this point, and are no longer available
438 int cap_bprm_secureexec(struct linux_binprm *bprm)
440 const struct cred *cred = current_cred();
442 if (cred->uid != 0) {
443 if (bprm->cap_effective)
445 if (!cap_isclear(cred->cap_permitted))
449 return (cred->euid != cred->uid ||
450 cred->egid != cred->gid);
453 int cap_inode_setxattr(struct dentry *dentry, const char *name,
454 const void *value, size_t size, int flags)
456 if (!strcmp(name, XATTR_NAME_CAPS)) {
457 if (!capable(CAP_SETFCAP))
460 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
461 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
462 !capable(CAP_SYS_ADMIN))
467 int cap_inode_removexattr(struct dentry *dentry, const char *name)
469 if (!strcmp(name, XATTR_NAME_CAPS)) {
470 if (!capable(CAP_SETFCAP))
473 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
474 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
475 !capable(CAP_SYS_ADMIN))
480 /* moved from kernel/sys.c. */
482 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
483 * a process after a call to setuid, setreuid, or setresuid.
485 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
486 * {r,e,s}uid != 0, the permitted and effective capabilities are
489 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
490 * capabilities of the process are cleared.
492 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
493 * capabilities are set to the permitted capabilities.
495 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
500 * cevans - New behaviour, Oct '99
501 * A process may, via prctl(), elect to keep its capabilities when it
502 * calls setuid() and switches away from uid==0. Both permitted and
503 * effective sets will be retained.
504 * Without this change, it was impossible for a daemon to drop only some
505 * of its privilege. The call to setuid(!=0) would drop all privileges!
506 * Keeping uid 0 is not an option because uid 0 owns too many vital
508 * Thanks to Olaf Kirch and Peter Benie for spotting this.
510 static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
512 if ((old->uid == 0 || old->euid == 0 || old->suid == 0) &&
513 (new->uid != 0 && new->euid != 0 && new->suid != 0) &&
514 !issecure(SECURE_KEEP_CAPS)) {
515 cap_clear(new->cap_permitted);
516 cap_clear(new->cap_effective);
518 if (old->euid == 0 && new->euid != 0)
519 cap_clear(new->cap_effective);
520 if (old->euid != 0 && new->euid == 0)
521 new->cap_effective = new->cap_permitted;
524 int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
530 /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
531 if (!issecure(SECURE_NO_SETUID_FIXUP))
532 cap_emulate_setxuid(new, old);
535 /* Copied from kernel/sys.c:setfsuid. */
538 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
539 * if not, we might be a bit too harsh here.
541 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
542 if (old->fsuid == 0 && new->fsuid != 0) {
544 cap_drop_fs_set(new->cap_effective);
546 if (old->fsuid != 0 && new->fsuid == 0) {
548 cap_raise_fs_set(new->cap_effective,
560 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
562 * Rationale: code calling task_setscheduler, task_setioprio, and
563 * task_setnice, assumes that
564 * . if capable(cap_sys_nice), then those actions should be allowed
565 * . if not capable(cap_sys_nice), but acting on your own processes,
566 * then those actions should be allowed
567 * This is insufficient now since you can call code without suid, but
568 * yet with increased caps.
569 * So we check for increased caps on the target process.
571 static int cap_safe_nice(struct task_struct *p)
576 is_subset = cap_issubset(__task_cred(p)->cap_permitted,
577 current_cred()->cap_permitted);
580 if (!is_subset && !capable(CAP_SYS_NICE))
585 int cap_task_setscheduler (struct task_struct *p, int policy,
586 struct sched_param *lp)
588 return cap_safe_nice(p);
591 int cap_task_setioprio (struct task_struct *p, int ioprio)
593 return cap_safe_nice(p);
596 int cap_task_setnice (struct task_struct *p, int nice)
598 return cap_safe_nice(p);
602 * called from kernel/sys.c for prctl(PR_CABSET_DROP)
603 * done without task_capability_lock() because it introduces
604 * no new races - i.e. only another task doing capget() on
605 * this task could get inconsistent info. There can be no
606 * racing writer bc a task can only change its own caps.
608 static long cap_prctl_drop(struct cred *new, unsigned long cap)
610 if (!capable(CAP_SETPCAP))
615 cap_lower(new->cap_bset, cap);
620 int cap_task_setscheduler (struct task_struct *p, int policy,
621 struct sched_param *lp)
625 int cap_task_setioprio (struct task_struct *p, int ioprio)
629 int cap_task_setnice (struct task_struct *p, int nice)
635 int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
636 unsigned long arg4, unsigned long arg5)
641 new = prepare_creds();
646 case PR_CAPBSET_READ:
648 if (!cap_valid(arg2))
650 error = !!cap_raised(new->cap_bset, arg2);
653 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
654 case PR_CAPBSET_DROP:
655 error = cap_prctl_drop(new, arg2);
661 * The next four prctl's remain to assist with transitioning a
662 * system from legacy UID=0 based privilege (when filesystem
663 * capabilities are not in use) to a system using filesystem
664 * capabilities only - as the POSIX.1e draft intended.
668 * PR_SET_SECUREBITS =
669 * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
670 * | issecure_mask(SECURE_NOROOT)
671 * | issecure_mask(SECURE_NOROOT_LOCKED)
672 * | issecure_mask(SECURE_NO_SETUID_FIXUP)
673 * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
675 * will ensure that the current process and all of its
676 * children will be locked into a pure
677 * capability-based-privilege environment.
679 case PR_SET_SECUREBITS:
681 if ((((new->securebits & SECURE_ALL_LOCKS) >> 1)
682 & (new->securebits ^ arg2)) /*[1]*/
683 || ((new->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
684 || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
685 || (cap_capable(current, CAP_SETPCAP, SECURITY_CAP_AUDIT) != 0) /*[4]*/
687 * [1] no changing of bits that are locked
688 * [2] no unlocking of locks
689 * [3] no setting of unsupported bits
690 * [4] doing anything requires privilege (go read about
691 * the "sendmail capabilities bug")
694 /* cannot change a locked bit */
696 new->securebits = arg2;
699 case PR_GET_SECUREBITS:
700 error = new->securebits;
703 #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
705 case PR_GET_KEEPCAPS:
706 if (issecure(SECURE_KEEP_CAPS))
710 case PR_SET_KEEPCAPS:
712 if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
715 if (issecure(SECURE_KEEP_CAPS_LOCKED))
718 new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
720 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
724 /* No functionality available - continue with default */
729 /* Functionality provided */
731 return commit_creds(new);
740 int cap_syslog (int type)
742 if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
747 int cap_vm_enough_memory(struct mm_struct *mm, long pages)
749 int cap_sys_admin = 0;
751 if (cap_capable(current, CAP_SYS_ADMIN, SECURITY_CAP_NOAUDIT) == 0)
753 return __vm_enough_memory(mm, pages, cap_sys_admin);