1 /* Updated: Karl MacMillan <kmacmillan@tresys.com>
3 * Added conditional policy language extensions
5 * Updated: Hewlett-Packard <paul@paul-moore.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>
31 #include <linux/kobject.h>
32 #include <linux/ctype.h>
34 /* selinuxfs pseudo filesystem for exporting the security policy API.
35 Based on the proc code and the fs/nfsd/nfsctl.c code. */
42 #include "conditional.h"
44 /* Policy capability filenames */
45 static char *policycap_names
[] = {
46 "network_peer_controls",
49 "always_check_network"
52 unsigned int selinux_checkreqprot
= CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE
;
54 static int __init
checkreqprot_setup(char *str
)
56 unsigned long checkreqprot
;
57 if (!kstrtoul(str
, 0, &checkreqprot
))
58 selinux_checkreqprot
= checkreqprot
? 1 : 0;
61 __setup("checkreqprot=", checkreqprot_setup
);
63 static DEFINE_MUTEX(sel_mutex
);
65 /* global data for booleans */
66 static struct dentry
*bool_dir
;
68 static char **bool_pending_names
;
69 static int *bool_pending_values
;
71 /* global data for classes */
72 static struct dentry
*class_dir
;
73 static unsigned long last_class_ino
;
75 static char policy_opened
;
77 /* global data for policy capabilities */
78 static struct dentry
*policycap_dir
;
80 /* Check whether a task is allowed to use a security operation. */
81 static int task_has_security(struct task_struct
*tsk
,
84 const struct task_security_struct
*tsec
;
88 tsec
= __task_cred(tsk
)->security
;
95 return avc_has_perm(sid
, SECINITSID_SECURITY
,
96 SECCLASS_SECURITY
, perms
, NULL
);
101 SEL_LOAD
, /* load policy */
102 SEL_ENFORCE
, /* get or set enforcing status */
103 SEL_CONTEXT
, /* validate context */
104 SEL_ACCESS
, /* compute access decision */
105 SEL_CREATE
, /* compute create labeling decision */
106 SEL_RELABEL
, /* compute relabeling decision */
107 SEL_USER
, /* compute reachable user contexts */
108 SEL_POLICYVERS
, /* return policy version for this kernel */
109 SEL_COMMIT_BOOLS
, /* commit new boolean values */
110 SEL_MLS
, /* return if MLS policy is enabled */
111 SEL_DISABLE
, /* disable SELinux until next reboot */
112 SEL_MEMBER
, /* compute polyinstantiation membership decision */
113 SEL_CHECKREQPROT
, /* check requested protection, not kernel-applied one */
114 SEL_COMPAT_NET
, /* whether to use old compat network packet controls */
115 SEL_REJECT_UNKNOWN
, /* export unknown reject handling to userspace */
116 SEL_DENY_UNKNOWN
, /* export unknown deny handling to userspace */
117 SEL_STATUS
, /* export current status using mmap() */
118 SEL_POLICY
, /* allow userspace to read the in kernel policy */
119 SEL_INO_NEXT
, /* The next inode number to use */
122 static unsigned long sel_last_ino
= SEL_INO_NEXT
- 1;
124 #define SEL_INITCON_INO_OFFSET 0x01000000
125 #define SEL_BOOL_INO_OFFSET 0x02000000
126 #define SEL_CLASS_INO_OFFSET 0x04000000
127 #define SEL_POLICYCAP_INO_OFFSET 0x08000000
128 #define SEL_INO_MASK 0x00ffffff
131 static ssize_t
sel_read_enforce(struct file
*filp
, char __user
*buf
,
132 size_t count
, loff_t
*ppos
)
134 char tmpbuf
[TMPBUFLEN
];
137 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_enforcing
);
138 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
141 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
142 static ssize_t
sel_write_enforce(struct file
*file
, const char __user
*buf
,
143 size_t count
, loff_t
*ppos
)
150 if (count
>= PAGE_SIZE
)
153 /* No partial writes. */
157 page
= memdup_user_nul(buf
, count
);
159 return PTR_ERR(page
);
162 if (sscanf(page
, "%d", &new_value
) != 1)
165 if (new_value
!= selinux_enforcing
) {
166 length
= task_has_security(current
, SECURITY__SETENFORCE
);
169 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_STATUS
,
170 "enforcing=%d old_enforcing=%d auid=%u ses=%u",
171 new_value
, selinux_enforcing
,
172 from_kuid(&init_user_ns
, audit_get_loginuid(current
)),
173 audit_get_sessionid(current
));
174 selinux_enforcing
= new_value
;
175 if (selinux_enforcing
)
177 selnl_notify_setenforce(selinux_enforcing
);
178 selinux_status_update_setenforce(selinux_enforcing
);
186 #define sel_write_enforce NULL
189 static const struct file_operations sel_enforce_ops
= {
190 .read
= sel_read_enforce
,
191 .write
= sel_write_enforce
,
192 .llseek
= generic_file_llseek
,
195 static ssize_t
sel_read_handle_unknown(struct file
*filp
, char __user
*buf
,
196 size_t count
, loff_t
*ppos
)
198 char tmpbuf
[TMPBUFLEN
];
200 ino_t ino
= file_inode(filp
)->i_ino
;
201 int handle_unknown
= (ino
== SEL_REJECT_UNKNOWN
) ?
202 security_get_reject_unknown() : !security_get_allow_unknown();
204 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", handle_unknown
);
205 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
208 static const struct file_operations sel_handle_unknown_ops
= {
209 .read
= sel_read_handle_unknown
,
210 .llseek
= generic_file_llseek
,
213 static int sel_open_handle_status(struct inode
*inode
, struct file
*filp
)
215 struct page
*status
= selinux_kernel_status_page();
220 filp
->private_data
= status
;
225 static ssize_t
sel_read_handle_status(struct file
*filp
, char __user
*buf
,
226 size_t count
, loff_t
*ppos
)
228 struct page
*status
= filp
->private_data
;
232 return simple_read_from_buffer(buf
, count
, ppos
,
233 page_address(status
),
234 sizeof(struct selinux_kernel_status
));
237 static int sel_mmap_handle_status(struct file
*filp
,
238 struct vm_area_struct
*vma
)
240 struct page
*status
= filp
->private_data
;
241 unsigned long size
= vma
->vm_end
- vma
->vm_start
;
245 /* only allows one page from the head */
246 if (vma
->vm_pgoff
> 0 || size
!= PAGE_SIZE
)
248 /* disallow writable mapping */
249 if (vma
->vm_flags
& VM_WRITE
)
251 /* disallow mprotect() turns it into writable */
252 vma
->vm_flags
&= ~VM_MAYWRITE
;
254 return remap_pfn_range(vma
, vma
->vm_start
,
256 size
, vma
->vm_page_prot
);
259 static const struct file_operations sel_handle_status_ops
= {
260 .open
= sel_open_handle_status
,
261 .read
= sel_read_handle_status
,
262 .mmap
= sel_mmap_handle_status
,
263 .llseek
= generic_file_llseek
,
266 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
267 static ssize_t
sel_write_disable(struct file
*file
, const char __user
*buf
,
268 size_t count
, loff_t
*ppos
)
275 if (count
>= PAGE_SIZE
)
278 /* No partial writes. */
282 page
= memdup_user_nul(buf
, count
);
284 return PTR_ERR(page
);
287 if (sscanf(page
, "%d", &new_value
) != 1)
291 length
= selinux_disable();
294 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_STATUS
,
295 "selinux=0 auid=%u ses=%u",
296 from_kuid(&init_user_ns
, audit_get_loginuid(current
)),
297 audit_get_sessionid(current
));
306 #define sel_write_disable NULL
309 static const struct file_operations sel_disable_ops
= {
310 .write
= sel_write_disable
,
311 .llseek
= generic_file_llseek
,
314 static ssize_t
sel_read_policyvers(struct file
*filp
, char __user
*buf
,
315 size_t count
, loff_t
*ppos
)
317 char tmpbuf
[TMPBUFLEN
];
320 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", POLICYDB_VERSION_MAX
);
321 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
324 static const struct file_operations sel_policyvers_ops
= {
325 .read
= sel_read_policyvers
,
326 .llseek
= generic_file_llseek
,
329 /* declaration for sel_write_load */
330 static int sel_make_bools(void);
331 static int sel_make_classes(void);
332 static int sel_make_policycap(void);
334 /* declaration for sel_make_class_dirs */
335 static struct dentry
*sel_make_dir(struct dentry
*dir
, const char *name
,
338 static ssize_t
sel_read_mls(struct file
*filp
, char __user
*buf
,
339 size_t count
, loff_t
*ppos
)
341 char tmpbuf
[TMPBUFLEN
];
344 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d",
345 security_mls_enabled());
346 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
349 static const struct file_operations sel_mls_ops
= {
350 .read
= sel_read_mls
,
351 .llseek
= generic_file_llseek
,
354 struct policy_load_memory
{
359 static int sel_open_policy(struct inode
*inode
, struct file
*filp
)
361 struct policy_load_memory
*plm
= NULL
;
364 BUG_ON(filp
->private_data
);
366 mutex_lock(&sel_mutex
);
368 rc
= task_has_security(current
, SECURITY__READ_POLICY
);
377 plm
= kzalloc(sizeof(*plm
), GFP_KERNEL
);
381 if (i_size_read(inode
) != security_policydb_len()) {
382 mutex_lock(&inode
->i_mutex
);
383 i_size_write(inode
, security_policydb_len());
384 mutex_unlock(&inode
->i_mutex
);
387 rc
= security_read_policy(&plm
->data
, &plm
->len
);
393 filp
->private_data
= plm
;
395 mutex_unlock(&sel_mutex
);
399 mutex_unlock(&sel_mutex
);
407 static int sel_release_policy(struct inode
*inode
, struct file
*filp
)
409 struct policy_load_memory
*plm
= filp
->private_data
;
421 static ssize_t
sel_read_policy(struct file
*filp
, char __user
*buf
,
422 size_t count
, loff_t
*ppos
)
424 struct policy_load_memory
*plm
= filp
->private_data
;
427 mutex_lock(&sel_mutex
);
429 ret
= task_has_security(current
, SECURITY__READ_POLICY
);
433 ret
= simple_read_from_buffer(buf
, count
, ppos
, plm
->data
, plm
->len
);
435 mutex_unlock(&sel_mutex
);
439 static int sel_mmap_policy_fault(struct vm_area_struct
*vma
,
440 struct vm_fault
*vmf
)
442 struct policy_load_memory
*plm
= vma
->vm_file
->private_data
;
443 unsigned long offset
;
446 if (vmf
->flags
& (FAULT_FLAG_MKWRITE
| FAULT_FLAG_WRITE
))
447 return VM_FAULT_SIGBUS
;
449 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
450 if (offset
>= roundup(plm
->len
, PAGE_SIZE
))
451 return VM_FAULT_SIGBUS
;
453 page
= vmalloc_to_page(plm
->data
+ offset
);
461 static const struct vm_operations_struct sel_mmap_policy_ops
= {
462 .fault
= sel_mmap_policy_fault
,
463 .page_mkwrite
= sel_mmap_policy_fault
,
466 static int sel_mmap_policy(struct file
*filp
, struct vm_area_struct
*vma
)
468 if (vma
->vm_flags
& VM_SHARED
) {
469 /* do not allow mprotect to make mapping writable */
470 vma
->vm_flags
&= ~VM_MAYWRITE
;
472 if (vma
->vm_flags
& VM_WRITE
)
476 vma
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
477 vma
->vm_ops
= &sel_mmap_policy_ops
;
482 static const struct file_operations sel_policy_ops
= {
483 .open
= sel_open_policy
,
484 .read
= sel_read_policy
,
485 .mmap
= sel_mmap_policy
,
486 .release
= sel_release_policy
,
487 .llseek
= generic_file_llseek
,
490 static ssize_t
sel_write_load(struct file
*file
, const char __user
*buf
,
491 size_t count
, loff_t
*ppos
)
497 mutex_lock(&sel_mutex
);
499 length
= task_has_security(current
, SECURITY__LOAD_POLICY
);
503 /* No partial writes. */
509 if (count
> 64 * 1024 * 1024)
513 data
= vmalloc(count
);
518 if (copy_from_user(data
, buf
, count
) != 0)
521 length
= security_load_policy(data
, count
);
525 length
= sel_make_bools();
529 length
= sel_make_classes();
533 length
= sel_make_policycap();
540 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_POLICY_LOAD
,
541 "policy loaded auid=%u ses=%u",
542 from_kuid(&init_user_ns
, audit_get_loginuid(current
)),
543 audit_get_sessionid(current
));
545 mutex_unlock(&sel_mutex
);
550 static const struct file_operations sel_load_ops
= {
551 .write
= sel_write_load
,
552 .llseek
= generic_file_llseek
,
555 static ssize_t
sel_write_context(struct file
*file
, char *buf
, size_t size
)
561 length
= task_has_security(current
, SECURITY__CHECK_CONTEXT
);
565 length
= security_context_to_sid(buf
, size
, &sid
, GFP_KERNEL
);
569 length
= security_sid_to_context(sid
, &canon
, &len
);
574 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
575 printk(KERN_ERR
"SELinux: %s: context size (%u) exceeds "
576 "payload max\n", __func__
, len
);
580 memcpy(buf
, canon
, len
);
587 static ssize_t
sel_read_checkreqprot(struct file
*filp
, char __user
*buf
,
588 size_t count
, loff_t
*ppos
)
590 char tmpbuf
[TMPBUFLEN
];
593 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", selinux_checkreqprot
);
594 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
597 static ssize_t
sel_write_checkreqprot(struct file
*file
, const char __user
*buf
,
598 size_t count
, loff_t
*ppos
)
602 unsigned int new_value
;
604 length
= task_has_security(current
, SECURITY__SETCHECKREQPROT
);
608 if (count
>= PAGE_SIZE
)
611 /* No partial writes. */
615 page
= memdup_user_nul(buf
, count
);
617 return PTR_ERR(page
);
620 if (sscanf(page
, "%u", &new_value
) != 1)
623 selinux_checkreqprot
= new_value
? 1 : 0;
629 static const struct file_operations sel_checkreqprot_ops
= {
630 .read
= sel_read_checkreqprot
,
631 .write
= sel_write_checkreqprot
,
632 .llseek
= generic_file_llseek
,
636 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
638 static ssize_t
sel_write_access(struct file
*file
, char *buf
, size_t size
);
639 static ssize_t
sel_write_create(struct file
*file
, char *buf
, size_t size
);
640 static ssize_t
sel_write_relabel(struct file
*file
, char *buf
, size_t size
);
641 static ssize_t
sel_write_user(struct file
*file
, char *buf
, size_t size
);
642 static ssize_t
sel_write_member(struct file
*file
, char *buf
, size_t size
);
644 static ssize_t (*write_op
[])(struct file
*, char *, size_t) = {
645 [SEL_ACCESS
] = sel_write_access
,
646 [SEL_CREATE
] = sel_write_create
,
647 [SEL_RELABEL
] = sel_write_relabel
,
648 [SEL_USER
] = sel_write_user
,
649 [SEL_MEMBER
] = sel_write_member
,
650 [SEL_CONTEXT
] = sel_write_context
,
653 static ssize_t
selinux_transaction_write(struct file
*file
, const char __user
*buf
, size_t size
, loff_t
*pos
)
655 ino_t ino
= file_inode(file
)->i_ino
;
659 if (ino
>= ARRAY_SIZE(write_op
) || !write_op
[ino
])
662 data
= simple_transaction_get(file
, buf
, size
);
664 return PTR_ERR(data
);
666 rv
= write_op
[ino
](file
, data
, size
);
668 simple_transaction_set(file
, rv
);
674 static const struct file_operations transaction_ops
= {
675 .write
= selinux_transaction_write
,
676 .read
= simple_transaction_read
,
677 .release
= simple_transaction_release
,
678 .llseek
= generic_file_llseek
,
682 * payload - write methods
683 * If the method has a response, the response should be put in buf,
684 * and the length returned. Otherwise return 0 or and -error.
687 static ssize_t
sel_write_access(struct file
*file
, char *buf
, size_t size
)
689 char *scon
= NULL
, *tcon
= NULL
;
692 struct av_decision avd
;
695 length
= task_has_security(current
, SECURITY__COMPUTE_AV
);
700 scon
= kzalloc(size
+ 1, GFP_KERNEL
);
705 tcon
= kzalloc(size
+ 1, GFP_KERNEL
);
710 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
713 length
= security_context_str_to_sid(scon
, &ssid
, GFP_KERNEL
);
717 length
= security_context_str_to_sid(tcon
, &tsid
, GFP_KERNEL
);
721 security_compute_av_user(ssid
, tsid
, tclass
, &avd
);
723 length
= scnprintf(buf
, SIMPLE_TRANSACTION_LIMIT
,
725 avd
.allowed
, 0xffffffff,
726 avd
.auditallow
, avd
.auditdeny
,
727 avd
.seqno
, avd
.flags
);
734 static ssize_t
sel_write_create(struct file
*file
, char *buf
, size_t size
)
736 char *scon
= NULL
, *tcon
= NULL
;
737 char *namebuf
= NULL
, *objname
= NULL
;
738 u32 ssid
, tsid
, newsid
;
745 length
= task_has_security(current
, SECURITY__COMPUTE_CREATE
);
750 scon
= kzalloc(size
+ 1, GFP_KERNEL
);
755 tcon
= kzalloc(size
+ 1, GFP_KERNEL
);
760 namebuf
= kzalloc(size
+ 1, GFP_KERNEL
);
765 nargs
= sscanf(buf
, "%s %s %hu %s", scon
, tcon
, &tclass
, namebuf
);
766 if (nargs
< 3 || nargs
> 4)
770 * If and when the name of new object to be queried contains
771 * either whitespace or multibyte characters, they shall be
772 * encoded based on the percentage-encoding rule.
773 * If not encoded, the sscanf logic picks up only left-half
774 * of the supplied name; splitted by a whitespace unexpectedly.
784 else if (c1
== '%') {
785 c1
= hex_to_bin(*r
++);
788 c2
= hex_to_bin(*r
++);
794 } while (c1
!= '\0');
799 length
= security_context_str_to_sid(scon
, &ssid
, GFP_KERNEL
);
803 length
= security_context_str_to_sid(tcon
, &tsid
, GFP_KERNEL
);
807 length
= security_transition_sid_user(ssid
, tsid
, tclass
,
812 length
= security_sid_to_context(newsid
, &newcon
, &len
);
817 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
818 printk(KERN_ERR
"SELinux: %s: context size (%u) exceeds "
819 "payload max\n", __func__
, len
);
823 memcpy(buf
, newcon
, len
);
833 static ssize_t
sel_write_relabel(struct file
*file
, char *buf
, size_t size
)
835 char *scon
= NULL
, *tcon
= NULL
;
836 u32 ssid
, tsid
, newsid
;
842 length
= task_has_security(current
, SECURITY__COMPUTE_RELABEL
);
847 scon
= kzalloc(size
+ 1, GFP_KERNEL
);
852 tcon
= kzalloc(size
+ 1, GFP_KERNEL
);
857 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
860 length
= security_context_str_to_sid(scon
, &ssid
, GFP_KERNEL
);
864 length
= security_context_str_to_sid(tcon
, &tsid
, GFP_KERNEL
);
868 length
= security_change_sid(ssid
, tsid
, tclass
, &newsid
);
872 length
= security_sid_to_context(newsid
, &newcon
, &len
);
877 if (len
> SIMPLE_TRANSACTION_LIMIT
)
880 memcpy(buf
, newcon
, len
);
889 static ssize_t
sel_write_user(struct file
*file
, char *buf
, size_t size
)
891 char *con
= NULL
, *user
= NULL
, *ptr
;
892 u32 sid
, *sids
= NULL
;
898 length
= task_has_security(current
, SECURITY__COMPUTE_USER
);
903 con
= kzalloc(size
+ 1, GFP_KERNEL
);
908 user
= kzalloc(size
+ 1, GFP_KERNEL
);
913 if (sscanf(buf
, "%s %s", con
, user
) != 2)
916 length
= security_context_str_to_sid(con
, &sid
, GFP_KERNEL
);
920 length
= security_get_user_sids(sid
, user
, &sids
, &nsids
);
924 length
= sprintf(buf
, "%u", nsids
) + 1;
926 for (i
= 0; i
< nsids
; i
++) {
927 rc
= security_sid_to_context(sids
[i
], &newcon
, &len
);
932 if ((length
+ len
) >= SIMPLE_TRANSACTION_LIMIT
) {
937 memcpy(ptr
, newcon
, len
);
949 static ssize_t
sel_write_member(struct file
*file
, char *buf
, size_t size
)
951 char *scon
= NULL
, *tcon
= NULL
;
952 u32 ssid
, tsid
, newsid
;
958 length
= task_has_security(current
, SECURITY__COMPUTE_MEMBER
);
963 scon
= kzalloc(size
+ 1, GFP_KERNEL
);
968 tcon
= kzalloc(size
+ 1, GFP_KERNEL
);
973 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
976 length
= security_context_str_to_sid(scon
, &ssid
, GFP_KERNEL
);
980 length
= security_context_str_to_sid(tcon
, &tsid
, GFP_KERNEL
);
984 length
= security_member_sid(ssid
, tsid
, tclass
, &newsid
);
988 length
= security_sid_to_context(newsid
, &newcon
, &len
);
993 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
994 printk(KERN_ERR
"SELinux: %s: context size (%u) exceeds "
995 "payload max\n", __func__
, len
);
999 memcpy(buf
, newcon
, len
);
1008 static struct inode
*sel_make_inode(struct super_block
*sb
, int mode
)
1010 struct inode
*ret
= new_inode(sb
);
1014 ret
->i_atime
= ret
->i_mtime
= ret
->i_ctime
= CURRENT_TIME
;
1019 static ssize_t
sel_read_bool(struct file
*filep
, char __user
*buf
,
1020 size_t count
, loff_t
*ppos
)
1026 unsigned index
= file_inode(filep
)->i_ino
& SEL_INO_MASK
;
1027 const char *name
= filep
->f_path
.dentry
->d_name
.name
;
1029 mutex_lock(&sel_mutex
);
1032 if (index
>= bool_num
|| strcmp(name
, bool_pending_names
[index
]))
1036 page
= (char *)get_zeroed_page(GFP_KERNEL
);
1040 cur_enforcing
= security_get_bool_value(index
);
1041 if (cur_enforcing
< 0) {
1042 ret
= cur_enforcing
;
1045 length
= scnprintf(page
, PAGE_SIZE
, "%d %d", cur_enforcing
,
1046 bool_pending_values
[index
]);
1047 ret
= simple_read_from_buffer(buf
, count
, ppos
, page
, length
);
1049 mutex_unlock(&sel_mutex
);
1050 free_page((unsigned long)page
);
1054 static ssize_t
sel_write_bool(struct file
*filep
, const char __user
*buf
,
1055 size_t count
, loff_t
*ppos
)
1060 unsigned index
= file_inode(filep
)->i_ino
& SEL_INO_MASK
;
1061 const char *name
= filep
->f_path
.dentry
->d_name
.name
;
1063 mutex_lock(&sel_mutex
);
1065 length
= task_has_security(current
, SECURITY__SETBOOL
);
1070 if (index
>= bool_num
|| strcmp(name
, bool_pending_names
[index
]))
1074 if (count
>= PAGE_SIZE
)
1077 /* No partial writes. */
1082 page
= memdup_user_nul(buf
, count
);
1084 length
= PTR_ERR(page
);
1090 if (sscanf(page
, "%d", &new_value
) != 1)
1096 bool_pending_values
[index
] = new_value
;
1100 mutex_unlock(&sel_mutex
);
1105 static const struct file_operations sel_bool_ops
= {
1106 .read
= sel_read_bool
,
1107 .write
= sel_write_bool
,
1108 .llseek
= generic_file_llseek
,
1111 static ssize_t
sel_commit_bools_write(struct file
*filep
,
1112 const char __user
*buf
,
1113 size_t count
, loff_t
*ppos
)
1119 mutex_lock(&sel_mutex
);
1121 length
= task_has_security(current
, SECURITY__SETBOOL
);
1126 if (count
>= PAGE_SIZE
)
1129 /* No partial writes. */
1134 page
= memdup_user_nul(buf
, count
);
1136 length
= PTR_ERR(page
);
1142 if (sscanf(page
, "%d", &new_value
) != 1)
1146 if (new_value
&& bool_pending_values
)
1147 length
= security_set_bools(bool_num
, bool_pending_values
);
1153 mutex_unlock(&sel_mutex
);
1158 static const struct file_operations sel_commit_bools_ops
= {
1159 .write
= sel_commit_bools_write
,
1160 .llseek
= generic_file_llseek
,
1163 static void sel_remove_entries(struct dentry
*de
)
1166 shrink_dcache_parent(de
);
1169 #define BOOL_DIR_NAME "booleans"
1171 static int sel_make_bools(void)
1175 struct dentry
*dentry
= NULL
;
1176 struct dentry
*dir
= bool_dir
;
1177 struct inode
*inode
= NULL
;
1178 struct inode_security_struct
*isec
;
1179 char **names
= NULL
, *page
;
1184 /* remove any existing files */
1185 for (i
= 0; i
< bool_num
; i
++)
1186 kfree(bool_pending_names
[i
]);
1187 kfree(bool_pending_names
);
1188 kfree(bool_pending_values
);
1190 bool_pending_names
= NULL
;
1191 bool_pending_values
= NULL
;
1193 sel_remove_entries(dir
);
1196 page
= (char *)get_zeroed_page(GFP_KERNEL
);
1200 ret
= security_get_bools(&num
, &names
, &values
);
1204 for (i
= 0; i
< num
; i
++) {
1206 dentry
= d_alloc_name(dir
, names
[i
]);
1211 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
| S_IRUGO
| S_IWUSR
);
1215 ret
= -ENAMETOOLONG
;
1216 len
= snprintf(page
, PAGE_SIZE
, "/%s/%s", BOOL_DIR_NAME
, names
[i
]);
1217 if (len
>= PAGE_SIZE
)
1220 isec
= (struct inode_security_struct
*)inode
->i_security
;
1221 ret
= security_genfs_sid("selinuxfs", page
, SECCLASS_FILE
, &sid
);
1226 isec
->initialized
= 1;
1227 inode
->i_fop
= &sel_bool_ops
;
1228 inode
->i_ino
= i
|SEL_BOOL_INO_OFFSET
;
1229 d_add(dentry
, inode
);
1232 bool_pending_names
= names
;
1233 bool_pending_values
= values
;
1235 free_page((unsigned long)page
);
1238 free_page((unsigned long)page
);
1241 for (i
= 0; i
< num
; i
++)
1246 sel_remove_entries(dir
);
1251 #define NULL_FILE_NAME "null"
1253 struct path selinux_null
;
1255 static ssize_t
sel_read_avc_cache_threshold(struct file
*filp
, char __user
*buf
,
1256 size_t count
, loff_t
*ppos
)
1258 char tmpbuf
[TMPBUFLEN
];
1261 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", avc_cache_threshold
);
1262 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1265 static ssize_t
sel_write_avc_cache_threshold(struct file
*file
,
1266 const char __user
*buf
,
1267 size_t count
, loff_t
*ppos
)
1274 ret
= task_has_security(current
, SECURITY__SETSECPARAM
);
1278 if (count
>= PAGE_SIZE
)
1281 /* No partial writes. */
1285 page
= memdup_user_nul(buf
, count
);
1287 return PTR_ERR(page
);
1290 if (sscanf(page
, "%u", &new_value
) != 1)
1293 avc_cache_threshold
= new_value
;
1301 static ssize_t
sel_read_avc_hash_stats(struct file
*filp
, char __user
*buf
,
1302 size_t count
, loff_t
*ppos
)
1307 page
= (char *)__get_free_page(GFP_KERNEL
);
1311 length
= avc_get_hash_stats(page
);
1313 length
= simple_read_from_buffer(buf
, count
, ppos
, page
, length
);
1314 free_page((unsigned long)page
);
1319 static const struct file_operations sel_avc_cache_threshold_ops
= {
1320 .read
= sel_read_avc_cache_threshold
,
1321 .write
= sel_write_avc_cache_threshold
,
1322 .llseek
= generic_file_llseek
,
1325 static const struct file_operations sel_avc_hash_stats_ops
= {
1326 .read
= sel_read_avc_hash_stats
,
1327 .llseek
= generic_file_llseek
,
1330 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1331 static struct avc_cache_stats
*sel_avc_get_stat_idx(loff_t
*idx
)
1335 for (cpu
= *idx
; cpu
< nr_cpu_ids
; ++cpu
) {
1336 if (!cpu_possible(cpu
))
1339 return &per_cpu(avc_cache_stats
, cpu
);
1344 static void *sel_avc_stats_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1346 loff_t n
= *pos
- 1;
1349 return SEQ_START_TOKEN
;
1351 return sel_avc_get_stat_idx(&n
);
1354 static void *sel_avc_stats_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1356 return sel_avc_get_stat_idx(pos
);
1359 static int sel_avc_stats_seq_show(struct seq_file
*seq
, void *v
)
1361 struct avc_cache_stats
*st
= v
;
1363 if (v
== SEQ_START_TOKEN
)
1364 seq_printf(seq
, "lookups hits misses allocations reclaims "
1367 unsigned int lookups
= st
->lookups
;
1368 unsigned int misses
= st
->misses
;
1369 unsigned int hits
= lookups
- misses
;
1370 seq_printf(seq
, "%u %u %u %u %u %u\n", lookups
,
1371 hits
, misses
, st
->allocations
,
1372 st
->reclaims
, st
->frees
);
1377 static void sel_avc_stats_seq_stop(struct seq_file
*seq
, void *v
)
1380 static const struct seq_operations sel_avc_cache_stats_seq_ops
= {
1381 .start
= sel_avc_stats_seq_start
,
1382 .next
= sel_avc_stats_seq_next
,
1383 .show
= sel_avc_stats_seq_show
,
1384 .stop
= sel_avc_stats_seq_stop
,
1387 static int sel_open_avc_cache_stats(struct inode
*inode
, struct file
*file
)
1389 return seq_open(file
, &sel_avc_cache_stats_seq_ops
);
1392 static const struct file_operations sel_avc_cache_stats_ops
= {
1393 .open
= sel_open_avc_cache_stats
,
1395 .llseek
= seq_lseek
,
1396 .release
= seq_release
,
1400 static int sel_make_avc_files(struct dentry
*dir
)
1403 static struct tree_descr files
[] = {
1404 { "cache_threshold",
1405 &sel_avc_cache_threshold_ops
, S_IRUGO
|S_IWUSR
},
1406 { "hash_stats", &sel_avc_hash_stats_ops
, S_IRUGO
},
1407 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1408 { "cache_stats", &sel_avc_cache_stats_ops
, S_IRUGO
},
1412 for (i
= 0; i
< ARRAY_SIZE(files
); i
++) {
1413 struct inode
*inode
;
1414 struct dentry
*dentry
;
1416 dentry
= d_alloc_name(dir
, files
[i
].name
);
1420 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|files
[i
].mode
);
1424 inode
->i_fop
= files
[i
].ops
;
1425 inode
->i_ino
= ++sel_last_ino
;
1426 d_add(dentry
, inode
);
1432 static ssize_t
sel_read_initcon(struct file
*file
, char __user
*buf
,
1433 size_t count
, loff_t
*ppos
)
1439 sid
= file_inode(file
)->i_ino
&SEL_INO_MASK
;
1440 ret
= security_sid_to_context(sid
, &con
, &len
);
1444 ret
= simple_read_from_buffer(buf
, count
, ppos
, con
, len
);
1449 static const struct file_operations sel_initcon_ops
= {
1450 .read
= sel_read_initcon
,
1451 .llseek
= generic_file_llseek
,
1454 static int sel_make_initcon_files(struct dentry
*dir
)
1458 for (i
= 1; i
<= SECINITSID_NUM
; i
++) {
1459 struct inode
*inode
;
1460 struct dentry
*dentry
;
1461 dentry
= d_alloc_name(dir
, security_get_initial_sid_context(i
));
1465 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1469 inode
->i_fop
= &sel_initcon_ops
;
1470 inode
->i_ino
= i
|SEL_INITCON_INO_OFFSET
;
1471 d_add(dentry
, inode
);
1477 static inline unsigned long sel_class_to_ino(u16
class)
1479 return (class * (SEL_VEC_MAX
+ 1)) | SEL_CLASS_INO_OFFSET
;
1482 static inline u16
sel_ino_to_class(unsigned long ino
)
1484 return (ino
& SEL_INO_MASK
) / (SEL_VEC_MAX
+ 1);
1487 static inline unsigned long sel_perm_to_ino(u16
class, u32 perm
)
1489 return (class * (SEL_VEC_MAX
+ 1) + perm
) | SEL_CLASS_INO_OFFSET
;
1492 static inline u32
sel_ino_to_perm(unsigned long ino
)
1494 return (ino
& SEL_INO_MASK
) % (SEL_VEC_MAX
+ 1);
1497 static ssize_t
sel_read_class(struct file
*file
, char __user
*buf
,
1498 size_t count
, loff_t
*ppos
)
1500 unsigned long ino
= file_inode(file
)->i_ino
;
1501 char res
[TMPBUFLEN
];
1502 ssize_t len
= snprintf(res
, sizeof(res
), "%d", sel_ino_to_class(ino
));
1503 return simple_read_from_buffer(buf
, count
, ppos
, res
, len
);
1506 static const struct file_operations sel_class_ops
= {
1507 .read
= sel_read_class
,
1508 .llseek
= generic_file_llseek
,
1511 static ssize_t
sel_read_perm(struct file
*file
, char __user
*buf
,
1512 size_t count
, loff_t
*ppos
)
1514 unsigned long ino
= file_inode(file
)->i_ino
;
1515 char res
[TMPBUFLEN
];
1516 ssize_t len
= snprintf(res
, sizeof(res
), "%d", sel_ino_to_perm(ino
));
1517 return simple_read_from_buffer(buf
, count
, ppos
, res
, len
);
1520 static const struct file_operations sel_perm_ops
= {
1521 .read
= sel_read_perm
,
1522 .llseek
= generic_file_llseek
,
1525 static ssize_t
sel_read_policycap(struct file
*file
, char __user
*buf
,
1526 size_t count
, loff_t
*ppos
)
1529 char tmpbuf
[TMPBUFLEN
];
1531 unsigned long i_ino
= file_inode(file
)->i_ino
;
1533 value
= security_policycap_supported(i_ino
& SEL_INO_MASK
);
1534 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", value
);
1536 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1539 static const struct file_operations sel_policycap_ops
= {
1540 .read
= sel_read_policycap
,
1541 .llseek
= generic_file_llseek
,
1544 static int sel_make_perm_files(char *objclass
, int classvalue
,
1550 rc
= security_get_permissions(objclass
, &perms
, &nperms
);
1554 for (i
= 0; i
< nperms
; i
++) {
1555 struct inode
*inode
;
1556 struct dentry
*dentry
;
1559 dentry
= d_alloc_name(dir
, perms
[i
]);
1564 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1568 inode
->i_fop
= &sel_perm_ops
;
1569 /* i+1 since perm values are 1-indexed */
1570 inode
->i_ino
= sel_perm_to_ino(classvalue
, i
+ 1);
1571 d_add(dentry
, inode
);
1575 for (i
= 0; i
< nperms
; i
++)
1581 static int sel_make_class_dir_entries(char *classname
, int index
,
1584 struct dentry
*dentry
= NULL
;
1585 struct inode
*inode
= NULL
;
1588 dentry
= d_alloc_name(dir
, "index");
1592 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1596 inode
->i_fop
= &sel_class_ops
;
1597 inode
->i_ino
= sel_class_to_ino(index
);
1598 d_add(dentry
, inode
);
1600 dentry
= sel_make_dir(dir
, "perms", &last_class_ino
);
1602 return PTR_ERR(dentry
);
1604 rc
= sel_make_perm_files(classname
, index
, dentry
);
1609 static int sel_make_classes(void)
1611 int rc
, nclasses
, i
;
1614 /* delete any existing entries */
1615 sel_remove_entries(class_dir
);
1617 rc
= security_get_classes(&classes
, &nclasses
);
1621 /* +2 since classes are 1-indexed */
1622 last_class_ino
= sel_class_to_ino(nclasses
+ 2);
1624 for (i
= 0; i
< nclasses
; i
++) {
1625 struct dentry
*class_name_dir
;
1627 class_name_dir
= sel_make_dir(class_dir
, classes
[i
],
1629 if (IS_ERR(class_name_dir
)) {
1630 rc
= PTR_ERR(class_name_dir
);
1634 /* i+1 since class values are 1-indexed */
1635 rc
= sel_make_class_dir_entries(classes
[i
], i
+ 1,
1642 for (i
= 0; i
< nclasses
; i
++)
1648 static int sel_make_policycap(void)
1651 struct dentry
*dentry
= NULL
;
1652 struct inode
*inode
= NULL
;
1654 sel_remove_entries(policycap_dir
);
1656 for (iter
= 0; iter
<= POLICYDB_CAPABILITY_MAX
; iter
++) {
1657 if (iter
< ARRAY_SIZE(policycap_names
))
1658 dentry
= d_alloc_name(policycap_dir
,
1659 policycap_names
[iter
]);
1661 dentry
= d_alloc_name(policycap_dir
, "unknown");
1666 inode
= sel_make_inode(policycap_dir
->d_sb
, S_IFREG
| S_IRUGO
);
1670 inode
->i_fop
= &sel_policycap_ops
;
1671 inode
->i_ino
= iter
| SEL_POLICYCAP_INO_OFFSET
;
1672 d_add(dentry
, inode
);
1678 static struct dentry
*sel_make_dir(struct dentry
*dir
, const char *name
,
1681 struct dentry
*dentry
= d_alloc_name(dir
, name
);
1682 struct inode
*inode
;
1685 return ERR_PTR(-ENOMEM
);
1687 inode
= sel_make_inode(dir
->d_sb
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
1690 return ERR_PTR(-ENOMEM
);
1693 inode
->i_op
= &simple_dir_inode_operations
;
1694 inode
->i_fop
= &simple_dir_operations
;
1695 inode
->i_ino
= ++(*ino
);
1696 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1698 d_add(dentry
, inode
);
1699 /* bump link count on parent directory, too */
1700 inc_nlink(d_inode(dir
));
1705 static int sel_fill_super(struct super_block
*sb
, void *data
, int silent
)
1708 struct dentry
*dentry
;
1709 struct inode
*inode
;
1710 struct inode_security_struct
*isec
;
1712 static struct tree_descr selinux_files
[] = {
1713 [SEL_LOAD
] = {"load", &sel_load_ops
, S_IRUSR
|S_IWUSR
},
1714 [SEL_ENFORCE
] = {"enforce", &sel_enforce_ops
, S_IRUGO
|S_IWUSR
},
1715 [SEL_CONTEXT
] = {"context", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1716 [SEL_ACCESS
] = {"access", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1717 [SEL_CREATE
] = {"create", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1718 [SEL_RELABEL
] = {"relabel", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1719 [SEL_USER
] = {"user", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1720 [SEL_POLICYVERS
] = {"policyvers", &sel_policyvers_ops
, S_IRUGO
},
1721 [SEL_COMMIT_BOOLS
] = {"commit_pending_bools", &sel_commit_bools_ops
, S_IWUSR
},
1722 [SEL_MLS
] = {"mls", &sel_mls_ops
, S_IRUGO
},
1723 [SEL_DISABLE
] = {"disable", &sel_disable_ops
, S_IWUSR
},
1724 [SEL_MEMBER
] = {"member", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1725 [SEL_CHECKREQPROT
] = {"checkreqprot", &sel_checkreqprot_ops
, S_IRUGO
|S_IWUSR
},
1726 [SEL_REJECT_UNKNOWN
] = {"reject_unknown", &sel_handle_unknown_ops
, S_IRUGO
},
1727 [SEL_DENY_UNKNOWN
] = {"deny_unknown", &sel_handle_unknown_ops
, S_IRUGO
},
1728 [SEL_STATUS
] = {"status", &sel_handle_status_ops
, S_IRUGO
},
1729 [SEL_POLICY
] = {"policy", &sel_policy_ops
, S_IRUGO
},
1732 ret
= simple_fill_super(sb
, SELINUX_MAGIC
, selinux_files
);
1736 bool_dir
= sel_make_dir(sb
->s_root
, BOOL_DIR_NAME
, &sel_last_ino
);
1737 if (IS_ERR(bool_dir
)) {
1738 ret
= PTR_ERR(bool_dir
);
1744 dentry
= d_alloc_name(sb
->s_root
, NULL_FILE_NAME
);
1749 inode
= sel_make_inode(sb
, S_IFCHR
| S_IRUGO
| S_IWUGO
);
1753 inode
->i_ino
= ++sel_last_ino
;
1754 isec
= (struct inode_security_struct
*)inode
->i_security
;
1755 isec
->sid
= SECINITSID_DEVNULL
;
1756 isec
->sclass
= SECCLASS_CHR_FILE
;
1757 isec
->initialized
= 1;
1759 init_special_inode(inode
, S_IFCHR
| S_IRUGO
| S_IWUGO
, MKDEV(MEM_MAJOR
, 3));
1760 d_add(dentry
, inode
);
1761 selinux_null
.dentry
= dentry
;
1763 dentry
= sel_make_dir(sb
->s_root
, "avc", &sel_last_ino
);
1764 if (IS_ERR(dentry
)) {
1765 ret
= PTR_ERR(dentry
);
1769 ret
= sel_make_avc_files(dentry
);
1773 dentry
= sel_make_dir(sb
->s_root
, "initial_contexts", &sel_last_ino
);
1774 if (IS_ERR(dentry
)) {
1775 ret
= PTR_ERR(dentry
);
1779 ret
= sel_make_initcon_files(dentry
);
1783 class_dir
= sel_make_dir(sb
->s_root
, "class", &sel_last_ino
);
1784 if (IS_ERR(class_dir
)) {
1785 ret
= PTR_ERR(class_dir
);
1790 policycap_dir
= sel_make_dir(sb
->s_root
, "policy_capabilities", &sel_last_ino
);
1791 if (IS_ERR(policycap_dir
)) {
1792 ret
= PTR_ERR(policycap_dir
);
1793 policycap_dir
= NULL
;
1798 printk(KERN_ERR
"SELinux: %s: failed while creating inodes\n",
1803 static struct dentry
*sel_mount(struct file_system_type
*fs_type
,
1804 int flags
, const char *dev_name
, void *data
)
1806 return mount_single(fs_type
, flags
, data
, sel_fill_super
);
1809 static struct file_system_type sel_fs_type
= {
1810 .name
= "selinuxfs",
1812 .kill_sb
= kill_litter_super
,
1815 struct vfsmount
*selinuxfs_mount
;
1817 static int __init
init_sel_fs(void)
1821 if (!selinux_enabled
)
1824 err
= sysfs_create_mount_point(fs_kobj
, "selinux");
1828 err
= register_filesystem(&sel_fs_type
);
1830 sysfs_remove_mount_point(fs_kobj
, "selinux");
1834 selinux_null
.mnt
= selinuxfs_mount
= kern_mount(&sel_fs_type
);
1835 if (IS_ERR(selinuxfs_mount
)) {
1836 printk(KERN_ERR
"selinuxfs: could not mount!\n");
1837 err
= PTR_ERR(selinuxfs_mount
);
1838 selinuxfs_mount
= NULL
;
1844 __initcall(init_sel_fs
);
1846 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1847 void exit_sel_fs(void)
1849 sysfs_remove_mount_point(fs_kobj
, "selinux");
1850 kern_unmount(selinuxfs_mount
);
1851 unregister_filesystem(&sel_fs_type
);