1 /* Updated: Karl MacMillan <kmacmillan@tresys.com>
3 * Added conditional policy language extensions
5 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, version 2.
11 #include <linux/config.h>
12 #include <linux/kernel.h>
13 #include <linux/pagemap.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
17 #include <linux/init.h>
18 #include <linux/string.h>
19 #include <linux/security.h>
20 #include <linux/major.h>
21 #include <asm/uaccess.h>
22 #include <asm/semaphore.h>
24 /* selinuxfs pseudo filesystem for exporting the security policy API.
25 Based on the proc code and the fs/nfsd/nfsctl.c code. */
32 #include "conditional.h"
34 static DECLARE_MUTEX(sel_sem
);
36 /* global data for booleans */
37 static struct dentry
*bool_dir
= NULL
;
38 static int bool_num
= 0;
39 static int *bool_pending_values
= NULL
;
41 extern void selnl_notify_setenforce(int val
);
43 /* Check whether a task is allowed to use a security operation. */
44 int task_has_security(struct task_struct
*tsk
,
47 struct task_security_struct
*tsec
;
53 return avc_has_perm(tsec
->sid
, SECINITSID_SECURITY
,
54 SECCLASS_SECURITY
, perms
, NULL
, NULL
);
59 SEL_LOAD
, /* load policy */
60 SEL_ENFORCE
, /* get or set enforcing status */
61 SEL_CONTEXT
, /* validate context */
62 SEL_ACCESS
, /* compute access decision */
63 SEL_CREATE
, /* compute create labeling decision */
64 SEL_RELABEL
, /* compute relabeling decision */
65 SEL_USER
, /* compute reachable user contexts */
66 SEL_POLICYVERS
, /* return policy version for this kernel */
67 SEL_COMMIT_BOOLS
, /* commit new boolean values */
68 SEL_MLS
, /* return if MLS policy is enabled */
69 SEL_DISABLE
/* disable SELinux until next reboot */
73 static ssize_t
sel_read_enforce(struct file
*filp
, char __user
*buf
,
74 size_t count
, loff_t
*ppos
)
76 char tmpbuf
[TMPBUFLEN
];
79 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_enforcing
);
80 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
83 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
84 static ssize_t
sel_write_enforce(struct file
* file
, const char __user
* buf
,
85 size_t count
, loff_t
*ppos
)
92 if (count
< 0 || count
>= PAGE_SIZE
)
95 /* No partial writes. */
98 page
= (char*)get_zeroed_page(GFP_KERNEL
);
102 if (copy_from_user(page
, buf
, count
))
106 if (sscanf(page
, "%d", &new_value
) != 1)
109 if (new_value
!= selinux_enforcing
) {
110 length
= task_has_security(current
, SECURITY__SETENFORCE
);
113 selinux_enforcing
= new_value
;
114 if (selinux_enforcing
)
116 selnl_notify_setenforce(selinux_enforcing
);
120 free_page((unsigned long) page
);
124 #define sel_write_enforce NULL
127 static struct file_operations sel_enforce_ops
= {
128 .read
= sel_read_enforce
,
129 .write
= sel_write_enforce
,
132 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
133 static ssize_t
sel_write_disable(struct file
* file
, const char __user
* buf
,
134 size_t count
, loff_t
*ppos
)
140 extern int selinux_disable(void);
142 if (count
< 0 || count
>= PAGE_SIZE
)
145 /* No partial writes. */
148 page
= (char*)get_zeroed_page(GFP_KERNEL
);
152 if (copy_from_user(page
, buf
, count
))
156 if (sscanf(page
, "%d", &new_value
) != 1)
160 length
= selinux_disable();
167 free_page((unsigned long) page
);
171 #define sel_write_disable NULL
174 static struct file_operations sel_disable_ops
= {
175 .write
= sel_write_disable
,
178 static ssize_t
sel_read_policyvers(struct file
*filp
, char __user
*buf
,
179 size_t count
, loff_t
*ppos
)
181 char tmpbuf
[TMPBUFLEN
];
184 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", POLICYDB_VERSION_MAX
);
185 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
188 static struct file_operations sel_policyvers_ops
= {
189 .read
= sel_read_policyvers
,
192 /* declaration for sel_write_load */
193 static int sel_make_bools(void);
195 static ssize_t
sel_read_mls(struct file
*filp
, char __user
*buf
,
196 size_t count
, loff_t
*ppos
)
198 char tmpbuf
[TMPBUFLEN
];
201 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_mls_enabled
);
202 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
205 static struct file_operations sel_mls_ops
= {
206 .read
= sel_read_mls
,
209 static ssize_t
sel_write_load(struct file
* file
, const char __user
* buf
,
210 size_t count
, loff_t
*ppos
)
219 length
= task_has_security(current
, SECURITY__LOAD_POLICY
);
224 /* No partial writes. */
229 if ((count
< 0) || (count
> 64 * 1024 * 1024)
230 || (data
= vmalloc(count
)) == NULL
) {
236 if (copy_from_user(data
, buf
, count
) != 0)
239 length
= security_load_policy(data
, count
);
243 ret
= sel_make_bools();
254 static struct file_operations sel_load_ops
= {
255 .write
= sel_write_load
,
259 static ssize_t
sel_write_context(struct file
* file
, const char __user
* buf
,
260 size_t count
, loff_t
*ppos
)
267 length
= task_has_security(current
, SECURITY__CHECK_CONTEXT
);
271 if (count
< 0 || count
>= PAGE_SIZE
)
274 /* No partial writes. */
277 page
= (char*)get_zeroed_page(GFP_KERNEL
);
281 if (copy_from_user(page
, buf
, count
))
284 length
= security_context_to_sid(page
, count
, &sid
);
290 free_page((unsigned long) page
);
294 static struct file_operations sel_context_ops
= {
295 .write
= sel_write_context
,
300 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
302 static ssize_t
sel_write_access(struct file
* file
, char *buf
, size_t size
);
303 static ssize_t
sel_write_create(struct file
* file
, char *buf
, size_t size
);
304 static ssize_t
sel_write_relabel(struct file
* file
, char *buf
, size_t size
);
305 static ssize_t
sel_write_user(struct file
* file
, char *buf
, size_t size
);
307 static ssize_t (*write_op
[])(struct file
*, char *, size_t) = {
308 [SEL_ACCESS
] = sel_write_access
,
309 [SEL_CREATE
] = sel_write_create
,
310 [SEL_RELABEL
] = sel_write_relabel
,
311 [SEL_USER
] = sel_write_user
,
314 static ssize_t
selinux_transaction_write(struct file
*file
, const char __user
*buf
, size_t size
, loff_t
*pos
)
316 ino_t ino
= file
->f_dentry
->d_inode
->i_ino
;
320 if (ino
>= sizeof(write_op
)/sizeof(write_op
[0]) || !write_op
[ino
])
323 data
= simple_transaction_get(file
, buf
, size
);
325 return PTR_ERR(data
);
327 rv
= write_op
[ino
](file
, data
, size
);
329 simple_transaction_set(file
, rv
);
335 static struct file_operations transaction_ops
= {
336 .write
= selinux_transaction_write
,
337 .read
= simple_transaction_read
,
338 .release
= simple_transaction_release
,
342 * payload - write methods
343 * If the method has a response, the response should be put in buf,
344 * and the length returned. Otherwise return 0 or and -error.
347 static ssize_t
sel_write_access(struct file
* file
, char *buf
, size_t size
)
353 struct av_decision avd
;
356 length
= task_has_security(current
, SECURITY__COMPUTE_AV
);
361 scon
= kmalloc(size
+1, GFP_KERNEL
);
364 memset(scon
, 0, size
+1);
366 tcon
= kmalloc(size
+1, GFP_KERNEL
);
369 memset(tcon
, 0, size
+1);
372 if (sscanf(buf
, "%s %s %hu %x", scon
, tcon
, &tclass
, &req
) != 4)
375 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
378 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
382 length
= security_compute_av(ssid
, tsid
, tclass
, req
, &avd
);
386 length
= scnprintf(buf
, SIMPLE_TRANSACTION_LIMIT
,
388 avd
.allowed
, avd
.decided
,
389 avd
.auditallow
, avd
.auditdeny
,
398 static ssize_t
sel_write_create(struct file
* file
, char *buf
, size_t size
)
401 u32 ssid
, tsid
, newsid
;
407 length
= task_has_security(current
, SECURITY__COMPUTE_CREATE
);
412 scon
= kmalloc(size
+1, GFP_KERNEL
);
415 memset(scon
, 0, size
+1);
417 tcon
= kmalloc(size
+1, GFP_KERNEL
);
420 memset(tcon
, 0, size
+1);
423 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
426 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
429 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
433 length
= security_transition_sid(ssid
, tsid
, tclass
, &newsid
);
437 length
= security_sid_to_context(newsid
, &newcon
, &len
);
441 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
442 printk(KERN_ERR
"%s: context size (%u) exceeds payload "
443 "max\n", __FUNCTION__
, len
);
448 memcpy(buf
, newcon
, len
);
459 static ssize_t
sel_write_relabel(struct file
* file
, char *buf
, size_t size
)
462 u32 ssid
, tsid
, newsid
;
468 length
= task_has_security(current
, SECURITY__COMPUTE_RELABEL
);
473 scon
= kmalloc(size
+1, GFP_KERNEL
);
476 memset(scon
, 0, size
+1);
478 tcon
= kmalloc(size
+1, GFP_KERNEL
);
481 memset(tcon
, 0, size
+1);
484 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
487 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
490 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
494 length
= security_change_sid(ssid
, tsid
, tclass
, &newsid
);
498 length
= security_sid_to_context(newsid
, &newcon
, &len
);
502 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
507 memcpy(buf
, newcon
, len
);
518 static ssize_t
sel_write_user(struct file
* file
, char *buf
, size_t size
)
520 char *con
, *user
, *ptr
;
527 length
= task_has_security(current
, SECURITY__COMPUTE_USER
);
532 con
= kmalloc(size
+1, GFP_KERNEL
);
535 memset(con
, 0, size
+1);
537 user
= kmalloc(size
+1, GFP_KERNEL
);
540 memset(user
, 0, size
+1);
543 if (sscanf(buf
, "%s %s", con
, user
) != 2)
546 length
= security_context_to_sid(con
, strlen(con
)+1, &sid
);
550 length
= security_get_user_sids(sid
, user
, &sids
, &nsids
);
554 length
= sprintf(buf
, "%u", nsids
) + 1;
556 for (i
= 0; i
< nsids
; i
++) {
557 rc
= security_sid_to_context(sids
[i
], &newcon
, &len
);
562 if ((length
+ len
) >= SIMPLE_TRANSACTION_LIMIT
) {
567 memcpy(ptr
, newcon
, len
);
581 static struct inode
*sel_make_inode(struct super_block
*sb
, int mode
)
583 struct inode
*ret
= new_inode(sb
);
587 ret
->i_uid
= ret
->i_gid
= 0;
588 ret
->i_blksize
= PAGE_CACHE_SIZE
;
590 ret
->i_atime
= ret
->i_mtime
= ret
->i_ctime
= CURRENT_TIME
;
595 #define BOOL_INO_OFFSET 30
597 static ssize_t
sel_read_bool(struct file
*filep
, char __user
*buf
,
598 size_t count
, loff_t
*ppos
)
611 /* check to see if this file has been deleted */
615 if (count
< 0 || count
> PAGE_SIZE
) {
619 if (!(page
= (char*)get_zeroed_page(GFP_KERNEL
))) {
624 inode
= filep
->f_dentry
->d_inode
;
625 cur_enforcing
= security_get_bool_value(inode
->i_ino
- BOOL_INO_OFFSET
);
626 if (cur_enforcing
< 0) {
631 length
= scnprintf(page
, PAGE_SIZE
, "%d %d", cur_enforcing
,
632 bool_pending_values
[inode
->i_ino
- BOOL_INO_OFFSET
]);
638 if (*ppos
>= length
) {
642 if (count
+ *ppos
> length
)
643 count
= length
- *ppos
;
645 if (copy_to_user(buf
, (char *) page
+ *ppos
, count
)) {
654 free_page((unsigned long)page
);
658 static ssize_t
sel_write_bool(struct file
*filep
, const char __user
*buf
,
659 size_t count
, loff_t
*ppos
)
662 ssize_t length
= -EFAULT
;
668 length
= task_has_security(current
, SECURITY__SETBOOL
);
672 /* check to see if this file has been deleted */
676 if (count
< 0 || count
>= PAGE_SIZE
) {
681 /* No partial writes. */
684 page
= (char*)get_zeroed_page(GFP_KERNEL
);
690 if (copy_from_user(page
, buf
, count
))
694 if (sscanf(page
, "%d", &new_value
) != 1)
700 inode
= filep
->f_dentry
->d_inode
;
701 bool_pending_values
[inode
->i_ino
- BOOL_INO_OFFSET
] = new_value
;
707 free_page((unsigned long) page
);
711 static struct file_operations sel_bool_ops
= {
712 .read
= sel_read_bool
,
713 .write
= sel_write_bool
,
716 static ssize_t
sel_commit_bools_write(struct file
*filep
,
717 const char __user
*buf
,
718 size_t count
, loff_t
*ppos
)
721 ssize_t length
= -EFAULT
;
726 length
= task_has_security(current
, SECURITY__SETBOOL
);
730 /* check to see if this file has been deleted */
734 if (count
< 0 || count
>= PAGE_SIZE
) {
739 /* No partial writes. */
742 page
= (char*)get_zeroed_page(GFP_KERNEL
);
748 if (copy_from_user(page
, buf
, count
))
752 if (sscanf(page
, "%d", &new_value
) != 1)
756 security_set_bools(bool_num
, bool_pending_values
);
764 free_page((unsigned long) page
);
768 static struct file_operations sel_commit_bools_ops
= {
769 .write
= sel_commit_bools_write
,
772 /* delete booleans - partial revoke() from
773 * fs/proc/generic.c proc_kill_inodes */
774 static void sel_remove_bools(struct dentry
*de
)
776 struct list_head
*p
, *node
;
777 struct super_block
*sb
= de
->d_sb
;
779 spin_lock(&dcache_lock
);
780 node
= de
->d_subdirs
.next
;
781 while (node
!= &de
->d_subdirs
) {
782 struct dentry
*d
= list_entry(node
, struct dentry
, d_child
);
787 spin_unlock(&dcache_lock
);
789 simple_unlink(de
->d_inode
, d
);
791 spin_lock(&dcache_lock
);
793 node
= de
->d_subdirs
.next
;
796 spin_unlock(&dcache_lock
);
799 list_for_each(p
, &sb
->s_files
) {
800 struct file
* filp
= list_entry(p
, struct file
, f_list
);
801 struct dentry
* dentry
= filp
->f_dentry
;
803 if (dentry
->d_parent
!= de
) {
811 #define BOOL_DIR_NAME "booleans"
813 static int sel_make_bools(void)
817 struct dentry
*dentry
= NULL
;
818 struct dentry
*dir
= bool_dir
;
819 struct inode
*inode
= NULL
;
820 struct inode_security_struct
*isec
;
822 char **names
= NULL
, *page
;
827 /* remove any existing files */
828 if (bool_pending_values
)
829 kfree(bool_pending_values
);
831 sel_remove_bools(dir
);
833 if (!(page
= (char*)get_zeroed_page(GFP_KERNEL
)))
836 ret
= security_get_bools(&num
, &names
, &values
);
840 for (i
= 0; i
< num
; i
++) {
841 qname
.name
= names
[i
];
842 qname
.len
= strlen(qname
.name
);
843 qname
.hash
= full_name_hash(qname
.name
, qname
.len
);
844 dentry
= d_alloc(dir
, &qname
);
849 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
| S_IRUGO
| S_IWUSR
);
855 len
= snprintf(page
, PAGE_SIZE
, "/%s/%s", BOOL_DIR_NAME
, names
[i
]);
859 } else if (len
>= PAGE_SIZE
) {
863 isec
= (struct inode_security_struct
*)inode
->i_security
;
864 if ((ret
= security_genfs_sid("selinuxfs", page
, SECCLASS_FILE
, &sid
)))
867 isec
->initialized
= 1;
868 inode
->i_fop
= &sel_bool_ops
;
869 inode
->i_ino
= i
+ BOOL_INO_OFFSET
;
870 d_add(dentry
, inode
);
873 bool_pending_values
= values
;
875 free_page((unsigned long)page
);
877 for (i
= 0; i
< num
; i
++) {
890 #define NULL_FILE_NAME "null"
892 struct dentry
*selinux_null
= NULL
;
894 static int sel_fill_super(struct super_block
* sb
, void * data
, int silent
)
897 struct dentry
*dentry
;
900 struct inode_security_struct
*isec
;
902 static struct tree_descr selinux_files
[] = {
903 [SEL_LOAD
] = {"load", &sel_load_ops
, S_IRUSR
|S_IWUSR
},
904 [SEL_ENFORCE
] = {"enforce", &sel_enforce_ops
, S_IRUGO
|S_IWUSR
},
905 [SEL_CONTEXT
] = {"context", &sel_context_ops
, S_IRUGO
|S_IWUGO
},
906 [SEL_ACCESS
] = {"access", &transaction_ops
, S_IRUGO
|S_IWUGO
},
907 [SEL_CREATE
] = {"create", &transaction_ops
, S_IRUGO
|S_IWUGO
},
908 [SEL_RELABEL
] = {"relabel", &transaction_ops
, S_IRUGO
|S_IWUGO
},
909 [SEL_USER
] = {"user", &transaction_ops
, S_IRUGO
|S_IWUGO
},
910 [SEL_POLICYVERS
] = {"policyvers", &sel_policyvers_ops
, S_IRUGO
},
911 [SEL_COMMIT_BOOLS
] = {"commit_pending_bools", &sel_commit_bools_ops
, S_IWUSR
},
912 [SEL_MLS
] = {"mls", &sel_mls_ops
, S_IRUGO
},
913 [SEL_DISABLE
] = {"disable", &sel_disable_ops
, S_IWUSR
},
916 ret
= simple_fill_super(sb
, SELINUX_MAGIC
, selinux_files
);
920 qname
.name
= BOOL_DIR_NAME
;
921 qname
.len
= strlen(qname
.name
);
922 qname
.hash
= full_name_hash(qname
.name
, qname
.len
);
923 dentry
= d_alloc(sb
->s_root
, &qname
);
927 inode
= sel_make_inode(sb
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
930 inode
->i_op
= &simple_dir_inode_operations
;
931 inode
->i_fop
= &simple_dir_operations
;
932 d_add(dentry
, inode
);
934 ret
= sel_make_bools();
938 qname
.name
= NULL_FILE_NAME
;
939 qname
.len
= strlen(qname
.name
);
940 qname
.hash
= full_name_hash(qname
.name
, qname
.len
);
941 dentry
= d_alloc(sb
->s_root
, &qname
);
945 inode
= sel_make_inode(sb
, S_IFCHR
| S_IRUGO
| S_IWUGO
);
948 isec
= (struct inode_security_struct
*)inode
->i_security
;
949 isec
->sid
= SECINITSID_DEVNULL
;
950 isec
->sclass
= SECCLASS_CHR_FILE
;
951 isec
->initialized
= 1;
953 init_special_inode(inode
, S_IFCHR
| S_IRUGO
| S_IWUGO
, MKDEV(MEM_MAJOR
, 3));
954 d_add(dentry
, inode
);
955 selinux_null
= dentry
;
960 printk(KERN_ERR
"%s: failed while creating inodes\n", __FUNCTION__
);
964 static struct super_block
*sel_get_sb(struct file_system_type
*fs_type
,
965 int flags
, const char *dev_name
, void *data
)
967 return get_sb_single(fs_type
, flags
, data
, sel_fill_super
);
970 static struct file_system_type sel_fs_type
= {
972 .get_sb
= sel_get_sb
,
973 .kill_sb
= kill_litter_super
,
976 struct vfsmount
*selinuxfs_mount
;
978 static int __init
init_sel_fs(void)
982 if (!selinux_enabled
)
984 err
= register_filesystem(&sel_fs_type
);
986 selinuxfs_mount
= kern_mount(&sel_fs_type
);
987 if (IS_ERR(selinuxfs_mount
)) {
988 printk(KERN_ERR
"selinuxfs: could not mount!\n");
989 err
= PTR_ERR(selinuxfs_mount
);
990 selinuxfs_mount
= NULL
;
996 __initcall(init_sel_fs
);
998 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
999 void exit_sel_fs(void)
1001 unregister_filesystem(&sel_fs_type
);