2 * NSA Security-Enhanced Linux (SELinux) security module
4 * This file contains the SELinux hook function implementations.
6 * Authors: Stephen Smalley, <sds@epoch.ncsc.mil>
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>
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License version 2,
23 * as published by the Free Software Foundation.
26 #include <linux/init.h>
28 #include <linux/kernel.h>
29 #include <linux/tracehook.h>
30 #include <linux/errno.h>
31 #include <linux/sched.h>
32 #include <linux/lsm_hooks.h>
33 #include <linux/xattr.h>
34 #include <linux/capability.h>
35 #include <linux/unistd.h>
37 #include <linux/mman.h>
38 #include <linux/slab.h>
39 #include <linux/pagemap.h>
40 #include <linux/proc_fs.h>
41 #include <linux/swap.h>
42 #include <linux/spinlock.h>
43 #include <linux/syscalls.h>
44 #include <linux/dcache.h>
45 #include <linux/file.h>
46 #include <linux/fdtable.h>
47 #include <linux/namei.h>
48 #include <linux/mount.h>
49 #include <linux/netfilter_ipv4.h>
50 #include <linux/netfilter_ipv6.h>
51 #include <linux/tty.h>
53 #include <net/ip.h> /* for local_port_range[] */
54 #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
55 #include <net/inet_connection_sock.h>
56 #include <net/net_namespace.h>
57 #include <net/netlabel.h>
58 #include <linux/uaccess.h>
59 #include <asm/ioctls.h>
60 #include <linux/atomic.h>
61 #include <linux/bitops.h>
62 #include <linux/interrupt.h>
63 #include <linux/netdevice.h> /* for network interface checks */
64 #include <net/netlink.h>
65 #include <linux/tcp.h>
66 #include <linux/udp.h>
67 #include <linux/dccp.h>
68 #include <linux/quota.h>
69 #include <linux/un.h> /* for Unix socket types */
70 #include <net/af_unix.h> /* for Unix socket types */
71 #include <linux/parser.h>
72 #include <linux/nfs_mount.h>
74 #include <linux/hugetlb.h>
75 #include <linux/personality.h>
76 #include <linux/audit.h>
77 #include <linux/string.h>
78 #include <linux/selinux.h>
79 #include <linux/mutex.h>
80 #include <linux/posix-timers.h>
81 #include <linux/syslog.h>
82 #include <linux/user_namespace.h>
83 #include <linux/export.h>
84 #include <linux/msg.h>
85 #include <linux/shm.h>
97 /* SECMARK reference count */
98 static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
100 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
101 int selinux_enforcing;
103 static int __init enforcing_setup(char *str)
105 unsigned long enforcing;
106 if (!kstrtoul(str, 0, &enforcing))
107 selinux_enforcing = enforcing ? 1 : 0;
110 __setup("enforcing=", enforcing_setup);
113 #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
114 int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
116 static int __init selinux_enabled_setup(char *str)
118 unsigned long enabled;
119 if (!kstrtoul(str, 0, &enabled))
120 selinux_enabled = enabled ? 1 : 0;
123 __setup("selinux=", selinux_enabled_setup);
125 int selinux_enabled = 1;
128 static struct kmem_cache *sel_inode_cache;
129 static struct kmem_cache *file_security_cache;
132 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
135 * This function checks the SECMARK reference counter to see if any SECMARK
136 * targets are currently configured, if the reference counter is greater than
137 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
138 * enabled, false (0) if SECMARK is disabled. If the always_check_network
139 * policy capability is enabled, SECMARK is always considered enabled.
142 static int selinux_secmark_enabled(void)
144 return (selinux_policycap_alwaysnetwork || atomic_read(&selinux_secmark_refcount));
148 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
151 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
152 * (1) if any are enabled or false (0) if neither are enabled. If the
153 * always_check_network policy capability is enabled, peer labeling
154 * is always considered enabled.
157 static int selinux_peerlbl_enabled(void)
159 return (selinux_policycap_alwaysnetwork || netlbl_enabled() || selinux_xfrm_enabled());
162 static int selinux_netcache_avc_callback(u32 event)
164 if (event == AVC_CALLBACK_RESET) {
174 * initialise the security for the init task
176 static void cred_init_security(void)
178 struct cred *cred = (struct cred *) current->real_cred;
179 struct task_security_struct *tsec;
181 tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
183 panic("SELinux: Failed to initialize initial task.\n");
185 tsec->osid = tsec->sid = SECINITSID_KERNEL;
186 cred->security = tsec;
190 * get the security ID of a set of credentials
192 static inline u32 cred_sid(const struct cred *cred)
194 const struct task_security_struct *tsec;
196 tsec = cred->security;
201 * get the objective security ID of a task
203 static inline u32 task_sid(const struct task_struct *task)
208 sid = cred_sid(__task_cred(task));
214 * get the subjective security ID of the current task
216 static inline u32 current_sid(void)
218 const struct task_security_struct *tsec = current_security();
223 /* Allocate and free functions for each kind of security blob. */
225 static int inode_alloc_security(struct inode *inode)
227 struct inode_security_struct *isec;
228 u32 sid = current_sid();
230 isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
234 spin_lock_init(&isec->lock);
235 INIT_LIST_HEAD(&isec->list);
237 isec->sid = SECINITSID_UNLABELED;
238 isec->sclass = SECCLASS_FILE;
239 isec->task_sid = sid;
240 isec->initialized = LABEL_INVALID;
241 inode->i_security = isec;
246 static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
249 * Try reloading inode security labels that have been marked as invalid. The
250 * @may_sleep parameter indicates when sleeping and thus reloading labels is
251 * allowed; when set to false, returns -ECHILD when the label is
252 * invalid. The @opt_dentry parameter should be set to a dentry of the inode;
253 * when no dentry is available, set it to NULL instead.
255 static int __inode_security_revalidate(struct inode *inode,
256 struct dentry *opt_dentry,
259 struct inode_security_struct *isec = inode->i_security;
261 might_sleep_if(may_sleep);
263 if (ss_initialized && isec->initialized != LABEL_INITIALIZED) {
268 * Try reloading the inode security label. This will fail if
269 * @opt_dentry is NULL and no dentry for this inode can be
270 * found; in that case, continue using the old label.
272 inode_doinit_with_dentry(inode, opt_dentry);
277 static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
279 return inode->i_security;
282 static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
286 error = __inode_security_revalidate(inode, NULL, !rcu);
288 return ERR_PTR(error);
289 return inode->i_security;
293 * Get the security label of an inode.
295 static struct inode_security_struct *inode_security(struct inode *inode)
297 __inode_security_revalidate(inode, NULL, true);
298 return inode->i_security;
301 static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
303 struct inode *inode = d_backing_inode(dentry);
305 return inode->i_security;
309 * Get the security label of a dentry's backing inode.
311 static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
313 struct inode *inode = d_backing_inode(dentry);
315 __inode_security_revalidate(inode, dentry, true);
316 return inode->i_security;
319 static void inode_free_rcu(struct rcu_head *head)
321 struct inode_security_struct *isec;
323 isec = container_of(head, struct inode_security_struct, rcu);
324 kmem_cache_free(sel_inode_cache, isec);
327 static void inode_free_security(struct inode *inode)
329 struct inode_security_struct *isec = inode->i_security;
330 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
333 * As not all inode security structures are in a list, we check for
334 * empty list outside of the lock to make sure that we won't waste
335 * time taking a lock doing nothing.
337 * The list_del_init() function can be safely called more than once.
338 * It should not be possible for this function to be called with
339 * concurrent list_add(), but for better safety against future changes
340 * in the code, we use list_empty_careful() here.
342 if (!list_empty_careful(&isec->list)) {
343 spin_lock(&sbsec->isec_lock);
344 list_del_init(&isec->list);
345 spin_unlock(&sbsec->isec_lock);
349 * The inode may still be referenced in a path walk and
350 * a call to selinux_inode_permission() can be made
351 * after inode_free_security() is called. Ideally, the VFS
352 * wouldn't do this, but fixing that is a much harder
353 * job. For now, simply free the i_security via RCU, and
354 * leave the current inode->i_security pointer intact.
355 * The inode will be freed after the RCU grace period too.
357 call_rcu(&isec->rcu, inode_free_rcu);
360 static int file_alloc_security(struct file *file)
362 struct file_security_struct *fsec;
363 u32 sid = current_sid();
365 fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL);
370 fsec->fown_sid = sid;
371 file->f_security = fsec;
376 static void file_free_security(struct file *file)
378 struct file_security_struct *fsec = file->f_security;
379 file->f_security = NULL;
380 kmem_cache_free(file_security_cache, fsec);
383 static int superblock_alloc_security(struct super_block *sb)
385 struct superblock_security_struct *sbsec;
387 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
391 mutex_init(&sbsec->lock);
392 INIT_LIST_HEAD(&sbsec->isec_head);
393 spin_lock_init(&sbsec->isec_lock);
395 sbsec->sid = SECINITSID_UNLABELED;
396 sbsec->def_sid = SECINITSID_FILE;
397 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
398 sb->s_security = sbsec;
403 static void superblock_free_security(struct super_block *sb)
405 struct superblock_security_struct *sbsec = sb->s_security;
406 sb->s_security = NULL;
410 static inline int inode_doinit(struct inode *inode)
412 return inode_doinit_with_dentry(inode, NULL);
421 Opt_labelsupport = 5,
425 #define NUM_SEL_MNT_OPTS (Opt_nextmntopt - 1)
427 static const match_table_t tokens = {
428 {Opt_context, CONTEXT_STR "%s"},
429 {Opt_fscontext, FSCONTEXT_STR "%s"},
430 {Opt_defcontext, DEFCONTEXT_STR "%s"},
431 {Opt_rootcontext, ROOTCONTEXT_STR "%s"},
432 {Opt_labelsupport, LABELSUPP_STR},
436 #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
438 static int may_context_mount_sb_relabel(u32 sid,
439 struct superblock_security_struct *sbsec,
440 const struct cred *cred)
442 const struct task_security_struct *tsec = cred->security;
445 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
446 FILESYSTEM__RELABELFROM, NULL);
450 rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
451 FILESYSTEM__RELABELTO, NULL);
455 static int may_context_mount_inode_relabel(u32 sid,
456 struct superblock_security_struct *sbsec,
457 const struct cred *cred)
459 const struct task_security_struct *tsec = cred->security;
461 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
462 FILESYSTEM__RELABELFROM, NULL);
466 rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
467 FILESYSTEM__ASSOCIATE, NULL);
471 static int selinux_is_genfs_special_handling(struct super_block *sb)
473 /* Special handling. Genfs but also in-core setxattr handler */
474 return !strcmp(sb->s_type->name, "sysfs") ||
475 !strcmp(sb->s_type->name, "pstore") ||
476 !strcmp(sb->s_type->name, "debugfs") ||
477 !strcmp(sb->s_type->name, "rootfs");
480 static int selinux_is_sblabel_mnt(struct super_block *sb)
482 struct superblock_security_struct *sbsec = sb->s_security;
485 * IMPORTANT: Double-check logic in this function when adding a new
486 * SECURITY_FS_USE_* definition!
488 BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7);
490 switch (sbsec->behavior) {
491 case SECURITY_FS_USE_XATTR:
492 case SECURITY_FS_USE_TRANS:
493 case SECURITY_FS_USE_TASK:
494 case SECURITY_FS_USE_NATIVE:
497 case SECURITY_FS_USE_GENFS:
498 return selinux_is_genfs_special_handling(sb);
500 /* Never allow relabeling on context mounts */
501 case SECURITY_FS_USE_MNTPOINT:
502 case SECURITY_FS_USE_NONE:
508 static int sb_finish_set_opts(struct super_block *sb)
510 struct superblock_security_struct *sbsec = sb->s_security;
511 struct dentry *root = sb->s_root;
512 struct inode *root_inode = d_backing_inode(root);
515 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
516 /* Make sure that the xattr handler exists and that no
517 error other than -ENODATA is returned by getxattr on
518 the root directory. -ENODATA is ok, as this may be
519 the first boot of the SELinux kernel before we have
520 assigned xattr values to the filesystem. */
521 if (!(root_inode->i_opflags & IOP_XATTR)) {
522 printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
523 "xattr support\n", sb->s_id, sb->s_type->name);
528 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
529 if (rc < 0 && rc != -ENODATA) {
530 if (rc == -EOPNOTSUPP)
531 printk(KERN_WARNING "SELinux: (dev %s, type "
532 "%s) has no security xattr handler\n",
533 sb->s_id, sb->s_type->name);
535 printk(KERN_WARNING "SELinux: (dev %s, type "
536 "%s) getxattr errno %d\n", sb->s_id,
537 sb->s_type->name, -rc);
542 sbsec->flags |= SE_SBINITIALIZED;
543 if (selinux_is_sblabel_mnt(sb))
544 sbsec->flags |= SBLABEL_MNT;
546 /* Initialize the root inode. */
547 rc = inode_doinit_with_dentry(root_inode, root);
549 /* Initialize any other inodes associated with the superblock, e.g.
550 inodes created prior to initial policy load or inodes created
551 during get_sb by a pseudo filesystem that directly
553 spin_lock(&sbsec->isec_lock);
555 if (!list_empty(&sbsec->isec_head)) {
556 struct inode_security_struct *isec =
557 list_entry(sbsec->isec_head.next,
558 struct inode_security_struct, list);
559 struct inode *inode = isec->inode;
560 list_del_init(&isec->list);
561 spin_unlock(&sbsec->isec_lock);
562 inode = igrab(inode);
564 if (!IS_PRIVATE(inode))
568 spin_lock(&sbsec->isec_lock);
571 spin_unlock(&sbsec->isec_lock);
577 * This function should allow an FS to ask what it's mount security
578 * options were so it can use those later for submounts, displaying
579 * mount options, or whatever.
581 static int selinux_get_mnt_opts(const struct super_block *sb,
582 struct security_mnt_opts *opts)
585 struct superblock_security_struct *sbsec = sb->s_security;
586 char *context = NULL;
590 security_init_mnt_opts(opts);
592 if (!(sbsec->flags & SE_SBINITIALIZED))
598 /* make sure we always check enough bits to cover the mask */
599 BUILD_BUG_ON(SE_MNTMASK >= (1 << NUM_SEL_MNT_OPTS));
601 tmp = sbsec->flags & SE_MNTMASK;
602 /* count the number of mount options for this sb */
603 for (i = 0; i < NUM_SEL_MNT_OPTS; i++) {
605 opts->num_mnt_opts++;
608 /* Check if the Label support flag is set */
609 if (sbsec->flags & SBLABEL_MNT)
610 opts->num_mnt_opts++;
612 opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
613 if (!opts->mnt_opts) {
618 opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
619 if (!opts->mnt_opts_flags) {
625 if (sbsec->flags & FSCONTEXT_MNT) {
626 rc = security_sid_to_context(sbsec->sid, &context, &len);
629 opts->mnt_opts[i] = context;
630 opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
632 if (sbsec->flags & CONTEXT_MNT) {
633 rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
636 opts->mnt_opts[i] = context;
637 opts->mnt_opts_flags[i++] = CONTEXT_MNT;
639 if (sbsec->flags & DEFCONTEXT_MNT) {
640 rc = security_sid_to_context(sbsec->def_sid, &context, &len);
643 opts->mnt_opts[i] = context;
644 opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
646 if (sbsec->flags & ROOTCONTEXT_MNT) {
647 struct dentry *root = sbsec->sb->s_root;
648 struct inode_security_struct *isec = backing_inode_security(root);
650 rc = security_sid_to_context(isec->sid, &context, &len);
653 opts->mnt_opts[i] = context;
654 opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
656 if (sbsec->flags & SBLABEL_MNT) {
657 opts->mnt_opts[i] = NULL;
658 opts->mnt_opts_flags[i++] = SBLABEL_MNT;
661 BUG_ON(i != opts->num_mnt_opts);
666 security_free_mnt_opts(opts);
670 static int bad_option(struct superblock_security_struct *sbsec, char flag,
671 u32 old_sid, u32 new_sid)
673 char mnt_flags = sbsec->flags & SE_MNTMASK;
675 /* check if the old mount command had the same options */
676 if (sbsec->flags & SE_SBINITIALIZED)
677 if (!(sbsec->flags & flag) ||
678 (old_sid != new_sid))
681 /* check if we were passed the same options twice,
682 * aka someone passed context=a,context=b
684 if (!(sbsec->flags & SE_SBINITIALIZED))
685 if (mnt_flags & flag)
691 * Allow filesystems with binary mount data to explicitly set mount point
692 * labeling information.
694 static int selinux_set_mnt_opts(struct super_block *sb,
695 struct security_mnt_opts *opts,
696 unsigned long kern_flags,
697 unsigned long *set_kern_flags)
699 const struct cred *cred = current_cred();
701 struct superblock_security_struct *sbsec = sb->s_security;
702 const char *name = sb->s_type->name;
703 struct dentry *root = sbsec->sb->s_root;
704 struct inode_security_struct *root_isec;
705 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
706 u32 defcontext_sid = 0;
707 char **mount_options = opts->mnt_opts;
708 int *flags = opts->mnt_opts_flags;
709 int num_opts = opts->num_mnt_opts;
711 mutex_lock(&sbsec->lock);
713 if (!ss_initialized) {
715 /* Defer initialization until selinux_complete_init,
716 after the initial policy is loaded and the security
717 server is ready to handle calls. */
721 printk(KERN_WARNING "SELinux: Unable to set superblock options "
722 "before the security server is initialized\n");
725 if (kern_flags && !set_kern_flags) {
726 /* Specifying internal flags without providing a place to
727 * place the results is not allowed */
733 * Binary mount data FS will come through this function twice. Once
734 * from an explicit call and once from the generic calls from the vfs.
735 * Since the generic VFS calls will not contain any security mount data
736 * we need to skip the double mount verification.
738 * This does open a hole in which we will not notice if the first
739 * mount using this sb set explict options and a second mount using
740 * this sb does not set any security options. (The first options
741 * will be used for both mounts)
743 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
747 root_isec = backing_inode_security_novalidate(root);
750 * parse the mount options, check if they are valid sids.
751 * also check if someone is trying to mount the same sb more
752 * than once with different security options.
754 for (i = 0; i < num_opts; i++) {
757 if (flags[i] == SBLABEL_MNT)
759 rc = security_context_str_to_sid(mount_options[i], &sid, GFP_KERNEL);
761 printk(KERN_WARNING "SELinux: security_context_str_to_sid"
762 "(%s) failed for (dev %s, type %s) errno=%d\n",
763 mount_options[i], sb->s_id, name, rc);
770 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
772 goto out_double_mount;
774 sbsec->flags |= FSCONTEXT_MNT;
779 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
781 goto out_double_mount;
783 sbsec->flags |= CONTEXT_MNT;
785 case ROOTCONTEXT_MNT:
786 rootcontext_sid = sid;
788 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
790 goto out_double_mount;
792 sbsec->flags |= ROOTCONTEXT_MNT;
796 defcontext_sid = sid;
798 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
800 goto out_double_mount;
802 sbsec->flags |= DEFCONTEXT_MNT;
811 if (sbsec->flags & SE_SBINITIALIZED) {
812 /* previously mounted with options, but not on this attempt? */
813 if ((sbsec->flags & SE_MNTMASK) && !num_opts)
814 goto out_double_mount;
819 if (strcmp(sb->s_type->name, "proc") == 0)
820 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
822 if (!strcmp(sb->s_type->name, "debugfs") ||
823 !strcmp(sb->s_type->name, "sysfs") ||
824 !strcmp(sb->s_type->name, "pstore"))
825 sbsec->flags |= SE_SBGENFS;
827 if (!sbsec->behavior) {
829 * Determine the labeling behavior to use for this
832 rc = security_fs_use(sb);
835 "%s: security_fs_use(%s) returned %d\n",
836 __func__, sb->s_type->name, rc);
842 * If this is a user namespace mount, no contexts are allowed
843 * on the command line and security labels must be ignored.
845 if (sb->s_user_ns != &init_user_ns) {
846 if (context_sid || fscontext_sid || rootcontext_sid ||
851 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
852 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
853 rc = security_transition_sid(current_sid(), current_sid(),
855 &sbsec->mntpoint_sid);
862 /* sets the context of the superblock for the fs being mounted. */
864 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
868 sbsec->sid = fscontext_sid;
872 * Switch to using mount point labeling behavior.
873 * sets the label used on all file below the mountpoint, and will set
874 * the superblock context if not already set.
876 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
877 sbsec->behavior = SECURITY_FS_USE_NATIVE;
878 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
882 if (!fscontext_sid) {
883 rc = may_context_mount_sb_relabel(context_sid, sbsec,
887 sbsec->sid = context_sid;
889 rc = may_context_mount_inode_relabel(context_sid, sbsec,
894 if (!rootcontext_sid)
895 rootcontext_sid = context_sid;
897 sbsec->mntpoint_sid = context_sid;
898 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
901 if (rootcontext_sid) {
902 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
907 root_isec->sid = rootcontext_sid;
908 root_isec->initialized = LABEL_INITIALIZED;
911 if (defcontext_sid) {
912 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
913 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
915 printk(KERN_WARNING "SELinux: defcontext option is "
916 "invalid for this filesystem type\n");
920 if (defcontext_sid != sbsec->def_sid) {
921 rc = may_context_mount_inode_relabel(defcontext_sid,
927 sbsec->def_sid = defcontext_sid;
931 rc = sb_finish_set_opts(sb);
933 mutex_unlock(&sbsec->lock);
937 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different "
938 "security settings for (dev %s, type %s)\n", sb->s_id, name);
942 static int selinux_cmp_sb_context(const struct super_block *oldsb,
943 const struct super_block *newsb)
945 struct superblock_security_struct *old = oldsb->s_security;
946 struct superblock_security_struct *new = newsb->s_security;
947 char oldflags = old->flags & SE_MNTMASK;
948 char newflags = new->flags & SE_MNTMASK;
950 if (oldflags != newflags)
952 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
954 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
956 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
958 if (oldflags & ROOTCONTEXT_MNT) {
959 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
960 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
961 if (oldroot->sid != newroot->sid)
966 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, "
967 "different security settings for (dev %s, "
968 "type %s)\n", newsb->s_id, newsb->s_type->name);
972 static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
973 struct super_block *newsb)
975 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
976 struct superblock_security_struct *newsbsec = newsb->s_security;
978 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
979 int set_context = (oldsbsec->flags & CONTEXT_MNT);
980 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
983 * if the parent was able to be mounted it clearly had no special lsm
984 * mount options. thus we can safely deal with this superblock later
989 /* how can we clone if the old one wasn't set up?? */
990 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
992 /* if fs is reusing a sb, make sure that the contexts match */
993 if (newsbsec->flags & SE_SBINITIALIZED)
994 return selinux_cmp_sb_context(oldsb, newsb);
996 mutex_lock(&newsbsec->lock);
998 newsbsec->flags = oldsbsec->flags;
1000 newsbsec->sid = oldsbsec->sid;
1001 newsbsec->def_sid = oldsbsec->def_sid;
1002 newsbsec->behavior = oldsbsec->behavior;
1005 u32 sid = oldsbsec->mntpoint_sid;
1008 newsbsec->sid = sid;
1009 if (!set_rootcontext) {
1010 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
1013 newsbsec->mntpoint_sid = sid;
1015 if (set_rootcontext) {
1016 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
1017 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
1019 newisec->sid = oldisec->sid;
1022 sb_finish_set_opts(newsb);
1023 mutex_unlock(&newsbsec->lock);
1027 static int selinux_parse_opts_str(char *options,
1028 struct security_mnt_opts *opts)
1031 char *context = NULL, *defcontext = NULL;
1032 char *fscontext = NULL, *rootcontext = NULL;
1033 int rc, num_mnt_opts = 0;
1035 opts->num_mnt_opts = 0;
1037 /* Standard string-based options. */
1038 while ((p = strsep(&options, "|")) != NULL) {
1040 substring_t args[MAX_OPT_ARGS];
1045 token = match_token(p, tokens, args);
1049 if (context || defcontext) {
1051 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
1054 context = match_strdup(&args[0]);
1064 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
1067 fscontext = match_strdup(&args[0]);
1074 case Opt_rootcontext:
1077 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
1080 rootcontext = match_strdup(&args[0]);
1087 case Opt_defcontext:
1088 if (context || defcontext) {
1090 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
1093 defcontext = match_strdup(&args[0]);
1099 case Opt_labelsupport:
1103 printk(KERN_WARNING "SELinux: unknown mount option\n");
1110 opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
1111 if (!opts->mnt_opts)
1114 opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
1115 if (!opts->mnt_opts_flags) {
1116 kfree(opts->mnt_opts);
1121 opts->mnt_opts[num_mnt_opts] = fscontext;
1122 opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
1125 opts->mnt_opts[num_mnt_opts] = context;
1126 opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
1129 opts->mnt_opts[num_mnt_opts] = rootcontext;
1130 opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
1133 opts->mnt_opts[num_mnt_opts] = defcontext;
1134 opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
1137 opts->num_mnt_opts = num_mnt_opts;
1148 * string mount options parsing and call set the sbsec
1150 static int superblock_doinit(struct super_block *sb, void *data)
1153 char *options = data;
1154 struct security_mnt_opts opts;
1156 security_init_mnt_opts(&opts);
1161 BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
1163 rc = selinux_parse_opts_str(options, &opts);
1168 rc = selinux_set_mnt_opts(sb, &opts, 0, NULL);
1171 security_free_mnt_opts(&opts);
1175 static void selinux_write_opts(struct seq_file *m,
1176 struct security_mnt_opts *opts)
1181 for (i = 0; i < opts->num_mnt_opts; i++) {
1184 if (opts->mnt_opts[i])
1185 has_comma = strchr(opts->mnt_opts[i], ',');
1189 switch (opts->mnt_opts_flags[i]) {
1191 prefix = CONTEXT_STR;
1194 prefix = FSCONTEXT_STR;
1196 case ROOTCONTEXT_MNT:
1197 prefix = ROOTCONTEXT_STR;
1199 case DEFCONTEXT_MNT:
1200 prefix = DEFCONTEXT_STR;
1204 seq_puts(m, LABELSUPP_STR);
1210 /* we need a comma before each option */
1212 seq_puts(m, prefix);
1215 seq_escape(m, opts->mnt_opts[i], "\"\n\\");
1221 static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1223 struct security_mnt_opts opts;
1226 rc = selinux_get_mnt_opts(sb, &opts);
1228 /* before policy load we may get EINVAL, don't show anything */
1234 selinux_write_opts(m, &opts);
1236 security_free_mnt_opts(&opts);
1241 static inline u16 inode_mode_to_security_class(umode_t mode)
1243 switch (mode & S_IFMT) {
1245 return SECCLASS_SOCK_FILE;
1247 return SECCLASS_LNK_FILE;
1249 return SECCLASS_FILE;
1251 return SECCLASS_BLK_FILE;
1253 return SECCLASS_DIR;
1255 return SECCLASS_CHR_FILE;
1257 return SECCLASS_FIFO_FILE;
1261 return SECCLASS_FILE;
1264 static inline int default_protocol_stream(int protocol)
1266 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1269 static inline int default_protocol_dgram(int protocol)
1271 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1274 static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1280 case SOCK_SEQPACKET:
1281 return SECCLASS_UNIX_STREAM_SOCKET;
1283 return SECCLASS_UNIX_DGRAM_SOCKET;
1290 if (default_protocol_stream(protocol))
1291 return SECCLASS_TCP_SOCKET;
1293 return SECCLASS_RAWIP_SOCKET;
1295 if (default_protocol_dgram(protocol))
1296 return SECCLASS_UDP_SOCKET;
1298 return SECCLASS_RAWIP_SOCKET;
1300 return SECCLASS_DCCP_SOCKET;
1302 return SECCLASS_RAWIP_SOCKET;
1308 return SECCLASS_NETLINK_ROUTE_SOCKET;
1309 case NETLINK_SOCK_DIAG:
1310 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1312 return SECCLASS_NETLINK_NFLOG_SOCKET;
1314 return SECCLASS_NETLINK_XFRM_SOCKET;
1315 case NETLINK_SELINUX:
1316 return SECCLASS_NETLINK_SELINUX_SOCKET;
1318 return SECCLASS_NETLINK_ISCSI_SOCKET;
1320 return SECCLASS_NETLINK_AUDIT_SOCKET;
1321 case NETLINK_FIB_LOOKUP:
1322 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1323 case NETLINK_CONNECTOR:
1324 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1325 case NETLINK_NETFILTER:
1326 return SECCLASS_NETLINK_NETFILTER_SOCKET;
1327 case NETLINK_DNRTMSG:
1328 return SECCLASS_NETLINK_DNRT_SOCKET;
1329 case NETLINK_KOBJECT_UEVENT:
1330 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
1331 case NETLINK_GENERIC:
1332 return SECCLASS_NETLINK_GENERIC_SOCKET;
1333 case NETLINK_SCSITRANSPORT:
1334 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1336 return SECCLASS_NETLINK_RDMA_SOCKET;
1337 case NETLINK_CRYPTO:
1338 return SECCLASS_NETLINK_CRYPTO_SOCKET;
1340 return SECCLASS_NETLINK_SOCKET;
1343 return SECCLASS_PACKET_SOCKET;
1345 return SECCLASS_KEY_SOCKET;
1347 return SECCLASS_APPLETALK_SOCKET;
1350 return SECCLASS_SOCKET;
1353 static int selinux_genfs_get_sid(struct dentry *dentry,
1359 struct super_block *sb = dentry->d_sb;
1360 char *buffer, *path;
1362 buffer = (char *)__get_free_page(GFP_KERNEL);
1366 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1370 if (flags & SE_SBPROC) {
1371 /* each process gets a /proc/PID/ entry. Strip off the
1372 * PID part to get a valid selinux labeling.
1373 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1374 while (path[1] >= '0' && path[1] <= '9') {
1379 rc = security_genfs_sid(sb->s_type->name, path, tclass, sid);
1381 free_page((unsigned long)buffer);
1385 /* The inode's security attributes must be initialized before first use. */
1386 static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1388 struct superblock_security_struct *sbsec = NULL;
1389 struct inode_security_struct *isec = inode->i_security;
1390 u32 task_sid, sid = 0;
1392 struct dentry *dentry;
1393 #define INITCONTEXTLEN 255
1394 char *context = NULL;
1398 if (isec->initialized == LABEL_INITIALIZED)
1401 spin_lock(&isec->lock);
1402 if (isec->initialized == LABEL_INITIALIZED)
1405 if (isec->sclass == SECCLASS_FILE)
1406 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1408 sbsec = inode->i_sb->s_security;
1409 if (!(sbsec->flags & SE_SBINITIALIZED)) {
1410 /* Defer initialization until selinux_complete_init,
1411 after the initial policy is loaded and the security
1412 server is ready to handle calls. */
1413 spin_lock(&sbsec->isec_lock);
1414 if (list_empty(&isec->list))
1415 list_add(&isec->list, &sbsec->isec_head);
1416 spin_unlock(&sbsec->isec_lock);
1420 sclass = isec->sclass;
1421 task_sid = isec->task_sid;
1423 isec->initialized = LABEL_PENDING;
1424 spin_unlock(&isec->lock);
1426 switch (sbsec->behavior) {
1427 case SECURITY_FS_USE_NATIVE:
1429 case SECURITY_FS_USE_XATTR:
1430 if (!(inode->i_opflags & IOP_XATTR)) {
1431 sid = sbsec->def_sid;
1434 /* Need a dentry, since the xattr API requires one.
1435 Life would be simpler if we could just pass the inode. */
1437 /* Called from d_instantiate or d_splice_alias. */
1438 dentry = dget(opt_dentry);
1440 /* Called from selinux_complete_init, try to find a dentry. */
1441 dentry = d_find_alias(inode);
1445 * this is can be hit on boot when a file is accessed
1446 * before the policy is loaded. When we load policy we
1447 * may find inodes that have no dentry on the
1448 * sbsec->isec_head list. No reason to complain as these
1449 * will get fixed up the next time we go through
1450 * inode_doinit with a dentry, before these inodes could
1451 * be used again by userspace.
1456 len = INITCONTEXTLEN;
1457 context = kmalloc(len+1, GFP_NOFS);
1463 context[len] = '\0';
1464 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
1465 if (rc == -ERANGE) {
1468 /* Need a larger buffer. Query for the right size. */
1469 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
1475 context = kmalloc(len+1, GFP_NOFS);
1481 context[len] = '\0';
1482 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
1486 if (rc != -ENODATA) {
1487 printk(KERN_WARNING "SELinux: %s: getxattr returned "
1488 "%d for dev=%s ino=%ld\n", __func__,
1489 -rc, inode->i_sb->s_id, inode->i_ino);
1493 /* Map ENODATA to the default file SID */
1494 sid = sbsec->def_sid;
1497 rc = security_context_to_sid_default(context, rc, &sid,
1501 char *dev = inode->i_sb->s_id;
1502 unsigned long ino = inode->i_ino;
1504 if (rc == -EINVAL) {
1505 if (printk_ratelimit())
1506 printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
1507 "context=%s. This indicates you may need to relabel the inode or the "
1508 "filesystem in question.\n", ino, dev, context);
1510 printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) "
1511 "returned %d for dev=%s ino=%ld\n",
1512 __func__, context, -rc, dev, ino);
1515 /* Leave with the unlabeled SID */
1522 case SECURITY_FS_USE_TASK:
1525 case SECURITY_FS_USE_TRANS:
1526 /* Default to the fs SID. */
1529 /* Try to obtain a transition SID. */
1530 rc = security_transition_sid(task_sid, sid, sclass, NULL, &sid);
1534 case SECURITY_FS_USE_MNTPOINT:
1535 sid = sbsec->mntpoint_sid;
1538 /* Default to the fs superblock SID. */
1541 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
1542 /* We must have a dentry to determine the label on
1545 /* Called from d_instantiate or
1546 * d_splice_alias. */
1547 dentry = dget(opt_dentry);
1549 /* Called from selinux_complete_init, try to
1551 dentry = d_find_alias(inode);
1553 * This can be hit on boot when a file is accessed
1554 * before the policy is loaded. When we load policy we
1555 * may find inodes that have no dentry on the
1556 * sbsec->isec_head list. No reason to complain as
1557 * these will get fixed up the next time we go through
1558 * inode_doinit() with a dentry, before these inodes
1559 * could be used again by userspace.
1563 rc = selinux_genfs_get_sid(dentry, sclass,
1564 sbsec->flags, &sid);
1573 spin_lock(&isec->lock);
1574 if (isec->initialized == LABEL_PENDING) {
1576 isec->initialized = LABEL_INVALID;
1579 isec->initialized = LABEL_INITIALIZED;
1584 spin_unlock(&isec->lock);
1588 spin_lock(&isec->lock);
1589 if (isec->initialized == LABEL_PENDING) {
1590 isec->initialized = LABEL_INVALID;
1593 spin_unlock(&isec->lock);
1597 /* Convert a Linux signal to an access vector. */
1598 static inline u32 signal_to_av(int sig)
1604 /* Commonly granted from child to parent. */
1605 perm = PROCESS__SIGCHLD;
1608 /* Cannot be caught or ignored */
1609 perm = PROCESS__SIGKILL;
1612 /* Cannot be caught or ignored */
1613 perm = PROCESS__SIGSTOP;
1616 /* All other signals. */
1617 perm = PROCESS__SIGNAL;
1625 * Check permission between a pair of credentials
1626 * fork check, ptrace check, etc.
1628 static int cred_has_perm(const struct cred *actor,
1629 const struct cred *target,
1632 u32 asid = cred_sid(actor), tsid = cred_sid(target);
1634 return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1638 * Check permission between a pair of tasks, e.g. signal checks,
1639 * fork check, ptrace check, etc.
1640 * tsk1 is the actor and tsk2 is the target
1641 * - this uses the default subjective creds of tsk1
1643 static int task_has_perm(const struct task_struct *tsk1,
1644 const struct task_struct *tsk2,
1647 const struct task_security_struct *__tsec1, *__tsec2;
1651 __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid;
1652 __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid;
1654 return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
1658 * Check permission between current and another task, e.g. signal checks,
1659 * fork check, ptrace check, etc.
1660 * current is the actor and tsk2 is the target
1661 * - this uses current's subjective creds
1663 static int current_has_perm(const struct task_struct *tsk,
1668 sid = current_sid();
1669 tsid = task_sid(tsk);
1670 return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
1673 #if CAP_LAST_CAP > 63
1674 #error Fix SELinux to handle capabilities > 63.
1677 /* Check whether a task is allowed to use a capability. */
1678 static int cred_has_capability(const struct cred *cred,
1679 int cap, int audit, bool initns)
1681 struct common_audit_data ad;
1682 struct av_decision avd;
1684 u32 sid = cred_sid(cred);
1685 u32 av = CAP_TO_MASK(cap);
1688 ad.type = LSM_AUDIT_DATA_CAP;
1691 switch (CAP_TO_INDEX(cap)) {
1693 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
1696 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
1700 "SELinux: out of range capability %d\n", cap);
1705 rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
1706 if (audit == SECURITY_CAP_AUDIT) {
1707 int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0);
1714 /* Check whether a task is allowed to use a system operation. */
1715 static int task_has_system(struct task_struct *tsk,
1718 u32 sid = task_sid(tsk);
1720 return avc_has_perm(sid, SECINITSID_KERNEL,
1721 SECCLASS_SYSTEM, perms, NULL);
1724 /* Check whether a task has a particular permission to an inode.
1725 The 'adp' parameter is optional and allows other audit
1726 data to be passed (e.g. the dentry). */
1727 static int inode_has_perm(const struct cred *cred,
1728 struct inode *inode,
1730 struct common_audit_data *adp)
1732 struct inode_security_struct *isec;
1735 validate_creds(cred);
1737 if (unlikely(IS_PRIVATE(inode)))
1740 sid = cred_sid(cred);
1741 isec = inode->i_security;
1743 return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp);
1746 /* Same as inode_has_perm, but pass explicit audit data containing
1747 the dentry to help the auditing code to more easily generate the
1748 pathname if needed. */
1749 static inline int dentry_has_perm(const struct cred *cred,
1750 struct dentry *dentry,
1753 struct inode *inode = d_backing_inode(dentry);
1754 struct common_audit_data ad;
1756 ad.type = LSM_AUDIT_DATA_DENTRY;
1757 ad.u.dentry = dentry;
1758 __inode_security_revalidate(inode, dentry, true);
1759 return inode_has_perm(cred, inode, av, &ad);
1762 /* Same as inode_has_perm, but pass explicit audit data containing
1763 the path to help the auditing code to more easily generate the
1764 pathname if needed. */
1765 static inline int path_has_perm(const struct cred *cred,
1766 const struct path *path,
1769 struct inode *inode = d_backing_inode(path->dentry);
1770 struct common_audit_data ad;
1772 ad.type = LSM_AUDIT_DATA_PATH;
1774 __inode_security_revalidate(inode, path->dentry, true);
1775 return inode_has_perm(cred, inode, av, &ad);
1778 /* Same as path_has_perm, but uses the inode from the file struct. */
1779 static inline int file_path_has_perm(const struct cred *cred,
1783 struct common_audit_data ad;
1785 ad.type = LSM_AUDIT_DATA_FILE;
1787 return inode_has_perm(cred, file_inode(file), av, &ad);
1790 /* Check whether a task can use an open file descriptor to
1791 access an inode in a given way. Check access to the
1792 descriptor itself, and then use dentry_has_perm to
1793 check a particular permission to the file.
1794 Access to the descriptor is implicitly granted if it
1795 has the same SID as the process. If av is zero, then
1796 access to the file is not checked, e.g. for cases
1797 where only the descriptor is affected like seek. */
1798 static int file_has_perm(const struct cred *cred,
1802 struct file_security_struct *fsec = file->f_security;
1803 struct inode *inode = file_inode(file);
1804 struct common_audit_data ad;
1805 u32 sid = cred_sid(cred);
1808 ad.type = LSM_AUDIT_DATA_FILE;
1811 if (sid != fsec->sid) {
1812 rc = avc_has_perm(sid, fsec->sid,
1820 /* av is zero if only checking access to the descriptor. */
1823 rc = inode_has_perm(cred, inode, av, &ad);
1830 * Determine the label for an inode that might be unioned.
1833 selinux_determine_inode_label(const struct task_security_struct *tsec,
1835 const struct qstr *name, u16 tclass,
1838 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
1840 if ((sbsec->flags & SE_SBINITIALIZED) &&
1841 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1842 *_new_isid = sbsec->mntpoint_sid;
1843 } else if ((sbsec->flags & SBLABEL_MNT) &&
1845 *_new_isid = tsec->create_sid;
1847 const struct inode_security_struct *dsec = inode_security(dir);
1848 return security_transition_sid(tsec->sid, dsec->sid, tclass,
1855 /* Check whether a task can create a file. */
1856 static int may_create(struct inode *dir,
1857 struct dentry *dentry,
1860 const struct task_security_struct *tsec = current_security();
1861 struct inode_security_struct *dsec;
1862 struct superblock_security_struct *sbsec;
1864 struct common_audit_data ad;
1867 dsec = inode_security(dir);
1868 sbsec = dir->i_sb->s_security;
1872 ad.type = LSM_AUDIT_DATA_DENTRY;
1873 ad.u.dentry = dentry;
1875 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
1876 DIR__ADD_NAME | DIR__SEARCH,
1881 rc = selinux_determine_inode_label(current_security(), dir,
1882 &dentry->d_name, tclass, &newsid);
1886 rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
1890 return avc_has_perm(newsid, sbsec->sid,
1891 SECCLASS_FILESYSTEM,
1892 FILESYSTEM__ASSOCIATE, &ad);
1895 /* Check whether a task can create a key. */
1896 static int may_create_key(u32 ksid,
1897 struct task_struct *ctx)
1899 u32 sid = task_sid(ctx);
1901 return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
1905 #define MAY_UNLINK 1
1908 /* Check whether a task can link, unlink, or rmdir a file/directory. */
1909 static int may_link(struct inode *dir,
1910 struct dentry *dentry,
1914 struct inode_security_struct *dsec, *isec;
1915 struct common_audit_data ad;
1916 u32 sid = current_sid();
1920 dsec = inode_security(dir);
1921 isec = backing_inode_security(dentry);
1923 ad.type = LSM_AUDIT_DATA_DENTRY;
1924 ad.u.dentry = dentry;
1927 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
1928 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
1943 printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n",
1948 rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
1952 static inline int may_rename(struct inode *old_dir,
1953 struct dentry *old_dentry,
1954 struct inode *new_dir,
1955 struct dentry *new_dentry)
1957 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
1958 struct common_audit_data ad;
1959 u32 sid = current_sid();
1961 int old_is_dir, new_is_dir;
1964 old_dsec = inode_security(old_dir);
1965 old_isec = backing_inode_security(old_dentry);
1966 old_is_dir = d_is_dir(old_dentry);
1967 new_dsec = inode_security(new_dir);
1969 ad.type = LSM_AUDIT_DATA_DENTRY;
1971 ad.u.dentry = old_dentry;
1972 rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
1973 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1976 rc = avc_has_perm(sid, old_isec->sid,
1977 old_isec->sclass, FILE__RENAME, &ad);
1980 if (old_is_dir && new_dir != old_dir) {
1981 rc = avc_has_perm(sid, old_isec->sid,
1982 old_isec->sclass, DIR__REPARENT, &ad);
1987 ad.u.dentry = new_dentry;
1988 av = DIR__ADD_NAME | DIR__SEARCH;
1989 if (d_is_positive(new_dentry))
1990 av |= DIR__REMOVE_NAME;
1991 rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
1994 if (d_is_positive(new_dentry)) {
1995 new_isec = backing_inode_security(new_dentry);
1996 new_is_dir = d_is_dir(new_dentry);
1997 rc = avc_has_perm(sid, new_isec->sid,
1999 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
2007 /* Check whether a task can perform a filesystem operation. */
2008 static int superblock_has_perm(const struct cred *cred,
2009 struct super_block *sb,
2011 struct common_audit_data *ad)
2013 struct superblock_security_struct *sbsec;
2014 u32 sid = cred_sid(cred);
2016 sbsec = sb->s_security;
2017 return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
2020 /* Convert a Linux mode and permission mask to an access vector. */
2021 static inline u32 file_mask_to_av(int mode, int mask)
2025 if (!S_ISDIR(mode)) {
2026 if (mask & MAY_EXEC)
2027 av |= FILE__EXECUTE;
2028 if (mask & MAY_READ)
2031 if (mask & MAY_APPEND)
2033 else if (mask & MAY_WRITE)
2037 if (mask & MAY_EXEC)
2039 if (mask & MAY_WRITE)
2041 if (mask & MAY_READ)
2048 /* Convert a Linux file to an access vector. */
2049 static inline u32 file_to_av(struct file *file)
2053 if (file->f_mode & FMODE_READ)
2055 if (file->f_mode & FMODE_WRITE) {
2056 if (file->f_flags & O_APPEND)
2063 * Special file opened with flags 3 for ioctl-only use.
2072 * Convert a file to an access vector and include the correct open
2075 static inline u32 open_file_to_av(struct file *file)
2077 u32 av = file_to_av(file);
2078 struct inode *inode = file_inode(file);
2080 if (selinux_policycap_openperm && inode->i_sb->s_magic != SOCKFS_MAGIC)
2086 /* Hook functions begin here. */
2088 static int selinux_binder_set_context_mgr(const struct cred *mgr)
2090 return avc_has_perm(current_sid(), cred_sid(mgr), SECCLASS_BINDER,
2091 BINDER__SET_CONTEXT_MGR, NULL);
2094 static int selinux_binder_transaction(const struct cred *from,
2095 const struct cred *to)
2097 u32 mysid = current_sid();
2098 u32 fromsid = cred_sid(from);
2099 u32 tosid = cred_sid(to);
2102 if (mysid != fromsid) {
2103 rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER,
2104 BINDER__IMPERSONATE, NULL);
2109 return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
2113 static int selinux_binder_transfer_binder(const struct cred *from,
2114 const struct cred *to)
2116 return avc_has_perm(cred_sid(from), cred_sid(to),
2117 SECCLASS_BINDER, BINDER__TRANSFER,
2121 static int selinux_binder_transfer_file(const struct cred *from,
2122 const struct cred *to,
2125 u32 sid = cred_sid(to);
2126 struct file_security_struct *fsec = file->f_security;
2127 struct dentry *dentry = file->f_path.dentry;
2128 struct inode_security_struct *isec;
2129 struct common_audit_data ad;
2132 ad.type = LSM_AUDIT_DATA_PATH;
2133 ad.u.path = file->f_path;
2135 if (sid != fsec->sid) {
2136 rc = avc_has_perm(sid, fsec->sid,
2144 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
2147 isec = backing_inode_security(dentry);
2148 return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file),
2152 static int selinux_ptrace_access_check(struct task_struct *child,
2155 if (mode & PTRACE_MODE_READ) {
2156 u32 sid = current_sid();
2157 u32 csid = task_sid(child);
2158 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
2161 return current_has_perm(child, PROCESS__PTRACE);
2164 static int selinux_ptrace_traceme(struct task_struct *parent)
2166 return task_has_perm(parent, current, PROCESS__PTRACE);
2169 static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
2170 kernel_cap_t *inheritable, kernel_cap_t *permitted)
2172 return current_has_perm(target, PROCESS__GETCAP);
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 cred_has_perm(old, new, PROCESS__SETCAP);
2184 * (This comment used to live with the selinux_task_setuid hook,
2185 * which was removed).
2187 * Since setuid only affects the current process, and since the SELinux
2188 * controls are not based on the Linux identity attributes, SELinux does not
2189 * need to control this operation. However, SELinux does control the use of
2190 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2193 static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2196 return cred_has_capability(cred, cap, audit, ns == &init_user_ns);
2199 static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2201 const struct cred *cred = current_cred();
2213 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
2218 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
2221 rc = 0; /* let the kernel handle invalid cmds */
2227 static int selinux_quota_on(struct dentry *dentry)
2229 const struct cred *cred = current_cred();
2231 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
2234 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 rc = task_has_system(current, SYSTEM__SYSLOG_READ);
2243 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2244 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2245 /* Set level of messages printed to console */
2246 case SYSLOG_ACTION_CONSOLE_LEVEL:
2247 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
2249 case SYSLOG_ACTION_CLOSE: /* Close log */
2250 case SYSLOG_ACTION_OPEN: /* Open log */
2251 case SYSLOG_ACTION_READ: /* Read from log */
2252 case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */
2253 case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
2255 rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
2262 * Check that a process has enough memory to allocate a new virtual
2263 * mapping. 0 means there is enough memory for the allocation to
2264 * succeed and -ENOMEM implies there is not.
2266 * Do not audit the selinux permission check, as this is applied to all
2267 * processes that allocate mappings.
2269 static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
2271 int rc, cap_sys_admin = 0;
2273 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
2274 SECURITY_CAP_NOAUDIT, true);
2278 return cap_sys_admin;
2281 /* binprm security operations */
2283 static u32 ptrace_parent_sid(struct task_struct *task)
2286 struct task_struct *tracer;
2289 tracer = ptrace_parent(task);
2291 sid = task_sid(tracer);
2297 static int check_nnp_nosuid(const struct linux_binprm *bprm,
2298 const struct task_security_struct *old_tsec,
2299 const struct task_security_struct *new_tsec)
2301 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
2302 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
2305 if (!nnp && !nosuid)
2306 return 0; /* neither NNP nor nosuid */
2308 if (new_tsec->sid == old_tsec->sid)
2309 return 0; /* No change in credentials */
2312 * The only transitions we permit under NNP or nosuid
2313 * are transitions to bounded SIDs, i.e. SIDs that are
2314 * guaranteed to only be allowed a subset of the permissions
2315 * of the current SID.
2317 rc = security_bounded_transition(old_tsec->sid, new_tsec->sid);
2320 * On failure, preserve the errno values for NNP vs nosuid.
2321 * NNP: Operation not permitted for caller.
2322 * nosuid: Permission denied to file.
2332 static int selinux_bprm_set_creds(struct linux_binprm *bprm)
2334 const struct task_security_struct *old_tsec;
2335 struct task_security_struct *new_tsec;
2336 struct inode_security_struct *isec;
2337 struct common_audit_data ad;
2338 struct inode *inode = file_inode(bprm->file);
2341 /* SELinux context only depends on initial program or script and not
2342 * the script interpreter */
2343 if (bprm->cred_prepared)
2346 old_tsec = current_security();
2347 new_tsec = bprm->cred->security;
2348 isec = inode_security(inode);
2350 /* Default to the current task SID. */
2351 new_tsec->sid = old_tsec->sid;
2352 new_tsec->osid = old_tsec->sid;
2354 /* Reset fs, key, and sock SIDs on execve. */
2355 new_tsec->create_sid = 0;
2356 new_tsec->keycreate_sid = 0;
2357 new_tsec->sockcreate_sid = 0;
2359 if (old_tsec->exec_sid) {
2360 new_tsec->sid = old_tsec->exec_sid;
2361 /* Reset exec SID on execve. */
2362 new_tsec->exec_sid = 0;
2364 /* Fail on NNP or nosuid if not an allowed transition. */
2365 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2369 /* Check for a default transition on this program. */
2370 rc = security_transition_sid(old_tsec->sid, isec->sid,
2371 SECCLASS_PROCESS, NULL,
2377 * Fallback to old SID on NNP or nosuid if not an allowed
2380 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2382 new_tsec->sid = old_tsec->sid;
2385 ad.type = LSM_AUDIT_DATA_FILE;
2386 ad.u.file = bprm->file;
2388 if (new_tsec->sid == old_tsec->sid) {
2389 rc = avc_has_perm(old_tsec->sid, isec->sid,
2390 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2394 /* Check permissions for the transition. */
2395 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2396 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2400 rc = avc_has_perm(new_tsec->sid, isec->sid,
2401 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2405 /* Check for shared state */
2406 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2407 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2408 SECCLASS_PROCESS, PROCESS__SHARE,
2414 /* Make sure that anyone attempting to ptrace over a task that
2415 * changes its SID has the appropriate permit */
2417 (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2418 u32 ptsid = ptrace_parent_sid(current);
2420 rc = avc_has_perm(ptsid, new_tsec->sid,
2422 PROCESS__PTRACE, NULL);
2428 /* Clear any possibly unsafe personality bits on exec: */
2429 bprm->per_clear |= PER_CLEAR_ON_SETID;
2435 static int selinux_bprm_secureexec(struct linux_binprm *bprm)
2437 const struct task_security_struct *tsec = current_security();
2445 /* Enable secure mode for SIDs transitions unless
2446 the noatsecure permission is granted between
2447 the two SIDs, i.e. ahp returns 0. */
2448 atsecure = avc_has_perm(osid, sid,
2450 PROCESS__NOATSECURE, NULL);
2456 static int match_file(const void *p, struct file *file, unsigned fd)
2458 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2461 /* Derived from fs/exec.c:flush_old_files. */
2462 static inline void flush_unauthorized_files(const struct cred *cred,
2463 struct files_struct *files)
2465 struct file *file, *devnull = NULL;
2466 struct tty_struct *tty;
2470 tty = get_current_tty();
2472 spin_lock(&tty->files_lock);
2473 if (!list_empty(&tty->tty_files)) {
2474 struct tty_file_private *file_priv;
2476 /* Revalidate access to controlling tty.
2477 Use file_path_has_perm on the tty path directly
2478 rather than using file_has_perm, as this particular
2479 open file may belong to another process and we are
2480 only interested in the inode-based check here. */
2481 file_priv = list_first_entry(&tty->tty_files,
2482 struct tty_file_private, list);
2483 file = file_priv->file;
2484 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
2487 spin_unlock(&tty->files_lock);
2490 /* Reset controlling tty. */
2494 /* Revalidate access to inherited open files. */
2495 n = iterate_fd(files, 0, match_file, cred);
2496 if (!n) /* none found? */
2499 devnull = dentry_open(&selinux_null, O_RDWR, cred);
2500 if (IS_ERR(devnull))
2502 /* replace all the matching ones with this */
2504 replace_fd(n - 1, devnull, 0);
2505 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2511 * Prepare a process for imminent new credential changes due to exec
2513 static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
2515 struct task_security_struct *new_tsec;
2516 struct rlimit *rlim, *initrlim;
2519 new_tsec = bprm->cred->security;
2520 if (new_tsec->sid == new_tsec->osid)
2523 /* Close files for which the new task SID is not authorized. */
2524 flush_unauthorized_files(bprm->cred, current->files);
2526 /* Always clear parent death signal on SID transitions. */
2527 current->pdeath_signal = 0;
2529 /* Check whether the new SID can inherit resource limits from the old
2530 * SID. If not, reset all soft limits to the lower of the current
2531 * task's hard limit and the init task's soft limit.
2533 * Note that the setting of hard limits (even to lower them) can be
2534 * controlled by the setrlimit check. The inclusion of the init task's
2535 * soft limit into the computation is to avoid resetting soft limits
2536 * higher than the default soft limit for cases where the default is
2537 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2539 rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2540 PROCESS__RLIMITINH, NULL);
2542 /* protect against do_prlimit() */
2544 for (i = 0; i < RLIM_NLIMITS; i++) {
2545 rlim = current->signal->rlim + i;
2546 initrlim = init_task.signal->rlim + i;
2547 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2549 task_unlock(current);
2550 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
2555 * Clean up the process immediately after the installation of new credentials
2558 static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2560 const struct task_security_struct *tsec = current_security();
2561 struct itimerval itimer;
2571 /* Check whether the new SID can inherit signal state from the old SID.
2572 * If not, clear itimers to avoid subsequent signal generation and
2573 * flush and unblock signals.
2575 * This must occur _after_ the task SID has been updated so that any
2576 * kill done after the flush will be checked against the new SID.
2578 rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
2580 memset(&itimer, 0, sizeof itimer);
2581 for (i = 0; i < 3; i++)
2582 do_setitimer(i, &itimer, NULL);
2583 spin_lock_irq(¤t->sighand->siglock);
2584 if (!fatal_signal_pending(current)) {
2585 flush_sigqueue(¤t->pending);
2586 flush_sigqueue(¤t->signal->shared_pending);
2587 flush_signal_handlers(current, 1);
2588 sigemptyset(¤t->blocked);
2589 recalc_sigpending();
2591 spin_unlock_irq(¤t->sighand->siglock);
2594 /* Wake up the parent if it is waiting so that it can recheck
2595 * wait permission to the new task SID. */
2596 read_lock(&tasklist_lock);
2597 __wake_up_parent(current, current->real_parent);
2598 read_unlock(&tasklist_lock);
2601 /* superblock security operations */
2603 static int selinux_sb_alloc_security(struct super_block *sb)
2605 return superblock_alloc_security(sb);
2608 static void selinux_sb_free_security(struct super_block *sb)
2610 superblock_free_security(sb);
2613 static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2618 return !memcmp(prefix, option, plen);
2621 static inline int selinux_option(char *option, int len)
2623 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2624 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2625 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
2626 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2627 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
2630 static inline void take_option(char **to, char *from, int *first, int len)
2637 memcpy(*to, from, len);
2641 static inline void take_selinux_option(char **to, char *from, int *first,
2644 int current_size = 0;
2652 while (current_size < len) {
2662 static int selinux_sb_copy_data(char *orig, char *copy)
2664 int fnosec, fsec, rc = 0;
2665 char *in_save, *in_curr, *in_end;
2666 char *sec_curr, *nosec_save, *nosec;
2672 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2680 in_save = in_end = orig;
2684 open_quote = !open_quote;
2685 if ((*in_end == ',' && open_quote == 0) ||
2687 int len = in_end - in_curr;
2689 if (selinux_option(in_curr, len))
2690 take_selinux_option(&sec_curr, in_curr, &fsec, len);
2692 take_option(&nosec, in_curr, &fnosec, len);
2694 in_curr = in_end + 1;
2696 } while (*in_end++);
2698 strcpy(in_save, nosec_save);
2699 free_page((unsigned long)nosec_save);
2704 static int selinux_sb_remount(struct super_block *sb, void *data)
2707 struct security_mnt_opts opts;
2708 char *secdata, **mount_options;
2709 struct superblock_security_struct *sbsec = sb->s_security;
2711 if (!(sbsec->flags & SE_SBINITIALIZED))
2717 if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2720 security_init_mnt_opts(&opts);
2721 secdata = alloc_secdata();
2724 rc = selinux_sb_copy_data(data, secdata);
2726 goto out_free_secdata;
2728 rc = selinux_parse_opts_str(secdata, &opts);
2730 goto out_free_secdata;
2732 mount_options = opts.mnt_opts;
2733 flags = opts.mnt_opts_flags;
2735 for (i = 0; i < opts.num_mnt_opts; i++) {
2738 if (flags[i] == SBLABEL_MNT)
2740 rc = security_context_str_to_sid(mount_options[i], &sid, GFP_KERNEL);
2742 printk(KERN_WARNING "SELinux: security_context_str_to_sid"
2743 "(%s) failed for (dev %s, type %s) errno=%d\n",
2744 mount_options[i], sb->s_id, sb->s_type->name, rc);
2750 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2751 goto out_bad_option;
2754 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2755 goto out_bad_option;
2757 case ROOTCONTEXT_MNT: {
2758 struct inode_security_struct *root_isec;
2759 root_isec = backing_inode_security(sb->s_root);
2761 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2762 goto out_bad_option;
2765 case DEFCONTEXT_MNT:
2766 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2767 goto out_bad_option;
2776 security_free_mnt_opts(&opts);
2778 free_secdata(secdata);
2781 printk(KERN_WARNING "SELinux: unable to change security options "
2782 "during remount (dev %s, type=%s)\n", sb->s_id,
2787 static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
2789 const struct cred *cred = current_cred();
2790 struct common_audit_data ad;
2793 rc = superblock_doinit(sb, data);
2797 /* Allow all mounts performed by the kernel */
2798 if (flags & (MS_KERNMOUNT | MS_SUBMOUNT))
2801 ad.type = LSM_AUDIT_DATA_DENTRY;
2802 ad.u.dentry = sb->s_root;
2803 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
2806 static int selinux_sb_statfs(struct dentry *dentry)
2808 const struct cred *cred = current_cred();
2809 struct common_audit_data ad;
2811 ad.type = LSM_AUDIT_DATA_DENTRY;
2812 ad.u.dentry = dentry->d_sb->s_root;
2813 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
2816 static int selinux_mount(const char *dev_name,
2817 const struct path *path,
2819 unsigned long flags,
2822 const struct cred *cred = current_cred();
2824 if (flags & MS_REMOUNT)
2825 return superblock_has_perm(cred, path->dentry->d_sb,
2826 FILESYSTEM__REMOUNT, NULL);
2828 return path_has_perm(cred, path, FILE__MOUNTON);
2831 static int selinux_umount(struct vfsmount *mnt, int flags)
2833 const struct cred *cred = current_cred();
2835 return superblock_has_perm(cred, mnt->mnt_sb,
2836 FILESYSTEM__UNMOUNT, NULL);
2839 /* inode security operations */
2841 static int selinux_inode_alloc_security(struct inode *inode)
2843 return inode_alloc_security(inode);
2846 static void selinux_inode_free_security(struct inode *inode)
2848 inode_free_security(inode);
2851 static int selinux_dentry_init_security(struct dentry *dentry, int mode,
2852 const struct qstr *name, void **ctx,
2858 rc = selinux_determine_inode_label(current_security(),
2859 d_inode(dentry->d_parent), name,
2860 inode_mode_to_security_class(mode),
2865 return security_sid_to_context(newsid, (char **)ctx, ctxlen);
2868 static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2870 const struct cred *old,
2875 struct task_security_struct *tsec;
2877 rc = selinux_determine_inode_label(old->security,
2878 d_inode(dentry->d_parent), name,
2879 inode_mode_to_security_class(mode),
2884 tsec = new->security;
2885 tsec->create_sid = newsid;
2889 static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
2890 const struct qstr *qstr,
2892 void **value, size_t *len)
2894 const struct task_security_struct *tsec = current_security();
2895 struct superblock_security_struct *sbsec;
2896 u32 sid, newsid, clen;
2900 sbsec = dir->i_sb->s_security;
2903 newsid = tsec->create_sid;
2905 rc = selinux_determine_inode_label(current_security(),
2907 inode_mode_to_security_class(inode->i_mode),
2912 /* Possibly defer initialization to selinux_complete_init. */
2913 if (sbsec->flags & SE_SBINITIALIZED) {
2914 struct inode_security_struct *isec = inode->i_security;
2915 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2917 isec->initialized = LABEL_INITIALIZED;
2920 if (!ss_initialized || !(sbsec->flags & SBLABEL_MNT))
2924 *name = XATTR_SELINUX_SUFFIX;
2927 rc = security_sid_to_context_force(newsid, &context, &clen);
2937 static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
2939 return may_create(dir, dentry, SECCLASS_FILE);
2942 static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2944 return may_link(dir, old_dentry, MAY_LINK);
2947 static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2949 return may_link(dir, dentry, MAY_UNLINK);
2952 static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2954 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2957 static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
2959 return may_create(dir, dentry, SECCLASS_DIR);
2962 static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2964 return may_link(dir, dentry, MAY_RMDIR);
2967 static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
2969 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2972 static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
2973 struct inode *new_inode, struct dentry *new_dentry)
2975 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2978 static int selinux_inode_readlink(struct dentry *dentry)
2980 const struct cred *cred = current_cred();
2982 return dentry_has_perm(cred, dentry, FILE__READ);
2985 static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2988 const struct cred *cred = current_cred();
2989 struct common_audit_data ad;
2990 struct inode_security_struct *isec;
2993 validate_creds(cred);
2995 ad.type = LSM_AUDIT_DATA_DENTRY;
2996 ad.u.dentry = dentry;
2997 sid = cred_sid(cred);
2998 isec = inode_security_rcu(inode, rcu);
3000 return PTR_ERR(isec);
3002 return avc_has_perm_flags(sid, isec->sid, isec->sclass, FILE__READ, &ad,
3003 rcu ? MAY_NOT_BLOCK : 0);
3006 static noinline int audit_inode_permission(struct inode *inode,
3007 u32 perms, u32 audited, u32 denied,
3011 struct common_audit_data ad;
3012 struct inode_security_struct *isec = inode->i_security;
3015 ad.type = LSM_AUDIT_DATA_INODE;
3018 rc = slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms,
3019 audited, denied, result, &ad, flags);
3025 static int selinux_inode_permission(struct inode *inode, int mask)
3027 const struct cred *cred = current_cred();
3030 unsigned flags = mask & MAY_NOT_BLOCK;
3031 struct inode_security_struct *isec;
3033 struct av_decision avd;
3035 u32 audited, denied;
3037 from_access = mask & MAY_ACCESS;
3038 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3040 /* No permission to check. Existence test. */
3044 validate_creds(cred);
3046 if (unlikely(IS_PRIVATE(inode)))
3049 perms = file_mask_to_av(inode->i_mode, mask);
3051 sid = cred_sid(cred);
3052 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
3054 return PTR_ERR(isec);
3056 rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0, &avd);
3057 audited = avc_audit_required(perms, &avd, rc,
3058 from_access ? FILE__AUDIT_ACCESS : 0,
3060 if (likely(!audited))
3063 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
3069 static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3071 const struct cred *cred = current_cred();
3072 struct inode *inode = d_backing_inode(dentry);
3073 unsigned int ia_valid = iattr->ia_valid;
3074 __u32 av = FILE__WRITE;
3076 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3077 if (ia_valid & ATTR_FORCE) {
3078 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3084 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3085 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
3086 return dentry_has_perm(cred, dentry, FILE__SETATTR);
3088 if (selinux_policycap_openperm &&
3089 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3090 (ia_valid & ATTR_SIZE) &&
3091 !(ia_valid & ATTR_FILE))
3094 return dentry_has_perm(cred, dentry, av);
3097 static int selinux_inode_getattr(const struct path *path)
3099 return path_has_perm(current_cred(), path, FILE__GETATTR);
3102 static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
3104 const struct cred *cred = current_cred();
3106 if (!strncmp(name, XATTR_SECURITY_PREFIX,
3107 sizeof XATTR_SECURITY_PREFIX - 1)) {
3108 if (!strcmp(name, XATTR_NAME_CAPS)) {
3109 if (!capable(CAP_SETFCAP))
3111 } else if (!capable(CAP_SYS_ADMIN)) {
3112 /* A different attribute in the security namespace.
3113 Restrict to administrator. */
3118 /* Not an attribute we recognize, so just check the
3119 ordinary setattr permission. */
3120 return dentry_has_perm(cred, dentry, FILE__SETATTR);
3123 static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3124 const void *value, size_t size, int flags)
3126 struct inode *inode = d_backing_inode(dentry);
3127 struct inode_security_struct *isec;
3128 struct superblock_security_struct *sbsec;
3129 struct common_audit_data ad;
3130 u32 newsid, sid = current_sid();
3133 if (strcmp(name, XATTR_NAME_SELINUX))
3134 return selinux_inode_setotherxattr(dentry, name);
3136 sbsec = inode->i_sb->s_security;
3137 if (!(sbsec->flags & SBLABEL_MNT))
3140 if (!inode_owner_or_capable(inode))
3143 ad.type = LSM_AUDIT_DATA_DENTRY;
3144 ad.u.dentry = dentry;
3146 isec = backing_inode_security(dentry);
3147 rc = avc_has_perm(sid, isec->sid, isec->sclass,
3148 FILE__RELABELFROM, &ad);
3152 rc = security_context_to_sid(value, size, &newsid, GFP_KERNEL);
3153 if (rc == -EINVAL) {
3154 if (!capable(CAP_MAC_ADMIN)) {
3155 struct audit_buffer *ab;
3159 /* We strip a nul only if it is at the end, otherwise the
3160 * context contains a nul and we should audit that */
3163 if (str[size - 1] == '\0')
3164 audit_size = size - 1;
3171 ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
3172 audit_log_format(ab, "op=setxattr invalid_context=");
3173 audit_log_n_untrustedstring(ab, value, audit_size);
3178 rc = security_context_to_sid_force(value, size, &newsid);
3183 rc = avc_has_perm(sid, newsid, isec->sclass,
3184 FILE__RELABELTO, &ad);
3188 rc = security_validate_transition(isec->sid, newsid, sid,
3193 return avc_has_perm(newsid,
3195 SECCLASS_FILESYSTEM,
3196 FILESYSTEM__ASSOCIATE,
3200 static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
3201 const void *value, size_t size,
3204 struct inode *inode = d_backing_inode(dentry);
3205 struct inode_security_struct *isec;
3209 if (strcmp(name, XATTR_NAME_SELINUX)) {
3210 /* Not an attribute we recognize, so nothing to do. */
3214 rc = security_context_to_sid_force(value, size, &newsid);
3216 printk(KERN_ERR "SELinux: unable to map context to SID"
3217 "for (%s, %lu), rc=%d\n",
3218 inode->i_sb->s_id, inode->i_ino, -rc);
3222 isec = backing_inode_security(dentry);
3223 spin_lock(&isec->lock);
3224 isec->sclass = inode_mode_to_security_class(inode->i_mode);
3226 isec->initialized = LABEL_INITIALIZED;
3227 spin_unlock(&isec->lock);
3232 static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
3234 const struct cred *cred = current_cred();
3236 return dentry_has_perm(cred, dentry, FILE__GETATTR);
3239 static int selinux_inode_listxattr(struct dentry *dentry)
3241 const struct cred *cred = current_cred();
3243 return dentry_has_perm(cred, dentry, FILE__GETATTR);
3246 static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
3248 if (strcmp(name, XATTR_NAME_SELINUX))
3249 return selinux_inode_setotherxattr(dentry, name);
3251 /* No one is allowed to remove a SELinux security label.
3252 You can change the label, but all data must be labeled. */
3257 * Copy the inode security context value to the user.
3259 * Permission check is handled by selinux_inode_getxattr hook.
3261 static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
3265 char *context = NULL;
3266 struct inode_security_struct *isec;
3268 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3272 * If the caller has CAP_MAC_ADMIN, then get the raw context
3273 * value even if it is not defined by current policy; otherwise,
3274 * use the in-core value under current policy.
3275 * Use the non-auditing forms of the permission checks since
3276 * getxattr may be called by unprivileged processes commonly
3277 * and lack of permission just means that we fall back to the
3278 * in-core context value, not a denial.
3280 error = cap_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
3281 SECURITY_CAP_NOAUDIT);
3283 error = cred_has_capability(current_cred(), CAP_MAC_ADMIN,
3284 SECURITY_CAP_NOAUDIT, true);
3285 isec = inode_security(inode);
3287 error = security_sid_to_context_force(isec->sid, &context,
3290 error = security_sid_to_context(isec->sid, &context, &size);
3303 static int selinux_inode_setsecurity(struct inode *inode, const char *name,
3304 const void *value, size_t size, int flags)
3306 struct inode_security_struct *isec = inode_security_novalidate(inode);
3307 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
3311 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3314 if (!(sbsec->flags & SBLABEL_MNT))
3317 if (!value || !size)
3320 rc = security_context_to_sid(value, size, &newsid, GFP_KERNEL);
3324 spin_lock(&isec->lock);
3325 isec->sclass = inode_mode_to_security_class(inode->i_mode);
3327 isec->initialized = LABEL_INITIALIZED;
3328 spin_unlock(&isec->lock);
3332 static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3334 const int len = sizeof(XATTR_NAME_SELINUX);
3335 if (buffer && len <= buffer_size)
3336 memcpy(buffer, XATTR_NAME_SELINUX, len);
3340 static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
3342 struct inode_security_struct *isec = inode_security_novalidate(inode);
3346 static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3349 struct task_security_struct *tsec;
3350 struct cred *new_creds = *new;
3352 if (new_creds == NULL) {
3353 new_creds = prepare_creds();
3358 tsec = new_creds->security;
3359 /* Get label from overlay inode and set it in create_sid */
3360 selinux_inode_getsecid(d_inode(src), &sid);
3361 tsec->create_sid = sid;
3366 static int selinux_inode_copy_up_xattr(const char *name)
3368 /* The copy_up hook above sets the initial context on an inode, but we
3369 * don't then want to overwrite it by blindly copying all the lower
3370 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3372 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3373 return 1; /* Discard */
3375 * Any other attribute apart from SELINUX is not claimed, supported
3381 /* file security operations */
3383 static int selinux_revalidate_file_permission(struct file *file, int mask)
3385 const struct cred *cred = current_cred();
3386 struct inode *inode = file_inode(file);
3388 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3389 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3392 return file_has_perm(cred, file,
3393 file_mask_to_av(inode->i_mode, mask));
3396 static int selinux_file_permission(struct file *file, int mask)
3398 struct inode *inode = file_inode(file);
3399 struct file_security_struct *fsec = file->f_security;
3400 struct inode_security_struct *isec;
3401 u32 sid = current_sid();
3404 /* No permission to check. Existence test. */
3407 isec = inode_security(inode);
3408 if (sid == fsec->sid && fsec->isid == isec->sid &&
3409 fsec->pseqno == avc_policy_seqno())
3410 /* No change since file_open check. */
3413 return selinux_revalidate_file_permission(file, mask);
3416 static int selinux_file_alloc_security(struct file *file)
3418 return file_alloc_security(file);
3421 static void selinux_file_free_security(struct file *file)
3423 file_free_security(file);
3427 * Check whether a task has the ioctl permission and cmd
3428 * operation to an inode.
3430 static int ioctl_has_perm(const struct cred *cred, struct file *file,
3431 u32 requested, u16 cmd)
3433 struct common_audit_data ad;
3434 struct file_security_struct *fsec = file->f_security;
3435 struct inode *inode = file_inode(file);
3436 struct inode_security_struct *isec;
3437 struct lsm_ioctlop_audit ioctl;
3438 u32 ssid = cred_sid(cred);
3440 u8 driver = cmd >> 8;
3441 u8 xperm = cmd & 0xff;
3443 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3446 ad.u.op->path = file->f_path;
3448 if (ssid != fsec->sid) {
3449 rc = avc_has_perm(ssid, fsec->sid,
3457 if (unlikely(IS_PRIVATE(inode)))
3460 isec = inode_security(inode);
3461 rc = avc_has_extended_perms(ssid, isec->sid, isec->sclass,
3462 requested, driver, xperm, &ad);
3467 static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3470 const struct cred *cred = current_cred();
3480 case FS_IOC_GETFLAGS:
3482 case FS_IOC_GETVERSION:
3483 error = file_has_perm(cred, file, FILE__GETATTR);
3486 case FS_IOC_SETFLAGS:
3488 case FS_IOC_SETVERSION:
3489 error = file_has_perm(cred, file, FILE__SETATTR);
3492 /* sys_ioctl() checks */
3496 error = file_has_perm(cred, file, 0);
3501 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3502 SECURITY_CAP_AUDIT, true);
3505 /* default case assumes that the command will go
3506 * to the file's ioctl() function.
3509 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
3514 static int default_noexec;
3516 static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3518 const struct cred *cred = current_cred();
3521 if (default_noexec &&
3522 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3523 (!shared && (prot & PROT_WRITE)))) {
3525 * We are making executable an anonymous mapping or a
3526 * private file mapping that will also be writable.
3527 * This has an additional check.
3529 rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
3535 /* read access is always possible with a mapping */
3536 u32 av = FILE__READ;
3538 /* write access only matters if the mapping is shared */
3539 if (shared && (prot & PROT_WRITE))
3542 if (prot & PROT_EXEC)
3543 av |= FILE__EXECUTE;
3545 return file_has_perm(cred, file, av);
3552 static int selinux_mmap_addr(unsigned long addr)
3556 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3557 u32 sid = current_sid();
3558 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3559 MEMPROTECT__MMAP_ZERO, NULL);
3565 static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3566 unsigned long prot, unsigned long flags)
3568 if (selinux_checkreqprot)
3571 return file_map_prot_check(file, prot,
3572 (flags & MAP_TYPE) == MAP_SHARED);
3575 static int selinux_file_mprotect(struct vm_area_struct *vma,
3576 unsigned long reqprot,
3579 const struct cred *cred = current_cred();
3581 if (selinux_checkreqprot)
3584 if (default_noexec &&
3585 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3587 if (vma->vm_start >= vma->vm_mm->start_brk &&
3588 vma->vm_end <= vma->vm_mm->brk) {
3589 rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
3590 } else if (!vma->vm_file &&
3591 ((vma->vm_start <= vma->vm_mm->start_stack &&
3592 vma->vm_end >= vma->vm_mm->start_stack) ||
3593 vma_is_stack_for_current(vma))) {
3594 rc = current_has_perm(current, PROCESS__EXECSTACK);
3595 } else if (vma->vm_file && vma->anon_vma) {
3597 * We are making executable a file mapping that has
3598 * had some COW done. Since pages might have been
3599 * written, check ability to execute the possibly
3600 * modified content. This typically should only
3601 * occur for text relocations.
3603 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
3609 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3612 static int selinux_file_lock(struct file *file, unsigned int cmd)
3614 const struct cred *cred = current_cred();
3616 return file_has_perm(cred, file, FILE__LOCK);
3619 static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3622 const struct cred *cred = current_cred();
3627 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
3628 err = file_has_perm(cred, file, FILE__WRITE);
3637 case F_GETOWNER_UIDS:
3638 /* Just check FD__USE permission */
3639 err = file_has_perm(cred, file, 0);
3647 #if BITS_PER_LONG == 32
3652 err = file_has_perm(cred, file, FILE__LOCK);
3659 static void selinux_file_set_fowner(struct file *file)
3661 struct file_security_struct *fsec;
3663 fsec = file->f_security;
3664 fsec->fown_sid = current_sid();
3667 static int selinux_file_send_sigiotask(struct task_struct *tsk,
3668 struct fown_struct *fown, int signum)
3671 u32 sid = task_sid(tsk);
3673 struct file_security_struct *fsec;
3675 /* struct fown_struct is never outside the context of a struct file */
3676 file = container_of(fown, struct file, f_owner);
3678 fsec = file->f_security;
3681 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3683 perm = signal_to_av(signum);
3685 return avc_has_perm(fsec->fown_sid, sid,
3686 SECCLASS_PROCESS, perm, NULL);
3689 static int selinux_file_receive(struct file *file)
3691 const struct cred *cred = current_cred();
3693 return file_has_perm(cred, file, file_to_av(file));
3696 static int selinux_file_open(struct file *file, const struct cred *cred)
3698 struct file_security_struct *fsec;
3699 struct inode_security_struct *isec;
3701 fsec = file->f_security;
3702 isec = inode_security(file_inode(file));
3704 * Save inode label and policy sequence number
3705 * at open-time so that selinux_file_permission
3706 * can determine whether revalidation is necessary.
3707 * Task label is already saved in the file security
3708 * struct as its SID.
3710 fsec->isid = isec->sid;
3711 fsec->pseqno = avc_policy_seqno();
3713 * Since the inode label or policy seqno may have changed
3714 * between the selinux_inode_permission check and the saving
3715 * of state above, recheck that access is still permitted.
3716 * Otherwise, access might never be revalidated against the
3717 * new inode label or new policy.
3718 * This check is not redundant - do not remove.
3720 return file_path_has_perm(cred, file, open_file_to_av(file));
3723 /* task security operations */
3725 static int selinux_task_create(unsigned long clone_flags)
3727 return current_has_perm(current, PROCESS__FORK);
3731 * allocate the SELinux part of blank credentials
3733 static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3735 struct task_security_struct *tsec;
3737 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3741 cred->security = tsec;
3746 * detach and free the LSM part of a set of credentials
3748 static void selinux_cred_free(struct cred *cred)
3750 struct task_security_struct *tsec = cred->security;
3753 * cred->security == NULL if security_cred_alloc_blank() or
3754 * security_prepare_creds() returned an error.
3756 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
3757 cred->security = (void *) 0x7UL;
3762 * prepare a new set of credentials for modification
3764 static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3767 const struct task_security_struct *old_tsec;
3768 struct task_security_struct *tsec;
3770 old_tsec = old->security;
3772 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3776 new->security = tsec;
3781 * transfer the SELinux data to a blank set of creds
3783 static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3785 const struct task_security_struct *old_tsec = old->security;
3786 struct task_security_struct *tsec = new->security;
3792 * set the security data for a kernel service
3793 * - all the creation contexts are set to unlabelled
3795 static int selinux_kernel_act_as(struct cred *new, u32 secid)
3797 struct task_security_struct *tsec = new->security;
3798 u32 sid = current_sid();
3801 ret = avc_has_perm(sid, secid,
3802 SECCLASS_KERNEL_SERVICE,
3803 KERNEL_SERVICE__USE_AS_OVERRIDE,
3807 tsec->create_sid = 0;
3808 tsec->keycreate_sid = 0;
3809 tsec->sockcreate_sid = 0;
3815 * set the file creation context in a security record to the same as the
3816 * objective context of the specified inode
3818 static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3820 struct inode_security_struct *isec = inode_security(inode);
3821 struct task_security_struct *tsec = new->security;
3822 u32 sid = current_sid();
3825 ret = avc_has_perm(sid, isec->sid,
3826 SECCLASS_KERNEL_SERVICE,
3827 KERNEL_SERVICE__CREATE_FILES_AS,
3831 tsec->create_sid = isec->sid;
3835 static int selinux_kernel_module_request(char *kmod_name)
3838 struct common_audit_data ad;
3840 sid = task_sid(current);
3842 ad.type = LSM_AUDIT_DATA_KMOD;
3843 ad.u.kmod_name = kmod_name;
3845 return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3846 SYSTEM__MODULE_REQUEST, &ad);
3849 static int selinux_kernel_module_from_file(struct file *file)
3851 struct common_audit_data ad;
3852 struct inode_security_struct *isec;
3853 struct file_security_struct *fsec;
3854 u32 sid = current_sid();
3859 return avc_has_perm(sid, sid, SECCLASS_SYSTEM,
3860 SYSTEM__MODULE_LOAD, NULL);
3864 ad.type = LSM_AUDIT_DATA_FILE;
3867 fsec = file->f_security;
3868 if (sid != fsec->sid) {
3869 rc = avc_has_perm(sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
3874 isec = inode_security(file_inode(file));
3875 return avc_has_perm(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_task_setpgid(struct task_struct *p, pid_t pgid)
3897 return current_has_perm(p, PROCESS__SETPGID);
3900 static int selinux_task_getpgid(struct task_struct *p)
3902 return current_has_perm(p, PROCESS__GETPGID);
3905 static int selinux_task_getsid(struct task_struct *p)
3907 return current_has_perm(p, PROCESS__GETSESSION);
3910 static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3912 *secid = task_sid(p);
3915 static int selinux_task_setnice(struct task_struct *p, int nice)
3917 return current_has_perm(p, PROCESS__SETSCHED);
3920 static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3922 return current_has_perm(p, PROCESS__SETSCHED);
3925 static int selinux_task_getioprio(struct task_struct *p)
3927 return current_has_perm(p, PROCESS__GETSCHED);
3930 static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3931 struct rlimit *new_rlim)
3933 struct rlimit *old_rlim = p->signal->rlim + resource;
3935 /* Control the ability to change the hard limit (whether
3936 lowering or raising it), so that the hard limit can
3937 later be used as a safe reset point for the soft limit
3938 upon context transitions. See selinux_bprm_committing_creds. */
3939 if (old_rlim->rlim_max != new_rlim->rlim_max)
3940 return current_has_perm(p, PROCESS__SETRLIMIT);
3945 static int selinux_task_setscheduler(struct task_struct *p)
3947 return current_has_perm(p, PROCESS__SETSCHED);
3950 static int selinux_task_getscheduler(struct task_struct *p)
3952 return current_has_perm(p, PROCESS__GETSCHED);
3955 static int selinux_task_movememory(struct task_struct *p)
3957 return current_has_perm(p, PROCESS__SETSCHED);
3960 static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3967 perm = PROCESS__SIGNULL; /* null signal; existence test */
3969 perm = signal_to_av(sig);
3971 rc = avc_has_perm(secid, task_sid(p),
3972 SECCLASS_PROCESS, perm, NULL);
3974 rc = current_has_perm(p, perm);
3978 static void selinux_task_to_inode(struct task_struct *p,
3979 struct inode *inode)
3981 struct inode_security_struct *isec = inode->i_security;
3982 u32 sid = task_sid(p);
3984 spin_lock(&isec->lock);
3985 isec->sclass = inode_mode_to_security_class(inode->i_mode);
3987 isec->initialized = LABEL_INITIALIZED;
3988 spin_unlock(&isec->lock);
3991 /* Returns error only if unable to parse addresses */
3992 static int selinux_parse_skb_ipv4(struct sk_buff *skb,
3993 struct common_audit_data *ad, u8 *proto)
3995 int offset, ihlen, ret = -EINVAL;
3996 struct iphdr _iph, *ih;
3998 offset = skb_network_offset(skb);
3999 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4003 ihlen = ih->ihl * 4;
4004 if (ihlen < sizeof(_iph))
4007 ad->u.net->v4info.saddr = ih->saddr;
4008 ad->u.net->v4info.daddr = ih->daddr;
4012 *proto = ih->protocol;
4014 switch (ih->protocol) {
4016 struct tcphdr _tcph, *th;
4018 if (ntohs(ih->frag_off) & IP_OFFSET)
4022 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4026 ad->u.net->sport = th->source;
4027 ad->u.net->dport = th->dest;
4032 struct udphdr _udph, *uh;
4034 if (ntohs(ih->frag_off) & IP_OFFSET)
4038 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4042 ad->u.net->sport = uh->source;
4043 ad->u.net->dport = uh->dest;
4047 case IPPROTO_DCCP: {
4048 struct dccp_hdr _dccph, *dh;
4050 if (ntohs(ih->frag_off) & IP_OFFSET)
4054 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4058 ad->u.net->sport = dh->dccph_sport;
4059 ad->u.net->dport = dh->dccph_dport;
4070 #if IS_ENABLED(CONFIG_IPV6)
4072 /* Returns error only if unable to parse addresses */
4073 static int selinux_parse_skb_ipv6(struct sk_buff *skb,
4074 struct common_audit_data *ad, u8 *proto)
4077 int ret = -EINVAL, offset;
4078 struct ipv6hdr _ipv6h, *ip6;
4081 offset = skb_network_offset(skb);
4082 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4086 ad->u.net->v6info.saddr = ip6->saddr;
4087 ad->u.net->v6info.daddr = ip6->daddr;
4090 nexthdr = ip6->nexthdr;
4091 offset += sizeof(_ipv6h);
4092 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
4101 struct tcphdr _tcph, *th;
4103 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4107 ad->u.net->sport = th->source;
4108 ad->u.net->dport = th->dest;
4113 struct udphdr _udph, *uh;
4115 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4119 ad->u.net->sport = uh->source;
4120 ad->u.net->dport = uh->dest;
4124 case IPPROTO_DCCP: {
4125 struct dccp_hdr _dccph, *dh;
4127 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4131 ad->u.net->sport = dh->dccph_sport;
4132 ad->u.net->dport = dh->dccph_dport;
4136 /* includes fragments */
4146 static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
4147 char **_addrp, int src, u8 *proto)
4152 switch (ad->u.net->family) {
4154 ret = selinux_parse_skb_ipv4(skb, ad, proto);
4157 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4158 &ad->u.net->v4info.daddr);
4161 #if IS_ENABLED(CONFIG_IPV6)
4163 ret = selinux_parse_skb_ipv6(skb, ad, proto);
4166 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4167 &ad->u.net->v6info.daddr);
4177 "SELinux: failure in selinux_parse_skb(),"
4178 " unable to parse packet\n");
4188 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
4190 * @family: protocol family
4191 * @sid: the packet's peer label SID
4194 * Check the various different forms of network peer labeling and determine
4195 * the peer label/SID for the packet; most of the magic actually occurs in
4196 * the security server function security_net_peersid_cmp(). The function
4197 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4198 * or -EACCES if @sid is invalid due to inconsistencies with the different
4202 static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
4209 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
4212 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4216 err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
4217 if (unlikely(err)) {
4219 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4220 " unable to determine packet's peer label\n");
4228 * selinux_conn_sid - Determine the child socket label for a connection
4229 * @sk_sid: the parent socket's SID
4230 * @skb_sid: the packet's SID
4231 * @conn_sid: the resulting connection SID
4233 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4234 * combined with the MLS information from @skb_sid in order to create
4235 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4236 * of @sk_sid. Returns zero on success, negative values on failure.
4239 static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4243 if (skb_sid != SECSID_NULL)
4244 err = security_sid_mls_copy(sk_sid, skb_sid, conn_sid);
4251 /* socket security operations */
4253 static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4254 u16 secclass, u32 *socksid)
4256 if (tsec->sockcreate_sid > SECSID_NULL) {
4257 *socksid = tsec->sockcreate_sid;
4261 return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
4265 static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
4267 struct sk_security_struct *sksec = sk->sk_security;
4268 struct common_audit_data ad;
4269 struct lsm_network_audit net = {0,};
4270 u32 tsid = task_sid(task);
4272 if (sksec->sid == SECINITSID_KERNEL)
4275 ad.type = LSM_AUDIT_DATA_NET;
4279 return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
4282 static int selinux_socket_create(int family, int type,
4283 int protocol, int kern)
4285 const struct task_security_struct *tsec = current_security();
4293 secclass = socket_type_to_security_class(family, type, protocol);
4294 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4298 return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
4301 static int selinux_socket_post_create(struct socket *sock, int family,
4302 int type, int protocol, int kern)
4304 const struct task_security_struct *tsec = current_security();
4305 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
4306 struct sk_security_struct *sksec;
4307 u16 sclass = socket_type_to_security_class(family, type, protocol);
4308 u32 sid = SECINITSID_KERNEL;
4312 err = socket_sockcreate_sid(tsec, sclass, &sid);
4317 isec->sclass = sclass;
4319 isec->initialized = LABEL_INITIALIZED;
4322 sksec = sock->sk->sk_security;
4323 sksec->sclass = sclass;
4325 err = selinux_netlbl_socket_post_create(sock->sk, family);
4331 /* Range of port numbers used to automatically bind.
4332 Need to determine whether we should perform a name_bind
4333 permission check between the socket and the port number. */
4335 static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4337 struct sock *sk = sock->sk;
4341 err = sock_has_perm(current, sk, SOCKET__BIND);
4346 * If PF_INET or PF_INET6, check name_bind permission for the port.
4347 * Multiple address binding for SCTP is not supported yet: we just
4348 * check the first address now.
4350 family = sk->sk_family;
4351 if (family == PF_INET || family == PF_INET6) {
4353 struct sk_security_struct *sksec = sk->sk_security;
4354 struct common_audit_data ad;
4355 struct lsm_network_audit net = {0,};
4356 struct sockaddr_in *addr4 = NULL;
4357 struct sockaddr_in6 *addr6 = NULL;
4358 unsigned short snum;
4361 if (family == PF_INET) {
4362 if (addrlen < sizeof(struct sockaddr_in)) {
4366 addr4 = (struct sockaddr_in *)address;
4367 snum = ntohs(addr4->sin_port);
4368 addrp = (char *)&addr4->sin_addr.s_addr;
4370 if (addrlen < SIN6_LEN_RFC2133) {
4374 addr6 = (struct sockaddr_in6 *)address;
4375 snum = ntohs(addr6->sin6_port);
4376 addrp = (char *)&addr6->sin6_addr.s6_addr;
4382 inet_get_local_port_range(sock_net(sk), &low, &high);
4384 if (snum < max(PROT_SOCK, low) || snum > high) {
4385 err = sel_netport_sid(sk->sk_protocol,
4389 ad.type = LSM_AUDIT_DATA_NET;
4391 ad.u.net->sport = htons(snum);
4392 ad.u.net->family = family;
4393 err = avc_has_perm(sksec->sid, sid,
4395 SOCKET__NAME_BIND, &ad);
4401 switch (sksec->sclass) {
4402 case SECCLASS_TCP_SOCKET:
4403 node_perm = TCP_SOCKET__NODE_BIND;
4406 case SECCLASS_UDP_SOCKET:
4407 node_perm = UDP_SOCKET__NODE_BIND;
4410 case SECCLASS_DCCP_SOCKET:
4411 node_perm = DCCP_SOCKET__NODE_BIND;
4415 node_perm = RAWIP_SOCKET__NODE_BIND;
4419 err = sel_netnode_sid(addrp, family, &sid);
4423 ad.type = LSM_AUDIT_DATA_NET;
4425 ad.u.net->sport = htons(snum);
4426 ad.u.net->family = family;
4428 if (family == PF_INET)
4429 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
4431 ad.u.net->v6info.saddr = addr6->sin6_addr;
4433 err = avc_has_perm(sksec->sid, sid,
4434 sksec->sclass, node_perm, &ad);
4442 static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
4444 struct sock *sk = sock->sk;
4445 struct sk_security_struct *sksec = sk->sk_security;
4448 err = sock_has_perm(current, sk, SOCKET__CONNECT);
4453 * If a TCP or DCCP socket, check name_connect permission for the port.
4455 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4456 sksec->sclass == SECCLASS_DCCP_SOCKET) {
4457 struct common_audit_data ad;
4458 struct lsm_network_audit net = {0,};
4459 struct sockaddr_in *addr4 = NULL;
4460 struct sockaddr_in6 *addr6 = NULL;
4461 unsigned short snum;
4464 if (sk->sk_family == PF_INET) {
4465 addr4 = (struct sockaddr_in *)address;
4466 if (addrlen < sizeof(struct sockaddr_in))
4468 snum = ntohs(addr4->sin_port);
4470 addr6 = (struct sockaddr_in6 *)address;
4471 if (addrlen < SIN6_LEN_RFC2133)
4473 snum = ntohs(addr6->sin6_port);
4476 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
4480 perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
4481 TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
4483 ad.type = LSM_AUDIT_DATA_NET;
4485 ad.u.net->dport = htons(snum);
4486 ad.u.net->family = sk->sk_family;
4487 err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
4492 err = selinux_netlbl_socket_connect(sk, address);
4498 static int selinux_socket_listen(struct socket *sock, int backlog)
4500 return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
4503 static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4506 struct inode_security_struct *isec;
4507 struct inode_security_struct *newisec;
4511 err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
4515 isec = inode_security_novalidate(SOCK_INODE(sock));
4516 spin_lock(&isec->lock);
4517 sclass = isec->sclass;
4519 spin_unlock(&isec->lock);
4521 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4522 newisec->sclass = sclass;
4524 newisec->initialized = LABEL_INITIALIZED;
4529 static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
4532 return sock_has_perm(current, sock->sk, SOCKET__WRITE);
4535 static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4536 int size, int flags)
4538 return sock_has_perm(current, sock->sk, SOCKET__READ);
4541 static int selinux_socket_getsockname(struct socket *sock)
4543 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
4546 static int selinux_socket_getpeername(struct socket *sock)
4548 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
4551 static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
4555 err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
4559 return selinux_netlbl_socket_setsockopt(sock, level, optname);
4562 static int selinux_socket_getsockopt(struct socket *sock, int level,
4565 return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
4568 static int selinux_socket_shutdown(struct socket *sock, int how)
4570 return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
4573 static int selinux_socket_unix_stream_connect(struct sock *sock,
4577 struct sk_security_struct *sksec_sock = sock->sk_security;
4578 struct sk_security_struct *sksec_other = other->sk_security;
4579 struct sk_security_struct *sksec_new = newsk->sk_security;
4580 struct common_audit_data ad;
4581 struct lsm_network_audit net = {0,};
4584 ad.type = LSM_AUDIT_DATA_NET;
4586 ad.u.net->sk = other;
4588 err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
4589 sksec_other->sclass,
4590 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4594 /* server child socket */
4595 sksec_new->peer_sid = sksec_sock->sid;
4596 err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
4601 /* connecting socket */
4602 sksec_sock->peer_sid = sksec_new->sid;
4607 static int selinux_socket_unix_may_send(struct socket *sock,
4608 struct socket *other)
4610 struct sk_security_struct *ssec = sock->sk->sk_security;
4611 struct sk_security_struct *osec = other->sk->sk_security;
4612 struct common_audit_data ad;
4613 struct lsm_network_audit net = {0,};
4615 ad.type = LSM_AUDIT_DATA_NET;
4617 ad.u.net->sk = other->sk;
4619 return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4623 static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4624 char *addrp, u16 family, u32 peer_sid,
4625 struct common_audit_data *ad)
4631 err = sel_netif_sid(ns, ifindex, &if_sid);
4634 err = avc_has_perm(peer_sid, if_sid,
4635 SECCLASS_NETIF, NETIF__INGRESS, ad);
4639 err = sel_netnode_sid(addrp, family, &node_sid);
4642 return avc_has_perm(peer_sid, node_sid,
4643 SECCLASS_NODE, NODE__RECVFROM, ad);
4646 static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
4650 struct sk_security_struct *sksec = sk->sk_security;
4651 u32 sk_sid = sksec->sid;
4652 struct common_audit_data ad;
4653 struct lsm_network_audit net = {0,};
4656 ad.type = LSM_AUDIT_DATA_NET;
4658 ad.u.net->netif = skb->skb_iif;
4659 ad.u.net->family = family;
4660 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4664 if (selinux_secmark_enabled()) {
4665 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4671 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4674 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
4679 static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4682 struct sk_security_struct *sksec = sk->sk_security;
4683 u16 family = sk->sk_family;
4684 u32 sk_sid = sksec->sid;
4685 struct common_audit_data ad;
4686 struct lsm_network_audit net = {0,};
4691 if (family != PF_INET && family != PF_INET6)
4694 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
4695 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4698 /* If any sort of compatibility mode is enabled then handoff processing
4699 * to the selinux_sock_rcv_skb_compat() function to deal with the
4700 * special handling. We do this in an attempt to keep this function
4701 * as fast and as clean as possible. */
4702 if (!selinux_policycap_netpeer)
4703 return selinux_sock_rcv_skb_compat(sk, skb, family);
4705 secmark_active = selinux_secmark_enabled();
4706 peerlbl_active = selinux_peerlbl_enabled();
4707 if (!secmark_active && !peerlbl_active)
4710 ad.type = LSM_AUDIT_DATA_NET;
4712 ad.u.net->netif = skb->skb_iif;
4713 ad.u.net->family = family;
4714 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4718 if (peerlbl_active) {
4721 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4724 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4725 addrp, family, peer_sid, &ad);
4727 selinux_netlbl_err(skb, family, err, 0);
4730 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4733 selinux_netlbl_err(skb, family, err, 0);
4738 if (secmark_active) {
4739 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4748 static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4749 int __user *optlen, unsigned len)
4754 struct sk_security_struct *sksec = sock->sk->sk_security;
4755 u32 peer_sid = SECSID_NULL;
4757 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4758 sksec->sclass == SECCLASS_TCP_SOCKET)
4759 peer_sid = sksec->peer_sid;
4760 if (peer_sid == SECSID_NULL)
4761 return -ENOPROTOOPT;
4763 err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
4767 if (scontext_len > len) {
4772 if (copy_to_user(optval, scontext, scontext_len))
4776 if (put_user(scontext_len, optlen))
4782 static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
4784 u32 peer_secid = SECSID_NULL;
4786 struct inode_security_struct *isec;
4788 if (skb && skb->protocol == htons(ETH_P_IP))
4790 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4793 family = sock->sk->sk_family;
4797 if (sock && family == PF_UNIX) {
4798 isec = inode_security_novalidate(SOCK_INODE(sock));
4799 peer_secid = isec->sid;
4801 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
4804 *secid = peer_secid;
4805 if (peer_secid == SECSID_NULL)
4810 static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
4812 struct sk_security_struct *sksec;
4814 sksec = kzalloc(sizeof(*sksec), priority);
4818 sksec->peer_sid = SECINITSID_UNLABELED;
4819 sksec->sid = SECINITSID_UNLABELED;
4820 sksec->sclass = SECCLASS_SOCKET;
4821 selinux_netlbl_sk_security_reset(sksec);
4822 sk->sk_security = sksec;
4827 static void selinux_sk_free_security(struct sock *sk)
4829 struct sk_security_struct *sksec = sk->sk_security;
4831 sk->sk_security = NULL;
4832 selinux_netlbl_sk_security_free(sksec);
4836 static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4838 struct sk_security_struct *sksec = sk->sk_security;
4839 struct sk_security_struct *newsksec = newsk->sk_security;
4841 newsksec->sid = sksec->sid;
4842 newsksec->peer_sid = sksec->peer_sid;
4843 newsksec->sclass = sksec->sclass;
4845 selinux_netlbl_sk_security_reset(newsksec);
4848 static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
4851 *secid = SECINITSID_ANY_SOCKET;
4853 struct sk_security_struct *sksec = sk->sk_security;
4855 *secid = sksec->sid;
4859 static void selinux_sock_graft(struct sock *sk, struct socket *parent)
4861 struct inode_security_struct *isec =
4862 inode_security_novalidate(SOCK_INODE(parent));
4863 struct sk_security_struct *sksec = sk->sk_security;
4865 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
4866 sk->sk_family == PF_UNIX)
4867 isec->sid = sksec->sid;
4868 sksec->sclass = isec->sclass;
4871 static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4872 struct request_sock *req)
4874 struct sk_security_struct *sksec = sk->sk_security;
4876 u16 family = req->rsk_ops->family;
4880 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
4883 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
4886 req->secid = connsid;
4887 req->peer_secid = peersid;
4889 return selinux_netlbl_inet_conn_request(req, family);
4892 static void selinux_inet_csk_clone(struct sock *newsk,
4893 const struct request_sock *req)
4895 struct sk_security_struct *newsksec = newsk->sk_security;
4897 newsksec->sid = req->secid;
4898 newsksec->peer_sid = req->peer_secid;
4899 /* NOTE: Ideally, we should also get the isec->sid for the
4900 new socket in sync, but we don't have the isec available yet.
4901 So we will wait until sock_graft to do it, by which
4902 time it will have been created and available. */
4904 /* We don't need to take any sort of lock here as we are the only
4905 * thread with access to newsksec */
4906 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
4909 static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
4911 u16 family = sk->sk_family;
4912 struct sk_security_struct *sksec = sk->sk_security;
4914 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4915 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4918 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
4921 static int selinux_secmark_relabel_packet(u32 sid)
4923 const struct task_security_struct *__tsec;
4926 __tsec = current_security();
4929 return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
4932 static void selinux_secmark_refcount_inc(void)
4934 atomic_inc(&selinux_secmark_refcount);
4937 static void selinux_secmark_refcount_dec(void)
4939 atomic_dec(&selinux_secmark_refcount);
4942 static void selinux_req_classify_flow(const struct request_sock *req,
4945 fl->flowi_secid = req->secid;
4948 static int selinux_tun_dev_alloc_security(void **security)
4950 struct tun_security_struct *tunsec;
4952 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
4955 tunsec->sid = current_sid();
4961 static void selinux_tun_dev_free_security(void *security)
4966 static int selinux_tun_dev_create(void)
4968 u32 sid = current_sid();
4970 /* we aren't taking into account the "sockcreate" SID since the socket
4971 * that is being created here is not a socket in the traditional sense,
4972 * instead it is a private sock, accessible only to the kernel, and
4973 * representing a wide range of network traffic spanning multiple
4974 * connections unlike traditional sockets - check the TUN driver to
4975 * get a better understanding of why this socket is special */
4977 return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4981 static int selinux_tun_dev_attach_queue(void *security)
4983 struct tun_security_struct *tunsec = security;
4985 return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
4986 TUN_SOCKET__ATTACH_QUEUE, NULL);
4989 static int selinux_tun_dev_attach(struct sock *sk, void *security)
4991 struct tun_security_struct *tunsec = security;
4992 struct sk_security_struct *sksec = sk->sk_security;
4994 /* we don't currently perform any NetLabel based labeling here and it
4995 * isn't clear that we would want to do so anyway; while we could apply
4996 * labeling without the support of the TUN user the resulting labeled
4997 * traffic from the other end of the connection would almost certainly
4998 * cause confusion to the TUN user that had no idea network labeling
4999 * protocols were being used */
5001 sksec->sid = tunsec->sid;
5002 sksec->sclass = SECCLASS_TUN_SOCKET;
5007 static int selinux_tun_dev_open(void *security)
5009 struct tun_security_struct *tunsec = security;
5010 u32 sid = current_sid();
5013 err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
5014 TUN_SOCKET__RELABELFROM, NULL);
5017 err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
5018 TUN_SOCKET__RELABELTO, NULL);
5026 static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5029 unsigned int msg_len;
5030 unsigned int data_len = skb->len;
5031 unsigned char *data = skb->data;
5032 struct nlmsghdr *nlh;
5033 struct sk_security_struct *sksec = sk->sk_security;
5034 u16 sclass = sksec->sclass;
5037 while (data_len >= nlmsg_total_size(0)) {
5038 nlh = (struct nlmsghdr *)data;
5040 /* NOTE: the nlmsg_len field isn't reliably set by some netlink
5041 * users which means we can't reject skb's with bogus
5042 * length fields; our solution is to follow what
5043 * netlink_rcv_skb() does and simply skip processing at
5044 * messages with length fields that are clearly junk
5046 if (nlh->nlmsg_len < NLMSG_HDRLEN || nlh->nlmsg_len > data_len)
5049 rc = selinux_nlmsg_lookup(sclass, nlh->nlmsg_type, &perm);
5051 rc = sock_has_perm(current, sk, perm);
5054 } else if (rc == -EINVAL) {
5055 /* -EINVAL is a missing msg/perm mapping */
5056 pr_warn_ratelimited("SELinux: unrecognized netlink"
5057 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5058 " pid=%d comm=%s\n",
5059 sk->sk_protocol, nlh->nlmsg_type,
5060 secclass_map[sclass - 1].name,
5061 task_pid_nr(current), current->comm);
5062 if (selinux_enforcing && !security_get_allow_unknown())
5065 } else if (rc == -ENOENT) {
5066 /* -ENOENT is a missing socket/class mapping, ignore */
5072 /* move to the next message after applying netlink padding */
5073 msg_len = NLMSG_ALIGN(nlh->nlmsg_len);
5074 if (msg_len >= data_len)
5076 data_len -= msg_len;
5083 #ifdef CONFIG_NETFILTER
5085 static unsigned int selinux_ip_forward(struct sk_buff *skb,
5086 const struct net_device *indev,
5092 struct common_audit_data ad;
5093 struct lsm_network_audit net = {0,};
5098 if (!selinux_policycap_netpeer)
5101 secmark_active = selinux_secmark_enabled();
5102 netlbl_active = netlbl_enabled();
5103 peerlbl_active = selinux_peerlbl_enabled();
5104 if (!secmark_active && !peerlbl_active)
5107 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5110 ad.type = LSM_AUDIT_DATA_NET;
5112 ad.u.net->netif = indev->ifindex;
5113 ad.u.net->family = family;
5114 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5117 if (peerlbl_active) {
5118 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5119 addrp, family, peer_sid, &ad);
5121 selinux_netlbl_err(skb, family, err, 1);
5127 if (avc_has_perm(peer_sid, skb->secmark,
5128 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5132 /* we do this in the FORWARD path and not the POST_ROUTING
5133 * path because we want to make sure we apply the necessary
5134 * labeling before IPsec is applied so we can leverage AH
5136 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5142 static unsigned int selinux_ipv4_forward(void *priv,
5143 struct sk_buff *skb,
5144 const struct nf_hook_state *state)
5146 return selinux_ip_forward(skb, state->in, PF_INET);
5149 #if IS_ENABLED(CONFIG_IPV6)
5150 static unsigned int selinux_ipv6_forward(void *priv,
5151 struct sk_buff *skb,
5152 const struct nf_hook_state *state)
5154 return selinux_ip_forward(skb, state->in, PF_INET6);
5158 static unsigned int selinux_ip_output(struct sk_buff *skb,
5164 if (!netlbl_enabled())
5167 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5168 * because we want to make sure we apply the necessary labeling
5169 * before IPsec is applied so we can leverage AH protection */
5172 struct sk_security_struct *sksec;
5174 if (sk_listener(sk))
5175 /* if the socket is the listening state then this
5176 * packet is a SYN-ACK packet which means it needs to
5177 * be labeled based on the connection/request_sock and
5178 * not the parent socket. unfortunately, we can't
5179 * lookup the request_sock yet as it isn't queued on
5180 * the parent socket until after the SYN-ACK is sent.
5181 * the "solution" is to simply pass the packet as-is
5182 * as any IP option based labeling should be copied
5183 * from the initial connection request (in the IP
5184 * layer). it is far from ideal, but until we get a
5185 * security label in the packet itself this is the
5186 * best we can do. */
5189 /* standard practice, label using the parent socket */
5190 sksec = sk->sk_security;
5193 sid = SECINITSID_KERNEL;
5194 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5200 static unsigned int selinux_ipv4_output(void *priv,
5201 struct sk_buff *skb,
5202 const struct nf_hook_state *state)
5204 return selinux_ip_output(skb, PF_INET);
5207 #if IS_ENABLED(CONFIG_IPV6)
5208 static unsigned int selinux_ipv6_output(void *priv,
5209 struct sk_buff *skb,
5210 const struct nf_hook_state *state)
5212 return selinux_ip_output(skb, PF_INET6);
5216 static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5220 struct sock *sk = skb_to_full_sk(skb);
5221 struct sk_security_struct *sksec;
5222 struct common_audit_data ad;
5223 struct lsm_network_audit net = {0,};
5229 sksec = sk->sk_security;
5231 ad.type = LSM_AUDIT_DATA_NET;
5233 ad.u.net->netif = ifindex;
5234 ad.u.net->family = family;
5235 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5238 if (selinux_secmark_enabled())
5239 if (avc_has_perm(sksec->sid, skb->secmark,
5240 SECCLASS_PACKET, PACKET__SEND, &ad))
5241 return NF_DROP_ERR(-ECONNREFUSED);
5243 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5244 return NF_DROP_ERR(-ECONNREFUSED);
5249 static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5250 const struct net_device *outdev,
5255 int ifindex = outdev->ifindex;
5257 struct common_audit_data ad;
5258 struct lsm_network_audit net = {0,};
5263 /* If any sort of compatibility mode is enabled then handoff processing
5264 * to the selinux_ip_postroute_compat() function to deal with the
5265 * special handling. We do this in an attempt to keep this function
5266 * as fast and as clean as possible. */
5267 if (!selinux_policycap_netpeer)
5268 return selinux_ip_postroute_compat(skb, ifindex, family);
5270 secmark_active = selinux_secmark_enabled();
5271 peerlbl_active = selinux_peerlbl_enabled();
5272 if (!secmark_active && !peerlbl_active)
5275 sk = skb_to_full_sk(skb);
5278 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5279 * packet transformation so allow the packet to pass without any checks
5280 * since we'll have another chance to perform access control checks
5281 * when the packet is on it's final way out.
5282 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
5283 * is NULL, in this case go ahead and apply access control.
5284 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5285 * TCP listening state we cannot wait until the XFRM processing
5286 * is done as we will miss out on the SA label if we do;
5287 * unfortunately, this means more work, but it is only once per
5289 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
5290 !(sk && sk_listener(sk)))
5295 /* Without an associated socket the packet is either coming
5296 * from the kernel or it is being forwarded; check the packet
5297 * to determine which and if the packet is being forwarded
5298 * query the packet directly to determine the security label. */
5300 secmark_perm = PACKET__FORWARD_OUT;
5301 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
5304 secmark_perm = PACKET__SEND;
5305 peer_sid = SECINITSID_KERNEL;
5307 } else if (sk_listener(sk)) {
5308 /* Locally generated packet but the associated socket is in the
5309 * listening state which means this is a SYN-ACK packet. In
5310 * this particular case the correct security label is assigned
5311 * to the connection/request_sock but unfortunately we can't
5312 * query the request_sock as it isn't queued on the parent
5313 * socket until after the SYN-ACK packet is sent; the only
5314 * viable choice is to regenerate the label like we do in
5315 * selinux_inet_conn_request(). See also selinux_ip_output()
5316 * for similar problems. */
5318 struct sk_security_struct *sksec;
5320 sksec = sk->sk_security;
5321 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5323 /* At this point, if the returned skb peerlbl is SECSID_NULL
5324 * and the packet has been through at least one XFRM
5325 * transformation then we must be dealing with the "final"
5326 * form of labeled IPsec packet; since we've already applied
5327 * all of our access controls on this packet we can safely
5328 * pass the packet. */
5329 if (skb_sid == SECSID_NULL) {
5332 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5336 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5340 return NF_DROP_ERR(-ECONNREFUSED);
5343 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5345 secmark_perm = PACKET__SEND;
5347 /* Locally generated packet, fetch the security label from the
5348 * associated socket. */
5349 struct sk_security_struct *sksec = sk->sk_security;
5350 peer_sid = sksec->sid;
5351 secmark_perm = PACKET__SEND;
5354 ad.type = LSM_AUDIT_DATA_NET;
5356 ad.u.net->netif = ifindex;
5357 ad.u.net->family = family;
5358 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
5362 if (avc_has_perm(peer_sid, skb->secmark,
5363 SECCLASS_PACKET, secmark_perm, &ad))
5364 return NF_DROP_ERR(-ECONNREFUSED);
5366 if (peerlbl_active) {
5370 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
5372 if (avc_has_perm(peer_sid, if_sid,
5373 SECCLASS_NETIF, NETIF__EGRESS, &ad))
5374 return NF_DROP_ERR(-ECONNREFUSED);
5376 if (sel_netnode_sid(addrp, family, &node_sid))
5378 if (avc_has_perm(peer_sid, node_sid,
5379 SECCLASS_NODE, NODE__SENDTO, &ad))
5380 return NF_DROP_ERR(-ECONNREFUSED);
5386 static unsigned int selinux_ipv4_postroute(void *priv,
5387 struct sk_buff *skb,
5388 const struct nf_hook_state *state)
5390 return selinux_ip_postroute(skb, state->out, PF_INET);
5393 #if IS_ENABLED(CONFIG_IPV6)
5394 static unsigned int selinux_ipv6_postroute(void *priv,
5395 struct sk_buff *skb,
5396 const struct nf_hook_state *state)
5398 return selinux_ip_postroute(skb, state->out, PF_INET6);
5402 #endif /* CONFIG_NETFILTER */
5404 static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5406 return selinux_nlmsg_perm(sk, skb);
5409 static int ipc_alloc_security(struct task_struct *task,
5410 struct kern_ipc_perm *perm,
5413 struct ipc_security_struct *isec;
5416 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
5420 sid = task_sid(task);
5421 isec->sclass = sclass;
5423 perm->security = isec;
5428 static void ipc_free_security(struct kern_ipc_perm *perm)
5430 struct ipc_security_struct *isec = perm->security;
5431 perm->security = NULL;
5435 static int msg_msg_alloc_security(struct msg_msg *msg)
5437 struct msg_security_struct *msec;
5439 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
5443 msec->sid = SECINITSID_UNLABELED;
5444 msg->security = msec;
5449 static void msg_msg_free_security(struct msg_msg *msg)
5451 struct msg_security_struct *msec = msg->security;
5453 msg->security = NULL;
5457 static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
5460 struct ipc_security_struct *isec;
5461 struct common_audit_data ad;
5462 u32 sid = current_sid();
5464 isec = ipc_perms->security;
5466 ad.type = LSM_AUDIT_DATA_IPC;
5467 ad.u.ipc_id = ipc_perms->key;
5469 return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
5472 static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5474 return msg_msg_alloc_security(msg);
5477 static void selinux_msg_msg_free_security(struct msg_msg *msg)
5479 msg_msg_free_security(msg);
5482 /* message queue security operations */
5483 static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
5485 struct ipc_security_struct *isec;
5486 struct common_audit_data ad;
5487 u32 sid = current_sid();
5490 rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
5494 isec = msq->q_perm.security;
5496 ad.type = LSM_AUDIT_DATA_IPC;
5497 ad.u.ipc_id = msq->q_perm.key;
5499 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
5502 ipc_free_security(&msq->q_perm);
5508 static void selinux_msg_queue_free_security(struct msg_queue *msq)
5510 ipc_free_security(&msq->q_perm);
5513 static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
5515 struct ipc_security_struct *isec;
5516 struct common_audit_data ad;
5517 u32 sid = current_sid();
5519 isec = msq->q_perm.security;
5521 ad.type = LSM_AUDIT_DATA_IPC;
5522 ad.u.ipc_id = msq->q_perm.key;
5524 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
5525 MSGQ__ASSOCIATE, &ad);
5528 static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
5536 /* No specific object, just general system-wide information. */
5537 return task_has_system(current, SYSTEM__IPC_INFO);
5540 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5543 perms = MSGQ__SETATTR;
5546 perms = MSGQ__DESTROY;
5552 err = ipc_has_perm(&msq->q_perm, perms);
5556 static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
5558 struct ipc_security_struct *isec;
5559 struct msg_security_struct *msec;
5560 struct common_audit_data ad;
5561 u32 sid = current_sid();
5564 isec = msq->q_perm.security;
5565 msec = msg->security;
5568 * First time through, need to assign label to the message
5570 if (msec->sid == SECINITSID_UNLABELED) {
5572 * Compute new sid based on current process and
5573 * message queue this message will be stored in
5575 rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
5581 ad.type = LSM_AUDIT_DATA_IPC;
5582 ad.u.ipc_id = msq->q_perm.key;
5584 /* Can this process write to the queue? */
5585 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
5588 /* Can this process send the message */
5589 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
5592 /* Can the message be put in the queue? */
5593 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5594 MSGQ__ENQUEUE, &ad);
5599 static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
5600 struct task_struct *target,
5601 long type, int mode)
5603 struct ipc_security_struct *isec;
5604 struct msg_security_struct *msec;
5605 struct common_audit_data ad;
5606 u32 sid = task_sid(target);
5609 isec = msq->q_perm.security;
5610 msec = msg->security;
5612 ad.type = LSM_AUDIT_DATA_IPC;
5613 ad.u.ipc_id = msq->q_perm.key;
5615 rc = avc_has_perm(sid, isec->sid,
5616 SECCLASS_MSGQ, MSGQ__READ, &ad);
5618 rc = avc_has_perm(sid, msec->sid,
5619 SECCLASS_MSG, MSG__RECEIVE, &ad);
5623 /* Shared Memory security operations */
5624 static int selinux_shm_alloc_security(struct shmid_kernel *shp)
5626 struct ipc_security_struct *isec;
5627 struct common_audit_data ad;
5628 u32 sid = current_sid();
5631 rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
5635 isec = shp->shm_perm.security;
5637 ad.type = LSM_AUDIT_DATA_IPC;
5638 ad.u.ipc_id = shp->shm_perm.key;
5640 rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
5643 ipc_free_security(&shp->shm_perm);
5649 static void selinux_shm_free_security(struct shmid_kernel *shp)
5651 ipc_free_security(&shp->shm_perm);
5654 static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
5656 struct ipc_security_struct *isec;
5657 struct common_audit_data ad;
5658 u32 sid = current_sid();
5660 isec = shp->shm_perm.security;
5662 ad.type = LSM_AUDIT_DATA_IPC;
5663 ad.u.ipc_id = shp->shm_perm.key;
5665 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
5666 SHM__ASSOCIATE, &ad);
5669 /* Note, at this point, shp is locked down */
5670 static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
5678 /* No specific object, just general system-wide information. */
5679 return task_has_system(current, SYSTEM__IPC_INFO);
5682 perms = SHM__GETATTR | SHM__ASSOCIATE;
5685 perms = SHM__SETATTR;
5692 perms = SHM__DESTROY;
5698 err = ipc_has_perm(&shp->shm_perm, perms);
5702 static int selinux_shm_shmat(struct shmid_kernel *shp,
5703 char __user *shmaddr, int shmflg)
5707 if (shmflg & SHM_RDONLY)
5710 perms = SHM__READ | SHM__WRITE;
5712 return ipc_has_perm(&shp->shm_perm, perms);
5715 /* Semaphore security operations */
5716 static int selinux_sem_alloc_security(struct sem_array *sma)
5718 struct ipc_security_struct *isec;
5719 struct common_audit_data ad;
5720 u32 sid = current_sid();
5723 rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
5727 isec = sma->sem_perm.security;
5729 ad.type = LSM_AUDIT_DATA_IPC;
5730 ad.u.ipc_id = sma->sem_perm.key;
5732 rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
5735 ipc_free_security(&sma->sem_perm);
5741 static void selinux_sem_free_security(struct sem_array *sma)
5743 ipc_free_security(&sma->sem_perm);
5746 static int selinux_sem_associate(struct sem_array *sma, int semflg)
5748 struct ipc_security_struct *isec;
5749 struct common_audit_data ad;
5750 u32 sid = current_sid();
5752 isec = sma->sem_perm.security;
5754 ad.type = LSM_AUDIT_DATA_IPC;
5755 ad.u.ipc_id = sma->sem_perm.key;
5757 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
5758 SEM__ASSOCIATE, &ad);
5761 /* Note, at this point, sma is locked down */
5762 static int selinux_sem_semctl(struct sem_array *sma, int cmd)
5770 /* No specific object, just general system-wide information. */
5771 return task_has_system(current, SYSTEM__IPC_INFO);
5775 perms = SEM__GETATTR;
5786 perms = SEM__DESTROY;
5789 perms = SEM__SETATTR;
5793 perms = SEM__GETATTR | SEM__ASSOCIATE;
5799 err = ipc_has_perm(&sma->sem_perm, perms);
5803 static int selinux_sem_semop(struct sem_array *sma,
5804 struct sembuf *sops, unsigned nsops, int alter)
5809 perms = SEM__READ | SEM__WRITE;
5813 return ipc_has_perm(&sma->sem_perm, perms);
5816 static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
5822 av |= IPC__UNIX_READ;
5824 av |= IPC__UNIX_WRITE;
5829 return ipc_has_perm(ipcp, av);
5832 static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5834 struct ipc_security_struct *isec = ipcp->security;
5838 static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
5841 inode_doinit_with_dentry(inode, dentry);
5844 static int selinux_getprocattr(struct task_struct *p,
5845 char *name, char **value)
5847 const struct task_security_struct *__tsec;
5853 error = current_has_perm(p, PROCESS__GETATTR);
5859 __tsec = __task_cred(p)->security;
5861 if (!strcmp(name, "current"))
5863 else if (!strcmp(name, "prev"))
5865 else if (!strcmp(name, "exec"))
5866 sid = __tsec->exec_sid;
5867 else if (!strcmp(name, "fscreate"))
5868 sid = __tsec->create_sid;
5869 else if (!strcmp(name, "keycreate"))
5870 sid = __tsec->keycreate_sid;
5871 else if (!strcmp(name, "sockcreate"))
5872 sid = __tsec->sockcreate_sid;
5880 error = security_sid_to_context(sid, value, &len);
5890 static int selinux_setprocattr(struct task_struct *p,
5891 char *name, void *value, size_t size)
5893 struct task_security_struct *tsec;
5900 /* SELinux only allows a process to change its own
5901 security attributes. */
5906 * Basic control over ability to set these attributes at all.
5907 * current == p, but we'll pass them separately in case the
5908 * above restriction is ever removed.
5910 if (!strcmp(name, "exec"))
5911 error = current_has_perm(p, PROCESS__SETEXEC);
5912 else if (!strcmp(name, "fscreate"))
5913 error = current_has_perm(p, PROCESS__SETFSCREATE);
5914 else if (!strcmp(name, "keycreate"))
5915 error = current_has_perm(p, PROCESS__SETKEYCREATE);
5916 else if (!strcmp(name, "sockcreate"))
5917 error = current_has_perm(p, PROCESS__SETSOCKCREATE);
5918 else if (!strcmp(name, "current"))
5919 error = current_has_perm(p, PROCESS__SETCURRENT);
5925 /* Obtain a SID for the context, if one was specified. */
5926 if (size && str[0] && str[0] != '\n') {
5927 if (str[size-1] == '\n') {
5931 error = security_context_to_sid(value, size, &sid, GFP_KERNEL);
5932 if (error == -EINVAL && !strcmp(name, "fscreate")) {
5933 if (!capable(CAP_MAC_ADMIN)) {
5934 struct audit_buffer *ab;
5937 /* We strip a nul only if it is at the end, otherwise the
5938 * context contains a nul and we should audit that */
5939 if (str[size - 1] == '\0')
5940 audit_size = size - 1;
5943 ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
5944 audit_log_format(ab, "op=fscreate invalid_context=");
5945 audit_log_n_untrustedstring(ab, value, audit_size);
5950 error = security_context_to_sid_force(value, size,
5957 new = prepare_creds();
5961 /* Permission checking based on the specified context is
5962 performed during the actual operation (execve,
5963 open/mkdir/...), when we know the full context of the
5964 operation. See selinux_bprm_set_creds for the execve
5965 checks and may_create for the file creation checks. The
5966 operation will then fail if the context is not permitted. */
5967 tsec = new->security;
5968 if (!strcmp(name, "exec")) {
5969 tsec->exec_sid = sid;
5970 } else if (!strcmp(name, "fscreate")) {
5971 tsec->create_sid = sid;
5972 } else if (!strcmp(name, "keycreate")) {
5973 error = may_create_key(sid, p);
5976 tsec->keycreate_sid = sid;
5977 } else if (!strcmp(name, "sockcreate")) {
5978 tsec->sockcreate_sid = sid;
5979 } else if (!strcmp(name, "current")) {
5984 /* Only allow single threaded processes to change context */
5986 if (!current_is_single_threaded()) {
5987 error = security_bounded_transition(tsec->sid, sid);
5992 /* Check permissions for the transition. */
5993 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
5994 PROCESS__DYNTRANSITION, NULL);
5998 /* Check for ptracing, and update the task SID if ok.
5999 Otherwise, leave SID unchanged and fail. */
6000 ptsid = ptrace_parent_sid(p);
6002 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
6003 PROCESS__PTRACE, NULL);
6022 static int selinux_ismaclabel(const char *name)
6024 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6027 static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6029 return security_sid_to_context(secid, secdata, seclen);
6032 static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
6034 return security_context_to_sid(secdata, seclen, secid, GFP_KERNEL);
6037 static void selinux_release_secctx(char *secdata, u32 seclen)
6042 static void selinux_inode_invalidate_secctx(struct inode *inode)
6044 struct inode_security_struct *isec = inode->i_security;
6046 spin_lock(&isec->lock);
6047 isec->initialized = LABEL_INVALID;
6048 spin_unlock(&isec->lock);
6052 * called with inode->i_mutex locked
6054 static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6056 int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
6058 /* Do not return error when suppressing label (SBLABEL_MNT not set). */
6059 return rc == -EOPNOTSUPP ? 0 : rc;
6063 * called with inode->i_mutex locked
6065 static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6067 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6070 static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6073 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6082 static int selinux_key_alloc(struct key *k, const struct cred *cred,
6083 unsigned long flags)
6085 const struct task_security_struct *tsec;
6086 struct key_security_struct *ksec;
6088 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6092 tsec = cred->security;
6093 if (tsec->keycreate_sid)
6094 ksec->sid = tsec->keycreate_sid;
6096 ksec->sid = tsec->sid;
6102 static void selinux_key_free(struct key *k)
6104 struct key_security_struct *ksec = k->security;
6110 static int selinux_key_permission(key_ref_t key_ref,
6111 const struct cred *cred,
6115 struct key_security_struct *ksec;
6118 /* if no specific permissions are requested, we skip the
6119 permission check. No serious, additional covert channels
6120 appear to be created. */
6124 sid = cred_sid(cred);
6126 key = key_ref_to_ptr(key_ref);
6127 ksec = key->security;
6129 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
6132 static int selinux_key_getsecurity(struct key *key, char **_buffer)
6134 struct key_security_struct *ksec = key->security;
6135 char *context = NULL;
6139 rc = security_sid_to_context(ksec->sid, &context, &len);
6148 static struct security_hook_list selinux_hooks[] = {
6149 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6150 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6151 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6152 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
6154 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6155 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6156 LSM_HOOK_INIT(capget, selinux_capget),
6157 LSM_HOOK_INIT(capset, selinux_capset),
6158 LSM_HOOK_INIT(capable, selinux_capable),
6159 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6160 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6161 LSM_HOOK_INIT(syslog, selinux_syslog),
6162 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
6164 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
6166 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6167 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6168 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
6169 LSM_HOOK_INIT(bprm_secureexec, selinux_bprm_secureexec),
6171 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6172 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
6173 LSM_HOOK_INIT(sb_copy_data, selinux_sb_copy_data),
6174 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6175 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6176 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6177 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6178 LSM_HOOK_INIT(sb_mount, selinux_mount),
6179 LSM_HOOK_INIT(sb_umount, selinux_umount),
6180 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6181 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
6182 LSM_HOOK_INIT(sb_parse_opts_str, selinux_parse_opts_str),
6184 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
6185 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
6187 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6188 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6189 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6190 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6191 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6192 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6193 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6194 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6195 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6196 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6197 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6198 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6199 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6200 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6201 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6202 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6203 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6204 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6205 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6206 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6207 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6208 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6209 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6210 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6211 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
6212 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
6213 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
6215 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6216 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6217 LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
6218 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6219 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6220 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6221 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6222 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6223 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6224 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6225 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6226 LSM_HOOK_INIT(file_receive, selinux_file_receive),
6228 LSM_HOOK_INIT(file_open, selinux_file_open),
6230 LSM_HOOK_INIT(task_create, selinux_task_create),
6231 LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank),
6232 LSM_HOOK_INIT(cred_free, selinux_cred_free),
6233 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6234 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
6235 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6236 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6237 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
6238 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
6239 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6240 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6241 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6242 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6243 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6244 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6245 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
6246 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6247 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6248 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6249 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6250 LSM_HOOK_INIT(task_kill, selinux_task_kill),
6251 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
6253 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6254 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
6256 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6257 LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
6259 LSM_HOOK_INIT(msg_queue_alloc_security,
6260 selinux_msg_queue_alloc_security),
6261 LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
6262 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6263 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6264 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6265 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
6267 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6268 LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
6269 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6270 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6271 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
6273 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6274 LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
6275 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6276 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6277 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
6279 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
6281 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6282 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
6284 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6285 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6286 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6287 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
6288 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
6289 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6290 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6291 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
6293 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6294 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
6296 LSM_HOOK_INIT(socket_create, selinux_socket_create),
6297 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
6298 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6299 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6300 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6301 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6302 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6303 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6304 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6305 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6306 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6307 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6308 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6309 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6310 LSM_HOOK_INIT(socket_getpeersec_stream,
6311 selinux_socket_getpeersec_stream),
6312 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6313 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6314 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6315 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6316 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6317 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
6318 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6319 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6320 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6321 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6322 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6323 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6324 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6325 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6326 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6327 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6328 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6329 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6330 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
6332 #ifdef CONFIG_SECURITY_NETWORK_XFRM
6333 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6334 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6335 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6336 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6337 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6338 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6339 selinux_xfrm_state_alloc_acquire),
6340 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6341 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6342 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6343 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6344 selinux_xfrm_state_pol_flow_match),
6345 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
6349 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6350 LSM_HOOK_INIT(key_free, selinux_key_free),
6351 LSM_HOOK_INIT(key_permission, selinux_key_permission),
6352 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
6356 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6357 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6358 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6359 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
6363 static __init int selinux_init(void)
6365 if (!security_module_enable("selinux")) {
6366 selinux_enabled = 0;
6370 if (!selinux_enabled) {
6371 printk(KERN_INFO "SELinux: Disabled at boot.\n");
6375 printk(KERN_INFO "SELinux: Initializing.\n");
6377 /* Set the security state for the initial task. */
6378 cred_init_security();
6380 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
6382 sel_inode_cache = kmem_cache_create("selinux_inode_security",
6383 sizeof(struct inode_security_struct),
6384 0, SLAB_PANIC, NULL);
6385 file_security_cache = kmem_cache_create("selinux_file_security",
6386 sizeof(struct file_security_struct),
6387 0, SLAB_PANIC, NULL);
6390 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
6392 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
6393 panic("SELinux: Unable to register AVC netcache callback\n");
6395 if (selinux_enforcing)
6396 printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n");
6398 printk(KERN_DEBUG "SELinux: Starting in permissive mode\n");
6403 static void delayed_superblock_init(struct super_block *sb, void *unused)
6405 superblock_doinit(sb, NULL);
6408 void selinux_complete_init(void)
6410 printk(KERN_DEBUG "SELinux: Completing initialization.\n");
6412 /* Set up any superblocks initialized prior to the policy load. */
6413 printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n");
6414 iterate_supers(delayed_superblock_init, NULL);
6417 /* SELinux requires early initialization in order to label
6418 all processes and objects when they are created. */
6419 security_initcall(selinux_init);
6421 #if defined(CONFIG_NETFILTER)
6423 static struct nf_hook_ops selinux_nf_ops[] = {
6425 .hook = selinux_ipv4_postroute,
6427 .hooknum = NF_INET_POST_ROUTING,
6428 .priority = NF_IP_PRI_SELINUX_LAST,
6431 .hook = selinux_ipv4_forward,
6433 .hooknum = NF_INET_FORWARD,
6434 .priority = NF_IP_PRI_SELINUX_FIRST,
6437 .hook = selinux_ipv4_output,
6439 .hooknum = NF_INET_LOCAL_OUT,
6440 .priority = NF_IP_PRI_SELINUX_FIRST,
6442 #if IS_ENABLED(CONFIG_IPV6)
6444 .hook = selinux_ipv6_postroute,
6446 .hooknum = NF_INET_POST_ROUTING,
6447 .priority = NF_IP6_PRI_SELINUX_LAST,
6450 .hook = selinux_ipv6_forward,
6452 .hooknum = NF_INET_FORWARD,
6453 .priority = NF_IP6_PRI_SELINUX_FIRST,
6456 .hook = selinux_ipv6_output,
6458 .hooknum = NF_INET_LOCAL_OUT,
6459 .priority = NF_IP6_PRI_SELINUX_FIRST,
6464 static int __init selinux_nf_ip_init(void)
6468 if (!selinux_enabled)
6471 printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n");
6473 err = nf_register_hooks(selinux_nf_ops, ARRAY_SIZE(selinux_nf_ops));
6475 panic("SELinux: nf_register_hooks: error %d\n", err);
6480 __initcall(selinux_nf_ip_init);
6482 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
6483 static void selinux_nf_ip_exit(void)
6485 printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n");
6487 nf_unregister_hooks(selinux_nf_ops, ARRAY_SIZE(selinux_nf_ops));
6491 #else /* CONFIG_NETFILTER */
6493 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
6494 #define selinux_nf_ip_exit()
6497 #endif /* CONFIG_NETFILTER */
6499 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
6500 static int selinux_disabled;
6502 int selinux_disable(void)
6504 if (ss_initialized) {
6505 /* Not permitted after initial policy load. */
6509 if (selinux_disabled) {
6510 /* Only do this once. */
6514 printk(KERN_INFO "SELinux: Disabled at runtime.\n");
6516 selinux_disabled = 1;
6517 selinux_enabled = 0;
6519 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
6521 /* Try to destroy the avc node cache */
6524 /* Unregister netfilter hooks. */
6525 selinux_nf_ip_exit();
6527 /* Unregister selinuxfs. */