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 Red Hat, Inc., James Morris <jmorris@redhat.com>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2,
16 * as published by the Free Software Foundation.
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/ptrace.h>
24 #include <linux/errno.h>
25 #include <linux/sched.h>
26 #include <linux/security.h>
27 #include <linux/xattr.h>
28 #include <linux/capability.h>
29 #include <linux/unistd.h>
31 #include <linux/mman.h>
32 #include <linux/slab.h>
33 #include <linux/pagemap.h>
34 #include <linux/swap.h>
35 #include <linux/smp_lock.h>
36 #include <linux/spinlock.h>
37 #include <linux/syscalls.h>
38 #include <linux/file.h>
39 #include <linux/namei.h>
40 #include <linux/mount.h>
41 #include <linux/ext2_fs.h>
42 #include <linux/proc_fs.h>
44 #include <linux/netfilter_ipv4.h>
45 #include <linux/netfilter_ipv6.h>
46 #include <linux/tty.h>
48 #include <net/ip.h> /* for sysctl_local_port_range[] */
49 #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
50 #include <asm/uaccess.h>
51 #include <asm/semaphore.h>
52 #include <asm/ioctls.h>
53 #include <linux/bitops.h>
54 #include <linux/interrupt.h>
55 #include <linux/netdevice.h> /* for network interface checks */
56 #include <linux/netlink.h>
57 #include <linux/tcp.h>
58 #include <linux/udp.h>
59 #include <linux/quota.h>
60 #include <linux/un.h> /* for Unix socket types */
61 #include <net/af_unix.h> /* for Unix socket types */
62 #include <linux/parser.h>
63 #include <linux/nfs_mount.h>
65 #include <linux/hugetlb.h>
66 #include <linux/personality.h>
72 #define XATTR_SELINUX_SUFFIX "selinux"
73 #define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX
75 extern int policydb_loaded_version
;
76 extern int selinux_nlmsg_lookup(u16 sclass
, u16 nlmsg_type
, u32
*perm
);
78 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
79 int selinux_enforcing
= 0;
81 static int __init
enforcing_setup(char *str
)
83 selinux_enforcing
= simple_strtol(str
,NULL
,0);
86 __setup("enforcing=", enforcing_setup
);
89 #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
90 int selinux_enabled
= CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE
;
92 static int __init
selinux_enabled_setup(char *str
)
94 selinux_enabled
= simple_strtol(str
, NULL
, 0);
97 __setup("selinux=", selinux_enabled_setup
);
100 /* Original (dummy) security module. */
101 static struct security_operations
*original_ops
= NULL
;
103 /* Minimal support for a secondary security module,
104 just to allow the use of the dummy or capability modules.
105 The owlsm module can alternatively be used as a secondary
106 module as long as CONFIG_OWLSM_FD is not enabled. */
107 static struct security_operations
*secondary_ops
= NULL
;
109 /* Lists of inode and superblock security structures initialized
110 before the policy was loaded. */
111 static LIST_HEAD(superblock_security_head
);
112 static spinlock_t sb_security_lock
= SPIN_LOCK_UNLOCKED
;
114 /* Allocate and free functions for each kind of security blob. */
116 static int task_alloc_security(struct task_struct
*task
)
118 struct task_security_struct
*tsec
;
120 tsec
= kmalloc(sizeof(struct task_security_struct
), GFP_KERNEL
);
124 memset(tsec
, 0, sizeof(struct task_security_struct
));
125 tsec
->magic
= SELINUX_MAGIC
;
127 tsec
->osid
= tsec
->sid
= tsec
->ptrace_sid
= SECINITSID_UNLABELED
;
128 task
->security
= tsec
;
133 static void task_free_security(struct task_struct
*task
)
135 struct task_security_struct
*tsec
= task
->security
;
137 if (!tsec
|| tsec
->magic
!= SELINUX_MAGIC
)
140 task
->security
= NULL
;
144 static int inode_alloc_security(struct inode
*inode
)
146 struct task_security_struct
*tsec
= current
->security
;
147 struct inode_security_struct
*isec
;
149 isec
= kmalloc(sizeof(struct inode_security_struct
), GFP_KERNEL
);
153 memset(isec
, 0, sizeof(struct inode_security_struct
));
154 init_MUTEX(&isec
->sem
);
155 INIT_LIST_HEAD(&isec
->list
);
156 isec
->magic
= SELINUX_MAGIC
;
158 isec
->sid
= SECINITSID_UNLABELED
;
159 isec
->sclass
= SECCLASS_FILE
;
160 if (tsec
&& tsec
->magic
== SELINUX_MAGIC
)
161 isec
->task_sid
= tsec
->sid
;
163 isec
->task_sid
= SECINITSID_UNLABELED
;
164 inode
->i_security
= isec
;
169 static void inode_free_security(struct inode
*inode
)
171 struct inode_security_struct
*isec
= inode
->i_security
;
172 struct superblock_security_struct
*sbsec
= inode
->i_sb
->s_security
;
174 if (!isec
|| isec
->magic
!= SELINUX_MAGIC
)
177 spin_lock(&sbsec
->isec_lock
);
178 if (!list_empty(&isec
->list
))
179 list_del_init(&isec
->list
);
180 spin_unlock(&sbsec
->isec_lock
);
182 inode
->i_security
= NULL
;
186 static int file_alloc_security(struct file
*file
)
188 struct task_security_struct
*tsec
= current
->security
;
189 struct file_security_struct
*fsec
;
191 fsec
= kmalloc(sizeof(struct file_security_struct
), GFP_ATOMIC
);
195 memset(fsec
, 0, sizeof(struct file_security_struct
));
196 fsec
->magic
= SELINUX_MAGIC
;
198 if (tsec
&& tsec
->magic
== SELINUX_MAGIC
) {
199 fsec
->sid
= tsec
->sid
;
200 fsec
->fown_sid
= tsec
->sid
;
202 fsec
->sid
= SECINITSID_UNLABELED
;
203 fsec
->fown_sid
= SECINITSID_UNLABELED
;
205 file
->f_security
= fsec
;
210 static void file_free_security(struct file
*file
)
212 struct file_security_struct
*fsec
= file
->f_security
;
214 if (!fsec
|| fsec
->magic
!= SELINUX_MAGIC
)
217 file
->f_security
= NULL
;
221 static int superblock_alloc_security(struct super_block
*sb
)
223 struct superblock_security_struct
*sbsec
;
225 sbsec
= kmalloc(sizeof(struct superblock_security_struct
), GFP_KERNEL
);
229 memset(sbsec
, 0, sizeof(struct superblock_security_struct
));
230 init_MUTEX(&sbsec
->sem
);
231 INIT_LIST_HEAD(&sbsec
->list
);
232 INIT_LIST_HEAD(&sbsec
->isec_head
);
233 spin_lock_init(&sbsec
->isec_lock
);
234 sbsec
->magic
= SELINUX_MAGIC
;
236 sbsec
->sid
= SECINITSID_UNLABELED
;
237 sbsec
->def_sid
= SECINITSID_FILE
;
238 sb
->s_security
= sbsec
;
243 static void superblock_free_security(struct super_block
*sb
)
245 struct superblock_security_struct
*sbsec
= sb
->s_security
;
247 if (!sbsec
|| sbsec
->magic
!= SELINUX_MAGIC
)
250 spin_lock(&sb_security_lock
);
251 if (!list_empty(&sbsec
->list
))
252 list_del_init(&sbsec
->list
);
253 spin_unlock(&sb_security_lock
);
255 sb
->s_security
= NULL
;
259 #ifdef CONFIG_SECURITY_NETWORK
260 static int sk_alloc_security(struct sock
*sk
, int family
, int priority
)
262 struct sk_security_struct
*ssec
;
264 if (family
!= PF_UNIX
)
267 ssec
= kmalloc(sizeof(*ssec
), priority
);
271 memset(ssec
, 0, sizeof(*ssec
));
272 ssec
->magic
= SELINUX_MAGIC
;
274 ssec
->peer_sid
= SECINITSID_UNLABELED
;
275 sk
->sk_security
= ssec
;
280 static void sk_free_security(struct sock
*sk
)
282 struct sk_security_struct
*ssec
= sk
->sk_security
;
284 if (sk
->sk_family
!= PF_UNIX
|| ssec
->magic
!= SELINUX_MAGIC
)
287 sk
->sk_security
= NULL
;
290 #endif /* CONFIG_SECURITY_NETWORK */
292 /* The security server must be initialized before
293 any labeling or access decisions can be provided. */
294 extern int ss_initialized
;
296 /* The file system's label must be initialized prior to use. */
298 static char *labeling_behaviors
[6] = {
300 "uses transition SIDs",
302 "uses genfs_contexts",
303 "not configured for labeling",
304 "uses mountpoint labeling",
307 static int inode_doinit_with_dentry(struct inode
*inode
, struct dentry
*opt_dentry
);
309 static inline int inode_doinit(struct inode
*inode
)
311 return inode_doinit_with_dentry(inode
, NULL
);
320 static match_table_t tokens
= {
321 {Opt_context
, "context=%s"},
322 {Opt_fscontext
, "fscontext=%s"},
323 {Opt_defcontext
, "defcontext=%s"},
326 #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
328 static int try_context_mount(struct super_block
*sb
, void *data
)
330 char *context
= NULL
, *defcontext
= NULL
;
333 int alloc
= 0, rc
= 0, seen
= 0;
334 struct task_security_struct
*tsec
= current
->security
;
335 struct superblock_security_struct
*sbsec
= sb
->s_security
;
340 name
= sb
->s_type
->name
;
342 if (sb
->s_type
->fs_flags
& FS_BINARY_MOUNTDATA
) {
344 /* NFS we understand. */
345 if (!strcmp(name
, "nfs")) {
346 struct nfs_mount_data
*d
= data
;
348 if (d
->version
< NFS_MOUNT_VERSION
)
352 context
= d
->context
;
359 /* Standard string-based options. */
360 char *p
, *options
= data
;
362 while ((p
= strsep(&options
, ",")) != NULL
) {
364 substring_t args
[MAX_OPT_ARGS
];
369 token
= match_token(p
, tokens
, args
);
375 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG
);
378 context
= match_strdup(&args
[0]);
389 if (sbsec
->behavior
!= SECURITY_FS_USE_XATTR
) {
391 printk(KERN_WARNING
"SELinux: "
392 "fscontext option is invalid for"
393 " this filesystem type\n");
396 if (seen
& (Opt_context
|Opt_fscontext
)) {
398 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG
);
401 context
= match_strdup(&args
[0]);
408 seen
|= Opt_fscontext
;
412 if (sbsec
->behavior
!= SECURITY_FS_USE_XATTR
) {
414 printk(KERN_WARNING
"SELinux: "
415 "defcontext option is invalid "
416 "for this filesystem type\n");
419 if (seen
& (Opt_context
|Opt_defcontext
)) {
421 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG
);
424 defcontext
= match_strdup(&args
[0]);
431 seen
|= Opt_defcontext
;
436 printk(KERN_WARNING
"SELinux: unknown mount "
448 rc
= security_context_to_sid(context
, strlen(context
), &sid
);
450 printk(KERN_WARNING
"SELinux: security_context_to_sid"
451 "(%s) failed for (dev %s, type %s) errno=%d\n",
452 context
, sb
->s_id
, name
, rc
);
456 rc
= avc_has_perm(tsec
->sid
, sbsec
->sid
, SECCLASS_FILESYSTEM
,
457 FILESYSTEM__RELABELFROM
, NULL
, NULL
);
461 rc
= avc_has_perm(tsec
->sid
, sid
, SECCLASS_FILESYSTEM
,
462 FILESYSTEM__RELABELTO
, NULL
, NULL
);
468 if (seen
& Opt_context
)
469 sbsec
->behavior
= SECURITY_FS_USE_MNTPOINT
;
473 rc
= security_context_to_sid(defcontext
, strlen(defcontext
), &sid
);
475 printk(KERN_WARNING
"SELinux: security_context_to_sid"
476 "(%s) failed for (dev %s, type %s) errno=%d\n",
477 defcontext
, sb
->s_id
, name
, rc
);
481 if (sid
== sbsec
->def_sid
)
484 rc
= avc_has_perm(tsec
->sid
, sbsec
->sid
, SECCLASS_FILESYSTEM
,
485 FILESYSTEM__RELABELFROM
, NULL
, NULL
);
489 rc
= avc_has_perm(sid
, sbsec
->sid
, SECCLASS_FILESYSTEM
,
490 FILESYSTEM__ASSOCIATE
, NULL
, NULL
);
494 sbsec
->def_sid
= sid
;
506 static int superblock_doinit(struct super_block
*sb
, void *data
)
508 struct superblock_security_struct
*sbsec
= sb
->s_security
;
509 struct dentry
*root
= sb
->s_root
;
510 struct inode
*inode
= root
->d_inode
;
514 if (sbsec
->initialized
)
517 if (!ss_initialized
) {
518 /* Defer initialization until selinux_complete_init,
519 after the initial policy is loaded and the security
520 server is ready to handle calls. */
521 spin_lock(&sb_security_lock
);
522 if (list_empty(&sbsec
->list
))
523 list_add(&sbsec
->list
, &superblock_security_head
);
524 spin_unlock(&sb_security_lock
);
528 /* Determine the labeling behavior to use for this filesystem type. */
529 rc
= security_fs_use(sb
->s_type
->name
, &sbsec
->behavior
, &sbsec
->sid
);
531 printk(KERN_WARNING
"%s: security_fs_use(%s) returned %d\n",
532 __FUNCTION__
, sb
->s_type
->name
, rc
);
536 rc
= try_context_mount(sb
, data
);
540 if (sbsec
->behavior
== SECURITY_FS_USE_XATTR
) {
541 /* Make sure that the xattr handler exists and that no
542 error other than -ENODATA is returned by getxattr on
543 the root directory. -ENODATA is ok, as this may be
544 the first boot of the SELinux kernel before we have
545 assigned xattr values to the filesystem. */
546 if (!inode
->i_op
->getxattr
) {
547 printk(KERN_WARNING
"SELinux: (dev %s, type %s) has no "
548 "xattr support\n", sb
->s_id
, sb
->s_type
->name
);
552 rc
= inode
->i_op
->getxattr(root
, XATTR_NAME_SELINUX
, NULL
, 0);
553 if (rc
< 0 && rc
!= -ENODATA
) {
554 if (rc
== -EOPNOTSUPP
)
555 printk(KERN_WARNING
"SELinux: (dev %s, type "
556 "%s) has no security xattr handler\n",
557 sb
->s_id
, sb
->s_type
->name
);
559 printk(KERN_WARNING
"SELinux: (dev %s, type "
560 "%s) getxattr errno %d\n", sb
->s_id
,
561 sb
->s_type
->name
, -rc
);
566 if (strcmp(sb
->s_type
->name
, "proc") == 0)
569 sbsec
->initialized
= 1;
571 if (sbsec
->behavior
> ARRAY_SIZE(labeling_behaviors
)) {
572 printk(KERN_INFO
"SELinux: initialized (dev %s, type %s), unknown behavior\n",
573 sb
->s_id
, sb
->s_type
->name
);
576 printk(KERN_INFO
"SELinux: initialized (dev %s, type %s), %s\n",
577 sb
->s_id
, sb
->s_type
->name
,
578 labeling_behaviors
[sbsec
->behavior
-1]);
581 /* Initialize the root inode. */
582 rc
= inode_doinit_with_dentry(sb
->s_root
->d_inode
, sb
->s_root
);
584 /* Initialize any other inodes associated with the superblock, e.g.
585 inodes created prior to initial policy load or inodes created
586 during get_sb by a pseudo filesystem that directly
588 spin_lock(&sbsec
->isec_lock
);
590 if (!list_empty(&sbsec
->isec_head
)) {
591 struct inode_security_struct
*isec
=
592 list_entry(sbsec
->isec_head
.next
,
593 struct inode_security_struct
, list
);
594 struct inode
*inode
= isec
->inode
;
595 spin_unlock(&sbsec
->isec_lock
);
596 inode
= igrab(inode
);
601 spin_lock(&sbsec
->isec_lock
);
602 list_del_init(&isec
->list
);
605 spin_unlock(&sbsec
->isec_lock
);
611 static inline u16
inode_mode_to_security_class(umode_t mode
)
613 switch (mode
& S_IFMT
) {
615 return SECCLASS_SOCK_FILE
;
617 return SECCLASS_LNK_FILE
;
619 return SECCLASS_FILE
;
621 return SECCLASS_BLK_FILE
;
625 return SECCLASS_CHR_FILE
;
627 return SECCLASS_FIFO_FILE
;
631 return SECCLASS_FILE
;
634 static inline u16
socket_type_to_security_class(int family
, int type
, int protocol
)
640 return SECCLASS_UNIX_STREAM_SOCKET
;
642 return SECCLASS_UNIX_DGRAM_SOCKET
;
648 return SECCLASS_TCP_SOCKET
;
650 return SECCLASS_UDP_SOCKET
;
652 return SECCLASS_RAWIP_SOCKET
;
657 return SECCLASS_NETLINK_ROUTE_SOCKET
;
658 case NETLINK_FIREWALL
:
659 return SECCLASS_NETLINK_FIREWALL_SOCKET
;
660 case NETLINK_TCPDIAG
:
661 return SECCLASS_NETLINK_TCPDIAG_SOCKET
;
663 return SECCLASS_NETLINK_NFLOG_SOCKET
;
665 return SECCLASS_NETLINK_XFRM_SOCKET
;
666 case NETLINK_SELINUX
:
667 return SECCLASS_NETLINK_SELINUX_SOCKET
;
669 return SECCLASS_NETLINK_AUDIT_SOCKET
;
671 return SECCLASS_NETLINK_IP6FW_SOCKET
;
672 case NETLINK_DNRTMSG
:
673 return SECCLASS_NETLINK_DNRT_SOCKET
;
675 return SECCLASS_NETLINK_SOCKET
;
678 return SECCLASS_PACKET_SOCKET
;
680 return SECCLASS_KEY_SOCKET
;
683 return SECCLASS_SOCKET
;
686 #ifdef CONFIG_PROC_FS
687 static int selinux_proc_get_sid(struct proc_dir_entry
*de
,
692 char *buffer
, *path
, *end
;
694 buffer
= (char*)__get_free_page(GFP_KERNEL
);
704 while (de
&& de
!= de
->parent
) {
705 buflen
-= de
->namelen
+ 1;
709 memcpy(end
, de
->name
, de
->namelen
);
714 rc
= security_genfs_sid("proc", path
, tclass
, sid
);
715 free_page((unsigned long)buffer
);
719 static int selinux_proc_get_sid(struct proc_dir_entry
*de
,
727 /* The inode's security attributes must be initialized before first use. */
728 static int inode_doinit_with_dentry(struct inode
*inode
, struct dentry
*opt_dentry
)
730 struct superblock_security_struct
*sbsec
= NULL
;
731 struct inode_security_struct
*isec
= inode
->i_security
;
733 struct dentry
*dentry
;
734 #define INITCONTEXTLEN 255
735 char *context
= NULL
;
740 if (isec
->initialized
)
745 if (isec
->initialized
)
748 sbsec
= inode
->i_sb
->s_security
;
749 if (!sbsec
->initialized
) {
750 /* Defer initialization until selinux_complete_init,
751 after the initial policy is loaded and the security
752 server is ready to handle calls. */
753 spin_lock(&sbsec
->isec_lock
);
754 if (list_empty(&isec
->list
))
755 list_add(&isec
->list
, &sbsec
->isec_head
);
756 spin_unlock(&sbsec
->isec_lock
);
760 switch (sbsec
->behavior
) {
761 case SECURITY_FS_USE_XATTR
:
762 if (!inode
->i_op
->getxattr
) {
763 isec
->sid
= sbsec
->def_sid
;
767 /* Need a dentry, since the xattr API requires one.
768 Life would be simpler if we could just pass the inode. */
770 /* Called from d_instantiate or d_splice_alias. */
771 dentry
= dget(opt_dentry
);
773 /* Called from selinux_complete_init, try to find a dentry. */
774 dentry
= d_find_alias(inode
);
777 printk(KERN_WARNING
"%s: no dentry for dev=%s "
778 "ino=%ld\n", __FUNCTION__
, inode
->i_sb
->s_id
,
783 len
= INITCONTEXTLEN
;
784 context
= kmalloc(len
, GFP_KERNEL
);
790 rc
= inode
->i_op
->getxattr(dentry
, XATTR_NAME_SELINUX
,
793 /* Need a larger buffer. Query for the right size. */
794 rc
= inode
->i_op
->getxattr(dentry
, XATTR_NAME_SELINUX
,
802 context
= kmalloc(len
, GFP_KERNEL
);
808 rc
= inode
->i_op
->getxattr(dentry
,
814 if (rc
!= -ENODATA
) {
815 printk(KERN_WARNING
"%s: getxattr returned "
816 "%d for dev=%s ino=%ld\n", __FUNCTION__
,
817 -rc
, inode
->i_sb
->s_id
, inode
->i_ino
);
821 /* Map ENODATA to the default file SID */
822 sid
= sbsec
->def_sid
;
825 rc
= security_context_to_sid(context
, rc
, &sid
);
827 printk(KERN_WARNING
"%s: context_to_sid(%s) "
828 "returned %d for dev=%s ino=%ld\n",
829 __FUNCTION__
, context
, -rc
,
830 inode
->i_sb
->s_id
, inode
->i_ino
);
838 case SECURITY_FS_USE_TASK
:
839 isec
->sid
= isec
->task_sid
;
841 case SECURITY_FS_USE_TRANS
:
842 /* Default to the fs SID. */
843 isec
->sid
= sbsec
->sid
;
845 /* Try to obtain a transition SID. */
846 isec
->sclass
= inode_mode_to_security_class(inode
->i_mode
);
847 rc
= security_transition_sid(isec
->task_sid
,
856 /* Default to the fs SID. */
857 isec
->sid
= sbsec
->sid
;
860 struct proc_inode
*proci
= PROC_I(inode
);
862 isec
->sclass
= inode_mode_to_security_class(inode
->i_mode
);
863 rc
= selinux_proc_get_sid(proci
->pde
,
874 isec
->initialized
= 1;
878 struct socket
*sock
= SOCKET_I(inode
);
880 isec
->sclass
= socket_type_to_security_class(sock
->sk
->sk_family
,
882 sock
->sk
->sk_protocol
);
884 isec
->sclass
= SECCLASS_SOCKET
;
887 isec
->sclass
= inode_mode_to_security_class(inode
->i_mode
);
895 /* Convert a Linux signal to an access vector. */
896 static inline u32
signal_to_av(int sig
)
902 /* Commonly granted from child to parent. */
903 perm
= PROCESS__SIGCHLD
;
906 /* Cannot be caught or ignored */
907 perm
= PROCESS__SIGKILL
;
910 /* Cannot be caught or ignored */
911 perm
= PROCESS__SIGSTOP
;
914 /* All other signals. */
915 perm
= PROCESS__SIGNAL
;
922 /* Check permission betweeen a pair of tasks, e.g. signal checks,
923 fork check, ptrace check, etc. */
924 int task_has_perm(struct task_struct
*tsk1
,
925 struct task_struct
*tsk2
,
928 struct task_security_struct
*tsec1
, *tsec2
;
930 tsec1
= tsk1
->security
;
931 tsec2
= tsk2
->security
;
932 return avc_has_perm(tsec1
->sid
, tsec2
->sid
,
933 SECCLASS_PROCESS
, perms
, &tsec2
->avcr
, NULL
);
936 /* Check whether a task is allowed to use a capability. */
937 int task_has_capability(struct task_struct
*tsk
,
940 struct task_security_struct
*tsec
;
941 struct avc_audit_data ad
;
943 tsec
= tsk
->security
;
945 AVC_AUDIT_DATA_INIT(&ad
,CAP
);
949 return avc_has_perm(tsec
->sid
, tsec
->sid
,
950 SECCLASS_CAPABILITY
, CAP_TO_MASK(cap
), NULL
, &ad
);
953 /* Check whether a task is allowed to use a system operation. */
954 int task_has_system(struct task_struct
*tsk
,
957 struct task_security_struct
*tsec
;
959 tsec
= tsk
->security
;
961 return avc_has_perm(tsec
->sid
, SECINITSID_KERNEL
,
962 SECCLASS_SYSTEM
, perms
, NULL
, NULL
);
965 /* Check whether a task has a particular permission to an inode.
966 The 'aeref' parameter is optional and allows other AVC
967 entry references to be passed (e.g. the one in the struct file).
968 The 'adp' parameter is optional and allows other audit
969 data to be passed (e.g. the dentry). */
970 int inode_has_perm(struct task_struct
*tsk
,
973 struct avc_entry_ref
*aeref
,
974 struct avc_audit_data
*adp
)
976 struct task_security_struct
*tsec
;
977 struct inode_security_struct
*isec
;
978 struct avc_audit_data ad
;
980 tsec
= tsk
->security
;
981 isec
= inode
->i_security
;
985 AVC_AUDIT_DATA_INIT(&ad
, FS
);
986 ad
.u
.fs
.inode
= inode
;
989 return avc_has_perm(tsec
->sid
, isec
->sid
, isec
->sclass
,
990 perms
, aeref
? aeref
: &isec
->avcr
, adp
);
993 /* Same as inode_has_perm, but pass explicit audit data containing
994 the dentry to help the auditing code to more easily generate the
995 pathname if needed. */
996 static inline int dentry_has_perm(struct task_struct
*tsk
,
997 struct vfsmount
*mnt
,
998 struct dentry
*dentry
,
1001 struct inode
*inode
= dentry
->d_inode
;
1002 struct avc_audit_data ad
;
1003 AVC_AUDIT_DATA_INIT(&ad
,FS
);
1005 ad
.u
.fs
.dentry
= dentry
;
1006 return inode_has_perm(tsk
, inode
, av
, NULL
, &ad
);
1009 /* Check whether a task can use an open file descriptor to
1010 access an inode in a given way. Check access to the
1011 descriptor itself, and then use dentry_has_perm to
1012 check a particular permission to the file.
1013 Access to the descriptor is implicitly granted if it
1014 has the same SID as the process. If av is zero, then
1015 access to the file is not checked, e.g. for cases
1016 where only the descriptor is affected like seek. */
1017 static inline int file_has_perm(struct task_struct
*tsk
,
1021 struct task_security_struct
*tsec
= tsk
->security
;
1022 struct file_security_struct
*fsec
= file
->f_security
;
1023 struct vfsmount
*mnt
= file
->f_vfsmnt
;
1024 struct dentry
*dentry
= file
->f_dentry
;
1025 struct inode
*inode
= dentry
->d_inode
;
1026 struct avc_audit_data ad
;
1029 AVC_AUDIT_DATA_INIT(&ad
, FS
);
1031 ad
.u
.fs
.dentry
= dentry
;
1033 if (tsec
->sid
!= fsec
->sid
) {
1034 rc
= avc_has_perm(tsec
->sid
, fsec
->sid
,
1042 /* av is zero if only checking access to the descriptor. */
1044 return inode_has_perm(tsk
, inode
, av
, &fsec
->inode_avcr
, &ad
);
1049 /* Check whether a task can create a file. */
1050 static int may_create(struct inode
*dir
,
1051 struct dentry
*dentry
,
1054 struct task_security_struct
*tsec
;
1055 struct inode_security_struct
*dsec
;
1056 struct superblock_security_struct
*sbsec
;
1058 struct avc_audit_data ad
;
1061 tsec
= current
->security
;
1062 dsec
= dir
->i_security
;
1063 sbsec
= dir
->i_sb
->s_security
;
1065 AVC_AUDIT_DATA_INIT(&ad
, FS
);
1066 ad
.u
.fs
.dentry
= dentry
;
1068 rc
= avc_has_perm(tsec
->sid
, dsec
->sid
, SECCLASS_DIR
,
1069 DIR__ADD_NAME
| DIR__SEARCH
,
1074 if (tsec
->create_sid
&& sbsec
->behavior
!= SECURITY_FS_USE_MNTPOINT
) {
1075 newsid
= tsec
->create_sid
;
1077 rc
= security_transition_sid(tsec
->sid
, dsec
->sid
, tclass
,
1083 rc
= avc_has_perm(tsec
->sid
, newsid
, tclass
, FILE__CREATE
, NULL
, &ad
);
1087 return avc_has_perm(newsid
, sbsec
->sid
,
1088 SECCLASS_FILESYSTEM
,
1089 FILESYSTEM__ASSOCIATE
, NULL
, &ad
);
1093 #define MAY_UNLINK 1
1096 /* Check whether a task can link, unlink, or rmdir a file/directory. */
1097 static int may_link(struct inode
*dir
,
1098 struct dentry
*dentry
,
1102 struct task_security_struct
*tsec
;
1103 struct inode_security_struct
*dsec
, *isec
;
1104 struct avc_audit_data ad
;
1108 tsec
= current
->security
;
1109 dsec
= dir
->i_security
;
1110 isec
= dentry
->d_inode
->i_security
;
1112 AVC_AUDIT_DATA_INIT(&ad
, FS
);
1113 ad
.u
.fs
.dentry
= dentry
;
1116 av
|= (kind
? DIR__REMOVE_NAME
: DIR__ADD_NAME
);
1117 rc
= avc_has_perm(tsec
->sid
, dsec
->sid
, SECCLASS_DIR
,
1118 av
, &dsec
->avcr
, &ad
);
1133 printk(KERN_WARNING
"may_link: unrecognized kind %d\n", kind
);
1137 rc
= avc_has_perm(tsec
->sid
, isec
->sid
, isec
->sclass
,
1138 av
, &isec
->avcr
, &ad
);
1142 static inline int may_rename(struct inode
*old_dir
,
1143 struct dentry
*old_dentry
,
1144 struct inode
*new_dir
,
1145 struct dentry
*new_dentry
)
1147 struct task_security_struct
*tsec
;
1148 struct inode_security_struct
*old_dsec
, *new_dsec
, *old_isec
, *new_isec
;
1149 struct avc_audit_data ad
;
1151 int old_is_dir
, new_is_dir
;
1154 tsec
= current
->security
;
1155 old_dsec
= old_dir
->i_security
;
1156 old_isec
= old_dentry
->d_inode
->i_security
;
1157 old_is_dir
= S_ISDIR(old_dentry
->d_inode
->i_mode
);
1158 new_dsec
= new_dir
->i_security
;
1160 AVC_AUDIT_DATA_INIT(&ad
, FS
);
1162 ad
.u
.fs
.dentry
= old_dentry
;
1163 rc
= avc_has_perm(tsec
->sid
, old_dsec
->sid
, SECCLASS_DIR
,
1164 DIR__REMOVE_NAME
| DIR__SEARCH
,
1165 &old_dsec
->avcr
, &ad
);
1168 rc
= avc_has_perm(tsec
->sid
, old_isec
->sid
,
1171 &old_isec
->avcr
, &ad
);
1174 if (old_is_dir
&& new_dir
!= old_dir
) {
1175 rc
= avc_has_perm(tsec
->sid
, old_isec
->sid
,
1178 &old_isec
->avcr
, &ad
);
1183 ad
.u
.fs
.dentry
= new_dentry
;
1184 av
= DIR__ADD_NAME
| DIR__SEARCH
;
1185 if (new_dentry
->d_inode
)
1186 av
|= DIR__REMOVE_NAME
;
1187 rc
= avc_has_perm(tsec
->sid
, new_dsec
->sid
, SECCLASS_DIR
,
1188 av
,&new_dsec
->avcr
, &ad
);
1191 if (new_dentry
->d_inode
) {
1192 new_isec
= new_dentry
->d_inode
->i_security
;
1193 new_is_dir
= S_ISDIR(new_dentry
->d_inode
->i_mode
);
1194 rc
= avc_has_perm(tsec
->sid
, new_isec
->sid
,
1196 (new_is_dir
? DIR__RMDIR
: FILE__UNLINK
),
1197 &new_isec
->avcr
, &ad
);
1205 /* Check whether a task can perform a filesystem operation. */
1206 int superblock_has_perm(struct task_struct
*tsk
,
1207 struct super_block
*sb
,
1209 struct avc_audit_data
*ad
)
1211 struct task_security_struct
*tsec
;
1212 struct superblock_security_struct
*sbsec
;
1214 tsec
= tsk
->security
;
1215 sbsec
= sb
->s_security
;
1216 return avc_has_perm(tsec
->sid
, sbsec
->sid
, SECCLASS_FILESYSTEM
,
1220 /* Convert a Linux mode and permission mask to an access vector. */
1221 static inline u32
file_mask_to_av(int mode
, int mask
)
1225 if ((mode
& S_IFMT
) != S_IFDIR
) {
1226 if (mask
& MAY_EXEC
)
1227 av
|= FILE__EXECUTE
;
1228 if (mask
& MAY_READ
)
1231 if (mask
& MAY_APPEND
)
1233 else if (mask
& MAY_WRITE
)
1237 if (mask
& MAY_EXEC
)
1239 if (mask
& MAY_WRITE
)
1241 if (mask
& MAY_READ
)
1248 /* Convert a Linux file to an access vector. */
1249 static inline u32
file_to_av(struct file
*file
)
1253 if (file
->f_mode
& FMODE_READ
)
1255 if (file
->f_mode
& FMODE_WRITE
) {
1256 if (file
->f_flags
& O_APPEND
)
1265 /* Set an inode's SID to a specified value. */
1266 int inode_security_set_sid(struct inode
*inode
, u32 sid
)
1268 struct inode_security_struct
*isec
= inode
->i_security
;
1269 struct superblock_security_struct
*sbsec
= inode
->i_sb
->s_security
;
1271 if (!sbsec
->initialized
) {
1272 /* Defer initialization to selinux_complete_init. */
1277 isec
->sclass
= inode_mode_to_security_class(inode
->i_mode
);
1279 isec
->initialized
= 1;
1284 /* Set the security attributes on a newly created file. */
1285 static int post_create(struct inode
*dir
,
1286 struct dentry
*dentry
)
1289 struct task_security_struct
*tsec
;
1290 struct inode
*inode
;
1291 struct inode_security_struct
*dsec
;
1292 struct superblock_security_struct
*sbsec
;
1298 tsec
= current
->security
;
1299 dsec
= dir
->i_security
;
1300 sbsec
= dir
->i_sb
->s_security
;
1302 inode
= dentry
->d_inode
;
1304 /* Some file system types (e.g. NFS) may not instantiate
1305 a dentry for all create operations (e.g. symlink),
1306 so we have to check to see if the inode is non-NULL. */
1307 printk(KERN_WARNING
"post_create: no inode, dir (dev=%s, "
1308 "ino=%ld)\n", dir
->i_sb
->s_id
, dir
->i_ino
);
1312 if (tsec
->create_sid
&& sbsec
->behavior
!= SECURITY_FS_USE_MNTPOINT
) {
1313 newsid
= tsec
->create_sid
;
1315 rc
= security_transition_sid(tsec
->sid
, dsec
->sid
,
1316 inode_mode_to_security_class(inode
->i_mode
),
1319 printk(KERN_WARNING
"post_create: "
1320 "security_transition_sid failed, rc=%d (dev=%s "
1322 -rc
, inode
->i_sb
->s_id
, inode
->i_ino
);
1327 rc
= inode_security_set_sid(inode
, newsid
);
1329 printk(KERN_WARNING
"post_create: inode_security_set_sid "
1330 "failed, rc=%d (dev=%s ino=%ld)\n",
1331 -rc
, inode
->i_sb
->s_id
, inode
->i_ino
);
1335 if (sbsec
->behavior
== SECURITY_FS_USE_XATTR
&&
1336 inode
->i_op
->setxattr
) {
1337 /* Use extended attributes. */
1338 rc
= security_sid_to_context(newsid
, &context
, &len
);
1340 printk(KERN_WARNING
"post_create: sid_to_context "
1341 "failed, rc=%d (dev=%s ino=%ld)\n",
1342 -rc
, inode
->i_sb
->s_id
, inode
->i_ino
);
1345 down(&inode
->i_sem
);
1346 rc
= inode
->i_op
->setxattr(dentry
,
1352 printk(KERN_WARNING
"post_create: setxattr failed, "
1353 "rc=%d (dev=%s ino=%ld)\n",
1354 -rc
, inode
->i_sb
->s_id
, inode
->i_ino
);
1363 /* Hook functions begin here. */
1365 static int selinux_ptrace(struct task_struct
*parent
, struct task_struct
*child
)
1367 struct task_security_struct
*psec
= parent
->security
;
1368 struct task_security_struct
*csec
= child
->security
;
1371 rc
= secondary_ops
->ptrace(parent
,child
);
1375 rc
= task_has_perm(parent
, child
, PROCESS__PTRACE
);
1376 /* Save the SID of the tracing process for later use in apply_creds. */
1378 csec
->ptrace_sid
= psec
->sid
;
1382 static int selinux_capget(struct task_struct
*target
, kernel_cap_t
*effective
,
1383 kernel_cap_t
*inheritable
, kernel_cap_t
*permitted
)
1387 error
= task_has_perm(current
, target
, PROCESS__GETCAP
);
1391 return secondary_ops
->capget(target
, effective
, inheritable
, permitted
);
1394 static int selinux_capset_check(struct task_struct
*target
, kernel_cap_t
*effective
,
1395 kernel_cap_t
*inheritable
, kernel_cap_t
*permitted
)
1399 error
= secondary_ops
->capset_check(target
, effective
, inheritable
, permitted
);
1403 return task_has_perm(current
, target
, PROCESS__SETCAP
);
1406 static void selinux_capset_set(struct task_struct
*target
, kernel_cap_t
*effective
,
1407 kernel_cap_t
*inheritable
, kernel_cap_t
*permitted
)
1411 error
= task_has_perm(current
, target
, PROCESS__SETCAP
);
1415 secondary_ops
->capset_set(target
, effective
, inheritable
, permitted
);
1418 static int selinux_capable(struct task_struct
*tsk
, int cap
)
1422 rc
= secondary_ops
->capable(tsk
, cap
);
1426 return task_has_capability(tsk
,cap
);
1429 static int selinux_sysctl(ctl_table
*table
, int op
)
1433 struct task_security_struct
*tsec
;
1437 rc
= secondary_ops
->sysctl(table
, op
);
1441 tsec
= current
->security
;
1443 rc
= selinux_proc_get_sid(table
->de
, (op
== 001) ?
1444 SECCLASS_DIR
: SECCLASS_FILE
, &tsid
);
1446 /* Default to the well-defined sysctl SID. */
1447 tsid
= SECINITSID_SYSCTL
;
1450 /* The op values are "defined" in sysctl.c, thereby creating
1451 * a bad coupling between this module and sysctl.c */
1453 error
= avc_has_perm(tsec
->sid
, tsid
,
1454 SECCLASS_DIR
, DIR__SEARCH
, NULL
, NULL
);
1462 error
= avc_has_perm(tsec
->sid
, tsid
,
1463 SECCLASS_FILE
, av
, NULL
, NULL
);
1469 static int selinux_quotactl(int cmds
, int type
, int id
, struct super_block
*sb
)
1482 rc
= superblock_has_perm(current
,
1484 FILESYSTEM__QUOTAMOD
, NULL
);
1489 rc
= superblock_has_perm(current
,
1491 FILESYSTEM__QUOTAGET
, NULL
);
1494 rc
= 0; /* let the kernel handle invalid cmds */
1500 static int selinux_quota_on(struct file
*f
)
1502 return file_has_perm(current
, f
, FILE__QUOTAON
);
1505 static int selinux_syslog(int type
)
1509 rc
= secondary_ops
->syslog(type
);
1514 case 3: /* Read last kernel messages */
1515 case 10: /* Return size of the log buffer */
1516 rc
= task_has_system(current
, SYSTEM__SYSLOG_READ
);
1518 case 6: /* Disable logging to console */
1519 case 7: /* Enable logging to console */
1520 case 8: /* Set level of messages printed to console */
1521 rc
= task_has_system(current
, SYSTEM__SYSLOG_CONSOLE
);
1523 case 0: /* Close log */
1524 case 1: /* Open log */
1525 case 2: /* Read from log */
1526 case 4: /* Read/clear last kernel messages */
1527 case 5: /* Clear ring buffer */
1529 rc
= task_has_system(current
, SYSTEM__SYSLOG_MOD
);
1536 * Check that a process has enough memory to allocate a new virtual
1537 * mapping. 0 means there is enough memory for the allocation to
1538 * succeed and -ENOMEM implies there is not.
1540 * We currently support three overcommit policies, which are set via the
1541 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1543 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1544 * Additional code 2002 Jul 20 by Robert Love.
1546 static int selinux_vm_enough_memory(long pages
)
1548 unsigned long free
, allowed
;
1550 struct task_security_struct
*tsec
= current
->security
;
1552 vm_acct_memory(pages
);
1555 * Sometimes we want to use more memory than we have
1557 if (sysctl_overcommit_memory
== OVERCOMMIT_ALWAYS
)
1560 if (sysctl_overcommit_memory
== OVERCOMMIT_GUESS
) {
1561 free
= get_page_cache_size();
1562 free
+= nr_free_pages();
1563 free
+= nr_swap_pages
;
1566 * Any slabs which are created with the
1567 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1568 * which are reclaimable, under pressure. The dentry
1569 * cache and most inode caches should fall into this
1571 free
+= atomic_read(&slab_reclaim_pages
);
1574 * Leave the last 3% for privileged processes.
1575 * Don't audit the check, as it is applied to all processes
1576 * that allocate mappings.
1578 rc
= secondary_ops
->capable(current
, CAP_SYS_ADMIN
);
1580 rc
= avc_has_perm_noaudit(tsec
->sid
, tsec
->sid
,
1581 SECCLASS_CAPABILITY
,
1582 CAP_TO_MASK(CAP_SYS_ADMIN
),
1590 vm_unacct_memory(pages
);
1594 allowed
= (totalram_pages
- hugetlb_total_pages())
1595 * sysctl_overcommit_ratio
/ 100;
1596 allowed
+= total_swap_pages
;
1598 if (atomic_read(&vm_committed_space
) < allowed
)
1601 vm_unacct_memory(pages
);
1606 /* binprm security operations */
1608 static int selinux_bprm_alloc_security(struct linux_binprm
*bprm
)
1610 struct bprm_security_struct
*bsec
;
1612 bsec
= kmalloc(sizeof(struct bprm_security_struct
), GFP_KERNEL
);
1616 memset(bsec
, 0, sizeof *bsec
);
1617 bsec
->magic
= SELINUX_MAGIC
;
1619 bsec
->sid
= SECINITSID_UNLABELED
;
1622 bprm
->security
= bsec
;
1626 static int selinux_bprm_set_security(struct linux_binprm
*bprm
)
1628 struct task_security_struct
*tsec
;
1629 struct inode
*inode
= bprm
->file
->f_dentry
->d_inode
;
1630 struct inode_security_struct
*isec
;
1631 struct bprm_security_struct
*bsec
;
1633 struct avc_audit_data ad
;
1636 rc
= secondary_ops
->bprm_set_security(bprm
);
1640 bsec
= bprm
->security
;
1645 tsec
= current
->security
;
1646 isec
= inode
->i_security
;
1648 /* Default to the current task SID. */
1649 bsec
->sid
= tsec
->sid
;
1651 /* Reset create SID on execve. */
1652 tsec
->create_sid
= 0;
1654 if (tsec
->exec_sid
) {
1655 newsid
= tsec
->exec_sid
;
1656 /* Reset exec SID on execve. */
1659 /* Check for a default transition on this program. */
1660 rc
= security_transition_sid(tsec
->sid
, isec
->sid
,
1661 SECCLASS_PROCESS
, &newsid
);
1666 AVC_AUDIT_DATA_INIT(&ad
, FS
);
1667 ad
.u
.fs
.mnt
= bprm
->file
->f_vfsmnt
;
1668 ad
.u
.fs
.dentry
= bprm
->file
->f_dentry
;
1670 if (bprm
->file
->f_vfsmnt
->mnt_flags
& MNT_NOSUID
)
1673 if (tsec
->sid
== newsid
) {
1674 rc
= avc_has_perm(tsec
->sid
, isec
->sid
,
1675 SECCLASS_FILE
, FILE__EXECUTE_NO_TRANS
,
1680 /* Check permissions for the transition. */
1681 rc
= avc_has_perm(tsec
->sid
, newsid
,
1682 SECCLASS_PROCESS
, PROCESS__TRANSITION
,
1688 rc
= avc_has_perm(newsid
, isec
->sid
,
1689 SECCLASS_FILE
, FILE__ENTRYPOINT
,
1694 /* Clear any possibly unsafe personality bits on exec: */
1695 current
->personality
&= ~PER_CLEAR_ON_SETID
;
1697 /* Set the security field to the new SID. */
1705 static int selinux_bprm_check_security (struct linux_binprm
*bprm
)
1707 return secondary_ops
->bprm_check_security(bprm
);
1711 static int selinux_bprm_secureexec (struct linux_binprm
*bprm
)
1713 struct task_security_struct
*tsec
= current
->security
;
1716 if (tsec
->osid
!= tsec
->sid
) {
1717 /* Enable secure mode for SIDs transitions unless
1718 the noatsecure permission is granted between
1719 the two SIDs, i.e. ahp returns 0. */
1720 atsecure
= avc_has_perm(tsec
->osid
, tsec
->sid
,
1722 PROCESS__NOATSECURE
, NULL
, NULL
);
1725 return (atsecure
|| secondary_ops
->bprm_secureexec(bprm
));
1728 static void selinux_bprm_free_security(struct linux_binprm
*bprm
)
1730 struct bprm_security_struct
*bsec
= bprm
->security
;
1731 bprm
->security
= NULL
;
1735 extern struct vfsmount
*selinuxfs_mount
;
1736 extern struct dentry
*selinux_null
;
1738 /* Derived from fs/exec.c:flush_old_files. */
1739 static inline void flush_unauthorized_files(struct files_struct
* files
)
1741 struct avc_audit_data ad
;
1742 struct file
*file
, *devnull
= NULL
;
1743 struct tty_struct
*tty
= current
->signal
->tty
;
1748 file
= list_entry(tty
->tty_files
.next
, typeof(*file
), f_list
);
1750 /* Revalidate access to controlling tty.
1751 Use inode_has_perm on the tty inode directly rather
1752 than using file_has_perm, as this particular open
1753 file may belong to another process and we are only
1754 interested in the inode-based check here. */
1755 struct inode
*inode
= file
->f_dentry
->d_inode
;
1756 if (inode_has_perm(current
, inode
,
1757 FILE__READ
| FILE__WRITE
,
1759 /* Reset controlling tty. */
1760 current
->signal
->tty
= NULL
;
1761 current
->signal
->tty_old_pgrp
= 0;
1767 /* Revalidate access to inherited open files. */
1769 AVC_AUDIT_DATA_INIT(&ad
,FS
);
1771 spin_lock(&files
->file_lock
);
1773 unsigned long set
, i
;
1778 if (i
>= files
->max_fds
|| i
>= files
->max_fdset
)
1780 set
= files
->open_fds
->fds_bits
[j
];
1783 spin_unlock(&files
->file_lock
);
1784 for ( ; set
; i
++,set
>>= 1) {
1789 if (file_has_perm(current
,
1791 file_to_av(file
))) {
1793 fd
= get_unused_fd();
1801 atomic_inc(&devnull
->f_count
);
1803 devnull
= dentry_open(dget(selinux_null
), mntget(selinuxfs_mount
), O_RDWR
);
1810 fd_install(fd
, devnull
);
1815 spin_lock(&files
->file_lock
);
1818 spin_unlock(&files
->file_lock
);
1821 static void selinux_bprm_apply_creds(struct linux_binprm
*bprm
, int unsafe
)
1823 struct task_security_struct
*tsec
;
1824 struct bprm_security_struct
*bsec
;
1826 struct av_decision avd
;
1827 struct itimerval itimer
;
1828 struct rlimit
*rlim
, *initrlim
;
1831 secondary_ops
->bprm_apply_creds(bprm
, unsafe
);
1833 tsec
= current
->security
;
1835 bsec
= bprm
->security
;
1838 tsec
->osid
= tsec
->sid
;
1839 if (tsec
->sid
!= sid
) {
1840 /* Check for shared state. If not ok, leave SID
1841 unchanged and kill. */
1842 if (unsafe
& LSM_UNSAFE_SHARE
) {
1843 rc
= avc_has_perm_noaudit(tsec
->sid
, sid
,
1844 SECCLASS_PROCESS
, PROCESS__SHARE
,
1847 task_unlock(current
);
1848 avc_audit(tsec
->sid
, sid
, SECCLASS_PROCESS
,
1849 PROCESS__SHARE
, &avd
, rc
, NULL
);
1850 force_sig_specific(SIGKILL
, current
);
1855 /* Check for ptracing, and update the task SID if ok.
1856 Otherwise, leave SID unchanged and kill. */
1857 if (unsafe
& (LSM_UNSAFE_PTRACE
| LSM_UNSAFE_PTRACE_CAP
)) {
1858 rc
= avc_has_perm_noaudit(tsec
->ptrace_sid
, sid
,
1859 SECCLASS_PROCESS
, PROCESS__PTRACE
,
1863 task_unlock(current
);
1864 avc_audit(tsec
->ptrace_sid
, sid
, SECCLASS_PROCESS
,
1865 PROCESS__PTRACE
, &avd
, rc
, NULL
);
1867 force_sig_specific(SIGKILL
, current
);
1872 task_unlock(current
);
1875 /* Close files for which the new task SID is not authorized. */
1876 flush_unauthorized_files(current
->files
);
1878 /* Check whether the new SID can inherit signal state
1879 from the old SID. If not, clear itimers to avoid
1880 subsequent signal generation and flush and unblock
1881 signals. This must occur _after_ the task SID has
1882 been updated so that any kill done after the flush
1883 will be checked against the new SID. */
1884 rc
= avc_has_perm(tsec
->osid
, tsec
->sid
, SECCLASS_PROCESS
,
1885 PROCESS__SIGINH
, NULL
, NULL
);
1887 memset(&itimer
, 0, sizeof itimer
);
1888 for (i
= 0; i
< 3; i
++)
1889 do_setitimer(i
, &itimer
, NULL
);
1890 flush_signals(current
);
1891 spin_lock_irq(¤t
->sighand
->siglock
);
1892 flush_signal_handlers(current
, 1);
1893 sigemptyset(¤t
->blocked
);
1894 recalc_sigpending();
1895 spin_unlock_irq(¤t
->sighand
->siglock
);
1898 /* Check whether the new SID can inherit resource limits
1899 from the old SID. If not, reset all soft limits to
1900 the lower of the current task's hard limit and the init
1901 task's soft limit. Note that the setting of hard limits
1902 (even to lower them) can be controlled by the setrlimit
1903 check. The inclusion of the init task's soft limit into
1904 the computation is to avoid resetting soft limits higher
1905 than the default soft limit for cases where the default
1906 is lower than the hard limit, e.g. RLIMIT_CORE or
1908 rc
= avc_has_perm(tsec
->osid
, tsec
->sid
, SECCLASS_PROCESS
,
1909 PROCESS__RLIMITINH
, NULL
, NULL
);
1911 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
1912 rlim
= current
->rlim
+ i
;
1913 initrlim
= init_task
.rlim
+i
;
1914 rlim
->rlim_cur
= min(rlim
->rlim_max
,initrlim
->rlim_cur
);
1918 /* Wake up the parent if it is waiting so that it can
1919 recheck wait permission to the new task SID. */
1920 wake_up_interruptible(¤t
->parent
->wait_chldexit
);
1928 /* superblock security operations */
1930 static int selinux_sb_alloc_security(struct super_block
*sb
)
1932 return superblock_alloc_security(sb
);
1935 static void selinux_sb_free_security(struct super_block
*sb
)
1937 superblock_free_security(sb
);
1940 static inline int match_prefix(char *prefix
, int plen
, char *option
, int olen
)
1945 return !memcmp(prefix
, option
, plen
);
1948 static inline int selinux_option(char *option
, int len
)
1950 return (match_prefix("context=", sizeof("context=")-1, option
, len
) ||
1951 match_prefix("fscontext=", sizeof("fscontext=")-1, option
, len
) ||
1952 match_prefix("defcontext=", sizeof("defcontext=")-1, option
, len
));
1955 static inline void take_option(char **to
, char *from
, int *first
, int len
)
1963 memcpy(*to
, from
, len
);
1967 static int selinux_sb_copy_data(struct file_system_type
*type
, void *orig
, void *copy
)
1969 int fnosec
, fsec
, rc
= 0;
1970 char *in_save
, *in_curr
, *in_end
;
1971 char *sec_curr
, *nosec_save
, *nosec
;
1976 /* Binary mount data: just copy */
1977 if (type
->fs_flags
& FS_BINARY_MOUNTDATA
) {
1978 copy_page(sec_curr
, in_curr
);
1982 nosec
= (char *)get_zeroed_page(GFP_KERNEL
);
1990 in_save
= in_end
= orig
;
1993 if (*in_end
== ',' || *in_end
== '\0') {
1994 int len
= in_end
- in_curr
;
1996 if (selinux_option(in_curr
, len
))
1997 take_option(&sec_curr
, in_curr
, &fsec
, len
);
1999 take_option(&nosec
, in_curr
, &fnosec
, len
);
2001 in_curr
= in_end
+ 1;
2003 } while (*in_end
++);
2005 copy_page(in_save
, nosec_save
);
2010 static int selinux_sb_kern_mount(struct super_block
*sb
, void *data
)
2012 struct avc_audit_data ad
;
2015 rc
= superblock_doinit(sb
, data
);
2019 AVC_AUDIT_DATA_INIT(&ad
,FS
);
2020 ad
.u
.fs
.dentry
= sb
->s_root
;
2021 return superblock_has_perm(current
, sb
, FILESYSTEM__MOUNT
, &ad
);
2024 static int selinux_sb_statfs(struct super_block
*sb
)
2026 struct avc_audit_data ad
;
2028 AVC_AUDIT_DATA_INIT(&ad
,FS
);
2029 ad
.u
.fs
.dentry
= sb
->s_root
;
2030 return superblock_has_perm(current
, sb
, FILESYSTEM__GETATTR
, &ad
);
2033 static int selinux_mount(char * dev_name
,
2034 struct nameidata
*nd
,
2036 unsigned long flags
,
2041 rc
= secondary_ops
->sb_mount(dev_name
, nd
, type
, flags
, data
);
2045 if (flags
& MS_REMOUNT
)
2046 return superblock_has_perm(current
, nd
->mnt
->mnt_sb
,
2047 FILESYSTEM__REMOUNT
, NULL
);
2049 return dentry_has_perm(current
, nd
->mnt
, nd
->dentry
,
2053 static int selinux_umount(struct vfsmount
*mnt
, int flags
)
2057 rc
= secondary_ops
->sb_umount(mnt
, flags
);
2061 return superblock_has_perm(current
,mnt
->mnt_sb
,
2062 FILESYSTEM__UNMOUNT
,NULL
);
2065 /* inode security operations */
2067 static int selinux_inode_alloc_security(struct inode
*inode
)
2069 return inode_alloc_security(inode
);
2072 static void selinux_inode_free_security(struct inode
*inode
)
2074 inode_free_security(inode
);
2077 static int selinux_inode_create(struct inode
*dir
, struct dentry
*dentry
, int mask
)
2079 return may_create(dir
, dentry
, SECCLASS_FILE
);
2082 static void selinux_inode_post_create(struct inode
*dir
, struct dentry
*dentry
, int mask
)
2084 post_create(dir
, dentry
);
2087 static int selinux_inode_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
2091 rc
= secondary_ops
->inode_link(old_dentry
,dir
,new_dentry
);
2094 return may_link(dir
, old_dentry
, MAY_LINK
);
2097 static void selinux_inode_post_link(struct dentry
*old_dentry
, struct inode
*inode
, struct dentry
*new_dentry
)
2102 static int selinux_inode_unlink(struct inode
*dir
, struct dentry
*dentry
)
2106 rc
= secondary_ops
->inode_unlink(dir
, dentry
);
2109 return may_link(dir
, dentry
, MAY_UNLINK
);
2112 static int selinux_inode_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *name
)
2114 return may_create(dir
, dentry
, SECCLASS_LNK_FILE
);
2117 static void selinux_inode_post_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *name
)
2119 post_create(dir
, dentry
);
2122 static int selinux_inode_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mask
)
2124 return may_create(dir
, dentry
, SECCLASS_DIR
);
2127 static void selinux_inode_post_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mask
)
2129 post_create(dir
, dentry
);
2132 static int selinux_inode_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2134 return may_link(dir
, dentry
, MAY_RMDIR
);
2137 static int selinux_inode_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
2141 rc
= secondary_ops
->inode_mknod(dir
, dentry
, mode
, dev
);
2145 return may_create(dir
, dentry
, inode_mode_to_security_class(mode
));
2148 static void selinux_inode_post_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
2150 post_create(dir
, dentry
);
2153 static int selinux_inode_rename(struct inode
*old_inode
, struct dentry
*old_dentry
,
2154 struct inode
*new_inode
, struct dentry
*new_dentry
)
2156 return may_rename(old_inode
, old_dentry
, new_inode
, new_dentry
);
2159 static void selinux_inode_post_rename(struct inode
*old_inode
, struct dentry
*old_dentry
,
2160 struct inode
*new_inode
, struct dentry
*new_dentry
)
2165 static int selinux_inode_readlink(struct dentry
*dentry
)
2167 return dentry_has_perm(current
, NULL
, dentry
, FILE__READ
);
2170 static int selinux_inode_follow_link(struct dentry
*dentry
, struct nameidata
*nameidata
)
2174 rc
= secondary_ops
->inode_follow_link(dentry
,nameidata
);
2177 return dentry_has_perm(current
, NULL
, dentry
, FILE__READ
);
2180 static int selinux_inode_permission(struct inode
*inode
, int mask
,
2181 struct nameidata
*nd
)
2185 rc
= secondary_ops
->inode_permission(inode
, mask
, nd
);
2190 /* No permission to check. Existence test. */
2194 return inode_has_perm(current
, inode
,
2195 file_mask_to_av(inode
->i_mode
, mask
), NULL
, NULL
);
2198 static int selinux_inode_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
2202 rc
= secondary_ops
->inode_setattr(dentry
, iattr
);
2206 if (iattr
->ia_valid
& (ATTR_MODE
| ATTR_UID
| ATTR_GID
|
2207 ATTR_ATIME_SET
| ATTR_MTIME_SET
))
2208 return dentry_has_perm(current
, NULL
, dentry
, FILE__SETATTR
);
2210 return dentry_has_perm(current
, NULL
, dentry
, FILE__WRITE
);
2213 static int selinux_inode_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
)
2215 return dentry_has_perm(current
, mnt
, dentry
, FILE__GETATTR
);
2218 static int selinux_inode_setxattr(struct dentry
*dentry
, char *name
, void *value
, size_t size
, int flags
)
2220 struct task_security_struct
*tsec
= current
->security
;
2221 struct inode
*inode
= dentry
->d_inode
;
2222 struct inode_security_struct
*isec
= inode
->i_security
;
2223 struct superblock_security_struct
*sbsec
;
2224 struct avc_audit_data ad
;
2228 if (strcmp(name
, XATTR_NAME_SELINUX
)) {
2229 if (!strncmp(name
, XATTR_SECURITY_PREFIX
,
2230 sizeof XATTR_SECURITY_PREFIX
- 1) &&
2231 !capable(CAP_SYS_ADMIN
)) {
2232 /* A different attribute in the security namespace.
2233 Restrict to administrator. */
2237 /* Not an attribute we recognize, so just check the
2238 ordinary setattr permission. */
2239 return dentry_has_perm(current
, NULL
, dentry
, FILE__SETATTR
);
2242 sbsec
= inode
->i_sb
->s_security
;
2243 if (sbsec
->behavior
== SECURITY_FS_USE_MNTPOINT
)
2246 AVC_AUDIT_DATA_INIT(&ad
,FS
);
2247 ad
.u
.fs
.dentry
= dentry
;
2249 rc
= avc_has_perm(tsec
->sid
, isec
->sid
, isec
->sclass
,
2255 rc
= security_context_to_sid(value
, size
, &newsid
);
2259 rc
= avc_has_perm(tsec
->sid
, newsid
, isec
->sclass
,
2260 FILE__RELABELTO
, NULL
, &ad
);
2264 return avc_has_perm(newsid
,
2266 SECCLASS_FILESYSTEM
,
2267 FILESYSTEM__ASSOCIATE
,
2272 static void selinux_inode_post_setxattr(struct dentry
*dentry
, char *name
,
2273 void *value
, size_t size
, int flags
)
2275 struct inode
*inode
= dentry
->d_inode
;
2276 struct inode_security_struct
*isec
= inode
->i_security
;
2280 if (strcmp(name
, XATTR_NAME_SELINUX
)) {
2281 /* Not an attribute we recognize, so nothing to do. */
2285 rc
= security_context_to_sid(value
, size
, &newsid
);
2287 printk(KERN_WARNING
"%s: unable to obtain SID for context "
2288 "%s, rc=%d\n", __FUNCTION__
, (char*)value
, -rc
);
2296 static int selinux_inode_getxattr (struct dentry
*dentry
, char *name
)
2298 struct inode
*inode
= dentry
->d_inode
;
2299 struct superblock_security_struct
*sbsec
= inode
->i_sb
->s_security
;
2301 if (sbsec
->behavior
== SECURITY_FS_USE_MNTPOINT
)
2304 return dentry_has_perm(current
, NULL
, dentry
, FILE__GETATTR
);
2307 static int selinux_inode_listxattr (struct dentry
*dentry
)
2309 return dentry_has_perm(current
, NULL
, dentry
, FILE__GETATTR
);
2312 static int selinux_inode_removexattr (struct dentry
*dentry
, char *name
)
2314 if (strcmp(name
, XATTR_NAME_SELINUX
)) {
2315 if (!strncmp(name
, XATTR_SECURITY_PREFIX
,
2316 sizeof XATTR_SECURITY_PREFIX
- 1) &&
2317 !capable(CAP_SYS_ADMIN
)) {
2318 /* A different attribute in the security namespace.
2319 Restrict to administrator. */
2323 /* Not an attribute we recognize, so just check the
2324 ordinary setattr permission. Might want a separate
2325 permission for removexattr. */
2326 return dentry_has_perm(current
, NULL
, dentry
, FILE__SETATTR
);
2329 /* No one is allowed to remove a SELinux security label.
2330 You can change the label, but all data must be labeled. */
2334 static int selinux_inode_getsecurity(struct dentry
*dentry
, const char *name
, void *buffer
, size_t size
)
2336 struct inode
*inode
= dentry
->d_inode
;
2337 struct inode_security_struct
*isec
= inode
->i_security
;
2342 /* Permission check handled by selinux_inode_getxattr hook.*/
2344 if (strcmp(name
, XATTR_SELINUX_SUFFIX
))
2347 rc
= security_sid_to_context(isec
->sid
, &context
, &len
);
2351 if (!buffer
|| !size
) {
2359 memcpy(buffer
, context
, len
);
2364 static int selinux_inode_setsecurity(struct dentry
*dentry
, const char *name
,
2365 const void *value
, size_t size
, int flags
)
2367 struct inode
*inode
= dentry
->d_inode
;
2368 struct inode_security_struct
*isec
= inode
->i_security
;
2372 if (strcmp(name
, XATTR_SELINUX_SUFFIX
))
2375 if (!value
|| !size
)
2378 rc
= security_context_to_sid((void*)value
, size
, &newsid
);
2386 static int selinux_inode_listsecurity(struct dentry
*dentry
, char *buffer
)
2388 const int len
= sizeof(XATTR_NAME_SELINUX
);
2390 memcpy(buffer
, XATTR_NAME_SELINUX
, len
);
2394 /* file security operations */
2396 static int selinux_file_permission(struct file
*file
, int mask
)
2398 struct inode
*inode
= file
->f_dentry
->d_inode
;
2401 /* No permission to check. Existence test. */
2405 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
2406 if ((file
->f_flags
& O_APPEND
) && (mask
& MAY_WRITE
))
2409 return file_has_perm(current
, file
,
2410 file_mask_to_av(inode
->i_mode
, mask
));
2413 static int selinux_file_alloc_security(struct file
*file
)
2415 return file_alloc_security(file
);
2418 static void selinux_file_free_security(struct file
*file
)
2420 file_free_security(file
);
2423 static int selinux_file_ioctl(struct file
*file
, unsigned int cmd
,
2435 case EXT2_IOC_GETFLAGS
:
2437 case EXT2_IOC_GETVERSION
:
2438 error
= file_has_perm(current
, file
, FILE__GETATTR
);
2441 case EXT2_IOC_SETFLAGS
:
2443 case EXT2_IOC_SETVERSION
:
2444 error
= file_has_perm(current
, file
, FILE__SETATTR
);
2447 /* sys_ioctl() checks */
2451 error
= file_has_perm(current
, file
, 0);
2456 error
= task_has_capability(current
,CAP_SYS_TTY_CONFIG
);
2459 /* default case assumes that the command will go
2460 * to the file's ioctl() function.
2463 error
= file_has_perm(current
, file
, FILE__IOCTL
);
2469 static int file_map_prot_check(struct file
*file
, unsigned long prot
, int shared
)
2472 /* read access is always possible with a mapping */
2473 u32 av
= FILE__READ
;
2475 /* write access only matters if the mapping is shared */
2476 if (shared
&& (prot
& PROT_WRITE
))
2479 if (prot
& PROT_EXEC
)
2480 av
|= FILE__EXECUTE
;
2482 return file_has_perm(current
, file
, av
);
2487 static int selinux_file_mmap(struct file
*file
, unsigned long prot
, unsigned long flags
)
2491 rc
= secondary_ops
->file_mmap(file
, prot
, flags
);
2495 return file_map_prot_check(file
, prot
,
2496 (flags
& MAP_TYPE
) == MAP_SHARED
);
2499 static int selinux_file_mprotect(struct vm_area_struct
*vma
,
2504 rc
= secondary_ops
->file_mprotect(vma
, prot
);
2508 return file_map_prot_check(vma
->vm_file
, prot
, vma
->vm_flags
&VM_SHARED
);
2511 static int selinux_file_lock(struct file
*file
, unsigned int cmd
)
2513 return file_has_perm(current
, file
, FILE__LOCK
);
2516 static int selinux_file_fcntl(struct file
*file
, unsigned int cmd
,
2523 if (!file
->f_dentry
|| !file
->f_dentry
->d_inode
) {
2528 if ((file
->f_flags
& O_APPEND
) && !(arg
& O_APPEND
)) {
2529 err
= file_has_perm(current
, file
,FILE__WRITE
);
2538 /* Just check FD__USE permission */
2539 err
= file_has_perm(current
, file
, 0);
2544 #if BITS_PER_LONG == 32
2549 if (!file
->f_dentry
|| !file
->f_dentry
->d_inode
) {
2553 err
= file_has_perm(current
, file
, FILE__LOCK
);
2560 static int selinux_file_set_fowner(struct file
*file
)
2562 struct task_security_struct
*tsec
;
2563 struct file_security_struct
*fsec
;
2565 tsec
= current
->security
;
2566 fsec
= file
->f_security
;
2567 fsec
->fown_sid
= tsec
->sid
;
2572 static int selinux_file_send_sigiotask(struct task_struct
*tsk
,
2573 struct fown_struct
*fown
,
2578 struct task_security_struct
*tsec
;
2579 struct file_security_struct
*fsec
;
2581 /* struct fown_struct is never outside the context of a struct file */
2582 file
= (struct file
*)((long)fown
- offsetof(struct file
,f_owner
));
2584 tsec
= tsk
->security
;
2585 fsec
= file
->f_security
;
2588 perm
= signal_to_av(SIGIO
); /* as per send_sigio_to_task */
2590 perm
= signal_to_av(fown
->signum
);
2592 return avc_has_perm(fsec
->fown_sid
, tsec
->sid
,
2593 SECCLASS_PROCESS
, perm
, NULL
, NULL
);
2596 static int selinux_file_receive(struct file
*file
)
2598 return file_has_perm(current
, file
, file_to_av(file
));
2601 /* task security operations */
2603 static int selinux_task_create(unsigned long clone_flags
)
2607 rc
= secondary_ops
->task_create(clone_flags
);
2611 return task_has_perm(current
, current
, PROCESS__FORK
);
2614 static int selinux_task_alloc_security(struct task_struct
*tsk
)
2616 struct task_security_struct
*tsec1
, *tsec2
;
2619 tsec1
= current
->security
;
2621 rc
= task_alloc_security(tsk
);
2624 tsec2
= tsk
->security
;
2626 tsec2
->osid
= tsec1
->osid
;
2627 tsec2
->sid
= tsec1
->sid
;
2629 /* Retain the exec and create SIDs across fork */
2630 tsec2
->exec_sid
= tsec1
->exec_sid
;
2631 tsec2
->create_sid
= tsec1
->create_sid
;
2633 /* Retain ptracer SID across fork, if any.
2634 This will be reset by the ptrace hook upon any
2635 subsequent ptrace_attach operations. */
2636 tsec2
->ptrace_sid
= tsec1
->ptrace_sid
;
2641 static void selinux_task_free_security(struct task_struct
*tsk
)
2643 task_free_security(tsk
);
2646 static int selinux_task_setuid(uid_t id0
, uid_t id1
, uid_t id2
, int flags
)
2648 /* Since setuid only affects the current process, and
2649 since the SELinux controls are not based on the Linux
2650 identity attributes, SELinux does not need to control
2651 this operation. However, SELinux does control the use
2652 of the CAP_SETUID and CAP_SETGID capabilities using the
2657 static int selinux_task_post_setuid(uid_t id0
, uid_t id1
, uid_t id2
, int flags
)
2659 return secondary_ops
->task_post_setuid(id0
,id1
,id2
,flags
);
2662 static int selinux_task_setgid(gid_t id0
, gid_t id1
, gid_t id2
, int flags
)
2664 /* See the comment for setuid above. */
2668 static int selinux_task_setpgid(struct task_struct
*p
, pid_t pgid
)
2670 return task_has_perm(current
, p
, PROCESS__SETPGID
);
2673 static int selinux_task_getpgid(struct task_struct
*p
)
2675 return task_has_perm(current
, p
, PROCESS__GETPGID
);
2678 static int selinux_task_getsid(struct task_struct
*p
)
2680 return task_has_perm(current
, p
, PROCESS__GETSESSION
);
2683 static int selinux_task_setgroups(struct group_info
*group_info
)
2685 /* See the comment for setuid above. */
2689 static int selinux_task_setnice(struct task_struct
*p
, int nice
)
2693 rc
= secondary_ops
->task_setnice(p
, nice
);
2697 return task_has_perm(current
,p
, PROCESS__SETSCHED
);
2700 static int selinux_task_setrlimit(unsigned int resource
, struct rlimit
*new_rlim
)
2702 struct rlimit
*old_rlim
= current
->rlim
+ resource
;
2705 rc
= secondary_ops
->task_setrlimit(resource
, new_rlim
);
2709 /* Control the ability to change the hard limit (whether
2710 lowering or raising it), so that the hard limit can
2711 later be used as a safe reset point for the soft limit
2712 upon context transitions. See selinux_bprm_apply_creds. */
2713 if (old_rlim
->rlim_max
!= new_rlim
->rlim_max
)
2714 return task_has_perm(current
, current
, PROCESS__SETRLIMIT
);
2719 static int selinux_task_setscheduler(struct task_struct
*p
, int policy
, struct sched_param
*lp
)
2721 struct task_security_struct
*tsec1
, *tsec2
;
2723 tsec1
= current
->security
;
2724 tsec2
= p
->security
;
2726 /* No auditing from the setscheduler hook, since the runqueue lock
2727 is held and the system will deadlock if we try to log an audit
2729 return avc_has_perm_noaudit(tsec1
->sid
, tsec2
->sid
,
2730 SECCLASS_PROCESS
, PROCESS__SETSCHED
,
2731 &tsec2
->avcr
, NULL
);
2734 static int selinux_task_getscheduler(struct task_struct
*p
)
2736 return task_has_perm(current
, p
, PROCESS__GETSCHED
);
2739 static int selinux_task_kill(struct task_struct
*p
, struct siginfo
*info
, int sig
)
2744 rc
= secondary_ops
->task_kill(p
, info
, sig
);
2748 if (info
&& ((unsigned long)info
== 1 ||
2749 (unsigned long)info
== 2 || SI_FROMKERNEL(info
)))
2753 perm
= PROCESS__SIGNULL
; /* null signal; existence test */
2755 perm
= signal_to_av(sig
);
2757 return task_has_perm(current
, p
, perm
);
2760 static int selinux_task_prctl(int option
,
2766 /* The current prctl operations do not appear to require
2767 any SELinux controls since they merely observe or modify
2768 the state of the current process. */
2772 static int selinux_task_wait(struct task_struct
*p
)
2776 perm
= signal_to_av(p
->exit_signal
);
2778 return task_has_perm(p
, current
, perm
);
2781 static void selinux_task_reparent_to_init(struct task_struct
*p
)
2783 struct task_security_struct
*tsec
;
2785 secondary_ops
->task_reparent_to_init(p
);
2788 tsec
->osid
= tsec
->sid
;
2789 tsec
->sid
= SECINITSID_KERNEL
;
2793 static void selinux_task_to_inode(struct task_struct
*p
,
2794 struct inode
*inode
)
2796 struct task_security_struct
*tsec
= p
->security
;
2797 struct inode_security_struct
*isec
= inode
->i_security
;
2799 isec
->sid
= tsec
->sid
;
2800 isec
->initialized
= 1;
2804 #ifdef CONFIG_SECURITY_NETWORK
2806 /* Returns error only if unable to parse addresses */
2807 static int selinux_parse_skb_ipv4(struct sk_buff
*skb
, struct avc_audit_data
*ad
)
2809 int offset
, ihlen
, ret
= -EINVAL
;
2810 struct iphdr _iph
, *ih
;
2812 offset
= skb
->nh
.raw
- skb
->data
;
2813 ih
= skb_header_pointer(skb
, offset
, sizeof(_iph
), &_iph
);
2817 ihlen
= ih
->ihl
* 4;
2818 if (ihlen
< sizeof(_iph
))
2821 ad
->u
.net
.v4info
.saddr
= ih
->saddr
;
2822 ad
->u
.net
.v4info
.daddr
= ih
->daddr
;
2825 switch (ih
->protocol
) {
2827 struct tcphdr _tcph
, *th
;
2829 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
2833 th
= skb_header_pointer(skb
, offset
, sizeof(_tcph
), &_tcph
);
2837 ad
->u
.net
.sport
= th
->source
;
2838 ad
->u
.net
.dport
= th
->dest
;
2843 struct udphdr _udph
, *uh
;
2845 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
2849 uh
= skb_header_pointer(skb
, offset
, sizeof(_udph
), &_udph
);
2853 ad
->u
.net
.sport
= uh
->source
;
2854 ad
->u
.net
.dport
= uh
->dest
;
2865 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2867 /* Returns error only if unable to parse addresses */
2868 static int selinux_parse_skb_ipv6(struct sk_buff
*skb
, struct avc_audit_data
*ad
)
2871 int ret
= -EINVAL
, offset
;
2872 struct ipv6hdr _ipv6h
, *ip6
;
2874 offset
= skb
->nh
.raw
- skb
->data
;
2875 ip6
= skb_header_pointer(skb
, offset
, sizeof(_ipv6h
), &_ipv6h
);
2879 ipv6_addr_copy(&ad
->u
.net
.v6info
.saddr
, &ip6
->saddr
);
2880 ipv6_addr_copy(&ad
->u
.net
.v6info
.daddr
, &ip6
->daddr
);
2883 nexthdr
= ip6
->nexthdr
;
2884 offset
+= sizeof(_ipv6h
);
2885 offset
= ipv6_skip_exthdr(skb
, offset
, &nexthdr
,
2886 skb
->tail
- skb
->head
- offset
);
2892 struct tcphdr _tcph
, *th
;
2894 th
= skb_header_pointer(skb
, offset
, sizeof(_tcph
), &_tcph
);
2898 ad
->u
.net
.sport
= th
->source
;
2899 ad
->u
.net
.dport
= th
->dest
;
2904 struct udphdr _udph
, *uh
;
2906 uh
= skb_header_pointer(skb
, offset
, sizeof(_udph
), &_udph
);
2910 ad
->u
.net
.sport
= uh
->source
;
2911 ad
->u
.net
.dport
= uh
->dest
;
2915 /* includes fragments */
2925 static int selinux_parse_skb(struct sk_buff
*skb
, struct avc_audit_data
*ad
,
2926 char **addrp
, int *len
, int src
)
2930 switch (ad
->u
.net
.family
) {
2932 ret
= selinux_parse_skb_ipv4(skb
, ad
);
2936 *addrp
= (char *)(src
? &ad
->u
.net
.v4info
.saddr
:
2937 &ad
->u
.net
.v4info
.daddr
);
2940 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2942 ret
= selinux_parse_skb_ipv6(skb
, ad
);
2946 *addrp
= (char *)(src
? &ad
->u
.net
.v6info
.saddr
:
2947 &ad
->u
.net
.v6info
.daddr
);
2957 /* socket security operations */
2958 static int socket_has_perm(struct task_struct
*task
, struct socket
*sock
,
2961 struct inode_security_struct
*isec
;
2962 struct task_security_struct
*tsec
;
2963 struct avc_audit_data ad
;
2966 tsec
= task
->security
;
2967 isec
= SOCK_INODE(sock
)->i_security
;
2969 if (isec
->sid
== SECINITSID_KERNEL
)
2972 AVC_AUDIT_DATA_INIT(&ad
,NET
);
2973 ad
.u
.net
.sk
= sock
->sk
;
2974 err
= avc_has_perm(tsec
->sid
, isec
->sid
, isec
->sclass
,
2975 perms
, &isec
->avcr
, &ad
);
2981 static int selinux_socket_create(int family
, int type
,
2982 int protocol
, int kern
)
2985 struct task_security_struct
*tsec
;
2990 tsec
= current
->security
;
2991 err
= avc_has_perm(tsec
->sid
, tsec
->sid
,
2992 socket_type_to_security_class(family
, type
,
2993 protocol
), SOCKET__CREATE
, NULL
, NULL
);
2999 static void selinux_socket_post_create(struct socket
*sock
, int family
,
3000 int type
, int protocol
, int kern
)
3003 struct inode_security_struct
*isec
;
3004 struct task_security_struct
*tsec
;
3006 err
= inode_doinit(SOCK_INODE(sock
));
3009 isec
= SOCK_INODE(sock
)->i_security
;
3011 tsec
= current
->security
;
3012 isec
->sclass
= socket_type_to_security_class(family
, type
, protocol
);
3013 isec
->sid
= kern
? SECINITSID_KERNEL
: tsec
->sid
;
3018 /* Range of port numbers used to automatically bind.
3019 Need to determine whether we should perform a name_bind
3020 permission check between the socket and the port number. */
3021 #define ip_local_port_range_0 sysctl_local_port_range[0]
3022 #define ip_local_port_range_1 sysctl_local_port_range[1]
3024 static int selinux_socket_bind(struct socket
*sock
, struct sockaddr
*address
, int addrlen
)
3029 err
= socket_has_perm(current
, sock
, SOCKET__BIND
);
3034 * If PF_INET or PF_INET6, check name_bind permission for the port.
3036 family
= sock
->sk
->sk_family
;
3037 if (family
== PF_INET
|| family
== PF_INET6
) {
3039 struct inode_security_struct
*isec
;
3040 struct task_security_struct
*tsec
;
3041 struct avc_audit_data ad
;
3042 struct sockaddr_in
*addr4
= NULL
;
3043 struct sockaddr_in6
*addr6
= NULL
;
3044 unsigned short snum
;
3045 struct sock
*sk
= sock
->sk
;
3046 u32 sid
, node_perm
, addrlen
;
3048 tsec
= current
->security
;
3049 isec
= SOCK_INODE(sock
)->i_security
;
3051 if (family
== PF_INET
) {
3052 addr4
= (struct sockaddr_in
*)address
;
3053 snum
= ntohs(addr4
->sin_port
);
3054 addrlen
= sizeof(addr4
->sin_addr
.s_addr
);
3055 addrp
= (char *)&addr4
->sin_addr
.s_addr
;
3057 addr6
= (struct sockaddr_in6
*)address
;
3058 snum
= ntohs(addr6
->sin6_port
);
3059 addrlen
= sizeof(addr6
->sin6_addr
.s6_addr
);
3060 addrp
= (char *)&addr6
->sin6_addr
.s6_addr
;
3063 if (snum
&&(snum
< max(PROT_SOCK
,ip_local_port_range_0
) ||
3064 snum
> ip_local_port_range_1
)) {
3065 err
= security_port_sid(sk
->sk_family
, sk
->sk_type
,
3066 sk
->sk_protocol
, snum
, &sid
);
3069 AVC_AUDIT_DATA_INIT(&ad
,NET
);
3070 ad
.u
.net
.sport
= htons(snum
);
3071 ad
.u
.net
.family
= family
;
3072 err
= avc_has_perm(isec
->sid
, sid
,
3074 SOCKET__NAME_BIND
, NULL
, &ad
);
3079 switch(sk
->sk_protocol
) {
3081 node_perm
= TCP_SOCKET__NODE_BIND
;
3085 node_perm
= UDP_SOCKET__NODE_BIND
;
3089 node_perm
= RAWIP_SOCKET__NODE_BIND
;
3093 err
= security_node_sid(family
, addrp
, addrlen
, &sid
);
3097 AVC_AUDIT_DATA_INIT(&ad
,NET
);
3098 ad
.u
.net
.sport
= htons(snum
);
3099 ad
.u
.net
.family
= family
;
3101 if (family
== PF_INET
)
3102 ad
.u
.net
.v4info
.saddr
= addr4
->sin_addr
.s_addr
;
3104 ipv6_addr_copy(&ad
.u
.net
.v6info
.saddr
, &addr6
->sin6_addr
);
3106 err
= avc_has_perm(isec
->sid
, sid
,
3107 isec
->sclass
, node_perm
, NULL
, &ad
);
3115 static int selinux_socket_connect(struct socket
*sock
, struct sockaddr
*address
, int addrlen
)
3117 return socket_has_perm(current
, sock
, SOCKET__CONNECT
);
3120 static int selinux_socket_listen(struct socket
*sock
, int backlog
)
3122 return socket_has_perm(current
, sock
, SOCKET__LISTEN
);
3125 static int selinux_socket_accept(struct socket
*sock
, struct socket
*newsock
)
3128 struct inode_security_struct
*isec
;
3129 struct inode_security_struct
*newisec
;
3131 err
= socket_has_perm(current
, sock
, SOCKET__ACCEPT
);
3135 err
= inode_doinit(SOCK_INODE(newsock
));
3138 newisec
= SOCK_INODE(newsock
)->i_security
;
3140 isec
= SOCK_INODE(sock
)->i_security
;
3141 newisec
->sclass
= isec
->sclass
;
3142 newisec
->sid
= isec
->sid
;
3147 static int selinux_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
3150 return socket_has_perm(current
, sock
, SOCKET__WRITE
);
3153 static int selinux_socket_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
3154 int size
, int flags
)
3156 return socket_has_perm(current
, sock
, SOCKET__READ
);
3159 static int selinux_socket_getsockname(struct socket
*sock
)
3161 return socket_has_perm(current
, sock
, SOCKET__GETATTR
);
3164 static int selinux_socket_getpeername(struct socket
*sock
)
3166 return socket_has_perm(current
, sock
, SOCKET__GETATTR
);
3169 static int selinux_socket_setsockopt(struct socket
*sock
,int level
,int optname
)
3171 return socket_has_perm(current
, sock
, SOCKET__SETOPT
);
3174 static int selinux_socket_getsockopt(struct socket
*sock
, int level
,
3177 return socket_has_perm(current
, sock
, SOCKET__GETOPT
);
3180 static int selinux_socket_shutdown(struct socket
*sock
, int how
)
3182 return socket_has_perm(current
, sock
, SOCKET__SHUTDOWN
);
3185 static int selinux_socket_unix_stream_connect(struct socket
*sock
,
3186 struct socket
*other
,
3189 struct sk_security_struct
*ssec
;
3190 struct inode_security_struct
*isec
;
3191 struct inode_security_struct
*other_isec
;
3192 struct avc_audit_data ad
;
3195 err
= secondary_ops
->unix_stream_connect(sock
, other
, newsk
);
3199 isec
= SOCK_INODE(sock
)->i_security
;
3200 other_isec
= SOCK_INODE(other
)->i_security
;
3202 AVC_AUDIT_DATA_INIT(&ad
,NET
);
3203 ad
.u
.net
.sk
= other
->sk
;
3205 err
= avc_has_perm(isec
->sid
, other_isec
->sid
,
3207 UNIX_STREAM_SOCKET__CONNECTTO
,
3208 &other_isec
->avcr
, &ad
);
3212 /* connecting socket */
3213 ssec
= sock
->sk
->sk_security
;
3214 ssec
->peer_sid
= other_isec
->sid
;
3216 /* server child socket */
3217 ssec
= newsk
->sk_security
;
3218 ssec
->peer_sid
= isec
->sid
;
3223 static int selinux_socket_unix_may_send(struct socket
*sock
,
3224 struct socket
*other
)
3226 struct inode_security_struct
*isec
;
3227 struct inode_security_struct
*other_isec
;
3228 struct avc_audit_data ad
;
3231 isec
= SOCK_INODE(sock
)->i_security
;
3232 other_isec
= SOCK_INODE(other
)->i_security
;
3234 AVC_AUDIT_DATA_INIT(&ad
,NET
);
3235 ad
.u
.net
.sk
= other
->sk
;
3237 err
= avc_has_perm(isec
->sid
, other_isec
->sid
,
3240 &other_isec
->avcr
, &ad
);
3247 static int selinux_socket_sock_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
3252 u32 netif_perm
, node_perm
, node_sid
, recv_perm
= 0;
3255 struct socket
*sock
;
3256 struct net_device
*dev
;
3257 struct sel_netif
*netif
;
3258 struct netif_security_struct
*nsec
;
3259 struct avc_audit_data ad
;
3261 family
= sk
->sk_family
;
3262 if (family
!= PF_INET
&& family
!= PF_INET6
)
3265 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
3266 if (family
== PF_INET6
&& skb
->protocol
== ntohs(ETH_P_IP
))
3269 read_lock_bh(&sk
->sk_callback_lock
);
3270 sock
= sk
->sk_socket
;
3272 struct inode
*inode
;
3273 inode
= SOCK_INODE(sock
);
3275 struct inode_security_struct
*isec
;
3276 isec
= inode
->i_security
;
3277 sock_sid
= isec
->sid
;
3278 sock_class
= isec
->sclass
;
3281 read_unlock_bh(&sk
->sk_callback_lock
);
3289 netif
= sel_netif_lookup(dev
);
3290 if (IS_ERR(netif
)) {
3291 err
= PTR_ERR(netif
);
3295 nsec
= &netif
->nsec
;
3297 switch (sock_class
) {
3298 case SECCLASS_UDP_SOCKET
:
3299 netif_perm
= NETIF__UDP_RECV
;
3300 node_perm
= NODE__UDP_RECV
;
3301 recv_perm
= UDP_SOCKET__RECV_MSG
;
3304 case SECCLASS_TCP_SOCKET
:
3305 netif_perm
= NETIF__TCP_RECV
;
3306 node_perm
= NODE__TCP_RECV
;
3307 recv_perm
= TCP_SOCKET__RECV_MSG
;
3311 netif_perm
= NETIF__RAWIP_RECV
;
3312 node_perm
= NODE__RAWIP_RECV
;
3316 AVC_AUDIT_DATA_INIT(&ad
, NET
);
3317 ad
.u
.net
.netif
= dev
->name
;
3318 ad
.u
.net
.family
= family
;
3320 err
= selinux_parse_skb(skb
, &ad
, &addrp
, &len
, 1);
3322 sel_netif_put(netif
);
3326 err
= avc_has_perm(sock_sid
, nsec
->if_sid
, SECCLASS_NETIF
,
3327 netif_perm
, &nsec
->avcr
, &ad
);
3328 sel_netif_put(netif
);
3332 /* Fixme: this lookup is inefficient */
3333 err
= security_node_sid(family
, addrp
, len
, &node_sid
);
3337 err
= avc_has_perm(sock_sid
, node_sid
, SECCLASS_NODE
, node_perm
, NULL
, &ad
);
3344 /* Fixme: make this more efficient */
3345 err
= security_port_sid(sk
->sk_family
, sk
->sk_type
,
3346 sk
->sk_protocol
, ntohs(ad
.u
.net
.sport
),
3351 err
= avc_has_perm(sock_sid
, port_sid
, sock_class
,
3352 recv_perm
, NULL
, &ad
);
3358 static int selinux_socket_getpeersec(struct socket
*sock
, char __user
*optval
,
3359 int __user
*optlen
, unsigned len
)
3364 struct sk_security_struct
*ssec
;
3365 struct inode_security_struct
*isec
;
3367 isec
= SOCK_INODE(sock
)->i_security
;
3368 if (isec
->sclass
!= SECCLASS_UNIX_STREAM_SOCKET
) {
3373 ssec
= sock
->sk
->sk_security
;
3375 err
= security_sid_to_context(ssec
->peer_sid
, &scontext
, &scontext_len
);
3379 if (scontext_len
> len
) {
3384 if (copy_to_user(optval
, scontext
, scontext_len
))
3388 if (put_user(scontext_len
, optlen
))
3396 static int selinux_sk_alloc_security(struct sock
*sk
, int family
, int priority
)
3398 return sk_alloc_security(sk
, family
, priority
);
3401 static void selinux_sk_free_security(struct sock
*sk
)
3403 sk_free_security(sk
);
3406 static int selinux_nlmsg_perm(struct sock
*sk
, struct sk_buff
*skb
)
3410 struct nlmsghdr
*nlh
;
3411 struct socket
*sock
= sk
->sk_socket
;
3412 struct inode_security_struct
*isec
= SOCK_INODE(sock
)->i_security
;
3414 if (skb
->len
< NLMSG_SPACE(0)) {
3418 nlh
= (struct nlmsghdr
*)skb
->data
;
3420 err
= selinux_nlmsg_lookup(isec
->sclass
, nlh
->nlmsg_type
, &perm
);
3428 err
= socket_has_perm(current
, sock
, perm
);
3433 #ifdef CONFIG_NETFILTER
3435 static unsigned int selinux_ip_postroute_last(unsigned int hooknum
,
3436 struct sk_buff
**pskb
,
3437 const struct net_device
*in
,
3438 const struct net_device
*out
,
3439 int (*okfn
)(struct sk_buff
*),
3443 int len
, err
= NF_ACCEPT
;
3444 u32 netif_perm
, node_perm
, node_sid
, send_perm
= 0;
3446 struct socket
*sock
;
3447 struct inode
*inode
;
3448 struct sel_netif
*netif
;
3449 struct sk_buff
*skb
= *pskb
;
3450 struct netif_security_struct
*nsec
;
3451 struct inode_security_struct
*isec
;
3452 struct avc_audit_data ad
;
3453 struct net_device
*dev
= (struct net_device
*)out
;
3459 sock
= sk
->sk_socket
;
3463 inode
= SOCK_INODE(sock
);
3467 netif
= sel_netif_lookup(dev
);
3468 if (IS_ERR(netif
)) {
3473 nsec
= &netif
->nsec
;
3474 isec
= inode
->i_security
;
3476 switch (isec
->sclass
) {
3477 case SECCLASS_UDP_SOCKET
:
3478 netif_perm
= NETIF__UDP_SEND
;
3479 node_perm
= NODE__UDP_SEND
;
3480 send_perm
= UDP_SOCKET__SEND_MSG
;
3483 case SECCLASS_TCP_SOCKET
:
3484 netif_perm
= NETIF__TCP_SEND
;
3485 node_perm
= NODE__TCP_SEND
;
3486 send_perm
= TCP_SOCKET__SEND_MSG
;
3490 netif_perm
= NETIF__RAWIP_SEND
;
3491 node_perm
= NODE__RAWIP_SEND
;
3496 AVC_AUDIT_DATA_INIT(&ad
, NET
);
3497 ad
.u
.net
.netif
= dev
->name
;
3498 ad
.u
.net
.family
= family
;
3500 err
= selinux_parse_skb(skb
, &ad
, &addrp
,
3501 &len
, 0) ? NF_DROP
: NF_ACCEPT
;
3502 if (err
!= NF_ACCEPT
) {
3503 sel_netif_put(netif
);
3507 err
= avc_has_perm(isec
->sid
, nsec
->if_sid
, SECCLASS_NETIF
,
3508 netif_perm
, &nsec
->avcr
, &ad
) ? NF_DROP
: NF_ACCEPT
;
3509 sel_netif_put(netif
);
3510 if (err
!= NF_ACCEPT
)
3513 /* Fixme: this lookup is inefficient */
3514 err
= security_node_sid(family
, addrp
, len
,
3515 &node_sid
) ? NF_DROP
: NF_ACCEPT
;
3516 if (err
!= NF_ACCEPT
)
3519 err
= avc_has_perm(isec
->sid
, node_sid
, SECCLASS_NODE
,
3520 node_perm
, NULL
, &ad
) ? NF_DROP
: NF_ACCEPT
;
3521 if (err
!= NF_ACCEPT
)
3527 /* Fixme: make this more efficient */
3528 err
= security_port_sid(sk
->sk_family
,
3531 ntohs(ad
.u
.net
.dport
),
3532 &port_sid
) ? NF_DROP
: NF_ACCEPT
;
3533 if (err
!= NF_ACCEPT
)
3536 err
= avc_has_perm(isec
->sid
, port_sid
, isec
->sclass
,
3537 send_perm
, NULL
, &ad
) ? NF_DROP
: NF_ACCEPT
;
3544 static unsigned int selinux_ipv4_postroute_last(unsigned int hooknum
,
3545 struct sk_buff
**pskb
,
3546 const struct net_device
*in
,
3547 const struct net_device
*out
,
3548 int (*okfn
)(struct sk_buff
*))
3550 return selinux_ip_postroute_last(hooknum
, pskb
, in
, out
, okfn
, PF_INET
);
3553 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3555 static unsigned int selinux_ipv6_postroute_last(unsigned int hooknum
,
3556 struct sk_buff
**pskb
,
3557 const struct net_device
*in
,
3558 const struct net_device
*out
,
3559 int (*okfn
)(struct sk_buff
*))
3561 return selinux_ip_postroute_last(hooknum
, pskb
, in
, out
, okfn
, PF_INET6
);
3566 #endif /* CONFIG_NETFILTER */
3570 static inline int selinux_nlmsg_perm(struct sock
*sk
, struct sk_buff
*skb
)
3575 #endif /* CONFIG_SECURITY_NETWORK */
3577 static int selinux_netlink_send(struct sock
*sk
, struct sk_buff
*skb
)
3581 if (capable(CAP_NET_ADMIN
))
3582 cap_raise (NETLINK_CB (skb
).eff_cap
, CAP_NET_ADMIN
);
3584 NETLINK_CB(skb
).eff_cap
= 0;
3586 if (policydb_loaded_version
>= POLICYDB_VERSION_NLCLASS
)
3587 err
= selinux_nlmsg_perm(sk
, skb
);
3592 static int selinux_netlink_recv(struct sk_buff
*skb
)
3594 if (!cap_raised(NETLINK_CB(skb
).eff_cap
, CAP_NET_ADMIN
))
3599 static int ipc_alloc_security(struct task_struct
*task
,
3600 struct kern_ipc_perm
*perm
,
3603 struct task_security_struct
*tsec
= task
->security
;
3604 struct ipc_security_struct
*isec
;
3606 isec
= kmalloc(sizeof(struct ipc_security_struct
), GFP_KERNEL
);
3610 memset(isec
, 0, sizeof(struct ipc_security_struct
));
3611 isec
->magic
= SELINUX_MAGIC
;
3612 isec
->sclass
= sclass
;
3613 isec
->ipc_perm
= perm
;
3615 isec
->sid
= tsec
->sid
;
3617 isec
->sid
= SECINITSID_UNLABELED
;
3619 perm
->security
= isec
;
3624 static void ipc_free_security(struct kern_ipc_perm
*perm
)
3626 struct ipc_security_struct
*isec
= perm
->security
;
3627 if (!isec
|| isec
->magic
!= SELINUX_MAGIC
)
3630 perm
->security
= NULL
;
3634 static int msg_msg_alloc_security(struct msg_msg
*msg
)
3636 struct msg_security_struct
*msec
;
3638 msec
= kmalloc(sizeof(struct msg_security_struct
), GFP_KERNEL
);
3642 memset(msec
, 0, sizeof(struct msg_security_struct
));
3643 msec
->magic
= SELINUX_MAGIC
;
3645 msec
->sid
= SECINITSID_UNLABELED
;
3646 msg
->security
= msec
;
3651 static void msg_msg_free_security(struct msg_msg
*msg
)
3653 struct msg_security_struct
*msec
= msg
->security
;
3654 if (!msec
|| msec
->magic
!= SELINUX_MAGIC
)
3657 msg
->security
= NULL
;
3661 static int ipc_has_perm(struct kern_ipc_perm
*ipc_perms
,
3662 u16 sclass
, u32 perms
)
3664 struct task_security_struct
*tsec
;
3665 struct ipc_security_struct
*isec
;
3666 struct avc_audit_data ad
;
3668 tsec
= current
->security
;
3669 isec
= ipc_perms
->security
;
3671 AVC_AUDIT_DATA_INIT(&ad
, IPC
);
3672 ad
.u
.ipc_id
= ipc_perms
->key
;
3674 return avc_has_perm(tsec
->sid
, isec
->sid
, sclass
,
3675 perms
, &isec
->avcr
, &ad
);
3678 static int selinux_msg_msg_alloc_security(struct msg_msg
*msg
)
3680 return msg_msg_alloc_security(msg
);
3683 static void selinux_msg_msg_free_security(struct msg_msg
*msg
)
3685 msg_msg_free_security(msg
);
3688 /* message queue security operations */
3689 static int selinux_msg_queue_alloc_security(struct msg_queue
*msq
)
3691 struct task_security_struct
*tsec
;
3692 struct ipc_security_struct
*isec
;
3693 struct avc_audit_data ad
;
3696 rc
= ipc_alloc_security(current
, &msq
->q_perm
, SECCLASS_MSGQ
);
3700 tsec
= current
->security
;
3701 isec
= msq
->q_perm
.security
;
3703 AVC_AUDIT_DATA_INIT(&ad
, IPC
);
3704 ad
.u
.ipc_id
= msq
->q_perm
.key
;
3706 rc
= avc_has_perm(tsec
->sid
, isec
->sid
, SECCLASS_MSGQ
,
3707 MSGQ__CREATE
, &isec
->avcr
, &ad
);
3709 ipc_free_security(&msq
->q_perm
);
3715 static void selinux_msg_queue_free_security(struct msg_queue
*msq
)
3717 ipc_free_security(&msq
->q_perm
);
3720 static int selinux_msg_queue_associate(struct msg_queue
*msq
, int msqflg
)
3722 struct task_security_struct
*tsec
;
3723 struct ipc_security_struct
*isec
;
3724 struct avc_audit_data ad
;
3726 tsec
= current
->security
;
3727 isec
= msq
->q_perm
.security
;
3729 AVC_AUDIT_DATA_INIT(&ad
, IPC
);
3730 ad
.u
.ipc_id
= msq
->q_perm
.key
;
3732 return avc_has_perm(tsec
->sid
, isec
->sid
, SECCLASS_MSGQ
,
3733 MSGQ__ASSOCIATE
, &isec
->avcr
, &ad
);
3736 static int selinux_msg_queue_msgctl(struct msg_queue
*msq
, int cmd
)
3744 /* No specific object, just general system-wide information. */
3745 return task_has_system(current
, SYSTEM__IPC_INFO
);
3748 perms
= MSGQ__GETATTR
| MSGQ__ASSOCIATE
;
3751 perms
= MSGQ__SETATTR
;
3754 perms
= MSGQ__DESTROY
;
3760 err
= ipc_has_perm(&msq
->q_perm
, SECCLASS_MSGQ
, perms
);
3764 static int selinux_msg_queue_msgsnd(struct msg_queue
*msq
, struct msg_msg
*msg
, int msqflg
)
3766 struct task_security_struct
*tsec
;
3767 struct ipc_security_struct
*isec
;
3768 struct msg_security_struct
*msec
;
3769 struct avc_audit_data ad
;
3772 tsec
= current
->security
;
3773 isec
= msq
->q_perm
.security
;
3774 msec
= msg
->security
;
3777 * First time through, need to assign label to the message
3779 if (msec
->sid
== SECINITSID_UNLABELED
) {
3781 * Compute new sid based on current process and
3782 * message queue this message will be stored in
3784 rc
= security_transition_sid(tsec
->sid
,
3792 AVC_AUDIT_DATA_INIT(&ad
, IPC
);
3793 ad
.u
.ipc_id
= msq
->q_perm
.key
;
3795 /* Can this process write to the queue? */
3796 rc
= avc_has_perm(tsec
->sid
, isec
->sid
, SECCLASS_MSGQ
,
3797 MSGQ__WRITE
, &isec
->avcr
, &ad
);
3799 /* Can this process send the message */
3800 rc
= avc_has_perm(tsec
->sid
, msec
->sid
,
3801 SECCLASS_MSG
, MSG__SEND
,
3804 /* Can the message be put in the queue? */
3805 rc
= avc_has_perm(msec
->sid
, isec
->sid
,
3806 SECCLASS_MSGQ
, MSGQ__ENQUEUE
,
3812 static int selinux_msg_queue_msgrcv(struct msg_queue
*msq
, struct msg_msg
*msg
,
3813 struct task_struct
*target
,
3814 long type
, int mode
)
3816 struct task_security_struct
*tsec
;
3817 struct ipc_security_struct
*isec
;
3818 struct msg_security_struct
*msec
;
3819 struct avc_audit_data ad
;
3822 tsec
= target
->security
;
3823 isec
= msq
->q_perm
.security
;
3824 msec
= msg
->security
;
3826 AVC_AUDIT_DATA_INIT(&ad
, IPC
);
3827 ad
.u
.ipc_id
= msq
->q_perm
.key
;
3829 rc
= avc_has_perm(tsec
->sid
, isec
->sid
,
3830 SECCLASS_MSGQ
, MSGQ__READ
,
3833 rc
= avc_has_perm(tsec
->sid
, msec
->sid
,
3834 SECCLASS_MSG
, MSG__RECEIVE
,
3839 /* Shared Memory security operations */
3840 static int selinux_shm_alloc_security(struct shmid_kernel
*shp
)
3842 struct task_security_struct
*tsec
;
3843 struct ipc_security_struct
*isec
;
3844 struct avc_audit_data ad
;
3847 rc
= ipc_alloc_security(current
, &shp
->shm_perm
, SECCLASS_SHM
);
3851 tsec
= current
->security
;
3852 isec
= shp
->shm_perm
.security
;
3854 AVC_AUDIT_DATA_INIT(&ad
, IPC
);
3855 ad
.u
.ipc_id
= shp
->shm_perm
.key
;
3857 rc
= avc_has_perm(tsec
->sid
, isec
->sid
, SECCLASS_SHM
,
3858 SHM__CREATE
, &isec
->avcr
, &ad
);
3860 ipc_free_security(&shp
->shm_perm
);
3866 static void selinux_shm_free_security(struct shmid_kernel
*shp
)
3868 ipc_free_security(&shp
->shm_perm
);
3871 static int selinux_shm_associate(struct shmid_kernel
*shp
, int shmflg
)
3873 struct task_security_struct
*tsec
;
3874 struct ipc_security_struct
*isec
;
3875 struct avc_audit_data ad
;
3877 tsec
= current
->security
;
3878 isec
= shp
->shm_perm
.security
;
3880 AVC_AUDIT_DATA_INIT(&ad
, IPC
);
3881 ad
.u
.ipc_id
= shp
->shm_perm
.key
;
3883 return avc_has_perm(tsec
->sid
, isec
->sid
, SECCLASS_SHM
,
3884 SHM__ASSOCIATE
, &isec
->avcr
, &ad
);
3887 /* Note, at this point, shp is locked down */
3888 static int selinux_shm_shmctl(struct shmid_kernel
*shp
, int cmd
)
3896 /* No specific object, just general system-wide information. */
3897 return task_has_system(current
, SYSTEM__IPC_INFO
);
3900 perms
= SHM__GETATTR
| SHM__ASSOCIATE
;
3903 perms
= SHM__SETATTR
;
3910 perms
= SHM__DESTROY
;
3916 err
= ipc_has_perm(&shp
->shm_perm
, SECCLASS_SHM
, perms
);
3920 static int selinux_shm_shmat(struct shmid_kernel
*shp
,
3921 char __user
*shmaddr
, int shmflg
)
3926 rc
= secondary_ops
->shm_shmat(shp
, shmaddr
, shmflg
);
3930 if (shmflg
& SHM_RDONLY
)
3933 perms
= SHM__READ
| SHM__WRITE
;
3935 return ipc_has_perm(&shp
->shm_perm
, SECCLASS_SHM
, perms
);
3938 /* Semaphore security operations */
3939 static int selinux_sem_alloc_security(struct sem_array
*sma
)
3941 struct task_security_struct
*tsec
;
3942 struct ipc_security_struct
*isec
;
3943 struct avc_audit_data ad
;
3946 rc
= ipc_alloc_security(current
, &sma
->sem_perm
, SECCLASS_SEM
);
3950 tsec
= current
->security
;
3951 isec
= sma
->sem_perm
.security
;
3953 AVC_AUDIT_DATA_INIT(&ad
, IPC
);
3954 ad
.u
.ipc_id
= sma
->sem_perm
.key
;
3956 rc
= avc_has_perm(tsec
->sid
, isec
->sid
, SECCLASS_SEM
,
3957 SEM__CREATE
, &isec
->avcr
, &ad
);
3959 ipc_free_security(&sma
->sem_perm
);
3965 static void selinux_sem_free_security(struct sem_array
*sma
)
3967 ipc_free_security(&sma
->sem_perm
);
3970 static int selinux_sem_associate(struct sem_array
*sma
, int semflg
)
3972 struct task_security_struct
*tsec
;
3973 struct ipc_security_struct
*isec
;
3974 struct avc_audit_data ad
;
3976 tsec
= current
->security
;
3977 isec
= sma
->sem_perm
.security
;
3979 AVC_AUDIT_DATA_INIT(&ad
, IPC
);
3980 ad
.u
.ipc_id
= sma
->sem_perm
.key
;
3982 return avc_has_perm(tsec
->sid
, isec
->sid
, SECCLASS_SEM
,
3983 SEM__ASSOCIATE
, &isec
->avcr
, &ad
);
3986 /* Note, at this point, sma is locked down */
3987 static int selinux_sem_semctl(struct sem_array
*sma
, int cmd
)
3995 /* No specific object, just general system-wide information. */
3996 return task_has_system(current
, SYSTEM__IPC_INFO
);
4000 perms
= SEM__GETATTR
;
4011 perms
= SEM__DESTROY
;
4014 perms
= SEM__SETATTR
;
4018 perms
= SEM__GETATTR
| SEM__ASSOCIATE
;
4024 err
= ipc_has_perm(&sma
->sem_perm
, SECCLASS_SEM
, perms
);
4028 static int selinux_sem_semop(struct sem_array
*sma
,
4029 struct sembuf
*sops
, unsigned nsops
, int alter
)
4034 perms
= SEM__READ
| SEM__WRITE
;
4038 return ipc_has_perm(&sma
->sem_perm
, SECCLASS_SEM
, perms
);
4041 static int selinux_ipc_permission(struct kern_ipc_perm
*ipcp
, short flag
)
4043 struct ipc_security_struct
*isec
= ipcp
->security
;
4044 u16 sclass
= SECCLASS_IPC
;
4047 if (isec
&& isec
->magic
== SELINUX_MAGIC
)
4048 sclass
= isec
->sclass
;
4052 av
|= IPC__UNIX_READ
;
4054 av
|= IPC__UNIX_WRITE
;
4059 return ipc_has_perm(ipcp
, sclass
, av
);
4062 /* module stacking operations */
4063 int selinux_register_security (const char *name
, struct security_operations
*ops
)
4065 if (secondary_ops
!= original_ops
) {
4066 printk(KERN_INFO
"%s: There is already a secondary security "
4067 "module registered.\n", __FUNCTION__
);
4071 secondary_ops
= ops
;
4073 printk(KERN_INFO
"%s: Registering secondary module %s\n",
4080 int selinux_unregister_security (const char *name
, struct security_operations
*ops
)
4082 if (ops
!= secondary_ops
) {
4083 printk (KERN_INFO
"%s: trying to unregister a security module "
4084 "that is not registered.\n", __FUNCTION__
);
4088 secondary_ops
= original_ops
;
4093 static void selinux_d_instantiate (struct dentry
*dentry
, struct inode
*inode
)
4096 inode_doinit_with_dentry(inode
, dentry
);
4099 static int selinux_getprocattr(struct task_struct
*p
,
4100 char *name
, void *value
, size_t size
)
4102 struct task_security_struct
*tsec
;
4108 error
= task_has_perm(current
, p
, PROCESS__GETATTR
);
4118 if (!strcmp(name
, "current"))
4120 else if (!strcmp(name
, "prev"))
4122 else if (!strcmp(name
, "exec"))
4123 sid
= tsec
->exec_sid
;
4124 else if (!strcmp(name
, "fscreate"))
4125 sid
= tsec
->create_sid
;
4132 error
= security_sid_to_context(sid
, &context
, &len
);
4139 memcpy(value
, context
, len
);
4144 static int selinux_setprocattr(struct task_struct
*p
,
4145 char *name
, void *value
, size_t size
)
4147 struct task_security_struct
*tsec
;
4151 if (current
!= p
|| !strcmp(name
, "current")) {
4152 /* SELinux only allows a process to change its own
4153 security attributes, and it only allows the process
4154 current SID to change via exec. */
4159 * Basic control over ability to set these attributes at all.
4160 * current == p, but we'll pass them separately in case the
4161 * above restriction is ever removed.
4163 if (!strcmp(name
, "exec"))
4164 error
= task_has_perm(current
, p
, PROCESS__SETEXEC
);
4165 else if (!strcmp(name
, "fscreate"))
4166 error
= task_has_perm(current
, p
, PROCESS__SETFSCREATE
);
4172 /* Obtain a SID for the context, if one was specified. */
4175 error
= security_context_to_sid(value
, size
, &sid
);
4180 /* Permission checking based on the specified context is
4181 performed during the actual operation (execve,
4182 open/mkdir/...), when we know the full context of the
4183 operation. See selinux_bprm_set_security for the execve
4184 checks and may_create for the file creation checks. The
4185 operation will then fail if the context is not permitted. */
4187 if (!strcmp(name
, "exec"))
4188 tsec
->exec_sid
= sid
;
4189 else if (!strcmp(name
, "fscreate"))
4190 tsec
->create_sid
= sid
;
4197 struct security_operations selinux_ops
= {
4198 .ptrace
= selinux_ptrace
,
4199 .capget
= selinux_capget
,
4200 .capset_check
= selinux_capset_check
,
4201 .capset_set
= selinux_capset_set
,
4202 .sysctl
= selinux_sysctl
,
4203 .capable
= selinux_capable
,
4204 .quotactl
= selinux_quotactl
,
4205 .quota_on
= selinux_quota_on
,
4206 .syslog
= selinux_syslog
,
4207 .vm_enough_memory
= selinux_vm_enough_memory
,
4209 .netlink_send
= selinux_netlink_send
,
4210 .netlink_recv
= selinux_netlink_recv
,
4212 .bprm_alloc_security
= selinux_bprm_alloc_security
,
4213 .bprm_free_security
= selinux_bprm_free_security
,
4214 .bprm_apply_creds
= selinux_bprm_apply_creds
,
4215 .bprm_set_security
= selinux_bprm_set_security
,
4216 .bprm_check_security
= selinux_bprm_check_security
,
4217 .bprm_secureexec
= selinux_bprm_secureexec
,
4219 .sb_alloc_security
= selinux_sb_alloc_security
,
4220 .sb_free_security
= selinux_sb_free_security
,
4221 .sb_copy_data
= selinux_sb_copy_data
,
4222 .sb_kern_mount
= selinux_sb_kern_mount
,
4223 .sb_statfs
= selinux_sb_statfs
,
4224 .sb_mount
= selinux_mount
,
4225 .sb_umount
= selinux_umount
,
4227 .inode_alloc_security
= selinux_inode_alloc_security
,
4228 .inode_free_security
= selinux_inode_free_security
,
4229 .inode_create
= selinux_inode_create
,
4230 .inode_post_create
= selinux_inode_post_create
,
4231 .inode_link
= selinux_inode_link
,
4232 .inode_post_link
= selinux_inode_post_link
,
4233 .inode_unlink
= selinux_inode_unlink
,
4234 .inode_symlink
= selinux_inode_symlink
,
4235 .inode_post_symlink
= selinux_inode_post_symlink
,
4236 .inode_mkdir
= selinux_inode_mkdir
,
4237 .inode_post_mkdir
= selinux_inode_post_mkdir
,
4238 .inode_rmdir
= selinux_inode_rmdir
,
4239 .inode_mknod
= selinux_inode_mknod
,
4240 .inode_post_mknod
= selinux_inode_post_mknod
,
4241 .inode_rename
= selinux_inode_rename
,
4242 .inode_post_rename
= selinux_inode_post_rename
,
4243 .inode_readlink
= selinux_inode_readlink
,
4244 .inode_follow_link
= selinux_inode_follow_link
,
4245 .inode_permission
= selinux_inode_permission
,
4246 .inode_setattr
= selinux_inode_setattr
,
4247 .inode_getattr
= selinux_inode_getattr
,
4248 .inode_setxattr
= selinux_inode_setxattr
,
4249 .inode_post_setxattr
= selinux_inode_post_setxattr
,
4250 .inode_getxattr
= selinux_inode_getxattr
,
4251 .inode_listxattr
= selinux_inode_listxattr
,
4252 .inode_removexattr
= selinux_inode_removexattr
,
4253 .inode_getsecurity
= selinux_inode_getsecurity
,
4254 .inode_setsecurity
= selinux_inode_setsecurity
,
4255 .inode_listsecurity
= selinux_inode_listsecurity
,
4257 .file_permission
= selinux_file_permission
,
4258 .file_alloc_security
= selinux_file_alloc_security
,
4259 .file_free_security
= selinux_file_free_security
,
4260 .file_ioctl
= selinux_file_ioctl
,
4261 .file_mmap
= selinux_file_mmap
,
4262 .file_mprotect
= selinux_file_mprotect
,
4263 .file_lock
= selinux_file_lock
,
4264 .file_fcntl
= selinux_file_fcntl
,
4265 .file_set_fowner
= selinux_file_set_fowner
,
4266 .file_send_sigiotask
= selinux_file_send_sigiotask
,
4267 .file_receive
= selinux_file_receive
,
4269 .task_create
= selinux_task_create
,
4270 .task_alloc_security
= selinux_task_alloc_security
,
4271 .task_free_security
= selinux_task_free_security
,
4272 .task_setuid
= selinux_task_setuid
,
4273 .task_post_setuid
= selinux_task_post_setuid
,
4274 .task_setgid
= selinux_task_setgid
,
4275 .task_setpgid
= selinux_task_setpgid
,
4276 .task_getpgid
= selinux_task_getpgid
,
4277 .task_getsid
= selinux_task_getsid
,
4278 .task_setgroups
= selinux_task_setgroups
,
4279 .task_setnice
= selinux_task_setnice
,
4280 .task_setrlimit
= selinux_task_setrlimit
,
4281 .task_setscheduler
= selinux_task_setscheduler
,
4282 .task_getscheduler
= selinux_task_getscheduler
,
4283 .task_kill
= selinux_task_kill
,
4284 .task_wait
= selinux_task_wait
,
4285 .task_prctl
= selinux_task_prctl
,
4286 .task_reparent_to_init
= selinux_task_reparent_to_init
,
4287 .task_to_inode
= selinux_task_to_inode
,
4289 .ipc_permission
= selinux_ipc_permission
,
4291 .msg_msg_alloc_security
= selinux_msg_msg_alloc_security
,
4292 .msg_msg_free_security
= selinux_msg_msg_free_security
,
4294 .msg_queue_alloc_security
= selinux_msg_queue_alloc_security
,
4295 .msg_queue_free_security
= selinux_msg_queue_free_security
,
4296 .msg_queue_associate
= selinux_msg_queue_associate
,
4297 .msg_queue_msgctl
= selinux_msg_queue_msgctl
,
4298 .msg_queue_msgsnd
= selinux_msg_queue_msgsnd
,
4299 .msg_queue_msgrcv
= selinux_msg_queue_msgrcv
,
4301 .shm_alloc_security
= selinux_shm_alloc_security
,
4302 .shm_free_security
= selinux_shm_free_security
,
4303 .shm_associate
= selinux_shm_associate
,
4304 .shm_shmctl
= selinux_shm_shmctl
,
4305 .shm_shmat
= selinux_shm_shmat
,
4307 .sem_alloc_security
= selinux_sem_alloc_security
,
4308 .sem_free_security
= selinux_sem_free_security
,
4309 .sem_associate
= selinux_sem_associate
,
4310 .sem_semctl
= selinux_sem_semctl
,
4311 .sem_semop
= selinux_sem_semop
,
4313 .register_security
= selinux_register_security
,
4314 .unregister_security
= selinux_unregister_security
,
4316 .d_instantiate
= selinux_d_instantiate
,
4318 .getprocattr
= selinux_getprocattr
,
4319 .setprocattr
= selinux_setprocattr
,
4321 #ifdef CONFIG_SECURITY_NETWORK
4322 .unix_stream_connect
= selinux_socket_unix_stream_connect
,
4323 .unix_may_send
= selinux_socket_unix_may_send
,
4325 .socket_create
= selinux_socket_create
,
4326 .socket_post_create
= selinux_socket_post_create
,
4327 .socket_bind
= selinux_socket_bind
,
4328 .socket_connect
= selinux_socket_connect
,
4329 .socket_listen
= selinux_socket_listen
,
4330 .socket_accept
= selinux_socket_accept
,
4331 .socket_sendmsg
= selinux_socket_sendmsg
,
4332 .socket_recvmsg
= selinux_socket_recvmsg
,
4333 .socket_getsockname
= selinux_socket_getsockname
,
4334 .socket_getpeername
= selinux_socket_getpeername
,
4335 .socket_getsockopt
= selinux_socket_getsockopt
,
4336 .socket_setsockopt
= selinux_socket_setsockopt
,
4337 .socket_shutdown
= selinux_socket_shutdown
,
4338 .socket_sock_rcv_skb
= selinux_socket_sock_rcv_skb
,
4339 .socket_getpeersec
= selinux_socket_getpeersec
,
4340 .sk_alloc_security
= selinux_sk_alloc_security
,
4341 .sk_free_security
= selinux_sk_free_security
,
4345 __init
int selinux_init(void)
4347 struct task_security_struct
*tsec
;
4349 if (!selinux_enabled
) {
4350 printk(KERN_INFO
"SELinux: Disabled at boot.\n");
4354 printk(KERN_INFO
"SELinux: Initializing.\n");
4356 /* Set the security state for the initial task. */
4357 if (task_alloc_security(current
))
4358 panic("SELinux: Failed to initialize initial task.\n");
4359 tsec
= current
->security
;
4360 tsec
->osid
= tsec
->sid
= SECINITSID_KERNEL
;
4364 original_ops
= secondary_ops
= security_ops
;
4366 panic ("SELinux: No initial security operations\n");
4367 if (register_security (&selinux_ops
))
4368 panic("SELinux: Unable to register with kernel.\n");
4370 if (selinux_enforcing
) {
4371 printk(KERN_INFO
"SELinux: Starting in enforcing mode\n");
4373 printk(KERN_INFO
"SELinux: Starting in permissive mode\n");
4378 void selinux_complete_init(void)
4380 printk(KERN_INFO
"SELinux: Completing initialization.\n");
4382 /* Set up any superblocks initialized prior to the policy load. */
4383 printk(KERN_INFO
"SELinux: Setting up existing superblocks.\n");
4384 spin_lock(&sb_security_lock
);
4386 if (!list_empty(&superblock_security_head
)) {
4387 struct superblock_security_struct
*sbsec
=
4388 list_entry(superblock_security_head
.next
,
4389 struct superblock_security_struct
,
4391 struct super_block
*sb
= sbsec
->sb
;
4392 spin_lock(&sb_lock
);
4394 spin_unlock(&sb_lock
);
4395 spin_unlock(&sb_security_lock
);
4396 down_read(&sb
->s_umount
);
4398 superblock_doinit(sb
, NULL
);
4400 spin_lock(&sb_security_lock
);
4401 list_del_init(&sbsec
->list
);
4404 spin_unlock(&sb_security_lock
);
4407 /* SELinux requires early initialization in order to label
4408 all processes and objects when they are created. */
4409 security_initcall(selinux_init
);
4411 #if defined(CONFIG_SECURITY_NETWORK) && defined(CONFIG_NETFILTER)
4413 static struct nf_hook_ops selinux_ipv4_op
= {
4414 .hook
= selinux_ipv4_postroute_last
,
4415 .owner
= THIS_MODULE
,
4417 .hooknum
= NF_IP_POST_ROUTING
,
4418 .priority
= NF_IP_PRI_SELINUX_LAST
,
4421 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4423 static struct nf_hook_ops selinux_ipv6_op
= {
4424 .hook
= selinux_ipv6_postroute_last
,
4425 .owner
= THIS_MODULE
,
4427 .hooknum
= NF_IP6_POST_ROUTING
,
4428 .priority
= NF_IP6_PRI_SELINUX_LAST
,
4433 static int __init
selinux_nf_ip_init(void)
4437 if (!selinux_enabled
)
4440 printk(KERN_INFO
"SELinux: Registering netfilter hooks\n");
4442 err
= nf_register_hook(&selinux_ipv4_op
);
4444 panic("SELinux: nf_register_hook for IPv4: error %d\n", err
);
4446 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4448 err
= nf_register_hook(&selinux_ipv6_op
);
4450 panic("SELinux: nf_register_hook for IPv6: error %d\n", err
);
4457 __initcall(selinux_nf_ip_init
);
4459 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
4460 static void selinux_nf_ip_exit(void)
4462 printk(KERN_INFO
"SELinux: Unregistering netfilter hooks\n");
4464 nf_unregister_hook(&selinux_ipv4_op
);
4465 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4466 nf_unregister_hook(&selinux_ipv6_op
);
4471 #else /* CONFIG_SECURITY_NETWORK && CONFIG_NETFILTER */
4473 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
4474 #define selinux_nf_ip_exit()
4477 #endif /* CONFIG_SECURITY_NETWORK && CONFIG_NETFILTER */
4479 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
4480 int selinux_disable(void)
4482 extern void exit_sel_fs(void);
4483 static int selinux_disabled
= 0;
4485 if (ss_initialized
) {
4486 /* Not permitted after initial policy load. */
4490 if (selinux_disabled
) {
4491 /* Only do this once. */
4495 printk(KERN_INFO
"SELinux: Disabled at runtime.\n");
4497 selinux_disabled
= 1;
4499 /* Reset security_ops to the secondary module, dummy or capability. */
4500 security_ops
= secondary_ops
;
4502 /* Unregister netfilter hooks. */
4503 selinux_nf_ip_exit();
4505 /* Unregister selinuxfs. */