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>
31 #include <asm/semaphore.h>
33 /* selinuxfs pseudo filesystem for exporting the security policy API.
34 Based on the proc code and the fs/nfsd/nfsctl.c code. */
41 #include "conditional.h"
43 /* Policy capability filenames */
44 static char *policycap_names
[] = {
45 "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
= NULL
;
77 static int bool_num
= 0;
78 static char **bool_pending_names
;
79 static int *bool_pending_values
= NULL
;
81 /* global data for classes */
82 static struct dentry
*class_dir
= NULL
;
83 static unsigned long last_class_ino
;
85 /* global data for policy capabilities */
86 static struct dentry
*policycap_dir
= NULL
;
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
"%s: context size (%u) exceeds payload "
394 "max\n", __FUNCTION__
, 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
"%s: context size (%u) exceeds payload "
647 "max\n", __FUNCTION__
, 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
"%s: context size (%u) exceeds payload "
824 "max\n", __FUNCTION__
, 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 if (!(page
= (char*)get_zeroed_page(GFP_KERNEL
))) {
880 cur_enforcing
= security_get_bool_value(index
);
881 if (cur_enforcing
< 0) {
885 length
= scnprintf(page
, PAGE_SIZE
, "%d %d", cur_enforcing
,
886 bool_pending_values
[index
]);
887 ret
= simple_read_from_buffer(buf
, count
, ppos
, page
, length
);
889 mutex_unlock(&sel_mutex
);
891 free_page((unsigned long)page
);
895 static ssize_t
sel_write_bool(struct file
*filep
, const char __user
*buf
,
896 size_t count
, loff_t
*ppos
)
901 struct inode
*inode
= filep
->f_path
.dentry
->d_inode
;
902 unsigned index
= inode
->i_ino
& SEL_INO_MASK
;
903 const char *name
= filep
->f_path
.dentry
->d_name
.name
;
905 mutex_lock(&sel_mutex
);
907 length
= task_has_security(current
, SECURITY__SETBOOL
);
911 if (index
>= bool_num
|| strcmp(name
, bool_pending_names
[index
])) {
916 if (count
>= PAGE_SIZE
) {
922 /* No partial writes. */
926 page
= (char*)get_zeroed_page(GFP_KERNEL
);
933 if (copy_from_user(page
, buf
, count
))
937 if (sscanf(page
, "%d", &new_value
) != 1)
943 bool_pending_values
[index
] = new_value
;
947 mutex_unlock(&sel_mutex
);
949 free_page((unsigned long) page
);
953 static const struct file_operations sel_bool_ops
= {
954 .read
= sel_read_bool
,
955 .write
= sel_write_bool
,
958 static ssize_t
sel_commit_bools_write(struct file
*filep
,
959 const char __user
*buf
,
960 size_t count
, loff_t
*ppos
)
966 mutex_lock(&sel_mutex
);
968 length
= task_has_security(current
, SECURITY__SETBOOL
);
972 if (count
>= PAGE_SIZE
) {
977 /* No partial writes. */
980 page
= (char*)get_zeroed_page(GFP_KERNEL
);
987 if (copy_from_user(page
, buf
, count
))
991 if (sscanf(page
, "%d", &new_value
) != 1)
994 if (new_value
&& bool_pending_values
) {
995 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 if (!(page
= (char*)get_zeroed_page(GFP_KERNEL
)))
1061 ret
= security_get_bools(&num
, &names
, &values
);
1065 for (i
= 0; i
< num
; i
++) {
1066 dentry
= d_alloc_name(dir
, names
[i
]);
1071 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
| S_IRUGO
| S_IWUSR
);
1077 len
= snprintf(page
, PAGE_SIZE
, "/%s/%s", BOOL_DIR_NAME
, names
[i
]);
1081 } else if (len
>= PAGE_SIZE
) {
1082 ret
= -ENAMETOOLONG
;
1085 isec
= (struct inode_security_struct
*)inode
->i_security
;
1086 if ((ret
= security_genfs_sid("selinuxfs", page
, SECCLASS_FILE
, &sid
)))
1089 isec
->initialized
= 1;
1090 inode
->i_fop
= &sel_bool_ops
;
1091 inode
->i_ino
= i
|SEL_BOOL_INO_OFFSET
;
1092 d_add(dentry
, inode
);
1095 bool_pending_names
= names
;
1096 bool_pending_values
= values
;
1098 free_page((unsigned long)page
);
1102 for (i
= 0; i
< num
; i
++)
1107 sel_remove_entries(dir
);
1112 #define NULL_FILE_NAME "null"
1114 struct dentry
*selinux_null
= NULL
;
1116 static ssize_t
sel_read_avc_cache_threshold(struct file
*filp
, char __user
*buf
,
1117 size_t count
, loff_t
*ppos
)
1119 char tmpbuf
[TMPBUFLEN
];
1122 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", avc_cache_threshold
);
1123 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1126 static ssize_t
sel_write_avc_cache_threshold(struct file
* file
,
1127 const char __user
* buf
,
1128 size_t count
, loff_t
*ppos
)
1135 if (count
>= PAGE_SIZE
) {
1141 /* No partial writes. */
1146 page
= (char*)get_zeroed_page(GFP_KERNEL
);
1152 if (copy_from_user(page
, buf
, count
)) {
1157 if (sscanf(page
, "%u", &new_value
) != 1) {
1162 if (new_value
!= avc_cache_threshold
) {
1163 ret
= task_has_security(current
, SECURITY__SETSECPARAM
);
1166 avc_cache_threshold
= new_value
;
1170 free_page((unsigned long)page
);
1175 static ssize_t
sel_read_avc_hash_stats(struct file
*filp
, char __user
*buf
,
1176 size_t count
, loff_t
*ppos
)
1181 page
= (char *)__get_free_page(GFP_KERNEL
);
1186 ret
= avc_get_hash_stats(page
);
1188 ret
= simple_read_from_buffer(buf
, count
, ppos
, page
, ret
);
1189 free_page((unsigned long)page
);
1194 static const struct file_operations sel_avc_cache_threshold_ops
= {
1195 .read
= sel_read_avc_cache_threshold
,
1196 .write
= sel_write_avc_cache_threshold
,
1199 static const struct file_operations sel_avc_hash_stats_ops
= {
1200 .read
= sel_read_avc_hash_stats
,
1203 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1204 static struct avc_cache_stats
*sel_avc_get_stat_idx(loff_t
*idx
)
1208 for (cpu
= *idx
; cpu
< NR_CPUS
; ++cpu
) {
1209 if (!cpu_possible(cpu
))
1212 return &per_cpu(avc_cache_stats
, cpu
);
1217 static void *sel_avc_stats_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1219 loff_t n
= *pos
- 1;
1222 return SEQ_START_TOKEN
;
1224 return sel_avc_get_stat_idx(&n
);
1227 static void *sel_avc_stats_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1229 return sel_avc_get_stat_idx(pos
);
1232 static int sel_avc_stats_seq_show(struct seq_file
*seq
, void *v
)
1234 struct avc_cache_stats
*st
= v
;
1236 if (v
== SEQ_START_TOKEN
)
1237 seq_printf(seq
, "lookups hits misses allocations reclaims "
1240 seq_printf(seq
, "%u %u %u %u %u %u\n", st
->lookups
,
1241 st
->hits
, st
->misses
, st
->allocations
,
1242 st
->reclaims
, st
->frees
);
1246 static void sel_avc_stats_seq_stop(struct seq_file
*seq
, void *v
)
1249 static const struct seq_operations sel_avc_cache_stats_seq_ops
= {
1250 .start
= sel_avc_stats_seq_start
,
1251 .next
= sel_avc_stats_seq_next
,
1252 .show
= sel_avc_stats_seq_show
,
1253 .stop
= sel_avc_stats_seq_stop
,
1256 static int sel_open_avc_cache_stats(struct inode
*inode
, struct file
*file
)
1258 return seq_open(file
, &sel_avc_cache_stats_seq_ops
);
1261 static const struct file_operations sel_avc_cache_stats_ops
= {
1262 .open
= sel_open_avc_cache_stats
,
1264 .llseek
= seq_lseek
,
1265 .release
= seq_release
,
1269 static int sel_make_avc_files(struct dentry
*dir
)
1272 static struct tree_descr files
[] = {
1273 { "cache_threshold",
1274 &sel_avc_cache_threshold_ops
, S_IRUGO
|S_IWUSR
},
1275 { "hash_stats", &sel_avc_hash_stats_ops
, S_IRUGO
},
1276 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1277 { "cache_stats", &sel_avc_cache_stats_ops
, S_IRUGO
},
1281 for (i
= 0; i
< ARRAY_SIZE(files
); i
++) {
1282 struct inode
*inode
;
1283 struct dentry
*dentry
;
1285 dentry
= d_alloc_name(dir
, files
[i
].name
);
1291 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|files
[i
].mode
);
1296 inode
->i_fop
= files
[i
].ops
;
1297 inode
->i_ino
= ++sel_last_ino
;
1298 d_add(dentry
, inode
);
1304 static ssize_t
sel_read_initcon(struct file
* file
, char __user
*buf
,
1305 size_t count
, loff_t
*ppos
)
1307 struct inode
*inode
;
1312 inode
= file
->f_path
.dentry
->d_inode
;
1313 sid
= inode
->i_ino
&SEL_INO_MASK
;
1314 ret
= security_sid_to_context(sid
, &con
, &len
);
1318 ret
= simple_read_from_buffer(buf
, count
, ppos
, con
, len
);
1323 static const struct file_operations sel_initcon_ops
= {
1324 .read
= sel_read_initcon
,
1327 static int sel_make_initcon_files(struct dentry
*dir
)
1331 for (i
= 1; i
<= SECINITSID_NUM
; i
++) {
1332 struct inode
*inode
;
1333 struct dentry
*dentry
;
1334 dentry
= d_alloc_name(dir
, security_get_initial_sid_context(i
));
1340 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1345 inode
->i_fop
= &sel_initcon_ops
;
1346 inode
->i_ino
= i
|SEL_INITCON_INO_OFFSET
;
1347 d_add(dentry
, inode
);
1353 static inline unsigned int sel_div(unsigned long a
, unsigned long b
)
1355 return a
/ b
- (a
% b
< 0);
1358 static inline unsigned long sel_class_to_ino(u16
class)
1360 return (class * (SEL_VEC_MAX
+ 1)) | SEL_CLASS_INO_OFFSET
;
1363 static inline u16
sel_ino_to_class(unsigned long ino
)
1365 return sel_div(ino
& SEL_INO_MASK
, SEL_VEC_MAX
+ 1);
1368 static inline unsigned long sel_perm_to_ino(u16
class, u32 perm
)
1370 return (class * (SEL_VEC_MAX
+ 1) + perm
) | SEL_CLASS_INO_OFFSET
;
1373 static inline u32
sel_ino_to_perm(unsigned long ino
)
1375 return (ino
& SEL_INO_MASK
) % (SEL_VEC_MAX
+ 1);
1378 static ssize_t
sel_read_class(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_class(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_class_ops
= {
1399 .read
= sel_read_class
,
1402 static ssize_t
sel_read_perm(struct file
* file
, char __user
*buf
,
1403 size_t count
, loff_t
*ppos
)
1407 unsigned long ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
1409 page
= (char *)__get_free_page(GFP_KERNEL
);
1415 len
= snprintf(page
, PAGE_SIZE
,"%d", sel_ino_to_perm(ino
));
1416 rc
= simple_read_from_buffer(buf
, count
, ppos
, page
, len
);
1417 free_page((unsigned long)page
);
1422 static const struct file_operations sel_perm_ops
= {
1423 .read
= sel_read_perm
,
1426 static ssize_t
sel_read_policycap(struct file
*file
, char __user
*buf
,
1427 size_t count
, loff_t
*ppos
)
1430 char tmpbuf
[TMPBUFLEN
];
1432 unsigned long i_ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
1434 value
= security_policycap_supported(i_ino
& SEL_INO_MASK
);
1435 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", value
);
1437 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1440 static const struct file_operations sel_policycap_ops
= {
1441 .read
= sel_read_policycap
,
1444 static int sel_make_perm_files(char *objclass
, int classvalue
,
1447 int i
, rc
= 0, nperms
;
1450 rc
= security_get_permissions(objclass
, &perms
, &nperms
);
1454 for (i
= 0; i
< nperms
; i
++) {
1455 struct inode
*inode
;
1456 struct dentry
*dentry
;
1458 dentry
= d_alloc_name(dir
, perms
[i
]);
1464 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1469 inode
->i_fop
= &sel_perm_ops
;
1470 /* i+1 since perm values are 1-indexed */
1471 inode
->i_ino
= sel_perm_to_ino(classvalue
, i
+1);
1472 d_add(dentry
, inode
);
1476 for (i
= 0; i
< nperms
; i
++)
1483 static int sel_make_class_dir_entries(char *classname
, int index
,
1486 struct dentry
*dentry
= NULL
;
1487 struct inode
*inode
= NULL
;
1490 dentry
= d_alloc_name(dir
, "index");
1496 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1502 inode
->i_fop
= &sel_class_ops
;
1503 inode
->i_ino
= sel_class_to_ino(index
);
1504 d_add(dentry
, inode
);
1506 dentry
= d_alloc_name(dir
, "perms");
1512 rc
= sel_make_dir(dir
->d_inode
, dentry
, &last_class_ino
);
1516 rc
= sel_make_perm_files(classname
, index
, dentry
);
1522 static void sel_remove_classes(void)
1524 struct list_head
*class_node
;
1526 list_for_each(class_node
, &class_dir
->d_subdirs
) {
1527 struct dentry
*class_subdir
= list_entry(class_node
,
1528 struct dentry
, d_u
.d_child
);
1529 struct list_head
*class_subdir_node
;
1531 list_for_each(class_subdir_node
, &class_subdir
->d_subdirs
) {
1532 struct dentry
*d
= list_entry(class_subdir_node
,
1533 struct dentry
, d_u
.d_child
);
1536 if (d
->d_inode
->i_mode
& S_IFDIR
)
1537 sel_remove_entries(d
);
1540 sel_remove_entries(class_subdir
);
1543 sel_remove_entries(class_dir
);
1546 static int sel_make_classes(void)
1548 int rc
= 0, nclasses
, i
;
1551 /* delete any existing entries */
1552 sel_remove_classes();
1554 rc
= security_get_classes(&classes
, &nclasses
);
1558 /* +2 since classes are 1-indexed */
1559 last_class_ino
= sel_class_to_ino(nclasses
+2);
1561 for (i
= 0; i
< nclasses
; i
++) {
1562 struct dentry
*class_name_dir
;
1564 class_name_dir
= d_alloc_name(class_dir
, classes
[i
]);
1565 if (!class_name_dir
) {
1570 rc
= sel_make_dir(class_dir
->d_inode
, class_name_dir
,
1575 /* i+1 since class values are 1-indexed */
1576 rc
= sel_make_class_dir_entries(classes
[i
], i
+1,
1583 for (i
= 0; i
< nclasses
; i
++)
1590 static int sel_make_policycap(void)
1593 struct dentry
*dentry
= NULL
;
1594 struct inode
*inode
= NULL
;
1596 sel_remove_entries(policycap_dir
);
1598 for (iter
= 0; iter
<= POLICYDB_CAPABILITY_MAX
; iter
++) {
1599 if (iter
< ARRAY_SIZE(policycap_names
))
1600 dentry
= d_alloc_name(policycap_dir
,
1601 policycap_names
[iter
]);
1603 dentry
= d_alloc_name(policycap_dir
, "unknown");
1608 inode
= sel_make_inode(policycap_dir
->d_sb
, S_IFREG
| S_IRUGO
);
1612 inode
->i_fop
= &sel_policycap_ops
;
1613 inode
->i_ino
= iter
| SEL_POLICYCAP_INO_OFFSET
;
1614 d_add(dentry
, inode
);
1620 static int sel_make_dir(struct inode
*dir
, struct dentry
*dentry
,
1624 struct inode
*inode
;
1626 inode
= sel_make_inode(dir
->i_sb
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
1631 inode
->i_op
= &simple_dir_inode_operations
;
1632 inode
->i_fop
= &simple_dir_operations
;
1633 inode
->i_ino
= ++(*ino
);
1634 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1636 d_add(dentry
, inode
);
1637 /* bump link count on parent directory, too */
1643 static int sel_fill_super(struct super_block
* sb
, void * data
, int silent
)
1646 struct dentry
*dentry
;
1647 struct inode
*inode
, *root_inode
;
1648 struct inode_security_struct
*isec
;
1650 static struct tree_descr selinux_files
[] = {
1651 [SEL_LOAD
] = {"load", &sel_load_ops
, S_IRUSR
|S_IWUSR
},
1652 [SEL_ENFORCE
] = {"enforce", &sel_enforce_ops
, S_IRUGO
|S_IWUSR
},
1653 [SEL_CONTEXT
] = {"context", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1654 [SEL_ACCESS
] = {"access", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1655 [SEL_CREATE
] = {"create", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1656 [SEL_RELABEL
] = {"relabel", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1657 [SEL_USER
] = {"user", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1658 [SEL_POLICYVERS
] = {"policyvers", &sel_policyvers_ops
, S_IRUGO
},
1659 [SEL_COMMIT_BOOLS
] = {"commit_pending_bools", &sel_commit_bools_ops
, S_IWUSR
},
1660 [SEL_MLS
] = {"mls", &sel_mls_ops
, S_IRUGO
},
1661 [SEL_DISABLE
] = {"disable", &sel_disable_ops
, S_IWUSR
},
1662 [SEL_MEMBER
] = {"member", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1663 [SEL_CHECKREQPROT
] = {"checkreqprot", &sel_checkreqprot_ops
, S_IRUGO
|S_IWUSR
},
1664 [SEL_COMPAT_NET
] = {"compat_net", &sel_compat_net_ops
, S_IRUGO
|S_IWUSR
},
1665 [SEL_REJECT_UNKNOWN
] = {"reject_unknown", &sel_handle_unknown_ops
, S_IRUGO
},
1666 [SEL_DENY_UNKNOWN
] = {"deny_unknown", &sel_handle_unknown_ops
, S_IRUGO
},
1669 ret
= simple_fill_super(sb
, SELINUX_MAGIC
, selinux_files
);
1673 root_inode
= sb
->s_root
->d_inode
;
1675 dentry
= d_alloc_name(sb
->s_root
, BOOL_DIR_NAME
);
1681 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1687 dentry
= d_alloc_name(sb
->s_root
, NULL_FILE_NAME
);
1693 inode
= sel_make_inode(sb
, S_IFCHR
| S_IRUGO
| S_IWUGO
);
1698 inode
->i_ino
= ++sel_last_ino
;
1699 isec
= (struct inode_security_struct
*)inode
->i_security
;
1700 isec
->sid
= SECINITSID_DEVNULL
;
1701 isec
->sclass
= SECCLASS_CHR_FILE
;
1702 isec
->initialized
= 1;
1704 init_special_inode(inode
, S_IFCHR
| S_IRUGO
| S_IWUGO
, MKDEV(MEM_MAJOR
, 3));
1705 d_add(dentry
, inode
);
1706 selinux_null
= dentry
;
1708 dentry
= d_alloc_name(sb
->s_root
, "avc");
1714 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1718 ret
= sel_make_avc_files(dentry
);
1722 dentry
= d_alloc_name(sb
->s_root
, "initial_contexts");
1728 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1732 ret
= sel_make_initcon_files(dentry
);
1736 dentry
= d_alloc_name(sb
->s_root
, "class");
1742 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1748 dentry
= d_alloc_name(sb
->s_root
, "policy_capabilities");
1754 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1758 policycap_dir
= dentry
;
1763 printk(KERN_ERR
"%s: failed while creating inodes\n", __FUNCTION__
);
1767 static int sel_get_sb(struct file_system_type
*fs_type
,
1768 int flags
, const char *dev_name
, void *data
,
1769 struct vfsmount
*mnt
)
1771 return get_sb_single(fs_type
, flags
, data
, sel_fill_super
, mnt
);
1774 static struct file_system_type sel_fs_type
= {
1775 .name
= "selinuxfs",
1776 .get_sb
= sel_get_sb
,
1777 .kill_sb
= kill_litter_super
,
1780 struct vfsmount
*selinuxfs_mount
;
1782 static int __init
init_sel_fs(void)
1786 if (!selinux_enabled
)
1788 err
= register_filesystem(&sel_fs_type
);
1790 selinuxfs_mount
= kern_mount(&sel_fs_type
);
1791 if (IS_ERR(selinuxfs_mount
)) {
1792 printk(KERN_ERR
"selinuxfs: could not mount!\n");
1793 err
= PTR_ERR(selinuxfs_mount
);
1794 selinuxfs_mount
= NULL
;
1800 __initcall(init_sel_fs
);
1802 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1803 void exit_sel_fs(void)
1805 unregister_filesystem(&sel_fs_type
);