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 extern void selnl_notify_setenforce(int val
);
72 /* Check whether a task is allowed to use a security operation. */
73 static int task_has_security(struct task_struct
*tsk
,
76 struct task_security_struct
*tsec
;
82 return avc_has_perm(tsec
->sid
, SECINITSID_SECURITY
,
83 SECCLASS_SECURITY
, perms
, NULL
);
88 SEL_LOAD
, /* load policy */
89 SEL_ENFORCE
, /* get or set enforcing status */
90 SEL_CONTEXT
, /* validate context */
91 SEL_ACCESS
, /* compute access decision */
92 SEL_CREATE
, /* compute create labeling decision */
93 SEL_RELABEL
, /* compute relabeling decision */
94 SEL_USER
, /* compute reachable user contexts */
95 SEL_POLICYVERS
, /* return policy version for this kernel */
96 SEL_COMMIT_BOOLS
, /* commit new boolean values */
97 SEL_MLS
, /* return if MLS policy is enabled */
98 SEL_DISABLE
, /* disable SELinux until next reboot */
99 SEL_AVC
, /* AVC management directory */
100 SEL_MEMBER
, /* compute polyinstantiation membership decision */
101 SEL_CHECKREQPROT
, /* check requested protection, not kernel-applied one */
102 SEL_COMPAT_NET
, /* whether to use old compat network packet controls */
106 static ssize_t
sel_read_enforce(struct file
*filp
, char __user
*buf
,
107 size_t count
, loff_t
*ppos
)
109 char tmpbuf
[TMPBUFLEN
];
112 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_enforcing
);
113 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
116 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
117 static ssize_t
sel_write_enforce(struct file
* file
, const char __user
* buf
,
118 size_t count
, loff_t
*ppos
)
125 if (count
>= PAGE_SIZE
)
128 /* No partial writes. */
131 page
= (char*)get_zeroed_page(GFP_KERNEL
);
135 if (copy_from_user(page
, buf
, count
))
139 if (sscanf(page
, "%d", &new_value
) != 1)
142 if (new_value
!= selinux_enforcing
) {
143 length
= task_has_security(current
, SECURITY__SETENFORCE
);
146 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_STATUS
,
147 "enforcing=%d old_enforcing=%d auid=%u", new_value
,
149 audit_get_loginuid(current
->audit_context
));
150 selinux_enforcing
= new_value
;
151 if (selinux_enforcing
)
153 selnl_notify_setenforce(selinux_enforcing
);
157 free_page((unsigned long) page
);
161 #define sel_write_enforce NULL
164 static const struct file_operations sel_enforce_ops
= {
165 .read
= sel_read_enforce
,
166 .write
= sel_write_enforce
,
169 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
170 static ssize_t
sel_write_disable(struct file
* file
, const char __user
* buf
,
171 size_t count
, loff_t
*ppos
)
177 extern int selinux_disable(void);
179 if (count
>= PAGE_SIZE
)
182 /* No partial writes. */
185 page
= (char*)get_zeroed_page(GFP_KERNEL
);
189 if (copy_from_user(page
, buf
, count
))
193 if (sscanf(page
, "%d", &new_value
) != 1)
197 length
= selinux_disable();
200 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_STATUS
,
202 audit_get_loginuid(current
->audit_context
));
207 free_page((unsigned long) page
);
211 #define sel_write_disable NULL
214 static const struct file_operations sel_disable_ops
= {
215 .write
= sel_write_disable
,
218 static ssize_t
sel_read_policyvers(struct file
*filp
, char __user
*buf
,
219 size_t count
, loff_t
*ppos
)
221 char tmpbuf
[TMPBUFLEN
];
224 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", POLICYDB_VERSION_MAX
);
225 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
228 static const struct file_operations sel_policyvers_ops
= {
229 .read
= sel_read_policyvers
,
232 /* declaration for sel_write_load */
233 static int sel_make_bools(void);
235 static ssize_t
sel_read_mls(struct file
*filp
, char __user
*buf
,
236 size_t count
, loff_t
*ppos
)
238 char tmpbuf
[TMPBUFLEN
];
241 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_mls_enabled
);
242 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
245 static const struct file_operations sel_mls_ops
= {
246 .read
= sel_read_mls
,
249 static ssize_t
sel_write_load(struct file
* file
, const char __user
* buf
,
250 size_t count
, loff_t
*ppos
)
257 mutex_lock(&sel_mutex
);
259 length
= task_has_security(current
, SECURITY__LOAD_POLICY
);
264 /* No partial writes. */
269 if ((count
> 64 * 1024 * 1024)
270 || (data
= vmalloc(count
)) == NULL
) {
276 if (copy_from_user(data
, buf
, count
) != 0)
279 length
= security_load_policy(data
, count
);
283 ret
= sel_make_bools();
288 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_POLICY_LOAD
,
289 "policy loaded auid=%u",
290 audit_get_loginuid(current
->audit_context
));
292 mutex_unlock(&sel_mutex
);
297 static const struct file_operations sel_load_ops
= {
298 .write
= sel_write_load
,
301 static ssize_t
sel_write_context(struct file
* file
, char *buf
, size_t size
)
307 length
= task_has_security(current
, SECURITY__CHECK_CONTEXT
);
311 length
= security_context_to_sid(buf
, size
, &sid
);
315 length
= security_sid_to_context(sid
, &canon
, &len
);
319 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
320 printk(KERN_ERR
"%s: context size (%u) exceeds payload "
321 "max\n", __FUNCTION__
, len
);
326 memcpy(buf
, canon
, len
);
333 static ssize_t
sel_read_checkreqprot(struct file
*filp
, char __user
*buf
,
334 size_t count
, loff_t
*ppos
)
336 char tmpbuf
[TMPBUFLEN
];
339 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", selinux_checkreqprot
);
340 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
343 static ssize_t
sel_write_checkreqprot(struct file
* file
, const char __user
* buf
,
344 size_t count
, loff_t
*ppos
)
348 unsigned int new_value
;
350 length
= task_has_security(current
, SECURITY__SETCHECKREQPROT
);
354 if (count
>= PAGE_SIZE
)
357 /* No partial writes. */
360 page
= (char*)get_zeroed_page(GFP_KERNEL
);
364 if (copy_from_user(page
, buf
, count
))
368 if (sscanf(page
, "%u", &new_value
) != 1)
371 selinux_checkreqprot
= new_value
? 1 : 0;
374 free_page((unsigned long) page
);
377 static const struct file_operations sel_checkreqprot_ops
= {
378 .read
= sel_read_checkreqprot
,
379 .write
= sel_write_checkreqprot
,
382 static ssize_t
sel_read_compat_net(struct file
*filp
, char __user
*buf
,
383 size_t count
, loff_t
*ppos
)
385 char tmpbuf
[TMPBUFLEN
];
388 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_compat_net
);
389 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
392 static ssize_t
sel_write_compat_net(struct file
* file
, const char __user
* buf
,
393 size_t count
, loff_t
*ppos
)
399 length
= task_has_security(current
, SECURITY__LOAD_POLICY
);
403 if (count
>= PAGE_SIZE
)
406 /* No partial writes. */
409 page
= (char*)get_zeroed_page(GFP_KERNEL
);
413 if (copy_from_user(page
, buf
, count
))
417 if (sscanf(page
, "%d", &new_value
) != 1)
420 selinux_compat_net
= new_value
? 1 : 0;
423 free_page((unsigned long) page
);
426 static const struct file_operations sel_compat_net_ops
= {
427 .read
= sel_read_compat_net
,
428 .write
= sel_write_compat_net
,
432 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
434 static ssize_t
sel_write_access(struct file
* file
, char *buf
, size_t size
);
435 static ssize_t
sel_write_create(struct file
* file
, char *buf
, size_t size
);
436 static ssize_t
sel_write_relabel(struct file
* file
, char *buf
, size_t size
);
437 static ssize_t
sel_write_user(struct file
* file
, char *buf
, size_t size
);
438 static ssize_t
sel_write_member(struct file
* file
, char *buf
, size_t size
);
440 static ssize_t (*write_op
[])(struct file
*, char *, size_t) = {
441 [SEL_ACCESS
] = sel_write_access
,
442 [SEL_CREATE
] = sel_write_create
,
443 [SEL_RELABEL
] = sel_write_relabel
,
444 [SEL_USER
] = sel_write_user
,
445 [SEL_MEMBER
] = sel_write_member
,
446 [SEL_CONTEXT
] = sel_write_context
,
449 static ssize_t
selinux_transaction_write(struct file
*file
, const char __user
*buf
, size_t size
, loff_t
*pos
)
451 ino_t ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
455 if (ino
>= ARRAY_SIZE(write_op
) || !write_op
[ino
])
458 data
= simple_transaction_get(file
, buf
, size
);
460 return PTR_ERR(data
);
462 rv
= write_op
[ino
](file
, data
, size
);
464 simple_transaction_set(file
, rv
);
470 static const struct file_operations transaction_ops
= {
471 .write
= selinux_transaction_write
,
472 .read
= simple_transaction_read
,
473 .release
= simple_transaction_release
,
477 * payload - write methods
478 * If the method has a response, the response should be put in buf,
479 * and the length returned. Otherwise return 0 or and -error.
482 static ssize_t
sel_write_access(struct file
* file
, char *buf
, size_t size
)
488 struct av_decision avd
;
491 length
= task_has_security(current
, SECURITY__COMPUTE_AV
);
496 scon
= kzalloc(size
+1, GFP_KERNEL
);
500 tcon
= kzalloc(size
+1, GFP_KERNEL
);
505 if (sscanf(buf
, "%s %s %hu %x", scon
, tcon
, &tclass
, &req
) != 4)
508 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
511 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
515 length
= security_compute_av(ssid
, tsid
, tclass
, req
, &avd
);
519 length
= scnprintf(buf
, SIMPLE_TRANSACTION_LIMIT
,
521 avd
.allowed
, avd
.decided
,
522 avd
.auditallow
, avd
.auditdeny
,
531 static ssize_t
sel_write_create(struct file
* file
, char *buf
, size_t size
)
534 u32 ssid
, tsid
, newsid
;
540 length
= task_has_security(current
, SECURITY__COMPUTE_CREATE
);
545 scon
= kzalloc(size
+1, GFP_KERNEL
);
549 tcon
= kzalloc(size
+1, GFP_KERNEL
);
554 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
557 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
560 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
564 length
= security_transition_sid(ssid
, tsid
, tclass
, &newsid
);
568 length
= security_sid_to_context(newsid
, &newcon
, &len
);
572 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
573 printk(KERN_ERR
"%s: context size (%u) exceeds payload "
574 "max\n", __FUNCTION__
, len
);
579 memcpy(buf
, newcon
, len
);
590 static ssize_t
sel_write_relabel(struct file
* file
, char *buf
, size_t size
)
593 u32 ssid
, tsid
, newsid
;
599 length
= task_has_security(current
, SECURITY__COMPUTE_RELABEL
);
604 scon
= kzalloc(size
+1, GFP_KERNEL
);
608 tcon
= kzalloc(size
+1, GFP_KERNEL
);
613 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
616 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
619 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
623 length
= security_change_sid(ssid
, tsid
, tclass
, &newsid
);
627 length
= security_sid_to_context(newsid
, &newcon
, &len
);
631 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
636 memcpy(buf
, newcon
, len
);
647 static ssize_t
sel_write_user(struct file
* file
, char *buf
, size_t size
)
649 char *con
, *user
, *ptr
;
656 length
= task_has_security(current
, SECURITY__COMPUTE_USER
);
661 con
= kzalloc(size
+1, GFP_KERNEL
);
665 user
= kzalloc(size
+1, GFP_KERNEL
);
670 if (sscanf(buf
, "%s %s", con
, user
) != 2)
673 length
= security_context_to_sid(con
, strlen(con
)+1, &sid
);
677 length
= security_get_user_sids(sid
, user
, &sids
, &nsids
);
681 length
= sprintf(buf
, "%u", nsids
) + 1;
683 for (i
= 0; i
< nsids
; i
++) {
684 rc
= security_sid_to_context(sids
[i
], &newcon
, &len
);
689 if ((length
+ len
) >= SIMPLE_TRANSACTION_LIMIT
) {
694 memcpy(ptr
, newcon
, len
);
708 static ssize_t
sel_write_member(struct file
* file
, char *buf
, size_t size
)
711 u32 ssid
, tsid
, newsid
;
717 length
= task_has_security(current
, SECURITY__COMPUTE_MEMBER
);
722 scon
= kzalloc(size
+1, GFP_KERNEL
);
726 tcon
= kzalloc(size
+1, GFP_KERNEL
);
731 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
734 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
737 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
741 length
= security_member_sid(ssid
, tsid
, tclass
, &newsid
);
745 length
= security_sid_to_context(newsid
, &newcon
, &len
);
749 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
750 printk(KERN_ERR
"%s: context size (%u) exceeds payload "
751 "max\n", __FUNCTION__
, len
);
756 memcpy(buf
, newcon
, len
);
767 static struct inode
*sel_make_inode(struct super_block
*sb
, int mode
)
769 struct inode
*ret
= new_inode(sb
);
773 ret
->i_uid
= ret
->i_gid
= 0;
775 ret
->i_atime
= ret
->i_mtime
= ret
->i_ctime
= CURRENT_TIME
;
780 #define BOOL_INO_OFFSET 30
782 static ssize_t
sel_read_bool(struct file
*filep
, char __user
*buf
,
783 size_t count
, loff_t
*ppos
)
791 mutex_lock(&sel_mutex
);
795 /* check to see if this file has been deleted */
799 if (count
> PAGE_SIZE
) {
803 if (!(page
= (char*)get_zeroed_page(GFP_KERNEL
))) {
808 inode
= filep
->f_path
.dentry
->d_inode
;
809 cur_enforcing
= security_get_bool_value(inode
->i_ino
- BOOL_INO_OFFSET
);
810 if (cur_enforcing
< 0) {
815 length
= scnprintf(page
, PAGE_SIZE
, "%d %d", cur_enforcing
,
816 bool_pending_values
[inode
->i_ino
- BOOL_INO_OFFSET
]);
817 ret
= simple_read_from_buffer(buf
, count
, ppos
, page
, length
);
819 mutex_unlock(&sel_mutex
);
821 free_page((unsigned long)page
);
825 static ssize_t
sel_write_bool(struct file
*filep
, const char __user
*buf
,
826 size_t count
, loff_t
*ppos
)
829 ssize_t length
= -EFAULT
;
833 mutex_lock(&sel_mutex
);
835 length
= task_has_security(current
, SECURITY__SETBOOL
);
839 /* check to see if this file has been deleted */
843 if (count
>= PAGE_SIZE
) {
848 /* No partial writes. */
851 page
= (char*)get_zeroed_page(GFP_KERNEL
);
857 if (copy_from_user(page
, buf
, count
))
861 if (sscanf(page
, "%d", &new_value
) != 1)
867 inode
= filep
->f_path
.dentry
->d_inode
;
868 bool_pending_values
[inode
->i_ino
- BOOL_INO_OFFSET
] = new_value
;
872 mutex_unlock(&sel_mutex
);
874 free_page((unsigned long) page
);
878 static const struct file_operations sel_bool_ops
= {
879 .read
= sel_read_bool
,
880 .write
= sel_write_bool
,
883 static ssize_t
sel_commit_bools_write(struct file
*filep
,
884 const char __user
*buf
,
885 size_t count
, loff_t
*ppos
)
888 ssize_t length
= -EFAULT
;
891 mutex_lock(&sel_mutex
);
893 length
= task_has_security(current
, SECURITY__SETBOOL
);
897 /* check to see if this file has been deleted */
901 if (count
>= PAGE_SIZE
) {
906 /* No partial writes. */
909 page
= (char*)get_zeroed_page(GFP_KERNEL
);
915 if (copy_from_user(page
, buf
, count
))
919 if (sscanf(page
, "%d", &new_value
) != 1)
922 if (new_value
&& bool_pending_values
) {
923 security_set_bools(bool_num
, bool_pending_values
);
929 mutex_unlock(&sel_mutex
);
931 free_page((unsigned long) page
);
935 static const struct file_operations sel_commit_bools_ops
= {
936 .write
= sel_commit_bools_write
,
939 /* delete booleans - partial revoke() from
940 * fs/proc/generic.c proc_kill_inodes */
941 static void sel_remove_bools(struct dentry
*de
)
943 struct list_head
*p
, *node
;
944 struct super_block
*sb
= de
->d_sb
;
946 spin_lock(&dcache_lock
);
947 node
= de
->d_subdirs
.next
;
948 while (node
!= &de
->d_subdirs
) {
949 struct dentry
*d
= list_entry(node
, struct dentry
, d_u
.d_child
);
954 spin_unlock(&dcache_lock
);
956 simple_unlink(de
->d_inode
, d
);
958 spin_lock(&dcache_lock
);
960 node
= de
->d_subdirs
.next
;
963 spin_unlock(&dcache_lock
);
966 list_for_each(p
, &sb
->s_files
) {
967 struct file
* filp
= list_entry(p
, struct file
, f_u
.fu_list
);
968 struct dentry
* dentry
= filp
->f_path
.dentry
;
970 if (dentry
->d_parent
!= de
) {
978 #define BOOL_DIR_NAME "booleans"
980 static int sel_make_bools(void)
984 struct dentry
*dentry
= NULL
;
985 struct dentry
*dir
= bool_dir
;
986 struct inode
*inode
= NULL
;
987 struct inode_security_struct
*isec
;
988 char **names
= NULL
, *page
;
993 /* remove any existing files */
994 kfree(bool_pending_values
);
995 bool_pending_values
= NULL
;
997 sel_remove_bools(dir
);
999 if (!(page
= (char*)get_zeroed_page(GFP_KERNEL
)))
1002 ret
= security_get_bools(&num
, &names
, &values
);
1006 for (i
= 0; i
< num
; i
++) {
1007 dentry
= d_alloc_name(dir
, names
[i
]);
1012 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
| S_IRUGO
| S_IWUSR
);
1018 len
= snprintf(page
, PAGE_SIZE
, "/%s/%s", BOOL_DIR_NAME
, names
[i
]);
1022 } else if (len
>= PAGE_SIZE
) {
1023 ret
= -ENAMETOOLONG
;
1026 isec
= (struct inode_security_struct
*)inode
->i_security
;
1027 if ((ret
= security_genfs_sid("selinuxfs", page
, SECCLASS_FILE
, &sid
)))
1030 isec
->initialized
= 1;
1031 inode
->i_fop
= &sel_bool_ops
;
1032 inode
->i_ino
= i
+ BOOL_INO_OFFSET
;
1033 d_add(dentry
, inode
);
1036 bool_pending_values
= values
;
1038 free_page((unsigned long)page
);
1040 for (i
= 0; i
< num
; i
++)
1047 sel_remove_bools(dir
);
1052 #define NULL_FILE_NAME "null"
1054 struct dentry
*selinux_null
= NULL
;
1056 static ssize_t
sel_read_avc_cache_threshold(struct file
*filp
, char __user
*buf
,
1057 size_t count
, loff_t
*ppos
)
1059 char tmpbuf
[TMPBUFLEN
];
1062 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", avc_cache_threshold
);
1063 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1066 static ssize_t
sel_write_avc_cache_threshold(struct file
* file
,
1067 const char __user
* buf
,
1068 size_t count
, loff_t
*ppos
)
1075 if (count
>= PAGE_SIZE
) {
1081 /* No partial writes. */
1086 page
= (char*)get_zeroed_page(GFP_KERNEL
);
1092 if (copy_from_user(page
, buf
, count
)) {
1097 if (sscanf(page
, "%u", &new_value
) != 1) {
1102 if (new_value
!= avc_cache_threshold
) {
1103 ret
= task_has_security(current
, SECURITY__SETSECPARAM
);
1106 avc_cache_threshold
= new_value
;
1110 free_page((unsigned long)page
);
1115 static ssize_t
sel_read_avc_hash_stats(struct file
*filp
, char __user
*buf
,
1116 size_t count
, loff_t
*ppos
)
1121 page
= (char *)__get_free_page(GFP_KERNEL
);
1126 ret
= avc_get_hash_stats(page
);
1128 ret
= simple_read_from_buffer(buf
, count
, ppos
, page
, ret
);
1129 free_page((unsigned long)page
);
1134 static const struct file_operations sel_avc_cache_threshold_ops
= {
1135 .read
= sel_read_avc_cache_threshold
,
1136 .write
= sel_write_avc_cache_threshold
,
1139 static const struct file_operations sel_avc_hash_stats_ops
= {
1140 .read
= sel_read_avc_hash_stats
,
1143 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1144 static struct avc_cache_stats
*sel_avc_get_stat_idx(loff_t
*idx
)
1148 for (cpu
= *idx
; cpu
< NR_CPUS
; ++cpu
) {
1149 if (!cpu_possible(cpu
))
1152 return &per_cpu(avc_cache_stats
, cpu
);
1157 static void *sel_avc_stats_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1159 loff_t n
= *pos
- 1;
1162 return SEQ_START_TOKEN
;
1164 return sel_avc_get_stat_idx(&n
);
1167 static void *sel_avc_stats_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1169 return sel_avc_get_stat_idx(pos
);
1172 static int sel_avc_stats_seq_show(struct seq_file
*seq
, void *v
)
1174 struct avc_cache_stats
*st
= v
;
1176 if (v
== SEQ_START_TOKEN
)
1177 seq_printf(seq
, "lookups hits misses allocations reclaims "
1180 seq_printf(seq
, "%u %u %u %u %u %u\n", st
->lookups
,
1181 st
->hits
, st
->misses
, st
->allocations
,
1182 st
->reclaims
, st
->frees
);
1186 static void sel_avc_stats_seq_stop(struct seq_file
*seq
, void *v
)
1189 static struct seq_operations sel_avc_cache_stats_seq_ops
= {
1190 .start
= sel_avc_stats_seq_start
,
1191 .next
= sel_avc_stats_seq_next
,
1192 .show
= sel_avc_stats_seq_show
,
1193 .stop
= sel_avc_stats_seq_stop
,
1196 static int sel_open_avc_cache_stats(struct inode
*inode
, struct file
*file
)
1198 return seq_open(file
, &sel_avc_cache_stats_seq_ops
);
1201 static const struct file_operations sel_avc_cache_stats_ops
= {
1202 .open
= sel_open_avc_cache_stats
,
1204 .llseek
= seq_lseek
,
1205 .release
= seq_release
,
1209 static int sel_make_avc_files(struct dentry
*dir
)
1212 static struct tree_descr files
[] = {
1213 { "cache_threshold",
1214 &sel_avc_cache_threshold_ops
, S_IRUGO
|S_IWUSR
},
1215 { "hash_stats", &sel_avc_hash_stats_ops
, S_IRUGO
},
1216 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1217 { "cache_stats", &sel_avc_cache_stats_ops
, S_IRUGO
},
1221 for (i
= 0; i
< ARRAY_SIZE(files
); i
++) {
1222 struct inode
*inode
;
1223 struct dentry
*dentry
;
1225 dentry
= d_alloc_name(dir
, files
[i
].name
);
1231 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|files
[i
].mode
);
1236 inode
->i_fop
= files
[i
].ops
;
1237 d_add(dentry
, inode
);
1243 static int sel_make_dir(struct inode
*dir
, struct dentry
*dentry
)
1246 struct inode
*inode
;
1248 inode
= sel_make_inode(dir
->i_sb
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
1253 inode
->i_op
= &simple_dir_inode_operations
;
1254 inode
->i_fop
= &simple_dir_operations
;
1255 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1257 d_add(dentry
, inode
);
1258 /* bump link count on parent directory, too */
1264 static int sel_fill_super(struct super_block
* sb
, void * data
, int silent
)
1267 struct dentry
*dentry
;
1268 struct inode
*inode
, *root_inode
;
1269 struct inode_security_struct
*isec
;
1271 static struct tree_descr selinux_files
[] = {
1272 [SEL_LOAD
] = {"load", &sel_load_ops
, S_IRUSR
|S_IWUSR
},
1273 [SEL_ENFORCE
] = {"enforce", &sel_enforce_ops
, S_IRUGO
|S_IWUSR
},
1274 [SEL_CONTEXT
] = {"context", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1275 [SEL_ACCESS
] = {"access", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1276 [SEL_CREATE
] = {"create", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1277 [SEL_RELABEL
] = {"relabel", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1278 [SEL_USER
] = {"user", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1279 [SEL_POLICYVERS
] = {"policyvers", &sel_policyvers_ops
, S_IRUGO
},
1280 [SEL_COMMIT_BOOLS
] = {"commit_pending_bools", &sel_commit_bools_ops
, S_IWUSR
},
1281 [SEL_MLS
] = {"mls", &sel_mls_ops
, S_IRUGO
},
1282 [SEL_DISABLE
] = {"disable", &sel_disable_ops
, S_IWUSR
},
1283 [SEL_MEMBER
] = {"member", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1284 [SEL_CHECKREQPROT
] = {"checkreqprot", &sel_checkreqprot_ops
, S_IRUGO
|S_IWUSR
},
1285 [SEL_COMPAT_NET
] = {"compat_net", &sel_compat_net_ops
, S_IRUGO
|S_IWUSR
},
1288 ret
= simple_fill_super(sb
, SELINUX_MAGIC
, selinux_files
);
1292 root_inode
= sb
->s_root
->d_inode
;
1294 dentry
= d_alloc_name(sb
->s_root
, BOOL_DIR_NAME
);
1300 ret
= sel_make_dir(root_inode
, dentry
);
1306 dentry
= d_alloc_name(sb
->s_root
, NULL_FILE_NAME
);
1312 inode
= sel_make_inode(sb
, S_IFCHR
| S_IRUGO
| S_IWUGO
);
1317 isec
= (struct inode_security_struct
*)inode
->i_security
;
1318 isec
->sid
= SECINITSID_DEVNULL
;
1319 isec
->sclass
= SECCLASS_CHR_FILE
;
1320 isec
->initialized
= 1;
1322 init_special_inode(inode
, S_IFCHR
| S_IRUGO
| S_IWUGO
, MKDEV(MEM_MAJOR
, 3));
1323 d_add(dentry
, inode
);
1324 selinux_null
= dentry
;
1326 dentry
= d_alloc_name(sb
->s_root
, "avc");
1332 ret
= sel_make_dir(root_inode
, dentry
);
1336 ret
= sel_make_avc_files(dentry
);
1342 printk(KERN_ERR
"%s: failed while creating inodes\n", __FUNCTION__
);
1346 static int sel_get_sb(struct file_system_type
*fs_type
,
1347 int flags
, const char *dev_name
, void *data
,
1348 struct vfsmount
*mnt
)
1350 return get_sb_single(fs_type
, flags
, data
, sel_fill_super
, mnt
);
1353 static struct file_system_type sel_fs_type
= {
1354 .name
= "selinuxfs",
1355 .get_sb
= sel_get_sb
,
1356 .kill_sb
= kill_litter_super
,
1359 struct vfsmount
*selinuxfs_mount
;
1361 static int __init
init_sel_fs(void)
1365 if (!selinux_enabled
)
1367 err
= register_filesystem(&sel_fs_type
);
1369 selinuxfs_mount
= kern_mount(&sel_fs_type
);
1370 if (IS_ERR(selinuxfs_mount
)) {
1371 printk(KERN_ERR
"selinuxfs: could not mount!\n");
1372 err
= PTR_ERR(selinuxfs_mount
);
1373 selinuxfs_mount
= NULL
;
1379 __initcall(init_sel_fs
);
1381 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1382 void exit_sel_fs(void)
1384 unregister_filesystem(&sel_fs_type
);