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_MEMBER
, /* compute polyinstantiation membership decision */
100 SEL_CHECKREQPROT
, /* check requested protection, not kernel-applied one */
101 SEL_COMPAT_NET
, /* whether to use old compat network packet controls */
102 SEL_INO_NEXT
, /* The next inode number to use */
105 static unsigned long sel_last_ino
= SEL_INO_NEXT
- 1;
107 #define SEL_INITCON_INO_OFFSET 0x01000000
108 #define SEL_BOOL_INO_OFFSET 0x02000000
109 #define SEL_INO_MASK 0x00ffffff
112 static ssize_t
sel_read_enforce(struct file
*filp
, char __user
*buf
,
113 size_t count
, loff_t
*ppos
)
115 char tmpbuf
[TMPBUFLEN
];
118 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_enforcing
);
119 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
122 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
123 static ssize_t
sel_write_enforce(struct file
* file
, const char __user
* buf
,
124 size_t count
, loff_t
*ppos
)
131 if (count
>= PAGE_SIZE
)
134 /* No partial writes. */
137 page
= (char*)get_zeroed_page(GFP_KERNEL
);
141 if (copy_from_user(page
, buf
, count
))
145 if (sscanf(page
, "%d", &new_value
) != 1)
148 if (new_value
!= selinux_enforcing
) {
149 length
= task_has_security(current
, SECURITY__SETENFORCE
);
152 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_STATUS
,
153 "enforcing=%d old_enforcing=%d auid=%u", new_value
,
155 audit_get_loginuid(current
->audit_context
));
156 selinux_enforcing
= new_value
;
157 if (selinux_enforcing
)
159 selnl_notify_setenforce(selinux_enforcing
);
163 free_page((unsigned long) page
);
167 #define sel_write_enforce NULL
170 static const struct file_operations sel_enforce_ops
= {
171 .read
= sel_read_enforce
,
172 .write
= sel_write_enforce
,
175 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
176 static ssize_t
sel_write_disable(struct file
* file
, const char __user
* buf
,
177 size_t count
, loff_t
*ppos
)
183 extern int selinux_disable(void);
185 if (count
>= PAGE_SIZE
)
188 /* No partial writes. */
191 page
= (char*)get_zeroed_page(GFP_KERNEL
);
195 if (copy_from_user(page
, buf
, count
))
199 if (sscanf(page
, "%d", &new_value
) != 1)
203 length
= selinux_disable();
206 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_STATUS
,
208 audit_get_loginuid(current
->audit_context
));
213 free_page((unsigned long) page
);
217 #define sel_write_disable NULL
220 static const struct file_operations sel_disable_ops
= {
221 .write
= sel_write_disable
,
224 static ssize_t
sel_read_policyvers(struct file
*filp
, char __user
*buf
,
225 size_t count
, loff_t
*ppos
)
227 char tmpbuf
[TMPBUFLEN
];
230 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", POLICYDB_VERSION_MAX
);
231 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
234 static const struct file_operations sel_policyvers_ops
= {
235 .read
= sel_read_policyvers
,
238 /* declaration for sel_write_load */
239 static int sel_make_bools(void);
241 static ssize_t
sel_read_mls(struct file
*filp
, char __user
*buf
,
242 size_t count
, loff_t
*ppos
)
244 char tmpbuf
[TMPBUFLEN
];
247 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_mls_enabled
);
248 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
251 static const struct file_operations sel_mls_ops
= {
252 .read
= sel_read_mls
,
255 static ssize_t
sel_write_load(struct file
* file
, const char __user
* buf
,
256 size_t count
, loff_t
*ppos
)
263 mutex_lock(&sel_mutex
);
265 length
= task_has_security(current
, SECURITY__LOAD_POLICY
);
270 /* No partial writes. */
275 if ((count
> 64 * 1024 * 1024)
276 || (data
= vmalloc(count
)) == NULL
) {
282 if (copy_from_user(data
, buf
, count
) != 0)
285 length
= security_load_policy(data
, count
);
289 ret
= sel_make_bools();
294 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_POLICY_LOAD
,
295 "policy loaded auid=%u",
296 audit_get_loginuid(current
->audit_context
));
298 mutex_unlock(&sel_mutex
);
303 static const struct file_operations sel_load_ops
= {
304 .write
= sel_write_load
,
307 static ssize_t
sel_write_context(struct file
* file
, char *buf
, size_t size
)
313 length
= task_has_security(current
, SECURITY__CHECK_CONTEXT
);
317 length
= security_context_to_sid(buf
, size
, &sid
);
321 length
= security_sid_to_context(sid
, &canon
, &len
);
325 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
326 printk(KERN_ERR
"%s: context size (%u) exceeds payload "
327 "max\n", __FUNCTION__
, len
);
332 memcpy(buf
, canon
, len
);
339 static ssize_t
sel_read_checkreqprot(struct file
*filp
, char __user
*buf
,
340 size_t count
, loff_t
*ppos
)
342 char tmpbuf
[TMPBUFLEN
];
345 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", selinux_checkreqprot
);
346 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
349 static ssize_t
sel_write_checkreqprot(struct file
* file
, const char __user
* buf
,
350 size_t count
, loff_t
*ppos
)
354 unsigned int new_value
;
356 length
= task_has_security(current
, SECURITY__SETCHECKREQPROT
);
360 if (count
>= PAGE_SIZE
)
363 /* No partial writes. */
366 page
= (char*)get_zeroed_page(GFP_KERNEL
);
370 if (copy_from_user(page
, buf
, count
))
374 if (sscanf(page
, "%u", &new_value
) != 1)
377 selinux_checkreqprot
= new_value
? 1 : 0;
380 free_page((unsigned long) page
);
383 static const struct file_operations sel_checkreqprot_ops
= {
384 .read
= sel_read_checkreqprot
,
385 .write
= sel_write_checkreqprot
,
388 static ssize_t
sel_read_compat_net(struct file
*filp
, char __user
*buf
,
389 size_t count
, loff_t
*ppos
)
391 char tmpbuf
[TMPBUFLEN
];
394 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_compat_net
);
395 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
398 static ssize_t
sel_write_compat_net(struct file
* file
, const char __user
* buf
,
399 size_t count
, loff_t
*ppos
)
405 length
= task_has_security(current
, SECURITY__LOAD_POLICY
);
409 if (count
>= PAGE_SIZE
)
412 /* No partial writes. */
415 page
= (char*)get_zeroed_page(GFP_KERNEL
);
419 if (copy_from_user(page
, buf
, count
))
423 if (sscanf(page
, "%d", &new_value
) != 1)
426 selinux_compat_net
= new_value
? 1 : 0;
429 free_page((unsigned long) page
);
432 static const struct file_operations sel_compat_net_ops
= {
433 .read
= sel_read_compat_net
,
434 .write
= sel_write_compat_net
,
438 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
440 static ssize_t
sel_write_access(struct file
* file
, char *buf
, size_t size
);
441 static ssize_t
sel_write_create(struct file
* file
, char *buf
, size_t size
);
442 static ssize_t
sel_write_relabel(struct file
* file
, char *buf
, size_t size
);
443 static ssize_t
sel_write_user(struct file
* file
, char *buf
, size_t size
);
444 static ssize_t
sel_write_member(struct file
* file
, char *buf
, size_t size
);
446 static ssize_t (*write_op
[])(struct file
*, char *, size_t) = {
447 [SEL_ACCESS
] = sel_write_access
,
448 [SEL_CREATE
] = sel_write_create
,
449 [SEL_RELABEL
] = sel_write_relabel
,
450 [SEL_USER
] = sel_write_user
,
451 [SEL_MEMBER
] = sel_write_member
,
452 [SEL_CONTEXT
] = sel_write_context
,
455 static ssize_t
selinux_transaction_write(struct file
*file
, const char __user
*buf
, size_t size
, loff_t
*pos
)
457 ino_t ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
461 if (ino
>= ARRAY_SIZE(write_op
) || !write_op
[ino
])
464 data
= simple_transaction_get(file
, buf
, size
);
466 return PTR_ERR(data
);
468 rv
= write_op
[ino
](file
, data
, size
);
470 simple_transaction_set(file
, rv
);
476 static const struct file_operations transaction_ops
= {
477 .write
= selinux_transaction_write
,
478 .read
= simple_transaction_read
,
479 .release
= simple_transaction_release
,
483 * payload - write methods
484 * If the method has a response, the response should be put in buf,
485 * and the length returned. Otherwise return 0 or and -error.
488 static ssize_t
sel_write_access(struct file
* file
, char *buf
, size_t size
)
494 struct av_decision avd
;
497 length
= task_has_security(current
, SECURITY__COMPUTE_AV
);
502 scon
= kzalloc(size
+1, GFP_KERNEL
);
506 tcon
= kzalloc(size
+1, GFP_KERNEL
);
511 if (sscanf(buf
, "%s %s %hu %x", scon
, tcon
, &tclass
, &req
) != 4)
514 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
517 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
521 length
= security_compute_av(ssid
, tsid
, tclass
, req
, &avd
);
525 length
= scnprintf(buf
, SIMPLE_TRANSACTION_LIMIT
,
527 avd
.allowed
, avd
.decided
,
528 avd
.auditallow
, avd
.auditdeny
,
537 static ssize_t
sel_write_create(struct file
* file
, char *buf
, size_t size
)
540 u32 ssid
, tsid
, newsid
;
546 length
= task_has_security(current
, SECURITY__COMPUTE_CREATE
);
551 scon
= kzalloc(size
+1, GFP_KERNEL
);
555 tcon
= kzalloc(size
+1, GFP_KERNEL
);
560 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
563 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
566 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
570 length
= security_transition_sid(ssid
, tsid
, tclass
, &newsid
);
574 length
= security_sid_to_context(newsid
, &newcon
, &len
);
578 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
579 printk(KERN_ERR
"%s: context size (%u) exceeds payload "
580 "max\n", __FUNCTION__
, len
);
585 memcpy(buf
, newcon
, len
);
596 static ssize_t
sel_write_relabel(struct file
* file
, char *buf
, size_t size
)
599 u32 ssid
, tsid
, newsid
;
605 length
= task_has_security(current
, SECURITY__COMPUTE_RELABEL
);
610 scon
= kzalloc(size
+1, GFP_KERNEL
);
614 tcon
= kzalloc(size
+1, GFP_KERNEL
);
619 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
622 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
625 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
629 length
= security_change_sid(ssid
, tsid
, tclass
, &newsid
);
633 length
= security_sid_to_context(newsid
, &newcon
, &len
);
637 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
642 memcpy(buf
, newcon
, len
);
653 static ssize_t
sel_write_user(struct file
* file
, char *buf
, size_t size
)
655 char *con
, *user
, *ptr
;
662 length
= task_has_security(current
, SECURITY__COMPUTE_USER
);
667 con
= kzalloc(size
+1, GFP_KERNEL
);
671 user
= kzalloc(size
+1, GFP_KERNEL
);
676 if (sscanf(buf
, "%s %s", con
, user
) != 2)
679 length
= security_context_to_sid(con
, strlen(con
)+1, &sid
);
683 length
= security_get_user_sids(sid
, user
, &sids
, &nsids
);
687 length
= sprintf(buf
, "%u", nsids
) + 1;
689 for (i
= 0; i
< nsids
; i
++) {
690 rc
= security_sid_to_context(sids
[i
], &newcon
, &len
);
695 if ((length
+ len
) >= SIMPLE_TRANSACTION_LIMIT
) {
700 memcpy(ptr
, newcon
, len
);
714 static ssize_t
sel_write_member(struct file
* file
, char *buf
, size_t size
)
717 u32 ssid
, tsid
, newsid
;
723 length
= task_has_security(current
, SECURITY__COMPUTE_MEMBER
);
728 scon
= kzalloc(size
+1, GFP_KERNEL
);
732 tcon
= kzalloc(size
+1, GFP_KERNEL
);
737 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
740 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
743 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
747 length
= security_member_sid(ssid
, tsid
, tclass
, &newsid
);
751 length
= security_sid_to_context(newsid
, &newcon
, &len
);
755 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
756 printk(KERN_ERR
"%s: context size (%u) exceeds payload "
757 "max\n", __FUNCTION__
, len
);
762 memcpy(buf
, newcon
, len
);
773 static struct inode
*sel_make_inode(struct super_block
*sb
, int mode
)
775 struct inode
*ret
= new_inode(sb
);
779 ret
->i_uid
= ret
->i_gid
= 0;
781 ret
->i_atime
= ret
->i_mtime
= ret
->i_ctime
= CURRENT_TIME
;
786 static ssize_t
sel_read_bool(struct file
*filep
, char __user
*buf
,
787 size_t count
, loff_t
*ppos
)
795 mutex_lock(&sel_mutex
);
799 /* check to see if this file has been deleted */
803 if (count
> PAGE_SIZE
) {
807 if (!(page
= (char*)get_zeroed_page(GFP_KERNEL
))) {
812 inode
= filep
->f_path
.dentry
->d_inode
;
813 cur_enforcing
= security_get_bool_value(inode
->i_ino
&SEL_INO_MASK
);
814 if (cur_enforcing
< 0) {
819 length
= scnprintf(page
, PAGE_SIZE
, "%d %d", cur_enforcing
,
820 bool_pending_values
[inode
->i_ino
&SEL_INO_MASK
]);
821 ret
= simple_read_from_buffer(buf
, count
, ppos
, page
, length
);
823 mutex_unlock(&sel_mutex
);
825 free_page((unsigned long)page
);
829 static ssize_t
sel_write_bool(struct file
*filep
, const char __user
*buf
,
830 size_t count
, loff_t
*ppos
)
833 ssize_t length
= -EFAULT
;
837 mutex_lock(&sel_mutex
);
839 length
= task_has_security(current
, SECURITY__SETBOOL
);
843 /* check to see if this file has been deleted */
847 if (count
>= PAGE_SIZE
) {
852 /* No partial writes. */
855 page
= (char*)get_zeroed_page(GFP_KERNEL
);
861 if (copy_from_user(page
, buf
, count
))
865 if (sscanf(page
, "%d", &new_value
) != 1)
871 inode
= filep
->f_path
.dentry
->d_inode
;
872 bool_pending_values
[inode
->i_ino
&SEL_INO_MASK
] = new_value
;
876 mutex_unlock(&sel_mutex
);
878 free_page((unsigned long) page
);
882 static const struct file_operations sel_bool_ops
= {
883 .read
= sel_read_bool
,
884 .write
= sel_write_bool
,
887 static ssize_t
sel_commit_bools_write(struct file
*filep
,
888 const char __user
*buf
,
889 size_t count
, loff_t
*ppos
)
892 ssize_t length
= -EFAULT
;
895 mutex_lock(&sel_mutex
);
897 length
= task_has_security(current
, SECURITY__SETBOOL
);
901 /* check to see if this file has been deleted */
905 if (count
>= PAGE_SIZE
) {
910 /* No partial writes. */
913 page
= (char*)get_zeroed_page(GFP_KERNEL
);
919 if (copy_from_user(page
, buf
, count
))
923 if (sscanf(page
, "%d", &new_value
) != 1)
926 if (new_value
&& bool_pending_values
) {
927 security_set_bools(bool_num
, bool_pending_values
);
933 mutex_unlock(&sel_mutex
);
935 free_page((unsigned long) page
);
939 static const struct file_operations sel_commit_bools_ops
= {
940 .write
= sel_commit_bools_write
,
943 /* partial revoke() from fs/proc/generic.c proc_kill_inodes */
944 static void sel_remove_entries(struct dentry
*de
)
946 struct list_head
*p
, *node
;
947 struct super_block
*sb
= de
->d_sb
;
949 spin_lock(&dcache_lock
);
950 node
= de
->d_subdirs
.next
;
951 while (node
!= &de
->d_subdirs
) {
952 struct dentry
*d
= list_entry(node
, struct dentry
, d_u
.d_child
);
957 spin_unlock(&dcache_lock
);
959 simple_unlink(de
->d_inode
, d
);
961 spin_lock(&dcache_lock
);
963 node
= de
->d_subdirs
.next
;
966 spin_unlock(&dcache_lock
);
969 list_for_each(p
, &sb
->s_files
) {
970 struct file
* filp
= list_entry(p
, struct file
, f_u
.fu_list
);
971 struct dentry
* dentry
= filp
->f_path
.dentry
;
973 if (dentry
->d_parent
!= de
) {
981 #define BOOL_DIR_NAME "booleans"
983 static int sel_make_bools(void)
987 struct dentry
*dentry
= NULL
;
988 struct dentry
*dir
= bool_dir
;
989 struct inode
*inode
= NULL
;
990 struct inode_security_struct
*isec
;
991 char **names
= NULL
, *page
;
996 /* remove any existing files */
997 kfree(bool_pending_values
);
998 bool_pending_values
= NULL
;
1000 sel_remove_entries(dir
);
1002 if (!(page
= (char*)get_zeroed_page(GFP_KERNEL
)))
1005 ret
= security_get_bools(&num
, &names
, &values
);
1009 for (i
= 0; i
< num
; i
++) {
1010 dentry
= d_alloc_name(dir
, names
[i
]);
1015 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
| S_IRUGO
| S_IWUSR
);
1021 len
= snprintf(page
, PAGE_SIZE
, "/%s/%s", BOOL_DIR_NAME
, names
[i
]);
1025 } else if (len
>= PAGE_SIZE
) {
1026 ret
= -ENAMETOOLONG
;
1029 isec
= (struct inode_security_struct
*)inode
->i_security
;
1030 if ((ret
= security_genfs_sid("selinuxfs", page
, SECCLASS_FILE
, &sid
)))
1033 isec
->initialized
= 1;
1034 inode
->i_fop
= &sel_bool_ops
;
1035 inode
->i_ino
= i
|SEL_BOOL_INO_OFFSET
;
1036 d_add(dentry
, inode
);
1039 bool_pending_values
= values
;
1041 free_page((unsigned long)page
);
1043 for (i
= 0; i
< num
; i
++)
1050 sel_remove_entries(dir
);
1055 #define NULL_FILE_NAME "null"
1057 struct dentry
*selinux_null
= NULL
;
1059 static ssize_t
sel_read_avc_cache_threshold(struct file
*filp
, char __user
*buf
,
1060 size_t count
, loff_t
*ppos
)
1062 char tmpbuf
[TMPBUFLEN
];
1065 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", avc_cache_threshold
);
1066 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1069 static ssize_t
sel_write_avc_cache_threshold(struct file
* file
,
1070 const char __user
* buf
,
1071 size_t count
, loff_t
*ppos
)
1078 if (count
>= PAGE_SIZE
) {
1084 /* No partial writes. */
1089 page
= (char*)get_zeroed_page(GFP_KERNEL
);
1095 if (copy_from_user(page
, buf
, count
)) {
1100 if (sscanf(page
, "%u", &new_value
) != 1) {
1105 if (new_value
!= avc_cache_threshold
) {
1106 ret
= task_has_security(current
, SECURITY__SETSECPARAM
);
1109 avc_cache_threshold
= new_value
;
1113 free_page((unsigned long)page
);
1118 static ssize_t
sel_read_avc_hash_stats(struct file
*filp
, char __user
*buf
,
1119 size_t count
, loff_t
*ppos
)
1124 page
= (char *)__get_free_page(GFP_KERNEL
);
1129 ret
= avc_get_hash_stats(page
);
1131 ret
= simple_read_from_buffer(buf
, count
, ppos
, page
, ret
);
1132 free_page((unsigned long)page
);
1137 static const struct file_operations sel_avc_cache_threshold_ops
= {
1138 .read
= sel_read_avc_cache_threshold
,
1139 .write
= sel_write_avc_cache_threshold
,
1142 static const struct file_operations sel_avc_hash_stats_ops
= {
1143 .read
= sel_read_avc_hash_stats
,
1146 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1147 static struct avc_cache_stats
*sel_avc_get_stat_idx(loff_t
*idx
)
1151 for (cpu
= *idx
; cpu
< NR_CPUS
; ++cpu
) {
1152 if (!cpu_possible(cpu
))
1155 return &per_cpu(avc_cache_stats
, cpu
);
1160 static void *sel_avc_stats_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1162 loff_t n
= *pos
- 1;
1165 return SEQ_START_TOKEN
;
1167 return sel_avc_get_stat_idx(&n
);
1170 static void *sel_avc_stats_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1172 return sel_avc_get_stat_idx(pos
);
1175 static int sel_avc_stats_seq_show(struct seq_file
*seq
, void *v
)
1177 struct avc_cache_stats
*st
= v
;
1179 if (v
== SEQ_START_TOKEN
)
1180 seq_printf(seq
, "lookups hits misses allocations reclaims "
1183 seq_printf(seq
, "%u %u %u %u %u %u\n", st
->lookups
,
1184 st
->hits
, st
->misses
, st
->allocations
,
1185 st
->reclaims
, st
->frees
);
1189 static void sel_avc_stats_seq_stop(struct seq_file
*seq
, void *v
)
1192 static struct seq_operations sel_avc_cache_stats_seq_ops
= {
1193 .start
= sel_avc_stats_seq_start
,
1194 .next
= sel_avc_stats_seq_next
,
1195 .show
= sel_avc_stats_seq_show
,
1196 .stop
= sel_avc_stats_seq_stop
,
1199 static int sel_open_avc_cache_stats(struct inode
*inode
, struct file
*file
)
1201 return seq_open(file
, &sel_avc_cache_stats_seq_ops
);
1204 static const struct file_operations sel_avc_cache_stats_ops
= {
1205 .open
= sel_open_avc_cache_stats
,
1207 .llseek
= seq_lseek
,
1208 .release
= seq_release
,
1212 static int sel_make_avc_files(struct dentry
*dir
)
1215 static struct tree_descr files
[] = {
1216 { "cache_threshold",
1217 &sel_avc_cache_threshold_ops
, S_IRUGO
|S_IWUSR
},
1218 { "hash_stats", &sel_avc_hash_stats_ops
, S_IRUGO
},
1219 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1220 { "cache_stats", &sel_avc_cache_stats_ops
, S_IRUGO
},
1224 for (i
= 0; i
< ARRAY_SIZE(files
); i
++) {
1225 struct inode
*inode
;
1226 struct dentry
*dentry
;
1228 dentry
= d_alloc_name(dir
, files
[i
].name
);
1234 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|files
[i
].mode
);
1239 inode
->i_fop
= files
[i
].ops
;
1240 inode
->i_ino
= ++sel_last_ino
;
1241 d_add(dentry
, inode
);
1247 static ssize_t
sel_read_initcon(struct file
* file
, char __user
*buf
,
1248 size_t count
, loff_t
*ppos
)
1250 struct inode
*inode
;
1255 inode
= file
->f_path
.dentry
->d_inode
;
1256 sid
= inode
->i_ino
&SEL_INO_MASK
;
1257 ret
= security_sid_to_context(sid
, &con
, &len
);
1261 ret
= simple_read_from_buffer(buf
, count
, ppos
, con
, len
);
1266 static const struct file_operations sel_initcon_ops
= {
1267 .read
= sel_read_initcon
,
1270 static int sel_make_initcon_files(struct dentry
*dir
)
1274 for (i
= 1; i
<= SECINITSID_NUM
; i
++) {
1275 struct inode
*inode
;
1276 struct dentry
*dentry
;
1277 dentry
= d_alloc_name(dir
, security_get_initial_sid_context(i
));
1283 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1288 inode
->i_fop
= &sel_initcon_ops
;
1289 inode
->i_ino
= i
|SEL_INITCON_INO_OFFSET
;
1290 d_add(dentry
, inode
);
1296 static int sel_make_dir(struct inode
*dir
, struct dentry
*dentry
)
1299 struct inode
*inode
;
1301 inode
= sel_make_inode(dir
->i_sb
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
1306 inode
->i_op
= &simple_dir_inode_operations
;
1307 inode
->i_fop
= &simple_dir_operations
;
1308 inode
->i_ino
= ++sel_last_ino
;
1309 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1311 d_add(dentry
, inode
);
1312 /* bump link count on parent directory, too */
1318 static int sel_fill_super(struct super_block
* sb
, void * data
, int silent
)
1321 struct dentry
*dentry
;
1322 struct inode
*inode
, *root_inode
;
1323 struct inode_security_struct
*isec
;
1325 static struct tree_descr selinux_files
[] = {
1326 [SEL_LOAD
] = {"load", &sel_load_ops
, S_IRUSR
|S_IWUSR
},
1327 [SEL_ENFORCE
] = {"enforce", &sel_enforce_ops
, S_IRUGO
|S_IWUSR
},
1328 [SEL_CONTEXT
] = {"context", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1329 [SEL_ACCESS
] = {"access", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1330 [SEL_CREATE
] = {"create", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1331 [SEL_RELABEL
] = {"relabel", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1332 [SEL_USER
] = {"user", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1333 [SEL_POLICYVERS
] = {"policyvers", &sel_policyvers_ops
, S_IRUGO
},
1334 [SEL_COMMIT_BOOLS
] = {"commit_pending_bools", &sel_commit_bools_ops
, S_IWUSR
},
1335 [SEL_MLS
] = {"mls", &sel_mls_ops
, S_IRUGO
},
1336 [SEL_DISABLE
] = {"disable", &sel_disable_ops
, S_IWUSR
},
1337 [SEL_MEMBER
] = {"member", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1338 [SEL_CHECKREQPROT
] = {"checkreqprot", &sel_checkreqprot_ops
, S_IRUGO
|S_IWUSR
},
1339 [SEL_COMPAT_NET
] = {"compat_net", &sel_compat_net_ops
, S_IRUGO
|S_IWUSR
},
1342 ret
= simple_fill_super(sb
, SELINUX_MAGIC
, selinux_files
);
1346 root_inode
= sb
->s_root
->d_inode
;
1348 dentry
= d_alloc_name(sb
->s_root
, BOOL_DIR_NAME
);
1354 ret
= sel_make_dir(root_inode
, dentry
);
1360 dentry
= d_alloc_name(sb
->s_root
, NULL_FILE_NAME
);
1366 inode
= sel_make_inode(sb
, S_IFCHR
| S_IRUGO
| S_IWUGO
);
1371 inode
->i_ino
= ++sel_last_ino
;
1372 isec
= (struct inode_security_struct
*)inode
->i_security
;
1373 isec
->sid
= SECINITSID_DEVNULL
;
1374 isec
->sclass
= SECCLASS_CHR_FILE
;
1375 isec
->initialized
= 1;
1377 init_special_inode(inode
, S_IFCHR
| S_IRUGO
| S_IWUGO
, MKDEV(MEM_MAJOR
, 3));
1378 d_add(dentry
, inode
);
1379 selinux_null
= dentry
;
1381 dentry
= d_alloc_name(sb
->s_root
, "avc");
1387 ret
= sel_make_dir(root_inode
, dentry
);
1391 ret
= sel_make_avc_files(dentry
);
1395 dentry
= d_alloc_name(sb
->s_root
, "initial_contexts");
1401 ret
= sel_make_dir(root_inode
, dentry
);
1405 ret
= sel_make_initcon_files(dentry
);
1412 printk(KERN_ERR
"%s: failed while creating inodes\n", __FUNCTION__
);
1416 static int sel_get_sb(struct file_system_type
*fs_type
,
1417 int flags
, const char *dev_name
, void *data
,
1418 struct vfsmount
*mnt
)
1420 return get_sb_single(fs_type
, flags
, data
, sel_fill_super
, mnt
);
1423 static struct file_system_type sel_fs_type
= {
1424 .name
= "selinuxfs",
1425 .get_sb
= sel_get_sb
,
1426 .kill_sb
= kill_litter_super
,
1429 struct vfsmount
*selinuxfs_mount
;
1431 static int __init
init_sel_fs(void)
1435 if (!selinux_enabled
)
1437 err
= register_filesystem(&sel_fs_type
);
1439 selinuxfs_mount
= kern_mount(&sel_fs_type
);
1440 if (IS_ERR(selinuxfs_mount
)) {
1441 printk(KERN_ERR
"selinuxfs: could not mount!\n");
1442 err
= PTR_ERR(selinuxfs_mount
);
1443 selinuxfs_mount
= NULL
;
1449 __initcall(init_sel_fs
);
1451 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1452 void exit_sel_fs(void)
1454 unregister_filesystem(&sel_fs_type
);