Commit 0619f0f5 authored by Stephen Smalley's avatar Stephen Smalley Committed by Paul Moore

selinux: wrap selinuxfs state

Move global selinuxfs state to a per-instance structure (selinux_fs_info),
and include a pointer to the selinux_state in this structure.
Pass this selinux_state to all security server operations, thereby
ensuring that each selinuxfs instance presents a view of and acts
as an interface to a particular selinux_state instance.

This change should have no effect on SELinux behavior or APIs
(userspace or LSM).  It merely wraps the selinuxfs global state,
links it to a particular selinux_state (currently always the single
global selinux_state) and uses that state for all operations.
Signed-off-by: default avatarStephen Smalley <sds@tycho.nsa.gov>
Signed-off-by: default avatarPaul Moore <paul@paul-moore.com>
parent 274f62e1
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/vmalloc.h> #include <linux/vmalloc.h>
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/mount.h>
#include <linux/mutex.h> #include <linux/mutex.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/string.h> #include <linux/string.h>
...@@ -41,23 +42,6 @@ ...@@ -41,23 +42,6 @@
#include "objsec.h" #include "objsec.h"
#include "conditional.h" #include "conditional.h"
static DEFINE_MUTEX(sel_mutex);
/* global data for booleans */
static struct dentry *bool_dir;
static int bool_num;
static char **bool_pending_names;
static int *bool_pending_values;
/* global data for classes */
static struct dentry *class_dir;
static unsigned long last_class_ino;
static char policy_opened;
/* global data for policy capabilities */
static struct dentry *policycap_dir;
enum sel_inos { enum sel_inos {
SEL_ROOT_INO = 2, SEL_ROOT_INO = 2,
SEL_LOAD, /* load policy */ SEL_LOAD, /* load policy */
...@@ -82,7 +66,51 @@ enum sel_inos { ...@@ -82,7 +66,51 @@ enum sel_inos {
SEL_INO_NEXT, /* The next inode number to use */ SEL_INO_NEXT, /* The next inode number to use */
}; };
static unsigned long sel_last_ino = SEL_INO_NEXT - 1; struct selinux_fs_info {
struct dentry *bool_dir;
unsigned int bool_num;
char **bool_pending_names;
unsigned int *bool_pending_values;
struct dentry *class_dir;
unsigned long last_class_ino;
bool policy_opened;
struct dentry *policycap_dir;
struct mutex mutex;
unsigned long last_ino;
struct selinux_state *state;
struct super_block *sb;
};
static int selinux_fs_info_create(struct super_block *sb)
{
struct selinux_fs_info *fsi;
fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
if (!fsi)
return -ENOMEM;
mutex_init(&fsi->mutex);
fsi->last_ino = SEL_INO_NEXT - 1;
fsi->state = &selinux_state;
fsi->sb = sb;
sb->s_fs_info = fsi;
return 0;
}
static void selinux_fs_info_free(struct super_block *sb)
{
struct selinux_fs_info *fsi = sb->s_fs_info;
int i;
if (fsi) {
for (i = 0; i < fsi->bool_num; i++)
kfree(fsi->bool_pending_names[i]);
kfree(fsi->bool_pending_names);
kfree(fsi->bool_pending_values);
}
kfree(sb->s_fs_info);
sb->s_fs_info = NULL;
}
#define SEL_INITCON_INO_OFFSET 0x01000000 #define SEL_INITCON_INO_OFFSET 0x01000000
#define SEL_BOOL_INO_OFFSET 0x02000000 #define SEL_BOOL_INO_OFFSET 0x02000000
...@@ -94,11 +122,12 @@ static unsigned long sel_last_ino = SEL_INO_NEXT - 1; ...@@ -94,11 +122,12 @@ static unsigned long sel_last_ino = SEL_INO_NEXT - 1;
static ssize_t sel_read_enforce(struct file *filp, char __user *buf, static ssize_t sel_read_enforce(struct file *filp, char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
char tmpbuf[TMPBUFLEN]; char tmpbuf[TMPBUFLEN];
ssize_t length; ssize_t length;
length = scnprintf(tmpbuf, TMPBUFLEN, "%d", length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
enforcing_enabled(&selinux_state)); enforcing_enabled(fsi->state));
return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
} }
...@@ -107,6 +136,8 @@ static ssize_t sel_write_enforce(struct file *file, const char __user *buf, ...@@ -107,6 +136,8 @@ static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
struct selinux_state *state = fsi->state;
char *page = NULL; char *page = NULL;
ssize_t length; ssize_t length;
int old_value, new_value; int old_value, new_value;
...@@ -128,8 +159,7 @@ static ssize_t sel_write_enforce(struct file *file, const char __user *buf, ...@@ -128,8 +159,7 @@ static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
new_value = !!new_value; new_value = !!new_value;
old_value = enforcing_enabled(&selinux_state); old_value = enforcing_enabled(state);
if (new_value != old_value) { if (new_value != old_value) {
length = avc_has_perm(current_sid(), SECINITSID_SECURITY, length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
SECCLASS_SECURITY, SECURITY__SETENFORCE, SECCLASS_SECURITY, SECURITY__SETENFORCE,
...@@ -141,12 +171,11 @@ static ssize_t sel_write_enforce(struct file *file, const char __user *buf, ...@@ -141,12 +171,11 @@ static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
new_value, old_value, new_value, old_value,
from_kuid(&init_user_ns, audit_get_loginuid(current)), from_kuid(&init_user_ns, audit_get_loginuid(current)),
audit_get_sessionid(current)); audit_get_sessionid(current));
enforcing_set(&selinux_state, new_value); enforcing_set(state, new_value);
if (new_value) if (new_value)
avc_ss_reset(0); avc_ss_reset(0);
selnl_notify_setenforce(new_value); selnl_notify_setenforce(new_value);
selinux_status_update_setenforce(&selinux_state, selinux_status_update_setenforce(state, new_value);
new_value);
if (!new_value) if (!new_value)
call_lsm_notifier(LSM_POLICY_CHANGE, NULL); call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
} }
...@@ -168,12 +197,14 @@ static const struct file_operations sel_enforce_ops = { ...@@ -168,12 +197,14 @@ static const struct file_operations sel_enforce_ops = {
static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf, static ssize_t sel_read_handle_unknown(struct file *filp, char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
struct selinux_state *state = fsi->state;
char tmpbuf[TMPBUFLEN]; char tmpbuf[TMPBUFLEN];
ssize_t length; ssize_t length;
ino_t ino = file_inode(filp)->i_ino; ino_t ino = file_inode(filp)->i_ino;
int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ? int handle_unknown = (ino == SEL_REJECT_UNKNOWN) ?
security_get_reject_unknown(&selinux_state) : security_get_reject_unknown(state) :
!security_get_allow_unknown(&selinux_state); !security_get_allow_unknown(state);
length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown); length = scnprintf(tmpbuf, TMPBUFLEN, "%d", handle_unknown);
return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
...@@ -186,7 +217,8 @@ static const struct file_operations sel_handle_unknown_ops = { ...@@ -186,7 +217,8 @@ static const struct file_operations sel_handle_unknown_ops = {
static int sel_open_handle_status(struct inode *inode, struct file *filp) static int sel_open_handle_status(struct inode *inode, struct file *filp)
{ {
struct page *status = selinux_kernel_status_page(&selinux_state); struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
struct page *status = selinux_kernel_status_page(fsi->state);
if (!status) if (!status)
return -ENOMEM; return -ENOMEM;
...@@ -242,6 +274,7 @@ static ssize_t sel_write_disable(struct file *file, const char __user *buf, ...@@ -242,6 +274,7 @@ static ssize_t sel_write_disable(struct file *file, const char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
char *page; char *page;
ssize_t length; ssize_t length;
int new_value; int new_value;
...@@ -262,7 +295,7 @@ static ssize_t sel_write_disable(struct file *file, const char __user *buf, ...@@ -262,7 +295,7 @@ static ssize_t sel_write_disable(struct file *file, const char __user *buf,
goto out; goto out;
if (new_value) { if (new_value) {
length = selinux_disable(&selinux_state); length = selinux_disable(fsi->state);
if (length) if (length)
goto out; goto out;
audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS, audit_log(current->audit_context, GFP_KERNEL, AUDIT_MAC_STATUS,
...@@ -301,9 +334,9 @@ static const struct file_operations sel_policyvers_ops = { ...@@ -301,9 +334,9 @@ static const struct file_operations sel_policyvers_ops = {
}; };
/* declaration for sel_write_load */ /* declaration for sel_write_load */
static int sel_make_bools(void); static int sel_make_bools(struct selinux_fs_info *fsi);
static int sel_make_classes(void); static int sel_make_classes(struct selinux_fs_info *fsi);
static int sel_make_policycap(void); static int sel_make_policycap(struct selinux_fs_info *fsi);
/* declaration for sel_make_class_dirs */ /* declaration for sel_make_class_dirs */
static struct dentry *sel_make_dir(struct dentry *dir, const char *name, static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
...@@ -312,11 +345,12 @@ static struct dentry *sel_make_dir(struct dentry *dir, const char *name, ...@@ -312,11 +345,12 @@ static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
static ssize_t sel_read_mls(struct file *filp, char __user *buf, static ssize_t sel_read_mls(struct file *filp, char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
char tmpbuf[TMPBUFLEN]; char tmpbuf[TMPBUFLEN];
ssize_t length; ssize_t length;
length = scnprintf(tmpbuf, TMPBUFLEN, "%d", length = scnprintf(tmpbuf, TMPBUFLEN, "%d",
security_mls_enabled(&selinux_state)); security_mls_enabled(fsi->state));
return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
} }
...@@ -332,12 +366,14 @@ struct policy_load_memory { ...@@ -332,12 +366,14 @@ struct policy_load_memory {
static int sel_open_policy(struct inode *inode, struct file *filp) static int sel_open_policy(struct inode *inode, struct file *filp)
{ {
struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
struct selinux_state *state = fsi->state;
struct policy_load_memory *plm = NULL; struct policy_load_memory *plm = NULL;
int rc; int rc;
BUG_ON(filp->private_data); BUG_ON(filp->private_data);
mutex_lock(&sel_mutex); mutex_lock(&fsi->mutex);
rc = avc_has_perm(current_sid(), SECINITSID_SECURITY, rc = avc_has_perm(current_sid(), SECINITSID_SECURITY,
SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL); SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
...@@ -345,7 +381,7 @@ static int sel_open_policy(struct inode *inode, struct file *filp) ...@@ -345,7 +381,7 @@ static int sel_open_policy(struct inode *inode, struct file *filp)
goto err; goto err;
rc = -EBUSY; rc = -EBUSY;
if (policy_opened) if (fsi->policy_opened)
goto err; goto err;
rc = -ENOMEM; rc = -ENOMEM;
...@@ -353,25 +389,25 @@ static int sel_open_policy(struct inode *inode, struct file *filp) ...@@ -353,25 +389,25 @@ static int sel_open_policy(struct inode *inode, struct file *filp)
if (!plm) if (!plm)
goto err; goto err;
if (i_size_read(inode) != security_policydb_len(&selinux_state)) { if (i_size_read(inode) != security_policydb_len(state)) {
inode_lock(inode); inode_lock(inode);
i_size_write(inode, security_policydb_len(&selinux_state)); i_size_write(inode, security_policydb_len(state));
inode_unlock(inode); inode_unlock(inode);
} }
rc = security_read_policy(&selinux_state, &plm->data, &plm->len); rc = security_read_policy(state, &plm->data, &plm->len);
if (rc) if (rc)
goto err; goto err;
policy_opened = 1; fsi->policy_opened = 1;
filp->private_data = plm; filp->private_data = plm;
mutex_unlock(&sel_mutex); mutex_unlock(&fsi->mutex);
return 0; return 0;
err: err:
mutex_unlock(&sel_mutex); mutex_unlock(&fsi->mutex);
if (plm) if (plm)
vfree(plm->data); vfree(plm->data);
...@@ -381,11 +417,12 @@ static int sel_open_policy(struct inode *inode, struct file *filp) ...@@ -381,11 +417,12 @@ static int sel_open_policy(struct inode *inode, struct file *filp)
static int sel_release_policy(struct inode *inode, struct file *filp) static int sel_release_policy(struct inode *inode, struct file *filp)
{ {
struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;
struct policy_load_memory *plm = filp->private_data; struct policy_load_memory *plm = filp->private_data;
BUG_ON(!plm); BUG_ON(!plm);
policy_opened = 0; fsi->policy_opened = 0;
vfree(plm->data); vfree(plm->data);
kfree(plm); kfree(plm);
...@@ -396,10 +433,11 @@ static int sel_release_policy(struct inode *inode, struct file *filp) ...@@ -396,10 +433,11 @@ static int sel_release_policy(struct inode *inode, struct file *filp)
static ssize_t sel_read_policy(struct file *filp, char __user *buf, static ssize_t sel_read_policy(struct file *filp, char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
struct policy_load_memory *plm = filp->private_data; struct policy_load_memory *plm = filp->private_data;
int ret; int ret;
mutex_lock(&sel_mutex); mutex_lock(&fsi->mutex);
ret = avc_has_perm(current_sid(), SECINITSID_SECURITY, ret = avc_has_perm(current_sid(), SECINITSID_SECURITY,
SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL); SECCLASS_SECURITY, SECURITY__READ_POLICY, NULL);
...@@ -408,7 +446,7 @@ static ssize_t sel_read_policy(struct file *filp, char __user *buf, ...@@ -408,7 +446,7 @@ static ssize_t sel_read_policy(struct file *filp, char __user *buf,
ret = simple_read_from_buffer(buf, count, ppos, plm->data, plm->len); ret = simple_read_from_buffer(buf, count, ppos, plm->data, plm->len);
out: out:
mutex_unlock(&sel_mutex); mutex_unlock(&fsi->mutex);
return ret; return ret;
} }
...@@ -462,14 +500,40 @@ static const struct file_operations sel_policy_ops = { ...@@ -462,14 +500,40 @@ static const struct file_operations sel_policy_ops = {
.llseek = generic_file_llseek, .llseek = generic_file_llseek,
}; };
static int sel_make_policy_nodes(struct selinux_fs_info *fsi)
{
int ret;
ret = sel_make_bools(fsi);
if (ret) {
pr_err("SELinux: failed to load policy booleans\n");
return ret;
}
ret = sel_make_classes(fsi);
if (ret) {
pr_err("SELinux: failed to load policy classes\n");
return ret;
}
ret = sel_make_policycap(fsi);
if (ret) {
pr_err("SELinux: failed to load policy capabilities\n");
return ret;
}
return 0;
}
static ssize_t sel_write_load(struct file *file, const char __user *buf, static ssize_t sel_write_load(struct file *file, const char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
ssize_t length; ssize_t length;
void *data = NULL; void *data = NULL;
mutex_lock(&sel_mutex); mutex_lock(&fsi->mutex);
length = avc_has_perm(current_sid(), SECINITSID_SECURITY, length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL); SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
...@@ -494,29 +558,15 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf, ...@@ -494,29 +558,15 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,
if (copy_from_user(data, buf, count) != 0) if (copy_from_user(data, buf, count) != 0)
goto out; goto out;
length = security_load_policy(&selinux_state, data, count); length = security_load_policy(fsi->state, data, count);
if (length) { if (length) {
pr_warn_ratelimited("SELinux: failed to load policy\n"); pr_warn_ratelimited("SELinux: failed to load policy\n");
goto out; goto out;
} }
length = sel_make_bools(); length = sel_make_policy_nodes(fsi);
if (length) { if (length)
pr_err("SELinux: failed to load policy booleans\n");
goto out1;
}
length = sel_make_classes();
if (length) {
pr_err("SELinux: failed to load policy classes\n");
goto out1;
}
length = sel_make_policycap();
if (length) {
pr_err("SELinux: failed to load policy capabilities\n");
goto out1; goto out1;
}
length = count; length = count;
...@@ -526,7 +576,7 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf, ...@@ -526,7 +576,7 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,
from_kuid(&init_user_ns, audit_get_loginuid(current)), from_kuid(&init_user_ns, audit_get_loginuid(current)),
audit_get_sessionid(current)); audit_get_sessionid(current));
out: out:
mutex_unlock(&sel_mutex); mutex_unlock(&fsi->mutex);
vfree(data); vfree(data);
return length; return length;
} }
...@@ -538,6 +588,8 @@ static const struct file_operations sel_load_ops = { ...@@ -538,6 +588,8 @@ static const struct file_operations sel_load_ops = {
static ssize_t sel_write_context(struct file *file, char *buf, size_t size) static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
{ {
struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
struct selinux_state *state = fsi->state;
char *canon = NULL; char *canon = NULL;
u32 sid, len; u32 sid, len;
ssize_t length; ssize_t length;
...@@ -547,12 +599,11 @@ static ssize_t sel_write_context(struct file *file, char *buf, size_t size) ...@@ -547,12 +599,11 @@ static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
if (length) if (length)
goto out; goto out;
length = security_context_to_sid(&selinux_state, buf, size, length = security_context_to_sid(state, buf, size, &sid, GFP_KERNEL);
&sid, GFP_KERNEL);
if (length) if (length)
goto out; goto out;
length = security_sid_to_context(&selinux_state, sid, &canon, &len); length = security_sid_to_context(state, sid, &canon, &len);
if (length) if (length)
goto out; goto out;
...@@ -573,16 +624,18 @@ static ssize_t sel_write_context(struct file *file, char *buf, size_t size) ...@@ -573,16 +624,18 @@ static ssize_t sel_write_context(struct file *file, char *buf, size_t size)
static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf, static ssize_t sel_read_checkreqprot(struct file *filp, char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct selinux_fs_info *fsi = file_inode(filp)->i_sb->s_fs_info;
char tmpbuf[TMPBUFLEN]; char tmpbuf[TMPBUFLEN];
ssize_t length; ssize_t length;
length = scnprintf(tmpbuf, TMPBUFLEN, "%u", selinux_state.checkreqprot); length = scnprintf(tmpbuf, TMPBUFLEN, "%u", fsi->state->checkreqprot);
return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
} }
static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf, static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
char *page; char *page;
ssize_t length; ssize_t length;
unsigned int new_value; unsigned int new_value;
...@@ -608,7 +661,7 @@ static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf, ...@@ -608,7 +661,7 @@ static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
if (sscanf(page, "%u", &new_value) != 1) if (sscanf(page, "%u", &new_value) != 1)
goto out; goto out;
selinux_state.checkreqprot = new_value ? 1 : 0; fsi->state->checkreqprot = new_value ? 1 : 0;
length = count; length = count;
out: out:
kfree(page); kfree(page);
...@@ -624,6 +677,8 @@ static ssize_t sel_write_validatetrans(struct file *file, ...@@ -624,6 +677,8 @@ static ssize_t sel_write_validatetrans(struct file *file,
const char __user *buf, const char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
struct selinux_state *state = fsi->state;
char *oldcon = NULL, *newcon = NULL, *taskcon = NULL; char *oldcon = NULL, *newcon = NULL, *taskcon = NULL;
char *req = NULL; char *req = NULL;
u32 osid, nsid, tsid; u32 osid, nsid, tsid;
...@@ -668,23 +723,19 @@ static ssize_t sel_write_validatetrans(struct file *file, ...@@ -668,23 +723,19 @@ static ssize_t sel_write_validatetrans(struct file *file,
if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4) if (sscanf(req, "%s %s %hu %s", oldcon, newcon, &tclass, taskcon) != 4)
goto out; goto out;
rc = security_context_str_to_sid(&selinux_state, oldcon, &osid, rc = security_context_str_to_sid(state, oldcon, &osid, GFP_KERNEL);
GFP_KERNEL);
if (rc) if (rc)
goto out; goto out;
rc = security_context_str_to_sid(&selinux_state, newcon, &nsid, rc = security_context_str_to_sid(state, newcon, &nsid, GFP_KERNEL);
GFP_KERNEL);
if (rc) if (rc)
goto out; goto out;
rc = security_context_str_to_sid(&selinux_state, taskcon, &tsid, rc = security_context_str_to_sid(state, taskcon, &tsid, GFP_KERNEL);
GFP_KERNEL);
if (rc) if (rc)
goto out; goto out;
rc = security_validate_transition_user(&selinux_state, osid, nsid, rc = security_validate_transition_user(state, osid, nsid, tsid, tclass);
tsid, tclass);
if (!rc) if (!rc)
rc = count; rc = count;
out: out:
...@@ -754,6 +805,8 @@ static const struct file_operations transaction_ops = { ...@@ -754,6 +805,8 @@ static const struct file_operations transaction_ops = {
static ssize_t sel_write_access(struct file *file, char *buf, size_t size) static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
{ {
struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
struct selinux_state *state = fsi->state;
char *scon = NULL, *tcon = NULL; char *scon = NULL, *tcon = NULL;
u32 ssid, tsid; u32 ssid, tsid;
u16 tclass; u16 tclass;
...@@ -779,17 +832,15 @@ static ssize_t sel_write_access(struct file *file, char *buf, size_t size) ...@@ -779,17 +832,15 @@ static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
goto out; goto out;
length = security_context_str_to_sid(&selinux_state, scon, &ssid, length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
GFP_KERNEL);
if (length) if (length)
goto out; goto out;
length = security_context_str_to_sid(&selinux_state, tcon, &tsid, length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
GFP_KERNEL);
if (length) if (length)
goto out; goto out;
security_compute_av_user(&selinux_state, ssid, tsid, tclass, &avd); security_compute_av_user(state, ssid, tsid, tclass, &avd);
length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, length = scnprintf(buf, SIMPLE_TRANSACTION_LIMIT,
"%x %x %x %x %u %x", "%x %x %x %x %u %x",
...@@ -804,6 +855,8 @@ static ssize_t sel_write_access(struct file *file, char *buf, size_t size) ...@@ -804,6 +855,8 @@ static ssize_t sel_write_access(struct file *file, char *buf, size_t size)
static ssize_t sel_write_create(struct file *file, char *buf, size_t size) static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
{ {
struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
struct selinux_state *state = fsi->state;
char *scon = NULL, *tcon = NULL; char *scon = NULL, *tcon = NULL;
char *namebuf = NULL, *objname = NULL; char *namebuf = NULL, *objname = NULL;
u32 ssid, tsid, newsid; u32 ssid, tsid, newsid;
...@@ -869,23 +922,20 @@ static ssize_t sel_write_create(struct file *file, char *buf, size_t size) ...@@ -869,23 +922,20 @@ static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
objname = namebuf; objname = namebuf;
} }
length = security_context_str_to_sid(&selinux_state, scon, &ssid, length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
GFP_KERNEL);
if (length) if (length)
goto out; goto out;
length = security_context_str_to_sid(&selinux_state, tcon, &tsid, length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
GFP_KERNEL);
if (length) if (length)
goto out; goto out;
length = security_transition_sid_user(&selinux_state, ssid, tsid, length = security_transition_sid_user(state, ssid, tsid, tclass,
tclass, objname, &newsid); objname, &newsid);
if (length) if (length)
goto out; goto out;
length = security_sid_to_context(&selinux_state, newsid, &newcon, length = security_sid_to_context(state, newsid, &newcon, &len);
&len);
if (length) if (length)
goto out; goto out;
...@@ -908,6 +958,8 @@ static ssize_t sel_write_create(struct file *file, char *buf, size_t size) ...@@ -908,6 +958,8 @@ static ssize_t sel_write_create(struct file *file, char *buf, size_t size)
static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size) static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
{ {
struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
struct selinux_state *state = fsi->state;
char *scon = NULL, *tcon = NULL; char *scon = NULL, *tcon = NULL;
u32 ssid, tsid, newsid; u32 ssid, tsid, newsid;
u16 tclass; u16 tclass;
...@@ -935,23 +987,19 @@ static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size) ...@@ -935,23 +987,19 @@ static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
goto out; goto out;
length = security_context_str_to_sid(&selinux_state, scon, &ssid, length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
GFP_KERNEL);
if (length) if (length)
goto out; goto out;
length = security_context_str_to_sid(&selinux_state, tcon, &tsid, length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
GFP_KERNEL);
if (length) if (length)
goto out; goto out;
length = security_change_sid(&selinux_state, ssid, tsid, tclass, length = security_change_sid(state, ssid, tsid, tclass, &newsid);
&newsid);
if (length) if (length)
goto out; goto out;
length = security_sid_to_context(&selinux_state, newsid, &newcon, length = security_sid_to_context(state, newsid, &newcon, &len);
&len);
if (length) if (length)
goto out; goto out;
...@@ -970,6 +1018,8 @@ static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size) ...@@ -970,6 +1018,8 @@ static ssize_t sel_write_relabel(struct file *file, char *buf, size_t size)
static ssize_t sel_write_user(struct file *file, char *buf, size_t size) static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
{ {
struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
struct selinux_state *state = fsi->state;
char *con = NULL, *user = NULL, *ptr; char *con = NULL, *user = NULL, *ptr;
u32 sid, *sids = NULL; u32 sid, *sids = NULL;
ssize_t length; ssize_t length;
...@@ -997,21 +1047,18 @@ static ssize_t sel_write_user(struct file *file, char *buf, size_t size) ...@@ -997,21 +1047,18 @@ static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
if (sscanf(buf, "%s %s", con, user) != 2) if (sscanf(buf, "%s %s", con, user) != 2)
goto out; goto out;
length = security_context_str_to_sid(&selinux_state, con, &sid, length = security_context_str_to_sid(state, con, &sid, GFP_KERNEL);
GFP_KERNEL);
if (length) if (length)
goto out; goto out;
length = security_get_user_sids(&selinux_state, sid, user, &sids, length = security_get_user_sids(state, sid, user, &sids, &nsids);
&nsids);
if (length) if (length)
goto out; goto out;
length = sprintf(buf, "%u", nsids) + 1; length = sprintf(buf, "%u", nsids) + 1;
ptr = buf + length; ptr = buf + length;
for (i = 0; i < nsids; i++) { for (i = 0; i < nsids; i++) {
rc = security_sid_to_context(&selinux_state, sids[i], rc = security_sid_to_context(state, sids[i], &newcon, &len);
&newcon, &len);
if (rc) { if (rc) {
length = rc; length = rc;
goto out; goto out;
...@@ -1035,6 +1082,8 @@ static ssize_t sel_write_user(struct file *file, char *buf, size_t size) ...@@ -1035,6 +1082,8 @@ static ssize_t sel_write_user(struct file *file, char *buf, size_t size)
static ssize_t sel_write_member(struct file *file, char *buf, size_t size) static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
{ {
struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
struct selinux_state *state = fsi->state;
char *scon = NULL, *tcon = NULL; char *scon = NULL, *tcon = NULL;
u32 ssid, tsid, newsid; u32 ssid, tsid, newsid;
u16 tclass; u16 tclass;
...@@ -1062,23 +1111,19 @@ static ssize_t sel_write_member(struct file *file, char *buf, size_t size) ...@@ -1062,23 +1111,19 @@ static ssize_t sel_write_member(struct file *file, char *buf, size_t size)
if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3) if (sscanf(buf, "%s %s %hu", scon, tcon, &tclass) != 3)
goto out; goto out;
length = security_context_str_to_sid(&selinux_state, scon, &ssid, length = security_context_str_to_sid(state, scon, &ssid, GFP_KERNEL);
GFP_KERNEL);
if (length) if (length)
goto out; goto out;
length = security_context_str_to_sid(&selinux_state, tcon, &tsid, length = security_context_str_to_sid(state, tcon, &tsid, GFP_KERNEL);
GFP_KERNEL);
if (length) if (length)
goto out; goto out;
length = security_member_sid(&selinux_state, ssid, tsid, tclass, length = security_member_sid(state, ssid, tsid, tclass, &newsid);
&newsid);
if (length) if (length)
goto out; goto out;
length = security_sid_to_context(&selinux_state, newsid, &newcon, length = security_sid_to_context(state, newsid, &newcon, &len);
&len);
if (length) if (length)
goto out; goto out;
...@@ -1112,6 +1157,7 @@ static struct inode *sel_make_inode(struct super_block *sb, int mode) ...@@ -1112,6 +1157,7 @@ static struct inode *sel_make_inode(struct super_block *sb, int mode)
static ssize_t sel_read_bool(struct file *filep, char __user *buf, static ssize_t sel_read_bool(struct file *filep, char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
char *page = NULL; char *page = NULL;
ssize_t length; ssize_t length;
ssize_t ret; ssize_t ret;
...@@ -1119,10 +1165,11 @@ static ssize_t sel_read_bool(struct file *filep, char __user *buf, ...@@ -1119,10 +1165,11 @@ static ssize_t sel_read_bool(struct file *filep, char __user *buf,
unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
const char *name = filep->f_path.dentry->d_name.name; const char *name = filep->f_path.dentry->d_name.name;
mutex_lock(&sel_mutex); mutex_lock(&fsi->mutex);
ret = -EINVAL; ret = -EINVAL;
if (index >= bool_num || strcmp(name, bool_pending_names[index])) if (index >= fsi->bool_num || strcmp(name,
fsi->bool_pending_names[index]))
goto out; goto out;
ret = -ENOMEM; ret = -ENOMEM;
...@@ -1130,16 +1177,16 @@ static ssize_t sel_read_bool(struct file *filep, char __user *buf, ...@@ -1130,16 +1177,16 @@ static ssize_t sel_read_bool(struct file *filep, char __user *buf,
if (!page) if (!page)
goto out; goto out;
cur_enforcing = security_get_bool_value(&selinux_state, index); cur_enforcing = security_get_bool_value(fsi->state, index);
if (cur_enforcing < 0) { if (cur_enforcing < 0) {
ret = cur_enforcing; ret = cur_enforcing;
goto out; goto out;
} }
length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing, length = scnprintf(page, PAGE_SIZE, "%d %d", cur_enforcing,
bool_pending_values[index]); fsi->bool_pending_values[index]);
ret = simple_read_from_buffer(buf, count, ppos, page, length); ret = simple_read_from_buffer(buf, count, ppos, page, length);
out: out:
mutex_unlock(&sel_mutex); mutex_unlock(&fsi->mutex);
free_page((unsigned long)page); free_page((unsigned long)page);
return ret; return ret;
} }
...@@ -1147,13 +1194,14 @@ static ssize_t sel_read_bool(struct file *filep, char __user *buf, ...@@ -1147,13 +1194,14 @@ static ssize_t sel_read_bool(struct file *filep, char __user *buf,
static ssize_t sel_write_bool(struct file *filep, const char __user *buf, static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
char *page = NULL; char *page = NULL;
ssize_t length; ssize_t length;
int new_value; int new_value;
unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK; unsigned index = file_inode(filep)->i_ino & SEL_INO_MASK;
const char *name = filep->f_path.dentry->d_name.name; const char *name = filep->f_path.dentry->d_name.name;
mutex_lock(&sel_mutex); mutex_lock(&fsi->mutex);
length = avc_has_perm(current_sid(), SECINITSID_SECURITY, length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
SECCLASS_SECURITY, SECURITY__SETBOOL, SECCLASS_SECURITY, SECURITY__SETBOOL,
...@@ -1162,7 +1210,8 @@ static ssize_t sel_write_bool(struct file *filep, const char __user *buf, ...@@ -1162,7 +1210,8 @@ static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
goto out; goto out;
length = -EINVAL; length = -EINVAL;
if (index >= bool_num || strcmp(name, bool_pending_names[index])) if (index >= fsi->bool_num || strcmp(name,
fsi->bool_pending_names[index]))
goto out; goto out;
length = -ENOMEM; length = -ENOMEM;
...@@ -1188,11 +1237,11 @@ static ssize_t sel_write_bool(struct file *filep, const char __user *buf, ...@@ -1188,11 +1237,11 @@ static ssize_t sel_write_bool(struct file *filep, const char __user *buf,
if (new_value) if (new_value)
new_value = 1; new_value = 1;
bool_pending_values[index] = new_value; fsi->bool_pending_values[index] = new_value;
length = count; length = count;
out: out:
mutex_unlock(&sel_mutex); mutex_unlock(&fsi->mutex);
kfree(page); kfree(page);
return length; return length;
} }
...@@ -1207,11 +1256,12 @@ static ssize_t sel_commit_bools_write(struct file *filep, ...@@ -1207,11 +1256,12 @@ static ssize_t sel_commit_bools_write(struct file *filep,
const char __user *buf, const char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct selinux_fs_info *fsi = file_inode(filep)->i_sb->s_fs_info;
char *page = NULL; char *page = NULL;
ssize_t length; ssize_t length;
int new_value; int new_value;
mutex_lock(&sel_mutex); mutex_lock(&fsi->mutex);
length = avc_has_perm(current_sid(), SECINITSID_SECURITY, length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
SECCLASS_SECURITY, SECURITY__SETBOOL, SECCLASS_SECURITY, SECURITY__SETBOOL,
...@@ -1240,15 +1290,15 @@ static ssize_t sel_commit_bools_write(struct file *filep, ...@@ -1240,15 +1290,15 @@ static ssize_t sel_commit_bools_write(struct file *filep,
goto out; goto out;
length = 0; length = 0;
if (new_value && bool_pending_values) if (new_value && fsi->bool_pending_values)
length = security_set_bools(&selinux_state, bool_num, length = security_set_bools(fsi->state, fsi->bool_num,
bool_pending_values); fsi->bool_pending_values);
if (!length) if (!length)
length = count; length = count;
out: out:
mutex_unlock(&sel_mutex); mutex_unlock(&fsi->mutex);
kfree(page); kfree(page);
return length; return length;
} }
...@@ -1266,12 +1316,12 @@ static void sel_remove_entries(struct dentry *de) ...@@ -1266,12 +1316,12 @@ static void sel_remove_entries(struct dentry *de)
#define BOOL_DIR_NAME "booleans" #define BOOL_DIR_NAME "booleans"
static int sel_make_bools(void) static int sel_make_bools(struct selinux_fs_info *fsi)
{ {
int i, ret; int i, ret;
ssize_t len; ssize_t len;
struct dentry *dentry = NULL; struct dentry *dentry = NULL;
struct dentry *dir = bool_dir; struct dentry *dir = fsi->bool_dir;
struct inode *inode = NULL; struct inode *inode = NULL;
struct inode_security_struct *isec; struct inode_security_struct *isec;
char **names = NULL, *page; char **names = NULL, *page;
...@@ -1280,13 +1330,13 @@ static int sel_make_bools(void) ...@@ -1280,13 +1330,13 @@ static int sel_make_bools(void)
u32 sid; u32 sid;
/* remove any existing files */ /* remove any existing files */
for (i = 0; i < bool_num; i++) for (i = 0; i < fsi->bool_num; i++)
kfree(bool_pending_names[i]); kfree(fsi->bool_pending_names[i]);
kfree(bool_pending_names); kfree(fsi->bool_pending_names);
kfree(bool_pending_values); kfree(fsi->bool_pending_values);
bool_num = 0; fsi->bool_num = 0;
bool_pending_names = NULL; fsi->bool_pending_names = NULL;
bool_pending_values = NULL; fsi->bool_pending_values = NULL;
sel_remove_entries(dir); sel_remove_entries(dir);
...@@ -1295,7 +1345,7 @@ static int sel_make_bools(void) ...@@ -1295,7 +1345,7 @@ static int sel_make_bools(void)
if (!page) if (!page)
goto out; goto out;
ret = security_get_bools(&selinux_state, &num, &names, &values); ret = security_get_bools(fsi->state, &num, &names, &values);
if (ret) if (ret)
goto out; goto out;
...@@ -1316,7 +1366,7 @@ static int sel_make_bools(void) ...@@ -1316,7 +1366,7 @@ static int sel_make_bools(void)
goto out; goto out;
isec = (struct inode_security_struct *)inode->i_security; isec = (struct inode_security_struct *)inode->i_security;
ret = security_genfs_sid(&selinux_state, "selinuxfs", page, ret = security_genfs_sid(fsi->state, "selinuxfs", page,
SECCLASS_FILE, &sid); SECCLASS_FILE, &sid);
if (ret) { if (ret) {
pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n", pr_warn_ratelimited("SELinux: no sid found, defaulting to security isid for %s\n",
...@@ -1330,9 +1380,9 @@ static int sel_make_bools(void) ...@@ -1330,9 +1380,9 @@ static int sel_make_bools(void)
inode->i_ino = i|SEL_BOOL_INO_OFFSET; inode->i_ino = i|SEL_BOOL_INO_OFFSET;
d_add(dentry, inode); d_add(dentry, inode);
} }
bool_num = num; fsi->bool_num = num;
bool_pending_names = names; fsi->bool_pending_names = names;
bool_pending_values = values; fsi->bool_pending_values = values;
free_page((unsigned long)page); free_page((unsigned long)page);
return 0; return 0;
...@@ -1350,10 +1400,6 @@ static int sel_make_bools(void) ...@@ -1350,10 +1400,6 @@ static int sel_make_bools(void)
return ret; return ret;
} }
#define NULL_FILE_NAME "null"
struct path selinux_null;
static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf, static ssize_t sel_read_avc_cache_threshold(struct file *filp, char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
...@@ -1503,6 +1549,8 @@ static const struct file_operations sel_avc_cache_stats_ops = { ...@@ -1503,6 +1549,8 @@ static const struct file_operations sel_avc_cache_stats_ops = {
static int sel_make_avc_files(struct dentry *dir) static int sel_make_avc_files(struct dentry *dir)
{ {
struct super_block *sb = dir->d_sb;
struct selinux_fs_info *fsi = sb->s_fs_info;
int i; int i;
static const struct tree_descr files[] = { static const struct tree_descr files[] = {
{ "cache_threshold", { "cache_threshold",
...@@ -1526,7 +1574,7 @@ static int sel_make_avc_files(struct dentry *dir) ...@@ -1526,7 +1574,7 @@ static int sel_make_avc_files(struct dentry *dir)
return -ENOMEM; return -ENOMEM;
inode->i_fop = files[i].ops; inode->i_fop = files[i].ops;
inode->i_ino = ++sel_last_ino; inode->i_ino = ++fsi->last_ino;
d_add(dentry, inode); d_add(dentry, inode);
} }
...@@ -1536,12 +1584,13 @@ static int sel_make_avc_files(struct dentry *dir) ...@@ -1536,12 +1584,13 @@ static int sel_make_avc_files(struct dentry *dir)
static ssize_t sel_read_initcon(struct file *file, char __user *buf, static ssize_t sel_read_initcon(struct file *file, char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
char *con; char *con;
u32 sid, len; u32 sid, len;
ssize_t ret; ssize_t ret;
sid = file_inode(file)->i_ino&SEL_INO_MASK; sid = file_inode(file)->i_ino&SEL_INO_MASK;
ret = security_sid_to_context(&selinux_state, sid, &con, &len); ret = security_sid_to_context(fsi->state, sid, &con, &len);
if (ret) if (ret)
return ret; return ret;
...@@ -1629,13 +1678,13 @@ static const struct file_operations sel_perm_ops = { ...@@ -1629,13 +1678,13 @@ static const struct file_operations sel_perm_ops = {
static ssize_t sel_read_policycap(struct file *file, char __user *buf, static ssize_t sel_read_policycap(struct file *file, char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
struct selinux_fs_info *fsi = file_inode(file)->i_sb->s_fs_info;
int value; int value;
char tmpbuf[TMPBUFLEN]; char tmpbuf[TMPBUFLEN];
ssize_t length; ssize_t length;
unsigned long i_ino = file_inode(file)->i_ino; unsigned long i_ino = file_inode(file)->i_ino;
value = security_policycap_supported(&selinux_state, value = security_policycap_supported(fsi->state, i_ino & SEL_INO_MASK);
i_ino & SEL_INO_MASK);
length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value); length = scnprintf(tmpbuf, TMPBUFLEN, "%d", value);
return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
...@@ -1649,11 +1698,11 @@ static const struct file_operations sel_policycap_ops = { ...@@ -1649,11 +1698,11 @@ static const struct file_operations sel_policycap_ops = {
static int sel_make_perm_files(char *objclass, int classvalue, static int sel_make_perm_files(char *objclass, int classvalue,
struct dentry *dir) struct dentry *dir)
{ {
struct selinux_fs_info *fsi = dir->d_sb->s_fs_info;
int i, rc, nperms; int i, rc, nperms;
char **perms; char **perms;
rc = security_get_permissions(&selinux_state, objclass, &perms, rc = security_get_permissions(fsi->state, objclass, &perms, &nperms);
&nperms);
if (rc) if (rc)
return rc; return rc;
...@@ -1687,6 +1736,8 @@ static int sel_make_perm_files(char *objclass, int classvalue, ...@@ -1687,6 +1736,8 @@ static int sel_make_perm_files(char *objclass, int classvalue,
static int sel_make_class_dir_entries(char *classname, int index, static int sel_make_class_dir_entries(char *classname, int index,
struct dentry *dir) struct dentry *dir)
{ {
struct super_block *sb = dir->d_sb;
struct selinux_fs_info *fsi = sb->s_fs_info;
struct dentry *dentry = NULL; struct dentry *dentry = NULL;
struct inode *inode = NULL; struct inode *inode = NULL;
int rc; int rc;
...@@ -1703,7 +1754,7 @@ static int sel_make_class_dir_entries(char *classname, int index, ...@@ -1703,7 +1754,7 @@ static int sel_make_class_dir_entries(char *classname, int index,
inode->i_ino = sel_class_to_ino(index); inode->i_ino = sel_class_to_ino(index);
d_add(dentry, inode); d_add(dentry, inode);
dentry = sel_make_dir(dir, "perms", &last_class_ino); dentry = sel_make_dir(dir, "perms", &fsi->last_class_ino);
if (IS_ERR(dentry)) if (IS_ERR(dentry))
return PTR_ERR(dentry); return PTR_ERR(dentry);
...@@ -1712,26 +1763,27 @@ static int sel_make_class_dir_entries(char *classname, int index, ...@@ -1712,26 +1763,27 @@ static int sel_make_class_dir_entries(char *classname, int index,
return rc; return rc;
} }
static int sel_make_classes(void) static int sel_make_classes(struct selinux_fs_info *fsi)
{ {
int rc, nclasses, i; int rc, nclasses, i;
char **classes; char **classes;
/* delete any existing entries */ /* delete any existing entries */
sel_remove_entries(class_dir); sel_remove_entries(fsi->class_dir);
rc = security_get_classes(&selinux_state, &classes, &nclasses); rc = security_get_classes(fsi->state, &classes, &nclasses);
if (rc) if (rc)
return rc; return rc;
/* +2 since classes are 1-indexed */ /* +2 since classes are 1-indexed */
last_class_ino = sel_class_to_ino(nclasses + 2); fsi->last_class_ino = sel_class_to_ino(nclasses + 2);
for (i = 0; i < nclasses; i++) { for (i = 0; i < nclasses; i++) {
struct dentry *class_name_dir; struct dentry *class_name_dir;
class_name_dir = sel_make_dir(class_dir, classes[i], class_name_dir = sel_make_dir(fsi->class_dir, classes[i],
&last_class_ino); &fsi->last_class_ino);
if (IS_ERR(class_name_dir)) { if (IS_ERR(class_name_dir)) {
rc = PTR_ERR(class_name_dir); rc = PTR_ERR(class_name_dir);
goto out; goto out;
...@@ -1751,25 +1803,25 @@ static int sel_make_classes(void) ...@@ -1751,25 +1803,25 @@ static int sel_make_classes(void)
return rc; return rc;
} }
static int sel_make_policycap(void) static int sel_make_policycap(struct selinux_fs_info *fsi)
{ {
unsigned int iter; unsigned int iter;
struct dentry *dentry = NULL; struct dentry *dentry = NULL;
struct inode *inode = NULL; struct inode *inode = NULL;
sel_remove_entries(policycap_dir); sel_remove_entries(fsi->policycap_dir);
for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) { for (iter = 0; iter <= POLICYDB_CAPABILITY_MAX; iter++) {
if (iter < ARRAY_SIZE(selinux_policycap_names)) if (iter < ARRAY_SIZE(selinux_policycap_names))
dentry = d_alloc_name(policycap_dir, dentry = d_alloc_name(fsi->policycap_dir,
selinux_policycap_names[iter]); selinux_policycap_names[iter]);
else else
dentry = d_alloc_name(policycap_dir, "unknown"); dentry = d_alloc_name(fsi->policycap_dir, "unknown");
if (dentry == NULL) if (dentry == NULL)
return -ENOMEM; return -ENOMEM;
inode = sel_make_inode(policycap_dir->d_sb, S_IFREG | S_IRUGO); inode = sel_make_inode(fsi->sb, S_IFREG | 0444);
if (inode == NULL) if (inode == NULL)
return -ENOMEM; return -ENOMEM;
...@@ -1808,8 +1860,11 @@ static struct dentry *sel_make_dir(struct dentry *dir, const char *name, ...@@ -1808,8 +1860,11 @@ static struct dentry *sel_make_dir(struct dentry *dir, const char *name,
return dentry; return dentry;
} }
#define NULL_FILE_NAME "null"
static int sel_fill_super(struct super_block *sb, void *data, int silent) static int sel_fill_super(struct super_block *sb, void *data, int silent)
{ {
struct selinux_fs_info *fsi;
int ret; int ret;
struct dentry *dentry; struct dentry *dentry;
struct inode *inode; struct inode *inode;
...@@ -1837,14 +1892,20 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent) ...@@ -1837,14 +1892,20 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent)
S_IWUGO}, S_IWUGO},
/* last one */ {""} /* last one */ {""}
}; };
ret = selinux_fs_info_create(sb);
if (ret)
goto err;
ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files); ret = simple_fill_super(sb, SELINUX_MAGIC, selinux_files);
if (ret) if (ret)
goto err; goto err;
bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &sel_last_ino); fsi = sb->s_fs_info;
if (IS_ERR(bool_dir)) { fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
ret = PTR_ERR(bool_dir); if (IS_ERR(fsi->bool_dir)) {
bool_dir = NULL; ret = PTR_ERR(fsi->bool_dir);
fsi->bool_dir = NULL;
goto err; goto err;
} }
...@@ -1858,7 +1919,7 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent) ...@@ -1858,7 +1919,7 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent)
if (!inode) if (!inode)
goto err; goto err;
inode->i_ino = ++sel_last_ino; inode->i_ino = ++fsi->last_ino;
isec = (struct inode_security_struct *)inode->i_security; isec = (struct inode_security_struct *)inode->i_security;
isec->sid = SECINITSID_DEVNULL; isec->sid = SECINITSID_DEVNULL;
isec->sclass = SECCLASS_CHR_FILE; isec->sclass = SECCLASS_CHR_FILE;
...@@ -1866,9 +1927,8 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent) ...@@ -1866,9 +1927,8 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent)
init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3)); init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO, MKDEV(MEM_MAJOR, 3));
d_add(dentry, inode); d_add(dentry, inode);
selinux_null.dentry = dentry;
dentry = sel_make_dir(sb->s_root, "avc", &sel_last_ino); dentry = sel_make_dir(sb->s_root, "avc", &fsi->last_ino);
if (IS_ERR(dentry)) { if (IS_ERR(dentry)) {
ret = PTR_ERR(dentry); ret = PTR_ERR(dentry);
goto err; goto err;
...@@ -1878,7 +1938,7 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent) ...@@ -1878,7 +1938,7 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent)
if (ret) if (ret)
goto err; goto err;
dentry = sel_make_dir(sb->s_root, "initial_contexts", &sel_last_ino); dentry = sel_make_dir(sb->s_root, "initial_contexts", &fsi->last_ino);
if (IS_ERR(dentry)) { if (IS_ERR(dentry)) {
ret = PTR_ERR(dentry); ret = PTR_ERR(dentry);
goto err; goto err;
...@@ -1888,23 +1948,31 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent) ...@@ -1888,23 +1948,31 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent)
if (ret) if (ret)
goto err; goto err;
class_dir = sel_make_dir(sb->s_root, "class", &sel_last_ino); fsi->class_dir = sel_make_dir(sb->s_root, "class", &fsi->last_ino);
if (IS_ERR(class_dir)) { if (IS_ERR(fsi->class_dir)) {
ret = PTR_ERR(class_dir); ret = PTR_ERR(fsi->class_dir);
class_dir = NULL; fsi->class_dir = NULL;
goto err; goto err;
} }
policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities", &sel_last_ino); fsi->policycap_dir = sel_make_dir(sb->s_root, "policy_capabilities",
if (IS_ERR(policycap_dir)) { &fsi->last_ino);
ret = PTR_ERR(policycap_dir); if (IS_ERR(fsi->policycap_dir)) {
policycap_dir = NULL; ret = PTR_ERR(fsi->policycap_dir);
fsi->policycap_dir = NULL;
goto err; goto err;
} }
ret = sel_make_policy_nodes(fsi);
if (ret)
goto err;
return 0; return 0;
err: err:
printk(KERN_ERR "SELinux: %s: failed while creating inodes\n", printk(KERN_ERR "SELinux: %s: failed while creating inodes\n",
__func__); __func__);
selinux_fs_info_free(sb);
return ret; return ret;
} }
...@@ -1914,16 +1982,25 @@ static struct dentry *sel_mount(struct file_system_type *fs_type, ...@@ -1914,16 +1982,25 @@ static struct dentry *sel_mount(struct file_system_type *fs_type,
return mount_single(fs_type, flags, data, sel_fill_super); return mount_single(fs_type, flags, data, sel_fill_super);
} }
static void sel_kill_sb(struct super_block *sb)
{
selinux_fs_info_free(sb);
kill_litter_super(sb);
}
static struct file_system_type sel_fs_type = { static struct file_system_type sel_fs_type = {
.name = "selinuxfs", .name = "selinuxfs",
.mount = sel_mount, .mount = sel_mount,
.kill_sb = kill_litter_super, .kill_sb = sel_kill_sb,
}; };
struct vfsmount *selinuxfs_mount; struct vfsmount *selinuxfs_mount;
struct path selinux_null;
static int __init init_sel_fs(void) static int __init init_sel_fs(void)
{ {
struct qstr null_name = QSTR_INIT(NULL_FILE_NAME,
sizeof(NULL_FILE_NAME)-1);
int err; int err;
if (!selinux_enabled) if (!selinux_enabled)
...@@ -1945,6 +2022,13 @@ static int __init init_sel_fs(void) ...@@ -1945,6 +2022,13 @@ static int __init init_sel_fs(void)
err = PTR_ERR(selinuxfs_mount); err = PTR_ERR(selinuxfs_mount);
selinuxfs_mount = NULL; selinuxfs_mount = NULL;
} }
selinux_null.dentry = d_hash_and_lookup(selinux_null.mnt->mnt_root,
&null_name);
if (IS_ERR(selinux_null.dentry)) {
pr_err("selinuxfs: could not lookup null!\n");
err = PTR_ERR(selinux_null.dentry);
selinux_null.dentry = NULL;
}
return err; return err;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment