1 /* Updated: Karl MacMillan <kmacmillan@tresys.com>
3 * Added conditional policy language extensions
5 * Updated: Hewlett-Packard <paul.moore@hp.com>
7 * Added support for the policy capability bitmap
9 * Copyright (C) 2007 Hewlett-Packard Development Company, L.P.
10 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
11 * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, version 2.
17 #include <linux/kernel.h>
18 #include <linux/pagemap.h>
19 #include <linux/slab.h>
20 #include <linux/vmalloc.h>
22 #include <linux/mutex.h>
23 #include <linux/init.h>
24 #include <linux/string.h>
25 #include <linux/security.h>
26 #include <linux/major.h>
27 #include <linux/seq_file.h>
28 #include <linux/percpu.h>
29 #include <linux/audit.h>
30 #include <asm/uaccess.h>
32 /* selinuxfs pseudo filesystem for exporting the security policy API.
33 Based on the proc code and the fs/nfsd/nfsctl.c code. */
40 #include "conditional.h"
42 /* Policy capability filenames */
43 static char *policycap_names
[] = {
44 "network_peer_controls",
48 unsigned int selinux_checkreqprot
= CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE
;
50 #ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT
51 #define SELINUX_COMPAT_NET_VALUE 0
53 #define SELINUX_COMPAT_NET_VALUE 1
56 int selinux_compat_net
= SELINUX_COMPAT_NET_VALUE
;
58 static int __init
checkreqprot_setup(char *str
)
60 selinux_checkreqprot
= simple_strtoul(str
, NULL
, 0) ? 1 : 0;
63 __setup("checkreqprot=", checkreqprot_setup
);
65 static int __init
selinux_compat_net_setup(char *str
)
67 selinux_compat_net
= simple_strtoul(str
, NULL
, 0) ? 1 : 0;
70 __setup("selinux_compat_net=", selinux_compat_net_setup
);
73 static DEFINE_MUTEX(sel_mutex
);
75 /* global data for booleans */
76 static struct dentry
*bool_dir
;
78 static char **bool_pending_names
;
79 static int *bool_pending_values
;
81 /* global data for classes */
82 static struct dentry
*class_dir
;
83 static unsigned long last_class_ino
;
85 /* global data for policy capabilities */
86 static struct dentry
*policycap_dir
;
88 extern void selnl_notify_setenforce(int val
);
90 /* Check whether a task is allowed to use a security operation. */
91 static int task_has_security(struct task_struct
*tsk
,
94 struct task_security_struct
*tsec
;
100 return avc_has_perm(tsec
->sid
, SECINITSID_SECURITY
,
101 SECCLASS_SECURITY
, perms
, NULL
);
106 SEL_LOAD
, /* load policy */
107 SEL_ENFORCE
, /* get or set enforcing status */
108 SEL_CONTEXT
, /* validate context */
109 SEL_ACCESS
, /* compute access decision */
110 SEL_CREATE
, /* compute create labeling decision */
111 SEL_RELABEL
, /* compute relabeling decision */
112 SEL_USER
, /* compute reachable user contexts */
113 SEL_POLICYVERS
, /* return policy version for this kernel */
114 SEL_COMMIT_BOOLS
, /* commit new boolean values */
115 SEL_MLS
, /* return if MLS policy is enabled */
116 SEL_DISABLE
, /* disable SELinux until next reboot */
117 SEL_MEMBER
, /* compute polyinstantiation membership decision */
118 SEL_CHECKREQPROT
, /* check requested protection, not kernel-applied one */
119 SEL_COMPAT_NET
, /* whether to use old compat network packet controls */
120 SEL_REJECT_UNKNOWN
, /* export unknown reject handling to userspace */
121 SEL_DENY_UNKNOWN
, /* export unknown deny handling to userspace */
122 SEL_INO_NEXT
, /* The next inode number to use */
125 static unsigned long sel_last_ino
= SEL_INO_NEXT
- 1;
127 #define SEL_INITCON_INO_OFFSET 0x01000000
128 #define SEL_BOOL_INO_OFFSET 0x02000000
129 #define SEL_CLASS_INO_OFFSET 0x04000000
130 #define SEL_POLICYCAP_INO_OFFSET 0x08000000
131 #define SEL_INO_MASK 0x00ffffff
134 static ssize_t
sel_read_enforce(struct file
*filp
, char __user
*buf
,
135 size_t count
, loff_t
*ppos
)
137 char tmpbuf
[TMPBUFLEN
];
140 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_enforcing
);
141 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
144 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
145 static ssize_t
sel_write_enforce(struct file
*file
, const char __user
*buf
,
146 size_t count
, loff_t
*ppos
)
153 if (count
>= PAGE_SIZE
)
156 /* No partial writes. */
159 page
= (char *)get_zeroed_page(GFP_KERNEL
);
163 if (copy_from_user(page
, buf
, count
))
167 if (sscanf(page
, "%d", &new_value
) != 1)
170 if (new_value
!= selinux_enforcing
) {
171 length
= task_has_security(current
, SECURITY__SETENFORCE
);
174 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_STATUS
,
175 "enforcing=%d old_enforcing=%d auid=%u ses=%u",
176 new_value
, selinux_enforcing
,
177 audit_get_loginuid(current
),
178 audit_get_sessionid(current
));
179 selinux_enforcing
= new_value
;
180 if (selinux_enforcing
)
182 selnl_notify_setenforce(selinux_enforcing
);
186 free_page((unsigned long) page
);
190 #define sel_write_enforce NULL
193 static const struct file_operations sel_enforce_ops
= {
194 .read
= sel_read_enforce
,
195 .write
= sel_write_enforce
,
198 static ssize_t
sel_read_handle_unknown(struct file
*filp
, char __user
*buf
,
199 size_t count
, loff_t
*ppos
)
201 char tmpbuf
[TMPBUFLEN
];
203 ino_t ino
= filp
->f_path
.dentry
->d_inode
->i_ino
;
204 int handle_unknown
= (ino
== SEL_REJECT_UNKNOWN
) ?
205 security_get_reject_unknown() : !security_get_allow_unknown();
207 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", handle_unknown
);
208 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
211 static const struct file_operations sel_handle_unknown_ops
= {
212 .read
= sel_read_handle_unknown
,
215 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
216 static ssize_t
sel_write_disable(struct file
*file
, const char __user
*buf
,
217 size_t count
, loff_t
*ppos
)
223 extern int selinux_disable(void);
225 if (count
>= PAGE_SIZE
)
228 /* No partial writes. */
231 page
= (char *)get_zeroed_page(GFP_KERNEL
);
235 if (copy_from_user(page
, buf
, count
))
239 if (sscanf(page
, "%d", &new_value
) != 1)
243 length
= selinux_disable();
246 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_STATUS
,
247 "selinux=0 auid=%u ses=%u",
248 audit_get_loginuid(current
),
249 audit_get_sessionid(current
));
254 free_page((unsigned long) page
);
258 #define sel_write_disable NULL
261 static const struct file_operations sel_disable_ops
= {
262 .write
= sel_write_disable
,
265 static ssize_t
sel_read_policyvers(struct file
*filp
, char __user
*buf
,
266 size_t count
, loff_t
*ppos
)
268 char tmpbuf
[TMPBUFLEN
];
271 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", POLICYDB_VERSION_MAX
);
272 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
275 static const struct file_operations sel_policyvers_ops
= {
276 .read
= sel_read_policyvers
,
279 /* declaration for sel_write_load */
280 static int sel_make_bools(void);
281 static int sel_make_classes(void);
282 static int sel_make_policycap(void);
284 /* declaration for sel_make_class_dirs */
285 static int sel_make_dir(struct inode
*dir
, struct dentry
*dentry
,
288 static ssize_t
sel_read_mls(struct file
*filp
, char __user
*buf
,
289 size_t count
, loff_t
*ppos
)
291 char tmpbuf
[TMPBUFLEN
];
294 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_mls_enabled
);
295 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
298 static const struct file_operations sel_mls_ops
= {
299 .read
= sel_read_mls
,
302 static ssize_t
sel_write_load(struct file
*file
, const char __user
*buf
,
303 size_t count
, loff_t
*ppos
)
310 mutex_lock(&sel_mutex
);
312 length
= task_has_security(current
, SECURITY__LOAD_POLICY
);
317 /* No partial writes. */
322 if ((count
> 64 * 1024 * 1024)
323 || (data
= vmalloc(count
)) == NULL
) {
329 if (copy_from_user(data
, buf
, count
) != 0)
332 length
= security_load_policy(data
, count
);
336 ret
= sel_make_bools();
342 ret
= sel_make_classes();
348 ret
= sel_make_policycap();
356 printk(KERN_INFO
"SELinux: policy loaded with handle_unknown=%s\n",
357 (security_get_reject_unknown() ? "reject" :
358 (security_get_allow_unknown() ? "allow" : "deny")));
360 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_POLICY_LOAD
,
361 "policy loaded auid=%u ses=%u",
362 audit_get_loginuid(current
),
363 audit_get_sessionid(current
));
365 mutex_unlock(&sel_mutex
);
370 static const struct file_operations sel_load_ops
= {
371 .write
= sel_write_load
,
374 static ssize_t
sel_write_context(struct file
*file
, char *buf
, size_t size
)
380 length
= task_has_security(current
, SECURITY__CHECK_CONTEXT
);
384 length
= security_context_to_sid(buf
, size
, &sid
);
388 length
= security_sid_to_context(sid
, &canon
, &len
);
392 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
393 printk(KERN_ERR
"SELinux: %s: context size (%u) exceeds "
394 "payload max\n", __func__
, len
);
399 memcpy(buf
, canon
, len
);
406 static ssize_t
sel_read_checkreqprot(struct file
*filp
, char __user
*buf
,
407 size_t count
, loff_t
*ppos
)
409 char tmpbuf
[TMPBUFLEN
];
412 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", selinux_checkreqprot
);
413 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
416 static ssize_t
sel_write_checkreqprot(struct file
*file
, const char __user
*buf
,
417 size_t count
, loff_t
*ppos
)
421 unsigned int new_value
;
423 length
= task_has_security(current
, SECURITY__SETCHECKREQPROT
);
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
, "%u", &new_value
) != 1)
444 selinux_checkreqprot
= new_value
? 1 : 0;
447 free_page((unsigned long) page
);
450 static const struct file_operations sel_checkreqprot_ops
= {
451 .read
= sel_read_checkreqprot
,
452 .write
= sel_write_checkreqprot
,
455 static ssize_t
sel_read_compat_net(struct file
*filp
, char __user
*buf
,
456 size_t count
, loff_t
*ppos
)
458 char tmpbuf
[TMPBUFLEN
];
461 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_compat_net
);
462 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
465 static ssize_t
sel_write_compat_net(struct file
*file
, const char __user
*buf
,
466 size_t count
, loff_t
*ppos
)
472 length
= task_has_security(current
, SECURITY__LOAD_POLICY
);
476 if (count
>= PAGE_SIZE
)
479 /* No partial writes. */
482 page
= (char *)get_zeroed_page(GFP_KERNEL
);
486 if (copy_from_user(page
, buf
, count
))
490 if (sscanf(page
, "%d", &new_value
) != 1)
493 selinux_compat_net
= new_value
? 1 : 0;
496 free_page((unsigned long) page
);
499 static const struct file_operations sel_compat_net_ops
= {
500 .read
= sel_read_compat_net
,
501 .write
= sel_write_compat_net
,
505 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
507 static ssize_t
sel_write_access(struct file
*file
, char *buf
, size_t size
);
508 static ssize_t
sel_write_create(struct file
*file
, char *buf
, size_t size
);
509 static ssize_t
sel_write_relabel(struct file
*file
, char *buf
, size_t size
);
510 static ssize_t
sel_write_user(struct file
*file
, char *buf
, size_t size
);
511 static ssize_t
sel_write_member(struct file
*file
, char *buf
, size_t size
);
513 static ssize_t (*write_op
[])(struct file
*, char *, size_t) = {
514 [SEL_ACCESS
] = sel_write_access
,
515 [SEL_CREATE
] = sel_write_create
,
516 [SEL_RELABEL
] = sel_write_relabel
,
517 [SEL_USER
] = sel_write_user
,
518 [SEL_MEMBER
] = sel_write_member
,
519 [SEL_CONTEXT
] = sel_write_context
,
522 static ssize_t
selinux_transaction_write(struct file
*file
, const char __user
*buf
, size_t size
, loff_t
*pos
)
524 ino_t ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
528 if (ino
>= ARRAY_SIZE(write_op
) || !write_op
[ino
])
531 data
= simple_transaction_get(file
, buf
, size
);
533 return PTR_ERR(data
);
535 rv
= write_op
[ino
](file
, data
, size
);
537 simple_transaction_set(file
, rv
);
543 static const struct file_operations transaction_ops
= {
544 .write
= selinux_transaction_write
,
545 .read
= simple_transaction_read
,
546 .release
= simple_transaction_release
,
550 * payload - write methods
551 * If the method has a response, the response should be put in buf,
552 * and the length returned. Otherwise return 0 or and -error.
555 static ssize_t
sel_write_access(struct file
*file
, char *buf
, size_t size
)
561 struct av_decision avd
;
564 length
= task_has_security(current
, SECURITY__COMPUTE_AV
);
569 scon
= kzalloc(size
+1, GFP_KERNEL
);
573 tcon
= kzalloc(size
+1, GFP_KERNEL
);
578 if (sscanf(buf
, "%s %s %hu %x", scon
, tcon
, &tclass
, &req
) != 4)
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_compute_av(ssid
, tsid
, tclass
, req
, &avd
);
592 length
= scnprintf(buf
, SIMPLE_TRANSACTION_LIMIT
,
594 avd
.allowed
, avd
.decided
,
595 avd
.auditallow
, avd
.auditdeny
,
604 static ssize_t
sel_write_create(struct file
*file
, char *buf
, size_t size
)
607 u32 ssid
, tsid
, newsid
;
613 length
= task_has_security(current
, SECURITY__COMPUTE_CREATE
);
618 scon
= kzalloc(size
+1, GFP_KERNEL
);
622 tcon
= kzalloc(size
+1, GFP_KERNEL
);
627 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
630 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
633 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
637 length
= security_transition_sid(ssid
, tsid
, tclass
, &newsid
);
641 length
= security_sid_to_context(newsid
, &newcon
, &len
);
645 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
646 printk(KERN_ERR
"SELinux: %s: context size (%u) exceeds "
647 "payload max\n", __func__
, len
);
652 memcpy(buf
, newcon
, len
);
663 static ssize_t
sel_write_relabel(struct file
*file
, char *buf
, size_t size
)
666 u32 ssid
, tsid
, newsid
;
672 length
= task_has_security(current
, SECURITY__COMPUTE_RELABEL
);
677 scon
= kzalloc(size
+1, GFP_KERNEL
);
681 tcon
= kzalloc(size
+1, GFP_KERNEL
);
686 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
689 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
692 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
696 length
= security_change_sid(ssid
, tsid
, tclass
, &newsid
);
700 length
= security_sid_to_context(newsid
, &newcon
, &len
);
704 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
709 memcpy(buf
, newcon
, len
);
720 static ssize_t
sel_write_user(struct file
*file
, char *buf
, size_t size
)
722 char *con
, *user
, *ptr
;
729 length
= task_has_security(current
, SECURITY__COMPUTE_USER
);
734 con
= kzalloc(size
+1, GFP_KERNEL
);
738 user
= kzalloc(size
+1, GFP_KERNEL
);
743 if (sscanf(buf
, "%s %s", con
, user
) != 2)
746 length
= security_context_to_sid(con
, strlen(con
)+1, &sid
);
750 length
= security_get_user_sids(sid
, user
, &sids
, &nsids
);
754 length
= sprintf(buf
, "%u", nsids
) + 1;
756 for (i
= 0; i
< nsids
; i
++) {
757 rc
= security_sid_to_context(sids
[i
], &newcon
, &len
);
762 if ((length
+ len
) >= SIMPLE_TRANSACTION_LIMIT
) {
767 memcpy(ptr
, newcon
, len
);
781 static ssize_t
sel_write_member(struct file
*file
, char *buf
, size_t size
)
784 u32 ssid
, tsid
, newsid
;
790 length
= task_has_security(current
, SECURITY__COMPUTE_MEMBER
);
795 scon
= kzalloc(size
+1, GFP_KERNEL
);
799 tcon
= kzalloc(size
+1, GFP_KERNEL
);
804 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
807 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
810 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
814 length
= security_member_sid(ssid
, tsid
, tclass
, &newsid
);
818 length
= security_sid_to_context(newsid
, &newcon
, &len
);
822 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
823 printk(KERN_ERR
"SELinux: %s: context size (%u) exceeds "
824 "payload max\n", __func__
, len
);
829 memcpy(buf
, newcon
, len
);
840 static struct inode
*sel_make_inode(struct super_block
*sb
, int mode
)
842 struct inode
*ret
= new_inode(sb
);
846 ret
->i_uid
= ret
->i_gid
= 0;
848 ret
->i_atime
= ret
->i_mtime
= ret
->i_ctime
= CURRENT_TIME
;
853 static ssize_t
sel_read_bool(struct file
*filep
, char __user
*buf
,
854 size_t count
, loff_t
*ppos
)
860 struct inode
*inode
= filep
->f_path
.dentry
->d_inode
;
861 unsigned index
= inode
->i_ino
& SEL_INO_MASK
;
862 const char *name
= filep
->f_path
.dentry
->d_name
.name
;
864 mutex_lock(&sel_mutex
);
866 if (index
>= bool_num
|| strcmp(name
, bool_pending_names
[index
])) {
871 if (count
> PAGE_SIZE
) {
875 page
= (char *)get_zeroed_page(GFP_KERNEL
);
881 cur_enforcing
= security_get_bool_value(index
);
882 if (cur_enforcing
< 0) {
886 length
= scnprintf(page
, PAGE_SIZE
, "%d %d", cur_enforcing
,
887 bool_pending_values
[index
]);
888 ret
= simple_read_from_buffer(buf
, count
, ppos
, page
, length
);
890 mutex_unlock(&sel_mutex
);
892 free_page((unsigned long)page
);
896 static ssize_t
sel_write_bool(struct file
*filep
, const char __user
*buf
,
897 size_t count
, loff_t
*ppos
)
902 struct inode
*inode
= filep
->f_path
.dentry
->d_inode
;
903 unsigned index
= inode
->i_ino
& SEL_INO_MASK
;
904 const char *name
= filep
->f_path
.dentry
->d_name
.name
;
906 mutex_lock(&sel_mutex
);
908 length
= task_has_security(current
, SECURITY__SETBOOL
);
912 if (index
>= bool_num
|| strcmp(name
, bool_pending_names
[index
])) {
917 if (count
>= PAGE_SIZE
) {
923 /* No partial writes. */
927 page
= (char *)get_zeroed_page(GFP_KERNEL
);
934 if (copy_from_user(page
, buf
, count
))
938 if (sscanf(page
, "%d", &new_value
) != 1)
944 bool_pending_values
[index
] = new_value
;
948 mutex_unlock(&sel_mutex
);
950 free_page((unsigned long) page
);
954 static const struct file_operations sel_bool_ops
= {
955 .read
= sel_read_bool
,
956 .write
= sel_write_bool
,
959 static ssize_t
sel_commit_bools_write(struct file
*filep
,
960 const char __user
*buf
,
961 size_t count
, loff_t
*ppos
)
967 mutex_lock(&sel_mutex
);
969 length
= task_has_security(current
, SECURITY__SETBOOL
);
973 if (count
>= PAGE_SIZE
) {
978 /* No partial writes. */
981 page
= (char *)get_zeroed_page(GFP_KERNEL
);
988 if (copy_from_user(page
, buf
, count
))
992 if (sscanf(page
, "%d", &new_value
) != 1)
995 if (new_value
&& bool_pending_values
)
996 security_set_bools(bool_num
, bool_pending_values
);
1001 mutex_unlock(&sel_mutex
);
1003 free_page((unsigned long) page
);
1007 static const struct file_operations sel_commit_bools_ops
= {
1008 .write
= sel_commit_bools_write
,
1011 static void sel_remove_entries(struct dentry
*de
)
1013 struct list_head
*node
;
1015 spin_lock(&dcache_lock
);
1016 node
= de
->d_subdirs
.next
;
1017 while (node
!= &de
->d_subdirs
) {
1018 struct dentry
*d
= list_entry(node
, struct dentry
, d_u
.d_child
);
1019 list_del_init(node
);
1023 spin_unlock(&dcache_lock
);
1025 simple_unlink(de
->d_inode
, d
);
1027 spin_lock(&dcache_lock
);
1029 node
= de
->d_subdirs
.next
;
1032 spin_unlock(&dcache_lock
);
1035 #define BOOL_DIR_NAME "booleans"
1037 static int sel_make_bools(void)
1041 struct dentry
*dentry
= NULL
;
1042 struct dentry
*dir
= bool_dir
;
1043 struct inode
*inode
= NULL
;
1044 struct inode_security_struct
*isec
;
1045 char **names
= NULL
, *page
;
1050 /* remove any existing files */
1051 kfree(bool_pending_names
);
1052 kfree(bool_pending_values
);
1053 bool_pending_names
= NULL
;
1054 bool_pending_values
= NULL
;
1056 sel_remove_entries(dir
);
1058 page
= (char *)get_zeroed_page(GFP_KERNEL
);
1062 ret
= security_get_bools(&num
, &names
, &values
);
1066 for (i
= 0; i
< num
; i
++) {
1067 dentry
= d_alloc_name(dir
, names
[i
]);
1072 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
| S_IRUGO
| S_IWUSR
);
1078 len
= snprintf(page
, PAGE_SIZE
, "/%s/%s", BOOL_DIR_NAME
, names
[i
]);
1082 } else if (len
>= PAGE_SIZE
) {
1083 ret
= -ENAMETOOLONG
;
1086 isec
= (struct inode_security_struct
*)inode
->i_security
;
1087 ret
= security_genfs_sid("selinuxfs", page
, SECCLASS_FILE
, &sid
);
1091 isec
->initialized
= 1;
1092 inode
->i_fop
= &sel_bool_ops
;
1093 inode
->i_ino
= i
|SEL_BOOL_INO_OFFSET
;
1094 d_add(dentry
, inode
);
1097 bool_pending_names
= names
;
1098 bool_pending_values
= values
;
1100 free_page((unsigned long)page
);
1104 for (i
= 0; i
< num
; i
++)
1109 sel_remove_entries(dir
);
1114 #define NULL_FILE_NAME "null"
1116 struct dentry
*selinux_null
;
1118 static ssize_t
sel_read_avc_cache_threshold(struct file
*filp
, char __user
*buf
,
1119 size_t count
, loff_t
*ppos
)
1121 char tmpbuf
[TMPBUFLEN
];
1124 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", avc_cache_threshold
);
1125 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1128 static ssize_t
sel_write_avc_cache_threshold(struct file
*file
,
1129 const char __user
*buf
,
1130 size_t count
, loff_t
*ppos
)
1137 if (count
>= PAGE_SIZE
) {
1143 /* No partial writes. */
1148 page
= (char *)get_zeroed_page(GFP_KERNEL
);
1154 if (copy_from_user(page
, buf
, count
)) {
1159 if (sscanf(page
, "%u", &new_value
) != 1) {
1164 if (new_value
!= avc_cache_threshold
) {
1165 ret
= task_has_security(current
, SECURITY__SETSECPARAM
);
1168 avc_cache_threshold
= new_value
;
1172 free_page((unsigned long)page
);
1177 static ssize_t
sel_read_avc_hash_stats(struct file
*filp
, char __user
*buf
,
1178 size_t count
, loff_t
*ppos
)
1183 page
= (char *)__get_free_page(GFP_KERNEL
);
1188 ret
= avc_get_hash_stats(page
);
1190 ret
= simple_read_from_buffer(buf
, count
, ppos
, page
, ret
);
1191 free_page((unsigned long)page
);
1196 static const struct file_operations sel_avc_cache_threshold_ops
= {
1197 .read
= sel_read_avc_cache_threshold
,
1198 .write
= sel_write_avc_cache_threshold
,
1201 static const struct file_operations sel_avc_hash_stats_ops
= {
1202 .read
= sel_read_avc_hash_stats
,
1205 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1206 static struct avc_cache_stats
*sel_avc_get_stat_idx(loff_t
*idx
)
1210 for (cpu
= *idx
; cpu
< NR_CPUS
; ++cpu
) {
1211 if (!cpu_possible(cpu
))
1214 return &per_cpu(avc_cache_stats
, cpu
);
1219 static void *sel_avc_stats_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1221 loff_t n
= *pos
- 1;
1224 return SEQ_START_TOKEN
;
1226 return sel_avc_get_stat_idx(&n
);
1229 static void *sel_avc_stats_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1231 return sel_avc_get_stat_idx(pos
);
1234 static int sel_avc_stats_seq_show(struct seq_file
*seq
, void *v
)
1236 struct avc_cache_stats
*st
= v
;
1238 if (v
== SEQ_START_TOKEN
)
1239 seq_printf(seq
, "lookups hits misses allocations reclaims "
1242 seq_printf(seq
, "%u %u %u %u %u %u\n", st
->lookups
,
1243 st
->hits
, st
->misses
, st
->allocations
,
1244 st
->reclaims
, st
->frees
);
1248 static void sel_avc_stats_seq_stop(struct seq_file
*seq
, void *v
)
1251 static const struct seq_operations sel_avc_cache_stats_seq_ops
= {
1252 .start
= sel_avc_stats_seq_start
,
1253 .next
= sel_avc_stats_seq_next
,
1254 .show
= sel_avc_stats_seq_show
,
1255 .stop
= sel_avc_stats_seq_stop
,
1258 static int sel_open_avc_cache_stats(struct inode
*inode
, struct file
*file
)
1260 return seq_open(file
, &sel_avc_cache_stats_seq_ops
);
1263 static const struct file_operations sel_avc_cache_stats_ops
= {
1264 .open
= sel_open_avc_cache_stats
,
1266 .llseek
= seq_lseek
,
1267 .release
= seq_release
,
1271 static int sel_make_avc_files(struct dentry
*dir
)
1274 static struct tree_descr files
[] = {
1275 { "cache_threshold",
1276 &sel_avc_cache_threshold_ops
, S_IRUGO
|S_IWUSR
},
1277 { "hash_stats", &sel_avc_hash_stats_ops
, S_IRUGO
},
1278 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1279 { "cache_stats", &sel_avc_cache_stats_ops
, S_IRUGO
},
1283 for (i
= 0; i
< ARRAY_SIZE(files
); i
++) {
1284 struct inode
*inode
;
1285 struct dentry
*dentry
;
1287 dentry
= d_alloc_name(dir
, files
[i
].name
);
1293 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|files
[i
].mode
);
1298 inode
->i_fop
= files
[i
].ops
;
1299 inode
->i_ino
= ++sel_last_ino
;
1300 d_add(dentry
, inode
);
1306 static ssize_t
sel_read_initcon(struct file
*file
, char __user
*buf
,
1307 size_t count
, loff_t
*ppos
)
1309 struct inode
*inode
;
1314 inode
= file
->f_path
.dentry
->d_inode
;
1315 sid
= inode
->i_ino
&SEL_INO_MASK
;
1316 ret
= security_sid_to_context(sid
, &con
, &len
);
1320 ret
= simple_read_from_buffer(buf
, count
, ppos
, con
, len
);
1325 static const struct file_operations sel_initcon_ops
= {
1326 .read
= sel_read_initcon
,
1329 static int sel_make_initcon_files(struct dentry
*dir
)
1333 for (i
= 1; i
<= SECINITSID_NUM
; i
++) {
1334 struct inode
*inode
;
1335 struct dentry
*dentry
;
1336 dentry
= d_alloc_name(dir
, security_get_initial_sid_context(i
));
1342 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1347 inode
->i_fop
= &sel_initcon_ops
;
1348 inode
->i_ino
= i
|SEL_INITCON_INO_OFFSET
;
1349 d_add(dentry
, inode
);
1355 static inline unsigned int sel_div(unsigned long a
, unsigned long b
)
1357 return a
/ b
- (a
% b
< 0);
1360 static inline unsigned long sel_class_to_ino(u16
class)
1362 return (class * (SEL_VEC_MAX
+ 1)) | SEL_CLASS_INO_OFFSET
;
1365 static inline u16
sel_ino_to_class(unsigned long ino
)
1367 return sel_div(ino
& SEL_INO_MASK
, SEL_VEC_MAX
+ 1);
1370 static inline unsigned long sel_perm_to_ino(u16
class, u32 perm
)
1372 return (class * (SEL_VEC_MAX
+ 1) + perm
) | SEL_CLASS_INO_OFFSET
;
1375 static inline u32
sel_ino_to_perm(unsigned long ino
)
1377 return (ino
& SEL_INO_MASK
) % (SEL_VEC_MAX
+ 1);
1380 static ssize_t
sel_read_class(struct file
*file
, char __user
*buf
,
1381 size_t count
, loff_t
*ppos
)
1385 unsigned long ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
1387 page
= (char *)__get_free_page(GFP_KERNEL
);
1393 len
= snprintf(page
, PAGE_SIZE
, "%d", sel_ino_to_class(ino
));
1394 rc
= simple_read_from_buffer(buf
, count
, ppos
, page
, len
);
1395 free_page((unsigned long)page
);
1400 static const struct file_operations sel_class_ops
= {
1401 .read
= sel_read_class
,
1404 static ssize_t
sel_read_perm(struct file
*file
, char __user
*buf
,
1405 size_t count
, loff_t
*ppos
)
1409 unsigned long ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
1411 page
= (char *)__get_free_page(GFP_KERNEL
);
1417 len
= snprintf(page
, PAGE_SIZE
, "%d", sel_ino_to_perm(ino
));
1418 rc
= simple_read_from_buffer(buf
, count
, ppos
, page
, len
);
1419 free_page((unsigned long)page
);
1424 static const struct file_operations sel_perm_ops
= {
1425 .read
= sel_read_perm
,
1428 static ssize_t
sel_read_policycap(struct file
*file
, char __user
*buf
,
1429 size_t count
, loff_t
*ppos
)
1432 char tmpbuf
[TMPBUFLEN
];
1434 unsigned long i_ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
1436 value
= security_policycap_supported(i_ino
& SEL_INO_MASK
);
1437 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", value
);
1439 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1442 static const struct file_operations sel_policycap_ops
= {
1443 .read
= sel_read_policycap
,
1446 static int sel_make_perm_files(char *objclass
, int classvalue
,
1449 int i
, rc
= 0, nperms
;
1452 rc
= security_get_permissions(objclass
, &perms
, &nperms
);
1456 for (i
= 0; i
< nperms
; i
++) {
1457 struct inode
*inode
;
1458 struct dentry
*dentry
;
1460 dentry
= d_alloc_name(dir
, perms
[i
]);
1466 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1471 inode
->i_fop
= &sel_perm_ops
;
1472 /* i+1 since perm values are 1-indexed */
1473 inode
->i_ino
= sel_perm_to_ino(classvalue
, i
+1);
1474 d_add(dentry
, inode
);
1478 for (i
= 0; i
< nperms
; i
++)
1485 static int sel_make_class_dir_entries(char *classname
, int index
,
1488 struct dentry
*dentry
= NULL
;
1489 struct inode
*inode
= NULL
;
1492 dentry
= d_alloc_name(dir
, "index");
1498 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1504 inode
->i_fop
= &sel_class_ops
;
1505 inode
->i_ino
= sel_class_to_ino(index
);
1506 d_add(dentry
, inode
);
1508 dentry
= d_alloc_name(dir
, "perms");
1514 rc
= sel_make_dir(dir
->d_inode
, dentry
, &last_class_ino
);
1518 rc
= sel_make_perm_files(classname
, index
, dentry
);
1524 static void sel_remove_classes(void)
1526 struct list_head
*class_node
;
1528 list_for_each(class_node
, &class_dir
->d_subdirs
) {
1529 struct dentry
*class_subdir
= list_entry(class_node
,
1530 struct dentry
, d_u
.d_child
);
1531 struct list_head
*class_subdir_node
;
1533 list_for_each(class_subdir_node
, &class_subdir
->d_subdirs
) {
1534 struct dentry
*d
= list_entry(class_subdir_node
,
1535 struct dentry
, d_u
.d_child
);
1538 if (d
->d_inode
->i_mode
& S_IFDIR
)
1539 sel_remove_entries(d
);
1542 sel_remove_entries(class_subdir
);
1545 sel_remove_entries(class_dir
);
1548 static int sel_make_classes(void)
1550 int rc
= 0, nclasses
, i
;
1553 /* delete any existing entries */
1554 sel_remove_classes();
1556 rc
= security_get_classes(&classes
, &nclasses
);
1560 /* +2 since classes are 1-indexed */
1561 last_class_ino
= sel_class_to_ino(nclasses
+2);
1563 for (i
= 0; i
< nclasses
; i
++) {
1564 struct dentry
*class_name_dir
;
1566 class_name_dir
= d_alloc_name(class_dir
, classes
[i
]);
1567 if (!class_name_dir
) {
1572 rc
= sel_make_dir(class_dir
->d_inode
, class_name_dir
,
1577 /* i+1 since class values are 1-indexed */
1578 rc
= sel_make_class_dir_entries(classes
[i
], i
+1,
1585 for (i
= 0; i
< nclasses
; i
++)
1592 static int sel_make_policycap(void)
1595 struct dentry
*dentry
= NULL
;
1596 struct inode
*inode
= NULL
;
1598 sel_remove_entries(policycap_dir
);
1600 for (iter
= 0; iter
<= POLICYDB_CAPABILITY_MAX
; iter
++) {
1601 if (iter
< ARRAY_SIZE(policycap_names
))
1602 dentry
= d_alloc_name(policycap_dir
,
1603 policycap_names
[iter
]);
1605 dentry
= d_alloc_name(policycap_dir
, "unknown");
1610 inode
= sel_make_inode(policycap_dir
->d_sb
, S_IFREG
| S_IRUGO
);
1614 inode
->i_fop
= &sel_policycap_ops
;
1615 inode
->i_ino
= iter
| SEL_POLICYCAP_INO_OFFSET
;
1616 d_add(dentry
, inode
);
1622 static int sel_make_dir(struct inode
*dir
, struct dentry
*dentry
,
1626 struct inode
*inode
;
1628 inode
= sel_make_inode(dir
->i_sb
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
1633 inode
->i_op
= &simple_dir_inode_operations
;
1634 inode
->i_fop
= &simple_dir_operations
;
1635 inode
->i_ino
= ++(*ino
);
1636 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1638 d_add(dentry
, inode
);
1639 /* bump link count on parent directory, too */
1645 static int sel_fill_super(struct super_block
*sb
, void *data
, int silent
)
1648 struct dentry
*dentry
;
1649 struct inode
*inode
, *root_inode
;
1650 struct inode_security_struct
*isec
;
1652 static struct tree_descr selinux_files
[] = {
1653 [SEL_LOAD
] = {"load", &sel_load_ops
, S_IRUSR
|S_IWUSR
},
1654 [SEL_ENFORCE
] = {"enforce", &sel_enforce_ops
, S_IRUGO
|S_IWUSR
},
1655 [SEL_CONTEXT
] = {"context", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1656 [SEL_ACCESS
] = {"access", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1657 [SEL_CREATE
] = {"create", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1658 [SEL_RELABEL
] = {"relabel", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1659 [SEL_USER
] = {"user", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1660 [SEL_POLICYVERS
] = {"policyvers", &sel_policyvers_ops
, S_IRUGO
},
1661 [SEL_COMMIT_BOOLS
] = {"commit_pending_bools", &sel_commit_bools_ops
, S_IWUSR
},
1662 [SEL_MLS
] = {"mls", &sel_mls_ops
, S_IRUGO
},
1663 [SEL_DISABLE
] = {"disable", &sel_disable_ops
, S_IWUSR
},
1664 [SEL_MEMBER
] = {"member", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1665 [SEL_CHECKREQPROT
] = {"checkreqprot", &sel_checkreqprot_ops
, S_IRUGO
|S_IWUSR
},
1666 [SEL_COMPAT_NET
] = {"compat_net", &sel_compat_net_ops
, S_IRUGO
|S_IWUSR
},
1667 [SEL_REJECT_UNKNOWN
] = {"reject_unknown", &sel_handle_unknown_ops
, S_IRUGO
},
1668 [SEL_DENY_UNKNOWN
] = {"deny_unknown", &sel_handle_unknown_ops
, S_IRUGO
},
1671 ret
= simple_fill_super(sb
, SELINUX_MAGIC
, selinux_files
);
1675 root_inode
= sb
->s_root
->d_inode
;
1677 dentry
= d_alloc_name(sb
->s_root
, BOOL_DIR_NAME
);
1683 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1689 dentry
= d_alloc_name(sb
->s_root
, NULL_FILE_NAME
);
1695 inode
= sel_make_inode(sb
, S_IFCHR
| S_IRUGO
| S_IWUGO
);
1700 inode
->i_ino
= ++sel_last_ino
;
1701 isec
= (struct inode_security_struct
*)inode
->i_security
;
1702 isec
->sid
= SECINITSID_DEVNULL
;
1703 isec
->sclass
= SECCLASS_CHR_FILE
;
1704 isec
->initialized
= 1;
1706 init_special_inode(inode
, S_IFCHR
| S_IRUGO
| S_IWUGO
, MKDEV(MEM_MAJOR
, 3));
1707 d_add(dentry
, inode
);
1708 selinux_null
= dentry
;
1710 dentry
= d_alloc_name(sb
->s_root
, "avc");
1716 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1720 ret
= sel_make_avc_files(dentry
);
1724 dentry
= d_alloc_name(sb
->s_root
, "initial_contexts");
1730 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1734 ret
= sel_make_initcon_files(dentry
);
1738 dentry
= d_alloc_name(sb
->s_root
, "class");
1744 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1750 dentry
= d_alloc_name(sb
->s_root
, "policy_capabilities");
1756 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1760 policycap_dir
= dentry
;
1765 printk(KERN_ERR
"SELinux: %s: failed while creating inodes\n",
1770 static int sel_get_sb(struct file_system_type
*fs_type
,
1771 int flags
, const char *dev_name
, void *data
,
1772 struct vfsmount
*mnt
)
1774 return get_sb_single(fs_type
, flags
, data
, sel_fill_super
, mnt
);
1777 static struct file_system_type sel_fs_type
= {
1778 .name
= "selinuxfs",
1779 .get_sb
= sel_get_sb
,
1780 .kill_sb
= kill_litter_super
,
1783 struct vfsmount
*selinuxfs_mount
;
1785 static int __init
init_sel_fs(void)
1789 if (!selinux_enabled
)
1791 err
= register_filesystem(&sel_fs_type
);
1793 selinuxfs_mount
= kern_mount(&sel_fs_type
);
1794 if (IS_ERR(selinuxfs_mount
)) {
1795 printk(KERN_ERR
"selinuxfs: could not mount!\n");
1796 err
= PTR_ERR(selinuxfs_mount
);
1797 selinuxfs_mount
= NULL
;
1803 __initcall(init_sel_fs
);
1805 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1806 void exit_sel_fs(void)
1808 unregister_filesystem(&sel_fs_type
);