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>
13 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
14 * <dgoeddel@trustedcs.com>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2,
18 * as published by the Free Software Foundation.
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/ptrace.h>
26 #include <linux/errno.h>
27 #include <linux/sched.h>
28 #include <linux/security.h>
29 #include <linux/xattr.h>
30 #include <linux/capability.h>
31 #include <linux/unistd.h>
33 #include <linux/mman.h>
34 #include <linux/slab.h>
35 #include <linux/pagemap.h>
36 #include <linux/swap.h>
37 #include <linux/smp_lock.h>
38 #include <linux/spinlock.h>
39 #include <linux/syscalls.h>
40 #include <linux/file.h>
41 #include <linux/namei.h>
42 #include <linux/mount.h>
43 #include <linux/ext2_fs.h>
44 #include <linux/proc_fs.h>
46 #include <linux/netfilter_ipv4.h>
47 #include <linux/netfilter_ipv6.h>
48 #include <linux/tty.h>
50 #include <net/ip.h> /* for sysctl_local_port_range[] */
51 #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
52 #include <asm/uaccess.h>
53 #include <asm/semaphore.h>
54 #include <asm/ioctls.h>
55 #include <linux/bitops.h>
56 #include <linux/interrupt.h>
57 #include <linux/netdevice.h> /* for network interface checks */
58 #include <linux/netlink.h>
59 #include <linux/tcp.h>
60 #include <linux/udp.h>
61 #include <linux/quota.h>
62 #include <linux/un.h> /* for Unix socket types */
63 #include <net/af_unix.h> /* for Unix socket types */
64 #include <linux/parser.h>
65 #include <linux/nfs_mount.h>
67 #include <linux/hugetlb.h>
68 #include <linux/personality.h>
69 #include <linux/sysctl.h>
70 #include <linux/audit.h>
71 #include <linux/string.h>
78 #define XATTR_SELINUX_SUFFIX "selinux"
79 #define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX
81 extern unsigned int policydb_loaded_version;
82 extern int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm);
84 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
85 int selinux_enforcing = 0;
87 static int __init enforcing_setup(char *str)
89 selinux_enforcing = simple_strtol(str,NULL,0);
92 __setup("enforcing=", enforcing_setup);
95 #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
96 int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
98 static int __init selinux_enabled_setup(char *str)
100 selinux_enabled = simple_strtol(str, NULL, 0);
103 __setup("selinux=", selinux_enabled_setup);
106 /* Original (dummy) security module. */
107 static struct security_operations *original_ops = NULL;
109 /* Minimal support for a secondary security module,
110 just to allow the use of the dummy or capability modules.
111 The owlsm module can alternatively be used as a secondary
112 module as long as CONFIG_OWLSM_FD is not enabled. */
113 static struct security_operations *secondary_ops = NULL;
115 /* Lists of inode and superblock security structures initialized
116 before the policy was loaded. */
117 static LIST_HEAD(superblock_security_head);
118 static DEFINE_SPINLOCK(sb_security_lock);
120 /* Allocate and free functions for each kind of security blob. */
122 static int task_alloc_security(struct task_struct *task)
124 struct task_security_struct *tsec;
126 tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
131 tsec->osid = tsec->sid = tsec->ptrace_sid = SECINITSID_UNLABELED;
132 task->security = tsec;
137 static void task_free_security(struct task_struct *task)
139 struct task_security_struct *tsec = task->security;
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 = kzalloc(sizeof(struct inode_security_struct), GFP_KERNEL);
153 init_MUTEX(&isec->sem);
154 INIT_LIST_HEAD(&isec->list);
156 isec->sid = SECINITSID_UNLABELED;
157 isec->sclass = SECCLASS_FILE;
158 isec->task_sid = tsec->sid;
159 inode->i_security = isec;
164 static void inode_free_security(struct inode *inode)
166 struct inode_security_struct *isec = inode->i_security;
167 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
169 spin_lock(&sbsec->isec_lock);
170 if (!list_empty(&isec->list))
171 list_del_init(&isec->list);
172 spin_unlock(&sbsec->isec_lock);
174 inode->i_security = NULL;
178 static int file_alloc_security(struct file *file)
180 struct task_security_struct *tsec = current->security;
181 struct file_security_struct *fsec;
183 fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
188 fsec->sid = tsec->sid;
189 fsec->fown_sid = tsec->sid;
190 file->f_security = fsec;
195 static void file_free_security(struct file *file)
197 struct file_security_struct *fsec = file->f_security;
198 file->f_security = NULL;
202 static int superblock_alloc_security(struct super_block *sb)
204 struct superblock_security_struct *sbsec;
206 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
210 init_MUTEX(&sbsec->sem);
211 INIT_LIST_HEAD(&sbsec->list);
212 INIT_LIST_HEAD(&sbsec->isec_head);
213 spin_lock_init(&sbsec->isec_lock);
215 sbsec->sid = SECINITSID_UNLABELED;
216 sbsec->def_sid = SECINITSID_FILE;
217 sb->s_security = sbsec;
222 static void superblock_free_security(struct super_block *sb)
224 struct superblock_security_struct *sbsec = sb->s_security;
226 spin_lock(&sb_security_lock);
227 if (!list_empty(&sbsec->list))
228 list_del_init(&sbsec->list);
229 spin_unlock(&sb_security_lock);
231 sb->s_security = NULL;
235 #ifdef CONFIG_SECURITY_NETWORK
236 static int sk_alloc_security(struct sock *sk, int family, gfp_t priority)
238 struct sk_security_struct *ssec;
240 if (family != PF_UNIX)
243 ssec = kzalloc(sizeof(*ssec), priority);
248 ssec->peer_sid = SECINITSID_UNLABELED;
249 sk->sk_security = ssec;
254 static void sk_free_security(struct sock *sk)
256 struct sk_security_struct *ssec = sk->sk_security;
258 if (sk->sk_family != PF_UNIX)
261 sk->sk_security = NULL;
264 #endif /* CONFIG_SECURITY_NETWORK */
266 /* The security server must be initialized before
267 any labeling or access decisions can be provided. */
268 extern int ss_initialized;
270 /* The file system's label must be initialized prior to use. */
272 static char *labeling_behaviors[6] = {
274 "uses transition SIDs",
276 "uses genfs_contexts",
277 "not configured for labeling",
278 "uses mountpoint labeling",
281 static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
283 static inline int inode_doinit(struct inode *inode)
285 return inode_doinit_with_dentry(inode, NULL);
294 static match_table_t tokens = {
295 {Opt_context, "context=%s"},
296 {Opt_fscontext, "fscontext=%s"},
297 {Opt_defcontext, "defcontext=%s"},
300 #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
302 static int try_context_mount(struct super_block *sb, void *data)
304 char *context = NULL, *defcontext = NULL;
307 int alloc = 0, rc = 0, seen = 0;
308 struct task_security_struct *tsec = current->security;
309 struct superblock_security_struct *sbsec = sb->s_security;
314 name = sb->s_type->name;
316 if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA) {
318 /* NFS we understand. */
319 if (!strcmp(name, "nfs")) {
320 struct nfs_mount_data *d = data;
322 if (d->version < NFS_MOUNT_VERSION)
326 context = d->context;
333 /* Standard string-based options. */
334 char *p, *options = data;
336 while ((p = strsep(&options, ",")) != NULL) {
338 substring_t args[MAX_OPT_ARGS];
343 token = match_token(p, tokens, args);
349 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
352 context = match_strdup(&args[0]);
363 if (seen & (Opt_context|Opt_fscontext)) {
365 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
368 context = match_strdup(&args[0]);
375 seen |= Opt_fscontext;
379 if (sbsec->behavior != SECURITY_FS_USE_XATTR) {
381 printk(KERN_WARNING "SELinux: "
382 "defcontext option is invalid "
383 "for this filesystem type\n");
386 if (seen & (Opt_context|Opt_defcontext)) {
388 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
391 defcontext = match_strdup(&args[0]);
398 seen |= Opt_defcontext;
403 printk(KERN_WARNING "SELinux: unknown mount "
415 rc = security_context_to_sid(context, strlen(context), &sid);
417 printk(KERN_WARNING "SELinux: security_context_to_sid"
418 "(%s) failed for (dev %s, type %s) errno=%d\n",
419 context, sb->s_id, name, rc);
423 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
424 FILESYSTEM__RELABELFROM, NULL);
428 rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
429 FILESYSTEM__RELABELTO, NULL);
435 if (seen & Opt_context)
436 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
440 rc = security_context_to_sid(defcontext, strlen(defcontext), &sid);
442 printk(KERN_WARNING "SELinux: security_context_to_sid"
443 "(%s) failed for (dev %s, type %s) errno=%d\n",
444 defcontext, sb->s_id, name, rc);
448 if (sid == sbsec->def_sid)
451 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
452 FILESYSTEM__RELABELFROM, NULL);
456 rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
457 FILESYSTEM__ASSOCIATE, NULL);
461 sbsec->def_sid = sid;
473 static int superblock_doinit(struct super_block *sb, void *data)
475 struct superblock_security_struct *sbsec = sb->s_security;
476 struct dentry *root = sb->s_root;
477 struct inode *inode = root->d_inode;
481 if (sbsec->initialized)
484 if (!ss_initialized) {
485 /* Defer initialization until selinux_complete_init,
486 after the initial policy is loaded and the security
487 server is ready to handle calls. */
488 spin_lock(&sb_security_lock);
489 if (list_empty(&sbsec->list))
490 list_add(&sbsec->list, &superblock_security_head);
491 spin_unlock(&sb_security_lock);
495 /* Determine the labeling behavior to use for this filesystem type. */
496 rc = security_fs_use(sb->s_type->name, &sbsec->behavior, &sbsec->sid);
498 printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n",
499 __FUNCTION__, sb->s_type->name, rc);
503 rc = try_context_mount(sb, data);
507 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
508 /* Make sure that the xattr handler exists and that no
509 error other than -ENODATA is returned by getxattr on
510 the root directory. -ENODATA is ok, as this may be
511 the first boot of the SELinux kernel before we have
512 assigned xattr values to the filesystem. */
513 if (!inode->i_op->getxattr) {
514 printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
515 "xattr support\n", sb->s_id, sb->s_type->name);
519 rc = inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
520 if (rc < 0 && rc != -ENODATA) {
521 if (rc == -EOPNOTSUPP)
522 printk(KERN_WARNING "SELinux: (dev %s, type "
523 "%s) has no security xattr handler\n",
524 sb->s_id, sb->s_type->name);
526 printk(KERN_WARNING "SELinux: (dev %s, type "
527 "%s) getxattr errno %d\n", sb->s_id,
528 sb->s_type->name, -rc);
533 if (strcmp(sb->s_type->name, "proc") == 0)
536 sbsec->initialized = 1;
538 if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors)) {
539 printk(KERN_INFO "SELinux: initialized (dev %s, type %s), unknown behavior\n",
540 sb->s_id, sb->s_type->name);
543 printk(KERN_INFO "SELinux: initialized (dev %s, type %s), %s\n",
544 sb->s_id, sb->s_type->name,
545 labeling_behaviors[sbsec->behavior-1]);
548 /* Initialize the root inode. */
549 rc = inode_doinit_with_dentry(sb->s_root->d_inode, sb->s_root);
551 /* Initialize any other inodes associated with the superblock, e.g.
552 inodes created prior to initial policy load or inodes created
553 during get_sb by a pseudo filesystem that directly
555 spin_lock(&sbsec->isec_lock);
557 if (!list_empty(&sbsec->isec_head)) {
558 struct inode_security_struct *isec =
559 list_entry(sbsec->isec_head.next,
560 struct inode_security_struct, list);
561 struct inode *inode = isec->inode;
562 spin_unlock(&sbsec->isec_lock);
563 inode = igrab(inode);
565 if (!IS_PRIVATE (inode))
569 spin_lock(&sbsec->isec_lock);
570 list_del_init(&isec->list);
573 spin_unlock(&sbsec->isec_lock);
579 static inline u16 inode_mode_to_security_class(umode_t mode)
581 switch (mode & S_IFMT) {
583 return SECCLASS_SOCK_FILE;
585 return SECCLASS_LNK_FILE;
587 return SECCLASS_FILE;
589 return SECCLASS_BLK_FILE;
593 return SECCLASS_CHR_FILE;
595 return SECCLASS_FIFO_FILE;
599 return SECCLASS_FILE;
602 static inline int default_protocol_stream(int protocol)
604 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
607 static inline int default_protocol_dgram(int protocol)
609 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
612 static inline u16 socket_type_to_security_class(int family, int type, int protocol)
619 return SECCLASS_UNIX_STREAM_SOCKET;
621 return SECCLASS_UNIX_DGRAM_SOCKET;
628 if (default_protocol_stream(protocol))
629 return SECCLASS_TCP_SOCKET;
631 return SECCLASS_RAWIP_SOCKET;
633 if (default_protocol_dgram(protocol))
634 return SECCLASS_UDP_SOCKET;
636 return SECCLASS_RAWIP_SOCKET;
638 return SECCLASS_RAWIP_SOCKET;
644 return SECCLASS_NETLINK_ROUTE_SOCKET;
645 case NETLINK_FIREWALL:
646 return SECCLASS_NETLINK_FIREWALL_SOCKET;
647 case NETLINK_INET_DIAG:
648 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
650 return SECCLASS_NETLINK_NFLOG_SOCKET;
652 return SECCLASS_NETLINK_XFRM_SOCKET;
653 case NETLINK_SELINUX:
654 return SECCLASS_NETLINK_SELINUX_SOCKET;
656 return SECCLASS_NETLINK_AUDIT_SOCKET;
658 return SECCLASS_NETLINK_IP6FW_SOCKET;
659 case NETLINK_DNRTMSG:
660 return SECCLASS_NETLINK_DNRT_SOCKET;
661 case NETLINK_KOBJECT_UEVENT:
662 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
664 return SECCLASS_NETLINK_SOCKET;
667 return SECCLASS_PACKET_SOCKET;
669 return SECCLASS_KEY_SOCKET;
672 return SECCLASS_SOCKET;
675 #ifdef CONFIG_PROC_FS
676 static int selinux_proc_get_sid(struct proc_dir_entry *de,
681 char *buffer, *path, *end;
683 buffer = (char*)__get_free_page(GFP_KERNEL);
693 while (de && de != de->parent) {
694 buflen -= de->namelen + 1;
698 memcpy(end, de->name, de->namelen);
703 rc = security_genfs_sid("proc", path, tclass, sid);
704 free_page((unsigned long)buffer);
708 static int selinux_proc_get_sid(struct proc_dir_entry *de,
716 /* The inode's security attributes must be initialized before first use. */
717 static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
719 struct superblock_security_struct *sbsec = NULL;
720 struct inode_security_struct *isec = inode->i_security;
722 struct dentry *dentry;
723 #define INITCONTEXTLEN 255
724 char *context = NULL;
729 if (isec->initialized)
734 if (isec->initialized)
737 sbsec = inode->i_sb->s_security;
738 if (!sbsec->initialized) {
739 /* Defer initialization until selinux_complete_init,
740 after the initial policy is loaded and the security
741 server is ready to handle calls. */
742 spin_lock(&sbsec->isec_lock);
743 if (list_empty(&isec->list))
744 list_add(&isec->list, &sbsec->isec_head);
745 spin_unlock(&sbsec->isec_lock);
749 switch (sbsec->behavior) {
750 case SECURITY_FS_USE_XATTR:
751 if (!inode->i_op->getxattr) {
752 isec->sid = sbsec->def_sid;
756 /* Need a dentry, since the xattr API requires one.
757 Life would be simpler if we could just pass the inode. */
759 /* Called from d_instantiate or d_splice_alias. */
760 dentry = dget(opt_dentry);
762 /* Called from selinux_complete_init, try to find a dentry. */
763 dentry = d_find_alias(inode);
766 printk(KERN_WARNING "%s: no dentry for dev=%s "
767 "ino=%ld\n", __FUNCTION__, inode->i_sb->s_id,
772 len = INITCONTEXTLEN;
773 context = kmalloc(len, GFP_KERNEL);
779 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
782 /* Need a larger buffer. Query for the right size. */
783 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
791 context = kmalloc(len, GFP_KERNEL);
797 rc = inode->i_op->getxattr(dentry,
803 if (rc != -ENODATA) {
804 printk(KERN_WARNING "%s: getxattr returned "
805 "%d for dev=%s ino=%ld\n", __FUNCTION__,
806 -rc, inode->i_sb->s_id, inode->i_ino);
810 /* Map ENODATA to the default file SID */
811 sid = sbsec->def_sid;
814 rc = security_context_to_sid_default(context, rc, &sid,
817 printk(KERN_WARNING "%s: context_to_sid(%s) "
818 "returned %d for dev=%s ino=%ld\n",
819 __FUNCTION__, context, -rc,
820 inode->i_sb->s_id, inode->i_ino);
822 /* Leave with the unlabeled SID */
830 case SECURITY_FS_USE_TASK:
831 isec->sid = isec->task_sid;
833 case SECURITY_FS_USE_TRANS:
834 /* Default to the fs SID. */
835 isec->sid = sbsec->sid;
837 /* Try to obtain a transition SID. */
838 isec->sclass = inode_mode_to_security_class(inode->i_mode);
839 rc = security_transition_sid(isec->task_sid,
848 /* Default to the fs SID. */
849 isec->sid = sbsec->sid;
852 struct proc_inode *proci = PROC_I(inode);
854 isec->sclass = inode_mode_to_security_class(inode->i_mode);
855 rc = selinux_proc_get_sid(proci->pde,
866 isec->initialized = 1;
869 if (isec->sclass == SECCLASS_FILE)
870 isec->sclass = inode_mode_to_security_class(inode->i_mode);
877 /* Convert a Linux signal to an access vector. */
878 static inline u32 signal_to_av(int sig)
884 /* Commonly granted from child to parent. */
885 perm = PROCESS__SIGCHLD;
888 /* Cannot be caught or ignored */
889 perm = PROCESS__SIGKILL;
892 /* Cannot be caught or ignored */
893 perm = PROCESS__SIGSTOP;
896 /* All other signals. */
897 perm = PROCESS__SIGNAL;
904 /* Check permission betweeen a pair of tasks, e.g. signal checks,
905 fork check, ptrace check, etc. */
906 static int task_has_perm(struct task_struct *tsk1,
907 struct task_struct *tsk2,
910 struct task_security_struct *tsec1, *tsec2;
912 tsec1 = tsk1->security;
913 tsec2 = tsk2->security;
914 return avc_has_perm(tsec1->sid, tsec2->sid,
915 SECCLASS_PROCESS, perms, NULL);
918 /* Check whether a task is allowed to use a capability. */
919 static int task_has_capability(struct task_struct *tsk,
922 struct task_security_struct *tsec;
923 struct avc_audit_data ad;
925 tsec = tsk->security;
927 AVC_AUDIT_DATA_INIT(&ad,CAP);
931 return avc_has_perm(tsec->sid, tsec->sid,
932 SECCLASS_CAPABILITY, CAP_TO_MASK(cap), &ad);
935 /* Check whether a task is allowed to use a system operation. */
936 static int task_has_system(struct task_struct *tsk,
939 struct task_security_struct *tsec;
941 tsec = tsk->security;
943 return avc_has_perm(tsec->sid, SECINITSID_KERNEL,
944 SECCLASS_SYSTEM, perms, NULL);
947 /* Check whether a task has a particular permission to an inode.
948 The 'adp' parameter is optional and allows other audit
949 data to be passed (e.g. the dentry). */
950 static int inode_has_perm(struct task_struct *tsk,
953 struct avc_audit_data *adp)
955 struct task_security_struct *tsec;
956 struct inode_security_struct *isec;
957 struct avc_audit_data ad;
959 tsec = tsk->security;
960 isec = inode->i_security;
964 AVC_AUDIT_DATA_INIT(&ad, FS);
965 ad.u.fs.inode = inode;
968 return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, adp);
971 /* Same as inode_has_perm, but pass explicit audit data containing
972 the dentry to help the auditing code to more easily generate the
973 pathname if needed. */
974 static inline int dentry_has_perm(struct task_struct *tsk,
975 struct vfsmount *mnt,
976 struct dentry *dentry,
979 struct inode *inode = dentry->d_inode;
980 struct avc_audit_data ad;
981 AVC_AUDIT_DATA_INIT(&ad,FS);
983 ad.u.fs.dentry = dentry;
984 return inode_has_perm(tsk, inode, av, &ad);
987 /* Check whether a task can use an open file descriptor to
988 access an inode in a given way. Check access to the
989 descriptor itself, and then use dentry_has_perm to
990 check a particular permission to the file.
991 Access to the descriptor is implicitly granted if it
992 has the same SID as the process. If av is zero, then
993 access to the file is not checked, e.g. for cases
994 where only the descriptor is affected like seek. */
995 static int file_has_perm(struct task_struct *tsk,
999 struct task_security_struct *tsec = tsk->security;
1000 struct file_security_struct *fsec = file->f_security;
1001 struct vfsmount *mnt = file->f_vfsmnt;
1002 struct dentry *dentry = file->f_dentry;
1003 struct inode *inode = dentry->d_inode;
1004 struct avc_audit_data ad;
1007 AVC_AUDIT_DATA_INIT(&ad, FS);
1009 ad.u.fs.dentry = dentry;
1011 if (tsec->sid != fsec->sid) {
1012 rc = avc_has_perm(tsec->sid, fsec->sid,
1020 /* av is zero if only checking access to the descriptor. */
1022 return inode_has_perm(tsk, inode, av, &ad);
1027 /* Check whether a task can create a file. */
1028 static int may_create(struct inode *dir,
1029 struct dentry *dentry,
1032 struct task_security_struct *tsec;
1033 struct inode_security_struct *dsec;
1034 struct superblock_security_struct *sbsec;
1036 struct avc_audit_data ad;
1039 tsec = current->security;
1040 dsec = dir->i_security;
1041 sbsec = dir->i_sb->s_security;
1043 AVC_AUDIT_DATA_INIT(&ad, FS);
1044 ad.u.fs.dentry = dentry;
1046 rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR,
1047 DIR__ADD_NAME | DIR__SEARCH,
1052 if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) {
1053 newsid = tsec->create_sid;
1055 rc = security_transition_sid(tsec->sid, dsec->sid, tclass,
1061 rc = avc_has_perm(tsec->sid, newsid, tclass, FILE__CREATE, &ad);
1065 return avc_has_perm(newsid, sbsec->sid,
1066 SECCLASS_FILESYSTEM,
1067 FILESYSTEM__ASSOCIATE, &ad);
1071 #define MAY_UNLINK 1
1074 /* Check whether a task can link, unlink, or rmdir a file/directory. */
1075 static int may_link(struct inode *dir,
1076 struct dentry *dentry,
1080 struct task_security_struct *tsec;
1081 struct inode_security_struct *dsec, *isec;
1082 struct avc_audit_data ad;
1086 tsec = current->security;
1087 dsec = dir->i_security;
1088 isec = dentry->d_inode->i_security;
1090 AVC_AUDIT_DATA_INIT(&ad, FS);
1091 ad.u.fs.dentry = dentry;
1094 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
1095 rc = avc_has_perm(tsec->sid, dsec->sid, SECCLASS_DIR, av, &ad);
1110 printk(KERN_WARNING "may_link: unrecognized kind %d\n", kind);
1114 rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass, av, &ad);
1118 static inline int may_rename(struct inode *old_dir,
1119 struct dentry *old_dentry,
1120 struct inode *new_dir,
1121 struct dentry *new_dentry)
1123 struct task_security_struct *tsec;
1124 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
1125 struct avc_audit_data ad;
1127 int old_is_dir, new_is_dir;
1130 tsec = current->security;
1131 old_dsec = old_dir->i_security;
1132 old_isec = old_dentry->d_inode->i_security;
1133 old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
1134 new_dsec = new_dir->i_security;
1136 AVC_AUDIT_DATA_INIT(&ad, FS);
1138 ad.u.fs.dentry = old_dentry;
1139 rc = avc_has_perm(tsec->sid, old_dsec->sid, SECCLASS_DIR,
1140 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1143 rc = avc_has_perm(tsec->sid, old_isec->sid,
1144 old_isec->sclass, FILE__RENAME, &ad);
1147 if (old_is_dir && new_dir != old_dir) {
1148 rc = avc_has_perm(tsec->sid, old_isec->sid,
1149 old_isec->sclass, DIR__REPARENT, &ad);
1154 ad.u.fs.dentry = new_dentry;
1155 av = DIR__ADD_NAME | DIR__SEARCH;
1156 if (new_dentry->d_inode)
1157 av |= DIR__REMOVE_NAME;
1158 rc = avc_has_perm(tsec->sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
1161 if (new_dentry->d_inode) {
1162 new_isec = new_dentry->d_inode->i_security;
1163 new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
1164 rc = avc_has_perm(tsec->sid, new_isec->sid,
1166 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1174 /* Check whether a task can perform a filesystem operation. */
1175 static int superblock_has_perm(struct task_struct *tsk,
1176 struct super_block *sb,
1178 struct avc_audit_data *ad)
1180 struct task_security_struct *tsec;
1181 struct superblock_security_struct *sbsec;
1183 tsec = tsk->security;
1184 sbsec = sb->s_security;
1185 return avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
1189 /* Convert a Linux mode and permission mask to an access vector. */
1190 static inline u32 file_mask_to_av(int mode, int mask)
1194 if ((mode & S_IFMT) != S_IFDIR) {
1195 if (mask & MAY_EXEC)
1196 av |= FILE__EXECUTE;
1197 if (mask & MAY_READ)
1200 if (mask & MAY_APPEND)
1202 else if (mask & MAY_WRITE)
1206 if (mask & MAY_EXEC)
1208 if (mask & MAY_WRITE)
1210 if (mask & MAY_READ)
1217 /* Convert a Linux file to an access vector. */
1218 static inline u32 file_to_av(struct file *file)
1222 if (file->f_mode & FMODE_READ)
1224 if (file->f_mode & FMODE_WRITE) {
1225 if (file->f_flags & O_APPEND)
1234 /* Set an inode's SID to a specified value. */
1235 static int inode_security_set_sid(struct inode *inode, u32 sid)
1237 struct inode_security_struct *isec = inode->i_security;
1238 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
1240 if (!sbsec->initialized) {
1241 /* Defer initialization to selinux_complete_init. */
1246 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1248 isec->initialized = 1;
1253 /* Hook functions begin here. */
1255 static int selinux_ptrace(struct task_struct *parent, struct task_struct *child)
1257 struct task_security_struct *psec = parent->security;
1258 struct task_security_struct *csec = child->security;
1261 rc = secondary_ops->ptrace(parent,child);
1265 rc = task_has_perm(parent, child, PROCESS__PTRACE);
1266 /* Save the SID of the tracing process for later use in apply_creds. */
1268 csec->ptrace_sid = psec->sid;
1272 static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
1273 kernel_cap_t *inheritable, kernel_cap_t *permitted)
1277 error = task_has_perm(current, target, PROCESS__GETCAP);
1281 return secondary_ops->capget(target, effective, inheritable, permitted);
1284 static int selinux_capset_check(struct task_struct *target, kernel_cap_t *effective,
1285 kernel_cap_t *inheritable, kernel_cap_t *permitted)
1289 error = secondary_ops->capset_check(target, effective, inheritable, permitted);
1293 return task_has_perm(current, target, PROCESS__SETCAP);
1296 static void selinux_capset_set(struct task_struct *target, kernel_cap_t *effective,
1297 kernel_cap_t *inheritable, kernel_cap_t *permitted)
1299 secondary_ops->capset_set(target, effective, inheritable, permitted);
1302 static int selinux_capable(struct task_struct *tsk, int cap)
1306 rc = secondary_ops->capable(tsk, cap);
1310 return task_has_capability(tsk,cap);
1313 static int selinux_sysctl(ctl_table *table, int op)
1317 struct task_security_struct *tsec;
1321 rc = secondary_ops->sysctl(table, op);
1325 tsec = current->security;
1327 rc = selinux_proc_get_sid(table->de, (op == 001) ?
1328 SECCLASS_DIR : SECCLASS_FILE, &tsid);
1330 /* Default to the well-defined sysctl SID. */
1331 tsid = SECINITSID_SYSCTL;
1334 /* The op values are "defined" in sysctl.c, thereby creating
1335 * a bad coupling between this module and sysctl.c */
1337 error = avc_has_perm(tsec->sid, tsid,
1338 SECCLASS_DIR, DIR__SEARCH, NULL);
1346 error = avc_has_perm(tsec->sid, tsid,
1347 SECCLASS_FILE, av, NULL);
1353 static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
1366 rc = superblock_has_perm(current,
1368 FILESYSTEM__QUOTAMOD, NULL);
1373 rc = superblock_has_perm(current,
1375 FILESYSTEM__QUOTAGET, NULL);
1378 rc = 0; /* let the kernel handle invalid cmds */
1384 static int selinux_quota_on(struct dentry *dentry)
1386 return dentry_has_perm(current, NULL, dentry, FILE__QUOTAON);
1389 static int selinux_syslog(int type)
1393 rc = secondary_ops->syslog(type);
1398 case 3: /* Read last kernel messages */
1399 case 10: /* Return size of the log buffer */
1400 rc = task_has_system(current, SYSTEM__SYSLOG_READ);
1402 case 6: /* Disable logging to console */
1403 case 7: /* Enable logging to console */
1404 case 8: /* Set level of messages printed to console */
1405 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
1407 case 0: /* Close log */
1408 case 1: /* Open log */
1409 case 2: /* Read from log */
1410 case 4: /* Read/clear last kernel messages */
1411 case 5: /* Clear ring buffer */
1413 rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
1420 * Check that a process has enough memory to allocate a new virtual
1421 * mapping. 0 means there is enough memory for the allocation to
1422 * succeed and -ENOMEM implies there is not.
1424 * Note that secondary_ops->capable and task_has_perm_noaudit return 0
1425 * if the capability is granted, but __vm_enough_memory requires 1 if
1426 * the capability is granted.
1428 * Do not audit the selinux permission check, as this is applied to all
1429 * processes that allocate mappings.
1431 static int selinux_vm_enough_memory(long pages)
1433 int rc, cap_sys_admin = 0;
1434 struct task_security_struct *tsec = current->security;
1436 rc = secondary_ops->capable(current, CAP_SYS_ADMIN);
1438 rc = avc_has_perm_noaudit(tsec->sid, tsec->sid,
1439 SECCLASS_CAPABILITY,
1440 CAP_TO_MASK(CAP_SYS_ADMIN),
1446 return __vm_enough_memory(pages, cap_sys_admin);
1449 /* binprm security operations */
1451 static int selinux_bprm_alloc_security(struct linux_binprm *bprm)
1453 struct bprm_security_struct *bsec;
1455 bsec = kzalloc(sizeof(struct bprm_security_struct), GFP_KERNEL);
1460 bsec->sid = SECINITSID_UNLABELED;
1463 bprm->security = bsec;
1467 static int selinux_bprm_set_security(struct linux_binprm *bprm)
1469 struct task_security_struct *tsec;
1470 struct inode *inode = bprm->file->f_dentry->d_inode;
1471 struct inode_security_struct *isec;
1472 struct bprm_security_struct *bsec;
1474 struct avc_audit_data ad;
1477 rc = secondary_ops->bprm_set_security(bprm);
1481 bsec = bprm->security;
1486 tsec = current->security;
1487 isec = inode->i_security;
1489 /* Default to the current task SID. */
1490 bsec->sid = tsec->sid;
1492 /* Reset create SID on execve. */
1493 tsec->create_sid = 0;
1495 if (tsec->exec_sid) {
1496 newsid = tsec->exec_sid;
1497 /* Reset exec SID on execve. */
1500 /* Check for a default transition on this program. */
1501 rc = security_transition_sid(tsec->sid, isec->sid,
1502 SECCLASS_PROCESS, &newsid);
1507 AVC_AUDIT_DATA_INIT(&ad, FS);
1508 ad.u.fs.mnt = bprm->file->f_vfsmnt;
1509 ad.u.fs.dentry = bprm->file->f_dentry;
1511 if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
1514 if (tsec->sid == newsid) {
1515 rc = avc_has_perm(tsec->sid, isec->sid,
1516 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
1520 /* Check permissions for the transition. */
1521 rc = avc_has_perm(tsec->sid, newsid,
1522 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
1526 rc = avc_has_perm(newsid, isec->sid,
1527 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
1531 /* Clear any possibly unsafe personality bits on exec: */
1532 current->personality &= ~PER_CLEAR_ON_SETID;
1534 /* Set the security field to the new SID. */
1542 static int selinux_bprm_check_security (struct linux_binprm *bprm)
1544 return secondary_ops->bprm_check_security(bprm);
1548 static int selinux_bprm_secureexec (struct linux_binprm *bprm)
1550 struct task_security_struct *tsec = current->security;
1553 if (tsec->osid != tsec->sid) {
1554 /* Enable secure mode for SIDs transitions unless
1555 the noatsecure permission is granted between
1556 the two SIDs, i.e. ahp returns 0. */
1557 atsecure = avc_has_perm(tsec->osid, tsec->sid,
1559 PROCESS__NOATSECURE, NULL);
1562 return (atsecure || secondary_ops->bprm_secureexec(bprm));
1565 static void selinux_bprm_free_security(struct linux_binprm *bprm)
1567 kfree(bprm->security);
1568 bprm->security = NULL;
1571 extern struct vfsmount *selinuxfs_mount;
1572 extern struct dentry *selinux_null;
1574 /* Derived from fs/exec.c:flush_old_files. */
1575 static inline void flush_unauthorized_files(struct files_struct * files)
1577 struct avc_audit_data ad;
1578 struct file *file, *devnull = NULL;
1579 struct tty_struct *tty = current->signal->tty;
1580 struct fdtable *fdt;
1585 file = list_entry(tty->tty_files.next, typeof(*file), f_u.fu_list);
1587 /* Revalidate access to controlling tty.
1588 Use inode_has_perm on the tty inode directly rather
1589 than using file_has_perm, as this particular open
1590 file may belong to another process and we are only
1591 interested in the inode-based check here. */
1592 struct inode *inode = file->f_dentry->d_inode;
1593 if (inode_has_perm(current, inode,
1594 FILE__READ | FILE__WRITE, NULL)) {
1595 /* Reset controlling tty. */
1596 current->signal->tty = NULL;
1597 current->signal->tty_old_pgrp = 0;
1603 /* Revalidate access to inherited open files. */
1605 AVC_AUDIT_DATA_INIT(&ad,FS);
1607 spin_lock(&files->file_lock);
1609 unsigned long set, i;
1614 fdt = files_fdtable(files);
1615 if (i >= fdt->max_fds || i >= fdt->max_fdset)
1617 set = fdt->open_fds->fds_bits[j];
1620 spin_unlock(&files->file_lock);
1621 for ( ; set ; i++,set >>= 1) {
1626 if (file_has_perm(current,
1628 file_to_av(file))) {
1630 fd = get_unused_fd();
1640 devnull = dentry_open(dget(selinux_null), mntget(selinuxfs_mount), O_RDWR);
1647 fd_install(fd, devnull);
1652 spin_lock(&files->file_lock);
1655 spin_unlock(&files->file_lock);
1658 static void selinux_bprm_apply_creds(struct linux_binprm *bprm, int unsafe)
1660 struct task_security_struct *tsec;
1661 struct bprm_security_struct *bsec;
1665 secondary_ops->bprm_apply_creds(bprm, unsafe);
1667 tsec = current->security;
1669 bsec = bprm->security;
1672 tsec->osid = tsec->sid;
1674 if (tsec->sid != sid) {
1675 /* Check for shared state. If not ok, leave SID
1676 unchanged and kill. */
1677 if (unsafe & LSM_UNSAFE_SHARE) {
1678 rc = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
1679 PROCESS__SHARE, NULL);
1686 /* Check for ptracing, and update the task SID if ok.
1687 Otherwise, leave SID unchanged and kill. */
1688 if (unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
1689 rc = avc_has_perm(tsec->ptrace_sid, sid,
1690 SECCLASS_PROCESS, PROCESS__PTRACE,
1702 * called after apply_creds without the task lock held
1704 static void selinux_bprm_post_apply_creds(struct linux_binprm *bprm)
1706 struct task_security_struct *tsec;
1707 struct rlimit *rlim, *initrlim;
1708 struct itimerval itimer;
1709 struct bprm_security_struct *bsec;
1712 tsec = current->security;
1713 bsec = bprm->security;
1716 force_sig_specific(SIGKILL, current);
1719 if (tsec->osid == tsec->sid)
1722 /* Close files for which the new task SID is not authorized. */
1723 flush_unauthorized_files(current->files);
1725 /* Check whether the new SID can inherit signal state
1726 from the old SID. If not, clear itimers to avoid
1727 subsequent signal generation and flush and unblock
1728 signals. This must occur _after_ the task SID has
1729 been updated so that any kill done after the flush
1730 will be checked against the new SID. */
1731 rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS,
1732 PROCESS__SIGINH, NULL);
1734 memset(&itimer, 0, sizeof itimer);
1735 for (i = 0; i < 3; i++)
1736 do_setitimer(i, &itimer, NULL);
1737 flush_signals(current);
1738 spin_lock_irq(¤t->sighand->siglock);
1739 flush_signal_handlers(current, 1);
1740 sigemptyset(¤t->blocked);
1741 recalc_sigpending();
1742 spin_unlock_irq(¤t->sighand->siglock);
1745 /* Check whether the new SID can inherit resource limits
1746 from the old SID. If not, reset all soft limits to
1747 the lower of the current task's hard limit and the init
1748 task's soft limit. Note that the setting of hard limits
1749 (even to lower them) can be controlled by the setrlimit
1750 check. The inclusion of the init task's soft limit into
1751 the computation is to avoid resetting soft limits higher
1752 than the default soft limit for cases where the default
1753 is lower than the hard limit, e.g. RLIMIT_CORE or
1755 rc = avc_has_perm(tsec->osid, tsec->sid, SECCLASS_PROCESS,
1756 PROCESS__RLIMITINH, NULL);
1758 for (i = 0; i < RLIM_NLIMITS; i++) {
1759 rlim = current->signal->rlim + i;
1760 initrlim = init_task.signal->rlim+i;
1761 rlim->rlim_cur = min(rlim->rlim_max,initrlim->rlim_cur);
1763 if (current->signal->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
1765 * This will cause RLIMIT_CPU calculations
1768 current->it_prof_expires = jiffies_to_cputime(1);
1772 /* Wake up the parent if it is waiting so that it can
1773 recheck wait permission to the new task SID. */
1774 wake_up_interruptible(¤t->parent->signal->wait_chldexit);
1777 /* superblock security operations */
1779 static int selinux_sb_alloc_security(struct super_block *sb)
1781 return superblock_alloc_security(sb);
1784 static void selinux_sb_free_security(struct super_block *sb)
1786 superblock_free_security(sb);
1789 static inline int match_prefix(char *prefix, int plen, char *option, int olen)
1794 return !memcmp(prefix, option, plen);
1797 static inline int selinux_option(char *option, int len)
1799 return (match_prefix("context=", sizeof("context=")-1, option, len) ||
1800 match_prefix("fscontext=", sizeof("fscontext=")-1, option, len) ||
1801 match_prefix("defcontext=", sizeof("defcontext=")-1, option, len));
1804 static inline void take_option(char **to, char *from, int *first, int len)
1812 memcpy(*to, from, len);
1816 static int selinux_sb_copy_data(struct file_system_type *type, void *orig, void *copy)
1818 int fnosec, fsec, rc = 0;
1819 char *in_save, *in_curr, *in_end;
1820 char *sec_curr, *nosec_save, *nosec;
1825 /* Binary mount data: just copy */
1826 if (type->fs_flags & FS_BINARY_MOUNTDATA) {
1827 copy_page(sec_curr, in_curr);
1831 nosec = (char *)get_zeroed_page(GFP_KERNEL);
1839 in_save = in_end = orig;
1842 if (*in_end == ',' || *in_end == '\0') {
1843 int len = in_end - in_curr;
1845 if (selinux_option(in_curr, len))
1846 take_option(&sec_curr, in_curr, &fsec, len);
1848 take_option(&nosec, in_curr, &fnosec, len);
1850 in_curr = in_end + 1;
1852 } while (*in_end++);
1854 strcpy(in_save, nosec_save);
1855 free_page((unsigned long)nosec_save);
1860 static int selinux_sb_kern_mount(struct super_block *sb, void *data)
1862 struct avc_audit_data ad;
1865 rc = superblock_doinit(sb, data);
1869 AVC_AUDIT_DATA_INIT(&ad,FS);
1870 ad.u.fs.dentry = sb->s_root;
1871 return superblock_has_perm(current, sb, FILESYSTEM__MOUNT, &ad);
1874 static int selinux_sb_statfs(struct super_block *sb)
1876 struct avc_audit_data ad;
1878 AVC_AUDIT_DATA_INIT(&ad,FS);
1879 ad.u.fs.dentry = sb->s_root;
1880 return superblock_has_perm(current, sb, FILESYSTEM__GETATTR, &ad);
1883 static int selinux_mount(char * dev_name,
1884 struct nameidata *nd,
1886 unsigned long flags,
1891 rc = secondary_ops->sb_mount(dev_name, nd, type, flags, data);
1895 if (flags & MS_REMOUNT)
1896 return superblock_has_perm(current, nd->mnt->mnt_sb,
1897 FILESYSTEM__REMOUNT, NULL);
1899 return dentry_has_perm(current, nd->mnt, nd->dentry,
1903 static int selinux_umount(struct vfsmount *mnt, int flags)
1907 rc = secondary_ops->sb_umount(mnt, flags);
1911 return superblock_has_perm(current,mnt->mnt_sb,
1912 FILESYSTEM__UNMOUNT,NULL);
1915 /* inode security operations */
1917 static int selinux_inode_alloc_security(struct inode *inode)
1919 return inode_alloc_security(inode);
1922 static void selinux_inode_free_security(struct inode *inode)
1924 inode_free_security(inode);
1927 static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
1928 char **name, void **value,
1931 struct task_security_struct *tsec;
1932 struct inode_security_struct *dsec;
1933 struct superblock_security_struct *sbsec;
1934 struct inode_security_struct *isec;
1937 char *namep = NULL, *context;
1939 tsec = current->security;
1940 dsec = dir->i_security;
1941 sbsec = dir->i_sb->s_security;
1942 isec = inode->i_security;
1944 if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) {
1945 newsid = tsec->create_sid;
1947 rc = security_transition_sid(tsec->sid, dsec->sid,
1948 inode_mode_to_security_class(inode->i_mode),
1951 printk(KERN_WARNING "%s: "
1952 "security_transition_sid failed, rc=%d (dev=%s "
1955 -rc, inode->i_sb->s_id, inode->i_ino);
1960 inode_security_set_sid(inode, newsid);
1962 if (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)
1966 namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_KERNEL);
1973 rc = security_sid_to_context(newsid, &context, &clen);
1985 static int selinux_inode_create(struct inode *dir, struct dentry *dentry, int mask)
1987 return may_create(dir, dentry, SECCLASS_FILE);
1990 static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1994 rc = secondary_ops->inode_link(old_dentry,dir,new_dentry);
1997 return may_link(dir, old_dentry, MAY_LINK);
2000 static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2004 rc = secondary_ops->inode_unlink(dir, dentry);
2007 return may_link(dir, dentry, MAY_UNLINK);
2010 static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2012 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2015 static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, int mask)
2017 return may_create(dir, dentry, SECCLASS_DIR);
2020 static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2022 return may_link(dir, dentry, MAY_RMDIR);
2025 static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2029 rc = secondary_ops->inode_mknod(dir, dentry, mode, dev);
2033 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2036 static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
2037 struct inode *new_inode, struct dentry *new_dentry)
2039 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2042 static int selinux_inode_readlink(struct dentry *dentry)
2044 return dentry_has_perm(current, NULL, dentry, FILE__READ);
2047 static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
2051 rc = secondary_ops->inode_follow_link(dentry,nameidata);
2054 return dentry_has_perm(current, NULL, dentry, FILE__READ);
2057 static int selinux_inode_permission(struct inode *inode, int mask,
2058 struct nameidata *nd)
2062 rc = secondary_ops->inode_permission(inode, mask, nd);
2067 /* No permission to check. Existence test. */
2071 return inode_has_perm(current, inode,
2072 file_mask_to_av(inode->i_mode, mask), NULL);
2075 static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
2079 rc = secondary_ops->inode_setattr(dentry, iattr);
2083 if (iattr->ia_valid & ATTR_FORCE)
2086 if (iattr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
2087 ATTR_ATIME_SET | ATTR_MTIME_SET))
2088 return dentry_has_perm(current, NULL, dentry, FILE__SETATTR);
2090 return dentry_has_perm(current, NULL, dentry, FILE__WRITE);
2093 static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
2095 return dentry_has_perm(current, mnt, dentry, FILE__GETATTR);
2098 static int selinux_inode_setxattr(struct dentry *dentry, char *name, void *value, size_t size, int flags)
2100 struct task_security_struct *tsec = current->security;
2101 struct inode *inode = dentry->d_inode;
2102 struct inode_security_struct *isec = inode->i_security;
2103 struct superblock_security_struct *sbsec;
2104 struct avc_audit_data ad;
2108 if (strcmp(name, XATTR_NAME_SELINUX)) {
2109 if (!strncmp(name, XATTR_SECURITY_PREFIX,
2110 sizeof XATTR_SECURITY_PREFIX - 1) &&
2111 !capable(CAP_SYS_ADMIN)) {
2112 /* A different attribute in the security namespace.
2113 Restrict to administrator. */
2117 /* Not an attribute we recognize, so just check the
2118 ordinary setattr permission. */
2119 return dentry_has_perm(current, NULL, dentry, FILE__SETATTR);
2122 sbsec = inode->i_sb->s_security;
2123 if (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)
2126 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
2129 AVC_AUDIT_DATA_INIT(&ad,FS);
2130 ad.u.fs.dentry = dentry;
2132 rc = avc_has_perm(tsec->sid, isec->sid, isec->sclass,
2133 FILE__RELABELFROM, &ad);
2137 rc = security_context_to_sid(value, size, &newsid);
2141 rc = avc_has_perm(tsec->sid, newsid, isec->sclass,
2142 FILE__RELABELTO, &ad);
2146 rc = security_validate_transition(isec->sid, newsid, tsec->sid,
2151 return avc_has_perm(newsid,
2153 SECCLASS_FILESYSTEM,
2154 FILESYSTEM__ASSOCIATE,
2158 static void selinux_inode_post_setxattr(struct dentry *dentry, char *name,
2159 void *value, size_t size, int flags)
2161 struct inode *inode = dentry->d_inode;
2162 struct inode_security_struct *isec = inode->i_security;
2166 if (strcmp(name, XATTR_NAME_SELINUX)) {
2167 /* Not an attribute we recognize, so nothing to do. */
2171 rc = security_context_to_sid(value, size, &newsid);
2173 printk(KERN_WARNING "%s: unable to obtain SID for context "
2174 "%s, rc=%d\n", __FUNCTION__, (char*)value, -rc);
2182 static int selinux_inode_getxattr (struct dentry *dentry, char *name)
2184 return dentry_has_perm(current, NULL, dentry, FILE__GETATTR);
2187 static int selinux_inode_listxattr (struct dentry *dentry)
2189 return dentry_has_perm(current, NULL, dentry, FILE__GETATTR);
2192 static int selinux_inode_removexattr (struct dentry *dentry, char *name)
2194 if (strcmp(name, XATTR_NAME_SELINUX)) {
2195 if (!strncmp(name, XATTR_SECURITY_PREFIX,
2196 sizeof XATTR_SECURITY_PREFIX - 1) &&
2197 !capable(CAP_SYS_ADMIN)) {
2198 /* A different attribute in the security namespace.
2199 Restrict to administrator. */
2203 /* Not an attribute we recognize, so just check the
2204 ordinary setattr permission. Might want a separate
2205 permission for removexattr. */
2206 return dentry_has_perm(current, NULL, dentry, FILE__SETATTR);
2209 /* No one is allowed to remove a SELinux security label.
2210 You can change the label, but all data must be labeled. */
2215 * Copy the in-core inode security context value to the user. If the
2216 * getxattr() prior to this succeeded, check to see if we need to
2217 * canonicalize the value to be finally returned to the user.
2219 * Permission check is handled by selinux_inode_getxattr hook.
2221 static int selinux_inode_getsecurity(struct inode *inode, const char *name, void *buffer, size_t size, int err)
2223 struct inode_security_struct *isec = inode->i_security;
2228 if (strcmp(name, XATTR_SELINUX_SUFFIX)) {
2233 rc = security_sid_to_context(isec->sid, &context, &len);
2237 /* Probe for required buffer size */
2238 if (!buffer || !size) {
2249 if ((len == err) && !(memcmp(context, buffer, len))) {
2250 /* Don't need to canonicalize value */
2254 memset(buffer, 0, size);
2256 memcpy(buffer, context, len);
2264 static int selinux_inode_setsecurity(struct inode *inode, const char *name,
2265 const void *value, size_t size, int flags)
2267 struct inode_security_struct *isec = inode->i_security;
2271 if (strcmp(name, XATTR_SELINUX_SUFFIX))
2274 if (!value || !size)
2277 rc = security_context_to_sid((void*)value, size, &newsid);
2285 static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
2287 const int len = sizeof(XATTR_NAME_SELINUX);
2288 if (buffer && len <= buffer_size)
2289 memcpy(buffer, XATTR_NAME_SELINUX, len);
2293 /* file security operations */
2295 static int selinux_file_permission(struct file *file, int mask)
2297 struct inode *inode = file->f_dentry->d_inode;
2300 /* No permission to check. Existence test. */
2304 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
2305 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
2308 return file_has_perm(current, file,
2309 file_mask_to_av(inode->i_mode, mask));
2312 static int selinux_file_alloc_security(struct file *file)
2314 return file_alloc_security(file);
2317 static void selinux_file_free_security(struct file *file)
2319 file_free_security(file);
2322 static int selinux_file_ioctl(struct file *file, unsigned int cmd,
2334 case EXT2_IOC_GETFLAGS:
2336 case EXT2_IOC_GETVERSION:
2337 error = file_has_perm(current, file, FILE__GETATTR);
2340 case EXT2_IOC_SETFLAGS:
2342 case EXT2_IOC_SETVERSION:
2343 error = file_has_perm(current, file, FILE__SETATTR);
2346 /* sys_ioctl() checks */
2350 error = file_has_perm(current, file, 0);
2355 error = task_has_capability(current,CAP_SYS_TTY_CONFIG);
2358 /* default case assumes that the command will go
2359 * to the file's ioctl() function.
2362 error = file_has_perm(current, file, FILE__IOCTL);
2368 static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
2370 #ifndef CONFIG_PPC32
2371 if ((prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
2373 * We are making executable an anonymous mapping or a
2374 * private file mapping that will also be writable.
2375 * This has an additional check.
2377 int rc = task_has_perm(current, current, PROCESS__EXECMEM);
2384 /* read access is always possible with a mapping */
2385 u32 av = FILE__READ;
2387 /* write access only matters if the mapping is shared */
2388 if (shared && (prot & PROT_WRITE))
2391 if (prot & PROT_EXEC)
2392 av |= FILE__EXECUTE;
2394 return file_has_perm(current, file, av);
2399 static int selinux_file_mmap(struct file *file, unsigned long reqprot,
2400 unsigned long prot, unsigned long flags)
2404 rc = secondary_ops->file_mmap(file, reqprot, prot, flags);
2408 if (selinux_checkreqprot)
2411 return file_map_prot_check(file, prot,
2412 (flags & MAP_TYPE) == MAP_SHARED);
2415 static int selinux_file_mprotect(struct vm_area_struct *vma,
2416 unsigned long reqprot,
2421 rc = secondary_ops->file_mprotect(vma, reqprot, prot);
2425 if (selinux_checkreqprot)
2428 #ifndef CONFIG_PPC32
2429 if ((prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
2431 if (vma->vm_start >= vma->vm_mm->start_brk &&
2432 vma->vm_end <= vma->vm_mm->brk) {
2433 rc = task_has_perm(current, current,
2435 } else if (!vma->vm_file &&
2436 vma->vm_start <= vma->vm_mm->start_stack &&
2437 vma->vm_end >= vma->vm_mm->start_stack) {
2438 rc = task_has_perm(current, current, PROCESS__EXECSTACK);
2439 } else if (vma->vm_file && vma->anon_vma) {
2441 * We are making executable a file mapping that has
2442 * had some COW done. Since pages might have been
2443 * written, check ability to execute the possibly
2444 * modified content. This typically should only
2445 * occur for text relocations.
2447 rc = file_has_perm(current, vma->vm_file,
2455 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
2458 static int selinux_file_lock(struct file *file, unsigned int cmd)
2460 return file_has_perm(current, file, FILE__LOCK);
2463 static int selinux_file_fcntl(struct file *file, unsigned int cmd,
2470 if (!file->f_dentry || !file->f_dentry->d_inode) {
2475 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
2476 err = file_has_perm(current, file,FILE__WRITE);
2485 /* Just check FD__USE permission */
2486 err = file_has_perm(current, file, 0);
2491 #if BITS_PER_LONG == 32
2496 if (!file->f_dentry || !file->f_dentry->d_inode) {
2500 err = file_has_perm(current, file, FILE__LOCK);
2507 static int selinux_file_set_fowner(struct file *file)
2509 struct task_security_struct *tsec;
2510 struct file_security_struct *fsec;
2512 tsec = current->security;
2513 fsec = file->f_security;
2514 fsec->fown_sid = tsec->sid;
2519 static int selinux_file_send_sigiotask(struct task_struct *tsk,
2520 struct fown_struct *fown, int signum)
2524 struct task_security_struct *tsec;
2525 struct file_security_struct *fsec;
2527 /* struct fown_struct is never outside the context of a struct file */
2528 file = (struct file *)((long)fown - offsetof(struct file,f_owner));
2530 tsec = tsk->security;
2531 fsec = file->f_security;
2534 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
2536 perm = signal_to_av(signum);
2538 return avc_has_perm(fsec->fown_sid, tsec->sid,
2539 SECCLASS_PROCESS, perm, NULL);
2542 static int selinux_file_receive(struct file *file)
2544 return file_has_perm(current, file, file_to_av(file));
2547 /* task security operations */
2549 static int selinux_task_create(unsigned long clone_flags)
2553 rc = secondary_ops->task_create(clone_flags);
2557 return task_has_perm(current, current, PROCESS__FORK);
2560 static int selinux_task_alloc_security(struct task_struct *tsk)
2562 struct task_security_struct *tsec1, *tsec2;
2565 tsec1 = current->security;
2567 rc = task_alloc_security(tsk);
2570 tsec2 = tsk->security;
2572 tsec2->osid = tsec1->osid;
2573 tsec2->sid = tsec1->sid;
2575 /* Retain the exec and create SIDs across fork */
2576 tsec2->exec_sid = tsec1->exec_sid;
2577 tsec2->create_sid = tsec1->create_sid;
2579 /* Retain ptracer SID across fork, if any.
2580 This will be reset by the ptrace hook upon any
2581 subsequent ptrace_attach operations. */
2582 tsec2->ptrace_sid = tsec1->ptrace_sid;
2587 static void selinux_task_free_security(struct task_struct *tsk)
2589 task_free_security(tsk);
2592 static int selinux_task_setuid(uid_t id0, uid_t id1, uid_t id2, int flags)
2594 /* Since setuid only affects the current process, and
2595 since the SELinux controls are not based on the Linux
2596 identity attributes, SELinux does not need to control
2597 this operation. However, SELinux does control the use
2598 of the CAP_SETUID and CAP_SETGID capabilities using the
2603 static int selinux_task_post_setuid(uid_t id0, uid_t id1, uid_t id2, int flags)
2605 return secondary_ops->task_post_setuid(id0,id1,id2,flags);
2608 static int selinux_task_setgid(gid_t id0, gid_t id1, gid_t id2, int flags)
2610 /* See the comment for setuid above. */
2614 static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
2616 return task_has_perm(current, p, PROCESS__SETPGID);
2619 static int selinux_task_getpgid(struct task_struct *p)
2621 return task_has_perm(current, p, PROCESS__GETPGID);
2624 static int selinux_task_getsid(struct task_struct *p)
2626 return task_has_perm(current, p, PROCESS__GETSESSION);
2629 static int selinux_task_setgroups(struct group_info *group_info)
2631 /* See the comment for setuid above. */
2635 static int selinux_task_setnice(struct task_struct *p, int nice)
2639 rc = secondary_ops->task_setnice(p, nice);
2643 return task_has_perm(current,p, PROCESS__SETSCHED);
2646 static int selinux_task_setrlimit(unsigned int resource, struct rlimit *new_rlim)
2648 struct rlimit *old_rlim = current->signal->rlim + resource;
2651 rc = secondary_ops->task_setrlimit(resource, new_rlim);
2655 /* Control the ability to change the hard limit (whether
2656 lowering or raising it), so that the hard limit can
2657 later be used as a safe reset point for the soft limit
2658 upon context transitions. See selinux_bprm_apply_creds. */
2659 if (old_rlim->rlim_max != new_rlim->rlim_max)
2660 return task_has_perm(current, current, PROCESS__SETRLIMIT);
2665 static int selinux_task_setscheduler(struct task_struct *p, int policy, struct sched_param *lp)
2667 return task_has_perm(current, p, PROCESS__SETSCHED);
2670 static int selinux_task_getscheduler(struct task_struct *p)
2672 return task_has_perm(current, p, PROCESS__GETSCHED);
2675 static int selinux_task_kill(struct task_struct *p, struct siginfo *info, int sig)
2680 rc = secondary_ops->task_kill(p, info, sig);
2684 if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info)))
2688 perm = PROCESS__SIGNULL; /* null signal; existence test */
2690 perm = signal_to_av(sig);
2692 return task_has_perm(current, p, perm);
2695 static int selinux_task_prctl(int option,
2701 /* The current prctl operations do not appear to require
2702 any SELinux controls since they merely observe or modify
2703 the state of the current process. */
2707 static int selinux_task_wait(struct task_struct *p)
2711 perm = signal_to_av(p->exit_signal);
2713 return task_has_perm(p, current, perm);
2716 static void selinux_task_reparent_to_init(struct task_struct *p)
2718 struct task_security_struct *tsec;
2720 secondary_ops->task_reparent_to_init(p);
2723 tsec->osid = tsec->sid;
2724 tsec->sid = SECINITSID_KERNEL;
2728 static void selinux_task_to_inode(struct task_struct *p,
2729 struct inode *inode)
2731 struct task_security_struct *tsec = p->security;
2732 struct inode_security_struct *isec = inode->i_security;
2734 isec->sid = tsec->sid;
2735 isec->initialized = 1;
2739 #ifdef CONFIG_SECURITY_NETWORK
2741 /* Returns error only if unable to parse addresses */
2742 static int selinux_parse_skb_ipv4(struct sk_buff *skb, struct avc_audit_data *ad)
2744 int offset, ihlen, ret = -EINVAL;
2745 struct iphdr _iph, *ih;
2747 offset = skb->nh.raw - skb->data;
2748 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
2752 ihlen = ih->ihl * 4;
2753 if (ihlen < sizeof(_iph))
2756 ad->u.net.v4info.saddr = ih->saddr;
2757 ad->u.net.v4info.daddr = ih->daddr;
2760 switch (ih->protocol) {
2762 struct tcphdr _tcph, *th;
2764 if (ntohs(ih->frag_off) & IP_OFFSET)
2768 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
2772 ad->u.net.sport = th->source;
2773 ad->u.net.dport = th->dest;
2778 struct udphdr _udph, *uh;
2780 if (ntohs(ih->frag_off) & IP_OFFSET)
2784 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
2788 ad->u.net.sport = uh->source;
2789 ad->u.net.dport = uh->dest;
2800 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2802 /* Returns error only if unable to parse addresses */
2803 static int selinux_parse_skb_ipv6(struct sk_buff *skb, struct avc_audit_data *ad)
2806 int ret = -EINVAL, offset;
2807 struct ipv6hdr _ipv6h, *ip6;
2809 offset = skb->nh.raw - skb->data;
2810 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
2814 ipv6_addr_copy(&ad->u.net.v6info.saddr, &ip6->saddr);
2815 ipv6_addr_copy(&ad->u.net.v6info.daddr, &ip6->daddr);
2818 nexthdr = ip6->nexthdr;
2819 offset += sizeof(_ipv6h);
2820 offset = ipv6_skip_exthdr(skb, offset, &nexthdr);
2826 struct tcphdr _tcph, *th;
2828 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
2832 ad->u.net.sport = th->source;
2833 ad->u.net.dport = th->dest;
2838 struct udphdr _udph, *uh;
2840 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
2844 ad->u.net.sport = uh->source;
2845 ad->u.net.dport = uh->dest;
2849 /* includes fragments */
2859 static int selinux_parse_skb(struct sk_buff *skb, struct avc_audit_data *ad,
2860 char **addrp, int *len, int src)
2864 switch (ad->u.net.family) {
2866 ret = selinux_parse_skb_ipv4(skb, ad);
2870 *addrp = (char *)(src ? &ad->u.net.v4info.saddr :
2871 &ad->u.net.v4info.daddr);
2874 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2876 ret = selinux_parse_skb_ipv6(skb, ad);
2880 *addrp = (char *)(src ? &ad->u.net.v6info.saddr :
2881 &ad->u.net.v6info.daddr);
2891 /* socket security operations */
2892 static int socket_has_perm(struct task_struct *task, struct socket *sock,
2895 struct inode_security_struct *isec;
2896 struct task_security_struct *tsec;
2897 struct avc_audit_data ad;
2900 tsec = task->security;
2901 isec = SOCK_INODE(sock)->i_security;
2903 if (isec->sid == SECINITSID_KERNEL)
2906 AVC_AUDIT_DATA_INIT(&ad,NET);
2907 ad.u.net.sk = sock->sk;
2908 err = avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad);
2914 static int selinux_socket_create(int family, int type,
2915 int protocol, int kern)
2918 struct task_security_struct *tsec;
2923 tsec = current->security;
2924 err = avc_has_perm(tsec->sid, tsec->sid,
2925 socket_type_to_security_class(family, type,
2926 protocol), SOCKET__CREATE, NULL);
2932 static void selinux_socket_post_create(struct socket *sock, int family,
2933 int type, int protocol, int kern)
2935 struct inode_security_struct *isec;
2936 struct task_security_struct *tsec;
2938 isec = SOCK_INODE(sock)->i_security;
2940 tsec = current->security;
2941 isec->sclass = socket_type_to_security_class(family, type, protocol);
2942 isec->sid = kern ? SECINITSID_KERNEL : tsec->sid;
2943 isec->initialized = 1;
2948 /* Range of port numbers used to automatically bind.
2949 Need to determine whether we should perform a name_bind
2950 permission check between the socket and the port number. */
2951 #define ip_local_port_range_0 sysctl_local_port_range[0]
2952 #define ip_local_port_range_1 sysctl_local_port_range[1]
2954 static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
2959 err = socket_has_perm(current, sock, SOCKET__BIND);
2964 * If PF_INET or PF_INET6, check name_bind permission for the port.
2965 * Multiple address binding for SCTP is not supported yet: we just
2966 * check the first address now.
2968 family = sock->sk->sk_family;
2969 if (family == PF_INET || family == PF_INET6) {
2971 struct inode_security_struct *isec;
2972 struct task_security_struct *tsec;
2973 struct avc_audit_data ad;
2974 struct sockaddr_in *addr4 = NULL;
2975 struct sockaddr_in6 *addr6 = NULL;
2976 unsigned short snum;
2977 struct sock *sk = sock->sk;
2978 u32 sid, node_perm, addrlen;
2980 tsec = current->security;
2981 isec = SOCK_INODE(sock)->i_security;
2983 if (family == PF_INET) {
2984 addr4 = (struct sockaddr_in *)address;
2985 snum = ntohs(addr4->sin_port);
2986 addrlen = sizeof(addr4->sin_addr.s_addr);
2987 addrp = (char *)&addr4->sin_addr.s_addr;
2989 addr6 = (struct sockaddr_in6 *)address;
2990 snum = ntohs(addr6->sin6_port);
2991 addrlen = sizeof(addr6->sin6_addr.s6_addr);
2992 addrp = (char *)&addr6->sin6_addr.s6_addr;
2995 if (snum&&(snum < max(PROT_SOCK,ip_local_port_range_0) ||
2996 snum > ip_local_port_range_1)) {
2997 err = security_port_sid(sk->sk_family, sk->sk_type,
2998 sk->sk_protocol, snum, &sid);
3001 AVC_AUDIT_DATA_INIT(&ad,NET);
3002 ad.u.net.sport = htons(snum);
3003 ad.u.net.family = family;
3004 err = avc_has_perm(isec->sid, sid,
3006 SOCKET__NAME_BIND, &ad);
3011 switch(isec->sclass) {
3012 case SECCLASS_TCP_SOCKET:
3013 node_perm = TCP_SOCKET__NODE_BIND;
3016 case SECCLASS_UDP_SOCKET:
3017 node_perm = UDP_SOCKET__NODE_BIND;
3021 node_perm = RAWIP_SOCKET__NODE_BIND;
3025 err = security_node_sid(family, addrp, addrlen, &sid);
3029 AVC_AUDIT_DATA_INIT(&ad,NET);
3030 ad.u.net.sport = htons(snum);
3031 ad.u.net.family = family;
3033 if (family == PF_INET)
3034 ad.u.net.v4info.saddr = addr4->sin_addr.s_addr;
3036 ipv6_addr_copy(&ad.u.net.v6info.saddr, &addr6->sin6_addr);
3038 err = avc_has_perm(isec->sid, sid,
3039 isec->sclass, node_perm, &ad);
3047 static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
3049 struct inode_security_struct *isec;
3052 err = socket_has_perm(current, sock, SOCKET__CONNECT);
3057 * If a TCP socket, check name_connect permission for the port.
3059 isec = SOCK_INODE(sock)->i_security;
3060 if (isec->sclass == SECCLASS_TCP_SOCKET) {
3061 struct sock *sk = sock->sk;
3062 struct avc_audit_data ad;
3063 struct sockaddr_in *addr4 = NULL;
3064 struct sockaddr_in6 *addr6 = NULL;
3065 unsigned short snum;
3068 if (sk->sk_family == PF_INET) {
3069 addr4 = (struct sockaddr_in *)address;
3070 if (addrlen < sizeof(struct sockaddr_in))
3072 snum = ntohs(addr4->sin_port);
3074 addr6 = (struct sockaddr_in6 *)address;
3075 if (addrlen < SIN6_LEN_RFC2133)
3077 snum = ntohs(addr6->sin6_port);
3080 err = security_port_sid(sk->sk_family, sk->sk_type,
3081 sk->sk_protocol, snum, &sid);
3085 AVC_AUDIT_DATA_INIT(&ad,NET);
3086 ad.u.net.dport = htons(snum);
3087 ad.u.net.family = sk->sk_family;
3088 err = avc_has_perm(isec->sid, sid, isec->sclass,
3089 TCP_SOCKET__NAME_CONNECT, &ad);
3098 static int selinux_socket_listen(struct socket *sock, int backlog)
3100 return socket_has_perm(current, sock, SOCKET__LISTEN);
3103 static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
3106 struct inode_security_struct *isec;
3107 struct inode_security_struct *newisec;
3109 err = socket_has_perm(current, sock, SOCKET__ACCEPT);
3113 newisec = SOCK_INODE(newsock)->i_security;
3115 isec = SOCK_INODE(sock)->i_security;
3116 newisec->sclass = isec->sclass;
3117 newisec->sid = isec->sid;
3118 newisec->initialized = 1;
3123 static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
3126 return socket_has_perm(current, sock, SOCKET__WRITE);
3129 static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
3130 int size, int flags)
3132 return socket_has_perm(current, sock, SOCKET__READ);
3135 static int selinux_socket_getsockname(struct socket *sock)
3137 return socket_has_perm(current, sock, SOCKET__GETATTR);
3140 static int selinux_socket_getpeername(struct socket *sock)
3142 return socket_has_perm(current, sock, SOCKET__GETATTR);
3145 static int selinux_socket_setsockopt(struct socket *sock,int level,int optname)
3147 return socket_has_perm(current, sock, SOCKET__SETOPT);
3150 static int selinux_socket_getsockopt(struct socket *sock, int level,
3153 return socket_has_perm(current, sock, SOCKET__GETOPT);
3156 static int selinux_socket_shutdown(struct socket *sock, int how)
3158 return socket_has_perm(current, sock, SOCKET__SHUTDOWN);
3161 static int selinux_socket_unix_stream_connect(struct socket *sock,
3162 struct socket *other,
3165 struct sk_security_struct *ssec;
3166 struct inode_security_struct *isec;
3167 struct inode_security_struct *other_isec;
3168 struct avc_audit_data ad;
3171 err = secondary_ops->unix_stream_connect(sock, other, newsk);
3175 isec = SOCK_INODE(sock)->i_security;
3176 other_isec = SOCK_INODE(other)->i_security;
3178 AVC_AUDIT_DATA_INIT(&ad,NET);
3179 ad.u.net.sk = other->sk;
3181 err = avc_has_perm(isec->sid, other_isec->sid,
3183 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
3187 /* connecting socket */
3188 ssec = sock->sk->sk_security;
3189 ssec->peer_sid = other_isec->sid;
3191 /* server child socket */
3192 ssec = newsk->sk_security;
3193 ssec->peer_sid = isec->sid;
3198 static int selinux_socket_unix_may_send(struct socket *sock,
3199 struct socket *other)
3201 struct inode_security_struct *isec;
3202 struct inode_security_struct *other_isec;
3203 struct avc_audit_data ad;
3206 isec = SOCK_INODE(sock)->i_security;
3207 other_isec = SOCK_INODE(other)->i_security;
3209 AVC_AUDIT_DATA_INIT(&ad,NET);
3210 ad.u.net.sk = other->sk;
3212 err = avc_has_perm(isec->sid, other_isec->sid,
3213 isec->sclass, SOCKET__SENDTO, &ad);
3220 static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
3225 u32 netif_perm, node_perm, node_sid, if_sid, recv_perm = 0;
3228 struct socket *sock;
3229 struct net_device *dev;
3230 struct avc_audit_data ad;
3232 family = sk->sk_family;
3233 if (family != PF_INET && family != PF_INET6)
3236 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
3237 if (family == PF_INET6 && skb->protocol == ntohs(ETH_P_IP))
3240 read_lock_bh(&sk->sk_callback_lock);
3241 sock = sk->sk_socket;
3243 struct inode *inode;
3244 inode = SOCK_INODE(sock);
3246 struct inode_security_struct *isec;
3247 isec = inode->i_security;
3248 sock_sid = isec->sid;
3249 sock_class = isec->sclass;
3252 read_unlock_bh(&sk->sk_callback_lock);
3260 err = sel_netif_sids(dev, &if_sid, NULL);
3264 switch (sock_class) {
3265 case SECCLASS_UDP_SOCKET:
3266 netif_perm = NETIF__UDP_RECV;
3267 node_perm = NODE__UDP_RECV;
3268 recv_perm = UDP_SOCKET__RECV_MSG;
3271 case SECCLASS_TCP_SOCKET:
3272 netif_perm = NETIF__TCP_RECV;
3273 node_perm = NODE__TCP_RECV;
3274 recv_perm = TCP_SOCKET__RECV_MSG;
3278 netif_perm = NETIF__RAWIP_RECV;
3279 node_perm = NODE__RAWIP_RECV;
3283 AVC_AUDIT_DATA_INIT(&ad, NET);
3284 ad.u.net.netif = dev->name;
3285 ad.u.net.family = family;
3287 err = selinux_parse_skb(skb, &ad, &addrp, &len, 1);
3291 err = avc_has_perm(sock_sid, if_sid, SECCLASS_NETIF, netif_perm, &ad);
3295 /* Fixme: this lookup is inefficient */
3296 err = security_node_sid(family, addrp, len, &node_sid);
3300 err = avc_has_perm(sock_sid, node_sid, SECCLASS_NODE, node_perm, &ad);
3307 /* Fixme: make this more efficient */
3308 err = security_port_sid(sk->sk_family, sk->sk_type,
3309 sk->sk_protocol, ntohs(ad.u.net.sport),
3314 err = avc_has_perm(sock_sid, port_sid,
3315 sock_class, recv_perm, &ad);
3319 err = selinux_xfrm_sock_rcv_skb(sock_sid, skb);
3325 static int selinux_socket_getpeersec(struct socket *sock, char __user *optval,
3326 int __user *optlen, unsigned len)
3331 struct sk_security_struct *ssec;
3332 struct inode_security_struct *isec;
3334 isec = SOCK_INODE(sock)->i_security;
3335 if (isec->sclass != SECCLASS_UNIX_STREAM_SOCKET) {
3340 ssec = sock->sk->sk_security;
3342 err = security_sid_to_context(ssec->peer_sid, &scontext, &scontext_len);
3346 if (scontext_len > len) {
3351 if (copy_to_user(optval, scontext, scontext_len))
3355 if (put_user(scontext_len, optlen))
3363 static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
3365 return sk_alloc_security(sk, family, priority);
3368 static void selinux_sk_free_security(struct sock *sk)
3370 sk_free_security(sk);
3373 static unsigned int selinux_sk_getsid_security(struct sock *sk, struct flowi *fl, u8 dir)
3375 struct inode_security_struct *isec;
3376 u32 sock_sid = SECINITSID_ANY_SOCKET;
3379 return selinux_no_sk_sid(fl);
3381 read_lock_bh(&sk->sk_callback_lock);
3382 isec = get_sock_isec(sk);
3385 sock_sid = isec->sid;
3387 read_unlock_bh(&sk->sk_callback_lock);
3391 static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
3395 struct nlmsghdr *nlh;
3396 struct socket *sock = sk->sk_socket;
3397 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
3399 if (skb->len < NLMSG_SPACE(0)) {
3403 nlh = (struct nlmsghdr *)skb->data;
3405 err = selinux_nlmsg_lookup(isec->sclass, nlh->nlmsg_type, &perm);
3407 if (err == -EINVAL) {
3408 audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
3409 "SELinux: unrecognized netlink message"
3410 " type=%hu for sclass=%hu\n",
3411 nlh->nlmsg_type, isec->sclass);
3412 if (!selinux_enforcing)
3422 err = socket_has_perm(current, sock, perm);
3427 #ifdef CONFIG_NETFILTER
3429 static unsigned int selinux_ip_postroute_last(unsigned int hooknum,
3430 struct sk_buff **pskb,
3431 const struct net_device *in,
3432 const struct net_device *out,
3433 int (*okfn)(struct sk_buff *),
3437 int len, err = NF_ACCEPT;
3438 u32 netif_perm, node_perm, node_sid, if_sid, send_perm = 0;
3440 struct socket *sock;
3441 struct inode *inode;
3442 struct sk_buff *skb = *pskb;
3443 struct inode_security_struct *isec;
3444 struct avc_audit_data ad;
3445 struct net_device *dev = (struct net_device *)out;
3451 sock = sk->sk_socket;
3455 inode = SOCK_INODE(sock);
3459 err = sel_netif_sids(dev, &if_sid, NULL);
3463 isec = inode->i_security;
3465 switch (isec->sclass) {
3466 case SECCLASS_UDP_SOCKET:
3467 netif_perm = NETIF__UDP_SEND;
3468 node_perm = NODE__UDP_SEND;
3469 send_perm = UDP_SOCKET__SEND_MSG;
3472 case SECCLASS_TCP_SOCKET:
3473 netif_perm = NETIF__TCP_SEND;
3474 node_perm = NODE__TCP_SEND;
3475 send_perm = TCP_SOCKET__SEND_MSG;
3479 netif_perm = NETIF__RAWIP_SEND;
3480 node_perm = NODE__RAWIP_SEND;
3485 AVC_AUDIT_DATA_INIT(&ad, NET);
3486 ad.u.net.netif = dev->name;
3487 ad.u.net.family = family;
3489 err = selinux_parse_skb(skb, &ad, &addrp,
3490 &len, 0) ? NF_DROP : NF_ACCEPT;
3491 if (err != NF_ACCEPT)
3494 err = avc_has_perm(isec->sid, if_sid, SECCLASS_NETIF,
3495 netif_perm, &ad) ? NF_DROP : NF_ACCEPT;
3496 if (err != NF_ACCEPT)
3499 /* Fixme: this lookup is inefficient */
3500 err = security_node_sid(family, addrp, len,
3501 &node_sid) ? NF_DROP : NF_ACCEPT;
3502 if (err != NF_ACCEPT)
3505 err = avc_has_perm(isec->sid, node_sid, SECCLASS_NODE,
3506 node_perm, &ad) ? NF_DROP : NF_ACCEPT;
3507 if (err != NF_ACCEPT)
3513 /* Fixme: make this more efficient */
3514 err = security_port_sid(sk->sk_family,
3517 ntohs(ad.u.net.dport),
3518 &port_sid) ? NF_DROP : NF_ACCEPT;
3519 if (err != NF_ACCEPT)
3522 err = avc_has_perm(isec->sid, port_sid, isec->sclass,
3523 send_perm, &ad) ? NF_DROP : NF_ACCEPT;
3526 if (err != NF_ACCEPT)
3529 err = selinux_xfrm_postroute_last(isec->sid, skb);
3535 static unsigned int selinux_ipv4_postroute_last(unsigned int hooknum,
3536 struct sk_buff **pskb,
3537 const struct net_device *in,
3538 const struct net_device *out,
3539 int (*okfn)(struct sk_buff *))
3541 return selinux_ip_postroute_last(hooknum, pskb, in, out, okfn, PF_INET);
3544 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3546 static unsigned int selinux_ipv6_postroute_last(unsigned int hooknum,
3547 struct sk_buff **pskb,
3548 const struct net_device *in,
3549 const struct net_device *out,
3550 int (*okfn)(struct sk_buff *))
3552 return selinux_ip_postroute_last(hooknum, pskb, in, out, okfn, PF_INET6);
3557 #endif /* CONFIG_NETFILTER */
3561 static inline int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
3566 #endif /* CONFIG_SECURITY_NETWORK */
3568 static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
3570 struct task_security_struct *tsec;
3571 struct av_decision avd;
3574 err = secondary_ops->netlink_send(sk, skb);
3578 tsec = current->security;
3581 avc_has_perm_noaudit(tsec->sid, tsec->sid,
3582 SECCLASS_CAPABILITY, ~0, &avd);
3583 cap_mask(NETLINK_CB(skb).eff_cap, avd.allowed);
3585 if (policydb_loaded_version >= POLICYDB_VERSION_NLCLASS)
3586 err = selinux_nlmsg_perm(sk, skb);
3591 static int selinux_netlink_recv(struct sk_buff *skb)
3593 if (!cap_raised(NETLINK_CB(skb).eff_cap, CAP_NET_ADMIN))
3598 static int ipc_alloc_security(struct task_struct *task,
3599 struct kern_ipc_perm *perm,
3602 struct task_security_struct *tsec = task->security;
3603 struct ipc_security_struct *isec;
3605 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
3609 isec->sclass = sclass;
3610 isec->ipc_perm = perm;
3611 isec->sid = tsec->sid;
3612 perm->security = isec;
3617 static void ipc_free_security(struct kern_ipc_perm *perm)
3619 struct ipc_security_struct *isec = perm->security;
3620 perm->security = NULL;
3624 static int msg_msg_alloc_security(struct msg_msg *msg)
3626 struct msg_security_struct *msec;
3628 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
3633 msec->sid = SECINITSID_UNLABELED;
3634 msg->security = msec;
3639 static void msg_msg_free_security(struct msg_msg *msg)
3641 struct msg_security_struct *msec = msg->security;
3643 msg->security = NULL;
3647 static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
3650 struct task_security_struct *tsec;
3651 struct ipc_security_struct *isec;
3652 struct avc_audit_data ad;
3654 tsec = current->security;
3655 isec = ipc_perms->security;
3657 AVC_AUDIT_DATA_INIT(&ad, IPC);
3658 ad.u.ipc_id = ipc_perms->key;
3660 return avc_has_perm(tsec->sid, isec->sid, isec->sclass, perms, &ad);
3663 static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
3665 return msg_msg_alloc_security(msg);
3668 static void selinux_msg_msg_free_security(struct msg_msg *msg)
3670 msg_msg_free_security(msg);
3673 /* message queue security operations */
3674 static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
3676 struct task_security_struct *tsec;
3677 struct ipc_security_struct *isec;
3678 struct avc_audit_data ad;
3681 rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
3685 tsec = current->security;
3686 isec = msq->q_perm.security;
3688 AVC_AUDIT_DATA_INIT(&ad, IPC);
3689 ad.u.ipc_id = msq->q_perm.key;
3691 rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ,
3694 ipc_free_security(&msq->q_perm);
3700 static void selinux_msg_queue_free_security(struct msg_queue *msq)
3702 ipc_free_security(&msq->q_perm);
3705 static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
3707 struct task_security_struct *tsec;
3708 struct ipc_security_struct *isec;
3709 struct avc_audit_data ad;
3711 tsec = current->security;
3712 isec = msq->q_perm.security;
3714 AVC_AUDIT_DATA_INIT(&ad, IPC);
3715 ad.u.ipc_id = msq->q_perm.key;
3717 return avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ,
3718 MSGQ__ASSOCIATE, &ad);
3721 static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
3729 /* No specific object, just general system-wide information. */
3730 return task_has_system(current, SYSTEM__IPC_INFO);
3733 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
3736 perms = MSGQ__SETATTR;
3739 perms = MSGQ__DESTROY;
3745 err = ipc_has_perm(&msq->q_perm, perms);
3749 static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
3751 struct task_security_struct *tsec;
3752 struct ipc_security_struct *isec;
3753 struct msg_security_struct *msec;
3754 struct avc_audit_data ad;
3757 tsec = current->security;
3758 isec = msq->q_perm.security;
3759 msec = msg->security;
3762 * First time through, need to assign label to the message
3764 if (msec->sid == SECINITSID_UNLABELED) {
3766 * Compute new sid based on current process and
3767 * message queue this message will be stored in
3769 rc = security_transition_sid(tsec->sid,
3777 AVC_AUDIT_DATA_INIT(&ad, IPC);
3778 ad.u.ipc_id = msq->q_perm.key;
3780 /* Can this process write to the queue? */
3781 rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_MSGQ,
3784 /* Can this process send the message */
3785 rc = avc_has_perm(tsec->sid, msec->sid,
3786 SECCLASS_MSG, MSG__SEND, &ad);
3788 /* Can the message be put in the queue? */
3789 rc = avc_has_perm(msec->sid, isec->sid,
3790 SECCLASS_MSGQ, MSGQ__ENQUEUE, &ad);
3795 static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
3796 struct task_struct *target,
3797 long type, int mode)
3799 struct task_security_struct *tsec;
3800 struct ipc_security_struct *isec;
3801 struct msg_security_struct *msec;
3802 struct avc_audit_data ad;
3805 tsec = target->security;
3806 isec = msq->q_perm.security;
3807 msec = msg->security;
3809 AVC_AUDIT_DATA_INIT(&ad, IPC);
3810 ad.u.ipc_id = msq->q_perm.key;
3812 rc = avc_has_perm(tsec->sid, isec->sid,
3813 SECCLASS_MSGQ, MSGQ__READ, &ad);
3815 rc = avc_has_perm(tsec->sid, msec->sid,
3816 SECCLASS_MSG, MSG__RECEIVE, &ad);
3820 /* Shared Memory security operations */
3821 static int selinux_shm_alloc_security(struct shmid_kernel *shp)
3823 struct task_security_struct *tsec;
3824 struct ipc_security_struct *isec;
3825 struct avc_audit_data ad;
3828 rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
3832 tsec = current->security;
3833 isec = shp->shm_perm.security;
3835 AVC_AUDIT_DATA_INIT(&ad, IPC);
3836 ad.u.ipc_id = shp->shm_perm.key;
3838 rc = avc_has_perm(tsec->sid, isec->sid, SECCLASS_SHM,
3841 ipc_free_security(&shp->shm_perm);
3847 static void selinux_shm_free_security(struct shmid_kernel *shp)
3849 ipc_free_security(&shp->shm_perm);
3852 static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
3854 struct task_security_struct *tsec;
3855 struct ipc_security_struct *isec;
3856 struct avc_audit_data ad;
3858 tsec = current->security;
3859 isec = shp->shm_perm.security;
3861 AVC_AUDIT_DATA_INIT(&ad, IPC);