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/security.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[] */
55 #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
56 #include <net/inet_connection_sock.h>
57 #include <net/net_namespace.h>
58 #include <net/netlabel.h>
59 #include <linux/uaccess.h>
60 #include <asm/ioctls.h>
61 #include <linux/atomic.h>
62 #include <linux/bitops.h>
63 #include <linux/interrupt.h>
64 #include <linux/netdevice.h> /* for network interface checks */
65 #include <net/netlink.h>
66 #include <linux/tcp.h>
67 #include <linux/udp.h>
68 #include <linux/dccp.h>
69 #include <linux/quota.h>
70 #include <linux/un.h> /* for Unix socket types */
71 #include <net/af_unix.h> /* for Unix socket types */
72 #include <linux/parser.h>
73 #include <linux/nfs_mount.h>
75 #include <linux/hugetlb.h>
76 #include <linux/personality.h>
77 #include <linux/audit.h>
78 #include <linux/string.h>
79 #include <linux/selinux.h>
80 #include <linux/mutex.h>
81 #include <linux/posix-timers.h>
82 #include <linux/syslog.h>
83 #include <linux/user_namespace.h>
84 #include <linux/export.h>
85 #include <linux/security.h>
86 #include <linux/msg.h>
87 #include <linux/shm.h>
99 extern struct security_operations *security_ops;
101 /* SECMARK reference count */
102 static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
104 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
105 int selinux_enforcing;
107 static int __init enforcing_setup(char *str)
109 unsigned long enforcing;
110 if (!strict_strtoul(str, 0, &enforcing))
111 selinux_enforcing = enforcing ? 1 : 0;
114 __setup("enforcing=", enforcing_setup);
117 #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
118 int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
120 static int __init selinux_enabled_setup(char *str)
122 unsigned long enabled;
123 if (!strict_strtoul(str, 0, &enabled))
124 selinux_enabled = enabled ? 1 : 0;
127 __setup("selinux=", selinux_enabled_setup);
129 int selinux_enabled = 1;
132 static struct kmem_cache *sel_inode_cache;
135 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
138 * This function checks the SECMARK reference counter to see if any SECMARK
139 * targets are currently configured, if the reference counter is greater than
140 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
141 * enabled, false (0) if SECMARK is disabled. If the always_check_network
142 * policy capability is enabled, SECMARK is always considered enabled.
145 static int selinux_secmark_enabled(void)
147 return (selinux_policycap_alwaysnetwork || atomic_read(&selinux_secmark_refcount));
151 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
154 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
155 * (1) if any are enabled or false (0) if neither are enabled. If the
156 * always_check_network policy capability is enabled, peer labeling
157 * is always considered enabled.
160 static int selinux_peerlbl_enabled(void)
162 return (selinux_policycap_alwaysnetwork || netlbl_enabled() || selinux_xfrm_enabled());
166 * initialise the security for the init task
168 static void cred_init_security(void)
170 struct cred *cred = (struct cred *) current->real_cred;
171 struct task_security_struct *tsec;
173 tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
175 panic("SELinux: Failed to initialize initial task.\n");
177 tsec->osid = tsec->sid = SECINITSID_KERNEL;
178 cred->security = tsec;
182 * get the security ID of a set of credentials
184 static inline u32 cred_sid(const struct cred *cred)
186 const struct task_security_struct *tsec;
188 tsec = cred->security;
193 * get the objective security ID of a task
195 static inline u32 task_sid(const struct task_struct *task)
200 sid = cred_sid(__task_cred(task));
206 * get the subjective security ID of the current task
208 static inline u32 current_sid(void)
210 const struct task_security_struct *tsec = current_security();
215 /* Allocate and free functions for each kind of security blob. */
217 static int inode_alloc_security(struct inode *inode)
219 struct inode_security_struct *isec;
220 u32 sid = current_sid();
222 isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
226 mutex_init(&isec->lock);
227 INIT_LIST_HEAD(&isec->list);
229 isec->sid = SECINITSID_UNLABELED;
230 isec->sclass = SECCLASS_FILE;
231 isec->task_sid = sid;
232 inode->i_security = isec;
237 static void inode_free_security(struct inode *inode)
239 struct inode_security_struct *isec = inode->i_security;
240 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
242 spin_lock(&sbsec->isec_lock);
243 if (!list_empty(&isec->list))
244 list_del_init(&isec->list);
245 spin_unlock(&sbsec->isec_lock);
247 inode->i_security = NULL;
248 kmem_cache_free(sel_inode_cache, isec);
251 static int file_alloc_security(struct file *file)
253 struct file_security_struct *fsec;
254 u32 sid = current_sid();
256 fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
261 fsec->fown_sid = sid;
262 file->f_security = fsec;
267 static void file_free_security(struct file *file)
269 struct file_security_struct *fsec = file->f_security;
270 file->f_security = NULL;
274 static int superblock_alloc_security(struct super_block *sb)
276 struct superblock_security_struct *sbsec;
278 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
282 mutex_init(&sbsec->lock);
283 INIT_LIST_HEAD(&sbsec->isec_head);
284 spin_lock_init(&sbsec->isec_lock);
286 sbsec->sid = SECINITSID_UNLABELED;
287 sbsec->def_sid = SECINITSID_FILE;
288 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
289 sb->s_security = sbsec;
294 static void superblock_free_security(struct super_block *sb)
296 struct superblock_security_struct *sbsec = sb->s_security;
297 sb->s_security = NULL;
301 /* The file system's label must be initialized prior to use. */
303 static const char *labeling_behaviors[7] = {
305 "uses transition SIDs",
307 "uses genfs_contexts",
308 "not configured for labeling",
309 "uses mountpoint labeling",
310 "uses native labeling",
313 static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
315 static inline int inode_doinit(struct inode *inode)
317 return inode_doinit_with_dentry(inode, NULL);
326 Opt_labelsupport = 5,
330 #define NUM_SEL_MNT_OPTS (Opt_nextmntopt - 1)
332 static const match_table_t tokens = {
333 {Opt_context, CONTEXT_STR "%s"},
334 {Opt_fscontext, FSCONTEXT_STR "%s"},
335 {Opt_defcontext, DEFCONTEXT_STR "%s"},
336 {Opt_rootcontext, ROOTCONTEXT_STR "%s"},
337 {Opt_labelsupport, LABELSUPP_STR},
341 #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
343 static int may_context_mount_sb_relabel(u32 sid,
344 struct superblock_security_struct *sbsec,
345 const struct cred *cred)
347 const struct task_security_struct *tsec = cred->security;
350 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
351 FILESYSTEM__RELABELFROM, NULL);
355 rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
356 FILESYSTEM__RELABELTO, NULL);
360 static int may_context_mount_inode_relabel(u32 sid,
361 struct superblock_security_struct *sbsec,
362 const struct cred *cred)
364 const struct task_security_struct *tsec = cred->security;
366 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
367 FILESYSTEM__RELABELFROM, NULL);
371 rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
372 FILESYSTEM__ASSOCIATE, NULL);
376 static int selinux_is_sblabel_mnt(struct super_block *sb)
378 struct superblock_security_struct *sbsec = sb->s_security;
380 if (sbsec->behavior == SECURITY_FS_USE_XATTR ||
381 sbsec->behavior == SECURITY_FS_USE_TRANS ||
382 sbsec->behavior == SECURITY_FS_USE_TASK)
385 /* Special handling for sysfs. Is genfs but also has setxattr handler*/
386 if (strncmp(sb->s_type->name, "sysfs", sizeof("sysfs")) == 0)
390 * Special handling for rootfs. Is genfs but supports
391 * setting SELinux context on in-core inodes.
393 if (strncmp(sb->s_type->name, "rootfs", sizeof("rootfs")) == 0)
399 static int sb_finish_set_opts(struct super_block *sb)
401 struct superblock_security_struct *sbsec = sb->s_security;
402 struct dentry *root = sb->s_root;
403 struct inode *root_inode = root->d_inode;
406 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
407 /* Make sure that the xattr handler exists and that no
408 error other than -ENODATA is returned by getxattr on
409 the root directory. -ENODATA is ok, as this may be
410 the first boot of the SELinux kernel before we have
411 assigned xattr values to the filesystem. */
412 if (!root_inode->i_op->getxattr) {
413 printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
414 "xattr support\n", sb->s_id, sb->s_type->name);
418 rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
419 if (rc < 0 && rc != -ENODATA) {
420 if (rc == -EOPNOTSUPP)
421 printk(KERN_WARNING "SELinux: (dev %s, type "
422 "%s) has no security xattr handler\n",
423 sb->s_id, sb->s_type->name);
425 printk(KERN_WARNING "SELinux: (dev %s, type "
426 "%s) getxattr errno %d\n", sb->s_id,
427 sb->s_type->name, -rc);
432 if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
433 printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
434 sb->s_id, sb->s_type->name);
436 printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n",
437 sb->s_id, sb->s_type->name,
438 labeling_behaviors[sbsec->behavior-1]);
440 sbsec->flags |= SE_SBINITIALIZED;
441 if (selinux_is_sblabel_mnt(sb))
442 sbsec->flags |= SBLABEL_MNT;
444 /* Initialize the root inode. */
445 rc = inode_doinit_with_dentry(root_inode, root);
447 /* Initialize any other inodes associated with the superblock, e.g.
448 inodes created prior to initial policy load or inodes created
449 during get_sb by a pseudo filesystem that directly
451 spin_lock(&sbsec->isec_lock);
453 if (!list_empty(&sbsec->isec_head)) {
454 struct inode_security_struct *isec =
455 list_entry(sbsec->isec_head.next,
456 struct inode_security_struct, list);
457 struct inode *inode = isec->inode;
458 spin_unlock(&sbsec->isec_lock);
459 inode = igrab(inode);
461 if (!IS_PRIVATE(inode))
465 spin_lock(&sbsec->isec_lock);
466 list_del_init(&isec->list);
469 spin_unlock(&sbsec->isec_lock);
475 * This function should allow an FS to ask what it's mount security
476 * options were so it can use those later for submounts, displaying
477 * mount options, or whatever.
479 static int selinux_get_mnt_opts(const struct super_block *sb,
480 struct security_mnt_opts *opts)
483 struct superblock_security_struct *sbsec = sb->s_security;
484 char *context = NULL;
488 security_init_mnt_opts(opts);
490 if (!(sbsec->flags & SE_SBINITIALIZED))
496 /* make sure we always check enough bits to cover the mask */
497 BUILD_BUG_ON(SE_MNTMASK >= (1 << NUM_SEL_MNT_OPTS));
499 tmp = sbsec->flags & SE_MNTMASK;
500 /* count the number of mount options for this sb */
501 for (i = 0; i < NUM_SEL_MNT_OPTS; i++) {
503 opts->num_mnt_opts++;
506 /* Check if the Label support flag is set */
507 if (sbsec->flags & SBLABEL_MNT)
508 opts->num_mnt_opts++;
510 opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
511 if (!opts->mnt_opts) {
516 opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
517 if (!opts->mnt_opts_flags) {
523 if (sbsec->flags & FSCONTEXT_MNT) {
524 rc = security_sid_to_context(sbsec->sid, &context, &len);
527 opts->mnt_opts[i] = context;
528 opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
530 if (sbsec->flags & CONTEXT_MNT) {
531 rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
534 opts->mnt_opts[i] = context;
535 opts->mnt_opts_flags[i++] = CONTEXT_MNT;
537 if (sbsec->flags & DEFCONTEXT_MNT) {
538 rc = security_sid_to_context(sbsec->def_sid, &context, &len);
541 opts->mnt_opts[i] = context;
542 opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
544 if (sbsec->flags & ROOTCONTEXT_MNT) {
545 struct inode *root = sbsec->sb->s_root->d_inode;
546 struct inode_security_struct *isec = root->i_security;
548 rc = security_sid_to_context(isec->sid, &context, &len);
551 opts->mnt_opts[i] = context;
552 opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
554 if (sbsec->flags & SBLABEL_MNT) {
555 opts->mnt_opts[i] = NULL;
556 opts->mnt_opts_flags[i++] = SBLABEL_MNT;
559 BUG_ON(i != opts->num_mnt_opts);
564 security_free_mnt_opts(opts);
568 static int bad_option(struct superblock_security_struct *sbsec, char flag,
569 u32 old_sid, u32 new_sid)
571 char mnt_flags = sbsec->flags & SE_MNTMASK;
573 /* check if the old mount command had the same options */
574 if (sbsec->flags & SE_SBINITIALIZED)
575 if (!(sbsec->flags & flag) ||
576 (old_sid != new_sid))
579 /* check if we were passed the same options twice,
580 * aka someone passed context=a,context=b
582 if (!(sbsec->flags & SE_SBINITIALIZED))
583 if (mnt_flags & flag)
589 * Allow filesystems with binary mount data to explicitly set mount point
590 * labeling information.
592 static int selinux_set_mnt_opts(struct super_block *sb,
593 struct security_mnt_opts *opts,
594 unsigned long kern_flags,
595 unsigned long *set_kern_flags)
597 const struct cred *cred = current_cred();
599 struct superblock_security_struct *sbsec = sb->s_security;
600 const char *name = sb->s_type->name;
601 struct inode *inode = sbsec->sb->s_root->d_inode;
602 struct inode_security_struct *root_isec = inode->i_security;
603 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
604 u32 defcontext_sid = 0;
605 char **mount_options = opts->mnt_opts;
606 int *flags = opts->mnt_opts_flags;
607 int num_opts = opts->num_mnt_opts;
609 mutex_lock(&sbsec->lock);
611 if (!ss_initialized) {
613 /* Defer initialization until selinux_complete_init,
614 after the initial policy is loaded and the security
615 server is ready to handle calls. */
619 printk(KERN_WARNING "SELinux: Unable to set superblock options "
620 "before the security server is initialized\n");
623 if (kern_flags && !set_kern_flags) {
624 /* Specifying internal flags without providing a place to
625 * place the results is not allowed */
631 * Binary mount data FS will come through this function twice. Once
632 * from an explicit call and once from the generic calls from the vfs.
633 * Since the generic VFS calls will not contain any security mount data
634 * we need to skip the double mount verification.
636 * This does open a hole in which we will not notice if the first
637 * mount using this sb set explict options and a second mount using
638 * this sb does not set any security options. (The first options
639 * will be used for both mounts)
641 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
646 * parse the mount options, check if they are valid sids.
647 * also check if someone is trying to mount the same sb more
648 * than once with different security options.
650 for (i = 0; i < num_opts; i++) {
653 if (flags[i] == SBLABEL_MNT)
655 rc = security_context_to_sid(mount_options[i],
656 strlen(mount_options[i]), &sid);
658 printk(KERN_WARNING "SELinux: security_context_to_sid"
659 "(%s) failed for (dev %s, type %s) errno=%d\n",
660 mount_options[i], sb->s_id, name, rc);
667 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
669 goto out_double_mount;
671 sbsec->flags |= FSCONTEXT_MNT;
676 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
678 goto out_double_mount;
680 sbsec->flags |= CONTEXT_MNT;
682 case ROOTCONTEXT_MNT:
683 rootcontext_sid = sid;
685 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
687 goto out_double_mount;
689 sbsec->flags |= ROOTCONTEXT_MNT;
693 defcontext_sid = sid;
695 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
697 goto out_double_mount;
699 sbsec->flags |= DEFCONTEXT_MNT;
708 if (sbsec->flags & SE_SBINITIALIZED) {
709 /* previously mounted with options, but not on this attempt? */
710 if ((sbsec->flags & SE_MNTMASK) && !num_opts)
711 goto out_double_mount;
716 if (strcmp(sb->s_type->name, "proc") == 0)
717 sbsec->flags |= SE_SBPROC;
719 if (!sbsec->behavior) {
721 * Determine the labeling behavior to use for this
724 rc = security_fs_use(sb);
727 "%s: security_fs_use(%s) returned %d\n",
728 __func__, sb->s_type->name, rc);
732 /* sets the context of the superblock for the fs being mounted. */
734 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
738 sbsec->sid = fscontext_sid;
742 * Switch to using mount point labeling behavior.
743 * sets the label used on all file below the mountpoint, and will set
744 * the superblock context if not already set.
746 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
747 sbsec->behavior = SECURITY_FS_USE_NATIVE;
748 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
752 if (!fscontext_sid) {
753 rc = may_context_mount_sb_relabel(context_sid, sbsec,
757 sbsec->sid = context_sid;
759 rc = may_context_mount_inode_relabel(context_sid, sbsec,
764 if (!rootcontext_sid)
765 rootcontext_sid = context_sid;
767 sbsec->mntpoint_sid = context_sid;
768 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
771 if (rootcontext_sid) {
772 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
777 root_isec->sid = rootcontext_sid;
778 root_isec->initialized = 1;
781 if (defcontext_sid) {
782 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
783 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
785 printk(KERN_WARNING "SELinux: defcontext option is "
786 "invalid for this filesystem type\n");
790 if (defcontext_sid != sbsec->def_sid) {
791 rc = may_context_mount_inode_relabel(defcontext_sid,
797 sbsec->def_sid = defcontext_sid;
800 rc = sb_finish_set_opts(sb);
802 mutex_unlock(&sbsec->lock);
806 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different "
807 "security settings for (dev %s, type %s)\n", sb->s_id, name);
811 static int selinux_cmp_sb_context(const struct super_block *oldsb,
812 const struct super_block *newsb)
814 struct superblock_security_struct *old = oldsb->s_security;
815 struct superblock_security_struct *new = newsb->s_security;
816 char oldflags = old->flags & SE_MNTMASK;
817 char newflags = new->flags & SE_MNTMASK;
819 if (oldflags != newflags)
821 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
823 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
825 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
827 if (oldflags & ROOTCONTEXT_MNT) {
828 struct inode_security_struct *oldroot = oldsb->s_root->d_inode->i_security;
829 struct inode_security_struct *newroot = newsb->s_root->d_inode->i_security;
830 if (oldroot->sid != newroot->sid)
835 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, "
836 "different security settings for (dev %s, "
837 "type %s)\n", newsb->s_id, newsb->s_type->name);
841 static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
842 struct super_block *newsb)
844 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
845 struct superblock_security_struct *newsbsec = newsb->s_security;
847 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
848 int set_context = (oldsbsec->flags & CONTEXT_MNT);
849 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
852 * if the parent was able to be mounted it clearly had no special lsm
853 * mount options. thus we can safely deal with this superblock later
858 /* how can we clone if the old one wasn't set up?? */
859 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
861 /* if fs is reusing a sb, make sure that the contexts match */
862 if (newsbsec->flags & SE_SBINITIALIZED)
863 return selinux_cmp_sb_context(oldsb, newsb);
865 mutex_lock(&newsbsec->lock);
867 newsbsec->flags = oldsbsec->flags;
869 newsbsec->sid = oldsbsec->sid;
870 newsbsec->def_sid = oldsbsec->def_sid;
871 newsbsec->behavior = oldsbsec->behavior;
874 u32 sid = oldsbsec->mntpoint_sid;
878 if (!set_rootcontext) {
879 struct inode *newinode = newsb->s_root->d_inode;
880 struct inode_security_struct *newisec = newinode->i_security;
883 newsbsec->mntpoint_sid = sid;
885 if (set_rootcontext) {
886 const struct inode *oldinode = oldsb->s_root->d_inode;
887 const struct inode_security_struct *oldisec = oldinode->i_security;
888 struct inode *newinode = newsb->s_root->d_inode;
889 struct inode_security_struct *newisec = newinode->i_security;
891 newisec->sid = oldisec->sid;
894 sb_finish_set_opts(newsb);
895 mutex_unlock(&newsbsec->lock);
899 static int selinux_parse_opts_str(char *options,
900 struct security_mnt_opts *opts)
903 char *context = NULL, *defcontext = NULL;
904 char *fscontext = NULL, *rootcontext = NULL;
905 int rc, num_mnt_opts = 0;
907 opts->num_mnt_opts = 0;
909 /* Standard string-based options. */
910 while ((p = strsep(&options, "|")) != NULL) {
912 substring_t args[MAX_OPT_ARGS];
917 token = match_token(p, tokens, args);
921 if (context || defcontext) {
923 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
926 context = match_strdup(&args[0]);
936 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
939 fscontext = match_strdup(&args[0]);
946 case Opt_rootcontext:
949 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
952 rootcontext = match_strdup(&args[0]);
960 if (context || defcontext) {
962 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
965 defcontext = match_strdup(&args[0]);
971 case Opt_labelsupport:
975 printk(KERN_WARNING "SELinux: unknown mount option\n");
982 opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
986 opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
987 if (!opts->mnt_opts_flags) {
988 kfree(opts->mnt_opts);
993 opts->mnt_opts[num_mnt_opts] = fscontext;
994 opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
997 opts->mnt_opts[num_mnt_opts] = context;
998 opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
1001 opts->mnt_opts[num_mnt_opts] = rootcontext;
1002 opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
1005 opts->mnt_opts[num_mnt_opts] = defcontext;
1006 opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
1009 opts->num_mnt_opts = num_mnt_opts;
1020 * string mount options parsing and call set the sbsec
1022 static int superblock_doinit(struct super_block *sb, void *data)
1025 char *options = data;
1026 struct security_mnt_opts opts;
1028 security_init_mnt_opts(&opts);
1033 BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
1035 rc = selinux_parse_opts_str(options, &opts);
1040 rc = selinux_set_mnt_opts(sb, &opts, 0, NULL);
1043 security_free_mnt_opts(&opts);
1047 static void selinux_write_opts(struct seq_file *m,
1048 struct security_mnt_opts *opts)
1053 for (i = 0; i < opts->num_mnt_opts; i++) {
1056 if (opts->mnt_opts[i])
1057 has_comma = strchr(opts->mnt_opts[i], ',');
1061 switch (opts->mnt_opts_flags[i]) {
1063 prefix = CONTEXT_STR;
1066 prefix = FSCONTEXT_STR;
1068 case ROOTCONTEXT_MNT:
1069 prefix = ROOTCONTEXT_STR;
1071 case DEFCONTEXT_MNT:
1072 prefix = DEFCONTEXT_STR;
1076 seq_puts(m, LABELSUPP_STR);
1082 /* we need a comma before each option */
1084 seq_puts(m, prefix);
1087 seq_puts(m, opts->mnt_opts[i]);
1093 static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1095 struct security_mnt_opts opts;
1098 rc = selinux_get_mnt_opts(sb, &opts);
1100 /* before policy load we may get EINVAL, don't show anything */
1106 selinux_write_opts(m, &opts);
1108 security_free_mnt_opts(&opts);
1113 static inline u16 inode_mode_to_security_class(umode_t mode)
1115 switch (mode & S_IFMT) {
1117 return SECCLASS_SOCK_FILE;
1119 return SECCLASS_LNK_FILE;
1121 return SECCLASS_FILE;
1123 return SECCLASS_BLK_FILE;
1125 return SECCLASS_DIR;
1127 return SECCLASS_CHR_FILE;
1129 return SECCLASS_FIFO_FILE;
1133 return SECCLASS_FILE;
1136 static inline int default_protocol_stream(int protocol)
1138 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1141 static inline int default_protocol_dgram(int protocol)
1143 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1146 static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1152 case SOCK_SEQPACKET:
1153 return SECCLASS_UNIX_STREAM_SOCKET;
1155 return SECCLASS_UNIX_DGRAM_SOCKET;
1162 if (default_protocol_stream(protocol))
1163 return SECCLASS_TCP_SOCKET;
1165 return SECCLASS_RAWIP_SOCKET;
1167 if (default_protocol_dgram(protocol))
1168 return SECCLASS_UDP_SOCKET;
1170 return SECCLASS_RAWIP_SOCKET;
1172 return SECCLASS_DCCP_SOCKET;
1174 return SECCLASS_RAWIP_SOCKET;
1180 return SECCLASS_NETLINK_ROUTE_SOCKET;
1181 case NETLINK_FIREWALL:
1182 return SECCLASS_NETLINK_FIREWALL_SOCKET;
1183 case NETLINK_SOCK_DIAG:
1184 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1186 return SECCLASS_NETLINK_NFLOG_SOCKET;
1188 return SECCLASS_NETLINK_XFRM_SOCKET;
1189 case NETLINK_SELINUX:
1190 return SECCLASS_NETLINK_SELINUX_SOCKET;
1192 return SECCLASS_NETLINK_AUDIT_SOCKET;
1193 case NETLINK_IP6_FW:
1194 return SECCLASS_NETLINK_IP6FW_SOCKET;
1195 case NETLINK_DNRTMSG:
1196 return SECCLASS_NETLINK_DNRT_SOCKET;
1197 case NETLINK_KOBJECT_UEVENT:
1198 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
1200 return SECCLASS_NETLINK_SOCKET;
1203 return SECCLASS_PACKET_SOCKET;
1205 return SECCLASS_KEY_SOCKET;
1207 return SECCLASS_APPLETALK_SOCKET;
1210 return SECCLASS_SOCKET;
1213 #ifdef CONFIG_PROC_FS
1214 static int selinux_proc_get_sid(struct dentry *dentry,
1219 char *buffer, *path;
1221 buffer = (char *)__get_free_page(GFP_KERNEL);
1225 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1229 /* each process gets a /proc/PID/ entry. Strip off the
1230 * PID part to get a valid selinux labeling.
1231 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1232 while (path[1] >= '0' && path[1] <= '9') {
1236 rc = security_genfs_sid("proc", path, tclass, sid);
1238 free_page((unsigned long)buffer);
1242 static int selinux_proc_get_sid(struct dentry *dentry,
1250 /* The inode's security attributes must be initialized before first use. */
1251 static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1253 struct superblock_security_struct *sbsec = NULL;
1254 struct inode_security_struct *isec = inode->i_security;
1256 struct dentry *dentry;
1257 #define INITCONTEXTLEN 255
1258 char *context = NULL;
1262 if (isec->initialized)
1265 mutex_lock(&isec->lock);
1266 if (isec->initialized)
1269 sbsec = inode->i_sb->s_security;
1270 if (!(sbsec->flags & SE_SBINITIALIZED)) {
1271 /* Defer initialization until selinux_complete_init,
1272 after the initial policy is loaded and the security
1273 server is ready to handle calls. */
1274 spin_lock(&sbsec->isec_lock);
1275 if (list_empty(&isec->list))
1276 list_add(&isec->list, &sbsec->isec_head);
1277 spin_unlock(&sbsec->isec_lock);
1281 switch (sbsec->behavior) {
1282 case SECURITY_FS_USE_NATIVE:
1284 case SECURITY_FS_USE_XATTR:
1285 if (!inode->i_op->getxattr) {
1286 isec->sid = sbsec->def_sid;
1290 /* Need a dentry, since the xattr API requires one.
1291 Life would be simpler if we could just pass the inode. */
1293 /* Called from d_instantiate or d_splice_alias. */
1294 dentry = dget(opt_dentry);
1296 /* Called from selinux_complete_init, try to find a dentry. */
1297 dentry = d_find_alias(inode);
1301 * this is can be hit on boot when a file is accessed
1302 * before the policy is loaded. When we load policy we
1303 * may find inodes that have no dentry on the
1304 * sbsec->isec_head list. No reason to complain as these
1305 * will get fixed up the next time we go through
1306 * inode_doinit with a dentry, before these inodes could
1307 * be used again by userspace.
1312 len = INITCONTEXTLEN;
1313 context = kmalloc(len+1, GFP_NOFS);
1319 context[len] = '\0';
1320 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1322 if (rc == -ERANGE) {
1325 /* Need a larger buffer. Query for the right size. */
1326 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1333 context = kmalloc(len+1, GFP_NOFS);
1339 context[len] = '\0';
1340 rc = inode->i_op->getxattr(dentry,
1346 if (rc != -ENODATA) {
1347 printk(KERN_WARNING "SELinux: %s: getxattr returned "
1348 "%d for dev=%s ino=%ld\n", __func__,
1349 -rc, inode->i_sb->s_id, inode->i_ino);
1353 /* Map ENODATA to the default file SID */
1354 sid = sbsec->def_sid;
1357 rc = security_context_to_sid_default(context, rc, &sid,
1361 char *dev = inode->i_sb->s_id;
1362 unsigned long ino = inode->i_ino;
1364 if (rc == -EINVAL) {
1365 if (printk_ratelimit())
1366 printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
1367 "context=%s. This indicates you may need to relabel the inode or the "
1368 "filesystem in question.\n", ino, dev, context);
1370 printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) "
1371 "returned %d for dev=%s ino=%ld\n",
1372 __func__, context, -rc, dev, ino);
1375 /* Leave with the unlabeled SID */
1383 case SECURITY_FS_USE_TASK:
1384 isec->sid = isec->task_sid;
1386 case SECURITY_FS_USE_TRANS:
1387 /* Default to the fs SID. */
1388 isec->sid = sbsec->sid;
1390 /* Try to obtain a transition SID. */
1391 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1392 rc = security_transition_sid(isec->task_sid, sbsec->sid,
1393 isec->sclass, NULL, &sid);
1398 case SECURITY_FS_USE_MNTPOINT:
1399 isec->sid = sbsec->mntpoint_sid;
1402 /* Default to the fs superblock SID. */
1403 isec->sid = sbsec->sid;
1405 if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
1407 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1408 rc = selinux_proc_get_sid(opt_dentry,
1419 isec->initialized = 1;
1422 mutex_unlock(&isec->lock);
1424 if (isec->sclass == SECCLASS_FILE)
1425 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1429 /* Convert a Linux signal to an access vector. */
1430 static inline u32 signal_to_av(int sig)
1436 /* Commonly granted from child to parent. */
1437 perm = PROCESS__SIGCHLD;
1440 /* Cannot be caught or ignored */
1441 perm = PROCESS__SIGKILL;
1444 /* Cannot be caught or ignored */
1445 perm = PROCESS__SIGSTOP;
1448 /* All other signals. */
1449 perm = PROCESS__SIGNAL;
1457 * Check permission between a pair of credentials
1458 * fork check, ptrace check, etc.
1460 static int cred_has_perm(const struct cred *actor,
1461 const struct cred *target,
1464 u32 asid = cred_sid(actor), tsid = cred_sid(target);
1466 return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1470 * Check permission between a pair of tasks, e.g. signal checks,
1471 * fork check, ptrace check, etc.
1472 * tsk1 is the actor and tsk2 is the target
1473 * - this uses the default subjective creds of tsk1
1475 static int task_has_perm(const struct task_struct *tsk1,
1476 const struct task_struct *tsk2,
1479 const struct task_security_struct *__tsec1, *__tsec2;
1483 __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid;
1484 __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid;
1486 return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
1490 * Check permission between current and another task, e.g. signal checks,
1491 * fork check, ptrace check, etc.
1492 * current is the actor and tsk2 is the target
1493 * - this uses current's subjective creds
1495 static int current_has_perm(const struct task_struct *tsk,
1500 sid = current_sid();
1501 tsid = task_sid(tsk);
1502 return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
1505 #if CAP_LAST_CAP > 63
1506 #error Fix SELinux to handle capabilities > 63.
1509 /* Check whether a task is allowed to use a capability. */
1510 static int cred_has_capability(const struct cred *cred,
1513 struct common_audit_data ad;
1514 struct av_decision avd;
1516 u32 sid = cred_sid(cred);
1517 u32 av = CAP_TO_MASK(cap);
1520 ad.type = LSM_AUDIT_DATA_CAP;
1523 switch (CAP_TO_INDEX(cap)) {
1525 sclass = SECCLASS_CAPABILITY;
1528 sclass = SECCLASS_CAPABILITY2;
1532 "SELinux: out of range capability %d\n", cap);
1537 rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
1538 if (audit == SECURITY_CAP_AUDIT) {
1539 int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
1546 /* Check whether a task is allowed to use a system operation. */
1547 static int task_has_system(struct task_struct *tsk,
1550 u32 sid = task_sid(tsk);
1552 return avc_has_perm(sid, SECINITSID_KERNEL,
1553 SECCLASS_SYSTEM, perms, NULL);
1556 /* Check whether a task has a particular permission to an inode.
1557 The 'adp' parameter is optional and allows other audit
1558 data to be passed (e.g. the dentry). */
1559 static int inode_has_perm(const struct cred *cred,
1560 struct inode *inode,
1562 struct common_audit_data *adp)
1564 struct inode_security_struct *isec;
1567 validate_creds(cred);
1569 if (unlikely(IS_PRIVATE(inode)))
1572 sid = cred_sid(cred);
1573 isec = inode->i_security;
1575 return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp);
1578 /* Same as inode_has_perm, but pass explicit audit data containing
1579 the dentry to help the auditing code to more easily generate the
1580 pathname if needed. */
1581 static inline int dentry_has_perm(const struct cred *cred,
1582 struct dentry *dentry,
1585 struct inode *inode = dentry->d_inode;
1586 struct common_audit_data ad;
1588 ad.type = LSM_AUDIT_DATA_DENTRY;
1589 ad.u.dentry = dentry;
1590 return inode_has_perm(cred, inode, av, &ad);
1593 /* Same as inode_has_perm, but pass explicit audit data containing
1594 the path to help the auditing code to more easily generate the
1595 pathname if needed. */
1596 static inline int path_has_perm(const struct cred *cred,
1600 struct inode *inode = path->dentry->d_inode;
1601 struct common_audit_data ad;
1603 ad.type = LSM_AUDIT_DATA_PATH;
1605 return inode_has_perm(cred, inode, av, &ad);
1608 /* Same as path_has_perm, but uses the inode from the file struct. */
1609 static inline int file_path_has_perm(const struct cred *cred,
1613 struct common_audit_data ad;
1615 ad.type = LSM_AUDIT_DATA_PATH;
1616 ad.u.path = file->f_path;
1617 return inode_has_perm(cred, file_inode(file), av, &ad);
1620 /* Check whether a task can use an open file descriptor to
1621 access an inode in a given way. Check access to the
1622 descriptor itself, and then use dentry_has_perm to
1623 check a particular permission to the file.
1624 Access to the descriptor is implicitly granted if it
1625 has the same SID as the process. If av is zero, then
1626 access to the file is not checked, e.g. for cases
1627 where only the descriptor is affected like seek. */
1628 static int file_has_perm(const struct cred *cred,
1632 struct file_security_struct *fsec = file->f_security;
1633 struct inode *inode = file_inode(file);
1634 struct common_audit_data ad;
1635 u32 sid = cred_sid(cred);
1638 ad.type = LSM_AUDIT_DATA_PATH;
1639 ad.u.path = file->f_path;
1641 if (sid != fsec->sid) {
1642 rc = avc_has_perm(sid, fsec->sid,
1650 /* av is zero if only checking access to the descriptor. */
1653 rc = inode_has_perm(cred, inode, av, &ad);
1659 /* Check whether a task can create a file. */
1660 static int may_create(struct inode *dir,
1661 struct dentry *dentry,
1664 const struct task_security_struct *tsec = current_security();
1665 struct inode_security_struct *dsec;
1666 struct superblock_security_struct *sbsec;
1668 struct common_audit_data ad;
1671 dsec = dir->i_security;
1672 sbsec = dir->i_sb->s_security;
1675 newsid = tsec->create_sid;
1677 ad.type = LSM_AUDIT_DATA_DENTRY;
1678 ad.u.dentry = dentry;
1680 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
1681 DIR__ADD_NAME | DIR__SEARCH,
1686 if (!newsid || !(sbsec->flags & SBLABEL_MNT)) {
1687 rc = security_transition_sid(sid, dsec->sid, tclass,
1688 &dentry->d_name, &newsid);
1693 rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
1697 return avc_has_perm(newsid, sbsec->sid,
1698 SECCLASS_FILESYSTEM,
1699 FILESYSTEM__ASSOCIATE, &ad);
1702 /* Check whether a task can create a key. */
1703 static int may_create_key(u32 ksid,
1704 struct task_struct *ctx)
1706 u32 sid = task_sid(ctx);
1708 return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
1712 #define MAY_UNLINK 1
1715 /* Check whether a task can link, unlink, or rmdir a file/directory. */
1716 static int may_link(struct inode *dir,
1717 struct dentry *dentry,
1721 struct inode_security_struct *dsec, *isec;
1722 struct common_audit_data ad;
1723 u32 sid = current_sid();
1727 dsec = dir->i_security;
1728 isec = dentry->d_inode->i_security;
1730 ad.type = LSM_AUDIT_DATA_DENTRY;
1731 ad.u.dentry = dentry;
1734 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
1735 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
1750 printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n",
1755 rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
1759 static inline int may_rename(struct inode *old_dir,
1760 struct dentry *old_dentry,
1761 struct inode *new_dir,
1762 struct dentry *new_dentry)
1764 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
1765 struct common_audit_data ad;
1766 u32 sid = current_sid();
1768 int old_is_dir, new_is_dir;
1771 old_dsec = old_dir->i_security;
1772 old_isec = old_dentry->d_inode->i_security;
1773 old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
1774 new_dsec = new_dir->i_security;
1776 ad.type = LSM_AUDIT_DATA_DENTRY;
1778 ad.u.dentry = old_dentry;
1779 rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
1780 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1783 rc = avc_has_perm(sid, old_isec->sid,
1784 old_isec->sclass, FILE__RENAME, &ad);
1787 if (old_is_dir && new_dir != old_dir) {
1788 rc = avc_has_perm(sid, old_isec->sid,
1789 old_isec->sclass, DIR__REPARENT, &ad);
1794 ad.u.dentry = new_dentry;
1795 av = DIR__ADD_NAME | DIR__SEARCH;
1796 if (new_dentry->d_inode)
1797 av |= DIR__REMOVE_NAME;
1798 rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
1801 if (new_dentry->d_inode) {
1802 new_isec = new_dentry->d_inode->i_security;
1803 new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
1804 rc = avc_has_perm(sid, new_isec->sid,
1806 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1814 /* Check whether a task can perform a filesystem operation. */
1815 static int superblock_has_perm(const struct cred *cred,
1816 struct super_block *sb,
1818 struct common_audit_data *ad)
1820 struct superblock_security_struct *sbsec;
1821 u32 sid = cred_sid(cred);
1823 sbsec = sb->s_security;
1824 return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
1827 /* Convert a Linux mode and permission mask to an access vector. */
1828 static inline u32 file_mask_to_av(int mode, int mask)
1832 if (!S_ISDIR(mode)) {
1833 if (mask & MAY_EXEC)
1834 av |= FILE__EXECUTE;
1835 if (mask & MAY_READ)
1838 if (mask & MAY_APPEND)
1840 else if (mask & MAY_WRITE)
1844 if (mask & MAY_EXEC)
1846 if (mask & MAY_WRITE)
1848 if (mask & MAY_READ)
1855 /* Convert a Linux file to an access vector. */
1856 static inline u32 file_to_av(struct file *file)
1860 if (file->f_mode & FMODE_READ)
1862 if (file->f_mode & FMODE_WRITE) {
1863 if (file->f_flags & O_APPEND)
1870 * Special file opened with flags 3 for ioctl-only use.
1879 * Convert a file to an access vector and include the correct open
1882 static inline u32 open_file_to_av(struct file *file)
1884 u32 av = file_to_av(file);
1886 if (selinux_policycap_openperm)
1892 /* Hook functions begin here. */
1894 static int selinux_ptrace_access_check(struct task_struct *child,
1899 rc = cap_ptrace_access_check(child, mode);
1903 if (mode & PTRACE_MODE_READ) {
1904 u32 sid = current_sid();
1905 u32 csid = task_sid(child);
1906 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
1909 return current_has_perm(child, PROCESS__PTRACE);
1912 static int selinux_ptrace_traceme(struct task_struct *parent)
1916 rc = cap_ptrace_traceme(parent);
1920 return task_has_perm(parent, current, PROCESS__PTRACE);
1923 static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
1924 kernel_cap_t *inheritable, kernel_cap_t *permitted)
1928 error = current_has_perm(target, PROCESS__GETCAP);
1932 return cap_capget(target, effective, inheritable, permitted);
1935 static int selinux_capset(struct cred *new, const struct cred *old,
1936 const kernel_cap_t *effective,
1937 const kernel_cap_t *inheritable,
1938 const kernel_cap_t *permitted)
1942 error = cap_capset(new, old,
1943 effective, inheritable, permitted);
1947 return cred_has_perm(old, new, PROCESS__SETCAP);
1951 * (This comment used to live with the selinux_task_setuid hook,
1952 * which was removed).
1954 * Since setuid only affects the current process, and since the SELinux
1955 * controls are not based on the Linux identity attributes, SELinux does not
1956 * need to control this operation. However, SELinux does control the use of
1957 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
1960 static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
1965 rc = cap_capable(cred, ns, cap, audit);
1969 return cred_has_capability(cred, cap, audit);
1972 static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
1974 const struct cred *cred = current_cred();
1986 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
1991 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
1994 rc = 0; /* let the kernel handle invalid cmds */
2000 static int selinux_quota_on(struct dentry *dentry)
2002 const struct cred *cred = current_cred();
2004 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
2007 static int selinux_syslog(int type)
2012 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2013 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
2014 rc = task_has_system(current, SYSTEM__SYSLOG_READ);
2016 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2017 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2018 /* Set level of messages printed to console */
2019 case SYSLOG_ACTION_CONSOLE_LEVEL:
2020 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
2022 case SYSLOG_ACTION_CLOSE: /* Close log */
2023 case SYSLOG_ACTION_OPEN: /* Open log */
2024 case SYSLOG_ACTION_READ: /* Read from log */
2025 case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */
2026 case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
2028 rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
2035 * Check that a process has enough memory to allocate a new virtual
2036 * mapping. 0 means there is enough memory for the allocation to
2037 * succeed and -ENOMEM implies there is not.
2039 * Do not audit the selinux permission check, as this is applied to all
2040 * processes that allocate mappings.
2042 static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
2044 int rc, cap_sys_admin = 0;
2046 rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
2047 SECURITY_CAP_NOAUDIT);
2051 return __vm_enough_memory(mm, pages, cap_sys_admin);
2054 /* binprm security operations */
2056 static int selinux_bprm_set_creds(struct linux_binprm *bprm)
2058 const struct task_security_struct *old_tsec;
2059 struct task_security_struct *new_tsec;
2060 struct inode_security_struct *isec;
2061 struct common_audit_data ad;
2062 struct inode *inode = file_inode(bprm->file);
2065 rc = cap_bprm_set_creds(bprm);
2069 /* SELinux context only depends on initial program or script and not
2070 * the script interpreter */
2071 if (bprm->cred_prepared)
2074 old_tsec = current_security();
2075 new_tsec = bprm->cred->security;
2076 isec = inode->i_security;
2078 /* Default to the current task SID. */
2079 new_tsec->sid = old_tsec->sid;
2080 new_tsec->osid = old_tsec->sid;
2082 /* Reset fs, key, and sock SIDs on execve. */
2083 new_tsec->create_sid = 0;
2084 new_tsec->keycreate_sid = 0;
2085 new_tsec->sockcreate_sid = 0;
2087 if (old_tsec->exec_sid) {
2088 new_tsec->sid = old_tsec->exec_sid;
2089 /* Reset exec SID on execve. */
2090 new_tsec->exec_sid = 0;
2093 * Minimize confusion: if no_new_privs and a transition is
2094 * explicitly requested, then fail the exec.
2096 if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)
2099 /* Check for a default transition on this program. */
2100 rc = security_transition_sid(old_tsec->sid, isec->sid,
2101 SECCLASS_PROCESS, NULL,
2107 ad.type = LSM_AUDIT_DATA_PATH;
2108 ad.u.path = bprm->file->f_path;
2110 if ((bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) ||
2111 (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS))
2112 new_tsec->sid = old_tsec->sid;
2114 if (new_tsec->sid == old_tsec->sid) {
2115 rc = avc_has_perm(old_tsec->sid, isec->sid,
2116 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2120 /* Check permissions for the transition. */
2121 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2122 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2126 rc = avc_has_perm(new_tsec->sid, isec->sid,
2127 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2131 /* Check for shared state */
2132 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2133 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2134 SECCLASS_PROCESS, PROCESS__SHARE,
2140 /* Make sure that anyone attempting to ptrace over a task that
2141 * changes its SID has the appropriate permit */
2143 (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2144 struct task_struct *tracer;
2145 struct task_security_struct *sec;
2149 tracer = ptrace_parent(current);
2150 if (likely(tracer != NULL)) {
2151 sec = __task_cred(tracer)->security;
2157 rc = avc_has_perm(ptsid, new_tsec->sid,
2159 PROCESS__PTRACE, NULL);
2165 /* Clear any possibly unsafe personality bits on exec: */
2166 bprm->per_clear |= PER_CLEAR_ON_SETID;
2172 static int selinux_bprm_secureexec(struct linux_binprm *bprm)
2174 const struct task_security_struct *tsec = current_security();
2182 /* Enable secure mode for SIDs transitions unless
2183 the noatsecure permission is granted between
2184 the two SIDs, i.e. ahp returns 0. */
2185 atsecure = avc_has_perm(osid, sid,
2187 PROCESS__NOATSECURE, NULL);
2190 return (atsecure || cap_bprm_secureexec(bprm));
2193 static int match_file(const void *p, struct file *file, unsigned fd)
2195 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2198 /* Derived from fs/exec.c:flush_old_files. */
2199 static inline void flush_unauthorized_files(const struct cred *cred,
2200 struct files_struct *files)
2202 struct file *file, *devnull = NULL;
2203 struct tty_struct *tty;
2207 tty = get_current_tty();
2209 spin_lock(&tty_files_lock);
2210 if (!list_empty(&tty->tty_files)) {
2211 struct tty_file_private *file_priv;
2213 /* Revalidate access to controlling tty.
2214 Use file_path_has_perm on the tty path directly
2215 rather than using file_has_perm, as this particular
2216 open file may belong to another process and we are
2217 only interested in the inode-based check here. */
2218 file_priv = list_first_entry(&tty->tty_files,
2219 struct tty_file_private, list);
2220 file = file_priv->file;
2221 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
2224 spin_unlock(&tty_files_lock);
2227 /* Reset controlling tty. */
2231 /* Revalidate access to inherited open files. */
2232 n = iterate_fd(files, 0, match_file, cred);
2233 if (!n) /* none found? */
2236 devnull = dentry_open(&selinux_null, O_RDWR, cred);
2237 if (IS_ERR(devnull))
2239 /* replace all the matching ones with this */
2241 replace_fd(n - 1, devnull, 0);
2242 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2248 * Prepare a process for imminent new credential changes due to exec
2250 static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
2252 struct task_security_struct *new_tsec;
2253 struct rlimit *rlim, *initrlim;
2256 new_tsec = bprm->cred->security;
2257 if (new_tsec->sid == new_tsec->osid)
2260 /* Close files for which the new task SID is not authorized. */
2261 flush_unauthorized_files(bprm->cred, current->files);
2263 /* Always clear parent death signal on SID transitions. */
2264 current->pdeath_signal = 0;
2266 /* Check whether the new SID can inherit resource limits from the old
2267 * SID. If not, reset all soft limits to the lower of the current
2268 * task's hard limit and the init task's soft limit.
2270 * Note that the setting of hard limits (even to lower them) can be
2271 * controlled by the setrlimit check. The inclusion of the init task's
2272 * soft limit into the computation is to avoid resetting soft limits
2273 * higher than the default soft limit for cases where the default is
2274 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2276 rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2277 PROCESS__RLIMITINH, NULL);
2279 /* protect against do_prlimit() */
2281 for (i = 0; i < RLIM_NLIMITS; i++) {
2282 rlim = current->signal->rlim + i;
2283 initrlim = init_task.signal->rlim + i;
2284 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2286 task_unlock(current);
2287 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
2292 * Clean up the process immediately after the installation of new credentials
2295 static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2297 const struct task_security_struct *tsec = current_security();
2298 struct itimerval itimer;
2308 /* Check whether the new SID can inherit signal state from the old SID.
2309 * If not, clear itimers to avoid subsequent signal generation and
2310 * flush and unblock signals.
2312 * This must occur _after_ the task SID has been updated so that any
2313 * kill done after the flush will be checked against the new SID.
2315 rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
2317 memset(&itimer, 0, sizeof itimer);
2318 for (i = 0; i < 3; i++)
2319 do_setitimer(i, &itimer, NULL);
2320 spin_lock_irq(¤t->sighand->siglock);
2321 if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
2322 __flush_signals(current);
2323 flush_signal_handlers(current, 1);
2324 sigemptyset(¤t->blocked);
2326 spin_unlock_irq(¤t->sighand->siglock);
2329 /* Wake up the parent if it is waiting so that it can recheck
2330 * wait permission to the new task SID. */
2331 read_lock(&tasklist_lock);
2332 __wake_up_parent(current, current->real_parent);
2333 read_unlock(&tasklist_lock);
2336 /* superblock security operations */
2338 static int selinux_sb_alloc_security(struct super_block *sb)
2340 return superblock_alloc_security(sb);
2343 static void selinux_sb_free_security(struct super_block *sb)
2345 superblock_free_security(sb);
2348 static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2353 return !memcmp(prefix, option, plen);
2356 static inline int selinux_option(char *option, int len)
2358 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2359 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2360 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
2361 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2362 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
2365 static inline void take_option(char **to, char *from, int *first, int len)
2372 memcpy(*to, from, len);
2376 static inline void take_selinux_option(char **to, char *from, int *first,
2379 int current_size = 0;
2387 while (current_size < len) {
2397 static int selinux_sb_copy_data(char *orig, char *copy)
2399 int fnosec, fsec, rc = 0;
2400 char *in_save, *in_curr, *in_end;
2401 char *sec_curr, *nosec_save, *nosec;
2407 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2415 in_save = in_end = orig;
2419 open_quote = !open_quote;
2420 if ((*in_end == ',' && open_quote == 0) ||
2422 int len = in_end - in_curr;
2424 if (selinux_option(in_curr, len))
2425 take_selinux_option(&sec_curr, in_curr, &fsec, len);
2427 take_option(&nosec, in_curr, &fnosec, len);
2429 in_curr = in_end + 1;
2431 } while (*in_end++);
2433 strcpy(in_save, nosec_save);
2434 free_page((unsigned long)nosec_save);
2439 static int selinux_sb_remount(struct super_block *sb, void *data)
2442 struct security_mnt_opts opts;
2443 char *secdata, **mount_options;
2444 struct superblock_security_struct *sbsec = sb->s_security;
2446 if (!(sbsec->flags & SE_SBINITIALIZED))
2452 if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2455 security_init_mnt_opts(&opts);
2456 secdata = alloc_secdata();
2459 rc = selinux_sb_copy_data(data, secdata);
2461 goto out_free_secdata;
2463 rc = selinux_parse_opts_str(secdata, &opts);
2465 goto out_free_secdata;
2467 mount_options = opts.mnt_opts;
2468 flags = opts.mnt_opts_flags;
2470 for (i = 0; i < opts.num_mnt_opts; i++) {
2474 if (flags[i] == SBLABEL_MNT)
2476 len = strlen(mount_options[i]);
2477 rc = security_context_to_sid(mount_options[i], len, &sid);
2479 printk(KERN_WARNING "SELinux: security_context_to_sid"
2480 "(%s) failed for (dev %s, type %s) errno=%d\n",
2481 mount_options[i], sb->s_id, sb->s_type->name, rc);
2487 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2488 goto out_bad_option;
2491 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2492 goto out_bad_option;
2494 case ROOTCONTEXT_MNT: {
2495 struct inode_security_struct *root_isec;
2496 root_isec = sb->s_root->d_inode->i_security;
2498 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2499 goto out_bad_option;
2502 case DEFCONTEXT_MNT:
2503 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2504 goto out_bad_option;
2513 security_free_mnt_opts(&opts);
2515 free_secdata(secdata);
2518 printk(KERN_WARNING "SELinux: unable to change security options "
2519 "during remount (dev %s, type=%s)\n", sb->s_id,
2524 static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
2526 const struct cred *cred = current_cred();
2527 struct common_audit_data ad;
2530 rc = superblock_doinit(sb, data);
2534 /* Allow all mounts performed by the kernel */
2535 if (flags & MS_KERNMOUNT)
2538 ad.type = LSM_AUDIT_DATA_DENTRY;
2539 ad.u.dentry = sb->s_root;
2540 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
2543 static int selinux_sb_statfs(struct dentry *dentry)
2545 const struct cred *cred = current_cred();
2546 struct common_audit_data ad;
2548 ad.type = LSM_AUDIT_DATA_DENTRY;
2549 ad.u.dentry = dentry->d_sb->s_root;
2550 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
2553 static int selinux_mount(const char *dev_name,
2556 unsigned long flags,
2559 const struct cred *cred = current_cred();
2561 if (flags & MS_REMOUNT)
2562 return superblock_has_perm(cred, path->dentry->d_sb,
2563 FILESYSTEM__REMOUNT, NULL);
2565 return path_has_perm(cred, path, FILE__MOUNTON);
2568 static int selinux_umount(struct vfsmount *mnt, int flags)
2570 const struct cred *cred = current_cred();
2572 return superblock_has_perm(cred, mnt->mnt_sb,
2573 FILESYSTEM__UNMOUNT, NULL);
2576 /* inode security operations */
2578 static int selinux_inode_alloc_security(struct inode *inode)
2580 return inode_alloc_security(inode);
2583 static void selinux_inode_free_security(struct inode *inode)
2585 inode_free_security(inode);
2588 static int selinux_dentry_init_security(struct dentry *dentry, int mode,
2589 struct qstr *name, void **ctx,
2592 const struct cred *cred = current_cred();
2593 struct task_security_struct *tsec;
2594 struct inode_security_struct *dsec;
2595 struct superblock_security_struct *sbsec;
2596 struct inode *dir = dentry->d_parent->d_inode;
2600 tsec = cred->security;
2601 dsec = dir->i_security;
2602 sbsec = dir->i_sb->s_security;
2604 if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) {
2605 newsid = tsec->create_sid;
2607 rc = security_transition_sid(tsec->sid, dsec->sid,
2608 inode_mode_to_security_class(mode),
2613 "%s: security_transition_sid failed, rc=%d\n",
2619 return security_sid_to_context(newsid, (char **)ctx, ctxlen);
2622 static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
2623 const struct qstr *qstr,
2625 void **value, size_t *len)
2627 const struct task_security_struct *tsec = current_security();
2628 struct inode_security_struct *dsec;
2629 struct superblock_security_struct *sbsec;
2630 u32 sid, newsid, clen;
2634 dsec = dir->i_security;
2635 sbsec = dir->i_sb->s_security;
2638 newsid = tsec->create_sid;
2640 if ((sbsec->flags & SE_SBINITIALIZED) &&
2641 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT))
2642 newsid = sbsec->mntpoint_sid;
2643 else if (!newsid || !(sbsec->flags & SBLABEL_MNT)) {
2644 rc = security_transition_sid(sid, dsec->sid,
2645 inode_mode_to_security_class(inode->i_mode),
2648 printk(KERN_WARNING "%s: "
2649 "security_transition_sid failed, rc=%d (dev=%s "
2652 -rc, inode->i_sb->s_id, inode->i_ino);
2657 /* Possibly defer initialization to selinux_complete_init. */
2658 if (sbsec->flags & SE_SBINITIALIZED) {
2659 struct inode_security_struct *isec = inode->i_security;
2660 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2662 isec->initialized = 1;
2665 if (!ss_initialized || !(sbsec->flags & SBLABEL_MNT))
2669 *name = XATTR_SELINUX_SUFFIX;
2672 rc = security_sid_to_context_force(newsid, &context, &clen);
2682 static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
2684 return may_create(dir, dentry, SECCLASS_FILE);
2687 static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2689 return may_link(dir, old_dentry, MAY_LINK);
2692 static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2694 return may_link(dir, dentry, MAY_UNLINK);
2697 static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2699 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2702 static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
2704 return may_create(dir, dentry, SECCLASS_DIR);
2707 static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2709 return may_link(dir, dentry, MAY_RMDIR);
2712 static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
2714 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2717 static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
2718 struct inode *new_inode, struct dentry *new_dentry)
2720 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2723 static int selinux_inode_readlink(struct dentry *dentry)
2725 const struct cred *cred = current_cred();
2727 return dentry_has_perm(cred, dentry, FILE__READ);
2730 static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
2732 const struct cred *cred = current_cred();
2734 return dentry_has_perm(cred, dentry, FILE__READ);
2737 static noinline int audit_inode_permission(struct inode *inode,
2738 u32 perms, u32 audited, u32 denied,
2741 struct common_audit_data ad;
2742 struct inode_security_struct *isec = inode->i_security;
2745 ad.type = LSM_AUDIT_DATA_INODE;
2748 rc = slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms,
2749 audited, denied, &ad, flags);
2755 static int selinux_inode_permission(struct inode *inode, int mask)
2757 const struct cred *cred = current_cred();
2760 unsigned flags = mask & MAY_NOT_BLOCK;
2761 struct inode_security_struct *isec;
2763 struct av_decision avd;
2765 u32 audited, denied;
2767 from_access = mask & MAY_ACCESS;
2768 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2770 /* No permission to check. Existence test. */
2774 validate_creds(cred);
2776 if (unlikely(IS_PRIVATE(inode)))
2779 perms = file_mask_to_av(inode->i_mode, mask);
2781 sid = cred_sid(cred);
2782 isec = inode->i_security;
2784 rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0, &avd);
2785 audited = avc_audit_required(perms, &avd, rc,
2786 from_access ? FILE__AUDIT_ACCESS : 0,
2788 if (likely(!audited))
2791 rc2 = audit_inode_permission(inode, perms, audited, denied, flags);
2797 static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
2799 const struct cred *cred = current_cred();
2800 unsigned int ia_valid = iattr->ia_valid;
2801 __u32 av = FILE__WRITE;
2803 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2804 if (ia_valid & ATTR_FORCE) {
2805 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
2811 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
2812 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
2813 return dentry_has_perm(cred, dentry, FILE__SETATTR);
2815 if (selinux_policycap_openperm && (ia_valid & ATTR_SIZE))
2818 return dentry_has_perm(cred, dentry, av);
2821 static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
2823 const struct cred *cred = current_cred();
2826 path.dentry = dentry;
2829 return path_has_perm(cred, &path, FILE__GETATTR);
2832 static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
2834 const struct cred *cred = current_cred();
2836 if (!strncmp(name, XATTR_SECURITY_PREFIX,
2837 sizeof XATTR_SECURITY_PREFIX - 1)) {
2838 if (!strcmp(name, XATTR_NAME_CAPS)) {
2839 if (!capable(CAP_SETFCAP))
2841 } else if (!capable(CAP_SYS_ADMIN)) {
2842 /* A different attribute in the security namespace.
2843 Restrict to administrator. */
2848 /* Not an attribute we recognize, so just check the
2849 ordinary setattr permission. */
2850 return dentry_has_perm(cred, dentry, FILE__SETATTR);
2853 static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
2854 const void *value, size_t size, int flags)
2856 struct inode *inode = dentry->d_inode;
2857 struct inode_security_struct *isec = inode->i_security;
2858 struct superblock_security_struct *sbsec;
2859 struct common_audit_data ad;
2860 u32 newsid, sid = current_sid();
2863 if (strcmp(name, XATTR_NAME_SELINUX))
2864 return selinux_inode_setotherxattr(dentry, name);
2866 sbsec = inode->i_sb->s_security;
2867 if (!(sbsec->flags & SBLABEL_MNT))
2870 if (!inode_owner_or_capable(inode))
2873 ad.type = LSM_AUDIT_DATA_DENTRY;
2874 ad.u.dentry = dentry;
2876 rc = avc_has_perm(sid, isec->sid, isec->sclass,
2877 FILE__RELABELFROM, &ad);
2881 rc = security_context_to_sid(value, size, &newsid);
2882 if (rc == -EINVAL) {
2883 if (!capable(CAP_MAC_ADMIN)) {
2884 struct audit_buffer *ab;
2888 /* We strip a nul only if it is at the end, otherwise the
2889 * context contains a nul and we should audit that */
2892 if (str[size - 1] == '\0')
2893 audit_size = size - 1;
2900 ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
2901 audit_log_format(ab, "op=setxattr invalid_context=");
2902 audit_log_n_untrustedstring(ab, value, audit_size);
2907 rc = security_context_to_sid_force(value, size, &newsid);
2912 rc = avc_has_perm(sid, newsid, isec->sclass,
2913 FILE__RELABELTO, &ad);
2917 rc = security_validate_transition(isec->sid, newsid, sid,
2922 return avc_has_perm(newsid,
2924 SECCLASS_FILESYSTEM,
2925 FILESYSTEM__ASSOCIATE,
2929 static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
2930 const void *value, size_t size,
2933 struct inode *inode = dentry->d_inode;
2934 struct inode_security_struct *isec = inode->i_security;
2938 if (strcmp(name, XATTR_NAME_SELINUX)) {
2939 /* Not an attribute we recognize, so nothing to do. */
2943 rc = security_context_to_sid_force(value, size, &newsid);
2945 printk(KERN_ERR "SELinux: unable to map context to SID"
2946 "for (%s, %lu), rc=%d\n",
2947 inode->i_sb->s_id, inode->i_ino, -rc);
2951 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2953 isec->initialized = 1;
2958 static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
2960 const struct cred *cred = current_cred();
2962 return dentry_has_perm(cred, dentry, FILE__GETATTR);
2965 static int selinux_inode_listxattr(struct dentry *dentry)
2967 const struct cred *cred = current_cred();
2969 return dentry_has_perm(cred, dentry, FILE__GETATTR);
2972 static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
2974 if (strcmp(name, XATTR_NAME_SELINUX))
2975 return selinux_inode_setotherxattr(dentry, name);
2977 /* No one is allowed to remove a SELinux security label.
2978 You can change the label, but all data must be labeled. */
2983 * Copy the inode security context value to the user.
2985 * Permission check is handled by selinux_inode_getxattr hook.
2987 static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
2991 char *context = NULL;
2992 struct inode_security_struct *isec = inode->i_security;
2994 if (strcmp(name, XATTR_SELINUX_SUFFIX))
2998 * If the caller has CAP_MAC_ADMIN, then get the raw context
2999 * value even if it is not defined by current policy; otherwise,
3000 * use the in-core value under current policy.
3001 * Use the non-auditing forms of the permission checks since
3002 * getxattr may be called by unprivileged processes commonly
3003 * and lack of permission just means that we fall back to the
3004 * in-core context value, not a denial.
3006 error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
3007 SECURITY_CAP_NOAUDIT);
3009 error = security_sid_to_context_force(isec->sid, &context,
3012 error = security_sid_to_context(isec->sid, &context, &size);
3025 static int selinux_inode_setsecurity(struct inode *inode, const char *name,
3026 const void *value, size_t size, int flags)
3028 struct inode_security_struct *isec = inode->i_security;
3032 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3035 if (!value || !size)
3038 rc = security_context_to_sid((void *)value, size, &newsid);
3042 isec->sclass = inode_mode_to_security_class(inode->i_mode);
3044 isec->initialized = 1;
3048 static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3050 const int len = sizeof(XATTR_NAME_SELINUX);
3051 if (buffer && len <= buffer_size)
3052 memcpy(buffer, XATTR_NAME_SELINUX, len);
3056 static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
3058 struct inode_security_struct *isec = inode->i_security;
3062 /* file security operations */
3064 static int selinux_revalidate_file_permission(struct file *file, int mask)
3066 const struct cred *cred = current_cred();
3067 struct inode *inode = file_inode(file);
3069 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3070 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3073 return file_has_perm(cred, file,
3074 file_mask_to_av(inode->i_mode, mask));
3077 static int selinux_file_permission(struct file *file, int mask)
3079 struct inode *inode = file_inode(file);
3080 struct file_security_struct *fsec = file->f_security;
3081 struct inode_security_struct *isec = inode->i_security;
3082 u32 sid = current_sid();
3085 /* No permission to check. Existence test. */
3088 if (sid == fsec->sid && fsec->isid == isec->sid &&
3089 fsec->pseqno == avc_policy_seqno())
3090 /* No change since file_open check. */
3093 return selinux_revalidate_file_permission(file, mask);
3096 static int selinux_file_alloc_security(struct file *file)
3098 return file_alloc_security(file);
3101 static void selinux_file_free_security(struct file *file)
3103 file_free_security(file);
3106 static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3109 const struct cred *cred = current_cred();
3119 case FS_IOC_GETFLAGS:
3121 case FS_IOC_GETVERSION:
3122 error = file_has_perm(cred, file, FILE__GETATTR);
3125 case FS_IOC_SETFLAGS:
3127 case FS_IOC_SETVERSION:
3128 error = file_has_perm(cred, file, FILE__SETATTR);
3131 /* sys_ioctl() checks */
3135 error = file_has_perm(cred, file, 0);
3140 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3141 SECURITY_CAP_AUDIT);
3144 /* default case assumes that the command will go
3145 * to the file's ioctl() function.
3148 error = file_has_perm(cred, file, FILE__IOCTL);
3153 static int default_noexec;
3155 static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3157 const struct cred *cred = current_cred();
3160 if (default_noexec &&
3161 (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
3163 * We are making executable an anonymous mapping or a
3164 * private file mapping that will also be writable.
3165 * This has an additional check.
3167 rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
3173 /* read access is always possible with a mapping */
3174 u32 av = FILE__READ;
3176 /* write access only matters if the mapping is shared */
3177 if (shared && (prot & PROT_WRITE))
3180 if (prot & PROT_EXEC)
3181 av |= FILE__EXECUTE;
3183 return file_has_perm(cred, file, av);
3190 static int selinux_mmap_addr(unsigned long addr)
3193 u32 sid = current_sid();
3196 * notice that we are intentionally putting the SELinux check before
3197 * the secondary cap_file_mmap check. This is such a likely attempt
3198 * at bad behaviour/exploit that we always want to get the AVC, even
3199 * if DAC would have also denied the operation.
3201 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3202 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3203 MEMPROTECT__MMAP_ZERO, NULL);
3208 /* do DAC check on address space usage */
3209 return cap_mmap_addr(addr);
3212 static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3213 unsigned long prot, unsigned long flags)
3215 if (selinux_checkreqprot)
3218 return file_map_prot_check(file, prot,
3219 (flags & MAP_TYPE) == MAP_SHARED);
3222 static int selinux_file_mprotect(struct vm_area_struct *vma,
3223 unsigned long reqprot,
3226 const struct cred *cred = current_cred();
3228 if (selinux_checkreqprot)
3231 if (default_noexec &&
3232 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3234 if (vma->vm_start >= vma->vm_mm->start_brk &&
3235 vma->vm_end <= vma->vm_mm->brk) {
3236 rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
3237 } else if (!vma->vm_file &&
3238 vma->vm_start <= vma->vm_mm->start_stack &&
3239 vma->vm_end >= vma->vm_mm->start_stack) {
3240 rc = current_has_perm(current, PROCESS__EXECSTACK);
3241 } else if (vma->vm_file && vma->anon_vma) {
3243 * We are making executable a file mapping that has
3244 * had some COW done. Since pages might have been
3245 * written, check ability to execute the possibly
3246 * modified content. This typically should only
3247 * occur for text relocations.
3249 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
3255 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3258 static int selinux_file_lock(struct file *file, unsigned int cmd)
3260 const struct cred *cred = current_cred();
3262 return file_has_perm(cred, file, FILE__LOCK);
3265 static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3268 const struct cred *cred = current_cred();
3273 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
3274 err = file_has_perm(cred, file, FILE__WRITE);
3283 case F_GETOWNER_UIDS:
3284 /* Just check FD__USE permission */
3285 err = file_has_perm(cred, file, 0);
3290 #if BITS_PER_LONG == 32
3295 err = file_has_perm(cred, file, FILE__LOCK);
3302 static int selinux_file_set_fowner(struct file *file)
3304 struct file_security_struct *fsec;
3306 fsec = file->f_security;
3307 fsec->fown_sid = current_sid();
3312 static int selinux_file_send_sigiotask(struct task_struct *tsk,
3313 struct fown_struct *fown, int signum)
3316 u32 sid = task_sid(tsk);
3318 struct file_security_struct *fsec;
3320 /* struct fown_struct is never outside the context of a struct file */
3321 file = container_of(fown, struct file, f_owner);
3323 fsec = file->f_security;
3326 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3328 perm = signal_to_av(signum);
3330 return avc_has_perm(fsec->fown_sid, sid,
3331 SECCLASS_PROCESS, perm, NULL);
3334 static int selinux_file_receive(struct file *file)
3336 const struct cred *cred = current_cred();
3338 return file_has_perm(cred, file, file_to_av(file));
3341 static int selinux_file_open(struct file *file, const struct cred *cred)
3343 struct file_security_struct *fsec;
3344 struct inode_security_struct *isec;
3346 fsec = file->f_security;
3347 isec = file_inode(file)->i_security;
3349 * Save inode label and policy sequence number
3350 * at open-time so that selinux_file_permission
3351 * can determine whether revalidation is necessary.
3352 * Task label is already saved in the file security
3353 * struct as its SID.
3355 fsec->isid = isec->sid;
3356 fsec->pseqno = avc_policy_seqno();
3358 * Since the inode label or policy seqno may have changed
3359 * between the selinux_inode_permission check and the saving
3360 * of state above, recheck that access is still permitted.
3361 * Otherwise, access might never be revalidated against the
3362 * new inode label or new policy.
3363 * This check is not redundant - do not remove.
3365 return file_path_has_perm(cred, file, open_file_to_av(file));
3368 /* task security operations */
3370 static int selinux_task_create(unsigned long clone_flags)
3372 return current_has_perm(current, PROCESS__FORK);
3376 * allocate the SELinux part of blank credentials
3378 static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3380 struct task_security_struct *tsec;
3382 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3386 cred->security = tsec;
3391 * detach and free the LSM part of a set of credentials
3393 static void selinux_cred_free(struct cred *cred)
3395 struct task_security_struct *tsec = cred->security;
3398 * cred->security == NULL if security_cred_alloc_blank() or
3399 * security_prepare_creds() returned an error.
3401 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
3402 cred->security = (void *) 0x7UL;
3407 * prepare a new set of credentials for modification
3409 static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3412 const struct task_security_struct *old_tsec;
3413 struct task_security_struct *tsec;
3415 old_tsec = old->security;
3417 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3421 new->security = tsec;
3426 * transfer the SELinux data to a blank set of creds
3428 static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3430 const struct task_security_struct *old_tsec = old->security;
3431 struct task_security_struct *tsec = new->security;
3437 * set the security data for a kernel service
3438 * - all the creation contexts are set to unlabelled
3440 static int selinux_kernel_act_as(struct cred *new, u32 secid)
3442 struct task_security_struct *tsec = new->security;
3443 u32 sid = current_sid();
3446 ret = avc_has_perm(sid, secid,
3447 SECCLASS_KERNEL_SERVICE,
3448 KERNEL_SERVICE__USE_AS_OVERRIDE,
3452 tsec->create_sid = 0;
3453 tsec->keycreate_sid = 0;
3454 tsec->sockcreate_sid = 0;
3460 * set the file creation context in a security record to the same as the
3461 * objective context of the specified inode
3463 static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3465 struct inode_security_struct *isec = inode->i_security;
3466 struct task_security_struct *tsec = new->security;
3467 u32 sid = current_sid();
3470 ret = avc_has_perm(sid, isec->sid,
3471 SECCLASS_KERNEL_SERVICE,
3472 KERNEL_SERVICE__CREATE_FILES_AS,
3476 tsec->create_sid = isec->sid;
3480 static int selinux_kernel_module_request(char *kmod_name)
3483 struct common_audit_data ad;
3485 sid = task_sid(current);
3487 ad.type = LSM_AUDIT_DATA_KMOD;
3488 ad.u.kmod_name = kmod_name;
3490 return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3491 SYSTEM__MODULE_REQUEST, &ad);
3494 static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3496 return current_has_perm(p, PROCESS__SETPGID);
3499 static int selinux_task_getpgid(struct task_struct *p)
3501 return current_has_perm(p, PROCESS__GETPGID);
3504 static int selinux_task_getsid(struct task_struct *p)
3506 return current_has_perm(p, PROCESS__GETSESSION);
3509 static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3511 *secid = task_sid(p);
3514 static int selinux_task_setnice(struct task_struct *p, int nice)
3518 rc = cap_task_setnice(p, nice);
3522 return current_has_perm(p, PROCESS__SETSCHED);
3525 static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3529 rc = cap_task_setioprio(p, ioprio);
3533 return current_has_perm(p, PROCESS__SETSCHED);
3536 static int selinux_task_getioprio(struct task_struct *p)
3538 return current_has_perm(p, PROCESS__GETSCHED);
3541 static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3542 struct rlimit *new_rlim)
3544 struct rlimit *old_rlim = p->signal->rlim + resource;
3546 /* Control the ability to change the hard limit (whether
3547 lowering or raising it), so that the hard limit can
3548 later be used as a safe reset point for the soft limit
3549 upon context transitions. See selinux_bprm_committing_creds. */
3550 if (old_rlim->rlim_max != new_rlim->rlim_max)
3551 return current_has_perm(p, PROCESS__SETRLIMIT);
3556 static int selinux_task_setscheduler(struct task_struct *p)
3560 rc = cap_task_setscheduler(p);
3564 return current_has_perm(p, PROCESS__SETSCHED);
3567 static int selinux_task_getscheduler(struct task_struct *p)
3569 return current_has_perm(p, PROCESS__GETSCHED);
3572 static int selinux_task_movememory(struct task_struct *p)
3574 return current_has_perm(p, PROCESS__SETSCHED);
3577 static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3584 perm = PROCESS__SIGNULL; /* null signal; existence test */
3586 perm = signal_to_av(sig);
3588 rc = avc_has_perm(secid, task_sid(p),
3589 SECCLASS_PROCESS, perm, NULL);
3591 rc = current_has_perm(p, perm);
3595 static int selinux_task_wait(struct task_struct *p)
3597 return task_has_perm(p, current, PROCESS__SIGCHLD);
3600 static void selinux_task_to_inode(struct task_struct *p,
3601 struct inode *inode)
3603 struct inode_security_struct *isec = inode->i_security;
3604 u32 sid = task_sid(p);
3607 isec->initialized = 1;
3610 /* Returns error only if unable to parse addresses */
3611 static int selinux_parse_skb_ipv4(struct sk_buff *skb,
3612 struct common_audit_data *ad, u8 *proto)
3614 int offset, ihlen, ret = -EINVAL;
3615 struct iphdr _iph, *ih;
3617 offset = skb_network_offset(skb);
3618 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
3622 ihlen = ih->ihl * 4;
3623 if (ihlen < sizeof(_iph))
3626 ad->u.net->v4info.saddr = ih->saddr;
3627 ad->u.net->v4info.daddr = ih->daddr;
3631 *proto = ih->protocol;
3633 switch (ih->protocol) {
3635 struct tcphdr _tcph, *th;
3637 if (ntohs(ih->frag_off) & IP_OFFSET)
3641 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3645 ad->u.net->sport = th->source;
3646 ad->u.net->dport = th->dest;
3651 struct udphdr _udph, *uh;
3653 if (ntohs(ih->frag_off) & IP_OFFSET)
3657 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3661 ad->u.net->sport = uh->source;
3662 ad->u.net->dport = uh->dest;
3666 case IPPROTO_DCCP: {
3667 struct dccp_hdr _dccph, *dh;
3669 if (ntohs(ih->frag_off) & IP_OFFSET)
3673 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3677 ad->u.net->sport = dh->dccph_sport;
3678 ad->u.net->dport = dh->dccph_dport;
3689 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3691 /* Returns error only if unable to parse addresses */
3692 static int selinux_parse_skb_ipv6(struct sk_buff *skb,
3693 struct common_audit_data *ad, u8 *proto)
3696 int ret = -EINVAL, offset;
3697 struct ipv6hdr _ipv6h, *ip6;
3700 offset = skb_network_offset(skb);
3701 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3705 ad->u.net->v6info.saddr = ip6->saddr;
3706 ad->u.net->v6info.daddr = ip6->daddr;
3709 nexthdr = ip6->nexthdr;
3710 offset += sizeof(_ipv6h);
3711 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
3720 struct tcphdr _tcph, *th;
3722 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3726 ad->u.net->sport = th->source;
3727 ad->u.net->dport = th->dest;
3732 struct udphdr _udph, *uh;
3734 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3738 ad->u.net->sport = uh->source;
3739 ad->u.net->dport = uh->dest;
3743 case IPPROTO_DCCP: {
3744 struct dccp_hdr _dccph, *dh;
3746 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3750 ad->u.net->sport = dh->dccph_sport;
3751 ad->u.net->dport = dh->dccph_dport;
3755 /* includes fragments */
3765 static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
3766 char **_addrp, int src, u8 *proto)
3771 switch (ad->u.net->family) {
3773 ret = selinux_parse_skb_ipv4(skb, ad, proto);
3776 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
3777 &ad->u.net->v4info.daddr);
3780 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3782 ret = selinux_parse_skb_ipv6(skb, ad, proto);
3785 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
3786 &ad->u.net->v6info.daddr);
3796 "SELinux: failure in selinux_parse_skb(),"
3797 " unable to parse packet\n");
3807 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
3809 * @family: protocol family
3810 * @sid: the packet's peer label SID
3813 * Check the various different forms of network peer labeling and determine
3814 * the peer label/SID for the packet; most of the magic actually occurs in
3815 * the security server function security_net_peersid_cmp(). The function
3816 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3817 * or -EACCES if @sid is invalid due to inconsistencies with the different
3821 static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
3828 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
3831 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
3835 err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
3836 if (unlikely(err)) {
3838 "SELinux: failure in selinux_skb_peerlbl_sid(),"
3839 " unable to determine packet's peer label\n");
3847 * selinux_conn_sid - Determine the child socket label for a connection
3848 * @sk_sid: the parent socket's SID
3849 * @skb_sid: the packet's SID
3850 * @conn_sid: the resulting connection SID
3852 * If @skb_sid is valid then the user:role:type information from @sk_sid is
3853 * combined with the MLS information from @skb_sid in order to create
3854 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
3855 * of @sk_sid. Returns zero on success, negative values on failure.
3858 static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
3862 if (skb_sid != SECSID_NULL)
3863 err = security_sid_mls_copy(sk_sid, skb_sid, conn_sid);
3870 /* socket security operations */
3872 static int socket_sockcreate_sid(const struct task_security_struct *tsec,
3873 u16 secclass, u32 *socksid)
3875 if (tsec->sockcreate_sid > SECSID_NULL) {
3876 *socksid = tsec->sockcreate_sid;
3880 return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
3884 static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
3886 struct sk_security_struct *sksec = sk->sk_security;
3887 struct common_audit_data ad;
3888 struct lsm_network_audit net = {0,};
3889 u32 tsid = task_sid(task);
3891 if (sksec->sid == SECINITSID_KERNEL)
3894 ad.type = LSM_AUDIT_DATA_NET;
3898 return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
3901 static int selinux_socket_create(int family, int type,
3902 int protocol, int kern)
3904 const struct task_security_struct *tsec = current_security();
3912 secclass = socket_type_to_security_class(family, type, protocol);
3913 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
3917 return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
3920 static int selinux_socket_post_create(struct socket *sock, int family,
3921 int type, int protocol, int kern)
3923 const struct task_security_struct *tsec = current_security();
3924 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
3925 struct sk_security_struct *sksec;
3928 isec->sclass = socket_type_to_security_class(family, type, protocol);
3931 isec->sid = SECINITSID_KERNEL;
3933 err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
3938 isec->initialized = 1;
3941 sksec = sock->sk->sk_security;
3942 sksec->sid = isec->sid;
3943 sksec->sclass = isec->sclass;
3944 err = selinux_netlbl_socket_post_create(sock->sk, family);
3950 /* Range of port numbers used to automatically bind.
3951 Need to determine whether we should perform a name_bind
3952 permission check between the socket and the port number. */
3954 static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
3956 struct sock *sk = sock->sk;
3960 err = sock_has_perm(current, sk, SOCKET__BIND);
3965 * If PF_INET or PF_INET6, check name_bind permission for the port.
3966 * Multiple address binding for SCTP is not supported yet: we just
3967 * check the first address now.
3969 family = sk->sk_family;
3970 if (family == PF_INET || family == PF_INET6) {
3972 struct sk_security_struct *sksec = sk->sk_security;
3973 struct common_audit_data ad;
3974 struct lsm_network_audit net = {0,};
3975 struct sockaddr_in *addr4 = NULL;
3976 struct sockaddr_in6 *addr6 = NULL;
3977 unsigned short snum;
3980 if (family == PF_INET) {
3981 addr4 = (struct sockaddr_in *)address;
3982 snum = ntohs(addr4->sin_port);
3983 addrp = (char *)&addr4->sin_addr.s_addr;
3985 addr6 = (struct sockaddr_in6 *)address;
3986 snum = ntohs(addr6->sin6_port);
3987 addrp = (char *)&addr6->sin6_addr.s6_addr;
3993 inet_get_local_port_range(sock_net(sk), &low, &high);
3995 if (snum < max(PROT_SOCK, low) || snum > high) {
3996 err = sel_netport_sid(sk->sk_protocol,
4000 ad.type = LSM_AUDIT_DATA_NET;
4002 ad.u.net->sport = htons(snum);
4003 ad.u.net->family = family;
4004 err = avc_has_perm(sksec->sid, sid,
4006 SOCKET__NAME_BIND, &ad);
4012 switch (sksec->sclass) {
4013 case SECCLASS_TCP_SOCKET:
4014 node_perm = TCP_SOCKET__NODE_BIND;
4017 case SECCLASS_UDP_SOCKET:
4018 node_perm = UDP_SOCKET__NODE_BIND;
4021 case SECCLASS_DCCP_SOCKET:
4022 node_perm = DCCP_SOCKET__NODE_BIND;
4026 node_perm = RAWIP_SOCKET__NODE_BIND;
4030 err = sel_netnode_sid(addrp, family, &sid);
4034 ad.type = LSM_AUDIT_DATA_NET;
4036 ad.u.net->sport = htons(snum);
4037 ad.u.net->family = family;
4039 if (family == PF_INET)
4040 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
4042 ad.u.net->v6info.saddr = addr6->sin6_addr;
4044 err = avc_has_perm(sksec->sid, sid,
4045 sksec->sclass, node_perm, &ad);
4053 static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
4055 struct sock *sk = sock->sk;
4056 struct sk_security_struct *sksec = sk->sk_security;
4059 err = sock_has_perm(current, sk, SOCKET__CONNECT);
4064 * If a TCP or DCCP socket, check name_connect permission for the port.
4066 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4067 sksec->sclass == SECCLASS_DCCP_SOCKET) {
4068 struct common_audit_data ad;
4069 struct lsm_network_audit net = {0,};
4070 struct sockaddr_in *addr4 = NULL;
4071 struct sockaddr_in6 *addr6 = NULL;
4072 unsigned short snum;
4075 if (sk->sk_family == PF_INET) {
4076 addr4 = (struct sockaddr_in *)address;
4077 if (addrlen < sizeof(struct sockaddr_in))
4079 snum = ntohs(addr4->sin_port);
4081 addr6 = (struct sockaddr_in6 *)address;
4082 if (addrlen < SIN6_LEN_RFC2133)
4084 snum = ntohs(addr6->sin6_port);
4087 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
4091 perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
4092 TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
4094 ad.type = LSM_AUDIT_DATA_NET;
4096 ad.u.net->dport = htons(snum);
4097 ad.u.net->family = sk->sk_family;
4098 err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
4103 err = selinux_netlbl_socket_connect(sk, address);
4109 static int selinux_socket_listen(struct socket *sock, int backlog)
4111 return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
4114 static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4117 struct inode_security_struct *isec;
4118 struct inode_security_struct *newisec;
4120 err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
4124 newisec = SOCK_INODE(newsock)->i_security;
4126 isec = SOCK_INODE(sock)->i_security;
4127 newisec->sclass = isec->sclass;
4128 newisec->sid = isec->sid;
4129 newisec->initialized = 1;
4134 static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
4137 return sock_has_perm(current, sock->sk, SOCKET__WRITE);
4140 static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4141 int size, int flags)
4143 return sock_has_perm(current, sock->sk, SOCKET__READ);
4146 static int selinux_socket_getsockname(struct socket *sock)
4148 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
4151 static int selinux_socket_getpeername(struct socket *sock)
4153 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
4156 static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
4160 err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
4164 return selinux_netlbl_socket_setsockopt(sock, level, optname);
4167 static int selinux_socket_getsockopt(struct socket *sock, int level,
4170 return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
4173 static int selinux_socket_shutdown(struct socket *sock, int how)
4175 return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
4178 static int selinux_socket_unix_stream_connect(struct sock *sock,
4182 struct sk_security_struct *sksec_sock = sock->sk_security;
4183 struct sk_security_struct *sksec_other = other->sk_security;
4184 struct sk_security_struct *sksec_new = newsk->sk_security;
4185 struct common_audit_data ad;
4186 struct lsm_network_audit net = {0,};
4189 ad.type = LSM_AUDIT_DATA_NET;
4191 ad.u.net->sk = other;
4193 err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
4194 sksec_other->sclass,
4195 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4199 /* server child socket */
4200 sksec_new->peer_sid = sksec_sock->sid;
4201 err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
4206 /* connecting socket */
4207 sksec_sock->peer_sid = sksec_new->sid;
4212 static int selinux_socket_unix_may_send(struct socket *sock,
4213 struct socket *other)
4215 struct sk_security_struct *ssec = sock->sk->sk_security;
4216 struct sk_security_struct *osec = other->sk->sk_security;
4217 struct common_audit_data ad;
4218 struct lsm_network_audit net = {0,};
4220 ad.type = LSM_AUDIT_DATA_NET;
4222 ad.u.net->sk = other->sk;
4224 return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4228 static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
4230 struct common_audit_data *ad)
4236 err = sel_netif_sid(ifindex, &if_sid);
4239 err = avc_has_perm(peer_sid, if_sid,
4240 SECCLASS_NETIF, NETIF__INGRESS, ad);
4244 err = sel_netnode_sid(addrp, family, &node_sid);
4247 return avc_has_perm(peer_sid, node_sid,
4248 SECCLASS_NODE, NODE__RECVFROM, ad);
4251 static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
4255 struct sk_security_struct *sksec = sk->sk_security;
4256 u32 sk_sid = sksec->sid;
4257 struct common_audit_data ad;
4258 struct lsm_network_audit net = {0,};
4261 ad.type = LSM_AUDIT_DATA_NET;
4263 ad.u.net->netif = skb->skb_iif;
4264 ad.u.net->family = family;
4265 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4269 if (selinux_secmark_enabled()) {
4270 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4276 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4279 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
4284 static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4287 struct sk_security_struct *sksec = sk->sk_security;
4288 u16 family = sk->sk_family;
4289 u32 sk_sid = sksec->sid;
4290 struct common_audit_data ad;
4291 struct lsm_network_audit net = {0,};
4296 if (family != PF_INET && family != PF_INET6)
4299 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
4300 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4303 /* If any sort of compatibility mode is enabled then handoff processing
4304 * to the selinux_sock_rcv_skb_compat() function to deal with the
4305 * special handling. We do this in an attempt to keep this function
4306 * as fast and as clean as possible. */
4307 if (!selinux_policycap_netpeer)
4308 return selinux_sock_rcv_skb_compat(sk, skb, family);
4310 secmark_active = selinux_secmark_enabled();
4311 peerlbl_active = selinux_peerlbl_enabled();
4312 if (!secmark_active && !peerlbl_active)
4315 ad.type = LSM_AUDIT_DATA_NET;
4317 ad.u.net->netif = skb->skb_iif;
4318 ad.u.net->family = family;
4319 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4323 if (peerlbl_active) {
4326 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4329 err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family,
4332 selinux_netlbl_err(skb, err, 0);
4335 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4338 selinux_netlbl_err(skb, err, 0);
4343 if (secmark_active) {
4344 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4353 static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4354 int __user *optlen, unsigned len)
4359 struct sk_security_struct *sksec = sock->sk->sk_security;
4360 u32 peer_sid = SECSID_NULL;
4362 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4363 sksec->sclass == SECCLASS_TCP_SOCKET)
4364 peer_sid = sksec->peer_sid;
4365 if (peer_sid == SECSID_NULL)
4366 return -ENOPROTOOPT;
4368 err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
4372 if (scontext_len > len) {
4377 if (copy_to_user(optval, scontext, scontext_len))
4381 if (put_user(scontext_len, optlen))
4387 static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
4389 u32 peer_secid = SECSID_NULL;
4392 if (skb && skb->protocol == htons(ETH_P_IP))
4394 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4397 family = sock->sk->sk_family;
4401 if (sock && family == PF_UNIX)
4402 selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
4404 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
4407 *secid = peer_secid;
4408 if (peer_secid == SECSID_NULL)
4413 static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
4415 struct sk_security_struct *sksec;
4417 sksec = kzalloc(sizeof(*sksec), priority);
4421 sksec->peer_sid = SECINITSID_UNLABELED;
4422 sksec->sid = SECINITSID_UNLABELED;
4423 selinux_netlbl_sk_security_reset(sksec);
4424 sk->sk_security = sksec;
4429 static void selinux_sk_free_security(struct sock *sk)
4431 struct sk_security_struct *sksec = sk->sk_security;
4433 sk->sk_security = NULL;
4434 selinux_netlbl_sk_security_free(sksec);
4438 static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4440 struct sk_security_struct *sksec = sk->sk_security;
4441 struct sk_security_struct *newsksec = newsk->sk_security;
4443 newsksec->sid = sksec->sid;
4444 newsksec->peer_sid = sksec->peer_sid;
4445 newsksec->sclass = sksec->sclass;
4447 selinux_netlbl_sk_security_reset(newsksec);
4450 static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
4453 *secid = SECINITSID_ANY_SOCKET;
4455 struct sk_security_struct *sksec = sk->sk_security;
4457 *secid = sksec->sid;
4461 static void selinux_sock_graft(struct sock *sk, struct socket *parent)
4463 struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
4464 struct sk_security_struct *sksec = sk->sk_security;
4466 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
4467 sk->sk_family == PF_UNIX)
4468 isec->sid = sksec->sid;
4469 sksec->sclass = isec->sclass;
4472 static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4473 struct request_sock *req)
4475 struct sk_security_struct *sksec = sk->sk_security;
4477 u16 family = sk->sk_family;
4481 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4482 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4485 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
4488 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
4491 req->secid = connsid;
4492 req->peer_secid = peersid;
4494 return selinux_netlbl_inet_conn_request(req, family);
4497 static void selinux_inet_csk_clone(struct sock *newsk,
4498 const struct request_sock *req)
4500 struct sk_security_struct *newsksec = newsk->sk_security;
4502 newsksec->sid = req->secid;
4503 newsksec->peer_sid = req->peer_secid;
4504 /* NOTE: Ideally, we should also get the isec->sid for the
4505 new socket in sync, but we don't have the isec available yet.
4506 So we will wait until sock_graft to do it, by which
4507 time it will have been created and available. */
4509 /* We don't need to take any sort of lock here as we are the only
4510 * thread with access to newsksec */
4511 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
4514 static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
4516 u16 family = sk->sk_family;
4517 struct sk_security_struct *sksec = sk->sk_security;
4519 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4520 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4523 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
4526 static void selinux_skb_owned_by(struct sk_buff *skb, struct sock *sk)
4528 skb_set_owner_w(skb, sk);
4531 static int selinux_secmark_relabel_packet(u32 sid)
4533 const struct task_security_struct *__tsec;
4536 __tsec = current_security();
4539 return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
4542 static void selinux_secmark_refcount_inc(void)
4544 atomic_inc(&selinux_secmark_refcount);
4547 static void selinux_secmark_refcount_dec(void)
4549 atomic_dec(&selinux_secmark_refcount);
4552 static void selinux_req_classify_flow(const struct request_sock *req,
4555 fl->flowi_secid = req->secid;
4558 static int selinux_tun_dev_alloc_security(void **security)
4560 struct tun_security_struct *tunsec;
4562 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
4565 tunsec->sid = current_sid();
4571 static void selinux_tun_dev_free_security(void *security)
4576 static int selinux_tun_dev_create(void)
4578 u32 sid = current_sid();
4580 /* we aren't taking into account the "sockcreate" SID since the socket
4581 * that is being created here is not a socket in the traditional sense,
4582 * instead it is a private sock, accessible only to the kernel, and
4583 * representing a wide range of network traffic spanning multiple
4584 * connections unlike traditional sockets - check the TUN driver to
4585 * get a better understanding of why this socket is special */
4587 return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4591 static int selinux_tun_dev_attach_queue(void *security)
4593 struct tun_security_struct *tunsec = security;
4595 return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
4596 TUN_SOCKET__ATTACH_QUEUE, NULL);
4599 static int selinux_tun_dev_attach(struct sock *sk, void *security)
4601 struct tun_security_struct *tunsec = security;
4602 struct sk_security_struct *sksec = sk->sk_security;
4604 /* we don't currently perform any NetLabel based labeling here and it
4605 * isn't clear that we would want to do so anyway; while we could apply
4606 * labeling without the support of the TUN user the resulting labeled
4607 * traffic from the other end of the connection would almost certainly
4608 * cause confusion to the TUN user that had no idea network labeling
4609 * protocols were being used */
4611 sksec->sid = tunsec->sid;
4612 sksec->sclass = SECCLASS_TUN_SOCKET;
4617 static int selinux_tun_dev_open(void *security)
4619 struct tun_security_struct *tunsec = security;
4620 u32 sid = current_sid();
4623 err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
4624 TUN_SOCKET__RELABELFROM, NULL);
4627 err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
4628 TUN_SOCKET__RELABELTO, NULL);
4636 static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
4640 struct nlmsghdr *nlh;
4641 struct sk_security_struct *sksec = sk->sk_security;
4643 if (skb->len < NLMSG_HDRLEN) {
4647 nlh = nlmsg_hdr(skb);
4649 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
4651 if (err == -EINVAL) {
4652 audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
4653 "SELinux: unrecognized netlink message"
4654 " type=%hu for sclass=%hu\n",
4655 nlh->nlmsg_type, sksec->sclass);
4656 if (!selinux_enforcing || security_get_allow_unknown())
4666 err = sock_has_perm(current, sk, perm);
4671 #ifdef CONFIG_NETFILTER
4673 static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4679 struct common_audit_data ad;
4680 struct lsm_network_audit net = {0,};
4685 if (!selinux_policycap_netpeer)
4688 secmark_active = selinux_secmark_enabled();
4689 netlbl_active = netlbl_enabled();
4690 peerlbl_active = selinux_peerlbl_enabled();
4691 if (!secmark_active && !peerlbl_active)
4694 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4697 ad.type = LSM_AUDIT_DATA_NET;
4699 ad.u.net->netif = ifindex;
4700 ad.u.net->family = family;
4701 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4704 if (peerlbl_active) {
4705 err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4708 selinux_netlbl_err(skb, err, 1);
4714 if (avc_has_perm(peer_sid, skb->secmark,
4715 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4719 /* we do this in the FORWARD path and not the POST_ROUTING
4720 * path because we want to make sure we apply the necessary
4721 * labeling before IPsec is applied so we can leverage AH
4723 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4729 static unsigned int selinux_ipv4_forward(const struct nf_hook_ops *ops,
4730 struct sk_buff *skb,
4731 const struct net_device *in,
4732 const struct net_device *out,
4733 int (*okfn)(struct sk_buff *))
4735 return selinux_ip_forward(skb, in->ifindex, PF_INET);
4738 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4739 static unsigned int selinux_ipv6_forward(const struct nf_hook_ops *ops,
4740 struct sk_buff *skb,
4741 const struct net_device *in,
4742 const struct net_device *out,
4743 int (*okfn)(struct sk_buff *))
4745 return selinux_ip_forward(skb, in->ifindex, PF_INET6);
4749 static unsigned int selinux_ip_output(struct sk_buff *skb,
4755 if (!netlbl_enabled())
4758 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4759 * because we want to make sure we apply the necessary labeling
4760 * before IPsec is applied so we can leverage AH protection */
4763 struct sk_security_struct *sksec;
4765 if (sk->sk_state == TCP_LISTEN)
4766 /* if the socket is the listening state then this
4767 * packet is a SYN-ACK packet which means it needs to
4768 * be labeled based on the connection/request_sock and
4769 * not the parent socket. unfortunately, we can't
4770 * lookup the request_sock yet as it isn't queued on
4771 * the parent socket until after the SYN-ACK is sent.
4772 * the "solution" is to simply pass the packet as-is
4773 * as any IP option based labeling should be copied
4774 * from the initial connection request (in the IP
4775 * layer). it is far from ideal, but until we get a
4776 * security label in the packet itself this is the
4777 * best we can do. */
4780 /* standard practice, label using the parent socket */
4781 sksec = sk->sk_security;
4784 sid = SECINITSID_KERNEL;
4785 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4791 static unsigned int selinux_ipv4_output(const struct nf_hook_ops *ops,
4792 struct sk_buff *skb,
4793 const struct net_device *in,
4794 const struct net_device *out,
4795 int (*okfn)(struct sk_buff *))
4797 return selinux_ip_output(skb, PF_INET);
4800 static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4804 struct sock *sk = skb->sk;
4805 struct sk_security_struct *sksec;
4806 struct common_audit_data ad;
4807 struct lsm_network_audit net = {0,};
4813 sksec = sk->sk_security;
4815 ad.type = LSM_AUDIT_DATA_NET;
4817 ad.u.net->netif = ifindex;
4818 ad.u.net->family = family;
4819 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4822 if (selinux_secmark_enabled())
4823 if (avc_has_perm(sksec->sid, skb->secmark,
4824 SECCLASS_PACKET, PACKET__SEND, &ad))
4825 return NF_DROP_ERR(-ECONNREFUSED);
4827 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
4828 return NF_DROP_ERR(-ECONNREFUSED);
4833 static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4839 struct common_audit_data ad;
4840 struct lsm_network_audit net = {0,};
4845 /* If any sort of compatibility mode is enabled then handoff processing
4846 * to the selinux_ip_postroute_compat() function to deal with the
4847 * special handling. We do this in an attempt to keep this function
4848 * as fast and as clean as possible. */
4849 if (!selinux_policycap_netpeer)
4850 return selinux_ip_postroute_compat(skb, ifindex, family);
4852 secmark_active = selinux_secmark_enabled();
4853 peerlbl_active = selinux_peerlbl_enabled();
4854 if (!secmark_active && !peerlbl_active)
4860 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4861 * packet transformation so allow the packet to pass without any checks
4862 * since we'll have another chance to perform access control checks
4863 * when the packet is on it's final way out.
4864 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4865 * is NULL, in this case go ahead and apply access control.
4866 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
4867 * TCP listening state we cannot wait until the XFRM processing
4868 * is done as we will miss out on the SA label if we do;
4869 * unfortunately, this means more work, but it is only once per
4871 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
4872 !(sk != NULL && sk->sk_state == TCP_LISTEN))
4877 /* Without an associated socket the packet is either coming
4878 * from the kernel or it is being forwarded; check the packet
4879 * to determine which and if the packet is being forwarded
4880 * query the packet directly to determine the security label. */
4882 secmark_perm = PACKET__FORWARD_OUT;
4883 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
4886 secmark_perm = PACKET__SEND;
4887 peer_sid = SECINITSID_KERNEL;
4889 } else if (sk->sk_state == TCP_LISTEN) {
4890 /* Locally generated packet but the associated socket is in the
4891 * listening state which means this is a SYN-ACK packet. In
4892 * this particular case the correct security label is assigned
4893 * to the connection/request_sock but unfortunately we can't
4894 * query the request_sock as it isn't queued on the parent
4895 * socket until after the SYN-ACK packet is sent; the only
4896 * viable choice is to regenerate the label like we do in
4897 * selinux_inet_conn_request(). See also selinux_ip_output()
4898 * for similar problems. */
4900 struct sk_security_struct *sksec = sk->sk_security;
4901 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
4903 /* At this point, if the returned skb peerlbl is SECSID_NULL
4904 * and the packet has been through at least one XFRM
4905 * transformation then we must be dealing with the "final"
4906 * form of labeled IPsec packet; since we've already applied
4907 * all of our access controls on this packet we can safely
4908 * pass the packet. */
4909 if (skb_sid == SECSID_NULL) {
4912 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
4916 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
4919 return NF_DROP_ERR(-ECONNREFUSED);
4922 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
4924 secmark_perm = PACKET__SEND;
4926 /* Locally generated packet, fetch the security label from the
4927 * associated socket. */
4928 struct sk_security_struct *sksec = sk->sk_security;
4929 peer_sid = sksec->sid;
4930 secmark_perm = PACKET__SEND;
4933 ad.type = LSM_AUDIT_DATA_NET;
4935 ad.u.net->netif = ifindex;
4936 ad.u.net->family = family;
4937 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
4941 if (avc_has_perm(peer_sid, skb->secmark,
4942 SECCLASS_PACKET, secmark_perm, &ad))
4943 return NF_DROP_ERR(-ECONNREFUSED);
4945 if (peerlbl_active) {
4949 if (sel_netif_sid(ifindex, &if_sid))
4951 if (avc_has_perm(peer_sid, if_sid,
4952 SECCLASS_NETIF, NETIF__EGRESS, &ad))
4953 return NF_DROP_ERR(-ECONNREFUSED);
4955 if (sel_netnode_sid(addrp, family, &node_sid))
4957 if (avc_has_perm(peer_sid, node_sid,
4958 SECCLASS_NODE, NODE__SENDTO, &ad))
4959 return NF_DROP_ERR(-ECONNREFUSED);
4965 static unsigned int selinux_ipv4_postroute(const struct nf_hook_ops *ops,
4966 struct sk_buff *skb,
4967 const struct net_device *in,
4968 const struct net_device *out,
4969 int (*okfn)(struct sk_buff *))
4971 return selinux_ip_postroute(skb, out->ifindex, PF_INET);
4974 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4975 static unsigned int selinux_ipv6_postroute(const struct nf_hook_ops *ops,
4976 struct sk_buff *skb,
4977 const struct net_device *in,
4978 const struct net_device *out,
4979 int (*okfn)(struct sk_buff *))
4981 return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
4985 #endif /* CONFIG_NETFILTER */
4987 static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
4991 err = cap_netlink_send(sk, skb);
4995 return selinux_nlmsg_perm(sk, skb);
4998 static int ipc_alloc_security(struct task_struct *task,
4999 struct kern_ipc_perm *perm,
5002 struct ipc_security_struct *isec;
5005 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
5009 sid = task_sid(task);
5010 isec->sclass = sclass;
5012 perm->security = isec;
5017 static void ipc_free_security(struct kern_ipc_perm *perm)
5019 struct ipc_security_struct *isec = perm->security;
5020 perm->security = NULL;
5024 static int msg_msg_alloc_security(struct msg_msg *msg)
5026 struct msg_security_struct *msec;
5028 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
5032 msec->sid = SECINITSID_UNLABELED;
5033 msg->security = msec;
5038 static void msg_msg_free_security(struct msg_msg *msg)
5040 struct msg_security_struct *msec = msg->security;
5042 msg->security = NULL;
5046 static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
5049 struct ipc_security_struct *isec;
5050 struct common_audit_data ad;
5051 u32 sid = current_sid();
5053 isec = ipc_perms->security;
5055 ad.type = LSM_AUDIT_DATA_IPC;
5056 ad.u.ipc_id = ipc_perms->key;
5058 return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
5061 static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5063 return msg_msg_alloc_security(msg);
5066 static void selinux_msg_msg_free_security(struct msg_msg *msg)
5068 msg_msg_free_security(msg);
5071 /* message queue security operations */
5072 static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
5074 struct ipc_security_struct *isec;
5075 struct common_audit_data ad;
5076 u32 sid = current_sid();
5079 rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
5083 isec = msq->q_perm.security;
5085 ad.type = LSM_AUDIT_DATA_IPC;
5086 ad.u.ipc_id = msq->q_perm.key;
5088 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
5091 ipc_free_security(&msq->q_perm);
5097 static void selinux_msg_queue_free_security(struct msg_queue *msq)
5099 ipc_free_security(&msq->q_perm);
5102 static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
5104 struct ipc_security_struct *isec;
5105 struct common_audit_data ad;
5106 u32 sid = current_sid();
5108 isec = msq->q_perm.security;
5110 ad.type = LSM_AUDIT_DATA_IPC;
5111 ad.u.ipc_id = msq->q_perm.key;
5113 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
5114 MSGQ__ASSOCIATE, &ad);
5117 static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
5125 /* No specific object, just general system-wide information. */
5126 return task_has_system(current, SYSTEM__IPC_INFO);
5129 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5132 perms = MSGQ__SETATTR;
5135 perms = MSGQ__DESTROY;
5141 err = ipc_has_perm(&msq->q_perm, perms);
5145 static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
5147 struct ipc_security_struct *isec;
5148 struct msg_security_struct *msec;
5149 struct common_audit_data ad;
5150 u32 sid = current_sid();
5153 isec = msq->q_perm.security;
5154 msec = msg->security;
5157 * First time through, need to assign label to the message
5159 if (msec->sid == SECINITSID_UNLABELED) {
5161 * Compute new sid based on current process and
5162 * message queue this message will be stored in
5164 rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
5170 ad.type = LSM_AUDIT_DATA_IPC;
5171 ad.u.ipc_id = msq->q_perm.key;
5173 /* Can this process write to the queue? */
5174 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
5177 /* Can this process send the message */
5178 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
5181 /* Can the message be put in the queue? */
5182 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5183 MSGQ__ENQUEUE, &ad);
5188 static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
5189 struct task_struct *target,
5190 long type, int mode)
5192 struct ipc_security_struct *isec;
5193 struct msg_security_struct *msec;
5194 struct common_audit_data ad;
5195 u32 sid = task_sid(target);
5198 isec = msq->q_perm.security;
5199 msec = msg->security;
5201 ad.type = LSM_AUDIT_DATA_IPC;
5202 ad.u.ipc_id = msq->q_perm.key;
5204 rc = avc_has_perm(sid, isec->sid,
5205 SECCLASS_MSGQ, MSGQ__READ, &ad);
5207 rc = avc_has_perm(sid, msec->sid,
5208 SECCLASS_MSG, MSG__RECEIVE, &ad);
5212 /* Shared Memory security operations */
5213 static int selinux_shm_alloc_security(struct shmid_kernel *shp)
5215 struct ipc_security_struct *isec;
5216 struct common_audit_data ad;
5217 u32 sid = current_sid();
5220 rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
5224 isec = shp->shm_perm.security;
5226 ad.type = LSM_AUDIT_DATA_IPC;
5227 ad.u.ipc_id = shp->shm_perm.key;
5229 rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
5232 ipc_free_security(&shp->shm_perm);
5238 static void selinux_shm_free_security(struct shmid_kernel *shp)
5240 ipc_free_security(&shp->shm_perm);
5243 static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
5245 struct ipc_security_struct *isec;
5246 struct common_audit_data ad;
5247 u32 sid = current_sid();
5249 isec = shp->shm_perm.security;
5251 ad.type = LSM_AUDIT_DATA_IPC;
5252 ad.u.ipc_id = shp->shm_perm.key;
5254 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
5255 SHM__ASSOCIATE, &ad);
5258 /* Note, at this point, shp is locked down */
5259 static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
5267 /* No specific object, just general system-wide information. */
5268 return task_has_system(current, SYSTEM__IPC_INFO);
5271 perms = SHM__GETATTR | SHM__ASSOCIATE;
5274 perms = SHM__SETATTR;
5281 perms = SHM__DESTROY;
5287 err = ipc_has_perm(&shp->shm_perm, perms);
5291 static int selinux_shm_shmat(struct shmid_kernel *shp,
5292 char __user *shmaddr, int shmflg)
5296 if (shmflg & SHM_RDONLY)
5299 perms = SHM__READ | SHM__WRITE;
5301 return ipc_has_perm(&shp->shm_perm, perms);
5304 /* Semaphore security operations */
5305 static int selinux_sem_alloc_security(struct sem_array *sma)
5307 struct ipc_security_struct *isec;
5308 struct common_audit_data ad;
5309 u32 sid = current_sid();
5312 rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
5316 isec = sma->sem_perm.security;
5318 ad.type = LSM_AUDIT_DATA_IPC;
5319 ad.u.ipc_id = sma->sem_perm.key;
5321 rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
5324 ipc_free_security(&sma->sem_perm);
5330 static void selinux_sem_free_security(struct sem_array *sma)
5332 ipc_free_security(&sma->sem_perm);
5335 static int selinux_sem_associate(struct sem_array *sma, int semflg)
5337 struct ipc_security_struct *isec;
5338 struct common_audit_data ad;
5339 u32 sid = current_sid();
5341 isec = sma->sem_perm.security;
5343 ad.type = LSM_AUDIT_DATA_IPC;
5344 ad.u.ipc_id = sma->sem_perm.key;
5346 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
5347 SEM__ASSOCIATE, &ad);
5350 /* Note, at this point, sma is locked down */
5351 static int selinux_sem_semctl(struct sem_array *sma, int cmd)
5359 /* No specific object, just general system-wide information. */
5360 return task_has_system(current, SYSTEM__IPC_INFO);
5364 perms = SEM__GETATTR;
5375 perms = SEM__DESTROY;
5378 perms = SEM__SETATTR;
5382 perms = SEM__GETATTR | SEM__ASSOCIATE;
5388 err = ipc_has_perm(&sma->sem_perm, perms);
5392 static int selinux_sem_semop(struct sem_array *sma,
5393 struct sembuf *sops, unsigned nsops, int alter)
5398 perms = SEM__READ | SEM__WRITE;
5402 return ipc_has_perm(&sma->sem_perm, perms);
5405 static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
5411 av |= IPC__UNIX_READ;
5413 av |= IPC__UNIX_WRITE;
5418 return ipc_has_perm(ipcp, av);
5421 static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5423 struct ipc_security_struct *isec = ipcp->security;
5427 static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
5430 inode_doinit_with_dentry(inode, dentry);
5433 static int selinux_getprocattr(struct task_struct *p,
5434 char *name, char **value)
5436 const struct task_security_struct *__tsec;
5442 error = current_has_perm(p, PROCESS__GETATTR);
5448 __tsec = __task_cred(p)->security;
5450 if (!strcmp(name, "current"))
5452 else if (!strcmp(name, "prev"))
5454 else if (!strcmp(name, "exec"))
5455 sid = __tsec->exec_sid;
5456 else if (!strcmp(name, "fscreate"))
5457 sid = __tsec->create_sid;
5458 else if (!strcmp(name, "keycreate"))
5459 sid = __tsec->keycreate_sid;
5460 else if (!strcmp(name, "sockcreate"))
5461 sid = __tsec->sockcreate_sid;
5469 error = security_sid_to_context(sid, value, &len);
5479 static int selinux_setprocattr(struct task_struct *p,
5480 char *name, void *value, size_t size)
5482 struct task_security_struct *tsec;
5483 struct task_struct *tracer;
5490 /* SELinux only allows a process to change its own
5491 security attributes. */
5496 * Basic control over ability to set these attributes at all.
5497 * current == p, but we'll pass them separately in case the
5498 * above restriction is ever removed.
5500 if (!strcmp(name, "exec"))
5501 error = current_has_perm(p, PROCESS__SETEXEC);
5502 else if (!strcmp(name, "fscreate"))
5503 error = current_has_perm(p, PROCESS__SETFSCREATE);
5504 else if (!strcmp(name, "keycreate"))
5505 error = current_has_perm(p, PROCESS__SETKEYCREATE);
5506 else if (!strcmp(name, "sockcreate"))
5507 error = current_has_perm(p, PROCESS__SETSOCKCREATE);
5508 else if (!strcmp(name, "current"))
5509 error = current_has_perm(p, PROCESS__SETCURRENT);
5515 /* Obtain a SID for the context, if one was specified. */
5516 if (size && str[1] && str[1] != '\n') {
5517 if (str[size-1] == '\n') {
5521 error = security_context_to_sid(value, size, &sid);
5522 if (error == -EINVAL && !strcmp(name, "fscreate")) {
5523 if (!capable(CAP_MAC_ADMIN)) {
5524 struct audit_buffer *ab;
5527 /* We strip a nul only if it is at the end, otherwise the
5528 * context contains a nul and we should audit that */
5529 if (str[size - 1] == '\0')
5530 audit_size = size - 1;
5533 ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
5534 audit_log_format(ab, "op=fscreate invalid_context=");
5535 audit_log_n_untrustedstring(ab, value, audit_size);
5540 error = security_context_to_sid_force(value, size,
5547 new = prepare_creds();
5551 /* Permission checking based on the specified context is
5552 performed during the actual operation (execve,
5553 open/mkdir/...), when we know the full context of the
5554 operation. See selinux_bprm_set_creds for the execve
5555 checks and may_create for the file creation checks. The
5556 operation will then fail if the context is not permitted. */
5557 tsec = new->security;
5558 if (!strcmp(name, "exec")) {
5559 tsec->exec_sid = sid;
5560 } else if (!strcmp(name, "fscreate")) {
5561 tsec->create_sid = sid;
5562 } else if (!strcmp(name, "keycreate")) {
5563 error = may_create_key(sid, p);
5566 tsec->keycreate_sid = sid;
5567 } else if (!strcmp(name, "sockcreate")) {
5568 tsec->sockcreate_sid = sid;
5569 } else if (!strcmp(name, "current")) {
5574 /* Only allow single threaded processes to change context */
5576 if (!current_is_single_threaded()) {
5577 error = security_bounded_transition(tsec->sid, sid);
5582 /* Check permissions for the transition. */
5583 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
5584 PROCESS__DYNTRANSITION, NULL);
5588 /* Check for ptracing, and update the task SID if ok.
5589 Otherwise, leave SID unchanged and fail. */
5592 tracer = ptrace_parent(p);
5594 ptsid = task_sid(tracer);
5598 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5599 PROCESS__PTRACE, NULL);
5618 static int selinux_ismaclabel(const char *name)
5620 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
5623 static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5625 return security_sid_to_context(secid, secdata, seclen);
5628 static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
5630 return security_context_to_sid(secdata, seclen, secid);
5633 static void selinux_release_secctx(char *secdata, u32 seclen)
5639 * called with inode->i_mutex locked
5641 static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
5643 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
5647 * called with inode->i_mutex locked
5649 static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
5651 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
5654 static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
5657 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
5666 static int selinux_key_alloc(struct key *k, const struct cred *cred,
5667 unsigned long flags)
5669 const struct task_security_struct *tsec;
5670 struct key_security_struct *ksec;
5672 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5676 tsec = cred->security;
5677 if (tsec->keycreate_sid)
5678 ksec->sid = tsec->keycreate_sid;
5680 ksec->sid = tsec->sid;
5686 static void selinux_key_free(struct key *k)
5688 struct key_security_struct *ksec = k->security;
5694 static int selinux_key_permission(key_ref_t key_ref,
5695 const struct cred *cred,
5699 struct key_security_struct *ksec;
5702 /* if no specific permissions are requested, we skip the
5703 permission check. No serious, additional covert channels
5704 appear to be created. */
5708 sid = cred_sid(cred);
5710 key = key_ref_to_ptr(key_ref);
5711 ksec = key->security;
5713 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
5716 static int selinux_key_getsecurity(struct key *key, char **_buffer)
5718 struct key_security_struct *ksec = key->security;
5719 char *context = NULL;
5723 rc = security_sid_to_context(ksec->sid, &context, &len);
5732 static struct security_operations selinux_ops = {
5735 .ptrace_access_check = selinux_ptrace_access_check,
5736 .ptrace_traceme = selinux_ptrace_traceme,
5737 .capget = selinux_capget,
5738 .capset = selinux_capset,
5739 .capable = selinux_capable,
5740 .quotactl = selinux_quotactl,
5741 .quota_on = selinux_quota_on,
5742 .syslog = selinux_syslog,
5743 .vm_enough_memory = selinux_vm_enough_memory,
5745 .netlink_send = selinux_netlink_send,
5747 .bprm_set_creds = selinux_bprm_set_creds,
5748 .bprm_committing_creds = selinux_bprm_committing_creds,
5749 .bprm_committed_creds = selinux_bprm_committed_creds,
5750 .bprm_secureexec = selinux_bprm_secureexec,
5752 .sb_alloc_security = selinux_sb_alloc_security,
5753 .sb_free_security = selinux_sb_free_security,
5754 .sb_copy_data = selinux_sb_copy_data,
5755 .sb_remount = selinux_sb_remount,
5756 .sb_kern_mount = selinux_sb_kern_mount,
5757 .sb_show_options = selinux_sb_show_options,
5758 .sb_statfs = selinux_sb_statfs,
5759 .sb_mount = selinux_mount,
5760 .sb_umount = selinux_umount,
5761 .sb_set_mnt_opts = selinux_set_mnt_opts,
5762 .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts,
5763 .sb_parse_opts_str = selinux_parse_opts_str,
5765 .dentry_init_security = selinux_dentry_init_security,
5767 .inode_alloc_security = selinux_inode_alloc_security,
5768 .inode_free_security = selinux_inode_free_security,
5769 .inode_init_security = selinux_inode_init_security,
5770 .inode_create = selinux_inode_create,
5771 .inode_link = selinux_inode_link,
5772 .inode_unlink = selinux_inode_unlink,
5773 .inode_symlink = selinux_inode_symlink,
5774 .inode_mkdir = selinux_inode_mkdir,
5775 .inode_rmdir = selinux_inode_rmdir,
5776 .inode_mknod = selinux_inode_mknod,
5777 .inode_rename = selinux_inode_rename,
5778 .inode_readlink = selinux_inode_readlink,
5779 .inode_follow_link = selinux_inode_follow_link,
5780 .inode_permission = selinux_inode_permission,
5781 .inode_setattr = selinux_inode_setattr,
5782 .inode_getattr = selinux_inode_getattr,
5783 .inode_setxattr = selinux_inode_setxattr,
5784 .inode_post_setxattr = selinux_inode_post_setxattr,
5785 .inode_getxattr = selinux_inode_getxattr,
5786 .inode_listxattr = selinux_inode_listxattr,
5787 .inode_removexattr = selinux_inode_removexattr,
5788 .inode_getsecurity = selinux_inode_getsecurity,
5789 .inode_setsecurity = selinux_inode_setsecurity,
5790 .inode_listsecurity = selinux_inode_listsecurity,
5791 .inode_getsecid = selinux_inode_getsecid,
5793 .file_permission = selinux_file_permission,
5794 .file_alloc_security = selinux_file_alloc_security,
5795 .file_free_security = selinux_file_free_security,
5796 .file_ioctl = selinux_file_ioctl,
5797 .mmap_file = selinux_mmap_file,
5798 .mmap_addr = selinux_mmap_addr,
5799 .file_mprotect = selinux_file_mprotect,
5800 .file_lock = selinux_file_lock,
5801 .file_fcntl = selinux_file_fcntl,
5802 .file_set_fowner = selinux_file_set_fowner,
5803 .file_send_sigiotask = selinux_file_send_sigiotask,
5804 .file_receive = selinux_file_receive,
5806 .file_open = selinux_file_open,
5808 .task_create = selinux_task_create,
5809 .cred_alloc_blank = selinux_cred_alloc_blank,
5810 .cred_free = selinux_cred_free,
5811 .cred_prepare = selinux_cred_prepare,
5812 .cred_transfer = selinux_cred_transfer,
5813 .kernel_act_as = selinux_kernel_act_as,
5814 .kernel_create_files_as = selinux_kernel_create_files_as,
5815 .kernel_module_request = selinux_kernel_module_request,
5816 .task_setpgid = selinux_task_setpgid,
5817 .task_getpgid = selinux_task_getpgid,
5818 .task_getsid = selinux_task_getsid,
5819 .task_getsecid = selinux_task_getsecid,
5820 .task_setnice = selinux_task_setnice,
5821 .task_setioprio = selinux_task_setioprio,
5822 .task_getioprio = selinux_task_getioprio,
5823 .task_setrlimit = selinux_task_setrlimit,
5824 .task_setscheduler = selinux_task_setscheduler,
5825 .task_getscheduler = selinux_task_getscheduler,
5826 .task_movememory = selinux_task_movememory,
5827 .task_kill = selinux_task_kill,
5828 .task_wait = selinux_task_wait,
5829 .task_to_inode = selinux_task_to_inode,
5831 .ipc_permission = selinux_ipc_permission,
5832 .ipc_getsecid = selinux_ipc_getsecid,
5834 .msg_msg_alloc_security = selinux_msg_msg_alloc_security,
5835 .msg_msg_free_security = selinux_msg_msg_free_security,
5837 .msg_queue_alloc_security = selinux_msg_queue_alloc_security,
5838 .msg_queue_free_security = selinux_msg_queue_free_security,
5839 .msg_queue_associate = selinux_msg_queue_associate,
5840 .msg_queue_msgctl = selinux_msg_queue_msgctl,
5841 .msg_queue_msgsnd = selinux_msg_queue_msgsnd,
5842 .msg_queue_msgrcv = selinux_msg_queue_msgrcv,
5844 .shm_alloc_security = selinux_shm_alloc_security,
5845 .shm_free_security = selinux_shm_free_security,
5846 .shm_associate = selinux_shm_associate,
5847 .shm_shmctl = selinux_shm_shmctl,
5848 .shm_shmat = selinux_shm_shmat,
5850 .sem_alloc_security = selinux_sem_alloc_security,
5851 .sem_free_security = selinux_sem_free_security,
5852 .sem_associate = selinux_sem_associate,
5853 .sem_semctl = selinux_sem_semctl,
5854 .sem_semop = selinux_sem_semop,
5856 .d_instantiate = selinux_d_instantiate,
5858 .getprocattr = selinux_getprocattr,
5859 .setprocattr = selinux_setprocattr,
5861 .ismaclabel = selinux_ismaclabel,
5862 .secid_to_secctx = selinux_secid_to_secctx,
5863 .secctx_to_secid = selinux_secctx_to_secid,
5864 .release_secctx = selinux_release_secctx,
5865 .inode_notifysecctx = selinux_inode_notifysecctx,
5866 .inode_setsecctx = selinux_inode_setsecctx,
5867 .inode_getsecctx = selinux_inode_getsecctx,
5869 .unix_stream_connect = selinux_socket_unix_stream_connect,
5870 .unix_may_send = selinux_socket_unix_may_send,
5872 .socket_create = selinux_socket_create,
5873 .socket_post_create = selinux_socket_post_create,
5874 .socket_bind = selinux_socket_bind,
5875 .socket_connect = selinux_socket_connect,
5876 .socket_listen = selinux_socket_listen,
5877 .socket_accept = selinux_socket_accept,
5878 .socket_sendmsg = selinux_socket_sendmsg,
5879 .socket_recvmsg = selinux_socket_recvmsg,
5880 .socket_getsockname = selinux_socket_getsockname,
5881 .socket_getpeername = selinux_socket_getpeername,
5882 .socket_getsockopt = selinux_socket_getsockopt,
5883 .socket_setsockopt = selinux_socket_setsockopt,
5884 .socket_shutdown = selinux_socket_shutdown,
5885 .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb,
5886 .socket_getpeersec_stream = selinux_socket_getpeersec_stream,
5887 .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram,
5888 .sk_alloc_security = selinux_sk_alloc_security,
5889 .sk_free_security = selinux_sk_free_security,
5890 .sk_clone_security = selinux_sk_clone_security,
5891 .sk_getsecid = selinux_sk_getsecid,
5892 .sock_graft = selinux_sock_graft,
5893 .inet_conn_request = selinux_inet_conn_request,
5894 .inet_csk_clone = selinux_inet_csk_clone,
5895 .inet_conn_established = selinux_inet_conn_established,
5896 .secmark_relabel_packet = selinux_secmark_relabel_packet,
5897 .secmark_refcount_inc = selinux_secmark_refcount_inc,
5898 .secmark_refcount_dec = selinux_secmark_refcount_dec,
5899 .req_classify_flow = selinux_req_classify_flow,
5900 .tun_dev_alloc_security = selinux_tun_dev_alloc_security,
5901 .tun_dev_free_security = selinux_tun_dev_free_security,
5902 .tun_dev_create = selinux_tun_dev_create,
5903 .tun_dev_attach_queue = selinux_tun_dev_attach_queue,
5904 .tun_dev_attach = selinux_tun_dev_attach,
5905 .tun_dev_open = selinux_tun_dev_open,
5906 .skb_owned_by = selinux_skb_owned_by,
5908 #ifdef CONFIG_SECURITY_NETWORK_XFRM
5909 .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc,
5910 .xfrm_policy_clone_security = selinux_xfrm_policy_clone,
5911 .xfrm_policy_free_security = selinux_xfrm_policy_free,
5912 .xfrm_policy_delete_security = selinux_xfrm_policy_delete,
5913 .xfrm_state_alloc = selinux_xfrm_state_alloc,
5914 .xfrm_state_alloc_acquire = selinux_xfrm_state_alloc_acquire,
5915 .xfrm_state_free_security = selinux_xfrm_state_free,
5916 .xfrm_state_delete_security = selinux_xfrm_state_delete,
5917 .xfrm_policy_lookup = selinux_xfrm_policy_lookup,
5918 .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match,
5919 .xfrm_decode_session = selinux_xfrm_decode_session,
5923 .key_alloc = selinux_key_alloc,
5924 .key_free = selinux_key_free,
5925 .key_permission = selinux_key_permission,
5926 .key_getsecurity = selinux_key_getsecurity,
5930 .audit_rule_init = selinux_audit_rule_init,
5931 .audit_rule_known = selinux_audit_rule_known,
5932 .audit_rule_match = selinux_audit_rule_match,
5933 .audit_rule_free = selinux_audit_rule_free,
5937 static __init int selinux_init(void)
5939 if (!security_module_enable(&selinux_ops)) {
5940 selinux_enabled = 0;
5944 if (!selinux_enabled) {
5945 printk(KERN_INFO "SELinux: Disabled at boot.\n");
5949 printk(KERN_INFO "SELinux: Initializing.\n");
5951 /* Set the security state for the initial task. */
5952 cred_init_security();
5954 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
5956 sel_inode_cache = kmem_cache_create("selinux_inode_security",
5957 sizeof(struct inode_security_struct),
5958 0, SLAB_PANIC, NULL);
5961 if (register_security(&selinux_ops))
5962 panic("SELinux: Unable to register with kernel.\n");
5964 if (selinux_enforcing)
5965 printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n");
5967 printk(KERN_DEBUG "SELinux: Starting in permissive mode\n");
5972 static void delayed_superblock_init(struct super_block *sb, void *unused)
5974 superblock_doinit(sb, NULL);
5977 void selinux_complete_init(void)
5979 printk(KERN_DEBUG "SELinux: Completing initialization.\n");
5981 /* Set up any superblocks initialized prior to the policy load. */
5982 printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n");
5983 iterate_supers(delayed_superblock_init, NULL);
5986 /* SELinux requires early initialization in order to label
5987 all processes and objects when they are created. */
5988 security_initcall(selinux_init);
5990 #if defined(CONFIG_NETFILTER)
5992 static struct nf_hook_ops selinux_ipv4_ops[] = {
5994 .hook = selinux_ipv4_postroute,
5995 .owner = THIS_MODULE,
5997 .hooknum = NF_INET_POST_ROUTING,
5998 .priority = NF_IP_PRI_SELINUX_LAST,
6001 .hook = selinux_ipv4_forward,
6002 .owner = THIS_MODULE,
6004 .hooknum = NF_INET_FORWARD,
6005 .priority = NF_IP_PRI_SELINUX_FIRST,
6008 .hook = selinux_ipv4_output,
6009 .owner = THIS_MODULE,
6011 .hooknum = NF_INET_LOCAL_OUT,
6012 .priority = NF_IP_PRI_SELINUX_FIRST,
6016 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6018 static struct nf_hook_ops selinux_ipv6_ops[] = {
6020 .hook = selinux_ipv6_postroute,
6021 .owner = THIS_MODULE,
6023 .hooknum = NF_INET_POST_ROUTING,
6024 .priority = NF_IP6_PRI_SELINUX_LAST,
6027 .hook = selinux_ipv6_forward,
6028 .owner = THIS_MODULE,
6030 .hooknum = NF_INET_FORWARD,
6031 .priority = NF_IP6_PRI_SELINUX_FIRST,
6037 static int __init selinux_nf_ip_init(void)
6041 if (!selinux_enabled)
6044 printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n");
6046 err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
6048 panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
6050 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6051 err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
6053 panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
6060 __initcall(selinux_nf_ip_init);
6062 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
6063 static void selinux_nf_ip_exit(void)
6065 printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n");
6067 nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
6068 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6069 nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
6074 #else /* CONFIG_NETFILTER */
6076 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
6077 #define selinux_nf_ip_exit()
6080 #endif /* CONFIG_NETFILTER */
6082 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
6083 static int selinux_disabled;
6085 int selinux_disable(void)
6087 if (ss_initialized) {
6088 /* Not permitted after initial policy load. */
6092 if (selinux_disabled) {
6093 /* Only do this once. */
6097 printk(KERN_INFO "SELinux: Disabled at runtime.\n");
6099 selinux_disabled = 1;
6100 selinux_enabled = 0;
6102 reset_security_ops();
6104 /* Try to destroy the avc node cache */
6107 /* Unregister netfilter hooks. */
6108 selinux_nf_ip_exit();
6110 /* Unregister selinuxfs. */