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/netfilter_ipv4.h>
52 #include <linux/netfilter_ipv6.h>
53 #include <linux/tty.h>
55 #include <net/ip.h> /* for local_port_range[] */
56 #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
57 #include <net/inet_connection_sock.h>
58 #include <net/net_namespace.h>
59 #include <net/netlabel.h>
60 #include <linux/uaccess.h>
61 #include <asm/ioctls.h>
62 #include <linux/atomic.h>
63 #include <linux/bitops.h>
64 #include <linux/interrupt.h>
65 #include <linux/netdevice.h> /* for network interface checks */
66 #include <net/netlink.h>
67 #include <linux/tcp.h>
68 #include <linux/udp.h>
69 #include <linux/dccp.h>
70 #include <linux/sctp.h>
71 #include <net/sctp/structs.h>
72 #include <linux/quota.h>
73 #include <linux/un.h> /* for Unix socket types */
74 #include <net/af_unix.h> /* for Unix socket types */
75 #include <linux/parser.h>
76 #include <linux/nfs_mount.h>
78 #include <linux/hugetlb.h>
79 #include <linux/personality.h>
80 #include <linux/audit.h>
81 #include <linux/string.h>
82 #include <linux/selinux.h>
83 #include <linux/mutex.h>
84 #include <linux/posix-timers.h>
85 #include <linux/syslog.h>
86 #include <linux/user_namespace.h>
87 #include <linux/export.h>
88 #include <linux/msg.h>
89 #include <linux/shm.h>
90 #include <linux/bpf.h>
91 #include <uapi/linux/mount.h>
100 #include "netlabel.h"
104 struct selinux_state selinux_state;
106 /* SECMARK reference count */
107 static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
109 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
110 static int selinux_enforcing_boot;
112 static int __init enforcing_setup(char *str)
114 unsigned long enforcing;
115 if (!kstrtoul(str, 0, &enforcing))
116 selinux_enforcing_boot = enforcing ? 1 : 0;
119 __setup("enforcing=", enforcing_setup);
121 #define selinux_enforcing_boot 1
124 #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
125 int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
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);
136 int selinux_enabled = 1;
139 static unsigned int selinux_checkreqprot_boot =
140 CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
142 static int __init checkreqprot_setup(char *str)
144 unsigned long checkreqprot;
146 if (!kstrtoul(str, 0, &checkreqprot))
147 selinux_checkreqprot_boot = checkreqprot ? 1 : 0;
150 __setup("checkreqprot=", checkreqprot_setup);
152 static struct kmem_cache *sel_inode_cache;
153 static struct kmem_cache *file_security_cache;
156 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
159 * This function checks the SECMARK reference counter to see if any SECMARK
160 * targets are currently configured, if the reference counter is greater than
161 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
162 * enabled, false (0) if SECMARK is disabled. If the always_check_network
163 * policy capability is enabled, SECMARK is always considered enabled.
166 static int selinux_secmark_enabled(void)
168 return (selinux_policycap_alwaysnetwork() ||
169 atomic_read(&selinux_secmark_refcount));
173 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
176 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
177 * (1) if any are enabled or false (0) if neither are enabled. If the
178 * always_check_network policy capability is enabled, peer labeling
179 * is always considered enabled.
182 static int selinux_peerlbl_enabled(void)
184 return (selinux_policycap_alwaysnetwork() ||
185 netlbl_enabled() || selinux_xfrm_enabled());
188 static int selinux_netcache_avc_callback(u32 event)
190 if (event == AVC_CALLBACK_RESET) {
199 static int selinux_lsm_notifier_avc_callback(u32 event)
201 if (event == AVC_CALLBACK_RESET) {
203 call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
210 * initialise the security for the init task
212 static void cred_init_security(void)
214 struct cred *cred = (struct cred *) current->real_cred;
215 struct task_security_struct *tsec;
217 tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
219 panic("SELinux: Failed to initialize initial task.\n");
221 tsec->osid = tsec->sid = SECINITSID_KERNEL;
222 cred->security = tsec;
226 * get the security ID of a set of credentials
228 static inline u32 cred_sid(const struct cred *cred)
230 const struct task_security_struct *tsec;
232 tsec = cred->security;
237 * get the objective security ID of a task
239 static inline u32 task_sid(const struct task_struct *task)
244 sid = cred_sid(__task_cred(task));
249 /* Allocate and free functions for each kind of security blob. */
251 static int inode_alloc_security(struct inode *inode)
253 struct inode_security_struct *isec;
254 u32 sid = current_sid();
256 isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
260 spin_lock_init(&isec->lock);
261 INIT_LIST_HEAD(&isec->list);
263 isec->sid = SECINITSID_UNLABELED;
264 isec->sclass = SECCLASS_FILE;
265 isec->task_sid = sid;
266 isec->initialized = LABEL_INVALID;
267 inode->i_security = isec;
272 static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
275 * Try reloading inode security labels that have been marked as invalid. The
276 * @may_sleep parameter indicates when sleeping and thus reloading labels is
277 * allowed; when set to false, returns -ECHILD when the label is
278 * invalid. The @dentry parameter should be set to a dentry of the inode.
280 static int __inode_security_revalidate(struct inode *inode,
281 struct dentry *dentry,
284 struct inode_security_struct *isec = inode->i_security;
286 might_sleep_if(may_sleep);
288 if (selinux_state.initialized &&
289 isec->initialized != LABEL_INITIALIZED) {
294 * Try reloading the inode security label. This will fail if
295 * @opt_dentry is NULL and no dentry for this inode can be
296 * found; in that case, continue using the old label.
298 inode_doinit_with_dentry(inode, dentry);
303 static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
305 return inode->i_security;
308 static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
312 error = __inode_security_revalidate(inode, NULL, !rcu);
314 return ERR_PTR(error);
315 return inode->i_security;
319 * Get the security label of an inode.
321 static struct inode_security_struct *inode_security(struct inode *inode)
323 __inode_security_revalidate(inode, NULL, true);
324 return inode->i_security;
327 static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
329 struct inode *inode = d_backing_inode(dentry);
331 return inode->i_security;
335 * Get the security label of a dentry's backing inode.
337 static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
339 struct inode *inode = d_backing_inode(dentry);
341 __inode_security_revalidate(inode, dentry, true);
342 return inode->i_security;
345 static void inode_free_rcu(struct rcu_head *head)
347 struct inode_security_struct *isec;
349 isec = container_of(head, struct inode_security_struct, rcu);
350 kmem_cache_free(sel_inode_cache, isec);
353 static void inode_free_security(struct inode *inode)
355 struct inode_security_struct *isec = inode->i_security;
356 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
359 * As not all inode security structures are in a list, we check for
360 * empty list outside of the lock to make sure that we won't waste
361 * time taking a lock doing nothing.
363 * The list_del_init() function can be safely called more than once.
364 * It should not be possible for this function to be called with
365 * concurrent list_add(), but for better safety against future changes
366 * in the code, we use list_empty_careful() here.
368 if (!list_empty_careful(&isec->list)) {
369 spin_lock(&sbsec->isec_lock);
370 list_del_init(&isec->list);
371 spin_unlock(&sbsec->isec_lock);
375 * The inode may still be referenced in a path walk and
376 * a call to selinux_inode_permission() can be made
377 * after inode_free_security() is called. Ideally, the VFS
378 * wouldn't do this, but fixing that is a much harder
379 * job. For now, simply free the i_security via RCU, and
380 * leave the current inode->i_security pointer intact.
381 * The inode will be freed after the RCU grace period too.
383 call_rcu(&isec->rcu, inode_free_rcu);
386 static int file_alloc_security(struct file *file)
388 struct file_security_struct *fsec;
389 u32 sid = current_sid();
391 fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL);
396 fsec->fown_sid = sid;
397 file->f_security = fsec;
402 static void file_free_security(struct file *file)
404 struct file_security_struct *fsec = file->f_security;
405 file->f_security = NULL;
406 kmem_cache_free(file_security_cache, fsec);
409 static int superblock_alloc_security(struct super_block *sb)
411 struct superblock_security_struct *sbsec;
413 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
417 mutex_init(&sbsec->lock);
418 INIT_LIST_HEAD(&sbsec->isec_head);
419 spin_lock_init(&sbsec->isec_lock);
421 sbsec->sid = SECINITSID_UNLABELED;
422 sbsec->def_sid = SECINITSID_FILE;
423 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
424 sb->s_security = sbsec;
429 static void superblock_free_security(struct super_block *sb)
431 struct superblock_security_struct *sbsec = sb->s_security;
432 sb->s_security = NULL;
436 struct selinux_mnt_opts {
437 const char *fscontext, *context, *rootcontext, *defcontext;
440 static void selinux_free_mnt_opts(void *mnt_opts)
442 struct selinux_mnt_opts *opts = mnt_opts;
443 kfree(opts->fscontext);
444 kfree(opts->context);
445 kfree(opts->rootcontext);
446 kfree(opts->defcontext);
450 static inline int inode_doinit(struct inode *inode)
452 return inode_doinit_with_dentry(inode, NULL);
464 #define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
474 A(rootcontext, true),
479 static int match_opt_prefix(char *s, int l, char **arg)
483 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
484 size_t len = tokens[i].len;
485 if (len > l || memcmp(s, tokens[i].name, len))
487 if (tokens[i].has_arg) {
488 if (len == l || s[len] != '=')
493 return tokens[i].opt;
498 #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
500 static int may_context_mount_sb_relabel(u32 sid,
501 struct superblock_security_struct *sbsec,
502 const struct cred *cred)
504 const struct task_security_struct *tsec = cred->security;
507 rc = avc_has_perm(&selinux_state,
508 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
509 FILESYSTEM__RELABELFROM, NULL);
513 rc = avc_has_perm(&selinux_state,
514 tsec->sid, sid, SECCLASS_FILESYSTEM,
515 FILESYSTEM__RELABELTO, NULL);
519 static int may_context_mount_inode_relabel(u32 sid,
520 struct superblock_security_struct *sbsec,
521 const struct cred *cred)
523 const struct task_security_struct *tsec = cred->security;
525 rc = avc_has_perm(&selinux_state,
526 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
527 FILESYSTEM__RELABELFROM, NULL);
531 rc = avc_has_perm(&selinux_state,
532 sid, sbsec->sid, SECCLASS_FILESYSTEM,
533 FILESYSTEM__ASSOCIATE, NULL);
537 static int selinux_is_genfs_special_handling(struct super_block *sb)
539 /* Special handling. Genfs but also in-core setxattr handler */
540 return !strcmp(sb->s_type->name, "sysfs") ||
541 !strcmp(sb->s_type->name, "pstore") ||
542 !strcmp(sb->s_type->name, "debugfs") ||
543 !strcmp(sb->s_type->name, "tracefs") ||
544 !strcmp(sb->s_type->name, "rootfs") ||
545 (selinux_policycap_cgroupseclabel() &&
546 (!strcmp(sb->s_type->name, "cgroup") ||
547 !strcmp(sb->s_type->name, "cgroup2")));
550 static int selinux_is_sblabel_mnt(struct super_block *sb)
552 struct superblock_security_struct *sbsec = sb->s_security;
555 * IMPORTANT: Double-check logic in this function when adding a new
556 * SECURITY_FS_USE_* definition!
558 BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7);
560 switch (sbsec->behavior) {
561 case SECURITY_FS_USE_XATTR:
562 case SECURITY_FS_USE_TRANS:
563 case SECURITY_FS_USE_TASK:
564 case SECURITY_FS_USE_NATIVE:
567 case SECURITY_FS_USE_GENFS:
568 return selinux_is_genfs_special_handling(sb);
570 /* Never allow relabeling on context mounts */
571 case SECURITY_FS_USE_MNTPOINT:
572 case SECURITY_FS_USE_NONE:
578 static int sb_finish_set_opts(struct super_block *sb)
580 struct superblock_security_struct *sbsec = sb->s_security;
581 struct dentry *root = sb->s_root;
582 struct inode *root_inode = d_backing_inode(root);
585 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
586 /* Make sure that the xattr handler exists and that no
587 error other than -ENODATA is returned by getxattr on
588 the root directory. -ENODATA is ok, as this may be
589 the first boot of the SELinux kernel before we have
590 assigned xattr values to the filesystem. */
591 if (!(root_inode->i_opflags & IOP_XATTR)) {
592 pr_warn("SELinux: (dev %s, type %s) has no "
593 "xattr support\n", sb->s_id, sb->s_type->name);
598 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
599 if (rc < 0 && rc != -ENODATA) {
600 if (rc == -EOPNOTSUPP)
601 pr_warn("SELinux: (dev %s, type "
602 "%s) has no security xattr handler\n",
603 sb->s_id, sb->s_type->name);
605 pr_warn("SELinux: (dev %s, type "
606 "%s) getxattr errno %d\n", sb->s_id,
607 sb->s_type->name, -rc);
612 sbsec->flags |= SE_SBINITIALIZED;
615 * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply
616 * leave the flag untouched because sb_clone_mnt_opts might be handing
617 * us a superblock that needs the flag to be cleared.
619 if (selinux_is_sblabel_mnt(sb))
620 sbsec->flags |= SBLABEL_MNT;
622 sbsec->flags &= ~SBLABEL_MNT;
624 /* Initialize the root inode. */
625 rc = inode_doinit_with_dentry(root_inode, root);
627 /* Initialize any other inodes associated with the superblock, e.g.
628 inodes created prior to initial policy load or inodes created
629 during get_sb by a pseudo filesystem that directly
631 spin_lock(&sbsec->isec_lock);
632 while (!list_empty(&sbsec->isec_head)) {
633 struct inode_security_struct *isec =
634 list_first_entry(&sbsec->isec_head,
635 struct inode_security_struct, list);
636 struct inode *inode = isec->inode;
637 list_del_init(&isec->list);
638 spin_unlock(&sbsec->isec_lock);
639 inode = igrab(inode);
641 if (!IS_PRIVATE(inode))
645 spin_lock(&sbsec->isec_lock);
647 spin_unlock(&sbsec->isec_lock);
652 static int bad_option(struct superblock_security_struct *sbsec, char flag,
653 u32 old_sid, u32 new_sid)
655 char mnt_flags = sbsec->flags & SE_MNTMASK;
657 /* check if the old mount command had the same options */
658 if (sbsec->flags & SE_SBINITIALIZED)
659 if (!(sbsec->flags & flag) ||
660 (old_sid != new_sid))
663 /* check if we were passed the same options twice,
664 * aka someone passed context=a,context=b
666 if (!(sbsec->flags & SE_SBINITIALIZED))
667 if (mnt_flags & flag)
672 static int parse_sid(struct super_block *sb, const char *s, u32 *sid)
674 int rc = security_context_str_to_sid(&selinux_state, s,
677 pr_warn("SELinux: security_context_str_to_sid"
678 "(%s) failed for (dev %s, type %s) errno=%d\n",
679 s, sb->s_id, sb->s_type->name, rc);
684 * Allow filesystems with binary mount data to explicitly set mount point
685 * labeling information.
687 static int selinux_set_mnt_opts(struct super_block *sb,
689 unsigned long kern_flags,
690 unsigned long *set_kern_flags)
692 const struct cred *cred = current_cred();
693 struct superblock_security_struct *sbsec = sb->s_security;
694 struct dentry *root = sbsec->sb->s_root;
695 struct selinux_mnt_opts *opts = mnt_opts;
696 struct inode_security_struct *root_isec;
697 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
698 u32 defcontext_sid = 0;
701 mutex_lock(&sbsec->lock);
703 if (!selinux_state.initialized) {
705 /* Defer initialization until selinux_complete_init,
706 after the initial policy is loaded and the security
707 server is ready to handle calls. */
711 pr_warn("SELinux: Unable to set superblock options "
712 "before the security server is initialized\n");
715 if (kern_flags && !set_kern_flags) {
716 /* Specifying internal flags without providing a place to
717 * place the results is not allowed */
723 * Binary mount data FS will come through this function twice. Once
724 * from an explicit call and once from the generic calls from the vfs.
725 * Since the generic VFS calls will not contain any security mount data
726 * we need to skip the double mount verification.
728 * This does open a hole in which we will not notice if the first
729 * mount using this sb set explict options and a second mount using
730 * this sb does not set any security options. (The first options
731 * will be used for both mounts)
733 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
737 root_isec = backing_inode_security_novalidate(root);
740 * parse the mount options, check if they are valid sids.
741 * also check if someone is trying to mount the same sb more
742 * than once with different security options.
745 if (opts->fscontext) {
746 rc = parse_sid(sb, opts->fscontext, &fscontext_sid);
749 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
751 goto out_double_mount;
752 sbsec->flags |= FSCONTEXT_MNT;
755 rc = parse_sid(sb, opts->context, &context_sid);
758 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
760 goto out_double_mount;
761 sbsec->flags |= CONTEXT_MNT;
763 if (opts->rootcontext) {
764 rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid);
767 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
769 goto out_double_mount;
770 sbsec->flags |= ROOTCONTEXT_MNT;
772 if (opts->defcontext) {
773 rc = parse_sid(sb, opts->defcontext, &defcontext_sid);
776 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
778 goto out_double_mount;
779 sbsec->flags |= DEFCONTEXT_MNT;
783 if (sbsec->flags & SE_SBINITIALIZED) {
784 /* previously mounted with options, but not on this attempt? */
785 if ((sbsec->flags & SE_MNTMASK) && !opts)
786 goto out_double_mount;
791 if (strcmp(sb->s_type->name, "proc") == 0)
792 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
794 if (!strcmp(sb->s_type->name, "debugfs") ||
795 !strcmp(sb->s_type->name, "tracefs") ||
796 !strcmp(sb->s_type->name, "sysfs") ||
797 !strcmp(sb->s_type->name, "pstore") ||
798 !strcmp(sb->s_type->name, "cgroup") ||
799 !strcmp(sb->s_type->name, "cgroup2"))
800 sbsec->flags |= SE_SBGENFS;
802 if (!sbsec->behavior) {
804 * Determine the labeling behavior to use for this
807 rc = security_fs_use(&selinux_state, sb);
809 pr_warn("%s: security_fs_use(%s) returned %d\n",
810 __func__, sb->s_type->name, rc);
816 * If this is a user namespace mount and the filesystem type is not
817 * explicitly whitelisted, then no contexts are allowed on the command
818 * line and security labels must be ignored.
820 if (sb->s_user_ns != &init_user_ns &&
821 strcmp(sb->s_type->name, "tmpfs") &&
822 strcmp(sb->s_type->name, "ramfs") &&
823 strcmp(sb->s_type->name, "devpts")) {
824 if (context_sid || fscontext_sid || rootcontext_sid ||
829 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
830 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
831 rc = security_transition_sid(&selinux_state,
835 &sbsec->mntpoint_sid);
842 /* sets the context of the superblock for the fs being mounted. */
844 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
848 sbsec->sid = fscontext_sid;
852 * Switch to using mount point labeling behavior.
853 * sets the label used on all file below the mountpoint, and will set
854 * the superblock context if not already set.
856 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
857 sbsec->behavior = SECURITY_FS_USE_NATIVE;
858 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
862 if (!fscontext_sid) {
863 rc = may_context_mount_sb_relabel(context_sid, sbsec,
867 sbsec->sid = context_sid;
869 rc = may_context_mount_inode_relabel(context_sid, sbsec,
874 if (!rootcontext_sid)
875 rootcontext_sid = context_sid;
877 sbsec->mntpoint_sid = context_sid;
878 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
881 if (rootcontext_sid) {
882 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
887 root_isec->sid = rootcontext_sid;
888 root_isec->initialized = LABEL_INITIALIZED;
891 if (defcontext_sid) {
892 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
893 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
895 pr_warn("SELinux: defcontext option is "
896 "invalid for this filesystem type\n");
900 if (defcontext_sid != sbsec->def_sid) {
901 rc = may_context_mount_inode_relabel(defcontext_sid,
907 sbsec->def_sid = defcontext_sid;
911 rc = sb_finish_set_opts(sb);
913 mutex_unlock(&sbsec->lock);
917 pr_warn("SELinux: mount invalid. Same superblock, different "
918 "security settings for (dev %s, type %s)\n", sb->s_id,
923 static int selinux_cmp_sb_context(const struct super_block *oldsb,
924 const struct super_block *newsb)
926 struct superblock_security_struct *old = oldsb->s_security;
927 struct superblock_security_struct *new = newsb->s_security;
928 char oldflags = old->flags & SE_MNTMASK;
929 char newflags = new->flags & SE_MNTMASK;
931 if (oldflags != newflags)
933 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
935 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
937 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
939 if (oldflags & ROOTCONTEXT_MNT) {
940 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
941 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
942 if (oldroot->sid != newroot->sid)
947 pr_warn("SELinux: mount invalid. Same superblock, "
948 "different security settings for (dev %s, "
949 "type %s)\n", newsb->s_id, newsb->s_type->name);
953 static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
954 struct super_block *newsb,
955 unsigned long kern_flags,
956 unsigned long *set_kern_flags)
959 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
960 struct superblock_security_struct *newsbsec = newsb->s_security;
962 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
963 int set_context = (oldsbsec->flags & CONTEXT_MNT);
964 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
967 * if the parent was able to be mounted it clearly had no special lsm
968 * mount options. thus we can safely deal with this superblock later
970 if (!selinux_state.initialized)
974 * Specifying internal flags without providing a place to
975 * place the results is not allowed.
977 if (kern_flags && !set_kern_flags)
980 /* how can we clone if the old one wasn't set up?? */
981 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
983 /* if fs is reusing a sb, make sure that the contexts match */
984 if (newsbsec->flags & SE_SBINITIALIZED) {
985 if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context)
986 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
987 return selinux_cmp_sb_context(oldsb, newsb);
990 mutex_lock(&newsbsec->lock);
992 newsbsec->flags = oldsbsec->flags;
994 newsbsec->sid = oldsbsec->sid;
995 newsbsec->def_sid = oldsbsec->def_sid;
996 newsbsec->behavior = oldsbsec->behavior;
998 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
999 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
1000 rc = security_fs_use(&selinux_state, newsb);
1005 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
1006 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
1007 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
1011 u32 sid = oldsbsec->mntpoint_sid;
1014 newsbsec->sid = sid;
1015 if (!set_rootcontext) {
1016 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
1019 newsbsec->mntpoint_sid = sid;
1021 if (set_rootcontext) {
1022 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
1023 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
1025 newisec->sid = oldisec->sid;
1028 sb_finish_set_opts(newsb);
1030 mutex_unlock(&newsbsec->lock);
1034 static int selinux_add_opt(int token, const char *s, void **mnt_opts)
1036 struct selinux_mnt_opts *opts = *mnt_opts;
1038 if (token == Opt_seclabel) /* eaten and completely ignored */
1042 opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
1051 if (opts->context || opts->defcontext)
1056 if (opts->fscontext)
1058 opts->fscontext = s;
1060 case Opt_rootcontext:
1061 if (opts->rootcontext)
1063 opts->rootcontext = s;
1065 case Opt_defcontext:
1066 if (opts->context || opts->defcontext)
1068 opts->defcontext = s;
1073 pr_warn(SEL_MOUNT_FAIL_MSG);
1077 static int selinux_add_mnt_opt(const char *option, const char *val, int len,
1080 int token = Opt_error;
1083 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
1084 if (strcmp(option, tokens[i].name) == 0) {
1085 token = tokens[i].opt;
1090 if (token == Opt_error)
1093 if (token != Opt_seclabel)
1094 val = kmemdup_nul(val, len, GFP_KERNEL);
1095 rc = selinux_add_opt(token, val, mnt_opts);
1099 selinux_free_mnt_opts(*mnt_opts);
1106 static int show_sid(struct seq_file *m, u32 sid)
1108 char *context = NULL;
1112 rc = security_sid_to_context(&selinux_state, sid,
1115 bool has_comma = context && strchr(context, ',');
1119 seq_escape(m, context, "\"\n\\");
1127 static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1129 struct superblock_security_struct *sbsec = sb->s_security;
1132 if (!(sbsec->flags & SE_SBINITIALIZED))
1135 if (!selinux_state.initialized)
1138 if (sbsec->flags & FSCONTEXT_MNT) {
1140 seq_puts(m, FSCONTEXT_STR);
1141 rc = show_sid(m, sbsec->sid);
1145 if (sbsec->flags & CONTEXT_MNT) {
1147 seq_puts(m, CONTEXT_STR);
1148 rc = show_sid(m, sbsec->mntpoint_sid);
1152 if (sbsec->flags & DEFCONTEXT_MNT) {
1154 seq_puts(m, DEFCONTEXT_STR);
1155 rc = show_sid(m, sbsec->def_sid);
1159 if (sbsec->flags & ROOTCONTEXT_MNT) {
1160 struct dentry *root = sbsec->sb->s_root;
1161 struct inode_security_struct *isec = backing_inode_security(root);
1163 seq_puts(m, ROOTCONTEXT_STR);
1164 rc = show_sid(m, isec->sid);
1168 if (sbsec->flags & SBLABEL_MNT) {
1170 seq_puts(m, LABELSUPP_STR);
1175 static inline u16 inode_mode_to_security_class(umode_t mode)
1177 switch (mode & S_IFMT) {
1179 return SECCLASS_SOCK_FILE;
1181 return SECCLASS_LNK_FILE;
1183 return SECCLASS_FILE;
1185 return SECCLASS_BLK_FILE;
1187 return SECCLASS_DIR;
1189 return SECCLASS_CHR_FILE;
1191 return SECCLASS_FIFO_FILE;
1195 return SECCLASS_FILE;
1198 static inline int default_protocol_stream(int protocol)
1200 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1203 static inline int default_protocol_dgram(int protocol)
1205 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1208 static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1210 int extsockclass = selinux_policycap_extsockclass();
1216 case SOCK_SEQPACKET:
1217 return SECCLASS_UNIX_STREAM_SOCKET;
1220 return SECCLASS_UNIX_DGRAM_SOCKET;
1227 case SOCK_SEQPACKET:
1228 if (default_protocol_stream(protocol))
1229 return SECCLASS_TCP_SOCKET;
1230 else if (extsockclass && protocol == IPPROTO_SCTP)
1231 return SECCLASS_SCTP_SOCKET;
1233 return SECCLASS_RAWIP_SOCKET;
1235 if (default_protocol_dgram(protocol))
1236 return SECCLASS_UDP_SOCKET;
1237 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1238 protocol == IPPROTO_ICMPV6))
1239 return SECCLASS_ICMP_SOCKET;
1241 return SECCLASS_RAWIP_SOCKET;
1243 return SECCLASS_DCCP_SOCKET;
1245 return SECCLASS_RAWIP_SOCKET;
1251 return SECCLASS_NETLINK_ROUTE_SOCKET;
1252 case NETLINK_SOCK_DIAG:
1253 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1255 return SECCLASS_NETLINK_NFLOG_SOCKET;
1257 return SECCLASS_NETLINK_XFRM_SOCKET;
1258 case NETLINK_SELINUX:
1259 return SECCLASS_NETLINK_SELINUX_SOCKET;
1261 return SECCLASS_NETLINK_ISCSI_SOCKET;
1263 return SECCLASS_NETLINK_AUDIT_SOCKET;
1264 case NETLINK_FIB_LOOKUP:
1265 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1266 case NETLINK_CONNECTOR:
1267 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1268 case NETLINK_NETFILTER:
1269 return SECCLASS_NETLINK_NETFILTER_SOCKET;
1270 case NETLINK_DNRTMSG:
1271 return SECCLASS_NETLINK_DNRT_SOCKET;
1272 case NETLINK_KOBJECT_UEVENT:
1273 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
1274 case NETLINK_GENERIC:
1275 return SECCLASS_NETLINK_GENERIC_SOCKET;
1276 case NETLINK_SCSITRANSPORT:
1277 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1279 return SECCLASS_NETLINK_RDMA_SOCKET;
1280 case NETLINK_CRYPTO:
1281 return SECCLASS_NETLINK_CRYPTO_SOCKET;
1283 return SECCLASS_NETLINK_SOCKET;
1286 return SECCLASS_PACKET_SOCKET;
1288 return SECCLASS_KEY_SOCKET;
1290 return SECCLASS_APPLETALK_SOCKET;
1296 return SECCLASS_AX25_SOCKET;
1298 return SECCLASS_IPX_SOCKET;
1300 return SECCLASS_NETROM_SOCKET;
1302 return SECCLASS_ATMPVC_SOCKET;
1304 return SECCLASS_X25_SOCKET;
1306 return SECCLASS_ROSE_SOCKET;
1308 return SECCLASS_DECNET_SOCKET;
1310 return SECCLASS_ATMSVC_SOCKET;
1312 return SECCLASS_RDS_SOCKET;
1314 return SECCLASS_IRDA_SOCKET;
1316 return SECCLASS_PPPOX_SOCKET;
1318 return SECCLASS_LLC_SOCKET;
1320 return SECCLASS_CAN_SOCKET;
1322 return SECCLASS_TIPC_SOCKET;
1324 return SECCLASS_BLUETOOTH_SOCKET;
1326 return SECCLASS_IUCV_SOCKET;
1328 return SECCLASS_RXRPC_SOCKET;
1330 return SECCLASS_ISDN_SOCKET;
1332 return SECCLASS_PHONET_SOCKET;
1334 return SECCLASS_IEEE802154_SOCKET;
1336 return SECCLASS_CAIF_SOCKET;
1338 return SECCLASS_ALG_SOCKET;
1340 return SECCLASS_NFC_SOCKET;
1342 return SECCLASS_VSOCK_SOCKET;
1344 return SECCLASS_KCM_SOCKET;
1346 return SECCLASS_QIPCRTR_SOCKET;
1348 return SECCLASS_SMC_SOCKET;
1350 return SECCLASS_XDP_SOCKET;
1352 #error New address family defined, please update this function.
1357 return SECCLASS_SOCKET;
1360 static int selinux_genfs_get_sid(struct dentry *dentry,
1366 struct super_block *sb = dentry->d_sb;
1367 char *buffer, *path;
1369 buffer = (char *)__get_free_page(GFP_KERNEL);
1373 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1377 if (flags & SE_SBPROC) {
1378 /* each process gets a /proc/PID/ entry. Strip off the
1379 * PID part to get a valid selinux labeling.
1380 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1381 while (path[1] >= '0' && path[1] <= '9') {
1386 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1388 if (rc == -ENOENT) {
1389 /* No match in policy, mark as unlabeled. */
1390 *sid = SECINITSID_UNLABELED;
1394 free_page((unsigned long)buffer);
1398 /* The inode's security attributes must be initialized before first use. */
1399 static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1401 struct superblock_security_struct *sbsec = NULL;
1402 struct inode_security_struct *isec = inode->i_security;
1403 u32 task_sid, sid = 0;
1405 struct dentry *dentry;
1406 #define INITCONTEXTLEN 255
1407 char *context = NULL;
1411 if (isec->initialized == LABEL_INITIALIZED)
1414 spin_lock(&isec->lock);
1415 if (isec->initialized == LABEL_INITIALIZED)
1418 if (isec->sclass == SECCLASS_FILE)
1419 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1421 sbsec = inode->i_sb->s_security;
1422 if (!(sbsec->flags & SE_SBINITIALIZED)) {
1423 /* Defer initialization until selinux_complete_init,
1424 after the initial policy is loaded and the security
1425 server is ready to handle calls. */
1426 spin_lock(&sbsec->isec_lock);
1427 if (list_empty(&isec->list))
1428 list_add(&isec->list, &sbsec->isec_head);
1429 spin_unlock(&sbsec->isec_lock);
1433 sclass = isec->sclass;
1434 task_sid = isec->task_sid;
1436 isec->initialized = LABEL_PENDING;
1437 spin_unlock(&isec->lock);
1439 switch (sbsec->behavior) {
1440 case SECURITY_FS_USE_NATIVE:
1442 case SECURITY_FS_USE_XATTR:
1443 if (!(inode->i_opflags & IOP_XATTR)) {
1444 sid = sbsec->def_sid;
1447 /* Need a dentry, since the xattr API requires one.
1448 Life would be simpler if we could just pass the inode. */
1450 /* Called from d_instantiate or d_splice_alias. */
1451 dentry = dget(opt_dentry);
1454 * Called from selinux_complete_init, try to find a dentry.
1455 * Some filesystems really want a connected one, so try
1456 * that first. We could split SECURITY_FS_USE_XATTR in
1457 * two, depending upon that...
1459 dentry = d_find_alias(inode);
1461 dentry = d_find_any_alias(inode);
1465 * this is can be hit on boot when a file is accessed
1466 * before the policy is loaded. When we load policy we
1467 * may find inodes that have no dentry on the
1468 * sbsec->isec_head list. No reason to complain as these
1469 * will get fixed up the next time we go through
1470 * inode_doinit with a dentry, before these inodes could
1471 * be used again by userspace.
1476 len = INITCONTEXTLEN;
1477 context = kmalloc(len+1, GFP_NOFS);
1483 context[len] = '\0';
1484 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
1485 if (rc == -ERANGE) {
1488 /* Need a larger buffer. Query for the right size. */
1489 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
1495 context = kmalloc(len+1, GFP_NOFS);
1501 context[len] = '\0';
1502 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
1506 if (rc != -ENODATA) {
1507 pr_warn("SELinux: %s: getxattr returned "
1508 "%d for dev=%s ino=%ld\n", __func__,
1509 -rc, inode->i_sb->s_id, inode->i_ino);
1513 /* Map ENODATA to the default file SID */
1514 sid = sbsec->def_sid;
1517 rc = security_context_to_sid_default(&selinux_state,
1522 char *dev = inode->i_sb->s_id;
1523 unsigned long ino = inode->i_ino;
1525 if (rc == -EINVAL) {
1526 if (printk_ratelimit())
1527 pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid "
1528 "context=%s. This indicates you may need to relabel the inode or the "
1529 "filesystem in question.\n", ino, dev, context);
1531 pr_warn("SELinux: %s: context_to_sid(%s) "
1532 "returned %d for dev=%s ino=%ld\n",
1533 __func__, context, -rc, dev, ino);
1536 /* Leave with the unlabeled SID */
1543 case SECURITY_FS_USE_TASK:
1546 case SECURITY_FS_USE_TRANS:
1547 /* Default to the fs SID. */
1550 /* Try to obtain a transition SID. */
1551 rc = security_transition_sid(&selinux_state, task_sid, sid,
1552 sclass, NULL, &sid);
1556 case SECURITY_FS_USE_MNTPOINT:
1557 sid = sbsec->mntpoint_sid;
1560 /* Default to the fs superblock SID. */
1563 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
1564 /* We must have a dentry to determine the label on
1567 /* Called from d_instantiate or
1568 * d_splice_alias. */
1569 dentry = dget(opt_dentry);
1571 /* Called from selinux_complete_init, try to
1572 * find a dentry. Some filesystems really want
1573 * a connected one, so try that first.
1575 dentry = d_find_alias(inode);
1577 dentry = d_find_any_alias(inode);
1580 * This can be hit on boot when a file is accessed
1581 * before the policy is loaded. When we load policy we
1582 * may find inodes that have no dentry on the
1583 * sbsec->isec_head list. No reason to complain as
1584 * these will get fixed up the next time we go through
1585 * inode_doinit() with a dentry, before these inodes
1586 * could be used again by userspace.
1590 rc = selinux_genfs_get_sid(dentry, sclass,
1591 sbsec->flags, &sid);
1600 spin_lock(&isec->lock);
1601 if (isec->initialized == LABEL_PENDING) {
1603 isec->initialized = LABEL_INVALID;
1607 isec->initialized = LABEL_INITIALIZED;
1612 spin_unlock(&isec->lock);
1616 /* Convert a Linux signal to an access vector. */
1617 static inline u32 signal_to_av(int sig)
1623 /* Commonly granted from child to parent. */
1624 perm = PROCESS__SIGCHLD;
1627 /* Cannot be caught or ignored */
1628 perm = PROCESS__SIGKILL;
1631 /* Cannot be caught or ignored */
1632 perm = PROCESS__SIGSTOP;
1635 /* All other signals. */
1636 perm = PROCESS__SIGNAL;
1643 #if CAP_LAST_CAP > 63
1644 #error Fix SELinux to handle capabilities > 63.
1647 /* Check whether a task is allowed to use a capability. */
1648 static int cred_has_capability(const struct cred *cred,
1649 int cap, int audit, bool initns)
1651 struct common_audit_data ad;
1652 struct av_decision avd;
1654 u32 sid = cred_sid(cred);
1655 u32 av = CAP_TO_MASK(cap);
1658 ad.type = LSM_AUDIT_DATA_CAP;
1661 switch (CAP_TO_INDEX(cap)) {
1663 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
1666 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
1669 pr_err("SELinux: out of range capability %d\n", cap);
1674 rc = avc_has_perm_noaudit(&selinux_state,
1675 sid, sid, sclass, av, 0, &avd);
1676 if (audit == SECURITY_CAP_AUDIT) {
1677 int rc2 = avc_audit(&selinux_state,
1678 sid, sid, sclass, av, &avd, rc, &ad, 0);
1685 /* Check whether a task has a particular permission to an inode.
1686 The 'adp' parameter is optional and allows other audit
1687 data to be passed (e.g. the dentry). */
1688 static int inode_has_perm(const struct cred *cred,
1689 struct inode *inode,
1691 struct common_audit_data *adp)
1693 struct inode_security_struct *isec;
1696 validate_creds(cred);
1698 if (unlikely(IS_PRIVATE(inode)))
1701 sid = cred_sid(cred);
1702 isec = inode->i_security;
1704 return avc_has_perm(&selinux_state,
1705 sid, isec->sid, isec->sclass, perms, adp);
1708 /* Same as inode_has_perm, but pass explicit audit data containing
1709 the dentry to help the auditing code to more easily generate the
1710 pathname if needed. */
1711 static inline int dentry_has_perm(const struct cred *cred,
1712 struct dentry *dentry,
1715 struct inode *inode = d_backing_inode(dentry);
1716 struct common_audit_data ad;
1718 ad.type = LSM_AUDIT_DATA_DENTRY;
1719 ad.u.dentry = dentry;
1720 __inode_security_revalidate(inode, dentry, true);
1721 return inode_has_perm(cred, inode, av, &ad);
1724 /* Same as inode_has_perm, but pass explicit audit data containing
1725 the path to help the auditing code to more easily generate the
1726 pathname if needed. */
1727 static inline int path_has_perm(const struct cred *cred,
1728 const struct path *path,
1731 struct inode *inode = d_backing_inode(path->dentry);
1732 struct common_audit_data ad;
1734 ad.type = LSM_AUDIT_DATA_PATH;
1736 __inode_security_revalidate(inode, path->dentry, true);
1737 return inode_has_perm(cred, inode, av, &ad);
1740 /* Same as path_has_perm, but uses the inode from the file struct. */
1741 static inline int file_path_has_perm(const struct cred *cred,
1745 struct common_audit_data ad;
1747 ad.type = LSM_AUDIT_DATA_FILE;
1749 return inode_has_perm(cred, file_inode(file), av, &ad);
1752 #ifdef CONFIG_BPF_SYSCALL
1753 static int bpf_fd_pass(struct file *file, u32 sid);
1756 /* Check whether a task can use an open file descriptor to
1757 access an inode in a given way. Check access to the
1758 descriptor itself, and then use dentry_has_perm to
1759 check a particular permission to the file.
1760 Access to the descriptor is implicitly granted if it
1761 has the same SID as the process. If av is zero, then
1762 access to the file is not checked, e.g. for cases
1763 where only the descriptor is affected like seek. */
1764 static int file_has_perm(const struct cred *cred,
1768 struct file_security_struct *fsec = file->f_security;
1769 struct inode *inode = file_inode(file);
1770 struct common_audit_data ad;
1771 u32 sid = cred_sid(cred);
1774 ad.type = LSM_AUDIT_DATA_FILE;
1777 if (sid != fsec->sid) {
1778 rc = avc_has_perm(&selinux_state,
1787 #ifdef CONFIG_BPF_SYSCALL
1788 rc = bpf_fd_pass(file, cred_sid(cred));
1793 /* av is zero if only checking access to the descriptor. */
1796 rc = inode_has_perm(cred, inode, av, &ad);
1803 * Determine the label for an inode that might be unioned.
1806 selinux_determine_inode_label(const struct task_security_struct *tsec,
1808 const struct qstr *name, u16 tclass,
1811 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
1813 if ((sbsec->flags & SE_SBINITIALIZED) &&
1814 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1815 *_new_isid = sbsec->mntpoint_sid;
1816 } else if ((sbsec->flags & SBLABEL_MNT) &&
1818 *_new_isid = tsec->create_sid;
1820 const struct inode_security_struct *dsec = inode_security(dir);
1821 return security_transition_sid(&selinux_state, tsec->sid,
1829 /* Check whether a task can create a file. */
1830 static int may_create(struct inode *dir,
1831 struct dentry *dentry,
1834 const struct task_security_struct *tsec = current_security();
1835 struct inode_security_struct *dsec;
1836 struct superblock_security_struct *sbsec;
1838 struct common_audit_data ad;
1841 dsec = inode_security(dir);
1842 sbsec = dir->i_sb->s_security;
1846 ad.type = LSM_AUDIT_DATA_DENTRY;
1847 ad.u.dentry = dentry;
1849 rc = avc_has_perm(&selinux_state,
1850 sid, dsec->sid, SECCLASS_DIR,
1851 DIR__ADD_NAME | DIR__SEARCH,
1856 rc = selinux_determine_inode_label(current_security(), dir,
1857 &dentry->d_name, tclass, &newsid);
1861 rc = avc_has_perm(&selinux_state,
1862 sid, newsid, tclass, FILE__CREATE, &ad);
1866 return avc_has_perm(&selinux_state,
1868 SECCLASS_FILESYSTEM,
1869 FILESYSTEM__ASSOCIATE, &ad);
1873 #define MAY_UNLINK 1
1876 /* Check whether a task can link, unlink, or rmdir a file/directory. */
1877 static int may_link(struct inode *dir,
1878 struct dentry *dentry,
1882 struct inode_security_struct *dsec, *isec;
1883 struct common_audit_data ad;
1884 u32 sid = current_sid();
1888 dsec = inode_security(dir);
1889 isec = backing_inode_security(dentry);
1891 ad.type = LSM_AUDIT_DATA_DENTRY;
1892 ad.u.dentry = dentry;
1895 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
1896 rc = avc_has_perm(&selinux_state,
1897 sid, dsec->sid, SECCLASS_DIR, av, &ad);
1912 pr_warn("SELinux: %s: unrecognized kind %d\n",
1917 rc = avc_has_perm(&selinux_state,
1918 sid, isec->sid, isec->sclass, av, &ad);
1922 static inline int may_rename(struct inode *old_dir,
1923 struct dentry *old_dentry,
1924 struct inode *new_dir,
1925 struct dentry *new_dentry)
1927 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
1928 struct common_audit_data ad;
1929 u32 sid = current_sid();
1931 int old_is_dir, new_is_dir;
1934 old_dsec = inode_security(old_dir);
1935 old_isec = backing_inode_security(old_dentry);
1936 old_is_dir = d_is_dir(old_dentry);
1937 new_dsec = inode_security(new_dir);
1939 ad.type = LSM_AUDIT_DATA_DENTRY;
1941 ad.u.dentry = old_dentry;
1942 rc = avc_has_perm(&selinux_state,
1943 sid, old_dsec->sid, SECCLASS_DIR,
1944 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1947 rc = avc_has_perm(&selinux_state,
1949 old_isec->sclass, FILE__RENAME, &ad);
1952 if (old_is_dir && new_dir != old_dir) {
1953 rc = avc_has_perm(&selinux_state,
1955 old_isec->sclass, DIR__REPARENT, &ad);
1960 ad.u.dentry = new_dentry;
1961 av = DIR__ADD_NAME | DIR__SEARCH;
1962 if (d_is_positive(new_dentry))
1963 av |= DIR__REMOVE_NAME;
1964 rc = avc_has_perm(&selinux_state,
1965 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
1968 if (d_is_positive(new_dentry)) {
1969 new_isec = backing_inode_security(new_dentry);
1970 new_is_dir = d_is_dir(new_dentry);
1971 rc = avc_has_perm(&selinux_state,
1974 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1982 /* Check whether a task can perform a filesystem operation. */
1983 static int superblock_has_perm(const struct cred *cred,
1984 struct super_block *sb,
1986 struct common_audit_data *ad)
1988 struct superblock_security_struct *sbsec;
1989 u32 sid = cred_sid(cred);
1991 sbsec = sb->s_security;
1992 return avc_has_perm(&selinux_state,
1993 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
1996 /* Convert a Linux mode and permission mask to an access vector. */
1997 static inline u32 file_mask_to_av(int mode, int mask)
2001 if (!S_ISDIR(mode)) {
2002 if (mask & MAY_EXEC)
2003 av |= FILE__EXECUTE;
2004 if (mask & MAY_READ)
2007 if (mask & MAY_APPEND)
2009 else if (mask & MAY_WRITE)
2013 if (mask & MAY_EXEC)
2015 if (mask & MAY_WRITE)
2017 if (mask & MAY_READ)
2024 /* Convert a Linux file to an access vector. */
2025 static inline u32 file_to_av(struct file *file)
2029 if (file->f_mode & FMODE_READ)
2031 if (file->f_mode & FMODE_WRITE) {
2032 if (file->f_flags & O_APPEND)
2039 * Special file opened with flags 3 for ioctl-only use.
2048 * Convert a file to an access vector and include the correct open
2051 static inline u32 open_file_to_av(struct file *file)
2053 u32 av = file_to_av(file);
2054 struct inode *inode = file_inode(file);
2056 if (selinux_policycap_openperm() &&
2057 inode->i_sb->s_magic != SOCKFS_MAGIC)
2063 /* Hook functions begin here. */
2065 static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2067 u32 mysid = current_sid();
2068 u32 mgrsid = task_sid(mgr);
2070 return avc_has_perm(&selinux_state,
2071 mysid, mgrsid, SECCLASS_BINDER,
2072 BINDER__SET_CONTEXT_MGR, NULL);
2075 static int selinux_binder_transaction(struct task_struct *from,
2076 struct task_struct *to)
2078 u32 mysid = current_sid();
2079 u32 fromsid = task_sid(from);
2080 u32 tosid = task_sid(to);
2083 if (mysid != fromsid) {
2084 rc = avc_has_perm(&selinux_state,
2085 mysid, fromsid, SECCLASS_BINDER,
2086 BINDER__IMPERSONATE, NULL);
2091 return avc_has_perm(&selinux_state,
2092 fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
2096 static int selinux_binder_transfer_binder(struct task_struct *from,
2097 struct task_struct *to)
2099 u32 fromsid = task_sid(from);
2100 u32 tosid = task_sid(to);
2102 return avc_has_perm(&selinux_state,
2103 fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
2107 static int selinux_binder_transfer_file(struct task_struct *from,
2108 struct task_struct *to,
2111 u32 sid = task_sid(to);
2112 struct file_security_struct *fsec = file->f_security;
2113 struct dentry *dentry = file->f_path.dentry;
2114 struct inode_security_struct *isec;
2115 struct common_audit_data ad;
2118 ad.type = LSM_AUDIT_DATA_PATH;
2119 ad.u.path = file->f_path;
2121 if (sid != fsec->sid) {
2122 rc = avc_has_perm(&selinux_state,
2131 #ifdef CONFIG_BPF_SYSCALL
2132 rc = bpf_fd_pass(file, sid);
2137 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2140 isec = backing_inode_security(dentry);
2141 return avc_has_perm(&selinux_state,
2142 sid, isec->sid, isec->sclass, file_to_av(file),
2146 static int selinux_ptrace_access_check(struct task_struct *child,
2149 u32 sid = current_sid();
2150 u32 csid = task_sid(child);
2152 if (mode & PTRACE_MODE_READ)
2153 return avc_has_perm(&selinux_state,
2154 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
2156 return avc_has_perm(&selinux_state,
2157 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
2160 static int selinux_ptrace_traceme(struct task_struct *parent)
2162 return avc_has_perm(&selinux_state,
2163 task_sid(parent), current_sid(), SECCLASS_PROCESS,
2164 PROCESS__PTRACE, NULL);
2167 static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
2168 kernel_cap_t *inheritable, kernel_cap_t *permitted)
2170 return avc_has_perm(&selinux_state,
2171 current_sid(), task_sid(target), SECCLASS_PROCESS,
2172 PROCESS__GETCAP, NULL);
2175 static int selinux_capset(struct cred *new, const struct cred *old,
2176 const kernel_cap_t *effective,
2177 const kernel_cap_t *inheritable,
2178 const kernel_cap_t *permitted)
2180 return avc_has_perm(&selinux_state,
2181 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
2182 PROCESS__SETCAP, NULL);
2186 * (This comment used to live with the selinux_task_setuid hook,
2187 * which was removed).
2189 * Since setuid only affects the current process, and since the SELinux
2190 * controls are not based on the Linux identity attributes, SELinux does not
2191 * need to control this operation. However, SELinux does control the use of
2192 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2195 static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2198 return cred_has_capability(cred, cap, audit, ns == &init_user_ns);
2201 static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2203 const struct cred *cred = current_cred();
2215 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
2220 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
2223 rc = 0; /* let the kernel handle invalid cmds */
2229 static int selinux_quota_on(struct dentry *dentry)
2231 const struct cred *cred = current_cred();
2233 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
2236 static int selinux_syslog(int type)
2239 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2240 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
2241 return avc_has_perm(&selinux_state,
2242 current_sid(), SECINITSID_KERNEL,
2243 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
2244 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2245 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2246 /* Set level of messages printed to console */
2247 case SYSLOG_ACTION_CONSOLE_LEVEL:
2248 return avc_has_perm(&selinux_state,
2249 current_sid(), SECINITSID_KERNEL,
2250 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2253 /* All other syslog types */
2254 return avc_has_perm(&selinux_state,
2255 current_sid(), SECINITSID_KERNEL,
2256 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
2260 * Check that a process has enough memory to allocate a new virtual
2261 * mapping. 0 means there is enough memory for the allocation to
2262 * succeed and -ENOMEM implies there is not.
2264 * Do not audit the selinux permission check, as this is applied to all
2265 * processes that allocate mappings.
2267 static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
2269 int rc, cap_sys_admin = 0;
2271 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
2272 SECURITY_CAP_NOAUDIT, true);
2276 return cap_sys_admin;
2279 /* binprm security operations */
2281 static u32 ptrace_parent_sid(void)
2284 struct task_struct *tracer;
2287 tracer = ptrace_parent(current);
2289 sid = task_sid(tracer);
2295 static int check_nnp_nosuid(const struct linux_binprm *bprm,
2296 const struct task_security_struct *old_tsec,
2297 const struct task_security_struct *new_tsec)
2299 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
2300 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
2304 if (!nnp && !nosuid)
2305 return 0; /* neither NNP nor nosuid */
2307 if (new_tsec->sid == old_tsec->sid)
2308 return 0; /* No change in credentials */
2311 * If the policy enables the nnp_nosuid_transition policy capability,
2312 * then we permit transitions under NNP or nosuid if the
2313 * policy allows the corresponding permission between
2314 * the old and new contexts.
2316 if (selinux_policycap_nnp_nosuid_transition()) {
2319 av |= PROCESS2__NNP_TRANSITION;
2321 av |= PROCESS2__NOSUID_TRANSITION;
2322 rc = avc_has_perm(&selinux_state,
2323 old_tsec->sid, new_tsec->sid,
2324 SECCLASS_PROCESS2, av, NULL);
2330 * We also permit NNP or nosuid transitions to bounded SIDs,
2331 * i.e. SIDs that are guaranteed to only be allowed a subset
2332 * of the permissions of the current SID.
2334 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2340 * On failure, preserve the errno values for NNP vs nosuid.
2341 * NNP: Operation not permitted for caller.
2342 * nosuid: Permission denied to file.
2349 static int selinux_bprm_set_creds(struct linux_binprm *bprm)
2351 const struct task_security_struct *old_tsec;
2352 struct task_security_struct *new_tsec;
2353 struct inode_security_struct *isec;
2354 struct common_audit_data ad;
2355 struct inode *inode = file_inode(bprm->file);
2358 /* SELinux context only depends on initial program or script and not
2359 * the script interpreter */
2360 if (bprm->called_set_creds)
2363 old_tsec = current_security();
2364 new_tsec = bprm->cred->security;
2365 isec = inode_security(inode);
2367 /* Default to the current task SID. */
2368 new_tsec->sid = old_tsec->sid;
2369 new_tsec->osid = old_tsec->sid;
2371 /* Reset fs, key, and sock SIDs on execve. */
2372 new_tsec->create_sid = 0;
2373 new_tsec->keycreate_sid = 0;
2374 new_tsec->sockcreate_sid = 0;
2376 if (old_tsec->exec_sid) {
2377 new_tsec->sid = old_tsec->exec_sid;
2378 /* Reset exec SID on execve. */
2379 new_tsec->exec_sid = 0;
2381 /* Fail on NNP or nosuid if not an allowed transition. */
2382 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2386 /* Check for a default transition on this program. */
2387 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2388 isec->sid, SECCLASS_PROCESS, NULL,
2394 * Fallback to old SID on NNP or nosuid if not an allowed
2397 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2399 new_tsec->sid = old_tsec->sid;
2402 ad.type = LSM_AUDIT_DATA_FILE;
2403 ad.u.file = bprm->file;
2405 if (new_tsec->sid == old_tsec->sid) {
2406 rc = avc_has_perm(&selinux_state,
2407 old_tsec->sid, isec->sid,
2408 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2412 /* Check permissions for the transition. */
2413 rc = avc_has_perm(&selinux_state,
2414 old_tsec->sid, new_tsec->sid,
2415 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2419 rc = avc_has_perm(&selinux_state,
2420 new_tsec->sid, isec->sid,
2421 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2425 /* Check for shared state */
2426 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2427 rc = avc_has_perm(&selinux_state,
2428 old_tsec->sid, new_tsec->sid,
2429 SECCLASS_PROCESS, PROCESS__SHARE,
2435 /* Make sure that anyone attempting to ptrace over a task that
2436 * changes its SID has the appropriate permit */
2437 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
2438 u32 ptsid = ptrace_parent_sid();
2440 rc = avc_has_perm(&selinux_state,
2441 ptsid, new_tsec->sid,
2443 PROCESS__PTRACE, NULL);
2449 /* Clear any possibly unsafe personality bits on exec: */
2450 bprm->per_clear |= PER_CLEAR_ON_SETID;
2452 /* Enable secure mode for SIDs transitions unless
2453 the noatsecure permission is granted between
2454 the two SIDs, i.e. ahp returns 0. */
2455 rc = avc_has_perm(&selinux_state,
2456 old_tsec->sid, new_tsec->sid,
2457 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2459 bprm->secureexec |= !!rc;
2465 static int match_file(const void *p, struct file *file, unsigned fd)
2467 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2470 /* Derived from fs/exec.c:flush_old_files. */
2471 static inline void flush_unauthorized_files(const struct cred *cred,
2472 struct files_struct *files)
2474 struct file *file, *devnull = NULL;
2475 struct tty_struct *tty;
2479 tty = get_current_tty();
2481 spin_lock(&tty->files_lock);
2482 if (!list_empty(&tty->tty_files)) {
2483 struct tty_file_private *file_priv;
2485 /* Revalidate access to controlling tty.
2486 Use file_path_has_perm on the tty path directly
2487 rather than using file_has_perm, as this particular
2488 open file may belong to another process and we are
2489 only interested in the inode-based check here. */
2490 file_priv = list_first_entry(&tty->tty_files,
2491 struct tty_file_private, list);
2492 file = file_priv->file;
2493 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
2496 spin_unlock(&tty->files_lock);
2499 /* Reset controlling tty. */
2503 /* Revalidate access to inherited open files. */
2504 n = iterate_fd(files, 0, match_file, cred);
2505 if (!n) /* none found? */
2508 devnull = dentry_open(&selinux_null, O_RDWR, cred);
2509 if (IS_ERR(devnull))
2511 /* replace all the matching ones with this */
2513 replace_fd(n - 1, devnull, 0);
2514 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2520 * Prepare a process for imminent new credential changes due to exec
2522 static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
2524 struct task_security_struct *new_tsec;
2525 struct rlimit *rlim, *initrlim;
2528 new_tsec = bprm->cred->security;
2529 if (new_tsec->sid == new_tsec->osid)
2532 /* Close files for which the new task SID is not authorized. */
2533 flush_unauthorized_files(bprm->cred, current->files);
2535 /* Always clear parent death signal on SID transitions. */
2536 current->pdeath_signal = 0;
2538 /* Check whether the new SID can inherit resource limits from the old
2539 * SID. If not, reset all soft limits to the lower of the current
2540 * task's hard limit and the init task's soft limit.
2542 * Note that the setting of hard limits (even to lower them) can be
2543 * controlled by the setrlimit check. The inclusion of the init task's
2544 * soft limit into the computation is to avoid resetting soft limits
2545 * higher than the default soft limit for cases where the default is
2546 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2548 rc = avc_has_perm(&selinux_state,
2549 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2550 PROCESS__RLIMITINH, NULL);
2552 /* protect against do_prlimit() */
2554 for (i = 0; i < RLIM_NLIMITS; i++) {
2555 rlim = current->signal->rlim + i;
2556 initrlim = init_task.signal->rlim + i;
2557 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2559 task_unlock(current);
2560 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2561 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
2566 * Clean up the process immediately after the installation of new credentials
2569 static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2571 const struct task_security_struct *tsec = current_security();
2572 struct itimerval itimer;
2582 /* Check whether the new SID can inherit signal state from the old SID.
2583 * If not, clear itimers to avoid subsequent signal generation and
2584 * flush and unblock signals.
2586 * This must occur _after_ the task SID has been updated so that any
2587 * kill done after the flush will be checked against the new SID.
2589 rc = avc_has_perm(&selinux_state,
2590 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
2592 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
2593 memset(&itimer, 0, sizeof itimer);
2594 for (i = 0; i < 3; i++)
2595 do_setitimer(i, &itimer, NULL);
2597 spin_lock_irq(¤t->sighand->siglock);
2598 if (!fatal_signal_pending(current)) {
2599 flush_sigqueue(¤t->pending);
2600 flush_sigqueue(¤t->signal->shared_pending);
2601 flush_signal_handlers(current, 1);
2602 sigemptyset(¤t->blocked);
2603 recalc_sigpending();
2605 spin_unlock_irq(¤t->sighand->siglock);
2608 /* Wake up the parent if it is waiting so that it can recheck
2609 * wait permission to the new task SID. */
2610 read_lock(&tasklist_lock);
2611 __wake_up_parent(current, current->real_parent);
2612 read_unlock(&tasklist_lock);
2615 /* superblock security operations */
2617 static int selinux_sb_alloc_security(struct super_block *sb)
2619 return superblock_alloc_security(sb);
2622 static void selinux_sb_free_security(struct super_block *sb)
2624 superblock_free_security(sb);
2627 static inline int opt_len(const char *s)
2629 bool open_quote = false;
2633 for (len = 0; (c = s[len]) != '\0'; len++) {
2635 open_quote = !open_quote;
2636 if (c == ',' && !open_quote)
2642 static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
2644 char *from = options;
2649 int len = opt_len(from);
2653 token = match_opt_prefix(from, len, &arg);
2655 if (token != Opt_error) {
2660 for (p = q = arg; p < from + len; p++) {
2665 arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
2667 rc = selinux_add_opt(token, arg, mnt_opts);
2671 selinux_free_mnt_opts(*mnt_opts);
2677 if (!first) { // copy with preceding comma
2682 memmove(to, from, len);
2694 static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
2696 struct selinux_mnt_opts *opts = mnt_opts;
2697 struct superblock_security_struct *sbsec = sb->s_security;
2701 if (!(sbsec->flags & SE_SBINITIALIZED))
2707 if (opts->fscontext) {
2708 rc = parse_sid(sb, opts->fscontext, &sid);
2711 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2712 goto out_bad_option;
2714 if (opts->context) {
2715 rc = parse_sid(sb, opts->context, &sid);
2718 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2719 goto out_bad_option;
2721 if (opts->rootcontext) {
2722 struct inode_security_struct *root_isec;
2723 root_isec = backing_inode_security(sb->s_root);
2724 rc = parse_sid(sb, opts->rootcontext, &sid);
2727 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2728 goto out_bad_option;
2730 if (opts->defcontext) {
2731 rc = parse_sid(sb, opts->defcontext, &sid);
2734 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2735 goto out_bad_option;
2740 pr_warn("SELinux: unable to change security options "
2741 "during remount (dev %s, type=%s)\n", sb->s_id,
2746 static int selinux_sb_kern_mount(struct super_block *sb)
2748 const struct cred *cred = current_cred();
2749 struct common_audit_data ad;
2751 ad.type = LSM_AUDIT_DATA_DENTRY;
2752 ad.u.dentry = sb->s_root;
2753 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
2756 static int selinux_sb_statfs(struct dentry *dentry)
2758 const struct cred *cred = current_cred();
2759 struct common_audit_data ad;
2761 ad.type = LSM_AUDIT_DATA_DENTRY;
2762 ad.u.dentry = dentry->d_sb->s_root;
2763 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
2766 static int selinux_mount(const char *dev_name,
2767 const struct path *path,
2769 unsigned long flags,
2772 const struct cred *cred = current_cred();
2774 if (flags & MS_REMOUNT)
2775 return superblock_has_perm(cred, path->dentry->d_sb,
2776 FILESYSTEM__REMOUNT, NULL);
2778 return path_has_perm(cred, path, FILE__MOUNTON);
2781 static int selinux_umount(struct vfsmount *mnt, int flags)
2783 const struct cred *cred = current_cred();
2785 return superblock_has_perm(cred, mnt->mnt_sb,
2786 FILESYSTEM__UNMOUNT, NULL);
2789 /* inode security operations */
2791 static int selinux_inode_alloc_security(struct inode *inode)
2793 return inode_alloc_security(inode);
2796 static void selinux_inode_free_security(struct inode *inode)
2798 inode_free_security(inode);
2801 static int selinux_dentry_init_security(struct dentry *dentry, int mode,
2802 const struct qstr *name, void **ctx,
2808 rc = selinux_determine_inode_label(current_security(),
2809 d_inode(dentry->d_parent), name,
2810 inode_mode_to_security_class(mode),
2815 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2819 static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2821 const struct cred *old,
2826 struct task_security_struct *tsec;
2828 rc = selinux_determine_inode_label(old->security,
2829 d_inode(dentry->d_parent), name,
2830 inode_mode_to_security_class(mode),
2835 tsec = new->security;
2836 tsec->create_sid = newsid;
2840 static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
2841 const struct qstr *qstr,
2843 void **value, size_t *len)
2845 const struct task_security_struct *tsec = current_security();
2846 struct superblock_security_struct *sbsec;
2851 sbsec = dir->i_sb->s_security;
2853 newsid = tsec->create_sid;
2855 rc = selinux_determine_inode_label(current_security(),
2857 inode_mode_to_security_class(inode->i_mode),
2862 /* Possibly defer initialization to selinux_complete_init. */
2863 if (sbsec->flags & SE_SBINITIALIZED) {
2864 struct inode_security_struct *isec = inode->i_security;
2865 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2867 isec->initialized = LABEL_INITIALIZED;
2870 if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
2874 *name = XATTR_SELINUX_SUFFIX;
2877 rc = security_sid_to_context_force(&selinux_state, newsid,
2888 static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
2890 return may_create(dir, dentry, SECCLASS_FILE);
2893 static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2895 return may_link(dir, old_dentry, MAY_LINK);
2898 static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2900 return may_link(dir, dentry, MAY_UNLINK);
2903 static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2905 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2908 static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
2910 return may_create(dir, dentry, SECCLASS_DIR);
2913 static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2915 return may_link(dir, dentry, MAY_RMDIR);
2918 static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
2920 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2923 static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
2924 struct inode *new_inode, struct dentry *new_dentry)
2926 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2929 static int selinux_inode_readlink(struct dentry *dentry)
2931 const struct cred *cred = current_cred();
2933 return dentry_has_perm(cred, dentry, FILE__READ);
2936 static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2939 const struct cred *cred = current_cred();
2940 struct common_audit_data ad;
2941 struct inode_security_struct *isec;
2944 validate_creds(cred);
2946 ad.type = LSM_AUDIT_DATA_DENTRY;
2947 ad.u.dentry = dentry;
2948 sid = cred_sid(cred);
2949 isec = inode_security_rcu(inode, rcu);
2951 return PTR_ERR(isec);
2953 return avc_has_perm(&selinux_state,
2954 sid, isec->sid, isec->sclass, FILE__READ, &ad);
2957 static noinline int audit_inode_permission(struct inode *inode,
2958 u32 perms, u32 audited, u32 denied,
2962 struct common_audit_data ad;
2963 struct inode_security_struct *isec = inode->i_security;
2966 ad.type = LSM_AUDIT_DATA_INODE;
2969 rc = slow_avc_audit(&selinux_state,
2970 current_sid(), isec->sid, isec->sclass, perms,
2971 audited, denied, result, &ad, flags);
2977 static int selinux_inode_permission(struct inode *inode, int mask)
2979 const struct cred *cred = current_cred();
2982 unsigned flags = mask & MAY_NOT_BLOCK;
2983 struct inode_security_struct *isec;
2985 struct av_decision avd;
2987 u32 audited, denied;
2989 from_access = mask & MAY_ACCESS;
2990 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2992 /* No permission to check. Existence test. */
2996 validate_creds(cred);
2998 if (unlikely(IS_PRIVATE(inode)))
3001 perms = file_mask_to_av(inode->i_mode, mask);
3003 sid = cred_sid(cred);
3004 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
3006 return PTR_ERR(isec);
3008 rc = avc_has_perm_noaudit(&selinux_state,
3009 sid, isec->sid, isec->sclass, perms,
3010 (flags & MAY_NOT_BLOCK) ? AVC_NONBLOCKING : 0,
3012 audited = avc_audit_required(perms, &avd, rc,
3013 from_access ? FILE__AUDIT_ACCESS : 0,
3015 if (likely(!audited))
3018 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
3024 static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3026 const struct cred *cred = current_cred();
3027 struct inode *inode = d_backing_inode(dentry);
3028 unsigned int ia_valid = iattr->ia_valid;
3029 __u32 av = FILE__WRITE;
3031 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3032 if (ia_valid & ATTR_FORCE) {
3033 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3039 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3040 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
3041 return dentry_has_perm(cred, dentry, FILE__SETATTR);
3043 if (selinux_policycap_openperm() &&
3044 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3045 (ia_valid & ATTR_SIZE) &&
3046 !(ia_valid & ATTR_FILE))
3049 return dentry_has_perm(cred, dentry, av);
3052 static int selinux_inode_getattr(const struct path *path)
3054 return path_has_perm(current_cred(), path, FILE__GETATTR);
3057 static bool has_cap_mac_admin(bool audit)
3059 const struct cred *cred = current_cred();
3060 int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT;
3062 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit))
3064 if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true))
3069 static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3070 const void *value, size_t size, int flags)
3072 struct inode *inode = d_backing_inode(dentry);
3073 struct inode_security_struct *isec;
3074 struct superblock_security_struct *sbsec;
3075 struct common_audit_data ad;
3076 u32 newsid, sid = current_sid();
3079 if (strcmp(name, XATTR_NAME_SELINUX)) {
3080 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3084 /* Not an attribute we recognize, so just check the
3085 ordinary setattr permission. */
3086 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3089 sbsec = inode->i_sb->s_security;
3090 if (!(sbsec->flags & SBLABEL_MNT))
3093 if (!inode_owner_or_capable(inode))
3096 ad.type = LSM_AUDIT_DATA_DENTRY;
3097 ad.u.dentry = dentry;
3099 isec = backing_inode_security(dentry);
3100 rc = avc_has_perm(&selinux_state,
3101 sid, isec->sid, isec->sclass,
3102 FILE__RELABELFROM, &ad);
3106 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3108 if (rc == -EINVAL) {
3109 if (!has_cap_mac_admin(true)) {
3110 struct audit_buffer *ab;
3113 /* We strip a nul only if it is at the end, otherwise the
3114 * context contains a nul and we should audit that */
3116 const char *str = value;
3118 if (str[size - 1] == '\0')
3119 audit_size = size - 1;
3125 ab = audit_log_start(audit_context(),
3126 GFP_ATOMIC, AUDIT_SELINUX_ERR);
3127 audit_log_format(ab, "op=setxattr invalid_context=");
3128 audit_log_n_untrustedstring(ab, value, audit_size);
3133 rc = security_context_to_sid_force(&selinux_state, value,
3139 rc = avc_has_perm(&selinux_state,
3140 sid, newsid, isec->sclass,
3141 FILE__RELABELTO, &ad);
3145 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3150 return avc_has_perm(&selinux_state,
3153 SECCLASS_FILESYSTEM,
3154 FILESYSTEM__ASSOCIATE,
3158 static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
3159 const void *value, size_t size,
3162 struct inode *inode = d_backing_inode(dentry);
3163 struct inode_security_struct *isec;
3167 if (strcmp(name, XATTR_NAME_SELINUX)) {
3168 /* Not an attribute we recognize, so nothing to do. */
3172 rc = security_context_to_sid_force(&selinux_state, value, size,
3175 pr_err("SELinux: unable to map context to SID"
3176 "for (%s, %lu), rc=%d\n",
3177 inode->i_sb->s_id, inode->i_ino, -rc);
3181 isec = backing_inode_security(dentry);
3182 spin_lock(&isec->lock);
3183 isec->sclass = inode_mode_to_security_class(inode->i_mode);
3185 isec->initialized = LABEL_INITIALIZED;
3186 spin_unlock(&isec->lock);
3191 static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
3193 const struct cred *cred = current_cred();
3195 return dentry_has_perm(cred, dentry, FILE__GETATTR);
3198 static int selinux_inode_listxattr(struct dentry *dentry)
3200 const struct cred *cred = current_cred();
3202 return dentry_has_perm(cred, dentry, FILE__GETATTR);
3205 static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
3207 if (strcmp(name, XATTR_NAME_SELINUX)) {
3208 int rc = cap_inode_removexattr(dentry, name);
3212 /* Not an attribute we recognize, so just check the
3213 ordinary setattr permission. */
3214 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3217 /* No one is allowed to remove a SELinux security label.
3218 You can change the label, but all data must be labeled. */
3223 * Copy the inode security context value to the user.
3225 * Permission check is handled by selinux_inode_getxattr hook.
3227 static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
3231 char *context = NULL;
3232 struct inode_security_struct *isec;
3234 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3238 * If the caller has CAP_MAC_ADMIN, then get the raw context
3239 * value even if it is not defined by current policy; otherwise,
3240 * use the in-core value under current policy.
3241 * Use the non-auditing forms of the permission checks since
3242 * getxattr may be called by unprivileged processes commonly
3243 * and lack of permission just means that we fall back to the
3244 * in-core context value, not a denial.
3246 isec = inode_security(inode);
3247 if (has_cap_mac_admin(false))
3248 error = security_sid_to_context_force(&selinux_state,
3249 isec->sid, &context,
3252 error = security_sid_to_context(&selinux_state, isec->sid,
3266 static int selinux_inode_setsecurity(struct inode *inode, const char *name,
3267 const void *value, size_t size, int flags)
3269 struct inode_security_struct *isec = inode_security_novalidate(inode);
3270 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
3274 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3277 if (!(sbsec->flags & SBLABEL_MNT))
3280 if (!value || !size)
3283 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3288 spin_lock(&isec->lock);
3289 isec->sclass = inode_mode_to_security_class(inode->i_mode);
3291 isec->initialized = LABEL_INITIALIZED;
3292 spin_unlock(&isec->lock);
3296 static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3298 const int len = sizeof(XATTR_NAME_SELINUX);
3299 if (buffer && len <= buffer_size)
3300 memcpy(buffer, XATTR_NAME_SELINUX, len);
3304 static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
3306 struct inode_security_struct *isec = inode_security_novalidate(inode);
3310 static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3313 struct task_security_struct *tsec;
3314 struct cred *new_creds = *new;
3316 if (new_creds == NULL) {
3317 new_creds = prepare_creds();
3322 tsec = new_creds->security;
3323 /* Get label from overlay inode and set it in create_sid */
3324 selinux_inode_getsecid(d_inode(src), &sid);
3325 tsec->create_sid = sid;
3330 static int selinux_inode_copy_up_xattr(const char *name)
3332 /* The copy_up hook above sets the initial context on an inode, but we
3333 * don't then want to overwrite it by blindly copying all the lower
3334 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3336 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3337 return 1; /* Discard */
3339 * Any other attribute apart from SELINUX is not claimed, supported
3345 /* file security operations */
3347 static int selinux_revalidate_file_permission(struct file *file, int mask)
3349 const struct cred *cred = current_cred();
3350 struct inode *inode = file_inode(file);
3352 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3353 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3356 return file_has_perm(cred, file,
3357 file_mask_to_av(inode->i_mode, mask));
3360 static int selinux_file_permission(struct file *file, int mask)
3362 struct inode *inode = file_inode(file);
3363 struct file_security_struct *fsec = file->f_security;
3364 struct inode_security_struct *isec;
3365 u32 sid = current_sid();
3368 /* No permission to check. Existence test. */
3371 isec = inode_security(inode);
3372 if (sid == fsec->sid && fsec->isid == isec->sid &&
3373 fsec->pseqno == avc_policy_seqno(&selinux_state))
3374 /* No change since file_open check. */
3377 return selinux_revalidate_file_permission(file, mask);
3380 static int selinux_file_alloc_security(struct file *file)
3382 return file_alloc_security(file);
3385 static void selinux_file_free_security(struct file *file)
3387 file_free_security(file);
3391 * Check whether a task has the ioctl permission and cmd
3392 * operation to an inode.
3394 static int ioctl_has_perm(const struct cred *cred, struct file *file,
3395 u32 requested, u16 cmd)
3397 struct common_audit_data ad;
3398 struct file_security_struct *fsec = file->f_security;
3399 struct inode *inode = file_inode(file);
3400 struct inode_security_struct *isec;
3401 struct lsm_ioctlop_audit ioctl;
3402 u32 ssid = cred_sid(cred);
3404 u8 driver = cmd >> 8;
3405 u8 xperm = cmd & 0xff;
3407 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3410 ad.u.op->path = file->f_path;
3412 if (ssid != fsec->sid) {
3413 rc = avc_has_perm(&selinux_state,
3422 if (unlikely(IS_PRIVATE(inode)))
3425 isec = inode_security(inode);
3426 rc = avc_has_extended_perms(&selinux_state,
3427 ssid, isec->sid, isec->sclass,
3428 requested, driver, xperm, &ad);
3433 static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3436 const struct cred *cred = current_cred();
3446 case FS_IOC_GETFLAGS:
3448 case FS_IOC_GETVERSION:
3449 error = file_has_perm(cred, file, FILE__GETATTR);
3452 case FS_IOC_SETFLAGS:
3454 case FS_IOC_SETVERSION:
3455 error = file_has_perm(cred, file, FILE__SETATTR);
3458 /* sys_ioctl() checks */
3462 error = file_has_perm(cred, file, 0);
3467 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3468 SECURITY_CAP_AUDIT, true);
3471 /* default case assumes that the command will go
3472 * to the file's ioctl() function.
3475 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
3480 static int default_noexec;
3482 static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3484 const struct cred *cred = current_cred();
3485 u32 sid = cred_sid(cred);
3488 if (default_noexec &&
3489 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3490 (!shared && (prot & PROT_WRITE)))) {
3492 * We are making executable an anonymous mapping or a
3493 * private file mapping that will also be writable.
3494 * This has an additional check.
3496 rc = avc_has_perm(&selinux_state,
3497 sid, sid, SECCLASS_PROCESS,
3498 PROCESS__EXECMEM, NULL);
3504 /* read access is always possible with a mapping */
3505 u32 av = FILE__READ;
3507 /* write access only matters if the mapping is shared */
3508 if (shared && (prot & PROT_WRITE))
3511 if (prot & PROT_EXEC)
3512 av |= FILE__EXECUTE;
3514 return file_has_perm(cred, file, av);
3521 static int selinux_mmap_addr(unsigned long addr)
3525 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3526 u32 sid = current_sid();
3527 rc = avc_has_perm(&selinux_state,
3528 sid, sid, SECCLASS_MEMPROTECT,
3529 MEMPROTECT__MMAP_ZERO, NULL);
3535 static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3536 unsigned long prot, unsigned long flags)
3538 struct common_audit_data ad;
3542 ad.type = LSM_AUDIT_DATA_FILE;
3544 rc = inode_has_perm(current_cred(), file_inode(file),
3550 if (selinux_state.checkreqprot)
3553 return file_map_prot_check(file, prot,
3554 (flags & MAP_TYPE) == MAP_SHARED);
3557 static int selinux_file_mprotect(struct vm_area_struct *vma,
3558 unsigned long reqprot,
3561 const struct cred *cred = current_cred();
3562 u32 sid = cred_sid(cred);
3564 if (selinux_state.checkreqprot)
3567 if (default_noexec &&
3568 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3570 if (vma->vm_start >= vma->vm_mm->start_brk &&
3571 vma->vm_end <= vma->vm_mm->brk) {
3572 rc = avc_has_perm(&selinux_state,
3573 sid, sid, SECCLASS_PROCESS,
3574 PROCESS__EXECHEAP, NULL);
3575 } else if (!vma->vm_file &&
3576 ((vma->vm_start <= vma->vm_mm->start_stack &&
3577 vma->vm_end >= vma->vm_mm->start_stack) ||
3578 vma_is_stack_for_current(vma))) {
3579 rc = avc_has_perm(&selinux_state,
3580 sid, sid, SECCLASS_PROCESS,
3581 PROCESS__EXECSTACK, NULL);
3582 } else if (vma->vm_file && vma->anon_vma) {
3584 * We are making executable a file mapping that has
3585 * had some COW done. Since pages might have been
3586 * written, check ability to execute the possibly
3587 * modified content. This typically should only
3588 * occur for text relocations.
3590 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
3596 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3599 static int selinux_file_lock(struct file *file, unsigned int cmd)
3601 const struct cred *cred = current_cred();
3603 return file_has_perm(cred, file, FILE__LOCK);
3606 static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3609 const struct cred *cred = current_cred();
3614 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
3615 err = file_has_perm(cred, file, FILE__WRITE);
3624 case F_GETOWNER_UIDS:
3625 /* Just check FD__USE permission */
3626 err = file_has_perm(cred, file, 0);
3634 #if BITS_PER_LONG == 32
3639 err = file_has_perm(cred, file, FILE__LOCK);
3646 static void selinux_file_set_fowner(struct file *file)
3648 struct file_security_struct *fsec;
3650 fsec = file->f_security;
3651 fsec->fown_sid = current_sid();
3654 static int selinux_file_send_sigiotask(struct task_struct *tsk,
3655 struct fown_struct *fown, int signum)
3658 u32 sid = task_sid(tsk);
3660 struct file_security_struct *fsec;
3662 /* struct fown_struct is never outside the context of a struct file */
3663 file = container_of(fown, struct file, f_owner);
3665 fsec = file->f_security;
3668 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3670 perm = signal_to_av(signum);
3672 return avc_has_perm(&selinux_state,
3673 fsec->fown_sid, sid,
3674 SECCLASS_PROCESS, perm, NULL);
3677 static int selinux_file_receive(struct file *file)
3679 const struct cred *cred = current_cred();
3681 return file_has_perm(cred, file, file_to_av(file));
3684 static int selinux_file_open(struct file *file)
3686 struct file_security_struct *fsec;
3687 struct inode_security_struct *isec;
3689 fsec = file->f_security;
3690 isec = inode_security(file_inode(file));
3692 * Save inode label and policy sequence number
3693 * at open-time so that selinux_file_permission
3694 * can determine whether revalidation is necessary.
3695 * Task label is already saved in the file security
3696 * struct as its SID.
3698 fsec->isid = isec->sid;
3699 fsec->pseqno = avc_policy_seqno(&selinux_state);
3701 * Since the inode label or policy seqno may have changed
3702 * between the selinux_inode_permission check and the saving
3703 * of state above, recheck that access is still permitted.
3704 * Otherwise, access might never be revalidated against the
3705 * new inode label or new policy.
3706 * This check is not redundant - do not remove.
3708 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
3711 /* task security operations */
3713 static int selinux_task_alloc(struct task_struct *task,
3714 unsigned long clone_flags)
3716 u32 sid = current_sid();
3718 return avc_has_perm(&selinux_state,
3719 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
3723 * allocate the SELinux part of blank credentials
3725 static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3727 struct task_security_struct *tsec;
3729 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3733 cred->security = tsec;
3738 * detach and free the LSM part of a set of credentials
3740 static void selinux_cred_free(struct cred *cred)
3742 struct task_security_struct *tsec = cred->security;
3745 * cred->security == NULL if security_cred_alloc_blank() or
3746 * security_prepare_creds() returned an error.
3748 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
3749 cred->security = (void *) 0x7UL;
3754 * prepare a new set of credentials for modification
3756 static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3759 const struct task_security_struct *old_tsec;
3760 struct task_security_struct *tsec;
3762 old_tsec = old->security;
3764 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3768 new->security = tsec;
3773 * transfer the SELinux data to a blank set of creds
3775 static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3777 const struct task_security_struct *old_tsec = old->security;
3778 struct task_security_struct *tsec = new->security;
3783 static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3785 *secid = cred_sid(c);
3789 * set the security data for a kernel service
3790 * - all the creation contexts are set to unlabelled
3792 static int selinux_kernel_act_as(struct cred *new, u32 secid)
3794 struct task_security_struct *tsec = new->security;
3795 u32 sid = current_sid();
3798 ret = avc_has_perm(&selinux_state,
3800 SECCLASS_KERNEL_SERVICE,
3801 KERNEL_SERVICE__USE_AS_OVERRIDE,
3805 tsec->create_sid = 0;
3806 tsec->keycreate_sid = 0;
3807 tsec->sockcreate_sid = 0;
3813 * set the file creation context in a security record to the same as the
3814 * objective context of the specified inode
3816 static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3818 struct inode_security_struct *isec = inode_security(inode);
3819 struct task_security_struct *tsec = new->security;
3820 u32 sid = current_sid();
3823 ret = avc_has_perm(&selinux_state,
3825 SECCLASS_KERNEL_SERVICE,
3826 KERNEL_SERVICE__CREATE_FILES_AS,
3830 tsec->create_sid = isec->sid;
3834 static int selinux_kernel_module_request(char *kmod_name)
3836 struct common_audit_data ad;
3838 ad.type = LSM_AUDIT_DATA_KMOD;
3839 ad.u.kmod_name = kmod_name;
3841 return avc_has_perm(&selinux_state,
3842 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
3843 SYSTEM__MODULE_REQUEST, &ad);
3846 static int selinux_kernel_module_from_file(struct file *file)
3848 struct common_audit_data ad;
3849 struct inode_security_struct *isec;
3850 struct file_security_struct *fsec;
3851 u32 sid = current_sid();
3856 return avc_has_perm(&selinux_state,
3857 sid, sid, SECCLASS_SYSTEM,
3858 SYSTEM__MODULE_LOAD, NULL);
3862 ad.type = LSM_AUDIT_DATA_FILE;
3865 fsec = file->f_security;
3866 if (sid != fsec->sid) {
3867 rc = avc_has_perm(&selinux_state,
3868 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
3873 isec = inode_security(file_inode(file));
3874 return avc_has_perm(&selinux_state,
3875 sid, isec->sid, SECCLASS_SYSTEM,
3876 SYSTEM__MODULE_LOAD, &ad);
3879 static int selinux_kernel_read_file(struct file *file,
3880 enum kernel_read_file_id id)
3885 case READING_MODULE:
3886 rc = selinux_kernel_module_from_file(file);
3895 static int selinux_kernel_load_data(enum kernel_load_data_id id)
3900 case LOADING_MODULE:
3901 rc = selinux_kernel_module_from_file(NULL);
3909 static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3911 return avc_has_perm(&selinux_state,
3912 current_sid(), task_sid(p), SECCLASS_PROCESS,
3913 PROCESS__SETPGID, NULL);
3916 static int selinux_task_getpgid(struct task_struct *p)
3918 return avc_has_perm(&selinux_state,
3919 current_sid(), task_sid(p), SECCLASS_PROCESS,
3920 PROCESS__GETPGID, NULL);
3923 static int selinux_task_getsid(struct task_struct *p)
3925 return avc_has_perm(&selinux_state,
3926 current_sid(), task_sid(p), SECCLASS_PROCESS,
3927 PROCESS__GETSESSION, NULL);
3930 static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3932 *secid = task_sid(p);
3935 static int selinux_task_setnice(struct task_struct *p, int nice)
3937 return avc_has_perm(&selinux_state,
3938 current_sid(), task_sid(p), SECCLASS_PROCESS,
3939 PROCESS__SETSCHED, NULL);
3942 static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3944 return avc_has_perm(&selinux_state,
3945 current_sid(), task_sid(p), SECCLASS_PROCESS,
3946 PROCESS__SETSCHED, NULL);
3949 static int selinux_task_getioprio(struct task_struct *p)
3951 return avc_has_perm(&selinux_state,
3952 current_sid(), task_sid(p), SECCLASS_PROCESS,
3953 PROCESS__GETSCHED, NULL);
3956 static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
3963 if (flags & LSM_PRLIMIT_WRITE)
3964 av |= PROCESS__SETRLIMIT;
3965 if (flags & LSM_PRLIMIT_READ)
3966 av |= PROCESS__GETRLIMIT;
3967 return avc_has_perm(&selinux_state,
3968 cred_sid(cred), cred_sid(tcred),
3969 SECCLASS_PROCESS, av, NULL);
3972 static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3973 struct rlimit *new_rlim)
3975 struct rlimit *old_rlim = p->signal->rlim + resource;
3977 /* Control the ability to change the hard limit (whether
3978 lowering or raising it), so that the hard limit can
3979 later be used as a safe reset point for the soft limit
3980 upon context transitions. See selinux_bprm_committing_creds. */
3981 if (old_rlim->rlim_max != new_rlim->rlim_max)
3982 return avc_has_perm(&selinux_state,
3983 current_sid(), task_sid(p),
3984 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
3989 static int selinux_task_setscheduler(struct task_struct *p)
3991 return avc_has_perm(&selinux_state,
3992 current_sid(), task_sid(p), SECCLASS_PROCESS,
3993 PROCESS__SETSCHED, NULL);
3996 static int selinux_task_getscheduler(struct task_struct *p)
3998 return avc_has_perm(&selinux_state,
3999 current_sid(), task_sid(p), SECCLASS_PROCESS,
4000 PROCESS__GETSCHED, NULL);
4003 static int selinux_task_movememory(struct task_struct *p)
4005 return avc_has_perm(&selinux_state,
4006 current_sid(), task_sid(p), SECCLASS_PROCESS,
4007 PROCESS__SETSCHED, NULL);
4010 static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
4011 int sig, const struct cred *cred)
4017 perm = PROCESS__SIGNULL; /* null signal; existence test */
4019 perm = signal_to_av(sig);
4021 secid = current_sid();
4023 secid = cred_sid(cred);
4024 return avc_has_perm(&selinux_state,
4025 secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
4028 static void selinux_task_to_inode(struct task_struct *p,
4029 struct inode *inode)
4031 struct inode_security_struct *isec = inode->i_security;
4032 u32 sid = task_sid(p);
4034 spin_lock(&isec->lock);
4035 isec->sclass = inode_mode_to_security_class(inode->i_mode);
4037 isec->initialized = LABEL_INITIALIZED;
4038 spin_unlock(&isec->lock);
4041 /* Returns error only if unable to parse addresses */
4042 static int selinux_parse_skb_ipv4(struct sk_buff *skb,
4043 struct common_audit_data *ad, u8 *proto)
4045 int offset, ihlen, ret = -EINVAL;
4046 struct iphdr _iph, *ih;
4048 offset = skb_network_offset(skb);
4049 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4053 ihlen = ih->ihl * 4;
4054 if (ihlen < sizeof(_iph))
4057 ad->u.net->v4info.saddr = ih->saddr;
4058 ad->u.net->v4info.daddr = ih->daddr;
4062 *proto = ih->protocol;
4064 switch (ih->protocol) {
4066 struct tcphdr _tcph, *th;
4068 if (ntohs(ih->frag_off) & IP_OFFSET)
4072 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4076 ad->u.net->sport = th->source;
4077 ad->u.net->dport = th->dest;
4082 struct udphdr _udph, *uh;
4084 if (ntohs(ih->frag_off) & IP_OFFSET)
4088 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4092 ad->u.net->sport = uh->source;
4093 ad->u.net->dport = uh->dest;
4097 case IPPROTO_DCCP: {
4098 struct dccp_hdr _dccph, *dh;
4100 if (ntohs(ih->frag_off) & IP_OFFSET)
4104 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4108 ad->u.net->sport = dh->dccph_sport;
4109 ad->u.net->dport = dh->dccph_dport;
4113 #if IS_ENABLED(CONFIG_IP_SCTP)
4114 case IPPROTO_SCTP: {
4115 struct sctphdr _sctph, *sh;
4117 if (ntohs(ih->frag_off) & IP_OFFSET)
4121 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4125 ad->u.net->sport = sh->source;
4126 ad->u.net->dport = sh->dest;
4137 #if IS_ENABLED(CONFIG_IPV6)
4139 /* Returns error only if unable to parse addresses */
4140 static int selinux_parse_skb_ipv6(struct sk_buff *skb,
4141 struct common_audit_data *ad, u8 *proto)
4144 int ret = -EINVAL, offset;
4145 struct ipv6hdr _ipv6h, *ip6;
4148 offset = skb_network_offset(skb);
4149 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4153 ad->u.net->v6info.saddr = ip6->saddr;
4154 ad->u.net->v6info.daddr = ip6->daddr;
4157 nexthdr = ip6->nexthdr;
4158 offset += sizeof(_ipv6h);
4159 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
4168 struct tcphdr _tcph, *th;
4170 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4174 ad->u.net->sport = th->source;
4175 ad->u.net->dport = th->dest;
4180 struct udphdr _udph, *uh;
4182 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4186 ad->u.net->sport = uh->source;
4187 ad->u.net->dport = uh->dest;
4191 case IPPROTO_DCCP: {
4192 struct dccp_hdr _dccph, *dh;
4194 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4198 ad->u.net->sport = dh->dccph_sport;
4199 ad->u.net->dport = dh->dccph_dport;
4203 #if IS_ENABLED(CONFIG_IP_SCTP)
4204 case IPPROTO_SCTP: {
4205 struct sctphdr _sctph, *sh;
4207 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4211 ad->u.net->sport = sh->source;
4212 ad->u.net->dport = sh->dest;
4216 /* includes fragments */
4226 static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
4227 char **_addrp, int src, u8 *proto)
4232 switch (ad->u.net->family) {
4234 ret = selinux_parse_skb_ipv4(skb, ad, proto);
4237 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4238 &ad->u.net->v4info.daddr);
4241 #if IS_ENABLED(CONFIG_IPV6)
4243 ret = selinux_parse_skb_ipv6(skb, ad, proto);
4246 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4247 &ad->u.net->v6info.daddr);
4257 "SELinux: failure in selinux_parse_skb(),"
4258 " unable to parse packet\n");
4268 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
4270 * @family: protocol family
4271 * @sid: the packet's peer label SID
4274 * Check the various different forms of network peer labeling and determine
4275 * the peer label/SID for the packet; most of the magic actually occurs in
4276 * the security server function security_net_peersid_cmp(). The function
4277 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4278 * or -EACCES if @sid is invalid due to inconsistencies with the different
4282 static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
4289 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
4292 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4296 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4297 nlbl_type, xfrm_sid, sid);
4298 if (unlikely(err)) {
4300 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4301 " unable to determine packet's peer label\n");
4309 * selinux_conn_sid - Determine the child socket label for a connection
4310 * @sk_sid: the parent socket's SID
4311 * @skb_sid: the packet's SID
4312 * @conn_sid: the resulting connection SID
4314 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4315 * combined with the MLS information from @skb_sid in order to create
4316 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4317 * of @sk_sid. Returns zero on success, negative values on failure.
4320 static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4324 if (skb_sid != SECSID_NULL)
4325 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4333 /* socket security operations */
4335 static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4336 u16 secclass, u32 *socksid)
4338 if (tsec->sockcreate_sid > SECSID_NULL) {
4339 *socksid = tsec->sockcreate_sid;
4343 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4344 secclass, NULL, socksid);
4347 static int sock_has_perm(struct sock *sk, u32 perms)
4349 struct sk_security_struct *sksec = sk->sk_security;
4350 struct common_audit_data ad;
4351 struct lsm_network_audit net = {0,};
4353 if (sksec->sid == SECINITSID_KERNEL)
4356 ad.type = LSM_AUDIT_DATA_NET;
4360 return avc_has_perm(&selinux_state,
4361 current_sid(), sksec->sid, sksec->sclass, perms,
4365 static int selinux_socket_create(int family, int type,
4366 int protocol, int kern)
4368 const struct task_security_struct *tsec = current_security();
4376 secclass = socket_type_to_security_class(family, type, protocol);
4377 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4381 return avc_has_perm(&selinux_state,
4382 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
4385 static int selinux_socket_post_create(struct socket *sock, int family,
4386 int type, int protocol, int kern)
4388 const struct task_security_struct *tsec = current_security();
4389 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
4390 struct sk_security_struct *sksec;
4391 u16 sclass = socket_type_to_security_class(family, type, protocol);
4392 u32 sid = SECINITSID_KERNEL;
4396 err = socket_sockcreate_sid(tsec, sclass, &sid);
4401 isec->sclass = sclass;
4403 isec->initialized = LABEL_INITIALIZED;
4406 sksec = sock->sk->sk_security;
4407 sksec->sclass = sclass;
4409 /* Allows detection of the first association on this socket */
4410 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4411 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4413 err = selinux_netlbl_socket_post_create(sock->sk, family);
4419 static int selinux_socket_socketpair(struct socket *socka,
4420 struct socket *sockb)
4422 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4423 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4425 sksec_a->peer_sid = sksec_b->sid;
4426 sksec_b->peer_sid = sksec_a->sid;
4431 /* Range of port numbers used to automatically bind.
4432 Need to determine whether we should perform a name_bind
4433 permission check between the socket and the port number. */
4435 static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4437 struct sock *sk = sock->sk;
4438 struct sk_security_struct *sksec = sk->sk_security;
4442 err = sock_has_perm(sk, SOCKET__BIND);
4446 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
4447 family = sk->sk_family;
4448 if (family == PF_INET || family == PF_INET6) {
4450 struct common_audit_data ad;
4451 struct lsm_network_audit net = {0,};
4452 struct sockaddr_in *addr4 = NULL;
4453 struct sockaddr_in6 *addr6 = NULL;
4454 u16 family_sa = address->sa_family;
4455 unsigned short snum;
4459 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4460 * that validates multiple binding addresses. Because of this
4461 * need to check address->sa_family as it is possible to have
4462 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4464 switch (family_sa) {
4467 if (addrlen < sizeof(struct sockaddr_in))
4469 addr4 = (struct sockaddr_in *)address;
4470 if (family_sa == AF_UNSPEC) {
4471 /* see __inet_bind(), we only want to allow
4472 * AF_UNSPEC if the address is INADDR_ANY
4474 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4476 family_sa = AF_INET;
4478 snum = ntohs(addr4->sin_port);
4479 addrp = (char *)&addr4->sin_addr.s_addr;
4482 if (addrlen < SIN6_LEN_RFC2133)
4484 addr6 = (struct sockaddr_in6 *)address;
4485 snum = ntohs(addr6->sin6_port);
4486 addrp = (char *)&addr6->sin6_addr.s6_addr;
4492 ad.type = LSM_AUDIT_DATA_NET;
4494 ad.u.net->sport = htons(snum);
4495 ad.u.net->family = family_sa;
4500 inet_get_local_port_range(sock_net(sk), &low, &high);
4502 if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
4504 err = sel_netport_sid(sk->sk_protocol,
4508 err = avc_has_perm(&selinux_state,
4511 SOCKET__NAME_BIND, &ad);
4517 switch (sksec->sclass) {
4518 case SECCLASS_TCP_SOCKET:
4519 node_perm = TCP_SOCKET__NODE_BIND;
4522 case SECCLASS_UDP_SOCKET:
4523 node_perm = UDP_SOCKET__NODE_BIND;
4526 case SECCLASS_DCCP_SOCKET:
4527 node_perm = DCCP_SOCKET__NODE_BIND;
4530 case SECCLASS_SCTP_SOCKET:
4531 node_perm = SCTP_SOCKET__NODE_BIND;
4535 node_perm = RAWIP_SOCKET__NODE_BIND;
4539 err = sel_netnode_sid(addrp, family_sa, &sid);
4543 if (family_sa == AF_INET)
4544 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
4546 ad.u.net->v6info.saddr = addr6->sin6_addr;
4548 err = avc_has_perm(&selinux_state,
4550 sksec->sclass, node_perm, &ad);
4557 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4558 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4560 return -EAFNOSUPPORT;
4563 /* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
4564 * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst
4566 static int selinux_socket_connect_helper(struct socket *sock,
4567 struct sockaddr *address, int addrlen)
4569 struct sock *sk = sock->sk;
4570 struct sk_security_struct *sksec = sk->sk_security;
4573 err = sock_has_perm(sk, SOCKET__CONNECT);
4578 * If a TCP, DCCP or SCTP socket, check name_connect permission
4581 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4582 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4583 sksec->sclass == SECCLASS_SCTP_SOCKET) {
4584 struct common_audit_data ad;
4585 struct lsm_network_audit net = {0,};
4586 struct sockaddr_in *addr4 = NULL;
4587 struct sockaddr_in6 *addr6 = NULL;
4588 unsigned short snum;
4591 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4592 * that validates multiple connect addresses. Because of this
4593 * need to check address->sa_family as it is possible to have
4594 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4596 switch (address->sa_family) {
4598 addr4 = (struct sockaddr_in *)address;
4599 if (addrlen < sizeof(struct sockaddr_in))
4601 snum = ntohs(addr4->sin_port);
4604 addr6 = (struct sockaddr_in6 *)address;
4605 if (addrlen < SIN6_LEN_RFC2133)
4607 snum = ntohs(addr6->sin6_port);
4610 /* Note that SCTP services expect -EINVAL, whereas
4611 * others expect -EAFNOSUPPORT.
4613 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4616 return -EAFNOSUPPORT;
4619 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
4623 switch (sksec->sclass) {
4624 case SECCLASS_TCP_SOCKET:
4625 perm = TCP_SOCKET__NAME_CONNECT;
4627 case SECCLASS_DCCP_SOCKET:
4628 perm = DCCP_SOCKET__NAME_CONNECT;
4630 case SECCLASS_SCTP_SOCKET:
4631 perm = SCTP_SOCKET__NAME_CONNECT;
4635 ad.type = LSM_AUDIT_DATA_NET;
4637 ad.u.net->dport = htons(snum);
4638 ad.u.net->family = address->sa_family;
4639 err = avc_has_perm(&selinux_state,
4640 sksec->sid, sid, sksec->sclass, perm, &ad);
4648 /* Supports connect(2), see comments in selinux_socket_connect_helper() */
4649 static int selinux_socket_connect(struct socket *sock,
4650 struct sockaddr *address, int addrlen)
4653 struct sock *sk = sock->sk;
4655 err = selinux_socket_connect_helper(sock, address, addrlen);
4659 return selinux_netlbl_socket_connect(sk, address);
4662 static int selinux_socket_listen(struct socket *sock, int backlog)
4664 return sock_has_perm(sock->sk, SOCKET__LISTEN);
4667 static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4670 struct inode_security_struct *isec;
4671 struct inode_security_struct *newisec;
4675 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
4679 isec = inode_security_novalidate(SOCK_INODE(sock));
4680 spin_lock(&isec->lock);
4681 sclass = isec->sclass;
4683 spin_unlock(&isec->lock);
4685 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4686 newisec->sclass = sclass;
4688 newisec->initialized = LABEL_INITIALIZED;
4693 static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
4696 return sock_has_perm(sock->sk, SOCKET__WRITE);
4699 static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4700 int size, int flags)
4702 return sock_has_perm(sock->sk, SOCKET__READ);
4705 static int selinux_socket_getsockname(struct socket *sock)
4707 return sock_has_perm(sock->sk, SOCKET__GETATTR);
4710 static int selinux_socket_getpeername(struct socket *sock)
4712 return sock_has_perm(sock->sk, SOCKET__GETATTR);
4715 static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
4719 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
4723 return selinux_netlbl_socket_setsockopt(sock, level, optname);
4726 static int selinux_socket_getsockopt(struct socket *sock, int level,
4729 return sock_has_perm(sock->sk, SOCKET__GETOPT);
4732 static int selinux_socket_shutdown(struct socket *sock, int how)
4734 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
4737 static int selinux_socket_unix_stream_connect(struct sock *sock,
4741 struct sk_security_struct *sksec_sock = sock->sk_security;
4742 struct sk_security_struct *sksec_other = other->sk_security;
4743 struct sk_security_struct *sksec_new = newsk->sk_security;
4744 struct common_audit_data ad;
4745 struct lsm_network_audit net = {0,};
4748 ad.type = LSM_AUDIT_DATA_NET;
4750 ad.u.net->sk = other;
4752 err = avc_has_perm(&selinux_state,
4753 sksec_sock->sid, sksec_other->sid,
4754 sksec_other->sclass,
4755 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4759 /* server child socket */
4760 sksec_new->peer_sid = sksec_sock->sid;
4761 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4762 sksec_sock->sid, &sksec_new->sid);
4766 /* connecting socket */
4767 sksec_sock->peer_sid = sksec_new->sid;
4772 static int selinux_socket_unix_may_send(struct socket *sock,
4773 struct socket *other)
4775 struct sk_security_struct *ssec = sock->sk->sk_security;
4776 struct sk_security_struct *osec = other->sk->sk_security;
4777 struct common_audit_data ad;
4778 struct lsm_network_audit net = {0,};
4780 ad.type = LSM_AUDIT_DATA_NET;
4782 ad.u.net->sk = other->sk;
4784 return avc_has_perm(&selinux_state,
4785 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4789 static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4790 char *addrp, u16 family, u32 peer_sid,
4791 struct common_audit_data *ad)
4797 err = sel_netif_sid(ns, ifindex, &if_sid);
4800 err = avc_has_perm(&selinux_state,
4802 SECCLASS_NETIF, NETIF__INGRESS, ad);
4806 err = sel_netnode_sid(addrp, family, &node_sid);
4809 return avc_has_perm(&selinux_state,
4811 SECCLASS_NODE, NODE__RECVFROM, ad);
4814 static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
4818 struct sk_security_struct *sksec = sk->sk_security;
4819 u32 sk_sid = sksec->sid;
4820 struct common_audit_data ad;
4821 struct lsm_network_audit net = {0,};
4824 ad.type = LSM_AUDIT_DATA_NET;
4826 ad.u.net->netif = skb->skb_iif;
4827 ad.u.net->family = family;
4828 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4832 if (selinux_secmark_enabled()) {
4833 err = avc_has_perm(&selinux_state,
4834 sk_sid, skb->secmark, SECCLASS_PACKET,
4840 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4843 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
4848 static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4851 struct sk_security_struct *sksec = sk->sk_security;
4852 u16 family = sk->sk_family;
4853 u32 sk_sid = sksec->sid;
4854 struct common_audit_data ad;
4855 struct lsm_network_audit net = {0,};
4860 if (family != PF_INET && family != PF_INET6)
4863 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
4864 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4867 /* If any sort of compatibility mode is enabled then handoff processing
4868 * to the selinux_sock_rcv_skb_compat() function to deal with the
4869 * special handling. We do this in an attempt to keep this function
4870 * as fast and as clean as possible. */
4871 if (!selinux_policycap_netpeer())
4872 return selinux_sock_rcv_skb_compat(sk, skb, family);
4874 secmark_active = selinux_secmark_enabled();
4875 peerlbl_active = selinux_peerlbl_enabled();
4876 if (!secmark_active && !peerlbl_active)
4879 ad.type = LSM_AUDIT_DATA_NET;
4881 ad.u.net->netif = skb->skb_iif;
4882 ad.u.net->family = family;
4883 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4887 if (peerlbl_active) {
4890 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4893 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4894 addrp, family, peer_sid, &ad);
4896 selinux_netlbl_err(skb, family, err, 0);
4899 err = avc_has_perm(&selinux_state,
4900 sk_sid, peer_sid, SECCLASS_PEER,
4903 selinux_netlbl_err(skb, family, err, 0);
4908 if (secmark_active) {
4909 err = avc_has_perm(&selinux_state,
4910 sk_sid, skb->secmark, SECCLASS_PACKET,
4919 static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4920 int __user *optlen, unsigned len)
4925 struct sk_security_struct *sksec = sock->sk->sk_security;
4926 u32 peer_sid = SECSID_NULL;
4928 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4929 sksec->sclass == SECCLASS_TCP_SOCKET ||
4930 sksec->sclass == SECCLASS_SCTP_SOCKET)
4931 peer_sid = sksec->peer_sid;
4932 if (peer_sid == SECSID_NULL)
4933 return -ENOPROTOOPT;
4935 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
4940 if (scontext_len > len) {
4945 if (copy_to_user(optval, scontext, scontext_len))
4949 if (put_user(scontext_len, optlen))
4955 static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
4957 u32 peer_secid = SECSID_NULL;
4959 struct inode_security_struct *isec;
4961 if (skb && skb->protocol == htons(ETH_P_IP))
4963 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4966 family = sock->sk->sk_family;
4970 if (sock && family == PF_UNIX) {
4971 isec = inode_security_novalidate(SOCK_INODE(sock));
4972 peer_secid = isec->sid;
4974 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
4977 *secid = peer_secid;
4978 if (peer_secid == SECSID_NULL)
4983 static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
4985 struct sk_security_struct *sksec;
4987 sksec = kzalloc(sizeof(*sksec), priority);
4991 sksec->peer_sid = SECINITSID_UNLABELED;
4992 sksec->sid = SECINITSID_UNLABELED;
4993 sksec->sclass = SECCLASS_SOCKET;
4994 selinux_netlbl_sk_security_reset(sksec);
4995 sk->sk_security = sksec;
5000 static void selinux_sk_free_security(struct sock *sk)
5002 struct sk_security_struct *sksec = sk->sk_security;
5004 sk->sk_security = NULL;
5005 selinux_netlbl_sk_security_free(sksec);
5009 static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5011 struct sk_security_struct *sksec = sk->sk_security;
5012 struct sk_security_struct *newsksec = newsk->sk_security;
5014 newsksec->sid = sksec->sid;
5015 newsksec->peer_sid = sksec->peer_sid;
5016 newsksec->sclass = sksec->sclass;
5018 selinux_netlbl_sk_security_reset(newsksec);
5021 static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
5024 *secid = SECINITSID_ANY_SOCKET;
5026 struct sk_security_struct *sksec = sk->sk_security;
5028 *secid = sksec->sid;
5032 static void selinux_sock_graft(struct sock *sk, struct socket *parent)
5034 struct inode_security_struct *isec =
5035 inode_security_novalidate(SOCK_INODE(parent));
5036 struct sk_security_struct *sksec = sk->sk_security;
5038 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5039 sk->sk_family == PF_UNIX)
5040 isec->sid = sksec->sid;
5041 sksec->sclass = isec->sclass;
5044 /* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5045 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5048 static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5049 struct sk_buff *skb)
5051 struct sk_security_struct *sksec = ep->base.sk->sk_security;
5052 struct common_audit_data ad;
5053 struct lsm_network_audit net = {0,};
5055 u32 peer_sid = SECINITSID_UNLABELED;
5059 if (!selinux_policycap_extsockclass())
5062 peerlbl_active = selinux_peerlbl_enabled();
5064 if (peerlbl_active) {
5065 /* This will return peer_sid = SECSID_NULL if there are
5066 * no peer labels, see security_net_peersid_resolve().
5068 err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5073 if (peer_sid == SECSID_NULL)
5074 peer_sid = SECINITSID_UNLABELED;
5077 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5078 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5080 /* Here as first association on socket. As the peer SID
5081 * was allowed by peer recv (and the netif/node checks),
5082 * then it is approved by policy and used as the primary
5083 * peer SID for getpeercon(3).
5085 sksec->peer_sid = peer_sid;
5086 } else if (sksec->peer_sid != peer_sid) {
5087 /* Other association peer SIDs are checked to enforce
5088 * consistency among the peer SIDs.
5090 ad.type = LSM_AUDIT_DATA_NET;
5092 ad.u.net->sk = ep->base.sk;
5093 err = avc_has_perm(&selinux_state,
5094 sksec->peer_sid, peer_sid, sksec->sclass,
5095 SCTP_SOCKET__ASSOCIATION, &ad);
5100 /* Compute the MLS component for the connection and store
5101 * the information in ep. This will be used by SCTP TCP type
5102 * sockets and peeled off connections as they cause a new
5103 * socket to be generated. selinux_sctp_sk_clone() will then
5104 * plug this into the new socket.
5106 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5110 ep->secid = conn_sid;
5111 ep->peer_secid = peer_sid;
5113 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5114 return selinux_netlbl_sctp_assoc_request(ep, skb);
5117 /* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5118 * based on their @optname.
5120 static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5121 struct sockaddr *address,
5124 int len, err = 0, walk_size = 0;
5126 struct sockaddr *addr;
5127 struct socket *sock;
5129 if (!selinux_policycap_extsockclass())
5132 /* Process one or more addresses that may be IPv4 or IPv6 */
5133 sock = sk->sk_socket;
5136 while (walk_size < addrlen) {
5137 if (walk_size + sizeof(sa_family_t) > addrlen)
5141 switch (addr->sa_family) {
5144 len = sizeof(struct sockaddr_in);
5147 len = sizeof(struct sockaddr_in6);
5153 if (walk_size + len > addrlen)
5159 case SCTP_PRIMARY_ADDR:
5160 case SCTP_SET_PEER_PRIMARY_ADDR:
5161 case SCTP_SOCKOPT_BINDX_ADD:
5162 err = selinux_socket_bind(sock, addr, len);
5164 /* Connect checks */
5165 case SCTP_SOCKOPT_CONNECTX:
5166 case SCTP_PARAM_SET_PRIMARY:
5167 case SCTP_PARAM_ADD_IP:
5168 case SCTP_SENDMSG_CONNECT:
5169 err = selinux_socket_connect_helper(sock, addr, len);
5173 /* As selinux_sctp_bind_connect() is called by the
5174 * SCTP protocol layer, the socket is already locked,
5175 * therefore selinux_netlbl_socket_connect_locked() is
5176 * is called here. The situations handled are:
5177 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5178 * whenever a new IP address is added or when a new
5179 * primary address is selected.
5180 * Note that an SCTP connect(2) call happens before
5181 * the SCTP protocol layer and is handled via
5182 * selinux_socket_connect().
5184 err = selinux_netlbl_socket_connect_locked(sk, addr);
5198 /* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5199 static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5202 struct sk_security_struct *sksec = sk->sk_security;
5203 struct sk_security_struct *newsksec = newsk->sk_security;
5205 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5206 * the non-sctp clone version.
5208 if (!selinux_policycap_extsockclass())
5209 return selinux_sk_clone_security(sk, newsk);
5211 newsksec->sid = ep->secid;
5212 newsksec->peer_sid = ep->peer_secid;
5213 newsksec->sclass = sksec->sclass;
5214 selinux_netlbl_sctp_sk_clone(sk, newsk);
5217 static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
5218 struct request_sock *req)
5220 struct sk_security_struct *sksec = sk->sk_security;
5222 u16 family = req->rsk_ops->family;
5226 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
5229 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5232 req->secid = connsid;
5233 req->peer_secid = peersid;
5235 return selinux_netlbl_inet_conn_request(req, family);
5238 static void selinux_inet_csk_clone(struct sock *newsk,
5239 const struct request_sock *req)
5241 struct sk_security_struct *newsksec = newsk->sk_security;
5243 newsksec->sid = req->secid;
5244 newsksec->peer_sid = req->peer_secid;
5245 /* NOTE: Ideally, we should also get the isec->sid for the
5246 new socket in sync, but we don't have the isec available yet.
5247 So we will wait until sock_graft to do it, by which
5248 time it will have been created and available. */
5250 /* We don't need to take any sort of lock here as we are the only
5251 * thread with access to newsksec */
5252 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
5255 static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
5257 u16 family = sk->sk_family;
5258 struct sk_security_struct *sksec = sk->sk_security;
5260 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5261 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5264 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
5267 static int selinux_secmark_relabel_packet(u32 sid)
5269 const struct task_security_struct *__tsec;
5272 __tsec = current_security();
5275 return avc_has_perm(&selinux_state,
5276 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5280 static void selinux_secmark_refcount_inc(void)
5282 atomic_inc(&selinux_secmark_refcount);
5285 static void selinux_secmark_refcount_dec(void)
5287 atomic_dec(&selinux_secmark_refcount);
5290 static void selinux_req_classify_flow(const struct request_sock *req,
5293 fl->flowi_secid = req->secid;
5296 static int selinux_tun_dev_alloc_security(void **security)
5298 struct tun_security_struct *tunsec;
5300 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5303 tunsec->sid = current_sid();
5309 static void selinux_tun_dev_free_security(void *security)
5314 static int selinux_tun_dev_create(void)
5316 u32 sid = current_sid();
5318 /* we aren't taking into account the "sockcreate" SID since the socket
5319 * that is being created here is not a socket in the traditional sense,
5320 * instead it is a private sock, accessible only to the kernel, and
5321 * representing a wide range of network traffic spanning multiple
5322 * connections unlike traditional sockets - check the TUN driver to
5323 * get a better understanding of why this socket is special */
5325 return avc_has_perm(&selinux_state,
5326 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
5330 static int selinux_tun_dev_attach_queue(void *security)
5332 struct tun_security_struct *tunsec = security;
5334 return avc_has_perm(&selinux_state,
5335 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
5336 TUN_SOCKET__ATTACH_QUEUE, NULL);
5339 static int selinux_tun_dev_attach(struct sock *sk, void *security)
5341 struct tun_security_struct *tunsec = security;
5342 struct sk_security_struct *sksec = sk->sk_security;
5344 /* we don't currently perform any NetLabel based labeling here and it
5345 * isn't clear that we would want to do so anyway; while we could apply
5346 * labeling without the support of the TUN user the resulting labeled
5347 * traffic from the other end of the connection would almost certainly
5348 * cause confusion to the TUN user that had no idea network labeling
5349 * protocols were being used */
5351 sksec->sid = tunsec->sid;
5352 sksec->sclass = SECCLASS_TUN_SOCKET;
5357 static int selinux_tun_dev_open(void *security)
5359 struct tun_security_struct *tunsec = security;
5360 u32 sid = current_sid();
5363 err = avc_has_perm(&selinux_state,
5364 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
5365 TUN_SOCKET__RELABELFROM, NULL);
5368 err = avc_has_perm(&selinux_state,
5369 sid, sid, SECCLASS_TUN_SOCKET,
5370 TUN_SOCKET__RELABELTO, NULL);
5378 static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5382 struct nlmsghdr *nlh;
5383 struct sk_security_struct *sksec = sk->sk_security;
5385 if (skb->len < NLMSG_HDRLEN) {
5389 nlh = nlmsg_hdr(skb);
5391 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
5393 if (err == -EINVAL) {
5394 pr_warn_ratelimited("SELinux: unrecognized netlink"
5395 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5396 " pig=%d comm=%s\n",
5397 sk->sk_protocol, nlh->nlmsg_type,
5398 secclass_map[sksec->sclass - 1].name,
5399 task_pid_nr(current), current->comm);
5400 if (!enforcing_enabled(&selinux_state) ||
5401 security_get_allow_unknown(&selinux_state))
5411 err = sock_has_perm(sk, perm);
5416 #ifdef CONFIG_NETFILTER
5418 static unsigned int selinux_ip_forward(struct sk_buff *skb,
5419 const struct net_device *indev,
5425 struct common_audit_data ad;
5426 struct lsm_network_audit net = {0,};
5431 if (!selinux_policycap_netpeer())
5434 secmark_active = selinux_secmark_enabled();
5435 netlbl_active = netlbl_enabled();
5436 peerlbl_active = selinux_peerlbl_enabled();
5437 if (!secmark_active && !peerlbl_active)
5440 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5443 ad.type = LSM_AUDIT_DATA_NET;
5445 ad.u.net->netif = indev->ifindex;
5446 ad.u.net->family = family;
5447 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5450 if (peerlbl_active) {
5451 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5452 addrp, family, peer_sid, &ad);
5454 selinux_netlbl_err(skb, family, err, 1);
5460 if (avc_has_perm(&selinux_state,
5461 peer_sid, skb->secmark,
5462 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5466 /* we do this in the FORWARD path and not the POST_ROUTING
5467 * path because we want to make sure we apply the necessary
5468 * labeling before IPsec is applied so we can leverage AH
5470 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5476 static unsigned int selinux_ipv4_forward(void *priv,
5477 struct sk_buff *skb,
5478 const struct nf_hook_state *state)
5480 return selinux_ip_forward(skb, state->in, PF_INET);
5483 #if IS_ENABLED(CONFIG_IPV6)
5484 static unsigned int selinux_ipv6_forward(void *priv,
5485 struct sk_buff *skb,
5486 const struct nf_hook_state *state)
5488 return selinux_ip_forward(skb, state->in, PF_INET6);
5492 static unsigned int selinux_ip_output(struct sk_buff *skb,
5498 if (!netlbl_enabled())
5501 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5502 * because we want to make sure we apply the necessary labeling
5503 * before IPsec is applied so we can leverage AH protection */
5506 struct sk_security_struct *sksec;
5508 if (sk_listener(sk))
5509 /* if the socket is the listening state then this
5510 * packet is a SYN-ACK packet which means it needs to
5511 * be labeled based on the connection/request_sock and
5512 * not the parent socket. unfortunately, we can't
5513 * lookup the request_sock yet as it isn't queued on
5514 * the parent socket until after the SYN-ACK is sent.
5515 * the "solution" is to simply pass the packet as-is
5516 * as any IP option based labeling should be copied
5517 * from the initial connection request (in the IP
5518 * layer). it is far from ideal, but until we get a
5519 * security label in the packet itself this is the
5520 * best we can do. */
5523 /* standard practice, label using the parent socket */
5524 sksec = sk->sk_security;
5527 sid = SECINITSID_KERNEL;
5528 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5534 static unsigned int selinux_ipv4_output(void *priv,
5535 struct sk_buff *skb,
5536 const struct nf_hook_state *state)
5538 return selinux_ip_output(skb, PF_INET);
5541 #if IS_ENABLED(CONFIG_IPV6)
5542 static unsigned int selinux_ipv6_output(void *priv,
5543 struct sk_buff *skb,
5544 const struct nf_hook_state *state)
5546 return selinux_ip_output(skb, PF_INET6);
5550 static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5554 struct sock *sk = skb_to_full_sk(skb);
5555 struct sk_security_struct *sksec;
5556 struct common_audit_data ad;
5557 struct lsm_network_audit net = {0,};
5563 sksec = sk->sk_security;
5565 ad.type = LSM_AUDIT_DATA_NET;
5567 ad.u.net->netif = ifindex;
5568 ad.u.net->family = family;
5569 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5572 if (selinux_secmark_enabled())
5573 if (avc_has_perm(&selinux_state,
5574 sksec->sid, skb->secmark,
5575 SECCLASS_PACKET, PACKET__SEND, &ad))
5576 return NF_DROP_ERR(-ECONNREFUSED);
5578 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5579 return NF_DROP_ERR(-ECONNREFUSED);
5584 static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5585 const struct net_device *outdev,
5590 int ifindex = outdev->ifindex;
5592 struct common_audit_data ad;
5593 struct lsm_network_audit net = {0,};
5598 /* If any sort of compatibility mode is enabled then handoff processing
5599 * to the selinux_ip_postroute_compat() function to deal with the
5600 * special handling. We do this in an attempt to keep this function
5601 * as fast and as clean as possible. */
5602 if (!selinux_policycap_netpeer())
5603 return selinux_ip_postroute_compat(skb, ifindex, family);
5605 secmark_active = selinux_secmark_enabled();
5606 peerlbl_active = selinux_peerlbl_enabled();
5607 if (!secmark_active && !peerlbl_active)
5610 sk = skb_to_full_sk(skb);
5613 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5614 * packet transformation so allow the packet to pass without any checks
5615 * since we'll have another chance to perform access control checks
5616 * when the packet is on it's final way out.
5617 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
5618 * is NULL, in this case go ahead and apply access control.
5619 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5620 * TCP listening state we cannot wait until the XFRM processing
5621 * is done as we will miss out on the SA label if we do;
5622 * unfortunately, this means more work, but it is only once per
5624 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
5625 !(sk && sk_listener(sk)))
5630 /* Without an associated socket the packet is either coming
5631 * from the kernel or it is being forwarded; check the packet
5632 * to determine which and if the packet is being forwarded
5633 * query the packet directly to determine the security label. */
5635 secmark_perm = PACKET__FORWARD_OUT;
5636 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
5639 secmark_perm = PACKET__SEND;
5640 peer_sid = SECINITSID_KERNEL;
5642 } else if (sk_listener(sk)) {
5643 /* Locally generated packet but the associated socket is in the
5644 * listening state which means this is a SYN-ACK packet. In
5645 * this particular case the correct security label is assigned
5646 * to the connection/request_sock but unfortunately we can't
5647 * query the request_sock as it isn't queued on the parent
5648 * socket until after the SYN-ACK packet is sent; the only
5649 * viable choice is to regenerate the label like we do in
5650 * selinux_inet_conn_request(). See also selinux_ip_output()
5651 * for similar problems. */
5653 struct sk_security_struct *sksec;
5655 sksec = sk->sk_security;
5656 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5658 /* At this point, if the returned skb peerlbl is SECSID_NULL
5659 * and the packet has been through at least one XFRM
5660 * transformation then we must be dealing with the "final"
5661 * form of labeled IPsec packet; since we've already applied
5662 * all of our access controls on this packet we can safely
5663 * pass the packet. */
5664 if (skb_sid == SECSID_NULL) {
5667 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5671 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5675 return NF_DROP_ERR(-ECONNREFUSED);
5678 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5680 secmark_perm = PACKET__SEND;
5682 /* Locally generated packet, fetch the security label from the
5683 * associated socket. */
5684 struct sk_security_struct *sksec = sk->sk_security;
5685 peer_sid = sksec->sid;
5686 secmark_perm = PACKET__SEND;
5689 ad.type = LSM_AUDIT_DATA_NET;
5691 ad.u.net->netif = ifindex;
5692 ad.u.net->family = family;
5693 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
5697 if (avc_has_perm(&selinux_state,
5698 peer_sid, skb->secmark,
5699 SECCLASS_PACKET, secmark_perm, &ad))
5700 return NF_DROP_ERR(-ECONNREFUSED);
5702 if (peerlbl_active) {
5706 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
5708 if (avc_has_perm(&selinux_state,
5710 SECCLASS_NETIF, NETIF__EGRESS, &ad))
5711 return NF_DROP_ERR(-ECONNREFUSED);
5713 if (sel_netnode_sid(addrp, family, &node_sid))
5715 if (avc_has_perm(&selinux_state,
5717 SECCLASS_NODE, NODE__SENDTO, &ad))
5718 return NF_DROP_ERR(-ECONNREFUSED);
5724 static unsigned int selinux_ipv4_postroute(void *priv,
5725 struct sk_buff *skb,
5726 const struct nf_hook_state *state)
5728 return selinux_ip_postroute(skb, state->out, PF_INET);
5731 #if IS_ENABLED(CONFIG_IPV6)
5732 static unsigned int selinux_ipv6_postroute(void *priv,
5733 struct sk_buff *skb,
5734 const struct nf_hook_state *state)
5736 return selinux_ip_postroute(skb, state->out, PF_INET6);
5740 #endif /* CONFIG_NETFILTER */
5742 static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5744 return selinux_nlmsg_perm(sk, skb);
5747 static int ipc_alloc_security(struct kern_ipc_perm *perm,
5750 struct ipc_security_struct *isec;
5752 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
5756 isec->sclass = sclass;
5757 isec->sid = current_sid();
5758 perm->security = isec;
5763 static void ipc_free_security(struct kern_ipc_perm *perm)
5765 struct ipc_security_struct *isec = perm->security;
5766 perm->security = NULL;
5770 static int msg_msg_alloc_security(struct msg_msg *msg)
5772 struct msg_security_struct *msec;
5774 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
5778 msec->sid = SECINITSID_UNLABELED;
5779 msg->security = msec;
5784 static void msg_msg_free_security(struct msg_msg *msg)
5786 struct msg_security_struct *msec = msg->security;
5788 msg->security = NULL;
5792 static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
5795 struct ipc_security_struct *isec;
5796 struct common_audit_data ad;
5797 u32 sid = current_sid();
5799 isec = ipc_perms->security;
5801 ad.type = LSM_AUDIT_DATA_IPC;
5802 ad.u.ipc_id = ipc_perms->key;
5804 return avc_has_perm(&selinux_state,
5805 sid, isec->sid, isec->sclass, perms, &ad);
5808 static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5810 return msg_msg_alloc_security(msg);
5813 static void selinux_msg_msg_free_security(struct msg_msg *msg)
5815 msg_msg_free_security(msg);
5818 /* message queue security operations */
5819 static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
5821 struct ipc_security_struct *isec;
5822 struct common_audit_data ad;
5823 u32 sid = current_sid();
5826 rc = ipc_alloc_security(msq, SECCLASS_MSGQ);
5830 isec = msq->security;
5832 ad.type = LSM_AUDIT_DATA_IPC;
5833 ad.u.ipc_id = msq->key;
5835 rc = avc_has_perm(&selinux_state,
5836 sid, isec->sid, SECCLASS_MSGQ,
5839 ipc_free_security(msq);
5845 static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq)
5847 ipc_free_security(msq);
5850 static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
5852 struct ipc_security_struct *isec;
5853 struct common_audit_data ad;
5854 u32 sid = current_sid();
5856 isec = msq->security;
5858 ad.type = LSM_AUDIT_DATA_IPC;
5859 ad.u.ipc_id = msq->key;
5861 return avc_has_perm(&selinux_state,
5862 sid, isec->sid, SECCLASS_MSGQ,
5863 MSGQ__ASSOCIATE, &ad);
5866 static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
5874 /* No specific object, just general system-wide information. */
5875 return avc_has_perm(&selinux_state,
5876 current_sid(), SECINITSID_KERNEL,
5877 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
5881 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5884 perms = MSGQ__SETATTR;
5887 perms = MSGQ__DESTROY;
5893 err = ipc_has_perm(msq, perms);
5897 static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
5899 struct ipc_security_struct *isec;
5900 struct msg_security_struct *msec;
5901 struct common_audit_data ad;
5902 u32 sid = current_sid();
5905 isec = msq->security;
5906 msec = msg->security;
5909 * First time through, need to assign label to the message
5911 if (msec->sid == SECINITSID_UNLABELED) {
5913 * Compute new sid based on current process and
5914 * message queue this message will be stored in
5916 rc = security_transition_sid(&selinux_state, sid, isec->sid,
5917 SECCLASS_MSG, NULL, &msec->sid);
5922 ad.type = LSM_AUDIT_DATA_IPC;
5923 ad.u.ipc_id = msq->key;
5925 /* Can this process write to the queue? */
5926 rc = avc_has_perm(&selinux_state,
5927 sid, isec->sid, SECCLASS_MSGQ,
5930 /* Can this process send the message */
5931 rc = avc_has_perm(&selinux_state,
5932 sid, msec->sid, SECCLASS_MSG,
5935 /* Can the message be put in the queue? */
5936 rc = avc_has_perm(&selinux_state,
5937 msec->sid, isec->sid, SECCLASS_MSGQ,
5938 MSGQ__ENQUEUE, &ad);
5943 static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
5944 struct task_struct *target,
5945 long type, int mode)
5947 struct ipc_security_struct *isec;
5948 struct msg_security_struct *msec;
5949 struct common_audit_data ad;
5950 u32 sid = task_sid(target);
5953 isec = msq->security;
5954 msec = msg->security;
5956 ad.type = LSM_AUDIT_DATA_IPC;
5957 ad.u.ipc_id = msq->key;
5959 rc = avc_has_perm(&selinux_state,
5961 SECCLASS_MSGQ, MSGQ__READ, &ad);
5963 rc = avc_has_perm(&selinux_state,
5965 SECCLASS_MSG, MSG__RECEIVE, &ad);
5969 /* Shared Memory security operations */
5970 static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
5972 struct ipc_security_struct *isec;
5973 struct common_audit_data ad;
5974 u32 sid = current_sid();
5977 rc = ipc_alloc_security(shp, SECCLASS_SHM);
5981 isec = shp->security;
5983 ad.type = LSM_AUDIT_DATA_IPC;
5984 ad.u.ipc_id = shp->key;
5986 rc = avc_has_perm(&selinux_state,
5987 sid, isec->sid, SECCLASS_SHM,
5990 ipc_free_security(shp);
5996 static void selinux_shm_free_security(struct kern_ipc_perm *shp)
5998 ipc_free_security(shp);
6001 static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
6003 struct ipc_security_struct *isec;
6004 struct common_audit_data ad;
6005 u32 sid = current_sid();
6007 isec = shp->security;
6009 ad.type = LSM_AUDIT_DATA_IPC;
6010 ad.u.ipc_id = shp->key;
6012 return avc_has_perm(&selinux_state,
6013 sid, isec->sid, SECCLASS_SHM,
6014 SHM__ASSOCIATE, &ad);
6017 /* Note, at this point, shp is locked down */
6018 static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
6026 /* No specific object, just general system-wide information. */
6027 return avc_has_perm(&selinux_state,
6028 current_sid(), SECINITSID_KERNEL,
6029 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
6033 perms = SHM__GETATTR | SHM__ASSOCIATE;
6036 perms = SHM__SETATTR;
6043 perms = SHM__DESTROY;
6049 err = ipc_has_perm(shp, perms);
6053 static int selinux_shm_shmat(struct kern_ipc_perm *shp,
6054 char __user *shmaddr, int shmflg)
6058 if (shmflg & SHM_RDONLY)
6061 perms = SHM__READ | SHM__WRITE;
6063 return ipc_has_perm(shp, perms);
6066 /* Semaphore security operations */
6067 static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
6069 struct ipc_security_struct *isec;
6070 struct common_audit_data ad;
6071 u32 sid = current_sid();
6074 rc = ipc_alloc_security(sma, SECCLASS_SEM);
6078 isec = sma->security;
6080 ad.type = LSM_AUDIT_DATA_IPC;
6081 ad.u.ipc_id = sma->key;
6083 rc = avc_has_perm(&selinux_state,
6084 sid, isec->sid, SECCLASS_SEM,
6087 ipc_free_security(sma);
6093 static void selinux_sem_free_security(struct kern_ipc_perm *sma)
6095 ipc_free_security(sma);
6098 static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
6100 struct ipc_security_struct *isec;
6101 struct common_audit_data ad;
6102 u32 sid = current_sid();
6104 isec = sma->security;
6106 ad.type = LSM_AUDIT_DATA_IPC;
6107 ad.u.ipc_id = sma->key;
6109 return avc_has_perm(&selinux_state,
6110 sid, isec->sid, SECCLASS_SEM,
6111 SEM__ASSOCIATE, &ad);
6114 /* Note, at this point, sma is locked down */
6115 static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
6123 /* No specific object, just general system-wide information. */
6124 return avc_has_perm(&selinux_state,
6125 current_sid(), SECINITSID_KERNEL,
6126 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
6130 perms = SEM__GETATTR;
6141 perms = SEM__DESTROY;
6144 perms = SEM__SETATTR;
6149 perms = SEM__GETATTR | SEM__ASSOCIATE;
6155 err = ipc_has_perm(sma, perms);
6159 static int selinux_sem_semop(struct kern_ipc_perm *sma,
6160 struct sembuf *sops, unsigned nsops, int alter)
6165 perms = SEM__READ | SEM__WRITE;
6169 return ipc_has_perm(sma, perms);
6172 static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6178 av |= IPC__UNIX_READ;
6180 av |= IPC__UNIX_WRITE;
6185 return ipc_has_perm(ipcp, av);
6188 static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6190 struct ipc_security_struct *isec = ipcp->security;
6194 static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
6197 inode_doinit_with_dentry(inode, dentry);
6200 static int selinux_getprocattr(struct task_struct *p,
6201 char *name, char **value)
6203 const struct task_security_struct *__tsec;
6209 __tsec = __task_cred(p)->security;
6212 error = avc_has_perm(&selinux_state,
6213 current_sid(), __tsec->sid,
6214 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6219 if (!strcmp(name, "current"))
6221 else if (!strcmp(name, "prev"))
6223 else if (!strcmp(name, "exec"))
6224 sid = __tsec->exec_sid;
6225 else if (!strcmp(name, "fscreate"))
6226 sid = __tsec->create_sid;
6227 else if (!strcmp(name, "keycreate"))
6228 sid = __tsec->keycreate_sid;
6229 else if (!strcmp(name, "sockcreate"))
6230 sid = __tsec->sockcreate_sid;
6240 error = security_sid_to_context(&selinux_state, sid, value, &len);
6250 static int selinux_setprocattr(const char *name, void *value, size_t size)
6252 struct task_security_struct *tsec;
6254 u32 mysid = current_sid(), sid = 0, ptsid;
6259 * Basic control over ability to set these attributes at all.
6261 if (!strcmp(name, "exec"))
6262 error = avc_has_perm(&selinux_state,
6263 mysid, mysid, SECCLASS_PROCESS,
6264 PROCESS__SETEXEC, NULL);
6265 else if (!strcmp(name, "fscreate"))
6266 error = avc_has_perm(&selinux_state,
6267 mysid, mysid, SECCLASS_PROCESS,
6268 PROCESS__SETFSCREATE, NULL);
6269 else if (!strcmp(name, "keycreate"))
6270 error = avc_has_perm(&selinux_state,
6271 mysid, mysid, SECCLASS_PROCESS,
6272 PROCESS__SETKEYCREATE, NULL);
6273 else if (!strcmp(name, "sockcreate"))
6274 error = avc_has_perm(&selinux_state,
6275 mysid, mysid, SECCLASS_PROCESS,
6276 PROCESS__SETSOCKCREATE, NULL);
6277 else if (!strcmp(name, "current"))
6278 error = avc_has_perm(&selinux_state,
6279 mysid, mysid, SECCLASS_PROCESS,
6280 PROCESS__SETCURRENT, NULL);
6286 /* Obtain a SID for the context, if one was specified. */
6287 if (size && str[0] && str[0] != '\n') {
6288 if (str[size-1] == '\n') {
6292 error = security_context_to_sid(&selinux_state, value, size,
6294 if (error == -EINVAL && !strcmp(name, "fscreate")) {
6295 if (!has_cap_mac_admin(true)) {
6296 struct audit_buffer *ab;
6299 /* We strip a nul only if it is at the end, otherwise the
6300 * context contains a nul and we should audit that */
6301 if (str[size - 1] == '\0')
6302 audit_size = size - 1;
6305 ab = audit_log_start(audit_context(),
6308 audit_log_format(ab, "op=fscreate invalid_context=");
6309 audit_log_n_untrustedstring(ab, value, audit_size);
6314 error = security_context_to_sid_force(
6322 new = prepare_creds();
6326 /* Permission checking based on the specified context is
6327 performed during the actual operation (execve,
6328 open/mkdir/...), when we know the full context of the
6329 operation. See selinux_bprm_set_creds for the execve
6330 checks and may_create for the file creation checks. The
6331 operation will then fail if the context is not permitted. */
6332 tsec = new->security;
6333 if (!strcmp(name, "exec")) {
6334 tsec->exec_sid = sid;
6335 } else if (!strcmp(name, "fscreate")) {
6336 tsec->create_sid = sid;
6337 } else if (!strcmp(name, "keycreate")) {
6338 error = avc_has_perm(&selinux_state,
6339 mysid, sid, SECCLASS_KEY, KEY__CREATE,
6343 tsec->keycreate_sid = sid;
6344 } else if (!strcmp(name, "sockcreate")) {
6345 tsec->sockcreate_sid = sid;
6346 } else if (!strcmp(name, "current")) {
6351 /* Only allow single threaded processes to change context */
6353 if (!current_is_single_threaded()) {
6354 error = security_bounded_transition(&selinux_state,
6360 /* Check permissions for the transition. */
6361 error = avc_has_perm(&selinux_state,
6362 tsec->sid, sid, SECCLASS_PROCESS,
6363 PROCESS__DYNTRANSITION, NULL);
6367 /* Check for ptracing, and update the task SID if ok.
6368 Otherwise, leave SID unchanged and fail. */
6369 ptsid = ptrace_parent_sid();
6371 error = avc_has_perm(&selinux_state,
6372 ptsid, sid, SECCLASS_PROCESS,
6373 PROCESS__PTRACE, NULL);
6392 static int selinux_ismaclabel(const char *name)
6394 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6397 static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6399 return security_sid_to_context(&selinux_state, secid,
6403 static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
6405 return security_context_to_sid(&selinux_state, secdata, seclen,
6409 static void selinux_release_secctx(char *secdata, u32 seclen)
6414 static void selinux_inode_invalidate_secctx(struct inode *inode)
6416 struct inode_security_struct *isec = inode->i_security;
6418 spin_lock(&isec->lock);
6419 isec->initialized = LABEL_INVALID;
6420 spin_unlock(&isec->lock);
6424 * called with inode->i_mutex locked
6426 static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6428 int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
6430 /* Do not return error when suppressing label (SBLABEL_MNT not set). */
6431 return rc == -EOPNOTSUPP ? 0 : rc;
6435 * called with inode->i_mutex locked
6437 static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6439 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6442 static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6445 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6454 static int selinux_key_alloc(struct key *k, const struct cred *cred,
6455 unsigned long flags)
6457 const struct task_security_struct *tsec;
6458 struct key_security_struct *ksec;
6460 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6464 tsec = cred->security;
6465 if (tsec->keycreate_sid)
6466 ksec->sid = tsec->keycreate_sid;
6468 ksec->sid = tsec->sid;
6474 static void selinux_key_free(struct key *k)
6476 struct key_security_struct *ksec = k->security;
6482 static int selinux_key_permission(key_ref_t key_ref,
6483 const struct cred *cred,
6487 struct key_security_struct *ksec;
6490 /* if no specific permissions are requested, we skip the
6491 permission check. No serious, additional covert channels
6492 appear to be created. */
6496 sid = cred_sid(cred);
6498 key = key_ref_to_ptr(key_ref);
6499 ksec = key->security;
6501 return avc_has_perm(&selinux_state,
6502 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
6505 static int selinux_key_getsecurity(struct key *key, char **_buffer)
6507 struct key_security_struct *ksec = key->security;
6508 char *context = NULL;
6512 rc = security_sid_to_context(&selinux_state, ksec->sid,
6521 #ifdef CONFIG_SECURITY_INFINIBAND
6522 static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6524 struct common_audit_data ad;
6527 struct ib_security_struct *sec = ib_sec;
6528 struct lsm_ibpkey_audit ibpkey;
6530 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
6534 ad.type = LSM_AUDIT_DATA_IBPKEY;
6535 ibpkey.subnet_prefix = subnet_prefix;
6536 ibpkey.pkey = pkey_val;
6537 ad.u.ibpkey = &ibpkey;
6538 return avc_has_perm(&selinux_state,
6540 SECCLASS_INFINIBAND_PKEY,
6541 INFINIBAND_PKEY__ACCESS, &ad);
6544 static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6547 struct common_audit_data ad;
6550 struct ib_security_struct *sec = ib_sec;
6551 struct lsm_ibendport_audit ibendport;
6553 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6559 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6560 strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6561 ibendport.port = port_num;
6562 ad.u.ibendport = &ibendport;
6563 return avc_has_perm(&selinux_state,
6565 SECCLASS_INFINIBAND_ENDPORT,
6566 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6569 static int selinux_ib_alloc_security(void **ib_sec)
6571 struct ib_security_struct *sec;
6573 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6576 sec->sid = current_sid();
6582 static void selinux_ib_free_security(void *ib_sec)
6588 #ifdef CONFIG_BPF_SYSCALL
6589 static int selinux_bpf(int cmd, union bpf_attr *attr,
6592 u32 sid = current_sid();
6596 case BPF_MAP_CREATE:
6597 ret = avc_has_perm(&selinux_state,
6598 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
6602 ret = avc_has_perm(&selinux_state,
6603 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
6614 static u32 bpf_map_fmode_to_av(fmode_t fmode)
6618 if (fmode & FMODE_READ)
6619 av |= BPF__MAP_READ;
6620 if (fmode & FMODE_WRITE)
6621 av |= BPF__MAP_WRITE;
6625 /* This function will check the file pass through unix socket or binder to see
6626 * if it is a bpf related object. And apply correspinding checks on the bpf
6627 * object based on the type. The bpf maps and programs, not like other files and
6628 * socket, are using a shared anonymous inode inside the kernel as their inode.
6629 * So checking that inode cannot identify if the process have privilege to
6630 * access the bpf object and that's why we have to add this additional check in
6631 * selinux_file_receive and selinux_binder_transfer_files.
6633 static int bpf_fd_pass(struct file *file, u32 sid)
6635 struct bpf_security_struct *bpfsec;
6636 struct bpf_prog *prog;
6637 struct bpf_map *map;
6640 if (file->f_op == &bpf_map_fops) {
6641 map = file->private_data;
6642 bpfsec = map->security;
6643 ret = avc_has_perm(&selinux_state,
6644 sid, bpfsec->sid, SECCLASS_BPF,
6645 bpf_map_fmode_to_av(file->f_mode), NULL);
6648 } else if (file->f_op == &bpf_prog_fops) {
6649 prog = file->private_data;
6650 bpfsec = prog->aux->security;
6651 ret = avc_has_perm(&selinux_state,
6652 sid, bpfsec->sid, SECCLASS_BPF,
6653 BPF__PROG_RUN, NULL);
6660 static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6662 u32 sid = current_sid();
6663 struct bpf_security_struct *bpfsec;
6665 bpfsec = map->security;
6666 return avc_has_perm(&selinux_state,
6667 sid, bpfsec->sid, SECCLASS_BPF,
6668 bpf_map_fmode_to_av(fmode), NULL);
6671 static int selinux_bpf_prog(struct bpf_prog *prog)
6673 u32 sid = current_sid();
6674 struct bpf_security_struct *bpfsec;
6676 bpfsec = prog->aux->security;
6677 return avc_has_perm(&selinux_state,
6678 sid, bpfsec->sid, SECCLASS_BPF,
6679 BPF__PROG_RUN, NULL);
6682 static int selinux_bpf_map_alloc(struct bpf_map *map)
6684 struct bpf_security_struct *bpfsec;
6686 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6690 bpfsec->sid = current_sid();
6691 map->security = bpfsec;
6696 static void selinux_bpf_map_free(struct bpf_map *map)
6698 struct bpf_security_struct *bpfsec = map->security;
6700 map->security = NULL;
6704 static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6706 struct bpf_security_struct *bpfsec;
6708 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6712 bpfsec->sid = current_sid();
6713 aux->security = bpfsec;
6718 static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6720 struct bpf_security_struct *bpfsec = aux->security;
6722 aux->security = NULL;
6727 static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
6728 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6729 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6730 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6731 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
6733 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6734 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6735 LSM_HOOK_INIT(capget, selinux_capget),
6736 LSM_HOOK_INIT(capset, selinux_capset),
6737 LSM_HOOK_INIT(capable, selinux_capable),
6738 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6739 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6740 LSM_HOOK_INIT(syslog, selinux_syslog),
6741 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
6743 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
6745 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6746 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6747 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
6749 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6750 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
6751 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
6752 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
6753 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6754 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6755 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6756 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6757 LSM_HOOK_INIT(sb_mount, selinux_mount),
6758 LSM_HOOK_INIT(sb_umount, selinux_umount),
6759 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6760 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
6761 LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt),
6763 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
6764 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
6766 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6767 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6768 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6769 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6770 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6771 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6772 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6773 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6774 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6775 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6776 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6777 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6778 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6779 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6780 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6781 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6782 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6783 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6784 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6785 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6786 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6787 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6788 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6789 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6790 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
6791 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
6792 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
6794 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6795 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6796 LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
6797 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6798 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6799 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6800 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6801 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6802 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6803 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6804 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6805 LSM_HOOK_INIT(file_receive, selinux_file_receive),
6807 LSM_HOOK_INIT(file_open, selinux_file_open),
6809 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
6810 LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank),
6811 LSM_HOOK_INIT(cred_free, selinux_cred_free),
6812 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6813 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
6814 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
6815 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6816 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6817 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
6818 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
6819 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
6820 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6821 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6822 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6823 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6824 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6825 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6826 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
6827 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
6828 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6829 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6830 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6831 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6832 LSM_HOOK_INIT(task_kill, selinux_task_kill),
6833 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
6835 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6836 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
6838 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6839 LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
6841 LSM_HOOK_INIT(msg_queue_alloc_security,
6842 selinux_msg_queue_alloc_security),
6843 LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
6844 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6845 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6846 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6847 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
6849 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6850 LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
6851 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6852 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6853 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
6855 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6856 LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
6857 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6858 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6859 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
6861 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
6863 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6864 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
6866 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6867 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6868 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6869 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
6870 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
6871 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6872 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6873 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
6875 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6876 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
6878 LSM_HOOK_INIT(socket_create, selinux_socket_create),
6879 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
6880 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
6881 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6882 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6883 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6884 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6885 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6886 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6887 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6888 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6889 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6890 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6891 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6892 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6893 LSM_HOOK_INIT(socket_getpeersec_stream,
6894 selinux_socket_getpeersec_stream),
6895 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6896 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6897 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6898 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6899 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6900 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
6901 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
6902 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
6903 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
6904 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6905 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6906 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6907 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6908 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6909 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6910 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6911 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6912 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6913 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6914 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6915 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6916 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
6917 #ifdef CONFIG_SECURITY_INFINIBAND
6918 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
6919 LSM_HOOK_INIT(ib_endport_manage_subnet,
6920 selinux_ib_endport_manage_subnet),
6921 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
6922 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
6924 #ifdef CONFIG_SECURITY_NETWORK_XFRM
6925 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6926 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6927 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6928 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6929 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6930 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6931 selinux_xfrm_state_alloc_acquire),
6932 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6933 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6934 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6935 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6936 selinux_xfrm_state_pol_flow_match),
6937 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
6941 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6942 LSM_HOOK_INIT(key_free, selinux_key_free),
6943 LSM_HOOK_INIT(key_permission, selinux_key_permission),
6944 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
6948 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6949 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6950 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6951 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
6954 #ifdef CONFIG_BPF_SYSCALL
6955 LSM_HOOK_INIT(bpf, selinux_bpf),
6956 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
6957 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
6958 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
6959 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
6960 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
6961 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
6965 static __init int selinux_init(void)
6967 if (!security_module_enable("selinux")) {
6968 selinux_enabled = 0;
6972 if (!selinux_enabled) {
6973 pr_info("SELinux: Disabled at boot.\n");
6977 pr_info("SELinux: Initializing.\n");
6979 memset(&selinux_state, 0, sizeof(selinux_state));
6980 enforcing_set(&selinux_state, selinux_enforcing_boot);
6981 selinux_state.checkreqprot = selinux_checkreqprot_boot;
6982 selinux_ss_init(&selinux_state.ss);
6983 selinux_avc_init(&selinux_state.avc);
6985 /* Set the security state for the initial task. */
6986 cred_init_security();
6988 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
6990 sel_inode_cache = kmem_cache_create("selinux_inode_security",
6991 sizeof(struct inode_security_struct),
6992 0, SLAB_PANIC, NULL);
6993 file_security_cache = kmem_cache_create("selinux_file_security",
6994 sizeof(struct file_security_struct),
6995 0, SLAB_PANIC, NULL);
7000 ebitmap_cache_init();
7002 hashtab_cache_init();
7004 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
7006 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7007 panic("SELinux: Unable to register AVC netcache callback\n");
7009 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
7010 panic("SELinux: Unable to register AVC LSM notifier callback\n");
7012 if (selinux_enforcing_boot)
7013 pr_debug("SELinux: Starting in enforcing mode\n");
7015 pr_debug("SELinux: Starting in permissive mode\n");
7020 static void delayed_superblock_init(struct super_block *sb, void *unused)
7022 selinux_set_mnt_opts(sb, NULL, 0, NULL);
7025 void selinux_complete_init(void)
7027 pr_debug("SELinux: Completing initialization.\n");
7029 /* Set up any superblocks initialized prior to the policy load. */
7030 pr_debug("SELinux: Setting up existing superblocks.\n");
7031 iterate_supers(delayed_superblock_init, NULL);
7034 /* SELinux requires early initialization in order to label
7035 all processes and objects when they are created. */
7036 DEFINE_LSM(selinux) = {
7038 .init = selinux_init,
7041 #if defined(CONFIG_NETFILTER)
7043 static const struct nf_hook_ops selinux_nf_ops[] = {
7045 .hook = selinux_ipv4_postroute,
7047 .hooknum = NF_INET_POST_ROUTING,
7048 .priority = NF_IP_PRI_SELINUX_LAST,
7051 .hook = selinux_ipv4_forward,
7053 .hooknum = NF_INET_FORWARD,
7054 .priority = NF_IP_PRI_SELINUX_FIRST,
7057 .hook = selinux_ipv4_output,
7059 .hooknum = NF_INET_LOCAL_OUT,
7060 .priority = NF_IP_PRI_SELINUX_FIRST,
7062 #if IS_ENABLED(CONFIG_IPV6)
7064 .hook = selinux_ipv6_postroute,
7066 .hooknum = NF_INET_POST_ROUTING,
7067 .priority = NF_IP6_PRI_SELINUX_LAST,
7070 .hook = selinux_ipv6_forward,
7072 .hooknum = NF_INET_FORWARD,
7073 .priority = NF_IP6_PRI_SELINUX_FIRST,
7076 .hook = selinux_ipv6_output,
7078 .hooknum = NF_INET_LOCAL_OUT,
7079 .priority = NF_IP6_PRI_SELINUX_FIRST,
7084 static int __net_init selinux_nf_register(struct net *net)
7086 return nf_register_net_hooks(net, selinux_nf_ops,
7087 ARRAY_SIZE(selinux_nf_ops));
7090 static void __net_exit selinux_nf_unregister(struct net *net)
7092 nf_unregister_net_hooks(net, selinux_nf_ops,
7093 ARRAY_SIZE(selinux_nf_ops));
7096 static struct pernet_operations selinux_net_ops = {
7097 .init = selinux_nf_register,
7098 .exit = selinux_nf_unregister,
7101 static int __init selinux_nf_ip_init(void)
7105 if (!selinux_enabled)
7108 pr_debug("SELinux: Registering netfilter hooks\n");
7110 err = register_pernet_subsys(&selinux_net_ops);
7112 panic("SELinux: register_pernet_subsys: error %d\n", err);
7116 __initcall(selinux_nf_ip_init);
7118 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7119 static void selinux_nf_ip_exit(void)
7121 pr_debug("SELinux: Unregistering netfilter hooks\n");
7123 unregister_pernet_subsys(&selinux_net_ops);
7127 #else /* CONFIG_NETFILTER */
7129 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7130 #define selinux_nf_ip_exit()
7133 #endif /* CONFIG_NETFILTER */
7135 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
7136 int selinux_disable(struct selinux_state *state)
7138 if (state->initialized) {
7139 /* Not permitted after initial policy load. */
7143 if (state->disabled) {
7144 /* Only do this once. */
7148 state->disabled = 1;
7150 pr_info("SELinux: Disabled at runtime.\n");
7152 selinux_enabled = 0;
7154 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
7156 /* Try to destroy the avc node cache */
7159 /* Unregister netfilter hooks. */
7160 selinux_nf_ip_exit();
7162 /* Unregister selinuxfs. */