1 /* Updated: Karl MacMillan <kmacmillan@tresys.com>
3 * Added conditional policy language extensions
5 * Copyright (C) 2003 - 2004 Tresys Technology, LLC
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, version 2.
11 #include <linux/config.h>
12 #include <linux/kernel.h>
13 #include <linux/pagemap.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
17 #include <linux/init.h>
18 #include <linux/string.h>
19 #include <linux/security.h>
20 #include <asm/uaccess.h>
21 #include <asm/semaphore.h>
23 /* selinuxfs pseudo filesystem for exporting the security policy API.
24 Based on the proc code and the fs/nfsd/nfsctl.c code. */
31 #include "conditional.h"
33 static DECLARE_MUTEX(sel_sem);
35 /* global data for booleans */
36 static struct dentry *bool_dir = NULL;
37 static int bool_num = 0;
38 static int *bool_pending_values = NULL;
40 extern void selnl_notify_setenforce(int val);
42 /* Check whether a task is allowed to use a security operation. */
43 int task_has_security(struct task_struct *tsk,
46 struct task_security_struct *tsec;
52 return avc_has_perm(tsec->sid, SECINITSID_SECURITY,
53 SECCLASS_SECURITY, perms, NULL, NULL);
58 SEL_LOAD, /* load policy */
59 SEL_ENFORCE, /* get or set enforcing status */
60 SEL_CONTEXT, /* validate context */
61 SEL_ACCESS, /* compute access decision */
62 SEL_CREATE, /* compute create labeling decision */
63 SEL_RELABEL, /* compute relabeling decision */
64 SEL_USER, /* compute reachable user contexts */
65 SEL_POLICYVERS, /* return policy version for this kernel */
66 SEL_COMMIT_BOOLS, /* commit new boolean values */
67 SEL_MLS, /* return if MLS policy is enabled */
68 SEL_DISABLE /* disable SELinux until next reboot */
71 static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
72 size_t count, loff_t *ppos)
78 if (count < 0 || count > PAGE_SIZE)
80 if (!(page = (char*)__get_free_page(GFP_KERNEL)))
82 memset(page, 0, PAGE_SIZE);
84 length = scnprintf(page, PAGE_SIZE, "%d", selinux_enforcing);
86 free_page((unsigned long)page);
90 if (*ppos >= length) {
91 free_page((unsigned long)page);
94 if (count + *ppos > length)
95 count = length - *ppos;
97 if (copy_to_user(buf, (char *) page + *ppos, count)) {
103 free_page((unsigned long)page);
107 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
108 static ssize_t sel_write_enforce(struct file * file, const char __user * buf,
109 size_t count, loff_t *ppos)
116 if (count < 0 || count >= PAGE_SIZE)
119 /* No partial writes. */
122 page = (char*)__get_free_page(GFP_KERNEL);
125 memset(page, 0, PAGE_SIZE);
127 if (copy_from_user(page, buf, count))
131 if (sscanf(page, "%d", &new_value) != 1)
134 if (new_value != selinux_enforcing) {
135 length = task_has_security(current, SECURITY__SETENFORCE);
138 selinux_enforcing = new_value;
139 if (selinux_enforcing)
141 selnl_notify_setenforce(selinux_enforcing);
145 free_page((unsigned long) page);
149 #define sel_write_enforce NULL
152 static struct file_operations sel_enforce_ops = {
153 .read = sel_read_enforce,
154 .write = sel_write_enforce,
157 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
158 static ssize_t sel_write_disable(struct file * file, const char __user * buf,
159 size_t count, loff_t *ppos)
165 extern int selinux_disable(void);
167 if (count < 0 || count >= PAGE_SIZE)
170 /* No partial writes. */
173 page = (char*)__get_free_page(GFP_KERNEL);
176 memset(page, 0, PAGE_SIZE);
178 if (copy_from_user(page, buf, count))
182 if (sscanf(page, "%d", &new_value) != 1)
186 length = selinux_disable();
193 free_page((unsigned long) page);
197 #define sel_write_disable NULL
200 static struct file_operations sel_disable_ops = {
201 .write = sel_write_disable,
204 static ssize_t sel_read_policyvers(struct file *filp, char __user *buf,
205 size_t count, loff_t *ppos)
211 if (count < 0 || count > PAGE_SIZE)
213 if (!(page = (char*)__get_free_page(GFP_KERNEL)))
215 memset(page, 0, PAGE_SIZE);
217 length = scnprintf(page, PAGE_SIZE, "%u", POLICYDB_VERSION_MAX);
219 free_page((unsigned long)page);
223 if (*ppos >= length) {
224 free_page((unsigned long)page);
227 if (count + *ppos > length)
228 count = length - *ppos;
230 if (copy_to_user(buf, (char *) page + *ppos, count)) {
236 free_page((unsigned long)page);
240 static struct file_operations sel_policyvers_ops = {
241 .read = sel_read_policyvers,
244 /* declaration for sel_write_load */
245 static int sel_make_bools(void);
247 static ssize_t sel_read_mls(struct file *filp, char __user *buf,
248 size_t count, loff_t *ppos)
254 if (count < 0 || count > PAGE_SIZE)
256 if (!(page = (char*)__get_free_page(GFP_KERNEL)))
258 memset(page, 0, PAGE_SIZE);
260 length = scnprintf(page, PAGE_SIZE, "%d", selinux_mls_enabled);
262 free_page((unsigned long)page);
266 if (*ppos >= length) {
267 free_page((unsigned long)page);
270 if (count + *ppos > length)
271 count = length - *ppos;
273 if (copy_to_user(buf, (char *) page + *ppos, count)) {
279 free_page((unsigned long)page);
283 static struct file_operations sel_mls_ops = {
284 .read = sel_read_mls,
287 static ssize_t sel_write_load(struct file * file, const char __user * buf,
288 size_t count, loff_t *ppos)
297 length = task_has_security(current, SECURITY__LOAD_POLICY);
302 /* No partial writes. */
307 if ((count < 0) || (count > 64 * 1024 * 1024)
308 || (data = vmalloc(count)) == NULL) {
314 if (copy_from_user(data, buf, count) != 0)
317 length = security_load_policy(data, count);
321 ret = sel_make_bools();
332 static struct file_operations sel_load_ops = {
333 .write = sel_write_load,
337 static ssize_t sel_write_context(struct file * file, const char __user * buf,
338 size_t count, loff_t *ppos)
345 length = task_has_security(current, SECURITY__CHECK_CONTEXT);
349 if (count < 0 || count >= PAGE_SIZE)
352 /* No partial writes. */
355 page = (char*)__get_free_page(GFP_KERNEL);
358 memset(page, 0, PAGE_SIZE);
360 if (copy_from_user(page, buf, count))
363 length = security_context_to_sid(page, count, &sid);
369 free_page((unsigned long) page);
373 static struct file_operations sel_context_ops = {
374 .write = sel_write_context,
379 * Remaining nodes use transaction based IO methods like nfsd/nfsctl.c
381 static ssize_t sel_write_access(struct file * file, char *buf, size_t size);
382 static ssize_t sel_write_create(struct file * file, char *buf, size_t size);
383 static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size);
384 static ssize_t sel_write_user(struct file * file, char *buf, size_t size);
386 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
387 [SEL_ACCESS] = sel_write_access,
388 [SEL_CREATE] = sel_write_create,
389 [SEL_RELABEL] = sel_write_relabel,
390 [SEL_USER] = sel_write_user,
393 /* an argresp is stored in an allocated page and holds the
394 * size of the argument or response, along with its content
401 #define PAYLOAD_SIZE (PAGE_SIZE - sizeof(struct argresp))
404 * transaction based IO methods.
405 * The file expects a single write which triggers the transaction, and then
406 * possibly a read which collects the result - which is stored in a
409 static ssize_t TA_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
411 ino_t ino = file->f_dentry->d_inode->i_ino;
415 if (ino >= sizeof(write_op)/sizeof(write_op[0]) || !write_op[ino])
417 if (file->private_data)
418 return -EINVAL; /* only one write allowed per open */
419 if (size > PAYLOAD_SIZE - 1) /* allow one byte for null terminator */
422 ar = kmalloc(PAGE_SIZE, GFP_KERNEL);
425 memset(ar, 0, PAGE_SIZE); /* clear buffer, particularly last byte */
427 down(&file->f_dentry->d_inode->i_sem);
428 if (file->private_data)
431 file->private_data = ar;
432 up(&file->f_dentry->d_inode->i_sem);
437 if (copy_from_user(ar->data, buf, size))
440 rv = write_op[ino](file, ar->data, size);
448 static ssize_t TA_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
453 if (file->private_data == NULL)
454 rv = TA_write(file, buf, 0, pos);
458 ar = file->private_data;
461 if (*pos >= ar->size)
463 if (*pos + size > ar->size)
464 size = ar->size - *pos;
465 if (copy_to_user(buf, ar->data + *pos, size))
471 static int TA_open(struct inode *inode, struct file *file)
473 file->private_data = NULL;
477 static int TA_release(struct inode *inode, struct file *file)
479 void *p = file->private_data;
480 file->private_data = NULL;
485 static struct file_operations transaction_ops = {
489 .release = TA_release,
493 * payload - write methods
494 * If the method has a response, the response should be put in buf,
495 * and the length returned. Otherwise return 0 or and -error.
498 static ssize_t sel_write_access(struct file * file, char *buf, size_t size)
504 struct av_decision avd;
507 length = task_has_security(current, SECURITY__COMPUTE_AV);
512 scon = kmalloc(size+1, GFP_KERNEL);
515 memset(scon, 0, size+1);
517 tcon = kmalloc(size+1, GFP_KERNEL);
520 memset(tcon, 0, size+1);
523 if (sscanf(buf, "%s %s %hu %x", scon, tcon, &tclass, &req) != 4)
526 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
529 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
533 length = security_compute_av(ssid, tsid, tclass, req, &avd);
537 length = scnprintf(buf, PAYLOAD_SIZE, "%x %x %x %x %u",
538 avd.allowed, avd.decided,
539 avd.auditallow, avd.auditdeny,
548 static ssize_t sel_write_create(struct file * file, char *buf, size_t size)
551 u32 ssid, tsid, newsid;
557 length = task_has_security(current, SECURITY__COMPUTE_CREATE);
562 scon = kmalloc(size+1, GFP_KERNEL);
565 memset(scon, 0, size+1);
567 tcon = kmalloc(size+1, GFP_KERNEL);
570 memset(tcon, 0, size+1);
573 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
576 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
579 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
583 length = security_transition_sid(ssid, tsid, tclass, &newsid);
587 length = security_sid_to_context(newsid, &newcon, &len);
591 if (len > PAYLOAD_SIZE) {
592 printk(KERN_ERR "%s: context size (%u) exceeds payload "
593 "max\n", __FUNCTION__, len);
598 memcpy(buf, newcon, len);
609 static ssize_t sel_write_relabel(struct file * file, char *buf, size_t size)
612 u32 ssid, tsid, newsid;
618 length = task_has_security(current, SECURITY__COMPUTE_RELABEL);
623 scon = kmalloc(size+1, GFP_KERNEL);
626 memset(scon, 0, size+1);
628 tcon = kmalloc(size+1, GFP_KERNEL);
631 memset(tcon, 0, size+1);
634 if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
637 length = security_context_to_sid(scon, strlen(scon)+1, &ssid);
640 length = security_context_to_sid(tcon, strlen(tcon)+1, &tsid);
644 length = security_change_sid(ssid, tsid, tclass, &newsid);
648 length = security_sid_to_context(newsid, &newcon, &len);
652 if (len > PAYLOAD_SIZE) {
657 memcpy(buf, newcon, len);
668 static ssize_t sel_write_user(struct file * file, char *buf, size_t size)
670 char *con, *user, *ptr;
677 length = task_has_security(current, SECURITY__COMPUTE_USER);
682 con = kmalloc(size+1, GFP_KERNEL);
685 memset(con, 0, size+1);
687 user = kmalloc(size+1, GFP_KERNEL);
690 memset(user, 0, size+1);
693 if (sscanf(buf, "%s %s", con, user) != 2)
696 length = security_context_to_sid(con, strlen(con)+1, &sid);
700 length = security_get_user_sids(sid, user, &sids, &nsids);
704 length = sprintf(buf, "%u", nsids) + 1;
706 for (i = 0; i < nsids; i++) {
707 rc = security_sid_to_context(sids[i], &newcon, &len);
712 if ((length + len) >= PAYLOAD_SIZE) {
717 memcpy(ptr, newcon, len);
731 static struct inode *sel_make_inode(struct super_block *sb, int mode)
733 struct inode *ret = new_inode(sb);
737 ret->i_uid = ret->i_gid = 0;
738 ret->i_blksize = PAGE_CACHE_SIZE;
740 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
745 #define BOOL_INO_OFFSET 30
747 static ssize_t sel_read_bool(struct file *filep, char __user *buf,
748 size_t count, loff_t *ppos)
761 /* check to see if this file has been deleted */
765 if (count < 0 || count > PAGE_SIZE) {
769 if (!(page = (char*)__get_free_page(GFP_KERNEL))) {
773 memset(page, 0, PAGE_SIZE);
775 inode = filep->f_dentry->d_inode;
776 cur_enforcing = security_get_bool_value(inode->i_ino - BOOL_INO_OFFSET);
777 if (cur_enforcing < 0) {
782 length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
783 bool_pending_values[inode->i_ino - BOOL_INO_OFFSET]);
789 if (*ppos >= length) {
793 if (count + *ppos > length)
794 count = length - *ppos;
796 if (copy_to_user(buf, (char *) page + *ppos, count)) {
805 free_page((unsigned long)page);
809 static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
810 size_t count, loff_t *ppos)
813 ssize_t length = -EFAULT;
819 length = task_has_security(current, SECURITY__SETBOOL);
823 /* check to see if this file has been deleted */
827 if (count < 0 || count >= PAGE_SIZE) {
832 /* No partial writes. */
835 page = (char*)__get_free_page(GFP_KERNEL);
840 memset(page, 0, PAGE_SIZE);
842 if (copy_from_user(page, buf, count))
846 if (sscanf(page, "%d", &new_value) != 1)
852 inode = filep->f_dentry->d_inode;
853 bool_pending_values[inode->i_ino - BOOL_INO_OFFSET] = new_value;
859 free_page((unsigned long) page);
863 static struct file_operations sel_bool_ops = {
864 .read = sel_read_bool,
865 .write = sel_write_bool,
868 static ssize_t sel_commit_bools_write(struct file *filep,
869 const char __user *buf,
870 size_t count, loff_t *ppos)
873 ssize_t length = -EFAULT;
878 length = task_has_security(current, SECURITY__SETBOOL);
882 /* check to see if this file has been deleted */
886 if (count < 0 || count >= PAGE_SIZE) {
891 /* No partial writes. */
894 page = (char*)__get_free_page(GFP_KERNEL);
900 memset(page, 0, PAGE_SIZE);
902 if (copy_from_user(page, buf, count))
906 if (sscanf(page, "%d", &new_value) != 1)
910 security_set_bools(bool_num, bool_pending_values);
918 free_page((unsigned long) page);
922 static struct file_operations sel_commit_bools_ops = {
923 .write = sel_commit_bools_write,
926 /* delete booleans - partial revoke() from
927 * fs/proc/generic.c proc_kill_inodes */
928 static void sel_remove_bools(struct dentry *de)
930 struct list_head *p, *node;
931 struct super_block *sb = de->d_sb;
933 spin_lock(&dcache_lock);
934 node = de->d_subdirs.next;
935 while (node != &de->d_subdirs) {
936 struct dentry *d = list_entry(node, struct dentry, d_child);
941 spin_unlock(&dcache_lock);
943 simple_unlink(de->d_inode, d);
945 spin_lock(&dcache_lock);
947 node = de->d_subdirs.next;
950 spin_unlock(&dcache_lock);
953 list_for_each(p, &sb->s_files) {
954 struct file * filp = list_entry(p, struct file, f_list);
955 struct dentry * dentry = filp->f_dentry;
957 if (dentry->d_parent != de) {
965 #define BOOL_DIR_NAME "booleans"
967 static int sel_make_bools(void)
971 struct dentry *dentry = NULL;
972 struct dentry *dir = bool_dir;
973 struct inode *inode = NULL;
974 struct inode_security_struct *isec;
976 char **names = NULL, *page;
981 /* remove any existing files */
982 if (bool_pending_values)
983 kfree(bool_pending_values);
985 sel_remove_bools(dir);
987 if (!(page = (char*)__get_free_page(GFP_KERNEL)))
989 memset(page, 0, PAGE_SIZE);
991 ret = security_get_bools(&num, &names, &values);
995 for (i = 0; i < num; i++) {
996 qname.name = names[i];
997 qname.len = strlen(qname.name);
998 qname.hash = full_name_hash(qname.name, qname.len);
999 dentry = d_alloc(dir, &qname);
1004 inode = sel_make_inode(dir->d_sb, S_IFREG | S_IRUGO | S_IWUSR);
1010 len = snprintf(page, PAGE_SIZE, "/%s/%s", BOOL_DIR_NAME, names[i]);
1014 } else if (len >= PAGE_SIZE) {
1015 ret = -ENAMETOOLONG;
1018 isec = (struct inode_security_struct*)inode->i_security;
1019 if ((ret = security_genfs_sid("selinuxfs", page, SECCLASS_FILE, &sid)))
1022 isec->initialized = 1;
1023 inode->i_fop = &sel_bool_ops;
1024 inode->i_ino = i + BOOL_INO_OFFSET;
1025 d_add(dentry, inode);
1028 bool_pending_values = values;
1030 free_page((unsigned long)page);
1032 for (i = 0; i < num; i++) {
1045 static int sel_fill_super(struct super_block * sb, void * data, int silent)
1048 struct dentry *dentry;
1049 struct inode *inode;
1052 static struct tree_descr selinux_files[] = {
1053 [SEL_LOAD] = {"load", &sel_load_ops, S_IRUSR|S_IWUSR},
1054 [SEL_ENFORCE] = {"enforce", &sel_enforce_ops, S_IRUGO|S_IWUSR},
1055 [SEL_CONTEXT] = {"context", &sel_context_ops, S_IRUGO|S_IWUGO},
1056 [SEL_ACCESS] = {"access", &transaction_ops, S_IRUGO|S_IWUGO},
1057 [SEL_CREATE] = {"create", &transaction_ops, S_IRUGO|S_IWUGO},
1058 [SEL_RELABEL] = {"relabel", &transaction_ops, S_IRUGO|S_IWUGO},
1059 [SEL_USER] = {"user", &transaction_ops, S_IRUGO|S_IWUGO},
1060 [SEL_POLICYVERS] = {"policyvers", &sel_policyvers_ops, S_IRUGO},
1061 [SEL_COMMIT_BOOLS] = {"commit_pending_bools", &sel_commit_bools_ops, S_IWUSR},
1062 [SEL_MLS] = {"mls", &sel_mls_ops, S_IRUGO},
1063 [SEL_DISABLE] = {"disable", &sel_disable_ops, S_IWUSR},
1066 ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
1070 qname.name = BOOL_DIR_NAME;
1071 qname.len = strlen(qname.name);
1072 qname.hash = full_name_hash(qname.name, qname.len);
1073 dentry = d_alloc(sb->s_root, &qname);
1077 inode = sel_make_inode(sb, S_IFDIR | S_IRUGO | S_IXUGO);
1080 inode->i_op = &simple_dir_inode_operations;
1081 inode->i_fop = &simple_dir_operations;
1082 d_add(dentry, inode);
1084 ret = sel_make_bools();
1091 printk(KERN_ERR "security: error creating conditional out_dput\n");
1095 static struct super_block *sel_get_sb(struct file_system_type *fs_type,
1096 int flags, const char *dev_name, void *data)
1098 return get_sb_single(fs_type, flags, data, sel_fill_super);
1101 static struct file_system_type sel_fs_type = {
1102 .name = "selinuxfs",
1103 .get_sb = sel_get_sb,
1104 .kill_sb = kill_litter_super,
1107 static int __init init_sel_fs(void)
1109 return selinux_enabled ? register_filesystem(&sel_fs_type) : 0;
1112 __initcall(init_sel_fs);
1114 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
1115 void exit_sel_fs(void)
1117 unregister_filesystem(&sel_fs_type);