2 * NSA Security-Enhanced Linux (SELinux) security module
4 * This file contains the SELinux hook function implementations.
11 * Copyright (C) 2001,2002 Networks Associates Technology, Inc.
14 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
16 * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
18 * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
20 * Copyright (C) 2016 Mellanox Technologies
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License version 2,
24 * as published by the Free Software Foundation.
27 #include <linux/init.h>
29 #include <linux/kernel.h>
30 #include <linux/tracehook.h>
31 #include <linux/errno.h>
32 #include <linux/sched/signal.h>
33 #include <linux/sched/task.h>
34 #include <linux/lsm_hooks.h>
35 #include <linux/xattr.h>
36 #include <linux/capability.h>
37 #include <linux/unistd.h>
39 #include <linux/mman.h>
40 #include <linux/slab.h>
41 #include <linux/pagemap.h>
42 #include <linux/proc_fs.h>
43 #include <linux/swap.h>
44 #include <linux/spinlock.h>
45 #include <linux/syscalls.h>
46 #include <linux/dcache.h>
47 #include <linux/file.h>
48 #include <linux/fdtable.h>
49 #include <linux/namei.h>
50 #include <linux/mount.h>
51 #include <linux/fs_context.h>
52 #include <linux/fs_parser.h>
53 #include <linux/netfilter_ipv4.h>
54 #include <linux/netfilter_ipv6.h>
55 #include <linux/tty.h>
57 #include <net/ip.h> /* for local_port_range[] */
58 #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
59 #include <net/inet_connection_sock.h>
60 #include <net/net_namespace.h>
61 #include <net/netlabel.h>
62 #include <linux/uaccess.h>
63 #include <asm/ioctls.h>
64 #include <linux/atomic.h>
65 #include <linux/bitops.h>
66 #include <linux/interrupt.h>
67 #include <linux/netdevice.h> /* for network interface checks */
68 #include <net/netlink.h>
69 #include <linux/tcp.h>
70 #include <linux/udp.h>
71 #include <linux/dccp.h>
72 #include <linux/sctp.h>
73 #include <net/sctp/structs.h>
74 #include <linux/quota.h>
75 #include <linux/un.h> /* for Unix socket types */
76 #include <net/af_unix.h> /* for Unix socket types */
77 #include <linux/parser.h>
78 #include <linux/nfs_mount.h>
80 #include <linux/hugetlb.h>
81 #include <linux/personality.h>
82 #include <linux/audit.h>
83 #include <linux/string.h>
84 #include <linux/mutex.h>
85 #include <linux/posix-timers.h>
86 #include <linux/syslog.h>
87 #include <linux/user_namespace.h>
88 #include <linux/export.h>
89 #include <linux/msg.h>
90 #include <linux/shm.h>
91 #include <linux/bpf.h>
92 #include <linux/kernfs.h>
93 #include <linux/stringhash.h> /* for hashlen_string() */
94 #include <uapi/linux/mount.h>
103 #include "netlabel.h"
107 struct selinux_state selinux_state;
109 /* SECMARK reference count */
110 static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
112 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
113 static int selinux_enforcing_boot;
115 static int __init enforcing_setup(char *str)
117 unsigned long enforcing;
118 if (!kstrtoul(str, 0, &enforcing))
119 selinux_enforcing_boot = enforcing ? 1 : 0;
122 __setup("enforcing=", enforcing_setup);
124 #define selinux_enforcing_boot 1
127 int selinux_enabled __lsm_ro_after_init = 1;
128 #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
129 static int __init selinux_enabled_setup(char *str)
131 unsigned long enabled;
132 if (!kstrtoul(str, 0, &enabled))
133 selinux_enabled = enabled ? 1 : 0;
136 __setup("selinux=", selinux_enabled_setup);
139 static unsigned int selinux_checkreqprot_boot =
140 CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
142 static int __init checkreqprot_setup(char *str)
144 unsigned long checkreqprot;
146 if (!kstrtoul(str, 0, &checkreqprot))
147 selinux_checkreqprot_boot = checkreqprot ? 1 : 0;
150 __setup("checkreqprot=", checkreqprot_setup);
153 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
156 * This function checks the SECMARK reference counter to see if any SECMARK
157 * targets are currently configured, if the reference counter is greater than
158 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
159 * enabled, false (0) if SECMARK is disabled. If the always_check_network
160 * policy capability is enabled, SECMARK is always considered enabled.
163 static int selinux_secmark_enabled(void)
165 return (selinux_policycap_alwaysnetwork() ||
166 atomic_read(&selinux_secmark_refcount));
170 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
173 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
174 * (1) if any are enabled or false (0) if neither are enabled. If the
175 * always_check_network policy capability is enabled, peer labeling
176 * is always considered enabled.
179 static int selinux_peerlbl_enabled(void)
181 return (selinux_policycap_alwaysnetwork() ||
182 netlbl_enabled() || selinux_xfrm_enabled());
185 static int selinux_netcache_avc_callback(u32 event)
187 if (event == AVC_CALLBACK_RESET) {
196 static int selinux_lsm_notifier_avc_callback(u32 event)
198 if (event == AVC_CALLBACK_RESET) {
200 call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
207 * initialise the security for the init task
209 static void cred_init_security(void)
211 struct cred *cred = (struct cred *) current->real_cred;
212 struct task_security_struct *tsec;
214 tsec = selinux_cred(cred);
215 tsec->osid = tsec->sid = SECINITSID_KERNEL;
219 * get the security ID of a set of credentials
221 static inline u32 cred_sid(const struct cred *cred)
223 const struct task_security_struct *tsec;
225 tsec = selinux_cred(cred);
230 * get the objective security ID of a task
232 static inline u32 task_sid(const struct task_struct *task)
237 sid = cred_sid(__task_cred(task));
242 /* Allocate and free functions for each kind of security blob. */
244 static int inode_alloc_security(struct inode *inode)
246 struct inode_security_struct *isec = selinux_inode(inode);
247 u32 sid = current_sid();
249 spin_lock_init(&isec->lock);
250 INIT_LIST_HEAD(&isec->list);
252 isec->sid = SECINITSID_UNLABELED;
253 isec->sclass = SECCLASS_FILE;
254 isec->task_sid = sid;
255 isec->initialized = LABEL_INVALID;
260 static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
263 * Try reloading inode security labels that have been marked as invalid. The
264 * @may_sleep parameter indicates when sleeping and thus reloading labels is
265 * allowed; when set to false, returns -ECHILD when the label is
266 * invalid. The @dentry parameter should be set to a dentry of the inode.
268 static int __inode_security_revalidate(struct inode *inode,
269 struct dentry *dentry,
272 struct inode_security_struct *isec = selinux_inode(inode);
274 might_sleep_if(may_sleep);
276 if (selinux_state.initialized &&
277 isec->initialized != LABEL_INITIALIZED) {
282 * Try reloading the inode security label. This will fail if
283 * @opt_dentry is NULL and no dentry for this inode can be
284 * found; in that case, continue using the old label.
286 inode_doinit_with_dentry(inode, dentry);
291 static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
293 return selinux_inode(inode);
296 static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
300 error = __inode_security_revalidate(inode, NULL, !rcu);
302 return ERR_PTR(error);
303 return selinux_inode(inode);
307 * Get the security label of an inode.
309 static struct inode_security_struct *inode_security(struct inode *inode)
311 __inode_security_revalidate(inode, NULL, true);
312 return selinux_inode(inode);
315 static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
317 struct inode *inode = d_backing_inode(dentry);
319 return selinux_inode(inode);
323 * Get the security label of a dentry's backing inode.
325 static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
327 struct inode *inode = d_backing_inode(dentry);
329 __inode_security_revalidate(inode, dentry, true);
330 return selinux_inode(inode);
333 static void inode_free_security(struct inode *inode)
335 struct inode_security_struct *isec = selinux_inode(inode);
336 struct superblock_security_struct *sbsec;
340 sbsec = inode->i_sb->s_security;
342 * As not all inode security structures are in a list, we check for
343 * empty list outside of the lock to make sure that we won't waste
344 * time taking a lock doing nothing.
346 * The list_del_init() function can be safely called more than once.
347 * It should not be possible for this function to be called with
348 * concurrent list_add(), but for better safety against future changes
349 * in the code, we use list_empty_careful() here.
351 if (!list_empty_careful(&isec->list)) {
352 spin_lock(&sbsec->isec_lock);
353 list_del_init(&isec->list);
354 spin_unlock(&sbsec->isec_lock);
358 static int file_alloc_security(struct file *file)
360 struct file_security_struct *fsec = selinux_file(file);
361 u32 sid = current_sid();
364 fsec->fown_sid = sid;
369 static int superblock_alloc_security(struct super_block *sb)
371 struct superblock_security_struct *sbsec;
373 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
377 mutex_init(&sbsec->lock);
378 INIT_LIST_HEAD(&sbsec->isec_head);
379 spin_lock_init(&sbsec->isec_lock);
381 sbsec->sid = SECINITSID_UNLABELED;
382 sbsec->def_sid = SECINITSID_FILE;
383 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
384 sb->s_security = sbsec;
389 static void superblock_free_security(struct super_block *sb)
391 struct superblock_security_struct *sbsec = sb->s_security;
392 sb->s_security = NULL;
396 struct selinux_mnt_opts {
397 const char *fscontext, *context, *rootcontext, *defcontext;
400 static void selinux_free_mnt_opts(void *mnt_opts)
402 struct selinux_mnt_opts *opts = mnt_opts;
403 kfree(opts->fscontext);
404 kfree(opts->context);
405 kfree(opts->rootcontext);
406 kfree(opts->defcontext);
410 static inline int inode_doinit(struct inode *inode)
412 return inode_doinit_with_dentry(inode, NULL);
424 #define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
434 A(rootcontext, true),
439 static int match_opt_prefix(char *s, int l, char **arg)
443 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
444 size_t len = tokens[i].len;
445 if (len > l || memcmp(s, tokens[i].name, len))
447 if (tokens[i].has_arg) {
448 if (len == l || s[len] != '=')
453 return tokens[i].opt;
458 #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
460 static int may_context_mount_sb_relabel(u32 sid,
461 struct superblock_security_struct *sbsec,
462 const struct cred *cred)
464 const struct task_security_struct *tsec = selinux_cred(cred);
467 rc = avc_has_perm(&selinux_state,
468 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
469 FILESYSTEM__RELABELFROM, NULL);
473 rc = avc_has_perm(&selinux_state,
474 tsec->sid, sid, SECCLASS_FILESYSTEM,
475 FILESYSTEM__RELABELTO, NULL);
479 static int may_context_mount_inode_relabel(u32 sid,
480 struct superblock_security_struct *sbsec,
481 const struct cred *cred)
483 const struct task_security_struct *tsec = selinux_cred(cred);
485 rc = avc_has_perm(&selinux_state,
486 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
487 FILESYSTEM__RELABELFROM, NULL);
491 rc = avc_has_perm(&selinux_state,
492 sid, sbsec->sid, SECCLASS_FILESYSTEM,
493 FILESYSTEM__ASSOCIATE, NULL);
497 static int selinux_is_genfs_special_handling(struct super_block *sb)
499 /* Special handling. Genfs but also in-core setxattr handler */
500 return !strcmp(sb->s_type->name, "sysfs") ||
501 !strcmp(sb->s_type->name, "pstore") ||
502 !strcmp(sb->s_type->name, "debugfs") ||
503 !strcmp(sb->s_type->name, "tracefs") ||
504 !strcmp(sb->s_type->name, "rootfs") ||
505 (selinux_policycap_cgroupseclabel() &&
506 (!strcmp(sb->s_type->name, "cgroup") ||
507 !strcmp(sb->s_type->name, "cgroup2")));
510 static int selinux_is_sblabel_mnt(struct super_block *sb)
512 struct superblock_security_struct *sbsec = sb->s_security;
515 * IMPORTANT: Double-check logic in this function when adding a new
516 * SECURITY_FS_USE_* definition!
518 BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7);
520 switch (sbsec->behavior) {
521 case SECURITY_FS_USE_XATTR:
522 case SECURITY_FS_USE_TRANS:
523 case SECURITY_FS_USE_TASK:
524 case SECURITY_FS_USE_NATIVE:
527 case SECURITY_FS_USE_GENFS:
528 return selinux_is_genfs_special_handling(sb);
530 /* Never allow relabeling on context mounts */
531 case SECURITY_FS_USE_MNTPOINT:
532 case SECURITY_FS_USE_NONE:
538 static int sb_finish_set_opts(struct super_block *sb)
540 struct superblock_security_struct *sbsec = sb->s_security;
541 struct dentry *root = sb->s_root;
542 struct inode *root_inode = d_backing_inode(root);
545 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
546 /* Make sure that the xattr handler exists and that no
547 error other than -ENODATA is returned by getxattr on
548 the root directory. -ENODATA is ok, as this may be
549 the first boot of the SELinux kernel before we have
550 assigned xattr values to the filesystem. */
551 if (!(root_inode->i_opflags & IOP_XATTR)) {
552 pr_warn("SELinux: (dev %s, type %s) has no "
553 "xattr support\n", sb->s_id, sb->s_type->name);
558 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
559 if (rc < 0 && rc != -ENODATA) {
560 if (rc == -EOPNOTSUPP)
561 pr_warn("SELinux: (dev %s, type "
562 "%s) has no security xattr handler\n",
563 sb->s_id, sb->s_type->name);
565 pr_warn("SELinux: (dev %s, type "
566 "%s) getxattr errno %d\n", sb->s_id,
567 sb->s_type->name, -rc);
572 sbsec->flags |= SE_SBINITIALIZED;
575 * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply
576 * leave the flag untouched because sb_clone_mnt_opts might be handing
577 * us a superblock that needs the flag to be cleared.
579 if (selinux_is_sblabel_mnt(sb))
580 sbsec->flags |= SBLABEL_MNT;
582 sbsec->flags &= ~SBLABEL_MNT;
584 /* Initialize the root inode. */
585 rc = inode_doinit_with_dentry(root_inode, root);
587 /* Initialize any other inodes associated with the superblock, e.g.
588 inodes created prior to initial policy load or inodes created
589 during get_sb by a pseudo filesystem that directly
591 spin_lock(&sbsec->isec_lock);
592 while (!list_empty(&sbsec->isec_head)) {
593 struct inode_security_struct *isec =
594 list_first_entry(&sbsec->isec_head,
595 struct inode_security_struct, list);
596 struct inode *inode = isec->inode;
597 list_del_init(&isec->list);
598 spin_unlock(&sbsec->isec_lock);
599 inode = igrab(inode);
601 if (!IS_PRIVATE(inode))
605 spin_lock(&sbsec->isec_lock);
607 spin_unlock(&sbsec->isec_lock);
612 static int bad_option(struct superblock_security_struct *sbsec, char flag,
613 u32 old_sid, u32 new_sid)
615 char mnt_flags = sbsec->flags & SE_MNTMASK;
617 /* check if the old mount command had the same options */
618 if (sbsec->flags & SE_SBINITIALIZED)
619 if (!(sbsec->flags & flag) ||
620 (old_sid != new_sid))
623 /* check if we were passed the same options twice,
624 * aka someone passed context=a,context=b
626 if (!(sbsec->flags & SE_SBINITIALIZED))
627 if (mnt_flags & flag)
632 static int parse_sid(struct super_block *sb, const char *s, u32 *sid)
634 int rc = security_context_str_to_sid(&selinux_state, s,
637 pr_warn("SELinux: security_context_str_to_sid"
638 "(%s) failed for (dev %s, type %s) errno=%d\n",
639 s, sb->s_id, sb->s_type->name, rc);
644 * Allow filesystems with binary mount data to explicitly set mount point
645 * labeling information.
647 static int selinux_set_mnt_opts(struct super_block *sb,
649 unsigned long kern_flags,
650 unsigned long *set_kern_flags)
652 const struct cred *cred = current_cred();
653 struct superblock_security_struct *sbsec = sb->s_security;
654 struct dentry *root = sbsec->sb->s_root;
655 struct selinux_mnt_opts *opts = mnt_opts;
656 struct inode_security_struct *root_isec;
657 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
658 u32 defcontext_sid = 0;
661 mutex_lock(&sbsec->lock);
663 if (!selinux_state.initialized) {
665 /* Defer initialization until selinux_complete_init,
666 after the initial policy is loaded and the security
667 server is ready to handle calls. */
671 pr_warn("SELinux: Unable to set superblock options "
672 "before the security server is initialized\n");
675 if (kern_flags && !set_kern_flags) {
676 /* Specifying internal flags without providing a place to
677 * place the results is not allowed */
683 * Binary mount data FS will come through this function twice. Once
684 * from an explicit call and once from the generic calls from the vfs.
685 * Since the generic VFS calls will not contain any security mount data
686 * we need to skip the double mount verification.
688 * This does open a hole in which we will not notice if the first
689 * mount using this sb set explict options and a second mount using
690 * this sb does not set any security options. (The first options
691 * will be used for both mounts)
693 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
697 root_isec = backing_inode_security_novalidate(root);
700 * parse the mount options, check if they are valid sids.
701 * also check if someone is trying to mount the same sb more
702 * than once with different security options.
705 if (opts->fscontext) {
706 rc = parse_sid(sb, opts->fscontext, &fscontext_sid);
709 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
711 goto out_double_mount;
712 sbsec->flags |= FSCONTEXT_MNT;
715 rc = parse_sid(sb, opts->context, &context_sid);
718 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
720 goto out_double_mount;
721 sbsec->flags |= CONTEXT_MNT;
723 if (opts->rootcontext) {
724 rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid);
727 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
729 goto out_double_mount;
730 sbsec->flags |= ROOTCONTEXT_MNT;
732 if (opts->defcontext) {
733 rc = parse_sid(sb, opts->defcontext, &defcontext_sid);
736 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
738 goto out_double_mount;
739 sbsec->flags |= DEFCONTEXT_MNT;
743 if (sbsec->flags & SE_SBINITIALIZED) {
744 /* previously mounted with options, but not on this attempt? */
745 if ((sbsec->flags & SE_MNTMASK) && !opts)
746 goto out_double_mount;
751 if (strcmp(sb->s_type->name, "proc") == 0)
752 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
754 if (!strcmp(sb->s_type->name, "debugfs") ||
755 !strcmp(sb->s_type->name, "tracefs") ||
756 !strcmp(sb->s_type->name, "pstore"))
757 sbsec->flags |= SE_SBGENFS;
759 if (!strcmp(sb->s_type->name, "sysfs") ||
760 !strcmp(sb->s_type->name, "cgroup") ||
761 !strcmp(sb->s_type->name, "cgroup2"))
762 sbsec->flags |= SE_SBGENFS | SE_SBGENFS_XATTR;
764 if (!sbsec->behavior) {
766 * Determine the labeling behavior to use for this
769 rc = security_fs_use(&selinux_state, sb);
771 pr_warn("%s: security_fs_use(%s) returned %d\n",
772 __func__, sb->s_type->name, rc);
778 * If this is a user namespace mount and the filesystem type is not
779 * explicitly whitelisted, then no contexts are allowed on the command
780 * line and security labels must be ignored.
782 if (sb->s_user_ns != &init_user_ns &&
783 strcmp(sb->s_type->name, "tmpfs") &&
784 strcmp(sb->s_type->name, "ramfs") &&
785 strcmp(sb->s_type->name, "devpts")) {
786 if (context_sid || fscontext_sid || rootcontext_sid ||
791 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
792 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
793 rc = security_transition_sid(&selinux_state,
797 &sbsec->mntpoint_sid);
804 /* sets the context of the superblock for the fs being mounted. */
806 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
810 sbsec->sid = fscontext_sid;
814 * Switch to using mount point labeling behavior.
815 * sets the label used on all file below the mountpoint, and will set
816 * the superblock context if not already set.
818 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
819 sbsec->behavior = SECURITY_FS_USE_NATIVE;
820 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
824 if (!fscontext_sid) {
825 rc = may_context_mount_sb_relabel(context_sid, sbsec,
829 sbsec->sid = context_sid;
831 rc = may_context_mount_inode_relabel(context_sid, sbsec,
836 if (!rootcontext_sid)
837 rootcontext_sid = context_sid;
839 sbsec->mntpoint_sid = context_sid;
840 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
843 if (rootcontext_sid) {
844 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
849 root_isec->sid = rootcontext_sid;
850 root_isec->initialized = LABEL_INITIALIZED;
853 if (defcontext_sid) {
854 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
855 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
857 pr_warn("SELinux: defcontext option is "
858 "invalid for this filesystem type\n");
862 if (defcontext_sid != sbsec->def_sid) {
863 rc = may_context_mount_inode_relabel(defcontext_sid,
869 sbsec->def_sid = defcontext_sid;
873 rc = sb_finish_set_opts(sb);
875 mutex_unlock(&sbsec->lock);
879 pr_warn("SELinux: mount invalid. Same superblock, different "
880 "security settings for (dev %s, type %s)\n", sb->s_id,
885 static int selinux_cmp_sb_context(const struct super_block *oldsb,
886 const struct super_block *newsb)
888 struct superblock_security_struct *old = oldsb->s_security;
889 struct superblock_security_struct *new = newsb->s_security;
890 char oldflags = old->flags & SE_MNTMASK;
891 char newflags = new->flags & SE_MNTMASK;
893 if (oldflags != newflags)
895 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
897 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
899 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
901 if (oldflags & ROOTCONTEXT_MNT) {
902 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
903 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
904 if (oldroot->sid != newroot->sid)
909 pr_warn("SELinux: mount invalid. Same superblock, "
910 "different security settings for (dev %s, "
911 "type %s)\n", newsb->s_id, newsb->s_type->name);
915 static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
916 struct super_block *newsb,
917 unsigned long kern_flags,
918 unsigned long *set_kern_flags)
921 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
922 struct superblock_security_struct *newsbsec = newsb->s_security;
924 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
925 int set_context = (oldsbsec->flags & CONTEXT_MNT);
926 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
929 * if the parent was able to be mounted it clearly had no special lsm
930 * mount options. thus we can safely deal with this superblock later
932 if (!selinux_state.initialized)
936 * Specifying internal flags without providing a place to
937 * place the results is not allowed.
939 if (kern_flags && !set_kern_flags)
942 /* how can we clone if the old one wasn't set up?? */
943 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
945 /* if fs is reusing a sb, make sure that the contexts match */
946 if (newsbsec->flags & SE_SBINITIALIZED) {
947 if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context)
948 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
949 return selinux_cmp_sb_context(oldsb, newsb);
952 mutex_lock(&newsbsec->lock);
954 newsbsec->flags = oldsbsec->flags;
956 newsbsec->sid = oldsbsec->sid;
957 newsbsec->def_sid = oldsbsec->def_sid;
958 newsbsec->behavior = oldsbsec->behavior;
960 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
961 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
962 rc = security_fs_use(&selinux_state, newsb);
967 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
968 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
969 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
973 u32 sid = oldsbsec->mntpoint_sid;
977 if (!set_rootcontext) {
978 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
981 newsbsec->mntpoint_sid = sid;
983 if (set_rootcontext) {
984 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
985 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
987 newisec->sid = oldisec->sid;
990 sb_finish_set_opts(newsb);
992 mutex_unlock(&newsbsec->lock);
996 static int selinux_add_opt(int token, const char *s, void **mnt_opts)
998 struct selinux_mnt_opts *opts = *mnt_opts;
1000 if (token == Opt_seclabel) /* eaten and completely ignored */
1004 opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
1013 if (opts->context || opts->defcontext)
1018 if (opts->fscontext)
1020 opts->fscontext = s;
1022 case Opt_rootcontext:
1023 if (opts->rootcontext)
1025 opts->rootcontext = s;
1027 case Opt_defcontext:
1028 if (opts->context || opts->defcontext)
1030 opts->defcontext = s;
1035 pr_warn(SEL_MOUNT_FAIL_MSG);
1039 static int selinux_add_mnt_opt(const char *option, const char *val, int len,
1042 int token = Opt_error;
1045 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
1046 if (strcmp(option, tokens[i].name) == 0) {
1047 token = tokens[i].opt;
1052 if (token == Opt_error)
1055 if (token != Opt_seclabel) {
1056 val = kmemdup_nul(val, len, GFP_KERNEL);
1062 rc = selinux_add_opt(token, val, mnt_opts);
1071 selinux_free_mnt_opts(*mnt_opts);
1077 static int show_sid(struct seq_file *m, u32 sid)
1079 char *context = NULL;
1083 rc = security_sid_to_context(&selinux_state, sid,
1086 bool has_comma = context && strchr(context, ',');
1091 seq_escape(m, context, "\"\n\\");
1099 static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1101 struct superblock_security_struct *sbsec = sb->s_security;
1104 if (!(sbsec->flags & SE_SBINITIALIZED))
1107 if (!selinux_state.initialized)
1110 if (sbsec->flags & FSCONTEXT_MNT) {
1112 seq_puts(m, FSCONTEXT_STR);
1113 rc = show_sid(m, sbsec->sid);
1117 if (sbsec->flags & CONTEXT_MNT) {
1119 seq_puts(m, CONTEXT_STR);
1120 rc = show_sid(m, sbsec->mntpoint_sid);
1124 if (sbsec->flags & DEFCONTEXT_MNT) {
1126 seq_puts(m, DEFCONTEXT_STR);
1127 rc = show_sid(m, sbsec->def_sid);
1131 if (sbsec->flags & ROOTCONTEXT_MNT) {
1132 struct dentry *root = sbsec->sb->s_root;
1133 struct inode_security_struct *isec = backing_inode_security(root);
1135 seq_puts(m, ROOTCONTEXT_STR);
1136 rc = show_sid(m, isec->sid);
1140 if (sbsec->flags & SBLABEL_MNT) {
1142 seq_puts(m, SECLABEL_STR);
1147 static inline u16 inode_mode_to_security_class(umode_t mode)
1149 switch (mode & S_IFMT) {
1151 return SECCLASS_SOCK_FILE;
1153 return SECCLASS_LNK_FILE;
1155 return SECCLASS_FILE;
1157 return SECCLASS_BLK_FILE;
1159 return SECCLASS_DIR;
1161 return SECCLASS_CHR_FILE;
1163 return SECCLASS_FIFO_FILE;
1167 return SECCLASS_FILE;
1170 static inline int default_protocol_stream(int protocol)
1172 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1175 static inline int default_protocol_dgram(int protocol)
1177 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1180 static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1182 int extsockclass = selinux_policycap_extsockclass();
1188 case SOCK_SEQPACKET:
1189 return SECCLASS_UNIX_STREAM_SOCKET;
1192 return SECCLASS_UNIX_DGRAM_SOCKET;
1199 case SOCK_SEQPACKET:
1200 if (default_protocol_stream(protocol))
1201 return SECCLASS_TCP_SOCKET;
1202 else if (extsockclass && protocol == IPPROTO_SCTP)
1203 return SECCLASS_SCTP_SOCKET;
1205 return SECCLASS_RAWIP_SOCKET;
1207 if (default_protocol_dgram(protocol))
1208 return SECCLASS_UDP_SOCKET;
1209 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1210 protocol == IPPROTO_ICMPV6))
1211 return SECCLASS_ICMP_SOCKET;
1213 return SECCLASS_RAWIP_SOCKET;
1215 return SECCLASS_DCCP_SOCKET;
1217 return SECCLASS_RAWIP_SOCKET;
1223 return SECCLASS_NETLINK_ROUTE_SOCKET;
1224 case NETLINK_SOCK_DIAG:
1225 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1227 return SECCLASS_NETLINK_NFLOG_SOCKET;
1229 return SECCLASS_NETLINK_XFRM_SOCKET;
1230 case NETLINK_SELINUX:
1231 return SECCLASS_NETLINK_SELINUX_SOCKET;
1233 return SECCLASS_NETLINK_ISCSI_SOCKET;
1235 return SECCLASS_NETLINK_AUDIT_SOCKET;
1236 case NETLINK_FIB_LOOKUP:
1237 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1238 case NETLINK_CONNECTOR:
1239 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1240 case NETLINK_NETFILTER:
1241 return SECCLASS_NETLINK_NETFILTER_SOCKET;
1242 case NETLINK_DNRTMSG:
1243 return SECCLASS_NETLINK_DNRT_SOCKET;
1244 case NETLINK_KOBJECT_UEVENT:
1245 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
1246 case NETLINK_GENERIC:
1247 return SECCLASS_NETLINK_GENERIC_SOCKET;
1248 case NETLINK_SCSITRANSPORT:
1249 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1251 return SECCLASS_NETLINK_RDMA_SOCKET;
1252 case NETLINK_CRYPTO:
1253 return SECCLASS_NETLINK_CRYPTO_SOCKET;
1255 return SECCLASS_NETLINK_SOCKET;
1258 return SECCLASS_PACKET_SOCKET;
1260 return SECCLASS_KEY_SOCKET;
1262 return SECCLASS_APPLETALK_SOCKET;
1268 return SECCLASS_AX25_SOCKET;
1270 return SECCLASS_IPX_SOCKET;
1272 return SECCLASS_NETROM_SOCKET;
1274 return SECCLASS_ATMPVC_SOCKET;
1276 return SECCLASS_X25_SOCKET;
1278 return SECCLASS_ROSE_SOCKET;
1280 return SECCLASS_DECNET_SOCKET;
1282 return SECCLASS_ATMSVC_SOCKET;
1284 return SECCLASS_RDS_SOCKET;
1286 return SECCLASS_IRDA_SOCKET;
1288 return SECCLASS_PPPOX_SOCKET;
1290 return SECCLASS_LLC_SOCKET;
1292 return SECCLASS_CAN_SOCKET;
1294 return SECCLASS_TIPC_SOCKET;
1296 return SECCLASS_BLUETOOTH_SOCKET;
1298 return SECCLASS_IUCV_SOCKET;
1300 return SECCLASS_RXRPC_SOCKET;
1302 return SECCLASS_ISDN_SOCKET;
1304 return SECCLASS_PHONET_SOCKET;
1306 return SECCLASS_IEEE802154_SOCKET;
1308 return SECCLASS_CAIF_SOCKET;
1310 return SECCLASS_ALG_SOCKET;
1312 return SECCLASS_NFC_SOCKET;
1314 return SECCLASS_VSOCK_SOCKET;
1316 return SECCLASS_KCM_SOCKET;
1318 return SECCLASS_QIPCRTR_SOCKET;
1320 return SECCLASS_SMC_SOCKET;
1322 return SECCLASS_XDP_SOCKET;
1324 #error New address family defined, please update this function.
1329 return SECCLASS_SOCKET;
1332 static int selinux_genfs_get_sid(struct dentry *dentry,
1338 struct super_block *sb = dentry->d_sb;
1339 char *buffer, *path;
1341 buffer = (char *)__get_free_page(GFP_KERNEL);
1345 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1349 if (flags & SE_SBPROC) {
1350 /* each process gets a /proc/PID/ entry. Strip off the
1351 * PID part to get a valid selinux labeling.
1352 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1353 while (path[1] >= '0' && path[1] <= '9') {
1358 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1360 if (rc == -ENOENT) {
1361 /* No match in policy, mark as unlabeled. */
1362 *sid = SECINITSID_UNLABELED;
1366 free_page((unsigned long)buffer);
1370 static int inode_doinit_use_xattr(struct inode *inode, struct dentry *dentry,
1371 u32 def_sid, u32 *sid)
1373 #define INITCONTEXTLEN 255
1378 len = INITCONTEXTLEN;
1379 context = kmalloc(len + 1, GFP_NOFS);
1383 context[len] = '\0';
1384 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
1385 if (rc == -ERANGE) {
1388 /* Need a larger buffer. Query for the right size. */
1389 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
1394 context = kmalloc(len + 1, GFP_NOFS);
1398 context[len] = '\0';
1399 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX,
1404 if (rc != -ENODATA) {
1405 pr_warn("SELinux: %s: getxattr returned %d for dev=%s ino=%ld\n",
1406 __func__, -rc, inode->i_sb->s_id, inode->i_ino);
1413 rc = security_context_to_sid_default(&selinux_state, context, rc, sid,
1416 char *dev = inode->i_sb->s_id;
1417 unsigned long ino = inode->i_ino;
1419 if (rc == -EINVAL) {
1420 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",
1423 pr_warn("SELinux: %s: context_to_sid(%s) returned %d for dev=%s ino=%ld\n",
1424 __func__, context, -rc, dev, ino);
1431 /* The inode's security attributes must be initialized before first use. */
1432 static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1434 struct superblock_security_struct *sbsec = NULL;
1435 struct inode_security_struct *isec = selinux_inode(inode);
1436 u32 task_sid, sid = 0;
1438 struct dentry *dentry;
1441 if (isec->initialized == LABEL_INITIALIZED)
1444 spin_lock(&isec->lock);
1445 if (isec->initialized == LABEL_INITIALIZED)
1448 if (isec->sclass == SECCLASS_FILE)
1449 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1451 sbsec = inode->i_sb->s_security;
1452 if (!(sbsec->flags & SE_SBINITIALIZED)) {
1453 /* Defer initialization until selinux_complete_init,
1454 after the initial policy is loaded and the security
1455 server is ready to handle calls. */
1456 spin_lock(&sbsec->isec_lock);
1457 if (list_empty(&isec->list))
1458 list_add(&isec->list, &sbsec->isec_head);
1459 spin_unlock(&sbsec->isec_lock);
1463 sclass = isec->sclass;
1464 task_sid = isec->task_sid;
1466 isec->initialized = LABEL_PENDING;
1467 spin_unlock(&isec->lock);
1469 switch (sbsec->behavior) {
1470 case SECURITY_FS_USE_NATIVE:
1472 case SECURITY_FS_USE_XATTR:
1473 if (!(inode->i_opflags & IOP_XATTR)) {
1474 sid = sbsec->def_sid;
1477 /* Need a dentry, since the xattr API requires one.
1478 Life would be simpler if we could just pass the inode. */
1480 /* Called from d_instantiate or d_splice_alias. */
1481 dentry = dget(opt_dentry);
1484 * Called from selinux_complete_init, try to find a dentry.
1485 * Some filesystems really want a connected one, so try
1486 * that first. We could split SECURITY_FS_USE_XATTR in
1487 * two, depending upon that...
1489 dentry = d_find_alias(inode);
1491 dentry = d_find_any_alias(inode);
1495 * this is can be hit on boot when a file is accessed
1496 * before the policy is loaded. When we load policy we
1497 * may find inodes that have no dentry on the
1498 * sbsec->isec_head list. No reason to complain as these
1499 * will get fixed up the next time we go through
1500 * inode_doinit with a dentry, before these inodes could
1501 * be used again by userspace.
1506 rc = inode_doinit_use_xattr(inode, dentry, sbsec->def_sid,
1512 case SECURITY_FS_USE_TASK:
1515 case SECURITY_FS_USE_TRANS:
1516 /* Default to the fs SID. */
1519 /* Try to obtain a transition SID. */
1520 rc = security_transition_sid(&selinux_state, task_sid, sid,
1521 sclass, NULL, &sid);
1525 case SECURITY_FS_USE_MNTPOINT:
1526 sid = sbsec->mntpoint_sid;
1529 /* Default to the fs superblock SID. */
1532 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
1533 /* We must have a dentry to determine the label on
1536 /* Called from d_instantiate or
1537 * d_splice_alias. */
1538 dentry = dget(opt_dentry);
1540 /* Called from selinux_complete_init, try to
1541 * find a dentry. Some filesystems really want
1542 * a connected one, so try that first.
1544 dentry = d_find_alias(inode);
1546 dentry = d_find_any_alias(inode);
1549 * This can be hit on boot when a file is accessed
1550 * before the policy is loaded. When we load policy we
1551 * may find inodes that have no dentry on the
1552 * sbsec->isec_head list. No reason to complain as
1553 * these will get fixed up the next time we go through
1554 * inode_doinit() with a dentry, before these inodes
1555 * could be used again by userspace.
1559 rc = selinux_genfs_get_sid(dentry, sclass,
1560 sbsec->flags, &sid);
1566 if ((sbsec->flags & SE_SBGENFS_XATTR) &&
1567 (inode->i_opflags & IOP_XATTR)) {
1568 rc = inode_doinit_use_xattr(inode, dentry,
1581 spin_lock(&isec->lock);
1582 if (isec->initialized == LABEL_PENDING) {
1584 isec->initialized = LABEL_INVALID;
1588 isec->initialized = LABEL_INITIALIZED;
1593 spin_unlock(&isec->lock);
1597 /* Convert a Linux signal to an access vector. */
1598 static inline u32 signal_to_av(int sig)
1604 /* Commonly granted from child to parent. */
1605 perm = PROCESS__SIGCHLD;
1608 /* Cannot be caught or ignored */
1609 perm = PROCESS__SIGKILL;
1612 /* Cannot be caught or ignored */
1613 perm = PROCESS__SIGSTOP;
1616 /* All other signals. */
1617 perm = PROCESS__SIGNAL;
1624 #if CAP_LAST_CAP > 63
1625 #error Fix SELinux to handle capabilities > 63.
1628 /* Check whether a task is allowed to use a capability. */
1629 static int cred_has_capability(const struct cred *cred,
1630 int cap, unsigned int opts, bool initns)
1632 struct common_audit_data ad;
1633 struct av_decision avd;
1635 u32 sid = cred_sid(cred);
1636 u32 av = CAP_TO_MASK(cap);
1639 ad.type = LSM_AUDIT_DATA_CAP;
1642 switch (CAP_TO_INDEX(cap)) {
1644 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
1647 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
1650 pr_err("SELinux: out of range capability %d\n", cap);
1655 rc = avc_has_perm_noaudit(&selinux_state,
1656 sid, sid, sclass, av, 0, &avd);
1657 if (!(opts & CAP_OPT_NOAUDIT)) {
1658 int rc2 = avc_audit(&selinux_state,
1659 sid, sid, sclass, av, &avd, rc, &ad, 0);
1666 /* Check whether a task has a particular permission to an inode.
1667 The 'adp' parameter is optional and allows other audit
1668 data to be passed (e.g. the dentry). */
1669 static int inode_has_perm(const struct cred *cred,
1670 struct inode *inode,
1672 struct common_audit_data *adp)
1674 struct inode_security_struct *isec;
1677 validate_creds(cred);
1679 if (unlikely(IS_PRIVATE(inode)))
1682 sid = cred_sid(cred);
1683 isec = selinux_inode(inode);
1685 return avc_has_perm(&selinux_state,
1686 sid, isec->sid, isec->sclass, perms, adp);
1689 /* Same as inode_has_perm, but pass explicit audit data containing
1690 the dentry to help the auditing code to more easily generate the
1691 pathname if needed. */
1692 static inline int dentry_has_perm(const struct cred *cred,
1693 struct dentry *dentry,
1696 struct inode *inode = d_backing_inode(dentry);
1697 struct common_audit_data ad;
1699 ad.type = LSM_AUDIT_DATA_DENTRY;
1700 ad.u.dentry = dentry;
1701 __inode_security_revalidate(inode, dentry, true);
1702 return inode_has_perm(cred, inode, av, &ad);
1705 /* Same as inode_has_perm, but pass explicit audit data containing
1706 the path to help the auditing code to more easily generate the
1707 pathname if needed. */
1708 static inline int path_has_perm(const struct cred *cred,
1709 const struct path *path,
1712 struct inode *inode = d_backing_inode(path->dentry);
1713 struct common_audit_data ad;
1715 ad.type = LSM_AUDIT_DATA_PATH;
1717 __inode_security_revalidate(inode, path->dentry, true);
1718 return inode_has_perm(cred, inode, av, &ad);
1721 /* Same as path_has_perm, but uses the inode from the file struct. */
1722 static inline int file_path_has_perm(const struct cred *cred,
1726 struct common_audit_data ad;
1728 ad.type = LSM_AUDIT_DATA_FILE;
1730 return inode_has_perm(cred, file_inode(file), av, &ad);
1733 #ifdef CONFIG_BPF_SYSCALL
1734 static int bpf_fd_pass(struct file *file, u32 sid);
1737 /* Check whether a task can use an open file descriptor to
1738 access an inode in a given way. Check access to the
1739 descriptor itself, and then use dentry_has_perm to
1740 check a particular permission to the file.
1741 Access to the descriptor is implicitly granted if it
1742 has the same SID as the process. If av is zero, then
1743 access to the file is not checked, e.g. for cases
1744 where only the descriptor is affected like seek. */
1745 static int file_has_perm(const struct cred *cred,
1749 struct file_security_struct *fsec = selinux_file(file);
1750 struct inode *inode = file_inode(file);
1751 struct common_audit_data ad;
1752 u32 sid = cred_sid(cred);
1755 ad.type = LSM_AUDIT_DATA_FILE;
1758 if (sid != fsec->sid) {
1759 rc = avc_has_perm(&selinux_state,
1768 #ifdef CONFIG_BPF_SYSCALL
1769 rc = bpf_fd_pass(file, cred_sid(cred));
1774 /* av is zero if only checking access to the descriptor. */
1777 rc = inode_has_perm(cred, inode, av, &ad);
1784 * Determine the label for an inode that might be unioned.
1787 selinux_determine_inode_label(const struct task_security_struct *tsec,
1789 const struct qstr *name, u16 tclass,
1792 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
1794 if ((sbsec->flags & SE_SBINITIALIZED) &&
1795 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1796 *_new_isid = sbsec->mntpoint_sid;
1797 } else if ((sbsec->flags & SBLABEL_MNT) &&
1799 *_new_isid = tsec->create_sid;
1801 const struct inode_security_struct *dsec = inode_security(dir);
1802 return security_transition_sid(&selinux_state, tsec->sid,
1810 /* Check whether a task can create a file. */
1811 static int may_create(struct inode *dir,
1812 struct dentry *dentry,
1815 const struct task_security_struct *tsec = selinux_cred(current_cred());
1816 struct inode_security_struct *dsec;
1817 struct superblock_security_struct *sbsec;
1819 struct common_audit_data ad;
1822 dsec = inode_security(dir);
1823 sbsec = dir->i_sb->s_security;
1827 ad.type = LSM_AUDIT_DATA_DENTRY;
1828 ad.u.dentry = dentry;
1830 rc = avc_has_perm(&selinux_state,
1831 sid, dsec->sid, SECCLASS_DIR,
1832 DIR__ADD_NAME | DIR__SEARCH,
1837 rc = selinux_determine_inode_label(selinux_cred(current_cred()), dir,
1838 &dentry->d_name, tclass, &newsid);
1842 rc = avc_has_perm(&selinux_state,
1843 sid, newsid, tclass, FILE__CREATE, &ad);
1847 return avc_has_perm(&selinux_state,
1849 SECCLASS_FILESYSTEM,
1850 FILESYSTEM__ASSOCIATE, &ad);
1854 #define MAY_UNLINK 1
1857 /* Check whether a task can link, unlink, or rmdir a file/directory. */
1858 static int may_link(struct inode *dir,
1859 struct dentry *dentry,
1863 struct inode_security_struct *dsec, *isec;
1864 struct common_audit_data ad;
1865 u32 sid = current_sid();
1869 dsec = inode_security(dir);
1870 isec = backing_inode_security(dentry);
1872 ad.type = LSM_AUDIT_DATA_DENTRY;
1873 ad.u.dentry = dentry;
1876 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
1877 rc = avc_has_perm(&selinux_state,
1878 sid, dsec->sid, SECCLASS_DIR, av, &ad);
1893 pr_warn("SELinux: %s: unrecognized kind %d\n",
1898 rc = avc_has_perm(&selinux_state,
1899 sid, isec->sid, isec->sclass, av, &ad);
1903 static inline int may_rename(struct inode *old_dir,
1904 struct dentry *old_dentry,
1905 struct inode *new_dir,
1906 struct dentry *new_dentry)
1908 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
1909 struct common_audit_data ad;
1910 u32 sid = current_sid();
1912 int old_is_dir, new_is_dir;
1915 old_dsec = inode_security(old_dir);
1916 old_isec = backing_inode_security(old_dentry);
1917 old_is_dir = d_is_dir(old_dentry);
1918 new_dsec = inode_security(new_dir);
1920 ad.type = LSM_AUDIT_DATA_DENTRY;
1922 ad.u.dentry = old_dentry;
1923 rc = avc_has_perm(&selinux_state,
1924 sid, old_dsec->sid, SECCLASS_DIR,
1925 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1928 rc = avc_has_perm(&selinux_state,
1930 old_isec->sclass, FILE__RENAME, &ad);
1933 if (old_is_dir && new_dir != old_dir) {
1934 rc = avc_has_perm(&selinux_state,
1936 old_isec->sclass, DIR__REPARENT, &ad);
1941 ad.u.dentry = new_dentry;
1942 av = DIR__ADD_NAME | DIR__SEARCH;
1943 if (d_is_positive(new_dentry))
1944 av |= DIR__REMOVE_NAME;
1945 rc = avc_has_perm(&selinux_state,
1946 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
1949 if (d_is_positive(new_dentry)) {
1950 new_isec = backing_inode_security(new_dentry);
1951 new_is_dir = d_is_dir(new_dentry);
1952 rc = avc_has_perm(&selinux_state,
1955 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1963 /* Check whether a task can perform a filesystem operation. */
1964 static int superblock_has_perm(const struct cred *cred,
1965 struct super_block *sb,
1967 struct common_audit_data *ad)
1969 struct superblock_security_struct *sbsec;
1970 u32 sid = cred_sid(cred);
1972 sbsec = sb->s_security;
1973 return avc_has_perm(&selinux_state,
1974 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
1977 /* Convert a Linux mode and permission mask to an access vector. */
1978 static inline u32 file_mask_to_av(int mode, int mask)
1982 if (!S_ISDIR(mode)) {
1983 if (mask & MAY_EXEC)
1984 av |= FILE__EXECUTE;
1985 if (mask & MAY_READ)
1988 if (mask & MAY_APPEND)
1990 else if (mask & MAY_WRITE)
1994 if (mask & MAY_EXEC)
1996 if (mask & MAY_WRITE)
1998 if (mask & MAY_READ)
2005 /* Convert a Linux file to an access vector. */
2006 static inline u32 file_to_av(struct file *file)
2010 if (file->f_mode & FMODE_READ)
2012 if (file->f_mode & FMODE_WRITE) {
2013 if (file->f_flags & O_APPEND)
2020 * Special file opened with flags 3 for ioctl-only use.
2029 * Convert a file to an access vector and include the correct open
2032 static inline u32 open_file_to_av(struct file *file)
2034 u32 av = file_to_av(file);
2035 struct inode *inode = file_inode(file);
2037 if (selinux_policycap_openperm() &&
2038 inode->i_sb->s_magic != SOCKFS_MAGIC)
2044 /* Hook functions begin here. */
2046 static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2048 u32 mysid = current_sid();
2049 u32 mgrsid = task_sid(mgr);
2051 return avc_has_perm(&selinux_state,
2052 mysid, mgrsid, SECCLASS_BINDER,
2053 BINDER__SET_CONTEXT_MGR, NULL);
2056 static int selinux_binder_transaction(struct task_struct *from,
2057 struct task_struct *to)
2059 u32 mysid = current_sid();
2060 u32 fromsid = task_sid(from);
2061 u32 tosid = task_sid(to);
2064 if (mysid != fromsid) {
2065 rc = avc_has_perm(&selinux_state,
2066 mysid, fromsid, SECCLASS_BINDER,
2067 BINDER__IMPERSONATE, NULL);
2072 return avc_has_perm(&selinux_state,
2073 fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
2077 static int selinux_binder_transfer_binder(struct task_struct *from,
2078 struct task_struct *to)
2080 u32 fromsid = task_sid(from);
2081 u32 tosid = task_sid(to);
2083 return avc_has_perm(&selinux_state,
2084 fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
2088 static int selinux_binder_transfer_file(struct task_struct *from,
2089 struct task_struct *to,
2092 u32 sid = task_sid(to);
2093 struct file_security_struct *fsec = selinux_file(file);
2094 struct dentry *dentry = file->f_path.dentry;
2095 struct inode_security_struct *isec;
2096 struct common_audit_data ad;
2099 ad.type = LSM_AUDIT_DATA_PATH;
2100 ad.u.path = file->f_path;
2102 if (sid != fsec->sid) {
2103 rc = avc_has_perm(&selinux_state,
2112 #ifdef CONFIG_BPF_SYSCALL
2113 rc = bpf_fd_pass(file, sid);
2118 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2121 isec = backing_inode_security(dentry);
2122 return avc_has_perm(&selinux_state,
2123 sid, isec->sid, isec->sclass, file_to_av(file),
2127 static int selinux_ptrace_access_check(struct task_struct *child,
2130 u32 sid = current_sid();
2131 u32 csid = task_sid(child);
2133 if (mode & PTRACE_MODE_READ)
2134 return avc_has_perm(&selinux_state,
2135 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
2137 return avc_has_perm(&selinux_state,
2138 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
2141 static int selinux_ptrace_traceme(struct task_struct *parent)
2143 return avc_has_perm(&selinux_state,
2144 task_sid(parent), current_sid(), SECCLASS_PROCESS,
2145 PROCESS__PTRACE, NULL);
2148 static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
2149 kernel_cap_t *inheritable, kernel_cap_t *permitted)
2151 return avc_has_perm(&selinux_state,
2152 current_sid(), task_sid(target), SECCLASS_PROCESS,
2153 PROCESS__GETCAP, NULL);
2156 static int selinux_capset(struct cred *new, const struct cred *old,
2157 const kernel_cap_t *effective,
2158 const kernel_cap_t *inheritable,
2159 const kernel_cap_t *permitted)
2161 return avc_has_perm(&selinux_state,
2162 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
2163 PROCESS__SETCAP, NULL);
2167 * (This comment used to live with the selinux_task_setuid hook,
2168 * which was removed).
2170 * Since setuid only affects the current process, and since the SELinux
2171 * controls are not based on the Linux identity attributes, SELinux does not
2172 * need to control this operation. However, SELinux does control the use of
2173 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2176 static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2177 int cap, unsigned int opts)
2179 return cred_has_capability(cred, cap, opts, ns == &init_user_ns);
2182 static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2184 const struct cred *cred = current_cred();
2196 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
2201 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
2204 rc = 0; /* let the kernel handle invalid cmds */
2210 static int selinux_quota_on(struct dentry *dentry)
2212 const struct cred *cred = current_cred();
2214 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
2217 static int selinux_syslog(int type)
2220 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2221 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
2222 return avc_has_perm(&selinux_state,
2223 current_sid(), SECINITSID_KERNEL,
2224 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
2225 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2226 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2227 /* Set level of messages printed to console */
2228 case SYSLOG_ACTION_CONSOLE_LEVEL:
2229 return avc_has_perm(&selinux_state,
2230 current_sid(), SECINITSID_KERNEL,
2231 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2234 /* All other syslog types */
2235 return avc_has_perm(&selinux_state,
2236 current_sid(), SECINITSID_KERNEL,
2237 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
2241 * Check that a process has enough memory to allocate a new virtual
2242 * mapping. 0 means there is enough memory for the allocation to
2243 * succeed and -ENOMEM implies there is not.
2245 * Do not audit the selinux permission check, as this is applied to all
2246 * processes that allocate mappings.
2248 static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
2250 int rc, cap_sys_admin = 0;
2252 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
2253 CAP_OPT_NOAUDIT, true);
2257 return cap_sys_admin;
2260 /* binprm security operations */
2262 static u32 ptrace_parent_sid(void)
2265 struct task_struct *tracer;
2268 tracer = ptrace_parent(current);
2270 sid = task_sid(tracer);
2276 static int check_nnp_nosuid(const struct linux_binprm *bprm,
2277 const struct task_security_struct *old_tsec,
2278 const struct task_security_struct *new_tsec)
2280 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
2281 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
2285 if (!nnp && !nosuid)
2286 return 0; /* neither NNP nor nosuid */
2288 if (new_tsec->sid == old_tsec->sid)
2289 return 0; /* No change in credentials */
2292 * If the policy enables the nnp_nosuid_transition policy capability,
2293 * then we permit transitions under NNP or nosuid if the
2294 * policy allows the corresponding permission between
2295 * the old and new contexts.
2297 if (selinux_policycap_nnp_nosuid_transition()) {
2300 av |= PROCESS2__NNP_TRANSITION;
2302 av |= PROCESS2__NOSUID_TRANSITION;
2303 rc = avc_has_perm(&selinux_state,
2304 old_tsec->sid, new_tsec->sid,
2305 SECCLASS_PROCESS2, av, NULL);
2311 * We also permit NNP or nosuid transitions to bounded SIDs,
2312 * i.e. SIDs that are guaranteed to only be allowed a subset
2313 * of the permissions of the current SID.
2315 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2321 * On failure, preserve the errno values for NNP vs nosuid.
2322 * NNP: Operation not permitted for caller.
2323 * nosuid: Permission denied to file.
2330 static int selinux_bprm_set_creds(struct linux_binprm *bprm)
2332 const struct task_security_struct *old_tsec;
2333 struct task_security_struct *new_tsec;
2334 struct inode_security_struct *isec;
2335 struct common_audit_data ad;
2336 struct inode *inode = file_inode(bprm->file);
2339 /* SELinux context only depends on initial program or script and not
2340 * the script interpreter */
2341 if (bprm->called_set_creds)
2344 old_tsec = selinux_cred(current_cred());
2345 new_tsec = selinux_cred(bprm->cred);
2346 isec = inode_security(inode);
2348 /* Default to the current task SID. */
2349 new_tsec->sid = old_tsec->sid;
2350 new_tsec->osid = old_tsec->sid;
2352 /* Reset fs, key, and sock SIDs on execve. */
2353 new_tsec->create_sid = 0;
2354 new_tsec->keycreate_sid = 0;
2355 new_tsec->sockcreate_sid = 0;
2357 if (old_tsec->exec_sid) {
2358 new_tsec->sid = old_tsec->exec_sid;
2359 /* Reset exec SID on execve. */
2360 new_tsec->exec_sid = 0;
2362 /* Fail on NNP or nosuid if not an allowed transition. */
2363 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2367 /* Check for a default transition on this program. */
2368 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2369 isec->sid, SECCLASS_PROCESS, NULL,
2375 * Fallback to old SID on NNP or nosuid if not an allowed
2378 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2380 new_tsec->sid = old_tsec->sid;
2383 ad.type = LSM_AUDIT_DATA_FILE;
2384 ad.u.file = bprm->file;
2386 if (new_tsec->sid == old_tsec->sid) {
2387 rc = avc_has_perm(&selinux_state,
2388 old_tsec->sid, isec->sid,
2389 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2393 /* Check permissions for the transition. */
2394 rc = avc_has_perm(&selinux_state,
2395 old_tsec->sid, new_tsec->sid,
2396 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2400 rc = avc_has_perm(&selinux_state,
2401 new_tsec->sid, isec->sid,
2402 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2406 /* Check for shared state */
2407 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2408 rc = avc_has_perm(&selinux_state,
2409 old_tsec->sid, new_tsec->sid,
2410 SECCLASS_PROCESS, PROCESS__SHARE,
2416 /* Make sure that anyone attempting to ptrace over a task that
2417 * changes its SID has the appropriate permit */
2418 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
2419 u32 ptsid = ptrace_parent_sid();
2421 rc = avc_has_perm(&selinux_state,
2422 ptsid, new_tsec->sid,
2424 PROCESS__PTRACE, NULL);
2430 /* Clear any possibly unsafe personality bits on exec: */
2431 bprm->per_clear |= PER_CLEAR_ON_SETID;
2433 /* Enable secure mode for SIDs transitions unless
2434 the noatsecure permission is granted between
2435 the two SIDs, i.e. ahp returns 0. */
2436 rc = avc_has_perm(&selinux_state,
2437 old_tsec->sid, new_tsec->sid,
2438 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2440 bprm->secureexec |= !!rc;
2446 static int match_file(const void *p, struct file *file, unsigned fd)
2448 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2451 /* Derived from fs/exec.c:flush_old_files. */
2452 static inline void flush_unauthorized_files(const struct cred *cred,
2453 struct files_struct *files)
2455 struct file *file, *devnull = NULL;
2456 struct tty_struct *tty;
2460 tty = get_current_tty();
2462 spin_lock(&tty->files_lock);
2463 if (!list_empty(&tty->tty_files)) {
2464 struct tty_file_private *file_priv;
2466 /* Revalidate access to controlling tty.
2467 Use file_path_has_perm on the tty path directly
2468 rather than using file_has_perm, as this particular
2469 open file may belong to another process and we are
2470 only interested in the inode-based check here. */
2471 file_priv = list_first_entry(&tty->tty_files,
2472 struct tty_file_private, list);
2473 file = file_priv->file;
2474 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
2477 spin_unlock(&tty->files_lock);
2480 /* Reset controlling tty. */
2484 /* Revalidate access to inherited open files. */
2485 n = iterate_fd(files, 0, match_file, cred);
2486 if (!n) /* none found? */
2489 devnull = dentry_open(&selinux_null, O_RDWR, cred);
2490 if (IS_ERR(devnull))
2492 /* replace all the matching ones with this */
2494 replace_fd(n - 1, devnull, 0);
2495 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2501 * Prepare a process for imminent new credential changes due to exec
2503 static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
2505 struct task_security_struct *new_tsec;
2506 struct rlimit *rlim, *initrlim;
2509 new_tsec = selinux_cred(bprm->cred);
2510 if (new_tsec->sid == new_tsec->osid)
2513 /* Close files for which the new task SID is not authorized. */
2514 flush_unauthorized_files(bprm->cred, current->files);
2516 /* Always clear parent death signal on SID transitions. */
2517 current->pdeath_signal = 0;
2519 /* Check whether the new SID can inherit resource limits from the old
2520 * SID. If not, reset all soft limits to the lower of the current
2521 * task's hard limit and the init task's soft limit.
2523 * Note that the setting of hard limits (even to lower them) can be
2524 * controlled by the setrlimit check. The inclusion of the init task's
2525 * soft limit into the computation is to avoid resetting soft limits
2526 * higher than the default soft limit for cases where the default is
2527 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2529 rc = avc_has_perm(&selinux_state,
2530 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2531 PROCESS__RLIMITINH, NULL);
2533 /* protect against do_prlimit() */
2535 for (i = 0; i < RLIM_NLIMITS; i++) {
2536 rlim = current->signal->rlim + i;
2537 initrlim = init_task.signal->rlim + i;
2538 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2540 task_unlock(current);
2541 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2542 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
2547 * Clean up the process immediately after the installation of new credentials
2550 static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2552 const struct task_security_struct *tsec = selinux_cred(current_cred());
2553 struct itimerval itimer;
2563 /* Check whether the new SID can inherit signal state from the old SID.
2564 * If not, clear itimers to avoid subsequent signal generation and
2565 * flush and unblock signals.
2567 * This must occur _after_ the task SID has been updated so that any
2568 * kill done after the flush will be checked against the new SID.
2570 rc = avc_has_perm(&selinux_state,
2571 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
2573 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
2574 memset(&itimer, 0, sizeof itimer);
2575 for (i = 0; i < 3; i++)
2576 do_setitimer(i, &itimer, NULL);
2578 spin_lock_irq(¤t->sighand->siglock);
2579 if (!fatal_signal_pending(current)) {
2580 flush_sigqueue(¤t->pending);
2581 flush_sigqueue(¤t->signal->shared_pending);
2582 flush_signal_handlers(current, 1);
2583 sigemptyset(¤t->blocked);
2584 recalc_sigpending();
2586 spin_unlock_irq(¤t->sighand->siglock);
2589 /* Wake up the parent if it is waiting so that it can recheck
2590 * wait permission to the new task SID. */
2591 read_lock(&tasklist_lock);
2592 __wake_up_parent(current, current->real_parent);
2593 read_unlock(&tasklist_lock);
2596 /* superblock security operations */
2598 static int selinux_sb_alloc_security(struct super_block *sb)
2600 return superblock_alloc_security(sb);
2603 static void selinux_sb_free_security(struct super_block *sb)
2605 superblock_free_security(sb);
2608 static inline int opt_len(const char *s)
2610 bool open_quote = false;
2614 for (len = 0; (c = s[len]) != '\0'; len++) {
2616 open_quote = !open_quote;
2617 if (c == ',' && !open_quote)
2623 static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
2625 char *from = options;
2631 int len = opt_len(from);
2635 token = match_opt_prefix(from, len, &arg);
2637 if (token != Opt_error) {
2642 for (p = q = arg; p < from + len; p++) {
2647 arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
2653 rc = selinux_add_opt(token, arg, mnt_opts);
2659 if (!first) { // copy with preceding comma
2664 memmove(to, from, len);
2677 selinux_free_mnt_opts(*mnt_opts);
2683 static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
2685 struct selinux_mnt_opts *opts = mnt_opts;
2686 struct superblock_security_struct *sbsec = sb->s_security;
2690 if (!(sbsec->flags & SE_SBINITIALIZED))
2696 if (opts->fscontext) {
2697 rc = parse_sid(sb, opts->fscontext, &sid);
2700 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2701 goto out_bad_option;
2703 if (opts->context) {
2704 rc = parse_sid(sb, opts->context, &sid);
2707 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2708 goto out_bad_option;
2710 if (opts->rootcontext) {
2711 struct inode_security_struct *root_isec;
2712 root_isec = backing_inode_security(sb->s_root);
2713 rc = parse_sid(sb, opts->rootcontext, &sid);
2716 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2717 goto out_bad_option;
2719 if (opts->defcontext) {
2720 rc = parse_sid(sb, opts->defcontext, &sid);
2723 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2724 goto out_bad_option;
2729 pr_warn("SELinux: unable to change security options "
2730 "during remount (dev %s, type=%s)\n", sb->s_id,
2735 static int selinux_sb_kern_mount(struct super_block *sb)
2737 const struct cred *cred = current_cred();
2738 struct common_audit_data ad;
2740 ad.type = LSM_AUDIT_DATA_DENTRY;
2741 ad.u.dentry = sb->s_root;
2742 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
2745 static int selinux_sb_statfs(struct dentry *dentry)
2747 const struct cred *cred = current_cred();
2748 struct common_audit_data ad;
2750 ad.type = LSM_AUDIT_DATA_DENTRY;
2751 ad.u.dentry = dentry->d_sb->s_root;
2752 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
2755 static int selinux_mount(const char *dev_name,
2756 const struct path *path,
2758 unsigned long flags,
2761 const struct cred *cred = current_cred();
2763 if (flags & MS_REMOUNT)
2764 return superblock_has_perm(cred, path->dentry->d_sb,
2765 FILESYSTEM__REMOUNT, NULL);
2767 return path_has_perm(cred, path, FILE__MOUNTON);
2770 static int selinux_umount(struct vfsmount *mnt, int flags)
2772 const struct cred *cred = current_cred();
2774 return superblock_has_perm(cred, mnt->mnt_sb,
2775 FILESYSTEM__UNMOUNT, NULL);
2778 static int selinux_fs_context_dup(struct fs_context *fc,
2779 struct fs_context *src_fc)
2781 const struct selinux_mnt_opts *src = src_fc->security;
2782 struct selinux_mnt_opts *opts;
2787 fc->security = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
2791 opts = fc->security;
2793 if (src->fscontext) {
2794 opts->fscontext = kstrdup(src->fscontext, GFP_KERNEL);
2795 if (!opts->fscontext)
2799 opts->context = kstrdup(src->context, GFP_KERNEL);
2803 if (src->rootcontext) {
2804 opts->rootcontext = kstrdup(src->rootcontext, GFP_KERNEL);
2805 if (!opts->rootcontext)
2808 if (src->defcontext) {
2809 opts->defcontext = kstrdup(src->defcontext, GFP_KERNEL);
2810 if (!opts->defcontext)
2816 static const struct fs_parameter_spec selinux_param_specs[] = {
2817 fsparam_string(CONTEXT_STR, Opt_context),
2818 fsparam_string(DEFCONTEXT_STR, Opt_defcontext),
2819 fsparam_string(FSCONTEXT_STR, Opt_fscontext),
2820 fsparam_string(ROOTCONTEXT_STR, Opt_rootcontext),
2821 fsparam_flag (SECLABEL_STR, Opt_seclabel),
2825 static const struct fs_parameter_description selinux_fs_parameters = {
2827 .specs = selinux_param_specs,
2830 static int selinux_fs_context_parse_param(struct fs_context *fc,
2831 struct fs_parameter *param)
2833 struct fs_parse_result result;
2836 opt = fs_parse(fc, &selinux_fs_parameters, param, &result);
2840 rc = selinux_add_opt(opt, param->string, &fc->security);
2842 param->string = NULL;
2848 /* inode security operations */
2850 static int selinux_inode_alloc_security(struct inode *inode)
2852 return inode_alloc_security(inode);
2855 static void selinux_inode_free_security(struct inode *inode)
2857 inode_free_security(inode);
2860 static int selinux_dentry_init_security(struct dentry *dentry, int mode,
2861 const struct qstr *name, void **ctx,
2867 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
2868 d_inode(dentry->d_parent), name,
2869 inode_mode_to_security_class(mode),
2874 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2878 static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2880 const struct cred *old,
2885 struct task_security_struct *tsec;
2887 rc = selinux_determine_inode_label(selinux_cred(old),
2888 d_inode(dentry->d_parent), name,
2889 inode_mode_to_security_class(mode),
2894 tsec = selinux_cred(new);
2895 tsec->create_sid = newsid;
2899 static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
2900 const struct qstr *qstr,
2902 void **value, size_t *len)
2904 const struct task_security_struct *tsec = selinux_cred(current_cred());
2905 struct superblock_security_struct *sbsec;
2910 sbsec = dir->i_sb->s_security;
2912 newsid = tsec->create_sid;
2914 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
2916 inode_mode_to_security_class(inode->i_mode),
2921 /* Possibly defer initialization to selinux_complete_init. */
2922 if (sbsec->flags & SE_SBINITIALIZED) {
2923 struct inode_security_struct *isec = selinux_inode(inode);
2924 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2926 isec->initialized = LABEL_INITIALIZED;
2929 if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
2933 *name = XATTR_SELINUX_SUFFIX;
2936 rc = security_sid_to_context_force(&selinux_state, newsid,
2947 static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
2949 return may_create(dir, dentry, SECCLASS_FILE);
2952 static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2954 return may_link(dir, old_dentry, MAY_LINK);
2957 static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2959 return may_link(dir, dentry, MAY_UNLINK);
2962 static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2964 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2967 static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
2969 return may_create(dir, dentry, SECCLASS_DIR);
2972 static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2974 return may_link(dir, dentry, MAY_RMDIR);
2977 static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
2979 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2982 static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
2983 struct inode *new_inode, struct dentry *new_dentry)
2985 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2988 static int selinux_inode_readlink(struct dentry *dentry)
2990 const struct cred *cred = current_cred();
2992 return dentry_has_perm(cred, dentry, FILE__READ);
2995 static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2998 const struct cred *cred = current_cred();
2999 struct common_audit_data ad;
3000 struct inode_security_struct *isec;
3003 validate_creds(cred);
3005 ad.type = LSM_AUDIT_DATA_DENTRY;
3006 ad.u.dentry = dentry;
3007 sid = cred_sid(cred);
3008 isec = inode_security_rcu(inode, rcu);
3010 return PTR_ERR(isec);
3012 return avc_has_perm(&selinux_state,
3013 sid, isec->sid, isec->sclass, FILE__READ, &ad);
3016 static noinline int audit_inode_permission(struct inode *inode,
3017 u32 perms, u32 audited, u32 denied,
3021 struct common_audit_data ad;
3022 struct inode_security_struct *isec = selinux_inode(inode);
3025 ad.type = LSM_AUDIT_DATA_INODE;
3028 rc = slow_avc_audit(&selinux_state,
3029 current_sid(), isec->sid, isec->sclass, perms,
3030 audited, denied, result, &ad, flags);
3036 static int selinux_inode_permission(struct inode *inode, int mask)
3038 const struct cred *cred = current_cred();
3041 unsigned flags = mask & MAY_NOT_BLOCK;
3042 struct inode_security_struct *isec;
3044 struct av_decision avd;
3046 u32 audited, denied;
3048 from_access = mask & MAY_ACCESS;
3049 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3051 /* No permission to check. Existence test. */
3055 validate_creds(cred);
3057 if (unlikely(IS_PRIVATE(inode)))
3060 perms = file_mask_to_av(inode->i_mode, mask);
3062 sid = cred_sid(cred);
3063 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
3065 return PTR_ERR(isec);
3067 rc = avc_has_perm_noaudit(&selinux_state,
3068 sid, isec->sid, isec->sclass, perms,
3069 (flags & MAY_NOT_BLOCK) ? AVC_NONBLOCKING : 0,
3071 audited = avc_audit_required(perms, &avd, rc,
3072 from_access ? FILE__AUDIT_ACCESS : 0,
3074 if (likely(!audited))
3077 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
3083 static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3085 const struct cred *cred = current_cred();
3086 struct inode *inode = d_backing_inode(dentry);
3087 unsigned int ia_valid = iattr->ia_valid;
3088 __u32 av = FILE__WRITE;
3090 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3091 if (ia_valid & ATTR_FORCE) {
3092 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3098 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3099 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
3100 return dentry_has_perm(cred, dentry, FILE__SETATTR);
3102 if (selinux_policycap_openperm() &&
3103 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3104 (ia_valid & ATTR_SIZE) &&
3105 !(ia_valid & ATTR_FILE))
3108 return dentry_has_perm(cred, dentry, av);
3111 static int selinux_inode_getattr(const struct path *path)
3113 return path_has_perm(current_cred(), path, FILE__GETATTR);
3116 static bool has_cap_mac_admin(bool audit)
3118 const struct cred *cred = current_cred();
3119 unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT;
3121 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts))
3123 if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true))
3128 static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3129 const void *value, size_t size, int flags)
3131 struct inode *inode = d_backing_inode(dentry);
3132 struct inode_security_struct *isec;
3133 struct superblock_security_struct *sbsec;
3134 struct common_audit_data ad;
3135 u32 newsid, sid = current_sid();
3138 if (strcmp(name, XATTR_NAME_SELINUX)) {
3139 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3143 /* Not an attribute we recognize, so just check the
3144 ordinary setattr permission. */
3145 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3148 sbsec = inode->i_sb->s_security;
3149 if (!(sbsec->flags & SBLABEL_MNT))
3152 if (!inode_owner_or_capable(inode))
3155 ad.type = LSM_AUDIT_DATA_DENTRY;
3156 ad.u.dentry = dentry;
3158 isec = backing_inode_security(dentry);
3159 rc = avc_has_perm(&selinux_state,
3160 sid, isec->sid, isec->sclass,
3161 FILE__RELABELFROM, &ad);
3165 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3167 if (rc == -EINVAL) {
3168 if (!has_cap_mac_admin(true)) {
3169 struct audit_buffer *ab;
3172 /* We strip a nul only if it is at the end, otherwise the
3173 * context contains a nul and we should audit that */
3175 const char *str = value;
3177 if (str[size - 1] == '\0')
3178 audit_size = size - 1;
3184 ab = audit_log_start(audit_context(),
3185 GFP_ATOMIC, AUDIT_SELINUX_ERR);
3186 audit_log_format(ab, "op=setxattr invalid_context=");
3187 audit_log_n_untrustedstring(ab, value, audit_size);
3192 rc = security_context_to_sid_force(&selinux_state, value,
3198 rc = avc_has_perm(&selinux_state,
3199 sid, newsid, isec->sclass,
3200 FILE__RELABELTO, &ad);
3204 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3209 return avc_has_perm(&selinux_state,
3212 SECCLASS_FILESYSTEM,
3213 FILESYSTEM__ASSOCIATE,
3217 static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
3218 const void *value, size_t size,
3221 struct inode *inode = d_backing_inode(dentry);
3222 struct inode_security_struct *isec;
3226 if (strcmp(name, XATTR_NAME_SELINUX)) {
3227 /* Not an attribute we recognize, so nothing to do. */
3231 rc = security_context_to_sid_force(&selinux_state, value, size,
3234 pr_err("SELinux: unable to map context to SID"
3235 "for (%s, %lu), rc=%d\n",
3236 inode->i_sb->s_id, inode->i_ino, -rc);
3240 isec = backing_inode_security(dentry);
3241 spin_lock(&isec->lock);
3242 isec->sclass = inode_mode_to_security_class(inode->i_mode);
3244 isec->initialized = LABEL_INITIALIZED;
3245 spin_unlock(&isec->lock);
3250 static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
3252 const struct cred *cred = current_cred();
3254 return dentry_has_perm(cred, dentry, FILE__GETATTR);
3257 static int selinux_inode_listxattr(struct dentry *dentry)
3259 const struct cred *cred = current_cred();
3261 return dentry_has_perm(cred, dentry, FILE__GETATTR);
3264 static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
3266 if (strcmp(name, XATTR_NAME_SELINUX)) {
3267 int rc = cap_inode_removexattr(dentry, name);
3271 /* Not an attribute we recognize, so just check the
3272 ordinary setattr permission. */
3273 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3276 /* No one is allowed to remove a SELinux security label.
3277 You can change the label, but all data must be labeled. */
3282 * Copy the inode security context value to the user.
3284 * Permission check is handled by selinux_inode_getxattr hook.
3286 static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
3290 char *context = NULL;
3291 struct inode_security_struct *isec;
3293 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3297 * If the caller has CAP_MAC_ADMIN, then get the raw context
3298 * value even if it is not defined by current policy; otherwise,
3299 * use the in-core value under current policy.
3300 * Use the non-auditing forms of the permission checks since
3301 * getxattr may be called by unprivileged processes commonly
3302 * and lack of permission just means that we fall back to the
3303 * in-core context value, not a denial.
3305 isec = inode_security(inode);
3306 if (has_cap_mac_admin(false))
3307 error = security_sid_to_context_force(&selinux_state,
3308 isec->sid, &context,
3311 error = security_sid_to_context(&selinux_state, isec->sid,
3325 static int selinux_inode_setsecurity(struct inode *inode, const char *name,
3326 const void *value, size_t size, int flags)
3328 struct inode_security_struct *isec = inode_security_novalidate(inode);
3329 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
3333 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3336 if (!(sbsec->flags & SBLABEL_MNT))
3339 if (!value || !size)
3342 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3347 spin_lock(&isec->lock);
3348 isec->sclass = inode_mode_to_security_class(inode->i_mode);
3350 isec->initialized = LABEL_INITIALIZED;
3351 spin_unlock(&isec->lock);
3355 static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3357 const int len = sizeof(XATTR_NAME_SELINUX);
3358 if (buffer && len <= buffer_size)
3359 memcpy(buffer, XATTR_NAME_SELINUX, len);
3363 static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
3365 struct inode_security_struct *isec = inode_security_novalidate(inode);
3369 static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3372 struct task_security_struct *tsec;
3373 struct cred *new_creds = *new;
3375 if (new_creds == NULL) {
3376 new_creds = prepare_creds();
3381 tsec = selinux_cred(new_creds);
3382 /* Get label from overlay inode and set it in create_sid */
3383 selinux_inode_getsecid(d_inode(src), &sid);
3384 tsec->create_sid = sid;
3389 static int selinux_inode_copy_up_xattr(const char *name)
3391 /* The copy_up hook above sets the initial context on an inode, but we
3392 * don't then want to overwrite it by blindly copying all the lower
3393 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3395 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3396 return 1; /* Discard */
3398 * Any other attribute apart from SELINUX is not claimed, supported
3404 /* kernfs node operations */
3406 static int selinux_kernfs_init_security(struct kernfs_node *kn_dir,
3407 struct kernfs_node *kn)
3409 const struct task_security_struct *tsec = current_security();
3410 u32 parent_sid, newsid, clen;
3414 rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, NULL, 0);
3421 context = kmalloc(clen, GFP_KERNEL);
3425 rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, context, clen);
3431 rc = security_context_to_sid(&selinux_state, context, clen, &parent_sid,
3437 if (tsec->create_sid) {
3438 newsid = tsec->create_sid;
3440 u16 secclass = inode_mode_to_security_class(kn->mode);
3444 q.hash_len = hashlen_string(kn_dir, kn->name);
3446 rc = security_transition_sid(&selinux_state, tsec->sid,
3447 parent_sid, secclass, &q,
3453 rc = security_sid_to_context_force(&selinux_state, newsid,
3458 rc = kernfs_xattr_set(kn, XATTR_NAME_SELINUX, context, clen,
3465 /* file security operations */
3467 static int selinux_revalidate_file_permission(struct file *file, int mask)
3469 const struct cred *cred = current_cred();
3470 struct inode *inode = file_inode(file);
3472 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3473 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3476 return file_has_perm(cred, file,
3477 file_mask_to_av(inode->i_mode, mask));
3480 static int selinux_file_permission(struct file *file, int mask)
3482 struct inode *inode = file_inode(file);
3483 struct file_security_struct *fsec = selinux_file(file);
3484 struct inode_security_struct *isec;
3485 u32 sid = current_sid();
3488 /* No permission to check. Existence test. */
3491 isec = inode_security(inode);
3492 if (sid == fsec->sid && fsec->isid == isec->sid &&
3493 fsec->pseqno == avc_policy_seqno(&selinux_state))
3494 /* No change since file_open check. */
3497 return selinux_revalidate_file_permission(file, mask);
3500 static int selinux_file_alloc_security(struct file *file)
3502 return file_alloc_security(file);
3506 * Check whether a task has the ioctl permission and cmd
3507 * operation to an inode.
3509 static int ioctl_has_perm(const struct cred *cred, struct file *file,
3510 u32 requested, u16 cmd)
3512 struct common_audit_data ad;
3513 struct file_security_struct *fsec = selinux_file(file);
3514 struct inode *inode = file_inode(file);
3515 struct inode_security_struct *isec;
3516 struct lsm_ioctlop_audit ioctl;
3517 u32 ssid = cred_sid(cred);
3519 u8 driver = cmd >> 8;
3520 u8 xperm = cmd & 0xff;
3522 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3525 ad.u.op->path = file->f_path;
3527 if (ssid != fsec->sid) {
3528 rc = avc_has_perm(&selinux_state,
3537 if (unlikely(IS_PRIVATE(inode)))
3540 isec = inode_security(inode);
3541 rc = avc_has_extended_perms(&selinux_state,
3542 ssid, isec->sid, isec->sclass,
3543 requested, driver, xperm, &ad);
3548 static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3551 const struct cred *cred = current_cred();
3561 case FS_IOC_GETFLAGS:
3563 case FS_IOC_GETVERSION:
3564 error = file_has_perm(cred, file, FILE__GETATTR);
3567 case FS_IOC_SETFLAGS:
3569 case FS_IOC_SETVERSION:
3570 error = file_has_perm(cred, file, FILE__SETATTR);
3573 /* sys_ioctl() checks */
3577 error = file_has_perm(cred, file, 0);
3582 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3583 CAP_OPT_NONE, true);
3586 /* default case assumes that the command will go
3587 * to the file's ioctl() function.
3590 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
3595 static int default_noexec;
3597 static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3599 const struct cred *cred = current_cred();
3600 u32 sid = cred_sid(cred);
3603 if (default_noexec &&
3604 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3605 (!shared && (prot & PROT_WRITE)))) {
3607 * We are making executable an anonymous mapping or a
3608 * private file mapping that will also be writable.
3609 * This has an additional check.
3611 rc = avc_has_perm(&selinux_state,
3612 sid, sid, SECCLASS_PROCESS,
3613 PROCESS__EXECMEM, NULL);
3619 /* read access is always possible with a mapping */
3620 u32 av = FILE__READ;
3622 /* write access only matters if the mapping is shared */
3623 if (shared && (prot & PROT_WRITE))
3626 if (prot & PROT_EXEC)
3627 av |= FILE__EXECUTE;
3629 return file_has_perm(cred, file, av);
3636 static int selinux_mmap_addr(unsigned long addr)
3640 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3641 u32 sid = current_sid();
3642 rc = avc_has_perm(&selinux_state,
3643 sid, sid, SECCLASS_MEMPROTECT,
3644 MEMPROTECT__MMAP_ZERO, NULL);
3650 static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3651 unsigned long prot, unsigned long flags)
3653 struct common_audit_data ad;
3657 ad.type = LSM_AUDIT_DATA_FILE;
3659 rc = inode_has_perm(current_cred(), file_inode(file),
3665 if (selinux_state.checkreqprot)
3668 return file_map_prot_check(file, prot,
3669 (flags & MAP_TYPE) == MAP_SHARED);
3672 static int selinux_file_mprotect(struct vm_area_struct *vma,
3673 unsigned long reqprot,
3676 const struct cred *cred = current_cred();
3677 u32 sid = cred_sid(cred);
3679 if (selinux_state.checkreqprot)
3682 if (default_noexec &&
3683 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3685 if (vma->vm_start >= vma->vm_mm->start_brk &&
3686 vma->vm_end <= vma->vm_mm->brk) {
3687 rc = avc_has_perm(&selinux_state,
3688 sid, sid, SECCLASS_PROCESS,
3689 PROCESS__EXECHEAP, NULL);
3690 } else if (!vma->vm_file &&
3691 ((vma->vm_start <= vma->vm_mm->start_stack &&
3692 vma->vm_end >= vma->vm_mm->start_stack) ||
3693 vma_is_stack_for_current(vma))) {
3694 rc = avc_has_perm(&selinux_state,
3695 sid, sid, SECCLASS_PROCESS,
3696 PROCESS__EXECSTACK, NULL);
3697 } else if (vma->vm_file && vma->anon_vma) {
3699 * We are making executable a file mapping that has
3700 * had some COW done. Since pages might have been
3701 * written, check ability to execute the possibly
3702 * modified content. This typically should only
3703 * occur for text relocations.
3705 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
3711 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3714 static int selinux_file_lock(struct file *file, unsigned int cmd)
3716 const struct cred *cred = current_cred();
3718 return file_has_perm(cred, file, FILE__LOCK);
3721 static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3724 const struct cred *cred = current_cred();
3729 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
3730 err = file_has_perm(cred, file, FILE__WRITE);
3739 case F_GETOWNER_UIDS:
3740 /* Just check FD__USE permission */
3741 err = file_has_perm(cred, file, 0);
3749 #if BITS_PER_LONG == 32
3754 err = file_has_perm(cred, file, FILE__LOCK);
3761 static void selinux_file_set_fowner(struct file *file)
3763 struct file_security_struct *fsec;
3765 fsec = selinux_file(file);
3766 fsec->fown_sid = current_sid();
3769 static int selinux_file_send_sigiotask(struct task_struct *tsk,
3770 struct fown_struct *fown, int signum)
3773 u32 sid = task_sid(tsk);
3775 struct file_security_struct *fsec;
3777 /* struct fown_struct is never outside the context of a struct file */
3778 file = container_of(fown, struct file, f_owner);
3780 fsec = selinux_file(file);
3783 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3785 perm = signal_to_av(signum);
3787 return avc_has_perm(&selinux_state,
3788 fsec->fown_sid, sid,
3789 SECCLASS_PROCESS, perm, NULL);
3792 static int selinux_file_receive(struct file *file)
3794 const struct cred *cred = current_cred();
3796 return file_has_perm(cred, file, file_to_av(file));
3799 static int selinux_file_open(struct file *file)
3801 struct file_security_struct *fsec;
3802 struct inode_security_struct *isec;
3804 fsec = selinux_file(file);
3805 isec = inode_security(file_inode(file));
3807 * Save inode label and policy sequence number
3808 * at open-time so that selinux_file_permission
3809 * can determine whether revalidation is necessary.
3810 * Task label is already saved in the file security
3811 * struct as its SID.
3813 fsec->isid = isec->sid;
3814 fsec->pseqno = avc_policy_seqno(&selinux_state);
3816 * Since the inode label or policy seqno may have changed
3817 * between the selinux_inode_permission check and the saving
3818 * of state above, recheck that access is still permitted.
3819 * Otherwise, access might never be revalidated against the
3820 * new inode label or new policy.
3821 * This check is not redundant - do not remove.
3823 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
3826 /* task security operations */
3828 static int selinux_task_alloc(struct task_struct *task,
3829 unsigned long clone_flags)
3831 u32 sid = current_sid();
3833 return avc_has_perm(&selinux_state,
3834 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
3838 * prepare a new set of credentials for modification
3840 static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3843 const struct task_security_struct *old_tsec = selinux_cred(old);
3844 struct task_security_struct *tsec = selinux_cred(new);
3851 * transfer the SELinux data to a blank set of creds
3853 static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3855 const struct task_security_struct *old_tsec = selinux_cred(old);
3856 struct task_security_struct *tsec = selinux_cred(new);
3861 static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3863 *secid = cred_sid(c);
3867 * set the security data for a kernel service
3868 * - all the creation contexts are set to unlabelled
3870 static int selinux_kernel_act_as(struct cred *new, u32 secid)
3872 struct task_security_struct *tsec = selinux_cred(new);
3873 u32 sid = current_sid();
3876 ret = avc_has_perm(&selinux_state,
3878 SECCLASS_KERNEL_SERVICE,
3879 KERNEL_SERVICE__USE_AS_OVERRIDE,
3883 tsec->create_sid = 0;
3884 tsec->keycreate_sid = 0;
3885 tsec->sockcreate_sid = 0;
3891 * set the file creation context in a security record to the same as the
3892 * objective context of the specified inode
3894 static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3896 struct inode_security_struct *isec = inode_security(inode);
3897 struct task_security_struct *tsec = selinux_cred(new);
3898 u32 sid = current_sid();
3901 ret = avc_has_perm(&selinux_state,
3903 SECCLASS_KERNEL_SERVICE,
3904 KERNEL_SERVICE__CREATE_FILES_AS,
3908 tsec->create_sid = isec->sid;
3912 static int selinux_kernel_module_request(char *kmod_name)
3914 struct common_audit_data ad;
3916 ad.type = LSM_AUDIT_DATA_KMOD;
3917 ad.u.kmod_name = kmod_name;
3919 return avc_has_perm(&selinux_state,
3920 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
3921 SYSTEM__MODULE_REQUEST, &ad);
3924 static int selinux_kernel_module_from_file(struct file *file)
3926 struct common_audit_data ad;
3927 struct inode_security_struct *isec;
3928 struct file_security_struct *fsec;
3929 u32 sid = current_sid();
3934 return avc_has_perm(&selinux_state,
3935 sid, sid, SECCLASS_SYSTEM,
3936 SYSTEM__MODULE_LOAD, NULL);
3940 ad.type = LSM_AUDIT_DATA_FILE;
3943 fsec = selinux_file(file);
3944 if (sid != fsec->sid) {
3945 rc = avc_has_perm(&selinux_state,
3946 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
3951 isec = inode_security(file_inode(file));
3952 return avc_has_perm(&selinux_state,
3953 sid, isec->sid, SECCLASS_SYSTEM,
3954 SYSTEM__MODULE_LOAD, &ad);
3957 static int selinux_kernel_read_file(struct file *file,
3958 enum kernel_read_file_id id)
3963 case READING_MODULE:
3964 rc = selinux_kernel_module_from_file(file);
3973 static int selinux_kernel_load_data(enum kernel_load_data_id id)
3978 case LOADING_MODULE:
3979 rc = selinux_kernel_module_from_file(NULL);
3987 static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3989 return avc_has_perm(&selinux_state,
3990 current_sid(), task_sid(p), SECCLASS_PROCESS,
3991 PROCESS__SETPGID, NULL);
3994 static int selinux_task_getpgid(struct task_struct *p)
3996 return avc_has_perm(&selinux_state,
3997 current_sid(), task_sid(p), SECCLASS_PROCESS,
3998 PROCESS__GETPGID, NULL);
4001 static int selinux_task_getsid(struct task_struct *p)
4003 return avc_has_perm(&selinux_state,
4004 current_sid(), task_sid(p), SECCLASS_PROCESS,
4005 PROCESS__GETSESSION, NULL);
4008 static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
4010 *secid = task_sid(p);
4013 static int selinux_task_setnice(struct task_struct *p, int nice)
4015 return avc_has_perm(&selinux_state,
4016 current_sid(), task_sid(p), SECCLASS_PROCESS,
4017 PROCESS__SETSCHED, NULL);
4020 static int selinux_task_setioprio(struct task_struct *p, int ioprio)
4022 return avc_has_perm(&selinux_state,
4023 current_sid(), task_sid(p), SECCLASS_PROCESS,
4024 PROCESS__SETSCHED, NULL);
4027 static int selinux_task_getioprio(struct task_struct *p)
4029 return avc_has_perm(&selinux_state,
4030 current_sid(), task_sid(p), SECCLASS_PROCESS,
4031 PROCESS__GETSCHED, NULL);
4034 static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
4041 if (flags & LSM_PRLIMIT_WRITE)
4042 av |= PROCESS__SETRLIMIT;
4043 if (flags & LSM_PRLIMIT_READ)
4044 av |= PROCESS__GETRLIMIT;
4045 return avc_has_perm(&selinux_state,
4046 cred_sid(cred), cred_sid(tcred),
4047 SECCLASS_PROCESS, av, NULL);
4050 static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
4051 struct rlimit *new_rlim)
4053 struct rlimit *old_rlim = p->signal->rlim + resource;
4055 /* Control the ability to change the hard limit (whether
4056 lowering or raising it), so that the hard limit can
4057 later be used as a safe reset point for the soft limit
4058 upon context transitions. See selinux_bprm_committing_creds. */
4059 if (old_rlim->rlim_max != new_rlim->rlim_max)
4060 return avc_has_perm(&selinux_state,
4061 current_sid(), task_sid(p),
4062 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
4067 static int selinux_task_setscheduler(struct task_struct *p)
4069 return avc_has_perm(&selinux_state,
4070 current_sid(), task_sid(p), SECCLASS_PROCESS,
4071 PROCESS__SETSCHED, NULL);
4074 static int selinux_task_getscheduler(struct task_struct *p)
4076 return avc_has_perm(&selinux_state,
4077 current_sid(), task_sid(p), SECCLASS_PROCESS,
4078 PROCESS__GETSCHED, NULL);
4081 static int selinux_task_movememory(struct task_struct *p)
4083 return avc_has_perm(&selinux_state,
4084 current_sid(), task_sid(p), SECCLASS_PROCESS,
4085 PROCESS__SETSCHED, NULL);
4088 static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
4089 int sig, const struct cred *cred)
4095 perm = PROCESS__SIGNULL; /* null signal; existence test */
4097 perm = signal_to_av(sig);
4099 secid = current_sid();
4101 secid = cred_sid(cred);
4102 return avc_has_perm(&selinux_state,
4103 secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
4106 static void selinux_task_to_inode(struct task_struct *p,
4107 struct inode *inode)
4109 struct inode_security_struct *isec = selinux_inode(inode);
4110 u32 sid = task_sid(p);
4112 spin_lock(&isec->lock);
4113 isec->sclass = inode_mode_to_security_class(inode->i_mode);
4115 isec->initialized = LABEL_INITIALIZED;
4116 spin_unlock(&isec->lock);
4119 /* Returns error only if unable to parse addresses */
4120 static int selinux_parse_skb_ipv4(struct sk_buff *skb,
4121 struct common_audit_data *ad, u8 *proto)
4123 int offset, ihlen, ret = -EINVAL;
4124 struct iphdr _iph, *ih;
4126 offset = skb_network_offset(skb);
4127 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4131 ihlen = ih->ihl * 4;
4132 if (ihlen < sizeof(_iph))
4135 ad->u.net->v4info.saddr = ih->saddr;
4136 ad->u.net->v4info.daddr = ih->daddr;
4140 *proto = ih->protocol;
4142 switch (ih->protocol) {
4144 struct tcphdr _tcph, *th;
4146 if (ntohs(ih->frag_off) & IP_OFFSET)
4150 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4154 ad->u.net->sport = th->source;
4155 ad->u.net->dport = th->dest;
4160 struct udphdr _udph, *uh;
4162 if (ntohs(ih->frag_off) & IP_OFFSET)
4166 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4170 ad->u.net->sport = uh->source;
4171 ad->u.net->dport = uh->dest;
4175 case IPPROTO_DCCP: {
4176 struct dccp_hdr _dccph, *dh;
4178 if (ntohs(ih->frag_off) & IP_OFFSET)
4182 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4186 ad->u.net->sport = dh->dccph_sport;
4187 ad->u.net->dport = dh->dccph_dport;
4191 #if IS_ENABLED(CONFIG_IP_SCTP)
4192 case IPPROTO_SCTP: {
4193 struct sctphdr _sctph, *sh;
4195 if (ntohs(ih->frag_off) & IP_OFFSET)
4199 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4203 ad->u.net->sport = sh->source;
4204 ad->u.net->dport = sh->dest;
4215 #if IS_ENABLED(CONFIG_IPV6)
4217 /* Returns error only if unable to parse addresses */
4218 static int selinux_parse_skb_ipv6(struct sk_buff *skb,
4219 struct common_audit_data *ad, u8 *proto)
4222 int ret = -EINVAL, offset;
4223 struct ipv6hdr _ipv6h, *ip6;
4226 offset = skb_network_offset(skb);
4227 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4231 ad->u.net->v6info.saddr = ip6->saddr;
4232 ad->u.net->v6info.daddr = ip6->daddr;
4235 nexthdr = ip6->nexthdr;
4236 offset += sizeof(_ipv6h);
4237 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
4246 struct tcphdr _tcph, *th;
4248 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4252 ad->u.net->sport = th->source;
4253 ad->u.net->dport = th->dest;
4258 struct udphdr _udph, *uh;
4260 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4264 ad->u.net->sport = uh->source;
4265 ad->u.net->dport = uh->dest;
4269 case IPPROTO_DCCP: {
4270 struct dccp_hdr _dccph, *dh;
4272 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4276 ad->u.net->sport = dh->dccph_sport;
4277 ad->u.net->dport = dh->dccph_dport;
4281 #if IS_ENABLED(CONFIG_IP_SCTP)
4282 case IPPROTO_SCTP: {
4283 struct sctphdr _sctph, *sh;
4285 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4289 ad->u.net->sport = sh->source;
4290 ad->u.net->dport = sh->dest;
4294 /* includes fragments */
4304 static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
4305 char **_addrp, int src, u8 *proto)
4310 switch (ad->u.net->family) {
4312 ret = selinux_parse_skb_ipv4(skb, ad, proto);
4315 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4316 &ad->u.net->v4info.daddr);
4319 #if IS_ENABLED(CONFIG_IPV6)
4321 ret = selinux_parse_skb_ipv6(skb, ad, proto);
4324 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4325 &ad->u.net->v6info.daddr);
4335 "SELinux: failure in selinux_parse_skb(),"
4336 " unable to parse packet\n");
4346 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
4348 * @family: protocol family
4349 * @sid: the packet's peer label SID
4352 * Check the various different forms of network peer labeling and determine
4353 * the peer label/SID for the packet; most of the magic actually occurs in
4354 * the security server function security_net_peersid_cmp(). The function
4355 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4356 * or -EACCES if @sid is invalid due to inconsistencies with the different
4360 static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
4367 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
4370 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4374 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4375 nlbl_type, xfrm_sid, sid);
4376 if (unlikely(err)) {
4378 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4379 " unable to determine packet's peer label\n");
4387 * selinux_conn_sid - Determine the child socket label for a connection
4388 * @sk_sid: the parent socket's SID
4389 * @skb_sid: the packet's SID
4390 * @conn_sid: the resulting connection SID
4392 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4393 * combined with the MLS information from @skb_sid in order to create
4394 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4395 * of @sk_sid. Returns zero on success, negative values on failure.
4398 static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4402 if (skb_sid != SECSID_NULL)
4403 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4411 /* socket security operations */
4413 static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4414 u16 secclass, u32 *socksid)
4416 if (tsec->sockcreate_sid > SECSID_NULL) {
4417 *socksid = tsec->sockcreate_sid;
4421 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4422 secclass, NULL, socksid);
4425 static int sock_has_perm(struct sock *sk, u32 perms)
4427 struct sk_security_struct *sksec = sk->sk_security;
4428 struct common_audit_data ad;
4429 struct lsm_network_audit net = {0,};
4431 if (sksec->sid == SECINITSID_KERNEL)
4434 ad.type = LSM_AUDIT_DATA_NET;
4438 return avc_has_perm(&selinux_state,
4439 current_sid(), sksec->sid, sksec->sclass, perms,
4443 static int selinux_socket_create(int family, int type,
4444 int protocol, int kern)
4446 const struct task_security_struct *tsec = selinux_cred(current_cred());
4454 secclass = socket_type_to_security_class(family, type, protocol);
4455 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4459 return avc_has_perm(&selinux_state,
4460 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
4463 static int selinux_socket_post_create(struct socket *sock, int family,
4464 int type, int protocol, int kern)
4466 const struct task_security_struct *tsec = selinux_cred(current_cred());
4467 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
4468 struct sk_security_struct *sksec;
4469 u16 sclass = socket_type_to_security_class(family, type, protocol);
4470 u32 sid = SECINITSID_KERNEL;
4474 err = socket_sockcreate_sid(tsec, sclass, &sid);
4479 isec->sclass = sclass;
4481 isec->initialized = LABEL_INITIALIZED;
4484 sksec = sock->sk->sk_security;
4485 sksec->sclass = sclass;
4487 /* Allows detection of the first association on this socket */
4488 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4489 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4491 err = selinux_netlbl_socket_post_create(sock->sk, family);
4497 static int selinux_socket_socketpair(struct socket *socka,
4498 struct socket *sockb)
4500 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4501 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4503 sksec_a->peer_sid = sksec_b->sid;
4504 sksec_b->peer_sid = sksec_a->sid;
4509 /* Range of port numbers used to automatically bind.
4510 Need to determine whether we should perform a name_bind
4511 permission check between the socket and the port number. */
4513 static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4515 struct sock *sk = sock->sk;
4516 struct sk_security_struct *sksec = sk->sk_security;
4520 err = sock_has_perm(sk, SOCKET__BIND);
4524 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
4525 family = sk->sk_family;
4526 if (family == PF_INET || family == PF_INET6) {
4528 struct common_audit_data ad;
4529 struct lsm_network_audit net = {0,};
4530 struct sockaddr_in *addr4 = NULL;
4531 struct sockaddr_in6 *addr6 = NULL;
4533 unsigned short snum;
4537 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4538 * that validates multiple binding addresses. Because of this
4539 * need to check address->sa_family as it is possible to have
4540 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4542 if (addrlen < offsetofend(struct sockaddr, sa_family))
4544 family_sa = address->sa_family;
4545 switch (family_sa) {
4548 if (addrlen < sizeof(struct sockaddr_in))
4550 addr4 = (struct sockaddr_in *)address;
4551 if (family_sa == AF_UNSPEC) {
4552 /* see __inet_bind(), we only want to allow
4553 * AF_UNSPEC if the address is INADDR_ANY
4555 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4557 family_sa = AF_INET;
4559 snum = ntohs(addr4->sin_port);
4560 addrp = (char *)&addr4->sin_addr.s_addr;
4563 if (addrlen < SIN6_LEN_RFC2133)
4565 addr6 = (struct sockaddr_in6 *)address;
4566 snum = ntohs(addr6->sin6_port);
4567 addrp = (char *)&addr6->sin6_addr.s6_addr;
4573 ad.type = LSM_AUDIT_DATA_NET;
4575 ad.u.net->sport = htons(snum);
4576 ad.u.net->family = family_sa;
4581 inet_get_local_port_range(sock_net(sk), &low, &high);
4583 if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
4585 err = sel_netport_sid(sk->sk_protocol,
4589 err = avc_has_perm(&selinux_state,
4592 SOCKET__NAME_BIND, &ad);
4598 switch (sksec->sclass) {
4599 case SECCLASS_TCP_SOCKET:
4600 node_perm = TCP_SOCKET__NODE_BIND;
4603 case SECCLASS_UDP_SOCKET:
4604 node_perm = UDP_SOCKET__NODE_BIND;
4607 case SECCLASS_DCCP_SOCKET:
4608 node_perm = DCCP_SOCKET__NODE_BIND;
4611 case SECCLASS_SCTP_SOCKET:
4612 node_perm = SCTP_SOCKET__NODE_BIND;
4616 node_perm = RAWIP_SOCKET__NODE_BIND;
4620 err = sel_netnode_sid(addrp, family_sa, &sid);
4624 if (family_sa == AF_INET)
4625 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
4627 ad.u.net->v6info.saddr = addr6->sin6_addr;
4629 err = avc_has_perm(&selinux_state,
4631 sksec->sclass, node_perm, &ad);
4638 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4639 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4641 return -EAFNOSUPPORT;
4644 /* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
4645 * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst
4647 static int selinux_socket_connect_helper(struct socket *sock,
4648 struct sockaddr *address, int addrlen)
4650 struct sock *sk = sock->sk;
4651 struct sk_security_struct *sksec = sk->sk_security;
4654 err = sock_has_perm(sk, SOCKET__CONNECT);
4657 if (addrlen < offsetofend(struct sockaddr, sa_family))
4660 /* connect(AF_UNSPEC) has special handling, as it is a documented
4661 * way to disconnect the socket
4663 if (address->sa_family == AF_UNSPEC)
4667 * If a TCP, DCCP or SCTP socket, check name_connect permission
4670 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4671 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4672 sksec->sclass == SECCLASS_SCTP_SOCKET) {
4673 struct common_audit_data ad;
4674 struct lsm_network_audit net = {0,};
4675 struct sockaddr_in *addr4 = NULL;
4676 struct sockaddr_in6 *addr6 = NULL;
4677 unsigned short snum;
4680 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4681 * that validates multiple connect addresses. Because of this
4682 * need to check address->sa_family as it is possible to have
4683 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4685 switch (address->sa_family) {
4687 addr4 = (struct sockaddr_in *)address;
4688 if (addrlen < sizeof(struct sockaddr_in))
4690 snum = ntohs(addr4->sin_port);
4693 addr6 = (struct sockaddr_in6 *)address;
4694 if (addrlen < SIN6_LEN_RFC2133)
4696 snum = ntohs(addr6->sin6_port);
4699 /* Note that SCTP services expect -EINVAL, whereas
4700 * others expect -EAFNOSUPPORT.
4702 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4705 return -EAFNOSUPPORT;
4708 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
4712 switch (sksec->sclass) {
4713 case SECCLASS_TCP_SOCKET:
4714 perm = TCP_SOCKET__NAME_CONNECT;
4716 case SECCLASS_DCCP_SOCKET:
4717 perm = DCCP_SOCKET__NAME_CONNECT;
4719 case SECCLASS_SCTP_SOCKET:
4720 perm = SCTP_SOCKET__NAME_CONNECT;
4724 ad.type = LSM_AUDIT_DATA_NET;
4726 ad.u.net->dport = htons(snum);
4727 ad.u.net->family = address->sa_family;
4728 err = avc_has_perm(&selinux_state,
4729 sksec->sid, sid, sksec->sclass, perm, &ad);
4737 /* Supports connect(2), see comments in selinux_socket_connect_helper() */
4738 static int selinux_socket_connect(struct socket *sock,
4739 struct sockaddr *address, int addrlen)
4742 struct sock *sk = sock->sk;
4744 err = selinux_socket_connect_helper(sock, address, addrlen);
4748 return selinux_netlbl_socket_connect(sk, address);
4751 static int selinux_socket_listen(struct socket *sock, int backlog)
4753 return sock_has_perm(sock->sk, SOCKET__LISTEN);
4756 static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4759 struct inode_security_struct *isec;
4760 struct inode_security_struct *newisec;
4764 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
4768 isec = inode_security_novalidate(SOCK_INODE(sock));
4769 spin_lock(&isec->lock);
4770 sclass = isec->sclass;
4772 spin_unlock(&isec->lock);
4774 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4775 newisec->sclass = sclass;
4777 newisec->initialized = LABEL_INITIALIZED;
4782 static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
4785 return sock_has_perm(sock->sk, SOCKET__WRITE);
4788 static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4789 int size, int flags)
4791 return sock_has_perm(sock->sk, SOCKET__READ);
4794 static int selinux_socket_getsockname(struct socket *sock)
4796 return sock_has_perm(sock->sk, SOCKET__GETATTR);
4799 static int selinux_socket_getpeername(struct socket *sock)
4801 return sock_has_perm(sock->sk, SOCKET__GETATTR);
4804 static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
4808 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
4812 return selinux_netlbl_socket_setsockopt(sock, level, optname);
4815 static int selinux_socket_getsockopt(struct socket *sock, int level,
4818 return sock_has_perm(sock->sk, SOCKET__GETOPT);
4821 static int selinux_socket_shutdown(struct socket *sock, int how)
4823 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
4826 static int selinux_socket_unix_stream_connect(struct sock *sock,
4830 struct sk_security_struct *sksec_sock = sock->sk_security;
4831 struct sk_security_struct *sksec_other = other->sk_security;
4832 struct sk_security_struct *sksec_new = newsk->sk_security;
4833 struct common_audit_data ad;
4834 struct lsm_network_audit net = {0,};
4837 ad.type = LSM_AUDIT_DATA_NET;
4839 ad.u.net->sk = other;
4841 err = avc_has_perm(&selinux_state,
4842 sksec_sock->sid, sksec_other->sid,
4843 sksec_other->sclass,
4844 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4848 /* server child socket */
4849 sksec_new->peer_sid = sksec_sock->sid;
4850 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4851 sksec_sock->sid, &sksec_new->sid);
4855 /* connecting socket */
4856 sksec_sock->peer_sid = sksec_new->sid;
4861 static int selinux_socket_unix_may_send(struct socket *sock,
4862 struct socket *other)
4864 struct sk_security_struct *ssec = sock->sk->sk_security;
4865 struct sk_security_struct *osec = other->sk->sk_security;
4866 struct common_audit_data ad;
4867 struct lsm_network_audit net = {0,};
4869 ad.type = LSM_AUDIT_DATA_NET;
4871 ad.u.net->sk = other->sk;
4873 return avc_has_perm(&selinux_state,
4874 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4878 static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4879 char *addrp, u16 family, u32 peer_sid,
4880 struct common_audit_data *ad)
4886 err = sel_netif_sid(ns, ifindex, &if_sid);
4889 err = avc_has_perm(&selinux_state,
4891 SECCLASS_NETIF, NETIF__INGRESS, ad);
4895 err = sel_netnode_sid(addrp, family, &node_sid);
4898 return avc_has_perm(&selinux_state,
4900 SECCLASS_NODE, NODE__RECVFROM, ad);
4903 static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
4907 struct sk_security_struct *sksec = sk->sk_security;
4908 u32 sk_sid = sksec->sid;
4909 struct common_audit_data ad;
4910 struct lsm_network_audit net = {0,};
4913 ad.type = LSM_AUDIT_DATA_NET;
4915 ad.u.net->netif = skb->skb_iif;
4916 ad.u.net->family = family;
4917 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4921 if (selinux_secmark_enabled()) {
4922 err = avc_has_perm(&selinux_state,
4923 sk_sid, skb->secmark, SECCLASS_PACKET,
4929 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4932 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
4937 static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4940 struct sk_security_struct *sksec = sk->sk_security;
4941 u16 family = sk->sk_family;
4942 u32 sk_sid = sksec->sid;
4943 struct common_audit_data ad;
4944 struct lsm_network_audit net = {0,};
4949 if (family != PF_INET && family != PF_INET6)
4952 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
4953 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4956 /* If any sort of compatibility mode is enabled then handoff processing
4957 * to the selinux_sock_rcv_skb_compat() function to deal with the
4958 * special handling. We do this in an attempt to keep this function
4959 * as fast and as clean as possible. */
4960 if (!selinux_policycap_netpeer())
4961 return selinux_sock_rcv_skb_compat(sk, skb, family);
4963 secmark_active = selinux_secmark_enabled();
4964 peerlbl_active = selinux_peerlbl_enabled();
4965 if (!secmark_active && !peerlbl_active)
4968 ad.type = LSM_AUDIT_DATA_NET;
4970 ad.u.net->netif = skb->skb_iif;
4971 ad.u.net->family = family;
4972 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4976 if (peerlbl_active) {
4979 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4982 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4983 addrp, family, peer_sid, &ad);
4985 selinux_netlbl_err(skb, family, err, 0);
4988 err = avc_has_perm(&selinux_state,
4989 sk_sid, peer_sid, SECCLASS_PEER,
4992 selinux_netlbl_err(skb, family, err, 0);
4997 if (secmark_active) {
4998 err = avc_has_perm(&selinux_state,
4999 sk_sid, skb->secmark, SECCLASS_PACKET,
5008 static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
5009 int __user *optlen, unsigned len)
5014 struct sk_security_struct *sksec = sock->sk->sk_security;
5015 u32 peer_sid = SECSID_NULL;
5017 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
5018 sksec->sclass == SECCLASS_TCP_SOCKET ||
5019 sksec->sclass == SECCLASS_SCTP_SOCKET)
5020 peer_sid = sksec->peer_sid;
5021 if (peer_sid == SECSID_NULL)
5022 return -ENOPROTOOPT;
5024 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
5029 if (scontext_len > len) {
5034 if (copy_to_user(optval, scontext, scontext_len))
5038 if (put_user(scontext_len, optlen))
5044 static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
5046 u32 peer_secid = SECSID_NULL;
5048 struct inode_security_struct *isec;
5050 if (skb && skb->protocol == htons(ETH_P_IP))
5052 else if (skb && skb->protocol == htons(ETH_P_IPV6))
5055 family = sock->sk->sk_family;
5059 if (sock && family == PF_UNIX) {
5060 isec = inode_security_novalidate(SOCK_INODE(sock));
5061 peer_secid = isec->sid;
5063 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
5066 *secid = peer_secid;
5067 if (peer_secid == SECSID_NULL)
5072 static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
5074 struct sk_security_struct *sksec;
5076 sksec = kzalloc(sizeof(*sksec), priority);
5080 sksec->peer_sid = SECINITSID_UNLABELED;
5081 sksec->sid = SECINITSID_UNLABELED;
5082 sksec->sclass = SECCLASS_SOCKET;
5083 selinux_netlbl_sk_security_reset(sksec);
5084 sk->sk_security = sksec;
5089 static void selinux_sk_free_security(struct sock *sk)
5091 struct sk_security_struct *sksec = sk->sk_security;
5093 sk->sk_security = NULL;
5094 selinux_netlbl_sk_security_free(sksec);
5098 static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5100 struct sk_security_struct *sksec = sk->sk_security;
5101 struct sk_security_struct *newsksec = newsk->sk_security;
5103 newsksec->sid = sksec->sid;
5104 newsksec->peer_sid = sksec->peer_sid;
5105 newsksec->sclass = sksec->sclass;
5107 selinux_netlbl_sk_security_reset(newsksec);
5110 static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
5113 *secid = SECINITSID_ANY_SOCKET;
5115 struct sk_security_struct *sksec = sk->sk_security;
5117 *secid = sksec->sid;
5121 static void selinux_sock_graft(struct sock *sk, struct socket *parent)
5123 struct inode_security_struct *isec =
5124 inode_security_novalidate(SOCK_INODE(parent));
5125 struct sk_security_struct *sksec = sk->sk_security;
5127 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5128 sk->sk_family == PF_UNIX)
5129 isec->sid = sksec->sid;
5130 sksec->sclass = isec->sclass;
5133 /* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5134 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5137 static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5138 struct sk_buff *skb)
5140 struct sk_security_struct *sksec = ep->base.sk->sk_security;
5141 struct common_audit_data ad;
5142 struct lsm_network_audit net = {0,};
5144 u32 peer_sid = SECINITSID_UNLABELED;
5148 if (!selinux_policycap_extsockclass())
5151 peerlbl_active = selinux_peerlbl_enabled();
5153 if (peerlbl_active) {
5154 /* This will return peer_sid = SECSID_NULL if there are
5155 * no peer labels, see security_net_peersid_resolve().
5157 err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5162 if (peer_sid == SECSID_NULL)
5163 peer_sid = SECINITSID_UNLABELED;
5166 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5167 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5169 /* Here as first association on socket. As the peer SID
5170 * was allowed by peer recv (and the netif/node checks),
5171 * then it is approved by policy and used as the primary
5172 * peer SID for getpeercon(3).
5174 sksec->peer_sid = peer_sid;
5175 } else if (sksec->peer_sid != peer_sid) {
5176 /* Other association peer SIDs are checked to enforce
5177 * consistency among the peer SIDs.
5179 ad.type = LSM_AUDIT_DATA_NET;
5181 ad.u.net->sk = ep->base.sk;
5182 err = avc_has_perm(&selinux_state,
5183 sksec->peer_sid, peer_sid, sksec->sclass,
5184 SCTP_SOCKET__ASSOCIATION, &ad);
5189 /* Compute the MLS component for the connection and store
5190 * the information in ep. This will be used by SCTP TCP type
5191 * sockets and peeled off connections as they cause a new
5192 * socket to be generated. selinux_sctp_sk_clone() will then
5193 * plug this into the new socket.
5195 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5199 ep->secid = conn_sid;
5200 ep->peer_secid = peer_sid;
5202 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5203 return selinux_netlbl_sctp_assoc_request(ep, skb);
5206 /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5207 * based on their @optname.
5209 static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5210 struct sockaddr *address,
5213 int len, err = 0, walk_size = 0;
5215 struct sockaddr *addr;
5216 struct socket *sock;
5218 if (!selinux_policycap_extsockclass())
5221 /* Process one or more addresses that may be IPv4 or IPv6 */
5222 sock = sk->sk_socket;
5225 while (walk_size < addrlen) {
5226 if (walk_size + sizeof(sa_family_t) > addrlen)
5230 switch (addr->sa_family) {
5233 len = sizeof(struct sockaddr_in);
5236 len = sizeof(struct sockaddr_in6);
5242 if (walk_size + len > addrlen)
5248 case SCTP_PRIMARY_ADDR:
5249 case SCTP_SET_PEER_PRIMARY_ADDR:
5250 case SCTP_SOCKOPT_BINDX_ADD:
5251 err = selinux_socket_bind(sock, addr, len);
5253 /* Connect checks */
5254 case SCTP_SOCKOPT_CONNECTX:
5255 case SCTP_PARAM_SET_PRIMARY:
5256 case SCTP_PARAM_ADD_IP:
5257 case SCTP_SENDMSG_CONNECT:
5258 err = selinux_socket_connect_helper(sock, addr, len);
5262 /* As selinux_sctp_bind_connect() is called by the
5263 * SCTP protocol layer, the socket is already locked,
5264 * therefore selinux_netlbl_socket_connect_locked() is
5265 * is called here. The situations handled are:
5266 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5267 * whenever a new IP address is added or when a new
5268 * primary address is selected.
5269 * Note that an SCTP connect(2) call happens before
5270 * the SCTP protocol layer and is handled via
5271 * selinux_socket_connect().
5273 err = selinux_netlbl_socket_connect_locked(sk, addr);
5287 /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5288 static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5291 struct sk_security_struct *sksec = sk->sk_security;
5292 struct sk_security_struct *newsksec = newsk->sk_security;
5294 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5295 * the non-sctp clone version.
5297 if (!selinux_policycap_extsockclass())
5298 return selinux_sk_clone_security(sk, newsk);
5300 newsksec->sid = ep->secid;
5301 newsksec->peer_sid = ep->peer_secid;
5302 newsksec->sclass = sksec->sclass;
5303 selinux_netlbl_sctp_sk_clone(sk, newsk);
5306 static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
5307 struct request_sock *req)
5309 struct sk_security_struct *sksec = sk->sk_security;
5311 u16 family = req->rsk_ops->family;
5315 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
5318 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5321 req->secid = connsid;
5322 req->peer_secid = peersid;
5324 return selinux_netlbl_inet_conn_request(req, family);
5327 static void selinux_inet_csk_clone(struct sock *newsk,
5328 const struct request_sock *req)
5330 struct sk_security_struct *newsksec = newsk->sk_security;
5332 newsksec->sid = req->secid;
5333 newsksec->peer_sid = req->peer_secid;
5334 /* NOTE: Ideally, we should also get the isec->sid for the
5335 new socket in sync, but we don't have the isec available yet.
5336 So we will wait until sock_graft to do it, by which
5337 time it will have been created and available. */
5339 /* We don't need to take any sort of lock here as we are the only
5340 * thread with access to newsksec */
5341 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
5344 static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
5346 u16 family = sk->sk_family;
5347 struct sk_security_struct *sksec = sk->sk_security;
5349 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5350 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5353 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
5356 static int selinux_secmark_relabel_packet(u32 sid)
5358 const struct task_security_struct *__tsec;
5361 __tsec = selinux_cred(current_cred());
5364 return avc_has_perm(&selinux_state,
5365 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5369 static void selinux_secmark_refcount_inc(void)
5371 atomic_inc(&selinux_secmark_refcount);
5374 static void selinux_secmark_refcount_dec(void)
5376 atomic_dec(&selinux_secmark_refcount);
5379 static void selinux_req_classify_flow(const struct request_sock *req,
5382 fl->flowi_secid = req->secid;
5385 static int selinux_tun_dev_alloc_security(void **security)
5387 struct tun_security_struct *tunsec;
5389 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5392 tunsec->sid = current_sid();
5398 static void selinux_tun_dev_free_security(void *security)
5403 static int selinux_tun_dev_create(void)
5405 u32 sid = current_sid();
5407 /* we aren't taking into account the "sockcreate" SID since the socket
5408 * that is being created here is not a socket in the traditional sense,
5409 * instead it is a private sock, accessible only to the kernel, and
5410 * representing a wide range of network traffic spanning multiple
5411 * connections unlike traditional sockets - check the TUN driver to
5412 * get a better understanding of why this socket is special */
5414 return avc_has_perm(&selinux_state,
5415 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
5419 static int selinux_tun_dev_attach_queue(void *security)
5421 struct tun_security_struct *tunsec = security;
5423 return avc_has_perm(&selinux_state,
5424 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
5425 TUN_SOCKET__ATTACH_QUEUE, NULL);
5428 static int selinux_tun_dev_attach(struct sock *sk, void *security)
5430 struct tun_security_struct *tunsec = security;
5431 struct sk_security_struct *sksec = sk->sk_security;
5433 /* we don't currently perform any NetLabel based labeling here and it
5434 * isn't clear that we would want to do so anyway; while we could apply
5435 * labeling without the support of the TUN user the resulting labeled
5436 * traffic from the other end of the connection would almost certainly
5437 * cause confusion to the TUN user that had no idea network labeling
5438 * protocols were being used */
5440 sksec->sid = tunsec->sid;
5441 sksec->sclass = SECCLASS_TUN_SOCKET;
5446 static int selinux_tun_dev_open(void *security)
5448 struct tun_security_struct *tunsec = security;
5449 u32 sid = current_sid();
5452 err = avc_has_perm(&selinux_state,
5453 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
5454 TUN_SOCKET__RELABELFROM, NULL);
5457 err = avc_has_perm(&selinux_state,
5458 sid, sid, SECCLASS_TUN_SOCKET,
5459 TUN_SOCKET__RELABELTO, NULL);
5467 static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5471 struct nlmsghdr *nlh;
5472 struct sk_security_struct *sksec = sk->sk_security;
5474 if (skb->len < NLMSG_HDRLEN) {
5478 nlh = nlmsg_hdr(skb);
5480 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
5482 if (err == -EINVAL) {
5483 pr_warn_ratelimited("SELinux: unrecognized netlink"
5484 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5485 " pig=%d comm=%s\n",
5486 sk->sk_protocol, nlh->nlmsg_type,
5487 secclass_map[sksec->sclass - 1].name,
5488 task_pid_nr(current), current->comm);
5489 if (!enforcing_enabled(&selinux_state) ||
5490 security_get_allow_unknown(&selinux_state))
5500 err = sock_has_perm(sk, perm);
5505 #ifdef CONFIG_NETFILTER
5507 static unsigned int selinux_ip_forward(struct sk_buff *skb,
5508 const struct net_device *indev,
5514 struct common_audit_data ad;
5515 struct lsm_network_audit net = {0,};
5520 if (!selinux_policycap_netpeer())
5523 secmark_active = selinux_secmark_enabled();
5524 netlbl_active = netlbl_enabled();
5525 peerlbl_active = selinux_peerlbl_enabled();
5526 if (!secmark_active && !peerlbl_active)
5529 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5532 ad.type = LSM_AUDIT_DATA_NET;
5534 ad.u.net->netif = indev->ifindex;
5535 ad.u.net->family = family;
5536 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5539 if (peerlbl_active) {
5540 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5541 addrp, family, peer_sid, &ad);
5543 selinux_netlbl_err(skb, family, err, 1);
5549 if (avc_has_perm(&selinux_state,
5550 peer_sid, skb->secmark,
5551 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5555 /* we do this in the FORWARD path and not the POST_ROUTING
5556 * path because we want to make sure we apply the necessary
5557 * labeling before IPsec is applied so we can leverage AH
5559 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5565 static unsigned int selinux_ipv4_forward(void *priv,
5566 struct sk_buff *skb,
5567 const struct nf_hook_state *state)
5569 return selinux_ip_forward(skb, state->in, PF_INET);
5572 #if IS_ENABLED(CONFIG_IPV6)
5573 static unsigned int selinux_ipv6_forward(void *priv,
5574 struct sk_buff *skb,
5575 const struct nf_hook_state *state)
5577 return selinux_ip_forward(skb, state->in, PF_INET6);
5581 static unsigned int selinux_ip_output(struct sk_buff *skb,
5587 if (!netlbl_enabled())
5590 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5591 * because we want to make sure we apply the necessary labeling
5592 * before IPsec is applied so we can leverage AH protection */
5595 struct sk_security_struct *sksec;
5597 if (sk_listener(sk))
5598 /* if the socket is the listening state then this
5599 * packet is a SYN-ACK packet which means it needs to
5600 * be labeled based on the connection/request_sock and
5601 * not the parent socket. unfortunately, we can't
5602 * lookup the request_sock yet as it isn't queued on
5603 * the parent socket until after the SYN-ACK is sent.
5604 * the "solution" is to simply pass the packet as-is
5605 * as any IP option based labeling should be copied
5606 * from the initial connection request (in the IP
5607 * layer). it is far from ideal, but until we get a
5608 * security label in the packet itself this is the
5609 * best we can do. */
5612 /* standard practice, label using the parent socket */
5613 sksec = sk->sk_security;
5616 sid = SECINITSID_KERNEL;
5617 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5623 static unsigned int selinux_ipv4_output(void *priv,
5624 struct sk_buff *skb,
5625 const struct nf_hook_state *state)
5627 return selinux_ip_output(skb, PF_INET);
5630 #if IS_ENABLED(CONFIG_IPV6)
5631 static unsigned int selinux_ipv6_output(void *priv,
5632 struct sk_buff *skb,
5633 const struct nf_hook_state *state)
5635 return selinux_ip_output(skb, PF_INET6);
5639 static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5643 struct sock *sk = skb_to_full_sk(skb);
5644 struct sk_security_struct *sksec;
5645 struct common_audit_data ad;
5646 struct lsm_network_audit net = {0,};
5652 sksec = sk->sk_security;
5654 ad.type = LSM_AUDIT_DATA_NET;
5656 ad.u.net->netif = ifindex;
5657 ad.u.net->family = family;
5658 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5661 if (selinux_secmark_enabled())
5662 if (avc_has_perm(&selinux_state,
5663 sksec->sid, skb->secmark,
5664 SECCLASS_PACKET, PACKET__SEND, &ad))
5665 return NF_DROP_ERR(-ECONNREFUSED);
5667 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5668 return NF_DROP_ERR(-ECONNREFUSED);
5673 static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5674 const struct net_device *outdev,
5679 int ifindex = outdev->ifindex;
5681 struct common_audit_data ad;
5682 struct lsm_network_audit net = {0,};
5687 /* If any sort of compatibility mode is enabled then handoff processing
5688 * to the selinux_ip_postroute_compat() function to deal with the
5689 * special handling. We do this in an attempt to keep this function
5690 * as fast and as clean as possible. */
5691 if (!selinux_policycap_netpeer())
5692 return selinux_ip_postroute_compat(skb, ifindex, family);
5694 secmark_active = selinux_secmark_enabled();
5695 peerlbl_active = selinux_peerlbl_enabled();
5696 if (!secmark_active && !peerlbl_active)
5699 sk = skb_to_full_sk(skb);
5702 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5703 * packet transformation so allow the packet to pass without any checks
5704 * since we'll have another chance to perform access control checks
5705 * when the packet is on it's final way out.
5706 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
5707 * is NULL, in this case go ahead and apply access control.
5708 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5709 * TCP listening state we cannot wait until the XFRM processing
5710 * is done as we will miss out on the SA label if we do;
5711 * unfortunately, this means more work, but it is only once per
5713 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
5714 !(sk && sk_listener(sk)))
5719 /* Without an associated socket the packet is either coming
5720 * from the kernel or it is being forwarded; check the packet
5721 * to determine which and if the packet is being forwarded
5722 * query the packet directly to determine the security label. */
5724 secmark_perm = PACKET__FORWARD_OUT;
5725 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
5728 secmark_perm = PACKET__SEND;
5729 peer_sid = SECINITSID_KERNEL;
5731 } else if (sk_listener(sk)) {
5732 /* Locally generated packet but the associated socket is in the
5733 * listening state which means this is a SYN-ACK packet. In
5734 * this particular case the correct security label is assigned
5735 * to the connection/request_sock but unfortunately we can't
5736 * query the request_sock as it isn't queued on the parent
5737 * socket until after the SYN-ACK packet is sent; the only
5738 * viable choice is to regenerate the label like we do in
5739 * selinux_inet_conn_request(). See also selinux_ip_output()
5740 * for similar problems. */
5742 struct sk_security_struct *sksec;
5744 sksec = sk->sk_security;
5745 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5747 /* At this point, if the returned skb peerlbl is SECSID_NULL
5748 * and the packet has been through at least one XFRM
5749 * transformation then we must be dealing with the "final"
5750 * form of labeled IPsec packet; since we've already applied
5751 * all of our access controls on this packet we can safely
5752 * pass the packet. */
5753 if (skb_sid == SECSID_NULL) {
5756 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5760 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5764 return NF_DROP_ERR(-ECONNREFUSED);
5767 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5769 secmark_perm = PACKET__SEND;
5771 /* Locally generated packet, fetch the security label from the
5772 * associated socket. */
5773 struct sk_security_struct *sksec = sk->sk_security;
5774 peer_sid = sksec->sid;
5775 secmark_perm = PACKET__SEND;
5778 ad.type = LSM_AUDIT_DATA_NET;
5780 ad.u.net->netif = ifindex;
5781 ad.u.net->family = family;
5782 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
5786 if (avc_has_perm(&selinux_state,
5787 peer_sid, skb->secmark,
5788 SECCLASS_PACKET, secmark_perm, &ad))
5789 return NF_DROP_ERR(-ECONNREFUSED);
5791 if (peerlbl_active) {
5795 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
5797 if (avc_has_perm(&selinux_state,
5799 SECCLASS_NETIF, NETIF__EGRESS, &ad))
5800 return NF_DROP_ERR(-ECONNREFUSED);
5802 if (sel_netnode_sid(addrp, family, &node_sid))
5804 if (avc_has_perm(&selinux_state,
5806 SECCLASS_NODE, NODE__SENDTO, &ad))
5807 return NF_DROP_ERR(-ECONNREFUSED);
5813 static unsigned int selinux_ipv4_postroute(void *priv,
5814 struct sk_buff *skb,
5815 const struct nf_hook_state *state)
5817 return selinux_ip_postroute(skb, state->out, PF_INET);
5820 #if IS_ENABLED(CONFIG_IPV6)
5821 static unsigned int selinux_ipv6_postroute(void *priv,
5822 struct sk_buff *skb,
5823 const struct nf_hook_state *state)
5825 return selinux_ip_postroute(skb, state->out, PF_INET6);
5829 #endif /* CONFIG_NETFILTER */
5831 static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5833 return selinux_nlmsg_perm(sk, skb);
5836 static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass)
5838 isec->sclass = sclass;
5839 isec->sid = current_sid();
5842 static int msg_msg_alloc_security(struct msg_msg *msg)
5844 struct msg_security_struct *msec;
5846 msec = selinux_msg_msg(msg);
5847 msec->sid = SECINITSID_UNLABELED;
5852 static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
5855 struct ipc_security_struct *isec;
5856 struct common_audit_data ad;
5857 u32 sid = current_sid();
5859 isec = selinux_ipc(ipc_perms);
5861 ad.type = LSM_AUDIT_DATA_IPC;
5862 ad.u.ipc_id = ipc_perms->key;
5864 return avc_has_perm(&selinux_state,
5865 sid, isec->sid, isec->sclass, perms, &ad);
5868 static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5870 return msg_msg_alloc_security(msg);
5873 /* message queue security operations */
5874 static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
5876 struct ipc_security_struct *isec;
5877 struct common_audit_data ad;
5878 u32 sid = current_sid();
5881 isec = selinux_ipc(msq);
5882 ipc_init_security(isec, SECCLASS_MSGQ);
5884 ad.type = LSM_AUDIT_DATA_IPC;
5885 ad.u.ipc_id = msq->key;
5887 rc = avc_has_perm(&selinux_state,
5888 sid, isec->sid, SECCLASS_MSGQ,
5893 static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
5895 struct ipc_security_struct *isec;
5896 struct common_audit_data ad;
5897 u32 sid = current_sid();
5899 isec = selinux_ipc(msq);
5901 ad.type = LSM_AUDIT_DATA_IPC;
5902 ad.u.ipc_id = msq->key;
5904 return avc_has_perm(&selinux_state,
5905 sid, isec->sid, SECCLASS_MSGQ,
5906 MSGQ__ASSOCIATE, &ad);
5909 static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
5917 /* No specific object, just general system-wide information. */
5918 return avc_has_perm(&selinux_state,
5919 current_sid(), SECINITSID_KERNEL,
5920 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
5924 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5927 perms = MSGQ__SETATTR;
5930 perms = MSGQ__DESTROY;
5936 err = ipc_has_perm(msq, perms);
5940 static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
5942 struct ipc_security_struct *isec;
5943 struct msg_security_struct *msec;
5944 struct common_audit_data ad;
5945 u32 sid = current_sid();
5948 isec = selinux_ipc(msq);
5949 msec = selinux_msg_msg(msg);
5952 * First time through, need to assign label to the message
5954 if (msec->sid == SECINITSID_UNLABELED) {
5956 * Compute new sid based on current process and
5957 * message queue this message will be stored in
5959 rc = security_transition_sid(&selinux_state, sid, isec->sid,
5960 SECCLASS_MSG, NULL, &msec->sid);
5965 ad.type = LSM_AUDIT_DATA_IPC;
5966 ad.u.ipc_id = msq->key;
5968 /* Can this process write to the queue? */
5969 rc = avc_has_perm(&selinux_state,
5970 sid, isec->sid, SECCLASS_MSGQ,
5973 /* Can this process send the message */
5974 rc = avc_has_perm(&selinux_state,
5975 sid, msec->sid, SECCLASS_MSG,
5978 /* Can the message be put in the queue? */
5979 rc = avc_has_perm(&selinux_state,
5980 msec->sid, isec->sid, SECCLASS_MSGQ,
5981 MSGQ__ENQUEUE, &ad);
5986 static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
5987 struct task_struct *target,
5988 long type, int mode)
5990 struct ipc_security_struct *isec;
5991 struct msg_security_struct *msec;
5992 struct common_audit_data ad;
5993 u32 sid = task_sid(target);
5996 isec = selinux_ipc(msq);
5997 msec = selinux_msg_msg(msg);
5999 ad.type = LSM_AUDIT_DATA_IPC;
6000 ad.u.ipc_id = msq->key;
6002 rc = avc_has_perm(&selinux_state,
6004 SECCLASS_MSGQ, MSGQ__READ, &ad);
6006 rc = avc_has_perm(&selinux_state,
6008 SECCLASS_MSG, MSG__RECEIVE, &ad);
6012 /* Shared Memory security operations */
6013 static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
6015 struct ipc_security_struct *isec;
6016 struct common_audit_data ad;
6017 u32 sid = current_sid();
6020 isec = selinux_ipc(shp);
6021 ipc_init_security(isec, SECCLASS_SHM);
6023 ad.type = LSM_AUDIT_DATA_IPC;
6024 ad.u.ipc_id = shp->key;
6026 rc = avc_has_perm(&selinux_state,
6027 sid, isec->sid, SECCLASS_SHM,
6032 static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
6034 struct ipc_security_struct *isec;
6035 struct common_audit_data ad;
6036 u32 sid = current_sid();
6038 isec = selinux_ipc(shp);
6040 ad.type = LSM_AUDIT_DATA_IPC;
6041 ad.u.ipc_id = shp->key;
6043 return avc_has_perm(&selinux_state,
6044 sid, isec->sid, SECCLASS_SHM,
6045 SHM__ASSOCIATE, &ad);
6048 /* Note, at this point, shp is locked down */
6049 static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
6057 /* No specific object, just general system-wide information. */
6058 return avc_has_perm(&selinux_state,
6059 current_sid(), SECINITSID_KERNEL,
6060 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
6064 perms = SHM__GETATTR | SHM__ASSOCIATE;
6067 perms = SHM__SETATTR;
6074 perms = SHM__DESTROY;
6080 err = ipc_has_perm(shp, perms);
6084 static int selinux_shm_shmat(struct kern_ipc_perm *shp,
6085 char __user *shmaddr, int shmflg)
6089 if (shmflg & SHM_RDONLY)
6092 perms = SHM__READ | SHM__WRITE;
6094 return ipc_has_perm(shp, perms);
6097 /* Semaphore security operations */
6098 static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
6100 struct ipc_security_struct *isec;
6101 struct common_audit_data ad;
6102 u32 sid = current_sid();
6105 isec = selinux_ipc(sma);
6106 ipc_init_security(isec, SECCLASS_SEM);
6108 ad.type = LSM_AUDIT_DATA_IPC;
6109 ad.u.ipc_id = sma->key;
6111 rc = avc_has_perm(&selinux_state,
6112 sid, isec->sid, SECCLASS_SEM,
6117 static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
6119 struct ipc_security_struct *isec;
6120 struct common_audit_data ad;
6121 u32 sid = current_sid();
6123 isec = selinux_ipc(sma);
6125 ad.type = LSM_AUDIT_DATA_IPC;
6126 ad.u.ipc_id = sma->key;
6128 return avc_has_perm(&selinux_state,
6129 sid, isec->sid, SECCLASS_SEM,
6130 SEM__ASSOCIATE, &ad);
6133 /* Note, at this point, sma is locked down */
6134 static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
6142 /* No specific object, just general system-wide information. */
6143 return avc_has_perm(&selinux_state,
6144 current_sid(), SECINITSID_KERNEL,
6145 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
6149 perms = SEM__GETATTR;
6160 perms = SEM__DESTROY;
6163 perms = SEM__SETATTR;
6168 perms = SEM__GETATTR | SEM__ASSOCIATE;
6174 err = ipc_has_perm(sma, perms);
6178 static int selinux_sem_semop(struct kern_ipc_perm *sma,
6179 struct sembuf *sops, unsigned nsops, int alter)
6184 perms = SEM__READ | SEM__WRITE;
6188 return ipc_has_perm(sma, perms);
6191 static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6197 av |= IPC__UNIX_READ;
6199 av |= IPC__UNIX_WRITE;
6204 return ipc_has_perm(ipcp, av);
6207 static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6209 struct ipc_security_struct *isec = selinux_ipc(ipcp);
6213 static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
6216 inode_doinit_with_dentry(inode, dentry);
6219 static int selinux_getprocattr(struct task_struct *p,
6220 char *name, char **value)
6222 const struct task_security_struct *__tsec;
6228 __tsec = selinux_cred(__task_cred(p));
6231 error = avc_has_perm(&selinux_state,
6232 current_sid(), __tsec->sid,
6233 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6238 if (!strcmp(name, "current"))
6240 else if (!strcmp(name, "prev"))
6242 else if (!strcmp(name, "exec"))
6243 sid = __tsec->exec_sid;
6244 else if (!strcmp(name, "fscreate"))
6245 sid = __tsec->create_sid;
6246 else if (!strcmp(name, "keycreate"))
6247 sid = __tsec->keycreate_sid;
6248 else if (!strcmp(name, "sockcreate"))
6249 sid = __tsec->sockcreate_sid;
6259 error = security_sid_to_context(&selinux_state, sid, value, &len);
6269 static int selinux_setprocattr(const char *name, void *value, size_t size)
6271 struct task_security_struct *tsec;
6273 u32 mysid = current_sid(), sid = 0, ptsid;
6278 * Basic control over ability to set these attributes at all.
6280 if (!strcmp(name, "exec"))
6281 error = avc_has_perm(&selinux_state,
6282 mysid, mysid, SECCLASS_PROCESS,
6283 PROCESS__SETEXEC, NULL);
6284 else if (!strcmp(name, "fscreate"))
6285 error = avc_has_perm(&selinux_state,
6286 mysid, mysid, SECCLASS_PROCESS,
6287 PROCESS__SETFSCREATE, NULL);
6288 else if (!strcmp(name, "keycreate"))
6289 error = avc_has_perm(&selinux_state,
6290 mysid, mysid, SECCLASS_PROCESS,
6291 PROCESS__SETKEYCREATE, NULL);
6292 else if (!strcmp(name, "sockcreate"))
6293 error = avc_has_perm(&selinux_state,
6294 mysid, mysid, SECCLASS_PROCESS,
6295 PROCESS__SETSOCKCREATE, NULL);
6296 else if (!strcmp(name, "current"))
6297 error = avc_has_perm(&selinux_state,
6298 mysid, mysid, SECCLASS_PROCESS,
6299 PROCESS__SETCURRENT, NULL);
6305 /* Obtain a SID for the context, if one was specified. */
6306 if (size && str[0] && str[0] != '\n') {
6307 if (str[size-1] == '\n') {
6311 error = security_context_to_sid(&selinux_state, value, size,
6313 if (error == -EINVAL && !strcmp(name, "fscreate")) {
6314 if (!has_cap_mac_admin(true)) {
6315 struct audit_buffer *ab;
6318 /* We strip a nul only if it is at the end, otherwise the
6319 * context contains a nul and we should audit that */
6320 if (str[size - 1] == '\0')
6321 audit_size = size - 1;
6324 ab = audit_log_start(audit_context(),
6327 audit_log_format(ab, "op=fscreate invalid_context=");
6328 audit_log_n_untrustedstring(ab, value, audit_size);
6333 error = security_context_to_sid_force(
6341 new = prepare_creds();
6345 /* Permission checking based on the specified context is
6346 performed during the actual operation (execve,
6347 open/mkdir/...), when we know the full context of the
6348 operation. See selinux_bprm_set_creds for the execve
6349 checks and may_create for the file creation checks. The
6350 operation will then fail if the context is not permitted. */
6351 tsec = selinux_cred(new);
6352 if (!strcmp(name, "exec")) {
6353 tsec->exec_sid = sid;
6354 } else if (!strcmp(name, "fscreate")) {
6355 tsec->create_sid = sid;
6356 } else if (!strcmp(name, "keycreate")) {
6357 error = avc_has_perm(&selinux_state,
6358 mysid, sid, SECCLASS_KEY, KEY__CREATE,
6362 tsec->keycreate_sid = sid;
6363 } else if (!strcmp(name, "sockcreate")) {
6364 tsec->sockcreate_sid = sid;
6365 } else if (!strcmp(name, "current")) {
6370 /* Only allow single threaded processes to change context */
6372 if (!current_is_single_threaded()) {
6373 error = security_bounded_transition(&selinux_state,
6379 /* Check permissions for the transition. */
6380 error = avc_has_perm(&selinux_state,
6381 tsec->sid, sid, SECCLASS_PROCESS,
6382 PROCESS__DYNTRANSITION, NULL);
6386 /* Check for ptracing, and update the task SID if ok.
6387 Otherwise, leave SID unchanged and fail. */
6388 ptsid = ptrace_parent_sid();
6390 error = avc_has_perm(&selinux_state,
6391 ptsid, sid, SECCLASS_PROCESS,
6392 PROCESS__PTRACE, NULL);
6411 static int selinux_ismaclabel(const char *name)
6413 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6416 static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6418 return security_sid_to_context(&selinux_state, secid,
6422 static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
6424 return security_context_to_sid(&selinux_state, secdata, seclen,
6428 static void selinux_release_secctx(char *secdata, u32 seclen)
6433 static void selinux_inode_invalidate_secctx(struct inode *inode)
6435 struct inode_security_struct *isec = selinux_inode(inode);
6437 spin_lock(&isec->lock);
6438 isec->initialized = LABEL_INVALID;
6439 spin_unlock(&isec->lock);
6443 * called with inode->i_mutex locked
6445 static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6447 int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
6449 /* Do not return error when suppressing label (SBLABEL_MNT not set). */
6450 return rc == -EOPNOTSUPP ? 0 : rc;
6454 * called with inode->i_mutex locked
6456 static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6458 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6461 static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6464 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6473 static int selinux_key_alloc(struct key *k, const struct cred *cred,
6474 unsigned long flags)
6476 const struct task_security_struct *tsec;
6477 struct key_security_struct *ksec;
6479 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6483 tsec = selinux_cred(cred);
6484 if (tsec->keycreate_sid)
6485 ksec->sid = tsec->keycreate_sid;
6487 ksec->sid = tsec->sid;
6493 static void selinux_key_free(struct key *k)
6495 struct key_security_struct *ksec = k->security;
6501 static int selinux_key_permission(key_ref_t key_ref,
6502 const struct cred *cred,
6506 struct key_security_struct *ksec;
6509 /* if no specific permissions are requested, we skip the
6510 permission check. No serious, additional covert channels
6511 appear to be created. */
6515 sid = cred_sid(cred);
6517 key = key_ref_to_ptr(key_ref);
6518 ksec = key->security;
6520 return avc_has_perm(&selinux_state,
6521 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
6524 static int selinux_key_getsecurity(struct key *key, char **_buffer)
6526 struct key_security_struct *ksec = key->security;
6527 char *context = NULL;
6531 rc = security_sid_to_context(&selinux_state, ksec->sid,
6540 #ifdef CONFIG_SECURITY_INFINIBAND
6541 static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6543 struct common_audit_data ad;
6546 struct ib_security_struct *sec = ib_sec;
6547 struct lsm_ibpkey_audit ibpkey;
6549 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
6553 ad.type = LSM_AUDIT_DATA_IBPKEY;
6554 ibpkey.subnet_prefix = subnet_prefix;
6555 ibpkey.pkey = pkey_val;
6556 ad.u.ibpkey = &ibpkey;
6557 return avc_has_perm(&selinux_state,
6559 SECCLASS_INFINIBAND_PKEY,
6560 INFINIBAND_PKEY__ACCESS, &ad);
6563 static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6566 struct common_audit_data ad;
6569 struct ib_security_struct *sec = ib_sec;
6570 struct lsm_ibendport_audit ibendport;
6572 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6578 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6579 strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6580 ibendport.port = port_num;
6581 ad.u.ibendport = &ibendport;
6582 return avc_has_perm(&selinux_state,
6584 SECCLASS_INFINIBAND_ENDPORT,
6585 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6588 static int selinux_ib_alloc_security(void **ib_sec)
6590 struct ib_security_struct *sec;
6592 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6595 sec->sid = current_sid();
6601 static void selinux_ib_free_security(void *ib_sec)
6607 #ifdef CONFIG_BPF_SYSCALL
6608 static int selinux_bpf(int cmd, union bpf_attr *attr,
6611 u32 sid = current_sid();
6615 case BPF_MAP_CREATE:
6616 ret = avc_has_perm(&selinux_state,
6617 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
6621 ret = avc_has_perm(&selinux_state,
6622 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
6633 static u32 bpf_map_fmode_to_av(fmode_t fmode)
6637 if (fmode & FMODE_READ)
6638 av |= BPF__MAP_READ;
6639 if (fmode & FMODE_WRITE)
6640 av |= BPF__MAP_WRITE;
6644 /* This function will check the file pass through unix socket or binder to see
6645 * if it is a bpf related object. And apply correspinding checks on the bpf
6646 * object based on the type. The bpf maps and programs, not like other files and
6647 * socket, are using a shared anonymous inode inside the kernel as their inode.
6648 * So checking that inode cannot identify if the process have privilege to
6649 * access the bpf object and that's why we have to add this additional check in
6650 * selinux_file_receive and selinux_binder_transfer_files.
6652 static int bpf_fd_pass(struct file *file, u32 sid)
6654 struct bpf_security_struct *bpfsec;
6655 struct bpf_prog *prog;
6656 struct bpf_map *map;
6659 if (file->f_op == &bpf_map_fops) {
6660 map = file->private_data;
6661 bpfsec = map->security;
6662 ret = avc_has_perm(&selinux_state,
6663 sid, bpfsec->sid, SECCLASS_BPF,
6664 bpf_map_fmode_to_av(file->f_mode), NULL);
6667 } else if (file->f_op == &bpf_prog_fops) {
6668 prog = file->private_data;
6669 bpfsec = prog->aux->security;
6670 ret = avc_has_perm(&selinux_state,
6671 sid, bpfsec->sid, SECCLASS_BPF,
6672 BPF__PROG_RUN, NULL);
6679 static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6681 u32 sid = current_sid();
6682 struct bpf_security_struct *bpfsec;
6684 bpfsec = map->security;
6685 return avc_has_perm(&selinux_state,
6686 sid, bpfsec->sid, SECCLASS_BPF,
6687 bpf_map_fmode_to_av(fmode), NULL);
6690 static int selinux_bpf_prog(struct bpf_prog *prog)
6692 u32 sid = current_sid();
6693 struct bpf_security_struct *bpfsec;
6695 bpfsec = prog->aux->security;
6696 return avc_has_perm(&selinux_state,
6697 sid, bpfsec->sid, SECCLASS_BPF,
6698 BPF__PROG_RUN, NULL);
6701 static int selinux_bpf_map_alloc(struct bpf_map *map)
6703 struct bpf_security_struct *bpfsec;
6705 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6709 bpfsec->sid = current_sid();
6710 map->security = bpfsec;
6715 static void selinux_bpf_map_free(struct bpf_map *map)
6717 struct bpf_security_struct *bpfsec = map->security;
6719 map->security = NULL;
6723 static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6725 struct bpf_security_struct *bpfsec;
6727 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6731 bpfsec->sid = current_sid();
6732 aux->security = bpfsec;
6737 static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6739 struct bpf_security_struct *bpfsec = aux->security;
6741 aux->security = NULL;
6746 struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
6747 .lbs_cred = sizeof(struct task_security_struct),
6748 .lbs_file = sizeof(struct file_security_struct),
6749 .lbs_inode = sizeof(struct inode_security_struct),
6750 .lbs_ipc = sizeof(struct ipc_security_struct),
6751 .lbs_msg_msg = sizeof(struct msg_security_struct),
6754 static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
6755 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6756 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6757 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6758 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
6760 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6761 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6762 LSM_HOOK_INIT(capget, selinux_capget),
6763 LSM_HOOK_INIT(capset, selinux_capset),
6764 LSM_HOOK_INIT(capable, selinux_capable),
6765 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6766 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6767 LSM_HOOK_INIT(syslog, selinux_syslog),
6768 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
6770 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
6772 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6773 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6774 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
6776 LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup),
6777 LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),
6779 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6780 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
6781 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
6782 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
6783 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6784 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6785 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6786 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6787 LSM_HOOK_INIT(sb_mount, selinux_mount),
6788 LSM_HOOK_INIT(sb_umount, selinux_umount),
6789 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6790 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
6791 LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt),
6793 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
6794 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
6796 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6797 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6798 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6799 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6800 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6801 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6802 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6803 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6804 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6805 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6806 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6807 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6808 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6809 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6810 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6811 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6812 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6813 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6814 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6815 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6816 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6817 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6818 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6819 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6820 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
6821 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
6822 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
6824 LSM_HOOK_INIT(kernfs_init_security, selinux_kernfs_init_security),
6826 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6827 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6828 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6829 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6830 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6831 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6832 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6833 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6834 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6835 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6836 LSM_HOOK_INIT(file_receive, selinux_file_receive),
6838 LSM_HOOK_INIT(file_open, selinux_file_open),
6840 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
6841 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6842 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
6843 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
6844 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6845 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6846 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
6847 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
6848 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
6849 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6850 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6851 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6852 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6853 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6854 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6855 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
6856 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
6857 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6858 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6859 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6860 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6861 LSM_HOOK_INIT(task_kill, selinux_task_kill),
6862 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
6864 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6865 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
6867 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6869 LSM_HOOK_INIT(msg_queue_alloc_security,
6870 selinux_msg_queue_alloc_security),
6871 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6872 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6873 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6874 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
6876 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6877 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6878 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6879 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
6881 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6882 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6883 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6884 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
6886 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
6888 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6889 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
6891 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6892 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6893 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6894 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
6895 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
6896 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6897 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6898 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
6900 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6901 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
6903 LSM_HOOK_INIT(socket_create, selinux_socket_create),
6904 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
6905 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
6906 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6907 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6908 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6909 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6910 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6911 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6912 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6913 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6914 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6915 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6916 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6917 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6918 LSM_HOOK_INIT(socket_getpeersec_stream,
6919 selinux_socket_getpeersec_stream),
6920 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6921 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6922 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6923 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6924 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6925 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
6926 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
6927 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
6928 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
6929 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6930 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6931 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6932 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6933 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6934 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6935 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6936 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6937 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6938 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6939 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6940 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6941 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
6942 #ifdef CONFIG_SECURITY_INFINIBAND
6943 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
6944 LSM_HOOK_INIT(ib_endport_manage_subnet,
6945 selinux_ib_endport_manage_subnet),
6946 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
6947 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
6949 #ifdef CONFIG_SECURITY_NETWORK_XFRM
6950 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6951 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6952 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6953 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6954 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6955 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6956 selinux_xfrm_state_alloc_acquire),
6957 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6958 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6959 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6960 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6961 selinux_xfrm_state_pol_flow_match),
6962 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
6966 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6967 LSM_HOOK_INIT(key_free, selinux_key_free),
6968 LSM_HOOK_INIT(key_permission, selinux_key_permission),
6969 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
6973 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6974 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6975 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6976 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
6979 #ifdef CONFIG_BPF_SYSCALL
6980 LSM_HOOK_INIT(bpf, selinux_bpf),
6981 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
6982 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
6983 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
6984 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
6985 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
6986 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
6990 static __init int selinux_init(void)
6992 pr_info("SELinux: Initializing.\n");
6994 memset(&selinux_state, 0, sizeof(selinux_state));
6995 enforcing_set(&selinux_state, selinux_enforcing_boot);
6996 selinux_state.checkreqprot = selinux_checkreqprot_boot;
6997 selinux_ss_init(&selinux_state.ss);
6998 selinux_avc_init(&selinux_state.avc);
7000 /* Set the security state for the initial task. */
7001 cred_init_security();
7003 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
7009 ebitmap_cache_init();
7011 hashtab_cache_init();
7013 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
7015 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7016 panic("SELinux: Unable to register AVC netcache callback\n");
7018 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
7019 panic("SELinux: Unable to register AVC LSM notifier callback\n");
7021 if (selinux_enforcing_boot)
7022 pr_debug("SELinux: Starting in enforcing mode\n");
7024 pr_debug("SELinux: Starting in permissive mode\n");
7026 fs_validate_description(&selinux_fs_parameters);
7031 static void delayed_superblock_init(struct super_block *sb, void *unused)
7033 selinux_set_mnt_opts(sb, NULL, 0, NULL);
7036 void selinux_complete_init(void)
7038 pr_debug("SELinux: Completing initialization.\n");
7040 /* Set up any superblocks initialized prior to the policy load. */
7041 pr_debug("SELinux: Setting up existing superblocks.\n");
7042 iterate_supers(delayed_superblock_init, NULL);
7045 /* SELinux requires early initialization in order to label
7046 all processes and objects when they are created. */
7047 DEFINE_LSM(selinux) = {
7049 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
7050 .enabled = &selinux_enabled,
7051 .blobs = &selinux_blob_sizes,
7052 .init = selinux_init,
7055 #if defined(CONFIG_NETFILTER)
7057 static const struct nf_hook_ops selinux_nf_ops[] = {
7059 .hook = selinux_ipv4_postroute,
7061 .hooknum = NF_INET_POST_ROUTING,
7062 .priority = NF_IP_PRI_SELINUX_LAST,
7065 .hook = selinux_ipv4_forward,
7067 .hooknum = NF_INET_FORWARD,
7068 .priority = NF_IP_PRI_SELINUX_FIRST,
7071 .hook = selinux_ipv4_output,
7073 .hooknum = NF_INET_LOCAL_OUT,
7074 .priority = NF_IP_PRI_SELINUX_FIRST,
7076 #if IS_ENABLED(CONFIG_IPV6)
7078 .hook = selinux_ipv6_postroute,
7080 .hooknum = NF_INET_POST_ROUTING,
7081 .priority = NF_IP6_PRI_SELINUX_LAST,
7084 .hook = selinux_ipv6_forward,
7086 .hooknum = NF_INET_FORWARD,
7087 .priority = NF_IP6_PRI_SELINUX_FIRST,
7090 .hook = selinux_ipv6_output,
7092 .hooknum = NF_INET_LOCAL_OUT,
7093 .priority = NF_IP6_PRI_SELINUX_FIRST,
7098 static int __net_init selinux_nf_register(struct net *net)
7100 return nf_register_net_hooks(net, selinux_nf_ops,
7101 ARRAY_SIZE(selinux_nf_ops));
7104 static void __net_exit selinux_nf_unregister(struct net *net)
7106 nf_unregister_net_hooks(net, selinux_nf_ops,
7107 ARRAY_SIZE(selinux_nf_ops));
7110 static struct pernet_operations selinux_net_ops = {
7111 .init = selinux_nf_register,
7112 .exit = selinux_nf_unregister,
7115 static int __init selinux_nf_ip_init(void)
7119 if (!selinux_enabled)
7122 pr_debug("SELinux: Registering netfilter hooks\n");
7124 err = register_pernet_subsys(&selinux_net_ops);
7126 panic("SELinux: register_pernet_subsys: error %d\n", err);
7130 __initcall(selinux_nf_ip_init);
7132 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7133 static void selinux_nf_ip_exit(void)
7135 pr_debug("SELinux: Unregistering netfilter hooks\n");
7137 unregister_pernet_subsys(&selinux_net_ops);
7141 #else /* CONFIG_NETFILTER */
7143 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7144 #define selinux_nf_ip_exit()
7147 #endif /* CONFIG_NETFILTER */
7149 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7150 int selinux_disable(struct selinux_state *state)
7152 if (state->initialized) {
7153 /* Not permitted after initial policy load. */
7157 if (state->disabled) {
7158 /* Only do this once. */
7162 state->disabled = 1;
7164 pr_info("SELinux: Disabled at runtime.\n");
7166 selinux_enabled = 0;
7168 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
7170 /* Try to destroy the avc node cache */
7173 /* Unregister netfilter hooks. */
7174 selinux_nf_ip_exit();
7176 /* Unregister selinuxfs. */