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/msg.h>
86 #include <linux/shm.h>
88 // [ SEC_SELINUX_PORTING COMMON
89 #include <linux/delay.h>
90 // ] SEC_SELINUX_PORTING COMMON
102 #define NUM_SEL_MNT_OPTS 5
104 extern struct security_operations
*security_ops
;
106 /* SECMARK reference count */
107 static atomic_t selinux_secmark_refcount
= ATOMIC_INIT(0);
109 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
110 int selinux_enforcing
;
112 static int __init
enforcing_setup(char *str
)
114 unsigned long enforcing
;
115 if (!strict_strtoul(str
, 0, &enforcing
))
116 selinux_enforcing
= enforcing
? 1 : 0;
120 __setup("enforcing=", enforcing_setup
);
123 #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
124 int selinux_enabled
= CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE
;
126 static int __init
selinux_enabled_setup(char *str
)
128 unsigned long enabled
;
129 if (!strict_strtoul(str
, 0, &enabled
))
130 selinux_enabled
= enabled
? 1 : 0;
134 __setup("selinux=", selinux_enabled_setup
);
136 int selinux_enabled
= 1;
139 static struct kmem_cache
*sel_inode_cache
;
142 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
145 * This function checks the SECMARK reference counter to see if any SECMARK
146 * targets are currently configured, if the reference counter is greater than
147 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
148 * enabled, false (0) if SECMARK is disabled.
151 static int selinux_secmark_enabled(void)
153 return (atomic_read(&selinux_secmark_refcount
) > 0);
157 * initialise the security for the init task
159 static void cred_init_security(void)
161 struct cred
*cred
= (struct cred
*) current
->real_cred
;
162 struct task_security_struct
*tsec
;
164 tsec
= kzalloc(sizeof(struct task_security_struct
), GFP_KERNEL
);
166 panic("SELinux: Failed to initialize initial task.\n");
168 tsec
->osid
= tsec
->sid
= SECINITSID_KERNEL
;
169 cred
->security
= tsec
;
173 * get the security ID of a set of credentials
175 static inline u32
cred_sid(const struct cred
*cred
)
177 const struct task_security_struct
*tsec
;
179 tsec
= cred
->security
;
184 * get the objective security ID of a task
186 static inline u32
task_sid(const struct task_struct
*task
)
191 sid
= cred_sid(__task_cred(task
));
197 * get the subjective security ID of the current task
199 static inline u32
current_sid(void)
201 const struct task_security_struct
*tsec
= current_security();
206 /* Allocate and free functions for each kind of security blob. */
208 static int inode_alloc_security(struct inode
*inode
)
210 struct inode_security_struct
*isec
;
211 u32 sid
= current_sid();
213 isec
= kmem_cache_zalloc(sel_inode_cache
, GFP_NOFS
);
217 mutex_init(&isec
->lock
);
218 INIT_LIST_HEAD(&isec
->list
);
220 isec
->sid
= SECINITSID_UNLABELED
;
221 isec
->sclass
= SECCLASS_FILE
;
222 isec
->task_sid
= sid
;
223 inode
->i_security
= isec
;
228 static void inode_free_rcu(struct rcu_head
*head
)
230 struct inode_security_struct
*isec
;
232 isec
= container_of(head
, struct inode_security_struct
, rcu
);
233 kmem_cache_free(sel_inode_cache
, isec
);
236 static void inode_free_security(struct inode
*inode
)
238 struct inode_security_struct
*isec
= inode
->i_security
;
239 struct superblock_security_struct
*sbsec
= inode
->i_sb
->s_security
;
241 spin_lock(&sbsec
->isec_lock
);
242 if (!list_empty(&isec
->list
))
243 list_del_init(&isec
->list
);
244 spin_unlock(&sbsec
->isec_lock
);
247 * The inode may still be referenced in a path walk and
248 * a call to selinux_inode_permission() can be made
249 * after inode_free_security() is called. Ideally, the VFS
250 * wouldn't do this, but fixing that is a much harder
251 * job. For now, simply free the i_security via RCU, and
252 * leave the current inode->i_security pointer intact.
253 * The inode will be freed after the RCU grace period too.
255 call_rcu(&isec
->rcu
, inode_free_rcu
);
258 static int file_alloc_security(struct file
*file
)
260 struct file_security_struct
*fsec
;
261 u32 sid
= current_sid();
263 fsec
= kzalloc(sizeof(struct file_security_struct
), GFP_KERNEL
);
268 fsec
->fown_sid
= sid
;
269 file
->f_security
= fsec
;
274 static void file_free_security(struct file
*file
)
276 struct file_security_struct
*fsec
= file
->f_security
;
277 file
->f_security
= NULL
;
281 static int superblock_alloc_security(struct super_block
*sb
)
283 struct superblock_security_struct
*sbsec
;
285 sbsec
= kzalloc(sizeof(struct superblock_security_struct
), GFP_KERNEL
);
289 mutex_init(&sbsec
->lock
);
290 INIT_LIST_HEAD(&sbsec
->isec_head
);
291 spin_lock_init(&sbsec
->isec_lock
);
293 sbsec
->sid
= SECINITSID_UNLABELED
;
294 sbsec
->def_sid
= SECINITSID_FILE
;
295 sbsec
->mntpoint_sid
= SECINITSID_UNLABELED
;
296 sb
->s_security
= sbsec
;
301 static void superblock_free_security(struct super_block
*sb
)
303 struct superblock_security_struct
*sbsec
= sb
->s_security
;
304 sb
->s_security
= NULL
;
308 /* The file system's label must be initialized prior to use. */
310 static const char *labeling_behaviors
[6] = {
312 "uses transition SIDs",
314 "uses genfs_contexts",
315 "not configured for labeling",
316 "uses mountpoint labeling",
319 static int inode_doinit_with_dentry(struct inode
*inode
, struct dentry
*opt_dentry
);
321 static inline int inode_doinit(struct inode
*inode
)
323 return inode_doinit_with_dentry(inode
, NULL
);
332 Opt_labelsupport
= 5,
335 static const match_table_t tokens
= {
336 {Opt_context
, CONTEXT_STR
"%s"},
337 {Opt_fscontext
, FSCONTEXT_STR
"%s"},
338 {Opt_defcontext
, DEFCONTEXT_STR
"%s"},
339 {Opt_rootcontext
, ROOTCONTEXT_STR
"%s"},
340 {Opt_labelsupport
, LABELSUPP_STR
},
344 #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
346 static int may_context_mount_sb_relabel(u32 sid
,
347 struct superblock_security_struct
*sbsec
,
348 const struct cred
*cred
)
350 const struct task_security_struct
*tsec
= cred
->security
;
353 rc
= avc_has_perm(tsec
->sid
, sbsec
->sid
, SECCLASS_FILESYSTEM
,
354 FILESYSTEM__RELABELFROM
, NULL
);
358 rc
= avc_has_perm(tsec
->sid
, sid
, SECCLASS_FILESYSTEM
,
359 FILESYSTEM__RELABELTO
, NULL
);
363 static int may_context_mount_inode_relabel(u32 sid
,
364 struct superblock_security_struct
*sbsec
,
365 const struct cred
*cred
)
367 const struct task_security_struct
*tsec
= cred
->security
;
369 rc
= avc_has_perm(tsec
->sid
, sbsec
->sid
, SECCLASS_FILESYSTEM
,
370 FILESYSTEM__RELABELFROM
, NULL
);
374 rc
= avc_has_perm(sid
, sbsec
->sid
, SECCLASS_FILESYSTEM
,
375 FILESYSTEM__ASSOCIATE
, NULL
);
379 static int sb_finish_set_opts(struct super_block
*sb
)
381 struct superblock_security_struct
*sbsec
= sb
->s_security
;
382 struct dentry
*root
= sb
->s_root
;
383 struct inode
*root_inode
= root
->d_inode
;
386 if (sbsec
->behavior
== SECURITY_FS_USE_XATTR
) {
387 /* Make sure that the xattr handler exists and that no
388 error other than -ENODATA is returned by getxattr on
389 the root directory. -ENODATA is ok, as this may be
390 the first boot of the SELinux kernel before we have
391 assigned xattr values to the filesystem. */
392 if (!root_inode
->i_op
->getxattr
) {
393 printk(KERN_WARNING
"SELinux: (dev %s, type %s) has no "
394 "xattr support\n", sb
->s_id
, sb
->s_type
->name
);
398 rc
= root_inode
->i_op
->getxattr(root
, XATTR_NAME_SELINUX
, NULL
, 0);
399 if (rc
< 0 && rc
!= -ENODATA
) {
400 if (rc
== -EOPNOTSUPP
)
401 printk(KERN_WARNING
"SELinux: (dev %s, type "
402 "%s) has no security xattr handler\n",
403 sb
->s_id
, sb
->s_type
->name
);
405 printk(KERN_WARNING
"SELinux: (dev %s, type "
406 "%s) getxattr errno %d\n", sb
->s_id
,
407 sb
->s_type
->name
, -rc
);
412 sbsec
->flags
|= (SE_SBINITIALIZED
| SE_SBLABELSUPP
);
414 if (sbsec
->behavior
> ARRAY_SIZE(labeling_behaviors
))
415 printk(KERN_ERR
"SELinux: initialized (dev %s, type %s), unknown behavior\n",
416 sb
->s_id
, sb
->s_type
->name
);
418 printk(KERN_DEBUG
"SELinux: initialized (dev %s, type %s), %s\n",
419 sb
->s_id
, sb
->s_type
->name
,
420 labeling_behaviors
[sbsec
->behavior
-1]);
422 if (sbsec
->behavior
== SECURITY_FS_USE_GENFS
||
423 sbsec
->behavior
== SECURITY_FS_USE_MNTPOINT
||
424 sbsec
->behavior
== SECURITY_FS_USE_NONE
||
425 sbsec
->behavior
> ARRAY_SIZE(labeling_behaviors
))
426 sbsec
->flags
&= ~SE_SBLABELSUPP
;
428 /* Special handling. Is genfs but also has in-core setxattr handler*/
429 if (!strcmp(sb
->s_type
->name
, "sysfs") ||
430 !strcmp(sb
->s_type
->name
, "pstore") ||
431 !strcmp(sb
->s_type
->name
, "debugfs") ||
432 !strcmp(sb
->s_type
->name
, "tmpfs") ||
433 !strcmp(sb
->s_type
->name
, "rootfs"))
434 sbsec
->flags
|= SE_SBLABELSUPP
;
436 /* Initialize the root inode. */
437 rc
= inode_doinit_with_dentry(root_inode
, root
);
439 /* Initialize any other inodes associated with the superblock, e.g.
440 inodes created prior to initial policy load or inodes created
441 during get_sb by a pseudo filesystem that directly
443 spin_lock(&sbsec
->isec_lock
);
445 if (!list_empty(&sbsec
->isec_head
)) {
446 struct inode_security_struct
*isec
=
447 list_entry(sbsec
->isec_head
.next
,
448 struct inode_security_struct
, list
);
449 struct inode
*inode
= isec
->inode
;
450 list_del_init(&isec
->list
);
451 spin_unlock(&sbsec
->isec_lock
);
452 inode
= igrab(inode
);
454 if (!IS_PRIVATE(inode
))
458 spin_lock(&sbsec
->isec_lock
);
461 spin_unlock(&sbsec
->isec_lock
);
467 * This function should allow an FS to ask what it's mount security
468 * options were so it can use those later for submounts, displaying
469 * mount options, or whatever.
471 static int selinux_get_mnt_opts(const struct super_block
*sb
,
472 struct security_mnt_opts
*opts
)
475 struct superblock_security_struct
*sbsec
= sb
->s_security
;
476 char *context
= NULL
;
480 security_init_mnt_opts(opts
);
482 if (!(sbsec
->flags
& SE_SBINITIALIZED
))
488 tmp
= sbsec
->flags
& SE_MNTMASK
;
489 /* count the number of mount options for this sb */
490 for (i
= 0; i
< 8; i
++) {
492 opts
->num_mnt_opts
++;
495 /* Check if the Label support flag is set */
496 if (sbsec
->flags
& SE_SBLABELSUPP
)
497 opts
->num_mnt_opts
++;
499 opts
->mnt_opts
= kcalloc(opts
->num_mnt_opts
, sizeof(char *), GFP_ATOMIC
);
500 if (!opts
->mnt_opts
) {
505 opts
->mnt_opts_flags
= kcalloc(opts
->num_mnt_opts
, sizeof(int), GFP_ATOMIC
);
506 if (!opts
->mnt_opts_flags
) {
512 if (sbsec
->flags
& FSCONTEXT_MNT
) {
513 rc
= security_sid_to_context(sbsec
->sid
, &context
, &len
);
516 opts
->mnt_opts
[i
] = context
;
517 opts
->mnt_opts_flags
[i
++] = FSCONTEXT_MNT
;
519 if (sbsec
->flags
& CONTEXT_MNT
) {
520 rc
= security_sid_to_context(sbsec
->mntpoint_sid
, &context
, &len
);
523 opts
->mnt_opts
[i
] = context
;
524 opts
->mnt_opts_flags
[i
++] = CONTEXT_MNT
;
526 if (sbsec
->flags
& DEFCONTEXT_MNT
) {
527 rc
= security_sid_to_context(sbsec
->def_sid
, &context
, &len
);
530 opts
->mnt_opts
[i
] = context
;
531 opts
->mnt_opts_flags
[i
++] = DEFCONTEXT_MNT
;
533 if (sbsec
->flags
& ROOTCONTEXT_MNT
) {
534 struct inode
*root
= sbsec
->sb
->s_root
->d_inode
;
535 struct inode_security_struct
*isec
= root
->i_security
;
537 rc
= security_sid_to_context(isec
->sid
, &context
, &len
);
540 opts
->mnt_opts
[i
] = context
;
541 opts
->mnt_opts_flags
[i
++] = ROOTCONTEXT_MNT
;
543 if (sbsec
->flags
& SE_SBLABELSUPP
) {
544 opts
->mnt_opts
[i
] = NULL
;
545 opts
->mnt_opts_flags
[i
++] = SE_SBLABELSUPP
;
548 BUG_ON(i
!= opts
->num_mnt_opts
);
553 security_free_mnt_opts(opts
);
557 static int bad_option(struct superblock_security_struct
*sbsec
, char flag
,
558 u32 old_sid
, u32 new_sid
)
560 char mnt_flags
= sbsec
->flags
& SE_MNTMASK
;
562 /* check if the old mount command had the same options */
563 if (sbsec
->flags
& SE_SBINITIALIZED
)
564 if (!(sbsec
->flags
& flag
) ||
565 (old_sid
!= new_sid
))
568 /* check if we were passed the same options twice,
569 * aka someone passed context=a,context=b
571 if (!(sbsec
->flags
& SE_SBINITIALIZED
))
572 if (mnt_flags
& flag
)
578 * Allow filesystems with binary mount data to explicitly set mount point
579 * labeling information.
581 static int selinux_set_mnt_opts(struct super_block
*sb
,
582 struct security_mnt_opts
*opts
)
584 const struct cred
*cred
= current_cred();
586 struct superblock_security_struct
*sbsec
= sb
->s_security
;
587 const char *name
= sb
->s_type
->name
;
588 struct inode
*inode
= sbsec
->sb
->s_root
->d_inode
;
589 struct inode_security_struct
*root_isec
= inode
->i_security
;
590 u32 fscontext_sid
= 0, context_sid
= 0, rootcontext_sid
= 0;
591 u32 defcontext_sid
= 0;
592 char **mount_options
= opts
->mnt_opts
;
593 int *flags
= opts
->mnt_opts_flags
;
594 int num_opts
= opts
->num_mnt_opts
;
596 mutex_lock(&sbsec
->lock
);
598 if (!ss_initialized
) {
600 /* Defer initialization until selinux_complete_init,
601 after the initial policy is loaded and the security
602 server is ready to handle calls. */
606 printk(KERN_WARNING
"SELinux: Unable to set superblock options "
607 "before the security server is initialized\n");
612 * Binary mount data FS will come through this function twice. Once
613 * from an explicit call and once from the generic calls from the vfs.
614 * Since the generic VFS calls will not contain any security mount data
615 * we need to skip the double mount verification.
617 * This does open a hole in which we will not notice if the first
618 * mount using this sb set explict options and a second mount using
619 * this sb does not set any security options. (The first options
620 * will be used for both mounts)
622 if ((sbsec
->flags
& SE_SBINITIALIZED
) && (sb
->s_type
->fs_flags
& FS_BINARY_MOUNTDATA
)
627 * parse the mount options, check if they are valid sids.
628 * also check if someone is trying to mount the same sb more
629 * than once with different security options.
631 for (i
= 0; i
< num_opts
; i
++) {
634 if (flags
[i
] == SE_SBLABELSUPP
)
636 rc
= security_context_to_sid(mount_options
[i
],
637 strlen(mount_options
[i
]), &sid
);
639 printk(KERN_WARNING
"SELinux: security_context_to_sid"
640 "(%s) failed for (dev %s, type %s) errno=%d\n",
641 mount_options
[i
], sb
->s_id
, name
, rc
);
648 if (bad_option(sbsec
, FSCONTEXT_MNT
, sbsec
->sid
,
650 goto out_double_mount
;
652 sbsec
->flags
|= FSCONTEXT_MNT
;
657 if (bad_option(sbsec
, CONTEXT_MNT
, sbsec
->mntpoint_sid
,
659 goto out_double_mount
;
661 sbsec
->flags
|= CONTEXT_MNT
;
663 case ROOTCONTEXT_MNT
:
664 rootcontext_sid
= sid
;
666 if (bad_option(sbsec
, ROOTCONTEXT_MNT
, root_isec
->sid
,
668 goto out_double_mount
;
670 sbsec
->flags
|= ROOTCONTEXT_MNT
;
674 defcontext_sid
= sid
;
676 if (bad_option(sbsec
, DEFCONTEXT_MNT
, sbsec
->def_sid
,
678 goto out_double_mount
;
680 sbsec
->flags
|= DEFCONTEXT_MNT
;
689 if (sbsec
->flags
& SE_SBINITIALIZED
) {
690 /* previously mounted with options, but not on this attempt? */
691 if ((sbsec
->flags
& SE_MNTMASK
) && !num_opts
)
692 goto out_double_mount
;
697 if (strcmp(sb
->s_type
->name
, "proc") == 0)
698 sbsec
->flags
|= SE_SBPROC
;
700 /* Determine the labeling behavior to use for this filesystem type. */
701 rc
= security_fs_use((sbsec
->flags
& SE_SBPROC
) ? "proc" : sb
->s_type
->name
, &sbsec
->behavior
, &sbsec
->sid
);
703 printk(KERN_WARNING
"%s: security_fs_use(%s) returned %d\n",
704 __func__
, sb
->s_type
->name
, rc
);
708 /* sets the context of the superblock for the fs being mounted. */
710 rc
= may_context_mount_sb_relabel(fscontext_sid
, sbsec
, cred
);
714 sbsec
->sid
= fscontext_sid
;
718 * Switch to using mount point labeling behavior.
719 * sets the label used on all file below the mountpoint, and will set
720 * the superblock context if not already set.
723 if (!fscontext_sid
) {
724 rc
= may_context_mount_sb_relabel(context_sid
, sbsec
,
728 sbsec
->sid
= context_sid
;
730 rc
= may_context_mount_inode_relabel(context_sid
, sbsec
,
735 if (!rootcontext_sid
)
736 rootcontext_sid
= context_sid
;
738 sbsec
->mntpoint_sid
= context_sid
;
739 sbsec
->behavior
= SECURITY_FS_USE_MNTPOINT
;
742 if (rootcontext_sid
) {
743 rc
= may_context_mount_inode_relabel(rootcontext_sid
, sbsec
,
748 root_isec
->sid
= rootcontext_sid
;
749 root_isec
->initialized
= 1;
752 if (defcontext_sid
) {
753 if (sbsec
->behavior
!= SECURITY_FS_USE_XATTR
) {
755 printk(KERN_WARNING
"SELinux: defcontext option is "
756 "invalid for this filesystem type\n");
760 if (defcontext_sid
!= sbsec
->def_sid
) {
761 rc
= may_context_mount_inode_relabel(defcontext_sid
,
767 sbsec
->def_sid
= defcontext_sid
;
770 rc
= sb_finish_set_opts(sb
);
772 mutex_unlock(&sbsec
->lock
);
776 printk(KERN_WARNING
"SELinux: mount invalid. Same superblock, different "
777 "security settings for (dev %s, type %s)\n", sb
->s_id
, name
);
781 static int selinux_cmp_sb_context(const struct super_block
*oldsb
,
782 const struct super_block
*newsb
)
784 struct superblock_security_struct
*old
= oldsb
->s_security
;
785 struct superblock_security_struct
*new = newsb
->s_security
;
786 char oldflags
= old
->flags
& SE_MNTMASK
;
787 char newflags
= new->flags
& SE_MNTMASK
;
789 if (oldflags
!= newflags
)
791 if ((oldflags
& FSCONTEXT_MNT
) && old
->sid
!= new->sid
)
793 if ((oldflags
& CONTEXT_MNT
) && old
->mntpoint_sid
!= new->mntpoint_sid
)
795 if ((oldflags
& DEFCONTEXT_MNT
) && old
->def_sid
!= new->def_sid
)
797 if (oldflags
& ROOTCONTEXT_MNT
) {
798 struct inode_security_struct
*oldroot
= oldsb
->s_root
->d_inode
->i_security
;
799 struct inode_security_struct
*newroot
= newsb
->s_root
->d_inode
->i_security
;
800 if (oldroot
->sid
!= newroot
->sid
)
805 printk(KERN_WARNING
"SELinux: mount invalid. Same superblock, "
806 "different security settings for (dev %s, "
807 "type %s)\n", newsb
->s_id
, newsb
->s_type
->name
);
811 static int selinux_sb_clone_mnt_opts(const struct super_block
*oldsb
,
812 struct super_block
*newsb
)
814 const struct superblock_security_struct
*oldsbsec
= oldsb
->s_security
;
815 struct superblock_security_struct
*newsbsec
= newsb
->s_security
;
817 int set_fscontext
= (oldsbsec
->flags
& FSCONTEXT_MNT
);
818 int set_context
= (oldsbsec
->flags
& CONTEXT_MNT
);
819 int set_rootcontext
= (oldsbsec
->flags
& ROOTCONTEXT_MNT
);
822 * if the parent was able to be mounted it clearly had no special lsm
823 * mount options. thus we can safely deal with this superblock later
828 /* how can we clone if the old one wasn't set up?? */
829 BUG_ON(!(oldsbsec
->flags
& SE_SBINITIALIZED
));
831 /* if fs is reusing a sb, make sure that the contexts match */
832 if (newsbsec
->flags
& SE_SBINITIALIZED
)
833 return selinux_cmp_sb_context(oldsb
, newsb
);
835 mutex_lock(&newsbsec
->lock
);
837 newsbsec
->flags
= oldsbsec
->flags
;
839 newsbsec
->sid
= oldsbsec
->sid
;
840 newsbsec
->def_sid
= oldsbsec
->def_sid
;
841 newsbsec
->behavior
= oldsbsec
->behavior
;
844 u32 sid
= oldsbsec
->mntpoint_sid
;
848 if (!set_rootcontext
) {
849 struct inode
*newinode
= newsb
->s_root
->d_inode
;
850 struct inode_security_struct
*newisec
= newinode
->i_security
;
853 newsbsec
->mntpoint_sid
= sid
;
855 if (set_rootcontext
) {
856 const struct inode
*oldinode
= oldsb
->s_root
->d_inode
;
857 const struct inode_security_struct
*oldisec
= oldinode
->i_security
;
858 struct inode
*newinode
= newsb
->s_root
->d_inode
;
859 struct inode_security_struct
*newisec
= newinode
->i_security
;
861 newisec
->sid
= oldisec
->sid
;
864 sb_finish_set_opts(newsb
);
865 mutex_unlock(&newsbsec
->lock
);
869 static int selinux_parse_opts_str(char *options
,
870 struct security_mnt_opts
*opts
)
873 char *context
= NULL
, *defcontext
= NULL
;
874 char *fscontext
= NULL
, *rootcontext
= NULL
;
875 int rc
, num_mnt_opts
= 0;
876 opts
->num_mnt_opts
= 0;
878 /* Standard string-based options. */
879 while ((p
= strsep(&options
, "|")) != NULL
) {
881 substring_t args
[MAX_OPT_ARGS
];
886 token
= match_token(p
, tokens
, args
);
890 if (context
|| defcontext
) {
892 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG
);
895 context
= match_strdup(&args
[0]);
905 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG
);
908 fscontext
= match_strdup(&args
[0]);
915 case Opt_rootcontext
:
918 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG
);
921 rootcontext
= match_strdup(&args
[0]);
929 if (context
|| defcontext
) {
931 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG
);
934 defcontext
= match_strdup(&args
[0]);
940 case Opt_labelsupport
:
944 printk(KERN_WARNING
"SELinux: unknown mount option\n");
951 opts
->mnt_opts
= kcalloc(NUM_SEL_MNT_OPTS
, sizeof(char *), GFP_ATOMIC
);
955 opts
->mnt_opts_flags
= kcalloc(NUM_SEL_MNT_OPTS
, sizeof(int), GFP_ATOMIC
);
956 if (!opts
->mnt_opts_flags
) {
957 kfree(opts
->mnt_opts
);
962 opts
->mnt_opts
[num_mnt_opts
] = fscontext
;
963 opts
->mnt_opts_flags
[num_mnt_opts
++] = FSCONTEXT_MNT
;
966 opts
->mnt_opts
[num_mnt_opts
] = context
;
967 opts
->mnt_opts_flags
[num_mnt_opts
++] = CONTEXT_MNT
;
970 opts
->mnt_opts
[num_mnt_opts
] = rootcontext
;
971 opts
->mnt_opts_flags
[num_mnt_opts
++] = ROOTCONTEXT_MNT
;
974 opts
->mnt_opts
[num_mnt_opts
] = defcontext
;
975 opts
->mnt_opts_flags
[num_mnt_opts
++] = DEFCONTEXT_MNT
;
978 opts
->num_mnt_opts
= num_mnt_opts
;
989 * string mount options parsing and call set the sbsec
991 static int superblock_doinit(struct super_block
*sb
, void *data
)
994 char *options
= data
;
995 struct security_mnt_opts opts
;
997 security_init_mnt_opts(&opts
);
1002 BUG_ON(sb
->s_type
->fs_flags
& FS_BINARY_MOUNTDATA
);
1004 rc
= selinux_parse_opts_str(options
, &opts
);
1009 rc
= selinux_set_mnt_opts(sb
, &opts
);
1012 security_free_mnt_opts(&opts
);
1016 static void selinux_write_opts(struct seq_file
*m
,
1017 struct security_mnt_opts
*opts
)
1022 for (i
= 0; i
< opts
->num_mnt_opts
; i
++) {
1025 if (opts
->mnt_opts
[i
])
1026 has_comma
= strchr(opts
->mnt_opts
[i
], ',');
1030 switch (opts
->mnt_opts_flags
[i
]) {
1032 prefix
= CONTEXT_STR
;
1035 prefix
= FSCONTEXT_STR
;
1037 case ROOTCONTEXT_MNT
:
1038 prefix
= ROOTCONTEXT_STR
;
1040 case DEFCONTEXT_MNT
:
1041 prefix
= DEFCONTEXT_STR
;
1043 case SE_SBLABELSUPP
:
1045 seq_puts(m
, LABELSUPP_STR
);
1051 /* we need a comma before each option */
1053 seq_puts(m
, prefix
);
1056 seq_puts(m
, opts
->mnt_opts
[i
]);
1062 static int selinux_sb_show_options(struct seq_file
*m
, struct super_block
*sb
)
1064 struct security_mnt_opts opts
;
1067 rc
= selinux_get_mnt_opts(sb
, &opts
);
1069 /* before policy load we may get EINVAL, don't show anything */
1075 selinux_write_opts(m
, &opts
);
1077 security_free_mnt_opts(&opts
);
1082 static inline u16
inode_mode_to_security_class(umode_t mode
)
1084 switch (mode
& S_IFMT
) {
1086 return SECCLASS_SOCK_FILE
;
1088 return SECCLASS_LNK_FILE
;
1090 return SECCLASS_FILE
;
1092 return SECCLASS_BLK_FILE
;
1094 return SECCLASS_DIR
;
1096 return SECCLASS_CHR_FILE
;
1098 return SECCLASS_FIFO_FILE
;
1102 return SECCLASS_FILE
;
1105 static inline int default_protocol_stream(int protocol
)
1107 return (protocol
== IPPROTO_IP
|| protocol
== IPPROTO_TCP
);
1110 static inline int default_protocol_dgram(int protocol
)
1112 return (protocol
== IPPROTO_IP
|| protocol
== IPPROTO_UDP
);
1115 static inline u16
socket_type_to_security_class(int family
, int type
, int protocol
)
1121 case SOCK_SEQPACKET
:
1122 return SECCLASS_UNIX_STREAM_SOCKET
;
1124 return SECCLASS_UNIX_DGRAM_SOCKET
;
1131 if (default_protocol_stream(protocol
))
1132 return SECCLASS_TCP_SOCKET
;
1134 return SECCLASS_RAWIP_SOCKET
;
1136 if (default_protocol_dgram(protocol
))
1137 return SECCLASS_UDP_SOCKET
;
1139 return SECCLASS_RAWIP_SOCKET
;
1141 return SECCLASS_DCCP_SOCKET
;
1143 return SECCLASS_RAWIP_SOCKET
;
1149 return SECCLASS_NETLINK_ROUTE_SOCKET
;
1150 case NETLINK_FIREWALL
:
1151 return SECCLASS_NETLINK_FIREWALL_SOCKET
;
1152 case NETLINK_SOCK_DIAG
:
1153 return SECCLASS_NETLINK_TCPDIAG_SOCKET
;
1155 return SECCLASS_NETLINK_NFLOG_SOCKET
;
1157 return SECCLASS_NETLINK_XFRM_SOCKET
;
1158 case NETLINK_SELINUX
:
1159 return SECCLASS_NETLINK_SELINUX_SOCKET
;
1161 return SECCLASS_NETLINK_AUDIT_SOCKET
;
1162 case NETLINK_IP6_FW
:
1163 return SECCLASS_NETLINK_IP6FW_SOCKET
;
1164 case NETLINK_DNRTMSG
:
1165 return SECCLASS_NETLINK_DNRT_SOCKET
;
1166 case NETLINK_KOBJECT_UEVENT
:
1167 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET
;
1169 return SECCLASS_NETLINK_SOCKET
;
1172 return SECCLASS_PACKET_SOCKET
;
1174 return SECCLASS_KEY_SOCKET
;
1176 return SECCLASS_APPLETALK_SOCKET
;
1179 return SECCLASS_SOCKET
;
1182 #ifdef CONFIG_PROC_FS
1183 static int selinux_proc_get_sid(struct dentry
*dentry
,
1188 char *buffer
, *path
;
1190 buffer
= (char *)__get_free_page(GFP_KERNEL
);
1194 path
= dentry_path_raw(dentry
, buffer
, PAGE_SIZE
);
1198 /* each process gets a /proc/PID/ entry. Strip off the
1199 * PID part to get a valid selinux labeling.
1200 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1201 while (path
[1] >= '0' && path
[1] <= '9') {
1205 rc
= security_genfs_sid("proc", path
, tclass
, sid
);
1207 free_page((unsigned long)buffer
);
1211 static int selinux_proc_get_sid(struct dentry
*dentry
,
1219 /* The inode's security attributes must be initialized before first use. */
1220 static int inode_doinit_with_dentry(struct inode
*inode
, struct dentry
*opt_dentry
)
1222 struct superblock_security_struct
*sbsec
= NULL
;
1223 struct inode_security_struct
*isec
= inode
->i_security
;
1225 struct dentry
*dentry
;
1226 #define INITCONTEXTLEN 255
1227 char *context
= NULL
;
1231 if (isec
->initialized
)
1234 mutex_lock(&isec
->lock
);
1235 if (isec
->initialized
)
1238 sbsec
= inode
->i_sb
->s_security
;
1239 /* To prevent Null pointer exception */
1241 printk(KERN_ERR
"[SELinux] sbsec is NULL, inode->i_sb->s_security is already freed. \n");
1246 if (!(sbsec
->flags
& SE_SBINITIALIZED
)) {
1247 /* Defer initialization until selinux_complete_init,
1248 after the initial policy is loaded and the security
1249 server is ready to handle calls. */
1250 spin_lock(&sbsec
->isec_lock
);
1251 if (list_empty(&isec
->list
))
1252 list_add(&isec
->list
, &sbsec
->isec_head
);
1253 spin_unlock(&sbsec
->isec_lock
);
1257 switch (sbsec
->behavior
) {
1258 case SECURITY_FS_USE_XATTR
:
1259 if (!inode
->i_op
->getxattr
) {
1260 isec
->sid
= sbsec
->def_sid
;
1264 /* Need a dentry, since the xattr API requires one.
1265 Life would be simpler if we could just pass the inode. */
1267 /* Called from d_instantiate or d_splice_alias. */
1268 dentry
= dget(opt_dentry
);
1270 /* Called from selinux_complete_init, try to find a dentry. */
1271 dentry
= d_find_alias(inode
);
1275 * this is can be hit on boot when a file is accessed
1276 * before the policy is loaded. When we load policy we
1277 * may find inodes that have no dentry on the
1278 * sbsec->isec_head list. No reason to complain as these
1279 * will get fixed up the next time we go through
1280 * inode_doinit with a dentry, before these inodes could
1281 * be used again by userspace.
1286 len
= INITCONTEXTLEN
;
1287 context
= kmalloc(len
+1, GFP_NOFS
);
1293 context
[len
] = '\0';
1294 rc
= inode
->i_op
->getxattr(dentry
, XATTR_NAME_SELINUX
,
1296 if (rc
== -ERANGE
) {
1299 /* Need a larger buffer. Query for the right size. */
1300 rc
= inode
->i_op
->getxattr(dentry
, XATTR_NAME_SELINUX
,
1307 context
= kmalloc(len
+1, GFP_NOFS
);
1313 context
[len
] = '\0';
1314 rc
= inode
->i_op
->getxattr(dentry
,
1320 if (rc
!= -ENODATA
) {
1321 printk(KERN_WARNING
"SELinux: %s: getxattr returned "
1322 "%d for dev=%s ino=%ld\n", __func__
,
1323 -rc
, inode
->i_sb
->s_id
, inode
->i_ino
);
1327 /* Map ENODATA to the default file SID */
1328 sid
= sbsec
->def_sid
;
1331 rc
= security_context_to_sid_default(context
, rc
, &sid
,
1335 char *dev
= inode
->i_sb
->s_id
;
1336 unsigned long ino
= inode
->i_ino
;
1338 if (rc
== -EINVAL
) {
1339 if (printk_ratelimit())
1340 printk(KERN_NOTICE
"SELinux: inode=%lu on dev=%s was found to have an invalid "
1341 "context=%s. This indicates you may need to relabel the inode or the "
1342 "filesystem in question.\n", ino
, dev
, context
);
1344 printk(KERN_WARNING
"SELinux: %s: context_to_sid(%s) "
1345 "returned %d for dev=%s ino=%ld\n",
1346 __func__
, context
, -rc
, dev
, ino
);
1349 /* Leave with the unlabeled SID */
1357 case SECURITY_FS_USE_TASK
:
1358 isec
->sid
= isec
->task_sid
;
1360 case SECURITY_FS_USE_TRANS
:
1361 /* Default to the fs SID. */
1362 isec
->sid
= sbsec
->sid
;
1364 /* Try to obtain a transition SID. */
1365 isec
->sclass
= inode_mode_to_security_class(inode
->i_mode
);
1366 rc
= security_transition_sid(isec
->task_sid
, sbsec
->sid
,
1367 isec
->sclass
, NULL
, &sid
);
1372 case SECURITY_FS_USE_MNTPOINT
:
1373 isec
->sid
= sbsec
->mntpoint_sid
;
1376 /* Default to the fs superblock SID. */
1377 isec
->sid
= sbsec
->sid
;
1379 if ((sbsec
->flags
& SE_SBPROC
) && !S_ISLNK(inode
->i_mode
)) {
1380 /* We must have a dentry to determine the label on
1383 /* Called from d_instantiate or
1384 * d_splice_alias. */
1385 dentry
= dget(opt_dentry
);
1387 /* Called from selinux_complete_init, try to
1389 dentry
= d_find_alias(inode
);
1391 * This can be hit on boot when a file is accessed
1392 * before the policy is loaded. When we load policy we
1393 * may find inodes that have no dentry on the
1394 * sbsec->isec_head list. No reason to complain as
1395 * these will get fixed up the next time we go through
1396 * inode_doinit() with a dentry, before these inodes
1397 * could be used again by userspace.
1401 isec
->sclass
= inode_mode_to_security_class(inode
->i_mode
);
1402 rc
= selinux_proc_get_sid(dentry
, isec
->sclass
, &sid
);
1411 isec
->initialized
= 1;
1414 mutex_unlock(&isec
->lock
);
1416 if (isec
->sclass
== SECCLASS_FILE
)
1417 isec
->sclass
= inode_mode_to_security_class(inode
->i_mode
);
1421 /* Convert a Linux signal to an access vector. */
1422 static inline u32
signal_to_av(int sig
)
1428 /* Commonly granted from child to parent. */
1429 perm
= PROCESS__SIGCHLD
;
1432 /* Cannot be caught or ignored */
1433 perm
= PROCESS__SIGKILL
;
1436 /* Cannot be caught or ignored */
1437 perm
= PROCESS__SIGSTOP
;
1440 /* All other signals. */
1441 perm
= PROCESS__SIGNAL
;
1449 * Check permission between a pair of credentials
1450 * fork check, ptrace check, etc.
1452 static int cred_has_perm(const struct cred
*actor
,
1453 const struct cred
*target
,
1456 u32 asid
= cred_sid(actor
), tsid
= cred_sid(target
);
1458 return avc_has_perm(asid
, tsid
, SECCLASS_PROCESS
, perms
, NULL
);
1462 * Check permission between a pair of tasks, e.g. signal checks,
1463 * fork check, ptrace check, etc.
1464 * tsk1 is the actor and tsk2 is the target
1465 * - this uses the default subjective creds of tsk1
1467 static int task_has_perm(const struct task_struct
*tsk1
,
1468 const struct task_struct
*tsk2
,
1471 const struct task_security_struct
*__tsec1
, *__tsec2
;
1476 __tsec1
= __task_cred(tsk1
)->security
; sid1
= __tsec1
->sid
;
1477 __tsec2
= __task_cred(tsk2
)->security
; sid2
= __tsec2
->sid
;
1480 return avc_has_perm(sid1
, sid2
, SECCLASS_PROCESS
, perms
, NULL
);
1484 * Check permission between current and another task, e.g. signal checks,
1485 * fork check, ptrace check, etc.
1486 * current is the actor and tsk2 is the target
1487 * - this uses current's subjective creds
1489 static int current_has_perm(const struct task_struct
*tsk
,
1494 sid
= current_sid();
1495 tsid
= task_sid(tsk
);
1496 return avc_has_perm(sid
, tsid
, SECCLASS_PROCESS
, perms
, NULL
);
1499 #if CAP_LAST_CAP > 63
1500 #error Fix SELinux to handle capabilities > 63.
1503 /* Check whether a task is allowed to use a capability. */
1504 static int cred_has_capability(const struct cred
*cred
,
1507 struct common_audit_data ad
;
1508 struct av_decision avd
;
1510 u32 sid
= cred_sid(cred
);
1511 u32 av
= CAP_TO_MASK(cap
);
1514 ad
.type
= LSM_AUDIT_DATA_CAP
;
1517 switch (CAP_TO_INDEX(cap
)) {
1519 sclass
= SECCLASS_CAPABILITY
;
1522 sclass
= SECCLASS_CAPABILITY2
;
1526 "SELinux: out of range capability %d\n", cap
);
1531 rc
= avc_has_perm_noaudit(sid
, sid
, sclass
, av
, 0, &avd
);
1532 if (audit
== SECURITY_CAP_AUDIT
) {
1533 int rc2
= avc_audit(sid
, sid
, sclass
, av
, &avd
, rc
, &ad
, 0);
1540 /* Check whether a task is allowed to use a system operation. */
1541 static int task_has_system(struct task_struct
*tsk
,
1544 u32 sid
= task_sid(tsk
);
1546 return avc_has_perm(sid
, SECINITSID_KERNEL
,
1547 SECCLASS_SYSTEM
, perms
, NULL
);
1550 /* Check whether a task has a particular permission to an inode.
1551 The 'adp' parameter is optional and allows other audit
1552 data to be passed (e.g. the dentry). */
1553 static int inode_has_perm(const struct cred
*cred
,
1554 struct inode
*inode
,
1556 struct common_audit_data
*adp
,
1559 struct inode_security_struct
*isec
;
1562 validate_creds(cred
);
1564 if (unlikely(IS_PRIVATE(inode
)))
1567 sid
= cred_sid(cred
);
1568 isec
= inode
->i_security
;
1570 if (unlikely(!isec
)){
1571 printk(KERN_CRIT
"[SELinux] isec is NULL, inode->i_security is already freed. \n");
1575 return avc_has_perm_flags(sid
, isec
->sid
, isec
->sclass
, perms
, adp
, flags
);
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
, 0);
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
, 0);
1608 /* Check whether a task can use an open file descriptor to
1609 access an inode in a given way. Check access to the
1610 descriptor itself, and then use dentry_has_perm to
1611 check a particular permission to the file.
1612 Access to the descriptor is implicitly granted if it
1613 has the same SID as the process. If av is zero, then
1614 access to the file is not checked, e.g. for cases
1615 where only the descriptor is affected like seek. */
1616 static int file_has_perm(const struct cred
*cred
,
1620 struct file_security_struct
*fsec
= file
->f_security
;
1621 struct inode
*inode
= file_inode(file
);
1622 struct common_audit_data ad
;
1623 u32 sid
= cred_sid(cred
);
1626 ad
.type
= LSM_AUDIT_DATA_PATH
;
1627 ad
.u
.path
= file
->f_path
;
1629 if (sid
!= fsec
->sid
) {
1630 rc
= avc_has_perm(sid
, fsec
->sid
,
1638 /* av is zero if only checking access to the descriptor. */
1641 rc
= inode_has_perm(cred
, inode
, av
, &ad
, 0);
1647 /* Check whether a task can create a file. */
1648 static int may_create(struct inode
*dir
,
1649 struct dentry
*dentry
,
1652 const struct task_security_struct
*tsec
= current_security();
1653 struct inode_security_struct
*dsec
;
1654 struct superblock_security_struct
*sbsec
;
1656 struct common_audit_data ad
;
1659 dsec
= dir
->i_security
;
1660 sbsec
= dir
->i_sb
->s_security
;
1663 newsid
= tsec
->create_sid
;
1665 ad
.type
= LSM_AUDIT_DATA_DENTRY
;
1666 ad
.u
.dentry
= dentry
;
1668 rc
= avc_has_perm(sid
, dsec
->sid
, SECCLASS_DIR
,
1669 DIR__ADD_NAME
| DIR__SEARCH
,
1674 if (!newsid
|| !(sbsec
->flags
& SE_SBLABELSUPP
)) {
1675 rc
= security_transition_sid(sid
, dsec
->sid
, tclass
,
1676 &dentry
->d_name
, &newsid
);
1681 rc
= avc_has_perm(sid
, newsid
, tclass
, FILE__CREATE
, &ad
);
1685 return avc_has_perm(newsid
, sbsec
->sid
,
1686 SECCLASS_FILESYSTEM
,
1687 FILESYSTEM__ASSOCIATE
, &ad
);
1690 /* Check whether a task can create a key. */
1691 static int may_create_key(u32 ksid
,
1692 struct task_struct
*ctx
)
1694 u32 sid
= task_sid(ctx
);
1696 return avc_has_perm(sid
, ksid
, SECCLASS_KEY
, KEY__CREATE
, NULL
);
1700 #define MAY_UNLINK 1
1703 /* Check whether a task can link, unlink, or rmdir a file/directory. */
1704 static int may_link(struct inode
*dir
,
1705 struct dentry
*dentry
,
1709 struct inode_security_struct
*dsec
, *isec
;
1710 struct common_audit_data ad
;
1711 u32 sid
= current_sid();
1715 dsec
= dir
->i_security
;
1716 isec
= dentry
->d_inode
->i_security
;
1718 ad
.type
= LSM_AUDIT_DATA_DENTRY
;
1719 ad
.u
.dentry
= dentry
;
1722 av
|= (kind
? DIR__REMOVE_NAME
: DIR__ADD_NAME
);
1723 rc
= avc_has_perm(sid
, dsec
->sid
, SECCLASS_DIR
, av
, &ad
);
1738 printk(KERN_WARNING
"SELinux: %s: unrecognized kind %d\n",
1743 rc
= avc_has_perm(sid
, isec
->sid
, isec
->sclass
, av
, &ad
);
1747 static inline int may_rename(struct inode
*old_dir
,
1748 struct dentry
*old_dentry
,
1749 struct inode
*new_dir
,
1750 struct dentry
*new_dentry
)
1752 struct inode_security_struct
*old_dsec
, *new_dsec
, *old_isec
, *new_isec
;
1753 struct common_audit_data ad
;
1754 u32 sid
= current_sid();
1756 int old_is_dir
, new_is_dir
;
1759 old_dsec
= old_dir
->i_security
;
1760 old_isec
= old_dentry
->d_inode
->i_security
;
1761 old_is_dir
= S_ISDIR(old_dentry
->d_inode
->i_mode
);
1762 new_dsec
= new_dir
->i_security
;
1764 ad
.type
= LSM_AUDIT_DATA_DENTRY
;
1766 ad
.u
.dentry
= old_dentry
;
1767 rc
= avc_has_perm(sid
, old_dsec
->sid
, SECCLASS_DIR
,
1768 DIR__REMOVE_NAME
| DIR__SEARCH
, &ad
);
1771 rc
= avc_has_perm(sid
, old_isec
->sid
,
1772 old_isec
->sclass
, FILE__RENAME
, &ad
);
1775 if (old_is_dir
&& new_dir
!= old_dir
) {
1776 rc
= avc_has_perm(sid
, old_isec
->sid
,
1777 old_isec
->sclass
, DIR__REPARENT
, &ad
);
1782 ad
.u
.dentry
= new_dentry
;
1783 av
= DIR__ADD_NAME
| DIR__SEARCH
;
1784 if (new_dentry
->d_inode
)
1785 av
|= DIR__REMOVE_NAME
;
1786 rc
= avc_has_perm(sid
, new_dsec
->sid
, SECCLASS_DIR
, av
, &ad
);
1789 if (new_dentry
->d_inode
) {
1790 new_isec
= new_dentry
->d_inode
->i_security
;
1791 new_is_dir
= S_ISDIR(new_dentry
->d_inode
->i_mode
);
1792 rc
= avc_has_perm(sid
, new_isec
->sid
,
1794 (new_is_dir
? DIR__RMDIR
: FILE__UNLINK
), &ad
);
1802 /* Check whether a task can perform a filesystem operation. */
1803 static int superblock_has_perm(const struct cred
*cred
,
1804 struct super_block
*sb
,
1806 struct common_audit_data
*ad
)
1808 struct superblock_security_struct
*sbsec
;
1809 u32 sid
= cred_sid(cred
);
1811 sbsec
= sb
->s_security
;
1812 return avc_has_perm(sid
, sbsec
->sid
, SECCLASS_FILESYSTEM
, perms
, ad
);
1815 /* Convert a Linux mode and permission mask to an access vector. */
1816 static inline u32
file_mask_to_av(int mode
, int mask
)
1820 if (!S_ISDIR(mode
)) {
1821 if (mask
& MAY_EXEC
)
1822 av
|= FILE__EXECUTE
;
1823 if (mask
& MAY_READ
)
1826 if (mask
& MAY_APPEND
)
1828 else if (mask
& MAY_WRITE
)
1832 if (mask
& MAY_EXEC
)
1834 if (mask
& MAY_WRITE
)
1836 if (mask
& MAY_READ
)
1843 /* Convert a Linux file to an access vector. */
1844 static inline u32
file_to_av(struct file
*file
)
1848 if (file
->f_mode
& FMODE_READ
)
1850 if (file
->f_mode
& FMODE_WRITE
) {
1851 if (file
->f_flags
& O_APPEND
)
1858 * Special file opened with flags 3 for ioctl-only use.
1867 * Convert a file to an access vector and include the correct open
1870 static inline u32
open_file_to_av(struct file
*file
)
1872 u32 av
= file_to_av(file
);
1874 if (selinux_policycap_openperm
)
1880 /* Hook functions begin here. */
1882 static int selinux_binder_set_context_mgr(struct task_struct
*mgr
)
1884 u32 mysid
= current_sid();
1885 u32 mgrsid
= task_sid(mgr
);
1887 return avc_has_perm(mysid
, mgrsid
, SECCLASS_BINDER
, BINDER__SET_CONTEXT_MGR
, NULL
);
1890 static int selinux_binder_transaction(struct task_struct
*from
, struct task_struct
*to
)
1892 u32 mysid
= current_sid();
1893 u32 fromsid
= task_sid(from
);
1894 u32 tosid
= task_sid(to
);
1897 if (mysid
!= fromsid
) {
1898 rc
= avc_has_perm(mysid
, fromsid
, SECCLASS_BINDER
, BINDER__IMPERSONATE
, NULL
);
1903 return avc_has_perm(fromsid
, tosid
, SECCLASS_BINDER
, BINDER__CALL
, NULL
);
1906 static int selinux_binder_transfer_binder(struct task_struct
*from
, struct task_struct
*to
)
1908 u32 fromsid
= task_sid(from
);
1909 u32 tosid
= task_sid(to
);
1911 return avc_has_perm(fromsid
, tosid
, SECCLASS_BINDER
, BINDER__TRANSFER
, NULL
);
1914 static int selinux_binder_transfer_file(struct task_struct
*from
, struct task_struct
*to
, struct file
*file
)
1916 u32 sid
= task_sid(to
);
1917 struct file_security_struct
*fsec
= file
->f_security
;
1918 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1919 struct inode_security_struct
*isec
= inode
->i_security
;
1920 struct common_audit_data ad
;
1921 struct selinux_audit_data sad
= {0,};
1924 ad
.type
= LSM_AUDIT_DATA_PATH
;
1925 ad
.u
.path
= file
->f_path
;
1926 ad
.selinux_audit_data
= &sad
;
1928 if (sid
!= fsec
->sid
) {
1929 rc
= avc_has_perm(sid
, fsec
->sid
,
1937 if (unlikely(IS_PRIVATE(inode
)))
1940 return avc_has_perm(sid
, isec
->sid
, isec
->sclass
, file_to_av(file
),
1944 static int selinux_ptrace_access_check(struct task_struct
*child
,
1949 rc
= cap_ptrace_access_check(child
, mode
);
1953 if (mode
& PTRACE_MODE_READ
) {
1954 u32 sid
= current_sid();
1955 u32 csid
= task_sid(child
);
1956 return avc_has_perm(sid
, csid
, SECCLASS_FILE
, FILE__READ
, NULL
);
1959 return current_has_perm(child
, PROCESS__PTRACE
);
1962 static int selinux_ptrace_traceme(struct task_struct
*parent
)
1966 rc
= cap_ptrace_traceme(parent
);
1970 return task_has_perm(parent
, current
, PROCESS__PTRACE
);
1973 static int selinux_capget(struct task_struct
*target
, kernel_cap_t
*effective
,
1974 kernel_cap_t
*inheritable
, kernel_cap_t
*permitted
)
1978 error
= current_has_perm(target
, PROCESS__GETCAP
);
1982 return cap_capget(target
, effective
, inheritable
, permitted
);
1985 static int selinux_capset(struct cred
*new, const struct cred
*old
,
1986 const kernel_cap_t
*effective
,
1987 const kernel_cap_t
*inheritable
,
1988 const kernel_cap_t
*permitted
)
1992 error
= cap_capset(new, old
,
1993 effective
, inheritable
, permitted
);
1997 return cred_has_perm(old
, new, PROCESS__SETCAP
);
2001 * (This comment used to live with the selinux_task_setuid hook,
2002 * which was removed).
2004 * Since setuid only affects the current process, and since the SELinux
2005 * controls are not based on the Linux identity attributes, SELinux does not
2006 * need to control this operation. However, SELinux does control the use of
2007 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2010 static int selinux_capable(const struct cred
*cred
, struct user_namespace
*ns
,
2015 rc
= cap_capable(cred
, ns
, cap
, audit
);
2019 return cred_has_capability(cred
, cap
, audit
);
2022 static int selinux_quotactl(int cmds
, int type
, int id
, struct super_block
*sb
)
2024 const struct cred
*cred
= current_cred();
2036 rc
= superblock_has_perm(cred
, sb
, FILESYSTEM__QUOTAMOD
, NULL
);
2041 rc
= superblock_has_perm(cred
, sb
, FILESYSTEM__QUOTAGET
, NULL
);
2044 rc
= 0; /* let the kernel handle invalid cmds */
2050 static int selinux_quota_on(struct dentry
*dentry
)
2052 const struct cred
*cred
= current_cred();
2054 return dentry_has_perm(cred
, dentry
, FILE__QUOTAON
);
2057 static int selinux_syslog(int type
)
2062 case SYSLOG_ACTION_READ_ALL
: /* Read last kernel messages */
2063 case SYSLOG_ACTION_SIZE_BUFFER
: /* Return size of the log buffer */
2064 rc
= task_has_system(current
, SYSTEM__SYSLOG_READ
);
2066 case SYSLOG_ACTION_CONSOLE_OFF
: /* Disable logging to console */
2067 case SYSLOG_ACTION_CONSOLE_ON
: /* Enable logging to console */
2068 /* Set level of messages printed to console */
2069 case SYSLOG_ACTION_CONSOLE_LEVEL
:
2070 rc
= task_has_system(current
, SYSTEM__SYSLOG_CONSOLE
);
2072 case SYSLOG_ACTION_CLOSE
: /* Close log */
2073 case SYSLOG_ACTION_OPEN
: /* Open log */
2074 case SYSLOG_ACTION_READ
: /* Read from log */
2075 case SYSLOG_ACTION_READ_CLEAR
: /* Read/clear last kernel messages */
2076 case SYSLOG_ACTION_CLEAR
: /* Clear ring buffer */
2078 rc
= task_has_system(current
, SYSTEM__SYSLOG_MOD
);
2085 * Check that a process has enough memory to allocate a new virtual
2086 * mapping. 0 means there is enough memory for the allocation to
2087 * succeed and -ENOMEM implies there is not.
2089 * Do not audit the selinux permission check, as this is applied to all
2090 * processes that allocate mappings.
2092 static int selinux_vm_enough_memory(struct mm_struct
*mm
, long pages
)
2094 int rc
, cap_sys_admin
= 0;
2096 rc
= selinux_capable(current_cred(), &init_user_ns
, CAP_SYS_ADMIN
,
2097 SECURITY_CAP_NOAUDIT
);
2101 return __vm_enough_memory(mm
, pages
, cap_sys_admin
);
2104 /* binprm security operations */
2106 static int selinux_bprm_set_creds(struct linux_binprm
*bprm
)
2108 const struct task_security_struct
*old_tsec
;
2109 struct task_security_struct
*new_tsec
;
2110 struct inode_security_struct
*isec
;
2111 struct common_audit_data ad
;
2112 struct inode
*inode
= file_inode(bprm
->file
);
2115 rc
= cap_bprm_set_creds(bprm
);
2119 /* SELinux context only depends on initial program or script and not
2120 * the script interpreter */
2121 if (bprm
->cred_prepared
)
2124 old_tsec
= current_security();
2125 new_tsec
= bprm
->cred
->security
;
2126 isec
= inode
->i_security
;
2128 /* Default to the current task SID. */
2129 new_tsec
->sid
= old_tsec
->sid
;
2130 new_tsec
->osid
= old_tsec
->sid
;
2132 /* Reset fs, key, and sock SIDs on execve. */
2133 new_tsec
->create_sid
= 0;
2134 new_tsec
->keycreate_sid
= 0;
2135 new_tsec
->sockcreate_sid
= 0;
2137 if (old_tsec
->exec_sid
) {
2138 new_tsec
->sid
= old_tsec
->exec_sid
;
2139 /* Reset exec SID on execve. */
2140 new_tsec
->exec_sid
= 0;
2143 * Minimize confusion: if no_new_privs and a transition is
2144 * explicitly requested, then fail the exec.
2146 if (bprm
->unsafe
& LSM_UNSAFE_NO_NEW_PRIVS
)
2149 /* Check for a default transition on this program. */
2150 rc
= security_transition_sid(old_tsec
->sid
, isec
->sid
,
2151 SECCLASS_PROCESS
, NULL
,
2157 ad
.type
= LSM_AUDIT_DATA_PATH
;
2158 ad
.u
.path
= bprm
->file
->f_path
;
2160 if ((bprm
->file
->f_path
.mnt
->mnt_flags
& MNT_NOSUID
) ||
2161 (bprm
->unsafe
& LSM_UNSAFE_NO_NEW_PRIVS
))
2162 new_tsec
->sid
= old_tsec
->sid
;
2164 if (new_tsec
->sid
== old_tsec
->sid
) {
2165 rc
= avc_has_perm(old_tsec
->sid
, isec
->sid
,
2166 SECCLASS_FILE
, FILE__EXECUTE_NO_TRANS
, &ad
);
2170 /* Check permissions for the transition. */
2171 rc
= avc_has_perm(old_tsec
->sid
, new_tsec
->sid
,
2172 SECCLASS_PROCESS
, PROCESS__TRANSITION
, &ad
);
2176 rc
= avc_has_perm(new_tsec
->sid
, isec
->sid
,
2177 SECCLASS_FILE
, FILE__ENTRYPOINT
, &ad
);
2181 /* Check for shared state */
2182 if (bprm
->unsafe
& LSM_UNSAFE_SHARE
) {
2183 rc
= avc_has_perm(old_tsec
->sid
, new_tsec
->sid
,
2184 SECCLASS_PROCESS
, PROCESS__SHARE
,
2190 /* Make sure that anyone attempting to ptrace over a task that
2191 * changes its SID has the appropriate permit */
2193 (LSM_UNSAFE_PTRACE
| LSM_UNSAFE_PTRACE_CAP
)) {
2194 struct task_struct
*tracer
;
2195 struct task_security_struct
*sec
;
2199 tracer
= ptrace_parent(current
);
2200 if (likely(tracer
!= NULL
)) {
2201 sec
= __task_cred(tracer
)->security
;
2207 rc
= avc_has_perm(ptsid
, new_tsec
->sid
,
2209 PROCESS__PTRACE
, NULL
);
2215 /* Clear any possibly unsafe personality bits on exec: */
2216 bprm
->per_clear
|= PER_CLEAR_ON_SETID
;
2222 static int selinux_bprm_secureexec(struct linux_binprm
*bprm
)
2224 const struct task_security_struct
*tsec
= current_security();
2232 /* Enable secure mode for SIDs transitions unless
2233 the noatsecure permission is granted between
2234 the two SIDs, i.e. ahp returns 0. */
2235 atsecure
= avc_has_perm(osid
, sid
,
2237 PROCESS__NOATSECURE
, NULL
);
2240 return (atsecure
|| cap_bprm_secureexec(bprm
));
2243 static int match_file(const void *p
, struct file
*file
, unsigned fd
)
2245 return file_has_perm(p
, file
, file_to_av(file
)) ? fd
+ 1 : 0;
2248 /* Derived from fs/exec.c:flush_old_files. */
2249 static inline void flush_unauthorized_files(const struct cred
*cred
,
2250 struct files_struct
*files
)
2252 struct file
*file
, *devnull
= NULL
;
2253 struct tty_struct
*tty
;
2257 tty
= get_current_tty();
2259 spin_lock(&tty_files_lock
);
2260 if (!list_empty(&tty
->tty_files
)) {
2261 struct tty_file_private
*file_priv
;
2263 /* Revalidate access to controlling tty.
2264 Use path_has_perm on the tty path directly rather
2265 than using file_has_perm, as this particular open
2266 file may belong to another process and we are only
2267 interested in the inode-based check here. */
2268 file_priv
= list_first_entry(&tty
->tty_files
,
2269 struct tty_file_private
, list
);
2270 file
= file_priv
->file
;
2271 if (path_has_perm(cred
, &file
->f_path
, FILE__READ
| FILE__WRITE
))
2274 spin_unlock(&tty_files_lock
);
2277 /* Reset controlling tty. */
2281 /* Revalidate access to inherited open files. */
2282 n
= iterate_fd(files
, 0, match_file
, cred
);
2283 if (!n
) /* none found? */
2286 devnull
= dentry_open(&selinux_null
, O_RDWR
, cred
);
2287 if (IS_ERR(devnull
))
2289 /* replace all the matching ones with this */
2291 replace_fd(n
- 1, devnull
, 0);
2292 } while ((n
= iterate_fd(files
, n
, match_file
, cred
)) != 0);
2298 * Prepare a process for imminent new credential changes due to exec
2300 static void selinux_bprm_committing_creds(struct linux_binprm
*bprm
)
2302 struct task_security_struct
*new_tsec
;
2303 struct rlimit
*rlim
, *initrlim
;
2306 new_tsec
= bprm
->cred
->security
;
2307 if (new_tsec
->sid
== new_tsec
->osid
)
2310 /* Close files for which the new task SID is not authorized. */
2311 flush_unauthorized_files(bprm
->cred
, current
->files
);
2313 /* Always clear parent death signal on SID transitions. */
2314 current
->pdeath_signal
= 0;
2316 /* Check whether the new SID can inherit resource limits from the old
2317 * SID. If not, reset all soft limits to the lower of the current
2318 * task's hard limit and the init task's soft limit.
2320 * Note that the setting of hard limits (even to lower them) can be
2321 * controlled by the setrlimit check. The inclusion of the init task's
2322 * soft limit into the computation is to avoid resetting soft limits
2323 * higher than the default soft limit for cases where the default is
2324 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2326 rc
= avc_has_perm(new_tsec
->osid
, new_tsec
->sid
, SECCLASS_PROCESS
,
2327 PROCESS__RLIMITINH
, NULL
);
2329 /* protect against do_prlimit() */
2331 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
2332 rlim
= current
->signal
->rlim
+ i
;
2333 initrlim
= init_task
.signal
->rlim
+ i
;
2334 rlim
->rlim_cur
= min(rlim
->rlim_max
, initrlim
->rlim_cur
);
2336 task_unlock(current
);
2337 update_rlimit_cpu(current
, rlimit(RLIMIT_CPU
));
2342 * Clean up the process immediately after the installation of new credentials
2345 static void selinux_bprm_committed_creds(struct linux_binprm
*bprm
)
2347 const struct task_security_struct
*tsec
= current_security();
2348 struct itimerval itimer
;
2358 /* Check whether the new SID can inherit signal state from the old SID.
2359 * If not, clear itimers to avoid subsequent signal generation and
2360 * flush and unblock signals.
2362 * This must occur _after_ the task SID has been updated so that any
2363 * kill done after the flush will be checked against the new SID.
2365 rc
= avc_has_perm(osid
, sid
, SECCLASS_PROCESS
, PROCESS__SIGINH
, NULL
);
2367 memset(&itimer
, 0, sizeof itimer
);
2368 for (i
= 0; i
< 3; i
++)
2369 do_setitimer(i
, &itimer
, NULL
);
2370 spin_lock_irq(¤t
->sighand
->siglock
);
2371 if (!(current
->signal
->flags
& SIGNAL_GROUP_EXIT
)) {
2372 __flush_signals(current
);
2373 flush_signal_handlers(current
, 1);
2374 sigemptyset(¤t
->blocked
);
2376 spin_unlock_irq(¤t
->sighand
->siglock
);
2379 /* Wake up the parent if it is waiting so that it can recheck
2380 * wait permission to the new task SID. */
2381 read_lock(&tasklist_lock
);
2382 __wake_up_parent(current
, current
->real_parent
);
2383 read_unlock(&tasklist_lock
);
2386 /* superblock security operations */
2388 static int selinux_sb_alloc_security(struct super_block
*sb
)
2390 return superblock_alloc_security(sb
);
2393 static void selinux_sb_free_security(struct super_block
*sb
)
2395 superblock_free_security(sb
);
2398 static inline int match_prefix(char *prefix
, int plen
, char *option
, int olen
)
2403 return !memcmp(prefix
, option
, plen
);
2406 static inline int selinux_option(char *option
, int len
)
2408 return (match_prefix(CONTEXT_STR
, sizeof(CONTEXT_STR
)-1, option
, len
) ||
2409 match_prefix(FSCONTEXT_STR
, sizeof(FSCONTEXT_STR
)-1, option
, len
) ||
2410 match_prefix(DEFCONTEXT_STR
, sizeof(DEFCONTEXT_STR
)-1, option
, len
) ||
2411 match_prefix(ROOTCONTEXT_STR
, sizeof(ROOTCONTEXT_STR
)-1, option
, len
) ||
2412 match_prefix(LABELSUPP_STR
, sizeof(LABELSUPP_STR
)-1, option
, len
));
2415 static inline void take_option(char **to
, char *from
, int *first
, int len
)
2422 memcpy(*to
, from
, len
);
2426 static inline void take_selinux_option(char **to
, char *from
, int *first
,
2429 int current_size
= 0;
2437 while (current_size
< len
) {
2447 static int selinux_sb_copy_data(char *orig
, char *copy
)
2449 int fnosec
, fsec
, rc
= 0;
2450 char *in_save
, *in_curr
, *in_end
;
2451 char *sec_curr
, *nosec_save
, *nosec
;
2457 nosec
= (char *)get_zeroed_page(GFP_KERNEL
);
2465 in_save
= in_end
= orig
;
2469 open_quote
= !open_quote
;
2470 if ((*in_end
== ',' && open_quote
== 0) ||
2472 int len
= in_end
- in_curr
;
2474 if (selinux_option(in_curr
, len
))
2475 take_selinux_option(&sec_curr
, in_curr
, &fsec
, len
);
2477 take_option(&nosec
, in_curr
, &fnosec
, len
);
2479 in_curr
= in_end
+ 1;
2481 } while (*in_end
++);
2483 strcpy(in_save
, nosec_save
);
2484 free_page((unsigned long)nosec_save
);
2489 static int selinux_sb_remount(struct super_block
*sb
, void *data
)
2492 struct security_mnt_opts opts
;
2493 char *secdata
, **mount_options
;
2494 struct superblock_security_struct
*sbsec
= sb
->s_security
;
2496 if (!(sbsec
->flags
& SE_SBINITIALIZED
))
2502 if (sb
->s_type
->fs_flags
& FS_BINARY_MOUNTDATA
)
2505 security_init_mnt_opts(&opts
);
2506 secdata
= alloc_secdata();
2509 rc
= selinux_sb_copy_data(data
, secdata
);
2511 goto out_free_secdata
;
2513 rc
= selinux_parse_opts_str(secdata
, &opts
);
2515 goto out_free_secdata
;
2517 mount_options
= opts
.mnt_opts
;
2518 flags
= opts
.mnt_opts_flags
;
2520 for (i
= 0; i
< opts
.num_mnt_opts
; i
++) {
2524 if (flags
[i
] == SE_SBLABELSUPP
)
2526 len
= strlen(mount_options
[i
]);
2527 rc
= security_context_to_sid(mount_options
[i
], len
, &sid
);
2529 printk(KERN_WARNING
"SELinux: security_context_to_sid"
2530 "(%s) failed for (dev %s, type %s) errno=%d\n",
2531 mount_options
[i
], sb
->s_id
, sb
->s_type
->name
, rc
);
2537 if (bad_option(sbsec
, FSCONTEXT_MNT
, sbsec
->sid
, sid
))
2538 goto out_bad_option
;
2541 if (bad_option(sbsec
, CONTEXT_MNT
, sbsec
->mntpoint_sid
, sid
))
2542 goto out_bad_option
;
2544 case ROOTCONTEXT_MNT
: {
2545 struct inode_security_struct
*root_isec
;
2546 root_isec
= sb
->s_root
->d_inode
->i_security
;
2548 if (bad_option(sbsec
, ROOTCONTEXT_MNT
, root_isec
->sid
, sid
))
2549 goto out_bad_option
;
2552 case DEFCONTEXT_MNT
:
2553 if (bad_option(sbsec
, DEFCONTEXT_MNT
, sbsec
->def_sid
, sid
))
2554 goto out_bad_option
;
2563 security_free_mnt_opts(&opts
);
2565 free_secdata(secdata
);
2568 printk(KERN_WARNING
"SELinux: unable to change security options "
2569 "during remount (dev %s, type=%s)\n", sb
->s_id
,
2574 static int selinux_sb_kern_mount(struct super_block
*sb
, int flags
, void *data
)
2576 const struct cred
*cred
= current_cred();
2577 struct common_audit_data ad
;
2580 rc
= superblock_doinit(sb
, data
);
2584 /* Allow all mounts performed by the kernel */
2585 if (flags
& MS_KERNMOUNT
)
2588 ad
.type
= LSM_AUDIT_DATA_DENTRY
;
2589 ad
.u
.dentry
= sb
->s_root
;
2590 return superblock_has_perm(cred
, sb
, FILESYSTEM__MOUNT
, &ad
);
2593 static int selinux_sb_statfs(struct dentry
*dentry
)
2595 const struct cred
*cred
= current_cred();
2596 struct common_audit_data ad
;
2598 ad
.type
= LSM_AUDIT_DATA_DENTRY
;
2599 ad
.u
.dentry
= dentry
->d_sb
->s_root
;
2600 return superblock_has_perm(cred
, dentry
->d_sb
, FILESYSTEM__GETATTR
, &ad
);
2603 static int selinux_mount(const char *dev_name
,
2606 unsigned long flags
,
2609 const struct cred
*cred
= current_cred();
2610 if (flags
& MS_REMOUNT
)
2611 return superblock_has_perm(cred
, path
->dentry
->d_sb
,
2612 FILESYSTEM__REMOUNT
, NULL
);
2614 return path_has_perm(cred
, path
, FILE__MOUNTON
);
2617 static int selinux_umount(struct vfsmount
*mnt
, int flags
)
2619 const struct cred
*cred
= current_cred();
2621 return superblock_has_perm(cred
, mnt
->mnt_sb
,
2622 FILESYSTEM__UNMOUNT
, NULL
);
2625 /* inode security operations */
2627 static int selinux_inode_alloc_security(struct inode
*inode
)
2629 return inode_alloc_security(inode
);
2632 static void selinux_inode_free_security(struct inode
*inode
)
2634 inode_free_security(inode
);
2637 static int selinux_inode_init_security(struct inode
*inode
, struct inode
*dir
,
2638 const struct qstr
*qstr
, char **name
,
2639 void **value
, size_t *len
)
2641 const struct task_security_struct
*tsec
= current_security();
2642 struct inode_security_struct
*dsec
;
2643 struct superblock_security_struct
*sbsec
;
2644 u32 sid
, newsid
, clen
;
2646 char *namep
= NULL
, *context
;
2648 dsec
= dir
->i_security
;
2649 sbsec
= dir
->i_sb
->s_security
;
2652 newsid
= tsec
->create_sid
;
2654 if ((sbsec
->flags
& SE_SBINITIALIZED
) &&
2655 (sbsec
->behavior
== SECURITY_FS_USE_MNTPOINT
))
2656 newsid
= sbsec
->mntpoint_sid
;
2657 else if (!newsid
|| !(sbsec
->flags
& SE_SBLABELSUPP
)) {
2658 rc
= security_transition_sid(sid
, dsec
->sid
,
2659 inode_mode_to_security_class(inode
->i_mode
),
2662 printk(KERN_WARNING
"%s: "
2663 "security_transition_sid failed, rc=%d (dev=%s "
2666 -rc
, inode
->i_sb
->s_id
, inode
->i_ino
);
2671 /* Possibly defer initialization to selinux_complete_init. */
2672 if (sbsec
->flags
& SE_SBINITIALIZED
) {
2673 struct inode_security_struct
*isec
= inode
->i_security
;
2674 isec
->sclass
= inode_mode_to_security_class(inode
->i_mode
);
2676 isec
->initialized
= 1;
2679 if (!ss_initialized
|| !(sbsec
->flags
& SE_SBLABELSUPP
))
2683 namep
= kstrdup(XATTR_SELINUX_SUFFIX
, GFP_NOFS
);
2690 rc
= security_sid_to_context_force(newsid
, &context
, &clen
);
2702 static int selinux_inode_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
2704 return may_create(dir
, dentry
, SECCLASS_FILE
);
2707 static int selinux_inode_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
2709 return may_link(dir
, old_dentry
, MAY_LINK
);
2712 static int selinux_inode_unlink(struct inode
*dir
, struct dentry
*dentry
)
2714 return may_link(dir
, dentry
, MAY_UNLINK
);
2717 static int selinux_inode_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *name
)
2719 return may_create(dir
, dentry
, SECCLASS_LNK_FILE
);
2722 static int selinux_inode_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mask
)
2724 return may_create(dir
, dentry
, SECCLASS_DIR
);
2727 static int selinux_inode_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2729 return may_link(dir
, dentry
, MAY_RMDIR
);
2732 static int selinux_inode_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, dev_t dev
)
2734 return may_create(dir
, dentry
, inode_mode_to_security_class(mode
));
2737 static int selinux_inode_rename(struct inode
*old_inode
, struct dentry
*old_dentry
,
2738 struct inode
*new_inode
, struct dentry
*new_dentry
)
2740 return may_rename(old_inode
, old_dentry
, new_inode
, new_dentry
);
2743 static int selinux_inode_readlink(struct dentry
*dentry
)
2745 const struct cred
*cred
= current_cred();
2747 return dentry_has_perm(cred
, dentry
, FILE__READ
);
2750 static int selinux_inode_follow_link(struct dentry
*dentry
, struct nameidata
*nameidata
)
2752 const struct cred
*cred
= current_cred();
2754 return dentry_has_perm(cred
, dentry
, FILE__READ
);
2757 static noinline
int audit_inode_permission(struct inode
*inode
,
2758 u32 perms
, u32 audited
, u32 denied
,
2762 struct common_audit_data ad
;
2763 struct inode_security_struct
*isec
= inode
->i_security
;
2766 ad
.type
= LSM_AUDIT_DATA_INODE
;
2769 rc
= slow_avc_audit(current_sid(), isec
->sid
, isec
->sclass
, perms
,
2770 audited
, denied
, result
, &ad
, flags
);
2776 static int selinux_inode_permission(struct inode
*inode
, int mask
)
2778 const struct cred
*cred
= current_cred();
2781 unsigned flags
= mask
& MAY_NOT_BLOCK
;
2782 struct inode_security_struct
*isec
;
2784 struct av_decision avd
;
2786 u32 audited
, denied
;
2788 from_access
= mask
& MAY_ACCESS
;
2789 mask
&= (MAY_READ
|MAY_WRITE
|MAY_EXEC
|MAY_APPEND
);
2791 /* No permission to check. Existence test. */
2795 validate_creds(cred
);
2797 if (unlikely(IS_PRIVATE(inode
)))
2800 perms
= file_mask_to_av(inode
->i_mode
, mask
);
2802 sid
= cred_sid(cred
);
2803 isec
= inode
->i_security
;
2805 // [ SEC_SELINUX_PORTING COMMON
2806 /* skip sid == 1(kernel), it means first boot time */
2807 if(isec
->initialized
!= 1 && sid
!= 1) {
2810 while(count
-- > 0) {
2811 printk(KERN_ERR
"SELinux : inode->i_security is not initialized. waiting...(%d/5)\n", 5-count
);
2813 if(isec
->initialized
== 1) {
2814 printk(KERN_ERR
"SELinux : inode->i_security is INITIALIZED.\n");
2818 if(isec
->initialized
!= 1) {
2819 printk(KERN_ERR
"SELinux : inode->i_security is not initialized. not fixed.\n");
2822 // ] SEC_SELINUX_PORTING COMMON
2824 rc
= avc_has_perm_noaudit(sid
, isec
->sid
, isec
->sclass
, perms
, 0, &avd
);
2825 audited
= avc_audit_required(perms
, &avd
, rc
,
2826 from_access
? FILE__AUDIT_ACCESS
: 0,
2828 if (likely(!audited
))
2831 rc2
= audit_inode_permission(inode
, perms
, audited
, denied
, rc
, flags
);
2837 static int selinux_inode_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
2839 const struct cred
*cred
= current_cred();
2840 unsigned int ia_valid
= iattr
->ia_valid
;
2841 __u32 av
= FILE__WRITE
;
2843 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2844 if (ia_valid
& ATTR_FORCE
) {
2845 ia_valid
&= ~(ATTR_KILL_SUID
| ATTR_KILL_SGID
| ATTR_MODE
|
2851 if (ia_valid
& (ATTR_MODE
| ATTR_UID
| ATTR_GID
|
2852 ATTR_ATIME_SET
| ATTR_MTIME_SET
| ATTR_TIMES_SET
))
2853 return dentry_has_perm(cred
, dentry
, FILE__SETATTR
);
2855 if (selinux_policycap_openperm
&& (ia_valid
& ATTR_SIZE
)
2856 && !(ia_valid
& ATTR_FILE
))
2859 return dentry_has_perm(cred
, dentry
, av
);
2862 static int selinux_inode_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
)
2864 const struct cred
*cred
= current_cred();
2867 path
.dentry
= dentry
;
2870 return path_has_perm(cred
, &path
, FILE__GETATTR
);
2873 static int selinux_inode_setotherxattr(struct dentry
*dentry
, const char *name
)
2875 const struct cred
*cred
= current_cred();
2877 if (!strncmp(name
, XATTR_SECURITY_PREFIX
,
2878 sizeof XATTR_SECURITY_PREFIX
- 1)) {
2879 if (!strcmp(name
, XATTR_NAME_CAPS
)) {
2880 if (!capable(CAP_SETFCAP
))
2882 } else if (!capable(CAP_SYS_ADMIN
)) {
2883 /* A different attribute in the security namespace.
2884 Restrict to administrator. */
2889 /* Not an attribute we recognize, so just check the
2890 ordinary setattr permission. */
2891 return dentry_has_perm(cred
, dentry
, FILE__SETATTR
);
2894 static int selinux_inode_setxattr(struct dentry
*dentry
, const char *name
,
2895 const void *value
, size_t size
, int flags
)
2897 struct inode
*inode
= dentry
->d_inode
;
2898 struct inode_security_struct
*isec
= inode
->i_security
;
2899 struct superblock_security_struct
*sbsec
;
2900 struct common_audit_data ad
;
2901 u32 newsid
, sid
= current_sid();
2904 if (strcmp(name
, XATTR_NAME_SELINUX
))
2905 return selinux_inode_setotherxattr(dentry
, name
);
2907 sbsec
= inode
->i_sb
->s_security
;
2908 if (!(sbsec
->flags
& SE_SBLABELSUPP
))
2911 if (!inode_owner_or_capable(inode
))
2914 ad
.type
= LSM_AUDIT_DATA_DENTRY
;
2915 ad
.u
.dentry
= dentry
;
2917 rc
= avc_has_perm(sid
, isec
->sid
, isec
->sclass
,
2918 FILE__RELABELFROM
, &ad
);
2922 rc
= security_context_to_sid(value
, size
, &newsid
);
2923 if (rc
== -EINVAL
) {
2924 if (!capable(CAP_MAC_ADMIN
)) {
2925 struct audit_buffer
*ab
;
2929 /* We strip a nul only if it is at the end, otherwise the
2930 * context contains a nul and we should audit that */
2933 if (str
[size
- 1] == '\0')
2934 audit_size
= size
- 1;
2941 ab
= audit_log_start(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
);
2942 audit_log_format(ab
, "op=setxattr invalid_context=");
2943 audit_log_n_untrustedstring(ab
, value
, audit_size
);
2948 rc
= security_context_to_sid_force(value
, size
, &newsid
);
2953 rc
= avc_has_perm(sid
, newsid
, isec
->sclass
,
2954 FILE__RELABELTO
, &ad
);
2958 rc
= security_validate_transition(isec
->sid
, newsid
, sid
,
2963 return avc_has_perm(newsid
,
2965 SECCLASS_FILESYSTEM
,
2966 FILESYSTEM__ASSOCIATE
,
2970 static void selinux_inode_post_setxattr(struct dentry
*dentry
, const char *name
,
2971 const void *value
, size_t size
,
2974 struct inode
*inode
= dentry
->d_inode
;
2975 struct inode_security_struct
*isec
= inode
->i_security
;
2979 if (strcmp(name
, XATTR_NAME_SELINUX
)) {
2980 /* Not an attribute we recognize, so nothing to do. */
2984 rc
= security_context_to_sid_force(value
, size
, &newsid
);
2986 printk(KERN_ERR
"SELinux: unable to map context to SID"
2987 "for (%s, %lu), rc=%d\n",
2988 inode
->i_sb
->s_id
, inode
->i_ino
, -rc
);
2996 static int selinux_inode_getxattr(struct dentry
*dentry
, const char *name
)
2998 const struct cred
*cred
= current_cred();
3000 return dentry_has_perm(cred
, dentry
, FILE__GETATTR
);
3003 static int selinux_inode_listxattr(struct dentry
*dentry
)
3005 const struct cred
*cred
= current_cred();
3007 return dentry_has_perm(cred
, dentry
, FILE__GETATTR
);
3010 static int selinux_inode_removexattr(struct dentry
*dentry
, const char *name
)
3012 if (strcmp(name
, XATTR_NAME_SELINUX
))
3013 return selinux_inode_setotherxattr(dentry
, name
);
3015 /* No one is allowed to remove a SELinux security label.
3016 You can change the label, but all data must be labeled. */
3021 * Copy the inode security context value to the user.
3023 * Permission check is handled by selinux_inode_getxattr hook.
3025 static int selinux_inode_getsecurity(const struct inode
*inode
, const char *name
, void **buffer
, bool alloc
)
3029 char *context
= NULL
;
3030 struct inode_security_struct
*isec
= inode
->i_security
;
3032 if (strcmp(name
, XATTR_SELINUX_SUFFIX
))
3036 * If the caller has CAP_MAC_ADMIN, then get the raw context
3037 * value even if it is not defined by current policy; otherwise,
3038 * use the in-core value under current policy.
3039 * Use the non-auditing forms of the permission checks since
3040 * getxattr may be called by unprivileged processes commonly
3041 * and lack of permission just means that we fall back to the
3042 * in-core context value, not a denial.
3044 error
= selinux_capable(current_cred(), &init_user_ns
, CAP_MAC_ADMIN
,
3045 SECURITY_CAP_NOAUDIT
);
3047 error
= security_sid_to_context_force(isec
->sid
, &context
,
3050 error
= security_sid_to_context(isec
->sid
, &context
, &size
);
3063 static int selinux_inode_setsecurity(struct inode
*inode
, const char *name
,
3064 const void *value
, size_t size
, int flags
)
3066 struct inode_security_struct
*isec
= inode
->i_security
;
3070 if (strcmp(name
, XATTR_SELINUX_SUFFIX
))
3073 if (!value
|| !size
)
3076 rc
= security_context_to_sid((void *)value
, size
, &newsid
);
3081 isec
->initialized
= 1;
3085 static int selinux_inode_listsecurity(struct inode
*inode
, char *buffer
, size_t buffer_size
)
3087 const int len
= sizeof(XATTR_NAME_SELINUX
);
3089 if (buffer
&& len
<= buffer_size
)
3090 memcpy(buffer
, XATTR_NAME_SELINUX
, len
);
3094 static void selinux_inode_getsecid(const struct inode
*inode
, u32
*secid
)
3096 struct inode_security_struct
*isec
= inode
->i_security
;
3100 /* file security operations */
3102 static int selinux_revalidate_file_permission(struct file
*file
, int mask
)
3104 const struct cred
*cred
= current_cred();
3105 struct inode
*inode
= file_inode(file
);
3107 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3108 if ((file
->f_flags
& O_APPEND
) && (mask
& MAY_WRITE
))
3111 return file_has_perm(cred
, file
,
3112 file_mask_to_av(inode
->i_mode
, mask
));
3115 static int selinux_file_permission(struct file
*file
, int mask
)
3117 struct inode
*inode
= file_inode(file
);
3118 struct file_security_struct
*fsec
= file
->f_security
;
3119 struct inode_security_struct
*isec
= inode
->i_security
;
3120 u32 sid
= current_sid();
3123 /* No permission to check. Existence test. */
3126 if (sid
== fsec
->sid
&& fsec
->isid
== isec
->sid
&&
3127 fsec
->pseqno
== avc_policy_seqno())
3128 /* No change since file_open check. */
3131 return selinux_revalidate_file_permission(file
, mask
);
3134 static int selinux_file_alloc_security(struct file
*file
)
3136 return file_alloc_security(file
);
3139 static void selinux_file_free_security(struct file
*file
)
3141 file_free_security(file
);
3145 * Check whether a task has the ioctl permission and cmd
3146 * operation to an inode.
3148 int ioctl_has_perm(const struct cred
*cred
, struct file
*file
,
3149 u32 requested
, u16 cmd
)
3151 struct common_audit_data ad
;
3152 struct file_security_struct
*fsec
= file
->f_security
;
3153 struct inode
*inode
= file_inode(file
);
3154 struct inode_security_struct
*isec
= inode
->i_security
;
3155 struct lsm_ioctlop_audit ioctl
;
3156 u32 ssid
= cred_sid(cred
);
3158 u8 driver
= cmd
>> 8;
3159 u8 xperm
= cmd
& 0xff;
3161 ad
.type
= LSM_AUDIT_DATA_IOCTL_OP
;
3164 ad
.u
.op
->path
= file
->f_path
;
3166 if (ssid
!= fsec
->sid
) {
3167 rc
= avc_has_perm(ssid
, fsec
->sid
,
3175 if (unlikely(IS_PRIVATE(inode
)))
3178 rc
= avc_has_extended_perms(ssid
, isec
->sid
, isec
->sclass
,
3179 requested
, driver
, xperm
, &ad
);
3184 static int selinux_file_ioctl(struct file
*file
, unsigned int cmd
,
3187 const struct cred
*cred
= current_cred();
3197 case FS_IOC_GETFLAGS
:
3199 case FS_IOC_GETVERSION
:
3200 error
= file_has_perm(cred
, file
, FILE__GETATTR
);
3203 case FS_IOC_SETFLAGS
:
3205 case FS_IOC_SETVERSION
:
3206 error
= file_has_perm(cred
, file
, FILE__SETATTR
);
3209 /* sys_ioctl() checks */
3213 error
= file_has_perm(cred
, file
, 0);
3218 error
= cred_has_capability(cred
, CAP_SYS_TTY_CONFIG
,
3219 SECURITY_CAP_AUDIT
);
3222 /* default case assumes that the command will go
3223 * to the file's ioctl() function.
3226 error
= ioctl_has_perm(cred
, file
, FILE__IOCTL
, (u16
) cmd
);
3231 static int default_noexec
;
3233 static int file_map_prot_check(struct file
*file
, unsigned long prot
, int shared
)
3235 const struct cred
*cred
= current_cred();
3238 if (default_noexec
&&
3239 (prot
& PROT_EXEC
) && (!file
|| (!shared
&& (prot
& PROT_WRITE
)))) {
3241 * We are making executable an anonymous mapping or a
3242 * private file mapping that will also be writable.
3243 * This has an additional check.
3245 rc
= cred_has_perm(cred
, cred
, PROCESS__EXECMEM
);
3251 /* read access is always possible with a mapping */
3252 u32 av
= FILE__READ
;
3254 /* write access only matters if the mapping is shared */
3255 if (shared
&& (prot
& PROT_WRITE
))
3258 if (prot
& PROT_EXEC
)
3259 av
|= FILE__EXECUTE
;
3261 return file_has_perm(cred
, file
, av
);
3268 static int selinux_mmap_addr(unsigned long addr
)
3271 u32 sid
= current_sid();
3274 * notice that we are intentionally putting the SELinux check before
3275 * the secondary cap_file_mmap check. This is such a likely attempt
3276 * at bad behaviour/exploit that we always want to get the AVC, even
3277 * if DAC would have also denied the operation.
3279 if (addr
< CONFIG_LSM_MMAP_MIN_ADDR
) {
3280 rc
= avc_has_perm(sid
, sid
, SECCLASS_MEMPROTECT
,
3281 MEMPROTECT__MMAP_ZERO
, NULL
);
3286 /* do DAC check on address space usage */
3287 return cap_mmap_addr(addr
);
3290 static int selinux_mmap_file(struct file
*file
, unsigned long reqprot
,
3291 unsigned long prot
, unsigned long flags
)
3293 if (selinux_checkreqprot
)
3296 return file_map_prot_check(file
, prot
,
3297 (flags
& MAP_TYPE
) == MAP_SHARED
);
3300 static int selinux_file_mprotect(struct vm_area_struct
*vma
,
3301 unsigned long reqprot
,
3304 const struct cred
*cred
= current_cred();
3306 if (selinux_checkreqprot
)
3309 if (default_noexec
&&
3310 (prot
& PROT_EXEC
) && !(vma
->vm_flags
& VM_EXEC
)) {
3312 if (vma
->vm_start
>= vma
->vm_mm
->start_brk
&&
3313 vma
->vm_end
<= vma
->vm_mm
->brk
) {
3314 rc
= cred_has_perm(cred
, cred
, PROCESS__EXECHEAP
);
3315 } else if (!vma
->vm_file
&&
3316 vma
->vm_start
<= vma
->vm_mm
->start_stack
&&
3317 vma
->vm_end
>= vma
->vm_mm
->start_stack
) {
3318 rc
= current_has_perm(current
, PROCESS__EXECSTACK
);
3319 } else if (vma
->vm_file
&& vma
->anon_vma
) {
3321 * We are making executable a file mapping that has
3322 * had some COW done. Since pages might have been
3323 * written, check ability to execute the possibly
3324 * modified content. This typically should only
3325 * occur for text relocations.
3327 rc
= file_has_perm(cred
, vma
->vm_file
, FILE__EXECMOD
);
3333 return file_map_prot_check(vma
->vm_file
, prot
, vma
->vm_flags
&VM_SHARED
);
3336 static int selinux_file_lock(struct file
*file
, unsigned int cmd
)
3338 const struct cred
*cred
= current_cred();
3340 return file_has_perm(cred
, file
, FILE__LOCK
);
3343 static int selinux_file_fcntl(struct file
*file
, unsigned int cmd
,
3346 const struct cred
*cred
= current_cred();
3351 if ((file
->f_flags
& O_APPEND
) && !(arg
& O_APPEND
)) {
3352 err
= file_has_perm(cred
, file
, FILE__WRITE
);
3361 case F_GETOWNER_UIDS
:
3362 /* Just check FD__USE permission */
3363 err
= file_has_perm(cred
, file
, 0);
3368 #if BITS_PER_LONG == 32
3373 err
= file_has_perm(cred
, file
, FILE__LOCK
);
3380 static int selinux_file_set_fowner(struct file
*file
)
3382 struct file_security_struct
*fsec
;
3384 fsec
= file
->f_security
;
3385 fsec
->fown_sid
= current_sid();
3390 static int selinux_file_send_sigiotask(struct task_struct
*tsk
,
3391 struct fown_struct
*fown
, int signum
)
3394 u32 sid
= task_sid(tsk
);
3396 struct file_security_struct
*fsec
;
3398 /* struct fown_struct is never outside the context of a struct file */
3399 file
= container_of(fown
, struct file
, f_owner
);
3401 fsec
= file
->f_security
;
3404 perm
= signal_to_av(SIGIO
); /* as per send_sigio_to_task */
3406 perm
= signal_to_av(signum
);
3408 return avc_has_perm(fsec
->fown_sid
, sid
,
3409 SECCLASS_PROCESS
, perm
, NULL
);
3412 static int selinux_file_receive(struct file
*file
)
3414 const struct cred
*cred
= current_cred();
3416 return file_has_perm(cred
, file
, file_to_av(file
));
3419 static int selinux_file_open(struct file
*file
, const struct cred
*cred
)
3421 struct file_security_struct
*fsec
;
3422 struct inode_security_struct
*isec
;
3424 fsec
= file
->f_security
;
3425 isec
= file_inode(file
)->i_security
;
3427 * Save inode label and policy sequence number
3428 * at open-time so that selinux_file_permission
3429 * can determine whether revalidation is necessary.
3430 * Task label is already saved in the file security
3431 * struct as its SID.
3433 fsec
->isid
= isec
->sid
;
3434 fsec
->pseqno
= avc_policy_seqno();
3436 * Since the inode label or policy seqno may have changed
3437 * between the selinux_inode_permission check and the saving
3438 * of state above, recheck that access is still permitted.
3439 * Otherwise, access might never be revalidated against the
3440 * new inode label or new policy.
3441 * This check is not redundant - do not remove.
3443 return path_has_perm(cred
, &file
->f_path
, open_file_to_av(file
));
3446 /* task security operations */
3448 static int selinux_task_create(unsigned long clone_flags
)
3450 return current_has_perm(current
, PROCESS__FORK
);
3454 * allocate the SELinux part of blank credentials
3456 static int selinux_cred_alloc_blank(struct cred
*cred
, gfp_t gfp
)
3458 struct task_security_struct
*tsec
;
3460 tsec
= kzalloc(sizeof(struct task_security_struct
), gfp
);
3464 cred
->security
= tsec
;
3469 * detach and free the LSM part of a set of credentials
3471 static void selinux_cred_free(struct cred
*cred
)
3473 struct task_security_struct
*tsec
= cred
->security
;
3476 * cred->security == NULL if security_cred_alloc_blank() or
3477 * security_prepare_creds() returned an error.
3479 BUG_ON(cred
->security
&& (unsigned long) cred
->security
< PAGE_SIZE
);
3480 cred
->security
= (void *) 0x7UL
;
3485 * prepare a new set of credentials for modification
3487 static int selinux_cred_prepare(struct cred
*new, const struct cred
*old
,
3490 const struct task_security_struct
*old_tsec
;
3491 struct task_security_struct
*tsec
;
3493 old_tsec
= old
->security
;
3495 tsec
= kmemdup(old_tsec
, sizeof(struct task_security_struct
), gfp
);
3499 new->security
= tsec
;
3504 * transfer the SELinux data to a blank set of creds
3506 static void selinux_cred_transfer(struct cred
*new, const struct cred
*old
)
3508 const struct task_security_struct
*old_tsec
= old
->security
;
3509 struct task_security_struct
*tsec
= new->security
;
3515 * set the security data for a kernel service
3516 * - all the creation contexts are set to unlabelled
3518 static int selinux_kernel_act_as(struct cred
*new, u32 secid
)
3520 struct task_security_struct
*tsec
= new->security
;
3521 u32 sid
= current_sid();
3524 ret
= avc_has_perm(sid
, secid
,
3525 SECCLASS_KERNEL_SERVICE
,
3526 KERNEL_SERVICE__USE_AS_OVERRIDE
,
3530 tsec
->create_sid
= 0;
3531 tsec
->keycreate_sid
= 0;
3532 tsec
->sockcreate_sid
= 0;
3538 * set the file creation context in a security record to the same as the
3539 * objective context of the specified inode
3541 static int selinux_kernel_create_files_as(struct cred
*new, struct inode
*inode
)
3543 struct inode_security_struct
*isec
= inode
->i_security
;
3544 struct task_security_struct
*tsec
= new->security
;
3545 u32 sid
= current_sid();
3548 ret
= avc_has_perm(sid
, isec
->sid
,
3549 SECCLASS_KERNEL_SERVICE
,
3550 KERNEL_SERVICE__CREATE_FILES_AS
,
3554 tsec
->create_sid
= isec
->sid
;
3558 static int selinux_kernel_module_request(char *kmod_name
)
3561 struct common_audit_data ad
;
3563 sid
= task_sid(current
);
3565 ad
.type
= LSM_AUDIT_DATA_KMOD
;
3566 ad
.u
.kmod_name
= kmod_name
;
3568 return avc_has_perm(sid
, SECINITSID_KERNEL
, SECCLASS_SYSTEM
,
3569 SYSTEM__MODULE_REQUEST
, &ad
);
3572 static int selinux_task_setpgid(struct task_struct
*p
, pid_t pgid
)
3574 return current_has_perm(p
, PROCESS__SETPGID
);
3577 static int selinux_task_getpgid(struct task_struct
*p
)
3579 return current_has_perm(p
, PROCESS__GETPGID
);
3582 static int selinux_task_getsid(struct task_struct
*p
)
3584 return current_has_perm(p
, PROCESS__GETSESSION
);
3587 static void selinux_task_getsecid(struct task_struct
*p
, u32
*secid
)
3589 *secid
= task_sid(p
);
3592 static int selinux_task_setnice(struct task_struct
*p
, int nice
)
3596 rc
= cap_task_setnice(p
, nice
);
3600 return current_has_perm(p
, PROCESS__SETSCHED
);
3603 static int selinux_task_setioprio(struct task_struct
*p
, int ioprio
)
3606 rc
= cap_task_setioprio(p
, ioprio
);
3610 return current_has_perm(p
, PROCESS__SETSCHED
);
3613 static int selinux_task_getioprio(struct task_struct
*p
)
3615 return current_has_perm(p
, PROCESS__GETSCHED
);
3618 static int selinux_task_setrlimit(struct task_struct
*p
, unsigned int resource
,
3619 struct rlimit
*new_rlim
)
3621 struct rlimit
*old_rlim
= p
->signal
->rlim
+ resource
;
3623 /* Control the ability to change the hard limit (whether
3624 lowering or raising it), so that the hard limit can
3625 later be used as a safe reset point for the soft limit
3626 upon context transitions. See selinux_bprm_committing_creds. */
3627 if (old_rlim
->rlim_max
!= new_rlim
->rlim_max
)
3628 return current_has_perm(p
, PROCESS__SETRLIMIT
);
3633 static int selinux_task_setscheduler(struct task_struct
*p
)
3637 rc
= cap_task_setscheduler(p
);
3641 return current_has_perm(p
, PROCESS__SETSCHED
);
3644 static int selinux_task_getscheduler(struct task_struct
*p
)
3646 return current_has_perm(p
, PROCESS__GETSCHED
);
3649 static int selinux_task_movememory(struct task_struct
*p
)
3651 return current_has_perm(p
, PROCESS__SETSCHED
);
3654 static int selinux_task_kill(struct task_struct
*p
, struct siginfo
*info
,
3661 perm
= PROCESS__SIGNULL
; /* null signal; existence test */
3663 perm
= signal_to_av(sig
);
3665 rc
= avc_has_perm(secid
, task_sid(p
),
3666 SECCLASS_PROCESS
, perm
, NULL
);
3668 rc
= current_has_perm(p
, perm
);
3672 static int selinux_task_wait(struct task_struct
*p
)
3674 return task_has_perm(p
, current
, PROCESS__SIGCHLD
);
3677 static void selinux_task_to_inode(struct task_struct
*p
,
3678 struct inode
*inode
)
3680 struct inode_security_struct
*isec
= inode
->i_security
;
3681 u32 sid
= task_sid(p
);
3684 isec
->initialized
= 1;
3687 /* Returns error only if unable to parse addresses */
3688 static int selinux_parse_skb_ipv4(struct sk_buff
*skb
,
3689 struct common_audit_data
*ad
, u8
*proto
)
3691 int offset
, ihlen
, ret
= -EINVAL
;
3692 struct iphdr _iph
, *ih
;
3694 offset
= skb_network_offset(skb
);
3695 ih
= skb_header_pointer(skb
, offset
, sizeof(_iph
), &_iph
);
3699 ihlen
= ih
->ihl
* 4;
3700 if (ihlen
< sizeof(_iph
))
3703 ad
->u
.net
->v4info
.saddr
= ih
->saddr
;
3704 ad
->u
.net
->v4info
.daddr
= ih
->daddr
;
3708 *proto
= ih
->protocol
;
3710 switch (ih
->protocol
) {
3712 struct tcphdr _tcph
, *th
;
3714 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
3718 th
= skb_header_pointer(skb
, offset
, sizeof(_tcph
), &_tcph
);
3722 ad
->u
.net
->sport
= th
->source
;
3723 ad
->u
.net
->dport
= th
->dest
;
3728 struct udphdr _udph
, *uh
;
3730 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
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 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
3750 dh
= skb_header_pointer(skb
, offset
, sizeof(_dccph
), &_dccph
);
3754 ad
->u
.net
->sport
= dh
->dccph_sport
;
3755 ad
->u
.net
->dport
= dh
->dccph_dport
;
3766 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3768 /* Returns error only if unable to parse addresses */
3769 static int selinux_parse_skb_ipv6(struct sk_buff
*skb
,
3770 struct common_audit_data
*ad
, u8
*proto
)
3773 int ret
= -EINVAL
, offset
;
3774 struct ipv6hdr _ipv6h
, *ip6
;
3777 offset
= skb_network_offset(skb
);
3778 ip6
= skb_header_pointer(skb
, offset
, sizeof(_ipv6h
), &_ipv6h
);
3782 ad
->u
.net
->v6info
.saddr
= ip6
->saddr
;
3783 ad
->u
.net
->v6info
.daddr
= ip6
->daddr
;
3786 nexthdr
= ip6
->nexthdr
;
3787 offset
+= sizeof(_ipv6h
);
3788 offset
= ipv6_skip_exthdr(skb
, offset
, &nexthdr
, &frag_off
);
3797 struct tcphdr _tcph
, *th
;
3799 th
= skb_header_pointer(skb
, offset
, sizeof(_tcph
), &_tcph
);
3803 ad
->u
.net
->sport
= th
->source
;
3804 ad
->u
.net
->dport
= th
->dest
;
3809 struct udphdr _udph
, *uh
;
3811 uh
= skb_header_pointer(skb
, offset
, sizeof(_udph
), &_udph
);
3815 ad
->u
.net
->sport
= uh
->source
;
3816 ad
->u
.net
->dport
= uh
->dest
;
3820 case IPPROTO_DCCP
: {
3821 struct dccp_hdr _dccph
, *dh
;
3823 dh
= skb_header_pointer(skb
, offset
, sizeof(_dccph
), &_dccph
);
3827 ad
->u
.net
->sport
= dh
->dccph_sport
;
3828 ad
->u
.net
->dport
= dh
->dccph_dport
;
3832 /* includes fragments */
3842 static int selinux_parse_skb(struct sk_buff
*skb
, struct common_audit_data
*ad
,
3843 char **_addrp
, int src
, u8
*proto
)
3848 switch (ad
->u
.net
->family
) {
3850 ret
= selinux_parse_skb_ipv4(skb
, ad
, proto
);
3853 addrp
= (char *)(src
? &ad
->u
.net
->v4info
.saddr
:
3854 &ad
->u
.net
->v4info
.daddr
);
3857 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3859 ret
= selinux_parse_skb_ipv6(skb
, ad
, proto
);
3862 addrp
= (char *)(src
? &ad
->u
.net
->v6info
.saddr
:
3863 &ad
->u
.net
->v6info
.daddr
);
3873 "SELinux: failure in selinux_parse_skb(),"
3874 " unable to parse packet\n");
3884 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
3886 * @family: protocol family
3887 * @sid: the packet's peer label SID
3890 * Check the various different forms of network peer labeling and determine
3891 * the peer label/SID for the packet; most of the magic actually occurs in
3892 * the security server function security_net_peersid_cmp(). The function
3893 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3894 * or -EACCES if @sid is invalid due to inconsistencies with the different
3898 static int selinux_skb_peerlbl_sid(struct sk_buff
*skb
, u16 family
, u32
*sid
)
3905 selinux_xfrm_skb_sid(skb
, &xfrm_sid
);
3906 selinux_netlbl_skbuff_getsid(skb
, family
, &nlbl_type
, &nlbl_sid
);
3908 err
= security_net_peersid_resolve(nlbl_sid
, nlbl_type
, xfrm_sid
, sid
);
3909 if (unlikely(err
)) {
3911 "SELinux: failure in selinux_skb_peerlbl_sid(),"
3912 " unable to determine packet's peer label\n");
3920 * selinux_conn_sid - Determine the child socket label for a connection
3921 * @sk_sid: the parent socket's SID
3922 * @skb_sid: the packet's SID
3923 * @conn_sid: the resulting connection SID
3925 * If @skb_sid is valid then the user:role:type information from @sk_sid is
3926 * combined with the MLS information from @skb_sid in order to create
3927 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
3928 * of @sk_sid. Returns zero on success, negative values on failure.
3931 static int selinux_conn_sid(u32 sk_sid
, u32 skb_sid
, u32
*conn_sid
)
3935 if (skb_sid
!= SECSID_NULL
)
3936 err
= security_sid_mls_copy(sk_sid
, skb_sid
, conn_sid
);
3943 /* socket security operations */
3945 static int socket_sockcreate_sid(const struct task_security_struct
*tsec
,
3946 u16 secclass
, u32
*socksid
)
3948 if (tsec
->sockcreate_sid
> SECSID_NULL
) {
3949 *socksid
= tsec
->sockcreate_sid
;
3953 return security_transition_sid(tsec
->sid
, tsec
->sid
, secclass
, NULL
,
3957 static int sock_has_perm(struct task_struct
*task
, struct sock
*sk
, u32 perms
)
3959 struct sk_security_struct
*sksec
= sk
->sk_security
;
3960 struct common_audit_data ad
;
3961 struct lsm_network_audit net
= {0,};
3962 u32 tsid
= task_sid(task
);
3964 if (unlikely(!sksec
)){
3965 printk(KERN_CRIT
"[SELinux] sksec is NULL, socket is already freed. \n");
3969 if (sksec
->sid
== SECINITSID_KERNEL
)
3972 ad
.type
= LSM_AUDIT_DATA_NET
;
3976 return avc_has_perm(tsid
, sksec
->sid
, sksec
->sclass
, perms
, &ad
);
3979 static int selinux_socket_create(int family
, int type
,
3980 int protocol
, int kern
)
3982 const struct task_security_struct
*tsec
= current_security();
3990 secclass
= socket_type_to_security_class(family
, type
, protocol
);
3991 rc
= socket_sockcreate_sid(tsec
, secclass
, &newsid
);
3995 return avc_has_perm(tsec
->sid
, newsid
, secclass
, SOCKET__CREATE
, NULL
);
3998 static int selinux_socket_post_create(struct socket
*sock
, int family
,
3999 int type
, int protocol
, int kern
)
4001 const struct task_security_struct
*tsec
= current_security();
4002 struct inode_security_struct
*isec
= SOCK_INODE(sock
)->i_security
;
4003 struct sk_security_struct
*sksec
;
4006 isec
->sclass
= socket_type_to_security_class(family
, type
, protocol
);
4009 isec
->sid
= SECINITSID_KERNEL
;
4011 err
= socket_sockcreate_sid(tsec
, isec
->sclass
, &(isec
->sid
));
4016 isec
->initialized
= 1;
4019 sksec
= sock
->sk
->sk_security
;
4020 sksec
->sid
= isec
->sid
;
4021 sksec
->sclass
= isec
->sclass
;
4022 err
= selinux_netlbl_socket_post_create(sock
->sk
, family
);
4028 /* Range of port numbers used to automatically bind.
4029 Need to determine whether we should perform a name_bind
4030 permission check between the socket and the port number. */
4032 static int selinux_socket_bind(struct socket
*sock
, struct sockaddr
*address
, int addrlen
)
4034 struct sock
*sk
= sock
->sk
;
4038 err
= sock_has_perm(current
, sk
, SOCKET__BIND
);
4043 * If PF_INET or PF_INET6, check name_bind permission for the port.
4044 * Multiple address binding for SCTP is not supported yet: we just
4045 * check the first address now.
4047 family
= sk
->sk_family
;
4048 if (family
== PF_INET
|| family
== PF_INET6
) {
4050 struct sk_security_struct
*sksec
= sk
->sk_security
;
4051 struct common_audit_data ad
;
4052 struct lsm_network_audit net
= {0,};
4053 struct sockaddr_in
*addr4
= NULL
;
4054 struct sockaddr_in6
*addr6
= NULL
;
4055 unsigned short snum
;
4058 if (family
== PF_INET
) {
4059 addr4
= (struct sockaddr_in
*)address
;
4060 snum
= ntohs(addr4
->sin_port
);
4061 addrp
= (char *)&addr4
->sin_addr
.s_addr
;
4063 addr6
= (struct sockaddr_in6
*)address
;
4064 snum
= ntohs(addr6
->sin6_port
);
4065 addrp
= (char *)&addr6
->sin6_addr
.s6_addr
;
4071 inet_get_local_port_range(&low
, &high
);
4073 if (snum
< max(PROT_SOCK
, low
) || snum
> high
) {
4074 err
= sel_netport_sid(sk
->sk_protocol
,
4078 ad
.type
= LSM_AUDIT_DATA_NET
;
4080 ad
.u
.net
->sport
= htons(snum
);
4081 ad
.u
.net
->family
= family
;
4082 err
= avc_has_perm(sksec
->sid
, sid
,
4084 SOCKET__NAME_BIND
, &ad
);
4090 switch (sksec
->sclass
) {
4091 case SECCLASS_TCP_SOCKET
:
4092 node_perm
= TCP_SOCKET__NODE_BIND
;
4095 case SECCLASS_UDP_SOCKET
:
4096 node_perm
= UDP_SOCKET__NODE_BIND
;
4099 case SECCLASS_DCCP_SOCKET
:
4100 node_perm
= DCCP_SOCKET__NODE_BIND
;
4104 node_perm
= RAWIP_SOCKET__NODE_BIND
;
4108 err
= sel_netnode_sid(addrp
, family
, &sid
);
4112 ad
.type
= LSM_AUDIT_DATA_NET
;
4114 ad
.u
.net
->sport
= htons(snum
);
4115 ad
.u
.net
->family
= family
;
4117 if (family
== PF_INET
)
4118 ad
.u
.net
->v4info
.saddr
= addr4
->sin_addr
.s_addr
;
4120 ad
.u
.net
->v6info
.saddr
= addr6
->sin6_addr
;
4122 err
= avc_has_perm(sksec
->sid
, sid
,
4123 sksec
->sclass
, node_perm
, &ad
);
4131 static int selinux_socket_connect(struct socket
*sock
, struct sockaddr
*address
, int addrlen
)
4133 struct sock
*sk
= sock
->sk
;
4134 struct sk_security_struct
*sksec
= sk
->sk_security
;
4137 err
= sock_has_perm(current
, sk
, SOCKET__CONNECT
);
4142 * If a TCP or DCCP socket, check name_connect permission for the port.
4144 if (sksec
->sclass
== SECCLASS_TCP_SOCKET
||
4145 sksec
->sclass
== SECCLASS_DCCP_SOCKET
) {
4146 struct common_audit_data ad
;
4147 struct lsm_network_audit net
= {0,};
4148 struct sockaddr_in
*addr4
= NULL
;
4149 struct sockaddr_in6
*addr6
= NULL
;
4150 unsigned short snum
;
4153 if (sk
->sk_family
== PF_INET
) {
4154 addr4
= (struct sockaddr_in
*)address
;
4155 if (addrlen
< sizeof(struct sockaddr_in
))
4157 snum
= ntohs(addr4
->sin_port
);
4159 addr6
= (struct sockaddr_in6
*)address
;
4160 if (addrlen
< SIN6_LEN_RFC2133
)
4162 snum
= ntohs(addr6
->sin6_port
);
4165 err
= sel_netport_sid(sk
->sk_protocol
, snum
, &sid
);
4169 perm
= (sksec
->sclass
== SECCLASS_TCP_SOCKET
) ?
4170 TCP_SOCKET__NAME_CONNECT
: DCCP_SOCKET__NAME_CONNECT
;
4172 ad
.type
= LSM_AUDIT_DATA_NET
;
4174 ad
.u
.net
->dport
= htons(snum
);
4175 ad
.u
.net
->family
= sk
->sk_family
;
4176 err
= avc_has_perm(sksec
->sid
, sid
, sksec
->sclass
, perm
, &ad
);
4181 err
= selinux_netlbl_socket_connect(sk
, address
);
4187 static int selinux_socket_listen(struct socket
*sock
, int backlog
)
4189 return sock_has_perm(current
, sock
->sk
, SOCKET__LISTEN
);
4192 static int selinux_socket_accept(struct socket
*sock
, struct socket
*newsock
)
4195 struct inode_security_struct
*isec
;
4196 struct inode_security_struct
*newisec
;
4198 err
= sock_has_perm(current
, sock
->sk
, SOCKET__ACCEPT
);
4202 newisec
= SOCK_INODE(newsock
)->i_security
;
4204 isec
= SOCK_INODE(sock
)->i_security
;
4205 newisec
->sclass
= isec
->sclass
;
4206 newisec
->sid
= isec
->sid
;
4207 newisec
->initialized
= 1;
4212 static int selinux_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
4215 return sock_has_perm(current
, sock
->sk
, SOCKET__WRITE
);
4218 static int selinux_socket_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
4219 int size
, int flags
)
4221 return sock_has_perm(current
, sock
->sk
, SOCKET__READ
);
4224 static int selinux_socket_getsockname(struct socket
*sock
)
4226 return sock_has_perm(current
, sock
->sk
, SOCKET__GETATTR
);
4229 static int selinux_socket_getpeername(struct socket
*sock
)
4231 return sock_has_perm(current
, sock
->sk
, SOCKET__GETATTR
);
4234 static int selinux_socket_setsockopt(struct socket
*sock
, int level
, int optname
)
4238 err
= sock_has_perm(current
, sock
->sk
, SOCKET__SETOPT
);
4242 return selinux_netlbl_socket_setsockopt(sock
, level
, optname
);
4245 static int selinux_socket_getsockopt(struct socket
*sock
, int level
,
4248 return sock_has_perm(current
, sock
->sk
, SOCKET__GETOPT
);
4251 static int selinux_socket_shutdown(struct socket
*sock
, int how
)
4253 return sock_has_perm(current
, sock
->sk
, SOCKET__SHUTDOWN
);
4256 static int selinux_socket_unix_stream_connect(struct sock
*sock
,
4260 struct sk_security_struct
*sksec_sock
= sock
->sk_security
;
4261 struct sk_security_struct
*sksec_other
= other
->sk_security
;
4262 struct sk_security_struct
*sksec_new
= newsk
->sk_security
;
4263 struct common_audit_data ad
;
4264 struct lsm_network_audit net
= {0,};
4267 ad
.type
= LSM_AUDIT_DATA_NET
;
4269 ad
.u
.net
->sk
= other
;
4271 err
= avc_has_perm(sksec_sock
->sid
, sksec_other
->sid
,
4272 sksec_other
->sclass
,
4273 UNIX_STREAM_SOCKET__CONNECTTO
, &ad
);
4277 /* server child socket */
4278 sksec_new
->peer_sid
= sksec_sock
->sid
;
4279 err
= security_sid_mls_copy(sksec_other
->sid
, sksec_sock
->sid
,
4284 /* connecting socket */
4285 sksec_sock
->peer_sid
= sksec_new
->sid
;
4290 static int selinux_socket_unix_may_send(struct socket
*sock
,
4291 struct socket
*other
)
4293 struct sk_security_struct
*ssec
= sock
->sk
->sk_security
;
4294 struct sk_security_struct
*osec
= other
->sk
->sk_security
;
4295 struct common_audit_data ad
;
4296 struct lsm_network_audit net
= {0,};
4298 ad
.type
= LSM_AUDIT_DATA_NET
;
4300 ad
.u
.net
->sk
= other
->sk
;
4302 return avc_has_perm(ssec
->sid
, osec
->sid
, osec
->sclass
, SOCKET__SENDTO
,
4306 static int selinux_inet_sys_rcv_skb(int ifindex
, char *addrp
, u16 family
,
4308 struct common_audit_data
*ad
)
4314 err
= sel_netif_sid(ifindex
, &if_sid
);
4317 err
= avc_has_perm(peer_sid
, if_sid
,
4318 SECCLASS_NETIF
, NETIF__INGRESS
, ad
);
4322 err
= sel_netnode_sid(addrp
, family
, &node_sid
);
4325 return avc_has_perm(peer_sid
, node_sid
,
4326 SECCLASS_NODE
, NODE__RECVFROM
, ad
);
4329 static int selinux_sock_rcv_skb_compat(struct sock
*sk
, struct sk_buff
*skb
,
4333 struct sk_security_struct
*sksec
= sk
->sk_security
;
4334 u32 sk_sid
= sksec
->sid
;
4335 struct common_audit_data ad
;
4336 struct lsm_network_audit net
= {0,};
4339 ad
.type
= LSM_AUDIT_DATA_NET
;
4341 ad
.u
.net
->netif
= skb
->skb_iif
;
4342 ad
.u
.net
->family
= family
;
4343 err
= selinux_parse_skb(skb
, &ad
, &addrp
, 1, NULL
);
4347 if (selinux_secmark_enabled()) {
4348 err
= avc_has_perm(sk_sid
, skb
->secmark
, SECCLASS_PACKET
,
4354 err
= selinux_netlbl_sock_rcv_skb(sksec
, skb
, family
, &ad
);
4357 err
= selinux_xfrm_sock_rcv_skb(sksec
->sid
, skb
, &ad
);
4362 static int selinux_socket_sock_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
4365 struct sk_security_struct
*sksec
= sk
->sk_security
;
4366 u16 family
= sk
->sk_family
;
4367 u32 sk_sid
= sksec
->sid
;
4368 struct common_audit_data ad
;
4369 struct lsm_network_audit net
= {0,};
4374 if (family
!= PF_INET
&& family
!= PF_INET6
)
4377 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
4378 if (family
== PF_INET6
&& skb
->protocol
== htons(ETH_P_IP
))
4381 /* If any sort of compatibility mode is enabled then handoff processing
4382 * to the selinux_sock_rcv_skb_compat() function to deal with the
4383 * special handling. We do this in an attempt to keep this function
4384 * as fast and as clean as possible. */
4385 if (!selinux_policycap_netpeer
)
4386 return selinux_sock_rcv_skb_compat(sk
, skb
, family
);
4388 secmark_active
= selinux_secmark_enabled();
4389 peerlbl_active
= netlbl_enabled() || selinux_xfrm_enabled();
4390 if (!secmark_active
&& !peerlbl_active
)
4393 ad
.type
= LSM_AUDIT_DATA_NET
;
4395 ad
.u
.net
->netif
= skb
->skb_iif
;
4396 ad
.u
.net
->family
= family
;
4397 err
= selinux_parse_skb(skb
, &ad
, &addrp
, 1, NULL
);
4401 if (peerlbl_active
) {
4404 err
= selinux_skb_peerlbl_sid(skb
, family
, &peer_sid
);
4407 err
= selinux_inet_sys_rcv_skb(skb
->skb_iif
, addrp
, family
,
4410 selinux_netlbl_err(skb
, err
, 0);
4413 err
= avc_has_perm(sk_sid
, peer_sid
, SECCLASS_PEER
,
4416 selinux_netlbl_err(skb
, err
, 0);
4421 if (secmark_active
) {
4422 err
= avc_has_perm(sk_sid
, skb
->secmark
, SECCLASS_PACKET
,
4431 static int selinux_socket_getpeersec_stream(struct socket
*sock
, char __user
*optval
,
4432 int __user
*optlen
, unsigned len
)
4437 struct sk_security_struct
*sksec
= sock
->sk
->sk_security
;
4438 u32 peer_sid
= SECSID_NULL
;
4440 if (sksec
->sclass
== SECCLASS_UNIX_STREAM_SOCKET
||
4441 sksec
->sclass
== SECCLASS_TCP_SOCKET
)
4442 peer_sid
= sksec
->peer_sid
;
4443 if (peer_sid
== SECSID_NULL
)
4444 return -ENOPROTOOPT
;
4446 err
= security_sid_to_context(peer_sid
, &scontext
, &scontext_len
);
4450 if (scontext_len
> len
) {
4455 if (copy_to_user(optval
, scontext
, scontext_len
))
4459 if (put_user(scontext_len
, optlen
))
4465 static int selinux_socket_getpeersec_dgram(struct socket
*sock
, struct sk_buff
*skb
, u32
*secid
)
4467 u32 peer_secid
= SECSID_NULL
;
4470 if (skb
&& skb
->protocol
== htons(ETH_P_IP
))
4472 else if (skb
&& skb
->protocol
== htons(ETH_P_IPV6
))
4475 family
= sock
->sk
->sk_family
;
4479 if (sock
&& family
== PF_UNIX
)
4480 selinux_inode_getsecid(SOCK_INODE(sock
), &peer_secid
);
4482 selinux_skb_peerlbl_sid(skb
, family
, &peer_secid
);
4485 *secid
= peer_secid
;
4486 if (peer_secid
== SECSID_NULL
)
4491 static int selinux_sk_alloc_security(struct sock
*sk
, int family
, gfp_t priority
)
4493 struct sk_security_struct
*sksec
;
4495 sksec
= kzalloc(sizeof(*sksec
), priority
);
4499 sksec
->peer_sid
= SECINITSID_UNLABELED
;
4500 sksec
->sid
= SECINITSID_UNLABELED
;
4501 selinux_netlbl_sk_security_reset(sksec
);
4502 sk
->sk_security
= sksec
;
4507 static void selinux_sk_free_security(struct sock
*sk
)
4509 struct sk_security_struct
*sksec
= sk
->sk_security
;
4511 sk
->sk_security
= NULL
;
4512 selinux_netlbl_sk_security_free(sksec
);
4516 static void selinux_sk_clone_security(const struct sock
*sk
, struct sock
*newsk
)
4518 struct sk_security_struct
*sksec
= sk
->sk_security
;
4519 struct sk_security_struct
*newsksec
= newsk
->sk_security
;
4521 newsksec
->sid
= sksec
->sid
;
4522 newsksec
->peer_sid
= sksec
->peer_sid
;
4523 newsksec
->sclass
= sksec
->sclass
;
4525 selinux_netlbl_sk_security_reset(newsksec
);
4528 static void selinux_sk_getsecid(struct sock
*sk
, u32
*secid
)
4531 *secid
= SECINITSID_ANY_SOCKET
;
4533 struct sk_security_struct
*sksec
= sk
->sk_security
;
4535 *secid
= sksec
->sid
;
4539 static void selinux_sock_graft(struct sock
*sk
, struct socket
*parent
)
4541 struct inode_security_struct
*isec
= SOCK_INODE(parent
)->i_security
;
4542 struct sk_security_struct
*sksec
= sk
->sk_security
;
4544 if (sk
->sk_family
== PF_INET
|| sk
->sk_family
== PF_INET6
||
4545 sk
->sk_family
== PF_UNIX
)
4546 isec
->sid
= sksec
->sid
;
4547 sksec
->sclass
= isec
->sclass
;
4550 static int selinux_inet_conn_request(struct sock
*sk
, struct sk_buff
*skb
,
4551 struct request_sock
*req
)
4553 struct sk_security_struct
*sksec
= sk
->sk_security
;
4555 u16 family
= sk
->sk_family
;
4559 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4560 if (family
== PF_INET6
&& skb
->protocol
== htons(ETH_P_IP
))
4563 err
= selinux_skb_peerlbl_sid(skb
, family
, &peersid
);
4566 err
= selinux_conn_sid(sksec
->sid
, peersid
, &connsid
);
4569 req
->secid
= connsid
;
4570 req
->peer_secid
= peersid
;
4572 return selinux_netlbl_inet_conn_request(req
, family
);
4575 static void selinux_inet_csk_clone(struct sock
*newsk
,
4576 const struct request_sock
*req
)
4578 struct sk_security_struct
*newsksec
= newsk
->sk_security
;
4580 newsksec
->sid
= req
->secid
;
4581 newsksec
->peer_sid
= req
->peer_secid
;
4582 /* NOTE: Ideally, we should also get the isec->sid for the
4583 new socket in sync, but we don't have the isec available yet.
4584 So we will wait until sock_graft to do it, by which
4585 time it will have been created and available. */
4587 /* We don't need to take any sort of lock here as we are the only
4588 * thread with access to newsksec */
4589 selinux_netlbl_inet_csk_clone(newsk
, req
->rsk_ops
->family
);
4592 static void selinux_inet_conn_established(struct sock
*sk
, struct sk_buff
*skb
)
4594 u16 family
= sk
->sk_family
;
4595 struct sk_security_struct
*sksec
= sk
->sk_security
;
4597 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4598 if (family
== PF_INET6
&& skb
->protocol
== htons(ETH_P_IP
))
4601 selinux_skb_peerlbl_sid(skb
, family
, &sksec
->peer_sid
);
4604 static void selinux_skb_owned_by(struct sk_buff
*skb
, struct sock
*sk
)
4606 skb_set_owner_w(skb
, sk
);
4609 static int selinux_secmark_relabel_packet(u32 sid
)
4611 const struct task_security_struct
*__tsec
;
4614 __tsec
= current_security();
4617 return avc_has_perm(tsid
, sid
, SECCLASS_PACKET
, PACKET__RELABELTO
, NULL
);
4620 static void selinux_secmark_refcount_inc(void)
4622 atomic_inc(&selinux_secmark_refcount
);
4625 static void selinux_secmark_refcount_dec(void)
4627 atomic_dec(&selinux_secmark_refcount
);
4630 static void selinux_req_classify_flow(const struct request_sock
*req
,
4633 fl
->flowi_secid
= req
->secid
;
4636 static int selinux_tun_dev_alloc_security(void **security
)
4638 struct tun_security_struct
*tunsec
;
4640 tunsec
= kzalloc(sizeof(*tunsec
), GFP_KERNEL
);
4643 tunsec
->sid
= current_sid();
4649 static void selinux_tun_dev_free_security(void *security
)
4654 static int selinux_tun_dev_create(void)
4656 u32 sid
= current_sid();
4658 /* we aren't taking into account the "sockcreate" SID since the socket
4659 * that is being created here is not a socket in the traditional sense,
4660 * instead it is a private sock, accessible only to the kernel, and
4661 * representing a wide range of network traffic spanning multiple
4662 * connections unlike traditional sockets - check the TUN driver to
4663 * get a better understanding of why this socket is special */
4665 return avc_has_perm(sid
, sid
, SECCLASS_TUN_SOCKET
, TUN_SOCKET__CREATE
,
4669 static int selinux_tun_dev_attach_queue(void *security
)
4671 struct tun_security_struct
*tunsec
= security
;
4673 return avc_has_perm(current_sid(), tunsec
->sid
, SECCLASS_TUN_SOCKET
,
4674 TUN_SOCKET__ATTACH_QUEUE
, NULL
);
4677 static int selinux_tun_dev_attach(struct sock
*sk
, void *security
)
4679 struct tun_security_struct
*tunsec
= security
;
4680 struct sk_security_struct
*sksec
= sk
->sk_security
;
4682 /* we don't currently perform any NetLabel based labeling here and it
4683 * isn't clear that we would want to do so anyway; while we could apply
4684 * labeling without the support of the TUN user the resulting labeled
4685 * traffic from the other end of the connection would almost certainly
4686 * cause confusion to the TUN user that had no idea network labeling
4687 * protocols were being used */
4689 sksec
->sid
= tunsec
->sid
;
4690 sksec
->sclass
= SECCLASS_TUN_SOCKET
;
4695 static int selinux_tun_dev_open(void *security
)
4697 struct tun_security_struct
*tunsec
= security
;
4698 u32 sid
= current_sid();
4701 err
= avc_has_perm(sid
, tunsec
->sid
, SECCLASS_TUN_SOCKET
,
4702 TUN_SOCKET__RELABELFROM
, NULL
);
4705 err
= avc_has_perm(sid
, sid
, SECCLASS_TUN_SOCKET
,
4706 TUN_SOCKET__RELABELTO
, NULL
);
4714 static int selinux_nlmsg_perm(struct sock
*sk
, struct sk_buff
*skb
)
4718 struct nlmsghdr
*nlh
;
4719 struct sk_security_struct
*sksec
= sk
->sk_security
;
4721 if (skb
->len
< NLMSG_HDRLEN
) {
4725 nlh
= nlmsg_hdr(skb
);
4727 err
= selinux_nlmsg_lookup(sksec
->sclass
, nlh
->nlmsg_type
, &perm
);
4729 if (err
== -EINVAL
) {
4730 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_SELINUX_ERR
,
4731 "SELinux: unrecognized netlink message"
4732 " type=%hu for sclass=%hu\n",
4733 nlh
->nlmsg_type
, sksec
->sclass
);
4734 if (!selinux_enforcing
|| security_get_allow_unknown())
4744 err
= sock_has_perm(current
, sk
, perm
);
4749 #ifdef CONFIG_NETFILTER
4751 static unsigned int selinux_ip_forward(struct sk_buff
*skb
, int ifindex
,
4757 struct common_audit_data ad
;
4758 struct lsm_network_audit net
= {0,};
4763 if (!selinux_policycap_netpeer
)
4766 secmark_active
= selinux_secmark_enabled();
4767 netlbl_active
= netlbl_enabled();
4768 peerlbl_active
= netlbl_active
|| selinux_xfrm_enabled();
4769 if (!secmark_active
&& !peerlbl_active
)
4772 if (selinux_skb_peerlbl_sid(skb
, family
, &peer_sid
) != 0)
4775 ad
.type
= LSM_AUDIT_DATA_NET
;
4777 ad
.u
.net
->netif
= ifindex
;
4778 ad
.u
.net
->family
= family
;
4779 if (selinux_parse_skb(skb
, &ad
, &addrp
, 1, NULL
) != 0)
4782 if (peerlbl_active
) {
4783 err
= selinux_inet_sys_rcv_skb(ifindex
, addrp
, family
,
4786 selinux_netlbl_err(skb
, err
, 1);
4792 if (avc_has_perm(peer_sid
, skb
->secmark
,
4793 SECCLASS_PACKET
, PACKET__FORWARD_IN
, &ad
))
4797 /* we do this in the FORWARD path and not the POST_ROUTING
4798 * path because we want to make sure we apply the necessary
4799 * labeling before IPsec is applied so we can leverage AH
4801 if (selinux_netlbl_skbuff_setsid(skb
, family
, peer_sid
) != 0)
4807 static unsigned int selinux_ipv4_forward(unsigned int hooknum
,
4808 struct sk_buff
*skb
,
4809 const struct net_device
*in
,
4810 const struct net_device
*out
,
4811 int (*okfn
)(struct sk_buff
*))
4813 return selinux_ip_forward(skb
, in
->ifindex
, PF_INET
);
4816 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4817 static unsigned int selinux_ipv6_forward(unsigned int hooknum
,
4818 struct sk_buff
*skb
,
4819 const struct net_device
*in
,
4820 const struct net_device
*out
,
4821 int (*okfn
)(struct sk_buff
*))
4823 return selinux_ip_forward(skb
, in
->ifindex
, PF_INET6
);
4827 static unsigned int selinux_ip_output(struct sk_buff
*skb
,
4833 if (!netlbl_enabled())
4836 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4837 * because we want to make sure we apply the necessary labeling
4838 * before IPsec is applied so we can leverage AH protection */
4841 struct sk_security_struct
*sksec
;
4843 if (sk
->sk_state
== TCP_LISTEN
)
4844 /* if the socket is the listening state then this
4845 * packet is a SYN-ACK packet which means it needs to
4846 * be labeled based on the connection/request_sock and
4847 * not the parent socket. unfortunately, we can't
4848 * lookup the request_sock yet as it isn't queued on
4849 * the parent socket until after the SYN-ACK is sent.
4850 * the "solution" is to simply pass the packet as-is
4851 * as any IP option based labeling should be copied
4852 * from the initial connection request (in the IP
4853 * layer). it is far from ideal, but until we get a
4854 * security label in the packet itself this is the
4855 * best we can do. */
4858 /* standard practice, label using the parent socket */
4859 sksec
= sk
->sk_security
;
4862 sid
= SECINITSID_KERNEL
;
4863 if (selinux_netlbl_skbuff_setsid(skb
, family
, sid
) != 0)
4869 static unsigned int selinux_ipv4_output(unsigned int hooknum
,
4870 struct sk_buff
*skb
,
4871 const struct net_device
*in
,
4872 const struct net_device
*out
,
4873 int (*okfn
)(struct sk_buff
*))
4875 return selinux_ip_output(skb
, PF_INET
);
4878 static unsigned int selinux_ip_postroute_compat(struct sk_buff
*skb
,
4882 struct sock
*sk
= skb
->sk
;
4883 struct sk_security_struct
*sksec
;
4884 struct common_audit_data ad
;
4885 struct lsm_network_audit net
= {0,};
4891 sksec
= sk
->sk_security
;
4893 ad
.type
= LSM_AUDIT_DATA_NET
;
4895 ad
.u
.net
->netif
= ifindex
;
4896 ad
.u
.net
->family
= family
;
4897 if (selinux_parse_skb(skb
, &ad
, &addrp
, 0, &proto
))
4900 if (selinux_secmark_enabled())
4901 if (avc_has_perm(sksec
->sid
, skb
->secmark
,
4902 SECCLASS_PACKET
, PACKET__SEND
, &ad
))
4903 return NF_DROP_ERR(-ECONNREFUSED
);
4905 if (selinux_xfrm_postroute_last(sksec
->sid
, skb
, &ad
, proto
))
4906 return NF_DROP_ERR(-ECONNREFUSED
);
4911 static unsigned int selinux_ip_postroute(struct sk_buff
*skb
, int ifindex
,
4917 struct common_audit_data ad
;
4918 struct lsm_network_audit net
= {0,};
4923 /* If any sort of compatibility mode is enabled then handoff processing
4924 * to the selinux_ip_postroute_compat() function to deal with the
4925 * special handling. We do this in an attempt to keep this function
4926 * as fast and as clean as possible. */
4927 if (!selinux_policycap_netpeer
)
4928 return selinux_ip_postroute_compat(skb
, ifindex
, family
);
4930 secmark_active
= selinux_secmark_enabled();
4931 peerlbl_active
= netlbl_enabled() || selinux_xfrm_enabled();
4932 if (!secmark_active
&& !peerlbl_active
)
4938 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4939 * packet transformation so allow the packet to pass without any checks
4940 * since we'll have another chance to perform access control checks
4941 * when the packet is on it's final way out.
4942 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4943 * is NULL, in this case go ahead and apply access control.
4944 * is NULL, in this case go ahead and apply access control.
4945 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
4946 * TCP listening state we cannot wait until the XFRM processing
4947 * is done as we will miss out on the SA label if we do;
4948 * unfortunately, this means more work, but it is only once per
4950 if (skb_dst(skb
) != NULL
&& skb_dst(skb
)->xfrm
!= NULL
&&
4951 !(sk
!= NULL
&& sk
->sk_state
== TCP_LISTEN
))
4956 /* Without an associated socket the packet is either coming
4957 * from the kernel or it is being forwarded; check the packet
4958 * to determine which and if the packet is being forwarded
4959 * query the packet directly to determine the security label. */
4961 secmark_perm
= PACKET__FORWARD_OUT
;
4962 if (selinux_skb_peerlbl_sid(skb
, family
, &peer_sid
))
4965 secmark_perm
= PACKET__SEND
;
4966 peer_sid
= SECINITSID_KERNEL
;
4968 } else if (sk
->sk_state
== TCP_LISTEN
) {
4969 /* Locally generated packet but the associated socket is in the
4970 * listening state which means this is a SYN-ACK packet. In
4971 * this particular case the correct security label is assigned
4972 * to the connection/request_sock but unfortunately we can't
4973 * query the request_sock as it isn't queued on the parent
4974 * socket until after the SYN-ACK packet is sent; the only
4975 * viable choice is to regenerate the label like we do in
4976 * selinux_inet_conn_request(). See also selinux_ip_output()
4977 * for similar problems. */
4979 struct sk_security_struct
*sksec
= sk
->sk_security
;
4980 if (selinux_skb_peerlbl_sid(skb
, family
, &skb_sid
))
4982 /* At this point, if the returned skb peerlbl is SECSID_NULL
4983 * and the packet has been through at least one XFRM
4984 * transformation then we must be dealing with the "final"
4985 * form of labeled IPsec packet; since we've already applied
4986 * all of our access controls on this packet we can safely
4987 * pass the packet. */
4988 if (skb_sid
== SECSID_NULL
) {
4991 if (IPCB(skb
)->flags
& IPSKB_XFRM_TRANSFORMED
)
4995 if (IP6CB(skb
)->flags
& IP6SKB_XFRM_TRANSFORMED
)
4998 return NF_DROP_ERR(-ECONNREFUSED
);
5001 if (selinux_conn_sid(sksec
->sid
, skb_sid
, &peer_sid
))
5003 secmark_perm
= PACKET__SEND
;
5005 /* Locally generated packet, fetch the security label from the
5006 * associated socket. */
5007 struct sk_security_struct
*sksec
= sk
->sk_security
;
5008 peer_sid
= sksec
->sid
;
5009 secmark_perm
= PACKET__SEND
;
5012 ad
.type
= LSM_AUDIT_DATA_NET
;
5014 ad
.u
.net
->netif
= ifindex
;
5015 ad
.u
.net
->family
= family
;
5016 if (selinux_parse_skb(skb
, &ad
, &addrp
, 0, NULL
))
5020 if (avc_has_perm(peer_sid
, skb
->secmark
,
5021 SECCLASS_PACKET
, secmark_perm
, &ad
))
5022 return NF_DROP_ERR(-ECONNREFUSED
);
5024 if (peerlbl_active
) {
5028 if (sel_netif_sid(ifindex
, &if_sid
))
5030 if (avc_has_perm(peer_sid
, if_sid
,
5031 SECCLASS_NETIF
, NETIF__EGRESS
, &ad
))
5032 return NF_DROP_ERR(-ECONNREFUSED
);
5034 if (sel_netnode_sid(addrp
, family
, &node_sid
))
5036 if (avc_has_perm(peer_sid
, node_sid
,
5037 SECCLASS_NODE
, NODE__SENDTO
, &ad
))
5038 return NF_DROP_ERR(-ECONNREFUSED
);
5044 static unsigned int selinux_ipv4_postroute(unsigned int hooknum
,
5045 struct sk_buff
*skb
,
5046 const struct net_device
*in
,
5047 const struct net_device
*out
,
5048 int (*okfn
)(struct sk_buff
*))
5050 return selinux_ip_postroute(skb
, out
->ifindex
, PF_INET
);
5053 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5054 static unsigned int selinux_ipv6_postroute(unsigned int hooknum
,
5055 struct sk_buff
*skb
,
5056 const struct net_device
*in
,
5057 const struct net_device
*out
,
5058 int (*okfn
)(struct sk_buff
*))
5060 return selinux_ip_postroute(skb
, out
->ifindex
, PF_INET6
);
5064 #endif /* CONFIG_NETFILTER */
5066 static int selinux_netlink_send(struct sock
*sk
, struct sk_buff
*skb
)
5070 err
= cap_netlink_send(sk
, skb
);
5074 return selinux_nlmsg_perm(sk
, skb
);
5077 static int ipc_alloc_security(struct task_struct
*task
,
5078 struct kern_ipc_perm
*perm
,
5081 struct ipc_security_struct
*isec
;
5084 isec
= kzalloc(sizeof(struct ipc_security_struct
), GFP_KERNEL
);
5088 sid
= task_sid(task
);
5089 isec
->sclass
= sclass
;
5091 perm
->security
= isec
;
5096 static void ipc_free_security(struct kern_ipc_perm
*perm
)
5098 struct ipc_security_struct
*isec
= perm
->security
;
5099 perm
->security
= NULL
;
5103 static int msg_msg_alloc_security(struct msg_msg
*msg
)
5105 struct msg_security_struct
*msec
;
5107 msec
= kzalloc(sizeof(struct msg_security_struct
), GFP_KERNEL
);
5111 msec
->sid
= SECINITSID_UNLABELED
;
5112 msg
->security
= msec
;
5117 static void msg_msg_free_security(struct msg_msg
*msg
)
5119 struct msg_security_struct
*msec
= msg
->security
;
5121 msg
->security
= NULL
;
5125 static int ipc_has_perm(struct kern_ipc_perm
*ipc_perms
,
5128 struct ipc_security_struct
*isec
;
5129 struct common_audit_data ad
;
5130 u32 sid
= current_sid();
5132 isec
= ipc_perms
->security
;
5134 ad
.type
= LSM_AUDIT_DATA_IPC
;
5135 ad
.u
.ipc_id
= ipc_perms
->key
;
5137 return avc_has_perm(sid
, isec
->sid
, isec
->sclass
, perms
, &ad
);
5140 static int selinux_msg_msg_alloc_security(struct msg_msg
*msg
)
5142 return msg_msg_alloc_security(msg
);
5145 static void selinux_msg_msg_free_security(struct msg_msg
*msg
)
5147 msg_msg_free_security(msg
);
5150 /* message queue security operations */
5151 static int selinux_msg_queue_alloc_security(struct msg_queue
*msq
)
5153 struct ipc_security_struct
*isec
;
5154 struct common_audit_data ad
;
5155 u32 sid
= current_sid();
5158 rc
= ipc_alloc_security(current
, &msq
->q_perm
, SECCLASS_MSGQ
);
5162 isec
= msq
->q_perm
.security
;
5164 ad
.type
= LSM_AUDIT_DATA_IPC
;
5165 ad
.u
.ipc_id
= msq
->q_perm
.key
;
5167 rc
= avc_has_perm(sid
, isec
->sid
, SECCLASS_MSGQ
,
5170 ipc_free_security(&msq
->q_perm
);
5176 static void selinux_msg_queue_free_security(struct msg_queue
*msq
)
5178 ipc_free_security(&msq
->q_perm
);
5181 static int selinux_msg_queue_associate(struct msg_queue
*msq
, int msqflg
)
5183 struct ipc_security_struct
*isec
;
5184 struct common_audit_data ad
;
5185 u32 sid
= current_sid();
5187 isec
= msq
->q_perm
.security
;
5189 ad
.type
= LSM_AUDIT_DATA_IPC
;
5190 ad
.u
.ipc_id
= msq
->q_perm
.key
;
5192 return avc_has_perm(sid
, isec
->sid
, SECCLASS_MSGQ
,
5193 MSGQ__ASSOCIATE
, &ad
);
5196 static int selinux_msg_queue_msgctl(struct msg_queue
*msq
, int cmd
)
5204 /* No specific object, just general system-wide information. */
5205 return task_has_system(current
, SYSTEM__IPC_INFO
);
5208 perms
= MSGQ__GETATTR
| MSGQ__ASSOCIATE
;
5211 perms
= MSGQ__SETATTR
;
5214 perms
= MSGQ__DESTROY
;
5220 err
= ipc_has_perm(&msq
->q_perm
, perms
);
5224 static int selinux_msg_queue_msgsnd(struct msg_queue
*msq
, struct msg_msg
*msg
, int msqflg
)
5226 struct ipc_security_struct
*isec
;
5227 struct msg_security_struct
*msec
;
5228 struct common_audit_data ad
;
5229 u32 sid
= current_sid();
5232 isec
= msq
->q_perm
.security
;
5233 msec
= msg
->security
;
5236 * First time through, need to assign label to the message
5238 if (msec
->sid
== SECINITSID_UNLABELED
) {
5240 * Compute new sid based on current process and
5241 * message queue this message will be stored in
5243 rc
= security_transition_sid(sid
, isec
->sid
, SECCLASS_MSG
,
5249 ad
.type
= LSM_AUDIT_DATA_IPC
;
5250 ad
.u
.ipc_id
= msq
->q_perm
.key
;
5252 /* Can this process write to the queue? */
5253 rc
= avc_has_perm(sid
, isec
->sid
, SECCLASS_MSGQ
,
5256 /* Can this process send the message */
5257 rc
= avc_has_perm(sid
, msec
->sid
, SECCLASS_MSG
,
5260 /* Can the message be put in the queue? */
5261 rc
= avc_has_perm(msec
->sid
, isec
->sid
, SECCLASS_MSGQ
,
5262 MSGQ__ENQUEUE
, &ad
);
5267 static int selinux_msg_queue_msgrcv(struct msg_queue
*msq
, struct msg_msg
*msg
,
5268 struct task_struct
*target
,
5269 long type
, int mode
)
5271 struct ipc_security_struct
*isec
;
5272 struct msg_security_struct
*msec
;
5273 struct common_audit_data ad
;
5274 u32 sid
= task_sid(target
);
5277 isec
= msq
->q_perm
.security
;
5278 msec
= msg
->security
;
5280 ad
.type
= LSM_AUDIT_DATA_IPC
;
5281 ad
.u
.ipc_id
= msq
->q_perm
.key
;
5283 rc
= avc_has_perm(sid
, isec
->sid
,
5284 SECCLASS_MSGQ
, MSGQ__READ
, &ad
);
5286 rc
= avc_has_perm(sid
, msec
->sid
,
5287 SECCLASS_MSG
, MSG__RECEIVE
, &ad
);
5291 /* Shared Memory security operations */
5292 static int selinux_shm_alloc_security(struct shmid_kernel
*shp
)
5294 struct ipc_security_struct
*isec
;
5295 struct common_audit_data ad
;
5296 u32 sid
= current_sid();
5299 rc
= ipc_alloc_security(current
, &shp
->shm_perm
, SECCLASS_SHM
);
5303 isec
= shp
->shm_perm
.security
;
5305 ad
.type
= LSM_AUDIT_DATA_IPC
;
5306 ad
.u
.ipc_id
= shp
->shm_perm
.key
;
5308 rc
= avc_has_perm(sid
, isec
->sid
, SECCLASS_SHM
,
5311 ipc_free_security(&shp
->shm_perm
);
5317 static void selinux_shm_free_security(struct shmid_kernel
*shp
)
5319 ipc_free_security(&shp
->shm_perm
);
5322 static int selinux_shm_associate(struct shmid_kernel
*shp
, int shmflg
)
5324 struct ipc_security_struct
*isec
;
5325 struct common_audit_data ad
;
5326 u32 sid
= current_sid();
5328 isec
= shp
->shm_perm
.security
;
5330 ad
.type
= LSM_AUDIT_DATA_IPC
;
5331 ad
.u
.ipc_id
= shp
->shm_perm
.key
;
5333 return avc_has_perm(sid
, isec
->sid
, SECCLASS_SHM
,
5334 SHM__ASSOCIATE
, &ad
);
5337 /* Note, at this point, shp is locked down */
5338 static int selinux_shm_shmctl(struct shmid_kernel
*shp
, int cmd
)
5346 /* No specific object, just general system-wide information. */
5347 return task_has_system(current
, SYSTEM__IPC_INFO
);
5350 perms
= SHM__GETATTR
| SHM__ASSOCIATE
;
5353 perms
= SHM__SETATTR
;
5360 perms
= SHM__DESTROY
;
5366 err
= ipc_has_perm(&shp
->shm_perm
, perms
);
5370 static int selinux_shm_shmat(struct shmid_kernel
*shp
,
5371 char __user
*shmaddr
, int shmflg
)
5375 if (shmflg
& SHM_RDONLY
)
5378 perms
= SHM__READ
| SHM__WRITE
;
5380 return ipc_has_perm(&shp
->shm_perm
, perms
);
5383 /* Semaphore security operations */
5384 static int selinux_sem_alloc_security(struct sem_array
*sma
)
5386 struct ipc_security_struct
*isec
;
5387 struct common_audit_data ad
;
5388 u32 sid
= current_sid();
5391 rc
= ipc_alloc_security(current
, &sma
->sem_perm
, SECCLASS_SEM
);
5395 isec
= sma
->sem_perm
.security
;
5397 ad
.type
= LSM_AUDIT_DATA_IPC
;
5398 ad
.u
.ipc_id
= sma
->sem_perm
.key
;
5400 rc
= avc_has_perm(sid
, isec
->sid
, SECCLASS_SEM
,
5403 ipc_free_security(&sma
->sem_perm
);
5409 static void selinux_sem_free_security(struct sem_array
*sma
)
5411 ipc_free_security(&sma
->sem_perm
);
5414 static int selinux_sem_associate(struct sem_array
*sma
, int semflg
)
5416 struct ipc_security_struct
*isec
;
5417 struct common_audit_data ad
;
5418 u32 sid
= current_sid();
5420 isec
= sma
->sem_perm
.security
;
5422 ad
.type
= LSM_AUDIT_DATA_IPC
;
5423 ad
.u
.ipc_id
= sma
->sem_perm
.key
;
5425 return avc_has_perm(sid
, isec
->sid
, SECCLASS_SEM
,
5426 SEM__ASSOCIATE
, &ad
);
5429 /* Note, at this point, sma is locked down */
5430 static int selinux_sem_semctl(struct sem_array
*sma
, int cmd
)
5438 /* No specific object, just general system-wide information. */
5439 return task_has_system(current
, SYSTEM__IPC_INFO
);
5443 perms
= SEM__GETATTR
;
5454 perms
= SEM__DESTROY
;
5457 perms
= SEM__SETATTR
;
5461 perms
= SEM__GETATTR
| SEM__ASSOCIATE
;
5467 err
= ipc_has_perm(&sma
->sem_perm
, perms
);
5471 static int selinux_sem_semop(struct sem_array
*sma
,
5472 struct sembuf
*sops
, unsigned nsops
, int alter
)
5477 perms
= SEM__READ
| SEM__WRITE
;
5481 return ipc_has_perm(&sma
->sem_perm
, perms
);
5484 static int selinux_ipc_permission(struct kern_ipc_perm
*ipcp
, short flag
)
5490 av
|= IPC__UNIX_READ
;
5492 av
|= IPC__UNIX_WRITE
;
5497 return ipc_has_perm(ipcp
, av
);
5500 static void selinux_ipc_getsecid(struct kern_ipc_perm
*ipcp
, u32
*secid
)
5502 struct ipc_security_struct
*isec
= ipcp
->security
;
5506 static void selinux_d_instantiate(struct dentry
*dentry
, struct inode
*inode
)
5509 inode_doinit_with_dentry(inode
, dentry
);
5512 static int selinux_getprocattr(struct task_struct
*p
,
5513 char *name
, char **value
)
5515 const struct task_security_struct
*__tsec
;
5521 error
= current_has_perm(p
, PROCESS__GETATTR
);
5527 __tsec
= __task_cred(p
)->security
;
5529 if (!strcmp(name
, "current"))
5531 else if (!strcmp(name
, "prev"))
5533 else if (!strcmp(name
, "exec"))
5534 sid
= __tsec
->exec_sid
;
5535 else if (!strcmp(name
, "fscreate"))
5536 sid
= __tsec
->create_sid
;
5537 else if (!strcmp(name
, "keycreate"))
5538 sid
= __tsec
->keycreate_sid
;
5539 else if (!strcmp(name
, "sockcreate"))
5540 sid
= __tsec
->sockcreate_sid
;
5548 error
= security_sid_to_context(sid
, value
, &len
);
5558 static int selinux_setprocattr(struct task_struct
*p
,
5559 char *name
, void *value
, size_t size
)
5561 struct task_security_struct
*tsec
;
5562 struct task_struct
*tracer
;
5569 /* SELinux only allows a process to change its own
5570 security attributes. */
5575 * Basic control over ability to set these attributes at all.
5576 * current == p, but we'll pass them separately in case the
5577 * above restriction is ever removed.
5579 if (!strcmp(name
, "exec"))
5580 error
= current_has_perm(p
, PROCESS__SETEXEC
);
5581 else if (!strcmp(name
, "fscreate"))
5582 error
= current_has_perm(p
, PROCESS__SETFSCREATE
);
5583 else if (!strcmp(name
, "keycreate"))
5584 error
= current_has_perm(p
, PROCESS__SETKEYCREATE
);
5585 else if (!strcmp(name
, "sockcreate"))
5586 error
= current_has_perm(p
, PROCESS__SETSOCKCREATE
);
5587 else if (!strcmp(name
, "current"))
5588 error
= current_has_perm(p
, PROCESS__SETCURRENT
);
5594 /* Obtain a SID for the context, if one was specified. */
5595 if (size
&& str
[0] && str
[0] != '\n') {
5596 if (str
[size
-1] == '\n') {
5600 error
= security_context_to_sid(value
, size
, &sid
);
5601 if (error
== -EINVAL
&& !strcmp(name
, "fscreate")) {
5602 if (!capable(CAP_MAC_ADMIN
)) {
5603 struct audit_buffer
*ab
;
5606 /* We strip a nul only if it is at the end, otherwise the
5607 * context contains a nul and we should audit that */
5608 if (str
[size
- 1] == '\0')
5609 audit_size
= size
- 1;
5612 ab
= audit_log_start(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
);
5613 audit_log_format(ab
, "op=fscreate invalid_context=");
5614 audit_log_n_untrustedstring(ab
, value
, audit_size
);
5619 error
= security_context_to_sid_force(value
, size
,
5626 new = prepare_creds();
5630 /* Permission checking based on the specified context is
5631 performed during the actual operation (execve,
5632 open/mkdir/...), when we know the full context of the
5633 operation. See selinux_bprm_set_creds for the execve
5634 checks and may_create for the file creation checks. The
5635 operation will then fail if the context is not permitted. */
5636 tsec
= new->security
;
5637 if (!strcmp(name
, "exec")) {
5638 tsec
->exec_sid
= sid
;
5639 } else if (!strcmp(name
, "fscreate")) {
5640 tsec
->create_sid
= sid
;
5641 } else if (!strcmp(name
, "keycreate")) {
5642 error
= may_create_key(sid
, p
);
5645 tsec
->keycreate_sid
= sid
;
5646 } else if (!strcmp(name
, "sockcreate")) {
5647 tsec
->sockcreate_sid
= sid
;
5648 } else if (!strcmp(name
, "current")) {
5653 /* Only allow single threaded processes to change context */
5655 if (!current_is_single_threaded()) {
5656 error
= security_bounded_transition(tsec
->sid
, sid
);
5661 /* Check permissions for the transition. */
5662 error
= avc_has_perm(tsec
->sid
, sid
, SECCLASS_PROCESS
,
5663 PROCESS__DYNTRANSITION
, NULL
);
5667 /* Check for ptracing, and update the task SID if ok.
5668 Otherwise, leave SID unchanged and fail. */
5671 tracer
= ptrace_parent(p
);
5673 ptsid
= task_sid(tracer
);
5677 error
= avc_has_perm(ptsid
, sid
, SECCLASS_PROCESS
,
5678 PROCESS__PTRACE
, NULL
);
5697 static int selinux_secid_to_secctx(u32 secid
, char **secdata
, u32
*seclen
)
5699 return security_sid_to_context(secid
, secdata
, seclen
);
5702 static int selinux_secctx_to_secid(const char *secdata
, u32 seclen
, u32
*secid
)
5704 return security_context_to_sid(secdata
, seclen
, secid
);
5707 static void selinux_release_secctx(char *secdata
, u32 seclen
)
5713 * called with inode->i_mutex locked
5715 static int selinux_inode_notifysecctx(struct inode
*inode
, void *ctx
, u32 ctxlen
)
5717 return selinux_inode_setsecurity(inode
, XATTR_SELINUX_SUFFIX
, ctx
, ctxlen
, 0);
5721 * called with inode->i_mutex locked
5723 static int selinux_inode_setsecctx(struct dentry
*dentry
, void *ctx
, u32 ctxlen
)
5725 return __vfs_setxattr_noperm(dentry
, XATTR_NAME_SELINUX
, ctx
, ctxlen
, 0);
5728 static int selinux_inode_getsecctx(struct inode
*inode
, void **ctx
, u32
*ctxlen
)
5732 len
= selinux_inode_getsecurity(inode
, XATTR_SELINUX_SUFFIX
,
5741 static int selinux_key_alloc(struct key
*k
, const struct cred
*cred
,
5742 unsigned long flags
)
5744 const struct task_security_struct
*tsec
;
5745 struct key_security_struct
*ksec
;
5747 ksec
= kzalloc(sizeof(struct key_security_struct
), GFP_KERNEL
);
5751 tsec
= cred
->security
;
5752 if (tsec
->keycreate_sid
)
5753 ksec
->sid
= tsec
->keycreate_sid
;
5755 ksec
->sid
= tsec
->sid
;
5761 static void selinux_key_free(struct key
*k
)
5763 struct key_security_struct
*ksec
= k
->security
;
5769 static int selinux_key_permission(key_ref_t key_ref
,
5770 const struct cred
*cred
,
5774 struct key_security_struct
*ksec
;
5777 /* if no specific permissions are requested, we skip the
5778 permission check. No serious, additional covert channels
5779 appear to be created. */
5783 sid
= cred_sid(cred
);
5785 key
= key_ref_to_ptr(key_ref
);
5786 ksec
= key
->security
;
5788 return avc_has_perm(sid
, ksec
->sid
, SECCLASS_KEY
, perm
, NULL
);
5791 static int selinux_key_getsecurity(struct key
*key
, char **_buffer
)
5793 struct key_security_struct
*ksec
= key
->security
;
5794 char *context
= NULL
;
5798 rc
= security_sid_to_context(ksec
->sid
, &context
, &len
);
5807 static struct security_operations selinux_ops
= {
5810 .binder_set_context_mgr
= selinux_binder_set_context_mgr
,
5811 .binder_transaction
= selinux_binder_transaction
,
5812 .binder_transfer_binder
= selinux_binder_transfer_binder
,
5813 .binder_transfer_file
= selinux_binder_transfer_file
,
5815 .ptrace_access_check
= selinux_ptrace_access_check
,
5816 .ptrace_traceme
= selinux_ptrace_traceme
,
5817 .capget
= selinux_capget
,
5818 .capset
= selinux_capset
,
5819 .capable
= selinux_capable
,
5820 .quotactl
= selinux_quotactl
,
5821 .quota_on
= selinux_quota_on
,
5822 .syslog
= selinux_syslog
,
5823 .vm_enough_memory
= selinux_vm_enough_memory
,
5825 .netlink_send
= selinux_netlink_send
,
5827 .bprm_set_creds
= selinux_bprm_set_creds
,
5828 .bprm_committing_creds
= selinux_bprm_committing_creds
,
5829 .bprm_committed_creds
= selinux_bprm_committed_creds
,
5830 .bprm_secureexec
= selinux_bprm_secureexec
,
5832 .sb_alloc_security
= selinux_sb_alloc_security
,
5833 .sb_free_security
= selinux_sb_free_security
,
5834 .sb_copy_data
= selinux_sb_copy_data
,
5835 .sb_remount
= selinux_sb_remount
,
5836 .sb_kern_mount
= selinux_sb_kern_mount
,
5837 .sb_show_options
= selinux_sb_show_options
,
5838 .sb_statfs
= selinux_sb_statfs
,
5839 .sb_mount
= selinux_mount
,
5840 .sb_umount
= selinux_umount
,
5841 .sb_set_mnt_opts
= selinux_set_mnt_opts
,
5842 .sb_clone_mnt_opts
= selinux_sb_clone_mnt_opts
,
5843 .sb_parse_opts_str
= selinux_parse_opts_str
,
5846 .inode_alloc_security
= selinux_inode_alloc_security
,
5847 .inode_free_security
= selinux_inode_free_security
,
5848 .inode_init_security
= selinux_inode_init_security
,
5849 .inode_create
= selinux_inode_create
,
5850 .inode_link
= selinux_inode_link
,
5851 .inode_unlink
= selinux_inode_unlink
,
5852 .inode_symlink
= selinux_inode_symlink
,
5853 .inode_mkdir
= selinux_inode_mkdir
,
5854 .inode_rmdir
= selinux_inode_rmdir
,
5855 .inode_mknod
= selinux_inode_mknod
,
5856 .inode_rename
= selinux_inode_rename
,
5857 .inode_readlink
= selinux_inode_readlink
,
5858 .inode_follow_link
= selinux_inode_follow_link
,
5859 .inode_permission
= selinux_inode_permission
,
5860 .inode_setattr
= selinux_inode_setattr
,
5861 .inode_getattr
= selinux_inode_getattr
,
5862 .inode_setxattr
= selinux_inode_setxattr
,
5863 .inode_post_setxattr
= selinux_inode_post_setxattr
,
5864 .inode_getxattr
= selinux_inode_getxattr
,
5865 .inode_listxattr
= selinux_inode_listxattr
,
5866 .inode_removexattr
= selinux_inode_removexattr
,
5867 .inode_getsecurity
= selinux_inode_getsecurity
,
5868 .inode_setsecurity
= selinux_inode_setsecurity
,
5869 .inode_listsecurity
= selinux_inode_listsecurity
,
5870 .inode_getsecid
= selinux_inode_getsecid
,
5872 .file_permission
= selinux_file_permission
,
5873 .file_alloc_security
= selinux_file_alloc_security
,
5874 .file_free_security
= selinux_file_free_security
,
5875 .file_ioctl
= selinux_file_ioctl
,
5876 .mmap_file
= selinux_mmap_file
,
5877 .mmap_addr
= selinux_mmap_addr
,
5878 .file_mprotect
= selinux_file_mprotect
,
5879 .file_lock
= selinux_file_lock
,
5880 .file_fcntl
= selinux_file_fcntl
,
5881 .file_set_fowner
= selinux_file_set_fowner
,
5882 .file_send_sigiotask
= selinux_file_send_sigiotask
,
5883 .file_receive
= selinux_file_receive
,
5885 .file_open
= selinux_file_open
,
5887 .task_create
= selinux_task_create
,
5888 .cred_alloc_blank
= selinux_cred_alloc_blank
,
5889 .cred_free
= selinux_cred_free
,
5890 .cred_prepare
= selinux_cred_prepare
,
5891 .cred_transfer
= selinux_cred_transfer
,
5892 .kernel_act_as
= selinux_kernel_act_as
,
5893 .kernel_create_files_as
= selinux_kernel_create_files_as
,
5894 .kernel_module_request
= selinux_kernel_module_request
,
5895 .task_setpgid
= selinux_task_setpgid
,
5896 .task_getpgid
= selinux_task_getpgid
,
5897 .task_getsid
= selinux_task_getsid
,
5898 .task_getsecid
= selinux_task_getsecid
,
5899 .task_setnice
= selinux_task_setnice
,
5900 .task_setioprio
= selinux_task_setioprio
,
5901 .task_getioprio
= selinux_task_getioprio
,
5902 .task_setrlimit
= selinux_task_setrlimit
,
5903 .task_setscheduler
= selinux_task_setscheduler
,
5904 .task_getscheduler
= selinux_task_getscheduler
,
5905 .task_movememory
= selinux_task_movememory
,
5906 .task_kill
= selinux_task_kill
,
5907 .task_wait
= selinux_task_wait
,
5908 .task_to_inode
= selinux_task_to_inode
,
5910 .ipc_permission
= selinux_ipc_permission
,
5911 .ipc_getsecid
= selinux_ipc_getsecid
,
5913 .msg_msg_alloc_security
= selinux_msg_msg_alloc_security
,
5914 .msg_msg_free_security
= selinux_msg_msg_free_security
,
5916 .msg_queue_alloc_security
= selinux_msg_queue_alloc_security
,
5917 .msg_queue_free_security
= selinux_msg_queue_free_security
,
5918 .msg_queue_associate
= selinux_msg_queue_associate
,
5919 .msg_queue_msgctl
= selinux_msg_queue_msgctl
,
5920 .msg_queue_msgsnd
= selinux_msg_queue_msgsnd
,
5921 .msg_queue_msgrcv
= selinux_msg_queue_msgrcv
,
5923 .shm_alloc_security
= selinux_shm_alloc_security
,
5924 .shm_free_security
= selinux_shm_free_security
,
5925 .shm_associate
= selinux_shm_associate
,
5926 .shm_shmctl
= selinux_shm_shmctl
,
5927 .shm_shmat
= selinux_shm_shmat
,
5929 .sem_alloc_security
= selinux_sem_alloc_security
,
5930 .sem_free_security
= selinux_sem_free_security
,
5931 .sem_associate
= selinux_sem_associate
,
5932 .sem_semctl
= selinux_sem_semctl
,
5933 .sem_semop
= selinux_sem_semop
,
5935 .d_instantiate
= selinux_d_instantiate
,
5937 .getprocattr
= selinux_getprocattr
,
5938 .setprocattr
= selinux_setprocattr
,
5940 .secid_to_secctx
= selinux_secid_to_secctx
,
5941 .secctx_to_secid
= selinux_secctx_to_secid
,
5942 .release_secctx
= selinux_release_secctx
,
5943 .inode_notifysecctx
= selinux_inode_notifysecctx
,
5944 .inode_setsecctx
= selinux_inode_setsecctx
,
5945 .inode_getsecctx
= selinux_inode_getsecctx
,
5947 .unix_stream_connect
= selinux_socket_unix_stream_connect
,
5948 .unix_may_send
= selinux_socket_unix_may_send
,
5950 .socket_create
= selinux_socket_create
,
5951 .socket_post_create
= selinux_socket_post_create
,
5952 .socket_bind
= selinux_socket_bind
,
5953 .socket_connect
= selinux_socket_connect
,
5954 .socket_listen
= selinux_socket_listen
,
5955 .socket_accept
= selinux_socket_accept
,
5956 .socket_sendmsg
= selinux_socket_sendmsg
,
5957 .socket_recvmsg
= selinux_socket_recvmsg
,
5958 .socket_getsockname
= selinux_socket_getsockname
,
5959 .socket_getpeername
= selinux_socket_getpeername
,
5960 .socket_getsockopt
= selinux_socket_getsockopt
,
5961 .socket_setsockopt
= selinux_socket_setsockopt
,
5962 .socket_shutdown
= selinux_socket_shutdown
,
5963 .socket_sock_rcv_skb
= selinux_socket_sock_rcv_skb
,
5964 .socket_getpeersec_stream
= selinux_socket_getpeersec_stream
,
5965 .socket_getpeersec_dgram
= selinux_socket_getpeersec_dgram
,
5966 .sk_alloc_security
= selinux_sk_alloc_security
,
5967 .sk_free_security
= selinux_sk_free_security
,
5968 .sk_clone_security
= selinux_sk_clone_security
,
5969 .sk_getsecid
= selinux_sk_getsecid
,
5970 .sock_graft
= selinux_sock_graft
,
5971 .inet_conn_request
= selinux_inet_conn_request
,
5972 .inet_csk_clone
= selinux_inet_csk_clone
,
5973 .inet_conn_established
= selinux_inet_conn_established
,
5974 .secmark_relabel_packet
= selinux_secmark_relabel_packet
,
5975 .secmark_refcount_inc
= selinux_secmark_refcount_inc
,
5976 .secmark_refcount_dec
= selinux_secmark_refcount_dec
,
5977 .req_classify_flow
= selinux_req_classify_flow
,
5978 .tun_dev_alloc_security
= selinux_tun_dev_alloc_security
,
5979 .tun_dev_free_security
= selinux_tun_dev_free_security
,
5980 .tun_dev_create
= selinux_tun_dev_create
,
5981 .tun_dev_attach_queue
= selinux_tun_dev_attach_queue
,
5982 .tun_dev_attach
= selinux_tun_dev_attach
,
5983 .tun_dev_open
= selinux_tun_dev_open
,
5984 .skb_owned_by
= selinux_skb_owned_by
,
5986 #ifdef CONFIG_SECURITY_NETWORK_XFRM
5987 .xfrm_policy_alloc_security
= selinux_xfrm_policy_alloc
,
5988 .xfrm_policy_clone_security
= selinux_xfrm_policy_clone
,
5989 .xfrm_policy_free_security
= selinux_xfrm_policy_free
,
5990 .xfrm_policy_delete_security
= selinux_xfrm_policy_delete
,
5991 .xfrm_state_alloc_security
= selinux_xfrm_state_alloc
,
5992 .xfrm_state_free_security
= selinux_xfrm_state_free
,
5993 .xfrm_state_delete_security
= selinux_xfrm_state_delete
,
5994 .xfrm_policy_lookup
= selinux_xfrm_policy_lookup
,
5995 .xfrm_state_pol_flow_match
= selinux_xfrm_state_pol_flow_match
,
5996 .xfrm_decode_session
= selinux_xfrm_decode_session
,
6000 .key_alloc
= selinux_key_alloc
,
6001 .key_free
= selinux_key_free
,
6002 .key_permission
= selinux_key_permission
,
6003 .key_getsecurity
= selinux_key_getsecurity
,
6007 .audit_rule_init
= selinux_audit_rule_init
,
6008 .audit_rule_known
= selinux_audit_rule_known
,
6009 .audit_rule_match
= selinux_audit_rule_match
,
6010 .audit_rule_free
= selinux_audit_rule_free
,
6014 static __init
int selinux_init(void)
6016 if (!security_module_enable(&selinux_ops
)) {
6017 selinux_enabled
= 0;
6021 if (!selinux_enabled
) {
6022 printk(KERN_INFO
"SELinux: Disabled at boot.\n");
6026 printk(KERN_INFO
"SELinux: Initializing.\n");
6028 /* Set the security state for the initial task. */
6029 cred_init_security();
6031 default_noexec
= !(VM_DATA_DEFAULT_FLAGS
& VM_EXEC
);
6033 sel_inode_cache
= kmem_cache_create("selinux_inode_security",
6034 sizeof(struct inode_security_struct
),
6035 0, SLAB_PANIC
, NULL
);
6038 if (register_security(&selinux_ops
))
6039 panic("SELinux: Unable to register with kernel.\n");
6041 if (selinux_enforcing
)
6042 printk(KERN_DEBUG
"SELinux: Starting in enforcing mode\n");
6044 printk(KERN_DEBUG
"SELinux: Starting in permissive mode\n");
6049 static void delayed_superblock_init(struct super_block
*sb
, void *unused
)
6051 superblock_doinit(sb
, NULL
);
6054 void selinux_complete_init(void)
6056 printk(KERN_DEBUG
"SELinux: Completing initialization.\n");
6058 /* Set up any superblocks initialized prior to the policy load. */
6059 printk(KERN_DEBUG
"SELinux: Setting up existing superblocks.\n");
6060 iterate_supers(delayed_superblock_init
, NULL
);
6063 /* SELinux requires early initialization in order to label
6064 all processes and objects when they are created. */
6065 security_initcall(selinux_init
);
6067 #if defined(CONFIG_NETFILTER)
6069 static struct nf_hook_ops selinux_ipv4_ops
[] = {
6071 .hook
= selinux_ipv4_postroute
,
6072 .owner
= THIS_MODULE
,
6074 .hooknum
= NF_INET_POST_ROUTING
,
6075 .priority
= NF_IP_PRI_SELINUX_LAST
,
6078 .hook
= selinux_ipv4_forward
,
6079 .owner
= THIS_MODULE
,
6081 .hooknum
= NF_INET_FORWARD
,
6082 .priority
= NF_IP_PRI_SELINUX_FIRST
,
6085 .hook
= selinux_ipv4_output
,
6086 .owner
= THIS_MODULE
,
6088 .hooknum
= NF_INET_LOCAL_OUT
,
6089 .priority
= NF_IP_PRI_SELINUX_FIRST
,
6093 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6095 static struct nf_hook_ops selinux_ipv6_ops
[] = {
6097 .hook
= selinux_ipv6_postroute
,
6098 .owner
= THIS_MODULE
,
6100 .hooknum
= NF_INET_POST_ROUTING
,
6101 .priority
= NF_IP6_PRI_SELINUX_LAST
,
6104 .hook
= selinux_ipv6_forward
,
6105 .owner
= THIS_MODULE
,
6107 .hooknum
= NF_INET_FORWARD
,
6108 .priority
= NF_IP6_PRI_SELINUX_FIRST
,
6114 static int __init
selinux_nf_ip_init(void)
6117 if (!selinux_enabled
)
6120 printk(KERN_DEBUG
"SELinux: Registering netfilter hooks\n");
6122 err
= nf_register_hooks(selinux_ipv4_ops
, ARRAY_SIZE(selinux_ipv4_ops
));
6124 panic("SELinux: nf_register_hooks for IPv4: error %d\n", err
);
6126 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6127 err
= nf_register_hooks(selinux_ipv6_ops
, ARRAY_SIZE(selinux_ipv6_ops
));
6129 panic("SELinux: nf_register_hooks for IPv6: error %d\n", err
);
6136 __initcall(selinux_nf_ip_init
);
6138 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
6139 static void selinux_nf_ip_exit(void)
6141 printk(KERN_DEBUG
"SELinux: Unregistering netfilter hooks\n");
6143 nf_unregister_hooks(selinux_ipv4_ops
, ARRAY_SIZE(selinux_ipv4_ops
));
6144 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6145 nf_unregister_hooks(selinux_ipv6_ops
, ARRAY_SIZE(selinux_ipv6_ops
));
6150 #else /* CONFIG_NETFILTER */
6152 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
6153 #define selinux_nf_ip_exit()
6156 #endif /* CONFIG_NETFILTER */
6158 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
6159 static int selinux_disabled
;
6161 int selinux_disable(void)
6163 if (ss_initialized
) {
6164 /* Not permitted after initial policy load. */
6168 if (selinux_disabled
) {
6169 /* Only do this once. */
6173 printk(KERN_INFO
"SELinux: Disabled at runtime.\n");
6175 selinux_disabled
= 1;
6176 selinux_enabled
= 0;
6178 reset_security_ops();
6180 /* Try to destroy the avc node cache */
6183 /* Unregister netfilter hooks. */
6184 selinux_nf_ip_exit();
6186 /* Unregister selinuxfs. */