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 char **bool_pending_names
;
69 static int *bool_pending_values
= NULL
;
71 /* global data for classes */
72 static struct dentry
*class_dir
= NULL
;
73 static unsigned long last_class_ino
;
75 extern void selnl_notify_setenforce(int val
);
77 /* Check whether a task is allowed to use a security operation. */
78 static int task_has_security(struct task_struct
*tsk
,
81 struct task_security_struct
*tsec
;
87 return avc_has_perm(tsec
->sid
, SECINITSID_SECURITY
,
88 SECCLASS_SECURITY
, perms
, NULL
);
93 SEL_LOAD
, /* load policy */
94 SEL_ENFORCE
, /* get or set enforcing status */
95 SEL_CONTEXT
, /* validate context */
96 SEL_ACCESS
, /* compute access decision */
97 SEL_CREATE
, /* compute create labeling decision */
98 SEL_RELABEL
, /* compute relabeling decision */
99 SEL_USER
, /* compute reachable user contexts */
100 SEL_POLICYVERS
, /* return policy version for this kernel */
101 SEL_COMMIT_BOOLS
, /* commit new boolean values */
102 SEL_MLS
, /* return if MLS policy is enabled */
103 SEL_DISABLE
, /* disable SELinux until next reboot */
104 SEL_MEMBER
, /* compute polyinstantiation membership decision */
105 SEL_CHECKREQPROT
, /* check requested protection, not kernel-applied one */
106 SEL_COMPAT_NET
, /* whether to use old compat network packet controls */
107 SEL_REJECT_UNKNOWN
, /* export unknown reject handling to userspace */
108 SEL_DENY_UNKNOWN
, /* export unknown deny handling to userspace */
109 SEL_INO_NEXT
, /* The next inode number to use */
112 static unsigned long sel_last_ino
= SEL_INO_NEXT
- 1;
114 #define SEL_INITCON_INO_OFFSET 0x01000000
115 #define SEL_BOOL_INO_OFFSET 0x02000000
116 #define SEL_CLASS_INO_OFFSET 0x04000000
117 #define SEL_INO_MASK 0x00ffffff
120 static ssize_t
sel_read_enforce(struct file
*filp
, char __user
*buf
,
121 size_t count
, loff_t
*ppos
)
123 char tmpbuf
[TMPBUFLEN
];
126 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_enforcing
);
127 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
130 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
131 static ssize_t
sel_write_enforce(struct file
* file
, const char __user
* buf
,
132 size_t count
, loff_t
*ppos
)
139 if (count
>= PAGE_SIZE
)
142 /* No partial writes. */
145 page
= (char*)get_zeroed_page(GFP_KERNEL
);
149 if (copy_from_user(page
, buf
, count
))
153 if (sscanf(page
, "%d", &new_value
) != 1)
156 if (new_value
!= selinux_enforcing
) {
157 length
= task_has_security(current
, SECURITY__SETENFORCE
);
160 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_STATUS
,
161 "enforcing=%d old_enforcing=%d auid=%u", new_value
,
163 audit_get_loginuid(current
->audit_context
));
164 selinux_enforcing
= new_value
;
165 if (selinux_enforcing
)
167 selnl_notify_setenforce(selinux_enforcing
);
171 free_page((unsigned long) page
);
175 #define sel_write_enforce NULL
178 static const struct file_operations sel_enforce_ops
= {
179 .read
= sel_read_enforce
,
180 .write
= sel_write_enforce
,
183 static ssize_t
sel_read_handle_unknown(struct file
*filp
, char __user
*buf
,
184 size_t count
, loff_t
*ppos
)
186 char tmpbuf
[TMPBUFLEN
];
188 ino_t ino
= filp
->f_path
.dentry
->d_inode
->i_ino
;
189 int handle_unknown
= (ino
== SEL_REJECT_UNKNOWN
) ?
190 security_get_reject_unknown() : !security_get_allow_unknown();
192 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", handle_unknown
);
193 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
196 static const struct file_operations sel_handle_unknown_ops
= {
197 .read
= sel_read_handle_unknown
,
200 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
201 static ssize_t
sel_write_disable(struct file
* file
, const char __user
* buf
,
202 size_t count
, loff_t
*ppos
)
208 extern int selinux_disable(void);
210 if (count
>= PAGE_SIZE
)
213 /* No partial writes. */
216 page
= (char*)get_zeroed_page(GFP_KERNEL
);
220 if (copy_from_user(page
, buf
, count
))
224 if (sscanf(page
, "%d", &new_value
) != 1)
228 length
= selinux_disable();
231 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_STATUS
,
233 audit_get_loginuid(current
->audit_context
));
238 free_page((unsigned long) page
);
242 #define sel_write_disable NULL
245 static const struct file_operations sel_disable_ops
= {
246 .write
= sel_write_disable
,
249 static ssize_t
sel_read_policyvers(struct file
*filp
, char __user
*buf
,
250 size_t count
, loff_t
*ppos
)
252 char tmpbuf
[TMPBUFLEN
];
255 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", POLICYDB_VERSION_MAX
);
256 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
259 static const struct file_operations sel_policyvers_ops
= {
260 .read
= sel_read_policyvers
,
263 /* declaration for sel_write_load */
264 static int sel_make_bools(void);
265 static int sel_make_classes(void);
267 /* declaration for sel_make_class_dirs */
268 static int sel_make_dir(struct inode
*dir
, struct dentry
*dentry
,
271 static ssize_t
sel_read_mls(struct file
*filp
, char __user
*buf
,
272 size_t count
, loff_t
*ppos
)
274 char tmpbuf
[TMPBUFLEN
];
277 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_mls_enabled
);
278 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
281 static const struct file_operations sel_mls_ops
= {
282 .read
= sel_read_mls
,
285 static ssize_t
sel_write_load(struct file
* file
, const char __user
* buf
,
286 size_t count
, loff_t
*ppos
)
293 mutex_lock(&sel_mutex
);
295 length
= task_has_security(current
, SECURITY__LOAD_POLICY
);
300 /* No partial writes. */
305 if ((count
> 64 * 1024 * 1024)
306 || (data
= vmalloc(count
)) == NULL
) {
312 if (copy_from_user(data
, buf
, count
) != 0)
315 length
= security_load_policy(data
, count
);
319 ret
= sel_make_bools();
325 ret
= sel_make_classes();
333 printk(KERN_INFO
"SELinux: policy loaded with handle_unknown=%s\n",
334 (security_get_reject_unknown() ? "reject" :
335 (security_get_allow_unknown() ? "allow" : "deny")));
337 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_POLICY_LOAD
,
338 "policy loaded auid=%u",
339 audit_get_loginuid(current
->audit_context
));
341 mutex_unlock(&sel_mutex
);
346 static const struct file_operations sel_load_ops
= {
347 .write
= sel_write_load
,
350 static ssize_t
sel_write_context(struct file
* file
, char *buf
, size_t size
)
356 length
= task_has_security(current
, SECURITY__CHECK_CONTEXT
);
360 length
= security_context_to_sid(buf
, size
, &sid
);
364 length
= security_sid_to_context(sid
, &canon
, &len
);
368 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
369 printk(KERN_ERR
"%s: context size (%u) exceeds payload "
370 "max\n", __FUNCTION__
, len
);
375 memcpy(buf
, canon
, len
);
382 static ssize_t
sel_read_checkreqprot(struct file
*filp
, char __user
*buf
,
383 size_t count
, loff_t
*ppos
)
385 char tmpbuf
[TMPBUFLEN
];
388 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", selinux_checkreqprot
);
389 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
392 static ssize_t
sel_write_checkreqprot(struct file
* file
, const char __user
* buf
,
393 size_t count
, loff_t
*ppos
)
397 unsigned int new_value
;
399 length
= task_has_security(current
, SECURITY__SETCHECKREQPROT
);
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
, "%u", &new_value
) != 1)
420 selinux_checkreqprot
= new_value
? 1 : 0;
423 free_page((unsigned long) page
);
426 static const struct file_operations sel_checkreqprot_ops
= {
427 .read
= sel_read_checkreqprot
,
428 .write
= sel_write_checkreqprot
,
431 static ssize_t
sel_read_compat_net(struct file
*filp
, char __user
*buf
,
432 size_t count
, loff_t
*ppos
)
434 char tmpbuf
[TMPBUFLEN
];
437 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_compat_net
);
438 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
441 static ssize_t
sel_write_compat_net(struct file
* file
, const char __user
* buf
,
442 size_t count
, loff_t
*ppos
)
448 length
= task_has_security(current
, SECURITY__LOAD_POLICY
);
452 if (count
>= PAGE_SIZE
)
455 /* No partial writes. */
458 page
= (char*)get_zeroed_page(GFP_KERNEL
);
462 if (copy_from_user(page
, buf
, count
))
466 if (sscanf(page
, "%d", &new_value
) != 1)
469 selinux_compat_net
= new_value
? 1 : 0;
472 free_page((unsigned long) page
);
475 static const struct file_operations sel_compat_net_ops
= {
476 .read
= sel_read_compat_net
,
477 .write
= sel_write_compat_net
,
481 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
483 static ssize_t
sel_write_access(struct file
* file
, char *buf
, size_t size
);
484 static ssize_t
sel_write_create(struct file
* file
, char *buf
, size_t size
);
485 static ssize_t
sel_write_relabel(struct file
* file
, char *buf
, size_t size
);
486 static ssize_t
sel_write_user(struct file
* file
, char *buf
, size_t size
);
487 static ssize_t
sel_write_member(struct file
* file
, char *buf
, size_t size
);
489 static ssize_t (*write_op
[])(struct file
*, char *, size_t) = {
490 [SEL_ACCESS
] = sel_write_access
,
491 [SEL_CREATE
] = sel_write_create
,
492 [SEL_RELABEL
] = sel_write_relabel
,
493 [SEL_USER
] = sel_write_user
,
494 [SEL_MEMBER
] = sel_write_member
,
495 [SEL_CONTEXT
] = sel_write_context
,
498 static ssize_t
selinux_transaction_write(struct file
*file
, const char __user
*buf
, size_t size
, loff_t
*pos
)
500 ino_t ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
504 if (ino
>= ARRAY_SIZE(write_op
) || !write_op
[ino
])
507 data
= simple_transaction_get(file
, buf
, size
);
509 return PTR_ERR(data
);
511 rv
= write_op
[ino
](file
, data
, size
);
513 simple_transaction_set(file
, rv
);
519 static const struct file_operations transaction_ops
= {
520 .write
= selinux_transaction_write
,
521 .read
= simple_transaction_read
,
522 .release
= simple_transaction_release
,
526 * payload - write methods
527 * If the method has a response, the response should be put in buf,
528 * and the length returned. Otherwise return 0 or and -error.
531 static ssize_t
sel_write_access(struct file
* file
, char *buf
, size_t size
)
537 struct av_decision avd
;
540 length
= task_has_security(current
, SECURITY__COMPUTE_AV
);
545 scon
= kzalloc(size
+1, GFP_KERNEL
);
549 tcon
= kzalloc(size
+1, GFP_KERNEL
);
554 if (sscanf(buf
, "%s %s %hu %x", scon
, tcon
, &tclass
, &req
) != 4)
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_compute_av(ssid
, tsid
, tclass
, req
, &avd
);
568 length
= scnprintf(buf
, SIMPLE_TRANSACTION_LIMIT
,
570 avd
.allowed
, avd
.decided
,
571 avd
.auditallow
, avd
.auditdeny
,
580 static ssize_t
sel_write_create(struct file
* file
, char *buf
, size_t size
)
583 u32 ssid
, tsid
, newsid
;
589 length
= task_has_security(current
, SECURITY__COMPUTE_CREATE
);
594 scon
= kzalloc(size
+1, GFP_KERNEL
);
598 tcon
= kzalloc(size
+1, GFP_KERNEL
);
603 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
606 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
609 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
613 length
= security_transition_sid(ssid
, tsid
, tclass
, &newsid
);
617 length
= security_sid_to_context(newsid
, &newcon
, &len
);
621 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
622 printk(KERN_ERR
"%s: context size (%u) exceeds payload "
623 "max\n", __FUNCTION__
, len
);
628 memcpy(buf
, newcon
, len
);
639 static ssize_t
sel_write_relabel(struct file
* file
, char *buf
, size_t size
)
642 u32 ssid
, tsid
, newsid
;
648 length
= task_has_security(current
, SECURITY__COMPUTE_RELABEL
);
653 scon
= kzalloc(size
+1, GFP_KERNEL
);
657 tcon
= kzalloc(size
+1, GFP_KERNEL
);
662 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
665 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
668 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
672 length
= security_change_sid(ssid
, tsid
, tclass
, &newsid
);
676 length
= security_sid_to_context(newsid
, &newcon
, &len
);
680 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
685 memcpy(buf
, newcon
, len
);
696 static ssize_t
sel_write_user(struct file
* file
, char *buf
, size_t size
)
698 char *con
, *user
, *ptr
;
705 length
= task_has_security(current
, SECURITY__COMPUTE_USER
);
710 con
= kzalloc(size
+1, GFP_KERNEL
);
714 user
= kzalloc(size
+1, GFP_KERNEL
);
719 if (sscanf(buf
, "%s %s", con
, user
) != 2)
722 length
= security_context_to_sid(con
, strlen(con
)+1, &sid
);
726 length
= security_get_user_sids(sid
, user
, &sids
, &nsids
);
730 length
= sprintf(buf
, "%u", nsids
) + 1;
732 for (i
= 0; i
< nsids
; i
++) {
733 rc
= security_sid_to_context(sids
[i
], &newcon
, &len
);
738 if ((length
+ len
) >= SIMPLE_TRANSACTION_LIMIT
) {
743 memcpy(ptr
, newcon
, len
);
757 static ssize_t
sel_write_member(struct file
* file
, char *buf
, size_t size
)
760 u32 ssid
, tsid
, newsid
;
766 length
= task_has_security(current
, SECURITY__COMPUTE_MEMBER
);
771 scon
= kzalloc(size
+1, GFP_KERNEL
);
775 tcon
= kzalloc(size
+1, GFP_KERNEL
);
780 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
783 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
786 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
790 length
= security_member_sid(ssid
, tsid
, tclass
, &newsid
);
794 length
= security_sid_to_context(newsid
, &newcon
, &len
);
798 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
799 printk(KERN_ERR
"%s: context size (%u) exceeds payload "
800 "max\n", __FUNCTION__
, len
);
805 memcpy(buf
, newcon
, len
);
816 static struct inode
*sel_make_inode(struct super_block
*sb
, int mode
)
818 struct inode
*ret
= new_inode(sb
);
822 ret
->i_uid
= ret
->i_gid
= 0;
824 ret
->i_atime
= ret
->i_mtime
= ret
->i_ctime
= CURRENT_TIME
;
829 static ssize_t
sel_read_bool(struct file
*filep
, char __user
*buf
,
830 size_t count
, loff_t
*ppos
)
836 struct inode
*inode
= filep
->f_path
.dentry
->d_inode
;
837 unsigned index
= inode
->i_ino
& SEL_INO_MASK
;
838 const char *name
= filep
->f_path
.dentry
->d_name
.name
;
840 mutex_lock(&sel_mutex
);
842 if (index
>= bool_num
|| strcmp(name
, bool_pending_names
[index
])) {
847 if (count
> PAGE_SIZE
) {
851 if (!(page
= (char*)get_zeroed_page(GFP_KERNEL
))) {
856 cur_enforcing
= security_get_bool_value(index
);
857 if (cur_enforcing
< 0) {
861 length
= scnprintf(page
, PAGE_SIZE
, "%d %d", cur_enforcing
,
862 bool_pending_values
[index
]);
863 ret
= simple_read_from_buffer(buf
, count
, ppos
, page
, length
);
865 mutex_unlock(&sel_mutex
);
867 free_page((unsigned long)page
);
871 static ssize_t
sel_write_bool(struct file
*filep
, const char __user
*buf
,
872 size_t count
, loff_t
*ppos
)
877 struct inode
*inode
= filep
->f_path
.dentry
->d_inode
;
878 unsigned index
= inode
->i_ino
& SEL_INO_MASK
;
879 const char *name
= filep
->f_path
.dentry
->d_name
.name
;
881 mutex_lock(&sel_mutex
);
883 length
= task_has_security(current
, SECURITY__SETBOOL
);
887 if (index
>= bool_num
|| strcmp(name
, bool_pending_names
[index
])) {
892 if (count
>= PAGE_SIZE
) {
898 /* No partial writes. */
902 page
= (char*)get_zeroed_page(GFP_KERNEL
);
909 if (copy_from_user(page
, buf
, count
))
913 if (sscanf(page
, "%d", &new_value
) != 1)
919 bool_pending_values
[index
] = new_value
;
923 mutex_unlock(&sel_mutex
);
925 free_page((unsigned long) page
);
929 static const struct file_operations sel_bool_ops
= {
930 .read
= sel_read_bool
,
931 .write
= sel_write_bool
,
934 static ssize_t
sel_commit_bools_write(struct file
*filep
,
935 const char __user
*buf
,
936 size_t count
, loff_t
*ppos
)
942 mutex_lock(&sel_mutex
);
944 length
= task_has_security(current
, SECURITY__SETBOOL
);
948 if (count
>= PAGE_SIZE
) {
953 /* No partial writes. */
956 page
= (char*)get_zeroed_page(GFP_KERNEL
);
963 if (copy_from_user(page
, buf
, count
))
967 if (sscanf(page
, "%d", &new_value
) != 1)
970 if (new_value
&& bool_pending_values
) {
971 security_set_bools(bool_num
, bool_pending_values
);
977 mutex_unlock(&sel_mutex
);
979 free_page((unsigned long) page
);
983 static const struct file_operations sel_commit_bools_ops
= {
984 .write
= sel_commit_bools_write
,
987 static void sel_remove_entries(struct dentry
*de
)
989 struct list_head
*node
;
991 spin_lock(&dcache_lock
);
992 node
= de
->d_subdirs
.next
;
993 while (node
!= &de
->d_subdirs
) {
994 struct dentry
*d
= list_entry(node
, struct dentry
, d_u
.d_child
);
999 spin_unlock(&dcache_lock
);
1001 simple_unlink(de
->d_inode
, d
);
1003 spin_lock(&dcache_lock
);
1005 node
= de
->d_subdirs
.next
;
1008 spin_unlock(&dcache_lock
);
1011 #define BOOL_DIR_NAME "booleans"
1013 static int sel_make_bools(void)
1017 struct dentry
*dentry
= NULL
;
1018 struct dentry
*dir
= bool_dir
;
1019 struct inode
*inode
= NULL
;
1020 struct inode_security_struct
*isec
;
1021 char **names
= NULL
, *page
;
1026 /* remove any existing files */
1027 kfree(bool_pending_names
);
1028 kfree(bool_pending_values
);
1029 bool_pending_names
= NULL
;
1030 bool_pending_values
= NULL
;
1032 sel_remove_entries(dir
);
1034 if (!(page
= (char*)get_zeroed_page(GFP_KERNEL
)))
1037 ret
= security_get_bools(&num
, &names
, &values
);
1041 for (i
= 0; i
< num
; i
++) {
1042 dentry
= d_alloc_name(dir
, names
[i
]);
1047 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
| S_IRUGO
| S_IWUSR
);
1053 len
= snprintf(page
, PAGE_SIZE
, "/%s/%s", BOOL_DIR_NAME
, names
[i
]);
1057 } else if (len
>= PAGE_SIZE
) {
1058 ret
= -ENAMETOOLONG
;
1061 isec
= (struct inode_security_struct
*)inode
->i_security
;
1062 if ((ret
= security_genfs_sid("selinuxfs", page
, SECCLASS_FILE
, &sid
)))
1065 isec
->initialized
= 1;
1066 inode
->i_fop
= &sel_bool_ops
;
1067 inode
->i_ino
= i
|SEL_BOOL_INO_OFFSET
;
1068 d_add(dentry
, inode
);
1071 bool_pending_names
= names
;
1072 bool_pending_values
= values
;
1074 free_page((unsigned long)page
);
1078 for (i
= 0; i
< num
; i
++)
1083 sel_remove_entries(dir
);
1088 #define NULL_FILE_NAME "null"
1090 struct dentry
*selinux_null
= NULL
;
1092 static ssize_t
sel_read_avc_cache_threshold(struct file
*filp
, char __user
*buf
,
1093 size_t count
, loff_t
*ppos
)
1095 char tmpbuf
[TMPBUFLEN
];
1098 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", avc_cache_threshold
);
1099 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1102 static ssize_t
sel_write_avc_cache_threshold(struct file
* file
,
1103 const char __user
* buf
,
1104 size_t count
, loff_t
*ppos
)
1111 if (count
>= PAGE_SIZE
) {
1117 /* No partial writes. */
1122 page
= (char*)get_zeroed_page(GFP_KERNEL
);
1128 if (copy_from_user(page
, buf
, count
)) {
1133 if (sscanf(page
, "%u", &new_value
) != 1) {
1138 if (new_value
!= avc_cache_threshold
) {
1139 ret
= task_has_security(current
, SECURITY__SETSECPARAM
);
1142 avc_cache_threshold
= new_value
;
1146 free_page((unsigned long)page
);
1151 static ssize_t
sel_read_avc_hash_stats(struct file
*filp
, char __user
*buf
,
1152 size_t count
, loff_t
*ppos
)
1157 page
= (char *)__get_free_page(GFP_KERNEL
);
1162 ret
= avc_get_hash_stats(page
);
1164 ret
= simple_read_from_buffer(buf
, count
, ppos
, page
, ret
);
1165 free_page((unsigned long)page
);
1170 static const struct file_operations sel_avc_cache_threshold_ops
= {
1171 .read
= sel_read_avc_cache_threshold
,
1172 .write
= sel_write_avc_cache_threshold
,
1175 static const struct file_operations sel_avc_hash_stats_ops
= {
1176 .read
= sel_read_avc_hash_stats
,
1179 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1180 static struct avc_cache_stats
*sel_avc_get_stat_idx(loff_t
*idx
)
1184 for (cpu
= *idx
; cpu
< NR_CPUS
; ++cpu
) {
1185 if (!cpu_possible(cpu
))
1188 return &per_cpu(avc_cache_stats
, cpu
);
1193 static void *sel_avc_stats_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1195 loff_t n
= *pos
- 1;
1198 return SEQ_START_TOKEN
;
1200 return sel_avc_get_stat_idx(&n
);
1203 static void *sel_avc_stats_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1205 return sel_avc_get_stat_idx(pos
);
1208 static int sel_avc_stats_seq_show(struct seq_file
*seq
, void *v
)
1210 struct avc_cache_stats
*st
= v
;
1212 if (v
== SEQ_START_TOKEN
)
1213 seq_printf(seq
, "lookups hits misses allocations reclaims "
1216 seq_printf(seq
, "%u %u %u %u %u %u\n", st
->lookups
,
1217 st
->hits
, st
->misses
, st
->allocations
,
1218 st
->reclaims
, st
->frees
);
1222 static void sel_avc_stats_seq_stop(struct seq_file
*seq
, void *v
)
1225 static struct seq_operations sel_avc_cache_stats_seq_ops
= {
1226 .start
= sel_avc_stats_seq_start
,
1227 .next
= sel_avc_stats_seq_next
,
1228 .show
= sel_avc_stats_seq_show
,
1229 .stop
= sel_avc_stats_seq_stop
,
1232 static int sel_open_avc_cache_stats(struct inode
*inode
, struct file
*file
)
1234 return seq_open(file
, &sel_avc_cache_stats_seq_ops
);
1237 static const struct file_operations sel_avc_cache_stats_ops
= {
1238 .open
= sel_open_avc_cache_stats
,
1240 .llseek
= seq_lseek
,
1241 .release
= seq_release
,
1245 static int sel_make_avc_files(struct dentry
*dir
)
1248 static struct tree_descr files
[] = {
1249 { "cache_threshold",
1250 &sel_avc_cache_threshold_ops
, S_IRUGO
|S_IWUSR
},
1251 { "hash_stats", &sel_avc_hash_stats_ops
, S_IRUGO
},
1252 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1253 { "cache_stats", &sel_avc_cache_stats_ops
, S_IRUGO
},
1257 for (i
= 0; i
< ARRAY_SIZE(files
); i
++) {
1258 struct inode
*inode
;
1259 struct dentry
*dentry
;
1261 dentry
= d_alloc_name(dir
, files
[i
].name
);
1267 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|files
[i
].mode
);
1272 inode
->i_fop
= files
[i
].ops
;
1273 inode
->i_ino
= ++sel_last_ino
;
1274 d_add(dentry
, inode
);
1280 static ssize_t
sel_read_initcon(struct file
* file
, char __user
*buf
,
1281 size_t count
, loff_t
*ppos
)
1283 struct inode
*inode
;
1288 inode
= file
->f_path
.dentry
->d_inode
;
1289 sid
= inode
->i_ino
&SEL_INO_MASK
;
1290 ret
= security_sid_to_context(sid
, &con
, &len
);
1294 ret
= simple_read_from_buffer(buf
, count
, ppos
, con
, len
);
1299 static const struct file_operations sel_initcon_ops
= {
1300 .read
= sel_read_initcon
,
1303 static int sel_make_initcon_files(struct dentry
*dir
)
1307 for (i
= 1; i
<= SECINITSID_NUM
; i
++) {
1308 struct inode
*inode
;
1309 struct dentry
*dentry
;
1310 dentry
= d_alloc_name(dir
, security_get_initial_sid_context(i
));
1316 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1321 inode
->i_fop
= &sel_initcon_ops
;
1322 inode
->i_ino
= i
|SEL_INITCON_INO_OFFSET
;
1323 d_add(dentry
, inode
);
1329 static inline unsigned int sel_div(unsigned long a
, unsigned long b
)
1331 return a
/ b
- (a
% b
< 0);
1334 static inline unsigned long sel_class_to_ino(u16
class)
1336 return (class * (SEL_VEC_MAX
+ 1)) | SEL_CLASS_INO_OFFSET
;
1339 static inline u16
sel_ino_to_class(unsigned long ino
)
1341 return sel_div(ino
& SEL_INO_MASK
, SEL_VEC_MAX
+ 1);
1344 static inline unsigned long sel_perm_to_ino(u16
class, u32 perm
)
1346 return (class * (SEL_VEC_MAX
+ 1) + perm
) | SEL_CLASS_INO_OFFSET
;
1349 static inline u32
sel_ino_to_perm(unsigned long ino
)
1351 return (ino
& SEL_INO_MASK
) % (SEL_VEC_MAX
+ 1);
1354 static ssize_t
sel_read_class(struct file
* file
, char __user
*buf
,
1355 size_t count
, loff_t
*ppos
)
1359 unsigned long ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
1361 page
= (char *)__get_free_page(GFP_KERNEL
);
1367 len
= snprintf(page
, PAGE_SIZE
, "%d", sel_ino_to_class(ino
));
1368 rc
= simple_read_from_buffer(buf
, count
, ppos
, page
, len
);
1369 free_page((unsigned long)page
);
1374 static const struct file_operations sel_class_ops
= {
1375 .read
= sel_read_class
,
1378 static ssize_t
sel_read_perm(struct file
* file
, char __user
*buf
,
1379 size_t count
, loff_t
*ppos
)
1383 unsigned long ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
1385 page
= (char *)__get_free_page(GFP_KERNEL
);
1391 len
= snprintf(page
, PAGE_SIZE
,"%d", sel_ino_to_perm(ino
));
1392 rc
= simple_read_from_buffer(buf
, count
, ppos
, page
, len
);
1393 free_page((unsigned long)page
);
1398 static const struct file_operations sel_perm_ops
= {
1399 .read
= sel_read_perm
,
1402 static int sel_make_perm_files(char *objclass
, int classvalue
,
1405 int i
, rc
= 0, nperms
;
1408 rc
= security_get_permissions(objclass
, &perms
, &nperms
);
1412 for (i
= 0; i
< nperms
; i
++) {
1413 struct inode
*inode
;
1414 struct dentry
*dentry
;
1416 dentry
= d_alloc_name(dir
, perms
[i
]);
1422 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1427 inode
->i_fop
= &sel_perm_ops
;
1428 /* i+1 since perm values are 1-indexed */
1429 inode
->i_ino
= sel_perm_to_ino(classvalue
, i
+1);
1430 d_add(dentry
, inode
);
1434 for (i
= 0; i
< nperms
; i
++)
1441 static int sel_make_class_dir_entries(char *classname
, int index
,
1444 struct dentry
*dentry
= NULL
;
1445 struct inode
*inode
= NULL
;
1448 dentry
= d_alloc_name(dir
, "index");
1454 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1460 inode
->i_fop
= &sel_class_ops
;
1461 inode
->i_ino
= sel_class_to_ino(index
);
1462 d_add(dentry
, inode
);
1464 dentry
= d_alloc_name(dir
, "perms");
1470 rc
= sel_make_dir(dir
->d_inode
, dentry
, &last_class_ino
);
1474 rc
= sel_make_perm_files(classname
, index
, dentry
);
1480 static void sel_remove_classes(void)
1482 struct list_head
*class_node
;
1484 list_for_each(class_node
, &class_dir
->d_subdirs
) {
1485 struct dentry
*class_subdir
= list_entry(class_node
,
1486 struct dentry
, d_u
.d_child
);
1487 struct list_head
*class_subdir_node
;
1489 list_for_each(class_subdir_node
, &class_subdir
->d_subdirs
) {
1490 struct dentry
*d
= list_entry(class_subdir_node
,
1491 struct dentry
, d_u
.d_child
);
1494 if (d
->d_inode
->i_mode
& S_IFDIR
)
1495 sel_remove_entries(d
);
1498 sel_remove_entries(class_subdir
);
1501 sel_remove_entries(class_dir
);
1504 static int sel_make_classes(void)
1506 int rc
= 0, nclasses
, i
;
1509 /* delete any existing entries */
1510 sel_remove_classes();
1512 rc
= security_get_classes(&classes
, &nclasses
);
1516 /* +2 since classes are 1-indexed */
1517 last_class_ino
= sel_class_to_ino(nclasses
+2);
1519 for (i
= 0; i
< nclasses
; i
++) {
1520 struct dentry
*class_name_dir
;
1522 class_name_dir
= d_alloc_name(class_dir
, classes
[i
]);
1523 if (!class_name_dir
) {
1528 rc
= sel_make_dir(class_dir
->d_inode
, class_name_dir
,
1533 /* i+1 since class values are 1-indexed */
1534 rc
= sel_make_class_dir_entries(classes
[i
], i
+1,
1541 for (i
= 0; i
< nclasses
; i
++)
1548 static int sel_make_dir(struct inode
*dir
, struct dentry
*dentry
,
1552 struct inode
*inode
;
1554 inode
= sel_make_inode(dir
->i_sb
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
1559 inode
->i_op
= &simple_dir_inode_operations
;
1560 inode
->i_fop
= &simple_dir_operations
;
1561 inode
->i_ino
= ++(*ino
);
1562 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1564 d_add(dentry
, inode
);
1565 /* bump link count on parent directory, too */
1571 static int sel_fill_super(struct super_block
* sb
, void * data
, int silent
)
1574 struct dentry
*dentry
;
1575 struct inode
*inode
, *root_inode
;
1576 struct inode_security_struct
*isec
;
1578 static struct tree_descr selinux_files
[] = {
1579 [SEL_LOAD
] = {"load", &sel_load_ops
, S_IRUSR
|S_IWUSR
},
1580 [SEL_ENFORCE
] = {"enforce", &sel_enforce_ops
, S_IRUGO
|S_IWUSR
},
1581 [SEL_CONTEXT
] = {"context", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1582 [SEL_ACCESS
] = {"access", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1583 [SEL_CREATE
] = {"create", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1584 [SEL_RELABEL
] = {"relabel", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1585 [SEL_USER
] = {"user", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1586 [SEL_POLICYVERS
] = {"policyvers", &sel_policyvers_ops
, S_IRUGO
},
1587 [SEL_COMMIT_BOOLS
] = {"commit_pending_bools", &sel_commit_bools_ops
, S_IWUSR
},
1588 [SEL_MLS
] = {"mls", &sel_mls_ops
, S_IRUGO
},
1589 [SEL_DISABLE
] = {"disable", &sel_disable_ops
, S_IWUSR
},
1590 [SEL_MEMBER
] = {"member", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1591 [SEL_CHECKREQPROT
] = {"checkreqprot", &sel_checkreqprot_ops
, S_IRUGO
|S_IWUSR
},
1592 [SEL_COMPAT_NET
] = {"compat_net", &sel_compat_net_ops
, S_IRUGO
|S_IWUSR
},
1593 [SEL_REJECT_UNKNOWN
] = {"reject_unknown", &sel_handle_unknown_ops
, S_IRUGO
},
1594 [SEL_DENY_UNKNOWN
] = {"deny_unknown", &sel_handle_unknown_ops
, S_IRUGO
},
1597 ret
= simple_fill_super(sb
, SELINUX_MAGIC
, selinux_files
);
1601 root_inode
= sb
->s_root
->d_inode
;
1603 dentry
= d_alloc_name(sb
->s_root
, BOOL_DIR_NAME
);
1609 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1615 dentry
= d_alloc_name(sb
->s_root
, NULL_FILE_NAME
);
1621 inode
= sel_make_inode(sb
, S_IFCHR
| S_IRUGO
| S_IWUGO
);
1626 inode
->i_ino
= ++sel_last_ino
;
1627 isec
= (struct inode_security_struct
*)inode
->i_security
;
1628 isec
->sid
= SECINITSID_DEVNULL
;
1629 isec
->sclass
= SECCLASS_CHR_FILE
;
1630 isec
->initialized
= 1;
1632 init_special_inode(inode
, S_IFCHR
| S_IRUGO
| S_IWUGO
, MKDEV(MEM_MAJOR
, 3));
1633 d_add(dentry
, inode
);
1634 selinux_null
= dentry
;
1636 dentry
= d_alloc_name(sb
->s_root
, "avc");
1642 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1646 ret
= sel_make_avc_files(dentry
);
1650 dentry
= d_alloc_name(sb
->s_root
, "initial_contexts");
1656 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1660 ret
= sel_make_initcon_files(dentry
);
1664 dentry
= d_alloc_name(sb
->s_root
, "class");
1670 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1679 printk(KERN_ERR
"%s: failed while creating inodes\n", __FUNCTION__
);
1683 static int sel_get_sb(struct file_system_type
*fs_type
,
1684 int flags
, const char *dev_name
, void *data
,
1685 struct vfsmount
*mnt
)
1687 return get_sb_single(fs_type
, flags
, data
, sel_fill_super
, mnt
);
1690 static struct file_system_type sel_fs_type
= {
1691 .name
= "selinuxfs",
1692 .get_sb
= sel_get_sb
,
1693 .kill_sb
= kill_litter_super
,
1696 struct vfsmount
*selinuxfs_mount
;
1698 static int __init
init_sel_fs(void)
1702 if (!selinux_enabled
)
1704 err
= register_filesystem(&sel_fs_type
);
1706 selinuxfs_mount
= kern_mount(&sel_fs_type
);
1707 if (IS_ERR(selinuxfs_mount
)) {
1708 printk(KERN_ERR
"selinuxfs: could not mount!\n");
1709 err
= PTR_ERR(selinuxfs_mount
);
1710 selinuxfs_mount
= NULL
;
1716 __initcall(init_sel_fs
);
1718 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1719 void exit_sel_fs(void)
1721 unregister_filesystem(&sel_fs_type
);