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_post_exec_permitted
);
166 bprm
->cap_effective
= false;
169 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
171 int cap_inode_need_killpriv(struct dentry
*dentry
)
173 struct inode
*inode
= dentry
->d_inode
;
176 if (!inode
->i_op
|| !inode
->i_op
->getxattr
)
179 error
= inode
->i_op
->getxattr(dentry
, XATTR_NAME_CAPS
, NULL
, 0);
185 int cap_inode_killpriv(struct dentry
*dentry
)
187 struct inode
*inode
= dentry
->d_inode
;
189 if (!inode
->i_op
|| !inode
->i_op
->removexattr
)
192 return inode
->i_op
->removexattr(dentry
, XATTR_NAME_CAPS
);
195 static inline int cap_from_disk(struct vfs_cap_data
*caps
,
196 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;
230 CAP_FOR_EACH_U32(i
) {
235 * Legacy capability sets have no upper bits
237 bprm
->cap_post_exec_permitted
.cap
[i
] = 0;
241 * pP' = (X & fP) | (pI & fI)
243 value_cpu
= le32_to_cpu(caps
->data
[i
].permitted
);
244 bprm
->cap_post_exec_permitted
.cap
[i
] =
245 (current
->cap_bset
.cap
[i
] & value_cpu
) |
246 (current
->cap_inheritable
.cap
[i
] &
247 le32_to_cpu(caps
->data
[i
].inheritable
));
248 if (value_cpu
& ~bprm
->cap_post_exec_permitted
.cap
[i
]) {
250 * insufficient to execute correctly
257 * For legacy apps, with no internal support for recognizing they
258 * do not have enough capabilities, we return an error if they are
259 * missing some "forced" (aka file-permitted) capabilities.
261 return bprm
->cap_effective
? ret
: 0;
264 /* Locate any VFS capabilities: */
265 static int get_file_caps(struct linux_binprm
*bprm
)
267 struct dentry
*dentry
;
269 struct vfs_cap_data vcaps
;
272 if (bprm
->file
->f_vfsmnt
->mnt_flags
& MNT_NOSUID
) {
273 bprm_clear_caps(bprm
);
277 dentry
= dget(bprm
->file
->f_dentry
);
278 inode
= dentry
->d_inode
;
279 if (!inode
->i_op
|| !inode
->i_op
->getxattr
)
282 rc
= inode
->i_op
->getxattr(dentry
, XATTR_NAME_CAPS
, &vcaps
,
284 if (rc
== -ENODATA
|| rc
== -EOPNOTSUPP
) {
285 /* no data, that's ok */
292 rc
= cap_from_disk(&vcaps
, bprm
, rc
);
294 printk(KERN_NOTICE
"%s: cap_from_disk returned %d for %s\n",
295 __func__
, rc
, bprm
->filename
);
300 bprm_clear_caps(bprm
);
306 int cap_inode_need_killpriv(struct dentry
*dentry
)
311 int cap_inode_killpriv(struct dentry
*dentry
)
316 static inline int get_file_caps(struct linux_binprm
*bprm
)
318 bprm_clear_caps(bprm
);
323 int cap_bprm_set_security (struct linux_binprm
*bprm
)
327 ret
= get_file_caps(bprm
);
329 if (!issecure(SECURE_NOROOT
)) {
331 * To support inheritance of root-permissions and suid-root
332 * executables under compatibility mode, we override the
333 * capability sets for the file.
335 * If only the real uid is 0, we do not set the effective
338 if (bprm
->e_uid
== 0 || current
->uid
== 0) {
339 /* pP' = (cap_bset & ~0) | (pI & ~0) */
340 bprm
->cap_post_exec_permitted
= cap_combine(
341 current
->cap_bset
, current
->cap_inheritable
343 bprm
->cap_effective
= (bprm
->e_uid
== 0);
351 void cap_bprm_apply_creds (struct linux_binprm
*bprm
, int unsafe
)
353 if (bprm
->e_uid
!= current
->uid
|| bprm
->e_gid
!= current
->gid
||
354 !cap_issubset(bprm
->cap_post_exec_permitted
,
355 current
->cap_permitted
)) {
356 set_dumpable(current
->mm
, suid_dumpable
);
357 current
->pdeath_signal
= 0;
359 if (unsafe
& ~LSM_UNSAFE_PTRACE_CAP
) {
360 if (!capable(CAP_SETUID
)) {
361 bprm
->e_uid
= current
->uid
;
362 bprm
->e_gid
= current
->gid
;
364 if (cap_limit_ptraced_target()) {
365 bprm
->cap_post_exec_permitted
= cap_intersect(
366 bprm
->cap_post_exec_permitted
,
367 current
->cap_permitted
);
372 current
->suid
= current
->euid
= current
->fsuid
= bprm
->e_uid
;
373 current
->sgid
= current
->egid
= current
->fsgid
= bprm
->e_gid
;
375 /* For init, we want to retain the capabilities set
376 * in the init_task struct. Thus we skip the usual
377 * capability rules */
378 if (!is_global_init(current
)) {
379 current
->cap_permitted
= bprm
->cap_post_exec_permitted
;
380 if (bprm
->cap_effective
)
381 current
->cap_effective
= bprm
->cap_post_exec_permitted
;
383 cap_clear(current
->cap_effective
);
386 /* AUD: Audit candidate if current->cap_effective is set */
388 current
->securebits
&= ~issecure_mask(SECURE_KEEP_CAPS
);
391 int cap_bprm_secureexec (struct linux_binprm
*bprm
)
393 if (current
->uid
!= 0) {
394 if (bprm
->cap_effective
)
396 if (!cap_isclear(bprm
->cap_post_exec_permitted
))
400 return (current
->euid
!= current
->uid
||
401 current
->egid
!= current
->gid
);
404 int cap_inode_setxattr(struct dentry
*dentry
, const char *name
,
405 const void *value
, size_t size
, int flags
)
407 if (!strcmp(name
, XATTR_NAME_CAPS
)) {
408 if (!capable(CAP_SETFCAP
))
411 } else if (!strncmp(name
, XATTR_SECURITY_PREFIX
,
412 sizeof(XATTR_SECURITY_PREFIX
) - 1) &&
413 !capable(CAP_SYS_ADMIN
))
418 int cap_inode_removexattr(struct dentry
*dentry
, const char *name
)
420 if (!strcmp(name
, XATTR_NAME_CAPS
)) {
421 if (!capable(CAP_SETFCAP
))
424 } else if (!strncmp(name
, XATTR_SECURITY_PREFIX
,
425 sizeof(XATTR_SECURITY_PREFIX
) - 1) &&
426 !capable(CAP_SYS_ADMIN
))
431 /* moved from kernel/sys.c. */
433 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
434 * a process after a call to setuid, setreuid, or setresuid.
436 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
437 * {r,e,s}uid != 0, the permitted and effective capabilities are
440 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
441 * capabilities of the process are cleared.
443 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
444 * capabilities are set to the permitted capabilities.
446 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
451 * cevans - New behaviour, Oct '99
452 * A process may, via prctl(), elect to keep its capabilities when it
453 * calls setuid() and switches away from uid==0. Both permitted and
454 * effective sets will be retained.
455 * Without this change, it was impossible for a daemon to drop only some
456 * of its privilege. The call to setuid(!=0) would drop all privileges!
457 * Keeping uid 0 is not an option because uid 0 owns too many vital
459 * Thanks to Olaf Kirch and Peter Benie for spotting this.
461 static inline void cap_emulate_setxuid (int old_ruid
, int old_euid
,
464 if ((old_ruid
== 0 || old_euid
== 0 || old_suid
== 0) &&
465 (current
->uid
!= 0 && current
->euid
!= 0 && current
->suid
!= 0) &&
466 !issecure(SECURE_KEEP_CAPS
)) {
467 cap_clear (current
->cap_permitted
);
468 cap_clear (current
->cap_effective
);
470 if (old_euid
== 0 && current
->euid
!= 0) {
471 cap_clear (current
->cap_effective
);
473 if (old_euid
!= 0 && current
->euid
== 0) {
474 current
->cap_effective
= current
->cap_permitted
;
478 int cap_task_post_setuid (uid_t old_ruid
, uid_t old_euid
, uid_t old_suid
,
485 /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
486 if (!issecure (SECURE_NO_SETUID_FIXUP
)) {
487 cap_emulate_setxuid (old_ruid
, old_euid
, old_suid
);
492 uid_t old_fsuid
= old_ruid
;
494 /* Copied from kernel/sys.c:setfsuid. */
497 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
498 * if not, we might be a bit too harsh here.
501 if (!issecure (SECURE_NO_SETUID_FIXUP
)) {
502 if (old_fsuid
== 0 && current
->fsuid
!= 0) {
503 current
->cap_effective
=
505 current
->cap_effective
);
507 if (old_fsuid
!= 0 && current
->fsuid
== 0) {
508 current
->cap_effective
=
510 current
->cap_effective
,
511 current
->cap_permitted
);
523 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
525 * Rationale: code calling task_setscheduler, task_setioprio, and
526 * task_setnice, assumes that
527 * . if capable(cap_sys_nice), then those actions should be allowed
528 * . if not capable(cap_sys_nice), but acting on your own processes,
529 * then those actions should be allowed
530 * This is insufficient now since you can call code without suid, but
531 * yet with increased caps.
532 * So we check for increased caps on the target process.
534 static inline int cap_safe_nice(struct task_struct
*p
)
536 if (!cap_issubset(p
->cap_permitted
, current
->cap_permitted
) &&
537 !__capable(current
, CAP_SYS_NICE
))
542 int cap_task_setscheduler (struct task_struct
*p
, int policy
,
543 struct sched_param
*lp
)
545 return cap_safe_nice(p
);
548 int cap_task_setioprio (struct task_struct
*p
, int ioprio
)
550 return cap_safe_nice(p
);
553 int cap_task_setnice (struct task_struct
*p
, int nice
)
555 return cap_safe_nice(p
);
559 * called from kernel/sys.c for prctl(PR_CABSET_DROP)
560 * done without task_capability_lock() because it introduces
561 * no new races - i.e. only another task doing capget() on
562 * this task could get inconsistent info. There can be no
563 * racing writer bc a task can only change its own caps.
565 static long cap_prctl_drop(unsigned long cap
)
567 if (!capable(CAP_SETPCAP
))
571 cap_lower(current
->cap_bset
, cap
);
576 int cap_task_setscheduler (struct task_struct
*p
, int policy
,
577 struct sched_param
*lp
)
581 int cap_task_setioprio (struct task_struct
*p
, int ioprio
)
585 int cap_task_setnice (struct task_struct
*p
, int nice
)
591 int cap_task_prctl(int option
, unsigned long arg2
, unsigned long arg3
,
592 unsigned long arg4
, unsigned long arg5
, long *rc_p
)
597 case PR_CAPBSET_READ
:
598 if (!cap_valid(arg2
))
601 error
= !!cap_raised(current
->cap_bset
, arg2
);
603 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
604 case PR_CAPBSET_DROP
:
605 error
= cap_prctl_drop(arg2
);
609 * The next four prctl's remain to assist with transitioning a
610 * system from legacy UID=0 based privilege (when filesystem
611 * capabilities are not in use) to a system using filesystem
612 * capabilities only - as the POSIX.1e draft intended.
616 * PR_SET_SECUREBITS =
617 * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
618 * | issecure_mask(SECURE_NOROOT)
619 * | issecure_mask(SECURE_NOROOT_LOCKED)
620 * | issecure_mask(SECURE_NO_SETUID_FIXUP)
621 * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
623 * will ensure that the current process and all of its
624 * children will be locked into a pure
625 * capability-based-privilege environment.
627 case PR_SET_SECUREBITS
:
628 if ((((current
->securebits
& SECURE_ALL_LOCKS
) >> 1)
629 & (current
->securebits
^ arg2
)) /*[1]*/
630 || ((current
->securebits
& SECURE_ALL_LOCKS
632 || (arg2
& ~(SECURE_ALL_LOCKS
| SECURE_ALL_BITS
)) /*[3]*/
633 || (cap_capable(current
, CAP_SETPCAP
) != 0)) { /*[4]*/
635 * [1] no changing of bits that are locked
636 * [2] no unlocking of locks
637 * [3] no setting of unsupported bits
638 * [4] doing anything requires privilege (go read about
639 * the "sendmail capabilities bug")
641 error
= -EPERM
; /* cannot change a locked bit */
643 current
->securebits
= arg2
;
646 case PR_GET_SECUREBITS
:
647 error
= current
->securebits
;
650 #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
652 case PR_GET_KEEPCAPS
:
653 if (issecure(SECURE_KEEP_CAPS
))
656 case PR_SET_KEEPCAPS
:
657 if (arg2
> 1) /* Note, we rely on arg2 being unsigned here */
659 else if (issecure(SECURE_KEEP_CAPS_LOCKED
))
662 current
->securebits
|= issecure_mask(SECURE_KEEP_CAPS
);
664 current
->securebits
&=
665 ~issecure_mask(SECURE_KEEP_CAPS
);
669 /* No functionality available - continue with default */
673 /* Functionality provided */
678 void cap_task_reparent_to_init (struct task_struct
*p
)
680 cap_set_init_eff(p
->cap_effective
);
681 cap_clear(p
->cap_inheritable
);
682 cap_set_full(p
->cap_permitted
);
683 p
->securebits
= SECUREBITS_DEFAULT
;
687 int cap_syslog (int type
)
689 if ((type
!= 3 && type
!= 10) && !capable(CAP_SYS_ADMIN
))
694 int cap_vm_enough_memory(struct mm_struct
*mm
, long pages
)
696 int cap_sys_admin
= 0;
698 if (cap_capable(current
, CAP_SYS_ADMIN
) == 0)
700 return __vm_enough_memory(mm
, pages
, cap_sys_admin
);