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 static int __init
checkreqprot_setup(char *str
)
52 unsigned long checkreqprot
;
53 if (!strict_strtoul(str
, 0, &checkreqprot
))
54 selinux_checkreqprot
= checkreqprot
? 1 : 0;
57 __setup("checkreqprot=", checkreqprot_setup
);
59 static DEFINE_MUTEX(sel_mutex
);
61 /* global data for booleans */
62 static struct dentry
*bool_dir
;
64 static char **bool_pending_names
;
65 static int *bool_pending_values
;
67 /* global data for classes */
68 static struct dentry
*class_dir
;
69 static unsigned long last_class_ino
;
71 static char policy_opened
;
73 /* global data for policy capabilities */
74 static struct dentry
*policycap_dir
;
76 extern void selnl_notify_setenforce(int val
);
78 /* Check whether a task is allowed to use a security operation. */
79 static int task_has_security(struct task_struct
*tsk
,
82 const struct task_security_struct
*tsec
;
86 tsec
= __task_cred(tsk
)->security
;
93 return avc_has_perm(sid
, SECINITSID_SECURITY
,
94 SECCLASS_SECURITY
, perms
, NULL
);
99 SEL_LOAD
, /* load policy */
100 SEL_ENFORCE
, /* get or set enforcing status */
101 SEL_CONTEXT
, /* validate context */
102 SEL_ACCESS
, /* compute access decision */
103 SEL_CREATE
, /* compute create labeling decision */
104 SEL_RELABEL
, /* compute relabeling decision */
105 SEL_USER
, /* compute reachable user contexts */
106 SEL_POLICYVERS
, /* return policy version for this kernel */
107 SEL_COMMIT_BOOLS
, /* commit new boolean values */
108 SEL_MLS
, /* return if MLS policy is enabled */
109 SEL_DISABLE
, /* disable SELinux until next reboot */
110 SEL_MEMBER
, /* compute polyinstantiation membership decision */
111 SEL_CHECKREQPROT
, /* check requested protection, not kernel-applied one */
112 SEL_COMPAT_NET
, /* whether to use old compat network packet controls */
113 SEL_REJECT_UNKNOWN
, /* export unknown reject handling to userspace */
114 SEL_DENY_UNKNOWN
, /* export unknown deny handling to userspace */
115 SEL_STATUS
, /* export current status using mmap() */
116 SEL_POLICY
, /* allow userspace to read the in kernel policy */
117 SEL_INO_NEXT
, /* The next inode number to use */
120 static unsigned long sel_last_ino
= SEL_INO_NEXT
- 1;
122 #define SEL_INITCON_INO_OFFSET 0x01000000
123 #define SEL_BOOL_INO_OFFSET 0x02000000
124 #define SEL_CLASS_INO_OFFSET 0x04000000
125 #define SEL_POLICYCAP_INO_OFFSET 0x08000000
126 #define SEL_INO_MASK 0x00ffffff
129 static ssize_t
sel_read_enforce(struct file
*filp
, char __user
*buf
,
130 size_t count
, loff_t
*ppos
)
132 char tmpbuf
[TMPBUFLEN
];
135 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", selinux_enforcing
);
136 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
139 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
140 static ssize_t
sel_write_enforce(struct file
*file
, const char __user
*buf
,
141 size_t count
, loff_t
*ppos
)
148 if (count
>= PAGE_SIZE
)
151 /* No partial writes. */
154 page
= (char *)get_zeroed_page(GFP_KERNEL
);
158 if (copy_from_user(page
, buf
, count
))
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 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
);
182 free_page((unsigned long) page
);
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
= filp
->f_path
.dentry
->d_inode
->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
)
274 extern int selinux_disable(void);
276 if (count
>= PAGE_SIZE
)
279 /* No partial writes. */
282 page
= (char *)get_zeroed_page(GFP_KERNEL
);
286 if (copy_from_user(page
, buf
, count
))
290 if (sscanf(page
, "%d", &new_value
) != 1)
294 length
= selinux_disable();
297 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_STATUS
,
298 "selinux=0 auid=%u ses=%u",
299 audit_get_loginuid(current
),
300 audit_get_sessionid(current
));
305 free_page((unsigned long) page
);
309 #define sel_write_disable NULL
312 static const struct file_operations sel_disable_ops
= {
313 .write
= sel_write_disable
,
314 .llseek
= generic_file_llseek
,
317 static ssize_t
sel_read_policyvers(struct file
*filp
, char __user
*buf
,
318 size_t count
, loff_t
*ppos
)
320 char tmpbuf
[TMPBUFLEN
];
323 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", POLICYDB_VERSION_MAX
);
324 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
327 static const struct file_operations sel_policyvers_ops
= {
328 .read
= sel_read_policyvers
,
329 .llseek
= generic_file_llseek
,
332 /* declaration for sel_write_load */
333 static int sel_make_bools(void);
334 static int sel_make_classes(void);
335 static int sel_make_policycap(void);
337 /* declaration for sel_make_class_dirs */
338 static int sel_make_dir(struct inode
*dir
, struct dentry
*dentry
,
341 static ssize_t
sel_read_mls(struct file
*filp
, char __user
*buf
,
342 size_t count
, loff_t
*ppos
)
344 char tmpbuf
[TMPBUFLEN
];
347 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d",
348 security_mls_enabled());
349 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
352 static const struct file_operations sel_mls_ops
= {
353 .read
= sel_read_mls
,
354 .llseek
= generic_file_llseek
,
357 struct policy_load_memory
{
362 static int sel_open_policy(struct inode
*inode
, struct file
*filp
)
364 struct policy_load_memory
*plm
= NULL
;
367 BUG_ON(filp
->private_data
);
369 mutex_lock(&sel_mutex
);
371 rc
= task_has_security(current
, SECURITY__READ_POLICY
);
380 plm
= kzalloc(sizeof(*plm
), GFP_KERNEL
);
384 if (i_size_read(inode
) != security_policydb_len()) {
385 mutex_lock(&inode
->i_mutex
);
386 i_size_write(inode
, security_policydb_len());
387 mutex_unlock(&inode
->i_mutex
);
390 rc
= security_read_policy(&plm
->data
, &plm
->len
);
396 filp
->private_data
= plm
;
398 mutex_unlock(&sel_mutex
);
402 mutex_unlock(&sel_mutex
);
410 static int sel_release_policy(struct inode
*inode
, struct file
*filp
)
412 struct policy_load_memory
*plm
= filp
->private_data
;
424 static ssize_t
sel_read_policy(struct file
*filp
, char __user
*buf
,
425 size_t count
, loff_t
*ppos
)
427 struct policy_load_memory
*plm
= filp
->private_data
;
430 mutex_lock(&sel_mutex
);
432 ret
= task_has_security(current
, SECURITY__READ_POLICY
);
436 ret
= simple_read_from_buffer(buf
, count
, ppos
, plm
->data
, plm
->len
);
438 mutex_unlock(&sel_mutex
);
442 static int sel_mmap_policy_fault(struct vm_area_struct
*vma
,
443 struct vm_fault
*vmf
)
445 struct policy_load_memory
*plm
= vma
->vm_file
->private_data
;
446 unsigned long offset
;
449 if (vmf
->flags
& (FAULT_FLAG_MKWRITE
| FAULT_FLAG_WRITE
))
450 return VM_FAULT_SIGBUS
;
452 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
453 if (offset
>= roundup(plm
->len
, PAGE_SIZE
))
454 return VM_FAULT_SIGBUS
;
456 page
= vmalloc_to_page(plm
->data
+ offset
);
464 static struct vm_operations_struct sel_mmap_policy_ops
= {
465 .fault
= sel_mmap_policy_fault
,
466 .page_mkwrite
= sel_mmap_policy_fault
,
469 int sel_mmap_policy(struct file
*filp
, struct vm_area_struct
*vma
)
471 if (vma
->vm_flags
& VM_SHARED
) {
472 /* do not allow mprotect to make mapping writable */
473 vma
->vm_flags
&= ~VM_MAYWRITE
;
475 if (vma
->vm_flags
& VM_WRITE
)
479 vma
->vm_flags
|= VM_RESERVED
;
480 vma
->vm_ops
= &sel_mmap_policy_ops
;
485 static const struct file_operations sel_policy_ops
= {
486 .open
= sel_open_policy
,
487 .read
= sel_read_policy
,
488 .mmap
= sel_mmap_policy
,
489 .release
= sel_release_policy
,
492 static ssize_t
sel_write_load(struct file
*file
, const char __user
*buf
,
493 size_t count
, loff_t
*ppos
)
500 mutex_lock(&sel_mutex
);
502 length
= task_has_security(current
, SECURITY__LOAD_POLICY
);
507 /* No partial writes. */
512 if ((count
> 64 * 1024 * 1024)
513 || (data
= vmalloc(count
)) == NULL
) {
519 if (copy_from_user(data
, buf
, count
) != 0)
522 length
= security_load_policy(data
, count
);
526 ret
= sel_make_bools();
532 ret
= sel_make_classes();
538 ret
= sel_make_policycap();
545 audit_log(current
->audit_context
, GFP_KERNEL
, AUDIT_MAC_POLICY_LOAD
,
546 "policy loaded auid=%u ses=%u",
547 audit_get_loginuid(current
),
548 audit_get_sessionid(current
));
550 mutex_unlock(&sel_mutex
);
555 static const struct file_operations sel_load_ops
= {
556 .write
= sel_write_load
,
557 .llseek
= generic_file_llseek
,
560 static ssize_t
sel_write_context(struct file
*file
, char *buf
, size_t size
)
566 length
= task_has_security(current
, SECURITY__CHECK_CONTEXT
);
570 length
= security_context_to_sid(buf
, size
, &sid
);
574 length
= security_sid_to_context(sid
, &canon
, &len
);
578 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
579 printk(KERN_ERR
"SELinux: %s: context size (%u) exceeds "
580 "payload max\n", __func__
, len
);
585 memcpy(buf
, canon
, len
);
592 static ssize_t
sel_read_checkreqprot(struct file
*filp
, char __user
*buf
,
593 size_t count
, loff_t
*ppos
)
595 char tmpbuf
[TMPBUFLEN
];
598 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", selinux_checkreqprot
);
599 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
602 static ssize_t
sel_write_checkreqprot(struct file
*file
, const char __user
*buf
,
603 size_t count
, loff_t
*ppos
)
607 unsigned int new_value
;
609 length
= task_has_security(current
, SECURITY__SETCHECKREQPROT
);
613 if (count
>= PAGE_SIZE
)
616 /* No partial writes. */
619 page
= (char *)get_zeroed_page(GFP_KERNEL
);
623 if (copy_from_user(page
, buf
, count
))
627 if (sscanf(page
, "%u", &new_value
) != 1)
630 selinux_checkreqprot
= new_value
? 1 : 0;
633 free_page((unsigned long) page
);
636 static const struct file_operations sel_checkreqprot_ops
= {
637 .read
= sel_read_checkreqprot
,
638 .write
= sel_write_checkreqprot
,
639 .llseek
= generic_file_llseek
,
643 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
645 static ssize_t
sel_write_access(struct file
*file
, char *buf
, size_t size
);
646 static ssize_t
sel_write_create(struct file
*file
, char *buf
, size_t size
);
647 static ssize_t
sel_write_relabel(struct file
*file
, char *buf
, size_t size
);
648 static ssize_t
sel_write_user(struct file
*file
, char *buf
, size_t size
);
649 static ssize_t
sel_write_member(struct file
*file
, char *buf
, size_t size
);
651 static ssize_t (*write_op
[])(struct file
*, char *, size_t) = {
652 [SEL_ACCESS
] = sel_write_access
,
653 [SEL_CREATE
] = sel_write_create
,
654 [SEL_RELABEL
] = sel_write_relabel
,
655 [SEL_USER
] = sel_write_user
,
656 [SEL_MEMBER
] = sel_write_member
,
657 [SEL_CONTEXT
] = sel_write_context
,
660 static ssize_t
selinux_transaction_write(struct file
*file
, const char __user
*buf
, size_t size
, loff_t
*pos
)
662 ino_t ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
666 if (ino
>= ARRAY_SIZE(write_op
) || !write_op
[ino
])
669 data
= simple_transaction_get(file
, buf
, size
);
671 return PTR_ERR(data
);
673 rv
= write_op
[ino
](file
, data
, size
);
675 simple_transaction_set(file
, rv
);
681 static const struct file_operations transaction_ops
= {
682 .write
= selinux_transaction_write
,
683 .read
= simple_transaction_read
,
684 .release
= simple_transaction_release
,
685 .llseek
= generic_file_llseek
,
689 * payload - write methods
690 * If the method has a response, the response should be put in buf,
691 * and the length returned. Otherwise return 0 or and -error.
694 static ssize_t
sel_write_access(struct file
*file
, char *buf
, size_t size
)
699 struct av_decision avd
;
702 length
= task_has_security(current
, SECURITY__COMPUTE_AV
);
707 scon
= kzalloc(size
+ 1, GFP_KERNEL
);
711 tcon
= kzalloc(size
+ 1, GFP_KERNEL
);
716 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
719 length
= security_context_to_sid(scon
, strlen(scon
) + 1, &ssid
);
722 length
= security_context_to_sid(tcon
, strlen(tcon
) + 1, &tsid
);
726 security_compute_av_user(ssid
, tsid
, tclass
, &avd
);
728 length
= scnprintf(buf
, SIMPLE_TRANSACTION_LIMIT
,
730 avd
.allowed
, 0xffffffff,
731 avd
.auditallow
, avd
.auditdeny
,
732 avd
.seqno
, avd
.flags
);
740 static ssize_t
sel_write_create(struct file
*file
, char *buf
, size_t size
)
743 u32 ssid
, tsid
, newsid
;
749 length
= task_has_security(current
, SECURITY__COMPUTE_CREATE
);
754 scon
= kzalloc(size
+ 1, GFP_KERNEL
);
758 tcon
= kzalloc(size
+ 1, GFP_KERNEL
);
763 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
766 length
= security_context_to_sid(scon
, strlen(scon
) + 1, &ssid
);
769 length
= security_context_to_sid(tcon
, strlen(tcon
) + 1, &tsid
);
773 length
= security_transition_sid_user(ssid
, tsid
, tclass
, &newsid
);
777 length
= security_sid_to_context(newsid
, &newcon
, &len
);
781 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
782 printk(KERN_ERR
"SELinux: %s: context size (%u) exceeds "
783 "payload max\n", __func__
, len
);
788 memcpy(buf
, newcon
, len
);
799 static ssize_t
sel_write_relabel(struct file
*file
, char *buf
, size_t size
)
802 u32 ssid
, tsid
, newsid
;
808 length
= task_has_security(current
, SECURITY__COMPUTE_RELABEL
);
813 scon
= kzalloc(size
+ 1, GFP_KERNEL
);
817 tcon
= kzalloc(size
+ 1, GFP_KERNEL
);
822 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
825 length
= security_context_to_sid(scon
, strlen(scon
) + 1, &ssid
);
828 length
= security_context_to_sid(tcon
, strlen(tcon
) + 1, &tsid
);
832 length
= security_change_sid(ssid
, tsid
, tclass
, &newsid
);
836 length
= security_sid_to_context(newsid
, &newcon
, &len
);
840 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
845 memcpy(buf
, newcon
, len
);
856 static ssize_t
sel_write_user(struct file
*file
, char *buf
, size_t size
)
858 char *con
, *user
, *ptr
;
865 length
= task_has_security(current
, SECURITY__COMPUTE_USER
);
870 con
= kzalloc(size
+ 1, GFP_KERNEL
);
874 user
= kzalloc(size
+ 1, GFP_KERNEL
);
879 if (sscanf(buf
, "%s %s", con
, user
) != 2)
882 length
= security_context_to_sid(con
, strlen(con
) + 1, &sid
);
886 length
= security_get_user_sids(sid
, user
, &sids
, &nsids
);
890 length
= sprintf(buf
, "%u", nsids
) + 1;
892 for (i
= 0; i
< nsids
; i
++) {
893 rc
= security_sid_to_context(sids
[i
], &newcon
, &len
);
898 if ((length
+ len
) >= SIMPLE_TRANSACTION_LIMIT
) {
903 memcpy(ptr
, newcon
, len
);
917 static ssize_t
sel_write_member(struct file
*file
, char *buf
, size_t size
)
920 u32 ssid
, tsid
, newsid
;
926 length
= task_has_security(current
, SECURITY__COMPUTE_MEMBER
);
931 scon
= kzalloc(size
+ 1, GFP_KERNEL
);
935 tcon
= kzalloc(size
+ 1, GFP_KERNEL
);
940 if (sscanf(buf
, "%s %s %hu", scon
, tcon
, &tclass
) != 3)
943 length
= security_context_to_sid(scon
, strlen(scon
) + 1, &ssid
);
946 length
= security_context_to_sid(tcon
, strlen(tcon
) + 1, &tsid
);
950 length
= security_member_sid(ssid
, tsid
, tclass
, &newsid
);
954 length
= security_sid_to_context(newsid
, &newcon
, &len
);
958 if (len
> SIMPLE_TRANSACTION_LIMIT
) {
959 printk(KERN_ERR
"SELinux: %s: context size (%u) exceeds "
960 "payload max\n", __func__
, len
);
965 memcpy(buf
, newcon
, len
);
976 static struct inode
*sel_make_inode(struct super_block
*sb
, int mode
)
978 struct inode
*ret
= new_inode(sb
);
981 ret
->i_ino
= get_next_ino();
983 ret
->i_atime
= ret
->i_mtime
= ret
->i_ctime
= CURRENT_TIME
;
988 static ssize_t
sel_read_bool(struct file
*filep
, char __user
*buf
,
989 size_t count
, loff_t
*ppos
)
995 struct inode
*inode
= filep
->f_path
.dentry
->d_inode
;
996 unsigned index
= inode
->i_ino
& SEL_INO_MASK
;
997 const char *name
= filep
->f_path
.dentry
->d_name
.name
;
999 mutex_lock(&sel_mutex
);
1001 if (index
>= bool_num
|| strcmp(name
, bool_pending_names
[index
])) {
1006 page
= (char *)get_zeroed_page(GFP_KERNEL
);
1012 cur_enforcing
= security_get_bool_value(index
);
1013 if (cur_enforcing
< 0) {
1014 ret
= cur_enforcing
;
1017 length
= scnprintf(page
, PAGE_SIZE
, "%d %d", cur_enforcing
,
1018 bool_pending_values
[index
]);
1019 ret
= simple_read_from_buffer(buf
, count
, ppos
, page
, length
);
1021 mutex_unlock(&sel_mutex
);
1023 free_page((unsigned long)page
);
1027 static ssize_t
sel_write_bool(struct file
*filep
, const char __user
*buf
,
1028 size_t count
, loff_t
*ppos
)
1033 struct inode
*inode
= filep
->f_path
.dentry
->d_inode
;
1034 unsigned index
= inode
->i_ino
& SEL_INO_MASK
;
1035 const char *name
= filep
->f_path
.dentry
->d_name
.name
;
1037 mutex_lock(&sel_mutex
);
1039 length
= task_has_security(current
, SECURITY__SETBOOL
);
1043 if (index
>= bool_num
|| strcmp(name
, bool_pending_names
[index
])) {
1048 if (count
>= PAGE_SIZE
) {
1054 /* No partial writes. */
1058 page
= (char *)get_zeroed_page(GFP_KERNEL
);
1065 if (copy_from_user(page
, buf
, count
))
1069 if (sscanf(page
, "%d", &new_value
) != 1)
1075 bool_pending_values
[index
] = new_value
;
1079 mutex_unlock(&sel_mutex
);
1081 free_page((unsigned long) page
);
1085 static const struct file_operations sel_bool_ops
= {
1086 .read
= sel_read_bool
,
1087 .write
= sel_write_bool
,
1088 .llseek
= generic_file_llseek
,
1091 static ssize_t
sel_commit_bools_write(struct file
*filep
,
1092 const char __user
*buf
,
1093 size_t count
, loff_t
*ppos
)
1099 mutex_lock(&sel_mutex
);
1101 length
= task_has_security(current
, SECURITY__SETBOOL
);
1105 if (count
>= PAGE_SIZE
) {
1110 /* No partial writes. */
1113 page
= (char *)get_zeroed_page(GFP_KERNEL
);
1120 if (copy_from_user(page
, buf
, count
))
1124 if (sscanf(page
, "%d", &new_value
) != 1)
1127 if (new_value
&& bool_pending_values
)
1128 security_set_bools(bool_num
, bool_pending_values
);
1133 mutex_unlock(&sel_mutex
);
1135 free_page((unsigned long) page
);
1139 static const struct file_operations sel_commit_bools_ops
= {
1140 .write
= sel_commit_bools_write
,
1141 .llseek
= generic_file_llseek
,
1144 static void sel_remove_entries(struct dentry
*de
)
1146 struct list_head
*node
;
1148 spin_lock(&dcache_lock
);
1149 spin_lock(&de
->d_lock
);
1150 node
= de
->d_subdirs
.next
;
1151 while (node
!= &de
->d_subdirs
) {
1152 struct dentry
*d
= list_entry(node
, struct dentry
, d_u
.d_child
);
1154 spin_lock_nested(&d
->d_lock
, DENTRY_D_LOCK_NESTED
);
1155 list_del_init(node
);
1158 dget_locked_dlock(d
);
1159 spin_unlock(&de
->d_lock
);
1160 spin_unlock(&d
->d_lock
);
1161 spin_unlock(&dcache_lock
);
1163 simple_unlink(de
->d_inode
, d
);
1165 spin_lock(&dcache_lock
);
1166 spin_lock(&de
->d_lock
);
1168 spin_unlock(&d
->d_lock
);
1169 node
= de
->d_subdirs
.next
;
1172 spin_unlock(&de
->d_lock
);
1173 spin_unlock(&dcache_lock
);
1176 #define BOOL_DIR_NAME "booleans"
1178 static int sel_make_bools(void)
1182 struct dentry
*dentry
= NULL
;
1183 struct dentry
*dir
= bool_dir
;
1184 struct inode
*inode
= NULL
;
1185 struct inode_security_struct
*isec
;
1186 char **names
= NULL
, *page
;
1191 /* remove any existing files */
1192 for (i
= 0; i
< bool_num
; i
++)
1193 kfree(bool_pending_names
[i
]);
1194 kfree(bool_pending_names
);
1195 kfree(bool_pending_values
);
1196 bool_pending_names
= NULL
;
1197 bool_pending_values
= NULL
;
1199 sel_remove_entries(dir
);
1201 page
= (char *)get_zeroed_page(GFP_KERNEL
);
1205 ret
= security_get_bools(&num
, &names
, &values
);
1209 for (i
= 0; i
< num
; i
++) {
1210 dentry
= d_alloc_name(dir
, names
[i
]);
1215 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
| S_IRUGO
| S_IWUSR
);
1221 len
= snprintf(page
, PAGE_SIZE
, "/%s/%s", BOOL_DIR_NAME
, names
[i
]);
1225 } else if (len
>= PAGE_SIZE
) {
1226 ret
= -ENAMETOOLONG
;
1229 isec
= (struct inode_security_struct
*)inode
->i_security
;
1230 ret
= security_genfs_sid("selinuxfs", page
, SECCLASS_FILE
, &sid
);
1234 isec
->initialized
= 1;
1235 inode
->i_fop
= &sel_bool_ops
;
1236 inode
->i_ino
= i
|SEL_BOOL_INO_OFFSET
;
1237 d_add(dentry
, inode
);
1240 bool_pending_names
= names
;
1241 bool_pending_values
= values
;
1243 free_page((unsigned long)page
);
1247 for (i
= 0; i
< num
; i
++)
1252 sel_remove_entries(dir
);
1257 #define NULL_FILE_NAME "null"
1259 struct dentry
*selinux_null
;
1261 static ssize_t
sel_read_avc_cache_threshold(struct file
*filp
, char __user
*buf
,
1262 size_t count
, loff_t
*ppos
)
1264 char tmpbuf
[TMPBUFLEN
];
1267 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u", avc_cache_threshold
);
1268 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1271 static ssize_t
sel_write_avc_cache_threshold(struct file
*file
,
1272 const char __user
*buf
,
1273 size_t count
, loff_t
*ppos
)
1280 if (count
>= PAGE_SIZE
) {
1286 /* No partial writes. */
1291 page
= (char *)get_zeroed_page(GFP_KERNEL
);
1297 if (copy_from_user(page
, buf
, count
)) {
1302 if (sscanf(page
, "%u", &new_value
) != 1) {
1307 if (new_value
!= avc_cache_threshold
) {
1308 ret
= task_has_security(current
, SECURITY__SETSECPARAM
);
1311 avc_cache_threshold
= new_value
;
1315 free_page((unsigned long)page
);
1320 static ssize_t
sel_read_avc_hash_stats(struct file
*filp
, char __user
*buf
,
1321 size_t count
, loff_t
*ppos
)
1326 page
= (char *)__get_free_page(GFP_KERNEL
);
1331 ret
= avc_get_hash_stats(page
);
1333 ret
= simple_read_from_buffer(buf
, count
, ppos
, page
, ret
);
1334 free_page((unsigned long)page
);
1339 static const struct file_operations sel_avc_cache_threshold_ops
= {
1340 .read
= sel_read_avc_cache_threshold
,
1341 .write
= sel_write_avc_cache_threshold
,
1342 .llseek
= generic_file_llseek
,
1345 static const struct file_operations sel_avc_hash_stats_ops
= {
1346 .read
= sel_read_avc_hash_stats
,
1347 .llseek
= generic_file_llseek
,
1350 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1351 static struct avc_cache_stats
*sel_avc_get_stat_idx(loff_t
*idx
)
1355 for (cpu
= *idx
; cpu
< nr_cpu_ids
; ++cpu
) {
1356 if (!cpu_possible(cpu
))
1359 return &per_cpu(avc_cache_stats
, cpu
);
1364 static void *sel_avc_stats_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1366 loff_t n
= *pos
- 1;
1369 return SEQ_START_TOKEN
;
1371 return sel_avc_get_stat_idx(&n
);
1374 static void *sel_avc_stats_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1376 return sel_avc_get_stat_idx(pos
);
1379 static int sel_avc_stats_seq_show(struct seq_file
*seq
, void *v
)
1381 struct avc_cache_stats
*st
= v
;
1383 if (v
== SEQ_START_TOKEN
)
1384 seq_printf(seq
, "lookups hits misses allocations reclaims "
1387 seq_printf(seq
, "%u %u %u %u %u %u\n", st
->lookups
,
1388 st
->hits
, st
->misses
, st
->allocations
,
1389 st
->reclaims
, st
->frees
);
1393 static void sel_avc_stats_seq_stop(struct seq_file
*seq
, void *v
)
1396 static const struct seq_operations sel_avc_cache_stats_seq_ops
= {
1397 .start
= sel_avc_stats_seq_start
,
1398 .next
= sel_avc_stats_seq_next
,
1399 .show
= sel_avc_stats_seq_show
,
1400 .stop
= sel_avc_stats_seq_stop
,
1403 static int sel_open_avc_cache_stats(struct inode
*inode
, struct file
*file
)
1405 return seq_open(file
, &sel_avc_cache_stats_seq_ops
);
1408 static const struct file_operations sel_avc_cache_stats_ops
= {
1409 .open
= sel_open_avc_cache_stats
,
1411 .llseek
= seq_lseek
,
1412 .release
= seq_release
,
1416 static int sel_make_avc_files(struct dentry
*dir
)
1419 static struct tree_descr files
[] = {
1420 { "cache_threshold",
1421 &sel_avc_cache_threshold_ops
, S_IRUGO
|S_IWUSR
},
1422 { "hash_stats", &sel_avc_hash_stats_ops
, S_IRUGO
},
1423 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1424 { "cache_stats", &sel_avc_cache_stats_ops
, S_IRUGO
},
1428 for (i
= 0; i
< ARRAY_SIZE(files
); i
++) {
1429 struct inode
*inode
;
1430 struct dentry
*dentry
;
1432 dentry
= d_alloc_name(dir
, files
[i
].name
);
1438 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|files
[i
].mode
);
1443 inode
->i_fop
= files
[i
].ops
;
1444 inode
->i_ino
= ++sel_last_ino
;
1445 d_add(dentry
, inode
);
1451 static ssize_t
sel_read_initcon(struct file
*file
, char __user
*buf
,
1452 size_t count
, loff_t
*ppos
)
1454 struct inode
*inode
;
1459 inode
= file
->f_path
.dentry
->d_inode
;
1460 sid
= inode
->i_ino
&SEL_INO_MASK
;
1461 ret
= security_sid_to_context(sid
, &con
, &len
);
1465 ret
= simple_read_from_buffer(buf
, count
, ppos
, con
, len
);
1470 static const struct file_operations sel_initcon_ops
= {
1471 .read
= sel_read_initcon
,
1472 .llseek
= generic_file_llseek
,
1475 static int sel_make_initcon_files(struct dentry
*dir
)
1479 for (i
= 1; i
<= SECINITSID_NUM
; i
++) {
1480 struct inode
*inode
;
1481 struct dentry
*dentry
;
1482 dentry
= d_alloc_name(dir
, security_get_initial_sid_context(i
));
1488 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1493 inode
->i_fop
= &sel_initcon_ops
;
1494 inode
->i_ino
= i
|SEL_INITCON_INO_OFFSET
;
1495 d_add(dentry
, inode
);
1501 static inline unsigned int sel_div(unsigned long a
, unsigned long b
)
1503 return a
/ b
- (a
% b
< 0);
1506 static inline unsigned long sel_class_to_ino(u16
class)
1508 return (class * (SEL_VEC_MAX
+ 1)) | SEL_CLASS_INO_OFFSET
;
1511 static inline u16
sel_ino_to_class(unsigned long ino
)
1513 return sel_div(ino
& SEL_INO_MASK
, SEL_VEC_MAX
+ 1);
1516 static inline unsigned long sel_perm_to_ino(u16
class, u32 perm
)
1518 return (class * (SEL_VEC_MAX
+ 1) + perm
) | SEL_CLASS_INO_OFFSET
;
1521 static inline u32
sel_ino_to_perm(unsigned long ino
)
1523 return (ino
& SEL_INO_MASK
) % (SEL_VEC_MAX
+ 1);
1526 static ssize_t
sel_read_class(struct file
*file
, char __user
*buf
,
1527 size_t count
, loff_t
*ppos
)
1531 unsigned long ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
1533 page
= (char *)__get_free_page(GFP_KERNEL
);
1539 len
= snprintf(page
, PAGE_SIZE
, "%d", sel_ino_to_class(ino
));
1540 rc
= simple_read_from_buffer(buf
, count
, ppos
, page
, len
);
1541 free_page((unsigned long)page
);
1546 static const struct file_operations sel_class_ops
= {
1547 .read
= sel_read_class
,
1548 .llseek
= generic_file_llseek
,
1551 static ssize_t
sel_read_perm(struct file
*file
, char __user
*buf
,
1552 size_t count
, loff_t
*ppos
)
1556 unsigned long ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
1558 page
= (char *)__get_free_page(GFP_KERNEL
);
1564 len
= snprintf(page
, PAGE_SIZE
, "%d", sel_ino_to_perm(ino
));
1565 rc
= simple_read_from_buffer(buf
, count
, ppos
, page
, len
);
1566 free_page((unsigned long)page
);
1571 static const struct file_operations sel_perm_ops
= {
1572 .read
= sel_read_perm
,
1573 .llseek
= generic_file_llseek
,
1576 static ssize_t
sel_read_policycap(struct file
*file
, char __user
*buf
,
1577 size_t count
, loff_t
*ppos
)
1580 char tmpbuf
[TMPBUFLEN
];
1582 unsigned long i_ino
= file
->f_path
.dentry
->d_inode
->i_ino
;
1584 value
= security_policycap_supported(i_ino
& SEL_INO_MASK
);
1585 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%d", value
);
1587 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1590 static const struct file_operations sel_policycap_ops
= {
1591 .read
= sel_read_policycap
,
1592 .llseek
= generic_file_llseek
,
1595 static int sel_make_perm_files(char *objclass
, int classvalue
,
1598 int i
, rc
= 0, nperms
;
1601 rc
= security_get_permissions(objclass
, &perms
, &nperms
);
1605 for (i
= 0; i
< nperms
; i
++) {
1606 struct inode
*inode
;
1607 struct dentry
*dentry
;
1609 dentry
= d_alloc_name(dir
, perms
[i
]);
1615 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1620 inode
->i_fop
= &sel_perm_ops
;
1621 /* i+1 since perm values are 1-indexed */
1622 inode
->i_ino
= sel_perm_to_ino(classvalue
, i
+ 1);
1623 d_add(dentry
, inode
);
1627 for (i
= 0; i
< nperms
; i
++)
1634 static int sel_make_class_dir_entries(char *classname
, int index
,
1637 struct dentry
*dentry
= NULL
;
1638 struct inode
*inode
= NULL
;
1641 dentry
= d_alloc_name(dir
, "index");
1647 inode
= sel_make_inode(dir
->d_sb
, S_IFREG
|S_IRUGO
);
1653 inode
->i_fop
= &sel_class_ops
;
1654 inode
->i_ino
= sel_class_to_ino(index
);
1655 d_add(dentry
, inode
);
1657 dentry
= d_alloc_name(dir
, "perms");
1663 rc
= sel_make_dir(dir
->d_inode
, dentry
, &last_class_ino
);
1667 rc
= sel_make_perm_files(classname
, index
, dentry
);
1673 static void sel_remove_classes(void)
1675 struct list_head
*class_node
;
1677 list_for_each(class_node
, &class_dir
->d_subdirs
) {
1678 struct dentry
*class_subdir
= list_entry(class_node
,
1679 struct dentry
, d_u
.d_child
);
1680 struct list_head
*class_subdir_node
;
1682 list_for_each(class_subdir_node
, &class_subdir
->d_subdirs
) {
1683 struct dentry
*d
= list_entry(class_subdir_node
,
1684 struct dentry
, d_u
.d_child
);
1687 if (d
->d_inode
->i_mode
& S_IFDIR
)
1688 sel_remove_entries(d
);
1691 sel_remove_entries(class_subdir
);
1694 sel_remove_entries(class_dir
);
1697 static int sel_make_classes(void)
1699 int rc
= 0, nclasses
, i
;
1702 /* delete any existing entries */
1703 sel_remove_classes();
1705 rc
= security_get_classes(&classes
, &nclasses
);
1709 /* +2 since classes are 1-indexed */
1710 last_class_ino
= sel_class_to_ino(nclasses
+ 2);
1712 for (i
= 0; i
< nclasses
; i
++) {
1713 struct dentry
*class_name_dir
;
1715 class_name_dir
= d_alloc_name(class_dir
, classes
[i
]);
1716 if (!class_name_dir
) {
1721 rc
= sel_make_dir(class_dir
->d_inode
, class_name_dir
,
1726 /* i+1 since class values are 1-indexed */
1727 rc
= sel_make_class_dir_entries(classes
[i
], i
+ 1,
1734 for (i
= 0; i
< nclasses
; i
++)
1741 static int sel_make_policycap(void)
1744 struct dentry
*dentry
= NULL
;
1745 struct inode
*inode
= NULL
;
1747 sel_remove_entries(policycap_dir
);
1749 for (iter
= 0; iter
<= POLICYDB_CAPABILITY_MAX
; iter
++) {
1750 if (iter
< ARRAY_SIZE(policycap_names
))
1751 dentry
= d_alloc_name(policycap_dir
,
1752 policycap_names
[iter
]);
1754 dentry
= d_alloc_name(policycap_dir
, "unknown");
1759 inode
= sel_make_inode(policycap_dir
->d_sb
, S_IFREG
| S_IRUGO
);
1763 inode
->i_fop
= &sel_policycap_ops
;
1764 inode
->i_ino
= iter
| SEL_POLICYCAP_INO_OFFSET
;
1765 d_add(dentry
, inode
);
1771 static int sel_make_dir(struct inode
*dir
, struct dentry
*dentry
,
1775 struct inode
*inode
;
1777 inode
= sel_make_inode(dir
->i_sb
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
1782 inode
->i_op
= &simple_dir_inode_operations
;
1783 inode
->i_fop
= &simple_dir_operations
;
1784 inode
->i_ino
= ++(*ino
);
1785 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1787 d_add(dentry
, inode
);
1788 /* bump link count on parent directory, too */
1794 static int sel_fill_super(struct super_block
*sb
, void *data
, int silent
)
1797 struct dentry
*dentry
;
1798 struct inode
*inode
, *root_inode
;
1799 struct inode_security_struct
*isec
;
1801 static struct tree_descr selinux_files
[] = {
1802 [SEL_LOAD
] = {"load", &sel_load_ops
, S_IRUSR
|S_IWUSR
},
1803 [SEL_ENFORCE
] = {"enforce", &sel_enforce_ops
, S_IRUGO
|S_IWUSR
},
1804 [SEL_CONTEXT
] = {"context", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1805 [SEL_ACCESS
] = {"access", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1806 [SEL_CREATE
] = {"create", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1807 [SEL_RELABEL
] = {"relabel", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1808 [SEL_USER
] = {"user", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1809 [SEL_POLICYVERS
] = {"policyvers", &sel_policyvers_ops
, S_IRUGO
},
1810 [SEL_COMMIT_BOOLS
] = {"commit_pending_bools", &sel_commit_bools_ops
, S_IWUSR
},
1811 [SEL_MLS
] = {"mls", &sel_mls_ops
, S_IRUGO
},
1812 [SEL_DISABLE
] = {"disable", &sel_disable_ops
, S_IWUSR
},
1813 [SEL_MEMBER
] = {"member", &transaction_ops
, S_IRUGO
|S_IWUGO
},
1814 [SEL_CHECKREQPROT
] = {"checkreqprot", &sel_checkreqprot_ops
, S_IRUGO
|S_IWUSR
},
1815 [SEL_REJECT_UNKNOWN
] = {"reject_unknown", &sel_handle_unknown_ops
, S_IRUGO
},
1816 [SEL_DENY_UNKNOWN
] = {"deny_unknown", &sel_handle_unknown_ops
, S_IRUGO
},
1817 [SEL_STATUS
] = {"status", &sel_handle_status_ops
, S_IRUGO
},
1818 [SEL_POLICY
] = {"policy", &sel_policy_ops
, S_IRUSR
},
1821 ret
= simple_fill_super(sb
, SELINUX_MAGIC
, selinux_files
);
1825 root_inode
= sb
->s_root
->d_inode
;
1827 dentry
= d_alloc_name(sb
->s_root
, BOOL_DIR_NAME
);
1833 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1839 dentry
= d_alloc_name(sb
->s_root
, NULL_FILE_NAME
);
1845 inode
= sel_make_inode(sb
, S_IFCHR
| S_IRUGO
| S_IWUGO
);
1850 inode
->i_ino
= ++sel_last_ino
;
1851 isec
= (struct inode_security_struct
*)inode
->i_security
;
1852 isec
->sid
= SECINITSID_DEVNULL
;
1853 isec
->sclass
= SECCLASS_CHR_FILE
;
1854 isec
->initialized
= 1;
1856 init_special_inode(inode
, S_IFCHR
| S_IRUGO
| S_IWUGO
, MKDEV(MEM_MAJOR
, 3));
1857 d_add(dentry
, inode
);
1858 selinux_null
= dentry
;
1860 dentry
= d_alloc_name(sb
->s_root
, "avc");
1866 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1870 ret
= sel_make_avc_files(dentry
);
1874 dentry
= d_alloc_name(sb
->s_root
, "initial_contexts");
1880 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1884 ret
= sel_make_initcon_files(dentry
);
1888 dentry
= d_alloc_name(sb
->s_root
, "class");
1894 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1900 dentry
= d_alloc_name(sb
->s_root
, "policy_capabilities");
1906 ret
= sel_make_dir(root_inode
, dentry
, &sel_last_ino
);
1910 policycap_dir
= dentry
;
1915 printk(KERN_ERR
"SELinux: %s: failed while creating inodes\n",
1920 static struct dentry
*sel_mount(struct file_system_type
*fs_type
,
1921 int flags
, const char *dev_name
, void *data
)
1923 return mount_single(fs_type
, flags
, data
, sel_fill_super
);
1926 static struct file_system_type sel_fs_type
= {
1927 .name
= "selinuxfs",
1929 .kill_sb
= kill_litter_super
,
1932 struct vfsmount
*selinuxfs_mount
;
1934 static int __init
init_sel_fs(void)
1938 if (!selinux_enabled
)
1940 err
= register_filesystem(&sel_fs_type
);
1942 selinuxfs_mount
= kern_mount(&sel_fs_type
);
1943 if (IS_ERR(selinuxfs_mount
)) {
1944 printk(KERN_ERR
"selinuxfs: could not mount!\n");
1945 err
= PTR_ERR(selinuxfs_mount
);
1946 selinuxfs_mount
= NULL
;
1952 __initcall(init_sel_fs
);
1954 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1955 void exit_sel_fs(void)
1957 unregister_filesystem(&sel_fs_type
);