2 * NSA Security-Enhanced Linux (SELinux) security module
4 * This file contains the SELinux hook function implementations.
6 * Authors: Stephen Smalley, <sds@epoch.ncsc.mil>
7 * Chris Vance, <cvance@nai.com>
8 * Wayne Salamon, <wsalamon@nai.com>
9 * James Morris <jmorris@redhat.com>
11 * Copyright (C) 2001,2002 Networks Associates Technology, Inc.
12 * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
13 * Eric Paris <eparis@redhat.com>
14 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
15 * <dgoeddel@trustedcs.com>
16 * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
17 * Paul Moore <paul@paul-moore.com>
18 * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
19 * Yuichi Nakamura <ynakam@hitachisoft.jp>
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License version 2,
23 * as published by the Free Software Foundation.
26 #include <linux/init.h>
28 #include <linux/kernel.h>
29 #include <linux/tracehook.h>
30 #include <linux/errno.h>
31 #include <linux/sched.h>
32 #include <linux/security.h>
33 #include <linux/xattr.h>
34 #include <linux/capability.h>
35 #include <linux/unistd.h>
37 #include <linux/mman.h>
38 #include <linux/slab.h>
39 #include <linux/pagemap.h>
40 #include <linux/proc_fs.h>
41 #include <linux/swap.h>
42 #include <linux/spinlock.h>
43 #include <linux/syscalls.h>
44 #include <linux/dcache.h>
45 #include <linux/file.h>
46 #include <linux/fdtable.h>
47 #include <linux/namei.h>
48 #include <linux/mount.h>
49 #include <linux/netfilter_ipv4.h>
50 #include <linux/netfilter_ipv6.h>
51 #include <linux/tty.h>
53 #include <net/ip.h> /* for local_port_range[] */
55 #include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
56 #include <net/inet_connection_sock.h>
57 #include <net/net_namespace.h>
58 #include <net/netlabel.h>
59 #include <linux/uaccess.h>
60 #include <asm/ioctls.h>
61 #include <linux/atomic.h>
62 #include <linux/bitops.h>
63 #include <linux/interrupt.h>
64 #include <linux/netdevice.h> /* for network interface checks */
65 #include <net/netlink.h>
66 #include <linux/tcp.h>
67 #include <linux/udp.h>
68 #include <linux/dccp.h>
69 #include <linux/quota.h>
70 #include <linux/un.h> /* for Unix socket types */
71 #include <net/af_unix.h> /* for Unix socket types */
72 #include <linux/parser.h>
73 #include <linux/nfs_mount.h>
75 #include <linux/hugetlb.h>
76 #include <linux/personality.h>
77 #include <linux/audit.h>
78 #include <linux/string.h>
79 #include <linux/selinux.h>
80 #include <linux/mutex.h>
81 #include <linux/posix-timers.h>
82 #include <linux/syslog.h>
83 #include <linux/user_namespace.h>
84 #include <linux/export.h>
85 #include <linux/msg.h>
86 #include <linux/shm.h>
98 #define NUM_SEL_MNT_OPTS 5
100 extern struct security_operations
*security_ops
;
102 /* SECMARK reference count */
103 static atomic_t selinux_secmark_refcount
= ATOMIC_INIT(0);
105 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
106 int selinux_enforcing
;
108 static int __init
enforcing_setup(char *str
)
110 unsigned long enforcing
;
111 if (!strict_strtoul(str
, 0, &enforcing
))
112 selinux_enforcing
= enforcing
? 1 : 0;
115 __setup("enforcing=", enforcing_setup
);
118 #ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
119 int selinux_enabled
= CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE
;
121 static int __init
selinux_enabled_setup(char *str
)
123 unsigned long enabled
;
124 if (!strict_strtoul(str
, 0, &enabled
))
125 selinux_enabled
= enabled
? 1 : 0;
128 __setup("selinux=", selinux_enabled_setup
);
130 int selinux_enabled
= 1;
133 static struct kmem_cache
*sel_inode_cache
;
136 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
139 * This function checks the SECMARK reference counter to see if any SECMARK
140 * targets are currently configured, if the reference counter is greater than
141 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
142 * enabled, false (0) if SECMARK is disabled.
145 static int selinux_secmark_enabled(void)
147 return (atomic_read(&selinux_secmark_refcount
) > 0);
151 * initialise the security for the init task
153 static void cred_init_security(void)
155 struct cred
*cred
= (struct cred
*) current
->real_cred
;
156 struct task_security_struct
*tsec
;
158 tsec
= kzalloc(sizeof(struct task_security_struct
), GFP_KERNEL
);
160 panic("SELinux: Failed to initialize initial task.\n");
162 tsec
->osid
= tsec
->sid
= SECINITSID_KERNEL
;
163 cred
->security
= tsec
;
167 * get the security ID of a set of credentials
169 static inline u32
cred_sid(const struct cred
*cred
)
171 const struct task_security_struct
*tsec
;
173 tsec
= cred
->security
;
178 * get the objective security ID of a task
180 static inline u32
task_sid(const struct task_struct
*task
)
185 sid
= cred_sid(__task_cred(task
));
191 * get the subjective security ID of the current task
193 static inline u32
current_sid(void)
195 const struct task_security_struct
*tsec
= current_security();
200 /* Allocate and free functions for each kind of security blob. */
202 static int inode_alloc_security(struct inode
*inode
)
204 struct inode_security_struct
*isec
;
205 u32 sid
= current_sid();
207 isec
= kmem_cache_zalloc(sel_inode_cache
, GFP_NOFS
);
211 mutex_init(&isec
->lock
);
212 INIT_LIST_HEAD(&isec
->list
);
214 isec
->sid
= SECINITSID_UNLABELED
;
215 isec
->sclass
= SECCLASS_FILE
;
216 isec
->task_sid
= sid
;
217 inode
->i_security
= isec
;
222 static void inode_free_rcu(struct rcu_head
*head
)
224 struct inode_security_struct
*isec
;
226 isec
= container_of(head
, struct inode_security_struct
, rcu
);
227 kmem_cache_free(sel_inode_cache
, isec
);
230 static void inode_free_security(struct inode
*inode
)
232 struct inode_security_struct
*isec
= inode
->i_security
;
233 struct superblock_security_struct
*sbsec
= inode
->i_sb
->s_security
;
235 spin_lock(&sbsec
->isec_lock
);
236 if (!list_empty(&isec
->list
))
237 list_del_init(&isec
->list
);
238 spin_unlock(&sbsec
->isec_lock
);
241 * The inode may still be referenced in a path walk and
242 * a call to selinux_inode_permission() can be made
243 * after inode_free_security() is called. Ideally, the VFS
244 * wouldn't do this, but fixing that is a much harder
245 * job. For now, simply free the i_security via RCU, and
246 * leave the current inode->i_security pointer intact.
247 * The inode will be freed after the RCU grace period too.
249 call_rcu(&isec
->rcu
, inode_free_rcu
);
252 static int file_alloc_security(struct file
*file
)
254 struct file_security_struct
*fsec
;
255 u32 sid
= current_sid();
257 fsec
= kzalloc(sizeof(struct file_security_struct
), GFP_KERNEL
);
262 fsec
->fown_sid
= sid
;
263 file
->f_security
= fsec
;
268 static void file_free_security(struct file
*file
)
270 struct file_security_struct
*fsec
= file
->f_security
;
271 file
->f_security
= NULL
;
275 static int superblock_alloc_security(struct super_block
*sb
)
277 struct superblock_security_struct
*sbsec
;
279 sbsec
= kzalloc(sizeof(struct superblock_security_struct
), GFP_KERNEL
);
283 mutex_init(&sbsec
->lock
);
284 INIT_LIST_HEAD(&sbsec
->isec_head
);
285 spin_lock_init(&sbsec
->isec_lock
);
287 sbsec
->sid
= SECINITSID_UNLABELED
;
288 sbsec
->def_sid
= SECINITSID_FILE
;
289 sbsec
->mntpoint_sid
= SECINITSID_UNLABELED
;
290 sb
->s_security
= sbsec
;
295 static void superblock_free_security(struct super_block
*sb
)
297 struct superblock_security_struct
*sbsec
= sb
->s_security
;
298 sb
->s_security
= NULL
;
302 /* The file system's label must be initialized prior to use. */
304 static const char *labeling_behaviors
[6] = {
306 "uses transition SIDs",
308 "uses genfs_contexts",
309 "not configured for labeling",
310 "uses mountpoint labeling",
313 static int inode_doinit_with_dentry(struct inode
*inode
, struct dentry
*opt_dentry
);
315 static inline int inode_doinit(struct inode
*inode
)
317 return inode_doinit_with_dentry(inode
, NULL
);
326 Opt_labelsupport
= 5,
329 static const match_table_t tokens
= {
330 {Opt_context
, CONTEXT_STR
"%s"},
331 {Opt_fscontext
, FSCONTEXT_STR
"%s"},
332 {Opt_defcontext
, DEFCONTEXT_STR
"%s"},
333 {Opt_rootcontext
, ROOTCONTEXT_STR
"%s"},
334 {Opt_labelsupport
, LABELSUPP_STR
},
338 #define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
340 static int may_context_mount_sb_relabel(u32 sid
,
341 struct superblock_security_struct
*sbsec
,
342 const struct cred
*cred
)
344 const struct task_security_struct
*tsec
= cred
->security
;
347 rc
= avc_has_perm(tsec
->sid
, sbsec
->sid
, SECCLASS_FILESYSTEM
,
348 FILESYSTEM__RELABELFROM
, NULL
);
352 rc
= avc_has_perm(tsec
->sid
, sid
, SECCLASS_FILESYSTEM
,
353 FILESYSTEM__RELABELTO
, NULL
);
357 static int may_context_mount_inode_relabel(u32 sid
,
358 struct superblock_security_struct
*sbsec
,
359 const struct cred
*cred
)
361 const struct task_security_struct
*tsec
= cred
->security
;
363 rc
= avc_has_perm(tsec
->sid
, sbsec
->sid
, SECCLASS_FILESYSTEM
,
364 FILESYSTEM__RELABELFROM
, NULL
);
368 rc
= avc_has_perm(sid
, sbsec
->sid
, SECCLASS_FILESYSTEM
,
369 FILESYSTEM__ASSOCIATE
, NULL
);
373 static int sb_finish_set_opts(struct super_block
*sb
)
375 struct superblock_security_struct
*sbsec
= sb
->s_security
;
376 struct dentry
*root
= sb
->s_root
;
377 struct inode
*root_inode
= root
->d_inode
;
380 if (sbsec
->behavior
== SECURITY_FS_USE_XATTR
) {
381 /* Make sure that the xattr handler exists and that no
382 error other than -ENODATA is returned by getxattr on
383 the root directory. -ENODATA is ok, as this may be
384 the first boot of the SELinux kernel before we have
385 assigned xattr values to the filesystem. */
386 if (!root_inode
->i_op
->getxattr
) {
387 printk(KERN_WARNING
"SELinux: (dev %s, type %s) has no "
388 "xattr support\n", sb
->s_id
, sb
->s_type
->name
);
392 rc
= root_inode
->i_op
->getxattr(root
, XATTR_NAME_SELINUX
, NULL
, 0);
393 if (rc
< 0 && rc
!= -ENODATA
) {
394 if (rc
== -EOPNOTSUPP
)
395 printk(KERN_WARNING
"SELinux: (dev %s, type "
396 "%s) has no security xattr handler\n",
397 sb
->s_id
, sb
->s_type
->name
);
399 printk(KERN_WARNING
"SELinux: (dev %s, type "
400 "%s) getxattr errno %d\n", sb
->s_id
,
401 sb
->s_type
->name
, -rc
);
406 sbsec
->flags
|= (SE_SBINITIALIZED
| SE_SBLABELSUPP
);
408 if (sbsec
->behavior
> ARRAY_SIZE(labeling_behaviors
))
409 printk(KERN_ERR
"SELinux: initialized (dev %s, type %s), unknown behavior\n",
410 sb
->s_id
, sb
->s_type
->name
);
412 printk(KERN_DEBUG
"SELinux: initialized (dev %s, type %s), %s\n",
413 sb
->s_id
, sb
->s_type
->name
,
414 labeling_behaviors
[sbsec
->behavior
-1]);
416 if (sbsec
->behavior
== SECURITY_FS_USE_GENFS
||
417 sbsec
->behavior
== SECURITY_FS_USE_MNTPOINT
||
418 sbsec
->behavior
== SECURITY_FS_USE_NONE
||
419 sbsec
->behavior
> ARRAY_SIZE(labeling_behaviors
))
420 sbsec
->flags
&= ~SE_SBLABELSUPP
;
422 /* Special handling for sysfs. Is genfs but also has setxattr handler*/
423 if (strncmp(sb
->s_type
->name
, "sysfs", sizeof("sysfs")) == 0)
424 sbsec
->flags
|= SE_SBLABELSUPP
;
427 * Special handling for rootfs. Is genfs but supports
428 * setting SELinux context on in-core inodes.
430 if (strncmp(sb
->s_type
->name
, "rootfs", sizeof("rootfs")) == 0)
431 sbsec
->flags
|= SE_SBLABELSUPP
;
433 /* Initialize the root inode. */
434 rc
= inode_doinit_with_dentry(root_inode
, root
);
436 /* Initialize any other inodes associated with the superblock, e.g.
437 inodes created prior to initial policy load or inodes created
438 during get_sb by a pseudo filesystem that directly
440 spin_lock(&sbsec
->isec_lock
);
442 if (!list_empty(&sbsec
->isec_head
)) {
443 struct inode_security_struct
*isec
=
444 list_entry(sbsec
->isec_head
.next
,
445 struct inode_security_struct
, list
);
446 struct inode
*inode
= isec
->inode
;
447 spin_unlock(&sbsec
->isec_lock
);
448 inode
= igrab(inode
);
450 if (!IS_PRIVATE(inode
))
454 spin_lock(&sbsec
->isec_lock
);
455 list_del_init(&isec
->list
);
458 spin_unlock(&sbsec
->isec_lock
);
464 * This function should allow an FS to ask what it's mount security
465 * options were so it can use those later for submounts, displaying
466 * mount options, or whatever.
468 static int selinux_get_mnt_opts(const struct super_block
*sb
,
469 struct security_mnt_opts
*opts
)
472 struct superblock_security_struct
*sbsec
= sb
->s_security
;
473 char *context
= NULL
;
477 security_init_mnt_opts(opts
);
479 if (!(sbsec
->flags
& SE_SBINITIALIZED
))
485 tmp
= sbsec
->flags
& SE_MNTMASK
;
486 /* count the number of mount options for this sb */
487 for (i
= 0; i
< 8; i
++) {
489 opts
->num_mnt_opts
++;
492 /* Check if the Label support flag is set */
493 if (sbsec
->flags
& SE_SBLABELSUPP
)
494 opts
->num_mnt_opts
++;
496 opts
->mnt_opts
= kcalloc(opts
->num_mnt_opts
, sizeof(char *), GFP_ATOMIC
);
497 if (!opts
->mnt_opts
) {
502 opts
->mnt_opts_flags
= kcalloc(opts
->num_mnt_opts
, sizeof(int), GFP_ATOMIC
);
503 if (!opts
->mnt_opts_flags
) {
509 if (sbsec
->flags
& FSCONTEXT_MNT
) {
510 rc
= security_sid_to_context(sbsec
->sid
, &context
, &len
);
513 opts
->mnt_opts
[i
] = context
;
514 opts
->mnt_opts_flags
[i
++] = FSCONTEXT_MNT
;
516 if (sbsec
->flags
& CONTEXT_MNT
) {
517 rc
= security_sid_to_context(sbsec
->mntpoint_sid
, &context
, &len
);
520 opts
->mnt_opts
[i
] = context
;
521 opts
->mnt_opts_flags
[i
++] = CONTEXT_MNT
;
523 if (sbsec
->flags
& DEFCONTEXT_MNT
) {
524 rc
= security_sid_to_context(sbsec
->def_sid
, &context
, &len
);
527 opts
->mnt_opts
[i
] = context
;
528 opts
->mnt_opts_flags
[i
++] = DEFCONTEXT_MNT
;
530 if (sbsec
->flags
& ROOTCONTEXT_MNT
) {
531 struct inode
*root
= sbsec
->sb
->s_root
->d_inode
;
532 struct inode_security_struct
*isec
= root
->i_security
;
534 rc
= security_sid_to_context(isec
->sid
, &context
, &len
);
537 opts
->mnt_opts
[i
] = context
;
538 opts
->mnt_opts_flags
[i
++] = ROOTCONTEXT_MNT
;
540 if (sbsec
->flags
& SE_SBLABELSUPP
) {
541 opts
->mnt_opts
[i
] = NULL
;
542 opts
->mnt_opts_flags
[i
++] = SE_SBLABELSUPP
;
545 BUG_ON(i
!= opts
->num_mnt_opts
);
550 security_free_mnt_opts(opts
);
554 static int bad_option(struct superblock_security_struct
*sbsec
, char flag
,
555 u32 old_sid
, u32 new_sid
)
557 char mnt_flags
= sbsec
->flags
& SE_MNTMASK
;
559 /* check if the old mount command had the same options */
560 if (sbsec
->flags
& SE_SBINITIALIZED
)
561 if (!(sbsec
->flags
& flag
) ||
562 (old_sid
!= new_sid
))
565 /* check if we were passed the same options twice,
566 * aka someone passed context=a,context=b
568 if (!(sbsec
->flags
& SE_SBINITIALIZED
))
569 if (mnt_flags
& flag
)
575 * Allow filesystems with binary mount data to explicitly set mount point
576 * labeling information.
578 static int selinux_set_mnt_opts(struct super_block
*sb
,
579 struct security_mnt_opts
*opts
)
581 const struct cred
*cred
= current_cred();
583 struct superblock_security_struct
*sbsec
= sb
->s_security
;
584 const char *name
= sb
->s_type
->name
;
585 struct inode
*inode
= sbsec
->sb
->s_root
->d_inode
;
586 struct inode_security_struct
*root_isec
= inode
->i_security
;
587 u32 fscontext_sid
= 0, context_sid
= 0, rootcontext_sid
= 0;
588 u32 defcontext_sid
= 0;
589 char **mount_options
= opts
->mnt_opts
;
590 int *flags
= opts
->mnt_opts_flags
;
591 int num_opts
= opts
->num_mnt_opts
;
593 mutex_lock(&sbsec
->lock
);
595 if (!ss_initialized
) {
597 /* Defer initialization until selinux_complete_init,
598 after the initial policy is loaded and the security
599 server is ready to handle calls. */
603 printk(KERN_WARNING
"SELinux: Unable to set superblock options "
604 "before the security server is initialized\n");
609 * Binary mount data FS will come through this function twice. Once
610 * from an explicit call and once from the generic calls from the vfs.
611 * Since the generic VFS calls will not contain any security mount data
612 * we need to skip the double mount verification.
614 * This does open a hole in which we will not notice if the first
615 * mount using this sb set explict options and a second mount using
616 * this sb does not set any security options. (The first options
617 * will be used for both mounts)
619 if ((sbsec
->flags
& SE_SBINITIALIZED
) && (sb
->s_type
->fs_flags
& FS_BINARY_MOUNTDATA
)
624 * parse the mount options, check if they are valid sids.
625 * also check if someone is trying to mount the same sb more
626 * than once with different security options.
628 for (i
= 0; i
< num_opts
; i
++) {
631 if (flags
[i
] == SE_SBLABELSUPP
)
633 rc
= security_context_to_sid(mount_options
[i
],
634 strlen(mount_options
[i
]), &sid
);
636 printk(KERN_WARNING
"SELinux: security_context_to_sid"
637 "(%s) failed for (dev %s, type %s) errno=%d\n",
638 mount_options
[i
], sb
->s_id
, name
, rc
);
645 if (bad_option(sbsec
, FSCONTEXT_MNT
, sbsec
->sid
,
647 goto out_double_mount
;
649 sbsec
->flags
|= FSCONTEXT_MNT
;
654 if (bad_option(sbsec
, CONTEXT_MNT
, sbsec
->mntpoint_sid
,
656 goto out_double_mount
;
658 sbsec
->flags
|= CONTEXT_MNT
;
660 case ROOTCONTEXT_MNT
:
661 rootcontext_sid
= sid
;
663 if (bad_option(sbsec
, ROOTCONTEXT_MNT
, root_isec
->sid
,
665 goto out_double_mount
;
667 sbsec
->flags
|= ROOTCONTEXT_MNT
;
671 defcontext_sid
= sid
;
673 if (bad_option(sbsec
, DEFCONTEXT_MNT
, sbsec
->def_sid
,
675 goto out_double_mount
;
677 sbsec
->flags
|= DEFCONTEXT_MNT
;
686 if (sbsec
->flags
& SE_SBINITIALIZED
) {
687 /* previously mounted with options, but not on this attempt? */
688 if ((sbsec
->flags
& SE_MNTMASK
) && !num_opts
)
689 goto out_double_mount
;
694 if (strcmp(sb
->s_type
->name
, "proc") == 0)
695 sbsec
->flags
|= SE_SBPROC
;
697 /* Determine the labeling behavior to use for this filesystem type. */
698 rc
= security_fs_use((sbsec
->flags
& SE_SBPROC
) ? "proc" : sb
->s_type
->name
, &sbsec
->behavior
, &sbsec
->sid
);
700 printk(KERN_WARNING
"%s: security_fs_use(%s) returned %d\n",
701 __func__
, sb
->s_type
->name
, rc
);
705 /* sets the context of the superblock for the fs being mounted. */
707 rc
= may_context_mount_sb_relabel(fscontext_sid
, sbsec
, cred
);
711 sbsec
->sid
= fscontext_sid
;
715 * Switch to using mount point labeling behavior.
716 * sets the label used on all file below the mountpoint, and will set
717 * the superblock context if not already set.
720 if (!fscontext_sid
) {
721 rc
= may_context_mount_sb_relabel(context_sid
, sbsec
,
725 sbsec
->sid
= context_sid
;
727 rc
= may_context_mount_inode_relabel(context_sid
, sbsec
,
732 if (!rootcontext_sid
)
733 rootcontext_sid
= context_sid
;
735 sbsec
->mntpoint_sid
= context_sid
;
736 sbsec
->behavior
= SECURITY_FS_USE_MNTPOINT
;
739 if (rootcontext_sid
) {
740 rc
= may_context_mount_inode_relabel(rootcontext_sid
, sbsec
,
745 root_isec
->sid
= rootcontext_sid
;
746 root_isec
->initialized
= 1;
749 if (defcontext_sid
) {
750 if (sbsec
->behavior
!= SECURITY_FS_USE_XATTR
) {
752 printk(KERN_WARNING
"SELinux: defcontext option is "
753 "invalid for this filesystem type\n");
757 if (defcontext_sid
!= sbsec
->def_sid
) {
758 rc
= may_context_mount_inode_relabel(defcontext_sid
,
764 sbsec
->def_sid
= defcontext_sid
;
767 rc
= sb_finish_set_opts(sb
);
769 mutex_unlock(&sbsec
->lock
);
773 printk(KERN_WARNING
"SELinux: mount invalid. Same superblock, different "
774 "security settings for (dev %s, type %s)\n", sb
->s_id
, name
);
778 static int selinux_cmp_sb_context(const struct super_block
*oldsb
,
779 const struct super_block
*newsb
)
781 struct superblock_security_struct
*old
= oldsb
->s_security
;
782 struct superblock_security_struct
*new = newsb
->s_security
;
783 char oldflags
= old
->flags
& SE_MNTMASK
;
784 char newflags
= new->flags
& SE_MNTMASK
;
786 if (oldflags
!= newflags
)
788 if ((oldflags
& FSCONTEXT_MNT
) && old
->sid
!= new->sid
)
790 if ((oldflags
& CONTEXT_MNT
) && old
->mntpoint_sid
!= new->mntpoint_sid
)
792 if ((oldflags
& DEFCONTEXT_MNT
) && old
->def_sid
!= new->def_sid
)
794 if (oldflags
& ROOTCONTEXT_MNT
) {
795 struct inode_security_struct
*oldroot
= oldsb
->s_root
->d_inode
->i_security
;
796 struct inode_security_struct
*newroot
= newsb
->s_root
->d_inode
->i_security
;
797 if (oldroot
->sid
!= newroot
->sid
)
802 printk(KERN_WARNING
"SELinux: mount invalid. Same superblock, "
803 "different security settings for (dev %s, "
804 "type %s)\n", newsb
->s_id
, newsb
->s_type
->name
);
808 static int selinux_sb_clone_mnt_opts(const struct super_block
*oldsb
,
809 struct super_block
*newsb
)
811 const struct superblock_security_struct
*oldsbsec
= oldsb
->s_security
;
812 struct superblock_security_struct
*newsbsec
= newsb
->s_security
;
814 int set_fscontext
= (oldsbsec
->flags
& FSCONTEXT_MNT
);
815 int set_context
= (oldsbsec
->flags
& CONTEXT_MNT
);
816 int set_rootcontext
= (oldsbsec
->flags
& ROOTCONTEXT_MNT
);
819 * if the parent was able to be mounted it clearly had no special lsm
820 * mount options. thus we can safely deal with this superblock later
825 /* how can we clone if the old one wasn't set up?? */
826 BUG_ON(!(oldsbsec
->flags
& SE_SBINITIALIZED
));
828 /* if fs is reusing a sb, make sure that the contexts match */
829 if (newsbsec
->flags
& SE_SBINITIALIZED
)
830 return selinux_cmp_sb_context(oldsb
, newsb
);
832 mutex_lock(&newsbsec
->lock
);
834 newsbsec
->flags
= oldsbsec
->flags
;
836 newsbsec
->sid
= oldsbsec
->sid
;
837 newsbsec
->def_sid
= oldsbsec
->def_sid
;
838 newsbsec
->behavior
= oldsbsec
->behavior
;
841 u32 sid
= oldsbsec
->mntpoint_sid
;
845 if (!set_rootcontext
) {
846 struct inode
*newinode
= newsb
->s_root
->d_inode
;
847 struct inode_security_struct
*newisec
= newinode
->i_security
;
850 newsbsec
->mntpoint_sid
= sid
;
852 if (set_rootcontext
) {
853 const struct inode
*oldinode
= oldsb
->s_root
->d_inode
;
854 const struct inode_security_struct
*oldisec
= oldinode
->i_security
;
855 struct inode
*newinode
= newsb
->s_root
->d_inode
;
856 struct inode_security_struct
*newisec
= newinode
->i_security
;
858 newisec
->sid
= oldisec
->sid
;
861 sb_finish_set_opts(newsb
);
862 mutex_unlock(&newsbsec
->lock
);
866 static int selinux_parse_opts_str(char *options
,
867 struct security_mnt_opts
*opts
)
870 char *context
= NULL
, *defcontext
= NULL
;
871 char *fscontext
= NULL
, *rootcontext
= NULL
;
872 int rc
, num_mnt_opts
= 0;
874 opts
->num_mnt_opts
= 0;
876 /* Standard string-based options. */
877 while ((p
= strsep(&options
, "|")) != NULL
) {
879 substring_t args
[MAX_OPT_ARGS
];
884 token
= match_token(p
, tokens
, args
);
888 if (context
|| defcontext
) {
890 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG
);
893 context
= match_strdup(&args
[0]);
903 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG
);
906 fscontext
= match_strdup(&args
[0]);
913 case Opt_rootcontext
:
916 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG
);
919 rootcontext
= match_strdup(&args
[0]);
927 if (context
|| defcontext
) {
929 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG
);
932 defcontext
= match_strdup(&args
[0]);
938 case Opt_labelsupport
:
942 printk(KERN_WARNING
"SELinux: unknown mount option\n");
949 opts
->mnt_opts
= kcalloc(NUM_SEL_MNT_OPTS
, sizeof(char *), GFP_ATOMIC
);
953 opts
->mnt_opts_flags
= kcalloc(NUM_SEL_MNT_OPTS
, sizeof(int), GFP_ATOMIC
);
954 if (!opts
->mnt_opts_flags
) {
955 kfree(opts
->mnt_opts
);
960 opts
->mnt_opts
[num_mnt_opts
] = fscontext
;
961 opts
->mnt_opts_flags
[num_mnt_opts
++] = FSCONTEXT_MNT
;
964 opts
->mnt_opts
[num_mnt_opts
] = context
;
965 opts
->mnt_opts_flags
[num_mnt_opts
++] = CONTEXT_MNT
;
968 opts
->mnt_opts
[num_mnt_opts
] = rootcontext
;
969 opts
->mnt_opts_flags
[num_mnt_opts
++] = ROOTCONTEXT_MNT
;
972 opts
->mnt_opts
[num_mnt_opts
] = defcontext
;
973 opts
->mnt_opts_flags
[num_mnt_opts
++] = DEFCONTEXT_MNT
;
976 opts
->num_mnt_opts
= num_mnt_opts
;
987 * string mount options parsing and call set the sbsec
989 static int superblock_doinit(struct super_block
*sb
, void *data
)
992 char *options
= data
;
993 struct security_mnt_opts opts
;
995 security_init_mnt_opts(&opts
);
1000 BUG_ON(sb
->s_type
->fs_flags
& FS_BINARY_MOUNTDATA
);
1002 rc
= selinux_parse_opts_str(options
, &opts
);
1007 rc
= selinux_set_mnt_opts(sb
, &opts
);
1010 security_free_mnt_opts(&opts
);
1014 static void selinux_write_opts(struct seq_file
*m
,
1015 struct security_mnt_opts
*opts
)
1020 for (i
= 0; i
< opts
->num_mnt_opts
; i
++) {
1023 if (opts
->mnt_opts
[i
])
1024 has_comma
= strchr(opts
->mnt_opts
[i
], ',');
1028 switch (opts
->mnt_opts_flags
[i
]) {
1030 prefix
= CONTEXT_STR
;
1033 prefix
= FSCONTEXT_STR
;
1035 case ROOTCONTEXT_MNT
:
1036 prefix
= ROOTCONTEXT_STR
;
1038 case DEFCONTEXT_MNT
:
1039 prefix
= DEFCONTEXT_STR
;
1041 case SE_SBLABELSUPP
:
1043 seq_puts(m
, LABELSUPP_STR
);
1049 /* we need a comma before each option */
1051 seq_puts(m
, prefix
);
1054 seq_puts(m
, opts
->mnt_opts
[i
]);
1060 static int selinux_sb_show_options(struct seq_file
*m
, struct super_block
*sb
)
1062 struct security_mnt_opts opts
;
1065 rc
= selinux_get_mnt_opts(sb
, &opts
);
1067 /* before policy load we may get EINVAL, don't show anything */
1073 selinux_write_opts(m
, &opts
);
1075 security_free_mnt_opts(&opts
);
1080 static inline u16
inode_mode_to_security_class(umode_t mode
)
1082 switch (mode
& S_IFMT
) {
1084 return SECCLASS_SOCK_FILE
;
1086 return SECCLASS_LNK_FILE
;
1088 return SECCLASS_FILE
;
1090 return SECCLASS_BLK_FILE
;
1092 return SECCLASS_DIR
;
1094 return SECCLASS_CHR_FILE
;
1096 return SECCLASS_FIFO_FILE
;
1100 return SECCLASS_FILE
;
1103 static inline int default_protocol_stream(int protocol
)
1105 return (protocol
== IPPROTO_IP
|| protocol
== IPPROTO_TCP
);
1108 static inline int default_protocol_dgram(int protocol
)
1110 return (protocol
== IPPROTO_IP
|| protocol
== IPPROTO_UDP
);
1113 static inline u16
socket_type_to_security_class(int family
, int type
, int protocol
)
1119 case SOCK_SEQPACKET
:
1120 return SECCLASS_UNIX_STREAM_SOCKET
;
1122 return SECCLASS_UNIX_DGRAM_SOCKET
;
1129 if (default_protocol_stream(protocol
))
1130 return SECCLASS_TCP_SOCKET
;
1132 return SECCLASS_RAWIP_SOCKET
;
1134 if (default_protocol_dgram(protocol
))
1135 return SECCLASS_UDP_SOCKET
;
1137 return SECCLASS_RAWIP_SOCKET
;
1139 return SECCLASS_DCCP_SOCKET
;
1141 return SECCLASS_RAWIP_SOCKET
;
1147 return SECCLASS_NETLINK_ROUTE_SOCKET
;
1148 case NETLINK_FIREWALL
:
1149 return SECCLASS_NETLINK_FIREWALL_SOCKET
;
1150 case NETLINK_SOCK_DIAG
:
1151 return SECCLASS_NETLINK_TCPDIAG_SOCKET
;
1153 return SECCLASS_NETLINK_NFLOG_SOCKET
;
1155 return SECCLASS_NETLINK_XFRM_SOCKET
;
1156 case NETLINK_SELINUX
:
1157 return SECCLASS_NETLINK_SELINUX_SOCKET
;
1159 return SECCLASS_NETLINK_AUDIT_SOCKET
;
1160 case NETLINK_IP6_FW
:
1161 return SECCLASS_NETLINK_IP6FW_SOCKET
;
1162 case NETLINK_DNRTMSG
:
1163 return SECCLASS_NETLINK_DNRT_SOCKET
;
1164 case NETLINK_KOBJECT_UEVENT
:
1165 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET
;
1167 return SECCLASS_NETLINK_SOCKET
;
1170 return SECCLASS_PACKET_SOCKET
;
1172 return SECCLASS_KEY_SOCKET
;
1174 return SECCLASS_APPLETALK_SOCKET
;
1177 return SECCLASS_SOCKET
;
1180 #ifdef CONFIG_PROC_FS
1181 static int selinux_proc_get_sid(struct dentry
*dentry
,
1186 char *buffer
, *path
;
1188 buffer
= (char *)__get_free_page(GFP_KERNEL
);
1192 path
= dentry_path_raw(dentry
, buffer
, PAGE_SIZE
);
1196 /* each process gets a /proc/PID/ entry. Strip off the
1197 * PID part to get a valid selinux labeling.
1198 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1199 while (path
[1] >= '0' && path
[1] <= '9') {
1203 rc
= security_genfs_sid("proc", path
, tclass
, sid
);
1205 free_page((unsigned long)buffer
);
1209 static int selinux_proc_get_sid(struct dentry
*dentry
,
1217 /* The inode's security attributes must be initialized before first use. */
1218 static int inode_doinit_with_dentry(struct inode
*inode
, struct dentry
*opt_dentry
)
1220 struct superblock_security_struct
*sbsec
= NULL
;
1221 struct inode_security_struct
*isec
= inode
->i_security
;
1223 struct dentry
*dentry
;
1224 #define INITCONTEXTLEN 255
1225 char *context
= NULL
;
1229 if (isec
->initialized
)
1232 mutex_lock(&isec
->lock
);
1233 if (isec
->initialized
)
1236 sbsec
= inode
->i_sb
->s_security
;
1237 if (!(sbsec
->flags
& SE_SBINITIALIZED
)) {
1238 /* Defer initialization until selinux_complete_init,
1239 after the initial policy is loaded and the security
1240 server is ready to handle calls. */
1241 spin_lock(&sbsec
->isec_lock
);
1242 if (list_empty(&isec
->list
))
1243 list_add(&isec
->list
, &sbsec
->isec_head
);
1244 spin_unlock(&sbsec
->isec_lock
);
1248 switch (sbsec
->behavior
) {
1249 case SECURITY_FS_USE_XATTR
:
1250 if (!inode
->i_op
->getxattr
) {
1251 isec
->sid
= sbsec
->def_sid
;
1255 /* Need a dentry, since the xattr API requires one.
1256 Life would be simpler if we could just pass the inode. */
1258 /* Called from d_instantiate or d_splice_alias. */
1259 dentry
= dget(opt_dentry
);
1261 /* Called from selinux_complete_init, try to find a dentry. */
1262 dentry
= d_find_alias(inode
);
1266 * this is can be hit on boot when a file is accessed
1267 * before the policy is loaded. When we load policy we
1268 * may find inodes that have no dentry on the
1269 * sbsec->isec_head list. No reason to complain as these
1270 * will get fixed up the next time we go through
1271 * inode_doinit with a dentry, before these inodes could
1272 * be used again by userspace.
1277 len
= INITCONTEXTLEN
;
1278 context
= kmalloc(len
+1, GFP_NOFS
);
1284 context
[len
] = '\0';
1285 rc
= inode
->i_op
->getxattr(dentry
, XATTR_NAME_SELINUX
,
1287 if (rc
== -ERANGE
) {
1290 /* Need a larger buffer. Query for the right size. */
1291 rc
= inode
->i_op
->getxattr(dentry
, XATTR_NAME_SELINUX
,
1298 context
= kmalloc(len
+1, GFP_NOFS
);
1304 context
[len
] = '\0';
1305 rc
= inode
->i_op
->getxattr(dentry
,
1311 if (rc
!= -ENODATA
) {
1312 printk(KERN_WARNING
"SELinux: %s: getxattr returned "
1313 "%d for dev=%s ino=%ld\n", __func__
,
1314 -rc
, inode
->i_sb
->s_id
, inode
->i_ino
);
1318 /* Map ENODATA to the default file SID */
1319 sid
= sbsec
->def_sid
;
1322 rc
= security_context_to_sid_default(context
, rc
, &sid
,
1326 char *dev
= inode
->i_sb
->s_id
;
1327 unsigned long ino
= inode
->i_ino
;
1329 if (rc
== -EINVAL
) {
1330 if (printk_ratelimit())
1331 printk(KERN_NOTICE
"SELinux: inode=%lu on dev=%s was found to have an invalid "
1332 "context=%s. This indicates you may need to relabel the inode or the "
1333 "filesystem in question.\n", ino
, dev
, context
);
1335 printk(KERN_WARNING
"SELinux: %s: context_to_sid(%s) "
1336 "returned %d for dev=%s ino=%ld\n",
1337 __func__
, context
, -rc
, dev
, ino
);
1340 /* Leave with the unlabeled SID */
1348 case SECURITY_FS_USE_TASK
:
1349 isec
->sid
= isec
->task_sid
;
1351 case SECURITY_FS_USE_TRANS
:
1352 /* Default to the fs SID. */
1353 isec
->sid
= sbsec
->sid
;
1355 /* Try to obtain a transition SID. */
1356 isec
->sclass
= inode_mode_to_security_class(inode
->i_mode
);
1357 rc
= security_transition_sid(isec
->task_sid
, sbsec
->sid
,
1358 isec
->sclass
, NULL
, &sid
);
1363 case SECURITY_FS_USE_MNTPOINT
:
1364 isec
->sid
= sbsec
->mntpoint_sid
;
1367 /* Default to the fs superblock SID. */
1368 isec
->sid
= sbsec
->sid
;
1370 if ((sbsec
->flags
& SE_SBPROC
) && !S_ISLNK(inode
->i_mode
)) {
1371 /* We must have a dentry to determine the label on
1374 /* Called from d_instantiate or
1375 * d_splice_alias. */
1376 dentry
= dget(opt_dentry
);
1378 /* Called from selinux_complete_init, try to
1380 dentry
= d_find_alias(inode
);
1382 * This can be hit on boot when a file is accessed
1383 * before the policy is loaded. When we load policy we
1384 * may find inodes that have no dentry on the
1385 * sbsec->isec_head list. No reason to complain as
1386 * these will get fixed up the next time we go through
1387 * inode_doinit() with a dentry, before these inodes
1388 * could be used again by userspace.
1392 isec
->sclass
= inode_mode_to_security_class(inode
->i_mode
);
1393 rc
= selinux_proc_get_sid(dentry
, isec
->sclass
, &sid
);
1402 isec
->initialized
= 1;
1405 mutex_unlock(&isec
->lock
);
1407 if (isec
->sclass
== SECCLASS_FILE
)
1408 isec
->sclass
= inode_mode_to_security_class(inode
->i_mode
);
1412 /* Convert a Linux signal to an access vector. */
1413 static inline u32
signal_to_av(int sig
)
1419 /* Commonly granted from child to parent. */
1420 perm
= PROCESS__SIGCHLD
;
1423 /* Cannot be caught or ignored */
1424 perm
= PROCESS__SIGKILL
;
1427 /* Cannot be caught or ignored */
1428 perm
= PROCESS__SIGSTOP
;
1431 /* All other signals. */
1432 perm
= PROCESS__SIGNAL
;
1440 * Check permission between a pair of credentials
1441 * fork check, ptrace check, etc.
1443 static int cred_has_perm(const struct cred
*actor
,
1444 const struct cred
*target
,
1447 u32 asid
= cred_sid(actor
), tsid
= cred_sid(target
);
1449 return avc_has_perm(asid
, tsid
, SECCLASS_PROCESS
, perms
, NULL
);
1453 * Check permission between a pair of tasks, e.g. signal checks,
1454 * fork check, ptrace check, etc.
1455 * tsk1 is the actor and tsk2 is the target
1456 * - this uses the default subjective creds of tsk1
1458 static int task_has_perm(const struct task_struct
*tsk1
,
1459 const struct task_struct
*tsk2
,
1462 const struct task_security_struct
*__tsec1
, *__tsec2
;
1466 __tsec1
= __task_cred(tsk1
)->security
; sid1
= __tsec1
->sid
;
1467 __tsec2
= __task_cred(tsk2
)->security
; sid2
= __tsec2
->sid
;
1469 return avc_has_perm(sid1
, sid2
, SECCLASS_PROCESS
, perms
, NULL
);
1473 * Check permission between current and another task, e.g. signal checks,
1474 * fork check, ptrace check, etc.
1475 * current is the actor and tsk2 is the target
1476 * - this uses current's subjective creds
1478 static int current_has_perm(const struct task_struct
*tsk
,
1483 sid
= current_sid();
1484 tsid
= task_sid(tsk
);
1485 return avc_has_perm(sid
, tsid
, SECCLASS_PROCESS
, perms
, NULL
);
1488 #if CAP_LAST_CAP > 63
1489 #error Fix SELinux to handle capabilities > 63.
1492 /* Check whether a task is allowed to use a capability. */
1493 static int cred_has_capability(const struct cred
*cred
,
1496 struct common_audit_data ad
;
1497 struct av_decision avd
;
1499 u32 sid
= cred_sid(cred
);
1500 u32 av
= CAP_TO_MASK(cap
);
1503 ad
.type
= LSM_AUDIT_DATA_CAP
;
1506 switch (CAP_TO_INDEX(cap
)) {
1508 sclass
= SECCLASS_CAPABILITY
;
1511 sclass
= SECCLASS_CAPABILITY2
;
1515 "SELinux: out of range capability %d\n", cap
);
1520 rc
= avc_has_perm_noaudit(sid
, sid
, sclass
, av
, 0, &avd
);
1521 if (audit
== SECURITY_CAP_AUDIT
) {
1522 int rc2
= avc_audit(sid
, sid
, sclass
, av
, &avd
, rc
, &ad
, 0);
1529 /* Check whether a task is allowed to use a system operation. */
1530 static int task_has_system(struct task_struct
*tsk
,
1533 u32 sid
= task_sid(tsk
);
1535 return avc_has_perm(sid
, SECINITSID_KERNEL
,
1536 SECCLASS_SYSTEM
, perms
, NULL
);
1539 /* Check whether a task has a particular permission to an inode.
1540 The 'adp' parameter is optional and allows other audit
1541 data to be passed (e.g. the dentry). */
1542 static int inode_has_perm(const struct cred
*cred
,
1543 struct inode
*inode
,
1545 struct common_audit_data
*adp
,
1548 struct inode_security_struct
*isec
;
1551 validate_creds(cred
);
1553 if (unlikely(IS_PRIVATE(inode
)))
1556 sid
= cred_sid(cred
);
1557 isec
= inode
->i_security
;
1559 return avc_has_perm_flags(sid
, isec
->sid
, isec
->sclass
, perms
, adp
, flags
);
1562 /* Same as inode_has_perm, but pass explicit audit data containing
1563 the dentry to help the auditing code to more easily generate the
1564 pathname if needed. */
1565 static inline int dentry_has_perm(const struct cred
*cred
,
1566 struct dentry
*dentry
,
1569 struct inode
*inode
= dentry
->d_inode
;
1570 struct common_audit_data ad
;
1572 ad
.type
= LSM_AUDIT_DATA_DENTRY
;
1573 ad
.u
.dentry
= dentry
;
1574 return inode_has_perm(cred
, inode
, av
, &ad
, 0);
1577 /* Same as inode_has_perm, but pass explicit audit data containing
1578 the path to help the auditing code to more easily generate the
1579 pathname if needed. */
1580 static inline int path_has_perm(const struct cred
*cred
,
1584 struct inode
*inode
= path
->dentry
->d_inode
;
1585 struct common_audit_data ad
;
1587 ad
.type
= LSM_AUDIT_DATA_PATH
;
1589 return inode_has_perm(cred
, inode
, av
, &ad
, 0);
1592 /* Check whether a task can use an open file descriptor to
1593 access an inode in a given way. Check access to the
1594 descriptor itself, and then use dentry_has_perm to
1595 check a particular permission to the file.
1596 Access to the descriptor is implicitly granted if it
1597 has the same SID as the process. If av is zero, then
1598 access to the file is not checked, e.g. for cases
1599 where only the descriptor is affected like seek. */
1600 static int file_has_perm(const struct cred
*cred
,
1604 struct file_security_struct
*fsec
= file
->f_security
;
1605 struct inode
*inode
= file_inode(file
);
1606 struct common_audit_data ad
;
1607 u32 sid
= cred_sid(cred
);
1610 ad
.type
= LSM_AUDIT_DATA_PATH
;
1611 ad
.u
.path
= file
->f_path
;
1613 if (sid
!= fsec
->sid
) {
1614 rc
= avc_has_perm(sid
, fsec
->sid
,
1622 /* av is zero if only checking access to the descriptor. */
1625 rc
= inode_has_perm(cred
, inode
, av
, &ad
, 0);
1631 /* Check whether a task can create a file. */
1632 static int may_create(struct inode
*dir
,
1633 struct dentry
*dentry
,
1636 const struct task_security_struct
*tsec
= current_security();
1637 struct inode_security_struct
*dsec
;
1638 struct superblock_security_struct
*sbsec
;
1640 struct common_audit_data ad
;
1643 dsec
= dir
->i_security
;
1644 sbsec
= dir
->i_sb
->s_security
;
1647 newsid
= tsec
->create_sid
;
1649 ad
.type
= LSM_AUDIT_DATA_DENTRY
;
1650 ad
.u
.dentry
= dentry
;
1652 rc
= avc_has_perm(sid
, dsec
->sid
, SECCLASS_DIR
,
1653 DIR__ADD_NAME
| DIR__SEARCH
,
1658 if (!newsid
|| !(sbsec
->flags
& SE_SBLABELSUPP
)) {
1659 rc
= security_transition_sid(sid
, dsec
->sid
, tclass
,
1660 &dentry
->d_name
, &newsid
);
1665 rc
= avc_has_perm(sid
, newsid
, tclass
, FILE__CREATE
, &ad
);
1669 return avc_has_perm(newsid
, sbsec
->sid
,
1670 SECCLASS_FILESYSTEM
,
1671 FILESYSTEM__ASSOCIATE
, &ad
);
1674 /* Check whether a task can create a key. */
1675 static int may_create_key(u32 ksid
,
1676 struct task_struct
*ctx
)
1678 u32 sid
= task_sid(ctx
);
1680 return avc_has_perm(sid
, ksid
, SECCLASS_KEY
, KEY__CREATE
, NULL
);
1684 #define MAY_UNLINK 1
1687 /* Check whether a task can link, unlink, or rmdir a file/directory. */
1688 static int may_link(struct inode
*dir
,
1689 struct dentry
*dentry
,
1693 struct inode_security_struct
*dsec
, *isec
;
1694 struct common_audit_data ad
;
1695 u32 sid
= current_sid();
1699 dsec
= dir
->i_security
;
1700 isec
= dentry
->d_inode
->i_security
;
1702 ad
.type
= LSM_AUDIT_DATA_DENTRY
;
1703 ad
.u
.dentry
= dentry
;
1706 av
|= (kind
? DIR__REMOVE_NAME
: DIR__ADD_NAME
);
1707 rc
= avc_has_perm(sid
, dsec
->sid
, SECCLASS_DIR
, av
, &ad
);
1722 printk(KERN_WARNING
"SELinux: %s: unrecognized kind %d\n",
1727 rc
= avc_has_perm(sid
, isec
->sid
, isec
->sclass
, av
, &ad
);
1731 static inline int may_rename(struct inode
*old_dir
,
1732 struct dentry
*old_dentry
,
1733 struct inode
*new_dir
,
1734 struct dentry
*new_dentry
)
1736 struct inode_security_struct
*old_dsec
, *new_dsec
, *old_isec
, *new_isec
;
1737 struct common_audit_data ad
;
1738 u32 sid
= current_sid();
1740 int old_is_dir
, new_is_dir
;
1743 old_dsec
= old_dir
->i_security
;
1744 old_isec
= old_dentry
->d_inode
->i_security
;
1745 old_is_dir
= S_ISDIR(old_dentry
->d_inode
->i_mode
);
1746 new_dsec
= new_dir
->i_security
;
1748 ad
.type
= LSM_AUDIT_DATA_DENTRY
;
1750 ad
.u
.dentry
= old_dentry
;
1751 rc
= avc_has_perm(sid
, old_dsec
->sid
, SECCLASS_DIR
,
1752 DIR__REMOVE_NAME
| DIR__SEARCH
, &ad
);
1755 rc
= avc_has_perm(sid
, old_isec
->sid
,
1756 old_isec
->sclass
, FILE__RENAME
, &ad
);
1759 if (old_is_dir
&& new_dir
!= old_dir
) {
1760 rc
= avc_has_perm(sid
, old_isec
->sid
,
1761 old_isec
->sclass
, DIR__REPARENT
, &ad
);
1766 ad
.u
.dentry
= new_dentry
;
1767 av
= DIR__ADD_NAME
| DIR__SEARCH
;
1768 if (new_dentry
->d_inode
)
1769 av
|= DIR__REMOVE_NAME
;
1770 rc
= avc_has_perm(sid
, new_dsec
->sid
, SECCLASS_DIR
, av
, &ad
);
1773 if (new_dentry
->d_inode
) {
1774 new_isec
= new_dentry
->d_inode
->i_security
;
1775 new_is_dir
= S_ISDIR(new_dentry
->d_inode
->i_mode
);
1776 rc
= avc_has_perm(sid
, new_isec
->sid
,
1778 (new_is_dir
? DIR__RMDIR
: FILE__UNLINK
), &ad
);
1786 /* Check whether a task can perform a filesystem operation. */
1787 static int superblock_has_perm(const struct cred
*cred
,
1788 struct super_block
*sb
,
1790 struct common_audit_data
*ad
)
1792 struct superblock_security_struct
*sbsec
;
1793 u32 sid
= cred_sid(cred
);
1795 sbsec
= sb
->s_security
;
1796 return avc_has_perm(sid
, sbsec
->sid
, SECCLASS_FILESYSTEM
, perms
, ad
);
1799 /* Convert a Linux mode and permission mask to an access vector. */
1800 static inline u32
file_mask_to_av(int mode
, int mask
)
1804 if (!S_ISDIR(mode
)) {
1805 if (mask
& MAY_EXEC
)
1806 av
|= FILE__EXECUTE
;
1807 if (mask
& MAY_READ
)
1810 if (mask
& MAY_APPEND
)
1812 else if (mask
& MAY_WRITE
)
1816 if (mask
& MAY_EXEC
)
1818 if (mask
& MAY_WRITE
)
1820 if (mask
& MAY_READ
)
1827 /* Convert a Linux file to an access vector. */
1828 static inline u32
file_to_av(struct file
*file
)
1832 if (file
->f_mode
& FMODE_READ
)
1834 if (file
->f_mode
& FMODE_WRITE
) {
1835 if (file
->f_flags
& O_APPEND
)
1842 * Special file opened with flags 3 for ioctl-only use.
1851 * Convert a file to an access vector and include the correct open
1854 static inline u32
open_file_to_av(struct file
*file
)
1856 u32 av
= file_to_av(file
);
1858 if (selinux_policycap_openperm
)
1864 /* Hook functions begin here. */
1866 static int selinux_binder_set_context_mgr(struct task_struct
*mgr
)
1868 u32 mysid
= current_sid();
1869 u32 mgrsid
= task_sid(mgr
);
1871 return avc_has_perm(mysid
, mgrsid
, SECCLASS_BINDER
, BINDER__SET_CONTEXT_MGR
, NULL
);
1874 static int selinux_binder_transaction(struct task_struct
*from
, struct task_struct
*to
)
1876 u32 mysid
= current_sid();
1877 u32 fromsid
= task_sid(from
);
1878 u32 tosid
= task_sid(to
);
1881 if (mysid
!= fromsid
) {
1882 rc
= avc_has_perm(mysid
, fromsid
, SECCLASS_BINDER
, BINDER__IMPERSONATE
, NULL
);
1887 return avc_has_perm(fromsid
, tosid
, SECCLASS_BINDER
, BINDER__CALL
, NULL
);
1890 static int selinux_binder_transfer_binder(struct task_struct
*from
, struct task_struct
*to
)
1892 u32 fromsid
= task_sid(from
);
1893 u32 tosid
= task_sid(to
);
1894 return avc_has_perm(fromsid
, tosid
, SECCLASS_BINDER
, BINDER__TRANSFER
, NULL
);
1897 static int selinux_binder_transfer_file(struct task_struct
*from
, struct task_struct
*to
, struct file
*file
)
1899 u32 sid
= task_sid(to
);
1900 struct file_security_struct
*fsec
= file
->f_security
;
1901 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1902 struct inode_security_struct
*isec
= inode
->i_security
;
1903 struct common_audit_data ad
;
1904 struct selinux_audit_data sad
= {0,};
1907 ad
.type
= LSM_AUDIT_DATA_PATH
;
1908 ad
.u
.path
= file
->f_path
;
1909 ad
.selinux_audit_data
= &sad
;
1911 if (sid
!= fsec
->sid
) {
1912 rc
= avc_has_perm(sid
, fsec
->sid
,
1920 if (unlikely(IS_PRIVATE(inode
)))
1923 return avc_has_perm(sid
, isec
->sid
, isec
->sclass
, file_to_av(file
),
1927 static int selinux_ptrace_access_check(struct task_struct
*child
,
1932 rc
= cap_ptrace_access_check(child
, mode
);
1936 if (mode
& PTRACE_MODE_READ
) {
1937 u32 sid
= current_sid();
1938 u32 csid
= task_sid(child
);
1939 return avc_has_perm(sid
, csid
, SECCLASS_FILE
, FILE__READ
, NULL
);
1942 return current_has_perm(child
, PROCESS__PTRACE
);
1945 static int selinux_ptrace_traceme(struct task_struct
*parent
)
1949 rc
= cap_ptrace_traceme(parent
);
1953 return task_has_perm(parent
, current
, PROCESS__PTRACE
);
1956 static int selinux_capget(struct task_struct
*target
, kernel_cap_t
*effective
,
1957 kernel_cap_t
*inheritable
, kernel_cap_t
*permitted
)
1961 error
= current_has_perm(target
, PROCESS__GETCAP
);
1965 return cap_capget(target
, effective
, inheritable
, permitted
);
1968 static int selinux_capset(struct cred
*new, const struct cred
*old
,
1969 const kernel_cap_t
*effective
,
1970 const kernel_cap_t
*inheritable
,
1971 const kernel_cap_t
*permitted
)
1975 error
= cap_capset(new, old
,
1976 effective
, inheritable
, permitted
);
1980 return cred_has_perm(old
, new, PROCESS__SETCAP
);
1984 * (This comment used to live with the selinux_task_setuid hook,
1985 * which was removed).
1987 * Since setuid only affects the current process, and since the SELinux
1988 * controls are not based on the Linux identity attributes, SELinux does not
1989 * need to control this operation. However, SELinux does control the use of
1990 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
1993 static int selinux_capable(const struct cred
*cred
, struct user_namespace
*ns
,
1998 rc
= cap_capable(cred
, ns
, cap
, audit
);
2002 return cred_has_capability(cred
, cap
, audit
);
2005 static int selinux_quotactl(int cmds
, int type
, int id
, struct super_block
*sb
)
2007 const struct cred
*cred
= current_cred();
2019 rc
= superblock_has_perm(cred
, sb
, FILESYSTEM__QUOTAMOD
, NULL
);
2024 rc
= superblock_has_perm(cred
, sb
, FILESYSTEM__QUOTAGET
, NULL
);
2027 rc
= 0; /* let the kernel handle invalid cmds */
2033 static int selinux_quota_on(struct dentry
*dentry
)
2035 const struct cred
*cred
= current_cred();
2037 return dentry_has_perm(cred
, dentry
, FILE__QUOTAON
);
2040 static int selinux_syslog(int type
)
2045 case SYSLOG_ACTION_READ_ALL
: /* Read last kernel messages */
2046 case SYSLOG_ACTION_SIZE_BUFFER
: /* Return size of the log buffer */
2047 rc
= task_has_system(current
, SYSTEM__SYSLOG_READ
);
2049 case SYSLOG_ACTION_CONSOLE_OFF
: /* Disable logging to console */
2050 case SYSLOG_ACTION_CONSOLE_ON
: /* Enable logging to console */
2051 /* Set level of messages printed to console */
2052 case SYSLOG_ACTION_CONSOLE_LEVEL
:
2053 rc
= task_has_system(current
, SYSTEM__SYSLOG_CONSOLE
);
2055 case SYSLOG_ACTION_CLOSE
: /* Close log */
2056 case SYSLOG_ACTION_OPEN
: /* Open log */
2057 case SYSLOG_ACTION_READ
: /* Read from log */
2058 case SYSLOG_ACTION_READ_CLEAR
: /* Read/clear last kernel messages */
2059 case SYSLOG_ACTION_CLEAR
: /* Clear ring buffer */
2061 rc
= task_has_system(current
, SYSTEM__SYSLOG_MOD
);
2068 * Check that a process has enough memory to allocate a new virtual
2069 * mapping. 0 means there is enough memory for the allocation to
2070 * succeed and -ENOMEM implies there is not.
2072 * Do not audit the selinux permission check, as this is applied to all
2073 * processes that allocate mappings.
2075 static int selinux_vm_enough_memory(struct mm_struct
*mm
, long pages
)
2077 int rc
, cap_sys_admin
= 0;
2079 rc
= selinux_capable(current_cred(), &init_user_ns
, CAP_SYS_ADMIN
,
2080 SECURITY_CAP_NOAUDIT
);
2084 return __vm_enough_memory(mm
, pages
, cap_sys_admin
);
2087 /* binprm security operations */
2089 static int selinux_bprm_set_creds(struct linux_binprm
*bprm
)
2091 const struct task_security_struct
*old_tsec
;
2092 struct task_security_struct
*new_tsec
;
2093 struct inode_security_struct
*isec
;
2094 struct common_audit_data ad
;
2095 struct inode
*inode
= file_inode(bprm
->file
);
2098 rc
= cap_bprm_set_creds(bprm
);
2102 /* SELinux context only depends on initial program or script and not
2103 * the script interpreter */
2104 if (bprm
->cred_prepared
)
2107 old_tsec
= current_security();
2108 new_tsec
= bprm
->cred
->security
;
2109 isec
= inode
->i_security
;
2111 /* Default to the current task SID. */
2112 new_tsec
->sid
= old_tsec
->sid
;
2113 new_tsec
->osid
= old_tsec
->sid
;
2115 /* Reset fs, key, and sock SIDs on execve. */
2116 new_tsec
->create_sid
= 0;
2117 new_tsec
->keycreate_sid
= 0;
2118 new_tsec
->sockcreate_sid
= 0;
2120 if (old_tsec
->exec_sid
) {
2121 new_tsec
->sid
= old_tsec
->exec_sid
;
2122 /* Reset exec SID on execve. */
2123 new_tsec
->exec_sid
= 0;
2126 * Minimize confusion: if no_new_privs and a transition is
2127 * explicitly requested, then fail the exec.
2129 if (bprm
->unsafe
& LSM_UNSAFE_NO_NEW_PRIVS
)
2132 /* Check for a default transition on this program. */
2133 rc
= security_transition_sid(old_tsec
->sid
, isec
->sid
,
2134 SECCLASS_PROCESS
, NULL
,
2140 ad
.type
= LSM_AUDIT_DATA_PATH
;
2141 ad
.u
.path
= bprm
->file
->f_path
;
2143 if ((bprm
->file
->f_path
.mnt
->mnt_flags
& MNT_NOSUID
) ||
2144 (bprm
->unsafe
& LSM_UNSAFE_NO_NEW_PRIVS
))
2145 new_tsec
->sid
= old_tsec
->sid
;
2147 if (new_tsec
->sid
== old_tsec
->sid
) {
2148 rc
= avc_has_perm(old_tsec
->sid
, isec
->sid
,
2149 SECCLASS_FILE
, FILE__EXECUTE_NO_TRANS
, &ad
);
2153 /* Check permissions for the transition. */
2154 rc
= avc_has_perm(old_tsec
->sid
, new_tsec
->sid
,
2155 SECCLASS_PROCESS
, PROCESS__TRANSITION
, &ad
);
2159 rc
= avc_has_perm(new_tsec
->sid
, isec
->sid
,
2160 SECCLASS_FILE
, FILE__ENTRYPOINT
, &ad
);
2164 /* Check for shared state */
2165 if (bprm
->unsafe
& LSM_UNSAFE_SHARE
) {
2166 rc
= avc_has_perm(old_tsec
->sid
, new_tsec
->sid
,
2167 SECCLASS_PROCESS
, PROCESS__SHARE
,
2173 /* Make sure that anyone attempting to ptrace over a task that
2174 * changes its SID has the appropriate permit */
2176 (LSM_UNSAFE_PTRACE
| LSM_UNSAFE_PTRACE_CAP
)) {
2177 struct task_struct
*tracer
;
2178 struct task_security_struct
*sec
;
2182 tracer
= ptrace_parent(current
);
2183 if (likely(tracer
!= NULL
)) {
2184 sec
= __task_cred(tracer
)->security
;
2190 rc
= avc_has_perm(ptsid
, new_tsec
->sid
,
2192 PROCESS__PTRACE
, NULL
);
2198 /* Clear any possibly unsafe personality bits on exec: */
2199 bprm
->per_clear
|= PER_CLEAR_ON_SETID
;
2205 static int selinux_bprm_secureexec(struct linux_binprm
*bprm
)
2207 const struct task_security_struct
*tsec
= current_security();
2215 /* Enable secure mode for SIDs transitions unless
2216 the noatsecure permission is granted between
2217 the two SIDs, i.e. ahp returns 0. */
2218 atsecure
= avc_has_perm(osid
, sid
,
2220 PROCESS__NOATSECURE
, NULL
);
2223 return (atsecure
|| cap_bprm_secureexec(bprm
));
2226 static int match_file(const void *p
, struct file
*file
, unsigned fd
)
2228 return file_has_perm(p
, file
, file_to_av(file
)) ? fd
+ 1 : 0;
2231 /* Derived from fs/exec.c:flush_old_files. */
2232 static inline void flush_unauthorized_files(const struct cred
*cred
,
2233 struct files_struct
*files
)
2235 struct file
*file
, *devnull
= NULL
;
2236 struct tty_struct
*tty
;
2240 tty
= get_current_tty();
2242 spin_lock(&tty_files_lock
);
2243 if (!list_empty(&tty
->tty_files
)) {
2244 struct tty_file_private
*file_priv
;
2246 /* Revalidate access to controlling tty.
2247 Use path_has_perm on the tty path directly rather
2248 than using file_has_perm, as this particular open
2249 file may belong to another process and we are only
2250 interested in the inode-based check here. */
2251 file_priv
= list_first_entry(&tty
->tty_files
,
2252 struct tty_file_private
, list
);
2253 file
= file_priv
->file
;
2254 if (path_has_perm(cred
, &file
->f_path
, FILE__READ
| FILE__WRITE
))
2257 spin_unlock(&tty_files_lock
);
2260 /* Reset controlling tty. */
2264 /* Revalidate access to inherited open files. */
2265 n
= iterate_fd(files
, 0, match_file
, cred
);
2266 if (!n
) /* none found? */
2269 devnull
= dentry_open(&selinux_null
, O_RDWR
, cred
);
2270 if (IS_ERR(devnull
))
2272 /* replace all the matching ones with this */
2274 replace_fd(n
- 1, devnull
, 0);
2275 } while ((n
= iterate_fd(files
, n
, match_file
, cred
)) != 0);
2281 * Prepare a process for imminent new credential changes due to exec
2283 static void selinux_bprm_committing_creds(struct linux_binprm
*bprm
)
2285 struct task_security_struct
*new_tsec
;
2286 struct rlimit
*rlim
, *initrlim
;
2289 new_tsec
= bprm
->cred
->security
;
2290 if (new_tsec
->sid
== new_tsec
->osid
)
2293 /* Close files for which the new task SID is not authorized. */
2294 flush_unauthorized_files(bprm
->cred
, current
->files
);
2296 /* Always clear parent death signal on SID transitions. */
2297 current
->pdeath_signal
= 0;
2299 /* Check whether the new SID can inherit resource limits from the old
2300 * SID. If not, reset all soft limits to the lower of the current
2301 * task's hard limit and the init task's soft limit.
2303 * Note that the setting of hard limits (even to lower them) can be
2304 * controlled by the setrlimit check. The inclusion of the init task's
2305 * soft limit into the computation is to avoid resetting soft limits
2306 * higher than the default soft limit for cases where the default is
2307 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2309 rc
= avc_has_perm(new_tsec
->osid
, new_tsec
->sid
, SECCLASS_PROCESS
,
2310 PROCESS__RLIMITINH
, NULL
);
2312 /* protect against do_prlimit() */
2314 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
2315 rlim
= current
->signal
->rlim
+ i
;
2316 initrlim
= init_task
.signal
->rlim
+ i
;
2317 rlim
->rlim_cur
= min(rlim
->rlim_max
, initrlim
->rlim_cur
);
2319 task_unlock(current
);
2320 update_rlimit_cpu(current
, rlimit(RLIMIT_CPU
));
2325 * Clean up the process immediately after the installation of new credentials
2328 static void selinux_bprm_committed_creds(struct linux_binprm
*bprm
)
2330 const struct task_security_struct
*tsec
= current_security();
2331 struct itimerval itimer
;
2341 /* Check whether the new SID can inherit signal state from the old SID.
2342 * If not, clear itimers to avoid subsequent signal generation and
2343 * flush and unblock signals.
2345 * This must occur _after_ the task SID has been updated so that any
2346 * kill done after the flush will be checked against the new SID.
2348 rc
= avc_has_perm(osid
, sid
, SECCLASS_PROCESS
, PROCESS__SIGINH
, NULL
);
2350 memset(&itimer
, 0, sizeof itimer
);
2351 for (i
= 0; i
< 3; i
++)
2352 do_setitimer(i
, &itimer
, NULL
);
2353 spin_lock_irq(¤t
->sighand
->siglock
);
2354 if (!(current
->signal
->flags
& SIGNAL_GROUP_EXIT
)) {
2355 __flush_signals(current
);
2356 flush_signal_handlers(current
, 1);
2357 sigemptyset(¤t
->blocked
);
2359 spin_unlock_irq(¤t
->sighand
->siglock
);
2362 /* Wake up the parent if it is waiting so that it can recheck
2363 * wait permission to the new task SID. */
2364 read_lock(&tasklist_lock
);
2365 __wake_up_parent(current
, current
->real_parent
);
2366 read_unlock(&tasklist_lock
);
2369 /* superblock security operations */
2371 static int selinux_sb_alloc_security(struct super_block
*sb
)
2373 return superblock_alloc_security(sb
);
2376 static void selinux_sb_free_security(struct super_block
*sb
)
2378 superblock_free_security(sb
);
2381 static inline int match_prefix(char *prefix
, int plen
, char *option
, int olen
)
2386 return !memcmp(prefix
, option
, plen
);
2389 static inline int selinux_option(char *option
, int len
)
2391 return (match_prefix(CONTEXT_STR
, sizeof(CONTEXT_STR
)-1, option
, len
) ||
2392 match_prefix(FSCONTEXT_STR
, sizeof(FSCONTEXT_STR
)-1, option
, len
) ||
2393 match_prefix(DEFCONTEXT_STR
, sizeof(DEFCONTEXT_STR
)-1, option
, len
) ||
2394 match_prefix(ROOTCONTEXT_STR
, sizeof(ROOTCONTEXT_STR
)-1, option
, len
) ||
2395 match_prefix(LABELSUPP_STR
, sizeof(LABELSUPP_STR
)-1, option
, len
));
2398 static inline void take_option(char **to
, char *from
, int *first
, int len
)
2405 memcpy(*to
, from
, len
);
2409 static inline void take_selinux_option(char **to
, char *from
, int *first
,
2412 int current_size
= 0;
2420 while (current_size
< len
) {
2430 static int selinux_sb_copy_data(char *orig
, char *copy
)
2432 int fnosec
, fsec
, rc
= 0;
2433 char *in_save
, *in_curr
, *in_end
;
2434 char *sec_curr
, *nosec_save
, *nosec
;
2440 nosec
= (char *)get_zeroed_page(GFP_KERNEL
);
2448 in_save
= in_end
= orig
;
2452 open_quote
= !open_quote
;
2453 if ((*in_end
== ',' && open_quote
== 0) ||
2455 int len
= in_end
- in_curr
;
2457 if (selinux_option(in_curr
, len
))
2458 take_selinux_option(&sec_curr
, in_curr
, &fsec
, len
);
2460 take_option(&nosec
, in_curr
, &fnosec
, len
);
2462 in_curr
= in_end
+ 1;
2464 } while (*in_end
++);
2466 strcpy(in_save
, nosec_save
);
2467 free_page((unsigned long)nosec_save
);
2472 static int selinux_sb_remount(struct super_block
*sb
, void *data
)
2475 struct security_mnt_opts opts
;
2476 char *secdata
, **mount_options
;
2477 struct superblock_security_struct
*sbsec
= sb
->s_security
;
2479 if (!(sbsec
->flags
& SE_SBINITIALIZED
))
2485 if (sb
->s_type
->fs_flags
& FS_BINARY_MOUNTDATA
)
2488 security_init_mnt_opts(&opts
);
2489 secdata
= alloc_secdata();
2492 rc
= selinux_sb_copy_data(data
, secdata
);
2494 goto out_free_secdata
;
2496 rc
= selinux_parse_opts_str(secdata
, &opts
);
2498 goto out_free_secdata
;
2500 mount_options
= opts
.mnt_opts
;
2501 flags
= opts
.mnt_opts_flags
;
2503 for (i
= 0; i
< opts
.num_mnt_opts
; i
++) {
2507 if (flags
[i
] == SE_SBLABELSUPP
)
2509 len
= strlen(mount_options
[i
]);
2510 rc
= security_context_to_sid(mount_options
[i
], len
, &sid
);
2512 printk(KERN_WARNING
"SELinux: security_context_to_sid"
2513 "(%s) failed for (dev %s, type %s) errno=%d\n",
2514 mount_options
[i
], sb
->s_id
, sb
->s_type
->name
, rc
);
2520 if (bad_option(sbsec
, FSCONTEXT_MNT
, sbsec
->sid
, sid
))
2521 goto out_bad_option
;
2524 if (bad_option(sbsec
, CONTEXT_MNT
, sbsec
->mntpoint_sid
, sid
))
2525 goto out_bad_option
;
2527 case ROOTCONTEXT_MNT
: {
2528 struct inode_security_struct
*root_isec
;
2529 root_isec
= sb
->s_root
->d_inode
->i_security
;
2531 if (bad_option(sbsec
, ROOTCONTEXT_MNT
, root_isec
->sid
, sid
))
2532 goto out_bad_option
;
2535 case DEFCONTEXT_MNT
:
2536 if (bad_option(sbsec
, DEFCONTEXT_MNT
, sbsec
->def_sid
, sid
))
2537 goto out_bad_option
;
2546 security_free_mnt_opts(&opts
);
2548 free_secdata(secdata
);
2551 printk(KERN_WARNING
"SELinux: unable to change security options "
2552 "during remount (dev %s, type=%s)\n", sb
->s_id
,
2557 static int selinux_sb_kern_mount(struct super_block
*sb
, int flags
, void *data
)
2559 const struct cred
*cred
= current_cred();
2560 struct common_audit_data ad
;
2563 rc
= superblock_doinit(sb
, data
);
2567 /* Allow all mounts performed by the kernel */
2568 if (flags
& MS_KERNMOUNT
)
2571 ad
.type
= LSM_AUDIT_DATA_DENTRY
;
2572 ad
.u
.dentry
= sb
->s_root
;
2573 return superblock_has_perm(cred
, sb
, FILESYSTEM__MOUNT
, &ad
);
2576 static int selinux_sb_statfs(struct dentry
*dentry
)
2578 const struct cred
*cred
= current_cred();
2579 struct common_audit_data ad
;
2581 ad
.type
= LSM_AUDIT_DATA_DENTRY
;
2582 ad
.u
.dentry
= dentry
->d_sb
->s_root
;
2583 return superblock_has_perm(cred
, dentry
->d_sb
, FILESYSTEM__GETATTR
, &ad
);
2586 static int selinux_mount(const char *dev_name
,
2589 unsigned long flags
,
2592 const struct cred
*cred
= current_cred();
2594 if (flags
& MS_REMOUNT
)
2595 return superblock_has_perm(cred
, path
->dentry
->d_sb
,
2596 FILESYSTEM__REMOUNT
, NULL
);
2598 return path_has_perm(cred
, path
, FILE__MOUNTON
);
2601 static int selinux_umount(struct vfsmount
*mnt
, int flags
)
2603 const struct cred
*cred
= current_cred();
2605 return superblock_has_perm(cred
, mnt
->mnt_sb
,
2606 FILESYSTEM__UNMOUNT
, NULL
);
2609 /* inode security operations */
2611 static int selinux_inode_alloc_security(struct inode
*inode
)
2613 return inode_alloc_security(inode
);
2616 static void selinux_inode_free_security(struct inode
*inode
)
2618 inode_free_security(inode
);
2621 static int selinux_inode_init_security(struct inode
*inode
, struct inode
*dir
,
2622 const struct qstr
*qstr
, char **name
,
2623 void **value
, size_t *len
)
2625 const struct task_security_struct
*tsec
= current_security();
2626 struct inode_security_struct
*dsec
;
2627 struct superblock_security_struct
*sbsec
;
2628 u32 sid
, newsid
, clen
;
2630 char *namep
= NULL
, *context
;
2632 dsec
= dir
->i_security
;
2633 sbsec
= dir
->i_sb
->s_security
;
2636 newsid
= tsec
->create_sid
;
2638 if ((sbsec
->flags
& SE_SBINITIALIZED
) &&
2639 (sbsec
->behavior
== SECURITY_FS_USE_MNTPOINT
))
2640 newsid
= sbsec
->mntpoint_sid
;
2641 else if (!newsid
|| !(sbsec
->flags
& SE_SBLABELSUPP
)) {
2642 rc
= security_transition_sid(sid
, dsec
->sid
,
2643 inode_mode_to_security_class(inode
->i_mode
),
2646 printk(KERN_WARNING
"%s: "
2647 "security_transition_sid failed, rc=%d (dev=%s "
2650 -rc
, inode
->i_sb
->s_id
, inode
->i_ino
);
2655 /* Possibly defer initialization to selinux_complete_init. */
2656 if (sbsec
->flags
& SE_SBINITIALIZED
) {
2657 struct inode_security_struct
*isec
= inode
->i_security
;
2658 isec
->sclass
= inode_mode_to_security_class(inode
->i_mode
);
2660 isec
->initialized
= 1;
2663 if (!ss_initialized
|| !(sbsec
->flags
& SE_SBLABELSUPP
))
2667 namep
= kstrdup(XATTR_SELINUX_SUFFIX
, GFP_NOFS
);
2674 rc
= security_sid_to_context_force(newsid
, &context
, &clen
);
2686 static int selinux_inode_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
2688 return may_create(dir
, dentry
, SECCLASS_FILE
);
2691 static int selinux_inode_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
2693 return may_link(dir
, old_dentry
, MAY_LINK
);
2696 static int selinux_inode_unlink(struct inode
*dir
, struct dentry
*dentry
)
2698 return may_link(dir
, dentry
, MAY_UNLINK
);
2701 static int selinux_inode_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *name
)
2703 return may_create(dir
, dentry
, SECCLASS_LNK_FILE
);
2706 static int selinux_inode_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mask
)
2708 return may_create(dir
, dentry
, SECCLASS_DIR
);
2711 static int selinux_inode_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2713 return may_link(dir
, dentry
, MAY_RMDIR
);
2716 static int selinux_inode_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, dev_t dev
)
2718 return may_create(dir
, dentry
, inode_mode_to_security_class(mode
));
2721 static int selinux_inode_rename(struct inode
*old_inode
, struct dentry
*old_dentry
,
2722 struct inode
*new_inode
, struct dentry
*new_dentry
)
2724 return may_rename(old_inode
, old_dentry
, new_inode
, new_dentry
);
2727 static int selinux_inode_readlink(struct dentry
*dentry
)
2729 const struct cred
*cred
= current_cred();
2731 return dentry_has_perm(cred
, dentry
, FILE__READ
);
2734 static int selinux_inode_follow_link(struct dentry
*dentry
, struct nameidata
*nameidata
)
2736 const struct cred
*cred
= current_cred();
2738 return dentry_has_perm(cred
, dentry
, FILE__READ
);
2741 static noinline
int audit_inode_permission(struct inode
*inode
,
2742 u32 perms
, u32 audited
, u32 denied
,
2746 struct common_audit_data ad
;
2747 struct inode_security_struct
*isec
= inode
->i_security
;
2750 ad
.type
= LSM_AUDIT_DATA_INODE
;
2753 rc
= slow_avc_audit(current_sid(), isec
->sid
, isec
->sclass
, perms
,
2754 audited
, denied
, result
, &ad
, flags
);
2760 static int selinux_inode_permission(struct inode
*inode
, int mask
)
2762 const struct cred
*cred
= current_cred();
2765 unsigned flags
= mask
& MAY_NOT_BLOCK
;
2766 struct inode_security_struct
*isec
;
2768 struct av_decision avd
;
2770 u32 audited
, denied
;
2772 from_access
= mask
& MAY_ACCESS
;
2773 mask
&= (MAY_READ
|MAY_WRITE
|MAY_EXEC
|MAY_APPEND
);
2775 /* No permission to check. Existence test. */
2779 validate_creds(cred
);
2781 if (unlikely(IS_PRIVATE(inode
)))
2784 perms
= file_mask_to_av(inode
->i_mode
, mask
);
2786 sid
= cred_sid(cred
);
2787 isec
= inode
->i_security
;
2789 rc
= avc_has_perm_noaudit(sid
, isec
->sid
, isec
->sclass
, perms
, 0, &avd
);
2790 audited
= avc_audit_required(perms
, &avd
, rc
,
2791 from_access
? FILE__AUDIT_ACCESS
: 0,
2793 if (likely(!audited
))
2796 rc2
= audit_inode_permission(inode
, perms
, audited
, denied
, rc
, flags
);
2802 static int selinux_inode_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
2804 const struct cred
*cred
= current_cred();
2805 unsigned int ia_valid
= iattr
->ia_valid
;
2806 __u32 av
= FILE__WRITE
;
2808 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2809 if (ia_valid
& ATTR_FORCE
) {
2810 ia_valid
&= ~(ATTR_KILL_SUID
| ATTR_KILL_SGID
| ATTR_MODE
|
2816 if (ia_valid
& (ATTR_MODE
| ATTR_UID
| ATTR_GID
|
2817 ATTR_ATIME_SET
| ATTR_MTIME_SET
| ATTR_TIMES_SET
))
2818 return dentry_has_perm(cred
, dentry
, FILE__SETATTR
);
2820 if (selinux_policycap_openperm
&& (ia_valid
& ATTR_SIZE
))
2823 return dentry_has_perm(cred
, dentry
, av
);
2826 static int selinux_inode_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
)
2828 const struct cred
*cred
= current_cred();
2831 path
.dentry
= dentry
;
2834 return path_has_perm(cred
, &path
, FILE__GETATTR
);
2837 static int selinux_inode_setotherxattr(struct dentry
*dentry
, const char *name
)
2839 const struct cred
*cred
= current_cred();
2841 if (!strncmp(name
, XATTR_SECURITY_PREFIX
,
2842 sizeof XATTR_SECURITY_PREFIX
- 1)) {
2843 if (!strcmp(name
, XATTR_NAME_CAPS
)) {
2844 if (!capable(CAP_SETFCAP
))
2846 } else if (!capable(CAP_SYS_ADMIN
)) {
2847 /* A different attribute in the security namespace.
2848 Restrict to administrator. */
2853 /* Not an attribute we recognize, so just check the
2854 ordinary setattr permission. */
2855 return dentry_has_perm(cred
, dentry
, FILE__SETATTR
);
2858 static int selinux_inode_setxattr(struct dentry
*dentry
, const char *name
,
2859 const void *value
, size_t size
, int flags
)
2861 struct inode
*inode
= dentry
->d_inode
;
2862 struct inode_security_struct
*isec
= inode
->i_security
;
2863 struct superblock_security_struct
*sbsec
;
2864 struct common_audit_data ad
;
2865 u32 newsid
, sid
= current_sid();
2868 if (strcmp(name
, XATTR_NAME_SELINUX
))
2869 return selinux_inode_setotherxattr(dentry
, name
);
2871 sbsec
= inode
->i_sb
->s_security
;
2872 if (!(sbsec
->flags
& SE_SBLABELSUPP
))
2875 if (!inode_owner_or_capable(inode
))
2878 ad
.type
= LSM_AUDIT_DATA_DENTRY
;
2879 ad
.u
.dentry
= dentry
;
2881 rc
= avc_has_perm(sid
, isec
->sid
, isec
->sclass
,
2882 FILE__RELABELFROM
, &ad
);
2886 rc
= security_context_to_sid(value
, size
, &newsid
);
2887 if (rc
== -EINVAL
) {
2888 if (!capable(CAP_MAC_ADMIN
)) {
2889 struct audit_buffer
*ab
;
2893 /* We strip a nul only if it is at the end, otherwise the
2894 * context contains a nul and we should audit that */
2897 if (str
[size
- 1] == '\0')
2898 audit_size
= size
- 1;
2905 ab
= audit_log_start(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
);
2906 audit_log_format(ab
, "op=setxattr invalid_context=");
2907 audit_log_n_untrustedstring(ab
, value
, audit_size
);
2912 rc
= security_context_to_sid_force(value
, size
, &newsid
);
2917 rc
= avc_has_perm(sid
, newsid
, isec
->sclass
,
2918 FILE__RELABELTO
, &ad
);
2922 rc
= security_validate_transition(isec
->sid
, newsid
, sid
,
2927 return avc_has_perm(newsid
,
2929 SECCLASS_FILESYSTEM
,
2930 FILESYSTEM__ASSOCIATE
,
2934 static void selinux_inode_post_setxattr(struct dentry
*dentry
, const char *name
,
2935 const void *value
, size_t size
,
2938 struct inode
*inode
= dentry
->d_inode
;
2939 struct inode_security_struct
*isec
= inode
->i_security
;
2943 if (strcmp(name
, XATTR_NAME_SELINUX
)) {
2944 /* Not an attribute we recognize, so nothing to do. */
2948 rc
= security_context_to_sid_force(value
, size
, &newsid
);
2950 printk(KERN_ERR
"SELinux: unable to map context to SID"
2951 "for (%s, %lu), rc=%d\n",
2952 inode
->i_sb
->s_id
, inode
->i_ino
, -rc
);
2960 static int selinux_inode_getxattr(struct dentry
*dentry
, const char *name
)
2962 const struct cred
*cred
= current_cred();
2964 return dentry_has_perm(cred
, dentry
, FILE__GETATTR
);
2967 static int selinux_inode_listxattr(struct dentry
*dentry
)
2969 const struct cred
*cred
= current_cred();
2971 return dentry_has_perm(cred
, dentry
, FILE__GETATTR
);
2974 static int selinux_inode_removexattr(struct dentry
*dentry
, const char *name
)
2976 if (strcmp(name
, XATTR_NAME_SELINUX
))
2977 return selinux_inode_setotherxattr(dentry
, name
);
2979 /* No one is allowed to remove a SELinux security label.
2980 You can change the label, but all data must be labeled. */
2985 * Copy the inode security context value to the user.
2987 * Permission check is handled by selinux_inode_getxattr hook.
2989 static int selinux_inode_getsecurity(const struct inode
*inode
, const char *name
, void **buffer
, bool alloc
)
2993 char *context
= NULL
;
2994 struct inode_security_struct
*isec
= inode
->i_security
;
2996 if (strcmp(name
, XATTR_SELINUX_SUFFIX
))
3000 * If the caller has CAP_MAC_ADMIN, then get the raw context
3001 * value even if it is not defined by current policy; otherwise,
3002 * use the in-core value under current policy.
3003 * Use the non-auditing forms of the permission checks since
3004 * getxattr may be called by unprivileged processes commonly
3005 * and lack of permission just means that we fall back to the
3006 * in-core context value, not a denial.
3008 error
= selinux_capable(current_cred(), &init_user_ns
, CAP_MAC_ADMIN
,
3009 SECURITY_CAP_NOAUDIT
);
3011 error
= security_sid_to_context_force(isec
->sid
, &context
,
3014 error
= security_sid_to_context(isec
->sid
, &context
, &size
);
3027 static int selinux_inode_setsecurity(struct inode
*inode
, const char *name
,
3028 const void *value
, size_t size
, int flags
)
3030 struct inode_security_struct
*isec
= inode
->i_security
;
3034 if (strcmp(name
, XATTR_SELINUX_SUFFIX
))
3037 if (!value
|| !size
)
3040 rc
= security_context_to_sid((void *)value
, size
, &newsid
);
3045 isec
->initialized
= 1;
3049 static int selinux_inode_listsecurity(struct inode
*inode
, char *buffer
, size_t buffer_size
)
3051 const int len
= sizeof(XATTR_NAME_SELINUX
);
3052 if (buffer
&& len
<= buffer_size
)
3053 memcpy(buffer
, XATTR_NAME_SELINUX
, len
);
3057 static void selinux_inode_getsecid(const struct inode
*inode
, u32
*secid
)
3059 struct inode_security_struct
*isec
= inode
->i_security
;
3063 /* file security operations */
3065 static int selinux_revalidate_file_permission(struct file
*file
, int mask
)
3067 const struct cred
*cred
= current_cred();
3068 struct inode
*inode
= file_inode(file
);
3070 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3071 if ((file
->f_flags
& O_APPEND
) && (mask
& MAY_WRITE
))
3074 return file_has_perm(cred
, file
,
3075 file_mask_to_av(inode
->i_mode
, mask
));
3078 static int selinux_file_permission(struct file
*file
, int mask
)
3080 struct inode
*inode
= file_inode(file
);
3081 struct file_security_struct
*fsec
= file
->f_security
;
3082 struct inode_security_struct
*isec
= inode
->i_security
;
3083 u32 sid
= current_sid();
3086 /* No permission to check. Existence test. */
3089 if (sid
== fsec
->sid
&& fsec
->isid
== isec
->sid
&&
3090 fsec
->pseqno
== avc_policy_seqno())
3091 /* No change since file_open check. */
3094 return selinux_revalidate_file_permission(file
, mask
);
3097 static int selinux_file_alloc_security(struct file
*file
)
3099 return file_alloc_security(file
);
3102 static void selinux_file_free_security(struct file
*file
)
3104 file_free_security(file
);
3107 static int selinux_file_ioctl(struct file
*file
, unsigned int cmd
,
3110 const struct cred
*cred
= current_cred();
3120 case FS_IOC_GETFLAGS
:
3122 case FS_IOC_GETVERSION
:
3123 error
= file_has_perm(cred
, file
, FILE__GETATTR
);
3126 case FS_IOC_SETFLAGS
:
3128 case FS_IOC_SETVERSION
:
3129 error
= file_has_perm(cred
, file
, FILE__SETATTR
);
3132 /* sys_ioctl() checks */
3136 error
= file_has_perm(cred
, file
, 0);
3141 error
= cred_has_capability(cred
, CAP_SYS_TTY_CONFIG
,
3142 SECURITY_CAP_AUDIT
);
3145 /* default case assumes that the command will go
3146 * to the file's ioctl() function.
3149 error
= file_has_perm(cred
, file
, FILE__IOCTL
);
3154 static int default_noexec
;
3156 static int file_map_prot_check(struct file
*file
, unsigned long prot
, int shared
)
3158 const struct cred
*cred
= current_cred();
3161 if (default_noexec
&&
3162 (prot
& PROT_EXEC
) && (!file
|| (!shared
&& (prot
& PROT_WRITE
)))) {
3164 * We are making executable an anonymous mapping or a
3165 * private file mapping that will also be writable.
3166 * This has an additional check.
3168 rc
= cred_has_perm(cred
, cred
, PROCESS__EXECMEM
);
3174 /* read access is always possible with a mapping */
3175 u32 av
= FILE__READ
;
3177 /* write access only matters if the mapping is shared */
3178 if (shared
&& (prot
& PROT_WRITE
))
3181 if (prot
& PROT_EXEC
)
3182 av
|= FILE__EXECUTE
;
3184 return file_has_perm(cred
, file
, av
);
3191 static int selinux_mmap_addr(unsigned long addr
)
3194 u32 sid
= current_sid();
3197 * notice that we are intentionally putting the SELinux check before
3198 * the secondary cap_file_mmap check. This is such a likely attempt
3199 * at bad behaviour/exploit that we always want to get the AVC, even
3200 * if DAC would have also denied the operation.
3202 if (addr
< CONFIG_LSM_MMAP_MIN_ADDR
) {
3203 rc
= avc_has_perm(sid
, sid
, SECCLASS_MEMPROTECT
,
3204 MEMPROTECT__MMAP_ZERO
, NULL
);
3209 /* do DAC check on address space usage */
3210 return cap_mmap_addr(addr
);
3213 static int selinux_mmap_file(struct file
*file
, unsigned long reqprot
,
3214 unsigned long prot
, unsigned long flags
)
3216 if (selinux_checkreqprot
)
3219 return file_map_prot_check(file
, prot
,
3220 (flags
& MAP_TYPE
) == MAP_SHARED
);
3223 static int selinux_file_mprotect(struct vm_area_struct
*vma
,
3224 unsigned long reqprot
,
3227 const struct cred
*cred
= current_cred();
3229 if (selinux_checkreqprot
)
3232 if (default_noexec
&&
3233 (prot
& PROT_EXEC
) && !(vma
->vm_flags
& VM_EXEC
)) {
3235 if (vma
->vm_start
>= vma
->vm_mm
->start_brk
&&
3236 vma
->vm_end
<= vma
->vm_mm
->brk
) {
3237 rc
= cred_has_perm(cred
, cred
, PROCESS__EXECHEAP
);
3238 } else if (!vma
->vm_file
&&
3239 vma
->vm_start
<= vma
->vm_mm
->start_stack
&&
3240 vma
->vm_end
>= vma
->vm_mm
->start_stack
) {
3241 rc
= current_has_perm(current
, PROCESS__EXECSTACK
);
3242 } else if (vma
->vm_file
&& vma
->anon_vma
) {
3244 * We are making executable a file mapping that has
3245 * had some COW done. Since pages might have been
3246 * written, check ability to execute the possibly
3247 * modified content. This typically should only
3248 * occur for text relocations.
3250 rc
= file_has_perm(cred
, vma
->vm_file
, FILE__EXECMOD
);
3256 return file_map_prot_check(vma
->vm_file
, prot
, vma
->vm_flags
&VM_SHARED
);
3259 static int selinux_file_lock(struct file
*file
, unsigned int cmd
)
3261 const struct cred
*cred
= current_cred();
3263 return file_has_perm(cred
, file
, FILE__LOCK
);
3266 static int selinux_file_fcntl(struct file
*file
, unsigned int cmd
,
3269 const struct cred
*cred
= current_cred();
3274 if ((file
->f_flags
& O_APPEND
) && !(arg
& O_APPEND
)) {
3275 err
= file_has_perm(cred
, file
, FILE__WRITE
);
3284 case F_GETOWNER_UIDS
:
3285 /* Just check FD__USE permission */
3286 err
= file_has_perm(cred
, file
, 0);
3291 #if BITS_PER_LONG == 32
3296 err
= file_has_perm(cred
, file
, FILE__LOCK
);
3303 static int selinux_file_set_fowner(struct file
*file
)
3305 struct file_security_struct
*fsec
;
3307 fsec
= file
->f_security
;
3308 fsec
->fown_sid
= current_sid();
3313 static int selinux_file_send_sigiotask(struct task_struct
*tsk
,
3314 struct fown_struct
*fown
, int signum
)
3317 u32 sid
= task_sid(tsk
);
3319 struct file_security_struct
*fsec
;
3321 /* struct fown_struct is never outside the context of a struct file */
3322 file
= container_of(fown
, struct file
, f_owner
);
3324 fsec
= file
->f_security
;
3327 perm
= signal_to_av(SIGIO
); /* as per send_sigio_to_task */
3329 perm
= signal_to_av(signum
);
3331 return avc_has_perm(fsec
->fown_sid
, sid
,
3332 SECCLASS_PROCESS
, perm
, NULL
);
3335 static int selinux_file_receive(struct file
*file
)
3337 const struct cred
*cred
= current_cred();
3339 return file_has_perm(cred
, file
, file_to_av(file
));
3342 static int selinux_file_open(struct file
*file
, const struct cred
*cred
)
3344 struct file_security_struct
*fsec
;
3345 struct inode_security_struct
*isec
;
3347 fsec
= file
->f_security
;
3348 isec
= file_inode(file
)->i_security
;
3350 * Save inode label and policy sequence number
3351 * at open-time so that selinux_file_permission
3352 * can determine whether revalidation is necessary.
3353 * Task label is already saved in the file security
3354 * struct as its SID.
3356 fsec
->isid
= isec
->sid
;
3357 fsec
->pseqno
= avc_policy_seqno();
3359 * Since the inode label or policy seqno may have changed
3360 * between the selinux_inode_permission check and the saving
3361 * of state above, recheck that access is still permitted.
3362 * Otherwise, access might never be revalidated against the
3363 * new inode label or new policy.
3364 * This check is not redundant - do not remove.
3366 return path_has_perm(cred
, &file
->f_path
, open_file_to_av(file
));
3369 /* task security operations */
3371 static int selinux_task_create(unsigned long clone_flags
)
3373 return current_has_perm(current
, PROCESS__FORK
);
3377 * allocate the SELinux part of blank credentials
3379 static int selinux_cred_alloc_blank(struct cred
*cred
, gfp_t gfp
)
3381 struct task_security_struct
*tsec
;
3383 tsec
= kzalloc(sizeof(struct task_security_struct
), gfp
);
3387 cred
->security
= tsec
;
3392 * detach and free the LSM part of a set of credentials
3394 static void selinux_cred_free(struct cred
*cred
)
3396 struct task_security_struct
*tsec
= cred
->security
;
3399 * cred->security == NULL if security_cred_alloc_blank() or
3400 * security_prepare_creds() returned an error.
3402 BUG_ON(cred
->security
&& (unsigned long) cred
->security
< PAGE_SIZE
);
3403 cred
->security
= (void *) 0x7UL
;
3408 * prepare a new set of credentials for modification
3410 static int selinux_cred_prepare(struct cred
*new, const struct cred
*old
,
3413 const struct task_security_struct
*old_tsec
;
3414 struct task_security_struct
*tsec
;
3416 old_tsec
= old
->security
;
3418 tsec
= kmemdup(old_tsec
, sizeof(struct task_security_struct
), gfp
);
3422 new->security
= tsec
;
3427 * transfer the SELinux data to a blank set of creds
3429 static void selinux_cred_transfer(struct cred
*new, const struct cred
*old
)
3431 const struct task_security_struct
*old_tsec
= old
->security
;
3432 struct task_security_struct
*tsec
= new->security
;
3438 * set the security data for a kernel service
3439 * - all the creation contexts are set to unlabelled
3441 static int selinux_kernel_act_as(struct cred
*new, u32 secid
)
3443 struct task_security_struct
*tsec
= new->security
;
3444 u32 sid
= current_sid();
3447 ret
= avc_has_perm(sid
, secid
,
3448 SECCLASS_KERNEL_SERVICE
,
3449 KERNEL_SERVICE__USE_AS_OVERRIDE
,
3453 tsec
->create_sid
= 0;
3454 tsec
->keycreate_sid
= 0;
3455 tsec
->sockcreate_sid
= 0;
3461 * set the file creation context in a security record to the same as the
3462 * objective context of the specified inode
3464 static int selinux_kernel_create_files_as(struct cred
*new, struct inode
*inode
)
3466 struct inode_security_struct
*isec
= inode
->i_security
;
3467 struct task_security_struct
*tsec
= new->security
;
3468 u32 sid
= current_sid();
3471 ret
= avc_has_perm(sid
, isec
->sid
,
3472 SECCLASS_KERNEL_SERVICE
,
3473 KERNEL_SERVICE__CREATE_FILES_AS
,
3477 tsec
->create_sid
= isec
->sid
;
3481 static int selinux_kernel_module_request(char *kmod_name
)
3484 struct common_audit_data ad
;
3486 sid
= task_sid(current
);
3488 ad
.type
= LSM_AUDIT_DATA_KMOD
;
3489 ad
.u
.kmod_name
= kmod_name
;
3491 return avc_has_perm(sid
, SECINITSID_KERNEL
, SECCLASS_SYSTEM
,
3492 SYSTEM__MODULE_REQUEST
, &ad
);
3495 static int selinux_task_setpgid(struct task_struct
*p
, pid_t pgid
)
3497 return current_has_perm(p
, PROCESS__SETPGID
);
3500 static int selinux_task_getpgid(struct task_struct
*p
)
3502 return current_has_perm(p
, PROCESS__GETPGID
);
3505 static int selinux_task_getsid(struct task_struct
*p
)
3507 return current_has_perm(p
, PROCESS__GETSESSION
);
3510 static void selinux_task_getsecid(struct task_struct
*p
, u32
*secid
)
3512 *secid
= task_sid(p
);
3515 static int selinux_task_setnice(struct task_struct
*p
, int nice
)
3519 rc
= cap_task_setnice(p
, nice
);
3523 return current_has_perm(p
, PROCESS__SETSCHED
);
3526 static int selinux_task_setioprio(struct task_struct
*p
, int ioprio
)
3530 rc
= cap_task_setioprio(p
, ioprio
);
3534 return current_has_perm(p
, PROCESS__SETSCHED
);
3537 static int selinux_task_getioprio(struct task_struct
*p
)
3539 return current_has_perm(p
, PROCESS__GETSCHED
);
3542 static int selinux_task_setrlimit(struct task_struct
*p
, unsigned int resource
,
3543 struct rlimit
*new_rlim
)
3545 struct rlimit
*old_rlim
= p
->signal
->rlim
+ resource
;
3547 /* Control the ability to change the hard limit (whether
3548 lowering or raising it), so that the hard limit can
3549 later be used as a safe reset point for the soft limit
3550 upon context transitions. See selinux_bprm_committing_creds. */
3551 if (old_rlim
->rlim_max
!= new_rlim
->rlim_max
)
3552 return current_has_perm(p
, PROCESS__SETRLIMIT
);
3557 static int selinux_task_setscheduler(struct task_struct
*p
)
3561 rc
= cap_task_setscheduler(p
);
3565 return current_has_perm(p
, PROCESS__SETSCHED
);
3568 static int selinux_task_getscheduler(struct task_struct
*p
)
3570 return current_has_perm(p
, PROCESS__GETSCHED
);
3573 static int selinux_task_movememory(struct task_struct
*p
)
3575 return current_has_perm(p
, PROCESS__SETSCHED
);
3578 static int selinux_task_kill(struct task_struct
*p
, struct siginfo
*info
,
3585 perm
= PROCESS__SIGNULL
; /* null signal; existence test */
3587 perm
= signal_to_av(sig
);
3589 rc
= avc_has_perm(secid
, task_sid(p
),
3590 SECCLASS_PROCESS
, perm
, NULL
);
3592 rc
= current_has_perm(p
, perm
);
3596 static int selinux_task_wait(struct task_struct
*p
)
3598 return task_has_perm(p
, current
, PROCESS__SIGCHLD
);
3601 static void selinux_task_to_inode(struct task_struct
*p
,
3602 struct inode
*inode
)
3604 struct inode_security_struct
*isec
= inode
->i_security
;
3605 u32 sid
= task_sid(p
);
3608 isec
->initialized
= 1;
3611 /* Returns error only if unable to parse addresses */
3612 static int selinux_parse_skb_ipv4(struct sk_buff
*skb
,
3613 struct common_audit_data
*ad
, u8
*proto
)
3615 int offset
, ihlen
, ret
= -EINVAL
;
3616 struct iphdr _iph
, *ih
;
3618 offset
= skb_network_offset(skb
);
3619 ih
= skb_header_pointer(skb
, offset
, sizeof(_iph
), &_iph
);
3623 ihlen
= ih
->ihl
* 4;
3624 if (ihlen
< sizeof(_iph
))
3627 ad
->u
.net
->v4info
.saddr
= ih
->saddr
;
3628 ad
->u
.net
->v4info
.daddr
= ih
->daddr
;
3632 *proto
= ih
->protocol
;
3634 switch (ih
->protocol
) {
3636 struct tcphdr _tcph
, *th
;
3638 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
3642 th
= skb_header_pointer(skb
, offset
, sizeof(_tcph
), &_tcph
);
3646 ad
->u
.net
->sport
= th
->source
;
3647 ad
->u
.net
->dport
= th
->dest
;
3652 struct udphdr _udph
, *uh
;
3654 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
3658 uh
= skb_header_pointer(skb
, offset
, sizeof(_udph
), &_udph
);
3662 ad
->u
.net
->sport
= uh
->source
;
3663 ad
->u
.net
->dport
= uh
->dest
;
3667 case IPPROTO_DCCP
: {
3668 struct dccp_hdr _dccph
, *dh
;
3670 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
3674 dh
= skb_header_pointer(skb
, offset
, sizeof(_dccph
), &_dccph
);
3678 ad
->u
.net
->sport
= dh
->dccph_sport
;
3679 ad
->u
.net
->dport
= dh
->dccph_dport
;
3690 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3692 /* Returns error only if unable to parse addresses */
3693 static int selinux_parse_skb_ipv6(struct sk_buff
*skb
,
3694 struct common_audit_data
*ad
, u8
*proto
)
3697 int ret
= -EINVAL
, offset
;
3698 struct ipv6hdr _ipv6h
, *ip6
;
3701 offset
= skb_network_offset(skb
);
3702 ip6
= skb_header_pointer(skb
, offset
, sizeof(_ipv6h
), &_ipv6h
);
3706 ad
->u
.net
->v6info
.saddr
= ip6
->saddr
;
3707 ad
->u
.net
->v6info
.daddr
= ip6
->daddr
;
3710 nexthdr
= ip6
->nexthdr
;
3711 offset
+= sizeof(_ipv6h
);
3712 offset
= ipv6_skip_exthdr(skb
, offset
, &nexthdr
, &frag_off
);
3721 struct tcphdr _tcph
, *th
;
3723 th
= skb_header_pointer(skb
, offset
, sizeof(_tcph
), &_tcph
);
3727 ad
->u
.net
->sport
= th
->source
;
3728 ad
->u
.net
->dport
= th
->dest
;
3733 struct udphdr _udph
, *uh
;
3735 uh
= skb_header_pointer(skb
, offset
, sizeof(_udph
), &_udph
);
3739 ad
->u
.net
->sport
= uh
->source
;
3740 ad
->u
.net
->dport
= uh
->dest
;
3744 case IPPROTO_DCCP
: {
3745 struct dccp_hdr _dccph
, *dh
;
3747 dh
= skb_header_pointer(skb
, offset
, sizeof(_dccph
), &_dccph
);
3751 ad
->u
.net
->sport
= dh
->dccph_sport
;
3752 ad
->u
.net
->dport
= dh
->dccph_dport
;
3756 /* includes fragments */
3766 static int selinux_parse_skb(struct sk_buff
*skb
, struct common_audit_data
*ad
,
3767 char **_addrp
, int src
, u8
*proto
)
3772 switch (ad
->u
.net
->family
) {
3774 ret
= selinux_parse_skb_ipv4(skb
, ad
, proto
);
3777 addrp
= (char *)(src
? &ad
->u
.net
->v4info
.saddr
:
3778 &ad
->u
.net
->v4info
.daddr
);
3781 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3783 ret
= selinux_parse_skb_ipv6(skb
, ad
, proto
);
3786 addrp
= (char *)(src
? &ad
->u
.net
->v6info
.saddr
:
3787 &ad
->u
.net
->v6info
.daddr
);
3797 "SELinux: failure in selinux_parse_skb(),"
3798 " unable to parse packet\n");
3808 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
3810 * @family: protocol family
3811 * @sid: the packet's peer label SID
3814 * Check the various different forms of network peer labeling and determine
3815 * the peer label/SID for the packet; most of the magic actually occurs in
3816 * the security server function security_net_peersid_cmp(). The function
3817 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3818 * or -EACCES if @sid is invalid due to inconsistencies with the different
3822 static int selinux_skb_peerlbl_sid(struct sk_buff
*skb
, u16 family
, u32
*sid
)
3829 selinux_xfrm_skb_sid(skb
, &xfrm_sid
);
3830 selinux_netlbl_skbuff_getsid(skb
, family
, &nlbl_type
, &nlbl_sid
);
3832 err
= security_net_peersid_resolve(nlbl_sid
, nlbl_type
, xfrm_sid
, sid
);
3833 if (unlikely(err
)) {
3835 "SELinux: failure in selinux_skb_peerlbl_sid(),"
3836 " unable to determine packet's peer label\n");
3844 * selinux_conn_sid - Determine the child socket label for a connection
3845 * @sk_sid: the parent socket's SID
3846 * @skb_sid: the packet's SID
3847 * @conn_sid: the resulting connection SID
3849 * If @skb_sid is valid then the user:role:type information from @sk_sid is
3850 * combined with the MLS information from @skb_sid in order to create
3851 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
3852 * of @sk_sid. Returns zero on success, negative values on failure.
3855 static int selinux_conn_sid(u32 sk_sid
, u32 skb_sid
, u32
*conn_sid
)
3859 if (skb_sid
!= SECSID_NULL
)
3860 err
= security_sid_mls_copy(sk_sid
, skb_sid
, conn_sid
);
3867 /* socket security operations */
3869 static int socket_sockcreate_sid(const struct task_security_struct
*tsec
,
3870 u16 secclass
, u32
*socksid
)
3872 if (tsec
->sockcreate_sid
> SECSID_NULL
) {
3873 *socksid
= tsec
->sockcreate_sid
;
3877 return security_transition_sid(tsec
->sid
, tsec
->sid
, secclass
, NULL
,
3881 static int sock_has_perm(struct task_struct
*task
, struct sock
*sk
, u32 perms
)
3883 struct sk_security_struct
*sksec
= sk
->sk_security
;
3884 struct common_audit_data ad
;
3885 struct lsm_network_audit net
= {0,};
3886 u32 tsid
= task_sid(task
);
3888 if (sksec
->sid
== SECINITSID_KERNEL
)
3891 ad
.type
= LSM_AUDIT_DATA_NET
;
3895 return avc_has_perm(tsid
, sksec
->sid
, sksec
->sclass
, perms
, &ad
);
3898 static int selinux_socket_create(int family
, int type
,
3899 int protocol
, int kern
)
3901 const struct task_security_struct
*tsec
= current_security();
3909 secclass
= socket_type_to_security_class(family
, type
, protocol
);
3910 rc
= socket_sockcreate_sid(tsec
, secclass
, &newsid
);
3914 return avc_has_perm(tsec
->sid
, newsid
, secclass
, SOCKET__CREATE
, NULL
);
3917 static int selinux_socket_post_create(struct socket
*sock
, int family
,
3918 int type
, int protocol
, int kern
)
3920 const struct task_security_struct
*tsec
= current_security();
3921 struct inode_security_struct
*isec
= SOCK_INODE(sock
)->i_security
;
3922 struct sk_security_struct
*sksec
;
3925 isec
->sclass
= socket_type_to_security_class(family
, type
, protocol
);
3928 isec
->sid
= SECINITSID_KERNEL
;
3930 err
= socket_sockcreate_sid(tsec
, isec
->sclass
, &(isec
->sid
));
3935 isec
->initialized
= 1;
3938 sksec
= sock
->sk
->sk_security
;
3939 sksec
->sid
= isec
->sid
;
3940 sksec
->sclass
= isec
->sclass
;
3941 err
= selinux_netlbl_socket_post_create(sock
->sk
, family
);
3947 /* Range of port numbers used to automatically bind.
3948 Need to determine whether we should perform a name_bind
3949 permission check between the socket and the port number. */
3951 static int selinux_socket_bind(struct socket
*sock
, struct sockaddr
*address
, int addrlen
)
3953 struct sock
*sk
= sock
->sk
;
3957 err
= sock_has_perm(current
, sk
, SOCKET__BIND
);
3962 * If PF_INET or PF_INET6, check name_bind permission for the port.
3963 * Multiple address binding for SCTP is not supported yet: we just
3964 * check the first address now.
3966 family
= sk
->sk_family
;
3967 if (family
== PF_INET
|| family
== PF_INET6
) {
3969 struct sk_security_struct
*sksec
= sk
->sk_security
;
3970 struct common_audit_data ad
;
3971 struct lsm_network_audit net
= {0,};
3972 struct sockaddr_in
*addr4
= NULL
;
3973 struct sockaddr_in6
*addr6
= NULL
;
3974 unsigned short snum
;
3977 if (family
== PF_INET
) {
3978 addr4
= (struct sockaddr_in
*)address
;
3979 snum
= ntohs(addr4
->sin_port
);
3980 addrp
= (char *)&addr4
->sin_addr
.s_addr
;
3982 addr6
= (struct sockaddr_in6
*)address
;
3983 snum
= ntohs(addr6
->sin6_port
);
3984 addrp
= (char *)&addr6
->sin6_addr
.s6_addr
;
3990 inet_get_local_port_range(&low
, &high
);
3992 if (snum
< max(PROT_SOCK
, low
) || snum
> high
) {
3993 err
= sel_netport_sid(sk
->sk_protocol
,
3997 ad
.type
= LSM_AUDIT_DATA_NET
;
3999 ad
.u
.net
->sport
= htons(snum
);
4000 ad
.u
.net
->family
= family
;
4001 err
= avc_has_perm(sksec
->sid
, sid
,
4003 SOCKET__NAME_BIND
, &ad
);
4009 switch (sksec
->sclass
) {
4010 case SECCLASS_TCP_SOCKET
:
4011 node_perm
= TCP_SOCKET__NODE_BIND
;
4014 case SECCLASS_UDP_SOCKET
:
4015 node_perm
= UDP_SOCKET__NODE_BIND
;
4018 case SECCLASS_DCCP_SOCKET
:
4019 node_perm
= DCCP_SOCKET__NODE_BIND
;
4023 node_perm
= RAWIP_SOCKET__NODE_BIND
;
4027 err
= sel_netnode_sid(addrp
, family
, &sid
);
4031 ad
.type
= LSM_AUDIT_DATA_NET
;
4033 ad
.u
.net
->sport
= htons(snum
);
4034 ad
.u
.net
->family
= family
;
4036 if (family
== PF_INET
)
4037 ad
.u
.net
->v4info
.saddr
= addr4
->sin_addr
.s_addr
;
4039 ad
.u
.net
->v6info
.saddr
= addr6
->sin6_addr
;
4041 err
= avc_has_perm(sksec
->sid
, sid
,
4042 sksec
->sclass
, node_perm
, &ad
);
4050 static int selinux_socket_connect(struct socket
*sock
, struct sockaddr
*address
, int addrlen
)
4052 struct sock
*sk
= sock
->sk
;
4053 struct sk_security_struct
*sksec
= sk
->sk_security
;
4056 err
= sock_has_perm(current
, sk
, SOCKET__CONNECT
);
4061 * If a TCP or DCCP socket, check name_connect permission for the port.
4063 if (sksec
->sclass
== SECCLASS_TCP_SOCKET
||
4064 sksec
->sclass
== SECCLASS_DCCP_SOCKET
) {
4065 struct common_audit_data ad
;
4066 struct lsm_network_audit net
= {0,};
4067 struct sockaddr_in
*addr4
= NULL
;
4068 struct sockaddr_in6
*addr6
= NULL
;
4069 unsigned short snum
;
4072 if (sk
->sk_family
== PF_INET
) {
4073 addr4
= (struct sockaddr_in
*)address
;
4074 if (addrlen
< sizeof(struct sockaddr_in
))
4076 snum
= ntohs(addr4
->sin_port
);
4078 addr6
= (struct sockaddr_in6
*)address
;
4079 if (addrlen
< SIN6_LEN_RFC2133
)
4081 snum
= ntohs(addr6
->sin6_port
);
4084 err
= sel_netport_sid(sk
->sk_protocol
, snum
, &sid
);
4088 perm
= (sksec
->sclass
== SECCLASS_TCP_SOCKET
) ?
4089 TCP_SOCKET__NAME_CONNECT
: DCCP_SOCKET__NAME_CONNECT
;
4091 ad
.type
= LSM_AUDIT_DATA_NET
;
4093 ad
.u
.net
->dport
= htons(snum
);
4094 ad
.u
.net
->family
= sk
->sk_family
;
4095 err
= avc_has_perm(sksec
->sid
, sid
, sksec
->sclass
, perm
, &ad
);
4100 err
= selinux_netlbl_socket_connect(sk
, address
);
4106 static int selinux_socket_listen(struct socket
*sock
, int backlog
)
4108 return sock_has_perm(current
, sock
->sk
, SOCKET__LISTEN
);
4111 static int selinux_socket_accept(struct socket
*sock
, struct socket
*newsock
)
4114 struct inode_security_struct
*isec
;
4115 struct inode_security_struct
*newisec
;
4117 err
= sock_has_perm(current
, sock
->sk
, SOCKET__ACCEPT
);
4121 newisec
= SOCK_INODE(newsock
)->i_security
;
4123 isec
= SOCK_INODE(sock
)->i_security
;
4124 newisec
->sclass
= isec
->sclass
;
4125 newisec
->sid
= isec
->sid
;
4126 newisec
->initialized
= 1;
4131 static int selinux_socket_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
4134 return sock_has_perm(current
, sock
->sk
, SOCKET__WRITE
);
4137 static int selinux_socket_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
4138 int size
, int flags
)
4140 return sock_has_perm(current
, sock
->sk
, SOCKET__READ
);
4143 static int selinux_socket_getsockname(struct socket
*sock
)
4145 return sock_has_perm(current
, sock
->sk
, SOCKET__GETATTR
);
4148 static int selinux_socket_getpeername(struct socket
*sock
)
4150 return sock_has_perm(current
, sock
->sk
, SOCKET__GETATTR
);
4153 static int selinux_socket_setsockopt(struct socket
*sock
, int level
, int optname
)
4157 err
= sock_has_perm(current
, sock
->sk
, SOCKET__SETOPT
);
4161 return selinux_netlbl_socket_setsockopt(sock
, level
, optname
);
4164 static int selinux_socket_getsockopt(struct socket
*sock
, int level
,
4167 return sock_has_perm(current
, sock
->sk
, SOCKET__GETOPT
);
4170 static int selinux_socket_shutdown(struct socket
*sock
, int how
)
4172 return sock_has_perm(current
, sock
->sk
, SOCKET__SHUTDOWN
);
4175 static int selinux_socket_unix_stream_connect(struct sock
*sock
,
4179 struct sk_security_struct
*sksec_sock
= sock
->sk_security
;
4180 struct sk_security_struct
*sksec_other
= other
->sk_security
;
4181 struct sk_security_struct
*sksec_new
= newsk
->sk_security
;
4182 struct common_audit_data ad
;
4183 struct lsm_network_audit net
= {0,};
4186 ad
.type
= LSM_AUDIT_DATA_NET
;
4188 ad
.u
.net
->sk
= other
;
4190 err
= avc_has_perm(sksec_sock
->sid
, sksec_other
->sid
,
4191 sksec_other
->sclass
,
4192 UNIX_STREAM_SOCKET__CONNECTTO
, &ad
);
4196 /* server child socket */
4197 sksec_new
->peer_sid
= sksec_sock
->sid
;
4198 err
= security_sid_mls_copy(sksec_other
->sid
, sksec_sock
->sid
,
4203 /* connecting socket */
4204 sksec_sock
->peer_sid
= sksec_new
->sid
;
4209 static int selinux_socket_unix_may_send(struct socket
*sock
,
4210 struct socket
*other
)
4212 struct sk_security_struct
*ssec
= sock
->sk
->sk_security
;
4213 struct sk_security_struct
*osec
= other
->sk
->sk_security
;
4214 struct common_audit_data ad
;
4215 struct lsm_network_audit net
= {0,};
4217 ad
.type
= LSM_AUDIT_DATA_NET
;
4219 ad
.u
.net
->sk
= other
->sk
;
4221 return avc_has_perm(ssec
->sid
, osec
->sid
, osec
->sclass
, SOCKET__SENDTO
,
4225 static int selinux_inet_sys_rcv_skb(int ifindex
, char *addrp
, u16 family
,
4227 struct common_audit_data
*ad
)
4233 err
= sel_netif_sid(ifindex
, &if_sid
);
4236 err
= avc_has_perm(peer_sid
, if_sid
,
4237 SECCLASS_NETIF
, NETIF__INGRESS
, ad
);
4241 err
= sel_netnode_sid(addrp
, family
, &node_sid
);
4244 return avc_has_perm(peer_sid
, node_sid
,
4245 SECCLASS_NODE
, NODE__RECVFROM
, ad
);
4248 static int selinux_sock_rcv_skb_compat(struct sock
*sk
, struct sk_buff
*skb
,
4252 struct sk_security_struct
*sksec
= sk
->sk_security
;
4253 u32 sk_sid
= sksec
->sid
;
4254 struct common_audit_data ad
;
4255 struct lsm_network_audit net
= {0,};
4258 ad
.type
= LSM_AUDIT_DATA_NET
;
4260 ad
.u
.net
->netif
= skb
->skb_iif
;
4261 ad
.u
.net
->family
= family
;
4262 err
= selinux_parse_skb(skb
, &ad
, &addrp
, 1, NULL
);
4266 if (selinux_secmark_enabled()) {
4267 err
= avc_has_perm(sk_sid
, skb
->secmark
, SECCLASS_PACKET
,
4273 err
= selinux_netlbl_sock_rcv_skb(sksec
, skb
, family
, &ad
);
4276 err
= selinux_xfrm_sock_rcv_skb(sksec
->sid
, skb
, &ad
);
4281 static int selinux_socket_sock_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
4284 struct sk_security_struct
*sksec
= sk
->sk_security
;
4285 u16 family
= sk
->sk_family
;
4286 u32 sk_sid
= sksec
->sid
;
4287 struct common_audit_data ad
;
4288 struct lsm_network_audit net
= {0,};
4293 if (family
!= PF_INET
&& family
!= PF_INET6
)
4296 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
4297 if (family
== PF_INET6
&& skb
->protocol
== htons(ETH_P_IP
))
4300 /* If any sort of compatibility mode is enabled then handoff processing
4301 * to the selinux_sock_rcv_skb_compat() function to deal with the
4302 * special handling. We do this in an attempt to keep this function
4303 * as fast and as clean as possible. */
4304 if (!selinux_policycap_netpeer
)
4305 return selinux_sock_rcv_skb_compat(sk
, skb
, family
);
4307 secmark_active
= selinux_secmark_enabled();
4308 peerlbl_active
= netlbl_enabled() || selinux_xfrm_enabled();
4309 if (!secmark_active
&& !peerlbl_active
)
4312 ad
.type
= LSM_AUDIT_DATA_NET
;
4314 ad
.u
.net
->netif
= skb
->skb_iif
;
4315 ad
.u
.net
->family
= family
;
4316 err
= selinux_parse_skb(skb
, &ad
, &addrp
, 1, NULL
);
4320 if (peerlbl_active
) {
4323 err
= selinux_skb_peerlbl_sid(skb
, family
, &peer_sid
);
4326 err
= selinux_inet_sys_rcv_skb(skb
->skb_iif
, addrp
, family
,
4329 selinux_netlbl_err(skb
, err
, 0);
4332 err
= avc_has_perm(sk_sid
, peer_sid
, SECCLASS_PEER
,
4335 selinux_netlbl_err(skb
, err
, 0);
4340 if (secmark_active
) {
4341 err
= avc_has_perm(sk_sid
, skb
->secmark
, SECCLASS_PACKET
,
4350 static int selinux_socket_getpeersec_stream(struct socket
*sock
, char __user
*optval
,
4351 int __user
*optlen
, unsigned len
)
4356 struct sk_security_struct
*sksec
= sock
->sk
->sk_security
;
4357 u32 peer_sid
= SECSID_NULL
;
4359 if (sksec
->sclass
== SECCLASS_UNIX_STREAM_SOCKET
||
4360 sksec
->sclass
== SECCLASS_TCP_SOCKET
)
4361 peer_sid
= sksec
->peer_sid
;
4362 if (peer_sid
== SECSID_NULL
)
4363 return -ENOPROTOOPT
;
4365 err
= security_sid_to_context(peer_sid
, &scontext
, &scontext_len
);
4369 if (scontext_len
> len
) {
4374 if (copy_to_user(optval
, scontext
, scontext_len
))
4378 if (put_user(scontext_len
, optlen
))
4384 static int selinux_socket_getpeersec_dgram(struct socket
*sock
, struct sk_buff
*skb
, u32
*secid
)
4386 u32 peer_secid
= SECSID_NULL
;
4389 if (skb
&& skb
->protocol
== htons(ETH_P_IP
))
4391 else if (skb
&& skb
->protocol
== htons(ETH_P_IPV6
))
4394 family
= sock
->sk
->sk_family
;
4398 if (sock
&& family
== PF_UNIX
)
4399 selinux_inode_getsecid(SOCK_INODE(sock
), &peer_secid
);
4401 selinux_skb_peerlbl_sid(skb
, family
, &peer_secid
);
4404 *secid
= peer_secid
;
4405 if (peer_secid
== SECSID_NULL
)
4410 static int selinux_sk_alloc_security(struct sock
*sk
, int family
, gfp_t priority
)
4412 struct sk_security_struct
*sksec
;
4414 sksec
= kzalloc(sizeof(*sksec
), priority
);
4418 sksec
->peer_sid
= SECINITSID_UNLABELED
;
4419 sksec
->sid
= SECINITSID_UNLABELED
;
4420 selinux_netlbl_sk_security_reset(sksec
);
4421 sk
->sk_security
= sksec
;
4426 static void selinux_sk_free_security(struct sock
*sk
)
4428 struct sk_security_struct
*sksec
= sk
->sk_security
;
4430 sk
->sk_security
= NULL
;
4431 selinux_netlbl_sk_security_free(sksec
);
4435 static void selinux_sk_clone_security(const struct sock
*sk
, struct sock
*newsk
)
4437 struct sk_security_struct
*sksec
= sk
->sk_security
;
4438 struct sk_security_struct
*newsksec
= newsk
->sk_security
;
4440 newsksec
->sid
= sksec
->sid
;
4441 newsksec
->peer_sid
= sksec
->peer_sid
;
4442 newsksec
->sclass
= sksec
->sclass
;
4444 selinux_netlbl_sk_security_reset(newsksec
);
4447 static void selinux_sk_getsecid(struct sock
*sk
, u32
*secid
)
4450 *secid
= SECINITSID_ANY_SOCKET
;
4452 struct sk_security_struct
*sksec
= sk
->sk_security
;
4454 *secid
= sksec
->sid
;
4458 static void selinux_sock_graft(struct sock
*sk
, struct socket
*parent
)
4460 struct inode_security_struct
*isec
= SOCK_INODE(parent
)->i_security
;
4461 struct sk_security_struct
*sksec
= sk
->sk_security
;
4463 if (sk
->sk_family
== PF_INET
|| sk
->sk_family
== PF_INET6
||
4464 sk
->sk_family
== PF_UNIX
)
4465 isec
->sid
= sksec
->sid
;
4466 sksec
->sclass
= isec
->sclass
;
4469 static int selinux_inet_conn_request(struct sock
*sk
, struct sk_buff
*skb
,
4470 struct request_sock
*req
)
4472 struct sk_security_struct
*sksec
= sk
->sk_security
;
4474 u16 family
= sk
->sk_family
;
4478 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4479 if (family
== PF_INET6
&& skb
->protocol
== htons(ETH_P_IP
))
4482 err
= selinux_skb_peerlbl_sid(skb
, family
, &peersid
);
4485 err
= selinux_conn_sid(sksec
->sid
, peersid
, &connsid
);
4488 req
->secid
= connsid
;
4489 req
->peer_secid
= peersid
;
4491 return selinux_netlbl_inet_conn_request(req
, family
);
4494 static void selinux_inet_csk_clone(struct sock
*newsk
,
4495 const struct request_sock
*req
)
4497 struct sk_security_struct
*newsksec
= newsk
->sk_security
;
4499 newsksec
->sid
= req
->secid
;
4500 newsksec
->peer_sid
= req
->peer_secid
;
4501 /* NOTE: Ideally, we should also get the isec->sid for the
4502 new socket in sync, but we don't have the isec available yet.
4503 So we will wait until sock_graft to do it, by which
4504 time it will have been created and available. */
4506 /* We don't need to take any sort of lock here as we are the only
4507 * thread with access to newsksec */
4508 selinux_netlbl_inet_csk_clone(newsk
, req
->rsk_ops
->family
);
4511 static void selinux_inet_conn_established(struct sock
*sk
, struct sk_buff
*skb
)
4513 u16 family
= sk
->sk_family
;
4514 struct sk_security_struct
*sksec
= sk
->sk_security
;
4516 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4517 if (family
== PF_INET6
&& skb
->protocol
== htons(ETH_P_IP
))
4520 selinux_skb_peerlbl_sid(skb
, family
, &sksec
->peer_sid
);
4523 static void selinux_skb_owned_by(struct sk_buff
*skb
, struct sock
*sk
)
4525 skb_set_owner_w(skb
, sk
);
4528 static int selinux_secmark_relabel_packet(u32 sid
)
4530 const struct task_security_struct
*__tsec
;
4533 __tsec
= current_security();
4536 return avc_has_perm(tsid
, sid
, SECCLASS_PACKET
, PACKET__RELABELTO
, NULL
);
4539 static void selinux_secmark_refcount_inc(void)
4541 atomic_inc(&selinux_secmark_refcount
);
4544 static void selinux_secmark_refcount_dec(void)
4546 atomic_dec(&selinux_secmark_refcount
);
4549 static void selinux_req_classify_flow(const struct request_sock
*req
,
4552 fl
->flowi_secid
= req
->secid
;
4555 static int selinux_tun_dev_alloc_security(void **security
)
4557 struct tun_security_struct
*tunsec
;
4559 tunsec
= kzalloc(sizeof(*tunsec
), GFP_KERNEL
);
4562 tunsec
->sid
= current_sid();
4568 static void selinux_tun_dev_free_security(void *security
)
4573 static int selinux_tun_dev_create(void)
4575 u32 sid
= current_sid();
4577 /* we aren't taking into account the "sockcreate" SID since the socket
4578 * that is being created here is not a socket in the traditional sense,
4579 * instead it is a private sock, accessible only to the kernel, and
4580 * representing a wide range of network traffic spanning multiple
4581 * connections unlike traditional sockets - check the TUN driver to
4582 * get a better understanding of why this socket is special */
4584 return avc_has_perm(sid
, sid
, SECCLASS_TUN_SOCKET
, TUN_SOCKET__CREATE
,
4588 static int selinux_tun_dev_attach_queue(void *security
)
4590 struct tun_security_struct
*tunsec
= security
;
4592 return avc_has_perm(current_sid(), tunsec
->sid
, SECCLASS_TUN_SOCKET
,
4593 TUN_SOCKET__ATTACH_QUEUE
, NULL
);
4596 static int selinux_tun_dev_attach(struct sock
*sk
, void *security
)
4598 struct tun_security_struct
*tunsec
= security
;
4599 struct sk_security_struct
*sksec
= sk
->sk_security
;
4601 /* we don't currently perform any NetLabel based labeling here and it
4602 * isn't clear that we would want to do so anyway; while we could apply
4603 * labeling without the support of the TUN user the resulting labeled
4604 * traffic from the other end of the connection would almost certainly
4605 * cause confusion to the TUN user that had no idea network labeling
4606 * protocols were being used */
4608 sksec
->sid
= tunsec
->sid
;
4609 sksec
->sclass
= SECCLASS_TUN_SOCKET
;
4614 static int selinux_tun_dev_open(void *security
)
4616 struct tun_security_struct
*tunsec
= security
;
4617 u32 sid
= current_sid();
4620 err
= avc_has_perm(sid
, tunsec
->sid
, SECCLASS_TUN_SOCKET
,
4621 TUN_SOCKET__RELABELFROM
, NULL
);
4624 err
= avc_has_perm(sid
, sid
, SECCLASS_TUN_SOCKET
,
4625 TUN_SOCKET__RELABELTO
, NULL
);
4633 static int selinux_nlmsg_perm(struct sock
*sk
, struct sk_buff
*skb
)
4637 struct nlmsghdr
*nlh
;
4638 struct sk_security_struct
*sksec
= sk
->sk_security
;
4640 if (skb
->len
< NLMSG_HDRLEN
) {
4644 nlh
= nlmsg_hdr(skb
);
4646 err
= selinux_nlmsg_lookup(sksec
->sclass
, nlh
->nlmsg_type
, &perm
);
4648 if (err
== -EINVAL
) {
4649 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_SELINUX_ERR
,
4650 "SELinux: unrecognized netlink message"
4651 " type=%hu for sclass=%hu\n",
4652 nlh
->nlmsg_type
, sksec
->sclass
);
4653 if (!selinux_enforcing
|| security_get_allow_unknown())
4663 err
= sock_has_perm(current
, sk
, perm
);
4668 #ifdef CONFIG_NETFILTER
4670 static unsigned int selinux_ip_forward(struct sk_buff
*skb
, int ifindex
,
4676 struct common_audit_data ad
;
4677 struct lsm_network_audit net
= {0,};
4682 if (!selinux_policycap_netpeer
)
4685 secmark_active
= selinux_secmark_enabled();
4686 netlbl_active
= netlbl_enabled();
4687 peerlbl_active
= netlbl_active
|| selinux_xfrm_enabled();
4688 if (!secmark_active
&& !peerlbl_active
)
4691 if (selinux_skb_peerlbl_sid(skb
, family
, &peer_sid
) != 0)
4694 ad
.type
= LSM_AUDIT_DATA_NET
;
4696 ad
.u
.net
->netif
= ifindex
;
4697 ad
.u
.net
->family
= family
;
4698 if (selinux_parse_skb(skb
, &ad
, &addrp
, 1, NULL
) != 0)
4701 if (peerlbl_active
) {
4702 err
= selinux_inet_sys_rcv_skb(ifindex
, addrp
, family
,
4705 selinux_netlbl_err(skb
, err
, 1);
4711 if (avc_has_perm(peer_sid
, skb
->secmark
,
4712 SECCLASS_PACKET
, PACKET__FORWARD_IN
, &ad
))
4716 /* we do this in the FORWARD path and not the POST_ROUTING
4717 * path because we want to make sure we apply the necessary
4718 * labeling before IPsec is applied so we can leverage AH
4720 if (selinux_netlbl_skbuff_setsid(skb
, family
, peer_sid
) != 0)
4726 static unsigned int selinux_ipv4_forward(unsigned int hooknum
,
4727 struct sk_buff
*skb
,
4728 const struct net_device
*in
,
4729 const struct net_device
*out
,
4730 int (*okfn
)(struct sk_buff
*))
4732 return selinux_ip_forward(skb
, in
->ifindex
, PF_INET
);
4735 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4736 static unsigned int selinux_ipv6_forward(unsigned int hooknum
,
4737 struct sk_buff
*skb
,
4738 const struct net_device
*in
,
4739 const struct net_device
*out
,
4740 int (*okfn
)(struct sk_buff
*))
4742 return selinux_ip_forward(skb
, in
->ifindex
, PF_INET6
);
4746 static unsigned int selinux_ip_output(struct sk_buff
*skb
,
4752 if (!netlbl_enabled())
4755 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4756 * because we want to make sure we apply the necessary labeling
4757 * before IPsec is applied so we can leverage AH protection */
4760 struct sk_security_struct
*sksec
;
4762 if (sk
->sk_state
== TCP_LISTEN
)
4763 /* if the socket is the listening state then this
4764 * packet is a SYN-ACK packet which means it needs to
4765 * be labeled based on the connection/request_sock and
4766 * not the parent socket. unfortunately, we can't
4767 * lookup the request_sock yet as it isn't queued on
4768 * the parent socket until after the SYN-ACK is sent.
4769 * the "solution" is to simply pass the packet as-is
4770 * as any IP option based labeling should be copied
4771 * from the initial connection request (in the IP
4772 * layer). it is far from ideal, but until we get a
4773 * security label in the packet itself this is the
4774 * best we can do. */
4777 /* standard practice, label using the parent socket */
4778 sksec
= sk
->sk_security
;
4781 sid
= SECINITSID_KERNEL
;
4782 if (selinux_netlbl_skbuff_setsid(skb
, family
, sid
) != 0)
4788 static unsigned int selinux_ipv4_output(unsigned int hooknum
,
4789 struct sk_buff
*skb
,
4790 const struct net_device
*in
,
4791 const struct net_device
*out
,
4792 int (*okfn
)(struct sk_buff
*))
4794 return selinux_ip_output(skb
, PF_INET
);
4797 static unsigned int selinux_ip_postroute_compat(struct sk_buff
*skb
,
4801 struct sock
*sk
= skb
->sk
;
4802 struct sk_security_struct
*sksec
;
4803 struct common_audit_data ad
;
4804 struct lsm_network_audit net
= {0,};
4810 sksec
= sk
->sk_security
;
4812 ad
.type
= LSM_AUDIT_DATA_NET
;
4814 ad
.u
.net
->netif
= ifindex
;
4815 ad
.u
.net
->family
= family
;
4816 if (selinux_parse_skb(skb
, &ad
, &addrp
, 0, &proto
))
4819 if (selinux_secmark_enabled())
4820 if (avc_has_perm(sksec
->sid
, skb
->secmark
,
4821 SECCLASS_PACKET
, PACKET__SEND
, &ad
))
4822 return NF_DROP_ERR(-ECONNREFUSED
);
4824 if (selinux_xfrm_postroute_last(sksec
->sid
, skb
, &ad
, proto
))
4825 return NF_DROP_ERR(-ECONNREFUSED
);
4830 static unsigned int selinux_ip_postroute(struct sk_buff
*skb
, int ifindex
,
4836 struct common_audit_data ad
;
4837 struct lsm_network_audit net
= {0,};
4842 /* If any sort of compatibility mode is enabled then handoff processing
4843 * to the selinux_ip_postroute_compat() function to deal with the
4844 * special handling. We do this in an attempt to keep this function
4845 * as fast and as clean as possible. */
4846 if (!selinux_policycap_netpeer
)
4847 return selinux_ip_postroute_compat(skb
, ifindex
, family
);
4849 secmark_active
= selinux_secmark_enabled();
4850 peerlbl_active
= netlbl_enabled() || selinux_xfrm_enabled();
4851 if (!secmark_active
&& !peerlbl_active
)
4857 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4858 * packet transformation so allow the packet to pass without any checks
4859 * since we'll have another chance to perform access control checks
4860 * when the packet is on it's final way out.
4861 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4862 * is NULL, in this case go ahead and apply access control.
4863 * is NULL, in this case go ahead and apply access control.
4864 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
4865 * TCP listening state we cannot wait until the XFRM processing
4866 * is done as we will miss out on the SA label if we do;
4867 * unfortunately, this means more work, but it is only once per
4869 if (skb_dst(skb
) != NULL
&& skb_dst(skb
)->xfrm
!= NULL
&&
4870 !(sk
!= NULL
&& sk
->sk_state
== TCP_LISTEN
))
4875 /* Without an associated socket the packet is either coming
4876 * from the kernel or it is being forwarded; check the packet
4877 * to determine which and if the packet is being forwarded
4878 * query the packet directly to determine the security label. */
4880 secmark_perm
= PACKET__FORWARD_OUT
;
4881 if (selinux_skb_peerlbl_sid(skb
, family
, &peer_sid
))
4884 secmark_perm
= PACKET__SEND
;
4885 peer_sid
= SECINITSID_KERNEL
;
4887 } else if (sk
->sk_state
== TCP_LISTEN
) {
4888 /* Locally generated packet but the associated socket is in the
4889 * listening state which means this is a SYN-ACK packet. In
4890 * this particular case the correct security label is assigned
4891 * to the connection/request_sock but unfortunately we can't
4892 * query the request_sock as it isn't queued on the parent
4893 * socket until after the SYN-ACK packet is sent; the only
4894 * viable choice is to regenerate the label like we do in
4895 * selinux_inet_conn_request(). See also selinux_ip_output()
4896 * for similar problems. */
4898 struct sk_security_struct
*sksec
= sk
->sk_security
;
4899 if (selinux_skb_peerlbl_sid(skb
, family
, &skb_sid
))
4901 /* At this point, if the returned skb peerlbl is SECSID_NULL
4902 * and the packet has been through at least one XFRM
4903 * transformation then we must be dealing with the "final"
4904 * form of labeled IPsec packet; since we've already applied
4905 * all of our access controls on this packet we can safely
4906 * pass the packet. */
4907 if (skb_sid
== SECSID_NULL
) {
4910 if (IPCB(skb
)->flags
& IPSKB_XFRM_TRANSFORMED
)
4914 if (IP6CB(skb
)->flags
& IP6SKB_XFRM_TRANSFORMED
)
4917 return NF_DROP_ERR(-ECONNREFUSED
);
4920 if (selinux_conn_sid(sksec
->sid
, skb_sid
, &peer_sid
))
4922 secmark_perm
= PACKET__SEND
;
4924 /* Locally generated packet, fetch the security label from the
4925 * associated socket. */
4926 struct sk_security_struct
*sksec
= sk
->sk_security
;
4927 peer_sid
= sksec
->sid
;
4928 secmark_perm
= PACKET__SEND
;
4931 ad
.type
= LSM_AUDIT_DATA_NET
;
4933 ad
.u
.net
->netif
= ifindex
;
4934 ad
.u
.net
->family
= family
;
4935 if (selinux_parse_skb(skb
, &ad
, &addrp
, 0, NULL
))
4939 if (avc_has_perm(peer_sid
, skb
->secmark
,
4940 SECCLASS_PACKET
, secmark_perm
, &ad
))
4941 return NF_DROP_ERR(-ECONNREFUSED
);
4943 if (peerlbl_active
) {
4947 if (sel_netif_sid(ifindex
, &if_sid
))
4949 if (avc_has_perm(peer_sid
, if_sid
,
4950 SECCLASS_NETIF
, NETIF__EGRESS
, &ad
))
4951 return NF_DROP_ERR(-ECONNREFUSED
);
4953 if (sel_netnode_sid(addrp
, family
, &node_sid
))
4955 if (avc_has_perm(peer_sid
, node_sid
,
4956 SECCLASS_NODE
, NODE__SENDTO
, &ad
))
4957 return NF_DROP_ERR(-ECONNREFUSED
);
4963 static unsigned int selinux_ipv4_postroute(unsigned int hooknum
,
4964 struct sk_buff
*skb
,
4965 const struct net_device
*in
,
4966 const struct net_device
*out
,
4967 int (*okfn
)(struct sk_buff
*))
4969 return selinux_ip_postroute(skb
, out
->ifindex
, PF_INET
);
4972 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4973 static unsigned int selinux_ipv6_postroute(unsigned int hooknum
,
4974 struct sk_buff
*skb
,
4975 const struct net_device
*in
,
4976 const struct net_device
*out
,
4977 int (*okfn
)(struct sk_buff
*))
4979 return selinux_ip_postroute(skb
, out
->ifindex
, PF_INET6
);
4983 #endif /* CONFIG_NETFILTER */
4985 static int selinux_netlink_send(struct sock
*sk
, struct sk_buff
*skb
)
4989 err
= cap_netlink_send(sk
, skb
);
4993 return selinux_nlmsg_perm(sk
, skb
);
4996 static int ipc_alloc_security(struct task_struct
*task
,
4997 struct kern_ipc_perm
*perm
,
5000 struct ipc_security_struct
*isec
;
5003 isec
= kzalloc(sizeof(struct ipc_security_struct
), GFP_KERNEL
);
5007 sid
= task_sid(task
);
5008 isec
->sclass
= sclass
;
5010 perm
->security
= isec
;
5015 static void ipc_free_security(struct kern_ipc_perm
*perm
)
5017 struct ipc_security_struct
*isec
= perm
->security
;
5018 perm
->security
= NULL
;
5022 static int msg_msg_alloc_security(struct msg_msg
*msg
)
5024 struct msg_security_struct
*msec
;
5026 msec
= kzalloc(sizeof(struct msg_security_struct
), GFP_KERNEL
);
5030 msec
->sid
= SECINITSID_UNLABELED
;
5031 msg
->security
= msec
;
5036 static void msg_msg_free_security(struct msg_msg
*msg
)
5038 struct msg_security_struct
*msec
= msg
->security
;
5040 msg
->security
= NULL
;
5044 static int ipc_has_perm(struct kern_ipc_perm
*ipc_perms
,
5047 struct ipc_security_struct
*isec
;
5048 struct common_audit_data ad
;
5049 u32 sid
= current_sid();
5051 isec
= ipc_perms
->security
;
5053 ad
.type
= LSM_AUDIT_DATA_IPC
;
5054 ad
.u
.ipc_id
= ipc_perms
->key
;
5056 return avc_has_perm(sid
, isec
->sid
, isec
->sclass
, perms
, &ad
);
5059 static int selinux_msg_msg_alloc_security(struct msg_msg
*msg
)
5061 return msg_msg_alloc_security(msg
);
5064 static void selinux_msg_msg_free_security(struct msg_msg
*msg
)
5066 msg_msg_free_security(msg
);
5069 /* message queue security operations */
5070 static int selinux_msg_queue_alloc_security(struct msg_queue
*msq
)
5072 struct ipc_security_struct
*isec
;
5073 struct common_audit_data ad
;
5074 u32 sid
= current_sid();
5077 rc
= ipc_alloc_security(current
, &msq
->q_perm
, SECCLASS_MSGQ
);
5081 isec
= msq
->q_perm
.security
;
5083 ad
.type
= LSM_AUDIT_DATA_IPC
;
5084 ad
.u
.ipc_id
= msq
->q_perm
.key
;
5086 rc
= avc_has_perm(sid
, isec
->sid
, SECCLASS_MSGQ
,
5089 ipc_free_security(&msq
->q_perm
);
5095 static void selinux_msg_queue_free_security(struct msg_queue
*msq
)
5097 ipc_free_security(&msq
->q_perm
);
5100 static int selinux_msg_queue_associate(struct msg_queue
*msq
, int msqflg
)
5102 struct ipc_security_struct
*isec
;
5103 struct common_audit_data ad
;
5104 u32 sid
= current_sid();
5106 isec
= msq
->q_perm
.security
;
5108 ad
.type
= LSM_AUDIT_DATA_IPC
;
5109 ad
.u
.ipc_id
= msq
->q_perm
.key
;
5111 return avc_has_perm(sid
, isec
->sid
, SECCLASS_MSGQ
,
5112 MSGQ__ASSOCIATE
, &ad
);
5115 static int selinux_msg_queue_msgctl(struct msg_queue
*msq
, int cmd
)
5123 /* No specific object, just general system-wide information. */
5124 return task_has_system(current
, SYSTEM__IPC_INFO
);
5127 perms
= MSGQ__GETATTR
| MSGQ__ASSOCIATE
;
5130 perms
= MSGQ__SETATTR
;
5133 perms
= MSGQ__DESTROY
;
5139 err
= ipc_has_perm(&msq
->q_perm
, perms
);
5143 static int selinux_msg_queue_msgsnd(struct msg_queue
*msq
, struct msg_msg
*msg
, int msqflg
)
5145 struct ipc_security_struct
*isec
;
5146 struct msg_security_struct
*msec
;
5147 struct common_audit_data ad
;
5148 u32 sid
= current_sid();
5151 isec
= msq
->q_perm
.security
;
5152 msec
= msg
->security
;
5155 * First time through, need to assign label to the message
5157 if (msec
->sid
== SECINITSID_UNLABELED
) {
5159 * Compute new sid based on current process and
5160 * message queue this message will be stored in
5162 rc
= security_transition_sid(sid
, isec
->sid
, SECCLASS_MSG
,
5168 ad
.type
= LSM_AUDIT_DATA_IPC
;
5169 ad
.u
.ipc_id
= msq
->q_perm
.key
;
5171 /* Can this process write to the queue? */
5172 rc
= avc_has_perm(sid
, isec
->sid
, SECCLASS_MSGQ
,
5175 /* Can this process send the message */
5176 rc
= avc_has_perm(sid
, msec
->sid
, SECCLASS_MSG
,
5179 /* Can the message be put in the queue? */
5180 rc
= avc_has_perm(msec
->sid
, isec
->sid
, SECCLASS_MSGQ
,
5181 MSGQ__ENQUEUE
, &ad
);
5186 static int selinux_msg_queue_msgrcv(struct msg_queue
*msq
, struct msg_msg
*msg
,
5187 struct task_struct
*target
,
5188 long type
, int mode
)
5190 struct ipc_security_struct
*isec
;
5191 struct msg_security_struct
*msec
;
5192 struct common_audit_data ad
;
5193 u32 sid
= task_sid(target
);
5196 isec
= msq
->q_perm
.security
;
5197 msec
= msg
->security
;
5199 ad
.type
= LSM_AUDIT_DATA_IPC
;
5200 ad
.u
.ipc_id
= msq
->q_perm
.key
;
5202 rc
= avc_has_perm(sid
, isec
->sid
,
5203 SECCLASS_MSGQ
, MSGQ__READ
, &ad
);
5205 rc
= avc_has_perm(sid
, msec
->sid
,
5206 SECCLASS_MSG
, MSG__RECEIVE
, &ad
);
5210 /* Shared Memory security operations */
5211 static int selinux_shm_alloc_security(struct shmid_kernel
*shp
)
5213 struct ipc_security_struct
*isec
;
5214 struct common_audit_data ad
;
5215 u32 sid
= current_sid();
5218 rc
= ipc_alloc_security(current
, &shp
->shm_perm
, SECCLASS_SHM
);
5222 isec
= shp
->shm_perm
.security
;
5224 ad
.type
= LSM_AUDIT_DATA_IPC
;
5225 ad
.u
.ipc_id
= shp
->shm_perm
.key
;
5227 rc
= avc_has_perm(sid
, isec
->sid
, SECCLASS_SHM
,
5230 ipc_free_security(&shp
->shm_perm
);
5236 static void selinux_shm_free_security(struct shmid_kernel
*shp
)
5238 ipc_free_security(&shp
->shm_perm
);
5241 static int selinux_shm_associate(struct shmid_kernel
*shp
, int shmflg
)
5243 struct ipc_security_struct
*isec
;
5244 struct common_audit_data ad
;
5245 u32 sid
= current_sid();
5247 isec
= shp
->shm_perm
.security
;
5249 ad
.type
= LSM_AUDIT_DATA_IPC
;
5250 ad
.u
.ipc_id
= shp
->shm_perm
.key
;
5252 return avc_has_perm(sid
, isec
->sid
, SECCLASS_SHM
,
5253 SHM__ASSOCIATE
, &ad
);
5256 /* Note, at this point, shp is locked down */
5257 static int selinux_shm_shmctl(struct shmid_kernel
*shp
, int cmd
)
5265 /* No specific object, just general system-wide information. */
5266 return task_has_system(current
, SYSTEM__IPC_INFO
);
5269 perms
= SHM__GETATTR
| SHM__ASSOCIATE
;
5272 perms
= SHM__SETATTR
;
5279 perms
= SHM__DESTROY
;
5285 err
= ipc_has_perm(&shp
->shm_perm
, perms
);
5289 static int selinux_shm_shmat(struct shmid_kernel
*shp
,
5290 char __user
*shmaddr
, int shmflg
)
5294 if (shmflg
& SHM_RDONLY
)
5297 perms
= SHM__READ
| SHM__WRITE
;
5299 return ipc_has_perm(&shp
->shm_perm
, perms
);
5302 /* Semaphore security operations */
5303 static int selinux_sem_alloc_security(struct sem_array
*sma
)
5305 struct ipc_security_struct
*isec
;
5306 struct common_audit_data ad
;
5307 u32 sid
= current_sid();
5310 rc
= ipc_alloc_security(current
, &sma
->sem_perm
, SECCLASS_SEM
);
5314 isec
= sma
->sem_perm
.security
;
5316 ad
.type
= LSM_AUDIT_DATA_IPC
;
5317 ad
.u
.ipc_id
= sma
->sem_perm
.key
;
5319 rc
= avc_has_perm(sid
, isec
->sid
, SECCLASS_SEM
,
5322 ipc_free_security(&sma
->sem_perm
);
5328 static void selinux_sem_free_security(struct sem_array
*sma
)
5330 ipc_free_security(&sma
->sem_perm
);
5333 static int selinux_sem_associate(struct sem_array
*sma
, int semflg
)
5335 struct ipc_security_struct
*isec
;
5336 struct common_audit_data ad
;
5337 u32 sid
= current_sid();
5339 isec
= sma
->sem_perm
.security
;
5341 ad
.type
= LSM_AUDIT_DATA_IPC
;
5342 ad
.u
.ipc_id
= sma
->sem_perm
.key
;
5344 return avc_has_perm(sid
, isec
->sid
, SECCLASS_SEM
,
5345 SEM__ASSOCIATE
, &ad
);
5348 /* Note, at this point, sma is locked down */
5349 static int selinux_sem_semctl(struct sem_array
*sma
, int cmd
)
5357 /* No specific object, just general system-wide information. */
5358 return task_has_system(current
, SYSTEM__IPC_INFO
);
5362 perms
= SEM__GETATTR
;
5373 perms
= SEM__DESTROY
;
5376 perms
= SEM__SETATTR
;
5380 perms
= SEM__GETATTR
| SEM__ASSOCIATE
;
5386 err
= ipc_has_perm(&sma
->sem_perm
, perms
);
5390 static int selinux_sem_semop(struct sem_array
*sma
,
5391 struct sembuf
*sops
, unsigned nsops
, int alter
)
5396 perms
= SEM__READ
| SEM__WRITE
;
5400 return ipc_has_perm(&sma
->sem_perm
, perms
);
5403 static int selinux_ipc_permission(struct kern_ipc_perm
*ipcp
, short flag
)
5409 av
|= IPC__UNIX_READ
;
5411 av
|= IPC__UNIX_WRITE
;
5416 return ipc_has_perm(ipcp
, av
);
5419 static void selinux_ipc_getsecid(struct kern_ipc_perm
*ipcp
, u32
*secid
)
5421 struct ipc_security_struct
*isec
= ipcp
->security
;
5425 static void selinux_d_instantiate(struct dentry
*dentry
, struct inode
*inode
)
5428 inode_doinit_with_dentry(inode
, dentry
);
5431 static int selinux_getprocattr(struct task_struct
*p
,
5432 char *name
, char **value
)
5434 const struct task_security_struct
*__tsec
;
5440 error
= current_has_perm(p
, PROCESS__GETATTR
);
5446 __tsec
= __task_cred(p
)->security
;
5448 if (!strcmp(name
, "current"))
5450 else if (!strcmp(name
, "prev"))
5452 else if (!strcmp(name
, "exec"))
5453 sid
= __tsec
->exec_sid
;
5454 else if (!strcmp(name
, "fscreate"))
5455 sid
= __tsec
->create_sid
;
5456 else if (!strcmp(name
, "keycreate"))
5457 sid
= __tsec
->keycreate_sid
;
5458 else if (!strcmp(name
, "sockcreate"))
5459 sid
= __tsec
->sockcreate_sid
;
5467 error
= security_sid_to_context(sid
, value
, &len
);
5477 static int selinux_setprocattr(struct task_struct
*p
,
5478 char *name
, void *value
, size_t size
)
5480 struct task_security_struct
*tsec
;
5481 struct task_struct
*tracer
;
5488 /* SELinux only allows a process to change its own
5489 security attributes. */
5494 * Basic control over ability to set these attributes at all.
5495 * current == p, but we'll pass them separately in case the
5496 * above restriction is ever removed.
5498 if (!strcmp(name
, "exec"))
5499 error
= current_has_perm(p
, PROCESS__SETEXEC
);
5500 else if (!strcmp(name
, "fscreate"))
5501 error
= current_has_perm(p
, PROCESS__SETFSCREATE
);
5502 else if (!strcmp(name
, "keycreate"))
5503 error
= current_has_perm(p
, PROCESS__SETKEYCREATE
);
5504 else if (!strcmp(name
, "sockcreate"))
5505 error
= current_has_perm(p
, PROCESS__SETSOCKCREATE
);
5506 else if (!strcmp(name
, "current"))
5507 error
= current_has_perm(p
, PROCESS__SETCURRENT
);
5513 /* Obtain a SID for the context, if one was specified. */
5514 if (size
&& str
[1] && str
[1] != '\n') {
5515 if (str
[size
-1] == '\n') {
5519 error
= security_context_to_sid(value
, size
, &sid
);
5520 if (error
== -EINVAL
&& !strcmp(name
, "fscreate")) {
5521 if (!capable(CAP_MAC_ADMIN
)) {
5522 struct audit_buffer
*ab
;
5525 /* We strip a nul only if it is at the end, otherwise the
5526 * context contains a nul and we should audit that */
5527 if (str
[size
- 1] == '\0')
5528 audit_size
= size
- 1;
5531 ab
= audit_log_start(current
->audit_context
, GFP_ATOMIC
, AUDIT_SELINUX_ERR
);
5532 audit_log_format(ab
, "op=fscreate invalid_context=");
5533 audit_log_n_untrustedstring(ab
, value
, audit_size
);
5538 error
= security_context_to_sid_force(value
, size
,
5545 new = prepare_creds();
5549 /* Permission checking based on the specified context is
5550 performed during the actual operation (execve,
5551 open/mkdir/...), when we know the full context of the
5552 operation. See selinux_bprm_set_creds for the execve
5553 checks and may_create for the file creation checks. The
5554 operation will then fail if the context is not permitted. */
5555 tsec
= new->security
;
5556 if (!strcmp(name
, "exec")) {
5557 tsec
->exec_sid
= sid
;
5558 } else if (!strcmp(name
, "fscreate")) {
5559 tsec
->create_sid
= sid
;
5560 } else if (!strcmp(name
, "keycreate")) {
5561 error
= may_create_key(sid
, p
);
5564 tsec
->keycreate_sid
= sid
;
5565 } else if (!strcmp(name
, "sockcreate")) {
5566 tsec
->sockcreate_sid
= sid
;
5567 } else if (!strcmp(name
, "current")) {
5572 /* Only allow single threaded processes to change context */
5574 if (!current_is_single_threaded()) {
5575 error
= security_bounded_transition(tsec
->sid
, sid
);
5580 /* Check permissions for the transition. */
5581 error
= avc_has_perm(tsec
->sid
, sid
, SECCLASS_PROCESS
,
5582 PROCESS__DYNTRANSITION
, NULL
);
5586 /* Check for ptracing, and update the task SID if ok.
5587 Otherwise, leave SID unchanged and fail. */
5590 tracer
= ptrace_parent(p
);
5592 ptsid
= task_sid(tracer
);
5596 error
= avc_has_perm(ptsid
, sid
, SECCLASS_PROCESS
,
5597 PROCESS__PTRACE
, NULL
);
5616 static int selinux_secid_to_secctx(u32 secid
, char **secdata
, u32
*seclen
)
5618 return security_sid_to_context(secid
, secdata
, seclen
);
5621 static int selinux_secctx_to_secid(const char *secdata
, u32 seclen
, u32
*secid
)
5623 return security_context_to_sid(secdata
, seclen
, secid
);
5626 static void selinux_release_secctx(char *secdata
, u32 seclen
)
5632 * called with inode->i_mutex locked
5634 static int selinux_inode_notifysecctx(struct inode
*inode
, void *ctx
, u32 ctxlen
)
5636 return selinux_inode_setsecurity(inode
, XATTR_SELINUX_SUFFIX
, ctx
, ctxlen
, 0);
5640 * called with inode->i_mutex locked
5642 static int selinux_inode_setsecctx(struct dentry
*dentry
, void *ctx
, u32 ctxlen
)
5644 return __vfs_setxattr_noperm(dentry
, XATTR_NAME_SELINUX
, ctx
, ctxlen
, 0);
5647 static int selinux_inode_getsecctx(struct inode
*inode
, void **ctx
, u32
*ctxlen
)
5650 len
= selinux_inode_getsecurity(inode
, XATTR_SELINUX_SUFFIX
,
5659 static int selinux_key_alloc(struct key
*k
, const struct cred
*cred
,
5660 unsigned long flags
)
5662 const struct task_security_struct
*tsec
;
5663 struct key_security_struct
*ksec
;
5665 ksec
= kzalloc(sizeof(struct key_security_struct
), GFP_KERNEL
);
5669 tsec
= cred
->security
;
5670 if (tsec
->keycreate_sid
)
5671 ksec
->sid
= tsec
->keycreate_sid
;
5673 ksec
->sid
= tsec
->sid
;
5679 static void selinux_key_free(struct key
*k
)
5681 struct key_security_struct
*ksec
= k
->security
;
5687 static int selinux_key_permission(key_ref_t key_ref
,
5688 const struct cred
*cred
,
5692 struct key_security_struct
*ksec
;
5695 /* if no specific permissions are requested, we skip the
5696 permission check. No serious, additional covert channels
5697 appear to be created. */
5701 sid
= cred_sid(cred
);
5703 key
= key_ref_to_ptr(key_ref
);
5704 ksec
= key
->security
;
5706 return avc_has_perm(sid
, ksec
->sid
, SECCLASS_KEY
, perm
, NULL
);
5709 static int selinux_key_getsecurity(struct key
*key
, char **_buffer
)
5711 struct key_security_struct
*ksec
= key
->security
;
5712 char *context
= NULL
;
5716 rc
= security_sid_to_context(ksec
->sid
, &context
, &len
);
5725 static struct security_operations selinux_ops
= {
5728 .binder_set_context_mgr
= selinux_binder_set_context_mgr
,
5729 .binder_transaction
= selinux_binder_transaction
,
5730 .binder_transfer_binder
= selinux_binder_transfer_binder
,
5731 .binder_transfer_file
= selinux_binder_transfer_file
,
5733 .ptrace_access_check
= selinux_ptrace_access_check
,
5734 .ptrace_traceme
= selinux_ptrace_traceme
,
5735 .capget
= selinux_capget
,
5736 .capset
= selinux_capset
,
5737 .capable
= selinux_capable
,
5738 .quotactl
= selinux_quotactl
,
5739 .quota_on
= selinux_quota_on
,
5740 .syslog
= selinux_syslog
,
5741 .vm_enough_memory
= selinux_vm_enough_memory
,
5743 .netlink_send
= selinux_netlink_send
,
5745 .bprm_set_creds
= selinux_bprm_set_creds
,
5746 .bprm_committing_creds
= selinux_bprm_committing_creds
,
5747 .bprm_committed_creds
= selinux_bprm_committed_creds
,
5748 .bprm_secureexec
= selinux_bprm_secureexec
,
5750 .sb_alloc_security
= selinux_sb_alloc_security
,
5751 .sb_free_security
= selinux_sb_free_security
,
5752 .sb_copy_data
= selinux_sb_copy_data
,
5753 .sb_remount
= selinux_sb_remount
,
5754 .sb_kern_mount
= selinux_sb_kern_mount
,
5755 .sb_show_options
= selinux_sb_show_options
,
5756 .sb_statfs
= selinux_sb_statfs
,
5757 .sb_mount
= selinux_mount
,
5758 .sb_umount
= selinux_umount
,
5759 .sb_set_mnt_opts
= selinux_set_mnt_opts
,
5760 .sb_clone_mnt_opts
= selinux_sb_clone_mnt_opts
,
5761 .sb_parse_opts_str
= selinux_parse_opts_str
,
5764 .inode_alloc_security
= selinux_inode_alloc_security
,
5765 .inode_free_security
= selinux_inode_free_security
,
5766 .inode_init_security
= selinux_inode_init_security
,
5767 .inode_create
= selinux_inode_create
,
5768 .inode_link
= selinux_inode_link
,
5769 .inode_unlink
= selinux_inode_unlink
,
5770 .inode_symlink
= selinux_inode_symlink
,
5771 .inode_mkdir
= selinux_inode_mkdir
,
5772 .inode_rmdir
= selinux_inode_rmdir
,
5773 .inode_mknod
= selinux_inode_mknod
,
5774 .inode_rename
= selinux_inode_rename
,
5775 .inode_readlink
= selinux_inode_readlink
,
5776 .inode_follow_link
= selinux_inode_follow_link
,
5777 .inode_permission
= selinux_inode_permission
,
5778 .inode_setattr
= selinux_inode_setattr
,
5779 .inode_getattr
= selinux_inode_getattr
,
5780 .inode_setxattr
= selinux_inode_setxattr
,
5781 .inode_post_setxattr
= selinux_inode_post_setxattr
,
5782 .inode_getxattr
= selinux_inode_getxattr
,
5783 .inode_listxattr
= selinux_inode_listxattr
,
5784 .inode_removexattr
= selinux_inode_removexattr
,
5785 .inode_getsecurity
= selinux_inode_getsecurity
,
5786 .inode_setsecurity
= selinux_inode_setsecurity
,
5787 .inode_listsecurity
= selinux_inode_listsecurity
,
5788 .inode_getsecid
= selinux_inode_getsecid
,
5790 .file_permission
= selinux_file_permission
,
5791 .file_alloc_security
= selinux_file_alloc_security
,
5792 .file_free_security
= selinux_file_free_security
,
5793 .file_ioctl
= selinux_file_ioctl
,
5794 .mmap_file
= selinux_mmap_file
,
5795 .mmap_addr
= selinux_mmap_addr
,
5796 .file_mprotect
= selinux_file_mprotect
,
5797 .file_lock
= selinux_file_lock
,
5798 .file_fcntl
= selinux_file_fcntl
,
5799 .file_set_fowner
= selinux_file_set_fowner
,
5800 .file_send_sigiotask
= selinux_file_send_sigiotask
,
5801 .file_receive
= selinux_file_receive
,
5803 .file_open
= selinux_file_open
,
5805 .task_create
= selinux_task_create
,
5806 .cred_alloc_blank
= selinux_cred_alloc_blank
,
5807 .cred_free
= selinux_cred_free
,
5808 .cred_prepare
= selinux_cred_prepare
,
5809 .cred_transfer
= selinux_cred_transfer
,
5810 .kernel_act_as
= selinux_kernel_act_as
,
5811 .kernel_create_files_as
= selinux_kernel_create_files_as
,
5812 .kernel_module_request
= selinux_kernel_module_request
,
5813 .task_setpgid
= selinux_task_setpgid
,
5814 .task_getpgid
= selinux_task_getpgid
,
5815 .task_getsid
= selinux_task_getsid
,
5816 .task_getsecid
= selinux_task_getsecid
,
5817 .task_setnice
= selinux_task_setnice
,
5818 .task_setioprio
= selinux_task_setioprio
,
5819 .task_getioprio
= selinux_task_getioprio
,
5820 .task_setrlimit
= selinux_task_setrlimit
,
5821 .task_setscheduler
= selinux_task_setscheduler
,
5822 .task_getscheduler
= selinux_task_getscheduler
,
5823 .task_movememory
= selinux_task_movememory
,
5824 .task_kill
= selinux_task_kill
,
5825 .task_wait
= selinux_task_wait
,
5826 .task_to_inode
= selinux_task_to_inode
,
5828 .ipc_permission
= selinux_ipc_permission
,
5829 .ipc_getsecid
= selinux_ipc_getsecid
,
5831 .msg_msg_alloc_security
= selinux_msg_msg_alloc_security
,
5832 .msg_msg_free_security
= selinux_msg_msg_free_security
,
5834 .msg_queue_alloc_security
= selinux_msg_queue_alloc_security
,
5835 .msg_queue_free_security
= selinux_msg_queue_free_security
,
5836 .msg_queue_associate
= selinux_msg_queue_associate
,
5837 .msg_queue_msgctl
= selinux_msg_queue_msgctl
,
5838 .msg_queue_msgsnd
= selinux_msg_queue_msgsnd
,
5839 .msg_queue_msgrcv
= selinux_msg_queue_msgrcv
,
5841 .shm_alloc_security
= selinux_shm_alloc_security
,
5842 .shm_free_security
= selinux_shm_free_security
,
5843 .shm_associate
= selinux_shm_associate
,
5844 .shm_shmctl
= selinux_shm_shmctl
,
5845 .shm_shmat
= selinux_shm_shmat
,
5847 .sem_alloc_security
= selinux_sem_alloc_security
,
5848 .sem_free_security
= selinux_sem_free_security
,
5849 .sem_associate
= selinux_sem_associate
,
5850 .sem_semctl
= selinux_sem_semctl
,
5851 .sem_semop
= selinux_sem_semop
,
5853 .d_instantiate
= selinux_d_instantiate
,
5855 .getprocattr
= selinux_getprocattr
,
5856 .setprocattr
= selinux_setprocattr
,
5858 .secid_to_secctx
= selinux_secid_to_secctx
,
5859 .secctx_to_secid
= selinux_secctx_to_secid
,
5860 .release_secctx
= selinux_release_secctx
,
5861 .inode_notifysecctx
= selinux_inode_notifysecctx
,
5862 .inode_setsecctx
= selinux_inode_setsecctx
,
5863 .inode_getsecctx
= selinux_inode_getsecctx
,
5865 .unix_stream_connect
= selinux_socket_unix_stream_connect
,
5866 .unix_may_send
= selinux_socket_unix_may_send
,
5868 .socket_create
= selinux_socket_create
,
5869 .socket_post_create
= selinux_socket_post_create
,
5870 .socket_bind
= selinux_socket_bind
,
5871 .socket_connect
= selinux_socket_connect
,
5872 .socket_listen
= selinux_socket_listen
,
5873 .socket_accept
= selinux_socket_accept
,
5874 .socket_sendmsg
= selinux_socket_sendmsg
,
5875 .socket_recvmsg
= selinux_socket_recvmsg
,
5876 .socket_getsockname
= selinux_socket_getsockname
,
5877 .socket_getpeername
= selinux_socket_getpeername
,
5878 .socket_getsockopt
= selinux_socket_getsockopt
,
5879 .socket_setsockopt
= selinux_socket_setsockopt
,
5880 .socket_shutdown
= selinux_socket_shutdown
,
5881 .socket_sock_rcv_skb
= selinux_socket_sock_rcv_skb
,
5882 .socket_getpeersec_stream
= selinux_socket_getpeersec_stream
,
5883 .socket_getpeersec_dgram
= selinux_socket_getpeersec_dgram
,
5884 .sk_alloc_security
= selinux_sk_alloc_security
,
5885 .sk_free_security
= selinux_sk_free_security
,
5886 .sk_clone_security
= selinux_sk_clone_security
,
5887 .sk_getsecid
= selinux_sk_getsecid
,
5888 .sock_graft
= selinux_sock_graft
,
5889 .inet_conn_request
= selinux_inet_conn_request
,
5890 .inet_csk_clone
= selinux_inet_csk_clone
,
5891 .inet_conn_established
= selinux_inet_conn_established
,
5892 .secmark_relabel_packet
= selinux_secmark_relabel_packet
,
5893 .secmark_refcount_inc
= selinux_secmark_refcount_inc
,
5894 .secmark_refcount_dec
= selinux_secmark_refcount_dec
,
5895 .req_classify_flow
= selinux_req_classify_flow
,
5896 .tun_dev_alloc_security
= selinux_tun_dev_alloc_security
,
5897 .tun_dev_free_security
= selinux_tun_dev_free_security
,
5898 .tun_dev_create
= selinux_tun_dev_create
,
5899 .tun_dev_attach_queue
= selinux_tun_dev_attach_queue
,
5900 .tun_dev_attach
= selinux_tun_dev_attach
,
5901 .tun_dev_open
= selinux_tun_dev_open
,
5902 .skb_owned_by
= selinux_skb_owned_by
,
5904 #ifdef CONFIG_SECURITY_NETWORK_XFRM
5905 .xfrm_policy_alloc_security
= selinux_xfrm_policy_alloc
,
5906 .xfrm_policy_clone_security
= selinux_xfrm_policy_clone
,
5907 .xfrm_policy_free_security
= selinux_xfrm_policy_free
,
5908 .xfrm_policy_delete_security
= selinux_xfrm_policy_delete
,
5909 .xfrm_state_alloc_security
= selinux_xfrm_state_alloc
,
5910 .xfrm_state_free_security
= selinux_xfrm_state_free
,
5911 .xfrm_state_delete_security
= selinux_xfrm_state_delete
,
5912 .xfrm_policy_lookup
= selinux_xfrm_policy_lookup
,
5913 .xfrm_state_pol_flow_match
= selinux_xfrm_state_pol_flow_match
,
5914 .xfrm_decode_session
= selinux_xfrm_decode_session
,
5918 .key_alloc
= selinux_key_alloc
,
5919 .key_free
= selinux_key_free
,
5920 .key_permission
= selinux_key_permission
,
5921 .key_getsecurity
= selinux_key_getsecurity
,
5925 .audit_rule_init
= selinux_audit_rule_init
,
5926 .audit_rule_known
= selinux_audit_rule_known
,
5927 .audit_rule_match
= selinux_audit_rule_match
,
5928 .audit_rule_free
= selinux_audit_rule_free
,
5932 static __init
int selinux_init(void)
5934 if (!security_module_enable(&selinux_ops
)) {
5935 selinux_enabled
= 0;
5939 if (!selinux_enabled
) {
5940 printk(KERN_INFO
"SELinux: Disabled at boot.\n");
5944 printk(KERN_INFO
"SELinux: Initializing.\n");
5946 /* Set the security state for the initial task. */
5947 cred_init_security();
5949 default_noexec
= !(VM_DATA_DEFAULT_FLAGS
& VM_EXEC
);
5951 sel_inode_cache
= kmem_cache_create("selinux_inode_security",
5952 sizeof(struct inode_security_struct
),
5953 0, SLAB_PANIC
, NULL
);
5956 if (register_security(&selinux_ops
))
5957 panic("SELinux: Unable to register with kernel.\n");
5959 if (selinux_enforcing
)
5960 printk(KERN_DEBUG
"SELinux: Starting in enforcing mode\n");
5962 printk(KERN_DEBUG
"SELinux: Starting in permissive mode\n");
5967 static void delayed_superblock_init(struct super_block
*sb
, void *unused
)
5969 superblock_doinit(sb
, NULL
);
5972 void selinux_complete_init(void)
5974 printk(KERN_DEBUG
"SELinux: Completing initialization.\n");
5976 /* Set up any superblocks initialized prior to the policy load. */
5977 printk(KERN_DEBUG
"SELinux: Setting up existing superblocks.\n");
5978 iterate_supers(delayed_superblock_init
, NULL
);
5981 /* SELinux requires early initialization in order to label
5982 all processes and objects when they are created. */
5983 security_initcall(selinux_init
);
5985 #if defined(CONFIG_NETFILTER)
5987 static struct nf_hook_ops selinux_ipv4_ops
[] = {
5989 .hook
= selinux_ipv4_postroute
,
5990 .owner
= THIS_MODULE
,
5992 .hooknum
= NF_INET_POST_ROUTING
,
5993 .priority
= NF_IP_PRI_SELINUX_LAST
,
5996 .hook
= selinux_ipv4_forward
,
5997 .owner
= THIS_MODULE
,
5999 .hooknum
= NF_INET_FORWARD
,
6000 .priority
= NF_IP_PRI_SELINUX_FIRST
,
6003 .hook
= selinux_ipv4_output
,
6004 .owner
= THIS_MODULE
,
6006 .hooknum
= NF_INET_LOCAL_OUT
,
6007 .priority
= NF_IP_PRI_SELINUX_FIRST
,
6011 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6013 static struct nf_hook_ops selinux_ipv6_ops
[] = {
6015 .hook
= selinux_ipv6_postroute
,
6016 .owner
= THIS_MODULE
,
6018 .hooknum
= NF_INET_POST_ROUTING
,
6019 .priority
= NF_IP6_PRI_SELINUX_LAST
,
6022 .hook
= selinux_ipv6_forward
,
6023 .owner
= THIS_MODULE
,
6025 .hooknum
= NF_INET_FORWARD
,
6026 .priority
= NF_IP6_PRI_SELINUX_FIRST
,
6032 static int __init
selinux_nf_ip_init(void)
6036 if (!selinux_enabled
)
6039 printk(KERN_DEBUG
"SELinux: Registering netfilter hooks\n");
6041 err
= nf_register_hooks(selinux_ipv4_ops
, ARRAY_SIZE(selinux_ipv4_ops
));
6043 panic("SELinux: nf_register_hooks for IPv4: error %d\n", err
);
6045 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6046 err
= nf_register_hooks(selinux_ipv6_ops
, ARRAY_SIZE(selinux_ipv6_ops
));
6048 panic("SELinux: nf_register_hooks for IPv6: error %d\n", err
);
6055 __initcall(selinux_nf_ip_init
);
6057 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
6058 static void selinux_nf_ip_exit(void)
6060 printk(KERN_DEBUG
"SELinux: Unregistering netfilter hooks\n");
6062 nf_unregister_hooks(selinux_ipv4_ops
, ARRAY_SIZE(selinux_ipv4_ops
));
6063 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6064 nf_unregister_hooks(selinux_ipv6_ops
, ARRAY_SIZE(selinux_ipv6_ops
));
6069 #else /* CONFIG_NETFILTER */
6071 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
6072 #define selinux_nf_ip_exit()
6075 #endif /* CONFIG_NETFILTER */
6077 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
6078 static int selinux_disabled
;
6080 int selinux_disable(void)
6082 if (ss_initialized
) {
6083 /* Not permitted after initial policy load. */
6087 if (selinux_disabled
) {
6088 /* Only do this once. */
6092 printk(KERN_INFO
"SELinux: Disabled at runtime.\n");
6094 selinux_disabled
= 1;
6095 selinux_enabled
= 0;
6097 reset_security_ops();
6099 /* Try to destroy the avc node cache */
6102 /* Unregister netfilter hooks. */
6103 selinux_nf_ip_exit();
6105 /* Unregister selinuxfs. */