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/errno.h>
29 #include <linux/sched/signal.h>
30 #include <linux/sched/task.h>
31 #include <linux/lsm_hooks.h>
32 #include <linux/xattr.h>
33 #include <linux/capability.h>
34 #include <linux/unistd.h>
36 #include <linux/mman.h>
37 #include <linux/slab.h>
38 #include <linux/pagemap.h>
39 #include <linux/proc_fs.h>
40 #include <linux/swap.h>
41 #include <linux/spinlock.h>
42 #include <linux/syscalls.h>
43 #include <linux/dcache.h>
44 #include <linux/file.h>
45 #include <linux/fdtable.h>
46 #include <linux/namei.h>
47 #include <linux/mount.h>
48 #include <linux/fs_context.h>
49 #include <linux/fs_parser.h>
50 #include <linux/netfilter_ipv4.h>
51 #include <linux/netfilter_ipv6.h>
52 #include <linux/tty.h>
54 #include <net/ip.h> /* for local_port_range[] */
55 #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
56 #include <net/inet_connection_sock.h>
57 #include <net/net_namespace.h>
58 #include <net/netlabel.h>
59 #include <linux/uaccess.h>
60 #include <asm/ioctls.h>
61 #include <linux/atomic.h>
62 #include <linux/bitops.h>
63 #include <linux/interrupt.h>
64 #include <linux/netdevice.h> /* for network interface checks */
65 #include <net/netlink.h>
66 #include <linux/tcp.h>
67 #include <linux/udp.h>
68 #include <linux/dccp.h>
69 #include <linux/sctp.h>
70 #include <net/sctp/structs.h>
71 #include <linux/quota.h>
72 #include <linux/un.h> /* for Unix socket types */
73 #include <net/af_unix.h> /* for Unix socket types */
74 #include <linux/parser.h>
75 #include <linux/nfs_mount.h>
77 #include <linux/hugetlb.h>
78 #include <linux/personality.h>
79 #include <linux/audit.h>
80 #include <linux/string.h>
81 #include <linux/mutex.h>
82 #include <linux/posix-timers.h>
83 #include <linux/syslog.h>
84 #include <linux/user_namespace.h>
85 #include <linux/export.h>
86 #include <linux/msg.h>
87 #include <linux/shm.h>
88 #include <linux/bpf.h>
89 #include <linux/kernfs.h>
90 #include <linux/stringhash.h> /* for hashlen_string() */
91 #include <uapi/linux/mount.h>
92 #include <linux/fsnotify.h>
93 #include <linux/fanotify.h>
102 #include "netlabel.h"
106 struct selinux_state selinux_state;
108 /* SECMARK reference count */
109 static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
111 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
112 static int selinux_enforcing_boot __initdata;
114 static int __init enforcing_setup(char *str)
116 unsigned long enforcing;
117 if (!kstrtoul(str, 0, &enforcing))
118 selinux_enforcing_boot = enforcing ? 1 : 0;
121 __setup("enforcing=", enforcing_setup);
123 #define selinux_enforcing_boot 1
126 int selinux_enabled_boot __initdata = 1;
127 #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
128 static int __init selinux_enabled_setup(char *str)
130 unsigned long enabled;
131 if (!kstrtoul(str, 0, &enabled))
132 selinux_enabled_boot = enabled ? 1 : 0;
135 __setup("selinux=", selinux_enabled_setup);
138 static unsigned int selinux_checkreqprot_boot =
139 CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
141 static int __init checkreqprot_setup(char *str)
143 unsigned long checkreqprot;
145 if (!kstrtoul(str, 0, &checkreqprot)) {
146 selinux_checkreqprot_boot = checkreqprot ? 1 : 0;
148 pr_warn("SELinux: checkreqprot set to 1 via kernel parameter. This is deprecated and will be rejected in a future kernel release.\n");
152 __setup("checkreqprot=", checkreqprot_setup);
155 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
158 * This function checks the SECMARK reference counter to see if any SECMARK
159 * targets are currently configured, if the reference counter is greater than
160 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
161 * enabled, false (0) if SECMARK is disabled. If the always_check_network
162 * policy capability is enabled, SECMARK is always considered enabled.
165 static int selinux_secmark_enabled(void)
167 return (selinux_policycap_alwaysnetwork() ||
168 atomic_read(&selinux_secmark_refcount));
172 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
175 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
176 * (1) if any are enabled or false (0) if neither are enabled. If the
177 * always_check_network policy capability is enabled, peer labeling
178 * is always considered enabled.
181 static int selinux_peerlbl_enabled(void)
183 return (selinux_policycap_alwaysnetwork() ||
184 netlbl_enabled() || selinux_xfrm_enabled());
187 static int selinux_netcache_avc_callback(u32 event)
189 if (event == AVC_CALLBACK_RESET) {
198 static int selinux_lsm_notifier_avc_callback(u32 event)
200 if (event == AVC_CALLBACK_RESET) {
202 call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
209 * initialise the security for the init task
211 static void cred_init_security(void)
213 struct task_security_struct *tsec;
215 tsec = selinux_cred(unrcu_pointer(current->real_cred));
216 tsec->osid = tsec->sid = SECINITSID_KERNEL;
220 * get the security ID of a set of credentials
222 static inline u32 cred_sid(const struct cred *cred)
224 const struct task_security_struct *tsec;
226 tsec = selinux_cred(cred);
231 * get the objective security ID of a task
233 static inline u32 task_sid_obj(const struct task_struct *task)
238 sid = cred_sid(__task_cred(task));
243 static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
246 * Try reloading inode security labels that have been marked as invalid. The
247 * @may_sleep parameter indicates when sleeping and thus reloading labels is
248 * allowed; when set to false, returns -ECHILD when the label is
249 * invalid. The @dentry parameter should be set to a dentry of the inode.
251 static int __inode_security_revalidate(struct inode *inode,
252 struct dentry *dentry,
255 struct inode_security_struct *isec = selinux_inode(inode);
257 might_sleep_if(may_sleep);
259 if (selinux_initialized(&selinux_state) &&
260 isec->initialized != LABEL_INITIALIZED) {
265 * Try reloading the inode security label. This will fail if
266 * @opt_dentry is NULL and no dentry for this inode can be
267 * found; in that case, continue using the old label.
269 inode_doinit_with_dentry(inode, dentry);
274 static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
276 return selinux_inode(inode);
279 static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
283 error = __inode_security_revalidate(inode, NULL, !rcu);
285 return ERR_PTR(error);
286 return selinux_inode(inode);
290 * Get the security label of an inode.
292 static struct inode_security_struct *inode_security(struct inode *inode)
294 __inode_security_revalidate(inode, NULL, true);
295 return selinux_inode(inode);
298 static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
300 struct inode *inode = d_backing_inode(dentry);
302 return selinux_inode(inode);
306 * Get the security label of a dentry's backing inode.
308 static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
310 struct inode *inode = d_backing_inode(dentry);
312 __inode_security_revalidate(inode, dentry, true);
313 return selinux_inode(inode);
316 static void inode_free_security(struct inode *inode)
318 struct inode_security_struct *isec = selinux_inode(inode);
319 struct superblock_security_struct *sbsec;
323 sbsec = selinux_superblock(inode->i_sb);
325 * As not all inode security structures are in a list, we check for
326 * empty list outside of the lock to make sure that we won't waste
327 * time taking a lock doing nothing.
329 * The list_del_init() function can be safely called more than once.
330 * It should not be possible for this function to be called with
331 * concurrent list_add(), but for better safety against future changes
332 * in the code, we use list_empty_careful() here.
334 if (!list_empty_careful(&isec->list)) {
335 spin_lock(&sbsec->isec_lock);
336 list_del_init(&isec->list);
337 spin_unlock(&sbsec->isec_lock);
341 struct selinux_mnt_opts {
348 static void selinux_free_mnt_opts(void *mnt_opts)
362 #define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
372 A(rootcontext, true),
377 static int match_opt_prefix(char *s, int l, char **arg)
381 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
382 size_t len = tokens[i].len;
383 if (len > l || memcmp(s, tokens[i].name, len))
385 if (tokens[i].has_arg) {
386 if (len == l || s[len] != '=')
391 return tokens[i].opt;
396 #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
398 static int may_context_mount_sb_relabel(u32 sid,
399 struct superblock_security_struct *sbsec,
400 const struct cred *cred)
402 const struct task_security_struct *tsec = selinux_cred(cred);
405 rc = avc_has_perm(&selinux_state,
406 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
407 FILESYSTEM__RELABELFROM, NULL);
411 rc = avc_has_perm(&selinux_state,
412 tsec->sid, sid, SECCLASS_FILESYSTEM,
413 FILESYSTEM__RELABELTO, NULL);
417 static int may_context_mount_inode_relabel(u32 sid,
418 struct superblock_security_struct *sbsec,
419 const struct cred *cred)
421 const struct task_security_struct *tsec = selinux_cred(cred);
423 rc = avc_has_perm(&selinux_state,
424 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
425 FILESYSTEM__RELABELFROM, NULL);
429 rc = avc_has_perm(&selinux_state,
430 sid, sbsec->sid, SECCLASS_FILESYSTEM,
431 FILESYSTEM__ASSOCIATE, NULL);
435 static int selinux_is_genfs_special_handling(struct super_block *sb)
437 /* Special handling. Genfs but also in-core setxattr handler */
438 return !strcmp(sb->s_type->name, "sysfs") ||
439 !strcmp(sb->s_type->name, "pstore") ||
440 !strcmp(sb->s_type->name, "debugfs") ||
441 !strcmp(sb->s_type->name, "tracefs") ||
442 !strcmp(sb->s_type->name, "rootfs") ||
443 (selinux_policycap_cgroupseclabel() &&
444 (!strcmp(sb->s_type->name, "cgroup") ||
445 !strcmp(sb->s_type->name, "cgroup2")));
448 static int selinux_is_sblabel_mnt(struct super_block *sb)
450 struct superblock_security_struct *sbsec = selinux_superblock(sb);
453 * IMPORTANT: Double-check logic in this function when adding a new
454 * SECURITY_FS_USE_* definition!
456 BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7);
458 switch (sbsec->behavior) {
459 case SECURITY_FS_USE_XATTR:
460 case SECURITY_FS_USE_TRANS:
461 case SECURITY_FS_USE_TASK:
462 case SECURITY_FS_USE_NATIVE:
465 case SECURITY_FS_USE_GENFS:
466 return selinux_is_genfs_special_handling(sb);
468 /* Never allow relabeling on context mounts */
469 case SECURITY_FS_USE_MNTPOINT:
470 case SECURITY_FS_USE_NONE:
476 static int sb_check_xattr_support(struct super_block *sb)
478 struct superblock_security_struct *sbsec = selinux_superblock(sb);
479 struct dentry *root = sb->s_root;
480 struct inode *root_inode = d_backing_inode(root);
485 * Make sure that the xattr handler exists and that no
486 * error other than -ENODATA is returned by getxattr on
487 * the root directory. -ENODATA is ok, as this may be
488 * the first boot of the SELinux kernel before we have
489 * assigned xattr values to the filesystem.
491 if (!(root_inode->i_opflags & IOP_XATTR)) {
492 pr_warn("SELinux: (dev %s, type %s) has no xattr support\n",
493 sb->s_id, sb->s_type->name);
497 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
498 if (rc < 0 && rc != -ENODATA) {
499 if (rc == -EOPNOTSUPP) {
500 pr_warn("SELinux: (dev %s, type %s) has no security xattr handler\n",
501 sb->s_id, sb->s_type->name);
504 pr_warn("SELinux: (dev %s, type %s) getxattr errno %d\n",
505 sb->s_id, sb->s_type->name, -rc);
512 /* No xattr support - try to fallback to genfs if possible. */
513 rc = security_genfs_sid(&selinux_state, sb->s_type->name, "/",
518 pr_warn("SELinux: (dev %s, type %s) falling back to genfs\n",
519 sb->s_id, sb->s_type->name);
520 sbsec->behavior = SECURITY_FS_USE_GENFS;
525 static int sb_finish_set_opts(struct super_block *sb)
527 struct superblock_security_struct *sbsec = selinux_superblock(sb);
528 struct dentry *root = sb->s_root;
529 struct inode *root_inode = d_backing_inode(root);
532 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
533 rc = sb_check_xattr_support(sb);
538 sbsec->flags |= SE_SBINITIALIZED;
541 * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply
542 * leave the flag untouched because sb_clone_mnt_opts might be handing
543 * us a superblock that needs the flag to be cleared.
545 if (selinux_is_sblabel_mnt(sb))
546 sbsec->flags |= SBLABEL_MNT;
548 sbsec->flags &= ~SBLABEL_MNT;
550 /* Initialize the root inode. */
551 rc = inode_doinit_with_dentry(root_inode, root);
553 /* Initialize any other inodes associated with the superblock, e.g.
554 inodes created prior to initial policy load or inodes created
555 during get_sb by a pseudo filesystem that directly
557 spin_lock(&sbsec->isec_lock);
558 while (!list_empty(&sbsec->isec_head)) {
559 struct inode_security_struct *isec =
560 list_first_entry(&sbsec->isec_head,
561 struct inode_security_struct, list);
562 struct inode *inode = isec->inode;
563 list_del_init(&isec->list);
564 spin_unlock(&sbsec->isec_lock);
565 inode = igrab(inode);
567 if (!IS_PRIVATE(inode))
568 inode_doinit_with_dentry(inode, NULL);
571 spin_lock(&sbsec->isec_lock);
573 spin_unlock(&sbsec->isec_lock);
577 static int bad_option(struct superblock_security_struct *sbsec, char flag,
578 u32 old_sid, u32 new_sid)
580 char mnt_flags = sbsec->flags & SE_MNTMASK;
582 /* check if the old mount command had the same options */
583 if (sbsec->flags & SE_SBINITIALIZED)
584 if (!(sbsec->flags & flag) ||
585 (old_sid != new_sid))
588 /* check if we were passed the same options twice,
589 * aka someone passed context=a,context=b
591 if (!(sbsec->flags & SE_SBINITIALIZED))
592 if (mnt_flags & flag)
598 * Allow filesystems with binary mount data to explicitly set mount point
599 * labeling information.
601 static int selinux_set_mnt_opts(struct super_block *sb,
603 unsigned long kern_flags,
604 unsigned long *set_kern_flags)
606 const struct cred *cred = current_cred();
607 struct superblock_security_struct *sbsec = selinux_superblock(sb);
608 struct dentry *root = sb->s_root;
609 struct selinux_mnt_opts *opts = mnt_opts;
610 struct inode_security_struct *root_isec;
611 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
612 u32 defcontext_sid = 0;
615 mutex_lock(&sbsec->lock);
617 if (!selinux_initialized(&selinux_state)) {
619 /* Defer initialization until selinux_complete_init,
620 after the initial policy is loaded and the security
621 server is ready to handle calls. */
625 pr_warn("SELinux: Unable to set superblock options "
626 "before the security server is initialized\n");
629 if (kern_flags && !set_kern_flags) {
630 /* Specifying internal flags without providing a place to
631 * place the results is not allowed */
637 * Binary mount data FS will come through this function twice. Once
638 * from an explicit call and once from the generic calls from the vfs.
639 * Since the generic VFS calls will not contain any security mount data
640 * we need to skip the double mount verification.
642 * This does open a hole in which we will not notice if the first
643 * mount using this sb set explict options and a second mount using
644 * this sb does not set any security options. (The first options
645 * will be used for both mounts)
647 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
651 root_isec = backing_inode_security_novalidate(root);
654 * parse the mount options, check if they are valid sids.
655 * also check if someone is trying to mount the same sb more
656 * than once with different security options.
659 if (opts->fscontext_sid) {
660 fscontext_sid = opts->fscontext_sid;
661 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
663 goto out_double_mount;
664 sbsec->flags |= FSCONTEXT_MNT;
666 if (opts->context_sid) {
667 context_sid = opts->context_sid;
668 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
670 goto out_double_mount;
671 sbsec->flags |= CONTEXT_MNT;
673 if (opts->rootcontext_sid) {
674 rootcontext_sid = opts->rootcontext_sid;
675 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
677 goto out_double_mount;
678 sbsec->flags |= ROOTCONTEXT_MNT;
680 if (opts->defcontext_sid) {
681 defcontext_sid = opts->defcontext_sid;
682 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
684 goto out_double_mount;
685 sbsec->flags |= DEFCONTEXT_MNT;
689 if (sbsec->flags & SE_SBINITIALIZED) {
690 /* previously mounted with options, but not on this attempt? */
691 if ((sbsec->flags & SE_MNTMASK) && !opts)
692 goto out_double_mount;
697 if (strcmp(sb->s_type->name, "proc") == 0)
698 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
700 if (!strcmp(sb->s_type->name, "debugfs") ||
701 !strcmp(sb->s_type->name, "tracefs") ||
702 !strcmp(sb->s_type->name, "binder") ||
703 !strcmp(sb->s_type->name, "bpf") ||
704 !strcmp(sb->s_type->name, "pstore") ||
705 !strcmp(sb->s_type->name, "securityfs"))
706 sbsec->flags |= SE_SBGENFS;
708 if (!strcmp(sb->s_type->name, "sysfs") ||
709 !strcmp(sb->s_type->name, "cgroup") ||
710 !strcmp(sb->s_type->name, "cgroup2"))
711 sbsec->flags |= SE_SBGENFS | SE_SBGENFS_XATTR;
713 if (!sbsec->behavior) {
715 * Determine the labeling behavior to use for this
718 rc = security_fs_use(&selinux_state, sb);
720 pr_warn("%s: security_fs_use(%s) returned %d\n",
721 __func__, sb->s_type->name, rc);
727 * If this is a user namespace mount and the filesystem type is not
728 * explicitly whitelisted, then no contexts are allowed on the command
729 * line and security labels must be ignored.
731 if (sb->s_user_ns != &init_user_ns &&
732 strcmp(sb->s_type->name, "tmpfs") &&
733 strcmp(sb->s_type->name, "ramfs") &&
734 strcmp(sb->s_type->name, "devpts") &&
735 strcmp(sb->s_type->name, "overlay")) {
736 if (context_sid || fscontext_sid || rootcontext_sid ||
741 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
742 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
743 rc = security_transition_sid(&selinux_state,
747 &sbsec->mntpoint_sid);
754 /* sets the context of the superblock for the fs being mounted. */
756 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
760 sbsec->sid = fscontext_sid;
764 * Switch to using mount point labeling behavior.
765 * sets the label used on all file below the mountpoint, and will set
766 * the superblock context if not already set.
768 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
769 sbsec->behavior = SECURITY_FS_USE_NATIVE;
770 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
774 if (!fscontext_sid) {
775 rc = may_context_mount_sb_relabel(context_sid, sbsec,
779 sbsec->sid = context_sid;
781 rc = may_context_mount_inode_relabel(context_sid, sbsec,
786 if (!rootcontext_sid)
787 rootcontext_sid = context_sid;
789 sbsec->mntpoint_sid = context_sid;
790 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
793 if (rootcontext_sid) {
794 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
799 root_isec->sid = rootcontext_sid;
800 root_isec->initialized = LABEL_INITIALIZED;
803 if (defcontext_sid) {
804 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
805 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
807 pr_warn("SELinux: defcontext option is "
808 "invalid for this filesystem type\n");
812 if (defcontext_sid != sbsec->def_sid) {
813 rc = may_context_mount_inode_relabel(defcontext_sid,
819 sbsec->def_sid = defcontext_sid;
823 rc = sb_finish_set_opts(sb);
825 mutex_unlock(&sbsec->lock);
829 pr_warn("SELinux: mount invalid. Same superblock, different "
830 "security settings for (dev %s, type %s)\n", sb->s_id,
835 static int selinux_cmp_sb_context(const struct super_block *oldsb,
836 const struct super_block *newsb)
838 struct superblock_security_struct *old = selinux_superblock(oldsb);
839 struct superblock_security_struct *new = selinux_superblock(newsb);
840 char oldflags = old->flags & SE_MNTMASK;
841 char newflags = new->flags & SE_MNTMASK;
843 if (oldflags != newflags)
845 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
847 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
849 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
851 if (oldflags & ROOTCONTEXT_MNT) {
852 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
853 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
854 if (oldroot->sid != newroot->sid)
859 pr_warn("SELinux: mount invalid. Same superblock, "
860 "different security settings for (dev %s, "
861 "type %s)\n", newsb->s_id, newsb->s_type->name);
865 static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
866 struct super_block *newsb,
867 unsigned long kern_flags,
868 unsigned long *set_kern_flags)
871 const struct superblock_security_struct *oldsbsec =
872 selinux_superblock(oldsb);
873 struct superblock_security_struct *newsbsec = selinux_superblock(newsb);
875 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
876 int set_context = (oldsbsec->flags & CONTEXT_MNT);
877 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
880 * if the parent was able to be mounted it clearly had no special lsm
881 * mount options. thus we can safely deal with this superblock later
883 if (!selinux_initialized(&selinux_state))
887 * Specifying internal flags without providing a place to
888 * place the results is not allowed.
890 if (kern_flags && !set_kern_flags)
893 /* how can we clone if the old one wasn't set up?? */
894 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
896 /* if fs is reusing a sb, make sure that the contexts match */
897 if (newsbsec->flags & SE_SBINITIALIZED) {
898 if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context)
899 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
900 return selinux_cmp_sb_context(oldsb, newsb);
903 mutex_lock(&newsbsec->lock);
905 newsbsec->flags = oldsbsec->flags;
907 newsbsec->sid = oldsbsec->sid;
908 newsbsec->def_sid = oldsbsec->def_sid;
909 newsbsec->behavior = oldsbsec->behavior;
911 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
912 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
913 rc = security_fs_use(&selinux_state, newsb);
918 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
919 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
920 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
924 u32 sid = oldsbsec->mntpoint_sid;
928 if (!set_rootcontext) {
929 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
932 newsbsec->mntpoint_sid = sid;
934 if (set_rootcontext) {
935 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
936 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
938 newisec->sid = oldisec->sid;
941 sb_finish_set_opts(newsb);
943 mutex_unlock(&newsbsec->lock);
947 static int selinux_add_opt(int token, const char *s, void **mnt_opts)
949 struct selinux_mnt_opts *opts = *mnt_opts;
950 bool is_alloc_opts = false;
954 if (token == Opt_seclabel)
955 /* eaten and completely ignored */
960 if (!selinux_initialized(&selinux_state)) {
961 pr_warn("SELinux: Unable to set superblock options before the security server is initialized\n");
966 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
970 is_alloc_opts = true;
975 if (opts->context_sid || opts->defcontext_sid)
977 dst_sid = &opts->context_sid;
980 if (opts->fscontext_sid)
982 dst_sid = &opts->fscontext_sid;
984 case Opt_rootcontext:
985 if (opts->rootcontext_sid)
987 dst_sid = &opts->rootcontext_sid;
990 if (opts->context_sid || opts->defcontext_sid)
992 dst_sid = &opts->defcontext_sid;
998 rc = security_context_str_to_sid(&selinux_state, s, dst_sid, GFP_KERNEL);
1000 pr_warn("SELinux: security_context_str_to_sid (%s) failed with errno=%d\n",
1005 if (is_alloc_opts) {
1009 pr_warn(SEL_MOUNT_FAIL_MSG);
1013 static int show_sid(struct seq_file *m, u32 sid)
1015 char *context = NULL;
1019 rc = security_sid_to_context(&selinux_state, sid,
1022 bool has_comma = context && strchr(context, ',');
1027 seq_escape(m, context, "\"\n\\");
1035 static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1037 struct superblock_security_struct *sbsec = selinux_superblock(sb);
1040 if (!(sbsec->flags & SE_SBINITIALIZED))
1043 if (!selinux_initialized(&selinux_state))
1046 if (sbsec->flags & FSCONTEXT_MNT) {
1048 seq_puts(m, FSCONTEXT_STR);
1049 rc = show_sid(m, sbsec->sid);
1053 if (sbsec->flags & CONTEXT_MNT) {
1055 seq_puts(m, CONTEXT_STR);
1056 rc = show_sid(m, sbsec->mntpoint_sid);
1060 if (sbsec->flags & DEFCONTEXT_MNT) {
1062 seq_puts(m, DEFCONTEXT_STR);
1063 rc = show_sid(m, sbsec->def_sid);
1067 if (sbsec->flags & ROOTCONTEXT_MNT) {
1068 struct dentry *root = sb->s_root;
1069 struct inode_security_struct *isec = backing_inode_security(root);
1071 seq_puts(m, ROOTCONTEXT_STR);
1072 rc = show_sid(m, isec->sid);
1076 if (sbsec->flags & SBLABEL_MNT) {
1078 seq_puts(m, SECLABEL_STR);
1083 static inline u16 inode_mode_to_security_class(umode_t mode)
1085 switch (mode & S_IFMT) {
1087 return SECCLASS_SOCK_FILE;
1089 return SECCLASS_LNK_FILE;
1091 return SECCLASS_FILE;
1093 return SECCLASS_BLK_FILE;
1095 return SECCLASS_DIR;
1097 return SECCLASS_CHR_FILE;
1099 return SECCLASS_FIFO_FILE;
1103 return SECCLASS_FILE;
1106 static inline int default_protocol_stream(int protocol)
1108 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP ||
1109 protocol == IPPROTO_MPTCP);
1112 static inline int default_protocol_dgram(int protocol)
1114 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1117 static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1119 int extsockclass = selinux_policycap_extsockclass();
1125 case SOCK_SEQPACKET:
1126 return SECCLASS_UNIX_STREAM_SOCKET;
1129 return SECCLASS_UNIX_DGRAM_SOCKET;
1136 case SOCK_SEQPACKET:
1137 if (default_protocol_stream(protocol))
1138 return SECCLASS_TCP_SOCKET;
1139 else if (extsockclass && protocol == IPPROTO_SCTP)
1140 return SECCLASS_SCTP_SOCKET;
1142 return SECCLASS_RAWIP_SOCKET;
1144 if (default_protocol_dgram(protocol))
1145 return SECCLASS_UDP_SOCKET;
1146 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1147 protocol == IPPROTO_ICMPV6))
1148 return SECCLASS_ICMP_SOCKET;
1150 return SECCLASS_RAWIP_SOCKET;
1152 return SECCLASS_DCCP_SOCKET;
1154 return SECCLASS_RAWIP_SOCKET;
1160 return SECCLASS_NETLINK_ROUTE_SOCKET;
1161 case NETLINK_SOCK_DIAG:
1162 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1164 return SECCLASS_NETLINK_NFLOG_SOCKET;
1166 return SECCLASS_NETLINK_XFRM_SOCKET;
1167 case NETLINK_SELINUX:
1168 return SECCLASS_NETLINK_SELINUX_SOCKET;
1170 return SECCLASS_NETLINK_ISCSI_SOCKET;
1172 return SECCLASS_NETLINK_AUDIT_SOCKET;
1173 case NETLINK_FIB_LOOKUP:
1174 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1175 case NETLINK_CONNECTOR:
1176 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1177 case NETLINK_NETFILTER:
1178 return SECCLASS_NETLINK_NETFILTER_SOCKET;
1179 case NETLINK_DNRTMSG:
1180 return SECCLASS_NETLINK_DNRT_SOCKET;
1181 case NETLINK_KOBJECT_UEVENT:
1182 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
1183 case NETLINK_GENERIC:
1184 return SECCLASS_NETLINK_GENERIC_SOCKET;
1185 case NETLINK_SCSITRANSPORT:
1186 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1188 return SECCLASS_NETLINK_RDMA_SOCKET;
1189 case NETLINK_CRYPTO:
1190 return SECCLASS_NETLINK_CRYPTO_SOCKET;
1192 return SECCLASS_NETLINK_SOCKET;
1195 return SECCLASS_PACKET_SOCKET;
1197 return SECCLASS_KEY_SOCKET;
1199 return SECCLASS_APPLETALK_SOCKET;
1205 return SECCLASS_AX25_SOCKET;
1207 return SECCLASS_IPX_SOCKET;
1209 return SECCLASS_NETROM_SOCKET;
1211 return SECCLASS_ATMPVC_SOCKET;
1213 return SECCLASS_X25_SOCKET;
1215 return SECCLASS_ROSE_SOCKET;
1217 return SECCLASS_DECNET_SOCKET;
1219 return SECCLASS_ATMSVC_SOCKET;
1221 return SECCLASS_RDS_SOCKET;
1223 return SECCLASS_IRDA_SOCKET;
1225 return SECCLASS_PPPOX_SOCKET;
1227 return SECCLASS_LLC_SOCKET;
1229 return SECCLASS_CAN_SOCKET;
1231 return SECCLASS_TIPC_SOCKET;
1233 return SECCLASS_BLUETOOTH_SOCKET;
1235 return SECCLASS_IUCV_SOCKET;
1237 return SECCLASS_RXRPC_SOCKET;
1239 return SECCLASS_ISDN_SOCKET;
1241 return SECCLASS_PHONET_SOCKET;
1243 return SECCLASS_IEEE802154_SOCKET;
1245 return SECCLASS_CAIF_SOCKET;
1247 return SECCLASS_ALG_SOCKET;
1249 return SECCLASS_NFC_SOCKET;
1251 return SECCLASS_VSOCK_SOCKET;
1253 return SECCLASS_KCM_SOCKET;
1255 return SECCLASS_QIPCRTR_SOCKET;
1257 return SECCLASS_SMC_SOCKET;
1259 return SECCLASS_XDP_SOCKET;
1261 return SECCLASS_MCTP_SOCKET;
1263 #error New address family defined, please update this function.
1268 return SECCLASS_SOCKET;
1271 static int selinux_genfs_get_sid(struct dentry *dentry,
1277 struct super_block *sb = dentry->d_sb;
1278 char *buffer, *path;
1280 buffer = (char *)__get_free_page(GFP_KERNEL);
1284 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1288 if (flags & SE_SBPROC) {
1289 /* each process gets a /proc/PID/ entry. Strip off the
1290 * PID part to get a valid selinux labeling.
1291 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1292 while (path[1] >= '0' && path[1] <= '9') {
1297 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1299 if (rc == -ENOENT) {
1300 /* No match in policy, mark as unlabeled. */
1301 *sid = SECINITSID_UNLABELED;
1305 free_page((unsigned long)buffer);
1309 static int inode_doinit_use_xattr(struct inode *inode, struct dentry *dentry,
1310 u32 def_sid, u32 *sid)
1312 #define INITCONTEXTLEN 255
1317 len = INITCONTEXTLEN;
1318 context = kmalloc(len + 1, GFP_NOFS);
1322 context[len] = '\0';
1323 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
1324 if (rc == -ERANGE) {
1327 /* Need a larger buffer. Query for the right size. */
1328 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
1333 context = kmalloc(len + 1, GFP_NOFS);
1337 context[len] = '\0';
1338 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX,
1343 if (rc != -ENODATA) {
1344 pr_warn("SELinux: %s: getxattr returned %d for dev=%s ino=%ld\n",
1345 __func__, -rc, inode->i_sb->s_id, inode->i_ino);
1352 rc = security_context_to_sid_default(&selinux_state, context, rc, sid,
1355 char *dev = inode->i_sb->s_id;
1356 unsigned long ino = inode->i_ino;
1358 if (rc == -EINVAL) {
1359 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",
1362 pr_warn("SELinux: %s: context_to_sid(%s) returned %d for dev=%s ino=%ld\n",
1363 __func__, context, -rc, dev, ino);
1370 /* The inode's security attributes must be initialized before first use. */
1371 static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1373 struct superblock_security_struct *sbsec = NULL;
1374 struct inode_security_struct *isec = selinux_inode(inode);
1375 u32 task_sid, sid = 0;
1377 struct dentry *dentry;
1380 if (isec->initialized == LABEL_INITIALIZED)
1383 spin_lock(&isec->lock);
1384 if (isec->initialized == LABEL_INITIALIZED)
1387 if (isec->sclass == SECCLASS_FILE)
1388 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1390 sbsec = selinux_superblock(inode->i_sb);
1391 if (!(sbsec->flags & SE_SBINITIALIZED)) {
1392 /* Defer initialization until selinux_complete_init,
1393 after the initial policy is loaded and the security
1394 server is ready to handle calls. */
1395 spin_lock(&sbsec->isec_lock);
1396 if (list_empty(&isec->list))
1397 list_add(&isec->list, &sbsec->isec_head);
1398 spin_unlock(&sbsec->isec_lock);
1402 sclass = isec->sclass;
1403 task_sid = isec->task_sid;
1405 isec->initialized = LABEL_PENDING;
1406 spin_unlock(&isec->lock);
1408 switch (sbsec->behavior) {
1409 case SECURITY_FS_USE_NATIVE:
1411 case SECURITY_FS_USE_XATTR:
1412 if (!(inode->i_opflags & IOP_XATTR)) {
1413 sid = sbsec->def_sid;
1416 /* Need a dentry, since the xattr API requires one.
1417 Life would be simpler if we could just pass the inode. */
1419 /* Called from d_instantiate or d_splice_alias. */
1420 dentry = dget(opt_dentry);
1423 * Called from selinux_complete_init, try to find a dentry.
1424 * Some filesystems really want a connected one, so try
1425 * that first. We could split SECURITY_FS_USE_XATTR in
1426 * two, depending upon that...
1428 dentry = d_find_alias(inode);
1430 dentry = d_find_any_alias(inode);
1434 * this is can be hit on boot when a file is accessed
1435 * before the policy is loaded. When we load policy we
1436 * may find inodes that have no dentry on the
1437 * sbsec->isec_head list. No reason to complain as these
1438 * will get fixed up the next time we go through
1439 * inode_doinit with a dentry, before these inodes could
1440 * be used again by userspace.
1445 rc = inode_doinit_use_xattr(inode, dentry, sbsec->def_sid,
1451 case SECURITY_FS_USE_TASK:
1454 case SECURITY_FS_USE_TRANS:
1455 /* Default to the fs SID. */
1458 /* Try to obtain a transition SID. */
1459 rc = security_transition_sid(&selinux_state, task_sid, sid,
1460 sclass, NULL, &sid);
1464 case SECURITY_FS_USE_MNTPOINT:
1465 sid = sbsec->mntpoint_sid;
1468 /* Default to the fs superblock SID. */
1471 if ((sbsec->flags & SE_SBGENFS) &&
1472 (!S_ISLNK(inode->i_mode) ||
1473 selinux_policycap_genfs_seclabel_symlinks())) {
1474 /* We must have a dentry to determine the label on
1477 /* Called from d_instantiate or
1478 * d_splice_alias. */
1479 dentry = dget(opt_dentry);
1481 /* Called from selinux_complete_init, try to
1482 * find a dentry. Some filesystems really want
1483 * a connected one, so try that first.
1485 dentry = d_find_alias(inode);
1487 dentry = d_find_any_alias(inode);
1490 * This can be hit on boot when a file is accessed
1491 * before the policy is loaded. When we load policy we
1492 * may find inodes that have no dentry on the
1493 * sbsec->isec_head list. No reason to complain as
1494 * these will get fixed up the next time we go through
1495 * inode_doinit() with a dentry, before these inodes
1496 * could be used again by userspace.
1500 rc = selinux_genfs_get_sid(dentry, sclass,
1501 sbsec->flags, &sid);
1507 if ((sbsec->flags & SE_SBGENFS_XATTR) &&
1508 (inode->i_opflags & IOP_XATTR)) {
1509 rc = inode_doinit_use_xattr(inode, dentry,
1522 spin_lock(&isec->lock);
1523 if (isec->initialized == LABEL_PENDING) {
1525 isec->initialized = LABEL_INVALID;
1528 isec->initialized = LABEL_INITIALIZED;
1533 spin_unlock(&isec->lock);
1537 spin_lock(&isec->lock);
1538 if (isec->initialized == LABEL_PENDING) {
1539 isec->initialized = LABEL_INVALID;
1542 spin_unlock(&isec->lock);
1546 /* Convert a Linux signal to an access vector. */
1547 static inline u32 signal_to_av(int sig)
1553 /* Commonly granted from child to parent. */
1554 perm = PROCESS__SIGCHLD;
1557 /* Cannot be caught or ignored */
1558 perm = PROCESS__SIGKILL;
1561 /* Cannot be caught or ignored */
1562 perm = PROCESS__SIGSTOP;
1565 /* All other signals. */
1566 perm = PROCESS__SIGNAL;
1573 #if CAP_LAST_CAP > 63
1574 #error Fix SELinux to handle capabilities > 63.
1577 /* Check whether a task is allowed to use a capability. */
1578 static int cred_has_capability(const struct cred *cred,
1579 int cap, unsigned int opts, bool initns)
1581 struct common_audit_data ad;
1582 struct av_decision avd;
1584 u32 sid = cred_sid(cred);
1585 u32 av = CAP_TO_MASK(cap);
1588 ad.type = LSM_AUDIT_DATA_CAP;
1591 switch (CAP_TO_INDEX(cap)) {
1593 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
1596 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
1599 pr_err("SELinux: out of range capability %d\n", cap);
1604 rc = avc_has_perm_noaudit(&selinux_state,
1605 sid, sid, sclass, av, 0, &avd);
1606 if (!(opts & CAP_OPT_NOAUDIT)) {
1607 int rc2 = avc_audit(&selinux_state,
1608 sid, sid, sclass, av, &avd, rc, &ad);
1615 /* Check whether a task has a particular permission to an inode.
1616 The 'adp' parameter is optional and allows other audit
1617 data to be passed (e.g. the dentry). */
1618 static int inode_has_perm(const struct cred *cred,
1619 struct inode *inode,
1621 struct common_audit_data *adp)
1623 struct inode_security_struct *isec;
1626 validate_creds(cred);
1628 if (unlikely(IS_PRIVATE(inode)))
1631 sid = cred_sid(cred);
1632 isec = selinux_inode(inode);
1634 return avc_has_perm(&selinux_state,
1635 sid, isec->sid, isec->sclass, perms, adp);
1638 /* Same as inode_has_perm, but pass explicit audit data containing
1639 the dentry to help the auditing code to more easily generate the
1640 pathname if needed. */
1641 static inline int dentry_has_perm(const struct cred *cred,
1642 struct dentry *dentry,
1645 struct inode *inode = d_backing_inode(dentry);
1646 struct common_audit_data ad;
1648 ad.type = LSM_AUDIT_DATA_DENTRY;
1649 ad.u.dentry = dentry;
1650 __inode_security_revalidate(inode, dentry, true);
1651 return inode_has_perm(cred, inode, av, &ad);
1654 /* Same as inode_has_perm, but pass explicit audit data containing
1655 the path to help the auditing code to more easily generate the
1656 pathname if needed. */
1657 static inline int path_has_perm(const struct cred *cred,
1658 const struct path *path,
1661 struct inode *inode = d_backing_inode(path->dentry);
1662 struct common_audit_data ad;
1664 ad.type = LSM_AUDIT_DATA_PATH;
1666 __inode_security_revalidate(inode, path->dentry, true);
1667 return inode_has_perm(cred, inode, av, &ad);
1670 /* Same as path_has_perm, but uses the inode from the file struct. */
1671 static inline int file_path_has_perm(const struct cred *cred,
1675 struct common_audit_data ad;
1677 ad.type = LSM_AUDIT_DATA_FILE;
1679 return inode_has_perm(cred, file_inode(file), av, &ad);
1682 #ifdef CONFIG_BPF_SYSCALL
1683 static int bpf_fd_pass(struct file *file, u32 sid);
1686 /* Check whether a task can use an open file descriptor to
1687 access an inode in a given way. Check access to the
1688 descriptor itself, and then use dentry_has_perm to
1689 check a particular permission to the file.
1690 Access to the descriptor is implicitly granted if it
1691 has the same SID as the process. If av is zero, then
1692 access to the file is not checked, e.g. for cases
1693 where only the descriptor is affected like seek. */
1694 static int file_has_perm(const struct cred *cred,
1698 struct file_security_struct *fsec = selinux_file(file);
1699 struct inode *inode = file_inode(file);
1700 struct common_audit_data ad;
1701 u32 sid = cred_sid(cred);
1704 ad.type = LSM_AUDIT_DATA_FILE;
1707 if (sid != fsec->sid) {
1708 rc = avc_has_perm(&selinux_state,
1717 #ifdef CONFIG_BPF_SYSCALL
1718 rc = bpf_fd_pass(file, cred_sid(cred));
1723 /* av is zero if only checking access to the descriptor. */
1726 rc = inode_has_perm(cred, inode, av, &ad);
1733 * Determine the label for an inode that might be unioned.
1736 selinux_determine_inode_label(const struct task_security_struct *tsec,
1738 const struct qstr *name, u16 tclass,
1741 const struct superblock_security_struct *sbsec =
1742 selinux_superblock(dir->i_sb);
1744 if ((sbsec->flags & SE_SBINITIALIZED) &&
1745 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1746 *_new_isid = sbsec->mntpoint_sid;
1747 } else if ((sbsec->flags & SBLABEL_MNT) &&
1749 *_new_isid = tsec->create_sid;
1751 const struct inode_security_struct *dsec = inode_security(dir);
1752 return security_transition_sid(&selinux_state, tsec->sid,
1760 /* Check whether a task can create a file. */
1761 static int may_create(struct inode *dir,
1762 struct dentry *dentry,
1765 const struct task_security_struct *tsec = selinux_cred(current_cred());
1766 struct inode_security_struct *dsec;
1767 struct superblock_security_struct *sbsec;
1769 struct common_audit_data ad;
1772 dsec = inode_security(dir);
1773 sbsec = selinux_superblock(dir->i_sb);
1777 ad.type = LSM_AUDIT_DATA_DENTRY;
1778 ad.u.dentry = dentry;
1780 rc = avc_has_perm(&selinux_state,
1781 sid, dsec->sid, SECCLASS_DIR,
1782 DIR__ADD_NAME | DIR__SEARCH,
1787 rc = selinux_determine_inode_label(tsec, dir, &dentry->d_name, tclass,
1792 rc = avc_has_perm(&selinux_state,
1793 sid, newsid, tclass, FILE__CREATE, &ad);
1797 return avc_has_perm(&selinux_state,
1799 SECCLASS_FILESYSTEM,
1800 FILESYSTEM__ASSOCIATE, &ad);
1804 #define MAY_UNLINK 1
1807 /* Check whether a task can link, unlink, or rmdir a file/directory. */
1808 static int may_link(struct inode *dir,
1809 struct dentry *dentry,
1813 struct inode_security_struct *dsec, *isec;
1814 struct common_audit_data ad;
1815 u32 sid = current_sid();
1819 dsec = inode_security(dir);
1820 isec = backing_inode_security(dentry);
1822 ad.type = LSM_AUDIT_DATA_DENTRY;
1823 ad.u.dentry = dentry;
1826 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
1827 rc = avc_has_perm(&selinux_state,
1828 sid, dsec->sid, SECCLASS_DIR, av, &ad);
1843 pr_warn("SELinux: %s: unrecognized kind %d\n",
1848 rc = avc_has_perm(&selinux_state,
1849 sid, isec->sid, isec->sclass, av, &ad);
1853 static inline int may_rename(struct inode *old_dir,
1854 struct dentry *old_dentry,
1855 struct inode *new_dir,
1856 struct dentry *new_dentry)
1858 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
1859 struct common_audit_data ad;
1860 u32 sid = current_sid();
1862 int old_is_dir, new_is_dir;
1865 old_dsec = inode_security(old_dir);
1866 old_isec = backing_inode_security(old_dentry);
1867 old_is_dir = d_is_dir(old_dentry);
1868 new_dsec = inode_security(new_dir);
1870 ad.type = LSM_AUDIT_DATA_DENTRY;
1872 ad.u.dentry = old_dentry;
1873 rc = avc_has_perm(&selinux_state,
1874 sid, old_dsec->sid, SECCLASS_DIR,
1875 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1878 rc = avc_has_perm(&selinux_state,
1880 old_isec->sclass, FILE__RENAME, &ad);
1883 if (old_is_dir && new_dir != old_dir) {
1884 rc = avc_has_perm(&selinux_state,
1886 old_isec->sclass, DIR__REPARENT, &ad);
1891 ad.u.dentry = new_dentry;
1892 av = DIR__ADD_NAME | DIR__SEARCH;
1893 if (d_is_positive(new_dentry))
1894 av |= DIR__REMOVE_NAME;
1895 rc = avc_has_perm(&selinux_state,
1896 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
1899 if (d_is_positive(new_dentry)) {
1900 new_isec = backing_inode_security(new_dentry);
1901 new_is_dir = d_is_dir(new_dentry);
1902 rc = avc_has_perm(&selinux_state,
1905 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1913 /* Check whether a task can perform a filesystem operation. */
1914 static int superblock_has_perm(const struct cred *cred,
1915 struct super_block *sb,
1917 struct common_audit_data *ad)
1919 struct superblock_security_struct *sbsec;
1920 u32 sid = cred_sid(cred);
1922 sbsec = selinux_superblock(sb);
1923 return avc_has_perm(&selinux_state,
1924 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
1927 /* Convert a Linux mode and permission mask to an access vector. */
1928 static inline u32 file_mask_to_av(int mode, int mask)
1932 if (!S_ISDIR(mode)) {
1933 if (mask & MAY_EXEC)
1934 av |= FILE__EXECUTE;
1935 if (mask & MAY_READ)
1938 if (mask & MAY_APPEND)
1940 else if (mask & MAY_WRITE)
1944 if (mask & MAY_EXEC)
1946 if (mask & MAY_WRITE)
1948 if (mask & MAY_READ)
1955 /* Convert a Linux file to an access vector. */
1956 static inline u32 file_to_av(struct file *file)
1960 if (file->f_mode & FMODE_READ)
1962 if (file->f_mode & FMODE_WRITE) {
1963 if (file->f_flags & O_APPEND)
1970 * Special file opened with flags 3 for ioctl-only use.
1979 * Convert a file to an access vector and include the correct
1982 static inline u32 open_file_to_av(struct file *file)
1984 u32 av = file_to_av(file);
1985 struct inode *inode = file_inode(file);
1987 if (selinux_policycap_openperm() &&
1988 inode->i_sb->s_magic != SOCKFS_MAGIC)
1994 /* Hook functions begin here. */
1996 static int selinux_binder_set_context_mgr(const struct cred *mgr)
1998 return avc_has_perm(&selinux_state,
1999 current_sid(), cred_sid(mgr), SECCLASS_BINDER,
2000 BINDER__SET_CONTEXT_MGR, NULL);
2003 static int selinux_binder_transaction(const struct cred *from,
2004 const struct cred *to)
2006 u32 mysid = current_sid();
2007 u32 fromsid = cred_sid(from);
2008 u32 tosid = cred_sid(to);
2011 if (mysid != fromsid) {
2012 rc = avc_has_perm(&selinux_state,
2013 mysid, fromsid, SECCLASS_BINDER,
2014 BINDER__IMPERSONATE, NULL);
2019 return avc_has_perm(&selinux_state, fromsid, tosid,
2020 SECCLASS_BINDER, BINDER__CALL, NULL);
2023 static int selinux_binder_transfer_binder(const struct cred *from,
2024 const struct cred *to)
2026 return avc_has_perm(&selinux_state,
2027 cred_sid(from), cred_sid(to),
2028 SECCLASS_BINDER, BINDER__TRANSFER,
2032 static int selinux_binder_transfer_file(const struct cred *from,
2033 const struct cred *to,
2036 u32 sid = cred_sid(to);
2037 struct file_security_struct *fsec = selinux_file(file);
2038 struct dentry *dentry = file->f_path.dentry;
2039 struct inode_security_struct *isec;
2040 struct common_audit_data ad;
2043 ad.type = LSM_AUDIT_DATA_PATH;
2044 ad.u.path = file->f_path;
2046 if (sid != fsec->sid) {
2047 rc = avc_has_perm(&selinux_state,
2056 #ifdef CONFIG_BPF_SYSCALL
2057 rc = bpf_fd_pass(file, sid);
2062 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2065 isec = backing_inode_security(dentry);
2066 return avc_has_perm(&selinux_state,
2067 sid, isec->sid, isec->sclass, file_to_av(file),
2071 static int selinux_ptrace_access_check(struct task_struct *child,
2074 u32 sid = current_sid();
2075 u32 csid = task_sid_obj(child);
2077 if (mode & PTRACE_MODE_READ)
2078 return avc_has_perm(&selinux_state,
2079 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
2081 return avc_has_perm(&selinux_state,
2082 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
2085 static int selinux_ptrace_traceme(struct task_struct *parent)
2087 return avc_has_perm(&selinux_state,
2088 task_sid_obj(parent), task_sid_obj(current),
2089 SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
2092 static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
2093 kernel_cap_t *inheritable, kernel_cap_t *permitted)
2095 return avc_has_perm(&selinux_state,
2096 current_sid(), task_sid_obj(target), SECCLASS_PROCESS,
2097 PROCESS__GETCAP, NULL);
2100 static int selinux_capset(struct cred *new, const struct cred *old,
2101 const kernel_cap_t *effective,
2102 const kernel_cap_t *inheritable,
2103 const kernel_cap_t *permitted)
2105 return avc_has_perm(&selinux_state,
2106 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
2107 PROCESS__SETCAP, NULL);
2111 * (This comment used to live with the selinux_task_setuid hook,
2112 * which was removed).
2114 * Since setuid only affects the current process, and since the SELinux
2115 * controls are not based on the Linux identity attributes, SELinux does not
2116 * need to control this operation. However, SELinux does control the use of
2117 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2120 static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2121 int cap, unsigned int opts)
2123 return cred_has_capability(cred, cap, opts, ns == &init_user_ns);
2126 static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2128 const struct cred *cred = current_cred();
2143 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
2151 case Q_XGETNEXTQUOTA:
2152 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
2155 rc = 0; /* let the kernel handle invalid cmds */
2161 static int selinux_quota_on(struct dentry *dentry)
2163 const struct cred *cred = current_cred();
2165 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
2168 static int selinux_syslog(int type)
2171 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2172 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
2173 return avc_has_perm(&selinux_state,
2174 current_sid(), SECINITSID_KERNEL,
2175 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
2176 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2177 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2178 /* Set level of messages printed to console */
2179 case SYSLOG_ACTION_CONSOLE_LEVEL:
2180 return avc_has_perm(&selinux_state,
2181 current_sid(), SECINITSID_KERNEL,
2182 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2185 /* All other syslog types */
2186 return avc_has_perm(&selinux_state,
2187 current_sid(), SECINITSID_KERNEL,
2188 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
2192 * Check that a process has enough memory to allocate a new virtual
2193 * mapping. 0 means there is enough memory for the allocation to
2194 * succeed and -ENOMEM implies there is not.
2196 * Do not audit the selinux permission check, as this is applied to all
2197 * processes that allocate mappings.
2199 static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
2201 int rc, cap_sys_admin = 0;
2203 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
2204 CAP_OPT_NOAUDIT, true);
2208 return cap_sys_admin;
2211 /* binprm security operations */
2213 static u32 ptrace_parent_sid(void)
2216 struct task_struct *tracer;
2219 tracer = ptrace_parent(current);
2221 sid = task_sid_obj(tracer);
2227 static int check_nnp_nosuid(const struct linux_binprm *bprm,
2228 const struct task_security_struct *old_tsec,
2229 const struct task_security_struct *new_tsec)
2231 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
2232 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
2236 if (!nnp && !nosuid)
2237 return 0; /* neither NNP nor nosuid */
2239 if (new_tsec->sid == old_tsec->sid)
2240 return 0; /* No change in credentials */
2243 * If the policy enables the nnp_nosuid_transition policy capability,
2244 * then we permit transitions under NNP or nosuid if the
2245 * policy allows the corresponding permission between
2246 * the old and new contexts.
2248 if (selinux_policycap_nnp_nosuid_transition()) {
2251 av |= PROCESS2__NNP_TRANSITION;
2253 av |= PROCESS2__NOSUID_TRANSITION;
2254 rc = avc_has_perm(&selinux_state,
2255 old_tsec->sid, new_tsec->sid,
2256 SECCLASS_PROCESS2, av, NULL);
2262 * We also permit NNP or nosuid transitions to bounded SIDs,
2263 * i.e. SIDs that are guaranteed to only be allowed a subset
2264 * of the permissions of the current SID.
2266 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2272 * On failure, preserve the errno values for NNP vs nosuid.
2273 * NNP: Operation not permitted for caller.
2274 * nosuid: Permission denied to file.
2281 static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
2283 const struct task_security_struct *old_tsec;
2284 struct task_security_struct *new_tsec;
2285 struct inode_security_struct *isec;
2286 struct common_audit_data ad;
2287 struct inode *inode = file_inode(bprm->file);
2290 /* SELinux context only depends on initial program or script and not
2291 * the script interpreter */
2293 old_tsec = selinux_cred(current_cred());
2294 new_tsec = selinux_cred(bprm->cred);
2295 isec = inode_security(inode);
2297 /* Default to the current task SID. */
2298 new_tsec->sid = old_tsec->sid;
2299 new_tsec->osid = old_tsec->sid;
2301 /* Reset fs, key, and sock SIDs on execve. */
2302 new_tsec->create_sid = 0;
2303 new_tsec->keycreate_sid = 0;
2304 new_tsec->sockcreate_sid = 0;
2306 if (old_tsec->exec_sid) {
2307 new_tsec->sid = old_tsec->exec_sid;
2308 /* Reset exec SID on execve. */
2309 new_tsec->exec_sid = 0;
2311 /* Fail on NNP or nosuid if not an allowed transition. */
2312 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2316 /* Check for a default transition on this program. */
2317 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2318 isec->sid, SECCLASS_PROCESS, NULL,
2324 * Fallback to old SID on NNP or nosuid if not an allowed
2327 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2329 new_tsec->sid = old_tsec->sid;
2332 ad.type = LSM_AUDIT_DATA_FILE;
2333 ad.u.file = bprm->file;
2335 if (new_tsec->sid == old_tsec->sid) {
2336 rc = avc_has_perm(&selinux_state,
2337 old_tsec->sid, isec->sid,
2338 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2342 /* Check permissions for the transition. */
2343 rc = avc_has_perm(&selinux_state,
2344 old_tsec->sid, new_tsec->sid,
2345 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2349 rc = avc_has_perm(&selinux_state,
2350 new_tsec->sid, isec->sid,
2351 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2355 /* Check for shared state */
2356 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2357 rc = avc_has_perm(&selinux_state,
2358 old_tsec->sid, new_tsec->sid,
2359 SECCLASS_PROCESS, PROCESS__SHARE,
2365 /* Make sure that anyone attempting to ptrace over a task that
2366 * changes its SID has the appropriate permit */
2367 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
2368 u32 ptsid = ptrace_parent_sid();
2370 rc = avc_has_perm(&selinux_state,
2371 ptsid, new_tsec->sid,
2373 PROCESS__PTRACE, NULL);
2379 /* Clear any possibly unsafe personality bits on exec: */
2380 bprm->per_clear |= PER_CLEAR_ON_SETID;
2382 /* Enable secure mode for SIDs transitions unless
2383 the noatsecure permission is granted between
2384 the two SIDs, i.e. ahp returns 0. */
2385 rc = avc_has_perm(&selinux_state,
2386 old_tsec->sid, new_tsec->sid,
2387 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2389 bprm->secureexec |= !!rc;
2395 static int match_file(const void *p, struct file *file, unsigned fd)
2397 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2400 /* Derived from fs/exec.c:flush_old_files. */
2401 static inline void flush_unauthorized_files(const struct cred *cred,
2402 struct files_struct *files)
2404 struct file *file, *devnull = NULL;
2405 struct tty_struct *tty;
2409 tty = get_current_tty();
2411 spin_lock(&tty->files_lock);
2412 if (!list_empty(&tty->tty_files)) {
2413 struct tty_file_private *file_priv;
2415 /* Revalidate access to controlling tty.
2416 Use file_path_has_perm on the tty path directly
2417 rather than using file_has_perm, as this particular
2418 open file may belong to another process and we are
2419 only interested in the inode-based check here. */
2420 file_priv = list_first_entry(&tty->tty_files,
2421 struct tty_file_private, list);
2422 file = file_priv->file;
2423 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
2426 spin_unlock(&tty->files_lock);
2429 /* Reset controlling tty. */
2433 /* Revalidate access to inherited open files. */
2434 n = iterate_fd(files, 0, match_file, cred);
2435 if (!n) /* none found? */
2438 devnull = dentry_open(&selinux_null, O_RDWR, cred);
2439 if (IS_ERR(devnull))
2441 /* replace all the matching ones with this */
2443 replace_fd(n - 1, devnull, 0);
2444 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2450 * Prepare a process for imminent new credential changes due to exec
2452 static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
2454 struct task_security_struct *new_tsec;
2455 struct rlimit *rlim, *initrlim;
2458 new_tsec = selinux_cred(bprm->cred);
2459 if (new_tsec->sid == new_tsec->osid)
2462 /* Close files for which the new task SID is not authorized. */
2463 flush_unauthorized_files(bprm->cred, current->files);
2465 /* Always clear parent death signal on SID transitions. */
2466 current->pdeath_signal = 0;
2468 /* Check whether the new SID can inherit resource limits from the old
2469 * SID. If not, reset all soft limits to the lower of the current
2470 * task's hard limit and the init task's soft limit.
2472 * Note that the setting of hard limits (even to lower them) can be
2473 * controlled by the setrlimit check. The inclusion of the init task's
2474 * soft limit into the computation is to avoid resetting soft limits
2475 * higher than the default soft limit for cases where the default is
2476 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2478 rc = avc_has_perm(&selinux_state,
2479 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2480 PROCESS__RLIMITINH, NULL);
2482 /* protect against do_prlimit() */
2484 for (i = 0; i < RLIM_NLIMITS; i++) {
2485 rlim = current->signal->rlim + i;
2486 initrlim = init_task.signal->rlim + i;
2487 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2489 task_unlock(current);
2490 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2491 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
2496 * Clean up the process immediately after the installation of new credentials
2499 static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2501 const struct task_security_struct *tsec = selinux_cred(current_cred());
2511 /* Check whether the new SID can inherit signal state from the old SID.
2512 * If not, clear itimers to avoid subsequent signal generation and
2513 * flush and unblock signals.
2515 * This must occur _after_ the task SID has been updated so that any
2516 * kill done after the flush will be checked against the new SID.
2518 rc = avc_has_perm(&selinux_state,
2519 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
2523 spin_lock_irq(&unrcu_pointer(current->sighand)->siglock);
2524 if (!fatal_signal_pending(current)) {
2525 flush_sigqueue(¤t->pending);
2526 flush_sigqueue(¤t->signal->shared_pending);
2527 flush_signal_handlers(current, 1);
2528 sigemptyset(¤t->blocked);
2529 recalc_sigpending();
2531 spin_unlock_irq(&unrcu_pointer(current->sighand)->siglock);
2534 /* Wake up the parent if it is waiting so that it can recheck
2535 * wait permission to the new task SID. */
2536 read_lock(&tasklist_lock);
2537 __wake_up_parent(current, unrcu_pointer(current->real_parent));
2538 read_unlock(&tasklist_lock);
2541 /* superblock security operations */
2543 static int selinux_sb_alloc_security(struct super_block *sb)
2545 struct superblock_security_struct *sbsec = selinux_superblock(sb);
2547 mutex_init(&sbsec->lock);
2548 INIT_LIST_HEAD(&sbsec->isec_head);
2549 spin_lock_init(&sbsec->isec_lock);
2550 sbsec->sid = SECINITSID_UNLABELED;
2551 sbsec->def_sid = SECINITSID_FILE;
2552 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
2557 static inline int opt_len(const char *s)
2559 bool open_quote = false;
2563 for (len = 0; (c = s[len]) != '\0'; len++) {
2565 open_quote = !open_quote;
2566 if (c == ',' && !open_quote)
2572 static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
2574 char *from = options;
2580 int len = opt_len(from);
2584 token = match_opt_prefix(from, len, &arg);
2586 if (token != Opt_error) {
2591 for (p = q = arg; p < from + len; p++) {
2596 arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
2602 rc = selinux_add_opt(token, arg, mnt_opts);
2608 if (!first) { // copy with preceding comma
2613 memmove(to, from, len);
2626 selinux_free_mnt_opts(*mnt_opts);
2632 static int selinux_sb_mnt_opts_compat(struct super_block *sb, void *mnt_opts)
2634 struct selinux_mnt_opts *opts = mnt_opts;
2635 struct superblock_security_struct *sbsec = selinux_superblock(sb);
2638 * Superblock not initialized (i.e. no options) - reject if any
2639 * options specified, otherwise accept.
2641 if (!(sbsec->flags & SE_SBINITIALIZED))
2642 return opts ? 1 : 0;
2645 * Superblock initialized and no options specified - reject if
2646 * superblock has any options set, otherwise accept.
2649 return (sbsec->flags & SE_MNTMASK) ? 1 : 0;
2651 if (opts->fscontext_sid) {
2652 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
2653 opts->fscontext_sid))
2656 if (opts->context_sid) {
2657 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
2661 if (opts->rootcontext_sid) {
2662 struct inode_security_struct *root_isec;
2664 root_isec = backing_inode_security(sb->s_root);
2665 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
2666 opts->rootcontext_sid))
2669 if (opts->defcontext_sid) {
2670 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
2671 opts->defcontext_sid))
2677 static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
2679 struct selinux_mnt_opts *opts = mnt_opts;
2680 struct superblock_security_struct *sbsec = selinux_superblock(sb);
2682 if (!(sbsec->flags & SE_SBINITIALIZED))
2688 if (opts->fscontext_sid) {
2689 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
2690 opts->fscontext_sid))
2691 goto out_bad_option;
2693 if (opts->context_sid) {
2694 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
2696 goto out_bad_option;
2698 if (opts->rootcontext_sid) {
2699 struct inode_security_struct *root_isec;
2700 root_isec = backing_inode_security(sb->s_root);
2701 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
2702 opts->rootcontext_sid))
2703 goto out_bad_option;
2705 if (opts->defcontext_sid) {
2706 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
2707 opts->defcontext_sid))
2708 goto out_bad_option;
2713 pr_warn("SELinux: unable to change security options "
2714 "during remount (dev %s, type=%s)\n", sb->s_id,
2719 static int selinux_sb_kern_mount(struct super_block *sb)
2721 const struct cred *cred = current_cred();
2722 struct common_audit_data ad;
2724 ad.type = LSM_AUDIT_DATA_DENTRY;
2725 ad.u.dentry = sb->s_root;
2726 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
2729 static int selinux_sb_statfs(struct dentry *dentry)
2731 const struct cred *cred = current_cred();
2732 struct common_audit_data ad;
2734 ad.type = LSM_AUDIT_DATA_DENTRY;
2735 ad.u.dentry = dentry->d_sb->s_root;
2736 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
2739 static int selinux_mount(const char *dev_name,
2740 const struct path *path,
2742 unsigned long flags,
2745 const struct cred *cred = current_cred();
2747 if (flags & MS_REMOUNT)
2748 return superblock_has_perm(cred, path->dentry->d_sb,
2749 FILESYSTEM__REMOUNT, NULL);
2751 return path_has_perm(cred, path, FILE__MOUNTON);
2754 static int selinux_move_mount(const struct path *from_path,
2755 const struct path *to_path)
2757 const struct cred *cred = current_cred();
2759 return path_has_perm(cred, to_path, FILE__MOUNTON);
2762 static int selinux_umount(struct vfsmount *mnt, int flags)
2764 const struct cred *cred = current_cred();
2766 return superblock_has_perm(cred, mnt->mnt_sb,
2767 FILESYSTEM__UNMOUNT, NULL);
2770 static int selinux_fs_context_dup(struct fs_context *fc,
2771 struct fs_context *src_fc)
2773 const struct selinux_mnt_opts *src = src_fc->security;
2778 fc->security = kmemdup(src, sizeof(*src), GFP_KERNEL);
2779 return fc->security ? 0 : -ENOMEM;
2782 static const struct fs_parameter_spec selinux_fs_parameters[] = {
2783 fsparam_string(CONTEXT_STR, Opt_context),
2784 fsparam_string(DEFCONTEXT_STR, Opt_defcontext),
2785 fsparam_string(FSCONTEXT_STR, Opt_fscontext),
2786 fsparam_string(ROOTCONTEXT_STR, Opt_rootcontext),
2787 fsparam_flag (SECLABEL_STR, Opt_seclabel),
2791 static int selinux_fs_context_parse_param(struct fs_context *fc,
2792 struct fs_parameter *param)
2794 struct fs_parse_result result;
2797 opt = fs_parse(fc, selinux_fs_parameters, param, &result);
2801 rc = selinux_add_opt(opt, param->string, &fc->security);
2803 param->string = NULL;
2808 /* inode security operations */
2810 static int selinux_inode_alloc_security(struct inode *inode)
2812 struct inode_security_struct *isec = selinux_inode(inode);
2813 u32 sid = current_sid();
2815 spin_lock_init(&isec->lock);
2816 INIT_LIST_HEAD(&isec->list);
2817 isec->inode = inode;
2818 isec->sid = SECINITSID_UNLABELED;
2819 isec->sclass = SECCLASS_FILE;
2820 isec->task_sid = sid;
2821 isec->initialized = LABEL_INVALID;
2826 static void selinux_inode_free_security(struct inode *inode)
2828 inode_free_security(inode);
2831 static int selinux_dentry_init_security(struct dentry *dentry, int mode,
2832 const struct qstr *name,
2833 const char **xattr_name, void **ctx,
2839 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
2840 d_inode(dentry->d_parent), name,
2841 inode_mode_to_security_class(mode),
2847 *xattr_name = XATTR_NAME_SELINUX;
2849 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2853 static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2855 const struct cred *old,
2860 struct task_security_struct *tsec;
2862 rc = selinux_determine_inode_label(selinux_cred(old),
2863 d_inode(dentry->d_parent), name,
2864 inode_mode_to_security_class(mode),
2869 tsec = selinux_cred(new);
2870 tsec->create_sid = newsid;
2874 static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
2875 const struct qstr *qstr,
2877 void **value, size_t *len)
2879 const struct task_security_struct *tsec = selinux_cred(current_cred());
2880 struct superblock_security_struct *sbsec;
2885 sbsec = selinux_superblock(dir->i_sb);
2887 newsid = tsec->create_sid;
2889 rc = selinux_determine_inode_label(tsec, dir, qstr,
2890 inode_mode_to_security_class(inode->i_mode),
2895 /* Possibly defer initialization to selinux_complete_init. */
2896 if (sbsec->flags & SE_SBINITIALIZED) {
2897 struct inode_security_struct *isec = selinux_inode(inode);
2898 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2900 isec->initialized = LABEL_INITIALIZED;
2903 if (!selinux_initialized(&selinux_state) ||
2904 !(sbsec->flags & SBLABEL_MNT))
2908 *name = XATTR_SELINUX_SUFFIX;
2911 rc = security_sid_to_context_force(&selinux_state, newsid,
2922 static int selinux_inode_init_security_anon(struct inode *inode,
2923 const struct qstr *name,
2924 const struct inode *context_inode)
2926 const struct task_security_struct *tsec = selinux_cred(current_cred());
2927 struct common_audit_data ad;
2928 struct inode_security_struct *isec;
2931 if (unlikely(!selinux_initialized(&selinux_state)))
2934 isec = selinux_inode(inode);
2937 * We only get here once per ephemeral inode. The inode has
2938 * been initialized via inode_alloc_security but is otherwise
2942 if (context_inode) {
2943 struct inode_security_struct *context_isec =
2944 selinux_inode(context_inode);
2945 if (context_isec->initialized != LABEL_INITIALIZED) {
2946 pr_err("SELinux: context_inode is not initialized");
2950 isec->sclass = context_isec->sclass;
2951 isec->sid = context_isec->sid;
2953 isec->sclass = SECCLASS_ANON_INODE;
2954 rc = security_transition_sid(
2955 &selinux_state, tsec->sid, tsec->sid,
2956 isec->sclass, name, &isec->sid);
2961 isec->initialized = LABEL_INITIALIZED;
2963 * Now that we've initialized security, check whether we're
2964 * allowed to actually create this type of anonymous inode.
2967 ad.type = LSM_AUDIT_DATA_INODE;
2970 return avc_has_perm(&selinux_state,
2978 static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
2980 return may_create(dir, dentry, SECCLASS_FILE);
2983 static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2985 return may_link(dir, old_dentry, MAY_LINK);
2988 static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2990 return may_link(dir, dentry, MAY_UNLINK);
2993 static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2995 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2998 static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
3000 return may_create(dir, dentry, SECCLASS_DIR);
3003 static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
3005 return may_link(dir, dentry, MAY_RMDIR);
3008 static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3010 return may_create(dir, dentry, inode_mode_to_security_class(mode));
3013 static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
3014 struct inode *new_inode, struct dentry *new_dentry)
3016 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
3019 static int selinux_inode_readlink(struct dentry *dentry)
3021 const struct cred *cred = current_cred();
3023 return dentry_has_perm(cred, dentry, FILE__READ);
3026 static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
3029 const struct cred *cred = current_cred();
3030 struct common_audit_data ad;
3031 struct inode_security_struct *isec;
3034 validate_creds(cred);
3036 ad.type = LSM_AUDIT_DATA_DENTRY;
3037 ad.u.dentry = dentry;
3038 sid = cred_sid(cred);
3039 isec = inode_security_rcu(inode, rcu);
3041 return PTR_ERR(isec);
3043 return avc_has_perm(&selinux_state,
3044 sid, isec->sid, isec->sclass, FILE__READ, &ad);
3047 static noinline int audit_inode_permission(struct inode *inode,
3048 u32 perms, u32 audited, u32 denied,
3051 struct common_audit_data ad;
3052 struct inode_security_struct *isec = selinux_inode(inode);
3054 ad.type = LSM_AUDIT_DATA_INODE;
3057 return slow_avc_audit(&selinux_state,
3058 current_sid(), isec->sid, isec->sclass, perms,
3059 audited, denied, result, &ad);
3062 static int selinux_inode_permission(struct inode *inode, int mask)
3064 const struct cred *cred = current_cred();
3067 bool no_block = mask & MAY_NOT_BLOCK;
3068 struct inode_security_struct *isec;
3070 struct av_decision avd;
3072 u32 audited, denied;
3074 from_access = mask & MAY_ACCESS;
3075 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3077 /* No permission to check. Existence test. */
3081 validate_creds(cred);
3083 if (unlikely(IS_PRIVATE(inode)))
3086 perms = file_mask_to_av(inode->i_mode, mask);
3088 sid = cred_sid(cred);
3089 isec = inode_security_rcu(inode, no_block);
3091 return PTR_ERR(isec);
3093 rc = avc_has_perm_noaudit(&selinux_state,
3094 sid, isec->sid, isec->sclass, perms, 0,
3096 audited = avc_audit_required(perms, &avd, rc,
3097 from_access ? FILE__AUDIT_ACCESS : 0,
3099 if (likely(!audited))
3102 rc2 = audit_inode_permission(inode, perms, audited, denied, rc);
3108 static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3110 const struct cred *cred = current_cred();
3111 struct inode *inode = d_backing_inode(dentry);
3112 unsigned int ia_valid = iattr->ia_valid;
3113 __u32 av = FILE__WRITE;
3115 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3116 if (ia_valid & ATTR_FORCE) {
3117 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3123 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3124 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
3125 return dentry_has_perm(cred, dentry, FILE__SETATTR);
3127 if (selinux_policycap_openperm() &&
3128 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3129 (ia_valid & ATTR_SIZE) &&
3130 !(ia_valid & ATTR_FILE))
3133 return dentry_has_perm(cred, dentry, av);
3136 static int selinux_inode_getattr(const struct path *path)
3138 return path_has_perm(current_cred(), path, FILE__GETATTR);
3141 static bool has_cap_mac_admin(bool audit)
3143 const struct cred *cred = current_cred();
3144 unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT;
3146 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts))
3148 if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true))
3153 static int selinux_inode_setxattr(struct user_namespace *mnt_userns,
3154 struct dentry *dentry, const char *name,
3155 const void *value, size_t size, int flags)
3157 struct inode *inode = d_backing_inode(dentry);
3158 struct inode_security_struct *isec;
3159 struct superblock_security_struct *sbsec;
3160 struct common_audit_data ad;
3161 u32 newsid, sid = current_sid();
3164 if (strcmp(name, XATTR_NAME_SELINUX)) {
3165 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3169 /* Not an attribute we recognize, so just check the
3170 ordinary setattr permission. */
3171 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3174 if (!selinux_initialized(&selinux_state))
3175 return (inode_owner_or_capable(mnt_userns, inode) ? 0 : -EPERM);
3177 sbsec = selinux_superblock(inode->i_sb);
3178 if (!(sbsec->flags & SBLABEL_MNT))
3181 if (!inode_owner_or_capable(mnt_userns, inode))
3184 ad.type = LSM_AUDIT_DATA_DENTRY;
3185 ad.u.dentry = dentry;
3187 isec = backing_inode_security(dentry);
3188 rc = avc_has_perm(&selinux_state,
3189 sid, isec->sid, isec->sclass,
3190 FILE__RELABELFROM, &ad);
3194 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3196 if (rc == -EINVAL) {
3197 if (!has_cap_mac_admin(true)) {
3198 struct audit_buffer *ab;
3201 /* We strip a nul only if it is at the end, otherwise the
3202 * context contains a nul and we should audit that */
3204 const char *str = value;
3206 if (str[size - 1] == '\0')
3207 audit_size = size - 1;
3213 ab = audit_log_start(audit_context(),
3214 GFP_ATOMIC, AUDIT_SELINUX_ERR);
3217 audit_log_format(ab, "op=setxattr invalid_context=");
3218 audit_log_n_untrustedstring(ab, value, audit_size);
3223 rc = security_context_to_sid_force(&selinux_state, value,
3229 rc = avc_has_perm(&selinux_state,
3230 sid, newsid, isec->sclass,
3231 FILE__RELABELTO, &ad);
3235 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3240 return avc_has_perm(&selinux_state,
3243 SECCLASS_FILESYSTEM,
3244 FILESYSTEM__ASSOCIATE,
3248 static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
3249 const void *value, size_t size,
3252 struct inode *inode = d_backing_inode(dentry);
3253 struct inode_security_struct *isec;
3257 if (strcmp(name, XATTR_NAME_SELINUX)) {
3258 /* Not an attribute we recognize, so nothing to do. */
3262 if (!selinux_initialized(&selinux_state)) {
3263 /* If we haven't even been initialized, then we can't validate
3264 * against a policy, so leave the label as invalid. It may
3265 * resolve to a valid label on the next revalidation try if
3266 * we've since initialized.
3271 rc = security_context_to_sid_force(&selinux_state, value, size,
3274 pr_err("SELinux: unable to map context to SID"
3275 "for (%s, %lu), rc=%d\n",
3276 inode->i_sb->s_id, inode->i_ino, -rc);
3280 isec = backing_inode_security(dentry);
3281 spin_lock(&isec->lock);
3282 isec->sclass = inode_mode_to_security_class(inode->i_mode);
3284 isec->initialized = LABEL_INITIALIZED;
3285 spin_unlock(&isec->lock);
3288 static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
3290 const struct cred *cred = current_cred();
3292 return dentry_has_perm(cred, dentry, FILE__GETATTR);
3295 static int selinux_inode_listxattr(struct dentry *dentry)
3297 const struct cred *cred = current_cred();
3299 return dentry_has_perm(cred, dentry, FILE__GETATTR);
3302 static int selinux_inode_removexattr(struct user_namespace *mnt_userns,
3303 struct dentry *dentry, const char *name)
3305 if (strcmp(name, XATTR_NAME_SELINUX)) {
3306 int rc = cap_inode_removexattr(mnt_userns, dentry, name);
3310 /* Not an attribute we recognize, so just check the
3311 ordinary setattr permission. */
3312 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3315 if (!selinux_initialized(&selinux_state))
3318 /* No one is allowed to remove a SELinux security label.
3319 You can change the label, but all data must be labeled. */
3323 static int selinux_path_notify(const struct path *path, u64 mask,
3324 unsigned int obj_type)
3329 struct common_audit_data ad;
3331 ad.type = LSM_AUDIT_DATA_PATH;
3335 * Set permission needed based on the type of mark being set.
3336 * Performs an additional check for sb watches.
3339 case FSNOTIFY_OBJ_TYPE_VFSMOUNT:
3340 perm = FILE__WATCH_MOUNT;
3342 case FSNOTIFY_OBJ_TYPE_SB:
3343 perm = FILE__WATCH_SB;
3344 ret = superblock_has_perm(current_cred(), path->dentry->d_sb,
3345 FILESYSTEM__WATCH, &ad);
3349 case FSNOTIFY_OBJ_TYPE_INODE:
3356 /* blocking watches require the file:watch_with_perm permission */
3357 if (mask & (ALL_FSNOTIFY_PERM_EVENTS))
3358 perm |= FILE__WATCH_WITH_PERM;
3360 /* watches on read-like events need the file:watch_reads permission */
3361 if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_CLOSE_NOWRITE))
3362 perm |= FILE__WATCH_READS;
3364 return path_has_perm(current_cred(), path, perm);
3368 * Copy the inode security context value to the user.
3370 * Permission check is handled by selinux_inode_getxattr hook.
3372 static int selinux_inode_getsecurity(struct user_namespace *mnt_userns,
3373 struct inode *inode, const char *name,
3374 void **buffer, bool alloc)
3378 char *context = NULL;
3379 struct inode_security_struct *isec;
3382 * If we're not initialized yet, then we can't validate contexts, so
3383 * just let vfs_getxattr fall back to using the on-disk xattr.
3385 if (!selinux_initialized(&selinux_state) ||
3386 strcmp(name, XATTR_SELINUX_SUFFIX))
3390 * If the caller has CAP_MAC_ADMIN, then get the raw context
3391 * value even if it is not defined by current policy; otherwise,
3392 * use the in-core value under current policy.
3393 * Use the non-auditing forms of the permission checks since
3394 * getxattr may be called by unprivileged processes commonly
3395 * and lack of permission just means that we fall back to the
3396 * in-core context value, not a denial.
3398 isec = inode_security(inode);
3399 if (has_cap_mac_admin(false))
3400 error = security_sid_to_context_force(&selinux_state,
3401 isec->sid, &context,
3404 error = security_sid_to_context(&selinux_state, isec->sid,
3418 static int selinux_inode_setsecurity(struct inode *inode, const char *name,
3419 const void *value, size_t size, int flags)
3421 struct inode_security_struct *isec = inode_security_novalidate(inode);
3422 struct superblock_security_struct *sbsec;
3426 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3429 sbsec = selinux_superblock(inode->i_sb);
3430 if (!(sbsec->flags & SBLABEL_MNT))
3433 if (!value || !size)
3436 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3441 spin_lock(&isec->lock);
3442 isec->sclass = inode_mode_to_security_class(inode->i_mode);
3444 isec->initialized = LABEL_INITIALIZED;
3445 spin_unlock(&isec->lock);
3449 static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3451 const int len = sizeof(XATTR_NAME_SELINUX);
3453 if (!selinux_initialized(&selinux_state))
3456 if (buffer && len <= buffer_size)
3457 memcpy(buffer, XATTR_NAME_SELINUX, len);
3461 static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
3463 struct inode_security_struct *isec = inode_security_novalidate(inode);
3467 static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3470 struct task_security_struct *tsec;
3471 struct cred *new_creds = *new;
3473 if (new_creds == NULL) {
3474 new_creds = prepare_creds();
3479 tsec = selinux_cred(new_creds);
3480 /* Get label from overlay inode and set it in create_sid */
3481 selinux_inode_getsecid(d_inode(src), &sid);
3482 tsec->create_sid = sid;
3487 static int selinux_inode_copy_up_xattr(const char *name)
3489 /* The copy_up hook above sets the initial context on an inode, but we
3490 * don't then want to overwrite it by blindly copying all the lower
3491 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3493 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3494 return 1; /* Discard */
3496 * Any other attribute apart from SELINUX is not claimed, supported
3502 /* kernfs node operations */
3504 static int selinux_kernfs_init_security(struct kernfs_node *kn_dir,
3505 struct kernfs_node *kn)
3507 const struct task_security_struct *tsec = selinux_cred(current_cred());
3508 u32 parent_sid, newsid, clen;
3512 rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, NULL, 0);
3519 context = kmalloc(clen, GFP_KERNEL);
3523 rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, context, clen);
3529 rc = security_context_to_sid(&selinux_state, context, clen, &parent_sid,
3535 if (tsec->create_sid) {
3536 newsid = tsec->create_sid;
3538 u16 secclass = inode_mode_to_security_class(kn->mode);
3542 q.hash_len = hashlen_string(kn_dir, kn->name);
3544 rc = security_transition_sid(&selinux_state, tsec->sid,
3545 parent_sid, secclass, &q,
3551 rc = security_sid_to_context_force(&selinux_state, newsid,
3556 rc = kernfs_xattr_set(kn, XATTR_NAME_SELINUX, context, clen,
3563 /* file security operations */
3565 static int selinux_revalidate_file_permission(struct file *file, int mask)
3567 const struct cred *cred = current_cred();
3568 struct inode *inode = file_inode(file);
3570 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3571 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3574 return file_has_perm(cred, file,
3575 file_mask_to_av(inode->i_mode, mask));
3578 static int selinux_file_permission(struct file *file, int mask)
3580 struct inode *inode = file_inode(file);
3581 struct file_security_struct *fsec = selinux_file(file);
3582 struct inode_security_struct *isec;
3583 u32 sid = current_sid();
3586 /* No permission to check. Existence test. */
3589 isec = inode_security(inode);
3590 if (sid == fsec->sid && fsec->isid == isec->sid &&
3591 fsec->pseqno == avc_policy_seqno(&selinux_state))
3592 /* No change since file_open check. */
3595 return selinux_revalidate_file_permission(file, mask);
3598 static int selinux_file_alloc_security(struct file *file)
3600 struct file_security_struct *fsec = selinux_file(file);
3601 u32 sid = current_sid();
3604 fsec->fown_sid = sid;
3610 * Check whether a task has the ioctl permission and cmd
3611 * operation to an inode.
3613 static int ioctl_has_perm(const struct cred *cred, struct file *file,
3614 u32 requested, u16 cmd)
3616 struct common_audit_data ad;
3617 struct file_security_struct *fsec = selinux_file(file);
3618 struct inode *inode = file_inode(file);
3619 struct inode_security_struct *isec;
3620 struct lsm_ioctlop_audit ioctl;
3621 u32 ssid = cred_sid(cred);
3623 u8 driver = cmd >> 8;
3624 u8 xperm = cmd & 0xff;
3626 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3629 ad.u.op->path = file->f_path;
3631 if (ssid != fsec->sid) {
3632 rc = avc_has_perm(&selinux_state,
3641 if (unlikely(IS_PRIVATE(inode)))
3644 isec = inode_security(inode);
3645 rc = avc_has_extended_perms(&selinux_state,
3646 ssid, isec->sid, isec->sclass,
3647 requested, driver, xperm, &ad);
3652 static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3655 const struct cred *cred = current_cred();
3662 case FS_IOC_GETFLAGS:
3663 case FS_IOC_GETVERSION:
3664 error = file_has_perm(cred, file, FILE__GETATTR);
3667 case FS_IOC_SETFLAGS:
3668 case FS_IOC_SETVERSION:
3669 error = file_has_perm(cred, file, FILE__SETATTR);
3672 /* sys_ioctl() checks */
3675 error = file_has_perm(cred, file, 0);
3680 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3681 CAP_OPT_NONE, true);
3686 if (!selinux_policycap_ioctl_skip_cloexec())
3687 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
3690 /* default case assumes that the command will go
3691 * to the file's ioctl() function.
3694 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
3699 static int default_noexec __ro_after_init;
3701 static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3703 const struct cred *cred = current_cred();
3704 u32 sid = cred_sid(cred);
3707 if (default_noexec &&
3708 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3709 (!shared && (prot & PROT_WRITE)))) {
3711 * We are making executable an anonymous mapping or a
3712 * private file mapping that will also be writable.
3713 * This has an additional check.
3715 rc = avc_has_perm(&selinux_state,
3716 sid, sid, SECCLASS_PROCESS,
3717 PROCESS__EXECMEM, NULL);
3723 /* read access is always possible with a mapping */
3724 u32 av = FILE__READ;
3726 /* write access only matters if the mapping is shared */
3727 if (shared && (prot & PROT_WRITE))
3730 if (prot & PROT_EXEC)
3731 av |= FILE__EXECUTE;
3733 return file_has_perm(cred, file, av);
3740 static int selinux_mmap_addr(unsigned long addr)
3744 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3745 u32 sid = current_sid();
3746 rc = avc_has_perm(&selinux_state,
3747 sid, sid, SECCLASS_MEMPROTECT,
3748 MEMPROTECT__MMAP_ZERO, NULL);
3754 static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3755 unsigned long prot, unsigned long flags)
3757 struct common_audit_data ad;
3761 ad.type = LSM_AUDIT_DATA_FILE;
3763 rc = inode_has_perm(current_cred(), file_inode(file),
3769 if (checkreqprot_get(&selinux_state))
3772 return file_map_prot_check(file, prot,
3773 (flags & MAP_TYPE) == MAP_SHARED);
3776 static int selinux_file_mprotect(struct vm_area_struct *vma,
3777 unsigned long reqprot,
3780 const struct cred *cred = current_cred();
3781 u32 sid = cred_sid(cred);
3783 if (checkreqprot_get(&selinux_state))
3786 if (default_noexec &&
3787 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3789 if (vma->vm_start >= vma->vm_mm->start_brk &&
3790 vma->vm_end <= vma->vm_mm->brk) {
3791 rc = avc_has_perm(&selinux_state,
3792 sid, sid, SECCLASS_PROCESS,
3793 PROCESS__EXECHEAP, NULL);
3794 } else if (!vma->vm_file &&
3795 ((vma->vm_start <= vma->vm_mm->start_stack &&
3796 vma->vm_end >= vma->vm_mm->start_stack) ||
3797 vma_is_stack_for_current(vma))) {
3798 rc = avc_has_perm(&selinux_state,
3799 sid, sid, SECCLASS_PROCESS,
3800 PROCESS__EXECSTACK, NULL);
3801 } else if (vma->vm_file && vma->anon_vma) {
3803 * We are making executable a file mapping that has
3804 * had some COW done. Since pages might have been
3805 * written, check ability to execute the possibly
3806 * modified content. This typically should only
3807 * occur for text relocations.
3809 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
3815 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3818 static int selinux_file_lock(struct file *file, unsigned int cmd)
3820 const struct cred *cred = current_cred();
3822 return file_has_perm(cred, file, FILE__LOCK);
3825 static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3828 const struct cred *cred = current_cred();
3833 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
3834 err = file_has_perm(cred, file, FILE__WRITE);
3843 case F_GETOWNER_UIDS:
3844 /* Just check FD__USE permission */
3845 err = file_has_perm(cred, file, 0);
3853 #if BITS_PER_LONG == 32
3858 err = file_has_perm(cred, file, FILE__LOCK);
3865 static void selinux_file_set_fowner(struct file *file)
3867 struct file_security_struct *fsec;
3869 fsec = selinux_file(file);
3870 fsec->fown_sid = current_sid();
3873 static int selinux_file_send_sigiotask(struct task_struct *tsk,
3874 struct fown_struct *fown, int signum)
3877 u32 sid = task_sid_obj(tsk);
3879 struct file_security_struct *fsec;
3881 /* struct fown_struct is never outside the context of a struct file */
3882 file = container_of(fown, struct file, f_owner);
3884 fsec = selinux_file(file);
3887 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3889 perm = signal_to_av(signum);
3891 return avc_has_perm(&selinux_state,
3892 fsec->fown_sid, sid,
3893 SECCLASS_PROCESS, perm, NULL);
3896 static int selinux_file_receive(struct file *file)
3898 const struct cred *cred = current_cred();
3900 return file_has_perm(cred, file, file_to_av(file));
3903 static int selinux_file_open(struct file *file)
3905 struct file_security_struct *fsec;
3906 struct inode_security_struct *isec;
3908 fsec = selinux_file(file);
3909 isec = inode_security(file_inode(file));
3911 * Save inode label and policy sequence number
3912 * at open-time so that selinux_file_permission
3913 * can determine whether revalidation is necessary.
3914 * Task label is already saved in the file security
3915 * struct as its SID.
3917 fsec->isid = isec->sid;
3918 fsec->pseqno = avc_policy_seqno(&selinux_state);
3920 * Since the inode label or policy seqno may have changed
3921 * between the selinux_inode_permission check and the saving
3922 * of state above, recheck that access is still permitted.
3923 * Otherwise, access might never be revalidated against the
3924 * new inode label or new policy.
3925 * This check is not redundant - do not remove.
3927 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
3930 /* task security operations */
3932 static int selinux_task_alloc(struct task_struct *task,
3933 unsigned long clone_flags)
3935 u32 sid = current_sid();
3937 return avc_has_perm(&selinux_state,
3938 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
3942 * prepare a new set of credentials for modification
3944 static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3947 const struct task_security_struct *old_tsec = selinux_cred(old);
3948 struct task_security_struct *tsec = selinux_cred(new);
3955 * transfer the SELinux data to a blank set of creds
3957 static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3959 const struct task_security_struct *old_tsec = selinux_cred(old);
3960 struct task_security_struct *tsec = selinux_cred(new);
3965 static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3967 *secid = cred_sid(c);
3971 * set the security data for a kernel service
3972 * - all the creation contexts are set to unlabelled
3974 static int selinux_kernel_act_as(struct cred *new, u32 secid)
3976 struct task_security_struct *tsec = selinux_cred(new);
3977 u32 sid = current_sid();
3980 ret = avc_has_perm(&selinux_state,
3982 SECCLASS_KERNEL_SERVICE,
3983 KERNEL_SERVICE__USE_AS_OVERRIDE,
3987 tsec->create_sid = 0;
3988 tsec->keycreate_sid = 0;
3989 tsec->sockcreate_sid = 0;
3995 * set the file creation context in a security record to the same as the
3996 * objective context of the specified inode
3998 static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
4000 struct inode_security_struct *isec = inode_security(inode);
4001 struct task_security_struct *tsec = selinux_cred(new);
4002 u32 sid = current_sid();
4005 ret = avc_has_perm(&selinux_state,
4007 SECCLASS_KERNEL_SERVICE,
4008 KERNEL_SERVICE__CREATE_FILES_AS,
4012 tsec->create_sid = isec->sid;
4016 static int selinux_kernel_module_request(char *kmod_name)
4018 struct common_audit_data ad;
4020 ad.type = LSM_AUDIT_DATA_KMOD;
4021 ad.u.kmod_name = kmod_name;
4023 return avc_has_perm(&selinux_state,
4024 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
4025 SYSTEM__MODULE_REQUEST, &ad);
4028 static int selinux_kernel_module_from_file(struct file *file)
4030 struct common_audit_data ad;
4031 struct inode_security_struct *isec;
4032 struct file_security_struct *fsec;
4033 u32 sid = current_sid();
4038 return avc_has_perm(&selinux_state,
4039 sid, sid, SECCLASS_SYSTEM,
4040 SYSTEM__MODULE_LOAD, NULL);
4044 ad.type = LSM_AUDIT_DATA_FILE;
4047 fsec = selinux_file(file);
4048 if (sid != fsec->sid) {
4049 rc = avc_has_perm(&selinux_state,
4050 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
4055 isec = inode_security(file_inode(file));
4056 return avc_has_perm(&selinux_state,
4057 sid, isec->sid, SECCLASS_SYSTEM,
4058 SYSTEM__MODULE_LOAD, &ad);
4061 static int selinux_kernel_read_file(struct file *file,
4062 enum kernel_read_file_id id,
4068 case READING_MODULE:
4069 rc = selinux_kernel_module_from_file(contents ? file : NULL);
4078 static int selinux_kernel_load_data(enum kernel_load_data_id id, bool contents)
4083 case LOADING_MODULE:
4084 rc = selinux_kernel_module_from_file(NULL);
4093 static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
4095 return avc_has_perm(&selinux_state,
4096 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4097 PROCESS__SETPGID, NULL);
4100 static int selinux_task_getpgid(struct task_struct *p)
4102 return avc_has_perm(&selinux_state,
4103 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4104 PROCESS__GETPGID, NULL);
4107 static int selinux_task_getsid(struct task_struct *p)
4109 return avc_has_perm(&selinux_state,
4110 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4111 PROCESS__GETSESSION, NULL);
4114 static void selinux_current_getsecid_subj(u32 *secid)
4116 *secid = current_sid();
4119 static void selinux_task_getsecid_obj(struct task_struct *p, u32 *secid)
4121 *secid = task_sid_obj(p);
4124 static int selinux_task_setnice(struct task_struct *p, int nice)
4126 return avc_has_perm(&selinux_state,
4127 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4128 PROCESS__SETSCHED, NULL);
4131 static int selinux_task_setioprio(struct task_struct *p, int ioprio)
4133 return avc_has_perm(&selinux_state,
4134 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4135 PROCESS__SETSCHED, NULL);
4138 static int selinux_task_getioprio(struct task_struct *p)
4140 return avc_has_perm(&selinux_state,
4141 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4142 PROCESS__GETSCHED, NULL);
4145 static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
4152 if (flags & LSM_PRLIMIT_WRITE)
4153 av |= PROCESS__SETRLIMIT;
4154 if (flags & LSM_PRLIMIT_READ)
4155 av |= PROCESS__GETRLIMIT;
4156 return avc_has_perm(&selinux_state,
4157 cred_sid(cred), cred_sid(tcred),
4158 SECCLASS_PROCESS, av, NULL);
4161 static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
4162 struct rlimit *new_rlim)
4164 struct rlimit *old_rlim = p->signal->rlim + resource;
4166 /* Control the ability to change the hard limit (whether
4167 lowering or raising it), so that the hard limit can
4168 later be used as a safe reset point for the soft limit
4169 upon context transitions. See selinux_bprm_committing_creds. */
4170 if (old_rlim->rlim_max != new_rlim->rlim_max)
4171 return avc_has_perm(&selinux_state,
4172 current_sid(), task_sid_obj(p),
4173 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
4178 static int selinux_task_setscheduler(struct task_struct *p)
4180 return avc_has_perm(&selinux_state,
4181 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4182 PROCESS__SETSCHED, NULL);
4185 static int selinux_task_getscheduler(struct task_struct *p)
4187 return avc_has_perm(&selinux_state,
4188 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4189 PROCESS__GETSCHED, NULL);
4192 static int selinux_task_movememory(struct task_struct *p)
4194 return avc_has_perm(&selinux_state,
4195 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4196 PROCESS__SETSCHED, NULL);
4199 static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
4200 int sig, const struct cred *cred)
4206 perm = PROCESS__SIGNULL; /* null signal; existence test */
4208 perm = signal_to_av(sig);
4210 secid = current_sid();
4212 secid = cred_sid(cred);
4213 return avc_has_perm(&selinux_state,
4214 secid, task_sid_obj(p), SECCLASS_PROCESS, perm, NULL);
4217 static void selinux_task_to_inode(struct task_struct *p,
4218 struct inode *inode)
4220 struct inode_security_struct *isec = selinux_inode(inode);
4221 u32 sid = task_sid_obj(p);
4223 spin_lock(&isec->lock);
4224 isec->sclass = inode_mode_to_security_class(inode->i_mode);
4226 isec->initialized = LABEL_INITIALIZED;
4227 spin_unlock(&isec->lock);
4230 /* Returns error only if unable to parse addresses */
4231 static int selinux_parse_skb_ipv4(struct sk_buff *skb,
4232 struct common_audit_data *ad, u8 *proto)
4234 int offset, ihlen, ret = -EINVAL;
4235 struct iphdr _iph, *ih;
4237 offset = skb_network_offset(skb);
4238 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4242 ihlen = ih->ihl * 4;
4243 if (ihlen < sizeof(_iph))
4246 ad->u.net->v4info.saddr = ih->saddr;
4247 ad->u.net->v4info.daddr = ih->daddr;
4251 *proto = ih->protocol;
4253 switch (ih->protocol) {
4255 struct tcphdr _tcph, *th;
4257 if (ntohs(ih->frag_off) & IP_OFFSET)
4261 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4265 ad->u.net->sport = th->source;
4266 ad->u.net->dport = th->dest;
4271 struct udphdr _udph, *uh;
4273 if (ntohs(ih->frag_off) & IP_OFFSET)
4277 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4281 ad->u.net->sport = uh->source;
4282 ad->u.net->dport = uh->dest;
4286 case IPPROTO_DCCP: {
4287 struct dccp_hdr _dccph, *dh;
4289 if (ntohs(ih->frag_off) & IP_OFFSET)
4293 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4297 ad->u.net->sport = dh->dccph_sport;
4298 ad->u.net->dport = dh->dccph_dport;
4302 #if IS_ENABLED(CONFIG_IP_SCTP)
4303 case IPPROTO_SCTP: {
4304 struct sctphdr _sctph, *sh;
4306 if (ntohs(ih->frag_off) & IP_OFFSET)
4310 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4314 ad->u.net->sport = sh->source;
4315 ad->u.net->dport = sh->dest;
4326 #if IS_ENABLED(CONFIG_IPV6)
4328 /* Returns error only if unable to parse addresses */
4329 static int selinux_parse_skb_ipv6(struct sk_buff *skb,
4330 struct common_audit_data *ad, u8 *proto)
4333 int ret = -EINVAL, offset;
4334 struct ipv6hdr _ipv6h, *ip6;
4337 offset = skb_network_offset(skb);
4338 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4342 ad->u.net->v6info.saddr = ip6->saddr;
4343 ad->u.net->v6info.daddr = ip6->daddr;
4346 nexthdr = ip6->nexthdr;
4347 offset += sizeof(_ipv6h);
4348 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
4357 struct tcphdr _tcph, *th;
4359 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4363 ad->u.net->sport = th->source;
4364 ad->u.net->dport = th->dest;
4369 struct udphdr _udph, *uh;
4371 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4375 ad->u.net->sport = uh->source;
4376 ad->u.net->dport = uh->dest;
4380 case IPPROTO_DCCP: {
4381 struct dccp_hdr _dccph, *dh;
4383 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4387 ad->u.net->sport = dh->dccph_sport;
4388 ad->u.net->dport = dh->dccph_dport;
4392 #if IS_ENABLED(CONFIG_IP_SCTP)
4393 case IPPROTO_SCTP: {
4394 struct sctphdr _sctph, *sh;
4396 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4400 ad->u.net->sport = sh->source;
4401 ad->u.net->dport = sh->dest;
4405 /* includes fragments */
4415 static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
4416 char **_addrp, int src, u8 *proto)
4421 switch (ad->u.net->family) {
4423 ret = selinux_parse_skb_ipv4(skb, ad, proto);
4426 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4427 &ad->u.net->v4info.daddr);
4430 #if IS_ENABLED(CONFIG_IPV6)
4432 ret = selinux_parse_skb_ipv6(skb, ad, proto);
4435 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4436 &ad->u.net->v6info.daddr);
4446 "SELinux: failure in selinux_parse_skb(),"
4447 " unable to parse packet\n");
4457 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
4459 * @family: protocol family
4460 * @sid: the packet's peer label SID
4463 * Check the various different forms of network peer labeling and determine
4464 * the peer label/SID for the packet; most of the magic actually occurs in
4465 * the security server function security_net_peersid_cmp(). The function
4466 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4467 * or -EACCES if @sid is invalid due to inconsistencies with the different
4471 static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
4478 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
4481 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4485 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4486 nlbl_type, xfrm_sid, sid);
4487 if (unlikely(err)) {
4489 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4490 " unable to determine packet's peer label\n");
4498 * selinux_conn_sid - Determine the child socket label for a connection
4499 * @sk_sid: the parent socket's SID
4500 * @skb_sid: the packet's SID
4501 * @conn_sid: the resulting connection SID
4503 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4504 * combined with the MLS information from @skb_sid in order to create
4505 * @conn_sid. If @skb_sid is not valid then @conn_sid is simply a copy
4506 * of @sk_sid. Returns zero on success, negative values on failure.
4509 static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4513 if (skb_sid != SECSID_NULL)
4514 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4522 /* socket security operations */
4524 static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4525 u16 secclass, u32 *socksid)
4527 if (tsec->sockcreate_sid > SECSID_NULL) {
4528 *socksid = tsec->sockcreate_sid;
4532 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4533 secclass, NULL, socksid);
4536 static int sock_has_perm(struct sock *sk, u32 perms)
4538 struct sk_security_struct *sksec = sk->sk_security;
4539 struct common_audit_data ad;
4540 struct lsm_network_audit net = {0,};
4542 if (sksec->sid == SECINITSID_KERNEL)
4545 ad.type = LSM_AUDIT_DATA_NET;
4549 return avc_has_perm(&selinux_state,
4550 current_sid(), sksec->sid, sksec->sclass, perms,
4554 static int selinux_socket_create(int family, int type,
4555 int protocol, int kern)
4557 const struct task_security_struct *tsec = selinux_cred(current_cred());
4565 secclass = socket_type_to_security_class(family, type, protocol);
4566 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4570 return avc_has_perm(&selinux_state,
4571 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
4574 static int selinux_socket_post_create(struct socket *sock, int family,
4575 int type, int protocol, int kern)
4577 const struct task_security_struct *tsec = selinux_cred(current_cred());
4578 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
4579 struct sk_security_struct *sksec;
4580 u16 sclass = socket_type_to_security_class(family, type, protocol);
4581 u32 sid = SECINITSID_KERNEL;
4585 err = socket_sockcreate_sid(tsec, sclass, &sid);
4590 isec->sclass = sclass;
4592 isec->initialized = LABEL_INITIALIZED;
4595 sksec = sock->sk->sk_security;
4596 sksec->sclass = sclass;
4598 /* Allows detection of the first association on this socket */
4599 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4600 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4602 err = selinux_netlbl_socket_post_create(sock->sk, family);
4608 static int selinux_socket_socketpair(struct socket *socka,
4609 struct socket *sockb)
4611 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4612 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4614 sksec_a->peer_sid = sksec_b->sid;
4615 sksec_b->peer_sid = sksec_a->sid;
4620 /* Range of port numbers used to automatically bind.
4621 Need to determine whether we should perform a name_bind
4622 permission check between the socket and the port number. */
4624 static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4626 struct sock *sk = sock->sk;
4627 struct sk_security_struct *sksec = sk->sk_security;
4631 err = sock_has_perm(sk, SOCKET__BIND);
4635 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
4636 family = sk->sk_family;
4637 if (family == PF_INET || family == PF_INET6) {
4639 struct common_audit_data ad;
4640 struct lsm_network_audit net = {0,};
4641 struct sockaddr_in *addr4 = NULL;
4642 struct sockaddr_in6 *addr6 = NULL;
4644 unsigned short snum;
4648 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4649 * that validates multiple binding addresses. Because of this
4650 * need to check address->sa_family as it is possible to have
4651 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4653 if (addrlen < offsetofend(struct sockaddr, sa_family))
4655 family_sa = address->sa_family;
4656 switch (family_sa) {
4659 if (addrlen < sizeof(struct sockaddr_in))
4661 addr4 = (struct sockaddr_in *)address;
4662 if (family_sa == AF_UNSPEC) {
4663 /* see __inet_bind(), we only want to allow
4664 * AF_UNSPEC if the address is INADDR_ANY
4666 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4668 family_sa = AF_INET;
4670 snum = ntohs(addr4->sin_port);
4671 addrp = (char *)&addr4->sin_addr.s_addr;
4674 if (addrlen < SIN6_LEN_RFC2133)
4676 addr6 = (struct sockaddr_in6 *)address;
4677 snum = ntohs(addr6->sin6_port);
4678 addrp = (char *)&addr6->sin6_addr.s6_addr;
4684 ad.type = LSM_AUDIT_DATA_NET;
4686 ad.u.net->sport = htons(snum);
4687 ad.u.net->family = family_sa;
4692 inet_get_local_port_range(sock_net(sk), &low, &high);
4694 if (inet_port_requires_bind_service(sock_net(sk), snum) ||
4695 snum < low || snum > high) {
4696 err = sel_netport_sid(sk->sk_protocol,
4700 err = avc_has_perm(&selinux_state,
4703 SOCKET__NAME_BIND, &ad);
4709 switch (sksec->sclass) {
4710 case SECCLASS_TCP_SOCKET:
4711 node_perm = TCP_SOCKET__NODE_BIND;
4714 case SECCLASS_UDP_SOCKET:
4715 node_perm = UDP_SOCKET__NODE_BIND;
4718 case SECCLASS_DCCP_SOCKET:
4719 node_perm = DCCP_SOCKET__NODE_BIND;
4722 case SECCLASS_SCTP_SOCKET:
4723 node_perm = SCTP_SOCKET__NODE_BIND;
4727 node_perm = RAWIP_SOCKET__NODE_BIND;
4731 err = sel_netnode_sid(addrp, family_sa, &sid);
4735 if (family_sa == AF_INET)
4736 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
4738 ad.u.net->v6info.saddr = addr6->sin6_addr;
4740 err = avc_has_perm(&selinux_state,
4742 sksec->sclass, node_perm, &ad);
4749 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4750 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4752 return -EAFNOSUPPORT;
4755 /* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
4756 * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst
4758 static int selinux_socket_connect_helper(struct socket *sock,
4759 struct sockaddr *address, int addrlen)
4761 struct sock *sk = sock->sk;
4762 struct sk_security_struct *sksec = sk->sk_security;
4765 err = sock_has_perm(sk, SOCKET__CONNECT);
4768 if (addrlen < offsetofend(struct sockaddr, sa_family))
4771 /* connect(AF_UNSPEC) has special handling, as it is a documented
4772 * way to disconnect the socket
4774 if (address->sa_family == AF_UNSPEC)
4778 * If a TCP, DCCP or SCTP socket, check name_connect permission
4781 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4782 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4783 sksec->sclass == SECCLASS_SCTP_SOCKET) {
4784 struct common_audit_data ad;
4785 struct lsm_network_audit net = {0,};
4786 struct sockaddr_in *addr4 = NULL;
4787 struct sockaddr_in6 *addr6 = NULL;
4788 unsigned short snum;
4791 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4792 * that validates multiple connect addresses. Because of this
4793 * need to check address->sa_family as it is possible to have
4794 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4796 switch (address->sa_family) {
4798 addr4 = (struct sockaddr_in *)address;
4799 if (addrlen < sizeof(struct sockaddr_in))
4801 snum = ntohs(addr4->sin_port);
4804 addr6 = (struct sockaddr_in6 *)address;
4805 if (addrlen < SIN6_LEN_RFC2133)
4807 snum = ntohs(addr6->sin6_port);
4810 /* Note that SCTP services expect -EINVAL, whereas
4811 * others expect -EAFNOSUPPORT.
4813 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4816 return -EAFNOSUPPORT;
4819 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
4823 switch (sksec->sclass) {
4824 case SECCLASS_TCP_SOCKET:
4825 perm = TCP_SOCKET__NAME_CONNECT;
4827 case SECCLASS_DCCP_SOCKET:
4828 perm = DCCP_SOCKET__NAME_CONNECT;
4830 case SECCLASS_SCTP_SOCKET:
4831 perm = SCTP_SOCKET__NAME_CONNECT;
4835 ad.type = LSM_AUDIT_DATA_NET;
4837 ad.u.net->dport = htons(snum);
4838 ad.u.net->family = address->sa_family;
4839 err = avc_has_perm(&selinux_state,
4840 sksec->sid, sid, sksec->sclass, perm, &ad);
4848 /* Supports connect(2), see comments in selinux_socket_connect_helper() */
4849 static int selinux_socket_connect(struct socket *sock,
4850 struct sockaddr *address, int addrlen)
4853 struct sock *sk = sock->sk;
4855 err = selinux_socket_connect_helper(sock, address, addrlen);
4859 return selinux_netlbl_socket_connect(sk, address);
4862 static int selinux_socket_listen(struct socket *sock, int backlog)
4864 return sock_has_perm(sock->sk, SOCKET__LISTEN);
4867 static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4870 struct inode_security_struct *isec;
4871 struct inode_security_struct *newisec;
4875 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
4879 isec = inode_security_novalidate(SOCK_INODE(sock));
4880 spin_lock(&isec->lock);
4881 sclass = isec->sclass;
4883 spin_unlock(&isec->lock);
4885 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4886 newisec->sclass = sclass;
4888 newisec->initialized = LABEL_INITIALIZED;
4893 static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
4896 return sock_has_perm(sock->sk, SOCKET__WRITE);
4899 static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4900 int size, int flags)
4902 return sock_has_perm(sock->sk, SOCKET__READ);
4905 static int selinux_socket_getsockname(struct socket *sock)
4907 return sock_has_perm(sock->sk, SOCKET__GETATTR);
4910 static int selinux_socket_getpeername(struct socket *sock)
4912 return sock_has_perm(sock->sk, SOCKET__GETATTR);
4915 static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
4919 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
4923 return selinux_netlbl_socket_setsockopt(sock, level, optname);
4926 static int selinux_socket_getsockopt(struct socket *sock, int level,
4929 return sock_has_perm(sock->sk, SOCKET__GETOPT);
4932 static int selinux_socket_shutdown(struct socket *sock, int how)
4934 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
4937 static int selinux_socket_unix_stream_connect(struct sock *sock,
4941 struct sk_security_struct *sksec_sock = sock->sk_security;
4942 struct sk_security_struct *sksec_other = other->sk_security;
4943 struct sk_security_struct *sksec_new = newsk->sk_security;
4944 struct common_audit_data ad;
4945 struct lsm_network_audit net = {0,};
4948 ad.type = LSM_AUDIT_DATA_NET;
4950 ad.u.net->sk = other;
4952 err = avc_has_perm(&selinux_state,
4953 sksec_sock->sid, sksec_other->sid,
4954 sksec_other->sclass,
4955 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4959 /* server child socket */
4960 sksec_new->peer_sid = sksec_sock->sid;
4961 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4962 sksec_sock->sid, &sksec_new->sid);
4966 /* connecting socket */
4967 sksec_sock->peer_sid = sksec_new->sid;
4972 static int selinux_socket_unix_may_send(struct socket *sock,
4973 struct socket *other)
4975 struct sk_security_struct *ssec = sock->sk->sk_security;
4976 struct sk_security_struct *osec = other->sk->sk_security;
4977 struct common_audit_data ad;
4978 struct lsm_network_audit net = {0,};
4980 ad.type = LSM_AUDIT_DATA_NET;
4982 ad.u.net->sk = other->sk;
4984 return avc_has_perm(&selinux_state,
4985 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4989 static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4990 char *addrp, u16 family, u32 peer_sid,
4991 struct common_audit_data *ad)
4997 err = sel_netif_sid(ns, ifindex, &if_sid);
5000 err = avc_has_perm(&selinux_state,
5002 SECCLASS_NETIF, NETIF__INGRESS, ad);
5006 err = sel_netnode_sid(addrp, family, &node_sid);
5009 return avc_has_perm(&selinux_state,
5011 SECCLASS_NODE, NODE__RECVFROM, ad);
5014 static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
5018 struct sk_security_struct *sksec = sk->sk_security;
5019 u32 sk_sid = sksec->sid;
5020 struct common_audit_data ad;
5021 struct lsm_network_audit net = {0,};
5024 ad.type = LSM_AUDIT_DATA_NET;
5026 ad.u.net->netif = skb->skb_iif;
5027 ad.u.net->family = family;
5028 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
5032 if (selinux_secmark_enabled()) {
5033 err = avc_has_perm(&selinux_state,
5034 sk_sid, skb->secmark, SECCLASS_PACKET,
5040 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
5043 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
5048 static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
5051 struct sk_security_struct *sksec = sk->sk_security;
5052 u16 family = sk->sk_family;
5053 u32 sk_sid = sksec->sid;
5054 struct common_audit_data ad;
5055 struct lsm_network_audit net = {0,};
5060 if (family != PF_INET && family != PF_INET6)
5063 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
5064 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5067 /* If any sort of compatibility mode is enabled then handoff processing
5068 * to the selinux_sock_rcv_skb_compat() function to deal with the
5069 * special handling. We do this in an attempt to keep this function
5070 * as fast and as clean as possible. */
5071 if (!selinux_policycap_netpeer())
5072 return selinux_sock_rcv_skb_compat(sk, skb, family);
5074 secmark_active = selinux_secmark_enabled();
5075 peerlbl_active = selinux_peerlbl_enabled();
5076 if (!secmark_active && !peerlbl_active)
5079 ad.type = LSM_AUDIT_DATA_NET;
5081 ad.u.net->netif = skb->skb_iif;
5082 ad.u.net->family = family;
5083 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
5087 if (peerlbl_active) {
5090 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
5093 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
5094 addrp, family, peer_sid, &ad);
5096 selinux_netlbl_err(skb, family, err, 0);
5099 err = avc_has_perm(&selinux_state,
5100 sk_sid, peer_sid, SECCLASS_PEER,
5103 selinux_netlbl_err(skb, family, err, 0);
5108 if (secmark_active) {
5109 err = avc_has_perm(&selinux_state,
5110 sk_sid, skb->secmark, SECCLASS_PACKET,
5119 static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
5120 int __user *optlen, unsigned len)
5125 struct sk_security_struct *sksec = sock->sk->sk_security;
5126 u32 peer_sid = SECSID_NULL;
5128 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
5129 sksec->sclass == SECCLASS_TCP_SOCKET ||
5130 sksec->sclass == SECCLASS_SCTP_SOCKET)
5131 peer_sid = sksec->peer_sid;
5132 if (peer_sid == SECSID_NULL)
5133 return -ENOPROTOOPT;
5135 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
5140 if (scontext_len > len) {
5145 if (copy_to_user(optval, scontext, scontext_len))
5149 if (put_user(scontext_len, optlen))
5155 static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
5157 u32 peer_secid = SECSID_NULL;
5159 struct inode_security_struct *isec;
5161 if (skb && skb->protocol == htons(ETH_P_IP))
5163 else if (skb && skb->protocol == htons(ETH_P_IPV6))
5166 family = sock->sk->sk_family;
5170 if (sock && family == PF_UNIX) {
5171 isec = inode_security_novalidate(SOCK_INODE(sock));
5172 peer_secid = isec->sid;
5174 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
5177 *secid = peer_secid;
5178 if (peer_secid == SECSID_NULL)
5183 static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
5185 struct sk_security_struct *sksec;
5187 sksec = kzalloc(sizeof(*sksec), priority);
5191 sksec->peer_sid = SECINITSID_UNLABELED;
5192 sksec->sid = SECINITSID_UNLABELED;
5193 sksec->sclass = SECCLASS_SOCKET;
5194 selinux_netlbl_sk_security_reset(sksec);
5195 sk->sk_security = sksec;
5200 static void selinux_sk_free_security(struct sock *sk)
5202 struct sk_security_struct *sksec = sk->sk_security;
5204 sk->sk_security = NULL;
5205 selinux_netlbl_sk_security_free(sksec);
5209 static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5211 struct sk_security_struct *sksec = sk->sk_security;
5212 struct sk_security_struct *newsksec = newsk->sk_security;
5214 newsksec->sid = sksec->sid;
5215 newsksec->peer_sid = sksec->peer_sid;
5216 newsksec->sclass = sksec->sclass;
5218 selinux_netlbl_sk_security_reset(newsksec);
5221 static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
5224 *secid = SECINITSID_ANY_SOCKET;
5226 struct sk_security_struct *sksec = sk->sk_security;
5228 *secid = sksec->sid;
5232 static void selinux_sock_graft(struct sock *sk, struct socket *parent)
5234 struct inode_security_struct *isec =
5235 inode_security_novalidate(SOCK_INODE(parent));
5236 struct sk_security_struct *sksec = sk->sk_security;
5238 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5239 sk->sk_family == PF_UNIX)
5240 isec->sid = sksec->sid;
5241 sksec->sclass = isec->sclass;
5245 * Determines peer_secid for the asoc and updates socket's peer label
5246 * if it's the first association on the socket.
5248 static int selinux_sctp_process_new_assoc(struct sctp_association *asoc,
5249 struct sk_buff *skb)
5251 struct sock *sk = asoc->base.sk;
5252 u16 family = sk->sk_family;
5253 struct sk_security_struct *sksec = sk->sk_security;
5254 struct common_audit_data ad;
5255 struct lsm_network_audit net = {0,};
5258 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5259 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5262 if (selinux_peerlbl_enabled()) {
5263 asoc->peer_secid = SECSID_NULL;
5265 /* This will return peer_sid = SECSID_NULL if there are
5266 * no peer labels, see security_net_peersid_resolve().
5268 err = selinux_skb_peerlbl_sid(skb, family, &asoc->peer_secid);
5272 if (asoc->peer_secid == SECSID_NULL)
5273 asoc->peer_secid = SECINITSID_UNLABELED;
5275 asoc->peer_secid = SECINITSID_UNLABELED;
5278 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5279 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5281 /* Here as first association on socket. As the peer SID
5282 * was allowed by peer recv (and the netif/node checks),
5283 * then it is approved by policy and used as the primary
5284 * peer SID for getpeercon(3).
5286 sksec->peer_sid = asoc->peer_secid;
5287 } else if (sksec->peer_sid != asoc->peer_secid) {
5288 /* Other association peer SIDs are checked to enforce
5289 * consistency among the peer SIDs.
5291 ad.type = LSM_AUDIT_DATA_NET;
5293 ad.u.net->sk = asoc->base.sk;
5294 err = avc_has_perm(&selinux_state,
5295 sksec->peer_sid, asoc->peer_secid,
5296 sksec->sclass, SCTP_SOCKET__ASSOCIATION,
5304 /* Called whenever SCTP receives an INIT or COOKIE ECHO chunk. This
5305 * happens on an incoming connect(2), sctp_connectx(3) or
5306 * sctp_sendmsg(3) (with no association already present).
5308 static int selinux_sctp_assoc_request(struct sctp_association *asoc,
5309 struct sk_buff *skb)
5311 struct sk_security_struct *sksec = asoc->base.sk->sk_security;
5315 if (!selinux_policycap_extsockclass())
5318 err = selinux_sctp_process_new_assoc(asoc, skb);
5322 /* Compute the MLS component for the connection and store
5323 * the information in asoc. This will be used by SCTP TCP type
5324 * sockets and peeled off connections as they cause a new
5325 * socket to be generated. selinux_sctp_sk_clone() will then
5326 * plug this into the new socket.
5328 err = selinux_conn_sid(sksec->sid, asoc->peer_secid, &conn_sid);
5332 asoc->secid = conn_sid;
5334 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5335 return selinux_netlbl_sctp_assoc_request(asoc, skb);
5338 /* Called when SCTP receives a COOKIE ACK chunk as the final
5339 * response to an association request (initited by us).
5341 static int selinux_sctp_assoc_established(struct sctp_association *asoc,
5342 struct sk_buff *skb)
5344 struct sk_security_struct *sksec = asoc->base.sk->sk_security;
5346 if (!selinux_policycap_extsockclass())
5349 /* Inherit secid from the parent socket - this will be picked up
5350 * by selinux_sctp_sk_clone() if the association gets peeled off
5351 * into a new socket.
5353 asoc->secid = sksec->sid;
5355 return selinux_sctp_process_new_assoc(asoc, skb);
5358 /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5359 * based on their @optname.
5361 static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5362 struct sockaddr *address,
5365 int len, err = 0, walk_size = 0;
5367 struct sockaddr *addr;
5368 struct socket *sock;
5370 if (!selinux_policycap_extsockclass())
5373 /* Process one or more addresses that may be IPv4 or IPv6 */
5374 sock = sk->sk_socket;
5377 while (walk_size < addrlen) {
5378 if (walk_size + sizeof(sa_family_t) > addrlen)
5382 switch (addr->sa_family) {
5385 len = sizeof(struct sockaddr_in);
5388 len = sizeof(struct sockaddr_in6);
5394 if (walk_size + len > addrlen)
5400 case SCTP_PRIMARY_ADDR:
5401 case SCTP_SET_PEER_PRIMARY_ADDR:
5402 case SCTP_SOCKOPT_BINDX_ADD:
5403 err = selinux_socket_bind(sock, addr, len);
5405 /* Connect checks */
5406 case SCTP_SOCKOPT_CONNECTX:
5407 case SCTP_PARAM_SET_PRIMARY:
5408 case SCTP_PARAM_ADD_IP:
5409 case SCTP_SENDMSG_CONNECT:
5410 err = selinux_socket_connect_helper(sock, addr, len);
5414 /* As selinux_sctp_bind_connect() is called by the
5415 * SCTP protocol layer, the socket is already locked,
5416 * therefore selinux_netlbl_socket_connect_locked()
5417 * is called here. The situations handled are:
5418 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5419 * whenever a new IP address is added or when a new
5420 * primary address is selected.
5421 * Note that an SCTP connect(2) call happens before
5422 * the SCTP protocol layer and is handled via
5423 * selinux_socket_connect().
5425 err = selinux_netlbl_socket_connect_locked(sk, addr);
5439 /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5440 static void selinux_sctp_sk_clone(struct sctp_association *asoc, struct sock *sk,
5443 struct sk_security_struct *sksec = sk->sk_security;
5444 struct sk_security_struct *newsksec = newsk->sk_security;
5446 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5447 * the non-sctp clone version.
5449 if (!selinux_policycap_extsockclass())
5450 return selinux_sk_clone_security(sk, newsk);
5452 newsksec->sid = asoc->secid;
5453 newsksec->peer_sid = asoc->peer_secid;
5454 newsksec->sclass = sksec->sclass;
5455 selinux_netlbl_sctp_sk_clone(sk, newsk);
5458 static int selinux_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
5459 struct request_sock *req)
5461 struct sk_security_struct *sksec = sk->sk_security;
5463 u16 family = req->rsk_ops->family;
5467 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
5470 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5473 req->secid = connsid;
5474 req->peer_secid = peersid;
5476 return selinux_netlbl_inet_conn_request(req, family);
5479 static void selinux_inet_csk_clone(struct sock *newsk,
5480 const struct request_sock *req)
5482 struct sk_security_struct *newsksec = newsk->sk_security;
5484 newsksec->sid = req->secid;
5485 newsksec->peer_sid = req->peer_secid;
5486 /* NOTE: Ideally, we should also get the isec->sid for the
5487 new socket in sync, but we don't have the isec available yet.
5488 So we will wait until sock_graft to do it, by which
5489 time it will have been created and available. */
5491 /* We don't need to take any sort of lock here as we are the only
5492 * thread with access to newsksec */
5493 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
5496 static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
5498 u16 family = sk->sk_family;
5499 struct sk_security_struct *sksec = sk->sk_security;
5501 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5502 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5505 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
5508 static int selinux_secmark_relabel_packet(u32 sid)
5510 const struct task_security_struct *__tsec;
5513 __tsec = selinux_cred(current_cred());
5516 return avc_has_perm(&selinux_state,
5517 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5521 static void selinux_secmark_refcount_inc(void)
5523 atomic_inc(&selinux_secmark_refcount);
5526 static void selinux_secmark_refcount_dec(void)
5528 atomic_dec(&selinux_secmark_refcount);
5531 static void selinux_req_classify_flow(const struct request_sock *req,
5532 struct flowi_common *flic)
5534 flic->flowic_secid = req->secid;
5537 static int selinux_tun_dev_alloc_security(void **security)
5539 struct tun_security_struct *tunsec;
5541 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5544 tunsec->sid = current_sid();
5550 static void selinux_tun_dev_free_security(void *security)
5555 static int selinux_tun_dev_create(void)
5557 u32 sid = current_sid();
5559 /* we aren't taking into account the "sockcreate" SID since the socket
5560 * that is being created here is not a socket in the traditional sense,
5561 * instead it is a private sock, accessible only to the kernel, and
5562 * representing a wide range of network traffic spanning multiple
5563 * connections unlike traditional sockets - check the TUN driver to
5564 * get a better understanding of why this socket is special */
5566 return avc_has_perm(&selinux_state,
5567 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
5571 static int selinux_tun_dev_attach_queue(void *security)
5573 struct tun_security_struct *tunsec = security;
5575 return avc_has_perm(&selinux_state,
5576 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
5577 TUN_SOCKET__ATTACH_QUEUE, NULL);
5580 static int selinux_tun_dev_attach(struct sock *sk, void *security)
5582 struct tun_security_struct *tunsec = security;
5583 struct sk_security_struct *sksec = sk->sk_security;
5585 /* we don't currently perform any NetLabel based labeling here and it
5586 * isn't clear that we would want to do so anyway; while we could apply
5587 * labeling without the support of the TUN user the resulting labeled
5588 * traffic from the other end of the connection would almost certainly
5589 * cause confusion to the TUN user that had no idea network labeling
5590 * protocols were being used */
5592 sksec->sid = tunsec->sid;
5593 sksec->sclass = SECCLASS_TUN_SOCKET;
5598 static int selinux_tun_dev_open(void *security)
5600 struct tun_security_struct *tunsec = security;
5601 u32 sid = current_sid();
5604 err = avc_has_perm(&selinux_state,
5605 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
5606 TUN_SOCKET__RELABELFROM, NULL);
5609 err = avc_has_perm(&selinux_state,
5610 sid, sid, SECCLASS_TUN_SOCKET,
5611 TUN_SOCKET__RELABELTO, NULL);
5619 #ifdef CONFIG_NETFILTER
5621 static unsigned int selinux_ip_forward(void *priv, struct sk_buff *skb,
5622 const struct nf_hook_state *state)
5628 struct common_audit_data ad;
5629 struct lsm_network_audit net = {0,};
5630 int secmark_active, peerlbl_active;
5632 if (!selinux_policycap_netpeer())
5635 secmark_active = selinux_secmark_enabled();
5636 peerlbl_active = selinux_peerlbl_enabled();
5637 if (!secmark_active && !peerlbl_active)
5641 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5644 ifindex = state->in->ifindex;
5645 ad.type = LSM_AUDIT_DATA_NET;
5647 ad.u.net->netif = ifindex;
5648 ad.u.net->family = family;
5649 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5652 if (peerlbl_active) {
5655 err = selinux_inet_sys_rcv_skb(state->net, ifindex,
5656 addrp, family, peer_sid, &ad);
5658 selinux_netlbl_err(skb, family, err, 1);
5664 if (avc_has_perm(&selinux_state,
5665 peer_sid, skb->secmark,
5666 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5669 if (netlbl_enabled())
5670 /* we do this in the FORWARD path and not the POST_ROUTING
5671 * path because we want to make sure we apply the necessary
5672 * labeling before IPsec is applied so we can leverage AH
5674 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5680 static unsigned int selinux_ip_output(void *priv, struct sk_buff *skb,
5681 const struct nf_hook_state *state)
5686 if (!netlbl_enabled())
5689 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5690 * because we want to make sure we apply the necessary labeling
5691 * before IPsec is applied so we can leverage AH protection */
5694 struct sk_security_struct *sksec;
5696 if (sk_listener(sk))
5697 /* if the socket is the listening state then this
5698 * packet is a SYN-ACK packet which means it needs to
5699 * be labeled based on the connection/request_sock and
5700 * not the parent socket. unfortunately, we can't
5701 * lookup the request_sock yet as it isn't queued on
5702 * the parent socket until after the SYN-ACK is sent.
5703 * the "solution" is to simply pass the packet as-is
5704 * as any IP option based labeling should be copied
5705 * from the initial connection request (in the IP
5706 * layer). it is far from ideal, but until we get a
5707 * security label in the packet itself this is the
5708 * best we can do. */
5711 /* standard practice, label using the parent socket */
5712 sksec = sk->sk_security;
5715 sid = SECINITSID_KERNEL;
5716 if (selinux_netlbl_skbuff_setsid(skb, state->pf, sid) != 0)
5723 static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5724 const struct nf_hook_state *state)
5727 struct sk_security_struct *sksec;
5728 struct common_audit_data ad;
5729 struct lsm_network_audit net = {0,};
5732 sk = skb_to_full_sk(skb);
5735 sksec = sk->sk_security;
5737 ad.type = LSM_AUDIT_DATA_NET;
5739 ad.u.net->netif = state->out->ifindex;
5740 ad.u.net->family = state->pf;
5741 if (selinux_parse_skb(skb, &ad, NULL, 0, &proto))
5744 if (selinux_secmark_enabled())
5745 if (avc_has_perm(&selinux_state,
5746 sksec->sid, skb->secmark,
5747 SECCLASS_PACKET, PACKET__SEND, &ad))
5748 return NF_DROP_ERR(-ECONNREFUSED);
5750 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5751 return NF_DROP_ERR(-ECONNREFUSED);
5756 static unsigned int selinux_ip_postroute(void *priv,
5757 struct sk_buff *skb,
5758 const struct nf_hook_state *state)
5765 struct common_audit_data ad;
5766 struct lsm_network_audit net = {0,};
5768 int secmark_active, peerlbl_active;
5770 /* If any sort of compatibility mode is enabled then handoff processing
5771 * to the selinux_ip_postroute_compat() function to deal with the
5772 * special handling. We do this in an attempt to keep this function
5773 * as fast and as clean as possible. */
5774 if (!selinux_policycap_netpeer())
5775 return selinux_ip_postroute_compat(skb, state);
5777 secmark_active = selinux_secmark_enabled();
5778 peerlbl_active = selinux_peerlbl_enabled();
5779 if (!secmark_active && !peerlbl_active)
5782 sk = skb_to_full_sk(skb);
5785 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5786 * packet transformation so allow the packet to pass without any checks
5787 * since we'll have another chance to perform access control checks
5788 * when the packet is on it's final way out.
5789 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
5790 * is NULL, in this case go ahead and apply access control.
5791 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5792 * TCP listening state we cannot wait until the XFRM processing
5793 * is done as we will miss out on the SA label if we do;
5794 * unfortunately, this means more work, but it is only once per
5796 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
5797 !(sk && sk_listener(sk)))
5803 /* Without an associated socket the packet is either coming
5804 * from the kernel or it is being forwarded; check the packet
5805 * to determine which and if the packet is being forwarded
5806 * query the packet directly to determine the security label. */
5808 secmark_perm = PACKET__FORWARD_OUT;
5809 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
5812 secmark_perm = PACKET__SEND;
5813 peer_sid = SECINITSID_KERNEL;
5815 } else if (sk_listener(sk)) {
5816 /* Locally generated packet but the associated socket is in the
5817 * listening state which means this is a SYN-ACK packet. In
5818 * this particular case the correct security label is assigned
5819 * to the connection/request_sock but unfortunately we can't
5820 * query the request_sock as it isn't queued on the parent
5821 * socket until after the SYN-ACK packet is sent; the only
5822 * viable choice is to regenerate the label like we do in
5823 * selinux_inet_conn_request(). See also selinux_ip_output()
5824 * for similar problems. */
5826 struct sk_security_struct *sksec;
5828 sksec = sk->sk_security;
5829 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5831 /* At this point, if the returned skb peerlbl is SECSID_NULL
5832 * and the packet has been through at least one XFRM
5833 * transformation then we must be dealing with the "final"
5834 * form of labeled IPsec packet; since we've already applied
5835 * all of our access controls on this packet we can safely
5836 * pass the packet. */
5837 if (skb_sid == SECSID_NULL) {
5840 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5844 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5848 return NF_DROP_ERR(-ECONNREFUSED);
5851 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5853 secmark_perm = PACKET__SEND;
5855 /* Locally generated packet, fetch the security label from the
5856 * associated socket. */
5857 struct sk_security_struct *sksec = sk->sk_security;
5858 peer_sid = sksec->sid;
5859 secmark_perm = PACKET__SEND;
5862 ifindex = state->out->ifindex;
5863 ad.type = LSM_AUDIT_DATA_NET;
5865 ad.u.net->netif = ifindex;
5866 ad.u.net->family = family;
5867 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
5871 if (avc_has_perm(&selinux_state,
5872 peer_sid, skb->secmark,
5873 SECCLASS_PACKET, secmark_perm, &ad))
5874 return NF_DROP_ERR(-ECONNREFUSED);
5876 if (peerlbl_active) {
5880 if (sel_netif_sid(state->net, ifindex, &if_sid))
5882 if (avc_has_perm(&selinux_state,
5884 SECCLASS_NETIF, NETIF__EGRESS, &ad))
5885 return NF_DROP_ERR(-ECONNREFUSED);
5887 if (sel_netnode_sid(addrp, family, &node_sid))
5889 if (avc_has_perm(&selinux_state,
5891 SECCLASS_NODE, NODE__SENDTO, &ad))
5892 return NF_DROP_ERR(-ECONNREFUSED);
5897 #endif /* CONFIG_NETFILTER */
5899 static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5902 unsigned int msg_len;
5903 unsigned int data_len = skb->len;
5904 unsigned char *data = skb->data;
5905 struct nlmsghdr *nlh;
5906 struct sk_security_struct *sksec = sk->sk_security;
5907 u16 sclass = sksec->sclass;
5910 while (data_len >= nlmsg_total_size(0)) {
5911 nlh = (struct nlmsghdr *)data;
5913 /* NOTE: the nlmsg_len field isn't reliably set by some netlink
5914 * users which means we can't reject skb's with bogus
5915 * length fields; our solution is to follow what
5916 * netlink_rcv_skb() does and simply skip processing at
5917 * messages with length fields that are clearly junk
5919 if (nlh->nlmsg_len < NLMSG_HDRLEN || nlh->nlmsg_len > data_len)
5922 rc = selinux_nlmsg_lookup(sclass, nlh->nlmsg_type, &perm);
5924 rc = sock_has_perm(sk, perm);
5927 } else if (rc == -EINVAL) {
5928 /* -EINVAL is a missing msg/perm mapping */
5929 pr_warn_ratelimited("SELinux: unrecognized netlink"
5930 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5931 " pid=%d comm=%s\n",
5932 sk->sk_protocol, nlh->nlmsg_type,
5933 secclass_map[sclass - 1].name,
5934 task_pid_nr(current), current->comm);
5935 if (enforcing_enabled(&selinux_state) &&
5936 !security_get_allow_unknown(&selinux_state))
5939 } else if (rc == -ENOENT) {
5940 /* -ENOENT is a missing socket/class mapping, ignore */
5946 /* move to the next message after applying netlink padding */
5947 msg_len = NLMSG_ALIGN(nlh->nlmsg_len);
5948 if (msg_len >= data_len)
5950 data_len -= msg_len;
5957 static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass)
5959 isec->sclass = sclass;
5960 isec->sid = current_sid();
5963 static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
5966 struct ipc_security_struct *isec;
5967 struct common_audit_data ad;
5968 u32 sid = current_sid();
5970 isec = selinux_ipc(ipc_perms);
5972 ad.type = LSM_AUDIT_DATA_IPC;
5973 ad.u.ipc_id = ipc_perms->key;
5975 return avc_has_perm(&selinux_state,
5976 sid, isec->sid, isec->sclass, perms, &ad);
5979 static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5981 struct msg_security_struct *msec;
5983 msec = selinux_msg_msg(msg);
5984 msec->sid = SECINITSID_UNLABELED;
5989 /* message queue security operations */
5990 static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
5992 struct ipc_security_struct *isec;
5993 struct common_audit_data ad;
5994 u32 sid = current_sid();
5997 isec = selinux_ipc(msq);
5998 ipc_init_security(isec, SECCLASS_MSGQ);
6000 ad.type = LSM_AUDIT_DATA_IPC;
6001 ad.u.ipc_id = msq->key;
6003 rc = avc_has_perm(&selinux_state,
6004 sid, isec->sid, SECCLASS_MSGQ,
6009 static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
6011 struct ipc_security_struct *isec;
6012 struct common_audit_data ad;
6013 u32 sid = current_sid();
6015 isec = selinux_ipc(msq);
6017 ad.type = LSM_AUDIT_DATA_IPC;
6018 ad.u.ipc_id = msq->key;
6020 return avc_has_perm(&selinux_state,
6021 sid, isec->sid, SECCLASS_MSGQ,
6022 MSGQ__ASSOCIATE, &ad);
6025 static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
6033 /* No specific object, just general system-wide information. */
6034 return avc_has_perm(&selinux_state,
6035 current_sid(), SECINITSID_KERNEL,
6036 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
6040 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
6043 perms = MSGQ__SETATTR;
6046 perms = MSGQ__DESTROY;
6052 err = ipc_has_perm(msq, perms);
6056 static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
6058 struct ipc_security_struct *isec;
6059 struct msg_security_struct *msec;
6060 struct common_audit_data ad;
6061 u32 sid = current_sid();
6064 isec = selinux_ipc(msq);
6065 msec = selinux_msg_msg(msg);
6068 * First time through, need to assign label to the message
6070 if (msec->sid == SECINITSID_UNLABELED) {
6072 * Compute new sid based on current process and
6073 * message queue this message will be stored in
6075 rc = security_transition_sid(&selinux_state, sid, isec->sid,
6076 SECCLASS_MSG, NULL, &msec->sid);
6081 ad.type = LSM_AUDIT_DATA_IPC;
6082 ad.u.ipc_id = msq->key;
6084 /* Can this process write to the queue? */
6085 rc = avc_has_perm(&selinux_state,
6086 sid, isec->sid, SECCLASS_MSGQ,
6089 /* Can this process send the message */
6090 rc = avc_has_perm(&selinux_state,
6091 sid, msec->sid, SECCLASS_MSG,
6094 /* Can the message be put in the queue? */
6095 rc = avc_has_perm(&selinux_state,
6096 msec->sid, isec->sid, SECCLASS_MSGQ,
6097 MSGQ__ENQUEUE, &ad);
6102 static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
6103 struct task_struct *target,
6104 long type, int mode)
6106 struct ipc_security_struct *isec;
6107 struct msg_security_struct *msec;
6108 struct common_audit_data ad;
6109 u32 sid = task_sid_obj(target);
6112 isec = selinux_ipc(msq);
6113 msec = selinux_msg_msg(msg);
6115 ad.type = LSM_AUDIT_DATA_IPC;
6116 ad.u.ipc_id = msq->key;
6118 rc = avc_has_perm(&selinux_state,
6120 SECCLASS_MSGQ, MSGQ__READ, &ad);
6122 rc = avc_has_perm(&selinux_state,
6124 SECCLASS_MSG, MSG__RECEIVE, &ad);
6128 /* Shared Memory security operations */
6129 static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
6131 struct ipc_security_struct *isec;
6132 struct common_audit_data ad;
6133 u32 sid = current_sid();
6136 isec = selinux_ipc(shp);
6137 ipc_init_security(isec, SECCLASS_SHM);
6139 ad.type = LSM_AUDIT_DATA_IPC;
6140 ad.u.ipc_id = shp->key;
6142 rc = avc_has_perm(&selinux_state,
6143 sid, isec->sid, SECCLASS_SHM,
6148 static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
6150 struct ipc_security_struct *isec;
6151 struct common_audit_data ad;
6152 u32 sid = current_sid();
6154 isec = selinux_ipc(shp);
6156 ad.type = LSM_AUDIT_DATA_IPC;
6157 ad.u.ipc_id = shp->key;
6159 return avc_has_perm(&selinux_state,
6160 sid, isec->sid, SECCLASS_SHM,
6161 SHM__ASSOCIATE, &ad);
6164 /* Note, at this point, shp is locked down */
6165 static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
6173 /* No specific object, just general system-wide information. */
6174 return avc_has_perm(&selinux_state,
6175 current_sid(), SECINITSID_KERNEL,
6176 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
6180 perms = SHM__GETATTR | SHM__ASSOCIATE;
6183 perms = SHM__SETATTR;
6190 perms = SHM__DESTROY;
6196 err = ipc_has_perm(shp, perms);
6200 static int selinux_shm_shmat(struct kern_ipc_perm *shp,
6201 char __user *shmaddr, int shmflg)
6205 if (shmflg & SHM_RDONLY)
6208 perms = SHM__READ | SHM__WRITE;
6210 return ipc_has_perm(shp, perms);
6213 /* Semaphore security operations */
6214 static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
6216 struct ipc_security_struct *isec;
6217 struct common_audit_data ad;
6218 u32 sid = current_sid();
6221 isec = selinux_ipc(sma);
6222 ipc_init_security(isec, SECCLASS_SEM);
6224 ad.type = LSM_AUDIT_DATA_IPC;
6225 ad.u.ipc_id = sma->key;
6227 rc = avc_has_perm(&selinux_state,
6228 sid, isec->sid, SECCLASS_SEM,
6233 static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
6235 struct ipc_security_struct *isec;
6236 struct common_audit_data ad;
6237 u32 sid = current_sid();
6239 isec = selinux_ipc(sma);
6241 ad.type = LSM_AUDIT_DATA_IPC;
6242 ad.u.ipc_id = sma->key;
6244 return avc_has_perm(&selinux_state,
6245 sid, isec->sid, SECCLASS_SEM,
6246 SEM__ASSOCIATE, &ad);
6249 /* Note, at this point, sma is locked down */
6250 static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
6258 /* No specific object, just general system-wide information. */
6259 return avc_has_perm(&selinux_state,
6260 current_sid(), SECINITSID_KERNEL,
6261 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
6265 perms = SEM__GETATTR;
6276 perms = SEM__DESTROY;
6279 perms = SEM__SETATTR;
6284 perms = SEM__GETATTR | SEM__ASSOCIATE;
6290 err = ipc_has_perm(sma, perms);
6294 static int selinux_sem_semop(struct kern_ipc_perm *sma,
6295 struct sembuf *sops, unsigned nsops, int alter)
6300 perms = SEM__READ | SEM__WRITE;
6304 return ipc_has_perm(sma, perms);
6307 static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6313 av |= IPC__UNIX_READ;
6315 av |= IPC__UNIX_WRITE;
6320 return ipc_has_perm(ipcp, av);
6323 static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6325 struct ipc_security_struct *isec = selinux_ipc(ipcp);
6329 static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
6332 inode_doinit_with_dentry(inode, dentry);
6335 static int selinux_getprocattr(struct task_struct *p,
6336 char *name, char **value)
6338 const struct task_security_struct *__tsec;
6344 __tsec = selinux_cred(__task_cred(p));
6347 error = avc_has_perm(&selinux_state,
6348 current_sid(), __tsec->sid,
6349 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6354 if (!strcmp(name, "current"))
6356 else if (!strcmp(name, "prev"))
6358 else if (!strcmp(name, "exec"))
6359 sid = __tsec->exec_sid;
6360 else if (!strcmp(name, "fscreate"))
6361 sid = __tsec->create_sid;
6362 else if (!strcmp(name, "keycreate"))
6363 sid = __tsec->keycreate_sid;
6364 else if (!strcmp(name, "sockcreate"))
6365 sid = __tsec->sockcreate_sid;
6375 error = security_sid_to_context(&selinux_state, sid, value, &len);
6385 static int selinux_setprocattr(const char *name, void *value, size_t size)
6387 struct task_security_struct *tsec;
6389 u32 mysid = current_sid(), sid = 0, ptsid;
6394 * Basic control over ability to set these attributes at all.
6396 if (!strcmp(name, "exec"))
6397 error = avc_has_perm(&selinux_state,
6398 mysid, mysid, SECCLASS_PROCESS,
6399 PROCESS__SETEXEC, NULL);
6400 else if (!strcmp(name, "fscreate"))
6401 error = avc_has_perm(&selinux_state,
6402 mysid, mysid, SECCLASS_PROCESS,
6403 PROCESS__SETFSCREATE, NULL);
6404 else if (!strcmp(name, "keycreate"))
6405 error = avc_has_perm(&selinux_state,
6406 mysid, mysid, SECCLASS_PROCESS,
6407 PROCESS__SETKEYCREATE, NULL);
6408 else if (!strcmp(name, "sockcreate"))
6409 error = avc_has_perm(&selinux_state,
6410 mysid, mysid, SECCLASS_PROCESS,
6411 PROCESS__SETSOCKCREATE, NULL);
6412 else if (!strcmp(name, "current"))
6413 error = avc_has_perm(&selinux_state,
6414 mysid, mysid, SECCLASS_PROCESS,
6415 PROCESS__SETCURRENT, NULL);
6421 /* Obtain a SID for the context, if one was specified. */
6422 if (size && str[0] && str[0] != '\n') {
6423 if (str[size-1] == '\n') {
6427 error = security_context_to_sid(&selinux_state, value, size,
6429 if (error == -EINVAL && !strcmp(name, "fscreate")) {
6430 if (!has_cap_mac_admin(true)) {
6431 struct audit_buffer *ab;
6434 /* We strip a nul only if it is at the end, otherwise the
6435 * context contains a nul and we should audit that */
6436 if (str[size - 1] == '\0')
6437 audit_size = size - 1;
6440 ab = audit_log_start(audit_context(),
6445 audit_log_format(ab, "op=fscreate invalid_context=");
6446 audit_log_n_untrustedstring(ab, value, audit_size);
6451 error = security_context_to_sid_force(
6459 new = prepare_creds();
6463 /* Permission checking based on the specified context is
6464 performed during the actual operation (execve,
6465 open/mkdir/...), when we know the full context of the
6466 operation. See selinux_bprm_creds_for_exec for the execve
6467 checks and may_create for the file creation checks. The
6468 operation will then fail if the context is not permitted. */
6469 tsec = selinux_cred(new);
6470 if (!strcmp(name, "exec")) {
6471 tsec->exec_sid = sid;
6472 } else if (!strcmp(name, "fscreate")) {
6473 tsec->create_sid = sid;
6474 } else if (!strcmp(name, "keycreate")) {
6476 error = avc_has_perm(&selinux_state, mysid, sid,
6477 SECCLASS_KEY, KEY__CREATE, NULL);
6481 tsec->keycreate_sid = sid;
6482 } else if (!strcmp(name, "sockcreate")) {
6483 tsec->sockcreate_sid = sid;
6484 } else if (!strcmp(name, "current")) {
6489 /* Only allow single threaded processes to change context */
6491 if (!current_is_single_threaded()) {
6492 error = security_bounded_transition(&selinux_state,
6498 /* Check permissions for the transition. */
6499 error = avc_has_perm(&selinux_state,
6500 tsec->sid, sid, SECCLASS_PROCESS,
6501 PROCESS__DYNTRANSITION, NULL);
6505 /* Check for ptracing, and update the task SID if ok.
6506 Otherwise, leave SID unchanged and fail. */
6507 ptsid = ptrace_parent_sid();
6509 error = avc_has_perm(&selinux_state,
6510 ptsid, sid, SECCLASS_PROCESS,
6511 PROCESS__PTRACE, NULL);
6530 static int selinux_ismaclabel(const char *name)
6532 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6535 static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6537 return security_sid_to_context(&selinux_state, secid,
6541 static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
6543 return security_context_to_sid(&selinux_state, secdata, seclen,
6547 static void selinux_release_secctx(char *secdata, u32 seclen)
6552 static void selinux_inode_invalidate_secctx(struct inode *inode)
6554 struct inode_security_struct *isec = selinux_inode(inode);
6556 spin_lock(&isec->lock);
6557 isec->initialized = LABEL_INVALID;
6558 spin_unlock(&isec->lock);
6562 * called with inode->i_mutex locked
6564 static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6566 int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
6568 /* Do not return error when suppressing label (SBLABEL_MNT not set). */
6569 return rc == -EOPNOTSUPP ? 0 : rc;
6573 * called with inode->i_mutex locked
6575 static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6577 return __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_SELINUX,
6581 static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6584 len = selinux_inode_getsecurity(&init_user_ns, inode,
6585 XATTR_SELINUX_SUFFIX, ctx, true);
6593 static int selinux_key_alloc(struct key *k, const struct cred *cred,
6594 unsigned long flags)
6596 const struct task_security_struct *tsec;
6597 struct key_security_struct *ksec;
6599 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6603 tsec = selinux_cred(cred);
6604 if (tsec->keycreate_sid)
6605 ksec->sid = tsec->keycreate_sid;
6607 ksec->sid = tsec->sid;
6613 static void selinux_key_free(struct key *k)
6615 struct key_security_struct *ksec = k->security;
6621 static int selinux_key_permission(key_ref_t key_ref,
6622 const struct cred *cred,
6623 enum key_need_perm need_perm)
6626 struct key_security_struct *ksec;
6629 switch (need_perm) {
6636 case KEY_NEED_WRITE:
6639 case KEY_NEED_SEARCH:
6645 case KEY_NEED_SETATTR:
6646 perm = KEY__SETATTR;
6648 case KEY_NEED_UNLINK:
6649 case KEY_SYSADMIN_OVERRIDE:
6650 case KEY_AUTHTOKEN_OVERRIDE:
6651 case KEY_DEFER_PERM_CHECK:
6659 sid = cred_sid(cred);
6660 key = key_ref_to_ptr(key_ref);
6661 ksec = key->security;
6663 return avc_has_perm(&selinux_state,
6664 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
6667 static int selinux_key_getsecurity(struct key *key, char **_buffer)
6669 struct key_security_struct *ksec = key->security;
6670 char *context = NULL;
6674 rc = security_sid_to_context(&selinux_state, ksec->sid,
6682 #ifdef CONFIG_KEY_NOTIFICATIONS
6683 static int selinux_watch_key(struct key *key)
6685 struct key_security_struct *ksec = key->security;
6686 u32 sid = current_sid();
6688 return avc_has_perm(&selinux_state,
6689 sid, ksec->sid, SECCLASS_KEY, KEY__VIEW, NULL);
6694 #ifdef CONFIG_SECURITY_INFINIBAND
6695 static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6697 struct common_audit_data ad;
6700 struct ib_security_struct *sec = ib_sec;
6701 struct lsm_ibpkey_audit ibpkey;
6703 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
6707 ad.type = LSM_AUDIT_DATA_IBPKEY;
6708 ibpkey.subnet_prefix = subnet_prefix;
6709 ibpkey.pkey = pkey_val;
6710 ad.u.ibpkey = &ibpkey;
6711 return avc_has_perm(&selinux_state,
6713 SECCLASS_INFINIBAND_PKEY,
6714 INFINIBAND_PKEY__ACCESS, &ad);
6717 static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6720 struct common_audit_data ad;
6723 struct ib_security_struct *sec = ib_sec;
6724 struct lsm_ibendport_audit ibendport;
6726 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6732 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6733 ibendport.dev_name = dev_name;
6734 ibendport.port = port_num;
6735 ad.u.ibendport = &ibendport;
6736 return avc_has_perm(&selinux_state,
6738 SECCLASS_INFINIBAND_ENDPORT,
6739 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6742 static int selinux_ib_alloc_security(void **ib_sec)
6744 struct ib_security_struct *sec;
6746 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6749 sec->sid = current_sid();
6755 static void selinux_ib_free_security(void *ib_sec)
6761 #ifdef CONFIG_BPF_SYSCALL
6762 static int selinux_bpf(int cmd, union bpf_attr *attr,
6765 u32 sid = current_sid();
6769 case BPF_MAP_CREATE:
6770 ret = avc_has_perm(&selinux_state,
6771 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
6775 ret = avc_has_perm(&selinux_state,
6776 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
6787 static u32 bpf_map_fmode_to_av(fmode_t fmode)
6791 if (fmode & FMODE_READ)
6792 av |= BPF__MAP_READ;
6793 if (fmode & FMODE_WRITE)
6794 av |= BPF__MAP_WRITE;
6798 /* This function will check the file pass through unix socket or binder to see
6799 * if it is a bpf related object. And apply correspinding checks on the bpf
6800 * object based on the type. The bpf maps and programs, not like other files and
6801 * socket, are using a shared anonymous inode inside the kernel as their inode.
6802 * So checking that inode cannot identify if the process have privilege to
6803 * access the bpf object and that's why we have to add this additional check in
6804 * selinux_file_receive and selinux_binder_transfer_files.
6806 static int bpf_fd_pass(struct file *file, u32 sid)
6808 struct bpf_security_struct *bpfsec;
6809 struct bpf_prog *prog;
6810 struct bpf_map *map;
6813 if (file->f_op == &bpf_map_fops) {
6814 map = file->private_data;
6815 bpfsec = map->security;
6816 ret = avc_has_perm(&selinux_state,
6817 sid, bpfsec->sid, SECCLASS_BPF,
6818 bpf_map_fmode_to_av(file->f_mode), NULL);
6821 } else if (file->f_op == &bpf_prog_fops) {
6822 prog = file->private_data;
6823 bpfsec = prog->aux->security;
6824 ret = avc_has_perm(&selinux_state,
6825 sid, bpfsec->sid, SECCLASS_BPF,
6826 BPF__PROG_RUN, NULL);
6833 static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6835 u32 sid = current_sid();
6836 struct bpf_security_struct *bpfsec;
6838 bpfsec = map->security;
6839 return avc_has_perm(&selinux_state,
6840 sid, bpfsec->sid, SECCLASS_BPF,
6841 bpf_map_fmode_to_av(fmode), NULL);
6844 static int selinux_bpf_prog(struct bpf_prog *prog)
6846 u32 sid = current_sid();
6847 struct bpf_security_struct *bpfsec;
6849 bpfsec = prog->aux->security;
6850 return avc_has_perm(&selinux_state,
6851 sid, bpfsec->sid, SECCLASS_BPF,
6852 BPF__PROG_RUN, NULL);
6855 static int selinux_bpf_map_alloc(struct bpf_map *map)
6857 struct bpf_security_struct *bpfsec;
6859 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6863 bpfsec->sid = current_sid();
6864 map->security = bpfsec;
6869 static void selinux_bpf_map_free(struct bpf_map *map)
6871 struct bpf_security_struct *bpfsec = map->security;
6873 map->security = NULL;
6877 static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6879 struct bpf_security_struct *bpfsec;
6881 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6885 bpfsec->sid = current_sid();
6886 aux->security = bpfsec;
6891 static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6893 struct bpf_security_struct *bpfsec = aux->security;
6895 aux->security = NULL;
6900 struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
6901 .lbs_cred = sizeof(struct task_security_struct),
6902 .lbs_file = sizeof(struct file_security_struct),
6903 .lbs_inode = sizeof(struct inode_security_struct),
6904 .lbs_ipc = sizeof(struct ipc_security_struct),
6905 .lbs_msg_msg = sizeof(struct msg_security_struct),
6906 .lbs_superblock = sizeof(struct superblock_security_struct),
6909 #ifdef CONFIG_PERF_EVENTS
6910 static int selinux_perf_event_open(struct perf_event_attr *attr, int type)
6912 u32 requested, sid = current_sid();
6914 if (type == PERF_SECURITY_OPEN)
6915 requested = PERF_EVENT__OPEN;
6916 else if (type == PERF_SECURITY_CPU)
6917 requested = PERF_EVENT__CPU;
6918 else if (type == PERF_SECURITY_KERNEL)
6919 requested = PERF_EVENT__KERNEL;
6920 else if (type == PERF_SECURITY_TRACEPOINT)
6921 requested = PERF_EVENT__TRACEPOINT;
6925 return avc_has_perm(&selinux_state, sid, sid, SECCLASS_PERF_EVENT,
6929 static int selinux_perf_event_alloc(struct perf_event *event)
6931 struct perf_event_security_struct *perfsec;
6933 perfsec = kzalloc(sizeof(*perfsec), GFP_KERNEL);
6937 perfsec->sid = current_sid();
6938 event->security = perfsec;
6943 static void selinux_perf_event_free(struct perf_event *event)
6945 struct perf_event_security_struct *perfsec = event->security;
6947 event->security = NULL;
6951 static int selinux_perf_event_read(struct perf_event *event)
6953 struct perf_event_security_struct *perfsec = event->security;
6954 u32 sid = current_sid();
6956 return avc_has_perm(&selinux_state, sid, perfsec->sid,
6957 SECCLASS_PERF_EVENT, PERF_EVENT__READ, NULL);
6960 static int selinux_perf_event_write(struct perf_event *event)
6962 struct perf_event_security_struct *perfsec = event->security;
6963 u32 sid = current_sid();
6965 return avc_has_perm(&selinux_state, sid, perfsec->sid,
6966 SECCLASS_PERF_EVENT, PERF_EVENT__WRITE, NULL);
6970 #ifdef CONFIG_IO_URING
6972 * selinux_uring_override_creds - check the requested cred override
6973 * @new: the target creds
6975 * Check to see if the current task is allowed to override it's credentials
6976 * to service an io_uring operation.
6978 static int selinux_uring_override_creds(const struct cred *new)
6980 return avc_has_perm(&selinux_state, current_sid(), cred_sid(new),
6981 SECCLASS_IO_URING, IO_URING__OVERRIDE_CREDS, NULL);
6985 * selinux_uring_sqpoll - check if a io_uring polling thread can be created
6987 * Check to see if the current task is allowed to create a new io_uring
6988 * kernel polling thread.
6990 static int selinux_uring_sqpoll(void)
6992 int sid = current_sid();
6994 return avc_has_perm(&selinux_state, sid, sid,
6995 SECCLASS_IO_URING, IO_URING__SQPOLL, NULL);
6997 #endif /* CONFIG_IO_URING */
7000 * IMPORTANT NOTE: When adding new hooks, please be careful to keep this order:
7001 * 1. any hooks that don't belong to (2.) or (3.) below,
7002 * 2. hooks that both access structures allocated by other hooks, and allocate
7003 * structures that can be later accessed by other hooks (mostly "cloning"
7005 * 3. hooks that only allocate structures that can be later accessed by other
7006 * hooks ("allocating" hooks).
7008 * Please follow block comment delimiters in the list to keep this order.
7010 * This ordering is needed for SELinux runtime disable to work at least somewhat
7011 * safely. Breaking the ordering rules above might lead to NULL pointer derefs
7012 * when disabling SELinux at runtime.
7014 static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
7015 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
7016 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
7017 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
7018 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
7020 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
7021 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
7022 LSM_HOOK_INIT(capget, selinux_capget),
7023 LSM_HOOK_INIT(capset, selinux_capset),
7024 LSM_HOOK_INIT(capable, selinux_capable),
7025 LSM_HOOK_INIT(quotactl, selinux_quotactl),
7026 LSM_HOOK_INIT(quota_on, selinux_quota_on),
7027 LSM_HOOK_INIT(syslog, selinux_syslog),
7028 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
7030 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
7032 LSM_HOOK_INIT(bprm_creds_for_exec, selinux_bprm_creds_for_exec),
7033 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
7034 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
7036 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
7037 LSM_HOOK_INIT(sb_mnt_opts_compat, selinux_sb_mnt_opts_compat),
7038 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
7039 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
7040 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
7041 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
7042 LSM_HOOK_INIT(sb_mount, selinux_mount),
7043 LSM_HOOK_INIT(sb_umount, selinux_umount),
7044 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
7045 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
7047 LSM_HOOK_INIT(move_mount, selinux_move_mount),
7049 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
7050 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
7052 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
7053 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
7054 LSM_HOOK_INIT(inode_init_security_anon, selinux_inode_init_security_anon),
7055 LSM_HOOK_INIT(inode_create, selinux_inode_create),
7056 LSM_HOOK_INIT(inode_link, selinux_inode_link),
7057 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
7058 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
7059 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
7060 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
7061 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
7062 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
7063 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
7064 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
7065 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
7066 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
7067 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
7068 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
7069 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
7070 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
7071 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
7072 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
7073 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
7074 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
7075 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
7076 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
7077 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
7078 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
7079 LSM_HOOK_INIT(path_notify, selinux_path_notify),
7081 LSM_HOOK_INIT(kernfs_init_security, selinux_kernfs_init_security),
7083 LSM_HOOK_INIT(file_permission, selinux_file_permission),
7084 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
7085 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
7086 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
7087 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
7088 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
7089 LSM_HOOK_INIT(file_lock, selinux_file_lock),
7090 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
7091 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
7092 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
7093 LSM_HOOK_INIT(file_receive, selinux_file_receive),
7095 LSM_HOOK_INIT(file_open, selinux_file_open),
7097 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
7098 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
7099 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
7100 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
7101 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
7102 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
7103 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
7104 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
7105 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
7106 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
7107 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
7108 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
7109 LSM_HOOK_INIT(current_getsecid_subj, selinux_current_getsecid_subj),
7110 LSM_HOOK_INIT(task_getsecid_obj, selinux_task_getsecid_obj),
7111 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
7112 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
7113 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
7114 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
7115 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
7116 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
7117 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
7118 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
7119 LSM_HOOK_INIT(task_kill, selinux_task_kill),
7120 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
7122 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
7123 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
7125 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
7126 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
7127 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
7128 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
7130 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
7131 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
7132 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
7134 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
7135 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
7136 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
7138 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
7140 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
7141 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
7143 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
7144 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
7145 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
7146 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
7147 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
7148 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
7150 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
7151 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
7153 LSM_HOOK_INIT(socket_create, selinux_socket_create),
7154 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
7155 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
7156 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
7157 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
7158 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
7159 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
7160 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
7161 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
7162 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
7163 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
7164 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
7165 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
7166 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
7167 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
7168 LSM_HOOK_INIT(socket_getpeersec_stream,
7169 selinux_socket_getpeersec_stream),
7170 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
7171 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
7172 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
7173 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
7174 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
7175 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
7176 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
7177 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
7178 LSM_HOOK_INIT(sctp_assoc_established, selinux_sctp_assoc_established),
7179 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
7180 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
7181 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
7182 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
7183 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
7184 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
7185 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
7186 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
7187 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
7188 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
7189 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
7190 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
7191 #ifdef CONFIG_SECURITY_INFINIBAND
7192 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
7193 LSM_HOOK_INIT(ib_endport_manage_subnet,
7194 selinux_ib_endport_manage_subnet),
7195 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
7197 #ifdef CONFIG_SECURITY_NETWORK_XFRM
7198 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
7199 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
7200 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
7201 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
7202 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
7203 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
7204 selinux_xfrm_state_pol_flow_match),
7205 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
7209 LSM_HOOK_INIT(key_free, selinux_key_free),
7210 LSM_HOOK_INIT(key_permission, selinux_key_permission),
7211 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
7212 #ifdef CONFIG_KEY_NOTIFICATIONS
7213 LSM_HOOK_INIT(watch_key, selinux_watch_key),
7218 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
7219 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
7220 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
7223 #ifdef CONFIG_BPF_SYSCALL
7224 LSM_HOOK_INIT(bpf, selinux_bpf),
7225 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
7226 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
7227 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
7228 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
7231 #ifdef CONFIG_PERF_EVENTS
7232 LSM_HOOK_INIT(perf_event_open, selinux_perf_event_open),
7233 LSM_HOOK_INIT(perf_event_free, selinux_perf_event_free),
7234 LSM_HOOK_INIT(perf_event_read, selinux_perf_event_read),
7235 LSM_HOOK_INIT(perf_event_write, selinux_perf_event_write),
7238 #ifdef CONFIG_IO_URING
7239 LSM_HOOK_INIT(uring_override_creds, selinux_uring_override_creds),
7240 LSM_HOOK_INIT(uring_sqpoll, selinux_uring_sqpoll),
7244 * PUT "CLONING" (ACCESSING + ALLOCATING) HOOKS HERE
7246 LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup),
7247 LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),
7248 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
7249 #ifdef CONFIG_SECURITY_NETWORK_XFRM
7250 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
7254 * PUT "ALLOCATING" HOOKS HERE
7256 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
7257 LSM_HOOK_INIT(msg_queue_alloc_security,
7258 selinux_msg_queue_alloc_security),
7259 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
7260 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
7261 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
7262 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
7263 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
7264 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
7265 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
7266 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
7267 #ifdef CONFIG_SECURITY_INFINIBAND
7268 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
7270 #ifdef CONFIG_SECURITY_NETWORK_XFRM
7271 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
7272 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
7273 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
7274 selinux_xfrm_state_alloc_acquire),
7277 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
7280 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
7282 #ifdef CONFIG_BPF_SYSCALL
7283 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
7284 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
7286 #ifdef CONFIG_PERF_EVENTS
7287 LSM_HOOK_INIT(perf_event_alloc, selinux_perf_event_alloc),
7291 static __init int selinux_init(void)
7293 pr_info("SELinux: Initializing.\n");
7295 memset(&selinux_state, 0, sizeof(selinux_state));
7296 enforcing_set(&selinux_state, selinux_enforcing_boot);
7297 checkreqprot_set(&selinux_state, selinux_checkreqprot_boot);
7298 selinux_avc_init(&selinux_state.avc);
7299 mutex_init(&selinux_state.status_lock);
7300 mutex_init(&selinux_state.policy_mutex);
7302 /* Set the security state for the initial task. */
7303 cred_init_security();
7305 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
7311 ebitmap_cache_init();
7313 hashtab_cache_init();
7315 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
7317 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7318 panic("SELinux: Unable to register AVC netcache callback\n");
7320 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
7321 panic("SELinux: Unable to register AVC LSM notifier callback\n");
7323 if (selinux_enforcing_boot)
7324 pr_debug("SELinux: Starting in enforcing mode\n");
7326 pr_debug("SELinux: Starting in permissive mode\n");
7328 fs_validate_description("selinux", selinux_fs_parameters);
7333 static void delayed_superblock_init(struct super_block *sb, void *unused)
7335 selinux_set_mnt_opts(sb, NULL, 0, NULL);
7338 void selinux_complete_init(void)
7340 pr_debug("SELinux: Completing initialization.\n");
7342 /* Set up any superblocks initialized prior to the policy load. */
7343 pr_debug("SELinux: Setting up existing superblocks.\n");
7344 iterate_supers(delayed_superblock_init, NULL);
7347 /* SELinux requires early initialization in order to label
7348 all processes and objects when they are created. */
7349 DEFINE_LSM(selinux) = {
7351 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
7352 .enabled = &selinux_enabled_boot,
7353 .blobs = &selinux_blob_sizes,
7354 .init = selinux_init,
7357 #if defined(CONFIG_NETFILTER)
7359 static const struct nf_hook_ops selinux_nf_ops[] = {
7361 .hook = selinux_ip_postroute,
7363 .hooknum = NF_INET_POST_ROUTING,
7364 .priority = NF_IP_PRI_SELINUX_LAST,
7367 .hook = selinux_ip_forward,
7369 .hooknum = NF_INET_FORWARD,
7370 .priority = NF_IP_PRI_SELINUX_FIRST,
7373 .hook = selinux_ip_output,
7375 .hooknum = NF_INET_LOCAL_OUT,
7376 .priority = NF_IP_PRI_SELINUX_FIRST,
7378 #if IS_ENABLED(CONFIG_IPV6)
7380 .hook = selinux_ip_postroute,
7382 .hooknum = NF_INET_POST_ROUTING,
7383 .priority = NF_IP6_PRI_SELINUX_LAST,
7386 .hook = selinux_ip_forward,
7388 .hooknum = NF_INET_FORWARD,
7389 .priority = NF_IP6_PRI_SELINUX_FIRST,
7392 .hook = selinux_ip_output,
7394 .hooknum = NF_INET_LOCAL_OUT,
7395 .priority = NF_IP6_PRI_SELINUX_FIRST,
7400 static int __net_init selinux_nf_register(struct net *net)
7402 return nf_register_net_hooks(net, selinux_nf_ops,
7403 ARRAY_SIZE(selinux_nf_ops));
7406 static void __net_exit selinux_nf_unregister(struct net *net)
7408 nf_unregister_net_hooks(net, selinux_nf_ops,
7409 ARRAY_SIZE(selinux_nf_ops));
7412 static struct pernet_operations selinux_net_ops = {
7413 .init = selinux_nf_register,
7414 .exit = selinux_nf_unregister,
7417 static int __init selinux_nf_ip_init(void)
7421 if (!selinux_enabled_boot)
7424 pr_debug("SELinux: Registering netfilter hooks\n");
7426 err = register_pernet_subsys(&selinux_net_ops);
7428 panic("SELinux: register_pernet_subsys: error %d\n", err);
7432 __initcall(selinux_nf_ip_init);
7434 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7435 static void selinux_nf_ip_exit(void)
7437 pr_debug("SELinux: Unregistering netfilter hooks\n");
7439 unregister_pernet_subsys(&selinux_net_ops);
7443 #else /* CONFIG_NETFILTER */
7445 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7446 #define selinux_nf_ip_exit()
7449 #endif /* CONFIG_NETFILTER */
7451 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7452 int selinux_disable(struct selinux_state *state)
7454 if (selinux_initialized(state)) {
7455 /* Not permitted after initial policy load. */
7459 if (selinux_disabled(state)) {
7460 /* Only do this once. */
7464 selinux_mark_disabled(state);
7466 pr_info("SELinux: Disabled at runtime.\n");
7469 * Unregister netfilter hooks.
7470 * Must be done before security_delete_hooks() to avoid breaking
7473 selinux_nf_ip_exit();
7475 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
7477 /* Try to destroy the avc node cache */
7480 /* Unregister selinuxfs. */