2 * NSA Security-Enhanced Linux (SELinux) security module
4 * This file contains the SELinux hook function implementations.
6 * Authors: Stephen Smalley, <sds@tycho.nsa.gov>
7 * Chris Vance, <cvance@nai.com>
8 * Wayne Salamon, <wsalamon@nai.com>
9 * James Morris <jmorris@redhat.com>
11 * Copyright (C) 2001,2002 Networks Associates Technology, Inc.
12 * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
13 * Eric Paris <eparis@redhat.com>
14 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
15 * <dgoeddel@trustedcs.com>
16 * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
17 * Paul Moore <paul@paul-moore.com>
18 * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
19 * Yuichi Nakamura <ynakam@hitachisoft.jp>
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 <uapi/linux/mount.h>
101 #include "netlabel.h"
105 struct selinux_state selinux_state;
107 /* SECMARK reference count */
108 static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
110 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
111 static int selinux_enforcing_boot;
113 static int __init enforcing_setup(char *str)
115 unsigned long enforcing;
116 if (!kstrtoul(str, 0, &enforcing))
117 selinux_enforcing_boot = enforcing ? 1 : 0;
120 __setup("enforcing=", enforcing_setup);
122 #define selinux_enforcing_boot 1
125 int selinux_enabled __lsm_ro_after_init = 1;
126 #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
127 static int __init selinux_enabled_setup(char *str)
129 unsigned long enabled;
130 if (!kstrtoul(str, 0, &enabled))
131 selinux_enabled = enabled ? 1 : 0;
134 __setup("selinux=", selinux_enabled_setup);
137 static unsigned int selinux_checkreqprot_boot =
138 CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
140 static int __init checkreqprot_setup(char *str)
142 unsigned long checkreqprot;
144 if (!kstrtoul(str, 0, &checkreqprot))
145 selinux_checkreqprot_boot = checkreqprot ? 1 : 0;
148 __setup("checkreqprot=", checkreqprot_setup);
151 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
154 * This function checks the SECMARK reference counter to see if any SECMARK
155 * targets are currently configured, if the reference counter is greater than
156 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
157 * enabled, false (0) if SECMARK is disabled. If the always_check_network
158 * policy capability is enabled, SECMARK is always considered enabled.
161 static int selinux_secmark_enabled(void)
163 return (selinux_policycap_alwaysnetwork() ||
164 atomic_read(&selinux_secmark_refcount));
168 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
171 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
172 * (1) if any are enabled or false (0) if neither are enabled. If the
173 * always_check_network policy capability is enabled, peer labeling
174 * is always considered enabled.
177 static int selinux_peerlbl_enabled(void)
179 return (selinux_policycap_alwaysnetwork() ||
180 netlbl_enabled() || selinux_xfrm_enabled());
183 static int selinux_netcache_avc_callback(u32 event)
185 if (event == AVC_CALLBACK_RESET) {
194 static int selinux_lsm_notifier_avc_callback(u32 event)
196 if (event == AVC_CALLBACK_RESET) {
198 call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
205 * initialise the security for the init task
207 static void cred_init_security(void)
209 struct cred *cred = (struct cred *) current->real_cred;
210 struct task_security_struct *tsec;
212 tsec = selinux_cred(cred);
213 tsec->osid = tsec->sid = SECINITSID_KERNEL;
217 * get the security ID of a set of credentials
219 static inline u32 cred_sid(const struct cred *cred)
221 const struct task_security_struct *tsec;
223 tsec = selinux_cred(cred);
228 * get the objective security ID of a task
230 static inline u32 task_sid(const struct task_struct *task)
235 sid = cred_sid(__task_cred(task));
240 /* Allocate and free functions for each kind of security blob. */
242 static int inode_alloc_security(struct inode *inode)
244 struct inode_security_struct *isec = selinux_inode(inode);
245 u32 sid = current_sid();
247 spin_lock_init(&isec->lock);
248 INIT_LIST_HEAD(&isec->list);
250 isec->sid = SECINITSID_UNLABELED;
251 isec->sclass = SECCLASS_FILE;
252 isec->task_sid = sid;
253 isec->initialized = LABEL_INVALID;
258 static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
261 * Try reloading inode security labels that have been marked as invalid. The
262 * @may_sleep parameter indicates when sleeping and thus reloading labels is
263 * allowed; when set to false, returns -ECHILD when the label is
264 * invalid. The @dentry parameter should be set to a dentry of the inode.
266 static int __inode_security_revalidate(struct inode *inode,
267 struct dentry *dentry,
270 struct inode_security_struct *isec = selinux_inode(inode);
272 might_sleep_if(may_sleep);
274 if (selinux_state.initialized &&
275 isec->initialized != LABEL_INITIALIZED) {
280 * Try reloading the inode security label. This will fail if
281 * @opt_dentry is NULL and no dentry for this inode can be
282 * found; in that case, continue using the old label.
284 inode_doinit_with_dentry(inode, dentry);
289 static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
291 return selinux_inode(inode);
294 static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
298 error = __inode_security_revalidate(inode, NULL, !rcu);
300 return ERR_PTR(error);
301 return selinux_inode(inode);
305 * Get the security label of an inode.
307 static struct inode_security_struct *inode_security(struct inode *inode)
309 __inode_security_revalidate(inode, NULL, true);
310 return selinux_inode(inode);
313 static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
315 struct inode *inode = d_backing_inode(dentry);
317 return selinux_inode(inode);
321 * Get the security label of a dentry's backing inode.
323 static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
325 struct inode *inode = d_backing_inode(dentry);
327 __inode_security_revalidate(inode, dentry, true);
328 return selinux_inode(inode);
331 static void inode_free_security(struct inode *inode)
333 struct inode_security_struct *isec = selinux_inode(inode);
334 struct superblock_security_struct *sbsec;
338 sbsec = inode->i_sb->s_security;
340 * As not all inode security structures are in a list, we check for
341 * empty list outside of the lock to make sure that we won't waste
342 * time taking a lock doing nothing.
344 * The list_del_init() function can be safely called more than once.
345 * It should not be possible for this function to be called with
346 * concurrent list_add(), but for better safety against future changes
347 * in the code, we use list_empty_careful() here.
349 if (!list_empty_careful(&isec->list)) {
350 spin_lock(&sbsec->isec_lock);
351 list_del_init(&isec->list);
352 spin_unlock(&sbsec->isec_lock);
356 static int file_alloc_security(struct file *file)
358 struct file_security_struct *fsec = selinux_file(file);
359 u32 sid = current_sid();
362 fsec->fown_sid = sid;
367 static int superblock_alloc_security(struct super_block *sb)
369 struct superblock_security_struct *sbsec;
371 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
375 mutex_init(&sbsec->lock);
376 INIT_LIST_HEAD(&sbsec->isec_head);
377 spin_lock_init(&sbsec->isec_lock);
379 sbsec->sid = SECINITSID_UNLABELED;
380 sbsec->def_sid = SECINITSID_FILE;
381 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
382 sb->s_security = sbsec;
387 static void superblock_free_security(struct super_block *sb)
389 struct superblock_security_struct *sbsec = sb->s_security;
390 sb->s_security = NULL;
394 struct selinux_mnt_opts {
395 const char *fscontext, *context, *rootcontext, *defcontext;
398 static void selinux_free_mnt_opts(void *mnt_opts)
400 struct selinux_mnt_opts *opts = mnt_opts;
401 kfree(opts->fscontext);
402 kfree(opts->context);
403 kfree(opts->rootcontext);
404 kfree(opts->defcontext);
408 static inline int inode_doinit(struct inode *inode)
410 return inode_doinit_with_dentry(inode, NULL);
422 #define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
432 A(rootcontext, true),
437 static int match_opt_prefix(char *s, int l, char **arg)
441 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
442 size_t len = tokens[i].len;
443 if (len > l || memcmp(s, tokens[i].name, len))
445 if (tokens[i].has_arg) {
446 if (len == l || s[len] != '=')
451 return tokens[i].opt;
456 #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
458 static int may_context_mount_sb_relabel(u32 sid,
459 struct superblock_security_struct *sbsec,
460 const struct cred *cred)
462 const struct task_security_struct *tsec = selinux_cred(cred);
465 rc = avc_has_perm(&selinux_state,
466 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
467 FILESYSTEM__RELABELFROM, NULL);
471 rc = avc_has_perm(&selinux_state,
472 tsec->sid, sid, SECCLASS_FILESYSTEM,
473 FILESYSTEM__RELABELTO, NULL);
477 static int may_context_mount_inode_relabel(u32 sid,
478 struct superblock_security_struct *sbsec,
479 const struct cred *cred)
481 const struct task_security_struct *tsec = selinux_cred(cred);
483 rc = avc_has_perm(&selinux_state,
484 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
485 FILESYSTEM__RELABELFROM, NULL);
489 rc = avc_has_perm(&selinux_state,
490 sid, sbsec->sid, SECCLASS_FILESYSTEM,
491 FILESYSTEM__ASSOCIATE, NULL);
495 static int selinux_is_genfs_special_handling(struct super_block *sb)
497 /* Special handling. Genfs but also in-core setxattr handler */
498 return !strcmp(sb->s_type->name, "sysfs") ||
499 !strcmp(sb->s_type->name, "pstore") ||
500 !strcmp(sb->s_type->name, "debugfs") ||
501 !strcmp(sb->s_type->name, "tracefs") ||
502 !strcmp(sb->s_type->name, "rootfs") ||
503 (selinux_policycap_cgroupseclabel() &&
504 (!strcmp(sb->s_type->name, "cgroup") ||
505 !strcmp(sb->s_type->name, "cgroup2")));
508 static int selinux_is_sblabel_mnt(struct super_block *sb)
510 struct superblock_security_struct *sbsec = sb->s_security;
513 * IMPORTANT: Double-check logic in this function when adding a new
514 * SECURITY_FS_USE_* definition!
516 BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7);
518 switch (sbsec->behavior) {
519 case SECURITY_FS_USE_XATTR:
520 case SECURITY_FS_USE_TRANS:
521 case SECURITY_FS_USE_TASK:
522 case SECURITY_FS_USE_NATIVE:
525 case SECURITY_FS_USE_GENFS:
526 return selinux_is_genfs_special_handling(sb);
528 /* Never allow relabeling on context mounts */
529 case SECURITY_FS_USE_MNTPOINT:
530 case SECURITY_FS_USE_NONE:
536 static int sb_finish_set_opts(struct super_block *sb)
538 struct superblock_security_struct *sbsec = sb->s_security;
539 struct dentry *root = sb->s_root;
540 struct inode *root_inode = d_backing_inode(root);
543 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
544 /* Make sure that the xattr handler exists and that no
545 error other than -ENODATA is returned by getxattr on
546 the root directory. -ENODATA is ok, as this may be
547 the first boot of the SELinux kernel before we have
548 assigned xattr values to the filesystem. */
549 if (!(root_inode->i_opflags & IOP_XATTR)) {
550 pr_warn("SELinux: (dev %s, type %s) has no "
551 "xattr support\n", sb->s_id, sb->s_type->name);
556 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
557 if (rc < 0 && rc != -ENODATA) {
558 if (rc == -EOPNOTSUPP)
559 pr_warn("SELinux: (dev %s, type "
560 "%s) has no security xattr handler\n",
561 sb->s_id, sb->s_type->name);
563 pr_warn("SELinux: (dev %s, type "
564 "%s) getxattr errno %d\n", sb->s_id,
565 sb->s_type->name, -rc);
570 sbsec->flags |= SE_SBINITIALIZED;
573 * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply
574 * leave the flag untouched because sb_clone_mnt_opts might be handing
575 * us a superblock that needs the flag to be cleared.
577 if (selinux_is_sblabel_mnt(sb))
578 sbsec->flags |= SBLABEL_MNT;
580 sbsec->flags &= ~SBLABEL_MNT;
582 /* Initialize the root inode. */
583 rc = inode_doinit_with_dentry(root_inode, root);
585 /* Initialize any other inodes associated with the superblock, e.g.
586 inodes created prior to initial policy load or inodes created
587 during get_sb by a pseudo filesystem that directly
589 spin_lock(&sbsec->isec_lock);
590 while (!list_empty(&sbsec->isec_head)) {
591 struct inode_security_struct *isec =
592 list_first_entry(&sbsec->isec_head,
593 struct inode_security_struct, list);
594 struct inode *inode = isec->inode;
595 list_del_init(&isec->list);
596 spin_unlock(&sbsec->isec_lock);
597 inode = igrab(inode);
599 if (!IS_PRIVATE(inode))
603 spin_lock(&sbsec->isec_lock);
605 spin_unlock(&sbsec->isec_lock);
610 static int bad_option(struct superblock_security_struct *sbsec, char flag,
611 u32 old_sid, u32 new_sid)
613 char mnt_flags = sbsec->flags & SE_MNTMASK;
615 /* check if the old mount command had the same options */
616 if (sbsec->flags & SE_SBINITIALIZED)
617 if (!(sbsec->flags & flag) ||
618 (old_sid != new_sid))
621 /* check if we were passed the same options twice,
622 * aka someone passed context=a,context=b
624 if (!(sbsec->flags & SE_SBINITIALIZED))
625 if (mnt_flags & flag)
630 static int parse_sid(struct super_block *sb, const char *s, u32 *sid)
632 int rc = security_context_str_to_sid(&selinux_state, s,
635 pr_warn("SELinux: security_context_str_to_sid"
636 "(%s) failed for (dev %s, type %s) errno=%d\n",
637 s, sb->s_id, sb->s_type->name, rc);
642 * Allow filesystems with binary mount data to explicitly set mount point
643 * labeling information.
645 static int selinux_set_mnt_opts(struct super_block *sb,
647 unsigned long kern_flags,
648 unsigned long *set_kern_flags)
650 const struct cred *cred = current_cred();
651 struct superblock_security_struct *sbsec = sb->s_security;
652 struct dentry *root = sbsec->sb->s_root;
653 struct selinux_mnt_opts *opts = mnt_opts;
654 struct inode_security_struct *root_isec;
655 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
656 u32 defcontext_sid = 0;
659 mutex_lock(&sbsec->lock);
661 if (!selinux_state.initialized) {
663 /* Defer initialization until selinux_complete_init,
664 after the initial policy is loaded and the security
665 server is ready to handle calls. */
669 pr_warn("SELinux: Unable to set superblock options "
670 "before the security server is initialized\n");
673 if (kern_flags && !set_kern_flags) {
674 /* Specifying internal flags without providing a place to
675 * place the results is not allowed */
681 * Binary mount data FS will come through this function twice. Once
682 * from an explicit call and once from the generic calls from the vfs.
683 * Since the generic VFS calls will not contain any security mount data
684 * we need to skip the double mount verification.
686 * This does open a hole in which we will not notice if the first
687 * mount using this sb set explict options and a second mount using
688 * this sb does not set any security options. (The first options
689 * will be used for both mounts)
691 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
695 root_isec = backing_inode_security_novalidate(root);
698 * parse the mount options, check if they are valid sids.
699 * also check if someone is trying to mount the same sb more
700 * than once with different security options.
703 if (opts->fscontext) {
704 rc = parse_sid(sb, opts->fscontext, &fscontext_sid);
707 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
709 goto out_double_mount;
710 sbsec->flags |= FSCONTEXT_MNT;
713 rc = parse_sid(sb, opts->context, &context_sid);
716 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
718 goto out_double_mount;
719 sbsec->flags |= CONTEXT_MNT;
721 if (opts->rootcontext) {
722 rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid);
725 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
727 goto out_double_mount;
728 sbsec->flags |= ROOTCONTEXT_MNT;
730 if (opts->defcontext) {
731 rc = parse_sid(sb, opts->defcontext, &defcontext_sid);
734 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
736 goto out_double_mount;
737 sbsec->flags |= DEFCONTEXT_MNT;
741 if (sbsec->flags & SE_SBINITIALIZED) {
742 /* previously mounted with options, but not on this attempt? */
743 if ((sbsec->flags & SE_MNTMASK) && !opts)
744 goto out_double_mount;
749 if (strcmp(sb->s_type->name, "proc") == 0)
750 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
752 if (!strcmp(sb->s_type->name, "debugfs") ||
753 !strcmp(sb->s_type->name, "tracefs") ||
754 !strcmp(sb->s_type->name, "sysfs") ||
755 !strcmp(sb->s_type->name, "pstore") ||
756 !strcmp(sb->s_type->name, "cgroup") ||
757 !strcmp(sb->s_type->name, "cgroup2"))
758 sbsec->flags |= SE_SBGENFS;
760 if (!sbsec->behavior) {
762 * Determine the labeling behavior to use for this
765 rc = security_fs_use(&selinux_state, sb);
767 pr_warn("%s: security_fs_use(%s) returned %d\n",
768 __func__, sb->s_type->name, rc);
774 * If this is a user namespace mount and the filesystem type is not
775 * explicitly whitelisted, then no contexts are allowed on the command
776 * line and security labels must be ignored.
778 if (sb->s_user_ns != &init_user_ns &&
779 strcmp(sb->s_type->name, "tmpfs") &&
780 strcmp(sb->s_type->name, "ramfs") &&
781 strcmp(sb->s_type->name, "devpts")) {
782 if (context_sid || fscontext_sid || rootcontext_sid ||
787 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
788 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
789 rc = security_transition_sid(&selinux_state,
793 &sbsec->mntpoint_sid);
800 /* sets the context of the superblock for the fs being mounted. */
802 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
806 sbsec->sid = fscontext_sid;
810 * Switch to using mount point labeling behavior.
811 * sets the label used on all file below the mountpoint, and will set
812 * the superblock context if not already set.
814 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
815 sbsec->behavior = SECURITY_FS_USE_NATIVE;
816 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
820 if (!fscontext_sid) {
821 rc = may_context_mount_sb_relabel(context_sid, sbsec,
825 sbsec->sid = context_sid;
827 rc = may_context_mount_inode_relabel(context_sid, sbsec,
832 if (!rootcontext_sid)
833 rootcontext_sid = context_sid;
835 sbsec->mntpoint_sid = context_sid;
836 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
839 if (rootcontext_sid) {
840 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
845 root_isec->sid = rootcontext_sid;
846 root_isec->initialized = LABEL_INITIALIZED;
849 if (defcontext_sid) {
850 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
851 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
853 pr_warn("SELinux: defcontext option is "
854 "invalid for this filesystem type\n");
858 if (defcontext_sid != sbsec->def_sid) {
859 rc = may_context_mount_inode_relabel(defcontext_sid,
865 sbsec->def_sid = defcontext_sid;
869 rc = sb_finish_set_opts(sb);
871 mutex_unlock(&sbsec->lock);
875 pr_warn("SELinux: mount invalid. Same superblock, different "
876 "security settings for (dev %s, type %s)\n", sb->s_id,
881 static int selinux_cmp_sb_context(const struct super_block *oldsb,
882 const struct super_block *newsb)
884 struct superblock_security_struct *old = oldsb->s_security;
885 struct superblock_security_struct *new = newsb->s_security;
886 char oldflags = old->flags & SE_MNTMASK;
887 char newflags = new->flags & SE_MNTMASK;
889 if (oldflags != newflags)
891 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
893 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
895 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
897 if (oldflags & ROOTCONTEXT_MNT) {
898 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
899 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
900 if (oldroot->sid != newroot->sid)
905 pr_warn("SELinux: mount invalid. Same superblock, "
906 "different security settings for (dev %s, "
907 "type %s)\n", newsb->s_id, newsb->s_type->name);
911 static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
912 struct super_block *newsb,
913 unsigned long kern_flags,
914 unsigned long *set_kern_flags)
917 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
918 struct superblock_security_struct *newsbsec = newsb->s_security;
920 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
921 int set_context = (oldsbsec->flags & CONTEXT_MNT);
922 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
925 * if the parent was able to be mounted it clearly had no special lsm
926 * mount options. thus we can safely deal with this superblock later
928 if (!selinux_state.initialized)
932 * Specifying internal flags without providing a place to
933 * place the results is not allowed.
935 if (kern_flags && !set_kern_flags)
938 /* how can we clone if the old one wasn't set up?? */
939 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
941 /* if fs is reusing a sb, make sure that the contexts match */
942 if (newsbsec->flags & SE_SBINITIALIZED)
943 return selinux_cmp_sb_context(oldsb, newsb);
945 mutex_lock(&newsbsec->lock);
947 newsbsec->flags = oldsbsec->flags;
949 newsbsec->sid = oldsbsec->sid;
950 newsbsec->def_sid = oldsbsec->def_sid;
951 newsbsec->behavior = oldsbsec->behavior;
953 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
954 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
955 rc = security_fs_use(&selinux_state, newsb);
960 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
961 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
962 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
966 u32 sid = oldsbsec->mntpoint_sid;
970 if (!set_rootcontext) {
971 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
974 newsbsec->mntpoint_sid = sid;
976 if (set_rootcontext) {
977 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
978 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
980 newisec->sid = oldisec->sid;
983 sb_finish_set_opts(newsb);
985 mutex_unlock(&newsbsec->lock);
989 static int selinux_add_opt(int token, const char *s, void **mnt_opts)
991 struct selinux_mnt_opts *opts = *mnt_opts;
993 if (token == Opt_seclabel) /* eaten and completely ignored */
997 opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
1006 if (opts->context || opts->defcontext)
1011 if (opts->fscontext)
1013 opts->fscontext = s;
1015 case Opt_rootcontext:
1016 if (opts->rootcontext)
1018 opts->rootcontext = s;
1020 case Opt_defcontext:
1021 if (opts->context || opts->defcontext)
1023 opts->defcontext = s;
1028 pr_warn(SEL_MOUNT_FAIL_MSG);
1032 static int selinux_add_mnt_opt(const char *option, const char *val, int len,
1035 int token = Opt_error;
1038 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
1039 if (strcmp(option, tokens[i].name) == 0) {
1040 token = tokens[i].opt;
1045 if (token == Opt_error)
1048 if (token != Opt_seclabel)
1049 val = kmemdup_nul(val, len, GFP_KERNEL);
1050 rc = selinux_add_opt(token, val, mnt_opts);
1054 selinux_free_mnt_opts(*mnt_opts);
1061 static int show_sid(struct seq_file *m, u32 sid)
1063 char *context = NULL;
1067 rc = security_sid_to_context(&selinux_state, sid,
1070 bool has_comma = context && strchr(context, ',');
1075 seq_escape(m, context, "\"\n\\");
1083 static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1085 struct superblock_security_struct *sbsec = sb->s_security;
1088 if (!(sbsec->flags & SE_SBINITIALIZED))
1091 if (!selinux_state.initialized)
1094 if (sbsec->flags & FSCONTEXT_MNT) {
1096 seq_puts(m, FSCONTEXT_STR);
1097 rc = show_sid(m, sbsec->sid);
1101 if (sbsec->flags & CONTEXT_MNT) {
1103 seq_puts(m, CONTEXT_STR);
1104 rc = show_sid(m, sbsec->mntpoint_sid);
1108 if (sbsec->flags & DEFCONTEXT_MNT) {
1110 seq_puts(m, DEFCONTEXT_STR);
1111 rc = show_sid(m, sbsec->def_sid);
1115 if (sbsec->flags & ROOTCONTEXT_MNT) {
1116 struct dentry *root = sbsec->sb->s_root;
1117 struct inode_security_struct *isec = backing_inode_security(root);
1119 seq_puts(m, ROOTCONTEXT_STR);
1120 rc = show_sid(m, isec->sid);
1124 if (sbsec->flags & SBLABEL_MNT) {
1126 seq_puts(m, SECLABEL_STR);
1131 static inline u16 inode_mode_to_security_class(umode_t mode)
1133 switch (mode & S_IFMT) {
1135 return SECCLASS_SOCK_FILE;
1137 return SECCLASS_LNK_FILE;
1139 return SECCLASS_FILE;
1141 return SECCLASS_BLK_FILE;
1143 return SECCLASS_DIR;
1145 return SECCLASS_CHR_FILE;
1147 return SECCLASS_FIFO_FILE;
1151 return SECCLASS_FILE;
1154 static inline int default_protocol_stream(int protocol)
1156 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1159 static inline int default_protocol_dgram(int protocol)
1161 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1164 static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1166 int extsockclass = selinux_policycap_extsockclass();
1172 case SOCK_SEQPACKET:
1173 return SECCLASS_UNIX_STREAM_SOCKET;
1176 return SECCLASS_UNIX_DGRAM_SOCKET;
1183 case SOCK_SEQPACKET:
1184 if (default_protocol_stream(protocol))
1185 return SECCLASS_TCP_SOCKET;
1186 else if (extsockclass && protocol == IPPROTO_SCTP)
1187 return SECCLASS_SCTP_SOCKET;
1189 return SECCLASS_RAWIP_SOCKET;
1191 if (default_protocol_dgram(protocol))
1192 return SECCLASS_UDP_SOCKET;
1193 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1194 protocol == IPPROTO_ICMPV6))
1195 return SECCLASS_ICMP_SOCKET;
1197 return SECCLASS_RAWIP_SOCKET;
1199 return SECCLASS_DCCP_SOCKET;
1201 return SECCLASS_RAWIP_SOCKET;
1207 return SECCLASS_NETLINK_ROUTE_SOCKET;
1208 case NETLINK_SOCK_DIAG:
1209 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1211 return SECCLASS_NETLINK_NFLOG_SOCKET;
1213 return SECCLASS_NETLINK_XFRM_SOCKET;
1214 case NETLINK_SELINUX:
1215 return SECCLASS_NETLINK_SELINUX_SOCKET;
1217 return SECCLASS_NETLINK_ISCSI_SOCKET;
1219 return SECCLASS_NETLINK_AUDIT_SOCKET;
1220 case NETLINK_FIB_LOOKUP:
1221 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1222 case NETLINK_CONNECTOR:
1223 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1224 case NETLINK_NETFILTER:
1225 return SECCLASS_NETLINK_NETFILTER_SOCKET;
1226 case NETLINK_DNRTMSG:
1227 return SECCLASS_NETLINK_DNRT_SOCKET;
1228 case NETLINK_KOBJECT_UEVENT:
1229 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
1230 case NETLINK_GENERIC:
1231 return SECCLASS_NETLINK_GENERIC_SOCKET;
1232 case NETLINK_SCSITRANSPORT:
1233 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1235 return SECCLASS_NETLINK_RDMA_SOCKET;
1236 case NETLINK_CRYPTO:
1237 return SECCLASS_NETLINK_CRYPTO_SOCKET;
1239 return SECCLASS_NETLINK_SOCKET;
1242 return SECCLASS_PACKET_SOCKET;
1244 return SECCLASS_KEY_SOCKET;
1246 return SECCLASS_APPLETALK_SOCKET;
1252 return SECCLASS_AX25_SOCKET;
1254 return SECCLASS_IPX_SOCKET;
1256 return SECCLASS_NETROM_SOCKET;
1258 return SECCLASS_ATMPVC_SOCKET;
1260 return SECCLASS_X25_SOCKET;
1262 return SECCLASS_ROSE_SOCKET;
1264 return SECCLASS_DECNET_SOCKET;
1266 return SECCLASS_ATMSVC_SOCKET;
1268 return SECCLASS_RDS_SOCKET;
1270 return SECCLASS_IRDA_SOCKET;
1272 return SECCLASS_PPPOX_SOCKET;
1274 return SECCLASS_LLC_SOCKET;
1276 return SECCLASS_CAN_SOCKET;
1278 return SECCLASS_TIPC_SOCKET;
1280 return SECCLASS_BLUETOOTH_SOCKET;
1282 return SECCLASS_IUCV_SOCKET;
1284 return SECCLASS_RXRPC_SOCKET;
1286 return SECCLASS_ISDN_SOCKET;
1288 return SECCLASS_PHONET_SOCKET;
1290 return SECCLASS_IEEE802154_SOCKET;
1292 return SECCLASS_CAIF_SOCKET;
1294 return SECCLASS_ALG_SOCKET;
1296 return SECCLASS_NFC_SOCKET;
1298 return SECCLASS_VSOCK_SOCKET;
1300 return SECCLASS_KCM_SOCKET;
1302 return SECCLASS_QIPCRTR_SOCKET;
1304 return SECCLASS_SMC_SOCKET;
1306 return SECCLASS_XDP_SOCKET;
1308 #error New address family defined, please update this function.
1313 return SECCLASS_SOCKET;
1316 static int selinux_genfs_get_sid(struct dentry *dentry,
1322 struct super_block *sb = dentry->d_sb;
1323 char *buffer, *path;
1325 buffer = (char *)__get_free_page(GFP_KERNEL);
1329 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1333 if (flags & SE_SBPROC) {
1334 /* each process gets a /proc/PID/ entry. Strip off the
1335 * PID part to get a valid selinux labeling.
1336 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1337 while (path[1] >= '0' && path[1] <= '9') {
1342 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1344 if (rc == -ENOENT) {
1345 /* No match in policy, mark as unlabeled. */
1346 *sid = SECINITSID_UNLABELED;
1350 free_page((unsigned long)buffer);
1354 /* The inode's security attributes must be initialized before first use. */
1355 static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1357 struct superblock_security_struct *sbsec = NULL;
1358 struct inode_security_struct *isec = selinux_inode(inode);
1359 u32 task_sid, sid = 0;
1361 struct dentry *dentry;
1362 #define INITCONTEXTLEN 255
1363 char *context = NULL;
1367 if (isec->initialized == LABEL_INITIALIZED)
1370 spin_lock(&isec->lock);
1371 if (isec->initialized == LABEL_INITIALIZED)
1374 if (isec->sclass == SECCLASS_FILE)
1375 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1377 sbsec = inode->i_sb->s_security;
1378 if (!(sbsec->flags & SE_SBINITIALIZED)) {
1379 /* Defer initialization until selinux_complete_init,
1380 after the initial policy is loaded and the security
1381 server is ready to handle calls. */
1382 spin_lock(&sbsec->isec_lock);
1383 if (list_empty(&isec->list))
1384 list_add(&isec->list, &sbsec->isec_head);
1385 spin_unlock(&sbsec->isec_lock);
1389 sclass = isec->sclass;
1390 task_sid = isec->task_sid;
1392 isec->initialized = LABEL_PENDING;
1393 spin_unlock(&isec->lock);
1395 switch (sbsec->behavior) {
1396 case SECURITY_FS_USE_NATIVE:
1398 case SECURITY_FS_USE_XATTR:
1399 if (!(inode->i_opflags & IOP_XATTR)) {
1400 sid = sbsec->def_sid;
1403 /* Need a dentry, since the xattr API requires one.
1404 Life would be simpler if we could just pass the inode. */
1406 /* Called from d_instantiate or d_splice_alias. */
1407 dentry = dget(opt_dentry);
1410 * Called from selinux_complete_init, try to find a dentry.
1411 * Some filesystems really want a connected one, so try
1412 * that first. We could split SECURITY_FS_USE_XATTR in
1413 * two, depending upon that...
1415 dentry = d_find_alias(inode);
1417 dentry = d_find_any_alias(inode);
1421 * this is can be hit on boot when a file is accessed
1422 * before the policy is loaded. When we load policy we
1423 * may find inodes that have no dentry on the
1424 * sbsec->isec_head list. No reason to complain as these
1425 * will get fixed up the next time we go through
1426 * inode_doinit with a dentry, before these inodes could
1427 * be used again by userspace.
1432 len = INITCONTEXTLEN;
1433 context = kmalloc(len+1, GFP_NOFS);
1439 context[len] = '\0';
1440 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
1441 if (rc == -ERANGE) {
1444 /* Need a larger buffer. Query for the right size. */
1445 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
1451 context = kmalloc(len+1, GFP_NOFS);
1457 context[len] = '\0';
1458 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
1462 if (rc != -ENODATA) {
1463 pr_warn("SELinux: %s: getxattr returned "
1464 "%d for dev=%s ino=%ld\n", __func__,
1465 -rc, inode->i_sb->s_id, inode->i_ino);
1469 /* Map ENODATA to the default file SID */
1470 sid = sbsec->def_sid;
1473 rc = security_context_to_sid_default(&selinux_state,
1478 char *dev = inode->i_sb->s_id;
1479 unsigned long ino = inode->i_ino;
1481 if (rc == -EINVAL) {
1482 if (printk_ratelimit())
1483 pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid "
1484 "context=%s. This indicates you may need to relabel the inode or the "
1485 "filesystem in question.\n", ino, dev, context);
1487 pr_warn("SELinux: %s: context_to_sid(%s) "
1488 "returned %d for dev=%s ino=%ld\n",
1489 __func__, context, -rc, dev, ino);
1492 /* Leave with the unlabeled SID */
1499 case SECURITY_FS_USE_TASK:
1502 case SECURITY_FS_USE_TRANS:
1503 /* Default to the fs SID. */
1506 /* Try to obtain a transition SID. */
1507 rc = security_transition_sid(&selinux_state, task_sid, sid,
1508 sclass, NULL, &sid);
1512 case SECURITY_FS_USE_MNTPOINT:
1513 sid = sbsec->mntpoint_sid;
1516 /* Default to the fs superblock SID. */
1519 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
1520 /* We must have a dentry to determine the label on
1523 /* Called from d_instantiate or
1524 * d_splice_alias. */
1525 dentry = dget(opt_dentry);
1527 /* Called from selinux_complete_init, try to
1528 * find a dentry. Some filesystems really want
1529 * a connected one, so try that first.
1531 dentry = d_find_alias(inode);
1533 dentry = d_find_any_alias(inode);
1536 * This can be hit on boot when a file is accessed
1537 * before the policy is loaded. When we load policy we
1538 * may find inodes that have no dentry on the
1539 * sbsec->isec_head list. No reason to complain as
1540 * these will get fixed up the next time we go through
1541 * inode_doinit() with a dentry, before these inodes
1542 * could be used again by userspace.
1546 rc = selinux_genfs_get_sid(dentry, sclass,
1547 sbsec->flags, &sid);
1556 spin_lock(&isec->lock);
1557 if (isec->initialized == LABEL_PENDING) {
1559 isec->initialized = LABEL_INVALID;
1563 isec->initialized = LABEL_INITIALIZED;
1568 spin_unlock(&isec->lock);
1572 /* Convert a Linux signal to an access vector. */
1573 static inline u32 signal_to_av(int sig)
1579 /* Commonly granted from child to parent. */
1580 perm = PROCESS__SIGCHLD;
1583 /* Cannot be caught or ignored */
1584 perm = PROCESS__SIGKILL;
1587 /* Cannot be caught or ignored */
1588 perm = PROCESS__SIGSTOP;
1591 /* All other signals. */
1592 perm = PROCESS__SIGNAL;
1599 #if CAP_LAST_CAP > 63
1600 #error Fix SELinux to handle capabilities > 63.
1603 /* Check whether a task is allowed to use a capability. */
1604 static int cred_has_capability(const struct cred *cred,
1605 int cap, unsigned int opts, bool initns)
1607 struct common_audit_data ad;
1608 struct av_decision avd;
1610 u32 sid = cred_sid(cred);
1611 u32 av = CAP_TO_MASK(cap);
1614 ad.type = LSM_AUDIT_DATA_CAP;
1617 switch (CAP_TO_INDEX(cap)) {
1619 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
1622 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
1625 pr_err("SELinux: out of range capability %d\n", cap);
1630 rc = avc_has_perm_noaudit(&selinux_state,
1631 sid, sid, sclass, av, 0, &avd);
1632 if (!(opts & CAP_OPT_NOAUDIT)) {
1633 int rc2 = avc_audit(&selinux_state,
1634 sid, sid, sclass, av, &avd, rc, &ad, 0);
1641 /* Check whether a task has a particular permission to an inode.
1642 The 'adp' parameter is optional and allows other audit
1643 data to be passed (e.g. the dentry). */
1644 static int inode_has_perm(const struct cred *cred,
1645 struct inode *inode,
1647 struct common_audit_data *adp)
1649 struct inode_security_struct *isec;
1652 validate_creds(cred);
1654 if (unlikely(IS_PRIVATE(inode)))
1657 sid = cred_sid(cred);
1658 isec = selinux_inode(inode);
1660 return avc_has_perm(&selinux_state,
1661 sid, isec->sid, isec->sclass, perms, adp);
1664 /* Same as inode_has_perm, but pass explicit audit data containing
1665 the dentry to help the auditing code to more easily generate the
1666 pathname if needed. */
1667 static inline int dentry_has_perm(const struct cred *cred,
1668 struct dentry *dentry,
1671 struct inode *inode = d_backing_inode(dentry);
1672 struct common_audit_data ad;
1674 ad.type = LSM_AUDIT_DATA_DENTRY;
1675 ad.u.dentry = dentry;
1676 __inode_security_revalidate(inode, dentry, true);
1677 return inode_has_perm(cred, inode, av, &ad);
1680 /* Same as inode_has_perm, but pass explicit audit data containing
1681 the path to help the auditing code to more easily generate the
1682 pathname if needed. */
1683 static inline int path_has_perm(const struct cred *cred,
1684 const struct path *path,
1687 struct inode *inode = d_backing_inode(path->dentry);
1688 struct common_audit_data ad;
1690 ad.type = LSM_AUDIT_DATA_PATH;
1692 __inode_security_revalidate(inode, path->dentry, true);
1693 return inode_has_perm(cred, inode, av, &ad);
1696 /* Same as path_has_perm, but uses the inode from the file struct. */
1697 static inline int file_path_has_perm(const struct cred *cred,
1701 struct common_audit_data ad;
1703 ad.type = LSM_AUDIT_DATA_FILE;
1705 return inode_has_perm(cred, file_inode(file), av, &ad);
1708 #ifdef CONFIG_BPF_SYSCALL
1709 static int bpf_fd_pass(struct file *file, u32 sid);
1712 /* Check whether a task can use an open file descriptor to
1713 access an inode in a given way. Check access to the
1714 descriptor itself, and then use dentry_has_perm to
1715 check a particular permission to the file.
1716 Access to the descriptor is implicitly granted if it
1717 has the same SID as the process. If av is zero, then
1718 access to the file is not checked, e.g. for cases
1719 where only the descriptor is affected like seek. */
1720 static int file_has_perm(const struct cred *cred,
1724 struct file_security_struct *fsec = selinux_file(file);
1725 struct inode *inode = file_inode(file);
1726 struct common_audit_data ad;
1727 u32 sid = cred_sid(cred);
1730 ad.type = LSM_AUDIT_DATA_FILE;
1733 if (sid != fsec->sid) {
1734 rc = avc_has_perm(&selinux_state,
1743 #ifdef CONFIG_BPF_SYSCALL
1744 rc = bpf_fd_pass(file, cred_sid(cred));
1749 /* av is zero if only checking access to the descriptor. */
1752 rc = inode_has_perm(cred, inode, av, &ad);
1759 * Determine the label for an inode that might be unioned.
1762 selinux_determine_inode_label(const struct task_security_struct *tsec,
1764 const struct qstr *name, u16 tclass,
1767 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
1769 if ((sbsec->flags & SE_SBINITIALIZED) &&
1770 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1771 *_new_isid = sbsec->mntpoint_sid;
1772 } else if ((sbsec->flags & SBLABEL_MNT) &&
1774 *_new_isid = tsec->create_sid;
1776 const struct inode_security_struct *dsec = inode_security(dir);
1777 return security_transition_sid(&selinux_state, tsec->sid,
1785 /* Check whether a task can create a file. */
1786 static int may_create(struct inode *dir,
1787 struct dentry *dentry,
1790 const struct task_security_struct *tsec = selinux_cred(current_cred());
1791 struct inode_security_struct *dsec;
1792 struct superblock_security_struct *sbsec;
1794 struct common_audit_data ad;
1797 dsec = inode_security(dir);
1798 sbsec = dir->i_sb->s_security;
1802 ad.type = LSM_AUDIT_DATA_DENTRY;
1803 ad.u.dentry = dentry;
1805 rc = avc_has_perm(&selinux_state,
1806 sid, dsec->sid, SECCLASS_DIR,
1807 DIR__ADD_NAME | DIR__SEARCH,
1812 rc = selinux_determine_inode_label(selinux_cred(current_cred()), dir,
1813 &dentry->d_name, tclass, &newsid);
1817 rc = avc_has_perm(&selinux_state,
1818 sid, newsid, tclass, FILE__CREATE, &ad);
1822 return avc_has_perm(&selinux_state,
1824 SECCLASS_FILESYSTEM,
1825 FILESYSTEM__ASSOCIATE, &ad);
1829 #define MAY_UNLINK 1
1832 /* Check whether a task can link, unlink, or rmdir a file/directory. */
1833 static int may_link(struct inode *dir,
1834 struct dentry *dentry,
1838 struct inode_security_struct *dsec, *isec;
1839 struct common_audit_data ad;
1840 u32 sid = current_sid();
1844 dsec = inode_security(dir);
1845 isec = backing_inode_security(dentry);
1847 ad.type = LSM_AUDIT_DATA_DENTRY;
1848 ad.u.dentry = dentry;
1851 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
1852 rc = avc_has_perm(&selinux_state,
1853 sid, dsec->sid, SECCLASS_DIR, av, &ad);
1868 pr_warn("SELinux: %s: unrecognized kind %d\n",
1873 rc = avc_has_perm(&selinux_state,
1874 sid, isec->sid, isec->sclass, av, &ad);
1878 static inline int may_rename(struct inode *old_dir,
1879 struct dentry *old_dentry,
1880 struct inode *new_dir,
1881 struct dentry *new_dentry)
1883 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
1884 struct common_audit_data ad;
1885 u32 sid = current_sid();
1887 int old_is_dir, new_is_dir;
1890 old_dsec = inode_security(old_dir);
1891 old_isec = backing_inode_security(old_dentry);
1892 old_is_dir = d_is_dir(old_dentry);
1893 new_dsec = inode_security(new_dir);
1895 ad.type = LSM_AUDIT_DATA_DENTRY;
1897 ad.u.dentry = old_dentry;
1898 rc = avc_has_perm(&selinux_state,
1899 sid, old_dsec->sid, SECCLASS_DIR,
1900 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1903 rc = avc_has_perm(&selinux_state,
1905 old_isec->sclass, FILE__RENAME, &ad);
1908 if (old_is_dir && new_dir != old_dir) {
1909 rc = avc_has_perm(&selinux_state,
1911 old_isec->sclass, DIR__REPARENT, &ad);
1916 ad.u.dentry = new_dentry;
1917 av = DIR__ADD_NAME | DIR__SEARCH;
1918 if (d_is_positive(new_dentry))
1919 av |= DIR__REMOVE_NAME;
1920 rc = avc_has_perm(&selinux_state,
1921 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
1924 if (d_is_positive(new_dentry)) {
1925 new_isec = backing_inode_security(new_dentry);
1926 new_is_dir = d_is_dir(new_dentry);
1927 rc = avc_has_perm(&selinux_state,
1930 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1938 /* Check whether a task can perform a filesystem operation. */
1939 static int superblock_has_perm(const struct cred *cred,
1940 struct super_block *sb,
1942 struct common_audit_data *ad)
1944 struct superblock_security_struct *sbsec;
1945 u32 sid = cred_sid(cred);
1947 sbsec = sb->s_security;
1948 return avc_has_perm(&selinux_state,
1949 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
1952 /* Convert a Linux mode and permission mask to an access vector. */
1953 static inline u32 file_mask_to_av(int mode, int mask)
1957 if (!S_ISDIR(mode)) {
1958 if (mask & MAY_EXEC)
1959 av |= FILE__EXECUTE;
1960 if (mask & MAY_READ)
1963 if (mask & MAY_APPEND)
1965 else if (mask & MAY_WRITE)
1969 if (mask & MAY_EXEC)
1971 if (mask & MAY_WRITE)
1973 if (mask & MAY_READ)
1980 /* Convert a Linux file to an access vector. */
1981 static inline u32 file_to_av(struct file *file)
1985 if (file->f_mode & FMODE_READ)
1987 if (file->f_mode & FMODE_WRITE) {
1988 if (file->f_flags & O_APPEND)
1995 * Special file opened with flags 3 for ioctl-only use.
2004 * Convert a file to an access vector and include the correct open
2007 static inline u32 open_file_to_av(struct file *file)
2009 u32 av = file_to_av(file);
2010 struct inode *inode = file_inode(file);
2012 if (selinux_policycap_openperm() &&
2013 inode->i_sb->s_magic != SOCKFS_MAGIC)
2019 /* Hook functions begin here. */
2021 static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2023 u32 mysid = current_sid();
2024 u32 mgrsid = task_sid(mgr);
2026 return avc_has_perm(&selinux_state,
2027 mysid, mgrsid, SECCLASS_BINDER,
2028 BINDER__SET_CONTEXT_MGR, NULL);
2031 static int selinux_binder_transaction(struct task_struct *from,
2032 struct task_struct *to)
2034 u32 mysid = current_sid();
2035 u32 fromsid = task_sid(from);
2036 u32 tosid = task_sid(to);
2039 if (mysid != fromsid) {
2040 rc = avc_has_perm(&selinux_state,
2041 mysid, fromsid, SECCLASS_BINDER,
2042 BINDER__IMPERSONATE, NULL);
2047 return avc_has_perm(&selinux_state,
2048 fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
2052 static int selinux_binder_transfer_binder(struct task_struct *from,
2053 struct task_struct *to)
2055 u32 fromsid = task_sid(from);
2056 u32 tosid = task_sid(to);
2058 return avc_has_perm(&selinux_state,
2059 fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
2063 static int selinux_binder_transfer_file(struct task_struct *from,
2064 struct task_struct *to,
2067 u32 sid = task_sid(to);
2068 struct file_security_struct *fsec = selinux_file(file);
2069 struct dentry *dentry = file->f_path.dentry;
2070 struct inode_security_struct *isec;
2071 struct common_audit_data ad;
2074 ad.type = LSM_AUDIT_DATA_PATH;
2075 ad.u.path = file->f_path;
2077 if (sid != fsec->sid) {
2078 rc = avc_has_perm(&selinux_state,
2087 #ifdef CONFIG_BPF_SYSCALL
2088 rc = bpf_fd_pass(file, sid);
2093 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2096 isec = backing_inode_security(dentry);
2097 return avc_has_perm(&selinux_state,
2098 sid, isec->sid, isec->sclass, file_to_av(file),
2102 static int selinux_ptrace_access_check(struct task_struct *child,
2105 u32 sid = current_sid();
2106 u32 csid = task_sid(child);
2108 if (mode & PTRACE_MODE_READ)
2109 return avc_has_perm(&selinux_state,
2110 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
2112 return avc_has_perm(&selinux_state,
2113 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
2116 static int selinux_ptrace_traceme(struct task_struct *parent)
2118 return avc_has_perm(&selinux_state,
2119 task_sid(parent), current_sid(), SECCLASS_PROCESS,
2120 PROCESS__PTRACE, NULL);
2123 static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
2124 kernel_cap_t *inheritable, kernel_cap_t *permitted)
2126 return avc_has_perm(&selinux_state,
2127 current_sid(), task_sid(target), SECCLASS_PROCESS,
2128 PROCESS__GETCAP, NULL);
2131 static int selinux_capset(struct cred *new, const struct cred *old,
2132 const kernel_cap_t *effective,
2133 const kernel_cap_t *inheritable,
2134 const kernel_cap_t *permitted)
2136 return avc_has_perm(&selinux_state,
2137 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
2138 PROCESS__SETCAP, NULL);
2142 * (This comment used to live with the selinux_task_setuid hook,
2143 * which was removed).
2145 * Since setuid only affects the current process, and since the SELinux
2146 * controls are not based on the Linux identity attributes, SELinux does not
2147 * need to control this operation. However, SELinux does control the use of
2148 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2151 static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2152 int cap, unsigned int opts)
2154 return cred_has_capability(cred, cap, opts, ns == &init_user_ns);
2157 static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2159 const struct cred *cred = current_cred();
2171 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
2176 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
2179 rc = 0; /* let the kernel handle invalid cmds */
2185 static int selinux_quota_on(struct dentry *dentry)
2187 const struct cred *cred = current_cred();
2189 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
2192 static int selinux_syslog(int type)
2195 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2196 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
2197 return avc_has_perm(&selinux_state,
2198 current_sid(), SECINITSID_KERNEL,
2199 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
2200 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2201 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2202 /* Set level of messages printed to console */
2203 case SYSLOG_ACTION_CONSOLE_LEVEL:
2204 return avc_has_perm(&selinux_state,
2205 current_sid(), SECINITSID_KERNEL,
2206 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2209 /* All other syslog types */
2210 return avc_has_perm(&selinux_state,
2211 current_sid(), SECINITSID_KERNEL,
2212 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
2216 * Check that a process has enough memory to allocate a new virtual
2217 * mapping. 0 means there is enough memory for the allocation to
2218 * succeed and -ENOMEM implies there is not.
2220 * Do not audit the selinux permission check, as this is applied to all
2221 * processes that allocate mappings.
2223 static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
2225 int rc, cap_sys_admin = 0;
2227 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
2228 CAP_OPT_NOAUDIT, true);
2232 return cap_sys_admin;
2235 /* binprm security operations */
2237 static u32 ptrace_parent_sid(void)
2240 struct task_struct *tracer;
2243 tracer = ptrace_parent(current);
2245 sid = task_sid(tracer);
2251 static int check_nnp_nosuid(const struct linux_binprm *bprm,
2252 const struct task_security_struct *old_tsec,
2253 const struct task_security_struct *new_tsec)
2255 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
2256 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
2260 if (!nnp && !nosuid)
2261 return 0; /* neither NNP nor nosuid */
2263 if (new_tsec->sid == old_tsec->sid)
2264 return 0; /* No change in credentials */
2267 * If the policy enables the nnp_nosuid_transition policy capability,
2268 * then we permit transitions under NNP or nosuid if the
2269 * policy allows the corresponding permission between
2270 * the old and new contexts.
2272 if (selinux_policycap_nnp_nosuid_transition()) {
2275 av |= PROCESS2__NNP_TRANSITION;
2277 av |= PROCESS2__NOSUID_TRANSITION;
2278 rc = avc_has_perm(&selinux_state,
2279 old_tsec->sid, new_tsec->sid,
2280 SECCLASS_PROCESS2, av, NULL);
2286 * We also permit NNP or nosuid transitions to bounded SIDs,
2287 * i.e. SIDs that are guaranteed to only be allowed a subset
2288 * of the permissions of the current SID.
2290 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2296 * On failure, preserve the errno values for NNP vs nosuid.
2297 * NNP: Operation not permitted for caller.
2298 * nosuid: Permission denied to file.
2305 static int selinux_bprm_set_creds(struct linux_binprm *bprm)
2307 const struct task_security_struct *old_tsec;
2308 struct task_security_struct *new_tsec;
2309 struct inode_security_struct *isec;
2310 struct common_audit_data ad;
2311 struct inode *inode = file_inode(bprm->file);
2314 /* SELinux context only depends on initial program or script and not
2315 * the script interpreter */
2316 if (bprm->called_set_creds)
2319 old_tsec = selinux_cred(current_cred());
2320 new_tsec = selinux_cred(bprm->cred);
2321 isec = inode_security(inode);
2323 /* Default to the current task SID. */
2324 new_tsec->sid = old_tsec->sid;
2325 new_tsec->osid = old_tsec->sid;
2327 /* Reset fs, key, and sock SIDs on execve. */
2328 new_tsec->create_sid = 0;
2329 new_tsec->keycreate_sid = 0;
2330 new_tsec->sockcreate_sid = 0;
2332 if (old_tsec->exec_sid) {
2333 new_tsec->sid = old_tsec->exec_sid;
2334 /* Reset exec SID on execve. */
2335 new_tsec->exec_sid = 0;
2337 /* Fail on NNP or nosuid if not an allowed transition. */
2338 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2342 /* Check for a default transition on this program. */
2343 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2344 isec->sid, SECCLASS_PROCESS, NULL,
2350 * Fallback to old SID on NNP or nosuid if not an allowed
2353 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2355 new_tsec->sid = old_tsec->sid;
2358 ad.type = LSM_AUDIT_DATA_FILE;
2359 ad.u.file = bprm->file;
2361 if (new_tsec->sid == old_tsec->sid) {
2362 rc = avc_has_perm(&selinux_state,
2363 old_tsec->sid, isec->sid,
2364 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2368 /* Check permissions for the transition. */
2369 rc = avc_has_perm(&selinux_state,
2370 old_tsec->sid, new_tsec->sid,
2371 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2375 rc = avc_has_perm(&selinux_state,
2376 new_tsec->sid, isec->sid,
2377 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2381 /* Check for shared state */
2382 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2383 rc = avc_has_perm(&selinux_state,
2384 old_tsec->sid, new_tsec->sid,
2385 SECCLASS_PROCESS, PROCESS__SHARE,
2391 /* Make sure that anyone attempting to ptrace over a task that
2392 * changes its SID has the appropriate permit */
2393 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
2394 u32 ptsid = ptrace_parent_sid();
2396 rc = avc_has_perm(&selinux_state,
2397 ptsid, new_tsec->sid,
2399 PROCESS__PTRACE, NULL);
2405 /* Clear any possibly unsafe personality bits on exec: */
2406 bprm->per_clear |= PER_CLEAR_ON_SETID;
2408 /* Enable secure mode for SIDs transitions unless
2409 the noatsecure permission is granted between
2410 the two SIDs, i.e. ahp returns 0. */
2411 rc = avc_has_perm(&selinux_state,
2412 old_tsec->sid, new_tsec->sid,
2413 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2415 bprm->secureexec |= !!rc;
2421 static int match_file(const void *p, struct file *file, unsigned fd)
2423 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2426 /* Derived from fs/exec.c:flush_old_files. */
2427 static inline void flush_unauthorized_files(const struct cred *cred,
2428 struct files_struct *files)
2430 struct file *file, *devnull = NULL;
2431 struct tty_struct *tty;
2435 tty = get_current_tty();
2437 spin_lock(&tty->files_lock);
2438 if (!list_empty(&tty->tty_files)) {
2439 struct tty_file_private *file_priv;
2441 /* Revalidate access to controlling tty.
2442 Use file_path_has_perm on the tty path directly
2443 rather than using file_has_perm, as this particular
2444 open file may belong to another process and we are
2445 only interested in the inode-based check here. */
2446 file_priv = list_first_entry(&tty->tty_files,
2447 struct tty_file_private, list);
2448 file = file_priv->file;
2449 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
2452 spin_unlock(&tty->files_lock);
2455 /* Reset controlling tty. */
2459 /* Revalidate access to inherited open files. */
2460 n = iterate_fd(files, 0, match_file, cred);
2461 if (!n) /* none found? */
2464 devnull = dentry_open(&selinux_null, O_RDWR, cred);
2465 if (IS_ERR(devnull))
2467 /* replace all the matching ones with this */
2469 replace_fd(n - 1, devnull, 0);
2470 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2476 * Prepare a process for imminent new credential changes due to exec
2478 static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
2480 struct task_security_struct *new_tsec;
2481 struct rlimit *rlim, *initrlim;
2484 new_tsec = selinux_cred(bprm->cred);
2485 if (new_tsec->sid == new_tsec->osid)
2488 /* Close files for which the new task SID is not authorized. */
2489 flush_unauthorized_files(bprm->cred, current->files);
2491 /* Always clear parent death signal on SID transitions. */
2492 current->pdeath_signal = 0;
2494 /* Check whether the new SID can inherit resource limits from the old
2495 * SID. If not, reset all soft limits to the lower of the current
2496 * task's hard limit and the init task's soft limit.
2498 * Note that the setting of hard limits (even to lower them) can be
2499 * controlled by the setrlimit check. The inclusion of the init task's
2500 * soft limit into the computation is to avoid resetting soft limits
2501 * higher than the default soft limit for cases where the default is
2502 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2504 rc = avc_has_perm(&selinux_state,
2505 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2506 PROCESS__RLIMITINH, NULL);
2508 /* protect against do_prlimit() */
2510 for (i = 0; i < RLIM_NLIMITS; i++) {
2511 rlim = current->signal->rlim + i;
2512 initrlim = init_task.signal->rlim + i;
2513 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2515 task_unlock(current);
2516 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2517 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
2522 * Clean up the process immediately after the installation of new credentials
2525 static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2527 const struct task_security_struct *tsec = selinux_cred(current_cred());
2528 struct itimerval itimer;
2538 /* Check whether the new SID can inherit signal state from the old SID.
2539 * If not, clear itimers to avoid subsequent signal generation and
2540 * flush and unblock signals.
2542 * This must occur _after_ the task SID has been updated so that any
2543 * kill done after the flush will be checked against the new SID.
2545 rc = avc_has_perm(&selinux_state,
2546 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
2548 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
2549 memset(&itimer, 0, sizeof itimer);
2550 for (i = 0; i < 3; i++)
2551 do_setitimer(i, &itimer, NULL);
2553 spin_lock_irq(¤t->sighand->siglock);
2554 if (!fatal_signal_pending(current)) {
2555 flush_sigqueue(¤t->pending);
2556 flush_sigqueue(¤t->signal->shared_pending);
2557 flush_signal_handlers(current, 1);
2558 sigemptyset(¤t->blocked);
2559 recalc_sigpending();
2561 spin_unlock_irq(¤t->sighand->siglock);
2564 /* Wake up the parent if it is waiting so that it can recheck
2565 * wait permission to the new task SID. */
2566 read_lock(&tasklist_lock);
2567 __wake_up_parent(current, current->real_parent);
2568 read_unlock(&tasklist_lock);
2571 /* superblock security operations */
2573 static int selinux_sb_alloc_security(struct super_block *sb)
2575 return superblock_alloc_security(sb);
2578 static void selinux_sb_free_security(struct super_block *sb)
2580 superblock_free_security(sb);
2583 static inline int opt_len(const char *s)
2585 bool open_quote = false;
2589 for (len = 0; (c = s[len]) != '\0'; len++) {
2591 open_quote = !open_quote;
2592 if (c == ',' && !open_quote)
2598 static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
2600 char *from = options;
2605 int len = opt_len(from);
2609 token = match_opt_prefix(from, len, &arg);
2611 if (token != Opt_error) {
2616 for (p = q = arg; p < from + len; p++) {
2621 arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
2623 rc = selinux_add_opt(token, arg, mnt_opts);
2627 selinux_free_mnt_opts(*mnt_opts);
2633 if (!first) { // copy with preceding comma
2638 memmove(to, from, len);
2650 static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
2652 struct selinux_mnt_opts *opts = mnt_opts;
2653 struct superblock_security_struct *sbsec = sb->s_security;
2657 if (!(sbsec->flags & SE_SBINITIALIZED))
2663 if (opts->fscontext) {
2664 rc = parse_sid(sb, opts->fscontext, &sid);
2667 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2668 goto out_bad_option;
2670 if (opts->context) {
2671 rc = parse_sid(sb, opts->context, &sid);
2674 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2675 goto out_bad_option;
2677 if (opts->rootcontext) {
2678 struct inode_security_struct *root_isec;
2679 root_isec = backing_inode_security(sb->s_root);
2680 rc = parse_sid(sb, opts->rootcontext, &sid);
2683 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2684 goto out_bad_option;
2686 if (opts->defcontext) {
2687 rc = parse_sid(sb, opts->defcontext, &sid);
2690 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2691 goto out_bad_option;
2696 pr_warn("SELinux: unable to change security options "
2697 "during remount (dev %s, type=%s)\n", sb->s_id,
2702 static int selinux_sb_kern_mount(struct super_block *sb)
2704 const struct cred *cred = current_cred();
2705 struct common_audit_data ad;
2707 ad.type = LSM_AUDIT_DATA_DENTRY;
2708 ad.u.dentry = sb->s_root;
2709 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
2712 static int selinux_sb_statfs(struct dentry *dentry)
2714 const struct cred *cred = current_cred();
2715 struct common_audit_data ad;
2717 ad.type = LSM_AUDIT_DATA_DENTRY;
2718 ad.u.dentry = dentry->d_sb->s_root;
2719 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
2722 static int selinux_mount(const char *dev_name,
2723 const struct path *path,
2725 unsigned long flags,
2728 const struct cred *cred = current_cred();
2730 if (flags & MS_REMOUNT)
2731 return superblock_has_perm(cred, path->dentry->d_sb,
2732 FILESYSTEM__REMOUNT, NULL);
2734 return path_has_perm(cred, path, FILE__MOUNTON);
2737 static int selinux_umount(struct vfsmount *mnt, int flags)
2739 const struct cred *cred = current_cred();
2741 return superblock_has_perm(cred, mnt->mnt_sb,
2742 FILESYSTEM__UNMOUNT, NULL);
2745 static int selinux_fs_context_dup(struct fs_context *fc,
2746 struct fs_context *src_fc)
2748 const struct selinux_mnt_opts *src = src_fc->security;
2749 struct selinux_mnt_opts *opts;
2754 fc->security = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
2758 opts = fc->security;
2760 if (src->fscontext) {
2761 opts->fscontext = kstrdup(src->fscontext, GFP_KERNEL);
2762 if (!opts->fscontext)
2766 opts->context = kstrdup(src->context, GFP_KERNEL);
2770 if (src->rootcontext) {
2771 opts->rootcontext = kstrdup(src->rootcontext, GFP_KERNEL);
2772 if (!opts->rootcontext)
2775 if (src->defcontext) {
2776 opts->defcontext = kstrdup(src->defcontext, GFP_KERNEL);
2777 if (!opts->defcontext)
2783 static const struct fs_parameter_spec selinux_param_specs[] = {
2784 fsparam_string(CONTEXT_STR, Opt_context),
2785 fsparam_string(DEFCONTEXT_STR, Opt_defcontext),
2786 fsparam_string(FSCONTEXT_STR, Opt_fscontext),
2787 fsparam_string(ROOTCONTEXT_STR, Opt_rootcontext),
2788 fsparam_flag (SECLABEL_STR, Opt_seclabel),
2792 static const struct fs_parameter_description selinux_fs_parameters = {
2794 .specs = selinux_param_specs,
2797 static int selinux_fs_context_parse_param(struct fs_context *fc,
2798 struct fs_parameter *param)
2800 struct fs_parse_result result;
2803 opt = fs_parse(fc, &selinux_fs_parameters, param, &result);
2807 rc = selinux_add_opt(opt, param->string, &fc->security);
2809 param->string = NULL;
2815 /* inode security operations */
2817 static int selinux_inode_alloc_security(struct inode *inode)
2819 return inode_alloc_security(inode);
2822 static void selinux_inode_free_security(struct inode *inode)
2824 inode_free_security(inode);
2827 static int selinux_dentry_init_security(struct dentry *dentry, int mode,
2828 const struct qstr *name, void **ctx,
2834 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
2835 d_inode(dentry->d_parent), name,
2836 inode_mode_to_security_class(mode),
2841 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2845 static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2847 const struct cred *old,
2852 struct task_security_struct *tsec;
2854 rc = selinux_determine_inode_label(selinux_cred(old),
2855 d_inode(dentry->d_parent), name,
2856 inode_mode_to_security_class(mode),
2861 tsec = selinux_cred(new);
2862 tsec->create_sid = newsid;
2866 static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
2867 const struct qstr *qstr,
2869 void **value, size_t *len)
2871 const struct task_security_struct *tsec = selinux_cred(current_cred());
2872 struct superblock_security_struct *sbsec;
2877 sbsec = dir->i_sb->s_security;
2879 newsid = tsec->create_sid;
2881 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
2883 inode_mode_to_security_class(inode->i_mode),
2888 /* Possibly defer initialization to selinux_complete_init. */
2889 if (sbsec->flags & SE_SBINITIALIZED) {
2890 struct inode_security_struct *isec = selinux_inode(inode);
2891 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2893 isec->initialized = LABEL_INITIALIZED;
2896 if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
2900 *name = XATTR_SELINUX_SUFFIX;
2903 rc = security_sid_to_context_force(&selinux_state, newsid,
2914 static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
2916 return may_create(dir, dentry, SECCLASS_FILE);
2919 static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2921 return may_link(dir, old_dentry, MAY_LINK);
2924 static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2926 return may_link(dir, dentry, MAY_UNLINK);
2929 static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2931 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2934 static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
2936 return may_create(dir, dentry, SECCLASS_DIR);
2939 static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2941 return may_link(dir, dentry, MAY_RMDIR);
2944 static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
2946 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2949 static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
2950 struct inode *new_inode, struct dentry *new_dentry)
2952 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2955 static int selinux_inode_readlink(struct dentry *dentry)
2957 const struct cred *cred = current_cred();
2959 return dentry_has_perm(cred, dentry, FILE__READ);
2962 static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2965 const struct cred *cred = current_cred();
2966 struct common_audit_data ad;
2967 struct inode_security_struct *isec;
2970 validate_creds(cred);
2972 ad.type = LSM_AUDIT_DATA_DENTRY;
2973 ad.u.dentry = dentry;
2974 sid = cred_sid(cred);
2975 isec = inode_security_rcu(inode, rcu);
2977 return PTR_ERR(isec);
2979 return avc_has_perm(&selinux_state,
2980 sid, isec->sid, isec->sclass, FILE__READ, &ad);
2983 static noinline int audit_inode_permission(struct inode *inode,
2984 u32 perms, u32 audited, u32 denied,
2988 struct common_audit_data ad;
2989 struct inode_security_struct *isec = selinux_inode(inode);
2992 ad.type = LSM_AUDIT_DATA_INODE;
2995 rc = slow_avc_audit(&selinux_state,
2996 current_sid(), isec->sid, isec->sclass, perms,
2997 audited, denied, result, &ad, flags);
3003 static int selinux_inode_permission(struct inode *inode, int mask)
3005 const struct cred *cred = current_cred();
3008 unsigned flags = mask & MAY_NOT_BLOCK;
3009 struct inode_security_struct *isec;
3011 struct av_decision avd;
3013 u32 audited, denied;
3015 from_access = mask & MAY_ACCESS;
3016 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3018 /* No permission to check. Existence test. */
3022 validate_creds(cred);
3024 if (unlikely(IS_PRIVATE(inode)))
3027 perms = file_mask_to_av(inode->i_mode, mask);
3029 sid = cred_sid(cred);
3030 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
3032 return PTR_ERR(isec);
3034 rc = avc_has_perm_noaudit(&selinux_state,
3035 sid, isec->sid, isec->sclass, perms,
3036 (flags & MAY_NOT_BLOCK) ? AVC_NONBLOCKING : 0,
3038 audited = avc_audit_required(perms, &avd, rc,
3039 from_access ? FILE__AUDIT_ACCESS : 0,
3041 if (likely(!audited))
3044 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
3050 static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3052 const struct cred *cred = current_cred();
3053 struct inode *inode = d_backing_inode(dentry);
3054 unsigned int ia_valid = iattr->ia_valid;
3055 __u32 av = FILE__WRITE;
3057 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3058 if (ia_valid & ATTR_FORCE) {
3059 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3065 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3066 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
3067 return dentry_has_perm(cred, dentry, FILE__SETATTR);
3069 if (selinux_policycap_openperm() &&
3070 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3071 (ia_valid & ATTR_SIZE) &&
3072 !(ia_valid & ATTR_FILE))
3075 return dentry_has_perm(cred, dentry, av);
3078 static int selinux_inode_getattr(const struct path *path)
3080 return path_has_perm(current_cred(), path, FILE__GETATTR);
3083 static bool has_cap_mac_admin(bool audit)
3085 const struct cred *cred = current_cred();
3086 unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT;
3088 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts))
3090 if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true))
3095 static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3096 const void *value, size_t size, int flags)
3098 struct inode *inode = d_backing_inode(dentry);
3099 struct inode_security_struct *isec;
3100 struct superblock_security_struct *sbsec;
3101 struct common_audit_data ad;
3102 u32 newsid, sid = current_sid();
3105 if (strcmp(name, XATTR_NAME_SELINUX)) {
3106 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3110 /* Not an attribute we recognize, so just check the
3111 ordinary setattr permission. */
3112 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3115 sbsec = inode->i_sb->s_security;
3116 if (!(sbsec->flags & SBLABEL_MNT))
3119 if (!inode_owner_or_capable(inode))
3122 ad.type = LSM_AUDIT_DATA_DENTRY;
3123 ad.u.dentry = dentry;
3125 isec = backing_inode_security(dentry);
3126 rc = avc_has_perm(&selinux_state,
3127 sid, isec->sid, isec->sclass,
3128 FILE__RELABELFROM, &ad);
3132 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3134 if (rc == -EINVAL) {
3135 if (!has_cap_mac_admin(true)) {
3136 struct audit_buffer *ab;
3139 /* We strip a nul only if it is at the end, otherwise the
3140 * context contains a nul and we should audit that */
3142 const char *str = value;
3144 if (str[size - 1] == '\0')
3145 audit_size = size - 1;
3151 ab = audit_log_start(audit_context(),
3152 GFP_ATOMIC, AUDIT_SELINUX_ERR);
3153 audit_log_format(ab, "op=setxattr invalid_context=");
3154 audit_log_n_untrustedstring(ab, value, audit_size);
3159 rc = security_context_to_sid_force(&selinux_state, value,
3165 rc = avc_has_perm(&selinux_state,
3166 sid, newsid, isec->sclass,
3167 FILE__RELABELTO, &ad);
3171 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3176 return avc_has_perm(&selinux_state,
3179 SECCLASS_FILESYSTEM,
3180 FILESYSTEM__ASSOCIATE,
3184 static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
3185 const void *value, size_t size,
3188 struct inode *inode = d_backing_inode(dentry);
3189 struct inode_security_struct *isec;
3193 if (strcmp(name, XATTR_NAME_SELINUX)) {
3194 /* Not an attribute we recognize, so nothing to do. */
3198 rc = security_context_to_sid_force(&selinux_state, value, size,
3201 pr_err("SELinux: unable to map context to SID"
3202 "for (%s, %lu), rc=%d\n",
3203 inode->i_sb->s_id, inode->i_ino, -rc);
3207 isec = backing_inode_security(dentry);
3208 spin_lock(&isec->lock);
3209 isec->sclass = inode_mode_to_security_class(inode->i_mode);
3211 isec->initialized = LABEL_INITIALIZED;
3212 spin_unlock(&isec->lock);
3217 static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
3219 const struct cred *cred = current_cred();
3221 return dentry_has_perm(cred, dentry, FILE__GETATTR);
3224 static int selinux_inode_listxattr(struct dentry *dentry)
3226 const struct cred *cred = current_cred();
3228 return dentry_has_perm(cred, dentry, FILE__GETATTR);
3231 static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
3233 if (strcmp(name, XATTR_NAME_SELINUX)) {
3234 int rc = cap_inode_removexattr(dentry, name);
3238 /* Not an attribute we recognize, so just check the
3239 ordinary setattr permission. */
3240 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3243 /* No one is allowed to remove a SELinux security label.
3244 You can change the label, but all data must be labeled. */
3249 * Copy the inode security context value to the user.
3251 * Permission check is handled by selinux_inode_getxattr hook.
3253 static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
3257 char *context = NULL;
3258 struct inode_security_struct *isec;
3260 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3264 * If the caller has CAP_MAC_ADMIN, then get the raw context
3265 * value even if it is not defined by current policy; otherwise,
3266 * use the in-core value under current policy.
3267 * Use the non-auditing forms of the permission checks since
3268 * getxattr may be called by unprivileged processes commonly
3269 * and lack of permission just means that we fall back to the
3270 * in-core context value, not a denial.
3272 isec = inode_security(inode);
3273 if (has_cap_mac_admin(false))
3274 error = security_sid_to_context_force(&selinux_state,
3275 isec->sid, &context,
3278 error = security_sid_to_context(&selinux_state, isec->sid,
3292 static int selinux_inode_setsecurity(struct inode *inode, const char *name,
3293 const void *value, size_t size, int flags)
3295 struct inode_security_struct *isec = inode_security_novalidate(inode);
3296 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
3300 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3303 if (!(sbsec->flags & SBLABEL_MNT))
3306 if (!value || !size)
3309 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3314 spin_lock(&isec->lock);
3315 isec->sclass = inode_mode_to_security_class(inode->i_mode);
3317 isec->initialized = LABEL_INITIALIZED;
3318 spin_unlock(&isec->lock);
3322 static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3324 const int len = sizeof(XATTR_NAME_SELINUX);
3325 if (buffer && len <= buffer_size)
3326 memcpy(buffer, XATTR_NAME_SELINUX, len);
3330 static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
3332 struct inode_security_struct *isec = inode_security_novalidate(inode);
3336 static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3339 struct task_security_struct *tsec;
3340 struct cred *new_creds = *new;
3342 if (new_creds == NULL) {
3343 new_creds = prepare_creds();
3348 tsec = selinux_cred(new_creds);
3349 /* Get label from overlay inode and set it in create_sid */
3350 selinux_inode_getsecid(d_inode(src), &sid);
3351 tsec->create_sid = sid;
3356 static int selinux_inode_copy_up_xattr(const char *name)
3358 /* The copy_up hook above sets the initial context on an inode, but we
3359 * don't then want to overwrite it by blindly copying all the lower
3360 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3362 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3363 return 1; /* Discard */
3365 * Any other attribute apart from SELINUX is not claimed, supported
3371 /* file security operations */
3373 static int selinux_revalidate_file_permission(struct file *file, int mask)
3375 const struct cred *cred = current_cred();
3376 struct inode *inode = file_inode(file);
3378 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3379 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3382 return file_has_perm(cred, file,
3383 file_mask_to_av(inode->i_mode, mask));
3386 static int selinux_file_permission(struct file *file, int mask)
3388 struct inode *inode = file_inode(file);
3389 struct file_security_struct *fsec = selinux_file(file);
3390 struct inode_security_struct *isec;
3391 u32 sid = current_sid();
3394 /* No permission to check. Existence test. */
3397 isec = inode_security(inode);
3398 if (sid == fsec->sid && fsec->isid == isec->sid &&
3399 fsec->pseqno == avc_policy_seqno(&selinux_state))
3400 /* No change since file_open check. */
3403 return selinux_revalidate_file_permission(file, mask);
3406 static int selinux_file_alloc_security(struct file *file)
3408 return file_alloc_security(file);
3412 * Check whether a task has the ioctl permission and cmd
3413 * operation to an inode.
3415 static int ioctl_has_perm(const struct cred *cred, struct file *file,
3416 u32 requested, u16 cmd)
3418 struct common_audit_data ad;
3419 struct file_security_struct *fsec = selinux_file(file);
3420 struct inode *inode = file_inode(file);
3421 struct inode_security_struct *isec;
3422 struct lsm_ioctlop_audit ioctl;
3423 u32 ssid = cred_sid(cred);
3425 u8 driver = cmd >> 8;
3426 u8 xperm = cmd & 0xff;
3428 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3431 ad.u.op->path = file->f_path;
3433 if (ssid != fsec->sid) {
3434 rc = avc_has_perm(&selinux_state,
3443 if (unlikely(IS_PRIVATE(inode)))
3446 isec = inode_security(inode);
3447 rc = avc_has_extended_perms(&selinux_state,
3448 ssid, isec->sid, isec->sclass,
3449 requested, driver, xperm, &ad);
3454 static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3457 const struct cred *cred = current_cred();
3467 case FS_IOC_GETFLAGS:
3469 case FS_IOC_GETVERSION:
3470 error = file_has_perm(cred, file, FILE__GETATTR);
3473 case FS_IOC_SETFLAGS:
3475 case FS_IOC_SETVERSION:
3476 error = file_has_perm(cred, file, FILE__SETATTR);
3479 /* sys_ioctl() checks */
3483 error = file_has_perm(cred, file, 0);
3488 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3489 CAP_OPT_NONE, true);
3492 /* default case assumes that the command will go
3493 * to the file's ioctl() function.
3496 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
3501 static int default_noexec;
3503 static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3505 const struct cred *cred = current_cred();
3506 u32 sid = cred_sid(cred);
3509 if (default_noexec &&
3510 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3511 (!shared && (prot & PROT_WRITE)))) {
3513 * We are making executable an anonymous mapping or a
3514 * private file mapping that will also be writable.
3515 * This has an additional check.
3517 rc = avc_has_perm(&selinux_state,
3518 sid, sid, SECCLASS_PROCESS,
3519 PROCESS__EXECMEM, NULL);
3525 /* read access is always possible with a mapping */
3526 u32 av = FILE__READ;
3528 /* write access only matters if the mapping is shared */
3529 if (shared && (prot & PROT_WRITE))
3532 if (prot & PROT_EXEC)
3533 av |= FILE__EXECUTE;
3535 return file_has_perm(cred, file, av);
3542 static int selinux_mmap_addr(unsigned long addr)
3546 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3547 u32 sid = current_sid();
3548 rc = avc_has_perm(&selinux_state,
3549 sid, sid, SECCLASS_MEMPROTECT,
3550 MEMPROTECT__MMAP_ZERO, NULL);
3556 static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3557 unsigned long prot, unsigned long flags)
3559 struct common_audit_data ad;
3563 ad.type = LSM_AUDIT_DATA_FILE;
3565 rc = inode_has_perm(current_cred(), file_inode(file),
3571 if (selinux_state.checkreqprot)
3574 return file_map_prot_check(file, prot,
3575 (flags & MAP_TYPE) == MAP_SHARED);
3578 static int selinux_file_mprotect(struct vm_area_struct *vma,
3579 unsigned long reqprot,
3582 const struct cred *cred = current_cred();
3583 u32 sid = cred_sid(cred);
3585 if (selinux_state.checkreqprot)
3588 if (default_noexec &&
3589 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3591 if (vma->vm_start >= vma->vm_mm->start_brk &&
3592 vma->vm_end <= vma->vm_mm->brk) {
3593 rc = avc_has_perm(&selinux_state,
3594 sid, sid, SECCLASS_PROCESS,
3595 PROCESS__EXECHEAP, NULL);
3596 } else if (!vma->vm_file &&
3597 ((vma->vm_start <= vma->vm_mm->start_stack &&
3598 vma->vm_end >= vma->vm_mm->start_stack) ||
3599 vma_is_stack_for_current(vma))) {
3600 rc = avc_has_perm(&selinux_state,
3601 sid, sid, SECCLASS_PROCESS,
3602 PROCESS__EXECSTACK, NULL);
3603 } else if (vma->vm_file && vma->anon_vma) {
3605 * We are making executable a file mapping that has
3606 * had some COW done. Since pages might have been
3607 * written, check ability to execute the possibly
3608 * modified content. This typically should only
3609 * occur for text relocations.
3611 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
3617 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3620 static int selinux_file_lock(struct file *file, unsigned int cmd)
3622 const struct cred *cred = current_cred();
3624 return file_has_perm(cred, file, FILE__LOCK);
3627 static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3630 const struct cred *cred = current_cred();
3635 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
3636 err = file_has_perm(cred, file, FILE__WRITE);
3645 case F_GETOWNER_UIDS:
3646 /* Just check FD__USE permission */
3647 err = file_has_perm(cred, file, 0);
3655 #if BITS_PER_LONG == 32
3660 err = file_has_perm(cred, file, FILE__LOCK);
3667 static void selinux_file_set_fowner(struct file *file)
3669 struct file_security_struct *fsec;
3671 fsec = selinux_file(file);
3672 fsec->fown_sid = current_sid();
3675 static int selinux_file_send_sigiotask(struct task_struct *tsk,
3676 struct fown_struct *fown, int signum)
3679 u32 sid = task_sid(tsk);
3681 struct file_security_struct *fsec;
3683 /* struct fown_struct is never outside the context of a struct file */
3684 file = container_of(fown, struct file, f_owner);
3686 fsec = selinux_file(file);
3689 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3691 perm = signal_to_av(signum);
3693 return avc_has_perm(&selinux_state,
3694 fsec->fown_sid, sid,
3695 SECCLASS_PROCESS, perm, NULL);
3698 static int selinux_file_receive(struct file *file)
3700 const struct cred *cred = current_cred();
3702 return file_has_perm(cred, file, file_to_av(file));
3705 static int selinux_file_open(struct file *file)
3707 struct file_security_struct *fsec;
3708 struct inode_security_struct *isec;
3710 fsec = selinux_file(file);
3711 isec = inode_security(file_inode(file));
3713 * Save inode label and policy sequence number
3714 * at open-time so that selinux_file_permission
3715 * can determine whether revalidation is necessary.
3716 * Task label is already saved in the file security
3717 * struct as its SID.
3719 fsec->isid = isec->sid;
3720 fsec->pseqno = avc_policy_seqno(&selinux_state);
3722 * Since the inode label or policy seqno may have changed
3723 * between the selinux_inode_permission check and the saving
3724 * of state above, recheck that access is still permitted.
3725 * Otherwise, access might never be revalidated against the
3726 * new inode label or new policy.
3727 * This check is not redundant - do not remove.
3729 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
3732 /* task security operations */
3734 static int selinux_task_alloc(struct task_struct *task,
3735 unsigned long clone_flags)
3737 u32 sid = current_sid();
3739 return avc_has_perm(&selinux_state,
3740 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
3744 * prepare a new set of credentials for modification
3746 static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3749 const struct task_security_struct *old_tsec = selinux_cred(old);
3750 struct task_security_struct *tsec = selinux_cred(new);
3757 * transfer the SELinux data to a blank set of creds
3759 static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3761 const struct task_security_struct *old_tsec = selinux_cred(old);
3762 struct task_security_struct *tsec = selinux_cred(new);
3767 static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3769 *secid = cred_sid(c);
3773 * set the security data for a kernel service
3774 * - all the creation contexts are set to unlabelled
3776 static int selinux_kernel_act_as(struct cred *new, u32 secid)
3778 struct task_security_struct *tsec = selinux_cred(new);
3779 u32 sid = current_sid();
3782 ret = avc_has_perm(&selinux_state,
3784 SECCLASS_KERNEL_SERVICE,
3785 KERNEL_SERVICE__USE_AS_OVERRIDE,
3789 tsec->create_sid = 0;
3790 tsec->keycreate_sid = 0;
3791 tsec->sockcreate_sid = 0;
3797 * set the file creation context in a security record to the same as the
3798 * objective context of the specified inode
3800 static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3802 struct inode_security_struct *isec = inode_security(inode);
3803 struct task_security_struct *tsec = selinux_cred(new);
3804 u32 sid = current_sid();
3807 ret = avc_has_perm(&selinux_state,
3809 SECCLASS_KERNEL_SERVICE,
3810 KERNEL_SERVICE__CREATE_FILES_AS,
3814 tsec->create_sid = isec->sid;
3818 static int selinux_kernel_module_request(char *kmod_name)
3820 struct common_audit_data ad;
3822 ad.type = LSM_AUDIT_DATA_KMOD;
3823 ad.u.kmod_name = kmod_name;
3825 return avc_has_perm(&selinux_state,
3826 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
3827 SYSTEM__MODULE_REQUEST, &ad);
3830 static int selinux_kernel_module_from_file(struct file *file)
3832 struct common_audit_data ad;
3833 struct inode_security_struct *isec;
3834 struct file_security_struct *fsec;
3835 u32 sid = current_sid();
3840 return avc_has_perm(&selinux_state,
3841 sid, sid, SECCLASS_SYSTEM,
3842 SYSTEM__MODULE_LOAD, NULL);
3846 ad.type = LSM_AUDIT_DATA_FILE;
3849 fsec = selinux_file(file);
3850 if (sid != fsec->sid) {
3851 rc = avc_has_perm(&selinux_state,
3852 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
3857 isec = inode_security(file_inode(file));
3858 return avc_has_perm(&selinux_state,
3859 sid, isec->sid, SECCLASS_SYSTEM,
3860 SYSTEM__MODULE_LOAD, &ad);
3863 static int selinux_kernel_read_file(struct file *file,
3864 enum kernel_read_file_id id)
3869 case READING_MODULE:
3870 rc = selinux_kernel_module_from_file(file);
3879 static int selinux_kernel_load_data(enum kernel_load_data_id id)
3884 case LOADING_MODULE:
3885 rc = selinux_kernel_module_from_file(NULL);
3893 static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3895 return avc_has_perm(&selinux_state,
3896 current_sid(), task_sid(p), SECCLASS_PROCESS,
3897 PROCESS__SETPGID, NULL);
3900 static int selinux_task_getpgid(struct task_struct *p)
3902 return avc_has_perm(&selinux_state,
3903 current_sid(), task_sid(p), SECCLASS_PROCESS,
3904 PROCESS__GETPGID, NULL);
3907 static int selinux_task_getsid(struct task_struct *p)
3909 return avc_has_perm(&selinux_state,
3910 current_sid(), task_sid(p), SECCLASS_PROCESS,
3911 PROCESS__GETSESSION, NULL);
3914 static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3916 *secid = task_sid(p);
3919 static int selinux_task_setnice(struct task_struct *p, int nice)
3921 return avc_has_perm(&selinux_state,
3922 current_sid(), task_sid(p), SECCLASS_PROCESS,
3923 PROCESS__SETSCHED, NULL);
3926 static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3928 return avc_has_perm(&selinux_state,
3929 current_sid(), task_sid(p), SECCLASS_PROCESS,
3930 PROCESS__SETSCHED, NULL);
3933 static int selinux_task_getioprio(struct task_struct *p)
3935 return avc_has_perm(&selinux_state,
3936 current_sid(), task_sid(p), SECCLASS_PROCESS,
3937 PROCESS__GETSCHED, NULL);
3940 static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
3947 if (flags & LSM_PRLIMIT_WRITE)
3948 av |= PROCESS__SETRLIMIT;
3949 if (flags & LSM_PRLIMIT_READ)
3950 av |= PROCESS__GETRLIMIT;
3951 return avc_has_perm(&selinux_state,
3952 cred_sid(cred), cred_sid(tcred),
3953 SECCLASS_PROCESS, av, NULL);
3956 static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3957 struct rlimit *new_rlim)
3959 struct rlimit *old_rlim = p->signal->rlim + resource;
3961 /* Control the ability to change the hard limit (whether
3962 lowering or raising it), so that the hard limit can
3963 later be used as a safe reset point for the soft limit
3964 upon context transitions. See selinux_bprm_committing_creds. */
3965 if (old_rlim->rlim_max != new_rlim->rlim_max)
3966 return avc_has_perm(&selinux_state,
3967 current_sid(), task_sid(p),
3968 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
3973 static int selinux_task_setscheduler(struct task_struct *p)
3975 return avc_has_perm(&selinux_state,
3976 current_sid(), task_sid(p), SECCLASS_PROCESS,
3977 PROCESS__SETSCHED, NULL);
3980 static int selinux_task_getscheduler(struct task_struct *p)
3982 return avc_has_perm(&selinux_state,
3983 current_sid(), task_sid(p), SECCLASS_PROCESS,
3984 PROCESS__GETSCHED, NULL);
3987 static int selinux_task_movememory(struct task_struct *p)
3989 return avc_has_perm(&selinux_state,
3990 current_sid(), task_sid(p), SECCLASS_PROCESS,
3991 PROCESS__SETSCHED, NULL);
3994 static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
3995 int sig, const struct cred *cred)
4001 perm = PROCESS__SIGNULL; /* null signal; existence test */
4003 perm = signal_to_av(sig);
4005 secid = current_sid();
4007 secid = cred_sid(cred);
4008 return avc_has_perm(&selinux_state,
4009 secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
4012 static void selinux_task_to_inode(struct task_struct *p,
4013 struct inode *inode)
4015 struct inode_security_struct *isec = selinux_inode(inode);
4016 u32 sid = task_sid(p);
4018 spin_lock(&isec->lock);
4019 isec->sclass = inode_mode_to_security_class(inode->i_mode);
4021 isec->initialized = LABEL_INITIALIZED;
4022 spin_unlock(&isec->lock);
4025 /* Returns error only if unable to parse addresses */
4026 static int selinux_parse_skb_ipv4(struct sk_buff *skb,
4027 struct common_audit_data *ad, u8 *proto)
4029 int offset, ihlen, ret = -EINVAL;
4030 struct iphdr _iph, *ih;
4032 offset = skb_network_offset(skb);
4033 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4037 ihlen = ih->ihl * 4;
4038 if (ihlen < sizeof(_iph))
4041 ad->u.net->v4info.saddr = ih->saddr;
4042 ad->u.net->v4info.daddr = ih->daddr;
4046 *proto = ih->protocol;
4048 switch (ih->protocol) {
4050 struct tcphdr _tcph, *th;
4052 if (ntohs(ih->frag_off) & IP_OFFSET)
4056 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4060 ad->u.net->sport = th->source;
4061 ad->u.net->dport = th->dest;
4066 struct udphdr _udph, *uh;
4068 if (ntohs(ih->frag_off) & IP_OFFSET)
4072 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4076 ad->u.net->sport = uh->source;
4077 ad->u.net->dport = uh->dest;
4081 case IPPROTO_DCCP: {
4082 struct dccp_hdr _dccph, *dh;
4084 if (ntohs(ih->frag_off) & IP_OFFSET)
4088 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4092 ad->u.net->sport = dh->dccph_sport;
4093 ad->u.net->dport = dh->dccph_dport;
4097 #if IS_ENABLED(CONFIG_IP_SCTP)
4098 case IPPROTO_SCTP: {
4099 struct sctphdr _sctph, *sh;
4101 if (ntohs(ih->frag_off) & IP_OFFSET)
4105 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4109 ad->u.net->sport = sh->source;
4110 ad->u.net->dport = sh->dest;
4121 #if IS_ENABLED(CONFIG_IPV6)
4123 /* Returns error only if unable to parse addresses */
4124 static int selinux_parse_skb_ipv6(struct sk_buff *skb,
4125 struct common_audit_data *ad, u8 *proto)
4128 int ret = -EINVAL, offset;
4129 struct ipv6hdr _ipv6h, *ip6;
4132 offset = skb_network_offset(skb);
4133 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4137 ad->u.net->v6info.saddr = ip6->saddr;
4138 ad->u.net->v6info.daddr = ip6->daddr;
4141 nexthdr = ip6->nexthdr;
4142 offset += sizeof(_ipv6h);
4143 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
4152 struct tcphdr _tcph, *th;
4154 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4158 ad->u.net->sport = th->source;
4159 ad->u.net->dport = th->dest;
4164 struct udphdr _udph, *uh;
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 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4182 ad->u.net->sport = dh->dccph_sport;
4183 ad->u.net->dport = dh->dccph_dport;
4187 #if IS_ENABLED(CONFIG_IP_SCTP)
4188 case IPPROTO_SCTP: {
4189 struct sctphdr _sctph, *sh;
4191 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4195 ad->u.net->sport = sh->source;
4196 ad->u.net->dport = sh->dest;
4200 /* includes fragments */
4210 static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
4211 char **_addrp, int src, u8 *proto)
4216 switch (ad->u.net->family) {
4218 ret = selinux_parse_skb_ipv4(skb, ad, proto);
4221 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4222 &ad->u.net->v4info.daddr);
4225 #if IS_ENABLED(CONFIG_IPV6)
4227 ret = selinux_parse_skb_ipv6(skb, ad, proto);
4230 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4231 &ad->u.net->v6info.daddr);
4241 "SELinux: failure in selinux_parse_skb(),"
4242 " unable to parse packet\n");
4252 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
4254 * @family: protocol family
4255 * @sid: the packet's peer label SID
4258 * Check the various different forms of network peer labeling and determine
4259 * the peer label/SID for the packet; most of the magic actually occurs in
4260 * the security server function security_net_peersid_cmp(). The function
4261 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4262 * or -EACCES if @sid is invalid due to inconsistencies with the different
4266 static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
4273 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
4276 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4280 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4281 nlbl_type, xfrm_sid, sid);
4282 if (unlikely(err)) {
4284 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4285 " unable to determine packet's peer label\n");
4293 * selinux_conn_sid - Determine the child socket label for a connection
4294 * @sk_sid: the parent socket's SID
4295 * @skb_sid: the packet's SID
4296 * @conn_sid: the resulting connection SID
4298 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4299 * combined with the MLS information from @skb_sid in order to create
4300 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4301 * of @sk_sid. Returns zero on success, negative values on failure.
4304 static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4308 if (skb_sid != SECSID_NULL)
4309 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4317 /* socket security operations */
4319 static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4320 u16 secclass, u32 *socksid)
4322 if (tsec->sockcreate_sid > SECSID_NULL) {
4323 *socksid = tsec->sockcreate_sid;
4327 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4328 secclass, NULL, socksid);
4331 static int sock_has_perm(struct sock *sk, u32 perms)
4333 struct sk_security_struct *sksec = sk->sk_security;
4334 struct common_audit_data ad;
4335 struct lsm_network_audit net = {0,};
4337 if (sksec->sid == SECINITSID_KERNEL)
4340 ad.type = LSM_AUDIT_DATA_NET;
4344 return avc_has_perm(&selinux_state,
4345 current_sid(), sksec->sid, sksec->sclass, perms,
4349 static int selinux_socket_create(int family, int type,
4350 int protocol, int kern)
4352 const struct task_security_struct *tsec = selinux_cred(current_cred());
4360 secclass = socket_type_to_security_class(family, type, protocol);
4361 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4365 return avc_has_perm(&selinux_state,
4366 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
4369 static int selinux_socket_post_create(struct socket *sock, int family,
4370 int type, int protocol, int kern)
4372 const struct task_security_struct *tsec = selinux_cred(current_cred());
4373 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
4374 struct sk_security_struct *sksec;
4375 u16 sclass = socket_type_to_security_class(family, type, protocol);
4376 u32 sid = SECINITSID_KERNEL;
4380 err = socket_sockcreate_sid(tsec, sclass, &sid);
4385 isec->sclass = sclass;
4387 isec->initialized = LABEL_INITIALIZED;
4390 sksec = sock->sk->sk_security;
4391 sksec->sclass = sclass;
4393 /* Allows detection of the first association on this socket */
4394 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4395 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4397 err = selinux_netlbl_socket_post_create(sock->sk, family);
4403 static int selinux_socket_socketpair(struct socket *socka,
4404 struct socket *sockb)
4406 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4407 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4409 sksec_a->peer_sid = sksec_b->sid;
4410 sksec_b->peer_sid = sksec_a->sid;
4415 /* Range of port numbers used to automatically bind.
4416 Need to determine whether we should perform a name_bind
4417 permission check between the socket and the port number. */
4419 static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4421 struct sock *sk = sock->sk;
4422 struct sk_security_struct *sksec = sk->sk_security;
4426 err = sock_has_perm(sk, SOCKET__BIND);
4430 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
4431 family = sk->sk_family;
4432 if (family == PF_INET || family == PF_INET6) {
4434 struct common_audit_data ad;
4435 struct lsm_network_audit net = {0,};
4436 struct sockaddr_in *addr4 = NULL;
4437 struct sockaddr_in6 *addr6 = NULL;
4438 u16 family_sa = address->sa_family;
4439 unsigned short snum;
4443 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4444 * that validates multiple binding addresses. Because of this
4445 * need to check address->sa_family as it is possible to have
4446 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4448 switch (family_sa) {
4451 if (addrlen < sizeof(struct sockaddr_in))
4453 addr4 = (struct sockaddr_in *)address;
4454 if (family_sa == AF_UNSPEC) {
4455 /* see __inet_bind(), we only want to allow
4456 * AF_UNSPEC if the address is INADDR_ANY
4458 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4460 family_sa = AF_INET;
4462 snum = ntohs(addr4->sin_port);
4463 addrp = (char *)&addr4->sin_addr.s_addr;
4466 if (addrlen < SIN6_LEN_RFC2133)
4468 addr6 = (struct sockaddr_in6 *)address;
4469 snum = ntohs(addr6->sin6_port);
4470 addrp = (char *)&addr6->sin6_addr.s6_addr;
4476 ad.type = LSM_AUDIT_DATA_NET;
4478 ad.u.net->sport = htons(snum);
4479 ad.u.net->family = family_sa;
4484 inet_get_local_port_range(sock_net(sk), &low, &high);
4486 if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
4488 err = sel_netport_sid(sk->sk_protocol,
4492 err = avc_has_perm(&selinux_state,
4495 SOCKET__NAME_BIND, &ad);
4501 switch (sksec->sclass) {
4502 case SECCLASS_TCP_SOCKET:
4503 node_perm = TCP_SOCKET__NODE_BIND;
4506 case SECCLASS_UDP_SOCKET:
4507 node_perm = UDP_SOCKET__NODE_BIND;
4510 case SECCLASS_DCCP_SOCKET:
4511 node_perm = DCCP_SOCKET__NODE_BIND;
4514 case SECCLASS_SCTP_SOCKET:
4515 node_perm = SCTP_SOCKET__NODE_BIND;
4519 node_perm = RAWIP_SOCKET__NODE_BIND;
4523 err = sel_netnode_sid(addrp, family_sa, &sid);
4527 if (family_sa == AF_INET)
4528 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
4530 ad.u.net->v6info.saddr = addr6->sin6_addr;
4532 err = avc_has_perm(&selinux_state,
4534 sksec->sclass, node_perm, &ad);
4541 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4542 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4544 return -EAFNOSUPPORT;
4547 /* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
4548 * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst
4550 static int selinux_socket_connect_helper(struct socket *sock,
4551 struct sockaddr *address, int addrlen)
4553 struct sock *sk = sock->sk;
4554 struct sk_security_struct *sksec = sk->sk_security;
4557 err = sock_has_perm(sk, SOCKET__CONNECT);
4562 * If a TCP, DCCP or SCTP socket, check name_connect permission
4565 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4566 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4567 sksec->sclass == SECCLASS_SCTP_SOCKET) {
4568 struct common_audit_data ad;
4569 struct lsm_network_audit net = {0,};
4570 struct sockaddr_in *addr4 = NULL;
4571 struct sockaddr_in6 *addr6 = NULL;
4572 unsigned short snum;
4575 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4576 * that validates multiple connect addresses. Because of this
4577 * need to check address->sa_family as it is possible to have
4578 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4580 switch (address->sa_family) {
4582 addr4 = (struct sockaddr_in *)address;
4583 if (addrlen < sizeof(struct sockaddr_in))
4585 snum = ntohs(addr4->sin_port);
4588 addr6 = (struct sockaddr_in6 *)address;
4589 if (addrlen < SIN6_LEN_RFC2133)
4591 snum = ntohs(addr6->sin6_port);
4594 /* Note that SCTP services expect -EINVAL, whereas
4595 * others expect -EAFNOSUPPORT.
4597 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4600 return -EAFNOSUPPORT;
4603 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
4607 switch (sksec->sclass) {
4608 case SECCLASS_TCP_SOCKET:
4609 perm = TCP_SOCKET__NAME_CONNECT;
4611 case SECCLASS_DCCP_SOCKET:
4612 perm = DCCP_SOCKET__NAME_CONNECT;
4614 case SECCLASS_SCTP_SOCKET:
4615 perm = SCTP_SOCKET__NAME_CONNECT;
4619 ad.type = LSM_AUDIT_DATA_NET;
4621 ad.u.net->dport = htons(snum);
4622 ad.u.net->family = address->sa_family;
4623 err = avc_has_perm(&selinux_state,
4624 sksec->sid, sid, sksec->sclass, perm, &ad);
4632 /* Supports connect(2), see comments in selinux_socket_connect_helper() */
4633 static int selinux_socket_connect(struct socket *sock,
4634 struct sockaddr *address, int addrlen)
4637 struct sock *sk = sock->sk;
4639 err = selinux_socket_connect_helper(sock, address, addrlen);
4643 return selinux_netlbl_socket_connect(sk, address);
4646 static int selinux_socket_listen(struct socket *sock, int backlog)
4648 return sock_has_perm(sock->sk, SOCKET__LISTEN);
4651 static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4654 struct inode_security_struct *isec;
4655 struct inode_security_struct *newisec;
4659 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
4663 isec = inode_security_novalidate(SOCK_INODE(sock));
4664 spin_lock(&isec->lock);
4665 sclass = isec->sclass;
4667 spin_unlock(&isec->lock);
4669 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4670 newisec->sclass = sclass;
4672 newisec->initialized = LABEL_INITIALIZED;
4677 static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
4680 return sock_has_perm(sock->sk, SOCKET__WRITE);
4683 static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4684 int size, int flags)
4686 return sock_has_perm(sock->sk, SOCKET__READ);
4689 static int selinux_socket_getsockname(struct socket *sock)
4691 return sock_has_perm(sock->sk, SOCKET__GETATTR);
4694 static int selinux_socket_getpeername(struct socket *sock)
4696 return sock_has_perm(sock->sk, SOCKET__GETATTR);
4699 static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
4703 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
4707 return selinux_netlbl_socket_setsockopt(sock, level, optname);
4710 static int selinux_socket_getsockopt(struct socket *sock, int level,
4713 return sock_has_perm(sock->sk, SOCKET__GETOPT);
4716 static int selinux_socket_shutdown(struct socket *sock, int how)
4718 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
4721 static int selinux_socket_unix_stream_connect(struct sock *sock,
4725 struct sk_security_struct *sksec_sock = sock->sk_security;
4726 struct sk_security_struct *sksec_other = other->sk_security;
4727 struct sk_security_struct *sksec_new = newsk->sk_security;
4728 struct common_audit_data ad;
4729 struct lsm_network_audit net = {0,};
4732 ad.type = LSM_AUDIT_DATA_NET;
4734 ad.u.net->sk = other;
4736 err = avc_has_perm(&selinux_state,
4737 sksec_sock->sid, sksec_other->sid,
4738 sksec_other->sclass,
4739 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4743 /* server child socket */
4744 sksec_new->peer_sid = sksec_sock->sid;
4745 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4746 sksec_sock->sid, &sksec_new->sid);
4750 /* connecting socket */
4751 sksec_sock->peer_sid = sksec_new->sid;
4756 static int selinux_socket_unix_may_send(struct socket *sock,
4757 struct socket *other)
4759 struct sk_security_struct *ssec = sock->sk->sk_security;
4760 struct sk_security_struct *osec = other->sk->sk_security;
4761 struct common_audit_data ad;
4762 struct lsm_network_audit net = {0,};
4764 ad.type = LSM_AUDIT_DATA_NET;
4766 ad.u.net->sk = other->sk;
4768 return avc_has_perm(&selinux_state,
4769 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4773 static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4774 char *addrp, u16 family, u32 peer_sid,
4775 struct common_audit_data *ad)
4781 err = sel_netif_sid(ns, ifindex, &if_sid);
4784 err = avc_has_perm(&selinux_state,
4786 SECCLASS_NETIF, NETIF__INGRESS, ad);
4790 err = sel_netnode_sid(addrp, family, &node_sid);
4793 return avc_has_perm(&selinux_state,
4795 SECCLASS_NODE, NODE__RECVFROM, ad);
4798 static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
4802 struct sk_security_struct *sksec = sk->sk_security;
4803 u32 sk_sid = sksec->sid;
4804 struct common_audit_data ad;
4805 struct lsm_network_audit net = {0,};
4808 ad.type = LSM_AUDIT_DATA_NET;
4810 ad.u.net->netif = skb->skb_iif;
4811 ad.u.net->family = family;
4812 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4816 if (selinux_secmark_enabled()) {
4817 err = avc_has_perm(&selinux_state,
4818 sk_sid, skb->secmark, SECCLASS_PACKET,
4824 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4827 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
4832 static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4835 struct sk_security_struct *sksec = sk->sk_security;
4836 u16 family = sk->sk_family;
4837 u32 sk_sid = sksec->sid;
4838 struct common_audit_data ad;
4839 struct lsm_network_audit net = {0,};
4844 if (family != PF_INET && family != PF_INET6)
4847 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
4848 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4851 /* If any sort of compatibility mode is enabled then handoff processing
4852 * to the selinux_sock_rcv_skb_compat() function to deal with the
4853 * special handling. We do this in an attempt to keep this function
4854 * as fast and as clean as possible. */
4855 if (!selinux_policycap_netpeer())
4856 return selinux_sock_rcv_skb_compat(sk, skb, family);
4858 secmark_active = selinux_secmark_enabled();
4859 peerlbl_active = selinux_peerlbl_enabled();
4860 if (!secmark_active && !peerlbl_active)
4863 ad.type = LSM_AUDIT_DATA_NET;
4865 ad.u.net->netif = skb->skb_iif;
4866 ad.u.net->family = family;
4867 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4871 if (peerlbl_active) {
4874 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4877 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4878 addrp, family, peer_sid, &ad);
4880 selinux_netlbl_err(skb, family, err, 0);
4883 err = avc_has_perm(&selinux_state,
4884 sk_sid, peer_sid, SECCLASS_PEER,
4887 selinux_netlbl_err(skb, family, err, 0);
4892 if (secmark_active) {
4893 err = avc_has_perm(&selinux_state,
4894 sk_sid, skb->secmark, SECCLASS_PACKET,
4903 static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4904 int __user *optlen, unsigned len)
4909 struct sk_security_struct *sksec = sock->sk->sk_security;
4910 u32 peer_sid = SECSID_NULL;
4912 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4913 sksec->sclass == SECCLASS_TCP_SOCKET ||
4914 sksec->sclass == SECCLASS_SCTP_SOCKET)
4915 peer_sid = sksec->peer_sid;
4916 if (peer_sid == SECSID_NULL)
4917 return -ENOPROTOOPT;
4919 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
4924 if (scontext_len > len) {
4929 if (copy_to_user(optval, scontext, scontext_len))
4933 if (put_user(scontext_len, optlen))
4939 static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
4941 u32 peer_secid = SECSID_NULL;
4943 struct inode_security_struct *isec;
4945 if (skb && skb->protocol == htons(ETH_P_IP))
4947 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4950 family = sock->sk->sk_family;
4954 if (sock && family == PF_UNIX) {
4955 isec = inode_security_novalidate(SOCK_INODE(sock));
4956 peer_secid = isec->sid;
4958 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
4961 *secid = peer_secid;
4962 if (peer_secid == SECSID_NULL)
4967 static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
4969 struct sk_security_struct *sksec;
4971 sksec = kzalloc(sizeof(*sksec), priority);
4975 sksec->peer_sid = SECINITSID_UNLABELED;
4976 sksec->sid = SECINITSID_UNLABELED;
4977 sksec->sclass = SECCLASS_SOCKET;
4978 selinux_netlbl_sk_security_reset(sksec);
4979 sk->sk_security = sksec;
4984 static void selinux_sk_free_security(struct sock *sk)
4986 struct sk_security_struct *sksec = sk->sk_security;
4988 sk->sk_security = NULL;
4989 selinux_netlbl_sk_security_free(sksec);
4993 static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4995 struct sk_security_struct *sksec = sk->sk_security;
4996 struct sk_security_struct *newsksec = newsk->sk_security;
4998 newsksec->sid = sksec->sid;
4999 newsksec->peer_sid = sksec->peer_sid;
5000 newsksec->sclass = sksec->sclass;
5002 selinux_netlbl_sk_security_reset(newsksec);
5005 static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
5008 *secid = SECINITSID_ANY_SOCKET;
5010 struct sk_security_struct *sksec = sk->sk_security;
5012 *secid = sksec->sid;
5016 static void selinux_sock_graft(struct sock *sk, struct socket *parent)
5018 struct inode_security_struct *isec =
5019 inode_security_novalidate(SOCK_INODE(parent));
5020 struct sk_security_struct *sksec = sk->sk_security;
5022 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5023 sk->sk_family == PF_UNIX)
5024 isec->sid = sksec->sid;
5025 sksec->sclass = isec->sclass;
5028 /* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5029 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5032 static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5033 struct sk_buff *skb)
5035 struct sk_security_struct *sksec = ep->base.sk->sk_security;
5036 struct common_audit_data ad;
5037 struct lsm_network_audit net = {0,};
5039 u32 peer_sid = SECINITSID_UNLABELED;
5043 if (!selinux_policycap_extsockclass())
5046 peerlbl_active = selinux_peerlbl_enabled();
5048 if (peerlbl_active) {
5049 /* This will return peer_sid = SECSID_NULL if there are
5050 * no peer labels, see security_net_peersid_resolve().
5052 err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5057 if (peer_sid == SECSID_NULL)
5058 peer_sid = SECINITSID_UNLABELED;
5061 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5062 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5064 /* Here as first association on socket. As the peer SID
5065 * was allowed by peer recv (and the netif/node checks),
5066 * then it is approved by policy and used as the primary
5067 * peer SID for getpeercon(3).
5069 sksec->peer_sid = peer_sid;
5070 } else if (sksec->peer_sid != peer_sid) {
5071 /* Other association peer SIDs are checked to enforce
5072 * consistency among the peer SIDs.
5074 ad.type = LSM_AUDIT_DATA_NET;
5076 ad.u.net->sk = ep->base.sk;
5077 err = avc_has_perm(&selinux_state,
5078 sksec->peer_sid, peer_sid, sksec->sclass,
5079 SCTP_SOCKET__ASSOCIATION, &ad);
5084 /* Compute the MLS component for the connection and store
5085 * the information in ep. This will be used by SCTP TCP type
5086 * sockets and peeled off connections as they cause a new
5087 * socket to be generated. selinux_sctp_sk_clone() will then
5088 * plug this into the new socket.
5090 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5094 ep->secid = conn_sid;
5095 ep->peer_secid = peer_sid;
5097 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5098 return selinux_netlbl_sctp_assoc_request(ep, skb);
5101 /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5102 * based on their @optname.
5104 static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5105 struct sockaddr *address,
5108 int len, err = 0, walk_size = 0;
5110 struct sockaddr *addr;
5111 struct socket *sock;
5113 if (!selinux_policycap_extsockclass())
5116 /* Process one or more addresses that may be IPv4 or IPv6 */
5117 sock = sk->sk_socket;
5120 while (walk_size < addrlen) {
5121 if (walk_size + sizeof(sa_family_t) > addrlen)
5125 switch (addr->sa_family) {
5128 len = sizeof(struct sockaddr_in);
5131 len = sizeof(struct sockaddr_in6);
5140 case SCTP_PRIMARY_ADDR:
5141 case SCTP_SET_PEER_PRIMARY_ADDR:
5142 case SCTP_SOCKOPT_BINDX_ADD:
5143 err = selinux_socket_bind(sock, addr, len);
5145 /* Connect checks */
5146 case SCTP_SOCKOPT_CONNECTX:
5147 case SCTP_PARAM_SET_PRIMARY:
5148 case SCTP_PARAM_ADD_IP:
5149 case SCTP_SENDMSG_CONNECT:
5150 err = selinux_socket_connect_helper(sock, addr, len);
5154 /* As selinux_sctp_bind_connect() is called by the
5155 * SCTP protocol layer, the socket is already locked,
5156 * therefore selinux_netlbl_socket_connect_locked() is
5157 * is called here. The situations handled are:
5158 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5159 * whenever a new IP address is added or when a new
5160 * primary address is selected.
5161 * Note that an SCTP connect(2) call happens before
5162 * the SCTP protocol layer and is handled via
5163 * selinux_socket_connect().
5165 err = selinux_netlbl_socket_connect_locked(sk, addr);
5179 /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5180 static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5183 struct sk_security_struct *sksec = sk->sk_security;
5184 struct sk_security_struct *newsksec = newsk->sk_security;
5186 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5187 * the non-sctp clone version.
5189 if (!selinux_policycap_extsockclass())
5190 return selinux_sk_clone_security(sk, newsk);
5192 newsksec->sid = ep->secid;
5193 newsksec->peer_sid = ep->peer_secid;
5194 newsksec->sclass = sksec->sclass;
5195 selinux_netlbl_sctp_sk_clone(sk, newsk);
5198 static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
5199 struct request_sock *req)
5201 struct sk_security_struct *sksec = sk->sk_security;
5203 u16 family = req->rsk_ops->family;
5207 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
5210 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5213 req->secid = connsid;
5214 req->peer_secid = peersid;
5216 return selinux_netlbl_inet_conn_request(req, family);
5219 static void selinux_inet_csk_clone(struct sock *newsk,
5220 const struct request_sock *req)
5222 struct sk_security_struct *newsksec = newsk->sk_security;
5224 newsksec->sid = req->secid;
5225 newsksec->peer_sid = req->peer_secid;
5226 /* NOTE: Ideally, we should also get the isec->sid for the
5227 new socket in sync, but we don't have the isec available yet.
5228 So we will wait until sock_graft to do it, by which
5229 time it will have been created and available. */
5231 /* We don't need to take any sort of lock here as we are the only
5232 * thread with access to newsksec */
5233 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
5236 static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
5238 u16 family = sk->sk_family;
5239 struct sk_security_struct *sksec = sk->sk_security;
5241 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5242 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5245 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
5248 static int selinux_secmark_relabel_packet(u32 sid)
5250 const struct task_security_struct *__tsec;
5253 __tsec = selinux_cred(current_cred());
5256 return avc_has_perm(&selinux_state,
5257 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5261 static void selinux_secmark_refcount_inc(void)
5263 atomic_inc(&selinux_secmark_refcount);
5266 static void selinux_secmark_refcount_dec(void)
5268 atomic_dec(&selinux_secmark_refcount);
5271 static void selinux_req_classify_flow(const struct request_sock *req,
5274 fl->flowi_secid = req->secid;
5277 static int selinux_tun_dev_alloc_security(void **security)
5279 struct tun_security_struct *tunsec;
5281 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5284 tunsec->sid = current_sid();
5290 static void selinux_tun_dev_free_security(void *security)
5295 static int selinux_tun_dev_create(void)
5297 u32 sid = current_sid();
5299 /* we aren't taking into account the "sockcreate" SID since the socket
5300 * that is being created here is not a socket in the traditional sense,
5301 * instead it is a private sock, accessible only to the kernel, and
5302 * representing a wide range of network traffic spanning multiple
5303 * connections unlike traditional sockets - check the TUN driver to
5304 * get a better understanding of why this socket is special */
5306 return avc_has_perm(&selinux_state,
5307 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
5311 static int selinux_tun_dev_attach_queue(void *security)
5313 struct tun_security_struct *tunsec = security;
5315 return avc_has_perm(&selinux_state,
5316 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
5317 TUN_SOCKET__ATTACH_QUEUE, NULL);
5320 static int selinux_tun_dev_attach(struct sock *sk, void *security)
5322 struct tun_security_struct *tunsec = security;
5323 struct sk_security_struct *sksec = sk->sk_security;
5325 /* we don't currently perform any NetLabel based labeling here and it
5326 * isn't clear that we would want to do so anyway; while we could apply
5327 * labeling without the support of the TUN user the resulting labeled
5328 * traffic from the other end of the connection would almost certainly
5329 * cause confusion to the TUN user that had no idea network labeling
5330 * protocols were being used */
5332 sksec->sid = tunsec->sid;
5333 sksec->sclass = SECCLASS_TUN_SOCKET;
5338 static int selinux_tun_dev_open(void *security)
5340 struct tun_security_struct *tunsec = security;
5341 u32 sid = current_sid();
5344 err = avc_has_perm(&selinux_state,
5345 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
5346 TUN_SOCKET__RELABELFROM, NULL);
5349 err = avc_has_perm(&selinux_state,
5350 sid, sid, SECCLASS_TUN_SOCKET,
5351 TUN_SOCKET__RELABELTO, NULL);
5359 static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5363 struct nlmsghdr *nlh;
5364 struct sk_security_struct *sksec = sk->sk_security;
5366 if (skb->len < NLMSG_HDRLEN) {
5370 nlh = nlmsg_hdr(skb);
5372 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
5374 if (err == -EINVAL) {
5375 pr_warn_ratelimited("SELinux: unrecognized netlink"
5376 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5377 " pig=%d comm=%s\n",
5378 sk->sk_protocol, nlh->nlmsg_type,
5379 secclass_map[sksec->sclass - 1].name,
5380 task_pid_nr(current), current->comm);
5381 if (!enforcing_enabled(&selinux_state) ||
5382 security_get_allow_unknown(&selinux_state))
5392 err = sock_has_perm(sk, perm);
5397 #ifdef CONFIG_NETFILTER
5399 static unsigned int selinux_ip_forward(struct sk_buff *skb,
5400 const struct net_device *indev,
5406 struct common_audit_data ad;
5407 struct lsm_network_audit net = {0,};
5412 if (!selinux_policycap_netpeer())
5415 secmark_active = selinux_secmark_enabled();
5416 netlbl_active = netlbl_enabled();
5417 peerlbl_active = selinux_peerlbl_enabled();
5418 if (!secmark_active && !peerlbl_active)
5421 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5424 ad.type = LSM_AUDIT_DATA_NET;
5426 ad.u.net->netif = indev->ifindex;
5427 ad.u.net->family = family;
5428 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5431 if (peerlbl_active) {
5432 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5433 addrp, family, peer_sid, &ad);
5435 selinux_netlbl_err(skb, family, err, 1);
5441 if (avc_has_perm(&selinux_state,
5442 peer_sid, skb->secmark,
5443 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5447 /* we do this in the FORWARD path and not the POST_ROUTING
5448 * path because we want to make sure we apply the necessary
5449 * labeling before IPsec is applied so we can leverage AH
5451 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5457 static unsigned int selinux_ipv4_forward(void *priv,
5458 struct sk_buff *skb,
5459 const struct nf_hook_state *state)
5461 return selinux_ip_forward(skb, state->in, PF_INET);
5464 #if IS_ENABLED(CONFIG_IPV6)
5465 static unsigned int selinux_ipv6_forward(void *priv,
5466 struct sk_buff *skb,
5467 const struct nf_hook_state *state)
5469 return selinux_ip_forward(skb, state->in, PF_INET6);
5473 static unsigned int selinux_ip_output(struct sk_buff *skb,
5479 if (!netlbl_enabled())
5482 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5483 * because we want to make sure we apply the necessary labeling
5484 * before IPsec is applied so we can leverage AH protection */
5487 struct sk_security_struct *sksec;
5489 if (sk_listener(sk))
5490 /* if the socket is the listening state then this
5491 * packet is a SYN-ACK packet which means it needs to
5492 * be labeled based on the connection/request_sock and
5493 * not the parent socket. unfortunately, we can't
5494 * lookup the request_sock yet as it isn't queued on
5495 * the parent socket until after the SYN-ACK is sent.
5496 * the "solution" is to simply pass the packet as-is
5497 * as any IP option based labeling should be copied
5498 * from the initial connection request (in the IP
5499 * layer). it is far from ideal, but until we get a
5500 * security label in the packet itself this is the
5501 * best we can do. */
5504 /* standard practice, label using the parent socket */
5505 sksec = sk->sk_security;
5508 sid = SECINITSID_KERNEL;
5509 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5515 static unsigned int selinux_ipv4_output(void *priv,
5516 struct sk_buff *skb,
5517 const struct nf_hook_state *state)
5519 return selinux_ip_output(skb, PF_INET);
5522 #if IS_ENABLED(CONFIG_IPV6)
5523 static unsigned int selinux_ipv6_output(void *priv,
5524 struct sk_buff *skb,
5525 const struct nf_hook_state *state)
5527 return selinux_ip_output(skb, PF_INET6);
5531 static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5535 struct sock *sk = skb_to_full_sk(skb);
5536 struct sk_security_struct *sksec;
5537 struct common_audit_data ad;
5538 struct lsm_network_audit net = {0,};
5544 sksec = sk->sk_security;
5546 ad.type = LSM_AUDIT_DATA_NET;
5548 ad.u.net->netif = ifindex;
5549 ad.u.net->family = family;
5550 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5553 if (selinux_secmark_enabled())
5554 if (avc_has_perm(&selinux_state,
5555 sksec->sid, skb->secmark,
5556 SECCLASS_PACKET, PACKET__SEND, &ad))
5557 return NF_DROP_ERR(-ECONNREFUSED);
5559 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5560 return NF_DROP_ERR(-ECONNREFUSED);
5565 static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5566 const struct net_device *outdev,
5571 int ifindex = outdev->ifindex;
5573 struct common_audit_data ad;
5574 struct lsm_network_audit net = {0,};
5579 /* If any sort of compatibility mode is enabled then handoff processing
5580 * to the selinux_ip_postroute_compat() function to deal with the
5581 * special handling. We do this in an attempt to keep this function
5582 * as fast and as clean as possible. */
5583 if (!selinux_policycap_netpeer())
5584 return selinux_ip_postroute_compat(skb, ifindex, family);
5586 secmark_active = selinux_secmark_enabled();
5587 peerlbl_active = selinux_peerlbl_enabled();
5588 if (!secmark_active && !peerlbl_active)
5591 sk = skb_to_full_sk(skb);
5594 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5595 * packet transformation so allow the packet to pass without any checks
5596 * since we'll have another chance to perform access control checks
5597 * when the packet is on it's final way out.
5598 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
5599 * is NULL, in this case go ahead and apply access control.
5600 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5601 * TCP listening state we cannot wait until the XFRM processing
5602 * is done as we will miss out on the SA label if we do;
5603 * unfortunately, this means more work, but it is only once per
5605 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
5606 !(sk && sk_listener(sk)))
5611 /* Without an associated socket the packet is either coming
5612 * from the kernel or it is being forwarded; check the packet
5613 * to determine which and if the packet is being forwarded
5614 * query the packet directly to determine the security label. */
5616 secmark_perm = PACKET__FORWARD_OUT;
5617 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
5620 secmark_perm = PACKET__SEND;
5621 peer_sid = SECINITSID_KERNEL;
5623 } else if (sk_listener(sk)) {
5624 /* Locally generated packet but the associated socket is in the
5625 * listening state which means this is a SYN-ACK packet. In
5626 * this particular case the correct security label is assigned
5627 * to the connection/request_sock but unfortunately we can't
5628 * query the request_sock as it isn't queued on the parent
5629 * socket until after the SYN-ACK packet is sent; the only
5630 * viable choice is to regenerate the label like we do in
5631 * selinux_inet_conn_request(). See also selinux_ip_output()
5632 * for similar problems. */
5634 struct sk_security_struct *sksec;
5636 sksec = sk->sk_security;
5637 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5639 /* At this point, if the returned skb peerlbl is SECSID_NULL
5640 * and the packet has been through at least one XFRM
5641 * transformation then we must be dealing with the "final"
5642 * form of labeled IPsec packet; since we've already applied
5643 * all of our access controls on this packet we can safely
5644 * pass the packet. */
5645 if (skb_sid == SECSID_NULL) {
5648 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5652 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5656 return NF_DROP_ERR(-ECONNREFUSED);
5659 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5661 secmark_perm = PACKET__SEND;
5663 /* Locally generated packet, fetch the security label from the
5664 * associated socket. */
5665 struct sk_security_struct *sksec = sk->sk_security;
5666 peer_sid = sksec->sid;
5667 secmark_perm = PACKET__SEND;
5670 ad.type = LSM_AUDIT_DATA_NET;
5672 ad.u.net->netif = ifindex;
5673 ad.u.net->family = family;
5674 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
5678 if (avc_has_perm(&selinux_state,
5679 peer_sid, skb->secmark,
5680 SECCLASS_PACKET, secmark_perm, &ad))
5681 return NF_DROP_ERR(-ECONNREFUSED);
5683 if (peerlbl_active) {
5687 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
5689 if (avc_has_perm(&selinux_state,
5691 SECCLASS_NETIF, NETIF__EGRESS, &ad))
5692 return NF_DROP_ERR(-ECONNREFUSED);
5694 if (sel_netnode_sid(addrp, family, &node_sid))
5696 if (avc_has_perm(&selinux_state,
5698 SECCLASS_NODE, NODE__SENDTO, &ad))
5699 return NF_DROP_ERR(-ECONNREFUSED);
5705 static unsigned int selinux_ipv4_postroute(void *priv,
5706 struct sk_buff *skb,
5707 const struct nf_hook_state *state)
5709 return selinux_ip_postroute(skb, state->out, PF_INET);
5712 #if IS_ENABLED(CONFIG_IPV6)
5713 static unsigned int selinux_ipv6_postroute(void *priv,
5714 struct sk_buff *skb,
5715 const struct nf_hook_state *state)
5717 return selinux_ip_postroute(skb, state->out, PF_INET6);
5721 #endif /* CONFIG_NETFILTER */
5723 static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5725 return selinux_nlmsg_perm(sk, skb);
5728 static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass)
5730 isec->sclass = sclass;
5731 isec->sid = current_sid();
5734 static int msg_msg_alloc_security(struct msg_msg *msg)
5736 struct msg_security_struct *msec;
5738 msec = selinux_msg_msg(msg);
5739 msec->sid = SECINITSID_UNLABELED;
5744 static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
5747 struct ipc_security_struct *isec;
5748 struct common_audit_data ad;
5749 u32 sid = current_sid();
5751 isec = selinux_ipc(ipc_perms);
5753 ad.type = LSM_AUDIT_DATA_IPC;
5754 ad.u.ipc_id = ipc_perms->key;
5756 return avc_has_perm(&selinux_state,
5757 sid, isec->sid, isec->sclass, perms, &ad);
5760 static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5762 return msg_msg_alloc_security(msg);
5765 /* message queue security operations */
5766 static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
5768 struct ipc_security_struct *isec;
5769 struct common_audit_data ad;
5770 u32 sid = current_sid();
5773 isec = selinux_ipc(msq);
5774 ipc_init_security(isec, SECCLASS_MSGQ);
5776 ad.type = LSM_AUDIT_DATA_IPC;
5777 ad.u.ipc_id = msq->key;
5779 rc = avc_has_perm(&selinux_state,
5780 sid, isec->sid, SECCLASS_MSGQ,
5785 static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
5787 struct ipc_security_struct *isec;
5788 struct common_audit_data ad;
5789 u32 sid = current_sid();
5791 isec = selinux_ipc(msq);
5793 ad.type = LSM_AUDIT_DATA_IPC;
5794 ad.u.ipc_id = msq->key;
5796 return avc_has_perm(&selinux_state,
5797 sid, isec->sid, SECCLASS_MSGQ,
5798 MSGQ__ASSOCIATE, &ad);
5801 static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
5809 /* No specific object, just general system-wide information. */
5810 return avc_has_perm(&selinux_state,
5811 current_sid(), SECINITSID_KERNEL,
5812 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
5816 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5819 perms = MSGQ__SETATTR;
5822 perms = MSGQ__DESTROY;
5828 err = ipc_has_perm(msq, perms);
5832 static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
5834 struct ipc_security_struct *isec;
5835 struct msg_security_struct *msec;
5836 struct common_audit_data ad;
5837 u32 sid = current_sid();
5840 isec = selinux_ipc(msq);
5841 msec = selinux_msg_msg(msg);
5844 * First time through, need to assign label to the message
5846 if (msec->sid == SECINITSID_UNLABELED) {
5848 * Compute new sid based on current process and
5849 * message queue this message will be stored in
5851 rc = security_transition_sid(&selinux_state, sid, isec->sid,
5852 SECCLASS_MSG, NULL, &msec->sid);
5857 ad.type = LSM_AUDIT_DATA_IPC;
5858 ad.u.ipc_id = msq->key;
5860 /* Can this process write to the queue? */
5861 rc = avc_has_perm(&selinux_state,
5862 sid, isec->sid, SECCLASS_MSGQ,
5865 /* Can this process send the message */
5866 rc = avc_has_perm(&selinux_state,
5867 sid, msec->sid, SECCLASS_MSG,
5870 /* Can the message be put in the queue? */
5871 rc = avc_has_perm(&selinux_state,
5872 msec->sid, isec->sid, SECCLASS_MSGQ,
5873 MSGQ__ENQUEUE, &ad);
5878 static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
5879 struct task_struct *target,
5880 long type, int mode)
5882 struct ipc_security_struct *isec;
5883 struct msg_security_struct *msec;
5884 struct common_audit_data ad;
5885 u32 sid = task_sid(target);
5888 isec = selinux_ipc(msq);
5889 msec = selinux_msg_msg(msg);
5891 ad.type = LSM_AUDIT_DATA_IPC;
5892 ad.u.ipc_id = msq->key;
5894 rc = avc_has_perm(&selinux_state,
5896 SECCLASS_MSGQ, MSGQ__READ, &ad);
5898 rc = avc_has_perm(&selinux_state,
5900 SECCLASS_MSG, MSG__RECEIVE, &ad);
5904 /* Shared Memory security operations */
5905 static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
5907 struct ipc_security_struct *isec;
5908 struct common_audit_data ad;
5909 u32 sid = current_sid();
5912 isec = selinux_ipc(shp);
5913 ipc_init_security(isec, SECCLASS_SHM);
5915 ad.type = LSM_AUDIT_DATA_IPC;
5916 ad.u.ipc_id = shp->key;
5918 rc = avc_has_perm(&selinux_state,
5919 sid, isec->sid, SECCLASS_SHM,
5924 static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
5926 struct ipc_security_struct *isec;
5927 struct common_audit_data ad;
5928 u32 sid = current_sid();
5930 isec = selinux_ipc(shp);
5932 ad.type = LSM_AUDIT_DATA_IPC;
5933 ad.u.ipc_id = shp->key;
5935 return avc_has_perm(&selinux_state,
5936 sid, isec->sid, SECCLASS_SHM,
5937 SHM__ASSOCIATE, &ad);
5940 /* Note, at this point, shp is locked down */
5941 static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
5949 /* No specific object, just general system-wide information. */
5950 return avc_has_perm(&selinux_state,
5951 current_sid(), SECINITSID_KERNEL,
5952 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
5956 perms = SHM__GETATTR | SHM__ASSOCIATE;
5959 perms = SHM__SETATTR;
5966 perms = SHM__DESTROY;
5972 err = ipc_has_perm(shp, perms);
5976 static int selinux_shm_shmat(struct kern_ipc_perm *shp,
5977 char __user *shmaddr, int shmflg)
5981 if (shmflg & SHM_RDONLY)
5984 perms = SHM__READ | SHM__WRITE;
5986 return ipc_has_perm(shp, perms);
5989 /* Semaphore security operations */
5990 static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
5992 struct ipc_security_struct *isec;
5993 struct common_audit_data ad;
5994 u32 sid = current_sid();
5997 isec = selinux_ipc(sma);
5998 ipc_init_security(isec, SECCLASS_SEM);
6000 ad.type = LSM_AUDIT_DATA_IPC;
6001 ad.u.ipc_id = sma->key;
6003 rc = avc_has_perm(&selinux_state,
6004 sid, isec->sid, SECCLASS_SEM,
6009 static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
6011 struct ipc_security_struct *isec;
6012 struct common_audit_data ad;
6013 u32 sid = current_sid();
6015 isec = selinux_ipc(sma);
6017 ad.type = LSM_AUDIT_DATA_IPC;
6018 ad.u.ipc_id = sma->key;
6020 return avc_has_perm(&selinux_state,
6021 sid, isec->sid, SECCLASS_SEM,
6022 SEM__ASSOCIATE, &ad);
6025 /* Note, at this point, sma is locked down */
6026 static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
6034 /* No specific object, just general system-wide information. */
6035 return avc_has_perm(&selinux_state,
6036 current_sid(), SECINITSID_KERNEL,
6037 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
6041 perms = SEM__GETATTR;
6052 perms = SEM__DESTROY;
6055 perms = SEM__SETATTR;
6060 perms = SEM__GETATTR | SEM__ASSOCIATE;
6066 err = ipc_has_perm(sma, perms);
6070 static int selinux_sem_semop(struct kern_ipc_perm *sma,
6071 struct sembuf *sops, unsigned nsops, int alter)
6076 perms = SEM__READ | SEM__WRITE;
6080 return ipc_has_perm(sma, perms);
6083 static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6089 av |= IPC__UNIX_READ;
6091 av |= IPC__UNIX_WRITE;
6096 return ipc_has_perm(ipcp, av);
6099 static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6101 struct ipc_security_struct *isec = selinux_ipc(ipcp);
6105 static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
6108 inode_doinit_with_dentry(inode, dentry);
6111 static int selinux_getprocattr(struct task_struct *p,
6112 char *name, char **value)
6114 const struct task_security_struct *__tsec;
6120 __tsec = selinux_cred(__task_cred(p));
6123 error = avc_has_perm(&selinux_state,
6124 current_sid(), __tsec->sid,
6125 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6130 if (!strcmp(name, "current"))
6132 else if (!strcmp(name, "prev"))
6134 else if (!strcmp(name, "exec"))
6135 sid = __tsec->exec_sid;
6136 else if (!strcmp(name, "fscreate"))
6137 sid = __tsec->create_sid;
6138 else if (!strcmp(name, "keycreate"))
6139 sid = __tsec->keycreate_sid;
6140 else if (!strcmp(name, "sockcreate"))
6141 sid = __tsec->sockcreate_sid;
6151 error = security_sid_to_context(&selinux_state, sid, value, &len);
6161 static int selinux_setprocattr(const char *name, void *value, size_t size)
6163 struct task_security_struct *tsec;
6165 u32 mysid = current_sid(), sid = 0, ptsid;
6170 * Basic control over ability to set these attributes at all.
6172 if (!strcmp(name, "exec"))
6173 error = avc_has_perm(&selinux_state,
6174 mysid, mysid, SECCLASS_PROCESS,
6175 PROCESS__SETEXEC, NULL);
6176 else if (!strcmp(name, "fscreate"))
6177 error = avc_has_perm(&selinux_state,
6178 mysid, mysid, SECCLASS_PROCESS,
6179 PROCESS__SETFSCREATE, NULL);
6180 else if (!strcmp(name, "keycreate"))
6181 error = avc_has_perm(&selinux_state,
6182 mysid, mysid, SECCLASS_PROCESS,
6183 PROCESS__SETKEYCREATE, NULL);
6184 else if (!strcmp(name, "sockcreate"))
6185 error = avc_has_perm(&selinux_state,
6186 mysid, mysid, SECCLASS_PROCESS,
6187 PROCESS__SETSOCKCREATE, NULL);
6188 else if (!strcmp(name, "current"))
6189 error = avc_has_perm(&selinux_state,
6190 mysid, mysid, SECCLASS_PROCESS,
6191 PROCESS__SETCURRENT, NULL);
6197 /* Obtain a SID for the context, if one was specified. */
6198 if (size && str[0] && str[0] != '\n') {
6199 if (str[size-1] == '\n') {
6203 error = security_context_to_sid(&selinux_state, value, size,
6205 if (error == -EINVAL && !strcmp(name, "fscreate")) {
6206 if (!has_cap_mac_admin(true)) {
6207 struct audit_buffer *ab;
6210 /* We strip a nul only if it is at the end, otherwise the
6211 * context contains a nul and we should audit that */
6212 if (str[size - 1] == '\0')
6213 audit_size = size - 1;
6216 ab = audit_log_start(audit_context(),
6219 audit_log_format(ab, "op=fscreate invalid_context=");
6220 audit_log_n_untrustedstring(ab, value, audit_size);
6225 error = security_context_to_sid_force(
6233 new = prepare_creds();
6237 /* Permission checking based on the specified context is
6238 performed during the actual operation (execve,
6239 open/mkdir/...), when we know the full context of the
6240 operation. See selinux_bprm_set_creds for the execve
6241 checks and may_create for the file creation checks. The
6242 operation will then fail if the context is not permitted. */
6243 tsec = selinux_cred(new);
6244 if (!strcmp(name, "exec")) {
6245 tsec->exec_sid = sid;
6246 } else if (!strcmp(name, "fscreate")) {
6247 tsec->create_sid = sid;
6248 } else if (!strcmp(name, "keycreate")) {
6249 error = avc_has_perm(&selinux_state,
6250 mysid, sid, SECCLASS_KEY, KEY__CREATE,
6254 tsec->keycreate_sid = sid;
6255 } else if (!strcmp(name, "sockcreate")) {
6256 tsec->sockcreate_sid = sid;
6257 } else if (!strcmp(name, "current")) {
6262 /* Only allow single threaded processes to change context */
6264 if (!current_is_single_threaded()) {
6265 error = security_bounded_transition(&selinux_state,
6271 /* Check permissions for the transition. */
6272 error = avc_has_perm(&selinux_state,
6273 tsec->sid, sid, SECCLASS_PROCESS,
6274 PROCESS__DYNTRANSITION, NULL);
6278 /* Check for ptracing, and update the task SID if ok.
6279 Otherwise, leave SID unchanged and fail. */
6280 ptsid = ptrace_parent_sid();
6282 error = avc_has_perm(&selinux_state,
6283 ptsid, sid, SECCLASS_PROCESS,
6284 PROCESS__PTRACE, NULL);
6303 static int selinux_ismaclabel(const char *name)
6305 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6308 static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6310 return security_sid_to_context(&selinux_state, secid,
6314 static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
6316 return security_context_to_sid(&selinux_state, secdata, seclen,
6320 static void selinux_release_secctx(char *secdata, u32 seclen)
6325 static void selinux_inode_invalidate_secctx(struct inode *inode)
6327 struct inode_security_struct *isec = selinux_inode(inode);
6329 spin_lock(&isec->lock);
6330 isec->initialized = LABEL_INVALID;
6331 spin_unlock(&isec->lock);
6335 * called with inode->i_mutex locked
6337 static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6339 int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
6341 /* Do not return error when suppressing label (SBLABEL_MNT not set). */
6342 return rc == -EOPNOTSUPP ? 0 : rc;
6346 * called with inode->i_mutex locked
6348 static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6350 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6353 static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6356 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6365 static int selinux_key_alloc(struct key *k, const struct cred *cred,
6366 unsigned long flags)
6368 const struct task_security_struct *tsec;
6369 struct key_security_struct *ksec;
6371 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6375 tsec = selinux_cred(cred);
6376 if (tsec->keycreate_sid)
6377 ksec->sid = tsec->keycreate_sid;
6379 ksec->sid = tsec->sid;
6385 static void selinux_key_free(struct key *k)
6387 struct key_security_struct *ksec = k->security;
6393 static int selinux_key_permission(key_ref_t key_ref,
6394 const struct cred *cred,
6398 struct key_security_struct *ksec;
6401 /* if no specific permissions are requested, we skip the
6402 permission check. No serious, additional covert channels
6403 appear to be created. */
6407 sid = cred_sid(cred);
6409 key = key_ref_to_ptr(key_ref);
6410 ksec = key->security;
6412 return avc_has_perm(&selinux_state,
6413 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
6416 static int selinux_key_getsecurity(struct key *key, char **_buffer)
6418 struct key_security_struct *ksec = key->security;
6419 char *context = NULL;
6423 rc = security_sid_to_context(&selinux_state, ksec->sid,
6432 #ifdef CONFIG_SECURITY_INFINIBAND
6433 static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6435 struct common_audit_data ad;
6438 struct ib_security_struct *sec = ib_sec;
6439 struct lsm_ibpkey_audit ibpkey;
6441 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
6445 ad.type = LSM_AUDIT_DATA_IBPKEY;
6446 ibpkey.subnet_prefix = subnet_prefix;
6447 ibpkey.pkey = pkey_val;
6448 ad.u.ibpkey = &ibpkey;
6449 return avc_has_perm(&selinux_state,
6451 SECCLASS_INFINIBAND_PKEY,
6452 INFINIBAND_PKEY__ACCESS, &ad);
6455 static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6458 struct common_audit_data ad;
6461 struct ib_security_struct *sec = ib_sec;
6462 struct lsm_ibendport_audit ibendport;
6464 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6470 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6471 strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6472 ibendport.port = port_num;
6473 ad.u.ibendport = &ibendport;
6474 return avc_has_perm(&selinux_state,
6476 SECCLASS_INFINIBAND_ENDPORT,
6477 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6480 static int selinux_ib_alloc_security(void **ib_sec)
6482 struct ib_security_struct *sec;
6484 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6487 sec->sid = current_sid();
6493 static void selinux_ib_free_security(void *ib_sec)
6499 #ifdef CONFIG_BPF_SYSCALL
6500 static int selinux_bpf(int cmd, union bpf_attr *attr,
6503 u32 sid = current_sid();
6507 case BPF_MAP_CREATE:
6508 ret = avc_has_perm(&selinux_state,
6509 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
6513 ret = avc_has_perm(&selinux_state,
6514 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
6525 static u32 bpf_map_fmode_to_av(fmode_t fmode)
6529 if (fmode & FMODE_READ)
6530 av |= BPF__MAP_READ;
6531 if (fmode & FMODE_WRITE)
6532 av |= BPF__MAP_WRITE;
6536 /* This function will check the file pass through unix socket or binder to see
6537 * if it is a bpf related object. And apply correspinding checks on the bpf
6538 * object based on the type. The bpf maps and programs, not like other files and
6539 * socket, are using a shared anonymous inode inside the kernel as their inode.
6540 * So checking that inode cannot identify if the process have privilege to
6541 * access the bpf object and that's why we have to add this additional check in
6542 * selinux_file_receive and selinux_binder_transfer_files.
6544 static int bpf_fd_pass(struct file *file, u32 sid)
6546 struct bpf_security_struct *bpfsec;
6547 struct bpf_prog *prog;
6548 struct bpf_map *map;
6551 if (file->f_op == &bpf_map_fops) {
6552 map = file->private_data;
6553 bpfsec = map->security;
6554 ret = avc_has_perm(&selinux_state,
6555 sid, bpfsec->sid, SECCLASS_BPF,
6556 bpf_map_fmode_to_av(file->f_mode), NULL);
6559 } else if (file->f_op == &bpf_prog_fops) {
6560 prog = file->private_data;
6561 bpfsec = prog->aux->security;
6562 ret = avc_has_perm(&selinux_state,
6563 sid, bpfsec->sid, SECCLASS_BPF,
6564 BPF__PROG_RUN, NULL);
6571 static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6573 u32 sid = current_sid();
6574 struct bpf_security_struct *bpfsec;
6576 bpfsec = map->security;
6577 return avc_has_perm(&selinux_state,
6578 sid, bpfsec->sid, SECCLASS_BPF,
6579 bpf_map_fmode_to_av(fmode), NULL);
6582 static int selinux_bpf_prog(struct bpf_prog *prog)
6584 u32 sid = current_sid();
6585 struct bpf_security_struct *bpfsec;
6587 bpfsec = prog->aux->security;
6588 return avc_has_perm(&selinux_state,
6589 sid, bpfsec->sid, SECCLASS_BPF,
6590 BPF__PROG_RUN, NULL);
6593 static int selinux_bpf_map_alloc(struct bpf_map *map)
6595 struct bpf_security_struct *bpfsec;
6597 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6601 bpfsec->sid = current_sid();
6602 map->security = bpfsec;
6607 static void selinux_bpf_map_free(struct bpf_map *map)
6609 struct bpf_security_struct *bpfsec = map->security;
6611 map->security = NULL;
6615 static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6617 struct bpf_security_struct *bpfsec;
6619 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6623 bpfsec->sid = current_sid();
6624 aux->security = bpfsec;
6629 static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6631 struct bpf_security_struct *bpfsec = aux->security;
6633 aux->security = NULL;
6638 struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
6639 .lbs_cred = sizeof(struct task_security_struct),
6640 .lbs_file = sizeof(struct file_security_struct),
6641 .lbs_inode = sizeof(struct inode_security_struct),
6642 .lbs_ipc = sizeof(struct ipc_security_struct),
6643 .lbs_msg_msg = sizeof(struct msg_security_struct),
6646 static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
6647 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6648 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6649 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6650 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
6652 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6653 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6654 LSM_HOOK_INIT(capget, selinux_capget),
6655 LSM_HOOK_INIT(capset, selinux_capset),
6656 LSM_HOOK_INIT(capable, selinux_capable),
6657 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6658 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6659 LSM_HOOK_INIT(syslog, selinux_syslog),
6660 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
6662 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
6664 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6665 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6666 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
6668 LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup),
6669 LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),
6671 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6672 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
6673 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
6674 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
6675 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6676 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6677 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6678 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6679 LSM_HOOK_INIT(sb_mount, selinux_mount),
6680 LSM_HOOK_INIT(sb_umount, selinux_umount),
6681 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6682 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
6683 LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt),
6685 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
6686 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
6688 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6689 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6690 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6691 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6692 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6693 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6694 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6695 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6696 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6697 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6698 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6699 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6700 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6701 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6702 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6703 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6704 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6705 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6706 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6707 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6708 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6709 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6710 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6711 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6712 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
6713 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
6714 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
6716 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6717 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6718 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6719 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6720 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6721 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6722 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6723 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6724 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6725 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6726 LSM_HOOK_INIT(file_receive, selinux_file_receive),
6728 LSM_HOOK_INIT(file_open, selinux_file_open),
6730 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
6731 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6732 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
6733 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
6734 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6735 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6736 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
6737 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
6738 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
6739 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6740 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6741 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6742 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6743 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6744 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6745 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
6746 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
6747 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6748 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6749 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6750 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6751 LSM_HOOK_INIT(task_kill, selinux_task_kill),
6752 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
6754 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6755 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
6757 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6759 LSM_HOOK_INIT(msg_queue_alloc_security,
6760 selinux_msg_queue_alloc_security),
6761 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6762 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6763 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6764 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
6766 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6767 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6768 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6769 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
6771 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6772 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6773 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6774 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
6776 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
6778 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6779 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
6781 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6782 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6783 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6784 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
6785 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
6786 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6787 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6788 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
6790 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6791 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
6793 LSM_HOOK_INIT(socket_create, selinux_socket_create),
6794 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
6795 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
6796 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6797 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6798 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6799 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6800 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6801 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6802 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6803 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6804 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6805 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6806 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6807 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6808 LSM_HOOK_INIT(socket_getpeersec_stream,
6809 selinux_socket_getpeersec_stream),
6810 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6811 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6812 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6813 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6814 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6815 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
6816 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
6817 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
6818 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
6819 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6820 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6821 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6822 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6823 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6824 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6825 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6826 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6827 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6828 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6829 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6830 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6831 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
6832 #ifdef CONFIG_SECURITY_INFINIBAND
6833 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
6834 LSM_HOOK_INIT(ib_endport_manage_subnet,
6835 selinux_ib_endport_manage_subnet),
6836 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
6837 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
6839 #ifdef CONFIG_SECURITY_NETWORK_XFRM
6840 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6841 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6842 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6843 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6844 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6845 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6846 selinux_xfrm_state_alloc_acquire),
6847 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6848 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6849 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6850 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6851 selinux_xfrm_state_pol_flow_match),
6852 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
6856 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6857 LSM_HOOK_INIT(key_free, selinux_key_free),
6858 LSM_HOOK_INIT(key_permission, selinux_key_permission),
6859 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
6863 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6864 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6865 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6866 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
6869 #ifdef CONFIG_BPF_SYSCALL
6870 LSM_HOOK_INIT(bpf, selinux_bpf),
6871 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
6872 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
6873 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
6874 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
6875 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
6876 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
6880 static __init int selinux_init(void)
6882 pr_info("SELinux: Initializing.\n");
6884 memset(&selinux_state, 0, sizeof(selinux_state));
6885 enforcing_set(&selinux_state, selinux_enforcing_boot);
6886 selinux_state.checkreqprot = selinux_checkreqprot_boot;
6887 selinux_ss_init(&selinux_state.ss);
6888 selinux_avc_init(&selinux_state.avc);
6890 /* Set the security state for the initial task. */
6891 cred_init_security();
6893 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
6899 ebitmap_cache_init();
6901 hashtab_cache_init();
6903 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
6905 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
6906 panic("SELinux: Unable to register AVC netcache callback\n");
6908 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
6909 panic("SELinux: Unable to register AVC LSM notifier callback\n");
6911 if (selinux_enforcing_boot)
6912 pr_debug("SELinux: Starting in enforcing mode\n");
6914 pr_debug("SELinux: Starting in permissive mode\n");
6916 fs_validate_description(&selinux_fs_parameters);
6921 static void delayed_superblock_init(struct super_block *sb, void *unused)
6923 selinux_set_mnt_opts(sb, NULL, 0, NULL);
6926 void selinux_complete_init(void)
6928 pr_debug("SELinux: Completing initialization.\n");
6930 /* Set up any superblocks initialized prior to the policy load. */
6931 pr_debug("SELinux: Setting up existing superblocks.\n");
6932 iterate_supers(delayed_superblock_init, NULL);
6935 /* SELinux requires early initialization in order to label
6936 all processes and objects when they are created. */
6937 DEFINE_LSM(selinux) = {
6939 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
6940 .enabled = &selinux_enabled,
6941 .blobs = &selinux_blob_sizes,
6942 .init = selinux_init,
6945 #if defined(CONFIG_NETFILTER)
6947 static const struct nf_hook_ops selinux_nf_ops[] = {
6949 .hook = selinux_ipv4_postroute,
6951 .hooknum = NF_INET_POST_ROUTING,
6952 .priority = NF_IP_PRI_SELINUX_LAST,
6955 .hook = selinux_ipv4_forward,
6957 .hooknum = NF_INET_FORWARD,
6958 .priority = NF_IP_PRI_SELINUX_FIRST,
6961 .hook = selinux_ipv4_output,
6963 .hooknum = NF_INET_LOCAL_OUT,
6964 .priority = NF_IP_PRI_SELINUX_FIRST,
6966 #if IS_ENABLED(CONFIG_IPV6)
6968 .hook = selinux_ipv6_postroute,
6970 .hooknum = NF_INET_POST_ROUTING,
6971 .priority = NF_IP6_PRI_SELINUX_LAST,
6974 .hook = selinux_ipv6_forward,
6976 .hooknum = NF_INET_FORWARD,
6977 .priority = NF_IP6_PRI_SELINUX_FIRST,
6980 .hook = selinux_ipv6_output,
6982 .hooknum = NF_INET_LOCAL_OUT,
6983 .priority = NF_IP6_PRI_SELINUX_FIRST,
6988 static int __net_init selinux_nf_register(struct net *net)
6990 return nf_register_net_hooks(net, selinux_nf_ops,
6991 ARRAY_SIZE(selinux_nf_ops));
6994 static void __net_exit selinux_nf_unregister(struct net *net)
6996 nf_unregister_net_hooks(net, selinux_nf_ops,
6997 ARRAY_SIZE(selinux_nf_ops));
7000 static struct pernet_operations selinux_net_ops = {
7001 .init = selinux_nf_register,
7002 .exit = selinux_nf_unregister,
7005 static int __init selinux_nf_ip_init(void)
7009 if (!selinux_enabled)
7012 pr_debug("SELinux: Registering netfilter hooks\n");
7014 err = register_pernet_subsys(&selinux_net_ops);
7016 panic("SELinux: register_pernet_subsys: error %d\n", err);
7020 __initcall(selinux_nf_ip_init);
7022 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7023 static void selinux_nf_ip_exit(void)
7025 pr_debug("SELinux: Unregistering netfilter hooks\n");
7027 unregister_pernet_subsys(&selinux_net_ops);
7031 #else /* CONFIG_NETFILTER */
7033 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7034 #define selinux_nf_ip_exit()
7037 #endif /* CONFIG_NETFILTER */
7039 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7040 int selinux_disable(struct selinux_state *state)
7042 if (state->initialized) {
7043 /* Not permitted after initial policy load. */
7047 if (state->disabled) {
7048 /* Only do this once. */
7052 state->disabled = 1;
7054 pr_info("SELinux: Disabled at runtime.\n");
7056 selinux_enabled = 0;
7058 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
7060 /* Try to destroy the avc node cache */
7063 /* Unregister netfilter hooks. */
7064 selinux_nf_ip_exit();
7066 /* Unregister selinuxfs. */