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>
28 /* Global security state */
30 unsigned securebits
= SECUREBITS_DEFAULT
; /* systemwide security settings */
31 EXPORT_SYMBOL(securebits
);
33 int cap_netlink_send(struct sock
*sk
, struct sk_buff
*skb
)
35 NETLINK_CB(skb
).eff_cap
= current
->cap_effective
;
39 int cap_netlink_recv(struct sk_buff
*skb
, int cap
)
41 if (!cap_raised(NETLINK_CB(skb
).eff_cap
, cap
))
46 EXPORT_SYMBOL(cap_netlink_recv
);
49 * NOTE WELL: cap_capable() cannot be used like the kernel's capable()
50 * function. That is, it has the reverse semantics: cap_capable()
51 * returns 0 when a task has a capability, but the kernel's capable()
52 * returns 1 for this case.
54 int cap_capable (struct task_struct
*tsk
, int cap
)
56 /* Derived from include/linux/sched.h:capable. */
57 if (cap_raised(tsk
->cap_effective
, cap
))
62 int cap_settime(struct timespec
*ts
, struct timezone
*tz
)
64 if (!capable(CAP_SYS_TIME
))
69 int cap_ptrace (struct task_struct
*parent
, struct task_struct
*child
)
71 /* Derived from arch/i386/kernel/ptrace.c:sys_ptrace. */
72 if (!cap_issubset(child
->cap_permitted
, parent
->cap_permitted
) &&
73 !__capable(parent
, CAP_SYS_PTRACE
))
78 int cap_capget (struct task_struct
*target
, kernel_cap_t
*effective
,
79 kernel_cap_t
*inheritable
, kernel_cap_t
*permitted
)
81 /* Derived from kernel/capability.c:sys_capget. */
82 *effective
= target
->cap_effective
;
83 *inheritable
= target
->cap_inheritable
;
84 *permitted
= target
->cap_permitted
;
88 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
90 static inline int cap_block_setpcap(struct task_struct
*target
)
93 * No support for remote process capability manipulation with
94 * filesystem capability support.
96 return (target
!= current
);
99 static inline int cap_inh_is_capped(void)
102 * Return 1 if changes to the inheritable set are limited
103 * to the old permitted set. That is, if the current task
104 * does *not* possess the CAP_SETPCAP capability.
106 return (cap_capable(current
, CAP_SETPCAP
) != 0);
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; }
114 #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
116 int cap_capset_check (struct task_struct
*target
, kernel_cap_t
*effective
,
117 kernel_cap_t
*inheritable
, kernel_cap_t
*permitted
)
119 if (cap_block_setpcap(target
)) {
122 if (cap_inh_is_capped()
123 && !cap_issubset(*inheritable
,
124 cap_combine(target
->cap_inheritable
,
125 current
->cap_permitted
))) {
126 /* incapable of using this inheritable set */
129 if (!cap_issubset(*inheritable
,
130 cap_combine(target
->cap_inheritable
,
131 current
->cap_bset
))) {
132 /* no new pI capabilities outside bounding set */
136 /* verify restrictions on target's new Permitted set */
137 if (!cap_issubset (*permitted
,
138 cap_combine (target
->cap_permitted
,
139 current
->cap_permitted
))) {
143 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
144 if (!cap_issubset (*effective
, *permitted
)) {
151 void cap_capset_set (struct task_struct
*target
, kernel_cap_t
*effective
,
152 kernel_cap_t
*inheritable
, kernel_cap_t
*permitted
)
154 target
->cap_effective
= *effective
;
155 target
->cap_inheritable
= *inheritable
;
156 target
->cap_permitted
= *permitted
;
159 static inline void bprm_clear_caps(struct linux_binprm
*bprm
)
161 cap_clear(bprm
->cap_inheritable
);
162 cap_clear(bprm
->cap_permitted
);
163 bprm
->cap_effective
= false;
166 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
168 int cap_inode_need_killpriv(struct dentry
*dentry
)
170 struct inode
*inode
= dentry
->d_inode
;
173 if (!inode
->i_op
|| !inode
->i_op
->getxattr
)
176 error
= inode
->i_op
->getxattr(dentry
, XATTR_NAME_CAPS
, NULL
, 0);
182 int cap_inode_killpriv(struct dentry
*dentry
)
184 struct inode
*inode
= dentry
->d_inode
;
186 if (!inode
->i_op
|| !inode
->i_op
->removexattr
)
189 return inode
->i_op
->removexattr(dentry
, XATTR_NAME_CAPS
);
192 static inline int cap_from_disk(struct vfs_cap_data
*caps
,
193 struct linux_binprm
*bprm
, unsigned size
)
198 if (size
< sizeof(magic_etc
))
201 magic_etc
= le32_to_cpu(caps
->magic_etc
);
203 switch ((magic_etc
& VFS_CAP_REVISION_MASK
)) {
204 case VFS_CAP_REVISION_1
:
205 if (size
!= XATTR_CAPS_SZ_1
)
207 tocopy
= VFS_CAP_U32_1
;
209 case VFS_CAP_REVISION_2
:
210 if (size
!= XATTR_CAPS_SZ_2
)
212 tocopy
= VFS_CAP_U32_2
;
218 if (magic_etc
& VFS_CAP_FLAGS_EFFECTIVE
) {
219 bprm
->cap_effective
= true;
221 bprm
->cap_effective
= false;
224 for (i
= 0; i
< tocopy
; ++i
) {
225 bprm
->cap_permitted
.cap
[i
] =
226 le32_to_cpu(caps
->data
[i
].permitted
);
227 bprm
->cap_inheritable
.cap
[i
] =
228 le32_to_cpu(caps
->data
[i
].inheritable
);
230 while (i
< VFS_CAP_U32
) {
231 bprm
->cap_permitted
.cap
[i
] = 0;
232 bprm
->cap_inheritable
.cap
[i
] = 0;
239 /* Locate any VFS capabilities: */
240 static int get_file_caps(struct linux_binprm
*bprm
)
242 struct dentry
*dentry
;
244 struct vfs_cap_data vcaps
;
247 if (bprm
->file
->f_vfsmnt
->mnt_flags
& MNT_NOSUID
) {
248 bprm_clear_caps(bprm
);
252 dentry
= dget(bprm
->file
->f_dentry
);
253 inode
= dentry
->d_inode
;
254 if (!inode
->i_op
|| !inode
->i_op
->getxattr
)
257 rc
= inode
->i_op
->getxattr(dentry
, XATTR_NAME_CAPS
, &vcaps
,
259 if (rc
== -ENODATA
|| rc
== -EOPNOTSUPP
) {
260 /* no data, that's ok */
267 rc
= cap_from_disk(&vcaps
, bprm
, rc
);
269 printk(KERN_NOTICE
"%s: cap_from_disk returned %d for %s\n",
270 __FUNCTION__
, rc
, bprm
->filename
);
275 bprm_clear_caps(bprm
);
281 int cap_inode_need_killpriv(struct dentry
*dentry
)
286 int cap_inode_killpriv(struct dentry
*dentry
)
291 static inline int get_file_caps(struct linux_binprm
*bprm
)
293 bprm_clear_caps(bprm
);
298 int cap_bprm_set_security (struct linux_binprm
*bprm
)
302 ret
= get_file_caps(bprm
);
304 printk(KERN_NOTICE
"%s: get_file_caps returned %d for %s\n",
305 __FUNCTION__
, ret
, bprm
->filename
);
307 /* To support inheritance of root-permissions and suid-root
308 * executables under compatibility mode, we raise all three
309 * capability sets for the file.
311 * If only the real uid is 0, we only raise the inheritable
312 * and permitted sets of the executable file.
315 if (!issecure (SECURE_NOROOT
)) {
316 if (bprm
->e_uid
== 0 || current
->uid
== 0) {
317 cap_set_full (bprm
->cap_inheritable
);
318 cap_set_full (bprm
->cap_permitted
);
320 if (bprm
->e_uid
== 0)
321 bprm
->cap_effective
= true;
327 void cap_bprm_apply_creds (struct linux_binprm
*bprm
, int unsafe
)
329 /* Derived from fs/exec.c:compute_creds. */
330 kernel_cap_t new_permitted
, working
;
332 new_permitted
= cap_intersect(bprm
->cap_permitted
,
334 working
= cap_intersect(bprm
->cap_inheritable
,
335 current
->cap_inheritable
);
336 new_permitted
= cap_combine(new_permitted
, working
);
338 if (bprm
->e_uid
!= current
->uid
|| bprm
->e_gid
!= current
->gid
||
339 !cap_issubset (new_permitted
, current
->cap_permitted
)) {
340 set_dumpable(current
->mm
, suid_dumpable
);
341 current
->pdeath_signal
= 0;
343 if (unsafe
& ~LSM_UNSAFE_PTRACE_CAP
) {
344 if (!capable(CAP_SETUID
)) {
345 bprm
->e_uid
= current
->uid
;
346 bprm
->e_gid
= current
->gid
;
348 if (!capable (CAP_SETPCAP
)) {
349 new_permitted
= cap_intersect (new_permitted
,
350 current
->cap_permitted
);
355 current
->suid
= current
->euid
= current
->fsuid
= bprm
->e_uid
;
356 current
->sgid
= current
->egid
= current
->fsgid
= bprm
->e_gid
;
358 /* For init, we want to retain the capabilities set
359 * in the init_task struct. Thus we skip the usual
360 * capability rules */
361 if (!is_global_init(current
)) {
362 current
->cap_permitted
= new_permitted
;
363 if (bprm
->cap_effective
)
364 current
->cap_effective
= new_permitted
;
366 cap_clear(current
->cap_effective
);
369 /* AUD: Audit candidate if current->cap_effective is set */
371 current
->keep_capabilities
= 0;
374 int cap_bprm_secureexec (struct linux_binprm
*bprm
)
376 if (current
->uid
!= 0) {
377 if (bprm
->cap_effective
)
379 if (!cap_isclear(bprm
->cap_permitted
))
381 if (!cap_isclear(bprm
->cap_inheritable
))
385 return (current
->euid
!= current
->uid
||
386 current
->egid
!= current
->gid
);
389 int cap_inode_setxattr(struct dentry
*dentry
, char *name
, void *value
,
390 size_t size
, int flags
)
392 if (!strcmp(name
, XATTR_NAME_CAPS
)) {
393 if (!capable(CAP_SETFCAP
))
396 } else if (!strncmp(name
, XATTR_SECURITY_PREFIX
,
397 sizeof(XATTR_SECURITY_PREFIX
) - 1) &&
398 !capable(CAP_SYS_ADMIN
))
403 int cap_inode_removexattr(struct dentry
*dentry
, char *name
)
405 if (!strcmp(name
, XATTR_NAME_CAPS
)) {
406 if (!capable(CAP_SETFCAP
))
409 } else if (!strncmp(name
, XATTR_SECURITY_PREFIX
,
410 sizeof(XATTR_SECURITY_PREFIX
) - 1) &&
411 !capable(CAP_SYS_ADMIN
))
416 /* moved from kernel/sys.c. */
418 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
419 * a process after a call to setuid, setreuid, or setresuid.
421 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
422 * {r,e,s}uid != 0, the permitted and effective capabilities are
425 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
426 * capabilities of the process are cleared.
428 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
429 * capabilities are set to the permitted capabilities.
431 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
436 * cevans - New behaviour, Oct '99
437 * A process may, via prctl(), elect to keep its capabilities when it
438 * calls setuid() and switches away from uid==0. Both permitted and
439 * effective sets will be retained.
440 * Without this change, it was impossible for a daemon to drop only some
441 * of its privilege. The call to setuid(!=0) would drop all privileges!
442 * Keeping uid 0 is not an option because uid 0 owns too many vital
444 * Thanks to Olaf Kirch and Peter Benie for spotting this.
446 static inline void cap_emulate_setxuid (int old_ruid
, int old_euid
,
449 if ((old_ruid
== 0 || old_euid
== 0 || old_suid
== 0) &&
450 (current
->uid
!= 0 && current
->euid
!= 0 && current
->suid
!= 0) &&
451 !current
->keep_capabilities
) {
452 cap_clear (current
->cap_permitted
);
453 cap_clear (current
->cap_effective
);
455 if (old_euid
== 0 && current
->euid
!= 0) {
456 cap_clear (current
->cap_effective
);
458 if (old_euid
!= 0 && current
->euid
== 0) {
459 current
->cap_effective
= current
->cap_permitted
;
463 int cap_task_post_setuid (uid_t old_ruid
, uid_t old_euid
, uid_t old_suid
,
470 /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
471 if (!issecure (SECURE_NO_SETUID_FIXUP
)) {
472 cap_emulate_setxuid (old_ruid
, old_euid
, old_suid
);
477 uid_t old_fsuid
= old_ruid
;
479 /* Copied from kernel/sys.c:setfsuid. */
482 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
483 * if not, we might be a bit too harsh here.
486 if (!issecure (SECURE_NO_SETUID_FIXUP
)) {
487 if (old_fsuid
== 0 && current
->fsuid
!= 0) {
488 current
->cap_effective
=
490 current
->cap_effective
);
492 if (old_fsuid
!= 0 && current
->fsuid
== 0) {
493 current
->cap_effective
=
495 current
->cap_effective
,
496 current
->cap_permitted
);
508 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
510 * Rationale: code calling task_setscheduler, task_setioprio, and
511 * task_setnice, assumes that
512 * . if capable(cap_sys_nice), then those actions should be allowed
513 * . if not capable(cap_sys_nice), but acting on your own processes,
514 * then those actions should be allowed
515 * This is insufficient now since you can call code without suid, but
516 * yet with increased caps.
517 * So we check for increased caps on the target process.
519 static inline int cap_safe_nice(struct task_struct
*p
)
521 if (!cap_issubset(p
->cap_permitted
, current
->cap_permitted
) &&
522 !__capable(current
, CAP_SYS_NICE
))
527 int cap_task_setscheduler (struct task_struct
*p
, int policy
,
528 struct sched_param
*lp
)
530 return cap_safe_nice(p
);
533 int cap_task_setioprio (struct task_struct
*p
, int ioprio
)
535 return cap_safe_nice(p
);
538 int cap_task_setnice (struct task_struct
*p
, int nice
)
540 return cap_safe_nice(p
);
543 int cap_task_kill(struct task_struct
*p
, struct siginfo
*info
,
546 if (info
!= SEND_SIG_NOINFO
&& (is_si_special(info
) || SI_FROMKERNEL(info
)))
550 * Running a setuid root program raises your capabilities.
551 * Killing your own setuid root processes was previously
553 * We must preserve legacy signal behavior in this case.
555 if (p
->euid
== 0 && p
->uid
== current
->uid
)
558 /* sigcont is permitted within same session */
559 if (sig
== SIGCONT
&& (task_session_nr(current
) == task_session_nr(p
)))
564 * Signal sent as a particular user.
565 * Capabilities are ignored. May be wrong, but it's the
566 * only thing we can do at the moment.
567 * Used only by usb drivers?
570 if (cap_issubset(p
->cap_permitted
, current
->cap_permitted
))
572 if (capable(CAP_KILL
))
579 * called from kernel/sys.c for prctl(PR_CABSET_DROP)
580 * done without task_capability_lock() because it introduces
581 * no new races - i.e. only another task doing capget() on
582 * this task could get inconsistent info. There can be no
583 * racing writer bc a task can only change its own caps.
585 long cap_prctl_drop(unsigned long cap
)
587 if (!capable(CAP_SETPCAP
))
591 cap_lower(current
->cap_bset
, cap
);
595 int cap_task_setscheduler (struct task_struct
*p
, int policy
,
596 struct sched_param
*lp
)
600 int cap_task_setioprio (struct task_struct
*p
, int ioprio
)
604 int cap_task_setnice (struct task_struct
*p
, int nice
)
608 int cap_task_kill(struct task_struct
*p
, struct siginfo
*info
,
615 void cap_task_reparent_to_init (struct task_struct
*p
)
617 cap_set_init_eff(p
->cap_effective
);
618 cap_clear(p
->cap_inheritable
);
619 cap_set_full(p
->cap_permitted
);
620 p
->keep_capabilities
= 0;
624 int cap_syslog (int type
)
626 if ((type
!= 3 && type
!= 10) && !capable(CAP_SYS_ADMIN
))
631 int cap_vm_enough_memory(struct mm_struct
*mm
, long pages
)
633 int cap_sys_admin
= 0;
635 if (cap_capable(current
, CAP_SYS_ADMIN
) == 0)
637 return __vm_enough_memory(mm
, pages
, cap_sys_admin
);