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/module.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/security.h>
15 #include <linux/file.h>
17 #include <linux/mman.h>
18 #include <linux/pagemap.h>
19 #include <linux/swap.h>
20 #include <linux/skbuff.h>
21 #include <linux/netlink.h>
22 #include <linux/ptrace.h>
23 #include <linux/xattr.h>
24 #include <linux/hugetlb.h>
25 #include <linux/mount.h>
26 #include <linux/sched.h>
27 #include <linux/prctl.h>
28 #include <linux/securebits.h>
30 int cap_netlink_send(struct sock
*sk
, struct sk_buff
*skb
)
32 NETLINK_CB(skb
).eff_cap
= current
->cap_effective
;
36 int cap_netlink_recv(struct sk_buff
*skb
, int cap
)
38 if (!cap_raised(NETLINK_CB(skb
).eff_cap
, cap
))
43 EXPORT_SYMBOL(cap_netlink_recv
);
46 * NOTE WELL: cap_capable() cannot be used like the kernel's capable()
47 * function. That is, it has the reverse semantics: cap_capable()
48 * returns 0 when a task has a capability, but the kernel's capable()
49 * returns 1 for this case.
51 int cap_capable (struct task_struct
*tsk
, int cap
)
53 /* Derived from include/linux/sched.h:capable. */
54 if (cap_raised(tsk
->cap_effective
, cap
))
59 int cap_settime(struct timespec
*ts
, struct timezone
*tz
)
61 if (!capable(CAP_SYS_TIME
))
66 int cap_ptrace (struct task_struct
*parent
, struct task_struct
*child
,
69 /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */
70 if (!cap_issubset(child
->cap_permitted
, parent
->cap_permitted
) &&
71 !__capable(parent
, CAP_SYS_PTRACE
))
76 int cap_capget (struct task_struct
*target
, kernel_cap_t
*effective
,
77 kernel_cap_t
*inheritable
, kernel_cap_t
*permitted
)
79 /* Derived from kernel/capability.c:sys_capget. */
80 *effective
= target
->cap_effective
;
81 *inheritable
= target
->cap_inheritable
;
82 *permitted
= target
->cap_permitted
;
86 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
88 static inline int cap_block_setpcap(struct task_struct
*target
)
91 * No support for remote process capability manipulation with
92 * filesystem capability support.
94 return (target
!= current
);
97 static inline int cap_inh_is_capped(void)
100 * Return 1 if changes to the inheritable set are limited
101 * to the old permitted set. That is, if the current task
102 * does *not* possess the CAP_SETPCAP capability.
104 return (cap_capable(current
, CAP_SETPCAP
) != 0);
107 static inline int cap_limit_ptraced_target(void) { return 1; }
109 #else /* ie., ndef CONFIG_SECURITY_FILE_CAPABILITIES */
111 static inline int cap_block_setpcap(struct task_struct
*t
) { return 0; }
112 static inline int cap_inh_is_capped(void) { return 1; }
113 static inline int cap_limit_ptraced_target(void)
115 return !capable(CAP_SETPCAP
);
118 #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
120 int cap_capset_check (struct task_struct
*target
, kernel_cap_t
*effective
,
121 kernel_cap_t
*inheritable
, kernel_cap_t
*permitted
)
123 if (cap_block_setpcap(target
)) {
126 if (cap_inh_is_capped()
127 && !cap_issubset(*inheritable
,
128 cap_combine(target
->cap_inheritable
,
129 current
->cap_permitted
))) {
130 /* incapable of using this inheritable set */
133 if (!cap_issubset(*inheritable
,
134 cap_combine(target
->cap_inheritable
,
135 current
->cap_bset
))) {
136 /* no new pI capabilities outside bounding set */
140 /* verify restrictions on target's new Permitted set */
141 if (!cap_issubset (*permitted
,
142 cap_combine (target
->cap_permitted
,
143 current
->cap_permitted
))) {
147 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
148 if (!cap_issubset (*effective
, *permitted
)) {
155 void cap_capset_set (struct task_struct
*target
, kernel_cap_t
*effective
,
156 kernel_cap_t
*inheritable
, kernel_cap_t
*permitted
)
158 target
->cap_effective
= *effective
;
159 target
->cap_inheritable
= *inheritable
;
160 target
->cap_permitted
= *permitted
;
163 static inline void bprm_clear_caps(struct linux_binprm
*bprm
)
165 cap_clear(bprm
->cap_inheritable
);
166 cap_clear(bprm
->cap_permitted
);
167 bprm
->cap_effective
= false;
170 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
172 int cap_inode_need_killpriv(struct dentry
*dentry
)
174 struct inode
*inode
= dentry
->d_inode
;
177 if (!inode
->i_op
|| !inode
->i_op
->getxattr
)
180 error
= inode
->i_op
->getxattr(dentry
, XATTR_NAME_CAPS
, NULL
, 0);
186 int cap_inode_killpriv(struct dentry
*dentry
)
188 struct inode
*inode
= dentry
->d_inode
;
190 if (!inode
->i_op
|| !inode
->i_op
->removexattr
)
193 return inode
->i_op
->removexattr(dentry
, XATTR_NAME_CAPS
);
196 static inline int cap_from_disk(struct vfs_cap_data
*caps
,
197 struct linux_binprm
*bprm
, unsigned size
)
202 if (size
< sizeof(magic_etc
))
205 magic_etc
= le32_to_cpu(caps
->magic_etc
);
207 switch ((magic_etc
& VFS_CAP_REVISION_MASK
)) {
208 case VFS_CAP_REVISION_1
:
209 if (size
!= XATTR_CAPS_SZ_1
)
211 tocopy
= VFS_CAP_U32_1
;
213 case VFS_CAP_REVISION_2
:
214 if (size
!= XATTR_CAPS_SZ_2
)
216 tocopy
= VFS_CAP_U32_2
;
222 if (magic_etc
& VFS_CAP_FLAGS_EFFECTIVE
) {
223 bprm
->cap_effective
= true;
225 bprm
->cap_effective
= false;
228 for (i
= 0; i
< tocopy
; ++i
) {
229 bprm
->cap_permitted
.cap
[i
] =
230 le32_to_cpu(caps
->data
[i
].permitted
);
231 bprm
->cap_inheritable
.cap
[i
] =
232 le32_to_cpu(caps
->data
[i
].inheritable
);
234 while (i
< VFS_CAP_U32
) {
235 bprm
->cap_permitted
.cap
[i
] = 0;
236 bprm
->cap_inheritable
.cap
[i
] = 0;
243 /* Locate any VFS capabilities: */
244 static int get_file_caps(struct linux_binprm
*bprm
)
246 struct dentry
*dentry
;
248 struct vfs_cap_data vcaps
;
251 if (bprm
->file
->f_vfsmnt
->mnt_flags
& MNT_NOSUID
) {
252 bprm_clear_caps(bprm
);
256 dentry
= dget(bprm
->file
->f_dentry
);
257 inode
= dentry
->d_inode
;
258 if (!inode
->i_op
|| !inode
->i_op
->getxattr
)
261 rc
= inode
->i_op
->getxattr(dentry
, XATTR_NAME_CAPS
, &vcaps
,
263 if (rc
== -ENODATA
|| rc
== -EOPNOTSUPP
) {
264 /* no data, that's ok */
271 rc
= cap_from_disk(&vcaps
, bprm
, rc
);
273 printk(KERN_NOTICE
"%s: cap_from_disk returned %d for %s\n",
274 __func__
, rc
, bprm
->filename
);
279 bprm_clear_caps(bprm
);
285 int cap_inode_need_killpriv(struct dentry
*dentry
)
290 int cap_inode_killpriv(struct dentry
*dentry
)
295 static inline int get_file_caps(struct linux_binprm
*bprm
)
297 bprm_clear_caps(bprm
);
302 int cap_bprm_set_security (struct linux_binprm
*bprm
)
306 ret
= get_file_caps(bprm
);
308 printk(KERN_NOTICE
"%s: get_file_caps returned %d for %s\n",
309 __func__
, ret
, bprm
->filename
);
311 /* To support inheritance of root-permissions and suid-root
312 * executables under compatibility mode, we raise all three
313 * capability sets for the file.
315 * If only the real uid is 0, we only raise the inheritable
316 * and permitted sets of the executable file.
319 if (!issecure (SECURE_NOROOT
)) {
320 if (bprm
->e_uid
== 0 || current
->uid
== 0) {
321 cap_set_full (bprm
->cap_inheritable
);
322 cap_set_full (bprm
->cap_permitted
);
324 if (bprm
->e_uid
== 0)
325 bprm
->cap_effective
= true;
331 void cap_bprm_apply_creds (struct linux_binprm
*bprm
, int unsafe
)
333 /* Derived from fs/exec.c:compute_creds. */
334 kernel_cap_t new_permitted
, working
;
336 new_permitted
= cap_intersect(bprm
->cap_permitted
,
338 working
= cap_intersect(bprm
->cap_inheritable
,
339 current
->cap_inheritable
);
340 new_permitted
= cap_combine(new_permitted
, working
);
342 if (bprm
->e_uid
!= current
->uid
|| bprm
->e_gid
!= current
->gid
||
343 !cap_issubset (new_permitted
, current
->cap_permitted
)) {
344 set_dumpable(current
->mm
, suid_dumpable
);
345 current
->pdeath_signal
= 0;
347 if (unsafe
& ~LSM_UNSAFE_PTRACE_CAP
) {
348 if (!capable(CAP_SETUID
)) {
349 bprm
->e_uid
= current
->uid
;
350 bprm
->e_gid
= current
->gid
;
352 if (cap_limit_ptraced_target()) {
354 cap_intersect(new_permitted
,
355 current
->cap_permitted
);
360 current
->suid
= current
->euid
= current
->fsuid
= bprm
->e_uid
;
361 current
->sgid
= current
->egid
= current
->fsgid
= bprm
->e_gid
;
363 /* For init, we want to retain the capabilities set
364 * in the init_task struct. Thus we skip the usual
365 * capability rules */
366 if (!is_global_init(current
)) {
367 current
->cap_permitted
= new_permitted
;
368 if (bprm
->cap_effective
)
369 current
->cap_effective
= new_permitted
;
371 cap_clear(current
->cap_effective
);
374 /* AUD: Audit candidate if current->cap_effective is set */
376 current
->securebits
&= ~issecure_mask(SECURE_KEEP_CAPS
);
379 int cap_bprm_secureexec (struct linux_binprm
*bprm
)
381 if (current
->uid
!= 0) {
382 if (bprm
->cap_effective
)
384 if (!cap_isclear(bprm
->cap_permitted
))
386 if (!cap_isclear(bprm
->cap_inheritable
))
390 return (current
->euid
!= current
->uid
||
391 current
->egid
!= current
->gid
);
394 int cap_inode_setxattr(struct dentry
*dentry
, const char *name
,
395 const void *value
, size_t size
, int flags
)
397 if (!strcmp(name
, XATTR_NAME_CAPS
)) {
398 if (!capable(CAP_SETFCAP
))
401 } else if (!strncmp(name
, XATTR_SECURITY_PREFIX
,
402 sizeof(XATTR_SECURITY_PREFIX
) - 1) &&
403 !capable(CAP_SYS_ADMIN
))
408 int cap_inode_removexattr(struct dentry
*dentry
, const char *name
)
410 if (!strcmp(name
, XATTR_NAME_CAPS
)) {
411 if (!capable(CAP_SETFCAP
))
414 } else if (!strncmp(name
, XATTR_SECURITY_PREFIX
,
415 sizeof(XATTR_SECURITY_PREFIX
) - 1) &&
416 !capable(CAP_SYS_ADMIN
))
421 /* moved from kernel/sys.c. */
423 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
424 * a process after a call to setuid, setreuid, or setresuid.
426 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
427 * {r,e,s}uid != 0, the permitted and effective capabilities are
430 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
431 * capabilities of the process are cleared.
433 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
434 * capabilities are set to the permitted capabilities.
436 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
441 * cevans - New behaviour, Oct '99
442 * A process may, via prctl(), elect to keep its capabilities when it
443 * calls setuid() and switches away from uid==0. Both permitted and
444 * effective sets will be retained.
445 * Without this change, it was impossible for a daemon to drop only some
446 * of its privilege. The call to setuid(!=0) would drop all privileges!
447 * Keeping uid 0 is not an option because uid 0 owns too many vital
449 * Thanks to Olaf Kirch and Peter Benie for spotting this.
451 static inline void cap_emulate_setxuid (int old_ruid
, int old_euid
,
454 if ((old_ruid
== 0 || old_euid
== 0 || old_suid
== 0) &&
455 (current
->uid
!= 0 && current
->euid
!= 0 && current
->suid
!= 0) &&
456 !issecure(SECURE_KEEP_CAPS
)) {
457 cap_clear (current
->cap_permitted
);
458 cap_clear (current
->cap_effective
);
460 if (old_euid
== 0 && current
->euid
!= 0) {
461 cap_clear (current
->cap_effective
);
463 if (old_euid
!= 0 && current
->euid
== 0) {
464 current
->cap_effective
= current
->cap_permitted
;
468 int cap_task_post_setuid (uid_t old_ruid
, uid_t old_euid
, uid_t old_suid
,
475 /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
476 if (!issecure (SECURE_NO_SETUID_FIXUP
)) {
477 cap_emulate_setxuid (old_ruid
, old_euid
, old_suid
);
482 uid_t old_fsuid
= old_ruid
;
484 /* Copied from kernel/sys.c:setfsuid. */
487 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
488 * if not, we might be a bit too harsh here.
491 if (!issecure (SECURE_NO_SETUID_FIXUP
)) {
492 if (old_fsuid
== 0 && current
->fsuid
!= 0) {
493 current
->cap_effective
=
495 current
->cap_effective
);
497 if (old_fsuid
!= 0 && current
->fsuid
== 0) {
498 current
->cap_effective
=
500 current
->cap_effective
,
501 current
->cap_permitted
);
513 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
515 * Rationale: code calling task_setscheduler, task_setioprio, and
516 * task_setnice, assumes that
517 * . if capable(cap_sys_nice), then those actions should be allowed
518 * . if not capable(cap_sys_nice), but acting on your own processes,
519 * then those actions should be allowed
520 * This is insufficient now since you can call code without suid, but
521 * yet with increased caps.
522 * So we check for increased caps on the target process.
524 static inline int cap_safe_nice(struct task_struct
*p
)
526 if (!cap_issubset(p
->cap_permitted
, current
->cap_permitted
) &&
527 !__capable(current
, CAP_SYS_NICE
))
532 int cap_task_setscheduler (struct task_struct
*p
, int policy
,
533 struct sched_param
*lp
)
535 return cap_safe_nice(p
);
538 int cap_task_setioprio (struct task_struct
*p
, int ioprio
)
540 return cap_safe_nice(p
);
543 int cap_task_setnice (struct task_struct
*p
, int nice
)
545 return cap_safe_nice(p
);
549 * called from kernel/sys.c for prctl(PR_CABSET_DROP)
550 * done without task_capability_lock() because it introduces
551 * no new races - i.e. only another task doing capget() on
552 * this task could get inconsistent info. There can be no
553 * racing writer bc a task can only change its own caps.
555 static long cap_prctl_drop(unsigned long cap
)
557 if (!capable(CAP_SETPCAP
))
561 cap_lower(current
->cap_bset
, cap
);
566 int cap_task_setscheduler (struct task_struct
*p
, int policy
,
567 struct sched_param
*lp
)
571 int cap_task_setioprio (struct task_struct
*p
, int ioprio
)
575 int cap_task_setnice (struct task_struct
*p
, int nice
)
581 int cap_task_prctl(int option
, unsigned long arg2
, unsigned long arg3
,
582 unsigned long arg4
, unsigned long arg5
, long *rc_p
)
587 case PR_CAPBSET_READ
:
588 if (!cap_valid(arg2
))
591 error
= !!cap_raised(current
->cap_bset
, arg2
);
593 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
594 case PR_CAPBSET_DROP
:
595 error
= cap_prctl_drop(arg2
);
599 * The next four prctl's remain to assist with transitioning a
600 * system from legacy UID=0 based privilege (when filesystem
601 * capabilities are not in use) to a system using filesystem
602 * capabilities only - as the POSIX.1e draft intended.
606 * PR_SET_SECUREBITS =
607 * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
608 * | issecure_mask(SECURE_NOROOT)
609 * | issecure_mask(SECURE_NOROOT_LOCKED)
610 * | issecure_mask(SECURE_NO_SETUID_FIXUP)
611 * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
613 * will ensure that the current process and all of its
614 * children will be locked into a pure
615 * capability-based-privilege environment.
617 case PR_SET_SECUREBITS
:
618 if ((((current
->securebits
& SECURE_ALL_LOCKS
) >> 1)
619 & (current
->securebits
^ arg2
)) /*[1]*/
620 || ((current
->securebits
& SECURE_ALL_LOCKS
622 || (arg2
& ~(SECURE_ALL_LOCKS
| SECURE_ALL_BITS
)) /*[3]*/
623 || (cap_capable(current
, CAP_SETPCAP
) != 0)) { /*[4]*/
625 * [1] no changing of bits that are locked
626 * [2] no unlocking of locks
627 * [3] no setting of unsupported bits
628 * [4] doing anything requires privilege (go read about
629 * the "sendmail capabilities bug")
631 error
= -EPERM
; /* cannot change a locked bit */
633 current
->securebits
= arg2
;
636 case PR_GET_SECUREBITS
:
637 error
= current
->securebits
;
640 #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
642 case PR_GET_KEEPCAPS
:
643 if (issecure(SECURE_KEEP_CAPS
))
646 case PR_SET_KEEPCAPS
:
647 if (arg2
> 1) /* Note, we rely on arg2 being unsigned here */
649 else if (issecure(SECURE_KEEP_CAPS_LOCKED
))
652 current
->securebits
|= issecure_mask(SECURE_KEEP_CAPS
);
654 current
->securebits
&=
655 ~issecure_mask(SECURE_KEEP_CAPS
);
659 /* No functionality available - continue with default */
663 /* Functionality provided */
668 void cap_task_reparent_to_init (struct task_struct
*p
)
670 cap_set_init_eff(p
->cap_effective
);
671 cap_clear(p
->cap_inheritable
);
672 cap_set_full(p
->cap_permitted
);
673 p
->securebits
= SECUREBITS_DEFAULT
;
677 int cap_syslog (int type
)
679 if ((type
!= 3 && type
!= 10) && !capable(CAP_SYS_ADMIN
))
684 int cap_vm_enough_memory(struct mm_struct
*mm
, long pages
)
686 int cap_sys_admin
= 0;
688 if (cap_capable(current
, CAP_SYS_ADMIN
) == 0)
690 return __vm_enough_memory(mm
, pages
, cap_sys_admin
);