[PATCH] sched: fix interactive ceiling code
[linux-2.6/linux-mips.git] / security / selinux / selinuxfs.c
blob7029bbc9bef8c1b897e4883bd8b9d587eb431eeb
1 /* Updated: Karl MacMillan <kmacmillan@tresys.com>
3 * Added conditional policy language extensions
5 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
6 * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2.
12 #include <linux/config.h>
13 #include <linux/kernel.h>
14 #include <linux/pagemap.h>
15 #include <linux/slab.h>
16 #include <linux/vmalloc.h>
17 #include <linux/fs.h>
18 #include <linux/mutex.h>
19 #include <linux/init.h>
20 #include <linux/string.h>
21 #include <linux/security.h>
22 #include <linux/major.h>
23 #include <linux/seq_file.h>
24 #include <linux/percpu.h>
25 #include <linux/audit.h>
26 #include <asm/uaccess.h>
27 #include <asm/semaphore.h>
29 /* selinuxfs pseudo filesystem for exporting the security policy API.
30 Based on the proc code and the fs/nfsd/nfsctl.c code. */
32 #include "flask.h"
33 #include "avc.h"
34 #include "avc_ss.h"
35 #include "security.h"
36 #include "objsec.h"
37 #include "conditional.h"
39 unsigned int selinux_checkreqprot = CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
41 #ifdef CONFIG_SECURITY_SELINUX_ENABLE_SECMARK_DEFAULT
42 #define SELINUX_COMPAT_NET_VALUE 0
43 #else
44 #define SELINUX_COMPAT_NET_VALUE 1
45 #endif
47 int selinux_compat_net = SELINUX_COMPAT_NET_VALUE;
49 static int __init checkreqprot_setup(char *str)
51 selinux_checkreqprot = simple_strtoul(str,NULL,0) ? 1 : 0;
52 return 1;
54 __setup("checkreqprot=", checkreqprot_setup);
56 static int __init selinux_compat_net_setup(char *str)
58 selinux_compat_net = simple_strtoul(str,NULL,0) ? 1 : 0;
59 return 1;
61 __setup("selinux_compat_net=", selinux_compat_net_setup);
64 static DEFINE_MUTEX(sel_mutex);
66 /* global data for booleans */
67 static struct dentry *bool_dir = NULL;
68 static int bool_num = 0;
69 static int *bool_pending_values = NULL;
71 extern void selnl_notify_setenforce(int val);
73 /* Check whether a task is allowed to use a security operation. */
74 static int task_has_security(struct task_struct *tsk,
75 u32 perms)
77 struct task_security_struct *tsec;
79 tsec = tsk->security;
80 if (!tsec)
81 return -EACCES;
83 return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
84 SECCLASS_SECURITY, perms, NULL);
87 enum sel_inos {
88 SEL_ROOT_INO = 2,
89 SEL_LOAD, /* load policy */
90 SEL_ENFORCE, /* get or set enforcing status */
91 SEL_CONTEXT, /* validate context */
92 SEL_ACCESS, /* compute access decision */
93 SEL_CREATE, /* compute create labeling decision */
94 SEL_RELABEL, /* compute relabeling decision */
95 SEL_USER, /* compute reachable user contexts */
96 SEL_POLICYVERS, /* return policy version for this kernel */
97 SEL_COMMIT_BOOLS, /* commit new boolean values */
98 SEL_MLS, /* return if MLS policy is enabled */
99 SEL_DISABLE, /* disable SELinux until next reboot */
100 SEL_AVC, /* AVC management directory */
101 SEL_MEMBER, /* compute polyinstantiation membership decision */
102 SEL_CHECKREQPROT, /* check requested protection, not kernel-applied one */
103 SEL_COMPAT_NET, /* whether to use old compat network packet controls */
106 #define TMPBUFLEN 12
107 static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
108 size_t count, loff_t *ppos)
110 char tmpbuf[TMPBUFLEN];
111 ssize_t length;
113 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_enforcing);
114 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
117 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
118 static ssize_t sel_write_enforce(struct file * file, const char __user * buf,
119 size_t count, loff_t *ppos)
122 char *page;
123 ssize_t length;
124 int new_value;
126 if (count >= PAGE_SIZE)
127 return -ENOMEM;
128 if (*ppos != 0) {
129 /* No partial writes. */
130 return -EINVAL;
132 page = (char*)get_zeroed_page(GFP_KERNEL);
133 if (!page)
134 return -ENOMEM;
135 length = -EFAULT;
136 if (copy_from_user(page, buf, count))
137 goto out;
139 length = -EINVAL;
140 if (sscanf(page, "%d", &new_value) != 1)
141 goto out;
143 if (new_value != selinux_enforcing) {
144 length = task_has_security(current, SECURITY__SETENFORCE);
145 if (length)
146 goto out;
147 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
148 "enforcing=%d old_enforcing=%d auid=%u", new_value,
149 selinux_enforcing,
150 audit_get_loginuid(current->audit_context));
151 selinux_enforcing = new_value;
152 if (selinux_enforcing)
153 avc_ss_reset(0);
154 selnl_notify_setenforce(selinux_enforcing);
156 length = count;
157 out:
158 free_page((unsigned long) page);
159 return length;
161 #else
162 #define sel_write_enforce NULL
163 #endif
165 static struct file_operations sel_enforce_ops = {
166 .read = sel_read_enforce,
167 .write = sel_write_enforce,
170 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
171 static ssize_t sel_write_disable(struct file * file, const char __user * buf,
172 size_t count, loff_t *ppos)
175 char *page;
176 ssize_t length;
177 int new_value;
178 extern int selinux_disable(void);
180 if (count >= PAGE_SIZE)
181 return -ENOMEM;
182 if (*ppos != 0) {
183 /* No partial writes. */
184 return -EINVAL;
186 page = (char*)get_zeroed_page(GFP_KERNEL);
187 if (!page)
188 return -ENOMEM;
189 length = -EFAULT;
190 if (copy_from_user(page, buf, count))
191 goto out;
193 length = -EINVAL;
194 if (sscanf(page, "%d", &new_value) != 1)
195 goto out;
197 if (new_value) {
198 length = selinux_disable();
199 if (length < 0)
200 goto out;
201 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
202 "selinux=0 auid=%u",
203 audit_get_loginuid(current->audit_context));
206 length = count;
207 out:
208 free_page((unsigned long) page);
209 return length;
211 #else
212 #define sel_write_disable NULL
213 #endif
215 static struct file_operations sel_disable_ops = {
216 .write = sel_write_disable,
219 static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
220 size_t count, loff_t *ppos)
222 char tmpbuf[TMPBUFLEN];
223 ssize_t length;
225 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", POLICYDB_VERSION_MAX);
226 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
229 static struct file_operations sel_policyvers_ops = {
230 .read = sel_read_policyvers,
233 /* declaration for sel_write_load */
234 static int sel_make_bools(void);
236 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
237 size_t count, loff_t *ppos)
239 char tmpbuf[TMPBUFLEN];
240 ssize_t length;
242 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_mls_enabled);
243 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
246 static struct file_operations sel_mls_ops = {
247 .read = sel_read_mls,
250 static ssize_t sel_write_load(struct file * file, const char __user * buf,
251 size_t count, loff_t *ppos)
254 int ret;
255 ssize_t length;
256 void *data = NULL;
258 mutex_lock(&sel_mutex);
260 length = task_has_security(current, SECURITY__LOAD_POLICY);
261 if (length)
262 goto out;
264 if (*ppos != 0) {
265 /* No partial writes. */
266 length = -EINVAL;
267 goto out;
270 if ((count > 64 * 1024 * 1024)
271 || (data = vmalloc(count)) == NULL) {
272 length = -ENOMEM;
273 goto out;
276 length = -EFAULT;
277 if (copy_from_user(data, buf, count) != 0)
278 goto out;
280 length = security_load_policy(data, count);
281 if (length)
282 goto out;
284 ret = sel_make_bools();
285 if (ret)
286 length = ret;
287 else
288 length = count;
289 audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
290 "policy loaded auid=%u",
291 audit_get_loginuid(current->audit_context));
292 out:
293 mutex_unlock(&sel_mutex);
294 vfree(data);
295 return length;
298 static struct file_operations sel_load_ops = {
299 .write = sel_write_load,
302 static ssize_t sel_write_context(struct file * file, char *buf, size_t size)
304 char *canon;
305 u32 sid, len;
306 ssize_t length;
308 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
309 if (length)
310 return length;
312 length = security_context_to_sid(buf, size, &sid);
313 if (length < 0)
314 return length;
316 length = security_sid_to_context(sid, &canon, &len);
317 if (length < 0)
318 return length;
320 if (len > SIMPLE_TRANSACTION_LIMIT) {
321 printk(KERN_ERR "%s: context size (%u) exceeds payload "
322 "max\n", __FUNCTION__, len);
323 length = -ERANGE;
324 goto out;
327 memcpy(buf, canon, len);
328 length = len;
329 out:
330 kfree(canon);
331 return length;
334 static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
335 size_t count, loff_t *ppos)
337 char tmpbuf[TMPBUFLEN];
338 ssize_t length;
340 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_checkreqprot);
341 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
344 static ssize_t sel_write_checkreqprot(struct file * file, const char __user * buf,
345 size_t count, loff_t *ppos)
347 char *page;
348 ssize_t length;
349 unsigned int new_value;
351 length = task_has_security(current, SECURITY__SETCHECKREQPROT);
352 if (length)
353 return length;
355 if (count >= PAGE_SIZE)
356 return -ENOMEM;
357 if (*ppos != 0) {
358 /* No partial writes. */
359 return -EINVAL;
361 page = (char*)get_zeroed_page(GFP_KERNEL);
362 if (!page)
363 return -ENOMEM;
364 length = -EFAULT;
365 if (copy_from_user(page, buf, count))
366 goto out;
368 length = -EINVAL;
369 if (sscanf(page, "%u", &new_value) != 1)
370 goto out;
372 selinux_checkreqprot = new_value ? 1 : 0;
373 length = count;
374 out:
375 free_page((unsigned long) page);
376 return length;
378 static struct file_operations sel_checkreqprot_ops = {
379 .read = sel_read_checkreqprot,
380 .write = sel_write_checkreqprot,
383 static ssize_t sel_read_compat_net(struct file *filp, char __user *buf,
384 size_t count, loff_t *ppos)
386 char tmpbuf[TMPBUFLEN];
387 ssize_t length;
389 length = scnprintf(tmpbuf, TMPBUFLEN, "%d", selinux_compat_net);
390 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
393 static ssize_t sel_write_compat_net(struct file * file, const char __user * buf,
394 size_t count, loff_t *ppos)
396 char *page;
397 ssize_t length;
398 int new_value;
400 length = task_has_security(current, SECURITY__LOAD_POLICY);
401 if (length)
402 return length;
404 if (count >= PAGE_SIZE)
405 return -ENOMEM;
406 if (*ppos != 0) {
407 /* No partial writes. */
408 return -EINVAL;
410 page = (char*)get_zeroed_page(GFP_KERNEL);
411 if (!page)
412 return -ENOMEM;
413 length = -EFAULT;
414 if (copy_from_user(page, buf, count))
415 goto out;
417 length = -EINVAL;
418 if (sscanf(page, "%d", &new_value) != 1)
419 goto out;
421 selinux_compat_net = new_value ? 1 : 0;
422 length = count;
423 out:
424 free_page((unsigned long) page);
425 return length;
427 static struct file_operations sel_compat_net_ops = {
428 .read = sel_read_compat_net,
429 .write = sel_write_compat_net,
433 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
435 static ssize_t sel_write_access(struct file * file, char *buf, size_t size);
436 static ssize_t sel_write_create(struct file * file, char *buf, size_t size);
437 static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size);
438 static ssize_t sel_write_user(struct file * file, char *buf, size_t size);
439 static ssize_t sel_write_member(struct file * file, char *buf, size_t size);
441 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
442 [SEL_ACCESS] = sel_write_access,
443 [SEL_CREATE] = sel_write_create,
444 [SEL_RELABEL] = sel_write_relabel,
445 [SEL_USER] = sel_write_user,
446 [SEL_MEMBER] = sel_write_member,
447 [SEL_CONTEXT] = sel_write_context,
450 static ssize_t selinux_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
452 ino_t ino = file->f_dentry->d_inode->i_ino;
453 char *data;
454 ssize_t rv;
456 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
457 return -EINVAL;
459 data = simple_transaction_get(file, buf, size);
460 if (IS_ERR(data))
461 return PTR_ERR(data);
463 rv = write_op[ino](file, data, size);
464 if (rv>0) {
465 simple_transaction_set(file, rv);
466 rv = size;
468 return rv;
471 static struct file_operations transaction_ops = {
472 .write = selinux_transaction_write,
473 .read = simple_transaction_read,
474 .release = simple_transaction_release,
478 * payload - write methods
479 * If the method has a response, the response should be put in buf,
480 * and the length returned. Otherwise return 0 or and -error.
483 static ssize_t sel_write_access(struct file * file, char *buf, size_t size)
485 char *scon, *tcon;
486 u32 ssid, tsid;
487 u16 tclass;
488 u32 req;
489 struct av_decision avd;
490 ssize_t length;
492 length = task_has_security(current, SECURITY__COMPUTE_AV);
493 if (length)
494 return length;
496 length = -ENOMEM;
497 scon = kzalloc(size+1, GFP_KERNEL);
498 if (!scon)
499 return length;
501 tcon = kzalloc(size+1, GFP_KERNEL);
502 if (!tcon)
503 goto out;
505 length = -EINVAL;
506 if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
507 goto out2;
509 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
510 if (length < 0)
511 goto out2;
512 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
513 if (length < 0)
514 goto out2;
516 length = security_compute_av(ssid, tsid, tclass, req, &avd);
517 if (length < 0)
518 goto out2;
520 length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
521 "%x %x %x %x %u",
522 avd.allowed, avd.decided,
523 avd.auditallow, avd.auditdeny,
524 avd.seqno);
525 out2:
526 kfree(tcon);
527 out:
528 kfree(scon);
529 return length;
532 static ssize_t sel_write_create(struct file * file, char *buf, size_t size)
534 char *scon, *tcon;
535 u32 ssid, tsid, newsid;
536 u16 tclass;
537 ssize_t length;
538 char *newcon;
539 u32 len;
541 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
542 if (length)
543 return length;
545 length = -ENOMEM;
546 scon = kzalloc(size+1, GFP_KERNEL);
547 if (!scon)
548 return length;
550 tcon = kzalloc(size+1, GFP_KERNEL);
551 if (!tcon)
552 goto out;
554 length = -EINVAL;
555 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
556 goto out2;
558 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
559 if (length < 0)
560 goto out2;
561 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
562 if (length < 0)
563 goto out2;
565 length = security_transition_sid(ssid, tsid, tclass, &newsid);
566 if (length < 0)
567 goto out2;
569 length = security_sid_to_context(newsid, &newcon, &len);
570 if (length < 0)
571 goto out2;
573 if (len > SIMPLE_TRANSACTION_LIMIT) {
574 printk(KERN_ERR "%s: context size (%u) exceeds payload "
575 "max\n", __FUNCTION__, len);
576 length = -ERANGE;
577 goto out3;
580 memcpy(buf, newcon, len);
581 length = len;
582 out3:
583 kfree(newcon);
584 out2:
585 kfree(tcon);
586 out:
587 kfree(scon);
588 return length;
591 static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size)
593 char *scon, *tcon;
594 u32 ssid, tsid, newsid;
595 u16 tclass;
596 ssize_t length;
597 char *newcon;
598 u32 len;
600 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
601 if (length)
602 return length;
604 length = -ENOMEM;
605 scon = kzalloc(size+1, GFP_KERNEL);
606 if (!scon)
607 return length;
609 tcon = kzalloc(size+1, GFP_KERNEL);
610 if (!tcon)
611 goto out;
613 length = -EINVAL;
614 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
615 goto out2;
617 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
618 if (length < 0)
619 goto out2;
620 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
621 if (length < 0)
622 goto out2;
624 length = security_change_sid(ssid, tsid, tclass, &newsid);
625 if (length < 0)
626 goto out2;
628 length = security_sid_to_context(newsid, &newcon, &len);
629 if (length < 0)
630 goto out2;
632 if (len > SIMPLE_TRANSACTION_LIMIT) {
633 length = -ERANGE;
634 goto out3;
637 memcpy(buf, newcon, len);
638 length = len;
639 out3:
640 kfree(newcon);
641 out2:
642 kfree(tcon);
643 out:
644 kfree(scon);
645 return length;
648 static ssize_t sel_write_user(struct file * file, char *buf, size_t size)
650 char *con, *user, *ptr;
651 u32 sid, *sids;
652 ssize_t length;
653 char *newcon;
654 int i, rc;
655 u32 len, nsids;
657 length = task_has_security(current, SECURITY__COMPUTE_USER);
658 if (length)
659 return length;
661 length = -ENOMEM;
662 con = kzalloc(size+1, GFP_KERNEL);
663 if (!con)
664 return length;
666 user = kzalloc(size+1, GFP_KERNEL);
667 if (!user)
668 goto out;
670 length = -EINVAL;
671 if (sscanf(buf, "%s %s", con, user) != 2)
672 goto out2;
674 length = security_context_to_sid(con, strlen(con)+1, &sid);
675 if (length < 0)
676 goto out2;
678 length = security_get_user_sids(sid, user, &sids, &nsids);
679 if (length < 0)
680 goto out2;
682 length = sprintf(buf, "%u", nsids) + 1;
683 ptr = buf + length;
684 for (i = 0; i < nsids; i++) {
685 rc = security_sid_to_context(sids[i], &newcon, &len);
686 if (rc) {
687 length = rc;
688 goto out3;
690 if ((length + len) >= SIMPLE_TRANSACTION_LIMIT) {
691 kfree(newcon);
692 length = -ERANGE;
693 goto out3;
695 memcpy(ptr, newcon, len);
696 kfree(newcon);
697 ptr += len;
698 length += len;
700 out3:
701 kfree(sids);
702 out2:
703 kfree(user);
704 out:
705 kfree(con);
706 return length;
709 static ssize_t sel_write_member(struct file * file, char *buf, size_t size)
711 char *scon, *tcon;
712 u32 ssid, tsid, newsid;
713 u16 tclass;
714 ssize_t length;
715 char *newcon;
716 u32 len;
718 length = task_has_security(current, SECURITY__COMPUTE_MEMBER);
719 if (length)
720 return length;
722 length = -ENOMEM;
723 scon = kzalloc(size+1, GFP_KERNEL);
724 if (!scon)
725 return length;
727 tcon = kzalloc(size+1, GFP_KERNEL);
728 if (!tcon)
729 goto out;
731 length = -EINVAL;
732 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
733 goto out2;
735 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
736 if (length < 0)
737 goto out2;
738 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
739 if (length < 0)
740 goto out2;
742 length = security_member_sid(ssid, tsid, tclass, &newsid);
743 if (length < 0)
744 goto out2;
746 length = security_sid_to_context(newsid, &newcon, &len);
747 if (length < 0)
748 goto out2;
750 if (len > SIMPLE_TRANSACTION_LIMIT) {
751 printk(KERN_ERR "%s: context size (%u) exceeds payload "
752 "max\n", __FUNCTION__, len);
753 length = -ERANGE;
754 goto out3;
757 memcpy(buf, newcon, len);
758 length = len;
759 out3:
760 kfree(newcon);
761 out2:
762 kfree(tcon);
763 out:
764 kfree(scon);
765 return length;
768 static struct inode *sel_make_inode(struct super_block *sb, int mode)
770 struct inode *ret = new_inode(sb);
772 if (ret) {
773 ret->i_mode = mode;
774 ret->i_uid = ret->i_gid = 0;
775 ret->i_blksize = PAGE_CACHE_SIZE;
776 ret->i_blocks = 0;
777 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
779 return ret;
782 #define BOOL_INO_OFFSET 30
784 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
785 size_t count, loff_t *ppos)
787 char *page = NULL;
788 ssize_t length;
789 ssize_t ret;
790 int cur_enforcing;
791 struct inode *inode;
793 mutex_lock(&sel_mutex);
795 ret = -EFAULT;
797 /* check to see if this file has been deleted */
798 if (!filep->f_op)
799 goto out;
801 if (count > PAGE_SIZE) {
802 ret = -EINVAL;
803 goto out;
805 if (!(page = (char*)get_zeroed_page(GFP_KERNEL))) {
806 ret = -ENOMEM;
807 goto out;
810 inode = filep->f_dentry->d_inode;
811 cur_enforcing = security_get_bool_value(inode->i_ino - BOOL_INO_OFFSET);
812 if (cur_enforcing < 0) {
813 ret = cur_enforcing;
814 goto out;
817 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
818 bool_pending_values[inode->i_ino - BOOL_INO_OFFSET]);
819 ret = simple_read_from_buffer(buf, count, ppos, page, length);
820 out:
821 mutex_unlock(&sel_mutex);
822 if (page)
823 free_page((unsigned long)page);
824 return ret;
827 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
828 size_t count, loff_t *ppos)
830 char *page = NULL;
831 ssize_t length = -EFAULT;
832 int new_value;
833 struct inode *inode;
835 mutex_lock(&sel_mutex);
837 length = task_has_security(current, SECURITY__SETBOOL);
838 if (length)
839 goto out;
841 /* check to see if this file has been deleted */
842 if (!filep->f_op)
843 goto out;
845 if (count >= PAGE_SIZE) {
846 length = -ENOMEM;
847 goto out;
849 if (*ppos != 0) {
850 /* No partial writes. */
851 goto out;
853 page = (char*)get_zeroed_page(GFP_KERNEL);
854 if (!page) {
855 length = -ENOMEM;
856 goto out;
859 if (copy_from_user(page, buf, count))
860 goto out;
862 length = -EINVAL;
863 if (sscanf(page, "%d", &new_value) != 1)
864 goto out;
866 if (new_value)
867 new_value = 1;
869 inode = filep->f_dentry->d_inode;
870 bool_pending_values[inode->i_ino - BOOL_INO_OFFSET] = new_value;
871 length = count;
873 out:
874 mutex_unlock(&sel_mutex);
875 if (page)
876 free_page((unsigned long) page);
877 return length;
880 static struct file_operations sel_bool_ops = {
881 .read = sel_read_bool,
882 .write = sel_write_bool,
885 static ssize_t sel_commit_bools_write(struct file *filep,
886 const char __user *buf,
887 size_t count, loff_t *ppos)
889 char *page = NULL;
890 ssize_t length = -EFAULT;
891 int new_value;
893 mutex_lock(&sel_mutex);
895 length = task_has_security(current, SECURITY__SETBOOL);
896 if (length)
897 goto out;
899 /* check to see if this file has been deleted */
900 if (!filep->f_op)
901 goto out;
903 if (count >= PAGE_SIZE) {
904 length = -ENOMEM;
905 goto out;
907 if (*ppos != 0) {
908 /* No partial writes. */
909 goto out;
911 page = (char*)get_zeroed_page(GFP_KERNEL);
912 if (!page) {
913 length = -ENOMEM;
914 goto out;
917 if (copy_from_user(page, buf, count))
918 goto out;
920 length = -EINVAL;
921 if (sscanf(page, "%d", &new_value) != 1)
922 goto out;
924 if (new_value && bool_pending_values) {
925 security_set_bools(bool_num, bool_pending_values);
928 length = count;
930 out:
931 mutex_unlock(&sel_mutex);
932 if (page)
933 free_page((unsigned long) page);
934 return length;
937 static struct file_operations sel_commit_bools_ops = {
938 .write = sel_commit_bools_write,
941 /* delete booleans - partial revoke() from
942 * fs/proc/generic.c proc_kill_inodes */
943 static void sel_remove_bools(struct dentry *de)
945 struct list_head *p, *node;
946 struct super_block *sb = de->d_sb;
948 spin_lock(&dcache_lock);
949 node = de->d_subdirs.next;
950 while (node != &de->d_subdirs) {
951 struct dentry *d = list_entry(node, struct dentry, d_u.d_child);
952 list_del_init(node);
954 if (d->d_inode) {
955 d = dget_locked(d);
956 spin_unlock(&dcache_lock);
957 d_delete(d);
958 simple_unlink(de->d_inode, d);
959 dput(d);
960 spin_lock(&dcache_lock);
962 node = de->d_subdirs.next;
965 spin_unlock(&dcache_lock);
967 file_list_lock();
968 list_for_each(p, &sb->s_files) {
969 struct file * filp = list_entry(p, struct file, f_u.fu_list);
970 struct dentry * dentry = filp->f_dentry;
972 if (dentry->d_parent != de) {
973 continue;
975 filp->f_op = NULL;
977 file_list_unlock();
980 #define BOOL_DIR_NAME "booleans"
982 static int sel_make_bools(void)
984 int i, ret = 0;
985 ssize_t len;
986 struct dentry *dentry = NULL;
987 struct dentry *dir = bool_dir;
988 struct inode *inode = NULL;
989 struct inode_security_struct *isec;
990 char **names = NULL, *page;
991 int num;
992 int *values = NULL;
993 u32 sid;
995 /* remove any existing files */
996 kfree(bool_pending_values);
997 bool_pending_values = NULL;
999 sel_remove_bools(dir);
1001 if (!(page = (char*)get_zeroed_page(GFP_KERNEL)))
1002 return -ENOMEM;
1004 ret = security_get_bools(&num, &names, &values);
1005 if (ret != 0)
1006 goto out;
1008 for (i = 0; i < num; i++) {
1009 dentry = d_alloc_name(dir, names[i]);
1010 if (!dentry) {
1011 ret = -ENOMEM;
1012 goto err;
1014 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1015 if (!inode) {
1016 ret = -ENOMEM;
1017 goto err;
1020 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1021 if (len < 0) {
1022 ret = -EINVAL;
1023 goto err;
1024 } else if (len >= PAGE_SIZE) {
1025 ret = -ENAMETOOLONG;
1026 goto err;
1028 isec = (struct inode_security_struct*)inode->i_security;
1029 if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid)))
1030 goto err;
1031 isec->sid = sid;
1032 isec->initialized = 1;
1033 inode->i_fop = &sel_bool_ops;
1034 inode->i_ino = i + BOOL_INO_OFFSET;
1035 d_add(dentry, inode);
1037 bool_num = num;
1038 bool_pending_values = values;
1039 out:
1040 free_page((unsigned long)page);
1041 if (names) {
1042 for (i = 0; i < num; i++)
1043 kfree(names[i]);
1044 kfree(names);
1046 return ret;
1047 err:
1048 kfree(values);
1049 sel_remove_bools(dir);
1050 ret = -ENOMEM;
1051 goto out;
1054 #define NULL_FILE_NAME "null"
1056 struct dentry *selinux_null = NULL;
1058 static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
1059 size_t count, loff_t *ppos)
1061 char tmpbuf[TMPBUFLEN];
1062 ssize_t length;
1064 length = scnprintf(tmpbuf, TMPBUFLEN, "%u", avc_cache_threshold);
1065 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1068 static ssize_t sel_write_avc_cache_threshold(struct file * file,
1069 const char __user * buf,
1070 size_t count, loff_t *ppos)
1073 char *page;
1074 ssize_t ret;
1075 int new_value;
1077 if (count >= PAGE_SIZE) {
1078 ret = -ENOMEM;
1079 goto out;
1082 if (*ppos != 0) {
1083 /* No partial writes. */
1084 ret = -EINVAL;
1085 goto out;
1088 page = (char*)get_zeroed_page(GFP_KERNEL);
1089 if (!page) {
1090 ret = -ENOMEM;
1091 goto out;
1094 if (copy_from_user(page, buf, count)) {
1095 ret = -EFAULT;
1096 goto out_free;
1099 if (sscanf(page, "%u", &new_value) != 1) {
1100 ret = -EINVAL;
1101 goto out;
1104 if (new_value != avc_cache_threshold) {
1105 ret = task_has_security(current, SECURITY__SETSECPARAM);
1106 if (ret)
1107 goto out_free;
1108 avc_cache_threshold = new_value;
1110 ret = count;
1111 out_free:
1112 free_page((unsigned long)page);
1113 out:
1114 return ret;
1117 static ssize_t sel_read_avc_hash_stats(struct file *filp, char __user *buf,
1118 size_t count, loff_t *ppos)
1120 char *page;
1121 ssize_t ret = 0;
1123 page = (char *)__get_free_page(GFP_KERNEL);
1124 if (!page) {
1125 ret = -ENOMEM;
1126 goto out;
1128 ret = avc_get_hash_stats(page);
1129 if (ret >= 0)
1130 ret = simple_read_from_buffer(buf, count, ppos, page, ret);
1131 free_page((unsigned long)page);
1132 out:
1133 return ret;
1136 static struct file_operations sel_avc_cache_threshold_ops = {
1137 .read = sel_read_avc_cache_threshold,
1138 .write = sel_write_avc_cache_threshold,
1141 static struct file_operations sel_avc_hash_stats_ops = {
1142 .read = sel_read_avc_hash_stats,
1145 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1146 static struct avc_cache_stats *sel_avc_get_stat_idx(loff_t *idx)
1148 int cpu;
1150 for (cpu = *idx; cpu < NR_CPUS; ++cpu) {
1151 if (!cpu_possible(cpu))
1152 continue;
1153 *idx = cpu + 1;
1154 return &per_cpu(avc_cache_stats, cpu);
1156 return NULL;
1159 static void *sel_avc_stats_seq_start(struct seq_file *seq, loff_t *pos)
1161 loff_t n = *pos - 1;
1163 if (*pos == 0)
1164 return SEQ_START_TOKEN;
1166 return sel_avc_get_stat_idx(&n);
1169 static void *sel_avc_stats_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1171 return sel_avc_get_stat_idx(pos);
1174 static int sel_avc_stats_seq_show(struct seq_file *seq, void *v)
1176 struct avc_cache_stats *st = v;
1178 if (v == SEQ_START_TOKEN)
1179 seq_printf(seq, "lookups hits misses allocations reclaims "
1180 "frees\n");
1181 else
1182 seq_printf(seq, "%u %u %u %u %u %u\n", st->lookups,
1183 st->hits, st->misses, st->allocations,
1184 st->reclaims, st->frees);
1185 return 0;
1188 static void sel_avc_stats_seq_stop(struct seq_file *seq, void *v)
1191 static struct seq_operations sel_avc_cache_stats_seq_ops = {
1192 .start = sel_avc_stats_seq_start,
1193 .next = sel_avc_stats_seq_next,
1194 .show = sel_avc_stats_seq_show,
1195 .stop = sel_avc_stats_seq_stop,
1198 static int sel_open_avc_cache_stats(struct inode *inode, struct file *file)
1200 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1203 static struct file_operations sel_avc_cache_stats_ops = {
1204 .open = sel_open_avc_cache_stats,
1205 .read = seq_read,
1206 .llseek = seq_lseek,
1207 .release = seq_release,
1209 #endif
1211 static int sel_make_avc_files(struct dentry *dir)
1213 int i, ret = 0;
1214 static struct tree_descr files[] = {
1215 { "cache_threshold",
1216 &sel_avc_cache_threshold_ops, S_IRUGO|S_IWUSR },
1217 { "hash_stats", &sel_avc_hash_stats_ops, S_IRUGO },
1218 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1219 { "cache_stats", &sel_avc_cache_stats_ops, S_IRUGO },
1220 #endif
1223 for (i = 0; i < ARRAY_SIZE(files); i++) {
1224 struct inode *inode;
1225 struct dentry *dentry;
1227 dentry = d_alloc_name(dir, files[i].name);
1228 if (!dentry) {
1229 ret = -ENOMEM;
1230 goto out;
1233 inode = sel_make_inode(dir->d_sb, S_IFREG|files[i].mode);
1234 if (!inode) {
1235 ret = -ENOMEM;
1236 goto out;
1238 inode->i_fop = files[i].ops;
1239 d_add(dentry, inode);
1241 out:
1242 return ret;
1245 static int sel_make_dir(struct inode *dir, struct dentry *dentry)
1247 int ret = 0;
1248 struct inode *inode;
1250 inode = sel_make_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
1251 if (!inode) {
1252 ret = -ENOMEM;
1253 goto out;
1255 inode->i_op = &simple_dir_inode_operations;
1256 inode->i_fop = &simple_dir_operations;
1257 /* directory inodes start off with i_nlink == 2 (for "." entry) */
1258 inode->i_nlink++;
1259 d_add(dentry, inode);
1260 /* bump link count on parent directory, too */
1261 dir->i_nlink++;
1262 out:
1263 return ret;
1266 static int sel_fill_super(struct super_block * sb, void * data, int silent)
1268 int ret;
1269 struct dentry *dentry;
1270 struct inode *inode, *root_inode;
1271 struct inode_security_struct *isec;
1273 static struct tree_descr selinux_files[] = {
1274 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1275 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1276 [SEL_CONTEXT] = {"context", &transaction_ops, S_IRUGO|S_IWUGO},
1277 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1278 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1279 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1280 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1281 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1282 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1283 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1284 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1285 [SEL_MEMBER] = {"member", &transaction_ops, S_IRUGO|S_IWUGO},
1286 [SEL_CHECKREQPROT] = {"checkreqprot", &sel_checkreqprot_ops, S_IRUGO|S_IWUSR},
1287 [SEL_COMPAT_NET] = {"compat_net", &sel_compat_net_ops, S_IRUGO|S_IWUSR},
1288 /* last one */ {""}
1290 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1291 if (ret)
1292 goto err;
1294 root_inode = sb->s_root->d_inode;
1296 dentry = d_alloc_name(sb->s_root, BOOL_DIR_NAME);
1297 if (!dentry) {
1298 ret = -ENOMEM;
1299 goto err;
1302 ret = sel_make_dir(root_inode, dentry);
1303 if (ret)
1304 goto err;
1306 bool_dir = dentry;
1308 dentry = d_alloc_name(sb->s_root, NULL_FILE_NAME);
1309 if (!dentry) {
1310 ret = -ENOMEM;
1311 goto err;
1314 inode = sel_make_inode(sb, S_IFCHR | S_IRUGO | S_IWUGO);
1315 if (!inode) {
1316 ret = -ENOMEM;
1317 goto err;
1319 isec = (struct inode_security_struct*)inode->i_security;
1320 isec->sid = SECINITSID_DEVNULL;
1321 isec->sclass = SECCLASS_CHR_FILE;
1322 isec->initialized = 1;
1324 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
1325 d_add(dentry, inode);
1326 selinux_null = dentry;
1328 dentry = d_alloc_name(sb->s_root, "avc");
1329 if (!dentry) {
1330 ret = -ENOMEM;
1331 goto err;
1334 ret = sel_make_dir(root_inode, dentry);
1335 if (ret)
1336 goto err;
1338 ret = sel_make_avc_files(dentry);
1339 if (ret)
1340 goto err;
1341 out:
1342 return ret;
1343 err:
1344 printk(KERN_ERR "%s: failed while creating inodes\n", __FUNCTION__);
1345 goto out;
1348 static int sel_get_sb(struct file_system_type *fs_type,
1349 int flags, const char *dev_name, void *data,
1350 struct vfsmount *mnt)
1352 return get_sb_single(fs_type, flags, data, sel_fill_super, mnt);
1355 static struct file_system_type sel_fs_type = {
1356 .name = "selinuxfs",
1357 .get_sb = sel_get_sb,
1358 .kill_sb = kill_litter_super,
1361 struct vfsmount *selinuxfs_mount;
1363 static int __init init_sel_fs(void)
1365 int err;
1367 if (!selinux_enabled)
1368 return 0;
1369 err = register_filesystem(&sel_fs_type);
1370 if (!err) {
1371 selinuxfs_mount = kern_mount(&sel_fs_type);
1372 if (IS_ERR(selinuxfs_mount)) {
1373 printk(KERN_ERR "selinuxfs: could not mount!\n");
1374 err = PTR_ERR(selinuxfs_mount);
1375 selinuxfs_mount = NULL;
1378 return err;
1381 __initcall(init_sel_fs);
1383 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1384 void exit_sel_fs(void)
1386 unregister_filesystem(&sel_fs_type);
1388 #endif