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 <linux/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 int selinux_compat_net
= 0;
52 static int __init
checkreqprot_setup(char *str
)
54 unsigned long checkreqprot
;
55 if (!strict_strtoul(str
, 0, &checkreqprot
))
56 selinux_checkreqprot
= checkreqprot
? 1 : 0;
59 __setup("checkreqprot=", checkreqprot_setup
);
61 static int __init
selinux_compat_net_setup(char *str
)
63 unsigned long compat_net
;
64 if (!strict_strtoul(str
, 0, &compat_net
))
65 selinux_compat_net
= compat_net
? 1 : 0;
68 __setup("selinux_compat_net=", selinux_compat_net_setup
);
71 static DEFINE_MUTEX(sel_mutex
);
73 /* global data for booleans */
74 static struct dentry
*bool_dir
;
76 static char **bool_pending_names
;
77 static int *bool_pending_values
;
79 /* global data for classes */
80 static struct dentry
*class_dir
;
81 static unsigned long last_class_ino
;
83 /* global data for policy capabilities */
84 static struct dentry
*policycap_dir
;
86 extern void selnl_notify_setenforce(int val
);
88 /* Check whether a task is allowed to use a security operation. */
89 static int task_has_security(struct task_struct
*tsk
,
92 const struct task_security_struct
*tsec
;
96 tsec
= __task_cred(tsk
)->security
;
103 return avc_has_perm(sid
, SECINITSID_SECURITY
,
104 SECCLASS_SECURITY
, perms
, NULL
);
109 SEL_LOAD
, /* load policy */
110 SEL_ENFORCE
, /* get or set enforcing status */
111 SEL_CONTEXT
, /* validate context */
112 SEL_ACCESS
, /* compute access decision */
113 SEL_CREATE
, /* compute create labeling decision */
114 SEL_RELABEL
, /* compute relabeling decision */
115 SEL_USER
, /* compute reachable user contexts */
116 SEL_POLICYVERS
, /* return policy version for this kernel */
117 SEL_COMMIT_BOOLS
, /* commit new boolean values */
118 SEL_MLS
, /* return if MLS policy is enabled */
119 SEL_DISABLE
, /* disable SELinux until next reboot */
120 SEL_MEMBER
, /* compute polyinstantiation membership decision */
121 SEL_CHECKREQPROT
, /* check requested protection, not kernel-applied one */
122 SEL_COMPAT_NET
, /* whether to use old compat network packet controls */
123 SEL_REJECT_UNKNOWN
, /* export unknown reject handling to userspace */
124 SEL_DENY_UNKNOWN
, /* export unknown deny handling to userspace */
125 SEL_INO_NEXT
, /* The next inode number to use */
128 static unsigned long sel_last_ino
= SEL_INO_NEXT
- 1;
130 #define SEL_INITCON_INO_OFFSET 0x01000000
131 #define SEL_BOOL_INO_OFFSET 0x02000000
132 #define SEL_CLASS_INO_OFFSET 0x04000000
133 #define SEL_POLICYCAP_INO_OFFSET 0x08000000
134 #define SEL_INO_MASK 0x00ffffff
137 static ssize_t
sel_read_enforce(struct file
*filp
, char __user
*buf
,
138 size_t count
, loff_t
*ppos
)
140 char tmpbuf
[TMPBUFLEN
];
143 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_enforcing
);
144 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
147 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
148 static ssize_t
sel_write_enforce(struct file
*file
, const char __user
*buf
,
149 size_t count
, loff_t
*ppos
)
156 if (count
>= PAGE_SIZE
)
159 /* No partial writes. */
162 page
= (char *)get_zeroed_page(GFP_KERNEL
);
166 if (copy_from_user(page
, buf
, count
))
170 if (sscanf(page
, "%d", &new_value
) != 1)
173 if (new_value
!= selinux_enforcing
) {
174 length
= task_has_security(current
, SECURITY__SETENFORCE
);
177 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_STATUS
,
178 "enforcing=%d old_enforcing=%d auid=%u ses=%u",
179 new_value
, selinux_enforcing
,
180 audit_get_loginuid(current
),
181 audit_get_sessionid(current
));
182 selinux_enforcing
= new_value
;
183 if (selinux_enforcing
)
185 selnl_notify_setenforce(selinux_enforcing
);
189 free_page((unsigned long) page
);
193 #define sel_write_enforce NULL
196 static const struct file_operations sel_enforce_ops
= {
197 .read
= sel_read_enforce
,
198 .write
= sel_write_enforce
,
201 static ssize_t
sel_read_handle_unknown(struct file
*filp
, char __user
*buf
,
202 size_t count
, loff_t
*ppos
)
204 char tmpbuf
[TMPBUFLEN
];
206 ino_t ino
= filp
->f_path
.dentry
->d_inode
->i_ino
;
207 int handle_unknown
= (ino
== SEL_REJECT_UNKNOWN
) ?
208 security_get_reject_unknown() : !security_get_allow_unknown();
210 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", handle_unknown
);
211 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
214 static const struct file_operations sel_handle_unknown_ops
= {
215 .read
= sel_read_handle_unknown
,
218 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
219 static ssize_t
sel_write_disable(struct file
*file
, const char __user
*buf
,
220 size_t count
, loff_t
*ppos
)
226 extern int selinux_disable(void);
228 if (count
>= PAGE_SIZE
)
231 /* No partial writes. */
234 page
= (char *)get_zeroed_page(GFP_KERNEL
);
238 if (copy_from_user(page
, buf
, count
))
242 if (sscanf(page
, "%d", &new_value
) != 1)
246 length
= selinux_disable();
249 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_STATUS
,
250 "selinux=0 auid=%u ses=%u",
251 audit_get_loginuid(current
),
252 audit_get_sessionid(current
));
257 free_page((unsigned long) page
);
261 #define sel_write_disable NULL
264 static const struct file_operations sel_disable_ops
= {
265 .write
= sel_write_disable
,
268 static ssize_t
sel_read_policyvers(struct file
*filp
, char __user
*buf
,
269 size_t count
, loff_t
*ppos
)
271 char tmpbuf
[TMPBUFLEN
];
274 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", POLICYDB_VERSION_MAX
);
275 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
278 static const struct file_operations sel_policyvers_ops
= {
279 .read
= sel_read_policyvers
,
282 /* declaration for sel_write_load */
283 static int sel_make_bools(void);
284 static int sel_make_classes(void);
285 static int sel_make_policycap(void);
287 /* declaration for sel_make_class_dirs */
288 static int sel_make_dir(struct inode
*dir
, struct dentry
*dentry
,
291 static ssize_t
sel_read_mls(struct file
*filp
, char __user
*buf
,
292 size_t count
, loff_t
*ppos
)
294 char tmpbuf
[TMPBUFLEN
];
297 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_mls_enabled
);
298 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
301 static const struct file_operations sel_mls_ops
= {
302 .read
= sel_read_mls
,
305 static ssize_t
sel_write_load(struct file
*file
, const char __user
*buf
,
306 size_t count
, loff_t
*ppos
)
313 mutex_lock(&sel_mutex
);
315 length
= task_has_security(current
, SECURITY__LOAD_POLICY
);
320 /* No partial writes. */
325 if ((count
> 64 * 1024 * 1024)
326 || (data
= vmalloc(count
)) == NULL
) {
332 if (copy_from_user(data
, buf
, count
) != 0)
335 length
= security_load_policy(data
, count
);
339 ret
= sel_make_bools();
345 ret
= sel_make_classes();
351 ret
= sel_make_policycap();
358 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_POLICY_LOAD
,
359 "policy loaded auid=%u ses=%u",
360 audit_get_loginuid(current
),
361 audit_get_sessionid(current
));
363 mutex_unlock(&sel_mutex
);
368 static const struct file_operations sel_load_ops
= {
369 .write
= sel_write_load
,
372 static ssize_t
sel_write_context(struct file
*file
, char *buf
, size_t size
)
378 length
= task_has_security(current
, SECURITY__CHECK_CONTEXT
);
382 length
= security_context_to_sid(buf
, size
, &sid
);
386 length
= security_sid_to_context(sid
, &canon
, &len
);
390 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
391 printk(KERN_ERR
"SELinux: %s: context size (%u) exceeds "
392 "payload max\n", __func__
, len
);
397 memcpy(buf
, canon
, len
);
404 static ssize_t
sel_read_checkreqprot(struct file
*filp
, char __user
*buf
,
405 size_t count
, loff_t
*ppos
)
407 char tmpbuf
[TMPBUFLEN
];
410 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", selinux_checkreqprot
);
411 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
414 static ssize_t
sel_write_checkreqprot(struct file
*file
, const char __user
*buf
,
415 size_t count
, loff_t
*ppos
)
419 unsigned int new_value
;
421 length
= task_has_security(current
, SECURITY__SETCHECKREQPROT
);
425 if (count
>= PAGE_SIZE
)
428 /* No partial writes. */
431 page
= (char *)get_zeroed_page(GFP_KERNEL
);
435 if (copy_from_user(page
, buf
, count
))
439 if (sscanf(page
, "%u", &new_value
) != 1)
442 selinux_checkreqprot
= new_value
? 1 : 0;
445 free_page((unsigned long) page
);
448 static const struct file_operations sel_checkreqprot_ops
= {
449 .read
= sel_read_checkreqprot
,
450 .write
= sel_write_checkreqprot
,
453 static ssize_t
sel_read_compat_net(struct file
*filp
, char __user
*buf
,
454 size_t count
, loff_t
*ppos
)
456 char tmpbuf
[TMPBUFLEN
];
459 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_compat_net
);
460 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
463 static ssize_t
sel_write_compat_net(struct file
*file
, const char __user
*buf
,
464 size_t count
, loff_t
*ppos
)
470 length
= task_has_security(current
, SECURITY__LOAD_POLICY
);
474 if (count
>= PAGE_SIZE
)
477 /* No partial writes. */
480 page
= (char *)get_zeroed_page(GFP_KERNEL
);
484 if (copy_from_user(page
, buf
, count
))
488 if (sscanf(page
, "%d", &new_value
) != 1)
493 "SELinux: compat_net is deprecated, please use secmark"
495 selinux_compat_net
= 1;
497 selinux_compat_net
= 0;
500 free_page((unsigned long) page
);
503 static const struct file_operations sel_compat_net_ops
= {
504 .read
= sel_read_compat_net
,
505 .write
= sel_write_compat_net
,
509 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
511 static ssize_t
sel_write_access(struct file
*file
, char *buf
, size_t size
);
512 static ssize_t
sel_write_create(struct file
*file
, char *buf
, size_t size
);
513 static ssize_t
sel_write_relabel(struct file
*file
, char *buf
, size_t size
);
514 static ssize_t
sel_write_user(struct file
*file
, char *buf
, size_t size
);
515 static ssize_t
sel_write_member(struct file
*file
, char *buf
, size_t size
);
517 static ssize_t (*write_op
[])(struct file
*, char *, size_t) = {
518 [SEL_ACCESS
] = sel_write_access
,
519 [SEL_CREATE
] = sel_write_create
,
520 [SEL_RELABEL
] = sel_write_relabel
,
521 [SEL_USER
] = sel_write_user
,
522 [SEL_MEMBER
] = sel_write_member
,
523 [SEL_CONTEXT
] = sel_write_context
,
526 static ssize_t
selinux_transaction_write(struct file
*file
, const char __user
*buf
, size_t size
, loff_t
*pos
)
528 ino_t ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
532 if (ino
>= ARRAY_SIZE(write_op
) || !write_op
[ino
])
535 data
= simple_transaction_get(file
, buf
, size
);
537 return PTR_ERR(data
);
539 rv
= write_op
[ino
](file
, data
, size
);
541 simple_transaction_set(file
, rv
);
547 static const struct file_operations transaction_ops
= {
548 .write
= selinux_transaction_write
,
549 .read
= simple_transaction_read
,
550 .release
= simple_transaction_release
,
554 * payload - write methods
555 * If the method has a response, the response should be put in buf,
556 * and the length returned. Otherwise return 0 or and -error.
559 static ssize_t
sel_write_access(struct file
*file
, char *buf
, size_t size
)
565 struct av_decision avd
;
568 length
= task_has_security(current
, SECURITY__COMPUTE_AV
);
573 scon
= kzalloc(size
+1, GFP_KERNEL
);
577 tcon
= kzalloc(size
+1, GFP_KERNEL
);
582 if (sscanf(buf
, "%s %s %hu %x", scon
, tcon
, &tclass
, &req
) != 4)
585 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
588 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
592 length
= security_compute_av(ssid
, tsid
, tclass
, req
, &avd
);
596 length
= scnprintf(buf
, SIMPLE_TRANSACTION_LIMIT
,
598 avd
.allowed
, avd
.decided
,
599 avd
.auditallow
, avd
.auditdeny
,
608 static ssize_t
sel_write_create(struct file
*file
, char *buf
, size_t size
)
611 u32 ssid
, tsid
, newsid
;
617 length
= task_has_security(current
, SECURITY__COMPUTE_CREATE
);
622 scon
= kzalloc(size
+1, GFP_KERNEL
);
626 tcon
= kzalloc(size
+1, GFP_KERNEL
);
631 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
634 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
637 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
641 length
= security_transition_sid(ssid
, tsid
, tclass
, &newsid
);
645 length
= security_sid_to_context(newsid
, &newcon
, &len
);
649 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
650 printk(KERN_ERR
"SELinux: %s: context size (%u) exceeds "
651 "payload max\n", __func__
, len
);
656 memcpy(buf
, newcon
, len
);
667 static ssize_t
sel_write_relabel(struct file
*file
, char *buf
, size_t size
)
670 u32 ssid
, tsid
, newsid
;
676 length
= task_has_security(current
, SECURITY__COMPUTE_RELABEL
);
681 scon
= kzalloc(size
+1, GFP_KERNEL
);
685 tcon
= kzalloc(size
+1, GFP_KERNEL
);
690 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
693 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
696 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
700 length
= security_change_sid(ssid
, tsid
, tclass
, &newsid
);
704 length
= security_sid_to_context(newsid
, &newcon
, &len
);
708 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
713 memcpy(buf
, newcon
, len
);
724 static ssize_t
sel_write_user(struct file
*file
, char *buf
, size_t size
)
726 char *con
, *user
, *ptr
;
733 length
= task_has_security(current
, SECURITY__COMPUTE_USER
);
738 con
= kzalloc(size
+1, GFP_KERNEL
);
742 user
= kzalloc(size
+1, GFP_KERNEL
);
747 if (sscanf(buf
, "%s %s", con
, user
) != 2)
750 length
= security_context_to_sid(con
, strlen(con
)+1, &sid
);
754 length
= security_get_user_sids(sid
, user
, &sids
, &nsids
);
758 length
= sprintf(buf
, "%u", nsids
) + 1;
760 for (i
= 0; i
< nsids
; i
++) {
761 rc
= security_sid_to_context(sids
[i
], &newcon
, &len
);
766 if ((length
+ len
) >= SIMPLE_TRANSACTION_LIMIT
) {
771 memcpy(ptr
, newcon
, len
);
785 static ssize_t
sel_write_member(struct file
*file
, char *buf
, size_t size
)
788 u32 ssid
, tsid
, newsid
;
794 length
= task_has_security(current
, SECURITY__COMPUTE_MEMBER
);
799 scon
= kzalloc(size
+1, GFP_KERNEL
);
803 tcon
= kzalloc(size
+1, GFP_KERNEL
);
808 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
811 length
= security_context_to_sid(scon
, strlen(scon
)+1, &ssid
);
814 length
= security_context_to_sid(tcon
, strlen(tcon
)+1, &tsid
);
818 length
= security_member_sid(ssid
, tsid
, tclass
, &newsid
);
822 length
= security_sid_to_context(newsid
, &newcon
, &len
);
826 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
827 printk(KERN_ERR
"SELinux: %s: context size (%u) exceeds "
828 "payload max\n", __func__
, len
);
833 memcpy(buf
, newcon
, len
);
844 static struct inode
*sel_make_inode(struct super_block
*sb
, int mode
)
846 struct inode
*ret
= new_inode(sb
);
850 ret
->i_atime
= ret
->i_mtime
= ret
->i_ctime
= CURRENT_TIME
;
855 static ssize_t
sel_read_bool(struct file
*filep
, char __user
*buf
,
856 size_t count
, loff_t
*ppos
)
862 struct inode
*inode
= filep
->f_path
.dentry
->d_inode
;
863 unsigned index
= inode
->i_ino
& SEL_INO_MASK
;
864 const char *name
= filep
->f_path
.dentry
->d_name
.name
;
866 mutex_lock(&sel_mutex
);
868 if (index
>= bool_num
|| strcmp(name
, bool_pending_names
[index
])) {
873 if (count
> PAGE_SIZE
) {
877 page
= (char *)get_zeroed_page(GFP_KERNEL
);
883 cur_enforcing
= security_get_bool_value(index
);
884 if (cur_enforcing
< 0) {
888 length
= scnprintf(page
, PAGE_SIZE
, "%d %d", cur_enforcing
,
889 bool_pending_values
[index
]);
890 ret
= simple_read_from_buffer(buf
, count
, ppos
, page
, length
);
892 mutex_unlock(&sel_mutex
);
894 free_page((unsigned long)page
);
898 static ssize_t
sel_write_bool(struct file
*filep
, const char __user
*buf
,
899 size_t count
, loff_t
*ppos
)
904 struct inode
*inode
= filep
->f_path
.dentry
->d_inode
;
905 unsigned index
= inode
->i_ino
& SEL_INO_MASK
;
906 const char *name
= filep
->f_path
.dentry
->d_name
.name
;
908 mutex_lock(&sel_mutex
);
910 length
= task_has_security(current
, SECURITY__SETBOOL
);
914 if (index
>= bool_num
|| strcmp(name
, bool_pending_names
[index
])) {
919 if (count
>= PAGE_SIZE
) {
925 /* No partial writes. */
929 page
= (char *)get_zeroed_page(GFP_KERNEL
);
936 if (copy_from_user(page
, buf
, count
))
940 if (sscanf(page
, "%d", &new_value
) != 1)
946 bool_pending_values
[index
] = new_value
;
950 mutex_unlock(&sel_mutex
);
952 free_page((unsigned long) page
);
956 static const struct file_operations sel_bool_ops
= {
957 .read
= sel_read_bool
,
958 .write
= sel_write_bool
,
961 static ssize_t
sel_commit_bools_write(struct file
*filep
,
962 const char __user
*buf
,
963 size_t count
, loff_t
*ppos
)
969 mutex_lock(&sel_mutex
);
971 length
= task_has_security(current
, SECURITY__SETBOOL
);
975 if (count
>= PAGE_SIZE
) {
980 /* No partial writes. */
983 page
= (char *)get_zeroed_page(GFP_KERNEL
);
990 if (copy_from_user(page
, buf
, count
))
994 if (sscanf(page
, "%d", &new_value
) != 1)
997 if (new_value
&& bool_pending_values
)
998 security_set_bools(bool_num
, bool_pending_values
);
1003 mutex_unlock(&sel_mutex
);
1005 free_page((unsigned long) page
);
1009 static const struct file_operations sel_commit_bools_ops
= {
1010 .write
= sel_commit_bools_write
,
1013 static void sel_remove_entries(struct dentry
*de
)
1015 struct list_head
*node
;
1017 spin_lock(&dcache_lock
);
1018 node
= de
->d_subdirs
.next
;
1019 while (node
!= &de
->d_subdirs
) {
1020 struct dentry
*d
= list_entry(node
, struct dentry
, d_u
.d_child
);
1021 list_del_init(node
);
1025 spin_unlock(&dcache_lock
);
1027 simple_unlink(de
->d_inode
, d
);
1029 spin_lock(&dcache_lock
);
1031 node
= de
->d_subdirs
.next
;
1034 spin_unlock(&dcache_lock
);
1037 #define BOOL_DIR_NAME "booleans"
1039 static int sel_make_bools(void)
1043 struct dentry
*dentry
= NULL
;
1044 struct dentry
*dir
= bool_dir
;
1045 struct inode
*inode
= NULL
;
1046 struct inode_security_struct
*isec
;
1047 char **names
= NULL
, *page
;
1052 /* remove any existing files */
1053 kfree(bool_pending_names
);
1054 kfree(bool_pending_values
);
1055 bool_pending_names
= NULL
;
1056 bool_pending_values
= NULL
;
1058 sel_remove_entries(dir
);
1060 page
= (char *)get_zeroed_page(GFP_KERNEL
);
1064 ret
= security_get_bools(&num
, &names
, &values
);
1068 for (i
= 0; i
< num
; i
++) {
1069 dentry
= d_alloc_name(dir
, names
[i
]);
1074 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
| S_IRUGO
| S_IWUSR
);
1080 len
= snprintf(page
, PAGE_SIZE
, "/%s/%s", BOOL_DIR_NAME
, names
[i
]);
1084 } else if (len
>= PAGE_SIZE
) {
1085 ret
= -ENAMETOOLONG
;
1088 isec
= (struct inode_security_struct
*)inode
->i_security
;
1089 ret
= security_genfs_sid("selinuxfs", page
, SECCLASS_FILE
, &sid
);
1093 isec
->initialized
= 1;
1094 inode
->i_fop
= &sel_bool_ops
;
1095 inode
->i_ino
= i
|SEL_BOOL_INO_OFFSET
;
1096 d_add(dentry
, inode
);
1099 bool_pending_names
= names
;
1100 bool_pending_values
= values
;
1102 free_page((unsigned long)page
);
1106 for (i
= 0; i
< num
; i
++)
1111 sel_remove_entries(dir
);
1116 #define NULL_FILE_NAME "null"
1118 struct dentry
*selinux_null
;
1120 static ssize_t
sel_read_avc_cache_threshold(struct file
*filp
, char __user
*buf
,
1121 size_t count
, loff_t
*ppos
)
1123 char tmpbuf
[TMPBUFLEN
];
1126 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", avc_cache_threshold
);
1127 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1130 static ssize_t
sel_write_avc_cache_threshold(struct file
*file
,
1131 const char __user
*buf
,
1132 size_t count
, loff_t
*ppos
)
1139 if (count
>= PAGE_SIZE
) {
1145 /* No partial writes. */
1150 page
= (char *)get_zeroed_page(GFP_KERNEL
);
1156 if (copy_from_user(page
, buf
, count
)) {
1161 if (sscanf(page
, "%u", &new_value
) != 1) {
1166 if (new_value
!= avc_cache_threshold
) {
1167 ret
= task_has_security(current
, SECURITY__SETSECPARAM
);
1170 avc_cache_threshold
= new_value
;
1174 free_page((unsigned long)page
);
1179 static ssize_t
sel_read_avc_hash_stats(struct file
*filp
, char __user
*buf
,
1180 size_t count
, loff_t
*ppos
)
1185 page
= (char *)__get_free_page(GFP_KERNEL
);
1190 ret
= avc_get_hash_stats(page
);
1192 ret
= simple_read_from_buffer(buf
, count
, ppos
, page
, ret
);
1193 free_page((unsigned long)page
);
1198 static const struct file_operations sel_avc_cache_threshold_ops
= {
1199 .read
= sel_read_avc_cache_threshold
,
1200 .write
= sel_write_avc_cache_threshold
,
1203 static const struct file_operations sel_avc_hash_stats_ops
= {
1204 .read
= sel_read_avc_hash_stats
,
1207 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1208 static struct avc_cache_stats
*sel_avc_get_stat_idx(loff_t
*idx
)
1212 for (cpu
= *idx
; cpu
< nr_cpu_ids
; ++cpu
) {
1213 if (!cpu_possible(cpu
))
1216 return &per_cpu(avc_cache_stats
, cpu
);
1221 static void *sel_avc_stats_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1223 loff_t n
= *pos
- 1;
1226 return SEQ_START_TOKEN
;
1228 return sel_avc_get_stat_idx(&n
);
1231 static void *sel_avc_stats_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1233 return sel_avc_get_stat_idx(pos
);
1236 static int sel_avc_stats_seq_show(struct seq_file
*seq
, void *v
)
1238 struct avc_cache_stats
*st
= v
;
1240 if (v
== SEQ_START_TOKEN
)
1241 seq_printf(seq
, "lookups hits misses allocations reclaims "
1244 seq_printf(seq
, "%u %u %u %u %u %u\n", st
->lookups
,
1245 st
->hits
, st
->misses
, st
->allocations
,
1246 st
->reclaims
, st
->frees
);
1250 static void sel_avc_stats_seq_stop(struct seq_file
*seq
, void *v
)
1253 static const struct seq_operations sel_avc_cache_stats_seq_ops
= {
1254 .start
= sel_avc_stats_seq_start
,
1255 .next
= sel_avc_stats_seq_next
,
1256 .show
= sel_avc_stats_seq_show
,
1257 .stop
= sel_avc_stats_seq_stop
,
1260 static int sel_open_avc_cache_stats(struct inode
*inode
, struct file
*file
)
1262 return seq_open(file
, &sel_avc_cache_stats_seq_ops
);
1265 static const struct file_operations sel_avc_cache_stats_ops
= {
1266 .open
= sel_open_avc_cache_stats
,
1268 .llseek
= seq_lseek
,
1269 .release
= seq_release
,
1273 static int sel_make_avc_files(struct dentry
*dir
)
1276 static struct tree_descr files
[] = {
1277 { "cache_threshold",
1278 &sel_avc_cache_threshold_ops
, S_IRUGO
|S_IWUSR
},
1279 { "hash_stats", &sel_avc_hash_stats_ops
, S_IRUGO
},
1280 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1281 { "cache_stats", &sel_avc_cache_stats_ops
, S_IRUGO
},
1285 for (i
= 0; i
< ARRAY_SIZE(files
); i
++) {
1286 struct inode
*inode
;
1287 struct dentry
*dentry
;
1289 dentry
= d_alloc_name(dir
, files
[i
].name
);
1295 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|files
[i
].mode
);
1300 inode
->i_fop
= files
[i
].ops
;
1301 inode
->i_ino
= ++sel_last_ino
;
1302 d_add(dentry
, inode
);
1308 static ssize_t
sel_read_initcon(struct file
*file
, char __user
*buf
,
1309 size_t count
, loff_t
*ppos
)
1311 struct inode
*inode
;
1316 inode
= file
->f_path
.dentry
->d_inode
;
1317 sid
= inode
->i_ino
&SEL_INO_MASK
;
1318 ret
= security_sid_to_context(sid
, &con
, &len
);
1322 ret
= simple_read_from_buffer(buf
, count
, ppos
, con
, len
);
1327 static const struct file_operations sel_initcon_ops
= {
1328 .read
= sel_read_initcon
,
1331 static int sel_make_initcon_files(struct dentry
*dir
)
1335 for (i
= 1; i
<= SECINITSID_NUM
; i
++) {
1336 struct inode
*inode
;
1337 struct dentry
*dentry
;
1338 dentry
= d_alloc_name(dir
, security_get_initial_sid_context(i
));
1344 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1349 inode
->i_fop
= &sel_initcon_ops
;
1350 inode
->i_ino
= i
|SEL_INITCON_INO_OFFSET
;
1351 d_add(dentry
, inode
);
1357 static inline unsigned int sel_div(unsigned long a
, unsigned long b
)
1359 return a
/ b
- (a
% b
< 0);
1362 static inline unsigned long sel_class_to_ino(u16
class)
1364 return (class * (SEL_VEC_MAX
+ 1)) | SEL_CLASS_INO_OFFSET
;
1367 static inline u16
sel_ino_to_class(unsigned long ino
)
1369 return sel_div(ino
& SEL_INO_MASK
, SEL_VEC_MAX
+ 1);
1372 static inline unsigned long sel_perm_to_ino(u16
class, u32 perm
)
1374 return (class * (SEL_VEC_MAX
+ 1) + perm
) | SEL_CLASS_INO_OFFSET
;
1377 static inline u32
sel_ino_to_perm(unsigned long ino
)
1379 return (ino
& SEL_INO_MASK
) % (SEL_VEC_MAX
+ 1);
1382 static ssize_t
sel_read_class(struct file
*file
, char __user
*buf
,
1383 size_t count
, loff_t
*ppos
)
1387 unsigned long ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
1389 page
= (char *)__get_free_page(GFP_KERNEL
);
1395 len
= snprintf(page
, PAGE_SIZE
, "%d", sel_ino_to_class(ino
));
1396 rc
= simple_read_from_buffer(buf
, count
, ppos
, page
, len
);
1397 free_page((unsigned long)page
);
1402 static const struct file_operations sel_class_ops
= {
1403 .read
= sel_read_class
,
1406 static ssize_t
sel_read_perm(struct file
*file
, char __user
*buf
,
1407 size_t count
, loff_t
*ppos
)
1411 unsigned long ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
1413 page
= (char *)__get_free_page(GFP_KERNEL
);
1419 len
= snprintf(page
, PAGE_SIZE
, "%d", sel_ino_to_perm(ino
));
1420 rc
= simple_read_from_buffer(buf
, count
, ppos
, page
, len
);
1421 free_page((unsigned long)page
);
1426 static const struct file_operations sel_perm_ops
= {
1427 .read
= sel_read_perm
,
1430 static ssize_t
sel_read_policycap(struct file
*file
, char __user
*buf
,
1431 size_t count
, loff_t
*ppos
)
1434 char tmpbuf
[TMPBUFLEN
];
1436 unsigned long i_ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
1438 value
= security_policycap_supported(i_ino
& SEL_INO_MASK
);
1439 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", value
);
1441 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1444 static const struct file_operations sel_policycap_ops
= {
1445 .read
= sel_read_policycap
,
1448 static int sel_make_perm_files(char *objclass
, int classvalue
,
1451 int i
, rc
= 0, nperms
;
1454 rc
= security_get_permissions(objclass
, &perms
, &nperms
);
1458 for (i
= 0; i
< nperms
; i
++) {
1459 struct inode
*inode
;
1460 struct dentry
*dentry
;
1462 dentry
= d_alloc_name(dir
, perms
[i
]);
1468 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1473 inode
->i_fop
= &sel_perm_ops
;
1474 /* i+1 since perm values are 1-indexed */
1475 inode
->i_ino
= sel_perm_to_ino(classvalue
, i
+1);
1476 d_add(dentry
, inode
);
1480 for (i
= 0; i
< nperms
; i
++)
1487 static int sel_make_class_dir_entries(char *classname
, int index
,
1490 struct dentry
*dentry
= NULL
;
1491 struct inode
*inode
= NULL
;
1494 dentry
= d_alloc_name(dir
, "index");
1500 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1506 inode
->i_fop
= &sel_class_ops
;
1507 inode
->i_ino
= sel_class_to_ino(index
);
1508 d_add(dentry
, inode
);
1510 dentry
= d_alloc_name(dir
, "perms");
1516 rc
= sel_make_dir(dir
->d_inode
, dentry
, &last_class_ino
);
1520 rc
= sel_make_perm_files(classname
, index
, dentry
);
1526 static void sel_remove_classes(void)
1528 struct list_head
*class_node
;
1530 list_for_each(class_node
, &class_dir
->d_subdirs
) {
1531 struct dentry
*class_subdir
= list_entry(class_node
,
1532 struct dentry
, d_u
.d_child
);
1533 struct list_head
*class_subdir_node
;
1535 list_for_each(class_subdir_node
, &class_subdir
->d_subdirs
) {
1536 struct dentry
*d
= list_entry(class_subdir_node
,
1537 struct dentry
, d_u
.d_child
);
1540 if (d
->d_inode
->i_mode
& S_IFDIR
)
1541 sel_remove_entries(d
);
1544 sel_remove_entries(class_subdir
);
1547 sel_remove_entries(class_dir
);
1550 static int sel_make_classes(void)
1552 int rc
= 0, nclasses
, i
;
1555 /* delete any existing entries */
1556 sel_remove_classes();
1558 rc
= security_get_classes(&classes
, &nclasses
);
1562 /* +2 since classes are 1-indexed */
1563 last_class_ino
= sel_class_to_ino(nclasses
+2);
1565 for (i
= 0; i
< nclasses
; i
++) {
1566 struct dentry
*class_name_dir
;
1568 class_name_dir
= d_alloc_name(class_dir
, classes
[i
]);
1569 if (!class_name_dir
) {
1574 rc
= sel_make_dir(class_dir
->d_inode
, class_name_dir
,
1579 /* i+1 since class values are 1-indexed */
1580 rc
= sel_make_class_dir_entries(classes
[i
], i
+1,
1587 for (i
= 0; i
< nclasses
; i
++)
1594 static int sel_make_policycap(void)
1597 struct dentry
*dentry
= NULL
;
1598 struct inode
*inode
= NULL
;
1600 sel_remove_entries(policycap_dir
);
1602 for (iter
= 0; iter
<= POLICYDB_CAPABILITY_MAX
; iter
++) {
1603 if (iter
< ARRAY_SIZE(policycap_names
))
1604 dentry
= d_alloc_name(policycap_dir
,
1605 policycap_names
[iter
]);
1607 dentry
= d_alloc_name(policycap_dir
, "unknown");
1612 inode
= sel_make_inode(policycap_dir
->d_sb
, S_IFREG
| S_IRUGO
);
1616 inode
->i_fop
= &sel_policycap_ops
;
1617 inode
->i_ino
= iter
| SEL_POLICYCAP_INO_OFFSET
;
1618 d_add(dentry
, inode
);
1624 static int sel_make_dir(struct inode
*dir
, struct dentry
*dentry
,
1628 struct inode
*inode
;
1630 inode
= sel_make_inode(dir
->i_sb
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
1635 inode
->i_op
= &simple_dir_inode_operations
;
1636 inode
->i_fop
= &simple_dir_operations
;
1637 inode
->i_ino
= ++(*ino
);
1638 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1640 d_add(dentry
, inode
);
1641 /* bump link count on parent directory, too */
1647 static int sel_fill_super(struct super_block
*sb
, void *data
, int silent
)
1650 struct dentry
*dentry
;
1651 struct inode
*inode
, *root_inode
;
1652 struct inode_security_struct
*isec
;
1654 static struct tree_descr selinux_files
[] = {
1655 [SEL_LOAD
] = {"load", &sel_load_ops
, S_IRUSR
|S_IWUSR
},
1656 [SEL_ENFORCE
] = {"enforce", &sel_enforce_ops
, S_IRUGO
|S_IWUSR
},
1657 [SEL_CONTEXT
] = {"context", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1658 [SEL_ACCESS
] = {"access", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1659 [SEL_CREATE
] = {"create", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1660 [SEL_RELABEL
] = {"relabel", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1661 [SEL_USER
] = {"user", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1662 [SEL_POLICYVERS
] = {"policyvers", &sel_policyvers_ops
, S_IRUGO
},
1663 [SEL_COMMIT_BOOLS
] = {"commit_pending_bools", &sel_commit_bools_ops
, S_IWUSR
},
1664 [SEL_MLS
] = {"mls", &sel_mls_ops
, S_IRUGO
},
1665 [SEL_DISABLE
] = {"disable", &sel_disable_ops
, S_IWUSR
},
1666 [SEL_MEMBER
] = {"member", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1667 [SEL_CHECKREQPROT
] = {"checkreqprot", &sel_checkreqprot_ops
, S_IRUGO
|S_IWUSR
},
1668 [SEL_COMPAT_NET
] = {"compat_net", &sel_compat_net_ops
, S_IRUGO
|S_IWUSR
},
1669 [SEL_REJECT_UNKNOWN
] = {"reject_unknown", &sel_handle_unknown_ops
, S_IRUGO
},
1670 [SEL_DENY_UNKNOWN
] = {"deny_unknown", &sel_handle_unknown_ops
, S_IRUGO
},
1673 ret
= simple_fill_super(sb
, SELINUX_MAGIC
, selinux_files
);
1677 root_inode
= sb
->s_root
->d_inode
;
1679 dentry
= d_alloc_name(sb
->s_root
, BOOL_DIR_NAME
);
1685 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1691 dentry
= d_alloc_name(sb
->s_root
, NULL_FILE_NAME
);
1697 inode
= sel_make_inode(sb
, S_IFCHR
| S_IRUGO
| S_IWUGO
);
1702 inode
->i_ino
= ++sel_last_ino
;
1703 isec
= (struct inode_security_struct
*)inode
->i_security
;
1704 isec
->sid
= SECINITSID_DEVNULL
;
1705 isec
->sclass
= SECCLASS_CHR_FILE
;
1706 isec
->initialized
= 1;
1708 init_special_inode(inode
, S_IFCHR
| S_IRUGO
| S_IWUGO
, MKDEV(MEM_MAJOR
, 3));
1709 d_add(dentry
, inode
);
1710 selinux_null
= dentry
;
1712 dentry
= d_alloc_name(sb
->s_root
, "avc");
1718 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1722 ret
= sel_make_avc_files(dentry
);
1726 dentry
= d_alloc_name(sb
->s_root
, "initial_contexts");
1732 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1736 ret
= sel_make_initcon_files(dentry
);
1740 dentry
= d_alloc_name(sb
->s_root
, "class");
1746 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1752 dentry
= d_alloc_name(sb
->s_root
, "policy_capabilities");
1758 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1762 policycap_dir
= dentry
;
1767 printk(KERN_ERR
"SELinux: %s: failed while creating inodes\n",
1772 static int sel_get_sb(struct file_system_type
*fs_type
,
1773 int flags
, const char *dev_name
, void *data
,
1774 struct vfsmount
*mnt
)
1776 return get_sb_single(fs_type
, flags
, data
, sel_fill_super
, mnt
);
1779 static struct file_system_type sel_fs_type
= {
1780 .name
= "selinuxfs",
1781 .get_sb
= sel_get_sb
,
1782 .kill_sb
= kill_litter_super
,
1785 struct vfsmount
*selinuxfs_mount
;
1787 static int __init
init_sel_fs(void)
1791 if (!selinux_enabled
)
1793 err
= register_filesystem(&sel_fs_type
);
1795 selinuxfs_mount
= kern_mount(&sel_fs_type
);
1796 if (IS_ERR(selinuxfs_mount
)) {
1797 printk(KERN_ERR
"selinuxfs: could not mount!\n");
1798 err
= PTR_ERR(selinuxfs_mount
);
1799 selinuxfs_mount
= NULL
;
1805 __initcall(init_sel_fs
);
1807 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1808 void exit_sel_fs(void)
1810 unregister_filesystem(&sel_fs_type
);