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_err("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);
2609 if (!first) { // copy with preceding comma
2614 memmove(to, from, len);
2627 selinux_free_mnt_opts(*mnt_opts);
2633 static int selinux_sb_mnt_opts_compat(struct super_block *sb, void *mnt_opts)
2635 struct selinux_mnt_opts *opts = mnt_opts;
2636 struct superblock_security_struct *sbsec = selinux_superblock(sb);
2639 * Superblock not initialized (i.e. no options) - reject if any
2640 * options specified, otherwise accept.
2642 if (!(sbsec->flags & SE_SBINITIALIZED))
2643 return opts ? 1 : 0;
2646 * Superblock initialized and no options specified - reject if
2647 * superblock has any options set, otherwise accept.
2650 return (sbsec->flags & SE_MNTMASK) ? 1 : 0;
2652 if (opts->fscontext_sid) {
2653 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
2654 opts->fscontext_sid))
2657 if (opts->context_sid) {
2658 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
2662 if (opts->rootcontext_sid) {
2663 struct inode_security_struct *root_isec;
2665 root_isec = backing_inode_security(sb->s_root);
2666 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
2667 opts->rootcontext_sid))
2670 if (opts->defcontext_sid) {
2671 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
2672 opts->defcontext_sid))
2678 static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
2680 struct selinux_mnt_opts *opts = mnt_opts;
2681 struct superblock_security_struct *sbsec = selinux_superblock(sb);
2683 if (!(sbsec->flags & SE_SBINITIALIZED))
2689 if (opts->fscontext_sid) {
2690 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
2691 opts->fscontext_sid))
2692 goto out_bad_option;
2694 if (opts->context_sid) {
2695 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
2697 goto out_bad_option;
2699 if (opts->rootcontext_sid) {
2700 struct inode_security_struct *root_isec;
2701 root_isec = backing_inode_security(sb->s_root);
2702 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
2703 opts->rootcontext_sid))
2704 goto out_bad_option;
2706 if (opts->defcontext_sid) {
2707 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
2708 opts->defcontext_sid))
2709 goto out_bad_option;
2714 pr_warn("SELinux: unable to change security options "
2715 "during remount (dev %s, type=%s)\n", sb->s_id,
2720 static int selinux_sb_kern_mount(struct super_block *sb)
2722 const struct cred *cred = current_cred();
2723 struct common_audit_data ad;
2725 ad.type = LSM_AUDIT_DATA_DENTRY;
2726 ad.u.dentry = sb->s_root;
2727 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
2730 static int selinux_sb_statfs(struct dentry *dentry)
2732 const struct cred *cred = current_cred();
2733 struct common_audit_data ad;
2735 ad.type = LSM_AUDIT_DATA_DENTRY;
2736 ad.u.dentry = dentry->d_sb->s_root;
2737 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
2740 static int selinux_mount(const char *dev_name,
2741 const struct path *path,
2743 unsigned long flags,
2746 const struct cred *cred = current_cred();
2748 if (flags & MS_REMOUNT)
2749 return superblock_has_perm(cred, path->dentry->d_sb,
2750 FILESYSTEM__REMOUNT, NULL);
2752 return path_has_perm(cred, path, FILE__MOUNTON);
2755 static int selinux_move_mount(const struct path *from_path,
2756 const struct path *to_path)
2758 const struct cred *cred = current_cred();
2760 return path_has_perm(cred, to_path, FILE__MOUNTON);
2763 static int selinux_umount(struct vfsmount *mnt, int flags)
2765 const struct cred *cred = current_cred();
2767 return superblock_has_perm(cred, mnt->mnt_sb,
2768 FILESYSTEM__UNMOUNT, NULL);
2771 static int selinux_fs_context_dup(struct fs_context *fc,
2772 struct fs_context *src_fc)
2774 const struct selinux_mnt_opts *src = src_fc->security;
2779 fc->security = kmemdup(src, sizeof(*src), GFP_KERNEL);
2780 return fc->security ? 0 : -ENOMEM;
2783 static const struct fs_parameter_spec selinux_fs_parameters[] = {
2784 fsparam_string(CONTEXT_STR, Opt_context),
2785 fsparam_string(DEFCONTEXT_STR, Opt_defcontext),
2786 fsparam_string(FSCONTEXT_STR, Opt_fscontext),
2787 fsparam_string(ROOTCONTEXT_STR, Opt_rootcontext),
2788 fsparam_flag (SECLABEL_STR, Opt_seclabel),
2792 static int selinux_fs_context_parse_param(struct fs_context *fc,
2793 struct fs_parameter *param)
2795 struct fs_parse_result result;
2798 opt = fs_parse(fc, selinux_fs_parameters, param, &result);
2802 return selinux_add_opt(opt, param->string, &fc->security);
2805 /* inode security operations */
2807 static int selinux_inode_alloc_security(struct inode *inode)
2809 struct inode_security_struct *isec = selinux_inode(inode);
2810 u32 sid = current_sid();
2812 spin_lock_init(&isec->lock);
2813 INIT_LIST_HEAD(&isec->list);
2814 isec->inode = inode;
2815 isec->sid = SECINITSID_UNLABELED;
2816 isec->sclass = SECCLASS_FILE;
2817 isec->task_sid = sid;
2818 isec->initialized = LABEL_INVALID;
2823 static void selinux_inode_free_security(struct inode *inode)
2825 inode_free_security(inode);
2828 static int selinux_dentry_init_security(struct dentry *dentry, int mode,
2829 const struct qstr *name,
2830 const char **xattr_name, void **ctx,
2836 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
2837 d_inode(dentry->d_parent), name,
2838 inode_mode_to_security_class(mode),
2844 *xattr_name = XATTR_NAME_SELINUX;
2846 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2850 static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2852 const struct cred *old,
2857 struct task_security_struct *tsec;
2859 rc = selinux_determine_inode_label(selinux_cred(old),
2860 d_inode(dentry->d_parent), name,
2861 inode_mode_to_security_class(mode),
2866 tsec = selinux_cred(new);
2867 tsec->create_sid = newsid;
2871 static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
2872 const struct qstr *qstr,
2874 void **value, size_t *len)
2876 const struct task_security_struct *tsec = selinux_cred(current_cred());
2877 struct superblock_security_struct *sbsec;
2882 sbsec = selinux_superblock(dir->i_sb);
2884 newsid = tsec->create_sid;
2886 rc = selinux_determine_inode_label(tsec, dir, qstr,
2887 inode_mode_to_security_class(inode->i_mode),
2892 /* Possibly defer initialization to selinux_complete_init. */
2893 if (sbsec->flags & SE_SBINITIALIZED) {
2894 struct inode_security_struct *isec = selinux_inode(inode);
2895 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2897 isec->initialized = LABEL_INITIALIZED;
2900 if (!selinux_initialized(&selinux_state) ||
2901 !(sbsec->flags & SBLABEL_MNT))
2905 *name = XATTR_SELINUX_SUFFIX;
2908 rc = security_sid_to_context_force(&selinux_state, newsid,
2919 static int selinux_inode_init_security_anon(struct inode *inode,
2920 const struct qstr *name,
2921 const struct inode *context_inode)
2923 const struct task_security_struct *tsec = selinux_cred(current_cred());
2924 struct common_audit_data ad;
2925 struct inode_security_struct *isec;
2928 if (unlikely(!selinux_initialized(&selinux_state)))
2931 isec = selinux_inode(inode);
2934 * We only get here once per ephemeral inode. The inode has
2935 * been initialized via inode_alloc_security but is otherwise
2939 if (context_inode) {
2940 struct inode_security_struct *context_isec =
2941 selinux_inode(context_inode);
2942 if (context_isec->initialized != LABEL_INITIALIZED) {
2943 pr_err("SELinux: context_inode is not initialized");
2947 isec->sclass = context_isec->sclass;
2948 isec->sid = context_isec->sid;
2950 isec->sclass = SECCLASS_ANON_INODE;
2951 rc = security_transition_sid(
2952 &selinux_state, tsec->sid, tsec->sid,
2953 isec->sclass, name, &isec->sid);
2958 isec->initialized = LABEL_INITIALIZED;
2960 * Now that we've initialized security, check whether we're
2961 * allowed to actually create this type of anonymous inode.
2964 ad.type = LSM_AUDIT_DATA_ANONINODE;
2965 ad.u.anonclass = name ? (const char *)name->name : "?";
2967 return avc_has_perm(&selinux_state,
2975 static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
2977 return may_create(dir, dentry, SECCLASS_FILE);
2980 static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2982 return may_link(dir, old_dentry, MAY_LINK);
2985 static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2987 return may_link(dir, dentry, MAY_UNLINK);
2990 static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2992 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2995 static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
2997 return may_create(dir, dentry, SECCLASS_DIR);
3000 static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
3002 return may_link(dir, dentry, MAY_RMDIR);
3005 static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3007 return may_create(dir, dentry, inode_mode_to_security_class(mode));
3010 static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
3011 struct inode *new_inode, struct dentry *new_dentry)
3013 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
3016 static int selinux_inode_readlink(struct dentry *dentry)
3018 const struct cred *cred = current_cred();
3020 return dentry_has_perm(cred, dentry, FILE__READ);
3023 static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
3026 const struct cred *cred = current_cred();
3027 struct common_audit_data ad;
3028 struct inode_security_struct *isec;
3031 validate_creds(cred);
3033 ad.type = LSM_AUDIT_DATA_DENTRY;
3034 ad.u.dentry = dentry;
3035 sid = cred_sid(cred);
3036 isec = inode_security_rcu(inode, rcu);
3038 return PTR_ERR(isec);
3040 return avc_has_perm(&selinux_state,
3041 sid, isec->sid, isec->sclass, FILE__READ, &ad);
3044 static noinline int audit_inode_permission(struct inode *inode,
3045 u32 perms, u32 audited, u32 denied,
3048 struct common_audit_data ad;
3049 struct inode_security_struct *isec = selinux_inode(inode);
3051 ad.type = LSM_AUDIT_DATA_INODE;
3054 return slow_avc_audit(&selinux_state,
3055 current_sid(), isec->sid, isec->sclass, perms,
3056 audited, denied, result, &ad);
3059 static int selinux_inode_permission(struct inode *inode, int mask)
3061 const struct cred *cred = current_cred();
3064 bool no_block = mask & MAY_NOT_BLOCK;
3065 struct inode_security_struct *isec;
3067 struct av_decision avd;
3069 u32 audited, denied;
3071 from_access = mask & MAY_ACCESS;
3072 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3074 /* No permission to check. Existence test. */
3078 validate_creds(cred);
3080 if (unlikely(IS_PRIVATE(inode)))
3083 perms = file_mask_to_av(inode->i_mode, mask);
3085 sid = cred_sid(cred);
3086 isec = inode_security_rcu(inode, no_block);
3088 return PTR_ERR(isec);
3090 rc = avc_has_perm_noaudit(&selinux_state,
3091 sid, isec->sid, isec->sclass, perms, 0,
3093 audited = avc_audit_required(perms, &avd, rc,
3094 from_access ? FILE__AUDIT_ACCESS : 0,
3096 if (likely(!audited))
3099 rc2 = audit_inode_permission(inode, perms, audited, denied, rc);
3105 static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3107 const struct cred *cred = current_cred();
3108 struct inode *inode = d_backing_inode(dentry);
3109 unsigned int ia_valid = iattr->ia_valid;
3110 __u32 av = FILE__WRITE;
3112 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3113 if (ia_valid & ATTR_FORCE) {
3114 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3120 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3121 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
3122 return dentry_has_perm(cred, dentry, FILE__SETATTR);
3124 if (selinux_policycap_openperm() &&
3125 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3126 (ia_valid & ATTR_SIZE) &&
3127 !(ia_valid & ATTR_FILE))
3130 return dentry_has_perm(cred, dentry, av);
3133 static int selinux_inode_getattr(const struct path *path)
3135 return path_has_perm(current_cred(), path, FILE__GETATTR);
3138 static bool has_cap_mac_admin(bool audit)
3140 const struct cred *cred = current_cred();
3141 unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT;
3143 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts))
3145 if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true))
3150 static int selinux_inode_setxattr(struct user_namespace *mnt_userns,
3151 struct dentry *dentry, const char *name,
3152 const void *value, size_t size, int flags)
3154 struct inode *inode = d_backing_inode(dentry);
3155 struct inode_security_struct *isec;
3156 struct superblock_security_struct *sbsec;
3157 struct common_audit_data ad;
3158 u32 newsid, sid = current_sid();
3161 if (strcmp(name, XATTR_NAME_SELINUX)) {
3162 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3166 /* Not an attribute we recognize, so just check the
3167 ordinary setattr permission. */
3168 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3171 if (!selinux_initialized(&selinux_state))
3172 return (inode_owner_or_capable(mnt_userns, inode) ? 0 : -EPERM);
3174 sbsec = selinux_superblock(inode->i_sb);
3175 if (!(sbsec->flags & SBLABEL_MNT))
3178 if (!inode_owner_or_capable(mnt_userns, inode))
3181 ad.type = LSM_AUDIT_DATA_DENTRY;
3182 ad.u.dentry = dentry;
3184 isec = backing_inode_security(dentry);
3185 rc = avc_has_perm(&selinux_state,
3186 sid, isec->sid, isec->sclass,
3187 FILE__RELABELFROM, &ad);
3191 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3193 if (rc == -EINVAL) {
3194 if (!has_cap_mac_admin(true)) {
3195 struct audit_buffer *ab;
3198 /* We strip a nul only if it is at the end, otherwise the
3199 * context contains a nul and we should audit that */
3201 const char *str = value;
3203 if (str[size - 1] == '\0')
3204 audit_size = size - 1;
3210 ab = audit_log_start(audit_context(),
3211 GFP_ATOMIC, AUDIT_SELINUX_ERR);
3214 audit_log_format(ab, "op=setxattr invalid_context=");
3215 audit_log_n_untrustedstring(ab, value, audit_size);
3220 rc = security_context_to_sid_force(&selinux_state, value,
3226 rc = avc_has_perm(&selinux_state,
3227 sid, newsid, isec->sclass,
3228 FILE__RELABELTO, &ad);
3232 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3237 return avc_has_perm(&selinux_state,
3240 SECCLASS_FILESYSTEM,
3241 FILESYSTEM__ASSOCIATE,
3245 static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
3246 const void *value, size_t size,
3249 struct inode *inode = d_backing_inode(dentry);
3250 struct inode_security_struct *isec;
3254 if (strcmp(name, XATTR_NAME_SELINUX)) {
3255 /* Not an attribute we recognize, so nothing to do. */
3259 if (!selinux_initialized(&selinux_state)) {
3260 /* If we haven't even been initialized, then we can't validate
3261 * against a policy, so leave the label as invalid. It may
3262 * resolve to a valid label on the next revalidation try if
3263 * we've since initialized.
3268 rc = security_context_to_sid_force(&selinux_state, value, size,
3271 pr_err("SELinux: unable to map context to SID"
3272 "for (%s, %lu), rc=%d\n",
3273 inode->i_sb->s_id, inode->i_ino, -rc);
3277 isec = backing_inode_security(dentry);
3278 spin_lock(&isec->lock);
3279 isec->sclass = inode_mode_to_security_class(inode->i_mode);
3281 isec->initialized = LABEL_INITIALIZED;
3282 spin_unlock(&isec->lock);
3285 static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
3287 const struct cred *cred = current_cred();
3289 return dentry_has_perm(cred, dentry, FILE__GETATTR);
3292 static int selinux_inode_listxattr(struct dentry *dentry)
3294 const struct cred *cred = current_cred();
3296 return dentry_has_perm(cred, dentry, FILE__GETATTR);
3299 static int selinux_inode_removexattr(struct user_namespace *mnt_userns,
3300 struct dentry *dentry, const char *name)
3302 if (strcmp(name, XATTR_NAME_SELINUX)) {
3303 int rc = cap_inode_removexattr(mnt_userns, dentry, name);
3307 /* Not an attribute we recognize, so just check the
3308 ordinary setattr permission. */
3309 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3312 if (!selinux_initialized(&selinux_state))
3315 /* No one is allowed to remove a SELinux security label.
3316 You can change the label, but all data must be labeled. */
3320 static int selinux_path_notify(const struct path *path, u64 mask,
3321 unsigned int obj_type)
3326 struct common_audit_data ad;
3328 ad.type = LSM_AUDIT_DATA_PATH;
3332 * Set permission needed based on the type of mark being set.
3333 * Performs an additional check for sb watches.
3336 case FSNOTIFY_OBJ_TYPE_VFSMOUNT:
3337 perm = FILE__WATCH_MOUNT;
3339 case FSNOTIFY_OBJ_TYPE_SB:
3340 perm = FILE__WATCH_SB;
3341 ret = superblock_has_perm(current_cred(), path->dentry->d_sb,
3342 FILESYSTEM__WATCH, &ad);
3346 case FSNOTIFY_OBJ_TYPE_INODE:
3353 /* blocking watches require the file:watch_with_perm permission */
3354 if (mask & (ALL_FSNOTIFY_PERM_EVENTS))
3355 perm |= FILE__WATCH_WITH_PERM;
3357 /* watches on read-like events need the file:watch_reads permission */
3358 if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_CLOSE_NOWRITE))
3359 perm |= FILE__WATCH_READS;
3361 return path_has_perm(current_cred(), path, perm);
3365 * Copy the inode security context value to the user.
3367 * Permission check is handled by selinux_inode_getxattr hook.
3369 static int selinux_inode_getsecurity(struct user_namespace *mnt_userns,
3370 struct inode *inode, const char *name,
3371 void **buffer, bool alloc)
3375 char *context = NULL;
3376 struct inode_security_struct *isec;
3379 * If we're not initialized yet, then we can't validate contexts, so
3380 * just let vfs_getxattr fall back to using the on-disk xattr.
3382 if (!selinux_initialized(&selinux_state) ||
3383 strcmp(name, XATTR_SELINUX_SUFFIX))
3387 * If the caller has CAP_MAC_ADMIN, then get the raw context
3388 * value even if it is not defined by current policy; otherwise,
3389 * use the in-core value under current policy.
3390 * Use the non-auditing forms of the permission checks since
3391 * getxattr may be called by unprivileged processes commonly
3392 * and lack of permission just means that we fall back to the
3393 * in-core context value, not a denial.
3395 isec = inode_security(inode);
3396 if (has_cap_mac_admin(false))
3397 error = security_sid_to_context_force(&selinux_state,
3398 isec->sid, &context,
3401 error = security_sid_to_context(&selinux_state, isec->sid,
3415 static int selinux_inode_setsecurity(struct inode *inode, const char *name,
3416 const void *value, size_t size, int flags)
3418 struct inode_security_struct *isec = inode_security_novalidate(inode);
3419 struct superblock_security_struct *sbsec;
3423 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3426 sbsec = selinux_superblock(inode->i_sb);
3427 if (!(sbsec->flags & SBLABEL_MNT))
3430 if (!value || !size)
3433 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3438 spin_lock(&isec->lock);
3439 isec->sclass = inode_mode_to_security_class(inode->i_mode);
3441 isec->initialized = LABEL_INITIALIZED;
3442 spin_unlock(&isec->lock);
3446 static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3448 const int len = sizeof(XATTR_NAME_SELINUX);
3450 if (!selinux_initialized(&selinux_state))
3453 if (buffer && len <= buffer_size)
3454 memcpy(buffer, XATTR_NAME_SELINUX, len);
3458 static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
3460 struct inode_security_struct *isec = inode_security_novalidate(inode);
3464 static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3467 struct task_security_struct *tsec;
3468 struct cred *new_creds = *new;
3470 if (new_creds == NULL) {
3471 new_creds = prepare_creds();
3476 tsec = selinux_cred(new_creds);
3477 /* Get label from overlay inode and set it in create_sid */
3478 selinux_inode_getsecid(d_inode(src), &sid);
3479 tsec->create_sid = sid;
3484 static int selinux_inode_copy_up_xattr(const char *name)
3486 /* The copy_up hook above sets the initial context on an inode, but we
3487 * don't then want to overwrite it by blindly copying all the lower
3488 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3490 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3491 return 1; /* Discard */
3493 * Any other attribute apart from SELINUX is not claimed, supported
3499 /* kernfs node operations */
3501 static int selinux_kernfs_init_security(struct kernfs_node *kn_dir,
3502 struct kernfs_node *kn)
3504 const struct task_security_struct *tsec = selinux_cred(current_cred());
3505 u32 parent_sid, newsid, clen;
3509 rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, NULL, 0);
3516 context = kmalloc(clen, GFP_KERNEL);
3520 rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, context, clen);
3526 rc = security_context_to_sid(&selinux_state, context, clen, &parent_sid,
3532 if (tsec->create_sid) {
3533 newsid = tsec->create_sid;
3535 u16 secclass = inode_mode_to_security_class(kn->mode);
3539 q.hash_len = hashlen_string(kn_dir, kn->name);
3541 rc = security_transition_sid(&selinux_state, tsec->sid,
3542 parent_sid, secclass, &q,
3548 rc = security_sid_to_context_force(&selinux_state, newsid,
3553 rc = kernfs_xattr_set(kn, XATTR_NAME_SELINUX, context, clen,
3560 /* file security operations */
3562 static int selinux_revalidate_file_permission(struct file *file, int mask)
3564 const struct cred *cred = current_cred();
3565 struct inode *inode = file_inode(file);
3567 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3568 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3571 return file_has_perm(cred, file,
3572 file_mask_to_av(inode->i_mode, mask));
3575 static int selinux_file_permission(struct file *file, int mask)
3577 struct inode *inode = file_inode(file);
3578 struct file_security_struct *fsec = selinux_file(file);
3579 struct inode_security_struct *isec;
3580 u32 sid = current_sid();
3583 /* No permission to check. Existence test. */
3586 isec = inode_security(inode);
3587 if (sid == fsec->sid && fsec->isid == isec->sid &&
3588 fsec->pseqno == avc_policy_seqno(&selinux_state))
3589 /* No change since file_open check. */
3592 return selinux_revalidate_file_permission(file, mask);
3595 static int selinux_file_alloc_security(struct file *file)
3597 struct file_security_struct *fsec = selinux_file(file);
3598 u32 sid = current_sid();
3601 fsec->fown_sid = sid;
3607 * Check whether a task has the ioctl permission and cmd
3608 * operation to an inode.
3610 static int ioctl_has_perm(const struct cred *cred, struct file *file,
3611 u32 requested, u16 cmd)
3613 struct common_audit_data ad;
3614 struct file_security_struct *fsec = selinux_file(file);
3615 struct inode *inode = file_inode(file);
3616 struct inode_security_struct *isec;
3617 struct lsm_ioctlop_audit ioctl;
3618 u32 ssid = cred_sid(cred);
3620 u8 driver = cmd >> 8;
3621 u8 xperm = cmd & 0xff;
3623 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3626 ad.u.op->path = file->f_path;
3628 if (ssid != fsec->sid) {
3629 rc = avc_has_perm(&selinux_state,
3638 if (unlikely(IS_PRIVATE(inode)))
3641 isec = inode_security(inode);
3642 rc = avc_has_extended_perms(&selinux_state,
3643 ssid, isec->sid, isec->sclass,
3644 requested, driver, xperm, &ad);
3649 static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3652 const struct cred *cred = current_cred();
3659 case FS_IOC_GETFLAGS:
3660 case FS_IOC_GETVERSION:
3661 error = file_has_perm(cred, file, FILE__GETATTR);
3664 case FS_IOC_SETFLAGS:
3665 case FS_IOC_SETVERSION:
3666 error = file_has_perm(cred, file, FILE__SETATTR);
3669 /* sys_ioctl() checks */
3672 error = file_has_perm(cred, file, 0);
3677 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3678 CAP_OPT_NONE, true);
3683 if (!selinux_policycap_ioctl_skip_cloexec())
3684 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
3687 /* default case assumes that the command will go
3688 * to the file's ioctl() function.
3691 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
3696 static int default_noexec __ro_after_init;
3698 static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3700 const struct cred *cred = current_cred();
3701 u32 sid = cred_sid(cred);
3704 if (default_noexec &&
3705 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3706 (!shared && (prot & PROT_WRITE)))) {
3708 * We are making executable an anonymous mapping or a
3709 * private file mapping that will also be writable.
3710 * This has an additional check.
3712 rc = avc_has_perm(&selinux_state,
3713 sid, sid, SECCLASS_PROCESS,
3714 PROCESS__EXECMEM, NULL);
3720 /* read access is always possible with a mapping */
3721 u32 av = FILE__READ;
3723 /* write access only matters if the mapping is shared */
3724 if (shared && (prot & PROT_WRITE))
3727 if (prot & PROT_EXEC)
3728 av |= FILE__EXECUTE;
3730 return file_has_perm(cred, file, av);
3737 static int selinux_mmap_addr(unsigned long addr)
3741 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3742 u32 sid = current_sid();
3743 rc = avc_has_perm(&selinux_state,
3744 sid, sid, SECCLASS_MEMPROTECT,
3745 MEMPROTECT__MMAP_ZERO, NULL);
3751 static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3752 unsigned long prot, unsigned long flags)
3754 struct common_audit_data ad;
3758 ad.type = LSM_AUDIT_DATA_FILE;
3760 rc = inode_has_perm(current_cred(), file_inode(file),
3766 if (checkreqprot_get(&selinux_state))
3769 return file_map_prot_check(file, prot,
3770 (flags & MAP_TYPE) == MAP_SHARED);
3773 static int selinux_file_mprotect(struct vm_area_struct *vma,
3774 unsigned long reqprot,
3777 const struct cred *cred = current_cred();
3778 u32 sid = cred_sid(cred);
3780 if (checkreqprot_get(&selinux_state))
3783 if (default_noexec &&
3784 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3786 if (vma->vm_start >= vma->vm_mm->start_brk &&
3787 vma->vm_end <= vma->vm_mm->brk) {
3788 rc = avc_has_perm(&selinux_state,
3789 sid, sid, SECCLASS_PROCESS,
3790 PROCESS__EXECHEAP, NULL);
3791 } else if (!vma->vm_file &&
3792 ((vma->vm_start <= vma->vm_mm->start_stack &&
3793 vma->vm_end >= vma->vm_mm->start_stack) ||
3794 vma_is_stack_for_current(vma))) {
3795 rc = avc_has_perm(&selinux_state,
3796 sid, sid, SECCLASS_PROCESS,
3797 PROCESS__EXECSTACK, NULL);
3798 } else if (vma->vm_file && vma->anon_vma) {
3800 * We are making executable a file mapping that has
3801 * had some COW done. Since pages might have been
3802 * written, check ability to execute the possibly
3803 * modified content. This typically should only
3804 * occur for text relocations.
3806 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
3812 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3815 static int selinux_file_lock(struct file *file, unsigned int cmd)
3817 const struct cred *cred = current_cred();
3819 return file_has_perm(cred, file, FILE__LOCK);
3822 static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3825 const struct cred *cred = current_cred();
3830 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
3831 err = file_has_perm(cred, file, FILE__WRITE);
3840 case F_GETOWNER_UIDS:
3841 /* Just check FD__USE permission */
3842 err = file_has_perm(cred, file, 0);
3850 #if BITS_PER_LONG == 32
3855 err = file_has_perm(cred, file, FILE__LOCK);
3862 static void selinux_file_set_fowner(struct file *file)
3864 struct file_security_struct *fsec;
3866 fsec = selinux_file(file);
3867 fsec->fown_sid = current_sid();
3870 static int selinux_file_send_sigiotask(struct task_struct *tsk,
3871 struct fown_struct *fown, int signum)
3874 u32 sid = task_sid_obj(tsk);
3876 struct file_security_struct *fsec;
3878 /* struct fown_struct is never outside the context of a struct file */
3879 file = container_of(fown, struct file, f_owner);
3881 fsec = selinux_file(file);
3884 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3886 perm = signal_to_av(signum);
3888 return avc_has_perm(&selinux_state,
3889 fsec->fown_sid, sid,
3890 SECCLASS_PROCESS, perm, NULL);
3893 static int selinux_file_receive(struct file *file)
3895 const struct cred *cred = current_cred();
3897 return file_has_perm(cred, file, file_to_av(file));
3900 static int selinux_file_open(struct file *file)
3902 struct file_security_struct *fsec;
3903 struct inode_security_struct *isec;
3905 fsec = selinux_file(file);
3906 isec = inode_security(file_inode(file));
3908 * Save inode label and policy sequence number
3909 * at open-time so that selinux_file_permission
3910 * can determine whether revalidation is necessary.
3911 * Task label is already saved in the file security
3912 * struct as its SID.
3914 fsec->isid = isec->sid;
3915 fsec->pseqno = avc_policy_seqno(&selinux_state);
3917 * Since the inode label or policy seqno may have changed
3918 * between the selinux_inode_permission check and the saving
3919 * of state above, recheck that access is still permitted.
3920 * Otherwise, access might never be revalidated against the
3921 * new inode label or new policy.
3922 * This check is not redundant - do not remove.
3924 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
3927 /* task security operations */
3929 static int selinux_task_alloc(struct task_struct *task,
3930 unsigned long clone_flags)
3932 u32 sid = current_sid();
3934 return avc_has_perm(&selinux_state,
3935 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
3939 * prepare a new set of credentials for modification
3941 static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3944 const struct task_security_struct *old_tsec = selinux_cred(old);
3945 struct task_security_struct *tsec = selinux_cred(new);
3952 * transfer the SELinux data to a blank set of creds
3954 static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3956 const struct task_security_struct *old_tsec = selinux_cred(old);
3957 struct task_security_struct *tsec = selinux_cred(new);
3962 static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3964 *secid = cred_sid(c);
3968 * set the security data for a kernel service
3969 * - all the creation contexts are set to unlabelled
3971 static int selinux_kernel_act_as(struct cred *new, u32 secid)
3973 struct task_security_struct *tsec = selinux_cred(new);
3974 u32 sid = current_sid();
3977 ret = avc_has_perm(&selinux_state,
3979 SECCLASS_KERNEL_SERVICE,
3980 KERNEL_SERVICE__USE_AS_OVERRIDE,
3984 tsec->create_sid = 0;
3985 tsec->keycreate_sid = 0;
3986 tsec->sockcreate_sid = 0;
3992 * set the file creation context in a security record to the same as the
3993 * objective context of the specified inode
3995 static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3997 struct inode_security_struct *isec = inode_security(inode);
3998 struct task_security_struct *tsec = selinux_cred(new);
3999 u32 sid = current_sid();
4002 ret = avc_has_perm(&selinux_state,
4004 SECCLASS_KERNEL_SERVICE,
4005 KERNEL_SERVICE__CREATE_FILES_AS,
4009 tsec->create_sid = isec->sid;
4013 static int selinux_kernel_module_request(char *kmod_name)
4015 struct common_audit_data ad;
4017 ad.type = LSM_AUDIT_DATA_KMOD;
4018 ad.u.kmod_name = kmod_name;
4020 return avc_has_perm(&selinux_state,
4021 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
4022 SYSTEM__MODULE_REQUEST, &ad);
4025 static int selinux_kernel_module_from_file(struct file *file)
4027 struct common_audit_data ad;
4028 struct inode_security_struct *isec;
4029 struct file_security_struct *fsec;
4030 u32 sid = current_sid();
4035 return avc_has_perm(&selinux_state,
4036 sid, sid, SECCLASS_SYSTEM,
4037 SYSTEM__MODULE_LOAD, NULL);
4041 ad.type = LSM_AUDIT_DATA_FILE;
4044 fsec = selinux_file(file);
4045 if (sid != fsec->sid) {
4046 rc = avc_has_perm(&selinux_state,
4047 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
4052 isec = inode_security(file_inode(file));
4053 return avc_has_perm(&selinux_state,
4054 sid, isec->sid, SECCLASS_SYSTEM,
4055 SYSTEM__MODULE_LOAD, &ad);
4058 static int selinux_kernel_read_file(struct file *file,
4059 enum kernel_read_file_id id,
4065 case READING_MODULE:
4066 rc = selinux_kernel_module_from_file(contents ? file : NULL);
4075 static int selinux_kernel_load_data(enum kernel_load_data_id id, bool contents)
4080 case LOADING_MODULE:
4081 rc = selinux_kernel_module_from_file(NULL);
4090 static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
4092 return avc_has_perm(&selinux_state,
4093 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4094 PROCESS__SETPGID, NULL);
4097 static int selinux_task_getpgid(struct task_struct *p)
4099 return avc_has_perm(&selinux_state,
4100 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4101 PROCESS__GETPGID, NULL);
4104 static int selinux_task_getsid(struct task_struct *p)
4106 return avc_has_perm(&selinux_state,
4107 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4108 PROCESS__GETSESSION, NULL);
4111 static void selinux_current_getsecid_subj(u32 *secid)
4113 *secid = current_sid();
4116 static void selinux_task_getsecid_obj(struct task_struct *p, u32 *secid)
4118 *secid = task_sid_obj(p);
4121 static int selinux_task_setnice(struct task_struct *p, int nice)
4123 return avc_has_perm(&selinux_state,
4124 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4125 PROCESS__SETSCHED, NULL);
4128 static int selinux_task_setioprio(struct task_struct *p, int ioprio)
4130 return avc_has_perm(&selinux_state,
4131 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4132 PROCESS__SETSCHED, NULL);
4135 static int selinux_task_getioprio(struct task_struct *p)
4137 return avc_has_perm(&selinux_state,
4138 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4139 PROCESS__GETSCHED, NULL);
4142 static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
4149 if (flags & LSM_PRLIMIT_WRITE)
4150 av |= PROCESS__SETRLIMIT;
4151 if (flags & LSM_PRLIMIT_READ)
4152 av |= PROCESS__GETRLIMIT;
4153 return avc_has_perm(&selinux_state,
4154 cred_sid(cred), cred_sid(tcred),
4155 SECCLASS_PROCESS, av, NULL);
4158 static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
4159 struct rlimit *new_rlim)
4161 struct rlimit *old_rlim = p->signal->rlim + resource;
4163 /* Control the ability to change the hard limit (whether
4164 lowering or raising it), so that the hard limit can
4165 later be used as a safe reset point for the soft limit
4166 upon context transitions. See selinux_bprm_committing_creds. */
4167 if (old_rlim->rlim_max != new_rlim->rlim_max)
4168 return avc_has_perm(&selinux_state,
4169 current_sid(), task_sid_obj(p),
4170 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
4175 static int selinux_task_setscheduler(struct task_struct *p)
4177 return avc_has_perm(&selinux_state,
4178 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4179 PROCESS__SETSCHED, NULL);
4182 static int selinux_task_getscheduler(struct task_struct *p)
4184 return avc_has_perm(&selinux_state,
4185 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4186 PROCESS__GETSCHED, NULL);
4189 static int selinux_task_movememory(struct task_struct *p)
4191 return avc_has_perm(&selinux_state,
4192 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
4193 PROCESS__SETSCHED, NULL);
4196 static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
4197 int sig, const struct cred *cred)
4203 perm = PROCESS__SIGNULL; /* null signal; existence test */
4205 perm = signal_to_av(sig);
4207 secid = current_sid();
4209 secid = cred_sid(cred);
4210 return avc_has_perm(&selinux_state,
4211 secid, task_sid_obj(p), SECCLASS_PROCESS, perm, NULL);
4214 static void selinux_task_to_inode(struct task_struct *p,
4215 struct inode *inode)
4217 struct inode_security_struct *isec = selinux_inode(inode);
4218 u32 sid = task_sid_obj(p);
4220 spin_lock(&isec->lock);
4221 isec->sclass = inode_mode_to_security_class(inode->i_mode);
4223 isec->initialized = LABEL_INITIALIZED;
4224 spin_unlock(&isec->lock);
4227 /* Returns error only if unable to parse addresses */
4228 static int selinux_parse_skb_ipv4(struct sk_buff *skb,
4229 struct common_audit_data *ad, u8 *proto)
4231 int offset, ihlen, ret = -EINVAL;
4232 struct iphdr _iph, *ih;
4234 offset = skb_network_offset(skb);
4235 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4239 ihlen = ih->ihl * 4;
4240 if (ihlen < sizeof(_iph))
4243 ad->u.net->v4info.saddr = ih->saddr;
4244 ad->u.net->v4info.daddr = ih->daddr;
4248 *proto = ih->protocol;
4250 switch (ih->protocol) {
4252 struct tcphdr _tcph, *th;
4254 if (ntohs(ih->frag_off) & IP_OFFSET)
4258 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4262 ad->u.net->sport = th->source;
4263 ad->u.net->dport = th->dest;
4268 struct udphdr _udph, *uh;
4270 if (ntohs(ih->frag_off) & IP_OFFSET)
4274 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4278 ad->u.net->sport = uh->source;
4279 ad->u.net->dport = uh->dest;
4283 case IPPROTO_DCCP: {
4284 struct dccp_hdr _dccph, *dh;
4286 if (ntohs(ih->frag_off) & IP_OFFSET)
4290 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4294 ad->u.net->sport = dh->dccph_sport;
4295 ad->u.net->dport = dh->dccph_dport;
4299 #if IS_ENABLED(CONFIG_IP_SCTP)
4300 case IPPROTO_SCTP: {
4301 struct sctphdr _sctph, *sh;
4303 if (ntohs(ih->frag_off) & IP_OFFSET)
4307 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4311 ad->u.net->sport = sh->source;
4312 ad->u.net->dport = sh->dest;
4323 #if IS_ENABLED(CONFIG_IPV6)
4325 /* Returns error only if unable to parse addresses */
4326 static int selinux_parse_skb_ipv6(struct sk_buff *skb,
4327 struct common_audit_data *ad, u8 *proto)
4330 int ret = -EINVAL, offset;
4331 struct ipv6hdr _ipv6h, *ip6;
4334 offset = skb_network_offset(skb);
4335 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4339 ad->u.net->v6info.saddr = ip6->saddr;
4340 ad->u.net->v6info.daddr = ip6->daddr;
4343 nexthdr = ip6->nexthdr;
4344 offset += sizeof(_ipv6h);
4345 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
4354 struct tcphdr _tcph, *th;
4356 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4360 ad->u.net->sport = th->source;
4361 ad->u.net->dport = th->dest;
4366 struct udphdr _udph, *uh;
4368 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4372 ad->u.net->sport = uh->source;
4373 ad->u.net->dport = uh->dest;
4377 case IPPROTO_DCCP: {
4378 struct dccp_hdr _dccph, *dh;
4380 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4384 ad->u.net->sport = dh->dccph_sport;
4385 ad->u.net->dport = dh->dccph_dport;
4389 #if IS_ENABLED(CONFIG_IP_SCTP)
4390 case IPPROTO_SCTP: {
4391 struct sctphdr _sctph, *sh;
4393 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4397 ad->u.net->sport = sh->source;
4398 ad->u.net->dport = sh->dest;
4402 /* includes fragments */
4412 static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
4413 char **_addrp, int src, u8 *proto)
4418 switch (ad->u.net->family) {
4420 ret = selinux_parse_skb_ipv4(skb, ad, proto);
4423 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4424 &ad->u.net->v4info.daddr);
4427 #if IS_ENABLED(CONFIG_IPV6)
4429 ret = selinux_parse_skb_ipv6(skb, ad, proto);
4432 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4433 &ad->u.net->v6info.daddr);
4443 "SELinux: failure in selinux_parse_skb(),"
4444 " unable to parse packet\n");
4454 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
4456 * @family: protocol family
4457 * @sid: the packet's peer label SID
4460 * Check the various different forms of network peer labeling and determine
4461 * the peer label/SID for the packet; most of the magic actually occurs in
4462 * the security server function security_net_peersid_cmp(). The function
4463 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4464 * or -EACCES if @sid is invalid due to inconsistencies with the different
4468 static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
4475 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
4478 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4482 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4483 nlbl_type, xfrm_sid, sid);
4484 if (unlikely(err)) {
4486 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4487 " unable to determine packet's peer label\n");
4495 * selinux_conn_sid - Determine the child socket label for a connection
4496 * @sk_sid: the parent socket's SID
4497 * @skb_sid: the packet's SID
4498 * @conn_sid: the resulting connection SID
4500 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4501 * combined with the MLS information from @skb_sid in order to create
4502 * @conn_sid. If @skb_sid is not valid then @conn_sid is simply a copy
4503 * of @sk_sid. Returns zero on success, negative values on failure.
4506 static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4510 if (skb_sid != SECSID_NULL)
4511 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4519 /* socket security operations */
4521 static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4522 u16 secclass, u32 *socksid)
4524 if (tsec->sockcreate_sid > SECSID_NULL) {
4525 *socksid = tsec->sockcreate_sid;
4529 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4530 secclass, NULL, socksid);
4533 static int sock_has_perm(struct sock *sk, u32 perms)
4535 struct sk_security_struct *sksec = sk->sk_security;
4536 struct common_audit_data ad;
4537 struct lsm_network_audit net = {0,};
4539 if (sksec->sid == SECINITSID_KERNEL)
4542 ad.type = LSM_AUDIT_DATA_NET;
4546 return avc_has_perm(&selinux_state,
4547 current_sid(), sksec->sid, sksec->sclass, perms,
4551 static int selinux_socket_create(int family, int type,
4552 int protocol, int kern)
4554 const struct task_security_struct *tsec = selinux_cred(current_cred());
4562 secclass = socket_type_to_security_class(family, type, protocol);
4563 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4567 return avc_has_perm(&selinux_state,
4568 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
4571 static int selinux_socket_post_create(struct socket *sock, int family,
4572 int type, int protocol, int kern)
4574 const struct task_security_struct *tsec = selinux_cred(current_cred());
4575 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
4576 struct sk_security_struct *sksec;
4577 u16 sclass = socket_type_to_security_class(family, type, protocol);
4578 u32 sid = SECINITSID_KERNEL;
4582 err = socket_sockcreate_sid(tsec, sclass, &sid);
4587 isec->sclass = sclass;
4589 isec->initialized = LABEL_INITIALIZED;
4592 sksec = sock->sk->sk_security;
4593 sksec->sclass = sclass;
4595 /* Allows detection of the first association on this socket */
4596 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4597 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4599 err = selinux_netlbl_socket_post_create(sock->sk, family);
4605 static int selinux_socket_socketpair(struct socket *socka,
4606 struct socket *sockb)
4608 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4609 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4611 sksec_a->peer_sid = sksec_b->sid;
4612 sksec_b->peer_sid = sksec_a->sid;
4617 /* Range of port numbers used to automatically bind.
4618 Need to determine whether we should perform a name_bind
4619 permission check between the socket and the port number. */
4621 static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4623 struct sock *sk = sock->sk;
4624 struct sk_security_struct *sksec = sk->sk_security;
4628 err = sock_has_perm(sk, SOCKET__BIND);
4632 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
4633 family = sk->sk_family;
4634 if (family == PF_INET || family == PF_INET6) {
4636 struct common_audit_data ad;
4637 struct lsm_network_audit net = {0,};
4638 struct sockaddr_in *addr4 = NULL;
4639 struct sockaddr_in6 *addr6 = NULL;
4641 unsigned short snum;
4645 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4646 * that validates multiple binding addresses. Because of this
4647 * need to check address->sa_family as it is possible to have
4648 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4650 if (addrlen < offsetofend(struct sockaddr, sa_family))
4652 family_sa = address->sa_family;
4653 switch (family_sa) {
4656 if (addrlen < sizeof(struct sockaddr_in))
4658 addr4 = (struct sockaddr_in *)address;
4659 if (family_sa == AF_UNSPEC) {
4660 /* see __inet_bind(), we only want to allow
4661 * AF_UNSPEC if the address is INADDR_ANY
4663 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4665 family_sa = AF_INET;
4667 snum = ntohs(addr4->sin_port);
4668 addrp = (char *)&addr4->sin_addr.s_addr;
4671 if (addrlen < SIN6_LEN_RFC2133)
4673 addr6 = (struct sockaddr_in6 *)address;
4674 snum = ntohs(addr6->sin6_port);
4675 addrp = (char *)&addr6->sin6_addr.s6_addr;
4681 ad.type = LSM_AUDIT_DATA_NET;
4683 ad.u.net->sport = htons(snum);
4684 ad.u.net->family = family_sa;
4689 inet_get_local_port_range(sock_net(sk), &low, &high);
4691 if (inet_port_requires_bind_service(sock_net(sk), snum) ||
4692 snum < low || snum > high) {
4693 err = sel_netport_sid(sk->sk_protocol,
4697 err = avc_has_perm(&selinux_state,
4700 SOCKET__NAME_BIND, &ad);
4706 switch (sksec->sclass) {
4707 case SECCLASS_TCP_SOCKET:
4708 node_perm = TCP_SOCKET__NODE_BIND;
4711 case SECCLASS_UDP_SOCKET:
4712 node_perm = UDP_SOCKET__NODE_BIND;
4715 case SECCLASS_DCCP_SOCKET:
4716 node_perm = DCCP_SOCKET__NODE_BIND;
4719 case SECCLASS_SCTP_SOCKET:
4720 node_perm = SCTP_SOCKET__NODE_BIND;
4724 node_perm = RAWIP_SOCKET__NODE_BIND;
4728 err = sel_netnode_sid(addrp, family_sa, &sid);
4732 if (family_sa == AF_INET)
4733 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
4735 ad.u.net->v6info.saddr = addr6->sin6_addr;
4737 err = avc_has_perm(&selinux_state,
4739 sksec->sclass, node_perm, &ad);
4746 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4747 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4749 return -EAFNOSUPPORT;
4752 /* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
4753 * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst
4755 static int selinux_socket_connect_helper(struct socket *sock,
4756 struct sockaddr *address, int addrlen)
4758 struct sock *sk = sock->sk;
4759 struct sk_security_struct *sksec = sk->sk_security;
4762 err = sock_has_perm(sk, SOCKET__CONNECT);
4765 if (addrlen < offsetofend(struct sockaddr, sa_family))
4768 /* connect(AF_UNSPEC) has special handling, as it is a documented
4769 * way to disconnect the socket
4771 if (address->sa_family == AF_UNSPEC)
4775 * If a TCP, DCCP or SCTP socket, check name_connect permission
4778 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4779 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4780 sksec->sclass == SECCLASS_SCTP_SOCKET) {
4781 struct common_audit_data ad;
4782 struct lsm_network_audit net = {0,};
4783 struct sockaddr_in *addr4 = NULL;
4784 struct sockaddr_in6 *addr6 = NULL;
4785 unsigned short snum;
4788 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4789 * that validates multiple connect addresses. Because of this
4790 * need to check address->sa_family as it is possible to have
4791 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4793 switch (address->sa_family) {
4795 addr4 = (struct sockaddr_in *)address;
4796 if (addrlen < sizeof(struct sockaddr_in))
4798 snum = ntohs(addr4->sin_port);
4801 addr6 = (struct sockaddr_in6 *)address;
4802 if (addrlen < SIN6_LEN_RFC2133)
4804 snum = ntohs(addr6->sin6_port);
4807 /* Note that SCTP services expect -EINVAL, whereas
4808 * others expect -EAFNOSUPPORT.
4810 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4813 return -EAFNOSUPPORT;
4816 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
4820 switch (sksec->sclass) {
4821 case SECCLASS_TCP_SOCKET:
4822 perm = TCP_SOCKET__NAME_CONNECT;
4824 case SECCLASS_DCCP_SOCKET:
4825 perm = DCCP_SOCKET__NAME_CONNECT;
4827 case SECCLASS_SCTP_SOCKET:
4828 perm = SCTP_SOCKET__NAME_CONNECT;
4832 ad.type = LSM_AUDIT_DATA_NET;
4834 ad.u.net->dport = htons(snum);
4835 ad.u.net->family = address->sa_family;
4836 err = avc_has_perm(&selinux_state,
4837 sksec->sid, sid, sksec->sclass, perm, &ad);
4845 /* Supports connect(2), see comments in selinux_socket_connect_helper() */
4846 static int selinux_socket_connect(struct socket *sock,
4847 struct sockaddr *address, int addrlen)
4850 struct sock *sk = sock->sk;
4852 err = selinux_socket_connect_helper(sock, address, addrlen);
4856 return selinux_netlbl_socket_connect(sk, address);
4859 static int selinux_socket_listen(struct socket *sock, int backlog)
4861 return sock_has_perm(sock->sk, SOCKET__LISTEN);
4864 static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4867 struct inode_security_struct *isec;
4868 struct inode_security_struct *newisec;
4872 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
4876 isec = inode_security_novalidate(SOCK_INODE(sock));
4877 spin_lock(&isec->lock);
4878 sclass = isec->sclass;
4880 spin_unlock(&isec->lock);
4882 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4883 newisec->sclass = sclass;
4885 newisec->initialized = LABEL_INITIALIZED;
4890 static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
4893 return sock_has_perm(sock->sk, SOCKET__WRITE);
4896 static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4897 int size, int flags)
4899 return sock_has_perm(sock->sk, SOCKET__READ);
4902 static int selinux_socket_getsockname(struct socket *sock)
4904 return sock_has_perm(sock->sk, SOCKET__GETATTR);
4907 static int selinux_socket_getpeername(struct socket *sock)
4909 return sock_has_perm(sock->sk, SOCKET__GETATTR);
4912 static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
4916 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
4920 return selinux_netlbl_socket_setsockopt(sock, level, optname);
4923 static int selinux_socket_getsockopt(struct socket *sock, int level,
4926 return sock_has_perm(sock->sk, SOCKET__GETOPT);
4929 static int selinux_socket_shutdown(struct socket *sock, int how)
4931 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
4934 static int selinux_socket_unix_stream_connect(struct sock *sock,
4938 struct sk_security_struct *sksec_sock = sock->sk_security;
4939 struct sk_security_struct *sksec_other = other->sk_security;
4940 struct sk_security_struct *sksec_new = newsk->sk_security;
4941 struct common_audit_data ad;
4942 struct lsm_network_audit net = {0,};
4945 ad.type = LSM_AUDIT_DATA_NET;
4947 ad.u.net->sk = other;
4949 err = avc_has_perm(&selinux_state,
4950 sksec_sock->sid, sksec_other->sid,
4951 sksec_other->sclass,
4952 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4956 /* server child socket */
4957 sksec_new->peer_sid = sksec_sock->sid;
4958 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4959 sksec_sock->sid, &sksec_new->sid);
4963 /* connecting socket */
4964 sksec_sock->peer_sid = sksec_new->sid;
4969 static int selinux_socket_unix_may_send(struct socket *sock,
4970 struct socket *other)
4972 struct sk_security_struct *ssec = sock->sk->sk_security;
4973 struct sk_security_struct *osec = other->sk->sk_security;
4974 struct common_audit_data ad;
4975 struct lsm_network_audit net = {0,};
4977 ad.type = LSM_AUDIT_DATA_NET;
4979 ad.u.net->sk = other->sk;
4981 return avc_has_perm(&selinux_state,
4982 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4986 static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4987 char *addrp, u16 family, u32 peer_sid,
4988 struct common_audit_data *ad)
4994 err = sel_netif_sid(ns, ifindex, &if_sid);
4997 err = avc_has_perm(&selinux_state,
4999 SECCLASS_NETIF, NETIF__INGRESS, ad);
5003 err = sel_netnode_sid(addrp, family, &node_sid);
5006 return avc_has_perm(&selinux_state,
5008 SECCLASS_NODE, NODE__RECVFROM, ad);
5011 static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
5015 struct sk_security_struct *sksec = sk->sk_security;
5016 u32 sk_sid = sksec->sid;
5017 struct common_audit_data ad;
5018 struct lsm_network_audit net = {0,};
5021 ad.type = LSM_AUDIT_DATA_NET;
5023 ad.u.net->netif = skb->skb_iif;
5024 ad.u.net->family = family;
5025 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
5029 if (selinux_secmark_enabled()) {
5030 err = avc_has_perm(&selinux_state,
5031 sk_sid, skb->secmark, SECCLASS_PACKET,
5037 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
5040 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
5045 static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
5048 struct sk_security_struct *sksec = sk->sk_security;
5049 u16 family = sk->sk_family;
5050 u32 sk_sid = sksec->sid;
5051 struct common_audit_data ad;
5052 struct lsm_network_audit net = {0,};
5057 if (family != PF_INET && family != PF_INET6)
5060 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
5061 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5064 /* If any sort of compatibility mode is enabled then handoff processing
5065 * to the selinux_sock_rcv_skb_compat() function to deal with the
5066 * special handling. We do this in an attempt to keep this function
5067 * as fast and as clean as possible. */
5068 if (!selinux_policycap_netpeer())
5069 return selinux_sock_rcv_skb_compat(sk, skb, family);
5071 secmark_active = selinux_secmark_enabled();
5072 peerlbl_active = selinux_peerlbl_enabled();
5073 if (!secmark_active && !peerlbl_active)
5076 ad.type = LSM_AUDIT_DATA_NET;
5078 ad.u.net->netif = skb->skb_iif;
5079 ad.u.net->family = family;
5080 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
5084 if (peerlbl_active) {
5087 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
5090 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
5091 addrp, family, peer_sid, &ad);
5093 selinux_netlbl_err(skb, family, err, 0);
5096 err = avc_has_perm(&selinux_state,
5097 sk_sid, peer_sid, SECCLASS_PEER,
5100 selinux_netlbl_err(skb, family, err, 0);
5105 if (secmark_active) {
5106 err = avc_has_perm(&selinux_state,
5107 sk_sid, skb->secmark, SECCLASS_PACKET,
5116 static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
5117 int __user *optlen, unsigned len)
5122 struct sk_security_struct *sksec = sock->sk->sk_security;
5123 u32 peer_sid = SECSID_NULL;
5125 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
5126 sksec->sclass == SECCLASS_TCP_SOCKET ||
5127 sksec->sclass == SECCLASS_SCTP_SOCKET)
5128 peer_sid = sksec->peer_sid;
5129 if (peer_sid == SECSID_NULL)
5130 return -ENOPROTOOPT;
5132 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
5137 if (scontext_len > len) {
5142 if (copy_to_user(optval, scontext, scontext_len))
5146 if (put_user(scontext_len, optlen))
5152 static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
5154 u32 peer_secid = SECSID_NULL;
5156 struct inode_security_struct *isec;
5158 if (skb && skb->protocol == htons(ETH_P_IP))
5160 else if (skb && skb->protocol == htons(ETH_P_IPV6))
5163 family = sock->sk->sk_family;
5167 if (sock && family == PF_UNIX) {
5168 isec = inode_security_novalidate(SOCK_INODE(sock));
5169 peer_secid = isec->sid;
5171 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
5174 *secid = peer_secid;
5175 if (peer_secid == SECSID_NULL)
5180 static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
5182 struct sk_security_struct *sksec;
5184 sksec = kzalloc(sizeof(*sksec), priority);
5188 sksec->peer_sid = SECINITSID_UNLABELED;
5189 sksec->sid = SECINITSID_UNLABELED;
5190 sksec->sclass = SECCLASS_SOCKET;
5191 selinux_netlbl_sk_security_reset(sksec);
5192 sk->sk_security = sksec;
5197 static void selinux_sk_free_security(struct sock *sk)
5199 struct sk_security_struct *sksec = sk->sk_security;
5201 sk->sk_security = NULL;
5202 selinux_netlbl_sk_security_free(sksec);
5206 static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5208 struct sk_security_struct *sksec = sk->sk_security;
5209 struct sk_security_struct *newsksec = newsk->sk_security;
5211 newsksec->sid = sksec->sid;
5212 newsksec->peer_sid = sksec->peer_sid;
5213 newsksec->sclass = sksec->sclass;
5215 selinux_netlbl_sk_security_reset(newsksec);
5218 static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
5221 *secid = SECINITSID_ANY_SOCKET;
5223 struct sk_security_struct *sksec = sk->sk_security;
5225 *secid = sksec->sid;
5229 static void selinux_sock_graft(struct sock *sk, struct socket *parent)
5231 struct inode_security_struct *isec =
5232 inode_security_novalidate(SOCK_INODE(parent));
5233 struct sk_security_struct *sksec = sk->sk_security;
5235 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5236 sk->sk_family == PF_UNIX)
5237 isec->sid = sksec->sid;
5238 sksec->sclass = isec->sclass;
5242 * Determines peer_secid for the asoc and updates socket's peer label
5243 * if it's the first association on the socket.
5245 static int selinux_sctp_process_new_assoc(struct sctp_association *asoc,
5246 struct sk_buff *skb)
5248 struct sock *sk = asoc->base.sk;
5249 u16 family = sk->sk_family;
5250 struct sk_security_struct *sksec = sk->sk_security;
5251 struct common_audit_data ad;
5252 struct lsm_network_audit net = {0,};
5255 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5256 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5259 if (selinux_peerlbl_enabled()) {
5260 asoc->peer_secid = SECSID_NULL;
5262 /* This will return peer_sid = SECSID_NULL if there are
5263 * no peer labels, see security_net_peersid_resolve().
5265 err = selinux_skb_peerlbl_sid(skb, family, &asoc->peer_secid);
5269 if (asoc->peer_secid == SECSID_NULL)
5270 asoc->peer_secid = SECINITSID_UNLABELED;
5272 asoc->peer_secid = SECINITSID_UNLABELED;
5275 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5276 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5278 /* Here as first association on socket. As the peer SID
5279 * was allowed by peer recv (and the netif/node checks),
5280 * then it is approved by policy and used as the primary
5281 * peer SID for getpeercon(3).
5283 sksec->peer_sid = asoc->peer_secid;
5284 } else if (sksec->peer_sid != asoc->peer_secid) {
5285 /* Other association peer SIDs are checked to enforce
5286 * consistency among the peer SIDs.
5288 ad.type = LSM_AUDIT_DATA_NET;
5290 ad.u.net->sk = asoc->base.sk;
5291 err = avc_has_perm(&selinux_state,
5292 sksec->peer_sid, asoc->peer_secid,
5293 sksec->sclass, SCTP_SOCKET__ASSOCIATION,
5301 /* Called whenever SCTP receives an INIT or COOKIE ECHO chunk. This
5302 * happens on an incoming connect(2), sctp_connectx(3) or
5303 * sctp_sendmsg(3) (with no association already present).
5305 static int selinux_sctp_assoc_request(struct sctp_association *asoc,
5306 struct sk_buff *skb)
5308 struct sk_security_struct *sksec = asoc->base.sk->sk_security;
5312 if (!selinux_policycap_extsockclass())
5315 err = selinux_sctp_process_new_assoc(asoc, skb);
5319 /* Compute the MLS component for the connection and store
5320 * the information in asoc. This will be used by SCTP TCP type
5321 * sockets and peeled off connections as they cause a new
5322 * socket to be generated. selinux_sctp_sk_clone() will then
5323 * plug this into the new socket.
5325 err = selinux_conn_sid(sksec->sid, asoc->peer_secid, &conn_sid);
5329 asoc->secid = conn_sid;
5331 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5332 return selinux_netlbl_sctp_assoc_request(asoc, skb);
5335 /* Called when SCTP receives a COOKIE ACK chunk as the final
5336 * response to an association request (initited by us).
5338 static int selinux_sctp_assoc_established(struct sctp_association *asoc,
5339 struct sk_buff *skb)
5341 struct sk_security_struct *sksec = asoc->base.sk->sk_security;
5343 if (!selinux_policycap_extsockclass())
5346 /* Inherit secid from the parent socket - this will be picked up
5347 * by selinux_sctp_sk_clone() if the association gets peeled off
5348 * into a new socket.
5350 asoc->secid = sksec->sid;
5352 return selinux_sctp_process_new_assoc(asoc, skb);
5355 /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5356 * based on their @optname.
5358 static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5359 struct sockaddr *address,
5362 int len, err = 0, walk_size = 0;
5364 struct sockaddr *addr;
5365 struct socket *sock;
5367 if (!selinux_policycap_extsockclass())
5370 /* Process one or more addresses that may be IPv4 or IPv6 */
5371 sock = sk->sk_socket;
5374 while (walk_size < addrlen) {
5375 if (walk_size + sizeof(sa_family_t) > addrlen)
5379 switch (addr->sa_family) {
5382 len = sizeof(struct sockaddr_in);
5385 len = sizeof(struct sockaddr_in6);
5391 if (walk_size + len > addrlen)
5397 case SCTP_PRIMARY_ADDR:
5398 case SCTP_SET_PEER_PRIMARY_ADDR:
5399 case SCTP_SOCKOPT_BINDX_ADD:
5400 err = selinux_socket_bind(sock, addr, len);
5402 /* Connect checks */
5403 case SCTP_SOCKOPT_CONNECTX:
5404 case SCTP_PARAM_SET_PRIMARY:
5405 case SCTP_PARAM_ADD_IP:
5406 case SCTP_SENDMSG_CONNECT:
5407 err = selinux_socket_connect_helper(sock, addr, len);
5411 /* As selinux_sctp_bind_connect() is called by the
5412 * SCTP protocol layer, the socket is already locked,
5413 * therefore selinux_netlbl_socket_connect_locked()
5414 * is called here. The situations handled are:
5415 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5416 * whenever a new IP address is added or when a new
5417 * primary address is selected.
5418 * Note that an SCTP connect(2) call happens before
5419 * the SCTP protocol layer and is handled via
5420 * selinux_socket_connect().
5422 err = selinux_netlbl_socket_connect_locked(sk, addr);
5436 /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5437 static void selinux_sctp_sk_clone(struct sctp_association *asoc, struct sock *sk,
5440 struct sk_security_struct *sksec = sk->sk_security;
5441 struct sk_security_struct *newsksec = newsk->sk_security;
5443 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5444 * the non-sctp clone version.
5446 if (!selinux_policycap_extsockclass())
5447 return selinux_sk_clone_security(sk, newsk);
5449 newsksec->sid = asoc->secid;
5450 newsksec->peer_sid = asoc->peer_secid;
5451 newsksec->sclass = sksec->sclass;
5452 selinux_netlbl_sctp_sk_clone(sk, newsk);
5455 static int selinux_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
5456 struct request_sock *req)
5458 struct sk_security_struct *sksec = sk->sk_security;
5460 u16 family = req->rsk_ops->family;
5464 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
5467 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5470 req->secid = connsid;
5471 req->peer_secid = peersid;
5473 return selinux_netlbl_inet_conn_request(req, family);
5476 static void selinux_inet_csk_clone(struct sock *newsk,
5477 const struct request_sock *req)
5479 struct sk_security_struct *newsksec = newsk->sk_security;
5481 newsksec->sid = req->secid;
5482 newsksec->peer_sid = req->peer_secid;
5483 /* NOTE: Ideally, we should also get the isec->sid for the
5484 new socket in sync, but we don't have the isec available yet.
5485 So we will wait until sock_graft to do it, by which
5486 time it will have been created and available. */
5488 /* We don't need to take any sort of lock here as we are the only
5489 * thread with access to newsksec */
5490 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
5493 static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
5495 u16 family = sk->sk_family;
5496 struct sk_security_struct *sksec = sk->sk_security;
5498 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5499 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5502 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
5505 static int selinux_secmark_relabel_packet(u32 sid)
5507 const struct task_security_struct *__tsec;
5510 __tsec = selinux_cred(current_cred());
5513 return avc_has_perm(&selinux_state,
5514 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5518 static void selinux_secmark_refcount_inc(void)
5520 atomic_inc(&selinux_secmark_refcount);
5523 static void selinux_secmark_refcount_dec(void)
5525 atomic_dec(&selinux_secmark_refcount);
5528 static void selinux_req_classify_flow(const struct request_sock *req,
5529 struct flowi_common *flic)
5531 flic->flowic_secid = req->secid;
5534 static int selinux_tun_dev_alloc_security(void **security)
5536 struct tun_security_struct *tunsec;
5538 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5541 tunsec->sid = current_sid();
5547 static void selinux_tun_dev_free_security(void *security)
5552 static int selinux_tun_dev_create(void)
5554 u32 sid = current_sid();
5556 /* we aren't taking into account the "sockcreate" SID since the socket
5557 * that is being created here is not a socket in the traditional sense,
5558 * instead it is a private sock, accessible only to the kernel, and
5559 * representing a wide range of network traffic spanning multiple
5560 * connections unlike traditional sockets - check the TUN driver to
5561 * get a better understanding of why this socket is special */
5563 return avc_has_perm(&selinux_state,
5564 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
5568 static int selinux_tun_dev_attach_queue(void *security)
5570 struct tun_security_struct *tunsec = security;
5572 return avc_has_perm(&selinux_state,
5573 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
5574 TUN_SOCKET__ATTACH_QUEUE, NULL);
5577 static int selinux_tun_dev_attach(struct sock *sk, void *security)
5579 struct tun_security_struct *tunsec = security;
5580 struct sk_security_struct *sksec = sk->sk_security;
5582 /* we don't currently perform any NetLabel based labeling here and it
5583 * isn't clear that we would want to do so anyway; while we could apply
5584 * labeling without the support of the TUN user the resulting labeled
5585 * traffic from the other end of the connection would almost certainly
5586 * cause confusion to the TUN user that had no idea network labeling
5587 * protocols were being used */
5589 sksec->sid = tunsec->sid;
5590 sksec->sclass = SECCLASS_TUN_SOCKET;
5595 static int selinux_tun_dev_open(void *security)
5597 struct tun_security_struct *tunsec = security;
5598 u32 sid = current_sid();
5601 err = avc_has_perm(&selinux_state,
5602 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
5603 TUN_SOCKET__RELABELFROM, NULL);
5606 err = avc_has_perm(&selinux_state,
5607 sid, sid, SECCLASS_TUN_SOCKET,
5608 TUN_SOCKET__RELABELTO, NULL);
5616 #ifdef CONFIG_NETFILTER
5618 static unsigned int selinux_ip_forward(void *priv, struct sk_buff *skb,
5619 const struct nf_hook_state *state)
5625 struct common_audit_data ad;
5626 struct lsm_network_audit net = {0,};
5627 int secmark_active, peerlbl_active;
5629 if (!selinux_policycap_netpeer())
5632 secmark_active = selinux_secmark_enabled();
5633 peerlbl_active = selinux_peerlbl_enabled();
5634 if (!secmark_active && !peerlbl_active)
5638 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5641 ifindex = state->in->ifindex;
5642 ad.type = LSM_AUDIT_DATA_NET;
5644 ad.u.net->netif = ifindex;
5645 ad.u.net->family = family;
5646 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5649 if (peerlbl_active) {
5652 err = selinux_inet_sys_rcv_skb(state->net, ifindex,
5653 addrp, family, peer_sid, &ad);
5655 selinux_netlbl_err(skb, family, err, 1);
5661 if (avc_has_perm(&selinux_state,
5662 peer_sid, skb->secmark,
5663 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5666 if (netlbl_enabled())
5667 /* we do this in the FORWARD path and not the POST_ROUTING
5668 * path because we want to make sure we apply the necessary
5669 * labeling before IPsec is applied so we can leverage AH
5671 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5677 static unsigned int selinux_ip_output(void *priv, struct sk_buff *skb,
5678 const struct nf_hook_state *state)
5683 if (!netlbl_enabled())
5686 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5687 * because we want to make sure we apply the necessary labeling
5688 * before IPsec is applied so we can leverage AH protection */
5691 struct sk_security_struct *sksec;
5693 if (sk_listener(sk))
5694 /* if the socket is the listening state then this
5695 * packet is a SYN-ACK packet which means it needs to
5696 * be labeled based on the connection/request_sock and
5697 * not the parent socket. unfortunately, we can't
5698 * lookup the request_sock yet as it isn't queued on
5699 * the parent socket until after the SYN-ACK is sent.
5700 * the "solution" is to simply pass the packet as-is
5701 * as any IP option based labeling should be copied
5702 * from the initial connection request (in the IP
5703 * layer). it is far from ideal, but until we get a
5704 * security label in the packet itself this is the
5705 * best we can do. */
5708 /* standard practice, label using the parent socket */
5709 sksec = sk->sk_security;
5712 sid = SECINITSID_KERNEL;
5713 if (selinux_netlbl_skbuff_setsid(skb, state->pf, sid) != 0)
5720 static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5721 const struct nf_hook_state *state)
5724 struct sk_security_struct *sksec;
5725 struct common_audit_data ad;
5726 struct lsm_network_audit net = {0,};
5729 sk = skb_to_full_sk(skb);
5732 sksec = sk->sk_security;
5734 ad.type = LSM_AUDIT_DATA_NET;
5736 ad.u.net->netif = state->out->ifindex;
5737 ad.u.net->family = state->pf;
5738 if (selinux_parse_skb(skb, &ad, NULL, 0, &proto))
5741 if (selinux_secmark_enabled())
5742 if (avc_has_perm(&selinux_state,
5743 sksec->sid, skb->secmark,
5744 SECCLASS_PACKET, PACKET__SEND, &ad))
5745 return NF_DROP_ERR(-ECONNREFUSED);
5747 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5748 return NF_DROP_ERR(-ECONNREFUSED);
5753 static unsigned int selinux_ip_postroute(void *priv,
5754 struct sk_buff *skb,
5755 const struct nf_hook_state *state)
5762 struct common_audit_data ad;
5763 struct lsm_network_audit net = {0,};
5765 int secmark_active, peerlbl_active;
5767 /* If any sort of compatibility mode is enabled then handoff processing
5768 * to the selinux_ip_postroute_compat() function to deal with the
5769 * special handling. We do this in an attempt to keep this function
5770 * as fast and as clean as possible. */
5771 if (!selinux_policycap_netpeer())
5772 return selinux_ip_postroute_compat(skb, state);
5774 secmark_active = selinux_secmark_enabled();
5775 peerlbl_active = selinux_peerlbl_enabled();
5776 if (!secmark_active && !peerlbl_active)
5779 sk = skb_to_full_sk(skb);
5782 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5783 * packet transformation so allow the packet to pass without any checks
5784 * since we'll have another chance to perform access control checks
5785 * when the packet is on it's final way out.
5786 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
5787 * is NULL, in this case go ahead and apply access control.
5788 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5789 * TCP listening state we cannot wait until the XFRM processing
5790 * is done as we will miss out on the SA label if we do;
5791 * unfortunately, this means more work, but it is only once per
5793 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
5794 !(sk && sk_listener(sk)))
5800 /* Without an associated socket the packet is either coming
5801 * from the kernel or it is being forwarded; check the packet
5802 * to determine which and if the packet is being forwarded
5803 * query the packet directly to determine the security label. */
5805 secmark_perm = PACKET__FORWARD_OUT;
5806 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
5809 secmark_perm = PACKET__SEND;
5810 peer_sid = SECINITSID_KERNEL;
5812 } else if (sk_listener(sk)) {
5813 /* Locally generated packet but the associated socket is in the
5814 * listening state which means this is a SYN-ACK packet. In
5815 * this particular case the correct security label is assigned
5816 * to the connection/request_sock but unfortunately we can't
5817 * query the request_sock as it isn't queued on the parent
5818 * socket until after the SYN-ACK packet is sent; the only
5819 * viable choice is to regenerate the label like we do in
5820 * selinux_inet_conn_request(). See also selinux_ip_output()
5821 * for similar problems. */
5823 struct sk_security_struct *sksec;
5825 sksec = sk->sk_security;
5826 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5828 /* At this point, if the returned skb peerlbl is SECSID_NULL
5829 * and the packet has been through at least one XFRM
5830 * transformation then we must be dealing with the "final"
5831 * form of labeled IPsec packet; since we've already applied
5832 * all of our access controls on this packet we can safely
5833 * pass the packet. */
5834 if (skb_sid == SECSID_NULL) {
5837 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5841 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5845 return NF_DROP_ERR(-ECONNREFUSED);
5848 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5850 secmark_perm = PACKET__SEND;
5852 /* Locally generated packet, fetch the security label from the
5853 * associated socket. */
5854 struct sk_security_struct *sksec = sk->sk_security;
5855 peer_sid = sksec->sid;
5856 secmark_perm = PACKET__SEND;
5859 ifindex = state->out->ifindex;
5860 ad.type = LSM_AUDIT_DATA_NET;
5862 ad.u.net->netif = ifindex;
5863 ad.u.net->family = family;
5864 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
5868 if (avc_has_perm(&selinux_state,
5869 peer_sid, skb->secmark,
5870 SECCLASS_PACKET, secmark_perm, &ad))
5871 return NF_DROP_ERR(-ECONNREFUSED);
5873 if (peerlbl_active) {
5877 if (sel_netif_sid(state->net, ifindex, &if_sid))
5879 if (avc_has_perm(&selinux_state,
5881 SECCLASS_NETIF, NETIF__EGRESS, &ad))
5882 return NF_DROP_ERR(-ECONNREFUSED);
5884 if (sel_netnode_sid(addrp, family, &node_sid))
5886 if (avc_has_perm(&selinux_state,
5888 SECCLASS_NODE, NODE__SENDTO, &ad))
5889 return NF_DROP_ERR(-ECONNREFUSED);
5894 #endif /* CONFIG_NETFILTER */
5896 static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5899 unsigned int msg_len;
5900 unsigned int data_len = skb->len;
5901 unsigned char *data = skb->data;
5902 struct nlmsghdr *nlh;
5903 struct sk_security_struct *sksec = sk->sk_security;
5904 u16 sclass = sksec->sclass;
5907 while (data_len >= nlmsg_total_size(0)) {
5908 nlh = (struct nlmsghdr *)data;
5910 /* NOTE: the nlmsg_len field isn't reliably set by some netlink
5911 * users which means we can't reject skb's with bogus
5912 * length fields; our solution is to follow what
5913 * netlink_rcv_skb() does and simply skip processing at
5914 * messages with length fields that are clearly junk
5916 if (nlh->nlmsg_len < NLMSG_HDRLEN || nlh->nlmsg_len > data_len)
5919 rc = selinux_nlmsg_lookup(sclass, nlh->nlmsg_type, &perm);
5921 rc = sock_has_perm(sk, perm);
5924 } else if (rc == -EINVAL) {
5925 /* -EINVAL is a missing msg/perm mapping */
5926 pr_warn_ratelimited("SELinux: unrecognized netlink"
5927 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5928 " pid=%d comm=%s\n",
5929 sk->sk_protocol, nlh->nlmsg_type,
5930 secclass_map[sclass - 1].name,
5931 task_pid_nr(current), current->comm);
5932 if (enforcing_enabled(&selinux_state) &&
5933 !security_get_allow_unknown(&selinux_state))
5936 } else if (rc == -ENOENT) {
5937 /* -ENOENT is a missing socket/class mapping, ignore */
5943 /* move to the next message after applying netlink padding */
5944 msg_len = NLMSG_ALIGN(nlh->nlmsg_len);
5945 if (msg_len >= data_len)
5947 data_len -= msg_len;
5954 static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass)
5956 isec->sclass = sclass;
5957 isec->sid = current_sid();
5960 static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
5963 struct ipc_security_struct *isec;
5964 struct common_audit_data ad;
5965 u32 sid = current_sid();
5967 isec = selinux_ipc(ipc_perms);
5969 ad.type = LSM_AUDIT_DATA_IPC;
5970 ad.u.ipc_id = ipc_perms->key;
5972 return avc_has_perm(&selinux_state,
5973 sid, isec->sid, isec->sclass, perms, &ad);
5976 static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5978 struct msg_security_struct *msec;
5980 msec = selinux_msg_msg(msg);
5981 msec->sid = SECINITSID_UNLABELED;
5986 /* message queue security operations */
5987 static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
5989 struct ipc_security_struct *isec;
5990 struct common_audit_data ad;
5991 u32 sid = current_sid();
5994 isec = selinux_ipc(msq);
5995 ipc_init_security(isec, SECCLASS_MSGQ);
5997 ad.type = LSM_AUDIT_DATA_IPC;
5998 ad.u.ipc_id = msq->key;
6000 rc = avc_has_perm(&selinux_state,
6001 sid, isec->sid, SECCLASS_MSGQ,
6006 static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
6008 struct ipc_security_struct *isec;
6009 struct common_audit_data ad;
6010 u32 sid = current_sid();
6012 isec = selinux_ipc(msq);
6014 ad.type = LSM_AUDIT_DATA_IPC;
6015 ad.u.ipc_id = msq->key;
6017 return avc_has_perm(&selinux_state,
6018 sid, isec->sid, SECCLASS_MSGQ,
6019 MSGQ__ASSOCIATE, &ad);
6022 static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
6030 /* No specific object, just general system-wide information. */
6031 return avc_has_perm(&selinux_state,
6032 current_sid(), SECINITSID_KERNEL,
6033 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
6037 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
6040 perms = MSGQ__SETATTR;
6043 perms = MSGQ__DESTROY;
6049 err = ipc_has_perm(msq, perms);
6053 static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
6055 struct ipc_security_struct *isec;
6056 struct msg_security_struct *msec;
6057 struct common_audit_data ad;
6058 u32 sid = current_sid();
6061 isec = selinux_ipc(msq);
6062 msec = selinux_msg_msg(msg);
6065 * First time through, need to assign label to the message
6067 if (msec->sid == SECINITSID_UNLABELED) {
6069 * Compute new sid based on current process and
6070 * message queue this message will be stored in
6072 rc = security_transition_sid(&selinux_state, sid, isec->sid,
6073 SECCLASS_MSG, NULL, &msec->sid);
6078 ad.type = LSM_AUDIT_DATA_IPC;
6079 ad.u.ipc_id = msq->key;
6081 /* Can this process write to the queue? */
6082 rc = avc_has_perm(&selinux_state,
6083 sid, isec->sid, SECCLASS_MSGQ,
6086 /* Can this process send the message */
6087 rc = avc_has_perm(&selinux_state,
6088 sid, msec->sid, SECCLASS_MSG,
6091 /* Can the message be put in the queue? */
6092 rc = avc_has_perm(&selinux_state,
6093 msec->sid, isec->sid, SECCLASS_MSGQ,
6094 MSGQ__ENQUEUE, &ad);
6099 static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
6100 struct task_struct *target,
6101 long type, int mode)
6103 struct ipc_security_struct *isec;
6104 struct msg_security_struct *msec;
6105 struct common_audit_data ad;
6106 u32 sid = task_sid_obj(target);
6109 isec = selinux_ipc(msq);
6110 msec = selinux_msg_msg(msg);
6112 ad.type = LSM_AUDIT_DATA_IPC;
6113 ad.u.ipc_id = msq->key;
6115 rc = avc_has_perm(&selinux_state,
6117 SECCLASS_MSGQ, MSGQ__READ, &ad);
6119 rc = avc_has_perm(&selinux_state,
6121 SECCLASS_MSG, MSG__RECEIVE, &ad);
6125 /* Shared Memory security operations */
6126 static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
6128 struct ipc_security_struct *isec;
6129 struct common_audit_data ad;
6130 u32 sid = current_sid();
6133 isec = selinux_ipc(shp);
6134 ipc_init_security(isec, SECCLASS_SHM);
6136 ad.type = LSM_AUDIT_DATA_IPC;
6137 ad.u.ipc_id = shp->key;
6139 rc = avc_has_perm(&selinux_state,
6140 sid, isec->sid, SECCLASS_SHM,
6145 static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
6147 struct ipc_security_struct *isec;
6148 struct common_audit_data ad;
6149 u32 sid = current_sid();
6151 isec = selinux_ipc(shp);
6153 ad.type = LSM_AUDIT_DATA_IPC;
6154 ad.u.ipc_id = shp->key;
6156 return avc_has_perm(&selinux_state,
6157 sid, isec->sid, SECCLASS_SHM,
6158 SHM__ASSOCIATE, &ad);
6161 /* Note, at this point, shp is locked down */
6162 static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
6170 /* No specific object, just general system-wide information. */
6171 return avc_has_perm(&selinux_state,
6172 current_sid(), SECINITSID_KERNEL,
6173 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
6177 perms = SHM__GETATTR | SHM__ASSOCIATE;
6180 perms = SHM__SETATTR;
6187 perms = SHM__DESTROY;
6193 err = ipc_has_perm(shp, perms);
6197 static int selinux_shm_shmat(struct kern_ipc_perm *shp,
6198 char __user *shmaddr, int shmflg)
6202 if (shmflg & SHM_RDONLY)
6205 perms = SHM__READ | SHM__WRITE;
6207 return ipc_has_perm(shp, perms);
6210 /* Semaphore security operations */
6211 static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
6213 struct ipc_security_struct *isec;
6214 struct common_audit_data ad;
6215 u32 sid = current_sid();
6218 isec = selinux_ipc(sma);
6219 ipc_init_security(isec, SECCLASS_SEM);
6221 ad.type = LSM_AUDIT_DATA_IPC;
6222 ad.u.ipc_id = sma->key;
6224 rc = avc_has_perm(&selinux_state,
6225 sid, isec->sid, SECCLASS_SEM,
6230 static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
6232 struct ipc_security_struct *isec;
6233 struct common_audit_data ad;
6234 u32 sid = current_sid();
6236 isec = selinux_ipc(sma);
6238 ad.type = LSM_AUDIT_DATA_IPC;
6239 ad.u.ipc_id = sma->key;
6241 return avc_has_perm(&selinux_state,
6242 sid, isec->sid, SECCLASS_SEM,
6243 SEM__ASSOCIATE, &ad);
6246 /* Note, at this point, sma is locked down */
6247 static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
6255 /* No specific object, just general system-wide information. */
6256 return avc_has_perm(&selinux_state,
6257 current_sid(), SECINITSID_KERNEL,
6258 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
6262 perms = SEM__GETATTR;
6273 perms = SEM__DESTROY;
6276 perms = SEM__SETATTR;
6281 perms = SEM__GETATTR | SEM__ASSOCIATE;
6287 err = ipc_has_perm(sma, perms);
6291 static int selinux_sem_semop(struct kern_ipc_perm *sma,
6292 struct sembuf *sops, unsigned nsops, int alter)
6297 perms = SEM__READ | SEM__WRITE;
6301 return ipc_has_perm(sma, perms);
6304 static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6310 av |= IPC__UNIX_READ;
6312 av |= IPC__UNIX_WRITE;
6317 return ipc_has_perm(ipcp, av);
6320 static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6322 struct ipc_security_struct *isec = selinux_ipc(ipcp);
6326 static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
6329 inode_doinit_with_dentry(inode, dentry);
6332 static int selinux_getprocattr(struct task_struct *p,
6333 char *name, char **value)
6335 const struct task_security_struct *__tsec;
6341 __tsec = selinux_cred(__task_cred(p));
6344 error = avc_has_perm(&selinux_state,
6345 current_sid(), __tsec->sid,
6346 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6351 if (!strcmp(name, "current"))
6353 else if (!strcmp(name, "prev"))
6355 else if (!strcmp(name, "exec"))
6356 sid = __tsec->exec_sid;
6357 else if (!strcmp(name, "fscreate"))
6358 sid = __tsec->create_sid;
6359 else if (!strcmp(name, "keycreate"))
6360 sid = __tsec->keycreate_sid;
6361 else if (!strcmp(name, "sockcreate"))
6362 sid = __tsec->sockcreate_sid;
6372 error = security_sid_to_context(&selinux_state, sid, value, &len);
6382 static int selinux_setprocattr(const char *name, void *value, size_t size)
6384 struct task_security_struct *tsec;
6386 u32 mysid = current_sid(), sid = 0, ptsid;
6391 * Basic control over ability to set these attributes at all.
6393 if (!strcmp(name, "exec"))
6394 error = avc_has_perm(&selinux_state,
6395 mysid, mysid, SECCLASS_PROCESS,
6396 PROCESS__SETEXEC, NULL);
6397 else if (!strcmp(name, "fscreate"))
6398 error = avc_has_perm(&selinux_state,
6399 mysid, mysid, SECCLASS_PROCESS,
6400 PROCESS__SETFSCREATE, NULL);
6401 else if (!strcmp(name, "keycreate"))
6402 error = avc_has_perm(&selinux_state,
6403 mysid, mysid, SECCLASS_PROCESS,
6404 PROCESS__SETKEYCREATE, NULL);
6405 else if (!strcmp(name, "sockcreate"))
6406 error = avc_has_perm(&selinux_state,
6407 mysid, mysid, SECCLASS_PROCESS,
6408 PROCESS__SETSOCKCREATE, NULL);
6409 else if (!strcmp(name, "current"))
6410 error = avc_has_perm(&selinux_state,
6411 mysid, mysid, SECCLASS_PROCESS,
6412 PROCESS__SETCURRENT, NULL);
6418 /* Obtain a SID for the context, if one was specified. */
6419 if (size && str[0] && str[0] != '\n') {
6420 if (str[size-1] == '\n') {
6424 error = security_context_to_sid(&selinux_state, value, size,
6426 if (error == -EINVAL && !strcmp(name, "fscreate")) {
6427 if (!has_cap_mac_admin(true)) {
6428 struct audit_buffer *ab;
6431 /* We strip a nul only if it is at the end, otherwise the
6432 * context contains a nul and we should audit that */
6433 if (str[size - 1] == '\0')
6434 audit_size = size - 1;
6437 ab = audit_log_start(audit_context(),
6442 audit_log_format(ab, "op=fscreate invalid_context=");
6443 audit_log_n_untrustedstring(ab, value, audit_size);
6448 error = security_context_to_sid_force(
6456 new = prepare_creds();
6460 /* Permission checking based on the specified context is
6461 performed during the actual operation (execve,
6462 open/mkdir/...), when we know the full context of the
6463 operation. See selinux_bprm_creds_for_exec for the execve
6464 checks and may_create for the file creation checks. The
6465 operation will then fail if the context is not permitted. */
6466 tsec = selinux_cred(new);
6467 if (!strcmp(name, "exec")) {
6468 tsec->exec_sid = sid;
6469 } else if (!strcmp(name, "fscreate")) {
6470 tsec->create_sid = sid;
6471 } else if (!strcmp(name, "keycreate")) {
6473 error = avc_has_perm(&selinux_state, mysid, sid,
6474 SECCLASS_KEY, KEY__CREATE, NULL);
6478 tsec->keycreate_sid = sid;
6479 } else if (!strcmp(name, "sockcreate")) {
6480 tsec->sockcreate_sid = sid;
6481 } else if (!strcmp(name, "current")) {
6486 /* Only allow single threaded processes to change context */
6487 if (!current_is_single_threaded()) {
6488 error = security_bounded_transition(&selinux_state,
6494 /* Check permissions for the transition. */
6495 error = avc_has_perm(&selinux_state,
6496 tsec->sid, sid, SECCLASS_PROCESS,
6497 PROCESS__DYNTRANSITION, NULL);
6501 /* Check for ptracing, and update the task SID if ok.
6502 Otherwise, leave SID unchanged and fail. */
6503 ptsid = ptrace_parent_sid();
6505 error = avc_has_perm(&selinux_state,
6506 ptsid, sid, SECCLASS_PROCESS,
6507 PROCESS__PTRACE, NULL);
6526 static int selinux_ismaclabel(const char *name)
6528 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6531 static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6533 return security_sid_to_context(&selinux_state, secid,
6537 static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
6539 return security_context_to_sid(&selinux_state, secdata, seclen,
6543 static void selinux_release_secctx(char *secdata, u32 seclen)
6548 static void selinux_inode_invalidate_secctx(struct inode *inode)
6550 struct inode_security_struct *isec = selinux_inode(inode);
6552 spin_lock(&isec->lock);
6553 isec->initialized = LABEL_INVALID;
6554 spin_unlock(&isec->lock);
6558 * called with inode->i_mutex locked
6560 static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6562 int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
6564 /* Do not return error when suppressing label (SBLABEL_MNT not set). */
6565 return rc == -EOPNOTSUPP ? 0 : rc;
6569 * called with inode->i_mutex locked
6571 static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6573 return __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_SELINUX,
6577 static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6580 len = selinux_inode_getsecurity(&init_user_ns, inode,
6581 XATTR_SELINUX_SUFFIX, ctx, true);
6589 static int selinux_key_alloc(struct key *k, const struct cred *cred,
6590 unsigned long flags)
6592 const struct task_security_struct *tsec;
6593 struct key_security_struct *ksec;
6595 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6599 tsec = selinux_cred(cred);
6600 if (tsec->keycreate_sid)
6601 ksec->sid = tsec->keycreate_sid;
6603 ksec->sid = tsec->sid;
6609 static void selinux_key_free(struct key *k)
6611 struct key_security_struct *ksec = k->security;
6617 static int selinux_key_permission(key_ref_t key_ref,
6618 const struct cred *cred,
6619 enum key_need_perm need_perm)
6622 struct key_security_struct *ksec;
6625 switch (need_perm) {
6632 case KEY_NEED_WRITE:
6635 case KEY_NEED_SEARCH:
6641 case KEY_NEED_SETATTR:
6642 perm = KEY__SETATTR;
6644 case KEY_NEED_UNLINK:
6645 case KEY_SYSADMIN_OVERRIDE:
6646 case KEY_AUTHTOKEN_OVERRIDE:
6647 case KEY_DEFER_PERM_CHECK:
6655 sid = cred_sid(cred);
6656 key = key_ref_to_ptr(key_ref);
6657 ksec = key->security;
6659 return avc_has_perm(&selinux_state,
6660 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
6663 static int selinux_key_getsecurity(struct key *key, char **_buffer)
6665 struct key_security_struct *ksec = key->security;
6666 char *context = NULL;
6670 rc = security_sid_to_context(&selinux_state, ksec->sid,
6678 #ifdef CONFIG_KEY_NOTIFICATIONS
6679 static int selinux_watch_key(struct key *key)
6681 struct key_security_struct *ksec = key->security;
6682 u32 sid = current_sid();
6684 return avc_has_perm(&selinux_state,
6685 sid, ksec->sid, SECCLASS_KEY, KEY__VIEW, NULL);
6690 #ifdef CONFIG_SECURITY_INFINIBAND
6691 static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6693 struct common_audit_data ad;
6696 struct ib_security_struct *sec = ib_sec;
6697 struct lsm_ibpkey_audit ibpkey;
6699 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
6703 ad.type = LSM_AUDIT_DATA_IBPKEY;
6704 ibpkey.subnet_prefix = subnet_prefix;
6705 ibpkey.pkey = pkey_val;
6706 ad.u.ibpkey = &ibpkey;
6707 return avc_has_perm(&selinux_state,
6709 SECCLASS_INFINIBAND_PKEY,
6710 INFINIBAND_PKEY__ACCESS, &ad);
6713 static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6716 struct common_audit_data ad;
6719 struct ib_security_struct *sec = ib_sec;
6720 struct lsm_ibendport_audit ibendport;
6722 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6728 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6729 ibendport.dev_name = dev_name;
6730 ibendport.port = port_num;
6731 ad.u.ibendport = &ibendport;
6732 return avc_has_perm(&selinux_state,
6734 SECCLASS_INFINIBAND_ENDPORT,
6735 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6738 static int selinux_ib_alloc_security(void **ib_sec)
6740 struct ib_security_struct *sec;
6742 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6745 sec->sid = current_sid();
6751 static void selinux_ib_free_security(void *ib_sec)
6757 #ifdef CONFIG_BPF_SYSCALL
6758 static int selinux_bpf(int cmd, union bpf_attr *attr,
6761 u32 sid = current_sid();
6765 case BPF_MAP_CREATE:
6766 ret = avc_has_perm(&selinux_state,
6767 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
6771 ret = avc_has_perm(&selinux_state,
6772 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
6783 static u32 bpf_map_fmode_to_av(fmode_t fmode)
6787 if (fmode & FMODE_READ)
6788 av |= BPF__MAP_READ;
6789 if (fmode & FMODE_WRITE)
6790 av |= BPF__MAP_WRITE;
6794 /* This function will check the file pass through unix socket or binder to see
6795 * if it is a bpf related object. And apply correspinding checks on the bpf
6796 * object based on the type. The bpf maps and programs, not like other files and
6797 * socket, are using a shared anonymous inode inside the kernel as their inode.
6798 * So checking that inode cannot identify if the process have privilege to
6799 * access the bpf object and that's why we have to add this additional check in
6800 * selinux_file_receive and selinux_binder_transfer_files.
6802 static int bpf_fd_pass(struct file *file, u32 sid)
6804 struct bpf_security_struct *bpfsec;
6805 struct bpf_prog *prog;
6806 struct bpf_map *map;
6809 if (file->f_op == &bpf_map_fops) {
6810 map = file->private_data;
6811 bpfsec = map->security;
6812 ret = avc_has_perm(&selinux_state,
6813 sid, bpfsec->sid, SECCLASS_BPF,
6814 bpf_map_fmode_to_av(file->f_mode), NULL);
6817 } else if (file->f_op == &bpf_prog_fops) {
6818 prog = file->private_data;
6819 bpfsec = prog->aux->security;
6820 ret = avc_has_perm(&selinux_state,
6821 sid, bpfsec->sid, SECCLASS_BPF,
6822 BPF__PROG_RUN, NULL);
6829 static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6831 u32 sid = current_sid();
6832 struct bpf_security_struct *bpfsec;
6834 bpfsec = map->security;
6835 return avc_has_perm(&selinux_state,
6836 sid, bpfsec->sid, SECCLASS_BPF,
6837 bpf_map_fmode_to_av(fmode), NULL);
6840 static int selinux_bpf_prog(struct bpf_prog *prog)
6842 u32 sid = current_sid();
6843 struct bpf_security_struct *bpfsec;
6845 bpfsec = prog->aux->security;
6846 return avc_has_perm(&selinux_state,
6847 sid, bpfsec->sid, SECCLASS_BPF,
6848 BPF__PROG_RUN, NULL);
6851 static int selinux_bpf_map_alloc(struct bpf_map *map)
6853 struct bpf_security_struct *bpfsec;
6855 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6859 bpfsec->sid = current_sid();
6860 map->security = bpfsec;
6865 static void selinux_bpf_map_free(struct bpf_map *map)
6867 struct bpf_security_struct *bpfsec = map->security;
6869 map->security = NULL;
6873 static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6875 struct bpf_security_struct *bpfsec;
6877 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6881 bpfsec->sid = current_sid();
6882 aux->security = bpfsec;
6887 static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6889 struct bpf_security_struct *bpfsec = aux->security;
6891 aux->security = NULL;
6896 struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
6897 .lbs_cred = sizeof(struct task_security_struct),
6898 .lbs_file = sizeof(struct file_security_struct),
6899 .lbs_inode = sizeof(struct inode_security_struct),
6900 .lbs_ipc = sizeof(struct ipc_security_struct),
6901 .lbs_msg_msg = sizeof(struct msg_security_struct),
6902 .lbs_superblock = sizeof(struct superblock_security_struct),
6905 #ifdef CONFIG_PERF_EVENTS
6906 static int selinux_perf_event_open(struct perf_event_attr *attr, int type)
6908 u32 requested, sid = current_sid();
6910 if (type == PERF_SECURITY_OPEN)
6911 requested = PERF_EVENT__OPEN;
6912 else if (type == PERF_SECURITY_CPU)
6913 requested = PERF_EVENT__CPU;
6914 else if (type == PERF_SECURITY_KERNEL)
6915 requested = PERF_EVENT__KERNEL;
6916 else if (type == PERF_SECURITY_TRACEPOINT)
6917 requested = PERF_EVENT__TRACEPOINT;
6921 return avc_has_perm(&selinux_state, sid, sid, SECCLASS_PERF_EVENT,
6925 static int selinux_perf_event_alloc(struct perf_event *event)
6927 struct perf_event_security_struct *perfsec;
6929 perfsec = kzalloc(sizeof(*perfsec), GFP_KERNEL);
6933 perfsec->sid = current_sid();
6934 event->security = perfsec;
6939 static void selinux_perf_event_free(struct perf_event *event)
6941 struct perf_event_security_struct *perfsec = event->security;
6943 event->security = NULL;
6947 static int selinux_perf_event_read(struct perf_event *event)
6949 struct perf_event_security_struct *perfsec = event->security;
6950 u32 sid = current_sid();
6952 return avc_has_perm(&selinux_state, sid, perfsec->sid,
6953 SECCLASS_PERF_EVENT, PERF_EVENT__READ, NULL);
6956 static int selinux_perf_event_write(struct perf_event *event)
6958 struct perf_event_security_struct *perfsec = event->security;
6959 u32 sid = current_sid();
6961 return avc_has_perm(&selinux_state, sid, perfsec->sid,
6962 SECCLASS_PERF_EVENT, PERF_EVENT__WRITE, NULL);
6966 #ifdef CONFIG_IO_URING
6968 * selinux_uring_override_creds - check the requested cred override
6969 * @new: the target creds
6971 * Check to see if the current task is allowed to override it's credentials
6972 * to service an io_uring operation.
6974 static int selinux_uring_override_creds(const struct cred *new)
6976 return avc_has_perm(&selinux_state, current_sid(), cred_sid(new),
6977 SECCLASS_IO_URING, IO_URING__OVERRIDE_CREDS, NULL);
6981 * selinux_uring_sqpoll - check if a io_uring polling thread can be created
6983 * Check to see if the current task is allowed to create a new io_uring
6984 * kernel polling thread.
6986 static int selinux_uring_sqpoll(void)
6988 int sid = current_sid();
6990 return avc_has_perm(&selinux_state, sid, sid,
6991 SECCLASS_IO_URING, IO_URING__SQPOLL, NULL);
6993 #endif /* CONFIG_IO_URING */
6996 * IMPORTANT NOTE: When adding new hooks, please be careful to keep this order:
6997 * 1. any hooks that don't belong to (2.) or (3.) below,
6998 * 2. hooks that both access structures allocated by other hooks, and allocate
6999 * structures that can be later accessed by other hooks (mostly "cloning"
7001 * 3. hooks that only allocate structures that can be later accessed by other
7002 * hooks ("allocating" hooks).
7004 * Please follow block comment delimiters in the list to keep this order.
7006 * This ordering is needed for SELinux runtime disable to work at least somewhat
7007 * safely. Breaking the ordering rules above might lead to NULL pointer derefs
7008 * when disabling SELinux at runtime.
7010 static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
7011 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
7012 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
7013 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
7014 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
7016 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
7017 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
7018 LSM_HOOK_INIT(capget, selinux_capget),
7019 LSM_HOOK_INIT(capset, selinux_capset),
7020 LSM_HOOK_INIT(capable, selinux_capable),
7021 LSM_HOOK_INIT(quotactl, selinux_quotactl),
7022 LSM_HOOK_INIT(quota_on, selinux_quota_on),
7023 LSM_HOOK_INIT(syslog, selinux_syslog),
7024 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
7026 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
7028 LSM_HOOK_INIT(bprm_creds_for_exec, selinux_bprm_creds_for_exec),
7029 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
7030 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
7032 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
7033 LSM_HOOK_INIT(sb_mnt_opts_compat, selinux_sb_mnt_opts_compat),
7034 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
7035 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
7036 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
7037 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
7038 LSM_HOOK_INIT(sb_mount, selinux_mount),
7039 LSM_HOOK_INIT(sb_umount, selinux_umount),
7040 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
7041 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
7043 LSM_HOOK_INIT(move_mount, selinux_move_mount),
7045 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
7046 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
7048 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
7049 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
7050 LSM_HOOK_INIT(inode_init_security_anon, selinux_inode_init_security_anon),
7051 LSM_HOOK_INIT(inode_create, selinux_inode_create),
7052 LSM_HOOK_INIT(inode_link, selinux_inode_link),
7053 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
7054 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
7055 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
7056 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
7057 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
7058 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
7059 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
7060 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
7061 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
7062 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
7063 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
7064 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
7065 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
7066 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
7067 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
7068 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
7069 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
7070 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
7071 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
7072 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
7073 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
7074 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
7075 LSM_HOOK_INIT(path_notify, selinux_path_notify),
7077 LSM_HOOK_INIT(kernfs_init_security, selinux_kernfs_init_security),
7079 LSM_HOOK_INIT(file_permission, selinux_file_permission),
7080 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
7081 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
7082 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
7083 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
7084 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
7085 LSM_HOOK_INIT(file_lock, selinux_file_lock),
7086 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
7087 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
7088 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
7089 LSM_HOOK_INIT(file_receive, selinux_file_receive),
7091 LSM_HOOK_INIT(file_open, selinux_file_open),
7093 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
7094 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
7095 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
7096 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
7097 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
7098 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
7099 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
7100 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
7101 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
7102 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
7103 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
7104 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
7105 LSM_HOOK_INIT(current_getsecid_subj, selinux_current_getsecid_subj),
7106 LSM_HOOK_INIT(task_getsecid_obj, selinux_task_getsecid_obj),
7107 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
7108 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
7109 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
7110 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
7111 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
7112 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
7113 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
7114 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
7115 LSM_HOOK_INIT(task_kill, selinux_task_kill),
7116 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
7118 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
7119 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
7121 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
7122 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
7123 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
7124 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
7126 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
7127 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
7128 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
7130 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
7131 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
7132 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
7134 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
7136 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
7137 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
7139 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
7140 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
7141 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
7142 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
7143 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
7144 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
7146 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
7147 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
7149 LSM_HOOK_INIT(socket_create, selinux_socket_create),
7150 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
7151 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
7152 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
7153 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
7154 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
7155 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
7156 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
7157 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
7158 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
7159 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
7160 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
7161 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
7162 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
7163 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
7164 LSM_HOOK_INIT(socket_getpeersec_stream,
7165 selinux_socket_getpeersec_stream),
7166 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
7167 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
7168 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
7169 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
7170 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
7171 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
7172 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
7173 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
7174 LSM_HOOK_INIT(sctp_assoc_established, selinux_sctp_assoc_established),
7175 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
7176 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
7177 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
7178 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
7179 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
7180 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
7181 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
7182 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
7183 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
7184 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
7185 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
7186 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
7187 #ifdef CONFIG_SECURITY_INFINIBAND
7188 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
7189 LSM_HOOK_INIT(ib_endport_manage_subnet,
7190 selinux_ib_endport_manage_subnet),
7191 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
7193 #ifdef CONFIG_SECURITY_NETWORK_XFRM
7194 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
7195 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
7196 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
7197 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
7198 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
7199 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
7200 selinux_xfrm_state_pol_flow_match),
7201 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
7205 LSM_HOOK_INIT(key_free, selinux_key_free),
7206 LSM_HOOK_INIT(key_permission, selinux_key_permission),
7207 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
7208 #ifdef CONFIG_KEY_NOTIFICATIONS
7209 LSM_HOOK_INIT(watch_key, selinux_watch_key),
7214 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
7215 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
7216 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
7219 #ifdef CONFIG_BPF_SYSCALL
7220 LSM_HOOK_INIT(bpf, selinux_bpf),
7221 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
7222 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
7223 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
7224 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
7227 #ifdef CONFIG_PERF_EVENTS
7228 LSM_HOOK_INIT(perf_event_open, selinux_perf_event_open),
7229 LSM_HOOK_INIT(perf_event_free, selinux_perf_event_free),
7230 LSM_HOOK_INIT(perf_event_read, selinux_perf_event_read),
7231 LSM_HOOK_INIT(perf_event_write, selinux_perf_event_write),
7234 #ifdef CONFIG_IO_URING
7235 LSM_HOOK_INIT(uring_override_creds, selinux_uring_override_creds),
7236 LSM_HOOK_INIT(uring_sqpoll, selinux_uring_sqpoll),
7240 * PUT "CLONING" (ACCESSING + ALLOCATING) HOOKS HERE
7242 LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup),
7243 LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),
7244 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
7245 #ifdef CONFIG_SECURITY_NETWORK_XFRM
7246 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
7250 * PUT "ALLOCATING" HOOKS HERE
7252 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
7253 LSM_HOOK_INIT(msg_queue_alloc_security,
7254 selinux_msg_queue_alloc_security),
7255 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
7256 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
7257 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
7258 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
7259 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
7260 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
7261 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
7262 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
7263 #ifdef CONFIG_SECURITY_INFINIBAND
7264 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
7266 #ifdef CONFIG_SECURITY_NETWORK_XFRM
7267 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
7268 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
7269 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
7270 selinux_xfrm_state_alloc_acquire),
7273 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
7276 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
7278 #ifdef CONFIG_BPF_SYSCALL
7279 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
7280 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
7282 #ifdef CONFIG_PERF_EVENTS
7283 LSM_HOOK_INIT(perf_event_alloc, selinux_perf_event_alloc),
7287 static __init int selinux_init(void)
7289 pr_info("SELinux: Initializing.\n");
7291 memset(&selinux_state, 0, sizeof(selinux_state));
7292 enforcing_set(&selinux_state, selinux_enforcing_boot);
7293 if (CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE)
7294 pr_err("SELinux: CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE is non-zero. This is deprecated and will be rejected in a future kernel release.\n");
7295 checkreqprot_set(&selinux_state, selinux_checkreqprot_boot);
7296 selinux_avc_init(&selinux_state.avc);
7297 mutex_init(&selinux_state.status_lock);
7298 mutex_init(&selinux_state.policy_mutex);
7300 /* Set the security state for the initial task. */
7301 cred_init_security();
7303 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
7309 ebitmap_cache_init();
7311 hashtab_cache_init();
7313 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
7315 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7316 panic("SELinux: Unable to register AVC netcache callback\n");
7318 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
7319 panic("SELinux: Unable to register AVC LSM notifier callback\n");
7321 if (selinux_enforcing_boot)
7322 pr_debug("SELinux: Starting in enforcing mode\n");
7324 pr_debug("SELinux: Starting in permissive mode\n");
7326 fs_validate_description("selinux", selinux_fs_parameters);
7331 static void delayed_superblock_init(struct super_block *sb, void *unused)
7333 selinux_set_mnt_opts(sb, NULL, 0, NULL);
7336 void selinux_complete_init(void)
7338 pr_debug("SELinux: Completing initialization.\n");
7340 /* Set up any superblocks initialized prior to the policy load. */
7341 pr_debug("SELinux: Setting up existing superblocks.\n");
7342 iterate_supers(delayed_superblock_init, NULL);
7345 /* SELinux requires early initialization in order to label
7346 all processes and objects when they are created. */
7347 DEFINE_LSM(selinux) = {
7349 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
7350 .enabled = &selinux_enabled_boot,
7351 .blobs = &selinux_blob_sizes,
7352 .init = selinux_init,
7355 #if defined(CONFIG_NETFILTER)
7357 static const struct nf_hook_ops selinux_nf_ops[] = {
7359 .hook = selinux_ip_postroute,
7361 .hooknum = NF_INET_POST_ROUTING,
7362 .priority = NF_IP_PRI_SELINUX_LAST,
7365 .hook = selinux_ip_forward,
7367 .hooknum = NF_INET_FORWARD,
7368 .priority = NF_IP_PRI_SELINUX_FIRST,
7371 .hook = selinux_ip_output,
7373 .hooknum = NF_INET_LOCAL_OUT,
7374 .priority = NF_IP_PRI_SELINUX_FIRST,
7376 #if IS_ENABLED(CONFIG_IPV6)
7378 .hook = selinux_ip_postroute,
7380 .hooknum = NF_INET_POST_ROUTING,
7381 .priority = NF_IP6_PRI_SELINUX_LAST,
7384 .hook = selinux_ip_forward,
7386 .hooknum = NF_INET_FORWARD,
7387 .priority = NF_IP6_PRI_SELINUX_FIRST,
7390 .hook = selinux_ip_output,
7392 .hooknum = NF_INET_LOCAL_OUT,
7393 .priority = NF_IP6_PRI_SELINUX_FIRST,
7398 static int __net_init selinux_nf_register(struct net *net)
7400 return nf_register_net_hooks(net, selinux_nf_ops,
7401 ARRAY_SIZE(selinux_nf_ops));
7404 static void __net_exit selinux_nf_unregister(struct net *net)
7406 nf_unregister_net_hooks(net, selinux_nf_ops,
7407 ARRAY_SIZE(selinux_nf_ops));
7410 static struct pernet_operations selinux_net_ops = {
7411 .init = selinux_nf_register,
7412 .exit = selinux_nf_unregister,
7415 static int __init selinux_nf_ip_init(void)
7419 if (!selinux_enabled_boot)
7422 pr_debug("SELinux: Registering netfilter hooks\n");
7424 err = register_pernet_subsys(&selinux_net_ops);
7426 panic("SELinux: register_pernet_subsys: error %d\n", err);
7430 __initcall(selinux_nf_ip_init);
7432 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7433 static void selinux_nf_ip_exit(void)
7435 pr_debug("SELinux: Unregistering netfilter hooks\n");
7437 unregister_pernet_subsys(&selinux_net_ops);
7441 #else /* CONFIG_NETFILTER */
7443 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7444 #define selinux_nf_ip_exit()
7447 #endif /* CONFIG_NETFILTER */
7449 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7450 int selinux_disable(struct selinux_state *state)
7452 if (selinux_initialized(state)) {
7453 /* Not permitted after initial policy load. */
7457 if (selinux_disabled(state)) {
7458 /* Only do this once. */
7462 selinux_mark_disabled(state);
7464 pr_info("SELinux: Disabled at runtime.\n");
7467 * Unregister netfilter hooks.
7468 * Must be done before security_delete_hooks() to avoid breaking
7471 selinux_nf_ip_exit();
7473 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
7475 /* Try to destroy the avc node cache */
7478 /* Unregister selinuxfs. */