1 // SPDX-License-Identifier: GPL-2.0-only
3 * NSA Security-Enhanced Linux (SELinux) security module
5 * This file contains the SELinux hook function implementations.
7 * Authors: Stephen Smalley, <sds@tycho.nsa.gov>
8 * Chris Vance, <cvance@nai.com>
9 * Wayne Salamon, <wsalamon@nai.com>
10 * James Morris <jmorris@redhat.com>
12 * Copyright (C) 2001,2002 Networks Associates Technology, Inc.
13 * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
14 * Eric Paris <eparis@redhat.com>
15 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
16 * <dgoeddel@trustedcs.com>
17 * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
18 * Paul Moore <paul@paul-moore.com>
19 * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
20 * Yuichi Nakamura <ynakam@hitachisoft.jp>
21 * Copyright (C) 2016 Mellanox Technologies
24 #include <linux/init.h>
26 #include <linux/kernel.h>
27 #include <linux/kernel_read_file.h>
28 #include <linux/tracehook.h>
29 #include <linux/errno.h>
30 #include <linux/sched/signal.h>
31 #include <linux/sched/task.h>
32 #include <linux/lsm_hooks.h>
33 #include <linux/xattr.h>
34 #include <linux/capability.h>
35 #include <linux/unistd.h>
37 #include <linux/mman.h>
38 #include <linux/slab.h>
39 #include <linux/pagemap.h>
40 #include <linux/proc_fs.h>
41 #include <linux/swap.h>
42 #include <linux/spinlock.h>
43 #include <linux/syscalls.h>
44 #include <linux/dcache.h>
45 #include <linux/file.h>
46 #include <linux/fdtable.h>
47 #include <linux/namei.h>
48 #include <linux/mount.h>
49 #include <linux/fs_context.h>
50 #include <linux/fs_parser.h>
51 #include <linux/netfilter_ipv4.h>
52 #include <linux/netfilter_ipv6.h>
53 #include <linux/tty.h>
55 #include <net/ip.h> /* for local_port_range[] */
56 #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
57 #include <net/inet_connection_sock.h>
58 #include <net/net_namespace.h>
59 #include <net/netlabel.h>
60 #include <linux/uaccess.h>
61 #include <asm/ioctls.h>
62 #include <linux/atomic.h>
63 #include <linux/bitops.h>
64 #include <linux/interrupt.h>
65 #include <linux/netdevice.h> /* for network interface checks */
66 #include <net/netlink.h>
67 #include <linux/tcp.h>
68 #include <linux/udp.h>
69 #include <linux/dccp.h>
70 #include <linux/sctp.h>
71 #include <net/sctp/structs.h>
72 #include <linux/quota.h>
73 #include <linux/un.h> /* for Unix socket types */
74 #include <net/af_unix.h> /* for Unix socket types */
75 #include <linux/parser.h>
76 #include <linux/nfs_mount.h>
78 #include <linux/hugetlb.h>
79 #include <linux/personality.h>
80 #include <linux/audit.h>
81 #include <linux/string.h>
82 #include <linux/mutex.h>
83 #include <linux/posix-timers.h>
84 #include <linux/syslog.h>
85 #include <linux/user_namespace.h>
86 #include <linux/export.h>
87 #include <linux/msg.h>
88 #include <linux/shm.h>
89 #include <linux/bpf.h>
90 #include <linux/kernfs.h>
91 #include <linux/stringhash.h> /* for hashlen_string() */
92 #include <uapi/linux/mount.h>
93 #include <linux/fsnotify.h>
94 #include <linux/fanotify.h>
103 #include "netlabel.h"
107 struct selinux_state selinux_state
;
109 /* SECMARK reference count */
110 static atomic_t selinux_secmark_refcount
= ATOMIC_INIT(0);
112 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
113 static int selinux_enforcing_boot __initdata
;
115 static int __init
enforcing_setup(char *str
)
117 unsigned long enforcing
;
118 if (!kstrtoul(str
, 0, &enforcing
))
119 selinux_enforcing_boot
= enforcing
? 1 : 0;
122 __setup("enforcing=", enforcing_setup
);
124 #define selinux_enforcing_boot 1
127 int selinux_enabled_boot __initdata
= 1;
128 #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
129 static int __init
selinux_enabled_setup(char *str
)
131 unsigned long enabled
;
132 if (!kstrtoul(str
, 0, &enabled
))
133 selinux_enabled_boot
= enabled
? 1 : 0;
136 __setup("selinux=", selinux_enabled_setup
);
139 static unsigned int selinux_checkreqprot_boot
=
140 CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE
;
142 static int __init
checkreqprot_setup(char *str
)
144 unsigned long checkreqprot
;
146 if (!kstrtoul(str
, 0, &checkreqprot
)) {
147 selinux_checkreqprot_boot
= checkreqprot
? 1 : 0;
149 pr_warn("SELinux: checkreqprot set to 1 via kernel parameter. This is deprecated and will be rejected in a future kernel release.\n");
153 __setup("checkreqprot=", checkreqprot_setup
);
156 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
159 * This function checks the SECMARK reference counter to see if any SECMARK
160 * targets are currently configured, if the reference counter is greater than
161 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
162 * enabled, false (0) if SECMARK is disabled. If the always_check_network
163 * policy capability is enabled, SECMARK is always considered enabled.
166 static int selinux_secmark_enabled(void)
168 return (selinux_policycap_alwaysnetwork() ||
169 atomic_read(&selinux_secmark_refcount
));
173 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
176 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
177 * (1) if any are enabled or false (0) if neither are enabled. If the
178 * always_check_network policy capability is enabled, peer labeling
179 * is always considered enabled.
182 static int selinux_peerlbl_enabled(void)
184 return (selinux_policycap_alwaysnetwork() ||
185 netlbl_enabled() || selinux_xfrm_enabled());
188 static int selinux_netcache_avc_callback(u32 event
)
190 if (event
== AVC_CALLBACK_RESET
) {
199 static int selinux_lsm_notifier_avc_callback(u32 event
)
201 if (event
== AVC_CALLBACK_RESET
) {
203 call_blocking_lsm_notifier(LSM_POLICY_CHANGE
, NULL
);
210 * initialise the security for the init task
212 static void cred_init_security(void)
214 struct cred
*cred
= (struct cred
*) current
->real_cred
;
215 struct task_security_struct
*tsec
;
217 tsec
= selinux_cred(cred
);
218 tsec
->osid
= tsec
->sid
= SECINITSID_KERNEL
;
222 * get the security ID of a set of credentials
224 static inline u32
cred_sid(const struct cred
*cred
)
226 const struct task_security_struct
*tsec
;
228 tsec
= selinux_cred(cred
);
233 * get the subjective security ID of a task
235 static inline u32
task_sid_subj(const struct task_struct
*task
)
240 sid
= cred_sid(rcu_dereference(task
->cred
));
246 * get the objective security ID of a task
248 static inline u32
task_sid_obj(const struct task_struct
*task
)
253 sid
= cred_sid(__task_cred(task
));
258 static int inode_doinit_with_dentry(struct inode
*inode
, struct dentry
*opt_dentry
);
261 * Try reloading inode security labels that have been marked as invalid. The
262 * @may_sleep parameter indicates when sleeping and thus reloading labels is
263 * allowed; when set to false, returns -ECHILD when the label is
264 * invalid. The @dentry parameter should be set to a dentry of the inode.
266 static int __inode_security_revalidate(struct inode
*inode
,
267 struct dentry
*dentry
,
270 struct inode_security_struct
*isec
= selinux_inode(inode
);
272 might_sleep_if(may_sleep
);
274 if (selinux_initialized(&selinux_state
) &&
275 isec
->initialized
!= LABEL_INITIALIZED
) {
280 * Try reloading the inode security label. This will fail if
281 * @opt_dentry is NULL and no dentry for this inode can be
282 * found; in that case, continue using the old label.
284 inode_doinit_with_dentry(inode
, dentry
);
289 static struct inode_security_struct
*inode_security_novalidate(struct inode
*inode
)
291 return selinux_inode(inode
);
294 static struct inode_security_struct
*inode_security_rcu(struct inode
*inode
, bool rcu
)
298 error
= __inode_security_revalidate(inode
, NULL
, !rcu
);
300 return ERR_PTR(error
);
301 return selinux_inode(inode
);
305 * Get the security label of an inode.
307 static struct inode_security_struct
*inode_security(struct inode
*inode
)
309 __inode_security_revalidate(inode
, NULL
, true);
310 return selinux_inode(inode
);
313 static struct inode_security_struct
*backing_inode_security_novalidate(struct dentry
*dentry
)
315 struct inode
*inode
= d_backing_inode(dentry
);
317 return selinux_inode(inode
);
321 * Get the security label of a dentry's backing inode.
323 static struct inode_security_struct
*backing_inode_security(struct dentry
*dentry
)
325 struct inode
*inode
= d_backing_inode(dentry
);
327 __inode_security_revalidate(inode
, dentry
, true);
328 return selinux_inode(inode
);
331 static void inode_free_security(struct inode
*inode
)
333 struct inode_security_struct
*isec
= selinux_inode(inode
);
334 struct superblock_security_struct
*sbsec
;
338 sbsec
= selinux_superblock(inode
->i_sb
);
340 * As not all inode security structures are in a list, we check for
341 * empty list outside of the lock to make sure that we won't waste
342 * time taking a lock doing nothing.
344 * The list_del_init() function can be safely called more than once.
345 * It should not be possible for this function to be called with
346 * concurrent list_add(), but for better safety against future changes
347 * in the code, we use list_empty_careful() here.
349 if (!list_empty_careful(&isec
->list
)) {
350 spin_lock(&sbsec
->isec_lock
);
351 list_del_init(&isec
->list
);
352 spin_unlock(&sbsec
->isec_lock
);
356 struct selinux_mnt_opts
{
357 const char *fscontext
, *context
, *rootcontext
, *defcontext
;
360 static void selinux_free_mnt_opts(void *mnt_opts
)
362 struct selinux_mnt_opts
*opts
= mnt_opts
;
363 kfree(opts
->fscontext
);
364 kfree(opts
->context
);
365 kfree(opts
->rootcontext
);
366 kfree(opts
->defcontext
);
379 #define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
389 A(rootcontext
, true),
394 static int match_opt_prefix(char *s
, int l
, char **arg
)
398 for (i
= 0; i
< ARRAY_SIZE(tokens
); i
++) {
399 size_t len
= tokens
[i
].len
;
400 if (len
> l
|| memcmp(s
, tokens
[i
].name
, len
))
402 if (tokens
[i
].has_arg
) {
403 if (len
== l
|| s
[len
] != '=')
408 return tokens
[i
].opt
;
413 #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
415 static int may_context_mount_sb_relabel(u32 sid
,
416 struct superblock_security_struct
*sbsec
,
417 const struct cred
*cred
)
419 const struct task_security_struct
*tsec
= selinux_cred(cred
);
422 rc
= avc_has_perm(&selinux_state
,
423 tsec
->sid
, sbsec
->sid
, SECCLASS_FILESYSTEM
,
424 FILESYSTEM__RELABELFROM
, NULL
);
428 rc
= avc_has_perm(&selinux_state
,
429 tsec
->sid
, sid
, SECCLASS_FILESYSTEM
,
430 FILESYSTEM__RELABELTO
, NULL
);
434 static int may_context_mount_inode_relabel(u32 sid
,
435 struct superblock_security_struct
*sbsec
,
436 const struct cred
*cred
)
438 const struct task_security_struct
*tsec
= selinux_cred(cred
);
440 rc
= avc_has_perm(&selinux_state
,
441 tsec
->sid
, sbsec
->sid
, SECCLASS_FILESYSTEM
,
442 FILESYSTEM__RELABELFROM
, NULL
);
446 rc
= avc_has_perm(&selinux_state
,
447 sid
, sbsec
->sid
, SECCLASS_FILESYSTEM
,
448 FILESYSTEM__ASSOCIATE
, NULL
);
452 static int selinux_is_genfs_special_handling(struct super_block
*sb
)
454 /* Special handling. Genfs but also in-core setxattr handler */
455 return !strcmp(sb
->s_type
->name
, "sysfs") ||
456 !strcmp(sb
->s_type
->name
, "pstore") ||
457 !strcmp(sb
->s_type
->name
, "debugfs") ||
458 !strcmp(sb
->s_type
->name
, "tracefs") ||
459 !strcmp(sb
->s_type
->name
, "rootfs") ||
460 (selinux_policycap_cgroupseclabel() &&
461 (!strcmp(sb
->s_type
->name
, "cgroup") ||
462 !strcmp(sb
->s_type
->name
, "cgroup2")));
465 static int selinux_is_sblabel_mnt(struct super_block
*sb
)
467 struct superblock_security_struct
*sbsec
= selinux_superblock(sb
);
470 * IMPORTANT: Double-check logic in this function when adding a new
471 * SECURITY_FS_USE_* definition!
473 BUILD_BUG_ON(SECURITY_FS_USE_MAX
!= 7);
475 switch (sbsec
->behavior
) {
476 case SECURITY_FS_USE_XATTR
:
477 case SECURITY_FS_USE_TRANS
:
478 case SECURITY_FS_USE_TASK
:
479 case SECURITY_FS_USE_NATIVE
:
482 case SECURITY_FS_USE_GENFS
:
483 return selinux_is_genfs_special_handling(sb
);
485 /* Never allow relabeling on context mounts */
486 case SECURITY_FS_USE_MNTPOINT
:
487 case SECURITY_FS_USE_NONE
:
493 static int sb_check_xattr_support(struct super_block
*sb
)
495 struct superblock_security_struct
*sbsec
= sb
->s_security
;
496 struct dentry
*root
= sb
->s_root
;
497 struct inode
*root_inode
= d_backing_inode(root
);
502 * Make sure that the xattr handler exists and that no
503 * error other than -ENODATA is returned by getxattr on
504 * the root directory. -ENODATA is ok, as this may be
505 * the first boot of the SELinux kernel before we have
506 * assigned xattr values to the filesystem.
508 if (!(root_inode
->i_opflags
& IOP_XATTR
)) {
509 pr_warn("SELinux: (dev %s, type %s) has no xattr support\n",
510 sb
->s_id
, sb
->s_type
->name
);
514 rc
= __vfs_getxattr(root
, root_inode
, XATTR_NAME_SELINUX
, NULL
, 0);
515 if (rc
< 0 && rc
!= -ENODATA
) {
516 if (rc
== -EOPNOTSUPP
) {
517 pr_warn("SELinux: (dev %s, type %s) has no security xattr handler\n",
518 sb
->s_id
, sb
->s_type
->name
);
521 pr_warn("SELinux: (dev %s, type %s) getxattr errno %d\n",
522 sb
->s_id
, sb
->s_type
->name
, -rc
);
529 /* No xattr support - try to fallback to genfs if possible. */
530 rc
= security_genfs_sid(&selinux_state
, sb
->s_type
->name
, "/",
535 pr_warn("SELinux: (dev %s, type %s) falling back to genfs\n",
536 sb
->s_id
, sb
->s_type
->name
);
537 sbsec
->behavior
= SECURITY_FS_USE_GENFS
;
542 static int sb_finish_set_opts(struct super_block
*sb
)
544 struct superblock_security_struct
*sbsec
= selinux_superblock(sb
);
545 struct dentry
*root
= sb
->s_root
;
546 struct inode
*root_inode
= d_backing_inode(root
);
549 if (sbsec
->behavior
== SECURITY_FS_USE_XATTR
) {
550 rc
= sb_check_xattr_support(sb
);
555 sbsec
->flags
|= SE_SBINITIALIZED
;
558 * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply
559 * leave the flag untouched because sb_clone_mnt_opts might be handing
560 * us a superblock that needs the flag to be cleared.
562 if (selinux_is_sblabel_mnt(sb
))
563 sbsec
->flags
|= SBLABEL_MNT
;
565 sbsec
->flags
&= ~SBLABEL_MNT
;
567 /* Initialize the root inode. */
568 rc
= inode_doinit_with_dentry(root_inode
, root
);
570 /* Initialize any other inodes associated with the superblock, e.g.
571 inodes created prior to initial policy load or inodes created
572 during get_sb by a pseudo filesystem that directly
574 spin_lock(&sbsec
->isec_lock
);
575 while (!list_empty(&sbsec
->isec_head
)) {
576 struct inode_security_struct
*isec
=
577 list_first_entry(&sbsec
->isec_head
,
578 struct inode_security_struct
, list
);
579 struct inode
*inode
= isec
->inode
;
580 list_del_init(&isec
->list
);
581 spin_unlock(&sbsec
->isec_lock
);
582 inode
= igrab(inode
);
584 if (!IS_PRIVATE(inode
))
585 inode_doinit_with_dentry(inode
, NULL
);
588 spin_lock(&sbsec
->isec_lock
);
590 spin_unlock(&sbsec
->isec_lock
);
594 static int bad_option(struct superblock_security_struct
*sbsec
, char flag
,
595 u32 old_sid
, u32 new_sid
)
597 char mnt_flags
= sbsec
->flags
& SE_MNTMASK
;
599 /* check if the old mount command had the same options */
600 if (sbsec
->flags
& SE_SBINITIALIZED
)
601 if (!(sbsec
->flags
& flag
) ||
602 (old_sid
!= new_sid
))
605 /* check if we were passed the same options twice,
606 * aka someone passed context=a,context=b
608 if (!(sbsec
->flags
& SE_SBINITIALIZED
))
609 if (mnt_flags
& flag
)
614 static int parse_sid(struct super_block
*sb
, const char *s
, u32
*sid
)
616 int rc
= security_context_str_to_sid(&selinux_state
, s
,
619 pr_warn("SELinux: security_context_str_to_sid"
620 "(%s) failed for (dev %s, type %s) errno=%d\n",
621 s
, sb
->s_id
, sb
->s_type
->name
, rc
);
626 * Allow filesystems with binary mount data to explicitly set mount point
627 * labeling information.
629 static int selinux_set_mnt_opts(struct super_block
*sb
,
631 unsigned long kern_flags
,
632 unsigned long *set_kern_flags
)
634 const struct cred
*cred
= current_cred();
635 struct superblock_security_struct
*sbsec
= selinux_superblock(sb
);
636 struct dentry
*root
= sb
->s_root
;
637 struct selinux_mnt_opts
*opts
= mnt_opts
;
638 struct inode_security_struct
*root_isec
;
639 u32 fscontext_sid
= 0, context_sid
= 0, rootcontext_sid
= 0;
640 u32 defcontext_sid
= 0;
643 mutex_lock(&sbsec
->lock
);
645 if (!selinux_initialized(&selinux_state
)) {
647 /* Defer initialization until selinux_complete_init,
648 after the initial policy is loaded and the security
649 server is ready to handle calls. */
653 pr_warn("SELinux: Unable to set superblock options "
654 "before the security server is initialized\n");
657 if (kern_flags
&& !set_kern_flags
) {
658 /* Specifying internal flags without providing a place to
659 * place the results is not allowed */
665 * Binary mount data FS will come through this function twice. Once
666 * from an explicit call and once from the generic calls from the vfs.
667 * Since the generic VFS calls will not contain any security mount data
668 * we need to skip the double mount verification.
670 * This does open a hole in which we will not notice if the first
671 * mount using this sb set explict options and a second mount using
672 * this sb does not set any security options. (The first options
673 * will be used for both mounts)
675 if ((sbsec
->flags
& SE_SBINITIALIZED
) && (sb
->s_type
->fs_flags
& FS_BINARY_MOUNTDATA
)
679 root_isec
= backing_inode_security_novalidate(root
);
682 * parse the mount options, check if they are valid sids.
683 * also check if someone is trying to mount the same sb more
684 * than once with different security options.
687 if (opts
->fscontext
) {
688 rc
= parse_sid(sb
, opts
->fscontext
, &fscontext_sid
);
691 if (bad_option(sbsec
, FSCONTEXT_MNT
, sbsec
->sid
,
693 goto out_double_mount
;
694 sbsec
->flags
|= FSCONTEXT_MNT
;
697 rc
= parse_sid(sb
, opts
->context
, &context_sid
);
700 if (bad_option(sbsec
, CONTEXT_MNT
, sbsec
->mntpoint_sid
,
702 goto out_double_mount
;
703 sbsec
->flags
|= CONTEXT_MNT
;
705 if (opts
->rootcontext
) {
706 rc
= parse_sid(sb
, opts
->rootcontext
, &rootcontext_sid
);
709 if (bad_option(sbsec
, ROOTCONTEXT_MNT
, root_isec
->sid
,
711 goto out_double_mount
;
712 sbsec
->flags
|= ROOTCONTEXT_MNT
;
714 if (opts
->defcontext
) {
715 rc
= parse_sid(sb
, opts
->defcontext
, &defcontext_sid
);
718 if (bad_option(sbsec
, DEFCONTEXT_MNT
, sbsec
->def_sid
,
720 goto out_double_mount
;
721 sbsec
->flags
|= DEFCONTEXT_MNT
;
725 if (sbsec
->flags
& SE_SBINITIALIZED
) {
726 /* previously mounted with options, but not on this attempt? */
727 if ((sbsec
->flags
& SE_MNTMASK
) && !opts
)
728 goto out_double_mount
;
733 if (strcmp(sb
->s_type
->name
, "proc") == 0)
734 sbsec
->flags
|= SE_SBPROC
| SE_SBGENFS
;
736 if (!strcmp(sb
->s_type
->name
, "debugfs") ||
737 !strcmp(sb
->s_type
->name
, "tracefs") ||
738 !strcmp(sb
->s_type
->name
, "binder") ||
739 !strcmp(sb
->s_type
->name
, "bpf") ||
740 !strcmp(sb
->s_type
->name
, "pstore"))
741 sbsec
->flags
|= SE_SBGENFS
;
743 if (!strcmp(sb
->s_type
->name
, "sysfs") ||
744 !strcmp(sb
->s_type
->name
, "cgroup") ||
745 !strcmp(sb
->s_type
->name
, "cgroup2"))
746 sbsec
->flags
|= SE_SBGENFS
| SE_SBGENFS_XATTR
;
748 if (!sbsec
->behavior
) {
750 * Determine the labeling behavior to use for this
753 rc
= security_fs_use(&selinux_state
, sb
);
755 pr_warn("%s: security_fs_use(%s) returned %d\n",
756 __func__
, sb
->s_type
->name
, rc
);
762 * If this is a user namespace mount and the filesystem type is not
763 * explicitly whitelisted, then no contexts are allowed on the command
764 * line and security labels must be ignored.
766 if (sb
->s_user_ns
!= &init_user_ns
&&
767 strcmp(sb
->s_type
->name
, "tmpfs") &&
768 strcmp(sb
->s_type
->name
, "ramfs") &&
769 strcmp(sb
->s_type
->name
, "devpts") &&
770 strcmp(sb
->s_type
->name
, "overlay")) {
771 if (context_sid
|| fscontext_sid
|| rootcontext_sid
||
776 if (sbsec
->behavior
== SECURITY_FS_USE_XATTR
) {
777 sbsec
->behavior
= SECURITY_FS_USE_MNTPOINT
;
778 rc
= security_transition_sid(&selinux_state
,
782 &sbsec
->mntpoint_sid
);
789 /* sets the context of the superblock for the fs being mounted. */
791 rc
= may_context_mount_sb_relabel(fscontext_sid
, sbsec
, cred
);
795 sbsec
->sid
= fscontext_sid
;
799 * Switch to using mount point labeling behavior.
800 * sets the label used on all file below the mountpoint, and will set
801 * the superblock context if not already set.
803 if (kern_flags
& SECURITY_LSM_NATIVE_LABELS
&& !context_sid
) {
804 sbsec
->behavior
= SECURITY_FS_USE_NATIVE
;
805 *set_kern_flags
|= SECURITY_LSM_NATIVE_LABELS
;
809 if (!fscontext_sid
) {
810 rc
= may_context_mount_sb_relabel(context_sid
, sbsec
,
814 sbsec
->sid
= context_sid
;
816 rc
= may_context_mount_inode_relabel(context_sid
, sbsec
,
821 if (!rootcontext_sid
)
822 rootcontext_sid
= context_sid
;
824 sbsec
->mntpoint_sid
= context_sid
;
825 sbsec
->behavior
= SECURITY_FS_USE_MNTPOINT
;
828 if (rootcontext_sid
) {
829 rc
= may_context_mount_inode_relabel(rootcontext_sid
, sbsec
,
834 root_isec
->sid
= rootcontext_sid
;
835 root_isec
->initialized
= LABEL_INITIALIZED
;
838 if (defcontext_sid
) {
839 if (sbsec
->behavior
!= SECURITY_FS_USE_XATTR
&&
840 sbsec
->behavior
!= SECURITY_FS_USE_NATIVE
) {
842 pr_warn("SELinux: defcontext option is "
843 "invalid for this filesystem type\n");
847 if (defcontext_sid
!= sbsec
->def_sid
) {
848 rc
= may_context_mount_inode_relabel(defcontext_sid
,
854 sbsec
->def_sid
= defcontext_sid
;
858 rc
= sb_finish_set_opts(sb
);
860 mutex_unlock(&sbsec
->lock
);
864 pr_warn("SELinux: mount invalid. Same superblock, different "
865 "security settings for (dev %s, type %s)\n", sb
->s_id
,
870 static int selinux_cmp_sb_context(const struct super_block
*oldsb
,
871 const struct super_block
*newsb
)
873 struct superblock_security_struct
*old
= selinux_superblock(oldsb
);
874 struct superblock_security_struct
*new = selinux_superblock(newsb
);
875 char oldflags
= old
->flags
& SE_MNTMASK
;
876 char newflags
= new->flags
& SE_MNTMASK
;
878 if (oldflags
!= newflags
)
880 if ((oldflags
& FSCONTEXT_MNT
) && old
->sid
!= new->sid
)
882 if ((oldflags
& CONTEXT_MNT
) && old
->mntpoint_sid
!= new->mntpoint_sid
)
884 if ((oldflags
& DEFCONTEXT_MNT
) && old
->def_sid
!= new->def_sid
)
886 if (oldflags
& ROOTCONTEXT_MNT
) {
887 struct inode_security_struct
*oldroot
= backing_inode_security(oldsb
->s_root
);
888 struct inode_security_struct
*newroot
= backing_inode_security(newsb
->s_root
);
889 if (oldroot
->sid
!= newroot
->sid
)
894 pr_warn("SELinux: mount invalid. Same superblock, "
895 "different security settings for (dev %s, "
896 "type %s)\n", newsb
->s_id
, newsb
->s_type
->name
);
900 static int selinux_sb_clone_mnt_opts(const struct super_block
*oldsb
,
901 struct super_block
*newsb
,
902 unsigned long kern_flags
,
903 unsigned long *set_kern_flags
)
906 const struct superblock_security_struct
*oldsbsec
=
907 selinux_superblock(oldsb
);
908 struct superblock_security_struct
*newsbsec
= selinux_superblock(newsb
);
910 int set_fscontext
= (oldsbsec
->flags
& FSCONTEXT_MNT
);
911 int set_context
= (oldsbsec
->flags
& CONTEXT_MNT
);
912 int set_rootcontext
= (oldsbsec
->flags
& ROOTCONTEXT_MNT
);
915 * if the parent was able to be mounted it clearly had no special lsm
916 * mount options. thus we can safely deal with this superblock later
918 if (!selinux_initialized(&selinux_state
))
922 * Specifying internal flags without providing a place to
923 * place the results is not allowed.
925 if (kern_flags
&& !set_kern_flags
)
928 /* how can we clone if the old one wasn't set up?? */
929 BUG_ON(!(oldsbsec
->flags
& SE_SBINITIALIZED
));
931 /* if fs is reusing a sb, make sure that the contexts match */
932 if (newsbsec
->flags
& SE_SBINITIALIZED
) {
933 if ((kern_flags
& SECURITY_LSM_NATIVE_LABELS
) && !set_context
)
934 *set_kern_flags
|= SECURITY_LSM_NATIVE_LABELS
;
935 return selinux_cmp_sb_context(oldsb
, newsb
);
938 mutex_lock(&newsbsec
->lock
);
940 newsbsec
->flags
= oldsbsec
->flags
;
942 newsbsec
->sid
= oldsbsec
->sid
;
943 newsbsec
->def_sid
= oldsbsec
->def_sid
;
944 newsbsec
->behavior
= oldsbsec
->behavior
;
946 if (newsbsec
->behavior
== SECURITY_FS_USE_NATIVE
&&
947 !(kern_flags
& SECURITY_LSM_NATIVE_LABELS
) && !set_context
) {
948 rc
= security_fs_use(&selinux_state
, newsb
);
953 if (kern_flags
& SECURITY_LSM_NATIVE_LABELS
&& !set_context
) {
954 newsbsec
->behavior
= SECURITY_FS_USE_NATIVE
;
955 *set_kern_flags
|= SECURITY_LSM_NATIVE_LABELS
;
959 u32 sid
= oldsbsec
->mntpoint_sid
;
963 if (!set_rootcontext
) {
964 struct inode_security_struct
*newisec
= backing_inode_security(newsb
->s_root
);
967 newsbsec
->mntpoint_sid
= sid
;
969 if (set_rootcontext
) {
970 const struct inode_security_struct
*oldisec
= backing_inode_security(oldsb
->s_root
);
971 struct inode_security_struct
*newisec
= backing_inode_security(newsb
->s_root
);
973 newisec
->sid
= oldisec
->sid
;
976 sb_finish_set_opts(newsb
);
978 mutex_unlock(&newsbsec
->lock
);
982 static int selinux_add_opt(int token
, const char *s
, void **mnt_opts
)
984 struct selinux_mnt_opts
*opts
= *mnt_opts
;
986 if (token
== Opt_seclabel
) /* eaten and completely ignored */
990 opts
= kzalloc(sizeof(struct selinux_mnt_opts
), GFP_KERNEL
);
999 if (opts
->context
|| opts
->defcontext
)
1004 if (opts
->fscontext
)
1006 opts
->fscontext
= s
;
1008 case Opt_rootcontext
:
1009 if (opts
->rootcontext
)
1011 opts
->rootcontext
= s
;
1013 case Opt_defcontext
:
1014 if (opts
->context
|| opts
->defcontext
)
1016 opts
->defcontext
= s
;
1021 pr_warn(SEL_MOUNT_FAIL_MSG
);
1025 static int selinux_add_mnt_opt(const char *option
, const char *val
, int len
,
1028 int token
= Opt_error
;
1031 for (i
= 0; i
< ARRAY_SIZE(tokens
); i
++) {
1032 if (strcmp(option
, tokens
[i
].name
) == 0) {
1033 token
= tokens
[i
].opt
;
1038 if (token
== Opt_error
)
1041 if (token
!= Opt_seclabel
) {
1042 val
= kmemdup_nul(val
, len
, GFP_KERNEL
);
1048 rc
= selinux_add_opt(token
, val
, mnt_opts
);
1057 selinux_free_mnt_opts(*mnt_opts
);
1063 static int show_sid(struct seq_file
*m
, u32 sid
)
1065 char *context
= NULL
;
1069 rc
= security_sid_to_context(&selinux_state
, sid
,
1072 bool has_comma
= context
&& strchr(context
, ',');
1077 seq_escape(m
, context
, "\"\n\\");
1085 static int selinux_sb_show_options(struct seq_file
*m
, struct super_block
*sb
)
1087 struct superblock_security_struct
*sbsec
= selinux_superblock(sb
);
1090 if (!(sbsec
->flags
& SE_SBINITIALIZED
))
1093 if (!selinux_initialized(&selinux_state
))
1096 if (sbsec
->flags
& FSCONTEXT_MNT
) {
1098 seq_puts(m
, FSCONTEXT_STR
);
1099 rc
= show_sid(m
, sbsec
->sid
);
1103 if (sbsec
->flags
& CONTEXT_MNT
) {
1105 seq_puts(m
, CONTEXT_STR
);
1106 rc
= show_sid(m
, sbsec
->mntpoint_sid
);
1110 if (sbsec
->flags
& DEFCONTEXT_MNT
) {
1112 seq_puts(m
, DEFCONTEXT_STR
);
1113 rc
= show_sid(m
, sbsec
->def_sid
);
1117 if (sbsec
->flags
& ROOTCONTEXT_MNT
) {
1118 struct dentry
*root
= sb
->s_root
;
1119 struct inode_security_struct
*isec
= backing_inode_security(root
);
1121 seq_puts(m
, ROOTCONTEXT_STR
);
1122 rc
= show_sid(m
, isec
->sid
);
1126 if (sbsec
->flags
& SBLABEL_MNT
) {
1128 seq_puts(m
, SECLABEL_STR
);
1133 static inline u16
inode_mode_to_security_class(umode_t mode
)
1135 switch (mode
& S_IFMT
) {
1137 return SECCLASS_SOCK_FILE
;
1139 return SECCLASS_LNK_FILE
;
1141 return SECCLASS_FILE
;
1143 return SECCLASS_BLK_FILE
;
1145 return SECCLASS_DIR
;
1147 return SECCLASS_CHR_FILE
;
1149 return SECCLASS_FIFO_FILE
;
1153 return SECCLASS_FILE
;
1156 static inline int default_protocol_stream(int protocol
)
1158 return (protocol
== IPPROTO_IP
|| protocol
== IPPROTO_TCP
||
1159 protocol
== IPPROTO_MPTCP
);
1162 static inline int default_protocol_dgram(int protocol
)
1164 return (protocol
== IPPROTO_IP
|| protocol
== IPPROTO_UDP
);
1167 static inline u16
socket_type_to_security_class(int family
, int type
, int protocol
)
1169 int extsockclass
= selinux_policycap_extsockclass();
1175 case SOCK_SEQPACKET
:
1176 return SECCLASS_UNIX_STREAM_SOCKET
;
1179 return SECCLASS_UNIX_DGRAM_SOCKET
;
1186 case SOCK_SEQPACKET
:
1187 if (default_protocol_stream(protocol
))
1188 return SECCLASS_TCP_SOCKET
;
1189 else if (extsockclass
&& protocol
== IPPROTO_SCTP
)
1190 return SECCLASS_SCTP_SOCKET
;
1192 return SECCLASS_RAWIP_SOCKET
;
1194 if (default_protocol_dgram(protocol
))
1195 return SECCLASS_UDP_SOCKET
;
1196 else if (extsockclass
&& (protocol
== IPPROTO_ICMP
||
1197 protocol
== IPPROTO_ICMPV6
))
1198 return SECCLASS_ICMP_SOCKET
;
1200 return SECCLASS_RAWIP_SOCKET
;
1202 return SECCLASS_DCCP_SOCKET
;
1204 return SECCLASS_RAWIP_SOCKET
;
1210 return SECCLASS_NETLINK_ROUTE_SOCKET
;
1211 case NETLINK_SOCK_DIAG
:
1212 return SECCLASS_NETLINK_TCPDIAG_SOCKET
;
1214 return SECCLASS_NETLINK_NFLOG_SOCKET
;
1216 return SECCLASS_NETLINK_XFRM_SOCKET
;
1217 case NETLINK_SELINUX
:
1218 return SECCLASS_NETLINK_SELINUX_SOCKET
;
1220 return SECCLASS_NETLINK_ISCSI_SOCKET
;
1222 return SECCLASS_NETLINK_AUDIT_SOCKET
;
1223 case NETLINK_FIB_LOOKUP
:
1224 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET
;
1225 case NETLINK_CONNECTOR
:
1226 return SECCLASS_NETLINK_CONNECTOR_SOCKET
;
1227 case NETLINK_NETFILTER
:
1228 return SECCLASS_NETLINK_NETFILTER_SOCKET
;
1229 case NETLINK_DNRTMSG
:
1230 return SECCLASS_NETLINK_DNRT_SOCKET
;
1231 case NETLINK_KOBJECT_UEVENT
:
1232 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET
;
1233 case NETLINK_GENERIC
:
1234 return SECCLASS_NETLINK_GENERIC_SOCKET
;
1235 case NETLINK_SCSITRANSPORT
:
1236 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET
;
1238 return SECCLASS_NETLINK_RDMA_SOCKET
;
1239 case NETLINK_CRYPTO
:
1240 return SECCLASS_NETLINK_CRYPTO_SOCKET
;
1242 return SECCLASS_NETLINK_SOCKET
;
1245 return SECCLASS_PACKET_SOCKET
;
1247 return SECCLASS_KEY_SOCKET
;
1249 return SECCLASS_APPLETALK_SOCKET
;
1255 return SECCLASS_AX25_SOCKET
;
1257 return SECCLASS_IPX_SOCKET
;
1259 return SECCLASS_NETROM_SOCKET
;
1261 return SECCLASS_ATMPVC_SOCKET
;
1263 return SECCLASS_X25_SOCKET
;
1265 return SECCLASS_ROSE_SOCKET
;
1267 return SECCLASS_DECNET_SOCKET
;
1269 return SECCLASS_ATMSVC_SOCKET
;
1271 return SECCLASS_RDS_SOCKET
;
1273 return SECCLASS_IRDA_SOCKET
;
1275 return SECCLASS_PPPOX_SOCKET
;
1277 return SECCLASS_LLC_SOCKET
;
1279 return SECCLASS_CAN_SOCKET
;
1281 return SECCLASS_TIPC_SOCKET
;
1283 return SECCLASS_BLUETOOTH_SOCKET
;
1285 return SECCLASS_IUCV_SOCKET
;
1287 return SECCLASS_RXRPC_SOCKET
;
1289 return SECCLASS_ISDN_SOCKET
;
1291 return SECCLASS_PHONET_SOCKET
;
1293 return SECCLASS_IEEE802154_SOCKET
;
1295 return SECCLASS_CAIF_SOCKET
;
1297 return SECCLASS_ALG_SOCKET
;
1299 return SECCLASS_NFC_SOCKET
;
1301 return SECCLASS_VSOCK_SOCKET
;
1303 return SECCLASS_KCM_SOCKET
;
1305 return SECCLASS_QIPCRTR_SOCKET
;
1307 return SECCLASS_SMC_SOCKET
;
1309 return SECCLASS_XDP_SOCKET
;
1311 return SECCLASS_MCTP_SOCKET
;
1313 #error New address family defined, please update this function.
1318 return SECCLASS_SOCKET
;
1321 static int selinux_genfs_get_sid(struct dentry
*dentry
,
1327 struct super_block
*sb
= dentry
->d_sb
;
1328 char *buffer
, *path
;
1330 buffer
= (char *)__get_free_page(GFP_KERNEL
);
1334 path
= dentry_path_raw(dentry
, buffer
, PAGE_SIZE
);
1338 if (flags
& SE_SBPROC
) {
1339 /* each process gets a /proc/PID/ entry. Strip off the
1340 * PID part to get a valid selinux labeling.
1341 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1342 while (path
[1] >= '0' && path
[1] <= '9') {
1347 rc
= security_genfs_sid(&selinux_state
, sb
->s_type
->name
,
1349 if (rc
== -ENOENT
) {
1350 /* No match in policy, mark as unlabeled. */
1351 *sid
= SECINITSID_UNLABELED
;
1355 free_page((unsigned long)buffer
);
1359 static int inode_doinit_use_xattr(struct inode
*inode
, struct dentry
*dentry
,
1360 u32 def_sid
, u32
*sid
)
1362 #define INITCONTEXTLEN 255
1367 len
= INITCONTEXTLEN
;
1368 context
= kmalloc(len
+ 1, GFP_NOFS
);
1372 context
[len
] = '\0';
1373 rc
= __vfs_getxattr(dentry
, inode
, XATTR_NAME_SELINUX
, context
, len
);
1374 if (rc
== -ERANGE
) {
1377 /* Need a larger buffer. Query for the right size. */
1378 rc
= __vfs_getxattr(dentry
, inode
, XATTR_NAME_SELINUX
, NULL
, 0);
1383 context
= kmalloc(len
+ 1, GFP_NOFS
);
1387 context
[len
] = '\0';
1388 rc
= __vfs_getxattr(dentry
, inode
, XATTR_NAME_SELINUX
,
1393 if (rc
!= -ENODATA
) {
1394 pr_warn("SELinux: %s: getxattr returned %d for dev=%s ino=%ld\n",
1395 __func__
, -rc
, inode
->i_sb
->s_id
, inode
->i_ino
);
1402 rc
= security_context_to_sid_default(&selinux_state
, context
, rc
, sid
,
1405 char *dev
= inode
->i_sb
->s_id
;
1406 unsigned long ino
= inode
->i_ino
;
1408 if (rc
== -EINVAL
) {
1409 pr_notice_ratelimited("SELinux: inode=%lu on dev=%s was found to have an invalid context=%s. This indicates you may need to relabel the inode or the filesystem in question.\n",
1412 pr_warn("SELinux: %s: context_to_sid(%s) returned %d for dev=%s ino=%ld\n",
1413 __func__
, context
, -rc
, dev
, ino
);
1420 /* The inode's security attributes must be initialized before first use. */
1421 static int inode_doinit_with_dentry(struct inode
*inode
, struct dentry
*opt_dentry
)
1423 struct superblock_security_struct
*sbsec
= NULL
;
1424 struct inode_security_struct
*isec
= selinux_inode(inode
);
1425 u32 task_sid
, sid
= 0;
1427 struct dentry
*dentry
;
1430 if (isec
->initialized
== LABEL_INITIALIZED
)
1433 spin_lock(&isec
->lock
);
1434 if (isec
->initialized
== LABEL_INITIALIZED
)
1437 if (isec
->sclass
== SECCLASS_FILE
)
1438 isec
->sclass
= inode_mode_to_security_class(inode
->i_mode
);
1440 sbsec
= selinux_superblock(inode
->i_sb
);
1441 if (!(sbsec
->flags
& SE_SBINITIALIZED
)) {
1442 /* Defer initialization until selinux_complete_init,
1443 after the initial policy is loaded and the security
1444 server is ready to handle calls. */
1445 spin_lock(&sbsec
->isec_lock
);
1446 if (list_empty(&isec
->list
))
1447 list_add(&isec
->list
, &sbsec
->isec_head
);
1448 spin_unlock(&sbsec
->isec_lock
);
1452 sclass
= isec
->sclass
;
1453 task_sid
= isec
->task_sid
;
1455 isec
->initialized
= LABEL_PENDING
;
1456 spin_unlock(&isec
->lock
);
1458 switch (sbsec
->behavior
) {
1459 case SECURITY_FS_USE_NATIVE
:
1461 case SECURITY_FS_USE_XATTR
:
1462 if (!(inode
->i_opflags
& IOP_XATTR
)) {
1463 sid
= sbsec
->def_sid
;
1466 /* Need a dentry, since the xattr API requires one.
1467 Life would be simpler if we could just pass the inode. */
1469 /* Called from d_instantiate or d_splice_alias. */
1470 dentry
= dget(opt_dentry
);
1473 * Called from selinux_complete_init, try to find a dentry.
1474 * Some filesystems really want a connected one, so try
1475 * that first. We could split SECURITY_FS_USE_XATTR in
1476 * two, depending upon that...
1478 dentry
= d_find_alias(inode
);
1480 dentry
= d_find_any_alias(inode
);
1484 * this is can be hit on boot when a file is accessed
1485 * before the policy is loaded. When we load policy we
1486 * may find inodes that have no dentry on the
1487 * sbsec->isec_head list. No reason to complain as these
1488 * will get fixed up the next time we go through
1489 * inode_doinit with a dentry, before these inodes could
1490 * be used again by userspace.
1495 rc
= inode_doinit_use_xattr(inode
, dentry
, sbsec
->def_sid
,
1501 case SECURITY_FS_USE_TASK
:
1504 case SECURITY_FS_USE_TRANS
:
1505 /* Default to the fs SID. */
1508 /* Try to obtain a transition SID. */
1509 rc
= security_transition_sid(&selinux_state
, task_sid
, sid
,
1510 sclass
, NULL
, &sid
);
1514 case SECURITY_FS_USE_MNTPOINT
:
1515 sid
= sbsec
->mntpoint_sid
;
1518 /* Default to the fs superblock SID. */
1521 if ((sbsec
->flags
& SE_SBGENFS
) &&
1522 (!S_ISLNK(inode
->i_mode
) ||
1523 selinux_policycap_genfs_seclabel_symlinks())) {
1524 /* We must have a dentry to determine the label on
1527 /* Called from d_instantiate or
1528 * d_splice_alias. */
1529 dentry
= dget(opt_dentry
);
1531 /* Called from selinux_complete_init, try to
1532 * find a dentry. Some filesystems really want
1533 * a connected one, so try that first.
1535 dentry
= d_find_alias(inode
);
1537 dentry
= d_find_any_alias(inode
);
1540 * This can be hit on boot when a file is accessed
1541 * before the policy is loaded. When we load policy we
1542 * may find inodes that have no dentry on the
1543 * sbsec->isec_head list. No reason to complain as
1544 * these will get fixed up the next time we go through
1545 * inode_doinit() with a dentry, before these inodes
1546 * could be used again by userspace.
1550 rc
= selinux_genfs_get_sid(dentry
, sclass
,
1551 sbsec
->flags
, &sid
);
1557 if ((sbsec
->flags
& SE_SBGENFS_XATTR
) &&
1558 (inode
->i_opflags
& IOP_XATTR
)) {
1559 rc
= inode_doinit_use_xattr(inode
, dentry
,
1572 spin_lock(&isec
->lock
);
1573 if (isec
->initialized
== LABEL_PENDING
) {
1575 isec
->initialized
= LABEL_INVALID
;
1578 isec
->initialized
= LABEL_INITIALIZED
;
1583 spin_unlock(&isec
->lock
);
1587 spin_lock(&isec
->lock
);
1588 if (isec
->initialized
== LABEL_PENDING
) {
1589 isec
->initialized
= LABEL_INVALID
;
1592 spin_unlock(&isec
->lock
);
1596 /* Convert a Linux signal to an access vector. */
1597 static inline u32
signal_to_av(int sig
)
1603 /* Commonly granted from child to parent. */
1604 perm
= PROCESS__SIGCHLD
;
1607 /* Cannot be caught or ignored */
1608 perm
= PROCESS__SIGKILL
;
1611 /* Cannot be caught or ignored */
1612 perm
= PROCESS__SIGSTOP
;
1615 /* All other signals. */
1616 perm
= PROCESS__SIGNAL
;
1623 #if CAP_LAST_CAP > 63
1624 #error Fix SELinux to handle capabilities > 63.
1627 /* Check whether a task is allowed to use a capability. */
1628 static int cred_has_capability(const struct cred
*cred
,
1629 int cap
, unsigned int opts
, bool initns
)
1631 struct common_audit_data ad
;
1632 struct av_decision avd
;
1634 u32 sid
= cred_sid(cred
);
1635 u32 av
= CAP_TO_MASK(cap
);
1638 ad
.type
= LSM_AUDIT_DATA_CAP
;
1641 switch (CAP_TO_INDEX(cap
)) {
1643 sclass
= initns
? SECCLASS_CAPABILITY
: SECCLASS_CAP_USERNS
;
1646 sclass
= initns
? SECCLASS_CAPABILITY2
: SECCLASS_CAP2_USERNS
;
1649 pr_err("SELinux: out of range capability %d\n", cap
);
1654 rc
= avc_has_perm_noaudit(&selinux_state
,
1655 sid
, sid
, sclass
, av
, 0, &avd
);
1656 if (!(opts
& CAP_OPT_NOAUDIT
)) {
1657 int rc2
= avc_audit(&selinux_state
,
1658 sid
, sid
, sclass
, av
, &avd
, rc
, &ad
);
1665 /* Check whether a task has a particular permission to an inode.
1666 The 'adp' parameter is optional and allows other audit
1667 data to be passed (e.g. the dentry). */
1668 static int inode_has_perm(const struct cred
*cred
,
1669 struct inode
*inode
,
1671 struct common_audit_data
*adp
)
1673 struct inode_security_struct
*isec
;
1676 validate_creds(cred
);
1678 if (unlikely(IS_PRIVATE(inode
)))
1681 sid
= cred_sid(cred
);
1682 isec
= selinux_inode(inode
);
1684 return avc_has_perm(&selinux_state
,
1685 sid
, isec
->sid
, isec
->sclass
, perms
, adp
);
1688 /* Same as inode_has_perm, but pass explicit audit data containing
1689 the dentry to help the auditing code to more easily generate the
1690 pathname if needed. */
1691 static inline int dentry_has_perm(const struct cred
*cred
,
1692 struct dentry
*dentry
,
1695 struct inode
*inode
= d_backing_inode(dentry
);
1696 struct common_audit_data ad
;
1698 ad
.type
= LSM_AUDIT_DATA_DENTRY
;
1699 ad
.u
.dentry
= dentry
;
1700 __inode_security_revalidate(inode
, dentry
, true);
1701 return inode_has_perm(cred
, inode
, av
, &ad
);
1704 /* Same as inode_has_perm, but pass explicit audit data containing
1705 the path to help the auditing code to more easily generate the
1706 pathname if needed. */
1707 static inline int path_has_perm(const struct cred
*cred
,
1708 const struct path
*path
,
1711 struct inode
*inode
= d_backing_inode(path
->dentry
);
1712 struct common_audit_data ad
;
1714 ad
.type
= LSM_AUDIT_DATA_PATH
;
1716 __inode_security_revalidate(inode
, path
->dentry
, true);
1717 return inode_has_perm(cred
, inode
, av
, &ad
);
1720 /* Same as path_has_perm, but uses the inode from the file struct. */
1721 static inline int file_path_has_perm(const struct cred
*cred
,
1725 struct common_audit_data ad
;
1727 ad
.type
= LSM_AUDIT_DATA_FILE
;
1729 return inode_has_perm(cred
, file_inode(file
), av
, &ad
);
1732 #ifdef CONFIG_BPF_SYSCALL
1733 static int bpf_fd_pass(struct file
*file
, u32 sid
);
1736 /* Check whether a task can use an open file descriptor to
1737 access an inode in a given way. Check access to the
1738 descriptor itself, and then use dentry_has_perm to
1739 check a particular permission to the file.
1740 Access to the descriptor is implicitly granted if it
1741 has the same SID as the process. If av is zero, then
1742 access to the file is not checked, e.g. for cases
1743 where only the descriptor is affected like seek. */
1744 static int file_has_perm(const struct cred
*cred
,
1748 struct file_security_struct
*fsec
= selinux_file(file
);
1749 struct inode
*inode
= file_inode(file
);
1750 struct common_audit_data ad
;
1751 u32 sid
= cred_sid(cred
);
1754 ad
.type
= LSM_AUDIT_DATA_FILE
;
1757 if (sid
!= fsec
->sid
) {
1758 rc
= avc_has_perm(&selinux_state
,
1767 #ifdef CONFIG_BPF_SYSCALL
1768 rc
= bpf_fd_pass(file
, cred_sid(cred
));
1773 /* av is zero if only checking access to the descriptor. */
1776 rc
= inode_has_perm(cred
, inode
, av
, &ad
);
1783 * Determine the label for an inode that might be unioned.
1786 selinux_determine_inode_label(const struct task_security_struct
*tsec
,
1788 const struct qstr
*name
, u16 tclass
,
1791 const struct superblock_security_struct
*sbsec
=
1792 selinux_superblock(dir
->i_sb
);
1794 if ((sbsec
->flags
& SE_SBINITIALIZED
) &&
1795 (sbsec
->behavior
== SECURITY_FS_USE_MNTPOINT
)) {
1796 *_new_isid
= sbsec
->mntpoint_sid
;
1797 } else if ((sbsec
->flags
& SBLABEL_MNT
) &&
1799 *_new_isid
= tsec
->create_sid
;
1801 const struct inode_security_struct
*dsec
= inode_security(dir
);
1802 return security_transition_sid(&selinux_state
, tsec
->sid
,
1810 /* Check whether a task can create a file. */
1811 static int may_create(struct inode
*dir
,
1812 struct dentry
*dentry
,
1815 const struct task_security_struct
*tsec
= selinux_cred(current_cred());
1816 struct inode_security_struct
*dsec
;
1817 struct superblock_security_struct
*sbsec
;
1819 struct common_audit_data ad
;
1822 dsec
= inode_security(dir
);
1823 sbsec
= selinux_superblock(dir
->i_sb
);
1827 ad
.type
= LSM_AUDIT_DATA_DENTRY
;
1828 ad
.u
.dentry
= dentry
;
1830 rc
= avc_has_perm(&selinux_state
,
1831 sid
, dsec
->sid
, SECCLASS_DIR
,
1832 DIR__ADD_NAME
| DIR__SEARCH
,
1837 rc
= selinux_determine_inode_label(tsec
, dir
, &dentry
->d_name
, tclass
,
1842 rc
= avc_has_perm(&selinux_state
,
1843 sid
, newsid
, tclass
, FILE__CREATE
, &ad
);
1847 return avc_has_perm(&selinux_state
,
1849 SECCLASS_FILESYSTEM
,
1850 FILESYSTEM__ASSOCIATE
, &ad
);
1854 #define MAY_UNLINK 1
1857 /* Check whether a task can link, unlink, or rmdir a file/directory. */
1858 static int may_link(struct inode
*dir
,
1859 struct dentry
*dentry
,
1863 struct inode_security_struct
*dsec
, *isec
;
1864 struct common_audit_data ad
;
1865 u32 sid
= current_sid();
1869 dsec
= inode_security(dir
);
1870 isec
= backing_inode_security(dentry
);
1872 ad
.type
= LSM_AUDIT_DATA_DENTRY
;
1873 ad
.u
.dentry
= dentry
;
1876 av
|= (kind
? DIR__REMOVE_NAME
: DIR__ADD_NAME
);
1877 rc
= avc_has_perm(&selinux_state
,
1878 sid
, dsec
->sid
, SECCLASS_DIR
, av
, &ad
);
1893 pr_warn("SELinux: %s: unrecognized kind %d\n",
1898 rc
= avc_has_perm(&selinux_state
,
1899 sid
, isec
->sid
, isec
->sclass
, av
, &ad
);
1903 static inline int may_rename(struct inode
*old_dir
,
1904 struct dentry
*old_dentry
,
1905 struct inode
*new_dir
,
1906 struct dentry
*new_dentry
)
1908 struct inode_security_struct
*old_dsec
, *new_dsec
, *old_isec
, *new_isec
;
1909 struct common_audit_data ad
;
1910 u32 sid
= current_sid();
1912 int old_is_dir
, new_is_dir
;
1915 old_dsec
= inode_security(old_dir
);
1916 old_isec
= backing_inode_security(old_dentry
);
1917 old_is_dir
= d_is_dir(old_dentry
);
1918 new_dsec
= inode_security(new_dir
);
1920 ad
.type
= LSM_AUDIT_DATA_DENTRY
;
1922 ad
.u
.dentry
= old_dentry
;
1923 rc
= avc_has_perm(&selinux_state
,
1924 sid
, old_dsec
->sid
, SECCLASS_DIR
,
1925 DIR__REMOVE_NAME
| DIR__SEARCH
, &ad
);
1928 rc
= avc_has_perm(&selinux_state
,
1930 old_isec
->sclass
, FILE__RENAME
, &ad
);
1933 if (old_is_dir
&& new_dir
!= old_dir
) {
1934 rc
= avc_has_perm(&selinux_state
,
1936 old_isec
->sclass
, DIR__REPARENT
, &ad
);
1941 ad
.u
.dentry
= new_dentry
;
1942 av
= DIR__ADD_NAME
| DIR__SEARCH
;
1943 if (d_is_positive(new_dentry
))
1944 av
|= DIR__REMOVE_NAME
;
1945 rc
= avc_has_perm(&selinux_state
,
1946 sid
, new_dsec
->sid
, SECCLASS_DIR
, av
, &ad
);
1949 if (d_is_positive(new_dentry
)) {
1950 new_isec
= backing_inode_security(new_dentry
);
1951 new_is_dir
= d_is_dir(new_dentry
);
1952 rc
= avc_has_perm(&selinux_state
,
1955 (new_is_dir
? DIR__RMDIR
: FILE__UNLINK
), &ad
);
1963 /* Check whether a task can perform a filesystem operation. */
1964 static int superblock_has_perm(const struct cred
*cred
,
1965 struct super_block
*sb
,
1967 struct common_audit_data
*ad
)
1969 struct superblock_security_struct
*sbsec
;
1970 u32 sid
= cred_sid(cred
);
1972 sbsec
= selinux_superblock(sb
);
1973 return avc_has_perm(&selinux_state
,
1974 sid
, sbsec
->sid
, SECCLASS_FILESYSTEM
, perms
, ad
);
1977 /* Convert a Linux mode and permission mask to an access vector. */
1978 static inline u32
file_mask_to_av(int mode
, int mask
)
1982 if (!S_ISDIR(mode
)) {
1983 if (mask
& MAY_EXEC
)
1984 av
|= FILE__EXECUTE
;
1985 if (mask
& MAY_READ
)
1988 if (mask
& MAY_APPEND
)
1990 else if (mask
& MAY_WRITE
)
1994 if (mask
& MAY_EXEC
)
1996 if (mask
& MAY_WRITE
)
1998 if (mask
& MAY_READ
)
2005 /* Convert a Linux file to an access vector. */
2006 static inline u32
file_to_av(struct file
*file
)
2010 if (file
->f_mode
& FMODE_READ
)
2012 if (file
->f_mode
& FMODE_WRITE
) {
2013 if (file
->f_flags
& O_APPEND
)
2020 * Special file opened with flags 3 for ioctl-only use.
2029 * Convert a file to an access vector and include the correct
2032 static inline u32
open_file_to_av(struct file
*file
)
2034 u32 av
= file_to_av(file
);
2035 struct inode
*inode
= file_inode(file
);
2037 if (selinux_policycap_openperm() &&
2038 inode
->i_sb
->s_magic
!= SOCKFS_MAGIC
)
2044 /* Hook functions begin here. */
2046 static int selinux_binder_set_context_mgr(const struct cred
*mgr
)
2048 return avc_has_perm(&selinux_state
,
2049 current_sid(), cred_sid(mgr
), SECCLASS_BINDER
,
2050 BINDER__SET_CONTEXT_MGR
, NULL
);
2053 static int selinux_binder_transaction(const struct cred
*from
,
2054 const struct cred
*to
)
2056 u32 mysid
= current_sid();
2057 u32 fromsid
= cred_sid(from
);
2058 u32 tosid
= cred_sid(to
);
2061 if (mysid
!= fromsid
) {
2062 rc
= avc_has_perm(&selinux_state
,
2063 mysid
, fromsid
, SECCLASS_BINDER
,
2064 BINDER__IMPERSONATE
, NULL
);
2069 return avc_has_perm(&selinux_state
, fromsid
, tosid
,
2070 SECCLASS_BINDER
, BINDER__CALL
, NULL
);
2073 static int selinux_binder_transfer_binder(const struct cred
*from
,
2074 const struct cred
*to
)
2076 return avc_has_perm(&selinux_state
,
2077 cred_sid(from
), cred_sid(to
),
2078 SECCLASS_BINDER
, BINDER__TRANSFER
,
2082 static int selinux_binder_transfer_file(const struct cred
*from
,
2083 const struct cred
*to
,
2086 u32 sid
= cred_sid(to
);
2087 struct file_security_struct
*fsec
= selinux_file(file
);
2088 struct dentry
*dentry
= file
->f_path
.dentry
;
2089 struct inode_security_struct
*isec
;
2090 struct common_audit_data ad
;
2093 ad
.type
= LSM_AUDIT_DATA_PATH
;
2094 ad
.u
.path
= file
->f_path
;
2096 if (sid
!= fsec
->sid
) {
2097 rc
= avc_has_perm(&selinux_state
,
2106 #ifdef CONFIG_BPF_SYSCALL
2107 rc
= bpf_fd_pass(file
, sid
);
2112 if (unlikely(IS_PRIVATE(d_backing_inode(dentry
))))
2115 isec
= backing_inode_security(dentry
);
2116 return avc_has_perm(&selinux_state
,
2117 sid
, isec
->sid
, isec
->sclass
, file_to_av(file
),
2121 static int selinux_ptrace_access_check(struct task_struct
*child
,
2124 u32 sid
= current_sid();
2125 u32 csid
= task_sid_obj(child
);
2127 if (mode
& PTRACE_MODE_READ
)
2128 return avc_has_perm(&selinux_state
,
2129 sid
, csid
, SECCLASS_FILE
, FILE__READ
, NULL
);
2131 return avc_has_perm(&selinux_state
,
2132 sid
, csid
, SECCLASS_PROCESS
, PROCESS__PTRACE
, NULL
);
2135 static int selinux_ptrace_traceme(struct task_struct
*parent
)
2137 return avc_has_perm(&selinux_state
,
2138 task_sid_obj(parent
), task_sid_obj(current
),
2139 SECCLASS_PROCESS
, PROCESS__PTRACE
, NULL
);
2142 static int selinux_capget(struct task_struct
*target
, kernel_cap_t
*effective
,
2143 kernel_cap_t
*inheritable
, kernel_cap_t
*permitted
)
2145 return avc_has_perm(&selinux_state
,
2146 current_sid(), task_sid_obj(target
), SECCLASS_PROCESS
,
2147 PROCESS__GETCAP
, NULL
);
2150 static int selinux_capset(struct cred
*new, const struct cred
*old
,
2151 const kernel_cap_t
*effective
,
2152 const kernel_cap_t
*inheritable
,
2153 const kernel_cap_t
*permitted
)
2155 return avc_has_perm(&selinux_state
,
2156 cred_sid(old
), cred_sid(new), SECCLASS_PROCESS
,
2157 PROCESS__SETCAP
, NULL
);
2161 * (This comment used to live with the selinux_task_setuid hook,
2162 * which was removed).
2164 * Since setuid only affects the current process, and since the SELinux
2165 * controls are not based on the Linux identity attributes, SELinux does not
2166 * need to control this operation. However, SELinux does control the use of
2167 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2170 static int selinux_capable(const struct cred
*cred
, struct user_namespace
*ns
,
2171 int cap
, unsigned int opts
)
2173 return cred_has_capability(cred
, cap
, opts
, ns
== &init_user_ns
);
2176 static int selinux_quotactl(int cmds
, int type
, int id
, struct super_block
*sb
)
2178 const struct cred
*cred
= current_cred();
2193 rc
= superblock_has_perm(cred
, sb
, FILESYSTEM__QUOTAMOD
, NULL
);
2201 case Q_XGETNEXTQUOTA
:
2202 rc
= superblock_has_perm(cred
, sb
, FILESYSTEM__QUOTAGET
, NULL
);
2205 rc
= 0; /* let the kernel handle invalid cmds */
2211 static int selinux_quota_on(struct dentry
*dentry
)
2213 const struct cred
*cred
= current_cred();
2215 return dentry_has_perm(cred
, dentry
, FILE__QUOTAON
);
2218 static int selinux_syslog(int type
)
2221 case SYSLOG_ACTION_READ_ALL
: /* Read last kernel messages */
2222 case SYSLOG_ACTION_SIZE_BUFFER
: /* Return size of the log buffer */
2223 return avc_has_perm(&selinux_state
,
2224 current_sid(), SECINITSID_KERNEL
,
2225 SECCLASS_SYSTEM
, SYSTEM__SYSLOG_READ
, NULL
);
2226 case SYSLOG_ACTION_CONSOLE_OFF
: /* Disable logging to console */
2227 case SYSLOG_ACTION_CONSOLE_ON
: /* Enable logging to console */
2228 /* Set level of messages printed to console */
2229 case SYSLOG_ACTION_CONSOLE_LEVEL
:
2230 return avc_has_perm(&selinux_state
,
2231 current_sid(), SECINITSID_KERNEL
,
2232 SECCLASS_SYSTEM
, SYSTEM__SYSLOG_CONSOLE
,
2235 /* All other syslog types */
2236 return avc_has_perm(&selinux_state
,
2237 current_sid(), SECINITSID_KERNEL
,
2238 SECCLASS_SYSTEM
, SYSTEM__SYSLOG_MOD
, NULL
);
2242 * Check that a process has enough memory to allocate a new virtual
2243 * mapping. 0 means there is enough memory for the allocation to
2244 * succeed and -ENOMEM implies there is not.
2246 * Do not audit the selinux permission check, as this is applied to all
2247 * processes that allocate mappings.
2249 static int selinux_vm_enough_memory(struct mm_struct
*mm
, long pages
)
2251 int rc
, cap_sys_admin
= 0;
2253 rc
= cred_has_capability(current_cred(), CAP_SYS_ADMIN
,
2254 CAP_OPT_NOAUDIT
, true);
2258 return cap_sys_admin
;
2261 /* binprm security operations */
2263 static u32
ptrace_parent_sid(void)
2266 struct task_struct
*tracer
;
2269 tracer
= ptrace_parent(current
);
2271 sid
= task_sid_obj(tracer
);
2277 static int check_nnp_nosuid(const struct linux_binprm
*bprm
,
2278 const struct task_security_struct
*old_tsec
,
2279 const struct task_security_struct
*new_tsec
)
2281 int nnp
= (bprm
->unsafe
& LSM_UNSAFE_NO_NEW_PRIVS
);
2282 int nosuid
= !mnt_may_suid(bprm
->file
->f_path
.mnt
);
2286 if (!nnp
&& !nosuid
)
2287 return 0; /* neither NNP nor nosuid */
2289 if (new_tsec
->sid
== old_tsec
->sid
)
2290 return 0; /* No change in credentials */
2293 * If the policy enables the nnp_nosuid_transition policy capability,
2294 * then we permit transitions under NNP or nosuid if the
2295 * policy allows the corresponding permission between
2296 * the old and new contexts.
2298 if (selinux_policycap_nnp_nosuid_transition()) {
2301 av
|= PROCESS2__NNP_TRANSITION
;
2303 av
|= PROCESS2__NOSUID_TRANSITION
;
2304 rc
= avc_has_perm(&selinux_state
,
2305 old_tsec
->sid
, new_tsec
->sid
,
2306 SECCLASS_PROCESS2
, av
, NULL
);
2312 * We also permit NNP or nosuid transitions to bounded SIDs,
2313 * i.e. SIDs that are guaranteed to only be allowed a subset
2314 * of the permissions of the current SID.
2316 rc
= security_bounded_transition(&selinux_state
, old_tsec
->sid
,
2322 * On failure, preserve the errno values for NNP vs nosuid.
2323 * NNP: Operation not permitted for caller.
2324 * nosuid: Permission denied to file.
2331 static int selinux_bprm_creds_for_exec(struct linux_binprm
*bprm
)
2333 const struct task_security_struct
*old_tsec
;
2334 struct task_security_struct
*new_tsec
;
2335 struct inode_security_struct
*isec
;
2336 struct common_audit_data ad
;
2337 struct inode
*inode
= file_inode(bprm
->file
);
2340 /* SELinux context only depends on initial program or script and not
2341 * the script interpreter */
2343 old_tsec
= selinux_cred(current_cred());
2344 new_tsec
= selinux_cred(bprm
->cred
);
2345 isec
= inode_security(inode
);
2347 /* Default to the current task SID. */
2348 new_tsec
->sid
= old_tsec
->sid
;
2349 new_tsec
->osid
= old_tsec
->sid
;
2351 /* Reset fs, key, and sock SIDs on execve. */
2352 new_tsec
->create_sid
= 0;
2353 new_tsec
->keycreate_sid
= 0;
2354 new_tsec
->sockcreate_sid
= 0;
2356 if (old_tsec
->exec_sid
) {
2357 new_tsec
->sid
= old_tsec
->exec_sid
;
2358 /* Reset exec SID on execve. */
2359 new_tsec
->exec_sid
= 0;
2361 /* Fail on NNP or nosuid if not an allowed transition. */
2362 rc
= check_nnp_nosuid(bprm
, old_tsec
, new_tsec
);
2366 /* Check for a default transition on this program. */
2367 rc
= security_transition_sid(&selinux_state
, old_tsec
->sid
,
2368 isec
->sid
, SECCLASS_PROCESS
, NULL
,
2374 * Fallback to old SID on NNP or nosuid if not an allowed
2377 rc
= check_nnp_nosuid(bprm
, old_tsec
, new_tsec
);
2379 new_tsec
->sid
= old_tsec
->sid
;
2382 ad
.type
= LSM_AUDIT_DATA_FILE
;
2383 ad
.u
.file
= bprm
->file
;
2385 if (new_tsec
->sid
== old_tsec
->sid
) {
2386 rc
= avc_has_perm(&selinux_state
,
2387 old_tsec
->sid
, isec
->sid
,
2388 SECCLASS_FILE
, FILE__EXECUTE_NO_TRANS
, &ad
);
2392 /* Check permissions for the transition. */
2393 rc
= avc_has_perm(&selinux_state
,
2394 old_tsec
->sid
, new_tsec
->sid
,
2395 SECCLASS_PROCESS
, PROCESS__TRANSITION
, &ad
);
2399 rc
= avc_has_perm(&selinux_state
,
2400 new_tsec
->sid
, isec
->sid
,
2401 SECCLASS_FILE
, FILE__ENTRYPOINT
, &ad
);
2405 /* Check for shared state */
2406 if (bprm
->unsafe
& LSM_UNSAFE_SHARE
) {
2407 rc
= avc_has_perm(&selinux_state
,
2408 old_tsec
->sid
, new_tsec
->sid
,
2409 SECCLASS_PROCESS
, PROCESS__SHARE
,
2415 /* Make sure that anyone attempting to ptrace over a task that
2416 * changes its SID has the appropriate permit */
2417 if (bprm
->unsafe
& LSM_UNSAFE_PTRACE
) {
2418 u32 ptsid
= ptrace_parent_sid();
2420 rc
= avc_has_perm(&selinux_state
,
2421 ptsid
, new_tsec
->sid
,
2423 PROCESS__PTRACE
, NULL
);
2429 /* Clear any possibly unsafe personality bits on exec: */
2430 bprm
->per_clear
|= PER_CLEAR_ON_SETID
;
2432 /* Enable secure mode for SIDs transitions unless
2433 the noatsecure permission is granted between
2434 the two SIDs, i.e. ahp returns 0. */
2435 rc
= avc_has_perm(&selinux_state
,
2436 old_tsec
->sid
, new_tsec
->sid
,
2437 SECCLASS_PROCESS
, PROCESS__NOATSECURE
,
2439 bprm
->secureexec
|= !!rc
;
2445 static int match_file(const void *p
, struct file
*file
, unsigned fd
)
2447 return file_has_perm(p
, file
, file_to_av(file
)) ? fd
+ 1 : 0;
2450 /* Derived from fs/exec.c:flush_old_files. */
2451 static inline void flush_unauthorized_files(const struct cred
*cred
,
2452 struct files_struct
*files
)
2454 struct file
*file
, *devnull
= NULL
;
2455 struct tty_struct
*tty
;
2459 tty
= get_current_tty();
2461 spin_lock(&tty
->files_lock
);
2462 if (!list_empty(&tty
->tty_files
)) {
2463 struct tty_file_private
*file_priv
;
2465 /* Revalidate access to controlling tty.
2466 Use file_path_has_perm on the tty path directly
2467 rather than using file_has_perm, as this particular
2468 open file may belong to another process and we are
2469 only interested in the inode-based check here. */
2470 file_priv
= list_first_entry(&tty
->tty_files
,
2471 struct tty_file_private
, list
);
2472 file
= file_priv
->file
;
2473 if (file_path_has_perm(cred
, file
, FILE__READ
| FILE__WRITE
))
2476 spin_unlock(&tty
->files_lock
);
2479 /* Reset controlling tty. */
2483 /* Revalidate access to inherited open files. */
2484 n
= iterate_fd(files
, 0, match_file
, cred
);
2485 if (!n
) /* none found? */
2488 devnull
= dentry_open(&selinux_null
, O_RDWR
, cred
);
2489 if (IS_ERR(devnull
))
2491 /* replace all the matching ones with this */
2493 replace_fd(n
- 1, devnull
, 0);
2494 } while ((n
= iterate_fd(files
, n
, match_file
, cred
)) != 0);
2500 * Prepare a process for imminent new credential changes due to exec
2502 static void selinux_bprm_committing_creds(struct linux_binprm
*bprm
)
2504 struct task_security_struct
*new_tsec
;
2505 struct rlimit
*rlim
, *initrlim
;
2508 new_tsec
= selinux_cred(bprm
->cred
);
2509 if (new_tsec
->sid
== new_tsec
->osid
)
2512 /* Close files for which the new task SID is not authorized. */
2513 flush_unauthorized_files(bprm
->cred
, current
->files
);
2515 /* Always clear parent death signal on SID transitions. */
2516 current
->pdeath_signal
= 0;
2518 /* Check whether the new SID can inherit resource limits from the old
2519 * SID. If not, reset all soft limits to the lower of the current
2520 * task's hard limit and the init task's soft limit.
2522 * Note that the setting of hard limits (even to lower them) can be
2523 * controlled by the setrlimit check. The inclusion of the init task's
2524 * soft limit into the computation is to avoid resetting soft limits
2525 * higher than the default soft limit for cases where the default is
2526 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2528 rc
= avc_has_perm(&selinux_state
,
2529 new_tsec
->osid
, new_tsec
->sid
, SECCLASS_PROCESS
,
2530 PROCESS__RLIMITINH
, NULL
);
2532 /* protect against do_prlimit() */
2534 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
2535 rlim
= current
->signal
->rlim
+ i
;
2536 initrlim
= init_task
.signal
->rlim
+ i
;
2537 rlim
->rlim_cur
= min(rlim
->rlim_max
, initrlim
->rlim_cur
);
2539 task_unlock(current
);
2540 if (IS_ENABLED(CONFIG_POSIX_TIMERS
))
2541 update_rlimit_cpu(current
, rlimit(RLIMIT_CPU
));
2546 * Clean up the process immediately after the installation of new credentials
2549 static void selinux_bprm_committed_creds(struct linux_binprm
*bprm
)
2551 const struct task_security_struct
*tsec
= selinux_cred(current_cred());
2561 /* Check whether the new SID can inherit signal state from the old SID.
2562 * If not, clear itimers to avoid subsequent signal generation and
2563 * flush and unblock signals.
2565 * This must occur _after_ the task SID has been updated so that any
2566 * kill done after the flush will be checked against the new SID.
2568 rc
= avc_has_perm(&selinux_state
,
2569 osid
, sid
, SECCLASS_PROCESS
, PROCESS__SIGINH
, NULL
);
2573 spin_lock_irq(¤t
->sighand
->siglock
);
2574 if (!fatal_signal_pending(current
)) {
2575 flush_sigqueue(¤t
->pending
);
2576 flush_sigqueue(¤t
->signal
->shared_pending
);
2577 flush_signal_handlers(current
, 1);
2578 sigemptyset(¤t
->blocked
);
2579 recalc_sigpending();
2581 spin_unlock_irq(¤t
->sighand
->siglock
);
2584 /* Wake up the parent if it is waiting so that it can recheck
2585 * wait permission to the new task SID. */
2586 read_lock(&tasklist_lock
);
2587 __wake_up_parent(current
, current
->real_parent
);
2588 read_unlock(&tasklist_lock
);
2591 /* superblock security operations */
2593 static int selinux_sb_alloc_security(struct super_block
*sb
)
2595 struct superblock_security_struct
*sbsec
= selinux_superblock(sb
);
2597 mutex_init(&sbsec
->lock
);
2598 INIT_LIST_HEAD(&sbsec
->isec_head
);
2599 spin_lock_init(&sbsec
->isec_lock
);
2600 sbsec
->sid
= SECINITSID_UNLABELED
;
2601 sbsec
->def_sid
= SECINITSID_FILE
;
2602 sbsec
->mntpoint_sid
= SECINITSID_UNLABELED
;
2607 static inline int opt_len(const char *s
)
2609 bool open_quote
= false;
2613 for (len
= 0; (c
= s
[len
]) != '\0'; len
++) {
2615 open_quote
= !open_quote
;
2616 if (c
== ',' && !open_quote
)
2622 static int selinux_sb_eat_lsm_opts(char *options
, void **mnt_opts
)
2624 char *from
= options
;
2630 int len
= opt_len(from
);
2634 token
= match_opt_prefix(from
, len
, &arg
);
2636 if (token
!= Opt_error
) {
2641 for (p
= q
= arg
; p
< from
+ len
; p
++) {
2646 arg
= kmemdup_nul(arg
, q
- arg
, GFP_KERNEL
);
2652 rc
= selinux_add_opt(token
, arg
, mnt_opts
);
2658 if (!first
) { // copy with preceding comma
2663 memmove(to
, from
, len
);
2676 selinux_free_mnt_opts(*mnt_opts
);
2682 static int selinux_sb_mnt_opts_compat(struct super_block
*sb
, void *mnt_opts
)
2684 struct selinux_mnt_opts
*opts
= mnt_opts
;
2685 struct superblock_security_struct
*sbsec
= sb
->s_security
;
2690 * Superblock not initialized (i.e. no options) - reject if any
2691 * options specified, otherwise accept.
2693 if (!(sbsec
->flags
& SE_SBINITIALIZED
))
2694 return opts
? 1 : 0;
2697 * Superblock initialized and no options specified - reject if
2698 * superblock has any options set, otherwise accept.
2701 return (sbsec
->flags
& SE_MNTMASK
) ? 1 : 0;
2703 if (opts
->fscontext
) {
2704 rc
= parse_sid(sb
, opts
->fscontext
, &sid
);
2707 if (bad_option(sbsec
, FSCONTEXT_MNT
, sbsec
->sid
, sid
))
2710 if (opts
->context
) {
2711 rc
= parse_sid(sb
, opts
->context
, &sid
);
2714 if (bad_option(sbsec
, CONTEXT_MNT
, sbsec
->mntpoint_sid
, sid
))
2717 if (opts
->rootcontext
) {
2718 struct inode_security_struct
*root_isec
;
2720 root_isec
= backing_inode_security(sb
->s_root
);
2721 rc
= parse_sid(sb
, opts
->rootcontext
, &sid
);
2724 if (bad_option(sbsec
, ROOTCONTEXT_MNT
, root_isec
->sid
, sid
))
2727 if (opts
->defcontext
) {
2728 rc
= parse_sid(sb
, opts
->defcontext
, &sid
);
2731 if (bad_option(sbsec
, DEFCONTEXT_MNT
, sbsec
->def_sid
, sid
))
2737 static int selinux_sb_remount(struct super_block
*sb
, void *mnt_opts
)
2739 struct selinux_mnt_opts
*opts
= mnt_opts
;
2740 struct superblock_security_struct
*sbsec
= selinux_superblock(sb
);
2744 if (!(sbsec
->flags
& SE_SBINITIALIZED
))
2750 if (opts
->fscontext
) {
2751 rc
= parse_sid(sb
, opts
->fscontext
, &sid
);
2754 if (bad_option(sbsec
, FSCONTEXT_MNT
, sbsec
->sid
, sid
))
2755 goto out_bad_option
;
2757 if (opts
->context
) {
2758 rc
= parse_sid(sb
, opts
->context
, &sid
);
2761 if (bad_option(sbsec
, CONTEXT_MNT
, sbsec
->mntpoint_sid
, sid
))
2762 goto out_bad_option
;
2764 if (opts
->rootcontext
) {
2765 struct inode_security_struct
*root_isec
;
2766 root_isec
= backing_inode_security(sb
->s_root
);
2767 rc
= parse_sid(sb
, opts
->rootcontext
, &sid
);
2770 if (bad_option(sbsec
, ROOTCONTEXT_MNT
, root_isec
->sid
, sid
))
2771 goto out_bad_option
;
2773 if (opts
->defcontext
) {
2774 rc
= parse_sid(sb
, opts
->defcontext
, &sid
);
2777 if (bad_option(sbsec
, DEFCONTEXT_MNT
, sbsec
->def_sid
, sid
))
2778 goto out_bad_option
;
2783 pr_warn("SELinux: unable to change security options "
2784 "during remount (dev %s, type=%s)\n", sb
->s_id
,
2789 static int selinux_sb_kern_mount(struct super_block
*sb
)
2791 const struct cred
*cred
= current_cred();
2792 struct common_audit_data ad
;
2794 ad
.type
= LSM_AUDIT_DATA_DENTRY
;
2795 ad
.u
.dentry
= sb
->s_root
;
2796 return superblock_has_perm(cred
, sb
, FILESYSTEM__MOUNT
, &ad
);
2799 static int selinux_sb_statfs(struct dentry
*dentry
)
2801 const struct cred
*cred
= current_cred();
2802 struct common_audit_data ad
;
2804 ad
.type
= LSM_AUDIT_DATA_DENTRY
;
2805 ad
.u
.dentry
= dentry
->d_sb
->s_root
;
2806 return superblock_has_perm(cred
, dentry
->d_sb
, FILESYSTEM__GETATTR
, &ad
);
2809 static int selinux_mount(const char *dev_name
,
2810 const struct path
*path
,
2812 unsigned long flags
,
2815 const struct cred
*cred
= current_cred();
2817 if (flags
& MS_REMOUNT
)
2818 return superblock_has_perm(cred
, path
->dentry
->d_sb
,
2819 FILESYSTEM__REMOUNT
, NULL
);
2821 return path_has_perm(cred
, path
, FILE__MOUNTON
);
2824 static int selinux_move_mount(const struct path
*from_path
,
2825 const struct path
*to_path
)
2827 const struct cred
*cred
= current_cred();
2829 return path_has_perm(cred
, to_path
, FILE__MOUNTON
);
2832 static int selinux_umount(struct vfsmount
*mnt
, int flags
)
2834 const struct cred
*cred
= current_cred();
2836 return superblock_has_perm(cred
, mnt
->mnt_sb
,
2837 FILESYSTEM__UNMOUNT
, NULL
);
2840 static int selinux_fs_context_dup(struct fs_context
*fc
,
2841 struct fs_context
*src_fc
)
2843 const struct selinux_mnt_opts
*src
= src_fc
->security
;
2844 struct selinux_mnt_opts
*opts
;
2849 fc
->security
= kzalloc(sizeof(struct selinux_mnt_opts
), GFP_KERNEL
);
2853 opts
= fc
->security
;
2855 if (src
->fscontext
) {
2856 opts
->fscontext
= kstrdup(src
->fscontext
, GFP_KERNEL
);
2857 if (!opts
->fscontext
)
2861 opts
->context
= kstrdup(src
->context
, GFP_KERNEL
);
2865 if (src
->rootcontext
) {
2866 opts
->rootcontext
= kstrdup(src
->rootcontext
, GFP_KERNEL
);
2867 if (!opts
->rootcontext
)
2870 if (src
->defcontext
) {
2871 opts
->defcontext
= kstrdup(src
->defcontext
, GFP_KERNEL
);
2872 if (!opts
->defcontext
)
2878 static const struct fs_parameter_spec selinux_fs_parameters
[] = {
2879 fsparam_string(CONTEXT_STR
, Opt_context
),
2880 fsparam_string(DEFCONTEXT_STR
, Opt_defcontext
),
2881 fsparam_string(FSCONTEXT_STR
, Opt_fscontext
),
2882 fsparam_string(ROOTCONTEXT_STR
, Opt_rootcontext
),
2883 fsparam_flag (SECLABEL_STR
, Opt_seclabel
),
2887 static int selinux_fs_context_parse_param(struct fs_context
*fc
,
2888 struct fs_parameter
*param
)
2890 struct fs_parse_result result
;
2893 opt
= fs_parse(fc
, selinux_fs_parameters
, param
, &result
);
2897 rc
= selinux_add_opt(opt
, param
->string
, &fc
->security
);
2899 param
->string
= NULL
;
2905 /* inode security operations */
2907 static int selinux_inode_alloc_security(struct inode
*inode
)
2909 struct inode_security_struct
*isec
= selinux_inode(inode
);
2910 u32 sid
= current_sid();
2912 spin_lock_init(&isec
->lock
);
2913 INIT_LIST_HEAD(&isec
->list
);
2914 isec
->inode
= inode
;
2915 isec
->sid
= SECINITSID_UNLABELED
;
2916 isec
->sclass
= SECCLASS_FILE
;
2917 isec
->task_sid
= sid
;
2918 isec
->initialized
= LABEL_INVALID
;
2923 static void selinux_inode_free_security(struct inode
*inode
)
2925 inode_free_security(inode
);
2928 static int selinux_dentry_init_security(struct dentry
*dentry
, int mode
,
2929 const struct qstr
*name
, void **ctx
,
2935 rc
= selinux_determine_inode_label(selinux_cred(current_cred()),
2936 d_inode(dentry
->d_parent
), name
,
2937 inode_mode_to_security_class(mode
),
2942 return security_sid_to_context(&selinux_state
, newsid
, (char **)ctx
,
2946 static int selinux_dentry_create_files_as(struct dentry
*dentry
, int mode
,
2948 const struct cred
*old
,
2953 struct task_security_struct
*tsec
;
2955 rc
= selinux_determine_inode_label(selinux_cred(old
),
2956 d_inode(dentry
->d_parent
), name
,
2957 inode_mode_to_security_class(mode
),
2962 tsec
= selinux_cred(new);
2963 tsec
->create_sid
= newsid
;
2967 static int selinux_inode_init_security(struct inode
*inode
, struct inode
*dir
,
2968 const struct qstr
*qstr
,
2970 void **value
, size_t *len
)
2972 const struct task_security_struct
*tsec
= selinux_cred(current_cred());
2973 struct superblock_security_struct
*sbsec
;
2978 sbsec
= selinux_superblock(dir
->i_sb
);
2980 newsid
= tsec
->create_sid
;
2982 rc
= selinux_determine_inode_label(tsec
, dir
, qstr
,
2983 inode_mode_to_security_class(inode
->i_mode
),
2988 /* Possibly defer initialization to selinux_complete_init. */
2989 if (sbsec
->flags
& SE_SBINITIALIZED
) {
2990 struct inode_security_struct
*isec
= selinux_inode(inode
);
2991 isec
->sclass
= inode_mode_to_security_class(inode
->i_mode
);
2993 isec
->initialized
= LABEL_INITIALIZED
;
2996 if (!selinux_initialized(&selinux_state
) ||
2997 !(sbsec
->flags
& SBLABEL_MNT
))
3001 *name
= XATTR_SELINUX_SUFFIX
;
3004 rc
= security_sid_to_context_force(&selinux_state
, newsid
,
3015 static int selinux_inode_init_security_anon(struct inode
*inode
,
3016 const struct qstr
*name
,
3017 const struct inode
*context_inode
)
3019 const struct task_security_struct
*tsec
= selinux_cred(current_cred());
3020 struct common_audit_data ad
;
3021 struct inode_security_struct
*isec
;
3024 if (unlikely(!selinux_initialized(&selinux_state
)))
3027 isec
= selinux_inode(inode
);
3030 * We only get here once per ephemeral inode. The inode has
3031 * been initialized via inode_alloc_security but is otherwise
3035 if (context_inode
) {
3036 struct inode_security_struct
*context_isec
=
3037 selinux_inode(context_inode
);
3038 if (context_isec
->initialized
!= LABEL_INITIALIZED
) {
3039 pr_err("SELinux: context_inode is not initialized");
3043 isec
->sclass
= context_isec
->sclass
;
3044 isec
->sid
= context_isec
->sid
;
3046 isec
->sclass
= SECCLASS_ANON_INODE
;
3047 rc
= security_transition_sid(
3048 &selinux_state
, tsec
->sid
, tsec
->sid
,
3049 isec
->sclass
, name
, &isec
->sid
);
3054 isec
->initialized
= LABEL_INITIALIZED
;
3056 * Now that we've initialized security, check whether we're
3057 * allowed to actually create this type of anonymous inode.
3060 ad
.type
= LSM_AUDIT_DATA_INODE
;
3063 return avc_has_perm(&selinux_state
,
3071 static int selinux_inode_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
3073 return may_create(dir
, dentry
, SECCLASS_FILE
);
3076 static int selinux_inode_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
3078 return may_link(dir
, old_dentry
, MAY_LINK
);
3081 static int selinux_inode_unlink(struct inode
*dir
, struct dentry
*dentry
)
3083 return may_link(dir
, dentry
, MAY_UNLINK
);
3086 static int selinux_inode_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *name
)
3088 return may_create(dir
, dentry
, SECCLASS_LNK_FILE
);
3091 static int selinux_inode_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mask
)
3093 return may_create(dir
, dentry
, SECCLASS_DIR
);
3096 static int selinux_inode_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3098 return may_link(dir
, dentry
, MAY_RMDIR
);
3101 static int selinux_inode_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, dev_t dev
)
3103 return may_create(dir
, dentry
, inode_mode_to_security_class(mode
));
3106 static int selinux_inode_rename(struct inode
*old_inode
, struct dentry
*old_dentry
,
3107 struct inode
*new_inode
, struct dentry
*new_dentry
)
3109 return may_rename(old_inode
, old_dentry
, new_inode
, new_dentry
);
3112 static int selinux_inode_readlink(struct dentry
*dentry
)
3114 const struct cred
*cred
= current_cred();
3116 return dentry_has_perm(cred
, dentry
, FILE__READ
);
3119 static int selinux_inode_follow_link(struct dentry
*dentry
, struct inode
*inode
,
3122 const struct cred
*cred
= current_cred();
3123 struct common_audit_data ad
;
3124 struct inode_security_struct
*isec
;
3127 validate_creds(cred
);
3129 ad
.type
= LSM_AUDIT_DATA_DENTRY
;
3130 ad
.u
.dentry
= dentry
;
3131 sid
= cred_sid(cred
);
3132 isec
= inode_security_rcu(inode
, rcu
);
3134 return PTR_ERR(isec
);
3136 return avc_has_perm(&selinux_state
,
3137 sid
, isec
->sid
, isec
->sclass
, FILE__READ
, &ad
);
3140 static noinline
int audit_inode_permission(struct inode
*inode
,
3141 u32 perms
, u32 audited
, u32 denied
,
3144 struct common_audit_data ad
;
3145 struct inode_security_struct
*isec
= selinux_inode(inode
);
3147 ad
.type
= LSM_AUDIT_DATA_INODE
;
3150 return slow_avc_audit(&selinux_state
,
3151 current_sid(), isec
->sid
, isec
->sclass
, perms
,
3152 audited
, denied
, result
, &ad
);
3155 static int selinux_inode_permission(struct inode
*inode
, int mask
)
3157 const struct cred
*cred
= current_cred();
3160 bool no_block
= mask
& MAY_NOT_BLOCK
;
3161 struct inode_security_struct
*isec
;
3163 struct av_decision avd
;
3165 u32 audited
, denied
;
3167 from_access
= mask
& MAY_ACCESS
;
3168 mask
&= (MAY_READ
|MAY_WRITE
|MAY_EXEC
|MAY_APPEND
);
3170 /* No permission to check. Existence test. */
3174 validate_creds(cred
);
3176 if (unlikely(IS_PRIVATE(inode
)))
3179 perms
= file_mask_to_av(inode
->i_mode
, mask
);
3181 sid
= cred_sid(cred
);
3182 isec
= inode_security_rcu(inode
, no_block
);
3184 return PTR_ERR(isec
);
3186 rc
= avc_has_perm_noaudit(&selinux_state
,
3187 sid
, isec
->sid
, isec
->sclass
, perms
, 0,
3189 audited
= avc_audit_required(perms
, &avd
, rc
,
3190 from_access
? FILE__AUDIT_ACCESS
: 0,
3192 if (likely(!audited
))
3195 rc2
= audit_inode_permission(inode
, perms
, audited
, denied
, rc
);
3201 static int selinux_inode_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
3203 const struct cred
*cred
= current_cred();
3204 struct inode
*inode
= d_backing_inode(dentry
);
3205 unsigned int ia_valid
= iattr
->ia_valid
;
3206 __u32 av
= FILE__WRITE
;
3208 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3209 if (ia_valid
& ATTR_FORCE
) {
3210 ia_valid
&= ~(ATTR_KILL_SUID
| ATTR_KILL_SGID
| ATTR_MODE
|
3216 if (ia_valid
& (ATTR_MODE
| ATTR_UID
| ATTR_GID
|
3217 ATTR_ATIME_SET
| ATTR_MTIME_SET
| ATTR_TIMES_SET
))
3218 return dentry_has_perm(cred
, dentry
, FILE__SETATTR
);
3220 if (selinux_policycap_openperm() &&
3221 inode
->i_sb
->s_magic
!= SOCKFS_MAGIC
&&
3222 (ia_valid
& ATTR_SIZE
) &&
3223 !(ia_valid
& ATTR_FILE
))
3226 return dentry_has_perm(cred
, dentry
, av
);
3229 static int selinux_inode_getattr(const struct path
*path
)
3231 return path_has_perm(current_cred(), path
, FILE__GETATTR
);
3234 static bool has_cap_mac_admin(bool audit
)
3236 const struct cred
*cred
= current_cred();
3237 unsigned int opts
= audit
? CAP_OPT_NONE
: CAP_OPT_NOAUDIT
;
3239 if (cap_capable(cred
, &init_user_ns
, CAP_MAC_ADMIN
, opts
))
3241 if (cred_has_capability(cred
, CAP_MAC_ADMIN
, opts
, true))
3246 static int selinux_inode_setxattr(struct user_namespace
*mnt_userns
,
3247 struct dentry
*dentry
, const char *name
,
3248 const void *value
, size_t size
, int flags
)
3250 struct inode
*inode
= d_backing_inode(dentry
);
3251 struct inode_security_struct
*isec
;
3252 struct superblock_security_struct
*sbsec
;
3253 struct common_audit_data ad
;
3254 u32 newsid
, sid
= current_sid();
3257 if (strcmp(name
, XATTR_NAME_SELINUX
)) {
3258 rc
= cap_inode_setxattr(dentry
, name
, value
, size
, flags
);
3262 /* Not an attribute we recognize, so just check the
3263 ordinary setattr permission. */
3264 return dentry_has_perm(current_cred(), dentry
, FILE__SETATTR
);
3267 if (!selinux_initialized(&selinux_state
))
3268 return (inode_owner_or_capable(mnt_userns
, inode
) ? 0 : -EPERM
);
3270 sbsec
= selinux_superblock(inode
->i_sb
);
3271 if (!(sbsec
->flags
& SBLABEL_MNT
))
3274 if (!inode_owner_or_capable(mnt_userns
, inode
))
3277 ad
.type
= LSM_AUDIT_DATA_DENTRY
;
3278 ad
.u
.dentry
= dentry
;
3280 isec
= backing_inode_security(dentry
);
3281 rc
= avc_has_perm(&selinux_state
,
3282 sid
, isec
->sid
, isec
->sclass
,
3283 FILE__RELABELFROM
, &ad
);
3287 rc
= security_context_to_sid(&selinux_state
, value
, size
, &newsid
,
3289 if (rc
== -EINVAL
) {
3290 if (!has_cap_mac_admin(true)) {
3291 struct audit_buffer
*ab
;
3294 /* We strip a nul only if it is at the end, otherwise the
3295 * context contains a nul and we should audit that */
3297 const char *str
= value
;
3299 if (str
[size
- 1] == '\0')
3300 audit_size
= size
- 1;
3306 ab
= audit_log_start(audit_context(),
3307 GFP_ATOMIC
, AUDIT_SELINUX_ERR
);
3310 audit_log_format(ab
, "op=setxattr invalid_context=");
3311 audit_log_n_untrustedstring(ab
, value
, audit_size
);
3316 rc
= security_context_to_sid_force(&selinux_state
, value
,
3322 rc
= avc_has_perm(&selinux_state
,
3323 sid
, newsid
, isec
->sclass
,
3324 FILE__RELABELTO
, &ad
);
3328 rc
= security_validate_transition(&selinux_state
, isec
->sid
, newsid
,
3333 return avc_has_perm(&selinux_state
,
3336 SECCLASS_FILESYSTEM
,
3337 FILESYSTEM__ASSOCIATE
,
3341 static void selinux_inode_post_setxattr(struct dentry
*dentry
, const char *name
,
3342 const void *value
, size_t size
,
3345 struct inode
*inode
= d_backing_inode(dentry
);
3346 struct inode_security_struct
*isec
;
3350 if (strcmp(name
, XATTR_NAME_SELINUX
)) {
3351 /* Not an attribute we recognize, so nothing to do. */
3355 if (!selinux_initialized(&selinux_state
)) {
3356 /* If we haven't even been initialized, then we can't validate
3357 * against a policy, so leave the label as invalid. It may
3358 * resolve to a valid label on the next revalidation try if
3359 * we've since initialized.
3364 rc
= security_context_to_sid_force(&selinux_state
, value
, size
,
3367 pr_err("SELinux: unable to map context to SID"
3368 "for (%s, %lu), rc=%d\n",
3369 inode
->i_sb
->s_id
, inode
->i_ino
, -rc
);
3373 isec
= backing_inode_security(dentry
);
3374 spin_lock(&isec
->lock
);
3375 isec
->sclass
= inode_mode_to_security_class(inode
->i_mode
);
3377 isec
->initialized
= LABEL_INITIALIZED
;
3378 spin_unlock(&isec
->lock
);
3383 static int selinux_inode_getxattr(struct dentry
*dentry
, const char *name
)
3385 const struct cred
*cred
= current_cred();
3387 return dentry_has_perm(cred
, dentry
, FILE__GETATTR
);
3390 static int selinux_inode_listxattr(struct dentry
*dentry
)
3392 const struct cred
*cred
= current_cred();
3394 return dentry_has_perm(cred
, dentry
, FILE__GETATTR
);
3397 static int selinux_inode_removexattr(struct user_namespace
*mnt_userns
,
3398 struct dentry
*dentry
, const char *name
)
3400 if (strcmp(name
, XATTR_NAME_SELINUX
)) {
3401 int rc
= cap_inode_removexattr(mnt_userns
, dentry
, name
);
3405 /* Not an attribute we recognize, so just check the
3406 ordinary setattr permission. */
3407 return dentry_has_perm(current_cred(), dentry
, FILE__SETATTR
);
3410 if (!selinux_initialized(&selinux_state
))
3413 /* No one is allowed to remove a SELinux security label.
3414 You can change the label, but all data must be labeled. */
3418 static int selinux_path_notify(const struct path
*path
, u64 mask
,
3419 unsigned int obj_type
)
3424 struct common_audit_data ad
;
3426 ad
.type
= LSM_AUDIT_DATA_PATH
;
3430 * Set permission needed based on the type of mark being set.
3431 * Performs an additional check for sb watches.
3434 case FSNOTIFY_OBJ_TYPE_VFSMOUNT
:
3435 perm
= FILE__WATCH_MOUNT
;
3437 case FSNOTIFY_OBJ_TYPE_SB
:
3438 perm
= FILE__WATCH_SB
;
3439 ret
= superblock_has_perm(current_cred(), path
->dentry
->d_sb
,
3440 FILESYSTEM__WATCH
, &ad
);
3444 case FSNOTIFY_OBJ_TYPE_INODE
:
3451 /* blocking watches require the file:watch_with_perm permission */
3452 if (mask
& (ALL_FSNOTIFY_PERM_EVENTS
))
3453 perm
|= FILE__WATCH_WITH_PERM
;
3455 /* watches on read-like events need the file:watch_reads permission */
3456 if (mask
& (FS_ACCESS
| FS_ACCESS_PERM
| FS_CLOSE_NOWRITE
))
3457 perm
|= FILE__WATCH_READS
;
3459 return path_has_perm(current_cred(), path
, perm
);
3463 * Copy the inode security context value to the user.
3465 * Permission check is handled by selinux_inode_getxattr hook.
3467 static int selinux_inode_getsecurity(struct user_namespace
*mnt_userns
,
3468 struct inode
*inode
, const char *name
,
3469 void **buffer
, bool alloc
)
3473 char *context
= NULL
;
3474 struct inode_security_struct
*isec
;
3477 * If we're not initialized yet, then we can't validate contexts, so
3478 * just let vfs_getxattr fall back to using the on-disk xattr.
3480 if (!selinux_initialized(&selinux_state
) ||
3481 strcmp(name
, XATTR_SELINUX_SUFFIX
))
3485 * If the caller has CAP_MAC_ADMIN, then get the raw context
3486 * value even if it is not defined by current policy; otherwise,
3487 * use the in-core value under current policy.
3488 * Use the non-auditing forms of the permission checks since
3489 * getxattr may be called by unprivileged processes commonly
3490 * and lack of permission just means that we fall back to the
3491 * in-core context value, not a denial.
3493 isec
= inode_security(inode
);
3494 if (has_cap_mac_admin(false))
3495 error
= security_sid_to_context_force(&selinux_state
,
3496 isec
->sid
, &context
,
3499 error
= security_sid_to_context(&selinux_state
, isec
->sid
,
3513 static int selinux_inode_setsecurity(struct inode
*inode
, const char *name
,
3514 const void *value
, size_t size
, int flags
)
3516 struct inode_security_struct
*isec
= inode_security_novalidate(inode
);
3517 struct superblock_security_struct
*sbsec
;
3521 if (strcmp(name
, XATTR_SELINUX_SUFFIX
))
3524 sbsec
= selinux_superblock(inode
->i_sb
);
3525 if (!(sbsec
->flags
& SBLABEL_MNT
))
3528 if (!value
|| !size
)
3531 rc
= security_context_to_sid(&selinux_state
, value
, size
, &newsid
,
3536 spin_lock(&isec
->lock
);
3537 isec
->sclass
= inode_mode_to_security_class(inode
->i_mode
);
3539 isec
->initialized
= LABEL_INITIALIZED
;
3540 spin_unlock(&isec
->lock
);
3544 static int selinux_inode_listsecurity(struct inode
*inode
, char *buffer
, size_t buffer_size
)
3546 const int len
= sizeof(XATTR_NAME_SELINUX
);
3548 if (!selinux_initialized(&selinux_state
))
3551 if (buffer
&& len
<= buffer_size
)
3552 memcpy(buffer
, XATTR_NAME_SELINUX
, len
);
3556 static void selinux_inode_getsecid(struct inode
*inode
, u32
*secid
)
3558 struct inode_security_struct
*isec
= inode_security_novalidate(inode
);
3562 static int selinux_inode_copy_up(struct dentry
*src
, struct cred
**new)
3565 struct task_security_struct
*tsec
;
3566 struct cred
*new_creds
= *new;
3568 if (new_creds
== NULL
) {
3569 new_creds
= prepare_creds();
3574 tsec
= selinux_cred(new_creds
);
3575 /* Get label from overlay inode and set it in create_sid */
3576 selinux_inode_getsecid(d_inode(src
), &sid
);
3577 tsec
->create_sid
= sid
;
3582 static int selinux_inode_copy_up_xattr(const char *name
)
3584 /* The copy_up hook above sets the initial context on an inode, but we
3585 * don't then want to overwrite it by blindly copying all the lower
3586 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3588 if (strcmp(name
, XATTR_NAME_SELINUX
) == 0)
3589 return 1; /* Discard */
3591 * Any other attribute apart from SELINUX is not claimed, supported
3597 /* kernfs node operations */
3599 static int selinux_kernfs_init_security(struct kernfs_node
*kn_dir
,
3600 struct kernfs_node
*kn
)
3602 const struct task_security_struct
*tsec
= selinux_cred(current_cred());
3603 u32 parent_sid
, newsid
, clen
;
3607 rc
= kernfs_xattr_get(kn_dir
, XATTR_NAME_SELINUX
, NULL
, 0);
3614 context
= kmalloc(clen
, GFP_KERNEL
);
3618 rc
= kernfs_xattr_get(kn_dir
, XATTR_NAME_SELINUX
, context
, clen
);
3624 rc
= security_context_to_sid(&selinux_state
, context
, clen
, &parent_sid
,
3630 if (tsec
->create_sid
) {
3631 newsid
= tsec
->create_sid
;
3633 u16 secclass
= inode_mode_to_security_class(kn
->mode
);
3637 q
.hash_len
= hashlen_string(kn_dir
, kn
->name
);
3639 rc
= security_transition_sid(&selinux_state
, tsec
->sid
,
3640 parent_sid
, secclass
, &q
,
3646 rc
= security_sid_to_context_force(&selinux_state
, newsid
,
3651 rc
= kernfs_xattr_set(kn
, XATTR_NAME_SELINUX
, context
, clen
,
3658 /* file security operations */
3660 static int selinux_revalidate_file_permission(struct file
*file
, int mask
)
3662 const struct cred
*cred
= current_cred();
3663 struct inode
*inode
= file_inode(file
);
3665 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3666 if ((file
->f_flags
& O_APPEND
) && (mask
& MAY_WRITE
))
3669 return file_has_perm(cred
, file
,
3670 file_mask_to_av(inode
->i_mode
, mask
));
3673 static int selinux_file_permission(struct file
*file
, int mask
)
3675 struct inode
*inode
= file_inode(file
);
3676 struct file_security_struct
*fsec
= selinux_file(file
);
3677 struct inode_security_struct
*isec
;
3678 u32 sid
= current_sid();
3681 /* No permission to check. Existence test. */
3684 isec
= inode_security(inode
);
3685 if (sid
== fsec
->sid
&& fsec
->isid
== isec
->sid
&&
3686 fsec
->pseqno
== avc_policy_seqno(&selinux_state
))
3687 /* No change since file_open check. */
3690 return selinux_revalidate_file_permission(file
, mask
);
3693 static int selinux_file_alloc_security(struct file
*file
)
3695 struct file_security_struct
*fsec
= selinux_file(file
);
3696 u32 sid
= current_sid();
3699 fsec
->fown_sid
= sid
;
3705 * Check whether a task has the ioctl permission and cmd
3706 * operation to an inode.
3708 static int ioctl_has_perm(const struct cred
*cred
, struct file
*file
,
3709 u32 requested
, u16 cmd
)
3711 struct common_audit_data ad
;
3712 struct file_security_struct
*fsec
= selinux_file(file
);
3713 struct inode
*inode
= file_inode(file
);
3714 struct inode_security_struct
*isec
;
3715 struct lsm_ioctlop_audit ioctl
;
3716 u32 ssid
= cred_sid(cred
);
3718 u8 driver
= cmd
>> 8;
3719 u8 xperm
= cmd
& 0xff;
3721 ad
.type
= LSM_AUDIT_DATA_IOCTL_OP
;
3724 ad
.u
.op
->path
= file
->f_path
;
3726 if (ssid
!= fsec
->sid
) {
3727 rc
= avc_has_perm(&selinux_state
,
3736 if (unlikely(IS_PRIVATE(inode
)))
3739 isec
= inode_security(inode
);
3740 rc
= avc_has_extended_perms(&selinux_state
,
3741 ssid
, isec
->sid
, isec
->sclass
,
3742 requested
, driver
, xperm
, &ad
);
3747 static int selinux_file_ioctl(struct file
*file
, unsigned int cmd
,
3750 const struct cred
*cred
= current_cred();
3757 case FS_IOC_GETFLAGS
:
3758 case FS_IOC_GETVERSION
:
3759 error
= file_has_perm(cred
, file
, FILE__GETATTR
);
3762 case FS_IOC_SETFLAGS
:
3763 case FS_IOC_SETVERSION
:
3764 error
= file_has_perm(cred
, file
, FILE__SETATTR
);
3767 /* sys_ioctl() checks */
3770 error
= file_has_perm(cred
, file
, 0);
3775 error
= cred_has_capability(cred
, CAP_SYS_TTY_CONFIG
,
3776 CAP_OPT_NONE
, true);
3779 /* default case assumes that the command will go
3780 * to the file's ioctl() function.
3783 error
= ioctl_has_perm(cred
, file
, FILE__IOCTL
, (u16
) cmd
);
3788 static int default_noexec __ro_after_init
;
3790 static int file_map_prot_check(struct file
*file
, unsigned long prot
, int shared
)
3792 const struct cred
*cred
= current_cred();
3793 u32 sid
= cred_sid(cred
);
3796 if (default_noexec
&&
3797 (prot
& PROT_EXEC
) && (!file
|| IS_PRIVATE(file_inode(file
)) ||
3798 (!shared
&& (prot
& PROT_WRITE
)))) {
3800 * We are making executable an anonymous mapping or a
3801 * private file mapping that will also be writable.
3802 * This has an additional check.
3804 rc
= avc_has_perm(&selinux_state
,
3805 sid
, sid
, SECCLASS_PROCESS
,
3806 PROCESS__EXECMEM
, NULL
);
3812 /* read access is always possible with a mapping */
3813 u32 av
= FILE__READ
;
3815 /* write access only matters if the mapping is shared */
3816 if (shared
&& (prot
& PROT_WRITE
))
3819 if (prot
& PROT_EXEC
)
3820 av
|= FILE__EXECUTE
;
3822 return file_has_perm(cred
, file
, av
);
3829 static int selinux_mmap_addr(unsigned long addr
)
3833 if (addr
< CONFIG_LSM_MMAP_MIN_ADDR
) {
3834 u32 sid
= current_sid();
3835 rc
= avc_has_perm(&selinux_state
,
3836 sid
, sid
, SECCLASS_MEMPROTECT
,
3837 MEMPROTECT__MMAP_ZERO
, NULL
);
3843 static int selinux_mmap_file(struct file
*file
, unsigned long reqprot
,
3844 unsigned long prot
, unsigned long flags
)
3846 struct common_audit_data ad
;
3850 ad
.type
= LSM_AUDIT_DATA_FILE
;
3852 rc
= inode_has_perm(current_cred(), file_inode(file
),
3858 if (checkreqprot_get(&selinux_state
))
3861 return file_map_prot_check(file
, prot
,
3862 (flags
& MAP_TYPE
) == MAP_SHARED
);
3865 static int selinux_file_mprotect(struct vm_area_struct
*vma
,
3866 unsigned long reqprot
,
3869 const struct cred
*cred
= current_cred();
3870 u32 sid
= cred_sid(cred
);
3872 if (checkreqprot_get(&selinux_state
))
3875 if (default_noexec
&&
3876 (prot
& PROT_EXEC
) && !(vma
->vm_flags
& VM_EXEC
)) {
3878 if (vma
->vm_start
>= vma
->vm_mm
->start_brk
&&
3879 vma
->vm_end
<= vma
->vm_mm
->brk
) {
3880 rc
= avc_has_perm(&selinux_state
,
3881 sid
, sid
, SECCLASS_PROCESS
,
3882 PROCESS__EXECHEAP
, NULL
);
3883 } else if (!vma
->vm_file
&&
3884 ((vma
->vm_start
<= vma
->vm_mm
->start_stack
&&
3885 vma
->vm_end
>= vma
->vm_mm
->start_stack
) ||
3886 vma_is_stack_for_current(vma
))) {
3887 rc
= avc_has_perm(&selinux_state
,
3888 sid
, sid
, SECCLASS_PROCESS
,
3889 PROCESS__EXECSTACK
, NULL
);
3890 } else if (vma
->vm_file
&& vma
->anon_vma
) {
3892 * We are making executable a file mapping that has
3893 * had some COW done. Since pages might have been
3894 * written, check ability to execute the possibly
3895 * modified content. This typically should only
3896 * occur for text relocations.
3898 rc
= file_has_perm(cred
, vma
->vm_file
, FILE__EXECMOD
);
3904 return file_map_prot_check(vma
->vm_file
, prot
, vma
->vm_flags
&VM_SHARED
);
3907 static int selinux_file_lock(struct file
*file
, unsigned int cmd
)
3909 const struct cred
*cred
= current_cred();
3911 return file_has_perm(cred
, file
, FILE__LOCK
);
3914 static int selinux_file_fcntl(struct file
*file
, unsigned int cmd
,
3917 const struct cred
*cred
= current_cred();
3922 if ((file
->f_flags
& O_APPEND
) && !(arg
& O_APPEND
)) {
3923 err
= file_has_perm(cred
, file
, FILE__WRITE
);
3932 case F_GETOWNER_UIDS
:
3933 /* Just check FD__USE permission */
3934 err
= file_has_perm(cred
, file
, 0);
3942 #if BITS_PER_LONG == 32
3947 err
= file_has_perm(cred
, file
, FILE__LOCK
);
3954 static void selinux_file_set_fowner(struct file
*file
)
3956 struct file_security_struct
*fsec
;
3958 fsec
= selinux_file(file
);
3959 fsec
->fown_sid
= current_sid();
3962 static int selinux_file_send_sigiotask(struct task_struct
*tsk
,
3963 struct fown_struct
*fown
, int signum
)
3966 u32 sid
= task_sid_obj(tsk
);
3968 struct file_security_struct
*fsec
;
3970 /* struct fown_struct is never outside the context of a struct file */
3971 file
= container_of(fown
, struct file
, f_owner
);
3973 fsec
= selinux_file(file
);
3976 perm
= signal_to_av(SIGIO
); /* as per send_sigio_to_task */
3978 perm
= signal_to_av(signum
);
3980 return avc_has_perm(&selinux_state
,
3981 fsec
->fown_sid
, sid
,
3982 SECCLASS_PROCESS
, perm
, NULL
);
3985 static int selinux_file_receive(struct file
*file
)
3987 const struct cred
*cred
= current_cred();
3989 return file_has_perm(cred
, file
, file_to_av(file
));
3992 static int selinux_file_open(struct file
*file
)
3994 struct file_security_struct
*fsec
;
3995 struct inode_security_struct
*isec
;
3997 fsec
= selinux_file(file
);
3998 isec
= inode_security(file_inode(file
));
4000 * Save inode label and policy sequence number
4001 * at open-time so that selinux_file_permission
4002 * can determine whether revalidation is necessary.
4003 * Task label is already saved in the file security
4004 * struct as its SID.
4006 fsec
->isid
= isec
->sid
;
4007 fsec
->pseqno
= avc_policy_seqno(&selinux_state
);
4009 * Since the inode label or policy seqno may have changed
4010 * between the selinux_inode_permission check and the saving
4011 * of state above, recheck that access is still permitted.
4012 * Otherwise, access might never be revalidated against the
4013 * new inode label or new policy.
4014 * This check is not redundant - do not remove.
4016 return file_path_has_perm(file
->f_cred
, file
, open_file_to_av(file
));
4019 /* task security operations */
4021 static int selinux_task_alloc(struct task_struct
*task
,
4022 unsigned long clone_flags
)
4024 u32 sid
= current_sid();
4026 return avc_has_perm(&selinux_state
,
4027 sid
, sid
, SECCLASS_PROCESS
, PROCESS__FORK
, NULL
);
4031 * prepare a new set of credentials for modification
4033 static int selinux_cred_prepare(struct cred
*new, const struct cred
*old
,
4036 const struct task_security_struct
*old_tsec
= selinux_cred(old
);
4037 struct task_security_struct
*tsec
= selinux_cred(new);
4044 * transfer the SELinux data to a blank set of creds
4046 static void selinux_cred_transfer(struct cred
*new, const struct cred
*old
)
4048 const struct task_security_struct
*old_tsec
= selinux_cred(old
);
4049 struct task_security_struct
*tsec
= selinux_cred(new);
4054 static void selinux_cred_getsecid(const struct cred
*c
, u32
*secid
)
4056 *secid
= cred_sid(c
);
4060 * set the security data for a kernel service
4061 * - all the creation contexts are set to unlabelled
4063 static int selinux_kernel_act_as(struct cred
*new, u32 secid
)
4065 struct task_security_struct
*tsec
= selinux_cred(new);
4066 u32 sid
= current_sid();
4069 ret
= avc_has_perm(&selinux_state
,
4071 SECCLASS_KERNEL_SERVICE
,
4072 KERNEL_SERVICE__USE_AS_OVERRIDE
,
4076 tsec
->create_sid
= 0;
4077 tsec
->keycreate_sid
= 0;
4078 tsec
->sockcreate_sid
= 0;
4084 * set the file creation context in a security record to the same as the
4085 * objective context of the specified inode
4087 static int selinux_kernel_create_files_as(struct cred
*new, struct inode
*inode
)
4089 struct inode_security_struct
*isec
= inode_security(inode
);
4090 struct task_security_struct
*tsec
= selinux_cred(new);
4091 u32 sid
= current_sid();
4094 ret
= avc_has_perm(&selinux_state
,
4096 SECCLASS_KERNEL_SERVICE
,
4097 KERNEL_SERVICE__CREATE_FILES_AS
,
4101 tsec
->create_sid
= isec
->sid
;
4105 static int selinux_kernel_module_request(char *kmod_name
)
4107 struct common_audit_data ad
;
4109 ad
.type
= LSM_AUDIT_DATA_KMOD
;
4110 ad
.u
.kmod_name
= kmod_name
;
4112 return avc_has_perm(&selinux_state
,
4113 current_sid(), SECINITSID_KERNEL
, SECCLASS_SYSTEM
,
4114 SYSTEM__MODULE_REQUEST
, &ad
);
4117 static int selinux_kernel_module_from_file(struct file
*file
)
4119 struct common_audit_data ad
;
4120 struct inode_security_struct
*isec
;
4121 struct file_security_struct
*fsec
;
4122 u32 sid
= current_sid();
4127 return avc_has_perm(&selinux_state
,
4128 sid
, sid
, SECCLASS_SYSTEM
,
4129 SYSTEM__MODULE_LOAD
, NULL
);
4133 ad
.type
= LSM_AUDIT_DATA_FILE
;
4136 fsec
= selinux_file(file
);
4137 if (sid
!= fsec
->sid
) {
4138 rc
= avc_has_perm(&selinux_state
,
4139 sid
, fsec
->sid
, SECCLASS_FD
, FD__USE
, &ad
);
4144 isec
= inode_security(file_inode(file
));
4145 return avc_has_perm(&selinux_state
,
4146 sid
, isec
->sid
, SECCLASS_SYSTEM
,
4147 SYSTEM__MODULE_LOAD
, &ad
);
4150 static int selinux_kernel_read_file(struct file
*file
,
4151 enum kernel_read_file_id id
,
4157 case READING_MODULE
:
4158 rc
= selinux_kernel_module_from_file(contents
? file
: NULL
);
4167 static int selinux_kernel_load_data(enum kernel_load_data_id id
, bool contents
)
4172 case LOADING_MODULE
:
4173 rc
= selinux_kernel_module_from_file(NULL
);
4182 static int selinux_task_setpgid(struct task_struct
*p
, pid_t pgid
)
4184 return avc_has_perm(&selinux_state
,
4185 current_sid(), task_sid_obj(p
), SECCLASS_PROCESS
,
4186 PROCESS__SETPGID
, NULL
);
4189 static int selinux_task_getpgid(struct task_struct
*p
)
4191 return avc_has_perm(&selinux_state
,
4192 current_sid(), task_sid_obj(p
), SECCLASS_PROCESS
,
4193 PROCESS__GETPGID
, NULL
);
4196 static int selinux_task_getsid(struct task_struct
*p
)
4198 return avc_has_perm(&selinux_state
,
4199 current_sid(), task_sid_obj(p
), SECCLASS_PROCESS
,
4200 PROCESS__GETSESSION
, NULL
);
4203 static void selinux_task_getsecid_subj(struct task_struct
*p
, u32
*secid
)
4205 *secid
= task_sid_subj(p
);
4208 static void selinux_task_getsecid_obj(struct task_struct
*p
, u32
*secid
)
4210 *secid
= task_sid_obj(p
);
4213 static int selinux_task_setnice(struct task_struct
*p
, int nice
)
4215 return avc_has_perm(&selinux_state
,
4216 current_sid(), task_sid_obj(p
), SECCLASS_PROCESS
,
4217 PROCESS__SETSCHED
, NULL
);
4220 static int selinux_task_setioprio(struct task_struct
*p
, int ioprio
)
4222 return avc_has_perm(&selinux_state
,
4223 current_sid(), task_sid_obj(p
), SECCLASS_PROCESS
,
4224 PROCESS__SETSCHED
, NULL
);
4227 static int selinux_task_getioprio(struct task_struct
*p
)
4229 return avc_has_perm(&selinux_state
,
4230 current_sid(), task_sid_obj(p
), SECCLASS_PROCESS
,
4231 PROCESS__GETSCHED
, NULL
);
4234 static int selinux_task_prlimit(const struct cred
*cred
, const struct cred
*tcred
,
4241 if (flags
& LSM_PRLIMIT_WRITE
)
4242 av
|= PROCESS__SETRLIMIT
;
4243 if (flags
& LSM_PRLIMIT_READ
)
4244 av
|= PROCESS__GETRLIMIT
;
4245 return avc_has_perm(&selinux_state
,
4246 cred_sid(cred
), cred_sid(tcred
),
4247 SECCLASS_PROCESS
, av
, NULL
);
4250 static int selinux_task_setrlimit(struct task_struct
*p
, unsigned int resource
,
4251 struct rlimit
*new_rlim
)
4253 struct rlimit
*old_rlim
= p
->signal
->rlim
+ resource
;
4255 /* Control the ability to change the hard limit (whether
4256 lowering or raising it), so that the hard limit can
4257 later be used as a safe reset point for the soft limit
4258 upon context transitions. See selinux_bprm_committing_creds. */
4259 if (old_rlim
->rlim_max
!= new_rlim
->rlim_max
)
4260 return avc_has_perm(&selinux_state
,
4261 current_sid(), task_sid_obj(p
),
4262 SECCLASS_PROCESS
, PROCESS__SETRLIMIT
, NULL
);
4267 static int selinux_task_setscheduler(struct task_struct
*p
)
4269 return avc_has_perm(&selinux_state
,
4270 current_sid(), task_sid_obj(p
), SECCLASS_PROCESS
,
4271 PROCESS__SETSCHED
, NULL
);
4274 static int selinux_task_getscheduler(struct task_struct
*p
)
4276 return avc_has_perm(&selinux_state
,
4277 current_sid(), task_sid_obj(p
), SECCLASS_PROCESS
,
4278 PROCESS__GETSCHED
, NULL
);
4281 static int selinux_task_movememory(struct task_struct
*p
)
4283 return avc_has_perm(&selinux_state
,
4284 current_sid(), task_sid_obj(p
), SECCLASS_PROCESS
,
4285 PROCESS__SETSCHED
, NULL
);
4288 static int selinux_task_kill(struct task_struct
*p
, struct kernel_siginfo
*info
,
4289 int sig
, const struct cred
*cred
)
4295 perm
= PROCESS__SIGNULL
; /* null signal; existence test */
4297 perm
= signal_to_av(sig
);
4299 secid
= current_sid();
4301 secid
= cred_sid(cred
);
4302 return avc_has_perm(&selinux_state
,
4303 secid
, task_sid_obj(p
), SECCLASS_PROCESS
, perm
, NULL
);
4306 static void selinux_task_to_inode(struct task_struct
*p
,
4307 struct inode
*inode
)
4309 struct inode_security_struct
*isec
= selinux_inode(inode
);
4310 u32 sid
= task_sid_obj(p
);
4312 spin_lock(&isec
->lock
);
4313 isec
->sclass
= inode_mode_to_security_class(inode
->i_mode
);
4315 isec
->initialized
= LABEL_INITIALIZED
;
4316 spin_unlock(&isec
->lock
);
4319 /* Returns error only if unable to parse addresses */
4320 static int selinux_parse_skb_ipv4(struct sk_buff
*skb
,
4321 struct common_audit_data
*ad
, u8
*proto
)
4323 int offset
, ihlen
, ret
= -EINVAL
;
4324 struct iphdr _iph
, *ih
;
4326 offset
= skb_network_offset(skb
);
4327 ih
= skb_header_pointer(skb
, offset
, sizeof(_iph
), &_iph
);
4331 ihlen
= ih
->ihl
* 4;
4332 if (ihlen
< sizeof(_iph
))
4335 ad
->u
.net
->v4info
.saddr
= ih
->saddr
;
4336 ad
->u
.net
->v4info
.daddr
= ih
->daddr
;
4340 *proto
= ih
->protocol
;
4342 switch (ih
->protocol
) {
4344 struct tcphdr _tcph
, *th
;
4346 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
4350 th
= skb_header_pointer(skb
, offset
, sizeof(_tcph
), &_tcph
);
4354 ad
->u
.net
->sport
= th
->source
;
4355 ad
->u
.net
->dport
= th
->dest
;
4360 struct udphdr _udph
, *uh
;
4362 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
4366 uh
= skb_header_pointer(skb
, offset
, sizeof(_udph
), &_udph
);
4370 ad
->u
.net
->sport
= uh
->source
;
4371 ad
->u
.net
->dport
= uh
->dest
;
4375 case IPPROTO_DCCP
: {
4376 struct dccp_hdr _dccph
, *dh
;
4378 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
4382 dh
= skb_header_pointer(skb
, offset
, sizeof(_dccph
), &_dccph
);
4386 ad
->u
.net
->sport
= dh
->dccph_sport
;
4387 ad
->u
.net
->dport
= dh
->dccph_dport
;
4391 #if IS_ENABLED(CONFIG_IP_SCTP)
4392 case IPPROTO_SCTP
: {
4393 struct sctphdr _sctph
, *sh
;
4395 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
4399 sh
= skb_header_pointer(skb
, offset
, sizeof(_sctph
), &_sctph
);
4403 ad
->u
.net
->sport
= sh
->source
;
4404 ad
->u
.net
->dport
= sh
->dest
;
4415 #if IS_ENABLED(CONFIG_IPV6)
4417 /* Returns error only if unable to parse addresses */
4418 static int selinux_parse_skb_ipv6(struct sk_buff
*skb
,
4419 struct common_audit_data
*ad
, u8
*proto
)
4422 int ret
= -EINVAL
, offset
;
4423 struct ipv6hdr _ipv6h
, *ip6
;
4426 offset
= skb_network_offset(skb
);
4427 ip6
= skb_header_pointer(skb
, offset
, sizeof(_ipv6h
), &_ipv6h
);
4431 ad
->u
.net
->v6info
.saddr
= ip6
->saddr
;
4432 ad
->u
.net
->v6info
.daddr
= ip6
->daddr
;
4435 nexthdr
= ip6
->nexthdr
;
4436 offset
+= sizeof(_ipv6h
);
4437 offset
= ipv6_skip_exthdr(skb
, offset
, &nexthdr
, &frag_off
);
4446 struct tcphdr _tcph
, *th
;
4448 th
= skb_header_pointer(skb
, offset
, sizeof(_tcph
), &_tcph
);
4452 ad
->u
.net
->sport
= th
->source
;
4453 ad
->u
.net
->dport
= th
->dest
;
4458 struct udphdr _udph
, *uh
;
4460 uh
= skb_header_pointer(skb
, offset
, sizeof(_udph
), &_udph
);
4464 ad
->u
.net
->sport
= uh
->source
;
4465 ad
->u
.net
->dport
= uh
->dest
;
4469 case IPPROTO_DCCP
: {
4470 struct dccp_hdr _dccph
, *dh
;
4472 dh
= skb_header_pointer(skb
, offset
, sizeof(_dccph
), &_dccph
);
4476 ad
->u
.net
->sport
= dh
->dccph_sport
;
4477 ad
->u
.net
->dport
= dh
->dccph_dport
;
4481 #if IS_ENABLED(CONFIG_IP_SCTP)
4482 case IPPROTO_SCTP
: {
4483 struct sctphdr _sctph
, *sh
;
4485 sh
= skb_header_pointer(skb
, offset
, sizeof(_sctph
), &_sctph
);
4489 ad
->u
.net
->sport
= sh
->source
;
4490 ad
->u
.net
->dport
= sh
->dest
;
4494 /* includes fragments */
4504 static int selinux_parse_skb(struct sk_buff
*skb
, struct common_audit_data
*ad
,
4505 char **_addrp
, int src
, u8
*proto
)
4510 switch (ad
->u
.net
->family
) {
4512 ret
= selinux_parse_skb_ipv4(skb
, ad
, proto
);
4515 addrp
= (char *)(src
? &ad
->u
.net
->v4info
.saddr
:
4516 &ad
->u
.net
->v4info
.daddr
);
4519 #if IS_ENABLED(CONFIG_IPV6)
4521 ret
= selinux_parse_skb_ipv6(skb
, ad
, proto
);
4524 addrp
= (char *)(src
? &ad
->u
.net
->v6info
.saddr
:
4525 &ad
->u
.net
->v6info
.daddr
);
4535 "SELinux: failure in selinux_parse_skb(),"
4536 " unable to parse packet\n");
4546 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
4548 * @family: protocol family
4549 * @sid: the packet's peer label SID
4552 * Check the various different forms of network peer labeling and determine
4553 * the peer label/SID for the packet; most of the magic actually occurs in
4554 * the security server function security_net_peersid_cmp(). The function
4555 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4556 * or -EACCES if @sid is invalid due to inconsistencies with the different
4560 static int selinux_skb_peerlbl_sid(struct sk_buff
*skb
, u16 family
, u32
*sid
)
4567 err
= selinux_xfrm_skb_sid(skb
, &xfrm_sid
);
4570 err
= selinux_netlbl_skbuff_getsid(skb
, family
, &nlbl_type
, &nlbl_sid
);
4574 err
= security_net_peersid_resolve(&selinux_state
, nlbl_sid
,
4575 nlbl_type
, xfrm_sid
, sid
);
4576 if (unlikely(err
)) {
4578 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4579 " unable to determine packet's peer label\n");
4587 * selinux_conn_sid - Determine the child socket label for a connection
4588 * @sk_sid: the parent socket's SID
4589 * @skb_sid: the packet's SID
4590 * @conn_sid: the resulting connection SID
4592 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4593 * combined with the MLS information from @skb_sid in order to create
4594 * @conn_sid. If @skb_sid is not valid then @conn_sid is simply a copy
4595 * of @sk_sid. Returns zero on success, negative values on failure.
4598 static int selinux_conn_sid(u32 sk_sid
, u32 skb_sid
, u32
*conn_sid
)
4602 if (skb_sid
!= SECSID_NULL
)
4603 err
= security_sid_mls_copy(&selinux_state
, sk_sid
, skb_sid
,
4611 /* socket security operations */
4613 static int socket_sockcreate_sid(const struct task_security_struct
*tsec
,
4614 u16 secclass
, u32
*socksid
)
4616 if (tsec
->sockcreate_sid
> SECSID_NULL
) {
4617 *socksid
= tsec
->sockcreate_sid
;
4621 return security_transition_sid(&selinux_state
, tsec
->sid
, tsec
->sid
,
4622 secclass
, NULL
, socksid
);
4625 static int sock_has_perm(struct sock
*sk
, u32 perms
)
4627 struct sk_security_struct
*sksec
= selinux_sock(sk
);
4628 struct common_audit_data ad
;
4629 struct lsm_network_audit net
= {0,};
4631 if (sksec
->sid
== SECINITSID_KERNEL
)
4634 ad
.type
= LSM_AUDIT_DATA_NET
;
4638 return avc_has_perm(&selinux_state
,
4639 current_sid(), sksec
->sid
, sksec
->sclass
, perms
,
4643 static int selinux_socket_create(int family
, int type
,
4644 int protocol
, int kern
)
4646 const struct task_security_struct
*tsec
= selinux_cred(current_cred());
4654 secclass
= socket_type_to_security_class(family
, type
, protocol
);
4655 rc
= socket_sockcreate_sid(tsec
, secclass
, &newsid
);
4659 return avc_has_perm(&selinux_state
,
4660 tsec
->sid
, newsid
, secclass
, SOCKET__CREATE
, NULL
);
4663 static int selinux_socket_post_create(struct socket
*sock
, int family
,
4664 int type
, int protocol
, int kern
)
4666 const struct task_security_struct
*tsec
= selinux_cred(current_cred());
4667 struct inode_security_struct
*isec
= inode_security_novalidate(SOCK_INODE(sock
));
4668 struct sk_security_struct
*sksec
;
4669 u16 sclass
= socket_type_to_security_class(family
, type
, protocol
);
4670 u32 sid
= SECINITSID_KERNEL
;
4674 err
= socket_sockcreate_sid(tsec
, sclass
, &sid
);
4679 isec
->sclass
= sclass
;
4681 isec
->initialized
= LABEL_INITIALIZED
;
4684 sksec
= selinux_sock(sock
->sk
);
4685 sksec
->sclass
= sclass
;
4687 /* Allows detection of the first association on this socket */
4688 if (sksec
->sclass
== SECCLASS_SCTP_SOCKET
)
4689 sksec
->sctp_assoc_state
= SCTP_ASSOC_UNSET
;
4691 err
= selinux_netlbl_socket_post_create(sock
->sk
, family
);
4697 static int selinux_socket_socketpair(struct socket
*socka
,
4698 struct socket
*sockb
)
4700 struct sk_security_struct
*sksec_a
= selinux_sock(socka
->sk
);
4701 struct sk_security_struct
*sksec_b
= selinux_sock(sockb
->sk
);
4703 sksec_a
->peer_sid
= sksec_b
->sid
;
4704 sksec_b
->peer_sid
= sksec_a
->sid
;
4709 /* Range of port numbers used to automatically bind.
4710 Need to determine whether we should perform a name_bind
4711 permission check between the socket and the port number. */
4713 static int selinux_socket_bind(struct socket
*sock
, struct sockaddr
*address
, int addrlen
)
4715 struct sock
*sk
= sock
->sk
;
4716 struct sk_security_struct
*sksec
= selinux_sock(sk
);
4720 err
= sock_has_perm(sk
, SOCKET__BIND
);
4724 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
4725 family
= sk
->sk_family
;
4726 if (family
== PF_INET
|| family
== PF_INET6
) {
4728 struct common_audit_data ad
;
4729 struct lsm_network_audit net
= {0,};
4730 struct sockaddr_in
*addr4
= NULL
;
4731 struct sockaddr_in6
*addr6
= NULL
;
4733 unsigned short snum
;
4737 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4738 * that validates multiple binding addresses. Because of this
4739 * need to check address->sa_family as it is possible to have
4740 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4742 if (addrlen
< offsetofend(struct sockaddr
, sa_family
))
4744 family_sa
= address
->sa_family
;
4745 switch (family_sa
) {
4748 if (addrlen
< sizeof(struct sockaddr_in
))
4750 addr4
= (struct sockaddr_in
*)address
;
4751 if (family_sa
== AF_UNSPEC
) {
4752 /* see __inet_bind(), we only want to allow
4753 * AF_UNSPEC if the address is INADDR_ANY
4755 if (addr4
->sin_addr
.s_addr
!= htonl(INADDR_ANY
))
4757 family_sa
= AF_INET
;
4759 snum
= ntohs(addr4
->sin_port
);
4760 addrp
= (char *)&addr4
->sin_addr
.s_addr
;
4763 if (addrlen
< SIN6_LEN_RFC2133
)
4765 addr6
= (struct sockaddr_in6
*)address
;
4766 snum
= ntohs(addr6
->sin6_port
);
4767 addrp
= (char *)&addr6
->sin6_addr
.s6_addr
;
4773 ad
.type
= LSM_AUDIT_DATA_NET
;
4775 ad
.u
.net
->sport
= htons(snum
);
4776 ad
.u
.net
->family
= family_sa
;
4781 inet_get_local_port_range(sock_net(sk
), &low
, &high
);
4783 if (inet_port_requires_bind_service(sock_net(sk
), snum
) ||
4784 snum
< low
|| snum
> high
) {
4785 err
= sel_netport_sid(sk
->sk_protocol
,
4789 err
= avc_has_perm(&selinux_state
,
4792 SOCKET__NAME_BIND
, &ad
);
4798 switch (sksec
->sclass
) {
4799 case SECCLASS_TCP_SOCKET
:
4800 node_perm
= TCP_SOCKET__NODE_BIND
;
4803 case SECCLASS_UDP_SOCKET
:
4804 node_perm
= UDP_SOCKET__NODE_BIND
;
4807 case SECCLASS_DCCP_SOCKET
:
4808 node_perm
= DCCP_SOCKET__NODE_BIND
;
4811 case SECCLASS_SCTP_SOCKET
:
4812 node_perm
= SCTP_SOCKET__NODE_BIND
;
4816 node_perm
= RAWIP_SOCKET__NODE_BIND
;
4820 err
= sel_netnode_sid(addrp
, family_sa
, &sid
);
4824 if (family_sa
== AF_INET
)
4825 ad
.u
.net
->v4info
.saddr
= addr4
->sin_addr
.s_addr
;
4827 ad
.u
.net
->v6info
.saddr
= addr6
->sin6_addr
;
4829 err
= avc_has_perm(&selinux_state
,
4831 sksec
->sclass
, node_perm
, &ad
);
4838 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4839 if (sksec
->sclass
== SECCLASS_SCTP_SOCKET
)
4841 return -EAFNOSUPPORT
;
4844 /* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
4845 * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst
4847 static int selinux_socket_connect_helper(struct socket
*sock
,
4848 struct sockaddr
*address
, int addrlen
)
4850 struct sock
*sk
= sock
->sk
;
4851 struct sk_security_struct
*sksec
= selinux_sock(sk
);
4854 err
= sock_has_perm(sk
, SOCKET__CONNECT
);
4857 if (addrlen
< offsetofend(struct sockaddr
, sa_family
))
4860 /* connect(AF_UNSPEC) has special handling, as it is a documented
4861 * way to disconnect the socket
4863 if (address
->sa_family
== AF_UNSPEC
)
4867 * If a TCP, DCCP or SCTP socket, check name_connect permission
4870 if (sksec
->sclass
== SECCLASS_TCP_SOCKET
||
4871 sksec
->sclass
== SECCLASS_DCCP_SOCKET
||
4872 sksec
->sclass
== SECCLASS_SCTP_SOCKET
) {
4873 struct common_audit_data ad
;
4874 struct lsm_network_audit net
= {0,};
4875 struct sockaddr_in
*addr4
= NULL
;
4876 struct sockaddr_in6
*addr6
= NULL
;
4877 unsigned short snum
;
4880 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4881 * that validates multiple connect addresses. Because of this
4882 * need to check address->sa_family as it is possible to have
4883 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4885 switch (address
->sa_family
) {
4887 addr4
= (struct sockaddr_in
*)address
;
4888 if (addrlen
< sizeof(struct sockaddr_in
))
4890 snum
= ntohs(addr4
->sin_port
);
4893 addr6
= (struct sockaddr_in6
*)address
;
4894 if (addrlen
< SIN6_LEN_RFC2133
)
4896 snum
= ntohs(addr6
->sin6_port
);
4899 /* Note that SCTP services expect -EINVAL, whereas
4900 * others expect -EAFNOSUPPORT.
4902 if (sksec
->sclass
== SECCLASS_SCTP_SOCKET
)
4905 return -EAFNOSUPPORT
;
4908 err
= sel_netport_sid(sk
->sk_protocol
, snum
, &sid
);
4912 switch (sksec
->sclass
) {
4913 case SECCLASS_TCP_SOCKET
:
4914 perm
= TCP_SOCKET__NAME_CONNECT
;
4916 case SECCLASS_DCCP_SOCKET
:
4917 perm
= DCCP_SOCKET__NAME_CONNECT
;
4919 case SECCLASS_SCTP_SOCKET
:
4920 perm
= SCTP_SOCKET__NAME_CONNECT
;
4924 ad
.type
= LSM_AUDIT_DATA_NET
;
4926 ad
.u
.net
->dport
= htons(snum
);
4927 ad
.u
.net
->family
= address
->sa_family
;
4928 err
= avc_has_perm(&selinux_state
,
4929 sksec
->sid
, sid
, sksec
->sclass
, perm
, &ad
);
4937 /* Supports connect(2), see comments in selinux_socket_connect_helper() */
4938 static int selinux_socket_connect(struct socket
*sock
,
4939 struct sockaddr
*address
, int addrlen
)
4942 struct sock
*sk
= sock
->sk
;
4944 err
= selinux_socket_connect_helper(sock
, address
, addrlen
);
4948 return selinux_netlbl_socket_connect(sk
, address
);
4951 static int selinux_socket_listen(struct socket
*sock
, int backlog
)
4953 return sock_has_perm(sock
->sk
, SOCKET__LISTEN
);
4956 static int selinux_socket_accept(struct socket
*sock
, struct socket
*newsock
)
4959 struct inode_security_struct
*isec
;
4960 struct inode_security_struct
*newisec
;
4964 err
= sock_has_perm(sock
->sk
, SOCKET__ACCEPT
);
4968 isec
= inode_security_novalidate(SOCK_INODE(sock
));
4969 spin_lock(&isec
->lock
);
4970 sclass
= isec
->sclass
;
4972 spin_unlock(&isec
->lock
);
4974 newisec
= inode_security_novalidate(SOCK_INODE(newsock
));
4975 newisec
->sclass
= sclass
;
4977 newisec
->initialized
= LABEL_INITIALIZED
;
4982 static int selinux_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
4985 return sock_has_perm(sock
->sk
, SOCKET__WRITE
);
4988 static int selinux_socket_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
4989 int size
, int flags
)
4991 return sock_has_perm(sock
->sk
, SOCKET__READ
);
4994 static int selinux_socket_getsockname(struct socket
*sock
)
4996 return sock_has_perm(sock
->sk
, SOCKET__GETATTR
);
4999 static int selinux_socket_getpeername(struct socket
*sock
)
5001 return sock_has_perm(sock
->sk
, SOCKET__GETATTR
);
5004 static int selinux_socket_setsockopt(struct socket
*sock
, int level
, int optname
)
5008 err
= sock_has_perm(sock
->sk
, SOCKET__SETOPT
);
5012 return selinux_netlbl_socket_setsockopt(sock
, level
, optname
);
5015 static int selinux_socket_getsockopt(struct socket
*sock
, int level
,
5018 return sock_has_perm(sock
->sk
, SOCKET__GETOPT
);
5021 static int selinux_socket_shutdown(struct socket
*sock
, int how
)
5023 return sock_has_perm(sock
->sk
, SOCKET__SHUTDOWN
);
5026 static int selinux_socket_unix_stream_connect(struct sock
*sock
,
5030 struct sk_security_struct
*sksec_sock
= selinux_sock(sock
);
5031 struct sk_security_struct
*sksec_other
= selinux_sock(other
);
5032 struct sk_security_struct
*sksec_new
= selinux_sock(newsk
);
5033 struct common_audit_data ad
;
5034 struct lsm_network_audit net
= {0,};
5037 ad
.type
= LSM_AUDIT_DATA_NET
;
5039 ad
.u
.net
->sk
= other
;
5041 err
= avc_has_perm(&selinux_state
,
5042 sksec_sock
->sid
, sksec_other
->sid
,
5043 sksec_other
->sclass
,
5044 UNIX_STREAM_SOCKET__CONNECTTO
, &ad
);
5048 /* server child socket */
5049 sksec_new
->peer_sid
= sksec_sock
->sid
;
5050 err
= security_sid_mls_copy(&selinux_state
, sksec_other
->sid
,
5051 sksec_sock
->sid
, &sksec_new
->sid
);
5055 /* connecting socket */
5056 sksec_sock
->peer_sid
= sksec_new
->sid
;
5061 static int selinux_socket_unix_may_send(struct socket
*sock
,
5062 struct socket
*other
)
5064 struct sk_security_struct
*ssec
= selinux_sock(sock
->sk
);
5065 struct sk_security_struct
*osec
= selinux_sock(other
->sk
);
5066 struct common_audit_data ad
;
5067 struct lsm_network_audit net
= {0,};
5069 ad
.type
= LSM_AUDIT_DATA_NET
;
5071 ad
.u
.net
->sk
= other
->sk
;
5073 return avc_has_perm(&selinux_state
,
5074 ssec
->sid
, osec
->sid
, osec
->sclass
, SOCKET__SENDTO
,
5078 static int selinux_inet_sys_rcv_skb(struct net
*ns
, int ifindex
,
5079 char *addrp
, u16 family
, u32 peer_sid
,
5080 struct common_audit_data
*ad
)
5086 err
= sel_netif_sid(ns
, ifindex
, &if_sid
);
5089 err
= avc_has_perm(&selinux_state
,
5091 SECCLASS_NETIF
, NETIF__INGRESS
, ad
);
5095 err
= sel_netnode_sid(addrp
, family
, &node_sid
);
5098 return avc_has_perm(&selinux_state
,
5100 SECCLASS_NODE
, NODE__RECVFROM
, ad
);
5103 static int selinux_sock_rcv_skb_compat(struct sock
*sk
, struct sk_buff
*skb
,
5107 struct sk_security_struct
*sksec
= selinux_sock(sk
);
5108 u32 sk_sid
= sksec
->sid
;
5109 struct common_audit_data ad
;
5110 struct lsm_network_audit net
= {0,};
5113 ad
.type
= LSM_AUDIT_DATA_NET
;
5115 ad
.u
.net
->netif
= skb
->skb_iif
;
5116 ad
.u
.net
->family
= family
;
5117 err
= selinux_parse_skb(skb
, &ad
, &addrp
, 1, NULL
);
5121 if (selinux_secmark_enabled()) {
5122 err
= avc_has_perm(&selinux_state
,
5123 sk_sid
, skb
->secmark
, SECCLASS_PACKET
,
5129 err
= selinux_netlbl_sock_rcv_skb(sksec
, skb
, family
, &ad
);
5132 err
= selinux_xfrm_sock_rcv_skb(sksec
->sid
, skb
, &ad
);
5137 static int selinux_socket_sock_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
5140 struct sk_security_struct
*sksec
= selinux_sock(sk
);
5141 u16 family
= sk
->sk_family
;
5142 u32 sk_sid
= sksec
->sid
;
5143 struct common_audit_data ad
;
5144 struct lsm_network_audit net
= {0,};
5149 if (family
!= PF_INET
&& family
!= PF_INET6
)
5152 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
5153 if (family
== PF_INET6
&& skb
->protocol
== htons(ETH_P_IP
))
5156 /* If any sort of compatibility mode is enabled then handoff processing
5157 * to the selinux_sock_rcv_skb_compat() function to deal with the
5158 * special handling. We do this in an attempt to keep this function
5159 * as fast and as clean as possible. */
5160 if (!selinux_policycap_netpeer())
5161 return selinux_sock_rcv_skb_compat(sk
, skb
, family
);
5163 secmark_active
= selinux_secmark_enabled();
5164 peerlbl_active
= selinux_peerlbl_enabled();
5165 if (!secmark_active
&& !peerlbl_active
)
5168 ad
.type
= LSM_AUDIT_DATA_NET
;
5170 ad
.u
.net
->netif
= skb
->skb_iif
;
5171 ad
.u
.net
->family
= family
;
5172 err
= selinux_parse_skb(skb
, &ad
, &addrp
, 1, NULL
);
5176 if (peerlbl_active
) {
5179 err
= selinux_skb_peerlbl_sid(skb
, family
, &peer_sid
);
5182 err
= selinux_inet_sys_rcv_skb(sock_net(sk
), skb
->skb_iif
,
5183 addrp
, family
, peer_sid
, &ad
);
5185 selinux_netlbl_err(skb
, family
, err
, 0);
5188 err
= avc_has_perm(&selinux_state
,
5189 sk_sid
, peer_sid
, SECCLASS_PEER
,
5192 selinux_netlbl_err(skb
, family
, err
, 0);
5197 if (secmark_active
) {
5198 err
= avc_has_perm(&selinux_state
,
5199 sk_sid
, skb
->secmark
, SECCLASS_PACKET
,
5208 static int selinux_socket_getpeersec_stream(struct socket
*sock
,
5209 char __user
*optval
,
5216 struct sk_security_struct
*sksec
= selinux_sock(sock
->sk
);
5217 u32 peer_sid
= SECSID_NULL
;
5219 if (sksec
->sclass
== SECCLASS_UNIX_STREAM_SOCKET
||
5220 sksec
->sclass
== SECCLASS_TCP_SOCKET
||
5221 sksec
->sclass
== SECCLASS_SCTP_SOCKET
)
5222 peer_sid
= sksec
->peer_sid
;
5223 if (peer_sid
== SECSID_NULL
)
5224 return -ENOPROTOOPT
;
5226 err
= security_sid_to_context(&selinux_state
, peer_sid
, &scontext
,
5231 if (scontext_len
> len
) {
5236 if (copy_to_user(optval
, scontext
, scontext_len
))
5240 if (put_user(scontext_len
, optlen
))
5246 static int selinux_socket_getpeersec_dgram(struct socket
*sock
, struct sk_buff
*skb
, u32
*secid
)
5248 u32 peer_secid
= SECSID_NULL
;
5250 struct inode_security_struct
*isec
;
5252 if (skb
&& skb
->protocol
== htons(ETH_P_IP
))
5254 else if (skb
&& skb
->protocol
== htons(ETH_P_IPV6
))
5257 family
= sock
->sk
->sk_family
;
5261 if (sock
&& family
== PF_UNIX
) {
5262 isec
= inode_security_novalidate(SOCK_INODE(sock
));
5263 peer_secid
= isec
->sid
;
5265 selinux_skb_peerlbl_sid(skb
, family
, &peer_secid
);
5268 *secid
= peer_secid
;
5269 if (peer_secid
== SECSID_NULL
)
5274 static int selinux_sk_alloc_security(struct sock
*sk
, int family
, gfp_t priority
)
5276 struct sk_security_struct
*sksec
= selinux_sock(sk
);
5278 sksec
->peer_sid
= SECINITSID_UNLABELED
;
5279 sksec
->sid
= SECINITSID_UNLABELED
;
5280 sksec
->sclass
= SECCLASS_SOCKET
;
5281 selinux_netlbl_sk_security_reset(sksec
);
5286 static void selinux_sk_free_security(struct sock
*sk
)
5288 struct sk_security_struct
*sksec
= selinux_sock(sk
);
5290 selinux_netlbl_sk_security_free(sksec
);
5293 static void selinux_sk_clone_security(const struct sock
*sk
, struct sock
*newsk
)
5295 struct sk_security_struct
*sksec
= selinux_sock(sk
);
5296 struct sk_security_struct
*newsksec
= selinux_sock(newsk
);
5298 newsksec
->sid
= sksec
->sid
;
5299 newsksec
->peer_sid
= sksec
->peer_sid
;
5300 newsksec
->sclass
= sksec
->sclass
;
5302 selinux_netlbl_sk_security_reset(newsksec
);
5305 static void selinux_sk_getsecid(struct sock
*sk
, u32
*secid
)
5308 *secid
= SECINITSID_ANY_SOCKET
;
5310 struct sk_security_struct
*sksec
= selinux_sock(sk
);
5312 *secid
= sksec
->sid
;
5316 static void selinux_sock_graft(struct sock
*sk
, struct socket
*parent
)
5318 struct inode_security_struct
*isec
=
5319 inode_security_novalidate(SOCK_INODE(parent
));
5320 struct sk_security_struct
*sksec
= selinux_sock(sk
);
5322 if (sk
->sk_family
== PF_INET
|| sk
->sk_family
== PF_INET6
||
5323 sk
->sk_family
== PF_UNIX
)
5324 isec
->sid
= sksec
->sid
;
5325 sksec
->sclass
= isec
->sclass
;
5328 /* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5329 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5332 static int selinux_sctp_assoc_request(struct sctp_endpoint
*ep
,
5333 struct sk_buff
*skb
)
5335 struct sk_security_struct
*sksec
= selinux_sock(ep
->base
.sk
);
5336 struct common_audit_data ad
;
5337 struct lsm_network_audit net
= {0,};
5339 u32 peer_sid
= SECINITSID_UNLABELED
;
5343 if (!selinux_policycap_extsockclass())
5346 peerlbl_active
= selinux_peerlbl_enabled();
5348 if (peerlbl_active
) {
5349 /* This will return peer_sid = SECSID_NULL if there are
5350 * no peer labels, see security_net_peersid_resolve().
5352 err
= selinux_skb_peerlbl_sid(skb
, ep
->base
.sk
->sk_family
,
5357 if (peer_sid
== SECSID_NULL
)
5358 peer_sid
= SECINITSID_UNLABELED
;
5361 if (sksec
->sctp_assoc_state
== SCTP_ASSOC_UNSET
) {
5362 sksec
->sctp_assoc_state
= SCTP_ASSOC_SET
;
5364 /* Here as first association on socket. As the peer SID
5365 * was allowed by peer recv (and the netif/node checks),
5366 * then it is approved by policy and used as the primary
5367 * peer SID for getpeercon(3).
5369 sksec
->peer_sid
= peer_sid
;
5370 } else if (sksec
->peer_sid
!= peer_sid
) {
5371 /* Other association peer SIDs are checked to enforce
5372 * consistency among the peer SIDs.
5374 ad
.type
= LSM_AUDIT_DATA_NET
;
5376 ad
.u
.net
->sk
= ep
->base
.sk
;
5377 err
= avc_has_perm(&selinux_state
,
5378 sksec
->peer_sid
, peer_sid
, sksec
->sclass
,
5379 SCTP_SOCKET__ASSOCIATION
, &ad
);
5384 /* Compute the MLS component for the connection and store
5385 * the information in ep. This will be used by SCTP TCP type
5386 * sockets and peeled off connections as they cause a new
5387 * socket to be generated. selinux_sctp_sk_clone() will then
5388 * plug this into the new socket.
5390 err
= selinux_conn_sid(sksec
->sid
, peer_sid
, &conn_sid
);
5394 ep
->secid
= conn_sid
;
5395 ep
->peer_secid
= peer_sid
;
5397 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5398 return selinux_netlbl_sctp_assoc_request(ep
, skb
);
5401 /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5402 * based on their @optname.
5404 static int selinux_sctp_bind_connect(struct sock
*sk
, int optname
,
5405 struct sockaddr
*address
,
5408 int len
, err
= 0, walk_size
= 0;
5410 struct sockaddr
*addr
;
5411 struct socket
*sock
;
5413 if (!selinux_policycap_extsockclass())
5416 /* Process one or more addresses that may be IPv4 or IPv6 */
5417 sock
= sk
->sk_socket
;
5420 while (walk_size
< addrlen
) {
5421 if (walk_size
+ sizeof(sa_family_t
) > addrlen
)
5425 switch (addr
->sa_family
) {
5428 len
= sizeof(struct sockaddr_in
);
5431 len
= sizeof(struct sockaddr_in6
);
5437 if (walk_size
+ len
> addrlen
)
5443 case SCTP_PRIMARY_ADDR
:
5444 case SCTP_SET_PEER_PRIMARY_ADDR
:
5445 case SCTP_SOCKOPT_BINDX_ADD
:
5446 err
= selinux_socket_bind(sock
, addr
, len
);
5448 /* Connect checks */
5449 case SCTP_SOCKOPT_CONNECTX
:
5450 case SCTP_PARAM_SET_PRIMARY
:
5451 case SCTP_PARAM_ADD_IP
:
5452 case SCTP_SENDMSG_CONNECT
:
5453 err
= selinux_socket_connect_helper(sock
, addr
, len
);
5457 /* As selinux_sctp_bind_connect() is called by the
5458 * SCTP protocol layer, the socket is already locked,
5459 * therefore selinux_netlbl_socket_connect_locked()
5460 * is called here. The situations handled are:
5461 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5462 * whenever a new IP address is added or when a new
5463 * primary address is selected.
5464 * Note that an SCTP connect(2) call happens before
5465 * the SCTP protocol layer and is handled via
5466 * selinux_socket_connect().
5468 err
= selinux_netlbl_socket_connect_locked(sk
, addr
);
5482 /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5483 static void selinux_sctp_sk_clone(struct sctp_endpoint
*ep
, struct sock
*sk
,
5486 struct sk_security_struct
*sksec
= selinux_sock(sk
);
5487 struct sk_security_struct
*newsksec
= selinux_sock(newsk
);
5489 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5490 * the non-sctp clone version.
5492 if (!selinux_policycap_extsockclass())
5493 return selinux_sk_clone_security(sk
, newsk
);
5495 newsksec
->sid
= ep
->secid
;
5496 newsksec
->peer_sid
= ep
->peer_secid
;
5497 newsksec
->sclass
= sksec
->sclass
;
5498 selinux_netlbl_sctp_sk_clone(sk
, newsk
);
5501 static int selinux_inet_conn_request(const struct sock
*sk
, struct sk_buff
*skb
,
5502 struct request_sock
*req
)
5504 struct sk_security_struct
*sksec
= selinux_sock(sk
);
5506 u16 family
= req
->rsk_ops
->family
;
5510 err
= selinux_skb_peerlbl_sid(skb
, family
, &peersid
);
5513 err
= selinux_conn_sid(sksec
->sid
, peersid
, &connsid
);
5516 req
->secid
= connsid
;
5517 req
->peer_secid
= peersid
;
5519 return selinux_netlbl_inet_conn_request(req
, family
);
5522 static void selinux_inet_csk_clone(struct sock
*newsk
,
5523 const struct request_sock
*req
)
5525 struct sk_security_struct
*newsksec
= selinux_sock(newsk
);
5527 newsksec
->sid
= req
->secid
;
5528 newsksec
->peer_sid
= req
->peer_secid
;
5529 /* NOTE: Ideally, we should also get the isec->sid for the
5530 new socket in sync, but we don't have the isec available yet.
5531 So we will wait until sock_graft to do it, by which
5532 time it will have been created and available. */
5534 /* We don't need to take any sort of lock here as we are the only
5535 * thread with access to newsksec */
5536 selinux_netlbl_inet_csk_clone(newsk
, req
->rsk_ops
->family
);
5539 static void selinux_inet_conn_established(struct sock
*sk
, struct sk_buff
*skb
)
5541 u16 family
= sk
->sk_family
;
5542 struct sk_security_struct
*sksec
= selinux_sock(sk
);
5544 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5545 if (family
== PF_INET6
&& skb
->protocol
== htons(ETH_P_IP
))
5548 selinux_skb_peerlbl_sid(skb
, family
, &sksec
->peer_sid
);
5551 static int selinux_secmark_relabel_packet(u32 sid
)
5553 const struct task_security_struct
*__tsec
;
5556 __tsec
= selinux_cred(current_cred());
5559 return avc_has_perm(&selinux_state
,
5560 tsid
, sid
, SECCLASS_PACKET
, PACKET__RELABELTO
,
5564 static void selinux_secmark_refcount_inc(void)
5566 atomic_inc(&selinux_secmark_refcount
);
5569 static void selinux_secmark_refcount_dec(void)
5571 atomic_dec(&selinux_secmark_refcount
);
5574 static void selinux_req_classify_flow(const struct request_sock
*req
,
5575 struct flowi_common
*flic
)
5577 flic
->flowic_secid
= req
->secid
;
5580 static int selinux_tun_dev_alloc_security(void **security
)
5582 struct tun_security_struct
*tunsec
;
5584 tunsec
= kzalloc(sizeof(*tunsec
), GFP_KERNEL
);
5587 tunsec
->sid
= current_sid();
5593 static void selinux_tun_dev_free_security(void *security
)
5598 static int selinux_tun_dev_create(void)
5600 u32 sid
= current_sid();
5602 /* we aren't taking into account the "sockcreate" SID since the socket
5603 * that is being created here is not a socket in the traditional sense,
5604 * instead it is a private sock, accessible only to the kernel, and
5605 * representing a wide range of network traffic spanning multiple
5606 * connections unlike traditional sockets - check the TUN driver to
5607 * get a better understanding of why this socket is special */
5609 return avc_has_perm(&selinux_state
,
5610 sid
, sid
, SECCLASS_TUN_SOCKET
, TUN_SOCKET__CREATE
,
5614 static int selinux_tun_dev_attach_queue(void *security
)
5616 struct tun_security_struct
*tunsec
= security
;
5618 return avc_has_perm(&selinux_state
,
5619 current_sid(), tunsec
->sid
, SECCLASS_TUN_SOCKET
,
5620 TUN_SOCKET__ATTACH_QUEUE
, NULL
);
5623 static int selinux_tun_dev_attach(struct sock
*sk
, void *security
)
5625 struct tun_security_struct
*tunsec
= security
;
5626 struct sk_security_struct
*sksec
= selinux_sock(sk
);
5628 /* we don't currently perform any NetLabel based labeling here and it
5629 * isn't clear that we would want to do so anyway; while we could apply
5630 * labeling without the support of the TUN user the resulting labeled
5631 * traffic from the other end of the connection would almost certainly
5632 * cause confusion to the TUN user that had no idea network labeling
5633 * protocols were being used */
5635 sksec
->sid
= tunsec
->sid
;
5636 sksec
->sclass
= SECCLASS_TUN_SOCKET
;
5641 static int selinux_tun_dev_open(void *security
)
5643 struct tun_security_struct
*tunsec
= security
;
5644 u32 sid
= current_sid();
5647 err
= avc_has_perm(&selinux_state
,
5648 sid
, tunsec
->sid
, SECCLASS_TUN_SOCKET
,
5649 TUN_SOCKET__RELABELFROM
, NULL
);
5652 err
= avc_has_perm(&selinux_state
,
5653 sid
, sid
, SECCLASS_TUN_SOCKET
,
5654 TUN_SOCKET__RELABELTO
, NULL
);
5662 #ifdef CONFIG_NETFILTER
5664 static unsigned int selinux_ip_forward(struct sk_buff
*skb
,
5665 const struct net_device
*indev
,
5671 struct common_audit_data ad
;
5672 struct lsm_network_audit net
= {0,};
5677 if (!selinux_policycap_netpeer())
5680 secmark_active
= selinux_secmark_enabled();
5681 netlbl_active
= netlbl_enabled();
5682 peerlbl_active
= selinux_peerlbl_enabled();
5683 if (!secmark_active
&& !peerlbl_active
)
5686 if (selinux_skb_peerlbl_sid(skb
, family
, &peer_sid
) != 0)
5689 ad
.type
= LSM_AUDIT_DATA_NET
;
5691 ad
.u
.net
->netif
= indev
->ifindex
;
5692 ad
.u
.net
->family
= family
;
5693 if (selinux_parse_skb(skb
, &ad
, &addrp
, 1, NULL
) != 0)
5696 if (peerlbl_active
) {
5697 err
= selinux_inet_sys_rcv_skb(dev_net(indev
), indev
->ifindex
,
5698 addrp
, family
, peer_sid
, &ad
);
5700 selinux_netlbl_err(skb
, family
, err
, 1);
5706 if (avc_has_perm(&selinux_state
,
5707 peer_sid
, skb
->secmark
,
5708 SECCLASS_PACKET
, PACKET__FORWARD_IN
, &ad
))
5712 /* we do this in the FORWARD path and not the POST_ROUTING
5713 * path because we want to make sure we apply the necessary
5714 * labeling before IPsec is applied so we can leverage AH
5716 if (selinux_netlbl_skbuff_setsid(skb
, family
, peer_sid
) != 0)
5722 static unsigned int selinux_ipv4_forward(void *priv
,
5723 struct sk_buff
*skb
,
5724 const struct nf_hook_state
*state
)
5726 return selinux_ip_forward(skb
, state
->in
, PF_INET
);
5729 #if IS_ENABLED(CONFIG_IPV6)
5730 static unsigned int selinux_ipv6_forward(void *priv
,
5731 struct sk_buff
*skb
,
5732 const struct nf_hook_state
*state
)
5734 return selinux_ip_forward(skb
, state
->in
, PF_INET6
);
5738 static unsigned int selinux_ip_output(struct sk_buff
*skb
,
5744 if (!netlbl_enabled())
5747 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5748 * because we want to make sure we apply the necessary labeling
5749 * before IPsec is applied so we can leverage AH protection */
5752 struct sk_security_struct
*sksec
;
5754 if (sk_listener(sk
))
5755 /* if the socket is the listening state then this
5756 * packet is a SYN-ACK packet which means it needs to
5757 * be labeled based on the connection/request_sock and
5758 * not the parent socket. unfortunately, we can't
5759 * lookup the request_sock yet as it isn't queued on
5760 * the parent socket until after the SYN-ACK is sent.
5761 * the "solution" is to simply pass the packet as-is
5762 * as any IP option based labeling should be copied
5763 * from the initial connection request (in the IP
5764 * layer). it is far from ideal, but until we get a
5765 * security label in the packet itself this is the
5766 * best we can do. */
5769 /* standard practice, label using the parent socket */
5770 sksec
= selinux_sock(sk
);
5773 sid
= SECINITSID_KERNEL
;
5774 if (selinux_netlbl_skbuff_setsid(skb
, family
, sid
) != 0)
5780 static unsigned int selinux_ipv4_output(void *priv
,
5781 struct sk_buff
*skb
,
5782 const struct nf_hook_state
*state
)
5784 return selinux_ip_output(skb
, PF_INET
);
5787 #if IS_ENABLED(CONFIG_IPV6)
5788 static unsigned int selinux_ipv6_output(void *priv
,
5789 struct sk_buff
*skb
,
5790 const struct nf_hook_state
*state
)
5792 return selinux_ip_output(skb
, PF_INET6
);
5796 static unsigned int selinux_ip_postroute_compat(struct sk_buff
*skb
,
5800 struct sock
*sk
= skb_to_full_sk(skb
);
5801 struct sk_security_struct
*sksec
;
5802 struct common_audit_data ad
;
5803 struct lsm_network_audit net
= {0,};
5809 sksec
= selinux_sock(sk
);
5811 ad
.type
= LSM_AUDIT_DATA_NET
;
5813 ad
.u
.net
->netif
= ifindex
;
5814 ad
.u
.net
->family
= family
;
5815 if (selinux_parse_skb(skb
, &ad
, &addrp
, 0, &proto
))
5818 if (selinux_secmark_enabled())
5819 if (avc_has_perm(&selinux_state
,
5820 sksec
->sid
, skb
->secmark
,
5821 SECCLASS_PACKET
, PACKET__SEND
, &ad
))
5822 return NF_DROP_ERR(-ECONNREFUSED
);
5824 if (selinux_xfrm_postroute_last(sksec
->sid
, skb
, &ad
, proto
))
5825 return NF_DROP_ERR(-ECONNREFUSED
);
5830 static unsigned int selinux_ip_postroute(struct sk_buff
*skb
,
5831 const struct net_device
*outdev
,
5836 int ifindex
= outdev
->ifindex
;
5838 struct common_audit_data ad
;
5839 struct lsm_network_audit net
= {0,};
5844 /* If any sort of compatibility mode is enabled then handoff processing
5845 * to the selinux_ip_postroute_compat() function to deal with the
5846 * special handling. We do this in an attempt to keep this function
5847 * as fast and as clean as possible. */
5848 if (!selinux_policycap_netpeer())
5849 return selinux_ip_postroute_compat(skb
, ifindex
, family
);
5851 secmark_active
= selinux_secmark_enabled();
5852 peerlbl_active
= selinux_peerlbl_enabled();
5853 if (!secmark_active
&& !peerlbl_active
)
5856 sk
= skb_to_full_sk(skb
);
5859 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5860 * packet transformation so allow the packet to pass without any checks
5861 * since we'll have another chance to perform access control checks
5862 * when the packet is on it's final way out.
5863 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
5864 * is NULL, in this case go ahead and apply access control.
5865 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5866 * TCP listening state we cannot wait until the XFRM processing
5867 * is done as we will miss out on the SA label if we do;
5868 * unfortunately, this means more work, but it is only once per
5870 if (skb_dst(skb
) != NULL
&& skb_dst(skb
)->xfrm
!= NULL
&&
5871 !(sk
&& sk_listener(sk
)))
5876 /* Without an associated socket the packet is either coming
5877 * from the kernel or it is being forwarded; check the packet
5878 * to determine which and if the packet is being forwarded
5879 * query the packet directly to determine the security label. */
5881 secmark_perm
= PACKET__FORWARD_OUT
;
5882 if (selinux_skb_peerlbl_sid(skb
, family
, &peer_sid
))
5885 secmark_perm
= PACKET__SEND
;
5886 peer_sid
= SECINITSID_KERNEL
;
5888 } else if (sk_listener(sk
)) {
5889 /* Locally generated packet but the associated socket is in the
5890 * listening state which means this is a SYN-ACK packet. In
5891 * this particular case the correct security label is assigned
5892 * to the connection/request_sock but unfortunately we can't
5893 * query the request_sock as it isn't queued on the parent
5894 * socket until after the SYN-ACK packet is sent; the only
5895 * viable choice is to regenerate the label like we do in
5896 * selinux_inet_conn_request(). See also selinux_ip_output()
5897 * for similar problems. */
5899 struct sk_security_struct
*sksec
;
5901 sksec
= selinux_sock(sk
);
5902 if (selinux_skb_peerlbl_sid(skb
, family
, &skb_sid
))
5904 /* At this point, if the returned skb peerlbl is SECSID_NULL
5905 * and the packet has been through at least one XFRM
5906 * transformation then we must be dealing with the "final"
5907 * form of labeled IPsec packet; since we've already applied
5908 * all of our access controls on this packet we can safely
5909 * pass the packet. */
5910 if (skb_sid
== SECSID_NULL
) {
5913 if (IPCB(skb
)->flags
& IPSKB_XFRM_TRANSFORMED
)
5917 if (IP6CB(skb
)->flags
& IP6SKB_XFRM_TRANSFORMED
)
5921 return NF_DROP_ERR(-ECONNREFUSED
);
5924 if (selinux_conn_sid(sksec
->sid
, skb_sid
, &peer_sid
))
5926 secmark_perm
= PACKET__SEND
;
5928 /* Locally generated packet, fetch the security label from the
5929 * associated socket. */
5930 struct sk_security_struct
*sksec
= selinux_sock(sk
);
5931 peer_sid
= sksec
->sid
;
5932 secmark_perm
= PACKET__SEND
;
5935 ad
.type
= LSM_AUDIT_DATA_NET
;
5937 ad
.u
.net
->netif
= ifindex
;
5938 ad
.u
.net
->family
= family
;
5939 if (selinux_parse_skb(skb
, &ad
, &addrp
, 0, NULL
))
5943 if (avc_has_perm(&selinux_state
,
5944 peer_sid
, skb
->secmark
,
5945 SECCLASS_PACKET
, secmark_perm
, &ad
))
5946 return NF_DROP_ERR(-ECONNREFUSED
);
5948 if (peerlbl_active
) {
5952 if (sel_netif_sid(dev_net(outdev
), ifindex
, &if_sid
))
5954 if (avc_has_perm(&selinux_state
,
5956 SECCLASS_NETIF
, NETIF__EGRESS
, &ad
))
5957 return NF_DROP_ERR(-ECONNREFUSED
);
5959 if (sel_netnode_sid(addrp
, family
, &node_sid
))
5961 if (avc_has_perm(&selinux_state
,
5963 SECCLASS_NODE
, NODE__SENDTO
, &ad
))
5964 return NF_DROP_ERR(-ECONNREFUSED
);
5970 static unsigned int selinux_ipv4_postroute(void *priv
,
5971 struct sk_buff
*skb
,
5972 const struct nf_hook_state
*state
)
5974 return selinux_ip_postroute(skb
, state
->out
, PF_INET
);
5977 #if IS_ENABLED(CONFIG_IPV6)
5978 static unsigned int selinux_ipv6_postroute(void *priv
,
5979 struct sk_buff
*skb
,
5980 const struct nf_hook_state
*state
)
5982 return selinux_ip_postroute(skb
, state
->out
, PF_INET6
);
5986 #endif /* CONFIG_NETFILTER */
5988 static int selinux_netlink_send(struct sock
*sk
, struct sk_buff
*skb
)
5991 unsigned int msg_len
;
5992 unsigned int data_len
= skb
->len
;
5993 unsigned char *data
= skb
->data
;
5994 struct nlmsghdr
*nlh
;
5995 struct sk_security_struct
*sksec
= selinux_sock(sk
);
5996 u16 sclass
= sksec
->sclass
;
5999 while (data_len
>= nlmsg_total_size(0)) {
6000 nlh
= (struct nlmsghdr
*)data
;
6002 /* NOTE: the nlmsg_len field isn't reliably set by some netlink
6003 * users which means we can't reject skb's with bogus
6004 * length fields; our solution is to follow what
6005 * netlink_rcv_skb() does and simply skip processing at
6006 * messages with length fields that are clearly junk
6008 if (nlh
->nlmsg_len
< NLMSG_HDRLEN
|| nlh
->nlmsg_len
> data_len
)
6011 rc
= selinux_nlmsg_lookup(sclass
, nlh
->nlmsg_type
, &perm
);
6013 rc
= sock_has_perm(sk
, perm
);
6016 } else if (rc
== -EINVAL
) {
6017 /* -EINVAL is a missing msg/perm mapping */
6018 pr_warn_ratelimited("SELinux: unrecognized netlink"
6019 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
6020 " pid=%d comm=%s\n",
6021 sk
->sk_protocol
, nlh
->nlmsg_type
,
6022 secclass_map
[sclass
- 1].name
,
6023 task_pid_nr(current
), current
->comm
);
6024 if (enforcing_enabled(&selinux_state
) &&
6025 !security_get_allow_unknown(&selinux_state
))
6028 } else if (rc
== -ENOENT
) {
6029 /* -ENOENT is a missing socket/class mapping, ignore */
6035 /* move to the next message after applying netlink padding */
6036 msg_len
= NLMSG_ALIGN(nlh
->nlmsg_len
);
6037 if (msg_len
>= data_len
)
6039 data_len
-= msg_len
;
6046 static void ipc_init_security(struct ipc_security_struct
*isec
, u16 sclass
)
6048 isec
->sclass
= sclass
;
6049 isec
->sid
= current_sid();
6052 static int ipc_has_perm(struct kern_ipc_perm
*ipc_perms
,
6055 struct ipc_security_struct
*isec
;
6056 struct common_audit_data ad
;
6057 u32 sid
= current_sid();
6059 isec
= selinux_ipc(ipc_perms
);
6061 ad
.type
= LSM_AUDIT_DATA_IPC
;
6062 ad
.u
.ipc_id
= ipc_perms
->key
;
6064 return avc_has_perm(&selinux_state
,
6065 sid
, isec
->sid
, isec
->sclass
, perms
, &ad
);
6068 static int selinux_msg_msg_alloc_security(struct msg_msg
*msg
)
6070 struct msg_security_struct
*msec
;
6072 msec
= selinux_msg_msg(msg
);
6073 msec
->sid
= SECINITSID_UNLABELED
;
6078 /* message queue security operations */
6079 static int selinux_msg_queue_alloc_security(struct kern_ipc_perm
*msq
)
6081 struct ipc_security_struct
*isec
;
6082 struct common_audit_data ad
;
6083 u32 sid
= current_sid();
6086 isec
= selinux_ipc(msq
);
6087 ipc_init_security(isec
, SECCLASS_MSGQ
);
6089 ad
.type
= LSM_AUDIT_DATA_IPC
;
6090 ad
.u
.ipc_id
= msq
->key
;
6092 rc
= avc_has_perm(&selinux_state
,
6093 sid
, isec
->sid
, SECCLASS_MSGQ
,
6098 static int selinux_msg_queue_associate(struct kern_ipc_perm
*msq
, int msqflg
)
6100 struct ipc_security_struct
*isec
;
6101 struct common_audit_data ad
;
6102 u32 sid
= current_sid();
6104 isec
= selinux_ipc(msq
);
6106 ad
.type
= LSM_AUDIT_DATA_IPC
;
6107 ad
.u
.ipc_id
= msq
->key
;
6109 return avc_has_perm(&selinux_state
,
6110 sid
, isec
->sid
, SECCLASS_MSGQ
,
6111 MSGQ__ASSOCIATE
, &ad
);
6114 static int selinux_msg_queue_msgctl(struct kern_ipc_perm
*msq
, int cmd
)
6122 /* No specific object, just general system-wide information. */
6123 return avc_has_perm(&selinux_state
,
6124 current_sid(), SECINITSID_KERNEL
,
6125 SECCLASS_SYSTEM
, SYSTEM__IPC_INFO
, NULL
);
6129 perms
= MSGQ__GETATTR
| MSGQ__ASSOCIATE
;
6132 perms
= MSGQ__SETATTR
;
6135 perms
= MSGQ__DESTROY
;
6141 err
= ipc_has_perm(msq
, perms
);
6145 static int selinux_msg_queue_msgsnd(struct kern_ipc_perm
*msq
, struct msg_msg
*msg
, int msqflg
)
6147 struct ipc_security_struct
*isec
;
6148 struct msg_security_struct
*msec
;
6149 struct common_audit_data ad
;
6150 u32 sid
= current_sid();
6153 isec
= selinux_ipc(msq
);
6154 msec
= selinux_msg_msg(msg
);
6157 * First time through, need to assign label to the message
6159 if (msec
->sid
== SECINITSID_UNLABELED
) {
6161 * Compute new sid based on current process and
6162 * message queue this message will be stored in
6164 rc
= security_transition_sid(&selinux_state
, sid
, isec
->sid
,
6165 SECCLASS_MSG
, NULL
, &msec
->sid
);
6170 ad
.type
= LSM_AUDIT_DATA_IPC
;
6171 ad
.u
.ipc_id
= msq
->key
;
6173 /* Can this process write to the queue? */
6174 rc
= avc_has_perm(&selinux_state
,
6175 sid
, isec
->sid
, SECCLASS_MSGQ
,
6178 /* Can this process send the message */
6179 rc
= avc_has_perm(&selinux_state
,
6180 sid
, msec
->sid
, SECCLASS_MSG
,
6183 /* Can the message be put in the queue? */
6184 rc
= avc_has_perm(&selinux_state
,
6185 msec
->sid
, isec
->sid
, SECCLASS_MSGQ
,
6186 MSGQ__ENQUEUE
, &ad
);
6191 static int selinux_msg_queue_msgrcv(struct kern_ipc_perm
*msq
, struct msg_msg
*msg
,
6192 struct task_struct
*target
,
6193 long type
, int mode
)
6195 struct ipc_security_struct
*isec
;
6196 struct msg_security_struct
*msec
;
6197 struct common_audit_data ad
;
6198 u32 sid
= task_sid_obj(target
);
6201 isec
= selinux_ipc(msq
);
6202 msec
= selinux_msg_msg(msg
);
6204 ad
.type
= LSM_AUDIT_DATA_IPC
;
6205 ad
.u
.ipc_id
= msq
->key
;
6207 rc
= avc_has_perm(&selinux_state
,
6209 SECCLASS_MSGQ
, MSGQ__READ
, &ad
);
6211 rc
= avc_has_perm(&selinux_state
,
6213 SECCLASS_MSG
, MSG__RECEIVE
, &ad
);
6217 /* Shared Memory security operations */
6218 static int selinux_shm_alloc_security(struct kern_ipc_perm
*shp
)
6220 struct ipc_security_struct
*isec
;
6221 struct common_audit_data ad
;
6222 u32 sid
= current_sid();
6225 isec
= selinux_ipc(shp
);
6226 ipc_init_security(isec
, SECCLASS_SHM
);
6228 ad
.type
= LSM_AUDIT_DATA_IPC
;
6229 ad
.u
.ipc_id
= shp
->key
;
6231 rc
= avc_has_perm(&selinux_state
,
6232 sid
, isec
->sid
, SECCLASS_SHM
,
6237 static int selinux_shm_associate(struct kern_ipc_perm
*shp
, int shmflg
)
6239 struct ipc_security_struct
*isec
;
6240 struct common_audit_data ad
;
6241 u32 sid
= current_sid();
6243 isec
= selinux_ipc(shp
);
6245 ad
.type
= LSM_AUDIT_DATA_IPC
;
6246 ad
.u
.ipc_id
= shp
->key
;
6248 return avc_has_perm(&selinux_state
,
6249 sid
, isec
->sid
, SECCLASS_SHM
,
6250 SHM__ASSOCIATE
, &ad
);
6253 /* Note, at this point, shp is locked down */
6254 static int selinux_shm_shmctl(struct kern_ipc_perm
*shp
, int cmd
)
6262 /* No specific object, just general system-wide information. */
6263 return avc_has_perm(&selinux_state
,
6264 current_sid(), SECINITSID_KERNEL
,
6265 SECCLASS_SYSTEM
, SYSTEM__IPC_INFO
, NULL
);
6269 perms
= SHM__GETATTR
| SHM__ASSOCIATE
;
6272 perms
= SHM__SETATTR
;
6279 perms
= SHM__DESTROY
;
6285 err
= ipc_has_perm(shp
, perms
);
6289 static int selinux_shm_shmat(struct kern_ipc_perm
*shp
,
6290 char __user
*shmaddr
, int shmflg
)
6294 if (shmflg
& SHM_RDONLY
)
6297 perms
= SHM__READ
| SHM__WRITE
;
6299 return ipc_has_perm(shp
, perms
);
6302 /* Semaphore security operations */
6303 static int selinux_sem_alloc_security(struct kern_ipc_perm
*sma
)
6305 struct ipc_security_struct
*isec
;
6306 struct common_audit_data ad
;
6307 u32 sid
= current_sid();
6310 isec
= selinux_ipc(sma
);
6311 ipc_init_security(isec
, SECCLASS_SEM
);
6313 ad
.type
= LSM_AUDIT_DATA_IPC
;
6314 ad
.u
.ipc_id
= sma
->key
;
6316 rc
= avc_has_perm(&selinux_state
,
6317 sid
, isec
->sid
, SECCLASS_SEM
,
6322 static int selinux_sem_associate(struct kern_ipc_perm
*sma
, int semflg
)
6324 struct ipc_security_struct
*isec
;
6325 struct common_audit_data ad
;
6326 u32 sid
= current_sid();
6328 isec
= selinux_ipc(sma
);
6330 ad
.type
= LSM_AUDIT_DATA_IPC
;
6331 ad
.u
.ipc_id
= sma
->key
;
6333 return avc_has_perm(&selinux_state
,
6334 sid
, isec
->sid
, SECCLASS_SEM
,
6335 SEM__ASSOCIATE
, &ad
);
6338 /* Note, at this point, sma is locked down */
6339 static int selinux_sem_semctl(struct kern_ipc_perm
*sma
, int cmd
)
6347 /* No specific object, just general system-wide information. */
6348 return avc_has_perm(&selinux_state
,
6349 current_sid(), SECINITSID_KERNEL
,
6350 SECCLASS_SYSTEM
, SYSTEM__IPC_INFO
, NULL
);
6354 perms
= SEM__GETATTR
;
6365 perms
= SEM__DESTROY
;
6368 perms
= SEM__SETATTR
;
6373 perms
= SEM__GETATTR
| SEM__ASSOCIATE
;
6379 err
= ipc_has_perm(sma
, perms
);
6383 static int selinux_sem_semop(struct kern_ipc_perm
*sma
,
6384 struct sembuf
*sops
, unsigned nsops
, int alter
)
6389 perms
= SEM__READ
| SEM__WRITE
;
6393 return ipc_has_perm(sma
, perms
);
6396 static int selinux_ipc_permission(struct kern_ipc_perm
*ipcp
, short flag
)
6402 av
|= IPC__UNIX_READ
;
6404 av
|= IPC__UNIX_WRITE
;
6409 return ipc_has_perm(ipcp
, av
);
6412 static void selinux_ipc_getsecid(struct kern_ipc_perm
*ipcp
, u32
*secid
)
6414 struct ipc_security_struct
*isec
= selinux_ipc(ipcp
);
6418 static void selinux_d_instantiate(struct dentry
*dentry
, struct inode
*inode
)
6421 inode_doinit_with_dentry(inode
, dentry
);
6424 static int selinux_getprocattr(struct task_struct
*p
,
6425 char *name
, char **value
)
6427 const struct task_security_struct
*__tsec
;
6433 __tsec
= selinux_cred(__task_cred(p
));
6436 error
= avc_has_perm(&selinux_state
,
6437 current_sid(), __tsec
->sid
,
6438 SECCLASS_PROCESS
, PROCESS__GETATTR
, NULL
);
6443 if (!strcmp(name
, "current") || !strcmp(name
, "context"))
6445 else if (!strcmp(name
, "prev"))
6447 else if (!strcmp(name
, "exec"))
6448 sid
= __tsec
->exec_sid
;
6449 else if (!strcmp(name
, "fscreate"))
6450 sid
= __tsec
->create_sid
;
6451 else if (!strcmp(name
, "keycreate"))
6452 sid
= __tsec
->keycreate_sid
;
6453 else if (!strcmp(name
, "sockcreate"))
6454 sid
= __tsec
->sockcreate_sid
;
6464 error
= security_sid_to_context(&selinux_state
, sid
, value
, &len
);
6474 static int selinux_setprocattr(const char *name
, void *value
, size_t size
)
6476 struct task_security_struct
*tsec
;
6478 u32 mysid
= current_sid(), sid
= 0, ptsid
;
6483 * Basic control over ability to set these attributes at all.
6487 * For setting display, we only perform a permission check;
6488 * the actual update to the display value is handled by the
6491 if (!strcmp(name
, "display"))
6492 return avc_has_perm(&selinux_state
,
6493 mysid
, mysid
, SECCLASS_PROCESS2
,
6494 PROCESS2__SETDISPLAY
, NULL
);
6496 if (!strcmp(name
, "exec"))
6497 error
= avc_has_perm(&selinux_state
,
6498 mysid
, mysid
, SECCLASS_PROCESS
,
6499 PROCESS__SETEXEC
, NULL
);
6500 else if (!strcmp(name
, "fscreate"))
6501 error
= avc_has_perm(&selinux_state
,
6502 mysid
, mysid
, SECCLASS_PROCESS
,
6503 PROCESS__SETFSCREATE
, NULL
);
6504 else if (!strcmp(name
, "keycreate"))
6505 error
= avc_has_perm(&selinux_state
,
6506 mysid
, mysid
, SECCLASS_PROCESS
,
6507 PROCESS__SETKEYCREATE
, NULL
);
6508 else if (!strcmp(name
, "sockcreate"))
6509 error
= avc_has_perm(&selinux_state
,
6510 mysid
, mysid
, SECCLASS_PROCESS
,
6511 PROCESS__SETSOCKCREATE
, NULL
);
6512 else if (!strcmp(name
, "current"))
6513 error
= avc_has_perm(&selinux_state
,
6514 mysid
, mysid
, SECCLASS_PROCESS
,
6515 PROCESS__SETCURRENT
, NULL
);
6521 /* Obtain a SID for the context, if one was specified. */
6522 if (size
&& str
[0] && str
[0] != '\n') {
6523 if (str
[size
-1] == '\n') {
6527 error
= security_context_to_sid(&selinux_state
, value
, size
,
6529 if (error
== -EINVAL
&& !strcmp(name
, "fscreate")) {
6530 if (!has_cap_mac_admin(true)) {
6531 struct audit_buffer
*ab
;
6534 /* We strip a nul only if it is at the end, otherwise the
6535 * context contains a nul and we should audit that */
6536 if (str
[size
- 1] == '\0')
6537 audit_size
= size
- 1;
6540 ab
= audit_log_start(audit_context(),
6545 audit_log_format(ab
, "op=fscreate invalid_context=");
6546 audit_log_n_untrustedstring(ab
, value
, audit_size
);
6551 error
= security_context_to_sid_force(
6559 new = prepare_creds();
6563 /* Permission checking based on the specified context is
6564 performed during the actual operation (execve,
6565 open/mkdir/...), when we know the full context of the
6566 operation. See selinux_bprm_creds_for_exec for the execve
6567 checks and may_create for the file creation checks. The
6568 operation will then fail if the context is not permitted. */
6569 tsec
= selinux_cred(new);
6570 if (!strcmp(name
, "exec")) {
6571 tsec
->exec_sid
= sid
;
6572 } else if (!strcmp(name
, "fscreate")) {
6573 tsec
->create_sid
= sid
;
6574 } else if (!strcmp(name
, "keycreate")) {
6576 error
= avc_has_perm(&selinux_state
, mysid
, sid
,
6577 SECCLASS_KEY
, KEY__CREATE
, NULL
);
6581 tsec
->keycreate_sid
= sid
;
6582 } else if (!strcmp(name
, "sockcreate")) {
6583 tsec
->sockcreate_sid
= sid
;
6584 } else if (!strcmp(name
, "current")) {
6589 /* Only allow single threaded processes to change context */
6591 if (!current_is_single_threaded()) {
6592 error
= security_bounded_transition(&selinux_state
,
6598 /* Check permissions for the transition. */
6599 error
= avc_has_perm(&selinux_state
,
6600 tsec
->sid
, sid
, SECCLASS_PROCESS
,
6601 PROCESS__DYNTRANSITION
, NULL
);
6605 /* Check for ptracing, and update the task SID if ok.
6606 Otherwise, leave SID unchanged and fail. */
6607 ptsid
= ptrace_parent_sid();
6609 error
= avc_has_perm(&selinux_state
,
6610 ptsid
, sid
, SECCLASS_PROCESS
,
6611 PROCESS__PTRACE
, NULL
);
6630 static int selinux_ismaclabel(const char *name
)
6632 return (strcmp(name
, XATTR_SELINUX_SUFFIX
) == 0);
6635 static int selinux_secid_to_secctx(u32 secid
, char **secdata
, u32
*seclen
)
6637 return security_sid_to_context(&selinux_state
, secid
,
6641 static int selinux_secctx_to_secid(const char *secdata
, u32 seclen
, u32
*secid
)
6643 return security_context_to_sid(&selinux_state
, secdata
, seclen
,
6647 static void selinux_release_secctx(char *secdata
, u32 seclen
)
6652 static void selinux_inode_invalidate_secctx(struct inode
*inode
)
6654 struct inode_security_struct
*isec
= selinux_inode(inode
);
6656 spin_lock(&isec
->lock
);
6657 isec
->initialized
= LABEL_INVALID
;
6658 spin_unlock(&isec
->lock
);
6662 * called with inode->i_mutex locked
6664 static int selinux_inode_notifysecctx(struct inode
*inode
, void *ctx
, u32 ctxlen
)
6666 int rc
= selinux_inode_setsecurity(inode
, XATTR_SELINUX_SUFFIX
,
6668 /* Do not return error when suppressing label (SBLABEL_MNT not set). */
6669 return rc
== -EOPNOTSUPP
? 0 : rc
;
6673 * called with inode->i_mutex locked
6675 static int selinux_inode_setsecctx(struct dentry
*dentry
, void *ctx
, u32 ctxlen
)
6677 return __vfs_setxattr_noperm(&init_user_ns
, dentry
, XATTR_NAME_SELINUX
,
6681 static int selinux_inode_getsecctx(struct inode
*inode
, void **ctx
, u32
*ctxlen
)
6684 len
= selinux_inode_getsecurity(&init_user_ns
, inode
,
6685 XATTR_SELINUX_SUFFIX
, ctx
, true);
6693 static int selinux_key_alloc(struct key
*k
, const struct cred
*cred
,
6694 unsigned long flags
)
6696 const struct task_security_struct
*tsec
;
6697 struct key_security_struct
*ksec
;
6699 ksec
= kzalloc(sizeof(struct key_security_struct
), GFP_KERNEL
);
6703 tsec
= selinux_cred(cred
);
6704 if (tsec
->keycreate_sid
)
6705 ksec
->sid
= tsec
->keycreate_sid
;
6707 ksec
->sid
= tsec
->sid
;
6713 static void selinux_key_free(struct key
*k
)
6715 struct key_security_struct
*ksec
= k
->security
;
6721 static int selinux_key_permission(key_ref_t key_ref
,
6722 const struct cred
*cred
,
6723 enum key_need_perm need_perm
)
6726 struct key_security_struct
*ksec
;
6729 switch (need_perm
) {
6736 case KEY_NEED_WRITE
:
6739 case KEY_NEED_SEARCH
:
6745 case KEY_NEED_SETATTR
:
6746 perm
= KEY__SETATTR
;
6748 case KEY_NEED_UNLINK
:
6749 case KEY_SYSADMIN_OVERRIDE
:
6750 case KEY_AUTHTOKEN_OVERRIDE
:
6751 case KEY_DEFER_PERM_CHECK
:
6759 sid
= cred_sid(cred
);
6760 key
= key_ref_to_ptr(key_ref
);
6761 ksec
= key
->security
;
6763 return avc_has_perm(&selinux_state
,
6764 sid
, ksec
->sid
, SECCLASS_KEY
, perm
, NULL
);
6767 static int selinux_key_getsecurity(struct key
*key
, char **_buffer
)
6769 struct key_security_struct
*ksec
= key
->security
;
6770 char *context
= NULL
;
6774 rc
= security_sid_to_context(&selinux_state
, ksec
->sid
,
6782 #ifdef CONFIG_KEY_NOTIFICATIONS
6783 static int selinux_watch_key(struct key
*key
)
6785 struct key_security_struct
*ksec
= key
->security
;
6786 u32 sid
= current_sid();
6788 return avc_has_perm(&selinux_state
,
6789 sid
, ksec
->sid
, SECCLASS_KEY
, KEY__VIEW
, NULL
);
6794 #ifdef CONFIG_SECURITY_INFINIBAND
6795 static int selinux_ib_pkey_access(void *ib_sec
, u64 subnet_prefix
, u16 pkey_val
)
6797 struct common_audit_data ad
;
6800 struct ib_security_struct
*sec
= ib_sec
;
6801 struct lsm_ibpkey_audit ibpkey
;
6803 err
= sel_ib_pkey_sid(subnet_prefix
, pkey_val
, &sid
);
6807 ad
.type
= LSM_AUDIT_DATA_IBPKEY
;
6808 ibpkey
.subnet_prefix
= subnet_prefix
;
6809 ibpkey
.pkey
= pkey_val
;
6810 ad
.u
.ibpkey
= &ibpkey
;
6811 return avc_has_perm(&selinux_state
,
6813 SECCLASS_INFINIBAND_PKEY
,
6814 INFINIBAND_PKEY__ACCESS
, &ad
);
6817 static int selinux_ib_endport_manage_subnet(void *ib_sec
, const char *dev_name
,
6820 struct common_audit_data ad
;
6823 struct ib_security_struct
*sec
= ib_sec
;
6824 struct lsm_ibendport_audit ibendport
;
6826 err
= security_ib_endport_sid(&selinux_state
, dev_name
, port_num
,
6832 ad
.type
= LSM_AUDIT_DATA_IBENDPORT
;
6833 ibendport
.dev_name
= dev_name
;
6834 ibendport
.port
= port_num
;
6835 ad
.u
.ibendport
= &ibendport
;
6836 return avc_has_perm(&selinux_state
,
6838 SECCLASS_INFINIBAND_ENDPORT
,
6839 INFINIBAND_ENDPORT__MANAGE_SUBNET
, &ad
);
6842 static int selinux_ib_alloc_security(void **ib_sec
)
6844 struct ib_security_struct
*sec
;
6846 sec
= kzalloc(sizeof(*sec
), GFP_KERNEL
);
6849 sec
->sid
= current_sid();
6855 static void selinux_ib_free_security(void *ib_sec
)
6861 #ifdef CONFIG_BPF_SYSCALL
6862 static int selinux_bpf(int cmd
, union bpf_attr
*attr
,
6865 u32 sid
= current_sid();
6869 case BPF_MAP_CREATE
:
6870 ret
= avc_has_perm(&selinux_state
,
6871 sid
, sid
, SECCLASS_BPF
, BPF__MAP_CREATE
,
6875 ret
= avc_has_perm(&selinux_state
,
6876 sid
, sid
, SECCLASS_BPF
, BPF__PROG_LOAD
,
6887 static u32
bpf_map_fmode_to_av(fmode_t fmode
)
6891 if (fmode
& FMODE_READ
)
6892 av
|= BPF__MAP_READ
;
6893 if (fmode
& FMODE_WRITE
)
6894 av
|= BPF__MAP_WRITE
;
6898 /* This function will check the file pass through unix socket or binder to see
6899 * if it is a bpf related object. And apply correspinding checks on the bpf
6900 * object based on the type. The bpf maps and programs, not like other files and
6901 * socket, are using a shared anonymous inode inside the kernel as their inode.
6902 * So checking that inode cannot identify if the process have privilege to
6903 * access the bpf object and that's why we have to add this additional check in
6904 * selinux_file_receive and selinux_binder_transfer_files.
6906 static int bpf_fd_pass(struct file
*file
, u32 sid
)
6908 struct bpf_security_struct
*bpfsec
;
6909 struct bpf_prog
*prog
;
6910 struct bpf_map
*map
;
6913 if (file
->f_op
== &bpf_map_fops
) {
6914 map
= file
->private_data
;
6915 bpfsec
= map
->security
;
6916 ret
= avc_has_perm(&selinux_state
,
6917 sid
, bpfsec
->sid
, SECCLASS_BPF
,
6918 bpf_map_fmode_to_av(file
->f_mode
), NULL
);
6921 } else if (file
->f_op
== &bpf_prog_fops
) {
6922 prog
= file
->private_data
;
6923 bpfsec
= prog
->aux
->security
;
6924 ret
= avc_has_perm(&selinux_state
,
6925 sid
, bpfsec
->sid
, SECCLASS_BPF
,
6926 BPF__PROG_RUN
, NULL
);
6933 static int selinux_bpf_map(struct bpf_map
*map
, fmode_t fmode
)
6935 u32 sid
= current_sid();
6936 struct bpf_security_struct
*bpfsec
;
6938 bpfsec
= map
->security
;
6939 return avc_has_perm(&selinux_state
,
6940 sid
, bpfsec
->sid
, SECCLASS_BPF
,
6941 bpf_map_fmode_to_av(fmode
), NULL
);
6944 static int selinux_bpf_prog(struct bpf_prog
*prog
)
6946 u32 sid
= current_sid();
6947 struct bpf_security_struct
*bpfsec
;
6949 bpfsec
= prog
->aux
->security
;
6950 return avc_has_perm(&selinux_state
,
6951 sid
, bpfsec
->sid
, SECCLASS_BPF
,
6952 BPF__PROG_RUN
, NULL
);
6955 static int selinux_bpf_map_alloc(struct bpf_map
*map
)
6957 struct bpf_security_struct
*bpfsec
;
6959 bpfsec
= kzalloc(sizeof(*bpfsec
), GFP_KERNEL
);
6963 bpfsec
->sid
= current_sid();
6964 map
->security
= bpfsec
;
6969 static void selinux_bpf_map_free(struct bpf_map
*map
)
6971 struct bpf_security_struct
*bpfsec
= map
->security
;
6973 map
->security
= NULL
;
6977 static int selinux_bpf_prog_alloc(struct bpf_prog_aux
*aux
)
6979 struct bpf_security_struct
*bpfsec
;
6981 bpfsec
= kzalloc(sizeof(*bpfsec
), GFP_KERNEL
);
6985 bpfsec
->sid
= current_sid();
6986 aux
->security
= bpfsec
;
6991 static void selinux_bpf_prog_free(struct bpf_prog_aux
*aux
)
6993 struct bpf_security_struct
*bpfsec
= aux
->security
;
6995 aux
->security
= NULL
;
7000 static int selinux_lockdown(enum lockdown_reason what
)
7002 struct common_audit_data ad
;
7003 u32 sid
= current_sid();
7004 int invalid_reason
= (what
<= LOCKDOWN_NONE
) ||
7005 (what
== LOCKDOWN_INTEGRITY_MAX
) ||
7006 (what
>= LOCKDOWN_CONFIDENTIALITY_MAX
);
7008 if (WARN(invalid_reason
, "Invalid lockdown reason")) {
7009 audit_log(audit_context(),
7010 GFP_ATOMIC
, AUDIT_SELINUX_ERR
,
7011 "lockdown_reason=invalid");
7015 ad
.type
= LSM_AUDIT_DATA_LOCKDOWN
;
7018 if (what
<= LOCKDOWN_INTEGRITY_MAX
)
7019 return avc_has_perm(&selinux_state
,
7020 sid
, sid
, SECCLASS_LOCKDOWN
,
7021 LOCKDOWN__INTEGRITY
, &ad
);
7023 return avc_has_perm(&selinux_state
,
7024 sid
, sid
, SECCLASS_LOCKDOWN
,
7025 LOCKDOWN__CONFIDENTIALITY
, &ad
);
7028 struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init
= {
7029 .lbs_cred
= sizeof(struct task_security_struct
),
7030 .lbs_file
= sizeof(struct file_security_struct
),
7031 .lbs_inode
= sizeof(struct inode_security_struct
),
7032 .lbs_ipc
= sizeof(struct ipc_security_struct
),
7033 .lbs_msg_msg
= sizeof(struct msg_security_struct
),
7034 .lbs_superblock
= sizeof(struct superblock_security_struct
),
7035 .lbs_sock
= sizeof(struct sk_security_struct
),
7038 #ifdef CONFIG_PERF_EVENTS
7039 static int selinux_perf_event_open(struct perf_event_attr
*attr
, int type
)
7041 u32 requested
, sid
= current_sid();
7043 if (type
== PERF_SECURITY_OPEN
)
7044 requested
= PERF_EVENT__OPEN
;
7045 else if (type
== PERF_SECURITY_CPU
)
7046 requested
= PERF_EVENT__CPU
;
7047 else if (type
== PERF_SECURITY_KERNEL
)
7048 requested
= PERF_EVENT__KERNEL
;
7049 else if (type
== PERF_SECURITY_TRACEPOINT
)
7050 requested
= PERF_EVENT__TRACEPOINT
;
7054 return avc_has_perm(&selinux_state
, sid
, sid
, SECCLASS_PERF_EVENT
,
7058 static int selinux_perf_event_alloc(struct perf_event
*event
)
7060 struct perf_event_security_struct
*perfsec
;
7062 perfsec
= kzalloc(sizeof(*perfsec
), GFP_KERNEL
);
7066 perfsec
->sid
= current_sid();
7067 event
->security
= perfsec
;
7072 static void selinux_perf_event_free(struct perf_event
*event
)
7074 struct perf_event_security_struct
*perfsec
= event
->security
;
7076 event
->security
= NULL
;
7080 static int selinux_perf_event_read(struct perf_event
*event
)
7082 struct perf_event_security_struct
*perfsec
= event
->security
;
7083 u32 sid
= current_sid();
7085 return avc_has_perm(&selinux_state
, sid
, perfsec
->sid
,
7086 SECCLASS_PERF_EVENT
, PERF_EVENT__READ
, NULL
);
7089 static int selinux_perf_event_write(struct perf_event
*event
)
7091 struct perf_event_security_struct
*perfsec
= event
->security
;
7092 u32 sid
= current_sid();
7094 return avc_has_perm(&selinux_state
, sid
, perfsec
->sid
,
7095 SECCLASS_PERF_EVENT
, PERF_EVENT__WRITE
, NULL
);
7099 struct lsm_id selinux_lsmid __lsm_ro_after_init
= {
7101 .slot
= LSMBLOB_NEEDED
7105 * IMPORTANT NOTE: When adding new hooks, please be careful to keep this order:
7106 * 1. any hooks that don't belong to (2.) or (3.) below,
7107 * 2. hooks that both access structures allocated by other hooks, and allocate
7108 * structures that can be later accessed by other hooks (mostly "cloning"
7110 * 3. hooks that only allocate structures that can be later accessed by other
7111 * hooks ("allocating" hooks).
7113 * Please follow block comment delimiters in the list to keep this order.
7115 * This ordering is needed for SELinux runtime disable to work at least somewhat
7116 * safely. Breaking the ordering rules above might lead to NULL pointer derefs
7117 * when disabling SELinux at runtime.
7119 static struct security_hook_list selinux_hooks
[] __lsm_ro_after_init
= {
7120 LSM_HOOK_INIT(binder_set_context_mgr
, selinux_binder_set_context_mgr
),
7121 LSM_HOOK_INIT(binder_transaction
, selinux_binder_transaction
),
7122 LSM_HOOK_INIT(binder_transfer_binder
, selinux_binder_transfer_binder
),
7123 LSM_HOOK_INIT(binder_transfer_file
, selinux_binder_transfer_file
),
7125 LSM_HOOK_INIT(ptrace_access_check
, selinux_ptrace_access_check
),
7126 LSM_HOOK_INIT(ptrace_traceme
, selinux_ptrace_traceme
),
7127 LSM_HOOK_INIT(capget
, selinux_capget
),
7128 LSM_HOOK_INIT(capset
, selinux_capset
),
7129 LSM_HOOK_INIT(capable
, selinux_capable
),
7130 LSM_HOOK_INIT(quotactl
, selinux_quotactl
),
7131 LSM_HOOK_INIT(quota_on
, selinux_quota_on
),
7132 LSM_HOOK_INIT(syslog
, selinux_syslog
),
7133 LSM_HOOK_INIT(vm_enough_memory
, selinux_vm_enough_memory
),
7135 LSM_HOOK_INIT(netlink_send
, selinux_netlink_send
),
7137 LSM_HOOK_INIT(bprm_creds_for_exec
, selinux_bprm_creds_for_exec
),
7138 LSM_HOOK_INIT(bprm_committing_creds
, selinux_bprm_committing_creds
),
7139 LSM_HOOK_INIT(bprm_committed_creds
, selinux_bprm_committed_creds
),
7141 LSM_HOOK_INIT(sb_free_mnt_opts
, selinux_free_mnt_opts
),
7142 LSM_HOOK_INIT(sb_mnt_opts_compat
, selinux_sb_mnt_opts_compat
),
7143 LSM_HOOK_INIT(sb_remount
, selinux_sb_remount
),
7144 LSM_HOOK_INIT(sb_kern_mount
, selinux_sb_kern_mount
),
7145 LSM_HOOK_INIT(sb_show_options
, selinux_sb_show_options
),
7146 LSM_HOOK_INIT(sb_statfs
, selinux_sb_statfs
),
7147 LSM_HOOK_INIT(sb_mount
, selinux_mount
),
7148 LSM_HOOK_INIT(sb_umount
, selinux_umount
),
7149 LSM_HOOK_INIT(sb_set_mnt_opts
, selinux_set_mnt_opts
),
7150 LSM_HOOK_INIT(sb_clone_mnt_opts
, selinux_sb_clone_mnt_opts
),
7152 LSM_HOOK_INIT(move_mount
, selinux_move_mount
),
7154 LSM_HOOK_INIT(dentry_init_security
, selinux_dentry_init_security
),
7155 LSM_HOOK_INIT(dentry_create_files_as
, selinux_dentry_create_files_as
),
7157 LSM_HOOK_INIT(inode_free_security
, selinux_inode_free_security
),
7158 LSM_HOOK_INIT(inode_init_security
, selinux_inode_init_security
),
7159 LSM_HOOK_INIT(inode_init_security_anon
, selinux_inode_init_security_anon
),
7160 LSM_HOOK_INIT(inode_create
, selinux_inode_create
),
7161 LSM_HOOK_INIT(inode_link
, selinux_inode_link
),
7162 LSM_HOOK_INIT(inode_unlink
, selinux_inode_unlink
),
7163 LSM_HOOK_INIT(inode_symlink
, selinux_inode_symlink
),
7164 LSM_HOOK_INIT(inode_mkdir
, selinux_inode_mkdir
),
7165 LSM_HOOK_INIT(inode_rmdir
, selinux_inode_rmdir
),
7166 LSM_HOOK_INIT(inode_mknod
, selinux_inode_mknod
),
7167 LSM_HOOK_INIT(inode_rename
, selinux_inode_rename
),
7168 LSM_HOOK_INIT(inode_readlink
, selinux_inode_readlink
),
7169 LSM_HOOK_INIT(inode_follow_link
, selinux_inode_follow_link
),
7170 LSM_HOOK_INIT(inode_permission
, selinux_inode_permission
),
7171 LSM_HOOK_INIT(inode_setattr
, selinux_inode_setattr
),
7172 LSM_HOOK_INIT(inode_getattr
, selinux_inode_getattr
),
7173 LSM_HOOK_INIT(inode_setxattr
, selinux_inode_setxattr
),
7174 LSM_HOOK_INIT(inode_post_setxattr
, selinux_inode_post_setxattr
),
7175 LSM_HOOK_INIT(inode_getxattr
, selinux_inode_getxattr
),
7176 LSM_HOOK_INIT(inode_listxattr
, selinux_inode_listxattr
),
7177 LSM_HOOK_INIT(inode_removexattr
, selinux_inode_removexattr
),
7178 LSM_HOOK_INIT(inode_getsecurity
, selinux_inode_getsecurity
),
7179 LSM_HOOK_INIT(inode_setsecurity
, selinux_inode_setsecurity
),
7180 LSM_HOOK_INIT(inode_listsecurity
, selinux_inode_listsecurity
),
7181 LSM_HOOK_INIT(inode_getsecid
, selinux_inode_getsecid
),
7182 LSM_HOOK_INIT(inode_copy_up
, selinux_inode_copy_up
),
7183 LSM_HOOK_INIT(inode_copy_up_xattr
, selinux_inode_copy_up_xattr
),
7184 LSM_HOOK_INIT(path_notify
, selinux_path_notify
),
7186 LSM_HOOK_INIT(kernfs_init_security
, selinux_kernfs_init_security
),
7188 LSM_HOOK_INIT(file_permission
, selinux_file_permission
),
7189 LSM_HOOK_INIT(file_alloc_security
, selinux_file_alloc_security
),
7190 LSM_HOOK_INIT(file_ioctl
, selinux_file_ioctl
),
7191 LSM_HOOK_INIT(mmap_file
, selinux_mmap_file
),
7192 LSM_HOOK_INIT(mmap_addr
, selinux_mmap_addr
),
7193 LSM_HOOK_INIT(file_mprotect
, selinux_file_mprotect
),
7194 LSM_HOOK_INIT(file_lock
, selinux_file_lock
),
7195 LSM_HOOK_INIT(file_fcntl
, selinux_file_fcntl
),
7196 LSM_HOOK_INIT(file_set_fowner
, selinux_file_set_fowner
),
7197 LSM_HOOK_INIT(file_send_sigiotask
, selinux_file_send_sigiotask
),
7198 LSM_HOOK_INIT(file_receive
, selinux_file_receive
),
7200 LSM_HOOK_INIT(file_open
, selinux_file_open
),
7202 LSM_HOOK_INIT(task_alloc
, selinux_task_alloc
),
7203 LSM_HOOK_INIT(cred_prepare
, selinux_cred_prepare
),
7204 LSM_HOOK_INIT(cred_transfer
, selinux_cred_transfer
),
7205 LSM_HOOK_INIT(cred_getsecid
, selinux_cred_getsecid
),
7206 LSM_HOOK_INIT(kernel_act_as
, selinux_kernel_act_as
),
7207 LSM_HOOK_INIT(kernel_create_files_as
, selinux_kernel_create_files_as
),
7208 LSM_HOOK_INIT(kernel_module_request
, selinux_kernel_module_request
),
7209 LSM_HOOK_INIT(kernel_load_data
, selinux_kernel_load_data
),
7210 LSM_HOOK_INIT(kernel_read_file
, selinux_kernel_read_file
),
7211 LSM_HOOK_INIT(task_setpgid
, selinux_task_setpgid
),
7212 LSM_HOOK_INIT(task_getpgid
, selinux_task_getpgid
),
7213 LSM_HOOK_INIT(task_getsid
, selinux_task_getsid
),
7214 LSM_HOOK_INIT(task_getsecid_subj
, selinux_task_getsecid_subj
),
7215 LSM_HOOK_INIT(task_getsecid_obj
, selinux_task_getsecid_obj
),
7216 LSM_HOOK_INIT(task_setnice
, selinux_task_setnice
),
7217 LSM_HOOK_INIT(task_setioprio
, selinux_task_setioprio
),
7218 LSM_HOOK_INIT(task_getioprio
, selinux_task_getioprio
),
7219 LSM_HOOK_INIT(task_prlimit
, selinux_task_prlimit
),
7220 LSM_HOOK_INIT(task_setrlimit
, selinux_task_setrlimit
),
7221 LSM_HOOK_INIT(task_setscheduler
, selinux_task_setscheduler
),
7222 LSM_HOOK_INIT(task_getscheduler
, selinux_task_getscheduler
),
7223 LSM_HOOK_INIT(task_movememory
, selinux_task_movememory
),
7224 LSM_HOOK_INIT(task_kill
, selinux_task_kill
),
7225 LSM_HOOK_INIT(task_to_inode
, selinux_task_to_inode
),
7227 LSM_HOOK_INIT(ipc_permission
, selinux_ipc_permission
),
7228 LSM_HOOK_INIT(ipc_getsecid
, selinux_ipc_getsecid
),
7230 LSM_HOOK_INIT(msg_queue_associate
, selinux_msg_queue_associate
),
7231 LSM_HOOK_INIT(msg_queue_msgctl
, selinux_msg_queue_msgctl
),
7232 LSM_HOOK_INIT(msg_queue_msgsnd
, selinux_msg_queue_msgsnd
),
7233 LSM_HOOK_INIT(msg_queue_msgrcv
, selinux_msg_queue_msgrcv
),
7235 LSM_HOOK_INIT(shm_associate
, selinux_shm_associate
),
7236 LSM_HOOK_INIT(shm_shmctl
, selinux_shm_shmctl
),
7237 LSM_HOOK_INIT(shm_shmat
, selinux_shm_shmat
),
7239 LSM_HOOK_INIT(sem_associate
, selinux_sem_associate
),
7240 LSM_HOOK_INIT(sem_semctl
, selinux_sem_semctl
),
7241 LSM_HOOK_INIT(sem_semop
, selinux_sem_semop
),
7243 LSM_HOOK_INIT(d_instantiate
, selinux_d_instantiate
),
7245 LSM_HOOK_INIT(getprocattr
, selinux_getprocattr
),
7246 LSM_HOOK_INIT(setprocattr
, selinux_setprocattr
),
7248 LSM_HOOK_INIT(ismaclabel
, selinux_ismaclabel
),
7249 LSM_HOOK_INIT(secctx_to_secid
, selinux_secctx_to_secid
),
7250 LSM_HOOK_INIT(release_secctx
, selinux_release_secctx
),
7251 LSM_HOOK_INIT(inode_invalidate_secctx
, selinux_inode_invalidate_secctx
),
7252 LSM_HOOK_INIT(inode_notifysecctx
, selinux_inode_notifysecctx
),
7253 LSM_HOOK_INIT(inode_setsecctx
, selinux_inode_setsecctx
),
7255 LSM_HOOK_INIT(unix_stream_connect
, selinux_socket_unix_stream_connect
),
7256 LSM_HOOK_INIT(unix_may_send
, selinux_socket_unix_may_send
),
7258 LSM_HOOK_INIT(socket_create
, selinux_socket_create
),
7259 LSM_HOOK_INIT(socket_post_create
, selinux_socket_post_create
),
7260 LSM_HOOK_INIT(socket_socketpair
, selinux_socket_socketpair
),
7261 LSM_HOOK_INIT(socket_bind
, selinux_socket_bind
),
7262 LSM_HOOK_INIT(socket_connect
, selinux_socket_connect
),
7263 LSM_HOOK_INIT(socket_listen
, selinux_socket_listen
),
7264 LSM_HOOK_INIT(socket_accept
, selinux_socket_accept
),
7265 LSM_HOOK_INIT(socket_sendmsg
, selinux_socket_sendmsg
),
7266 LSM_HOOK_INIT(socket_recvmsg
, selinux_socket_recvmsg
),
7267 LSM_HOOK_INIT(socket_getsockname
, selinux_socket_getsockname
),
7268 LSM_HOOK_INIT(socket_getpeername
, selinux_socket_getpeername
),
7269 LSM_HOOK_INIT(socket_getsockopt
, selinux_socket_getsockopt
),
7270 LSM_HOOK_INIT(socket_setsockopt
, selinux_socket_setsockopt
),
7271 LSM_HOOK_INIT(socket_shutdown
, selinux_socket_shutdown
),
7272 LSM_HOOK_INIT(socket_sock_rcv_skb
, selinux_socket_sock_rcv_skb
),
7273 LSM_HOOK_INIT(socket_getpeersec_stream
,
7274 selinux_socket_getpeersec_stream
),
7275 LSM_HOOK_INIT(socket_getpeersec_dgram
, selinux_socket_getpeersec_dgram
),
7276 LSM_HOOK_INIT(sk_free_security
, selinux_sk_free_security
),
7277 LSM_HOOK_INIT(sk_clone_security
, selinux_sk_clone_security
),
7278 LSM_HOOK_INIT(sk_getsecid
, selinux_sk_getsecid
),
7279 LSM_HOOK_INIT(sock_graft
, selinux_sock_graft
),
7280 LSM_HOOK_INIT(sctp_assoc_request
, selinux_sctp_assoc_request
),
7281 LSM_HOOK_INIT(sctp_sk_clone
, selinux_sctp_sk_clone
),
7282 LSM_HOOK_INIT(sctp_bind_connect
, selinux_sctp_bind_connect
),
7283 LSM_HOOK_INIT(inet_conn_request
, selinux_inet_conn_request
),
7284 LSM_HOOK_INIT(inet_csk_clone
, selinux_inet_csk_clone
),
7285 LSM_HOOK_INIT(inet_conn_established
, selinux_inet_conn_established
),
7286 LSM_HOOK_INIT(secmark_relabel_packet
, selinux_secmark_relabel_packet
),
7287 LSM_HOOK_INIT(secmark_refcount_inc
, selinux_secmark_refcount_inc
),
7288 LSM_HOOK_INIT(secmark_refcount_dec
, selinux_secmark_refcount_dec
),
7289 LSM_HOOK_INIT(req_classify_flow
, selinux_req_classify_flow
),
7290 LSM_HOOK_INIT(tun_dev_free_security
, selinux_tun_dev_free_security
),
7291 LSM_HOOK_INIT(tun_dev_create
, selinux_tun_dev_create
),
7292 LSM_HOOK_INIT(tun_dev_attach_queue
, selinux_tun_dev_attach_queue
),
7293 LSM_HOOK_INIT(tun_dev_attach
, selinux_tun_dev_attach
),
7294 LSM_HOOK_INIT(tun_dev_open
, selinux_tun_dev_open
),
7295 #ifdef CONFIG_SECURITY_INFINIBAND
7296 LSM_HOOK_INIT(ib_pkey_access
, selinux_ib_pkey_access
),
7297 LSM_HOOK_INIT(ib_endport_manage_subnet
,
7298 selinux_ib_endport_manage_subnet
),
7299 LSM_HOOK_INIT(ib_free_security
, selinux_ib_free_security
),
7301 #ifdef CONFIG_SECURITY_NETWORK_XFRM
7302 LSM_HOOK_INIT(xfrm_policy_free_security
, selinux_xfrm_policy_free
),
7303 LSM_HOOK_INIT(xfrm_policy_delete_security
, selinux_xfrm_policy_delete
),
7304 LSM_HOOK_INIT(xfrm_state_free_security
, selinux_xfrm_state_free
),
7305 LSM_HOOK_INIT(xfrm_state_delete_security
, selinux_xfrm_state_delete
),
7306 LSM_HOOK_INIT(xfrm_policy_lookup
, selinux_xfrm_policy_lookup
),
7307 LSM_HOOK_INIT(xfrm_state_pol_flow_match
,
7308 selinux_xfrm_state_pol_flow_match
),
7309 LSM_HOOK_INIT(xfrm_decode_session
, selinux_xfrm_decode_session
),
7313 LSM_HOOK_INIT(key_free
, selinux_key_free
),
7314 LSM_HOOK_INIT(key_permission
, selinux_key_permission
),
7315 LSM_HOOK_INIT(key_getsecurity
, selinux_key_getsecurity
),
7316 #ifdef CONFIG_KEY_NOTIFICATIONS
7317 LSM_HOOK_INIT(watch_key
, selinux_watch_key
),
7322 LSM_HOOK_INIT(audit_rule_known
, selinux_audit_rule_known
),
7323 LSM_HOOK_INIT(audit_rule_match
, selinux_audit_rule_match
),
7324 LSM_HOOK_INIT(audit_rule_free
, selinux_audit_rule_free
),
7327 #ifdef CONFIG_BPF_SYSCALL
7328 LSM_HOOK_INIT(bpf
, selinux_bpf
),
7329 LSM_HOOK_INIT(bpf_map
, selinux_bpf_map
),
7330 LSM_HOOK_INIT(bpf_prog
, selinux_bpf_prog
),
7331 LSM_HOOK_INIT(bpf_map_free_security
, selinux_bpf_map_free
),
7332 LSM_HOOK_INIT(bpf_prog_free_security
, selinux_bpf_prog_free
),
7335 #ifdef CONFIG_PERF_EVENTS
7336 LSM_HOOK_INIT(perf_event_open
, selinux_perf_event_open
),
7337 LSM_HOOK_INIT(perf_event_free
, selinux_perf_event_free
),
7338 LSM_HOOK_INIT(perf_event_read
, selinux_perf_event_read
),
7339 LSM_HOOK_INIT(perf_event_write
, selinux_perf_event_write
),
7342 LSM_HOOK_INIT(locked_down
, selinux_lockdown
),
7345 * PUT "CLONING" (ACCESSING + ALLOCATING) HOOKS HERE
7347 LSM_HOOK_INIT(fs_context_dup
, selinux_fs_context_dup
),
7348 LSM_HOOK_INIT(fs_context_parse_param
, selinux_fs_context_parse_param
),
7349 LSM_HOOK_INIT(sb_eat_lsm_opts
, selinux_sb_eat_lsm_opts
),
7350 LSM_HOOK_INIT(sb_add_mnt_opt
, selinux_add_mnt_opt
),
7351 #ifdef CONFIG_SECURITY_NETWORK_XFRM
7352 LSM_HOOK_INIT(xfrm_policy_clone_security
, selinux_xfrm_policy_clone
),
7356 * PUT "ALLOCATING" HOOKS HERE
7358 LSM_HOOK_INIT(msg_msg_alloc_security
, selinux_msg_msg_alloc_security
),
7359 LSM_HOOK_INIT(msg_queue_alloc_security
,
7360 selinux_msg_queue_alloc_security
),
7361 LSM_HOOK_INIT(shm_alloc_security
, selinux_shm_alloc_security
),
7362 LSM_HOOK_INIT(sb_alloc_security
, selinux_sb_alloc_security
),
7363 LSM_HOOK_INIT(inode_alloc_security
, selinux_inode_alloc_security
),
7364 LSM_HOOK_INIT(sem_alloc_security
, selinux_sem_alloc_security
),
7365 LSM_HOOK_INIT(secid_to_secctx
, selinux_secid_to_secctx
),
7366 LSM_HOOK_INIT(inode_getsecctx
, selinux_inode_getsecctx
),
7367 LSM_HOOK_INIT(sk_alloc_security
, selinux_sk_alloc_security
),
7368 LSM_HOOK_INIT(tun_dev_alloc_security
, selinux_tun_dev_alloc_security
),
7369 #ifdef CONFIG_SECURITY_INFINIBAND
7370 LSM_HOOK_INIT(ib_alloc_security
, selinux_ib_alloc_security
),
7372 #ifdef CONFIG_SECURITY_NETWORK_XFRM
7373 LSM_HOOK_INIT(xfrm_policy_alloc_security
, selinux_xfrm_policy_alloc
),
7374 LSM_HOOK_INIT(xfrm_state_alloc
, selinux_xfrm_state_alloc
),
7375 LSM_HOOK_INIT(xfrm_state_alloc_acquire
,
7376 selinux_xfrm_state_alloc_acquire
),
7379 LSM_HOOK_INIT(key_alloc
, selinux_key_alloc
),
7382 LSM_HOOK_INIT(audit_rule_init
, selinux_audit_rule_init
),
7384 #ifdef CONFIG_BPF_SYSCALL
7385 LSM_HOOK_INIT(bpf_map_alloc_security
, selinux_bpf_map_alloc
),
7386 LSM_HOOK_INIT(bpf_prog_alloc_security
, selinux_bpf_prog_alloc
),
7388 #ifdef CONFIG_PERF_EVENTS
7389 LSM_HOOK_INIT(perf_event_alloc
, selinux_perf_event_alloc
),
7393 static __init
int selinux_init(void)
7395 pr_info("SELinux: Initializing.\n");
7397 memset(&selinux_state
, 0, sizeof(selinux_state
));
7398 enforcing_set(&selinux_state
, selinux_enforcing_boot
);
7399 checkreqprot_set(&selinux_state
, selinux_checkreqprot_boot
);
7400 selinux_avc_init(&selinux_state
.avc
);
7401 mutex_init(&selinux_state
.status_lock
);
7402 mutex_init(&selinux_state
.policy_mutex
);
7404 /* Set the security state for the initial task. */
7405 cred_init_security();
7407 default_noexec
= !(VM_DATA_DEFAULT_FLAGS
& VM_EXEC
);
7413 ebitmap_cache_init();
7415 hashtab_cache_init();
7417 security_add_hooks(selinux_hooks
, ARRAY_SIZE(selinux_hooks
),
7420 if (avc_add_callback(selinux_netcache_avc_callback
, AVC_CALLBACK_RESET
))
7421 panic("SELinux: Unable to register AVC netcache callback\n");
7423 if (avc_add_callback(selinux_lsm_notifier_avc_callback
, AVC_CALLBACK_RESET
))
7424 panic("SELinux: Unable to register AVC LSM notifier callback\n");
7426 if (selinux_enforcing_boot
)
7427 pr_debug("SELinux: Starting in enforcing mode\n");
7429 pr_debug("SELinux: Starting in permissive mode\n");
7431 fs_validate_description("selinux", selinux_fs_parameters
);
7436 static void delayed_superblock_init(struct super_block
*sb
, void *unused
)
7438 selinux_set_mnt_opts(sb
, NULL
, 0, NULL
);
7441 void selinux_complete_init(void)
7443 pr_debug("SELinux: Completing initialization.\n");
7445 /* Set up any superblocks initialized prior to the policy load. */
7446 pr_debug("SELinux: Setting up existing superblocks.\n");
7447 iterate_supers(delayed_superblock_init
, NULL
);
7450 /* SELinux requires early initialization in order to label
7451 all processes and objects when they are created. */
7452 DEFINE_LSM(selinux
) = {
7454 .flags
= LSM_FLAG_LEGACY_MAJOR
| LSM_FLAG_EXCLUSIVE
,
7455 .enabled
= &selinux_enabled_boot
,
7456 .blobs
= &selinux_blob_sizes
,
7457 .init
= selinux_init
,
7460 #if defined(CONFIG_NETFILTER)
7462 static const struct nf_hook_ops selinux_nf_ops
[] = {
7464 .hook
= selinux_ipv4_postroute
,
7466 .hooknum
= NF_INET_POST_ROUTING
,
7467 .priority
= NF_IP_PRI_SELINUX_LAST
,
7470 .hook
= selinux_ipv4_forward
,
7472 .hooknum
= NF_INET_FORWARD
,
7473 .priority
= NF_IP_PRI_SELINUX_FIRST
,
7476 .hook
= selinux_ipv4_output
,
7478 .hooknum
= NF_INET_LOCAL_OUT
,
7479 .priority
= NF_IP_PRI_SELINUX_FIRST
,
7481 #if IS_ENABLED(CONFIG_IPV6)
7483 .hook
= selinux_ipv6_postroute
,
7485 .hooknum
= NF_INET_POST_ROUTING
,
7486 .priority
= NF_IP6_PRI_SELINUX_LAST
,
7489 .hook
= selinux_ipv6_forward
,
7491 .hooknum
= NF_INET_FORWARD
,
7492 .priority
= NF_IP6_PRI_SELINUX_FIRST
,
7495 .hook
= selinux_ipv6_output
,
7497 .hooknum
= NF_INET_LOCAL_OUT
,
7498 .priority
= NF_IP6_PRI_SELINUX_FIRST
,
7503 static int __net_init
selinux_nf_register(struct net
*net
)
7505 return nf_register_net_hooks(net
, selinux_nf_ops
,
7506 ARRAY_SIZE(selinux_nf_ops
));
7509 static void __net_exit
selinux_nf_unregister(struct net
*net
)
7511 nf_unregister_net_hooks(net
, selinux_nf_ops
,
7512 ARRAY_SIZE(selinux_nf_ops
));
7515 static struct pernet_operations selinux_net_ops
= {
7516 .init
= selinux_nf_register
,
7517 .exit
= selinux_nf_unregister
,
7520 static int __init
selinux_nf_ip_init(void)
7524 if (!selinux_enabled_boot
)
7527 pr_debug("SELinux: Registering netfilter hooks\n");
7529 err
= register_pernet_subsys(&selinux_net_ops
);
7531 panic("SELinux: register_pernet_subsys: error %d\n", err
);
7535 __initcall(selinux_nf_ip_init
);
7537 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7538 static void selinux_nf_ip_exit(void)
7540 pr_debug("SELinux: Unregistering netfilter hooks\n");
7542 unregister_pernet_subsys(&selinux_net_ops
);
7546 #else /* CONFIG_NETFILTER */
7548 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7549 #define selinux_nf_ip_exit()
7552 #endif /* CONFIG_NETFILTER */
7554 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7555 int selinux_disable(struct selinux_state
*state
)
7557 if (selinux_initialized(state
)) {
7558 /* Not permitted after initial policy load. */
7562 if (selinux_disabled(state
)) {
7563 /* Only do this once. */
7567 selinux_mark_disabled(state
);
7569 pr_info("SELinux: Disabled at runtime.\n");
7572 * Unregister netfilter hooks.
7573 * Must be done before security_delete_hooks() to avoid breaking
7576 selinux_nf_ip_exit();
7578 security_delete_hooks(selinux_hooks
, ARRAY_SIZE(selinux_hooks
));
7580 /* Try to destroy the avc node cache */
7583 /* Unregister selinuxfs. */