17 #include <linux/kernel.h>
19 #include <linux/slab.h>
24 #include <linux/string.h>
29 #include <linux/audit.h>
32 #include <linux/ctype.h>
42 #include "conditional.h"
45 static char *policycap_names[] = {
46 "network_peer_controls",
52 static int __init checkreqprot_setup(
char *
str)
54 unsigned long checkreqprot;
59 __setup(
"checkreqprot=", checkreqprot_setup);
64 static struct dentry *bool_dir;
66 static char **bool_pending_names;
67 static int *bool_pending_values;
71 static unsigned long last_class_ino;
73 static char policy_opened;
76 static struct dentry *policycap_dir;
79 static int task_has_security(
struct task_struct *tsk,
93 return avc_has_perm(sid, SECINITSID_SECURITY,
94 SECCLASS_SECURITY, perms,
NULL);
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)
139 #ifdef CONFIG_SECURITY_SELINUX_DEVELOP
141 size_t count, loff_t *ppos)
167 if (
sscanf(page,
"%d", &new_value) != 1)
171 length = task_has_security(
current, SECURITY__SETENFORCE);
175 "enforcing=%d old_enforcing=%d auid=%u ses=%u",
191 #define sel_write_enforce NULL
195 .read = sel_read_enforce,
200 static ssize_t sel_read_handle_unknown(
struct file *filp,
char __user *buf,
201 size_t count, loff_t *ppos)
214 .read = sel_read_handle_unknown,
218 static int sel_open_handle_status(
struct inode *
inode,
struct file *filp)
230 static ssize_t sel_read_handle_status(
struct file *filp,
char __user *buf,
231 size_t count, loff_t *ppos)
242 static int sel_mmap_handle_status(
struct file *filp,
265 .open = sel_open_handle_status,
266 .read = sel_read_handle_status,
267 .mmap = sel_mmap_handle_status,
271 #ifdef CONFIG_SECURITY_SELINUX_DISABLE
273 size_t count, loff_t *ppos)
299 if (
sscanf(page,
"%d", &new_value) != 1)
307 "selinux=0 auid=%u ses=%u",
318 #define sel_write_disable NULL
326 static ssize_t sel_read_policyvers(
struct file *filp,
char __user *buf,
327 size_t count, loff_t *ppos)
337 .read = sel_read_policyvers,
342 static int sel_make_bools(
void);
343 static int sel_make_classes(
void);
344 static int sel_make_policycap(
void);
350 static ssize_t sel_read_mls(
struct file *filp,
char __user *buf,
351 size_t count, loff_t *ppos)
362 .read = sel_read_mls,
371 static int sel_open_policy(
struct inode *inode,
struct file *filp)
380 rc = task_has_security(
current, SECURITY__READ_POLICY);
419 static int sel_release_policy(
struct inode *inode,
struct file *filp)
433 static ssize_t sel_read_policy(
struct file *filp,
char __user *buf,
434 size_t count, loff_t *ppos)
441 ret = task_has_security(
current, SECURITY__READ_POLICY);
452 struct vm_fault *vmf)
458 if (vmf->flags & (FAULT_FLAG_MKWRITE | FAULT_FLAG_WRITE))
459 return VM_FAULT_SIGBUS;
463 return VM_FAULT_SIGBUS;
473 static struct vm_operations_struct sel_mmap_policy_ops = {
474 .fault = sel_mmap_policy_fault,
475 .page_mkwrite = sel_mmap_policy_fault,
478 static int sel_mmap_policy(
struct file *filp,
struct vm_area_struct *vma)
488 vma->
vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
489 vma->
vm_ops = &sel_mmap_policy_ops;
495 .open = sel_open_policy,
496 .read = sel_read_policy,
497 .mmap = sel_mmap_policy,
498 .release = sel_release_policy,
502 static ssize_t sel_write_load(
struct file *file,
const char __user *buf,
503 size_t count, loff_t *ppos)
511 length = task_has_security(
current, SECURITY__LOAD_POLICY);
521 if (count > 64 * 1024 * 1024)
537 length = sel_make_bools();
541 length = sel_make_classes();
545 length = sel_make_policycap();
553 "policy loaded auid=%u ses=%u",
563 .write = sel_write_load,
567 static ssize_t sel_write_context(
struct file *file,
char *buf,
size_t size)
573 length = task_has_security(
current, SECURITY__CHECK_CONTEXT);
588 "payload max\n", __func__, len);
599 static ssize_t sel_read_checkreqprot(
struct file *filp,
char __user *buf,
600 size_t count, loff_t *ppos)
609 static ssize_t sel_write_checkreqprot(
struct file *file,
const char __user *buf,
610 size_t count, loff_t *ppos)
614 unsigned int new_value;
616 length = task_has_security(
current, SECURITY__SETCHECKREQPROT);
639 if (
sscanf(page,
"%u", &new_value) != 1)
649 .read = sel_read_checkreqprot,
650 .write = sel_write_checkreqprot,
657 static ssize_t sel_write_access(
struct file *file,
char *buf,
size_t size);
658 static ssize_t sel_write_create(
struct file *file,
char *buf,
size_t size);
659 static ssize_t sel_write_relabel(
struct file *file,
char *buf,
size_t size);
660 static ssize_t sel_write_user(
struct file *file,
char *buf,
size_t size);
661 static ssize_t sel_write_member(
struct file *file,
char *buf,
size_t size);
663 static ssize_t (*write_op[])(
struct file *,
char *,
size_t) = {
672 static ssize_t selinux_transaction_write(
struct file *file,
const char __user *buf,
size_t size, loff_t *
pos)
678 if (ino >=
ARRAY_SIZE(write_op) || !write_op[ino])
683 return PTR_ERR(data);
694 .write = selinux_transaction_write,
706 static ssize_t sel_write_access(
struct file *file,
char *buf,
size_t size)
714 length = task_has_security(
current, SECURITY__COMPUTE_AV);
729 if (
sscanf(buf,
"%s %s %hu", scon, tcon, &tclass) != 3)
744 avd.allowed, 0xffffffff,
745 avd.auditallow, avd.auditdeny,
746 avd.seqno, avd.flags);
753 static ssize_t sel_write_create(
struct file *file,
char *buf,
size_t size)
756 char *namebuf =
NULL, *objname =
NULL;
764 length = task_has_security(
current, SECURITY__COMPUTE_CREATE);
784 nargs =
sscanf(buf,
"%s %s %hu %s", scon, tcon, &tclass, namebuf);
785 if (nargs < 3 || nargs > 4)
803 else if (c1 ==
'%') {
813 }
while (c1 !=
'\0');
838 "payload max\n", __func__, len);
852 static ssize_t sel_write_relabel(
struct file *file,
char *buf,
size_t size)
861 length = task_has_security(
current, SECURITY__COMPUTE_RELABEL);
876 if (
sscanf(buf,
"%s %s %hu", scon, tcon, &tclass) != 3)
908 static ssize_t sel_write_user(
struct file *file,
char *buf,
size_t size)
917 length = task_has_security(
current, SECURITY__COMPUTE_USER);
932 if (
sscanf(buf,
"%s %s", con, user) != 2)
943 length =
sprintf(buf,
"%u", nsids) + 1;
945 for (i = 0; i < nsids; i++) {
968 static ssize_t sel_write_member(
struct file *file,
char *buf,
size_t size)
977 length = task_has_security(
current, SECURITY__COMPUTE_MEMBER);
992 if (
sscanf(buf,
"%s %s %hu", scon, tcon, &tclass) != 3)
1014 "payload max\n", __func__, len);
1018 memcpy(buf, newcon, len);
1038 static ssize_t sel_read_bool(
struct file *filep,
char __user *buf,
1039 size_t count, loff_t *ppos)
1045 struct inode *inode = filep->
f_path.dentry->d_inode;
1047 const char *
name = filep->
f_path.dentry->d_name.name;
1052 if (index >= bool_num ||
strcmp(name, bool_pending_names[index]))
1061 if (cur_enforcing < 0) {
1062 ret = cur_enforcing;
1066 bool_pending_values[index]);
1074 static ssize_t sel_write_bool(
struct file *filep,
const char __user *buf,
1075 size_t count, loff_t *ppos)
1080 struct inode *inode = filep->
f_path.dentry->d_inode;
1082 const char *name = filep->
f_path.dentry->d_name.name;
1086 length = task_has_security(
current, SECURITY__SETBOOL);
1091 if (index >= bool_num ||
strcmp(name, bool_pending_names[index]))
1113 if (
sscanf(page,
"%d", &new_value) != 1)
1119 bool_pending_values[
index] = new_value;
1129 .read = sel_read_bool,
1130 .write = sel_write_bool,
1134 static ssize_t sel_commit_bools_write(
struct file *filep,
1135 const char __user *buf,
1136 size_t count, loff_t *ppos)
1144 length = task_has_security(
current, SECURITY__SETBOOL);
1167 if (
sscanf(page,
"%d", &new_value) != 1)
1171 if (new_value && bool_pending_values)
1184 .write = sel_commit_bools_write,
1188 static void sel_remove_entries(
struct dentry *de)
1198 list_del_init(node);
1202 spin_unlock(&de->
d_lock);
1213 spin_unlock(&de->
d_lock);
1216 #define BOOL_DIR_NAME "booleans"
1218 static int sel_make_bools(
void)
1223 struct dentry *
dir = bool_dir;
1224 struct inode *inode =
NULL;
1232 for (i = 0; i < bool_num; i++)
1233 kfree(bool_pending_names[i]);
1234 kfree(bool_pending_names);
1235 kfree(bool_pending_values);
1237 bool_pending_names =
NULL;
1238 bool_pending_values =
NULL;
1240 sel_remove_entries(dir);
1251 for (i = 0; i < num; i++) {
1274 inode->
i_fop = &sel_bool_ops;
1276 d_add(dentry, inode);
1279 bool_pending_names = names;
1280 bool_pending_values =
values;
1288 for (i = 0; i < num; i++)
1293 sel_remove_entries(dir);
1298 #define NULL_FILE_NAME "null"
1302 static ssize_t sel_read_avc_cache_threshold(
struct file *filp,
char __user *buf,
1303 size_t count, loff_t *ppos)
1312 static ssize_t sel_write_avc_cache_threshold(
struct file *file,
1313 const char __user *buf,
1314 size_t count, loff_t *ppos)
1321 ret = task_has_security(
current, SECURITY__SETSECPARAM);
1344 if (
sscanf(page,
"%u", &new_value) != 1)
1355 static ssize_t sel_read_avc_hash_stats(
struct file *filp,
char __user *buf,
1356 size_t count, loff_t *ppos)
1374 .read = sel_read_avc_cache_threshold,
1375 .write = sel_write_avc_cache_threshold,
1380 .read = sel_read_avc_hash_stats,
1384 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1398 static void *sel_avc_stats_seq_start(
struct seq_file *seq, loff_t *pos)
1400 loff_t
n = *pos - 1;
1405 return sel_avc_get_stat_idx(&n);
1408 static void *sel_avc_stats_seq_next(
struct seq_file *seq,
void *
v, loff_t *pos)
1410 return sel_avc_get_stat_idx(pos);
1413 static int sel_avc_stats_seq_show(
struct seq_file *seq,
void *
v)
1418 seq_printf(seq,
"lookups hits misses allocations reclaims "
1423 unsigned int hits = lookups -
misses;
1424 seq_printf(seq,
"%u %u %u %u %u %u\n", lookups,
1431 static void sel_avc_stats_seq_stop(
struct seq_file *seq,
void *v)
1434 static const struct seq_operations sel_avc_cache_stats_seq_ops = {
1435 .
start = sel_avc_stats_seq_start,
1436 .next = sel_avc_stats_seq_next,
1437 .show = sel_avc_stats_seq_show,
1438 .stop = sel_avc_stats_seq_stop,
1441 static int sel_open_avc_cache_stats(
struct inode *inode,
struct file *file)
1443 return seq_open(file, &sel_avc_cache_stats_seq_ops);
1447 .
open = sel_open_avc_cache_stats,
1454 static int sel_make_avc_files(
struct dentry *dir)
1458 {
"cache_threshold",
1460 {
"hash_stats", &sel_avc_hash_stats_ops,
S_IRUGO },
1461 #ifdef CONFIG_SECURITY_SELINUX_AVC_STATS
1462 {
"cache_stats", &sel_avc_cache_stats_ops,
S_IRUGO },
1467 struct inode *
inode;
1479 inode->
i_ino = ++sel_last_ino;
1480 d_add(dentry, inode);
1486 static ssize_t sel_read_initcon(
struct file *file,
char __user *buf,
1487 size_t count, loff_t *ppos)
1489 struct inode *
inode;
1494 inode = file->
f_path.dentry->d_inode;
1506 .read = sel_read_initcon,
1510 static int sel_make_initcon_files(
struct dentry *dir)
1514 for (i = 1; i <= SECINITSID_NUM; i++) {
1515 struct inode *
inode;
1516 struct dentry *dentry;
1525 inode->
i_fop = &sel_initcon_ops;
1527 d_add(dentry, inode);
1533 static inline unsigned long sel_class_to_ino(
u16 class)
1538 static inline u16 sel_ino_to_class(
unsigned long ino)
1543 static inline unsigned long sel_perm_to_ino(
u16 class,
u32 perm)
1548 static inline u32 sel_ino_to_perm(
unsigned long ino)
1553 static ssize_t sel_read_class(
struct file *file,
char __user *buf,
1554 size_t count, loff_t *ppos)
1556 unsigned long ino = file->
f_path.dentry->d_inode->i_ino;
1558 ssize_t len =
snprintf(res,
sizeof(res),
"%d", sel_ino_to_class(ino));
1563 .read = sel_read_class,
1567 static ssize_t sel_read_perm(
struct file *file,
char __user *buf,
1568 size_t count, loff_t *ppos)
1570 unsigned long ino = file->
f_path.dentry->d_inode->i_ino;
1577 .read = sel_read_perm,
1581 static ssize_t sel_read_policycap(
struct file *file,
char __user *buf,
1582 size_t count, loff_t *ppos)
1587 unsigned long i_ino = file->
f_path.dentry->d_inode->i_ino;
1596 .read = sel_read_policycap,
1600 static int sel_make_perm_files(
char *objclass,
int classvalue,
1610 for (i = 0; i < nperms; i++) {
1611 struct inode *
inode;
1612 struct dentry *dentry;
1624 inode->
i_fop = &sel_perm_ops;
1626 inode->
i_ino = sel_perm_to_ino(classvalue, i + 1);
1627 d_add(dentry, inode);
1631 for (i = 0; i < nperms; i++)
1637 static int sel_make_class_dir_entries(
char *classname,
int index,
1640 struct dentry *dentry =
NULL;
1641 struct inode *inode =
NULL;
1652 inode->
i_fop = &sel_class_ops;
1653 inode->
i_ino = sel_class_to_ino(index);
1654 d_add(dentry, inode);
1656 dentry = sel_make_dir(dir,
"perms", &last_class_ino);
1658 return PTR_ERR(dentry);
1660 rc = sel_make_perm_files(classname, index, dentry);
1665 static void sel_remove_classes(
void)
1670 struct dentry *class_subdir =
list_entry(class_node,
1671 struct dentry,
d_u.d_child);
1676 struct dentry,
d_u.d_child);
1680 sel_remove_entries(d);
1683 sel_remove_entries(class_subdir);
1686 sel_remove_entries(class_dir);
1689 static int sel_make_classes(
void)
1691 int rc, nclasses,
i;
1695 sel_remove_classes();
1702 last_class_ino = sel_class_to_ino(nclasses + 2);
1704 for (i = 0; i < nclasses; i++) {
1705 struct dentry *class_name_dir;
1707 class_name_dir = sel_make_dir(class_dir, classes[i],
1709 if (IS_ERR(class_name_dir)) {
1710 rc = PTR_ERR(class_name_dir);
1715 rc = sel_make_class_dir_entries(classes[i], i + 1,
1722 for (i = 0; i < nclasses; i++)
1728 static int sel_make_policycap(
void)
1731 struct dentry *dentry =
NULL;
1732 struct inode *inode =
NULL;
1734 sel_remove_entries(policycap_dir);
1739 policycap_names[iter]);
1750 inode->
i_fop = &sel_policycap_ops;
1752 d_add(dentry, inode);
1758 static struct dentry *sel_make_dir(
struct dentry *dir,
const char *name,
1762 struct inode *
inode;
1775 inode->
i_ino = ++(*ino);
1778 d_add(dentry, inode);
1785 static int sel_fill_super(
struct super_block *sb,
void *data,
int silent)
1788 struct dentry *dentry;
1789 struct inode *
inode;
1817 if (IS_ERR(bool_dir)) {
1818 ret = PTR_ERR(bool_dir);
1833 inode->
i_ino = ++sel_last_ino;
1835 isec->
sid = SECINITSID_DEVNULL;
1836 isec->
sclass = SECCLASS_CHR_FILE;
1840 d_add(dentry, inode);
1843 dentry = sel_make_dir(sb->
s_root,
"avc", &sel_last_ino);
1844 if (IS_ERR(dentry)) {
1845 ret = PTR_ERR(dentry);
1849 ret = sel_make_avc_files(dentry);
1853 dentry = sel_make_dir(sb->
s_root,
"initial_contexts", &sel_last_ino);
1854 if (IS_ERR(dentry)) {
1855 ret = PTR_ERR(dentry);
1859 ret = sel_make_initcon_files(dentry);
1863 class_dir = sel_make_dir(sb->
s_root,
"class", &sel_last_ino);
1864 if (IS_ERR(class_dir)) {
1865 ret = PTR_ERR(class_dir);
1870 policycap_dir = sel_make_dir(sb->
s_root,
"policy_capabilities", &sel_last_ino);
1871 if (IS_ERR(policycap_dir)) {
1872 ret = PTR_ERR(policycap_dir);
1873 policycap_dir =
NULL;
1884 int flags,
const char *dev_name,
void *data)
1886 return mount_single(fs_type, flags, data, sel_fill_super);
1890 .name =
"selinuxfs",
1896 static struct kobject *selinuxfs_kobj;
1898 static int __init init_sel_fs(
void)
1906 if (!selinuxfs_kobj)
1916 if (IS_ERR(selinuxfs_mount)) {
1918 err = PTR_ERR(selinuxfs_mount);
1919 selinuxfs_mount =
NULL;
1927 #ifdef CONFIG_SECURITY_SELINUX_DISABLE