1 /* Common capabilities, needed by capability.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/lsm_hooks.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>
30 #include <linux/user_namespace.h>
31 #include <linux/binfmts.h>
32 #include <linux/personality.h>
34 #ifdef CONFIG_ANDROID_PARANOID_NETWORK
35 #include <linux/android_aid.h>
39 * If a non-root user executes a setuid-root binary in
40 * !secure(SECURE_NOROOT) mode, then we raise capabilities.
41 * However if fE is also set, then the intent is for only
42 * the file capabilities to be applied, and the setuid-root
43 * bit is left on either to change the uid (plausible) or
44 * to get full privilege on a kernel without file capabilities
45 * support. So in that case we do not raise capabilities.
47 * Warn if that happens, once per boot.
49 static void warn_setuid_and_fcaps_mixed(const char *fname
)
53 printk(KERN_INFO
"warning: `%s' has both setuid-root and"
54 " effective capabilities. Therefore not raising all"
55 " capabilities.\n", fname
);
61 * __cap_capable - Determine whether a task has a particular effective capability
62 * @cred: The credentials to use
63 * @ns: The user namespace in which we need the capability
64 * @cap: The capability to check for
65 * @audit: Whether to write an audit message or not
67 * Determine whether the nominated task has the specified capability amongst
68 * its effective set, returning 0 if it does, -ve if it does not.
70 * NOTE WELL: cap_has_capability() cannot be used like the kernel's capable()
71 * and has_capability() functions. That is, it has the reverse semantics:
72 * cap_has_capability() returns 0 when a task has a capability, but the
73 * kernel's capable() and has_capability() returns 1 for this case.
75 int __cap_capable(const struct cred
*cred
, struct user_namespace
*targ_ns
,
78 struct user_namespace
*ns
= targ_ns
;
80 /* See if cred has the capability in the target user namespace
81 * by examining the target user namespace and all of the target
82 * user namespace's parents.
85 /* Do we have the necessary capabilities? */
86 if (ns
== cred
->user_ns
)
87 return cap_raised(cred
->cap_effective
, cap
) ? 0 : -EPERM
;
90 * If we're already at a lower level than we're looking for,
91 * we're done searching.
93 if (ns
->level
<= cred
->user_ns
->level
)
97 * The owner of the user namespace in the parent of the
98 * user namespace has all caps.
100 if ((ns
->parent
== cred
->user_ns
) && uid_eq(ns
->owner
, cred
->euid
))
104 * If you have a capability in a parent user ns, then you have
105 * it over all children user namespaces as well.
110 /* We never get here */
113 int cap_capable(const struct cred
*cred
, struct user_namespace
*targ_ns
,
116 int ret
= __cap_capable(cred
, targ_ns
, cap
, audit
);
118 #ifdef CONFIG_ANDROID_PARANOID_NETWORK
119 if (ret
!= 0 && cap
== CAP_NET_RAW
&& in_egroup_p(AID_NET_RAW
)) {
120 printk("Process %s granted CAP_NET_RAW from Android group net_raw.\n", current
->comm
);
121 printk(" Please update the .rc file to explictly set 'capabilities NET_RAW'\n");
122 printk(" Implicit grants are deprecated and will be removed in the future.\n");
125 if (ret
!= 0 && cap
== CAP_NET_ADMIN
&& in_egroup_p(AID_NET_ADMIN
)) {
126 printk("Process %s granted CAP_NET_ADMIN from Android group net_admin.\n", current
->comm
);
127 printk(" Please update the .rc file to explictly set 'capabilities NET_ADMIN'\n");
128 printk(" Implicit grants are deprecated and will be removed in the future.\n");
135 * cap_settime - Determine whether the current process may set the system clock
136 * @ts: The time to set
137 * @tz: The timezone to set
139 * Determine whether the current process may set the system clock and timezone
140 * information, returning 0 if permission granted, -ve if denied.
142 int cap_settime(const struct timespec64
*ts
, const struct timezone
*tz
)
144 if (!capable(CAP_SYS_TIME
))
150 * cap_ptrace_access_check - Determine whether the current process may access
152 * @child: The process to be accessed
153 * @mode: The mode of attachment.
155 * If we are in the same or an ancestor user_ns and have all the target
156 * task's capabilities, then ptrace access is allowed.
157 * If we have the ptrace capability to the target user_ns, then ptrace
161 * Determine whether a process may access another, returning 0 if permission
162 * granted, -ve if denied.
164 int cap_ptrace_access_check(struct task_struct
*child
, unsigned int mode
)
167 const struct cred
*cred
, *child_cred
;
168 const kernel_cap_t
*caller_caps
;
171 cred
= current_cred();
172 child_cred
= __task_cred(child
);
173 if (mode
& PTRACE_MODE_FSCREDS
)
174 caller_caps
= &cred
->cap_effective
;
176 caller_caps
= &cred
->cap_permitted
;
177 if (cred
->user_ns
== child_cred
->user_ns
&&
178 cap_issubset(child_cred
->cap_permitted
, *caller_caps
))
180 if (ns_capable(child_cred
->user_ns
, CAP_SYS_PTRACE
))
189 * cap_ptrace_traceme - Determine whether another process may trace the current
190 * @parent: The task proposed to be the tracer
192 * If parent is in the same or an ancestor user_ns and has all current's
193 * capabilities, then ptrace access is allowed.
194 * If parent has the ptrace capability to current's user_ns, then ptrace
198 * Determine whether the nominated task is permitted to trace the current
199 * process, returning 0 if permission is granted, -ve if denied.
201 int cap_ptrace_traceme(struct task_struct
*parent
)
204 const struct cred
*cred
, *child_cred
;
207 cred
= __task_cred(parent
);
208 child_cred
= current_cred();
209 if (cred
->user_ns
== child_cred
->user_ns
&&
210 cap_issubset(child_cred
->cap_permitted
, cred
->cap_permitted
))
212 if (has_ns_capability(parent
, child_cred
->user_ns
, CAP_SYS_PTRACE
))
221 * cap_capget - Retrieve a task's capability sets
222 * @target: The task from which to retrieve the capability sets
223 * @effective: The place to record the effective set
224 * @inheritable: The place to record the inheritable set
225 * @permitted: The place to record the permitted set
227 * This function retrieves the capabilities of the nominated task and returns
228 * them to the caller.
230 int cap_capget(struct task_struct
*target
, kernel_cap_t
*effective
,
231 kernel_cap_t
*inheritable
, kernel_cap_t
*permitted
)
233 const struct cred
*cred
;
235 /* Derived from kernel/capability.c:sys_capget. */
237 cred
= __task_cred(target
);
238 *effective
= cred
->cap_effective
;
239 *inheritable
= cred
->cap_inheritable
;
240 *permitted
= cred
->cap_permitted
;
246 * Determine whether the inheritable capabilities are limited to the old
247 * permitted set. Returns 1 if they are limited, 0 if they are not.
249 static inline int cap_inh_is_capped(void)
252 /* they are so limited unless the current task has the CAP_SETPCAP
255 if (cap_capable(current_cred(), current_cred()->user_ns
,
256 CAP_SETPCAP
, SECURITY_CAP_AUDIT
) == 0)
262 * cap_capset - Validate and apply proposed changes to current's capabilities
263 * @new: The proposed new credentials; alterations should be made here
264 * @old: The current task's current credentials
265 * @effective: A pointer to the proposed new effective capabilities set
266 * @inheritable: A pointer to the proposed new inheritable capabilities set
267 * @permitted: A pointer to the proposed new permitted capabilities set
269 * This function validates and applies a proposed mass change to the current
270 * process's capability sets. The changes are made to the proposed new
271 * credentials, and assuming no error, will be committed by the caller of LSM.
273 int cap_capset(struct cred
*new,
274 const struct cred
*old
,
275 const kernel_cap_t
*effective
,
276 const kernel_cap_t
*inheritable
,
277 const kernel_cap_t
*permitted
)
279 if (cap_inh_is_capped() &&
280 !cap_issubset(*inheritable
,
281 cap_combine(old
->cap_inheritable
,
282 old
->cap_permitted
)))
283 /* incapable of using this inheritable set */
286 if (!cap_issubset(*inheritable
,
287 cap_combine(old
->cap_inheritable
,
289 /* no new pI capabilities outside bounding set */
292 /* verify restrictions on target's new Permitted set */
293 if (!cap_issubset(*permitted
, old
->cap_permitted
))
296 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
297 if (!cap_issubset(*effective
, *permitted
))
300 new->cap_effective
= *effective
;
301 new->cap_inheritable
= *inheritable
;
302 new->cap_permitted
= *permitted
;
305 * Mask off ambient bits that are no longer both permitted and
308 new->cap_ambient
= cap_intersect(new->cap_ambient
,
309 cap_intersect(*permitted
,
311 if (WARN_ON(!cap_ambient_invariant_ok(new)))
317 * cap_inode_need_killpriv - Determine if inode change affects privileges
318 * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV
320 * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV
321 * affects the security markings on that inode, and if it is, should
322 * inode_killpriv() be invoked or the change rejected.
324 * Returns 1 if security.capability has a value, meaning inode_killpriv()
325 * is required, 0 otherwise, meaning inode_killpriv() is not required.
327 int cap_inode_need_killpriv(struct dentry
*dentry
)
329 struct inode
*inode
= d_backing_inode(dentry
);
332 error
= __vfs_getxattr(dentry
, inode
, XATTR_NAME_CAPS
, NULL
, 0);
337 * cap_inode_killpriv - Erase the security markings on an inode
338 * @dentry: The inode/dentry to alter
340 * Erase the privilege-enhancing security markings on an inode.
342 * Returns 0 if successful, -ve on error.
344 int cap_inode_killpriv(struct dentry
*dentry
)
348 error
= __vfs_removexattr(dentry
, XATTR_NAME_CAPS
);
349 if (error
== -EOPNOTSUPP
)
354 static bool rootid_owns_currentns(kuid_t kroot
)
356 struct user_namespace
*ns
;
358 if (!uid_valid(kroot
))
361 for (ns
= current_user_ns(); ; ns
= ns
->parent
) {
362 if (from_kuid(ns
, kroot
) == 0)
364 if (ns
== &init_user_ns
)
371 static __u32
sansflags(__u32 m
)
373 return m
& ~VFS_CAP_FLAGS_EFFECTIVE
;
376 static bool is_v2header(size_t size
, const struct vfs_cap_data
*cap
)
378 if (size
!= XATTR_CAPS_SZ_2
)
380 return sansflags(le32_to_cpu(cap
->magic_etc
)) == VFS_CAP_REVISION_2
;
383 static bool is_v3header(size_t size
, const struct vfs_cap_data
*cap
)
385 if (size
!= XATTR_CAPS_SZ_3
)
387 return sansflags(le32_to_cpu(cap
->magic_etc
)) == VFS_CAP_REVISION_3
;
391 * getsecurity: We are called for security.* before any attempt to read the
392 * xattr from the inode itself.
394 * This gives us a chance to read the on-disk value and convert it. If we
395 * return -EOPNOTSUPP, then vfs_getxattr() will call the i_op handler.
397 * Note we are not called by vfs_getxattr_alloc(), but that is only called
398 * by the integrity subsystem, which really wants the unconverted values -
401 int cap_inode_getsecurity(struct inode
*inode
, const char *name
, void **buffer
,
406 uid_t root
, mappedroot
;
408 struct vfs_cap_data
*cap
;
409 struct vfs_ns_cap_data
*nscap
;
410 struct dentry
*dentry
;
411 struct user_namespace
*fs_ns
;
413 if (strcmp(name
, "capability") != 0)
416 dentry
= d_find_any_alias(inode
);
420 size
= sizeof(struct vfs_ns_cap_data
);
421 ret
= (int) vfs_getxattr_alloc(dentry
, XATTR_NAME_CAPS
,
422 &tmpbuf
, size
, GFP_NOFS
);
428 fs_ns
= inode
->i_sb
->s_user_ns
;
429 cap
= (struct vfs_cap_data
*) tmpbuf
;
430 if (is_v2header((size_t) ret
, cap
)) {
431 /* If this is sizeof(vfs_cap_data) then we're ok with the
432 * on-disk value, so return that. */
438 } else if (!is_v3header((size_t) ret
, cap
)) {
443 nscap
= (struct vfs_ns_cap_data
*) tmpbuf
;
444 root
= le32_to_cpu(nscap
->rootid
);
445 kroot
= make_kuid(fs_ns
, root
);
447 /* If the root kuid maps to a valid uid in current ns, then return
448 * this as a nscap. */
449 mappedroot
= from_kuid(current_user_ns(), kroot
);
450 if (mappedroot
!= (uid_t
)-1 && mappedroot
!= (uid_t
)0) {
453 nscap
->rootid
= cpu_to_le32(mappedroot
);
459 if (!rootid_owns_currentns(kroot
)) {
464 /* This comes from a parent namespace. Return as a v2 capability */
465 size
= sizeof(struct vfs_cap_data
);
467 *buffer
= kmalloc(size
, GFP_ATOMIC
);
469 struct vfs_cap_data
*cap
= *buffer
;
470 __le32 nsmagic
, magic
;
471 magic
= VFS_CAP_REVISION_2
;
472 nsmagic
= le32_to_cpu(nscap
->magic_etc
);
473 if (nsmagic
& VFS_CAP_FLAGS_EFFECTIVE
)
474 magic
|= VFS_CAP_FLAGS_EFFECTIVE
;
475 memcpy(&cap
->data
, &nscap
->data
, sizeof(__le32
) * 2 * VFS_CAP_U32
);
476 cap
->magic_etc
= cpu_to_le32(magic
);
485 static kuid_t
rootid_from_xattr(const void *value
, size_t size
,
486 struct user_namespace
*task_ns
)
488 const struct vfs_ns_cap_data
*nscap
= value
;
491 if (size
== XATTR_CAPS_SZ_3
)
492 rootid
= le32_to_cpu(nscap
->rootid
);
494 return make_kuid(task_ns
, rootid
);
497 static bool validheader(size_t size
, const struct vfs_cap_data
*cap
)
499 return is_v2header(size
, cap
) || is_v3header(size
, cap
);
503 * User requested a write of security.capability. If needed, update the
504 * xattr to change from v2 to v3, or to fixup the v3 rootid.
506 * If all is ok, we return the new size, on error return < 0.
508 int cap_convert_nscap(struct dentry
*dentry
, void **ivalue
, size_t size
)
510 struct vfs_ns_cap_data
*nscap
;
512 const struct vfs_cap_data
*cap
= *ivalue
;
513 __u32 magic
, nsmagic
;
514 struct inode
*inode
= d_backing_inode(dentry
);
515 struct user_namespace
*task_ns
= current_user_ns(),
516 *fs_ns
= inode
->i_sb
->s_user_ns
;
522 if (!validheader(size
, cap
))
524 if (!capable_wrt_inode_uidgid(inode
, CAP_SETFCAP
))
526 if (size
== XATTR_CAPS_SZ_2
)
527 if (ns_capable(inode
->i_sb
->s_user_ns
, CAP_SETFCAP
))
528 /* user is privileged, just write the v2 */
531 rootid
= rootid_from_xattr(*ivalue
, size
, task_ns
);
532 if (!uid_valid(rootid
))
535 nsrootid
= from_kuid(fs_ns
, rootid
);
539 newsize
= sizeof(struct vfs_ns_cap_data
);
540 nscap
= kmalloc(newsize
, GFP_ATOMIC
);
543 nscap
->rootid
= cpu_to_le32(nsrootid
);
544 nsmagic
= VFS_CAP_REVISION_3
;
545 magic
= le32_to_cpu(cap
->magic_etc
);
546 if (magic
& VFS_CAP_FLAGS_EFFECTIVE
)
547 nsmagic
|= VFS_CAP_FLAGS_EFFECTIVE
;
548 nscap
->magic_etc
= cpu_to_le32(nsmagic
);
549 memcpy(&nscap
->data
, &cap
->data
, sizeof(__le32
) * 2 * VFS_CAP_U32
);
557 * Calculate the new process capability sets from the capability sets attached
560 static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data
*caps
,
561 struct linux_binprm
*bprm
,
565 struct cred
*new = bprm
->cred
;
569 if (caps
->magic_etc
& VFS_CAP_FLAGS_EFFECTIVE
)
572 if (caps
->magic_etc
& VFS_CAP_REVISION_MASK
)
575 CAP_FOR_EACH_U32(i
) {
576 __u32 permitted
= caps
->permitted
.cap
[i
];
577 __u32 inheritable
= caps
->inheritable
.cap
[i
];
580 * pP' = (X & fP) | (pI & fI)
581 * The addition of pA' is handled later.
583 new->cap_permitted
.cap
[i
] =
584 (new->cap_bset
.cap
[i
] & permitted
) |
585 (new->cap_inheritable
.cap
[i
] & inheritable
);
587 if (permitted
& ~new->cap_permitted
.cap
[i
])
588 /* insufficient to execute correctly */
593 * For legacy apps, with no internal support for recognizing they
594 * do not have enough capabilities, we return an error if they are
595 * missing some "forced" (aka file-permitted) capabilities.
597 return *effective
? ret
: 0;
601 * Extract the on-exec-apply capability sets for an executable file.
603 int get_vfs_caps_from_disk(const struct dentry
*dentry
, struct cpu_vfs_cap_data
*cpu_caps
)
605 struct inode
*inode
= d_backing_inode(dentry
);
609 struct vfs_ns_cap_data data
, *nscaps
= &data
;
610 struct vfs_cap_data
*caps
= (struct vfs_cap_data
*) &data
;
612 struct user_namespace
*fs_ns
;
614 memset(cpu_caps
, 0, sizeof(struct cpu_vfs_cap_data
));
619 fs_ns
= inode
->i_sb
->s_user_ns
;
620 size
= __vfs_getxattr((struct dentry
*)dentry
, inode
,
621 XATTR_NAME_CAPS
, &data
, XATTR_CAPS_SZ
);
622 if (size
== -ENODATA
|| size
== -EOPNOTSUPP
)
623 /* no data, that's ok */
629 if (size
< sizeof(magic_etc
))
632 cpu_caps
->magic_etc
= magic_etc
= le32_to_cpu(caps
->magic_etc
);
634 rootkuid
= make_kuid(fs_ns
, 0);
635 switch (magic_etc
& VFS_CAP_REVISION_MASK
) {
636 case VFS_CAP_REVISION_1
:
637 if (size
!= XATTR_CAPS_SZ_1
)
639 tocopy
= VFS_CAP_U32_1
;
641 case VFS_CAP_REVISION_2
:
642 if (size
!= XATTR_CAPS_SZ_2
)
644 tocopy
= VFS_CAP_U32_2
;
646 case VFS_CAP_REVISION_3
:
647 if (size
!= XATTR_CAPS_SZ_3
)
649 tocopy
= VFS_CAP_U32_3
;
650 rootkuid
= make_kuid(fs_ns
, le32_to_cpu(nscaps
->rootid
));
656 /* Limit the caps to the mounter of the filesystem
657 * or the more limited uid specified in the xattr.
659 if (!rootid_owns_currentns(rootkuid
))
662 CAP_FOR_EACH_U32(i
) {
665 cpu_caps
->permitted
.cap
[i
] = le32_to_cpu(caps
->data
[i
].permitted
);
666 cpu_caps
->inheritable
.cap
[i
] = le32_to_cpu(caps
->data
[i
].inheritable
);
669 cpu_caps
->permitted
.cap
[CAP_LAST_U32
] &= CAP_LAST_U32_VALID_MASK
;
670 cpu_caps
->inheritable
.cap
[CAP_LAST_U32
] &= CAP_LAST_U32_VALID_MASK
;
676 * Attempt to get the on-exec apply capability sets for an executable file from
677 * its xattrs and, if present, apply them to the proposed credentials being
678 * constructed by execve().
680 static int get_file_caps(struct linux_binprm
*bprm
, bool *effective
, bool *has_cap
)
683 struct cpu_vfs_cap_data vcaps
;
685 cap_clear(bprm
->cred
->cap_permitted
);
687 if (!file_caps_enabled
)
690 if (!mnt_may_suid(bprm
->file
->f_path
.mnt
))
694 * This check is redundant with mnt_may_suid() but is kept to make
695 * explicit that capability bits are limited to s_user_ns and its
698 if (!current_in_userns(bprm
->file
->f_path
.mnt
->mnt_sb
->s_user_ns
))
701 rc
= get_vfs_caps_from_disk(bprm
->file
->f_path
.dentry
, &vcaps
);
704 printk(KERN_NOTICE
"Invalid argument reading file caps for %s\n",
706 else if (rc
== -ENODATA
)
711 rc
= bprm_caps_from_vfs_caps(&vcaps
, bprm
, effective
, has_cap
);
713 printk(KERN_NOTICE
"%s: cap_from_disk returned %d for %s\n",
714 __func__
, rc
, bprm
->filename
);
718 cap_clear(bprm
->cred
->cap_permitted
);
724 * cap_bprm_set_creds - Set up the proposed credentials for execve().
725 * @bprm: The execution parameters, including the proposed creds
727 * Set up the proposed credentials for a new execution context being
728 * constructed by execve(). The proposed creds in @bprm->cred is altered,
729 * which won't take effect immediately. Returns 0 if successful, -ve on error.
731 int cap_bprm_set_creds(struct linux_binprm
*bprm
)
733 const struct cred
*old
= current_cred();
734 struct cred
*new = bprm
->cred
;
735 bool effective
, has_cap
= false, is_setid
;
739 if (WARN_ON(!cap_ambient_invariant_ok(old
)))
743 ret
= get_file_caps(bprm
, &effective
, &has_cap
);
747 root_uid
= make_kuid(new->user_ns
, 0);
749 if (!issecure(SECURE_NOROOT
)) {
751 * If the legacy file capability is set, then don't set privs
752 * for a setuid root binary run by a non-root user. Do set it
753 * for a root user just to cause least surprise to an admin.
755 if (has_cap
&& !uid_eq(new->uid
, root_uid
) && uid_eq(new->euid
, root_uid
)) {
756 warn_setuid_and_fcaps_mixed(bprm
->filename
);
760 * To support inheritance of root-permissions and suid-root
761 * executables under compatibility mode, we override the
762 * capability sets for the file.
764 * If only the real uid is 0, we do not set the effective bit.
766 if (uid_eq(new->euid
, root_uid
) || uid_eq(new->uid
, root_uid
)) {
767 /* pP' = (cap_bset & ~0) | (pI & ~0) */
768 new->cap_permitted
= cap_combine(old
->cap_bset
,
769 old
->cap_inheritable
);
771 if (uid_eq(new->euid
, root_uid
))
776 /* if we have fs caps, clear dangerous personality flags */
777 if (!cap_issubset(new->cap_permitted
, old
->cap_permitted
))
778 bprm
->per_clear
|= PER_CLEAR_ON_SETID
;
781 /* Don't let someone trace a set[ug]id/setpcap binary with the revised
782 * credentials unless they have the appropriate permit.
784 * In addition, if NO_NEW_PRIVS, then ensure we get no new privs.
786 is_setid
= !uid_eq(new->euid
, old
->uid
) || !gid_eq(new->egid
, old
->gid
);
789 !cap_issubset(new->cap_permitted
, old
->cap_permitted
)) &&
790 ((bprm
->unsafe
& ~LSM_UNSAFE_PTRACE
) ||
791 !ptracer_capable(current
, new->user_ns
))) {
792 /* downgrade; they get no more than they had, and maybe less */
793 if (!ns_capable(new->user_ns
, CAP_SETUID
) ||
794 (bprm
->unsafe
& LSM_UNSAFE_NO_NEW_PRIVS
)) {
795 new->euid
= new->uid
;
796 new->egid
= new->gid
;
798 new->cap_permitted
= cap_intersect(new->cap_permitted
,
802 new->suid
= new->fsuid
= new->euid
;
803 new->sgid
= new->fsgid
= new->egid
;
805 /* File caps or setid cancels ambient. */
806 if (has_cap
|| is_setid
)
807 cap_clear(new->cap_ambient
);
810 * Now that we've computed pA', update pP' to give:
811 * pP' = (X & fP) | (pI & fI) | pA'
813 new->cap_permitted
= cap_combine(new->cap_permitted
, new->cap_ambient
);
816 * Set pE' = (fE ? pP' : pA'). Because pA' is zero if fE is set,
817 * this is the same as pE' = (fE ? pP' : 0) | pA'.
820 new->cap_effective
= new->cap_permitted
;
822 new->cap_effective
= new->cap_ambient
;
824 if (WARN_ON(!cap_ambient_invariant_ok(new)))
828 * Audit candidate if current->cap_effective is set
830 * We do not bother to audit if 3 things are true:
831 * 1) cap_effective has all caps
833 * 3) root is supposed to have all caps (SECURE_NOROOT)
834 * Since this is just a normal root execing a process.
836 * Number 1 above might fail if you don't have a full bset, but I think
837 * that is interesting information to audit.
839 if (!cap_issubset(new->cap_effective
, new->cap_ambient
)) {
840 if (!cap_issubset(CAP_FULL_SET
, new->cap_effective
) ||
841 !uid_eq(new->euid
, root_uid
) || !uid_eq(new->uid
, root_uid
) ||
842 issecure(SECURE_NOROOT
)) {
843 ret
= audit_log_bprm_fcaps(bprm
, new, old
);
849 new->securebits
&= ~issecure_mask(SECURE_KEEP_CAPS
);
851 if (WARN_ON(!cap_ambient_invariant_ok(new)))
854 /* Check for privilege-elevated exec. */
855 bprm
->cap_elevated
= 0;
857 bprm
->cap_elevated
= 1;
858 } else if (!uid_eq(new->uid
, root_uid
)) {
860 !cap_issubset(new->cap_permitted
, new->cap_ambient
))
861 bprm
->cap_elevated
= 1;
868 * cap_inode_setxattr - Determine whether an xattr may be altered
869 * @dentry: The inode/dentry being altered
870 * @name: The name of the xattr to be changed
871 * @value: The value that the xattr will be changed to
872 * @size: The size of value
873 * @flags: The replacement flag
875 * Determine whether an xattr may be altered or set on an inode, returning 0 if
876 * permission is granted, -ve if denied.
878 * This is used to make sure security xattrs don't get updated or set by those
879 * who aren't privileged to do so.
881 int cap_inode_setxattr(struct dentry
*dentry
, const char *name
,
882 const void *value
, size_t size
, int flags
)
884 /* Ignore non-security xattrs */
885 if (strncmp(name
, XATTR_SECURITY_PREFIX
,
886 sizeof(XATTR_SECURITY_PREFIX
) - 1) != 0)
890 * For XATTR_NAME_CAPS the check will be done in
891 * cap_convert_nscap(), called by setxattr()
893 if (strcmp(name
, XATTR_NAME_CAPS
) == 0)
896 if (!capable(CAP_SYS_ADMIN
))
902 * cap_inode_removexattr - Determine whether an xattr may be removed
903 * @dentry: The inode/dentry being altered
904 * @name: The name of the xattr to be changed
906 * Determine whether an xattr may be removed from an inode, returning 0 if
907 * permission is granted, -ve if denied.
909 * This is used to make sure security xattrs don't get removed by those who
910 * aren't privileged to remove them.
912 int cap_inode_removexattr(struct dentry
*dentry
, const char *name
)
914 /* Ignore non-security xattrs */
915 if (strncmp(name
, XATTR_SECURITY_PREFIX
,
916 sizeof(XATTR_SECURITY_PREFIX
) - 1) != 0)
919 if (strcmp(name
, XATTR_NAME_CAPS
) == 0) {
920 /* security.capability gets namespaced */
921 struct inode
*inode
= d_backing_inode(dentry
);
924 if (!capable_wrt_inode_uidgid(inode
, CAP_SETFCAP
))
929 if (!capable(CAP_SYS_ADMIN
))
935 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
936 * a process after a call to setuid, setreuid, or setresuid.
938 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
939 * {r,e,s}uid != 0, the permitted and effective capabilities are
942 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
943 * capabilities of the process are cleared.
945 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
946 * capabilities are set to the permitted capabilities.
948 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
953 * cevans - New behaviour, Oct '99
954 * A process may, via prctl(), elect to keep its capabilities when it
955 * calls setuid() and switches away from uid==0. Both permitted and
956 * effective sets will be retained.
957 * Without this change, it was impossible for a daemon to drop only some
958 * of its privilege. The call to setuid(!=0) would drop all privileges!
959 * Keeping uid 0 is not an option because uid 0 owns too many vital
961 * Thanks to Olaf Kirch and Peter Benie for spotting this.
963 static inline void cap_emulate_setxuid(struct cred
*new, const struct cred
*old
)
965 kuid_t root_uid
= make_kuid(old
->user_ns
, 0);
967 if ((uid_eq(old
->uid
, root_uid
) ||
968 uid_eq(old
->euid
, root_uid
) ||
969 uid_eq(old
->suid
, root_uid
)) &&
970 (!uid_eq(new->uid
, root_uid
) &&
971 !uid_eq(new->euid
, root_uid
) &&
972 !uid_eq(new->suid
, root_uid
))) {
973 if (!issecure(SECURE_KEEP_CAPS
)) {
974 cap_clear(new->cap_permitted
);
975 cap_clear(new->cap_effective
);
979 * Pre-ambient programs expect setresuid to nonroot followed
980 * by exec to drop capabilities. We should make sure that
981 * this remains the case.
983 cap_clear(new->cap_ambient
);
985 if (uid_eq(old
->euid
, root_uid
) && !uid_eq(new->euid
, root_uid
))
986 cap_clear(new->cap_effective
);
987 if (!uid_eq(old
->euid
, root_uid
) && uid_eq(new->euid
, root_uid
))
988 new->cap_effective
= new->cap_permitted
;
992 * cap_task_fix_setuid - Fix up the results of setuid() call
993 * @new: The proposed credentials
994 * @old: The current task's current credentials
995 * @flags: Indications of what has changed
997 * Fix up the results of setuid() call before the credential changes are
998 * actually applied, returning 0 to grant the changes, -ve to deny them.
1000 int cap_task_fix_setuid(struct cred
*new, const struct cred
*old
, int flags
)
1006 /* juggle the capabilities to follow [RES]UID changes unless
1007 * otherwise suppressed */
1008 if (!issecure(SECURE_NO_SETUID_FIXUP
))
1009 cap_emulate_setxuid(new, old
);
1013 /* juggle the capabilties to follow FSUID changes, unless
1014 * otherwise suppressed
1016 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
1017 * if not, we might be a bit too harsh here.
1019 if (!issecure(SECURE_NO_SETUID_FIXUP
)) {
1020 kuid_t root_uid
= make_kuid(old
->user_ns
, 0);
1021 if (uid_eq(old
->fsuid
, root_uid
) && !uid_eq(new->fsuid
, root_uid
))
1022 new->cap_effective
=
1023 cap_drop_fs_set(new->cap_effective
);
1025 if (!uid_eq(old
->fsuid
, root_uid
) && uid_eq(new->fsuid
, root_uid
))
1026 new->cap_effective
=
1027 cap_raise_fs_set(new->cap_effective
,
1028 new->cap_permitted
);
1040 * Rationale: code calling task_setscheduler, task_setioprio, and
1041 * task_setnice, assumes that
1042 * . if capable(cap_sys_nice), then those actions should be allowed
1043 * . if not capable(cap_sys_nice), but acting on your own processes,
1044 * then those actions should be allowed
1045 * This is insufficient now since you can call code without suid, but
1046 * yet with increased caps.
1047 * So we check for increased caps on the target process.
1049 static int cap_safe_nice(struct task_struct
*p
)
1051 int is_subset
, ret
= 0;
1054 is_subset
= cap_issubset(__task_cred(p
)->cap_permitted
,
1055 current_cred()->cap_permitted
);
1056 if (!is_subset
&& !ns_capable(__task_cred(p
)->user_ns
, CAP_SYS_NICE
))
1064 * cap_task_setscheduler - Detemine if scheduler policy change is permitted
1065 * @p: The task to affect
1067 * Detemine if the requested scheduler policy change is permitted for the
1068 * specified task, returning 0 if permission is granted, -ve if denied.
1070 int cap_task_setscheduler(struct task_struct
*p
)
1072 return cap_safe_nice(p
);
1076 * cap_task_ioprio - Detemine if I/O priority change is permitted
1077 * @p: The task to affect
1078 * @ioprio: The I/O priority to set
1080 * Detemine if the requested I/O priority change is permitted for the specified
1081 * task, returning 0 if permission is granted, -ve if denied.
1083 int cap_task_setioprio(struct task_struct
*p
, int ioprio
)
1085 return cap_safe_nice(p
);
1089 * cap_task_ioprio - Detemine if task priority change is permitted
1090 * @p: The task to affect
1091 * @nice: The nice value to set
1093 * Detemine if the requested task priority change is permitted for the
1094 * specified task, returning 0 if permission is granted, -ve if denied.
1096 int cap_task_setnice(struct task_struct
*p
, int nice
)
1098 return cap_safe_nice(p
);
1102 * Implement PR_CAPBSET_DROP. Attempt to remove the specified capability from
1103 * the current task's bounding set. Returns 0 on success, -ve on error.
1105 static int cap_prctl_drop(unsigned long cap
)
1109 if (!ns_capable(current_user_ns(), CAP_SETPCAP
))
1111 if (!cap_valid(cap
))
1114 new = prepare_creds();
1117 cap_lower(new->cap_bset
, cap
);
1118 return commit_creds(new);
1122 * cap_task_prctl - Implement process control functions for this security module
1123 * @option: The process control function requested
1124 * @arg2, @arg3, @arg4, @arg5: The argument data for this function
1126 * Allow process control functions (sys_prctl()) to alter capabilities; may
1127 * also deny access to other functions not otherwise implemented here.
1129 * Returns 0 or +ve on success, -ENOSYS if this function is not implemented
1130 * here, other -ve on error. If -ENOSYS is returned, sys_prctl() and other LSM
1131 * modules will consider performing the function.
1133 int cap_task_prctl(int option
, unsigned long arg2
, unsigned long arg3
,
1134 unsigned long arg4
, unsigned long arg5
)
1136 const struct cred
*old
= current_cred();
1140 case PR_CAPBSET_READ
:
1141 if (!cap_valid(arg2
))
1143 return !!cap_raised(old
->cap_bset
, arg2
);
1145 case PR_CAPBSET_DROP
:
1146 return cap_prctl_drop(arg2
);
1149 * The next four prctl's remain to assist with transitioning a
1150 * system from legacy UID=0 based privilege (when filesystem
1151 * capabilities are not in use) to a system using filesystem
1152 * capabilities only - as the POSIX.1e draft intended.
1156 * PR_SET_SECUREBITS =
1157 * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
1158 * | issecure_mask(SECURE_NOROOT)
1159 * | issecure_mask(SECURE_NOROOT_LOCKED)
1160 * | issecure_mask(SECURE_NO_SETUID_FIXUP)
1161 * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
1163 * will ensure that the current process and all of its
1164 * children will be locked into a pure
1165 * capability-based-privilege environment.
1167 case PR_SET_SECUREBITS
:
1168 if ((((old
->securebits
& SECURE_ALL_LOCKS
) >> 1)
1169 & (old
->securebits
^ arg2
)) /*[1]*/
1170 || ((old
->securebits
& SECURE_ALL_LOCKS
& ~arg2
)) /*[2]*/
1171 || (arg2
& ~(SECURE_ALL_LOCKS
| SECURE_ALL_BITS
)) /*[3]*/
1172 || (cap_capable(current_cred(),
1173 current_cred()->user_ns
, CAP_SETPCAP
,
1174 SECURITY_CAP_AUDIT
) != 0) /*[4]*/
1176 * [1] no changing of bits that are locked
1177 * [2] no unlocking of locks
1178 * [3] no setting of unsupported bits
1179 * [4] doing anything requires privilege (go read about
1180 * the "sendmail capabilities bug")
1183 /* cannot change a locked bit */
1186 new = prepare_creds();
1189 new->securebits
= arg2
;
1190 return commit_creds(new);
1192 case PR_GET_SECUREBITS
:
1193 return old
->securebits
;
1195 case PR_GET_KEEPCAPS
:
1196 return !!issecure(SECURE_KEEP_CAPS
);
1198 case PR_SET_KEEPCAPS
:
1199 if (arg2
> 1) /* Note, we rely on arg2 being unsigned here */
1201 if (issecure(SECURE_KEEP_CAPS_LOCKED
))
1204 new = prepare_creds();
1208 new->securebits
|= issecure_mask(SECURE_KEEP_CAPS
);
1210 new->securebits
&= ~issecure_mask(SECURE_KEEP_CAPS
);
1211 return commit_creds(new);
1213 case PR_CAP_AMBIENT
:
1214 if (arg2
== PR_CAP_AMBIENT_CLEAR_ALL
) {
1215 if (arg3
| arg4
| arg5
)
1218 new = prepare_creds();
1221 cap_clear(new->cap_ambient
);
1222 return commit_creds(new);
1225 if (((!cap_valid(arg3
)) | arg4
| arg5
))
1228 if (arg2
== PR_CAP_AMBIENT_IS_SET
) {
1229 return !!cap_raised(current_cred()->cap_ambient
, arg3
);
1230 } else if (arg2
!= PR_CAP_AMBIENT_RAISE
&&
1231 arg2
!= PR_CAP_AMBIENT_LOWER
) {
1234 if (arg2
== PR_CAP_AMBIENT_RAISE
&&
1235 (!cap_raised(current_cred()->cap_permitted
, arg3
) ||
1236 !cap_raised(current_cred()->cap_inheritable
,
1238 issecure(SECURE_NO_CAP_AMBIENT_RAISE
)))
1241 new = prepare_creds();
1244 if (arg2
== PR_CAP_AMBIENT_RAISE
)
1245 cap_raise(new->cap_ambient
, arg3
);
1247 cap_lower(new->cap_ambient
, arg3
);
1248 return commit_creds(new);
1252 /* No functionality available - continue with default */
1258 * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted
1259 * @mm: The VM space in which the new mapping is to be made
1260 * @pages: The size of the mapping
1262 * Determine whether the allocation of a new virtual mapping by the current
1263 * task is permitted, returning 1 if permission is granted, 0 if not.
1265 int cap_vm_enough_memory(struct mm_struct
*mm
, long pages
)
1267 int cap_sys_admin
= 0;
1269 if (cap_capable(current_cred(), &init_user_ns
, CAP_SYS_ADMIN
,
1270 SECURITY_CAP_NOAUDIT
) == 0)
1272 return cap_sys_admin
;
1276 * cap_mmap_addr - check if able to map given addr
1277 * @addr: address attempting to be mapped
1279 * If the process is attempting to map memory below dac_mmap_min_addr they need
1280 * CAP_SYS_RAWIO. The other parameters to this function are unused by the
1281 * capability security module. Returns 0 if this mapping should be allowed
1284 int cap_mmap_addr(unsigned long addr
)
1288 if (addr
< dac_mmap_min_addr
) {
1289 ret
= cap_capable(current_cred(), &init_user_ns
, CAP_SYS_RAWIO
,
1290 SECURITY_CAP_AUDIT
);
1291 /* set PF_SUPERPRIV if it turns out we allow the low mmap */
1293 current
->flags
|= PF_SUPERPRIV
;
1298 int cap_mmap_file(struct file
*file
, unsigned long reqprot
,
1299 unsigned long prot
, unsigned long flags
)
1304 #ifdef CONFIG_SECURITY
1306 struct security_hook_list capability_hooks
[] __lsm_ro_after_init
= {
1307 LSM_HOOK_INIT(capable
, cap_capable
),
1308 LSM_HOOK_INIT(settime
, cap_settime
),
1309 LSM_HOOK_INIT(ptrace_access_check
, cap_ptrace_access_check
),
1310 LSM_HOOK_INIT(ptrace_traceme
, cap_ptrace_traceme
),
1311 LSM_HOOK_INIT(capget
, cap_capget
),
1312 LSM_HOOK_INIT(capset
, cap_capset
),
1313 LSM_HOOK_INIT(bprm_set_creds
, cap_bprm_set_creds
),
1314 LSM_HOOK_INIT(inode_need_killpriv
, cap_inode_need_killpriv
),
1315 LSM_HOOK_INIT(inode_killpriv
, cap_inode_killpriv
),
1316 LSM_HOOK_INIT(inode_getsecurity
, cap_inode_getsecurity
),
1317 LSM_HOOK_INIT(mmap_addr
, cap_mmap_addr
),
1318 LSM_HOOK_INIT(mmap_file
, cap_mmap_file
),
1319 LSM_HOOK_INIT(task_fix_setuid
, cap_task_fix_setuid
),
1320 LSM_HOOK_INIT(task_prctl
, cap_task_prctl
),
1321 LSM_HOOK_INIT(task_setscheduler
, cap_task_setscheduler
),
1322 LSM_HOOK_INIT(task_setioprio
, cap_task_setioprio
),
1323 LSM_HOOK_INIT(task_setnice
, cap_task_setnice
),
1324 LSM_HOOK_INIT(vm_enough_memory
, cap_vm_enough_memory
),
1327 void __init
capability_add_hooks(void)
1329 security_add_hooks(capability_hooks
, ARRAY_SIZE(capability_hooks
),
1333 #endif /* CONFIG_SECURITY */