1 /* Updated: Karl MacMillan <kmacmillan@tresys.com>
3 * Added conditional policy language extensions
5 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
6 * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2.
12 #include <linux/kernel.h>
13 #include <linux/pagemap.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
17 #include <linux/mutex.h>
18 #include <linux/init.h>
19 #include <linux/string.h>
20 #include <linux/security.h>
21 #include <linux/major.h>
22 #include <linux/seq_file.h>
23 #include <linux/percpu.h>
24 #include <linux/audit.h>
25 #include <asm/uaccess.h>
26 #include <asm/semaphore.h>
28 /* selinuxfs pseudo filesystem for exporting the security policy API.
29 Based on the proc code and the fs/nfsd/nfsctl.c code. */
36 #include "conditional.h"
38 unsigned int selinux_checkreqprot
= CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE
;
40 #ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT
41 #define SELINUX_COMPAT_NET_VALUE 0
43 #define SELINUX_COMPAT_NET_VALUE 1
46 int selinux_compat_net
= SELINUX_COMPAT_NET_VALUE
;
48 static int __init
checkreqprot_setup(char *str
)
50 selinux_checkreqprot
= simple_strtoul(str
,NULL
,0) ? 1 : 0;
53 __setup("checkreqprot=", checkreqprot_setup
);
55 static int __init
selinux_compat_net_setup(char *str
)
57 selinux_compat_net
= simple_strtoul(str
,NULL
,0) ? 1 : 0;
60 __setup("selinux_compat_net=", selinux_compat_net_setup
);
63 static DEFINE_MUTEX(sel_mutex
);
65 /* global data for booleans */
66 static struct dentry
*bool_dir
= NULL
;
67 static int bool_num
= 0;
68 static int *bool_pending_values
= NULL
;
70 /* global data for classes */
71 static struct dentry
*class_dir
= NULL
;
72 static unsigned long last_class_ino
;
74 extern void selnl_notify_setenforce(int val
);
76 /* Check whether a task is allowed to use a security operation. */
77 static int task_has_security(struct task_struct
*tsk
,
80 struct task_security_struct
*tsec
;
86 return avc_has_perm(tsec
->sid
, SECINITSID_SECURITY
,
87 SECCLASS_SECURITY
, perms
, NULL
);
92 SEL_LOAD
, /* load policy */
93 SEL_ENFORCE
, /* get or set enforcing status */
94 SEL_CONTEXT
, /* validate context */
95 SEL_ACCESS
, /* compute access decision */
96 SEL_CREATE
, /* compute create labeling decision */
97 SEL_RELABEL
, /* compute relabeling decision */
98 SEL_USER
, /* compute reachable user contexts */
99 SEL_POLICYVERS
, /* return policy version for this kernel */
100 SEL_COMMIT_BOOLS
, /* commit new boolean values */
101 SEL_MLS
, /* return if MLS policy is enabled */
102 SEL_DISABLE
, /* disable SELinux until next reboot */
103 SEL_MEMBER
, /* compute polyinstantiation membership decision */
104 SEL_CHECKREQPROT
, /* check requested protection, not kernel-applied one */
105 SEL_COMPAT_NET
, /* whether to use old compat network packet controls */
106 SEL_INO_NEXT
, /* The next inode number to use */
109 static unsigned long sel_last_ino
= SEL_INO_NEXT
- 1;
111 #define SEL_INITCON_INO_OFFSET 0x01000000
112 #define SEL_BOOL_INO_OFFSET 0x02000000
113 #define SEL_CLASS_INO_OFFSET 0x04000000
114 #define SEL_INO_MASK 0x00ffffff
117 static ssize_t
sel_read_enforce(struct file
*filp
, char __user
*buf
,
118 size_t count
, loff_t
*ppos
)
120 char tmpbuf
[TMPBUFLEN
];
123 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_enforcing
);
124 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
127 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
128 static ssize_t
sel_write_enforce(struct file
* file
, const char __user
* buf
,
129 size_t count
, loff_t
*ppos
)
136 if (count
>= PAGE_SIZE
)
139 /* No partial writes. */
142 page
= (char*)get_zeroed_page(GFP_KERNEL
);
146 if (copy_from_user(page
, buf
, count
))
150 if (sscanf(page
, "%d", &new_value
) != 1)
153 if (new_value
!= selinux_enforcing
) {
154 length
= task_has_security(current
, SECURITY__SETENFORCE
);
157 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_STATUS
,
158 "enforcing=%d old_enforcing=%d auid=%u", new_value
,
160 audit_get_loginuid(current
->audit_context
));
161 selinux_enforcing
= new_value
;
162 if (selinux_enforcing
)
164 selnl_notify_setenforce(selinux_enforcing
);
168 free_page((unsigned long) page
);
172 #define sel_write_enforce NULL
175 static const struct file_operations sel_enforce_ops
= {
176 .read
= sel_read_enforce
,
177 .write
= sel_write_enforce
,
180 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
181 static ssize_t
sel_write_disable(struct file
* file
, const char __user
* buf
,
182 size_t count
, loff_t
*ppos
)
188 extern int selinux_disable(void);
190 if (count
>= PAGE_SIZE
)
193 /* No partial writes. */
196 page
= (char*)get_zeroed_page(GFP_KERNEL
);
200 if (copy_from_user(page
, buf
, count
))
204 if (sscanf(page
, "%d", &new_value
) != 1)
208 length
= selinux_disable();
211 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_STATUS
,
213 audit_get_loginuid(current
->audit_context
));
218 free_page((unsigned long) page
);
222 #define sel_write_disable NULL
225 static const struct file_operations sel_disable_ops
= {
226 .write
= sel_write_disable
,
229 static ssize_t
sel_read_policyvers(struct file
*filp
, char __user
*buf
,
230 size_t count
, loff_t
*ppos
)
232 char tmpbuf
[TMPBUFLEN
];
235 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", POLICYDB_VERSION_MAX
);
236 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
239 static const struct file_operations sel_policyvers_ops
= {
240 .read
= sel_read_policyvers
,
243 /* declaration for sel_write_load */
244 static int sel_make_bools(void);
245 static int sel_make_classes(void);
247 /* declaration for sel_make_class_dirs */
248 static int sel_make_dir(struct inode
*dir
, struct dentry
*dentry
,
251 static ssize_t
sel_read_mls(struct file
*filp
, char __user
*buf
,
252 size_t count
, loff_t
*ppos
)
254 char tmpbuf
[TMPBUFLEN
];
257 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_mls_enabled
);
258 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
261 static const struct file_operations sel_mls_ops
= {
262 .read
= sel_read_mls
,
265 static ssize_t
sel_write_load(struct file
* file
, const char __user
* buf
,
266 size_t count
, loff_t
*ppos
)
273 mutex_lock(&sel_mutex
);
275 length
= task_has_security(current
, SECURITY__LOAD_POLICY
);
280 /* No partial writes. */
285 if ((count
> 64 * 1024 * 1024)
286 || (data
= vmalloc(count
)) == NULL
) {
292 if (copy_from_user(data
, buf
, count
) != 0)
295 length
= security_load_policy(data
, count
);
299 ret
= sel_make_bools();
305 ret
= sel_make_classes();
312 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_POLICY_LOAD
,
313 "policy loaded auid=%u",
314 audit_get_loginuid(current
->audit_context
));
316 mutex_unlock(&sel_mutex
);
321 static const struct file_operations sel_load_ops
= {
322 .write
= sel_write_load
,
325 static ssize_t
sel_write_context(struct file
* file
, char *buf
, size_t size
)
331 length
= task_has_security(current
, SECURITY__CHECK_CONTEXT
);
335 length
= security_context_to_sid(buf
, size
, &sid
);
339 length
= security_sid_to_context(sid
, &canon
, &len
);
343 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
344 printk(KERN_ERR
"%s: context size (%u) exceeds payload "
345 "max\n", __FUNCTION__
, len
);
350 memcpy(buf
, canon
, len
);
357 static ssize_t
sel_read_checkreqprot(struct file
*filp
, char __user
*buf
,
358 size_t count
, loff_t
*ppos
)
360 char tmpbuf
[TMPBUFLEN
];
363 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", selinux_checkreqprot
);
364 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
367 static ssize_t
sel_write_checkreqprot(struct file
* file
, const char __user
* buf
,
368 size_t count
, loff_t
*ppos
)
372 unsigned int new_value
;
374 length
= task_has_security(current
, SECURITY__SETCHECKREQPROT
);
378 if (count
>= PAGE_SIZE
)
381 /* No partial writes. */
384 page
= (char*)get_zeroed_page(GFP_KERNEL
);
388 if (copy_from_user(page
, buf
, count
))
392 if (sscanf(page
, "%u", &new_value
) != 1)
395 selinux_checkreqprot
= new_value
? 1 : 0;
398 free_page((unsigned long) page
);
401 static const struct file_operations sel_checkreqprot_ops
= {
402 .read
= sel_read_checkreqprot
,
403 .write
= sel_write_checkreqprot
,
406 static ssize_t
sel_read_compat_net(struct file
*filp
, char __user
*buf
,
407 size_t count
, loff_t
*ppos
)
409 char tmpbuf
[TMPBUFLEN
];
412 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_compat_net
);
413 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
416 static ssize_t
sel_write_compat_net(struct file
* file
, const char __user
* buf
,
417 size_t count
, loff_t
*ppos
)
423 length
= task_has_security(current
, SECURITY__LOAD_POLICY
);
427 if (count
>= PAGE_SIZE
)
430 /* No partial writes. */
433 page
= (char*)get_zeroed_page(GFP_KERNEL
);
437 if (copy_from_user(page
, buf
, count
))
441 if (sscanf(page
, "%d", &new_value
) != 1)
444 selinux_compat_net
= new_value
? 1 : 0;
447 free_page((unsigned long) page
);
450 static const struct file_operations sel_compat_net_ops
= {
451 .read
= sel_read_compat_net
,
452 .write
= sel_write_compat_net
,
456 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
458 static ssize_t
sel_write_access(struct file
* file
, char *buf
, size_t size
);
459 static ssize_t
sel_write_create(struct file
* file
, char *buf
, size_t size
);
460 static ssize_t
sel_write_relabel(struct file
* file
, char *buf
, size_t size
);
461 static ssize_t
sel_write_user(struct file
* file
, char *buf
, size_t size
);
462 static ssize_t
sel_write_member(struct file
* file
, char *buf
, size_t size
);
464 static ssize_t (*write_op
[])(struct file
*, char *, size_t) = {
465 [SEL_ACCESS
] = sel_write_access
,
466 [SEL_CREATE
] = sel_write_create
,
467 [SEL_RELABEL
] = sel_write_relabel
,
468 [SEL_USER
] = sel_write_user
,
469 [SEL_MEMBER
] = sel_write_member
,
470 [SEL_CONTEXT
] = sel_write_context
,
473 static ssize_t
selinux_transaction_write(struct file
*file
, const char __user
*buf
, size_t size
, loff_t
*pos
)
475 ino_t ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
479 if (ino
>= ARRAY_SIZE(write_op
) || !write_op
[ino
])
482 data
= simple_transaction_get(file
, buf
, size
);
484 return PTR_ERR(data
);
486 rv
= write_op
[ino
](file
, data
, size
);
488 simple_transaction_set(file
, rv
);
494 static const struct file_operations transaction_ops
= {
495 .write
= selinux_transaction_write
,
496 .read
= simple_transaction_read
,
497 .release
= simple_transaction_release
,
501 * payload - write methods
502 * If the method has a response, the response should be put in buf,
503 * and the length returned. Otherwise return 0 or and -error.
506 static ssize_t
sel_write_access(struct file
* file
, char *buf
, size_t size
)
512 struct av_decision avd
;
515 length
= task_has_security(current
, SECURITY__COMPUTE_AV
);
520 scon
= kzalloc(size
+1, GFP_KERNEL
);
524 tcon
= kzalloc(size
+1, GFP_KERNEL
);
529 if (sscanf(buf
, "%s %s %hu %x", scon
, tcon
, &tclass
, &req
) != 4)
532 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
535 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
539 length
= security_compute_av(ssid
, tsid
, tclass
, req
, &avd
);
543 length
= scnprintf(buf
, SIMPLE_TRANSACTION_LIMIT
,
545 avd
.allowed
, avd
.decided
,
546 avd
.auditallow
, avd
.auditdeny
,
555 static ssize_t
sel_write_create(struct file
* file
, char *buf
, size_t size
)
558 u32 ssid
, tsid
, newsid
;
564 length
= task_has_security(current
, SECURITY__COMPUTE_CREATE
);
569 scon
= kzalloc(size
+1, GFP_KERNEL
);
573 tcon
= kzalloc(size
+1, GFP_KERNEL
);
578 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
581 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
584 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
588 length
= security_transition_sid(ssid
, tsid
, tclass
, &newsid
);
592 length
= security_sid_to_context(newsid
, &newcon
, &len
);
596 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
597 printk(KERN_ERR
"%s: context size (%u) exceeds payload "
598 "max\n", __FUNCTION__
, len
);
603 memcpy(buf
, newcon
, len
);
614 static ssize_t
sel_write_relabel(struct file
* file
, char *buf
, size_t size
)
617 u32 ssid
, tsid
, newsid
;
623 length
= task_has_security(current
, SECURITY__COMPUTE_RELABEL
);
628 scon
= kzalloc(size
+1, GFP_KERNEL
);
632 tcon
= kzalloc(size
+1, GFP_KERNEL
);
637 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
640 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
643 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
647 length
= security_change_sid(ssid
, tsid
, tclass
, &newsid
);
651 length
= security_sid_to_context(newsid
, &newcon
, &len
);
655 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
660 memcpy(buf
, newcon
, len
);
671 static ssize_t
sel_write_user(struct file
* file
, char *buf
, size_t size
)
673 char *con
, *user
, *ptr
;
680 length
= task_has_security(current
, SECURITY__COMPUTE_USER
);
685 con
= kzalloc(size
+1, GFP_KERNEL
);
689 user
= kzalloc(size
+1, GFP_KERNEL
);
694 if (sscanf(buf
, "%s %s", con
, user
) != 2)
697 length
= security_context_to_sid(con
, strlen(con
)+1, &sid
);
701 length
= security_get_user_sids(sid
, user
, &sids
, &nsids
);
705 length
= sprintf(buf
, "%u", nsids
) + 1;
707 for (i
= 0; i
< nsids
; i
++) {
708 rc
= security_sid_to_context(sids
[i
], &newcon
, &len
);
713 if ((length
+ len
) >= SIMPLE_TRANSACTION_LIMIT
) {
718 memcpy(ptr
, newcon
, len
);
732 static ssize_t
sel_write_member(struct file
* file
, char *buf
, size_t size
)
735 u32 ssid
, tsid
, newsid
;
741 length
= task_has_security(current
, SECURITY__COMPUTE_MEMBER
);
746 scon
= kzalloc(size
+1, GFP_KERNEL
);
750 tcon
= kzalloc(size
+1, GFP_KERNEL
);
755 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
758 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
761 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
765 length
= security_member_sid(ssid
, tsid
, tclass
, &newsid
);
769 length
= security_sid_to_context(newsid
, &newcon
, &len
);
773 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
774 printk(KERN_ERR
"%s: context size (%u) exceeds payload "
775 "max\n", __FUNCTION__
, len
);
780 memcpy(buf
, newcon
, len
);
791 static struct inode
*sel_make_inode(struct super_block
*sb
, int mode
)
793 struct inode
*ret
= new_inode(sb
);
797 ret
->i_uid
= ret
->i_gid
= 0;
799 ret
->i_atime
= ret
->i_mtime
= ret
->i_ctime
= CURRENT_TIME
;
804 static ssize_t
sel_read_bool(struct file
*filep
, char __user
*buf
,
805 size_t count
, loff_t
*ppos
)
813 mutex_lock(&sel_mutex
);
817 /* check to see if this file has been deleted */
821 if (count
> PAGE_SIZE
) {
825 if (!(page
= (char*)get_zeroed_page(GFP_KERNEL
))) {
830 inode
= filep
->f_path
.dentry
->d_inode
;
831 cur_enforcing
= security_get_bool_value(inode
->i_ino
&SEL_INO_MASK
);
832 if (cur_enforcing
< 0) {
837 length
= scnprintf(page
, PAGE_SIZE
, "%d %d", cur_enforcing
,
838 bool_pending_values
[inode
->i_ino
&SEL_INO_MASK
]);
839 ret
= simple_read_from_buffer(buf
, count
, ppos
, page
, length
);
841 mutex_unlock(&sel_mutex
);
843 free_page((unsigned long)page
);
847 static ssize_t
sel_write_bool(struct file
*filep
, const char __user
*buf
,
848 size_t count
, loff_t
*ppos
)
851 ssize_t length
= -EFAULT
;
855 mutex_lock(&sel_mutex
);
857 length
= task_has_security(current
, SECURITY__SETBOOL
);
861 /* check to see if this file has been deleted */
865 if (count
>= PAGE_SIZE
) {
870 /* No partial writes. */
873 page
= (char*)get_zeroed_page(GFP_KERNEL
);
879 if (copy_from_user(page
, buf
, count
))
883 if (sscanf(page
, "%d", &new_value
) != 1)
889 inode
= filep
->f_path
.dentry
->d_inode
;
890 bool_pending_values
[inode
->i_ino
&SEL_INO_MASK
] = new_value
;
894 mutex_unlock(&sel_mutex
);
896 free_page((unsigned long) page
);
900 static const struct file_operations sel_bool_ops
= {
901 .read
= sel_read_bool
,
902 .write
= sel_write_bool
,
905 static ssize_t
sel_commit_bools_write(struct file
*filep
,
906 const char __user
*buf
,
907 size_t count
, loff_t
*ppos
)
910 ssize_t length
= -EFAULT
;
913 mutex_lock(&sel_mutex
);
915 length
= task_has_security(current
, SECURITY__SETBOOL
);
919 /* check to see if this file has been deleted */
923 if (count
>= PAGE_SIZE
) {
928 /* No partial writes. */
931 page
= (char*)get_zeroed_page(GFP_KERNEL
);
937 if (copy_from_user(page
, buf
, count
))
941 if (sscanf(page
, "%d", &new_value
) != 1)
944 if (new_value
&& bool_pending_values
) {
945 security_set_bools(bool_num
, bool_pending_values
);
951 mutex_unlock(&sel_mutex
);
953 free_page((unsigned long) page
);
957 static const struct file_operations sel_commit_bools_ops
= {
958 .write
= sel_commit_bools_write
,
961 /* partial revoke() from fs/proc/generic.c proc_kill_inodes */
962 static void sel_remove_entries(struct dentry
*de
)
964 struct list_head
*p
, *node
;
965 struct super_block
*sb
= de
->d_sb
;
967 spin_lock(&dcache_lock
);
968 node
= de
->d_subdirs
.next
;
969 while (node
!= &de
->d_subdirs
) {
970 struct dentry
*d
= list_entry(node
, struct dentry
, d_u
.d_child
);
975 spin_unlock(&dcache_lock
);
977 simple_unlink(de
->d_inode
, d
);
979 spin_lock(&dcache_lock
);
981 node
= de
->d_subdirs
.next
;
984 spin_unlock(&dcache_lock
);
987 list_for_each(p
, &sb
->s_files
) {
988 struct file
* filp
= list_entry(p
, struct file
, f_u
.fu_list
);
989 struct dentry
* dentry
= filp
->f_path
.dentry
;
991 if (dentry
->d_parent
!= de
) {
999 #define BOOL_DIR_NAME "booleans"
1001 static int sel_make_bools(void)
1005 struct dentry
*dentry
= NULL
;
1006 struct dentry
*dir
= bool_dir
;
1007 struct inode
*inode
= NULL
;
1008 struct inode_security_struct
*isec
;
1009 char **names
= NULL
, *page
;
1014 /* remove any existing files */
1015 kfree(bool_pending_values
);
1016 bool_pending_values
= NULL
;
1018 sel_remove_entries(dir
);
1020 if (!(page
= (char*)get_zeroed_page(GFP_KERNEL
)))
1023 ret
= security_get_bools(&num
, &names
, &values
);
1027 for (i
= 0; i
< num
; i
++) {
1028 dentry
= d_alloc_name(dir
, names
[i
]);
1033 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
| S_IRUGO
| S_IWUSR
);
1039 len
= snprintf(page
, PAGE_SIZE
, "/%s/%s", BOOL_DIR_NAME
, names
[i
]);
1043 } else if (len
>= PAGE_SIZE
) {
1044 ret
= -ENAMETOOLONG
;
1047 isec
= (struct inode_security_struct
*)inode
->i_security
;
1048 if ((ret
= security_genfs_sid("selinuxfs", page
, SECCLASS_FILE
, &sid
)))
1051 isec
->initialized
= 1;
1052 inode
->i_fop
= &sel_bool_ops
;
1053 inode
->i_ino
= i
|SEL_BOOL_INO_OFFSET
;
1054 d_add(dentry
, inode
);
1057 bool_pending_values
= values
;
1059 free_page((unsigned long)page
);
1061 for (i
= 0; i
< num
; i
++)
1068 sel_remove_entries(dir
);
1073 #define NULL_FILE_NAME "null"
1075 struct dentry
*selinux_null
= NULL
;
1077 static ssize_t
sel_read_avc_cache_threshold(struct file
*filp
, char __user
*buf
,
1078 size_t count
, loff_t
*ppos
)
1080 char tmpbuf
[TMPBUFLEN
];
1083 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", avc_cache_threshold
);
1084 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1087 static ssize_t
sel_write_avc_cache_threshold(struct file
* file
,
1088 const char __user
* buf
,
1089 size_t count
, loff_t
*ppos
)
1096 if (count
>= PAGE_SIZE
) {
1102 /* No partial writes. */
1107 page
= (char*)get_zeroed_page(GFP_KERNEL
);
1113 if (copy_from_user(page
, buf
, count
)) {
1118 if (sscanf(page
, "%u", &new_value
) != 1) {
1123 if (new_value
!= avc_cache_threshold
) {
1124 ret
= task_has_security(current
, SECURITY__SETSECPARAM
);
1127 avc_cache_threshold
= new_value
;
1131 free_page((unsigned long)page
);
1136 static ssize_t
sel_read_avc_hash_stats(struct file
*filp
, char __user
*buf
,
1137 size_t count
, loff_t
*ppos
)
1142 page
= (char *)__get_free_page(GFP_KERNEL
);
1147 ret
= avc_get_hash_stats(page
);
1149 ret
= simple_read_from_buffer(buf
, count
, ppos
, page
, ret
);
1150 free_page((unsigned long)page
);
1155 static const struct file_operations sel_avc_cache_threshold_ops
= {
1156 .read
= sel_read_avc_cache_threshold
,
1157 .write
= sel_write_avc_cache_threshold
,
1160 static const struct file_operations sel_avc_hash_stats_ops
= {
1161 .read
= sel_read_avc_hash_stats
,
1164 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1165 static struct avc_cache_stats
*sel_avc_get_stat_idx(loff_t
*idx
)
1169 for (cpu
= *idx
; cpu
< NR_CPUS
; ++cpu
) {
1170 if (!cpu_possible(cpu
))
1173 return &per_cpu(avc_cache_stats
, cpu
);
1178 static void *sel_avc_stats_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1180 loff_t n
= *pos
- 1;
1183 return SEQ_START_TOKEN
;
1185 return sel_avc_get_stat_idx(&n
);
1188 static void *sel_avc_stats_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1190 return sel_avc_get_stat_idx(pos
);
1193 static int sel_avc_stats_seq_show(struct seq_file
*seq
, void *v
)
1195 struct avc_cache_stats
*st
= v
;
1197 if (v
== SEQ_START_TOKEN
)
1198 seq_printf(seq
, "lookups hits misses allocations reclaims "
1201 seq_printf(seq
, "%u %u %u %u %u %u\n", st
->lookups
,
1202 st
->hits
, st
->misses
, st
->allocations
,
1203 st
->reclaims
, st
->frees
);
1207 static void sel_avc_stats_seq_stop(struct seq_file
*seq
, void *v
)
1210 static struct seq_operations sel_avc_cache_stats_seq_ops
= {
1211 .start
= sel_avc_stats_seq_start
,
1212 .next
= sel_avc_stats_seq_next
,
1213 .show
= sel_avc_stats_seq_show
,
1214 .stop
= sel_avc_stats_seq_stop
,
1217 static int sel_open_avc_cache_stats(struct inode
*inode
, struct file
*file
)
1219 return seq_open(file
, &sel_avc_cache_stats_seq_ops
);
1222 static const struct file_operations sel_avc_cache_stats_ops
= {
1223 .open
= sel_open_avc_cache_stats
,
1225 .llseek
= seq_lseek
,
1226 .release
= seq_release
,
1230 static int sel_make_avc_files(struct dentry
*dir
)
1233 static struct tree_descr files
[] = {
1234 { "cache_threshold",
1235 &sel_avc_cache_threshold_ops
, S_IRUGO
|S_IWUSR
},
1236 { "hash_stats", &sel_avc_hash_stats_ops
, S_IRUGO
},
1237 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1238 { "cache_stats", &sel_avc_cache_stats_ops
, S_IRUGO
},
1242 for (i
= 0; i
< ARRAY_SIZE(files
); i
++) {
1243 struct inode
*inode
;
1244 struct dentry
*dentry
;
1246 dentry
= d_alloc_name(dir
, files
[i
].name
);
1252 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|files
[i
].mode
);
1257 inode
->i_fop
= files
[i
].ops
;
1258 inode
->i_ino
= ++sel_last_ino
;
1259 d_add(dentry
, inode
);
1265 static ssize_t
sel_read_initcon(struct file
* file
, char __user
*buf
,
1266 size_t count
, loff_t
*ppos
)
1268 struct inode
*inode
;
1273 inode
= file
->f_path
.dentry
->d_inode
;
1274 sid
= inode
->i_ino
&SEL_INO_MASK
;
1275 ret
= security_sid_to_context(sid
, &con
, &len
);
1279 ret
= simple_read_from_buffer(buf
, count
, ppos
, con
, len
);
1284 static const struct file_operations sel_initcon_ops
= {
1285 .read
= sel_read_initcon
,
1288 static int sel_make_initcon_files(struct dentry
*dir
)
1292 for (i
= 1; i
<= SECINITSID_NUM
; i
++) {
1293 struct inode
*inode
;
1294 struct dentry
*dentry
;
1295 dentry
= d_alloc_name(dir
, security_get_initial_sid_context(i
));
1301 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1306 inode
->i_fop
= &sel_initcon_ops
;
1307 inode
->i_ino
= i
|SEL_INITCON_INO_OFFSET
;
1308 d_add(dentry
, inode
);
1314 static inline unsigned int sel_div(unsigned long a
, unsigned long b
)
1316 return a
/ b
- (a
% b
< 0);
1319 static inline unsigned long sel_class_to_ino(u16
class)
1321 return (class * (SEL_VEC_MAX
+ 1)) | SEL_CLASS_INO_OFFSET
;
1324 static inline u16
sel_ino_to_class(unsigned long ino
)
1326 return sel_div(ino
& SEL_INO_MASK
, SEL_VEC_MAX
+ 1);
1329 static inline unsigned long sel_perm_to_ino(u16
class, u32 perm
)
1331 return (class * (SEL_VEC_MAX
+ 1) + perm
) | SEL_CLASS_INO_OFFSET
;
1334 static inline u32
sel_ino_to_perm(unsigned long ino
)
1336 return (ino
& SEL_INO_MASK
) % (SEL_VEC_MAX
+ 1);
1339 static ssize_t
sel_read_class(struct file
* file
, char __user
*buf
,
1340 size_t count
, loff_t
*ppos
)
1344 unsigned long ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
1346 page
= (char *)__get_free_page(GFP_KERNEL
);
1352 len
= snprintf(page
, PAGE_SIZE
, "%d", sel_ino_to_class(ino
));
1353 rc
= simple_read_from_buffer(buf
, count
, ppos
, page
, len
);
1354 free_page((unsigned long)page
);
1359 static const struct file_operations sel_class_ops
= {
1360 .read
= sel_read_class
,
1363 static ssize_t
sel_read_perm(struct file
* file
, char __user
*buf
,
1364 size_t count
, loff_t
*ppos
)
1368 unsigned long ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
1370 page
= (char *)__get_free_page(GFP_KERNEL
);
1376 len
= snprintf(page
, PAGE_SIZE
,"%d", sel_ino_to_perm(ino
));
1377 rc
= simple_read_from_buffer(buf
, count
, ppos
, page
, len
);
1378 free_page((unsigned long)page
);
1383 static const struct file_operations sel_perm_ops
= {
1384 .read
= sel_read_perm
,
1387 static int sel_make_perm_files(char *objclass
, int classvalue
,
1390 int i
, rc
= 0, nperms
;
1393 rc
= security_get_permissions(objclass
, &perms
, &nperms
);
1397 for (i
= 0; i
< nperms
; i
++) {
1398 struct inode
*inode
;
1399 struct dentry
*dentry
;
1401 dentry
= d_alloc_name(dir
, perms
[i
]);
1407 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1412 inode
->i_fop
= &sel_perm_ops
;
1413 /* i+1 since perm values are 1-indexed */
1414 inode
->i_ino
= sel_perm_to_ino(classvalue
, i
+1);
1415 d_add(dentry
, inode
);
1419 for (i
= 0; i
< nperms
; i
++)
1426 static int sel_make_class_dir_entries(char *classname
, int index
,
1429 struct dentry
*dentry
= NULL
;
1430 struct inode
*inode
= NULL
;
1433 dentry
= d_alloc_name(dir
, "index");
1439 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1445 inode
->i_fop
= &sel_class_ops
;
1446 inode
->i_ino
= sel_class_to_ino(index
);
1447 d_add(dentry
, inode
);
1449 dentry
= d_alloc_name(dir
, "perms");
1455 rc
= sel_make_dir(dir
->d_inode
, dentry
, &last_class_ino
);
1459 rc
= sel_make_perm_files(classname
, index
, dentry
);
1465 static void sel_remove_classes(void)
1467 struct list_head
*class_node
;
1469 list_for_each(class_node
, &class_dir
->d_subdirs
) {
1470 struct dentry
*class_subdir
= list_entry(class_node
,
1471 struct dentry
, d_u
.d_child
);
1472 struct list_head
*class_subdir_node
;
1474 list_for_each(class_subdir_node
, &class_subdir
->d_subdirs
) {
1475 struct dentry
*d
= list_entry(class_subdir_node
,
1476 struct dentry
, d_u
.d_child
);
1479 if (d
->d_inode
->i_mode
& S_IFDIR
)
1480 sel_remove_entries(d
);
1483 sel_remove_entries(class_subdir
);
1486 sel_remove_entries(class_dir
);
1489 static int sel_make_classes(void)
1491 int rc
= 0, nclasses
, i
;
1494 /* delete any existing entries */
1495 sel_remove_classes();
1497 rc
= security_get_classes(&classes
, &nclasses
);
1501 /* +2 since classes are 1-indexed */
1502 last_class_ino
= sel_class_to_ino(nclasses
+2);
1504 for (i
= 0; i
< nclasses
; i
++) {
1505 struct dentry
*class_name_dir
;
1507 class_name_dir
= d_alloc_name(class_dir
, classes
[i
]);
1508 if (!class_name_dir
) {
1513 rc
= sel_make_dir(class_dir
->d_inode
, class_name_dir
,
1518 /* i+1 since class values are 1-indexed */
1519 rc
= sel_make_class_dir_entries(classes
[i
], i
+1,
1526 for (i
= 0; i
< nclasses
; i
++)
1533 static int sel_make_dir(struct inode
*dir
, struct dentry
*dentry
,
1537 struct inode
*inode
;
1539 inode
= sel_make_inode(dir
->i_sb
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
1544 inode
->i_op
= &simple_dir_inode_operations
;
1545 inode
->i_fop
= &simple_dir_operations
;
1546 inode
->i_ino
= ++(*ino
);
1547 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1549 d_add(dentry
, inode
);
1550 /* bump link count on parent directory, too */
1556 static int sel_fill_super(struct super_block
* sb
, void * data
, int silent
)
1559 struct dentry
*dentry
;
1560 struct inode
*inode
, *root_inode
;
1561 struct inode_security_struct
*isec
;
1563 static struct tree_descr selinux_files
[] = {
1564 [SEL_LOAD
] = {"load", &sel_load_ops
, S_IRUSR
|S_IWUSR
},
1565 [SEL_ENFORCE
] = {"enforce", &sel_enforce_ops
, S_IRUGO
|S_IWUSR
},
1566 [SEL_CONTEXT
] = {"context", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1567 [SEL_ACCESS
] = {"access", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1568 [SEL_CREATE
] = {"create", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1569 [SEL_RELABEL
] = {"relabel", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1570 [SEL_USER
] = {"user", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1571 [SEL_POLICYVERS
] = {"policyvers", &sel_policyvers_ops
, S_IRUGO
},
1572 [SEL_COMMIT_BOOLS
] = {"commit_pending_bools", &sel_commit_bools_ops
, S_IWUSR
},
1573 [SEL_MLS
] = {"mls", &sel_mls_ops
, S_IRUGO
},
1574 [SEL_DISABLE
] = {"disable", &sel_disable_ops
, S_IWUSR
},
1575 [SEL_MEMBER
] = {"member", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1576 [SEL_CHECKREQPROT
] = {"checkreqprot", &sel_checkreqprot_ops
, S_IRUGO
|S_IWUSR
},
1577 [SEL_COMPAT_NET
] = {"compat_net", &sel_compat_net_ops
, S_IRUGO
|S_IWUSR
},
1580 ret
= simple_fill_super(sb
, SELINUX_MAGIC
, selinux_files
);
1584 root_inode
= sb
->s_root
->d_inode
;
1586 dentry
= d_alloc_name(sb
->s_root
, BOOL_DIR_NAME
);
1592 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1598 dentry
= d_alloc_name(sb
->s_root
, NULL_FILE_NAME
);
1604 inode
= sel_make_inode(sb
, S_IFCHR
| S_IRUGO
| S_IWUGO
);
1609 inode
->i_ino
= ++sel_last_ino
;
1610 isec
= (struct inode_security_struct
*)inode
->i_security
;
1611 isec
->sid
= SECINITSID_DEVNULL
;
1612 isec
->sclass
= SECCLASS_CHR_FILE
;
1613 isec
->initialized
= 1;
1615 init_special_inode(inode
, S_IFCHR
| S_IRUGO
| S_IWUGO
, MKDEV(MEM_MAJOR
, 3));
1616 d_add(dentry
, inode
);
1617 selinux_null
= dentry
;
1619 dentry
= d_alloc_name(sb
->s_root
, "avc");
1625 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1629 ret
= sel_make_avc_files(dentry
);
1633 dentry
= d_alloc_name(sb
->s_root
, "initial_contexts");
1639 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1643 ret
= sel_make_initcon_files(dentry
);
1647 dentry
= d_alloc_name(sb
->s_root
, "class");
1653 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1662 printk(KERN_ERR
"%s: failed while creating inodes\n", __FUNCTION__
);
1666 static int sel_get_sb(struct file_system_type
*fs_type
,
1667 int flags
, const char *dev_name
, void *data
,
1668 struct vfsmount
*mnt
)
1670 return get_sb_single(fs_type
, flags
, data
, sel_fill_super
, mnt
);
1673 static struct file_system_type sel_fs_type
= {
1674 .name
= "selinuxfs",
1675 .get_sb
= sel_get_sb
,
1676 .kill_sb
= kill_litter_super
,
1679 struct vfsmount
*selinuxfs_mount
;
1681 static int __init
init_sel_fs(void)
1685 if (!selinux_enabled
)
1687 err
= register_filesystem(&sel_fs_type
);
1689 selinuxfs_mount
= kern_mount(&sel_fs_type
);
1690 if (IS_ERR(selinuxfs_mount
)) {
1691 printk(KERN_ERR
"selinuxfs: could not mount!\n");
1692 err
= PTR_ERR(selinuxfs_mount
);
1693 selinuxfs_mount
= NULL
;
1699 __initcall(init_sel_fs
);
1701 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1702 void exit_sel_fs(void)
1704 unregister_filesystem(&sel_fs_type
);