]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - security/commoncap.c
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(child
->cred
->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 parent
->cred
->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_check(const kernel_cap_t
*effective
,
136 const kernel_cap_t
*inheritable
,
137 const kernel_cap_t
*permitted
)
139 const struct cred
*cred
= current
->cred
;
141 if (cap_inh_is_capped()
142 && !cap_issubset(*inheritable
,
143 cap_combine(cred
->cap_inheritable
,
144 cred
->cap_permitted
))) {
145 /* incapable of using this inheritable set */
148 if (!cap_issubset(*inheritable
,
149 cap_combine(cred
->cap_inheritable
,
151 /* no new pI capabilities outside bounding set */
155 /* verify restrictions on target's new Permitted set */
156 if (!cap_issubset (*permitted
,
157 cap_combine (cred
->cap_permitted
,
158 cred
->cap_permitted
))) {
162 /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
163 if (!cap_issubset (*effective
, *permitted
)) {
170 void cap_capset_set(const kernel_cap_t
*effective
,
171 const kernel_cap_t
*inheritable
,
172 const kernel_cap_t
*permitted
)
174 struct cred
*cred
= current
->cred
;
176 cred
->cap_effective
= *effective
;
177 cred
->cap_inheritable
= *inheritable
;
178 cred
->cap_permitted
= *permitted
;
181 static inline void bprm_clear_caps(struct linux_binprm
*bprm
)
183 cap_clear(bprm
->cap_post_exec_permitted
);
184 bprm
->cap_effective
= false;
187 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
189 int cap_inode_need_killpriv(struct dentry
*dentry
)
191 struct inode
*inode
= dentry
->d_inode
;
194 if (!inode
->i_op
|| !inode
->i_op
->getxattr
)
197 error
= inode
->i_op
->getxattr(dentry
, XATTR_NAME_CAPS
, NULL
, 0);
203 int cap_inode_killpriv(struct dentry
*dentry
)
205 struct inode
*inode
= dentry
->d_inode
;
207 if (!inode
->i_op
|| !inode
->i_op
->removexattr
)
210 return inode
->i_op
->removexattr(dentry
, XATTR_NAME_CAPS
);
213 static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data
*caps
,
214 struct linux_binprm
*bprm
)
219 if (caps
->magic_etc
& VFS_CAP_FLAGS_EFFECTIVE
)
220 bprm
->cap_effective
= true;
222 bprm
->cap_effective
= false;
224 CAP_FOR_EACH_U32(i
) {
225 __u32 permitted
= caps
->permitted
.cap
[i
];
226 __u32 inheritable
= caps
->inheritable
.cap
[i
];
229 * pP' = (X & fP) | (pI & fI)
231 bprm
->cap_post_exec_permitted
.cap
[i
] =
232 (current
->cred
->cap_bset
.cap
[i
] & permitted
) |
233 (current
->cred
->cap_inheritable
.cap
[i
] & inheritable
);
235 if (permitted
& ~bprm
->cap_post_exec_permitted
.cap
[i
]) {
237 * insufficient to execute correctly
244 * For legacy apps, with no internal support for recognizing they
245 * do not have enough capabilities, we return an error if they are
246 * missing some "forced" (aka file-permitted) capabilities.
248 return bprm
->cap_effective
? ret
: 0;
251 int get_vfs_caps_from_disk(const struct dentry
*dentry
, struct cpu_vfs_cap_data
*cpu_caps
)
253 struct inode
*inode
= dentry
->d_inode
;
257 struct vfs_cap_data caps
;
259 memset(cpu_caps
, 0, sizeof(struct cpu_vfs_cap_data
));
261 if (!inode
|| !inode
->i_op
|| !inode
->i_op
->getxattr
)
264 size
= inode
->i_op
->getxattr((struct dentry
*)dentry
, XATTR_NAME_CAPS
, &caps
,
266 if (size
== -ENODATA
|| size
== -EOPNOTSUPP
) {
267 /* no data, that's ok */
273 if (size
< sizeof(magic_etc
))
276 cpu_caps
->magic_etc
= magic_etc
= le32_to_cpu(caps
.magic_etc
);
278 switch ((magic_etc
& VFS_CAP_REVISION_MASK
)) {
279 case VFS_CAP_REVISION_1
:
280 if (size
!= XATTR_CAPS_SZ_1
)
282 tocopy
= VFS_CAP_U32_1
;
284 case VFS_CAP_REVISION_2
:
285 if (size
!= XATTR_CAPS_SZ_2
)
287 tocopy
= VFS_CAP_U32_2
;
293 CAP_FOR_EACH_U32(i
) {
296 cpu_caps
->permitted
.cap
[i
] = le32_to_cpu(caps
.data
[i
].permitted
);
297 cpu_caps
->inheritable
.cap
[i
] = le32_to_cpu(caps
.data
[i
].inheritable
);
302 /* Locate any VFS capabilities: */
303 static int get_file_caps(struct linux_binprm
*bprm
)
305 struct dentry
*dentry
;
307 struct cpu_vfs_cap_data vcaps
;
309 bprm_clear_caps(bprm
);
311 if (!file_caps_enabled
)
314 if (bprm
->file
->f_vfsmnt
->mnt_flags
& MNT_NOSUID
)
317 dentry
= dget(bprm
->file
->f_dentry
);
319 rc
= get_vfs_caps_from_disk(dentry
, &vcaps
);
322 printk(KERN_NOTICE
"%s: get_vfs_caps_from_disk returned %d for %s\n",
323 __func__
, rc
, bprm
->filename
);
324 else if (rc
== -ENODATA
)
329 rc
= bprm_caps_from_vfs_caps(&vcaps
, bprm
);
334 bprm_clear_caps(bprm
);
340 int cap_inode_need_killpriv(struct dentry
*dentry
)
345 int cap_inode_killpriv(struct dentry
*dentry
)
350 static inline int get_file_caps(struct linux_binprm
*bprm
)
352 bprm_clear_caps(bprm
);
357 int cap_bprm_set_security (struct linux_binprm
*bprm
)
361 ret
= get_file_caps(bprm
);
363 if (!issecure(SECURE_NOROOT
)) {
365 * To support inheritance of root-permissions and suid-root
366 * executables under compatibility mode, we override the
367 * capability sets for the file.
369 * If only the real uid is 0, we do not set the effective
372 if (bprm
->e_uid
== 0 || current_uid() == 0) {
373 /* pP' = (cap_bset & ~0) | (pI & ~0) */
374 bprm
->cap_post_exec_permitted
= cap_combine(
375 current
->cred
->cap_bset
,
376 current
->cred
->cap_inheritable
);
377 bprm
->cap_effective
= (bprm
->e_uid
== 0);
385 void cap_bprm_apply_creds (struct linux_binprm
*bprm
, int unsafe
)
387 struct cred
*cred
= current
->cred
;
389 if (bprm
->e_uid
!= cred
->uid
|| bprm
->e_gid
!= cred
->gid
||
390 !cap_issubset(bprm
->cap_post_exec_permitted
,
391 cred
->cap_permitted
)) {
392 set_dumpable(current
->mm
, suid_dumpable
);
393 current
->pdeath_signal
= 0;
395 if (unsafe
& ~LSM_UNSAFE_PTRACE_CAP
) {
396 if (!capable(CAP_SETUID
)) {
397 bprm
->e_uid
= cred
->uid
;
398 bprm
->e_gid
= cred
->gid
;
400 if (cap_limit_ptraced_target()) {
401 bprm
->cap_post_exec_permitted
= cap_intersect(
402 bprm
->cap_post_exec_permitted
,
403 cred
->cap_permitted
);
408 cred
->suid
= cred
->euid
= cred
->fsuid
= bprm
->e_uid
;
409 cred
->sgid
= cred
->egid
= cred
->fsgid
= bprm
->e_gid
;
411 /* For init, we want to retain the capabilities set
412 * in the init_task struct. Thus we skip the usual
413 * capability rules */
414 if (!is_global_init(current
)) {
415 cred
->cap_permitted
= bprm
->cap_post_exec_permitted
;
416 if (bprm
->cap_effective
)
417 cred
->cap_effective
= bprm
->cap_post_exec_permitted
;
419 cap_clear(cred
->cap_effective
);
423 * Audit candidate if current->cap_effective is set
425 * We do not bother to audit if 3 things are true:
426 * 1) cap_effective has all caps
428 * 3) root is supposed to have all caps (SECURE_NOROOT)
429 * Since this is just a normal root execing a process.
431 * Number 1 above might fail if you don't have a full bset, but I think
432 * that is interesting information to audit.
434 if (!cap_isclear(cred
->cap_effective
)) {
435 if (!cap_issubset(CAP_FULL_SET
, cred
->cap_effective
) ||
436 (bprm
->e_uid
!= 0) || (cred
->uid
!= 0) ||
437 issecure(SECURE_NOROOT
))
438 audit_log_bprm_fcaps(bprm
, &cred
->cap_permitted
,
439 &cred
->cap_effective
);
442 cred
->securebits
&= ~issecure_mask(SECURE_KEEP_CAPS
);
445 int cap_bprm_secureexec (struct linux_binprm
*bprm
)
447 const struct cred
*cred
= current_cred();
449 if (cred
->uid
!= 0) {
450 if (bprm
->cap_effective
)
452 if (!cap_isclear(bprm
->cap_post_exec_permitted
))
456 return (cred
->euid
!= cred
->uid
||
457 cred
->egid
!= cred
->gid
);
460 int cap_inode_setxattr(struct dentry
*dentry
, const char *name
,
461 const void *value
, size_t size
, int flags
)
463 if (!strcmp(name
, XATTR_NAME_CAPS
)) {
464 if (!capable(CAP_SETFCAP
))
467 } else if (!strncmp(name
, XATTR_SECURITY_PREFIX
,
468 sizeof(XATTR_SECURITY_PREFIX
) - 1) &&
469 !capable(CAP_SYS_ADMIN
))
474 int cap_inode_removexattr(struct dentry
*dentry
, const char *name
)
476 if (!strcmp(name
, XATTR_NAME_CAPS
)) {
477 if (!capable(CAP_SETFCAP
))
480 } else if (!strncmp(name
, XATTR_SECURITY_PREFIX
,
481 sizeof(XATTR_SECURITY_PREFIX
) - 1) &&
482 !capable(CAP_SYS_ADMIN
))
487 /* moved from kernel/sys.c. */
489 * cap_emulate_setxuid() fixes the effective / permitted capabilities of
490 * a process after a call to setuid, setreuid, or setresuid.
492 * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
493 * {r,e,s}uid != 0, the permitted and effective capabilities are
496 * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
497 * capabilities of the process are cleared.
499 * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
500 * capabilities are set to the permitted capabilities.
502 * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
507 * cevans - New behaviour, Oct '99
508 * A process may, via prctl(), elect to keep its capabilities when it
509 * calls setuid() and switches away from uid==0. Both permitted and
510 * effective sets will be retained.
511 * Without this change, it was impossible for a daemon to drop only some
512 * of its privilege. The call to setuid(!=0) would drop all privileges!
513 * Keeping uid 0 is not an option because uid 0 owns too many vital
515 * Thanks to Olaf Kirch and Peter Benie for spotting this.
517 static inline void cap_emulate_setxuid (int old_ruid
, int old_euid
,
520 struct cred
*cred
= current
->cred
;
522 if ((old_ruid
== 0 || old_euid
== 0 || old_suid
== 0) &&
523 (cred
->uid
!= 0 && cred
->euid
!= 0 && cred
->suid
!= 0) &&
524 !issecure(SECURE_KEEP_CAPS
)) {
525 cap_clear(cred
->cap_permitted
);
526 cap_clear(cred
->cap_effective
);
528 if (old_euid
== 0 && cred
->euid
!= 0) {
529 cap_clear(cred
->cap_effective
);
531 if (old_euid
!= 0 && cred
->euid
== 0) {
532 cred
->cap_effective
= cred
->cap_permitted
;
536 int cap_task_post_setuid (uid_t old_ruid
, uid_t old_euid
, uid_t old_suid
,
539 struct cred
*cred
= current
->cred
;
545 /* Copied from kernel/sys.c:setreuid/setuid/setresuid. */
546 if (!issecure (SECURE_NO_SETUID_FIXUP
)) {
547 cap_emulate_setxuid (old_ruid
, old_euid
, old_suid
);
552 uid_t old_fsuid
= old_ruid
;
554 /* Copied from kernel/sys.c:setfsuid. */
557 * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
558 * if not, we might be a bit too harsh here.
561 if (!issecure (SECURE_NO_SETUID_FIXUP
)) {
562 if (old_fsuid
== 0 && cred
->fsuid
!= 0) {
563 cred
->cap_effective
=
565 cred
->cap_effective
);
567 if (old_fsuid
!= 0 && cred
->fsuid
== 0) {
568 cred
->cap_effective
=
571 cred
->cap_permitted
);
583 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
585 * Rationale: code calling task_setscheduler, task_setioprio, and
586 * task_setnice, assumes that
587 * . if capable(cap_sys_nice), then those actions should be allowed
588 * . if not capable(cap_sys_nice), but acting on your own processes,
589 * then those actions should be allowed
590 * This is insufficient now since you can call code without suid, but
591 * yet with increased caps.
592 * So we check for increased caps on the target process.
594 static int cap_safe_nice(struct task_struct
*p
)
599 is_subset
= cap_issubset(__task_cred(p
)->cap_permitted
,
600 current_cred()->cap_permitted
);
603 if (!is_subset
&& !capable(CAP_SYS_NICE
))
608 int cap_task_setscheduler (struct task_struct
*p
, int policy
,
609 struct sched_param
*lp
)
611 return cap_safe_nice(p
);
614 int cap_task_setioprio (struct task_struct
*p
, int ioprio
)
616 return cap_safe_nice(p
);
619 int cap_task_setnice (struct task_struct
*p
, int nice
)
621 return cap_safe_nice(p
);
625 * called from kernel/sys.c for prctl(PR_CABSET_DROP)
626 * done without task_capability_lock() because it introduces
627 * no new races - i.e. only another task doing capget() on
628 * this task could get inconsistent info. There can be no
629 * racing writer bc a task can only change its own caps.
631 static long cap_prctl_drop(unsigned long cap
)
633 if (!capable(CAP_SETPCAP
))
637 cap_lower(current
->cred
->cap_bset
, cap
);
642 int cap_task_setscheduler (struct task_struct
*p
, int policy
,
643 struct sched_param
*lp
)
647 int cap_task_setioprio (struct task_struct
*p
, int ioprio
)
651 int cap_task_setnice (struct task_struct
*p
, int nice
)
657 int cap_task_prctl(int option
, unsigned long arg2
, unsigned long arg3
,
658 unsigned long arg4
, unsigned long arg5
, long *rc_p
)
660 struct cred
*cred
= current_cred();
664 case PR_CAPBSET_READ
:
665 if (!cap_valid(arg2
))
668 error
= !!cap_raised(cred
->cap_bset
, arg2
);
670 #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
671 case PR_CAPBSET_DROP
:
672 error
= cap_prctl_drop(arg2
);
676 * The next four prctl's remain to assist with transitioning a
677 * system from legacy UID=0 based privilege (when filesystem
678 * capabilities are not in use) to a system using filesystem
679 * capabilities only - as the POSIX.1e draft intended.
683 * PR_SET_SECUREBITS =
684 * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
685 * | issecure_mask(SECURE_NOROOT)
686 * | issecure_mask(SECURE_NOROOT_LOCKED)
687 * | issecure_mask(SECURE_NO_SETUID_FIXUP)
688 * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
690 * will ensure that the current process and all of its
691 * children will be locked into a pure
692 * capability-based-privilege environment.
694 case PR_SET_SECUREBITS
:
695 if ((((cred
->securebits
& SECURE_ALL_LOCKS
) >> 1)
696 & (cred
->securebits
^ arg2
)) /*[1]*/
697 || ((cred
->securebits
& SECURE_ALL_LOCKS
699 || (arg2
& ~(SECURE_ALL_LOCKS
| SECURE_ALL_BITS
)) /*[3]*/
700 || (cap_capable(current
, CAP_SETPCAP
, SECURITY_CAP_AUDIT
) != 0)) { /*[4]*/
702 * [1] no changing of bits that are locked
703 * [2] no unlocking of locks
704 * [3] no setting of unsupported bits
705 * [4] doing anything requires privilege (go read about
706 * the "sendmail capabilities bug")
708 error
= -EPERM
; /* cannot change a locked bit */
710 cred
->securebits
= arg2
;
713 case PR_GET_SECUREBITS
:
714 error
= cred
->securebits
;
717 #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
719 case PR_GET_KEEPCAPS
:
720 if (issecure(SECURE_KEEP_CAPS
))
723 case PR_SET_KEEPCAPS
:
724 if (arg2
> 1) /* Note, we rely on arg2 being unsigned here */
726 else if (issecure(SECURE_KEEP_CAPS_LOCKED
))
729 cred
->securebits
|= issecure_mask(SECURE_KEEP_CAPS
);
731 cred
->securebits
&= ~issecure_mask(SECURE_KEEP_CAPS
);
735 /* No functionality available - continue with default */
739 /* Functionality provided */
744 void cap_task_reparent_to_init (struct task_struct
*p
)
746 struct cred
*cred
= p
->cred
;
748 cap_set_init_eff(cred
->cap_effective
);
749 cap_clear(cred
->cap_inheritable
);
750 cap_set_full(cred
->cap_permitted
);
751 p
->cred
->securebits
= SECUREBITS_DEFAULT
;
754 int cap_syslog (int type
)
756 if ((type
!= 3 && type
!= 10) && !capable(CAP_SYS_ADMIN
))
761 int cap_vm_enough_memory(struct mm_struct
*mm
, long pages
)
763 int cap_sys_admin
= 0;
765 if (cap_capable(current
, CAP_SYS_ADMIN
, SECURITY_CAP_NOAUDIT
) == 0)
767 return __vm_enough_memory(mm
, pages
, cap_sys_admin
);