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->cap_post_exec_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)
206 if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
207 bprm->cap_effective = true;
209 bprm->cap_effective = false;
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 bprm->cap_post_exec_permitted.cap[i] =
219 (current->cred->cap_bset.cap[i] & permitted) |
220 (current->cred->cap_inheritable.cap[i] & inheritable);
222 if (permitted & ~bprm->cap_post_exec_permitted.cap[i]) {
224 * insufficient to execute correctly
231 * For legacy apps, with no internal support for recognizing they
232 * do not have enough capabilities, we return an error if they are
233 * missing some "forced" (aka file-permitted) capabilities.
235 return bprm->cap_effective ? ret : 0;
238 int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
240 struct inode *inode = dentry->d_inode;
244 struct vfs_cap_data caps;
246 memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
248 if (!inode || !inode->i_op || !inode->i_op->getxattr)
251 size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
253 if (size == -ENODATA || size == -EOPNOTSUPP) {
254 /* no data, that's ok */
260 if (size < sizeof(magic_etc))
263 cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
265 switch ((magic_etc & VFS_CAP_REVISION_MASK)) {
266 case VFS_CAP_REVISION_1:
267 if (size != XATTR_CAPS_SZ_1)
269 tocopy = VFS_CAP_U32_1;
271 case VFS_CAP_REVISION_2:
272 if (size != XATTR_CAPS_SZ_2)
274 tocopy = VFS_CAP_U32_2;
280 CAP_FOR_EACH_U32(i) {
283 cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
284 cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
289 /* Locate any VFS capabilities: */
290 static int get_file_caps(struct linux_binprm *bprm)
292 struct dentry *dentry;
294 struct cpu_vfs_cap_data vcaps;
296 bprm_clear_caps(bprm);
298 if (!file_caps_enabled)
301 if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
304 dentry = dget(bprm->file->f_dentry);
306 rc = get_vfs_caps_from_disk(dentry, &vcaps);
309 printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
310 __func__, rc, bprm->filename);
311 else if (rc == -ENODATA)
316 rc = bprm_caps_from_vfs_caps(&vcaps, bprm);
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)
339 bprm_clear_caps(bprm);
344 int cap_bprm_set_security (struct linux_binprm *bprm)
348 ret = get_file_caps(bprm);
350 if (!issecure(SECURE_NOROOT)) {
352 * To support inheritance of root-permissions and suid-root
353 * executables under compatibility mode, we override the
354 * capability sets for the file.
356 * If only the real uid is 0, we do not set the effective
359 if (bprm->e_uid == 0 || current_uid() == 0) {
360 /* pP' = (cap_bset & ~0) | (pI & ~0) */
361 bprm->cap_post_exec_permitted = cap_combine(
362 current->cred->cap_bset,
363 current->cred->cap_inheritable);
364 bprm->cap_effective = (bprm->e_uid == 0);
372 int cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
374 const struct cred *old = current_cred();
377 new = prepare_creds();
381 if (bprm->e_uid != old->uid || bprm->e_gid != old->gid ||
382 !cap_issubset(bprm->cap_post_exec_permitted,
383 old->cap_permitted)) {
384 set_dumpable(current->mm, suid_dumpable);
385 current->pdeath_signal = 0;
387 if (unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
388 if (!capable(CAP_SETUID)) {
389 bprm->e_uid = old->uid;
390 bprm->e_gid = old->gid;
392 if (cap_limit_ptraced_target()) {
393 bprm->cap_post_exec_permitted = cap_intersect(
394 bprm->cap_post_exec_permitted,
400 new->suid = new->euid = new->fsuid = bprm->e_uid;
401 new->sgid = new->egid = new->fsgid = bprm->e_gid;
403 /* For init, we want to retain the capabilities set
404 * in the init_task struct. Thus we skip the usual
405 * capability rules */
406 if (!is_global_init(current)) {
407 new->cap_permitted = bprm->cap_post_exec_permitted;
408 if (bprm->cap_effective)
409 new->cap_effective = bprm->cap_post_exec_permitted;
411 cap_clear(new->cap_effective);
415 * Audit candidate if current->cap_effective is set
417 * We do not bother to audit if 3 things are true:
418 * 1) cap_effective has all caps
420 * 3) root is supposed to have all caps (SECURE_NOROOT)
421 * Since this is just a normal root execing a process.
423 * Number 1 above might fail if you don't have a full bset, but I think
424 * that is interesting information to audit.
426 if (!cap_isclear(new->cap_effective)) {
427 if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
428 bprm->e_uid != 0 || new->uid != 0 ||
429 issecure(SECURE_NOROOT))
430 audit_log_bprm_fcaps(bprm, new, old);
433 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
434 return commit_creds(new);
437 int cap_bprm_secureexec (struct linux_binprm *bprm)
439 const struct cred *cred = current_cred();
441 if (cred->uid != 0) {
442 if (bprm->cap_effective)
444 if (!cap_isclear(bprm->cap_post_exec_permitted))
448 return (cred->euid != cred->uid ||
449 cred->egid != cred->gid);
452 int cap_inode_setxattr(struct dentry *dentry, const char *name,
453 const void *value, size_t size, int flags)
455 if (!strcmp(name, XATTR_NAME_CAPS)) {
456 if (!capable(CAP_SETFCAP))
459 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
460 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
461 !capable(CAP_SYS_ADMIN))
466 int cap_inode_removexattr(struct dentry *dentry, const char *name)
468 if (!strcmp(name, XATTR_NAME_CAPS)) {
469 if (!capable(CAP_SETFCAP))
472 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
473 sizeof(XATTR_SECURITY_PREFIX) - 1) &&
474 !capable(CAP_SYS_ADMIN))
479 /* moved from kernel/sys.c. */
481 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
482 * a process after a call to setuid, setreuid, or setresuid.
484 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
485 * {r,e,s}uid != 0, the permitted and effective capabilities are
488 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
489 * capabilities of the process are cleared.
491 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
492 * capabilities are set to the permitted capabilities.
494 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
499 * cevans - New behaviour, Oct '99
500 * A process may, via prctl(), elect to keep its capabilities when it
501 * calls setuid() and switches away from uid==0. Both permitted and
502 * effective sets will be retained.
503 * Without this change, it was impossible for a daemon to drop only some
504 * of its privilege. The call to setuid(!=0) would drop all privileges!
505 * Keeping uid 0 is not an option because uid 0 owns too many vital
507 * Thanks to Olaf Kirch and Peter Benie for spotting this.
509 static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
511 if ((old->uid == 0 || old->euid == 0 || old->suid == 0) &&
512 (new->uid != 0 && new->euid != 0 && new->suid != 0) &&
513 !issecure(SECURE_KEEP_CAPS)) {
514 cap_clear(new->cap_permitted);
515 cap_clear(new->cap_effective);
517 if (old->euid == 0 && new->euid != 0)
518 cap_clear(new->cap_effective);
519 if (old->euid != 0 && new->euid == 0)
520 new->cap_effective = new->cap_permitted;
523 int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
529 /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
530 if (!issecure(SECURE_NO_SETUID_FIXUP))
531 cap_emulate_setxuid(new, old);
534 /* Copied from kernel/sys.c:setfsuid. */
537 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
538 * if not, we might be a bit too harsh here.
540 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
541 if (old->fsuid == 0 && new->fsuid != 0) {
543 cap_drop_fs_set(new->cap_effective);
545 if (old->fsuid != 0 && new->fsuid == 0) {
547 cap_raise_fs_set(new->cap_effective,
559 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
561 * Rationale: code calling task_setscheduler, task_setioprio, and
562 * task_setnice, assumes that
563 * . if capable(cap_sys_nice), then those actions should be allowed
564 * . if not capable(cap_sys_nice), but acting on your own processes,
565 * then those actions should be allowed
566 * This is insufficient now since you can call code without suid, but
567 * yet with increased caps.
568 * So we check for increased caps on the target process.
570 static int cap_safe_nice(struct task_struct *p)
575 is_subset = cap_issubset(__task_cred(p)->cap_permitted,
576 current_cred()->cap_permitted);
579 if (!is_subset && !capable(CAP_SYS_NICE))
584 int cap_task_setscheduler (struct task_struct *p, int policy,
585 struct sched_param *lp)
587 return cap_safe_nice(p);
590 int cap_task_setioprio (struct task_struct *p, int ioprio)
592 return cap_safe_nice(p);
595 int cap_task_setnice (struct task_struct *p, int nice)
597 return cap_safe_nice(p);
601 * called from kernel/sys.c for prctl(PR_CABSET_DROP)
602 * done without task_capability_lock() because it introduces
603 * no new races - i.e. only another task doing capget() on
604 * this task could get inconsistent info. There can be no
605 * racing writer bc a task can only change its own caps.
607 static long cap_prctl_drop(struct cred *new, unsigned long cap)
609 if (!capable(CAP_SETPCAP))
614 cap_lower(new->cap_bset, cap);
619 int cap_task_setscheduler (struct task_struct *p, int policy,
620 struct sched_param *lp)
624 int cap_task_setioprio (struct task_struct *p, int ioprio)
628 int cap_task_setnice (struct task_struct *p, int nice)
634 int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
635 unsigned long arg4, unsigned long arg5)
640 new = prepare_creds();
645 case PR_CAPBSET_READ:
647 if (!cap_valid(arg2))
649 error = !!cap_raised(new->cap_bset, arg2);
652 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
653 case PR_CAPBSET_DROP:
654 error = cap_prctl_drop(new, 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:
680 if ((((new->securebits & SECURE_ALL_LOCKS) >> 1)
681 & (new->securebits ^ arg2)) /*[1]*/
682 || ((new->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
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")
693 /* cannot change a locked bit */
695 new->securebits = arg2;
698 case PR_GET_SECUREBITS:
699 error = new->securebits;
702 #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
704 case PR_GET_KEEPCAPS:
705 if (issecure(SECURE_KEEP_CAPS))
709 case PR_SET_KEEPCAPS:
711 if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
714 if (issecure(SECURE_KEEP_CAPS_LOCKED))
717 new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
719 new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
723 /* No functionality available - continue with default */
728 /* Functionality provided */
730 return commit_creds(new);
739 int cap_syslog (int type)
741 if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
746 int cap_vm_enough_memory(struct mm_struct *mm, long pages)
748 int cap_sys_admin = 0;
750 if (cap_capable(current, CAP_SYS_ADMIN, SECURITY_CAP_NOAUDIT) == 0)
752 return __vm_enough_memory(mm, pages, cap_sys_admin);