Commit 1ec4013b authored by Linus Torvalds's avatar Linus Torvalds

Merge tag 'safesetid-5.3' of git://github.com/micah-morton/linux

Pull safesetid updates from Micah Morton:
 "These changes from Jann Horn fix a couple issues in the recently added
  SafeSetID LSM:

   - There was a simple logic bug in one of the hooks for the LSM where
     the code was incorrectly returning early in some cases before all
     security checks had been passed.

   - There was a more high level issue with how this LSM gets configured
     that could allow for a program to bypass the security restrictions
     by switching to an allowed UID and then again to any other UID on
     the system if the target UID of the first transition is
     unconstrained on the system. Luckily this is an easy fix that we
     now enforce at the time the LSM gets configured.

  There are also some changes from Jann that make policy updates for
  this LSM atomic. Kees Cook, Jann and myself have reviewed these
  changes and they look good from our point of view"

* tag 'safesetid-5.3' of git://github.com/micah-morton/linux:
  LSM: SafeSetID: fix use of literal -1 in capable hook
  LSM: SafeSetID: verify transitive constrainedness
  LSM: SafeSetID: add read handler
  LSM: SafeSetID: rewrite userspace API to atomic updates
  LSM: SafeSetID: fix userns handling in securityfs
  LSM: SafeSetID: refactor policy parsing
  LSM: SafeSetID: refactor safesetid_security_capable()
  LSM: SafeSetID: refactor policy hash table
  LSM: SafeSetID: fix check for setresuid(new1, new2, new3)
  LSM: SafeSetID: fix pr_warn() to include newline
parents 3c69914b e10337da
...@@ -14,67 +14,50 @@ ...@@ -14,67 +14,50 @@
#define pr_fmt(fmt) "SafeSetID: " fmt #define pr_fmt(fmt) "SafeSetID: " fmt
#include <linux/hashtable.h>
#include <linux/lsm_hooks.h> #include <linux/lsm_hooks.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/ptrace.h> #include <linux/ptrace.h>
#include <linux/sched/task_stack.h> #include <linux/sched/task_stack.h>
#include <linux/security.h> #include <linux/security.h>
#include "lsm.h"
/* Flag indicating whether initialization completed */ /* Flag indicating whether initialization completed */
int safesetid_initialized; int safesetid_initialized;
#define NUM_BITS 8 /* 128 buckets in hash table */ struct setuid_ruleset __rcu *safesetid_setuid_rules;
static DEFINE_HASHTABLE(safesetid_whitelist_hashtable, NUM_BITS); /* Compute a decision for a transition from @src to @dst under @policy. */
enum sid_policy_type _setuid_policy_lookup(struct setuid_ruleset *policy,
/* kuid_t src, kuid_t dst)
* Hash table entry to store safesetid policy signifying that 'parent' user
* can setid to 'child' user.
*/
struct entry {
struct hlist_node next;
struct hlist_node dlist; /* for deletion cleanup */
uint64_t parent_kuid;
uint64_t child_kuid;
};
static DEFINE_SPINLOCK(safesetid_whitelist_hashtable_spinlock);
static bool check_setuid_policy_hashtable_key(kuid_t parent)
{ {
struct entry *entry; struct setuid_rule *rule;
enum sid_policy_type result = SIDPOL_DEFAULT;
rcu_read_lock();
hash_for_each_possible_rcu(safesetid_whitelist_hashtable, hash_for_each_possible(policy->rules, rule, next, __kuid_val(src)) {
entry, next, __kuid_val(parent)) { if (!uid_eq(rule->src_uid, src))
if (entry->parent_kuid == __kuid_val(parent)) { continue;
rcu_read_unlock(); if (uid_eq(rule->dst_uid, dst))
return true; return SIDPOL_ALLOWED;
} result = SIDPOL_CONSTRAINED;
} }
rcu_read_unlock(); return result;
return false;
} }
static bool check_setuid_policy_hashtable_key_value(kuid_t parent, /*
kuid_t child) * Compute a decision for a transition from @src to @dst under the active
* policy.
*/
static enum sid_policy_type setuid_policy_lookup(kuid_t src, kuid_t dst)
{ {
struct entry *entry; enum sid_policy_type result = SIDPOL_DEFAULT;
struct setuid_ruleset *pol;
rcu_read_lock(); rcu_read_lock();
hash_for_each_possible_rcu(safesetid_whitelist_hashtable, pol = rcu_dereference(safesetid_setuid_rules);
entry, next, __kuid_val(parent)) { if (pol)
if (entry->parent_kuid == __kuid_val(parent) && result = _setuid_policy_lookup(pol, src, dst);
entry->child_kuid == __kuid_val(child)) {
rcu_read_unlock();
return true;
}
}
rcu_read_unlock(); rcu_read_unlock();
return result;
return false;
} }
static int safesetid_security_capable(const struct cred *cred, static int safesetid_security_capable(const struct cred *cred,
...@@ -82,37 +65,59 @@ static int safesetid_security_capable(const struct cred *cred, ...@@ -82,37 +65,59 @@ static int safesetid_security_capable(const struct cred *cred,
int cap, int cap,
unsigned int opts) unsigned int opts)
{ {
if (cap == CAP_SETUID && /* We're only interested in CAP_SETUID. */
check_setuid_policy_hashtable_key(cred->uid)) { if (cap != CAP_SETUID)
if (!(opts & CAP_OPT_INSETID)) { return 0;
/*
* Deny if we're not in a set*uid() syscall to avoid /*
* giving powers gated by CAP_SETUID that are related * If CAP_SETUID is currently used for a set*uid() syscall, we want to
* to functionality other than calling set*uid() (e.g. * let it go through here; the real security check happens later, in the
* allowing user to set up userns uid mappings). * task_fix_setuid hook.
*/ */
pr_warn("Operation requires CAP_SETUID, which is not available to UID %u for operations besides approved set*uid transitions", if ((opts & CAP_OPT_INSETID) != 0)
__kuid_val(cred->uid)); return 0;
return -1;
} /*
} * If no policy applies to this task, allow the use of CAP_SETUID for
return 0; * other purposes.
*/
if (setuid_policy_lookup(cred->uid, INVALID_UID) == SIDPOL_DEFAULT)
return 0;
/*
* Reject use of CAP_SETUID for functionality other than calling
* set*uid() (e.g. setting up userns uid mappings).
*/
pr_warn("Operation requires CAP_SETUID, which is not available to UID %u for operations besides approved set*uid transitions\n",
__kuid_val(cred->uid));
return -EPERM;
} }
static int check_uid_transition(kuid_t parent, kuid_t child) /*
* Check whether a caller with old credentials @old is allowed to switch to
* credentials that contain @new_uid.
*/
static bool uid_permitted_for_cred(const struct cred *old, kuid_t new_uid)
{ {
if (check_setuid_policy_hashtable_key_value(parent, child)) bool permitted;
return 0;
pr_warn("UID transition (%d -> %d) blocked", /* If our old creds already had this UID in it, it's fine. */
__kuid_val(parent), if (uid_eq(new_uid, old->uid) || uid_eq(new_uid, old->euid) ||
__kuid_val(child)); uid_eq(new_uid, old->suid))
return true;
/* /*
* Kill this process to avoid potential security vulnerabilities * Transitions to new UIDs require a check against the policy of the old
* that could arise from a missing whitelist entry preventing a * RUID.
* privileged process from dropping to a lesser-privileged one.
*/ */
force_sig(SIGKILL); permitted =
return -EACCES; setuid_policy_lookup(old->uid, new_uid) != SIDPOL_CONSTRAINED;
if (!permitted) {
pr_warn("UID transition ((%d,%d,%d) -> %d) blocked\n",
__kuid_val(old->uid), __kuid_val(old->euid),
__kuid_val(old->suid), __kuid_val(new_uid));
}
return permitted;
} }
/* /*
...@@ -125,134 +130,23 @@ static int safesetid_task_fix_setuid(struct cred *new, ...@@ -125,134 +130,23 @@ static int safesetid_task_fix_setuid(struct cred *new,
int flags) int flags)
{ {
/* Do nothing if there are no setuid restrictions for this UID. */ /* Do nothing if there are no setuid restrictions for our old RUID. */
if (!check_setuid_policy_hashtable_key(old->uid)) if (setuid_policy_lookup(old->uid, INVALID_UID) == SIDPOL_DEFAULT)
return 0; return 0;
switch (flags) { if (uid_permitted_for_cred(old, new->uid) &&
case LSM_SETID_RE: uid_permitted_for_cred(old, new->euid) &&
/* uid_permitted_for_cred(old, new->suid) &&
* Users for which setuid restrictions exist can only set the uid_permitted_for_cred(old, new->fsuid))
* real UID to the real UID or the effective UID, unless an
* explicit whitelist policy allows the transition.
*/
if (!uid_eq(old->uid, new->uid) &&
!uid_eq(old->euid, new->uid)) {
return check_uid_transition(old->uid, new->uid);
}
/*
* Users for which setuid restrictions exist can only set the
* effective UID to the real UID, the effective UID, or the
* saved set-UID, unless an explicit whitelist policy allows
* the transition.
*/
if (!uid_eq(old->uid, new->euid) &&
!uid_eq(old->euid, new->euid) &&
!uid_eq(old->suid, new->euid)) {
return check_uid_transition(old->euid, new->euid);
}
break;
case LSM_SETID_ID:
/*
* Users for which setuid restrictions exist cannot change the
* real UID or saved set-UID unless an explicit whitelist
* policy allows the transition.
*/
if (!uid_eq(old->uid, new->uid))
return check_uid_transition(old->uid, new->uid);
if (!uid_eq(old->suid, new->suid))
return check_uid_transition(old->suid, new->suid);
break;
case LSM_SETID_RES:
/*
* Users for which setuid restrictions exist cannot change the
* real UID, effective UID, or saved set-UID to anything but
* one of: the current real UID, the current effective UID or
* the current saved set-user-ID unless an explicit whitelist
* policy allows the transition.
*/
if (!uid_eq(new->uid, old->uid) &&
!uid_eq(new->uid, old->euid) &&
!uid_eq(new->uid, old->suid)) {
return check_uid_transition(old->uid, new->uid);
}
if (!uid_eq(new->euid, old->uid) &&
!uid_eq(new->euid, old->euid) &&
!uid_eq(new->euid, old->suid)) {
return check_uid_transition(old->euid, new->euid);
}
if (!uid_eq(new->suid, old->uid) &&
!uid_eq(new->suid, old->euid) &&
!uid_eq(new->suid, old->suid)) {
return check_uid_transition(old->suid, new->suid);
}
break;
case LSM_SETID_FS:
/*
* Users for which setuid restrictions exist cannot change the
* filesystem UID to anything but one of: the current real UID,
* the current effective UID or the current saved set-UID
* unless an explicit whitelist policy allows the transition.
*/
if (!uid_eq(new->fsuid, old->uid) &&
!uid_eq(new->fsuid, old->euid) &&
!uid_eq(new->fsuid, old->suid) &&
!uid_eq(new->fsuid, old->fsuid)) {
return check_uid_transition(old->fsuid, new->fsuid);
}
break;
default:
pr_warn("Unknown setid state %d\n", flags);
force_sig(SIGKILL);
return -EINVAL;
}
return 0;
}
int add_safesetid_whitelist_entry(kuid_t parent, kuid_t child)
{
struct entry *new;
/* Return if entry already exists */
if (check_setuid_policy_hashtable_key_value(parent, child))
return 0; return 0;
new = kzalloc(sizeof(struct entry), GFP_KERNEL);
if (!new)
return -ENOMEM;
new->parent_kuid = __kuid_val(parent);
new->child_kuid = __kuid_val(child);
spin_lock(&safesetid_whitelist_hashtable_spinlock);
hash_add_rcu(safesetid_whitelist_hashtable,
&new->next,
__kuid_val(parent));
spin_unlock(&safesetid_whitelist_hashtable_spinlock);
return 0;
}
void flush_safesetid_whitelist_entries(void)
{
struct entry *entry;
struct hlist_node *hlist_node;
unsigned int bkt_loop_cursor;
HLIST_HEAD(free_list);
/* /*
* Could probably use hash_for_each_rcu here instead, but this should * Kill this process to avoid potential security vulnerabilities
* be fine as well. * that could arise from a missing whitelist entry preventing a
* privileged process from dropping to a lesser-privileged one.
*/ */
spin_lock(&safesetid_whitelist_hashtable_spinlock); force_sig(SIGKILL);
hash_for_each_safe(safesetid_whitelist_hashtable, bkt_loop_cursor, return -EACCES;
hlist_node, entry, next) {
hash_del_rcu(&entry->next);
hlist_add_head(&entry->dlist, &free_list);
}
spin_unlock(&safesetid_whitelist_hashtable_spinlock);
synchronize_rcu();
hlist_for_each_entry_safe(entry, hlist_node, &free_list, dlist) {
hlist_del(&entry->dlist);
kfree(entry);
}
} }
static struct security_hook_list safesetid_security_hooks[] = { static struct security_hook_list safesetid_security_hooks[] = {
......
...@@ -15,19 +15,39 @@ ...@@ -15,19 +15,39 @@
#define _SAFESETID_H #define _SAFESETID_H
#include <linux/types.h> #include <linux/types.h>
#include <linux/uidgid.h>
#include <linux/hashtable.h>
/* Flag indicating whether initialization completed */ /* Flag indicating whether initialization completed */
extern int safesetid_initialized; extern int safesetid_initialized;
/* Function type. */ enum sid_policy_type {
enum safesetid_whitelist_file_write_type { SIDPOL_DEFAULT, /* source ID is unaffected by policy */
SAFESETID_WHITELIST_ADD, /* Add whitelist policy. */ SIDPOL_CONSTRAINED, /* source ID is affected by policy */
SAFESETID_WHITELIST_FLUSH, /* Flush whitelist policies. */ SIDPOL_ALLOWED /* target ID explicitly allowed */
}; };
/* Add entry to safesetid whitelist to allow 'parent' to setid to 'child'. */ /*
int add_safesetid_whitelist_entry(kuid_t parent, kuid_t child); * Hash table entry to store safesetid policy signifying that 'src_uid'
* can setuid to 'dst_uid'.
*/
struct setuid_rule {
struct hlist_node next;
kuid_t src_uid;
kuid_t dst_uid;
};
#define SETID_HASH_BITS 8 /* 256 buckets in hash table */
struct setuid_ruleset {
DECLARE_HASHTABLE(rules, SETID_HASH_BITS);
char *policy_str;
struct rcu_head rcu;
};
enum sid_policy_type _setuid_policy_lookup(struct setuid_ruleset *policy,
kuid_t src, kuid_t dst);
void flush_safesetid_whitelist_entries(void); extern struct setuid_ruleset __rcu *safesetid_setuid_rules;
#endif /* _SAFESETID_H */ #endif /* _SAFESETID_H */
...@@ -11,92 +11,184 @@ ...@@ -11,92 +11,184 @@
* published by the Free Software Foundation. * published by the Free Software Foundation.
* *
*/ */
#define pr_fmt(fmt) "SafeSetID: " fmt
#include <linux/security.h> #include <linux/security.h>
#include <linux/cred.h> #include <linux/cred.h>
#include "lsm.h" #include "lsm.h"
static struct dentry *safesetid_policy_dir; static DEFINE_MUTEX(policy_update_lock);
struct safesetid_file_entry {
const char *name;
enum safesetid_whitelist_file_write_type type;
struct dentry *dentry;
};
static struct safesetid_file_entry safesetid_files[] = {
{.name = "add_whitelist_policy",
.type = SAFESETID_WHITELIST_ADD},
{.name = "flush_whitelist_policies",
.type = SAFESETID_WHITELIST_FLUSH},
};
/* /*
* In the case the input buffer contains one or more invalid UIDs, the kuid_t * In the case the input buffer contains one or more invalid UIDs, the kuid_t
* variables pointed to by 'parent' and 'child' will get updated but this * variables pointed to by @parent and @child will get updated but this
* function will return an error. * function will return an error.
* Contents of @buf may be modified.
*/ */
static int parse_safesetid_whitelist_policy(const char __user *buf, static int parse_policy_line(struct file *file, char *buf,
size_t len, struct setuid_rule *rule)
kuid_t *parent,
kuid_t *child)
{ {
char *kern_buf; char *child_str;
char *parent_buf;
char *child_buf;
const char separator[] = ":";
int ret; int ret;
size_t first_substring_length; u32 parsed_parent, parsed_child;
long parsed_parent;
long parsed_child;
/* Duplicate string from user memory and NULL-terminate */ /* Format of |buf| string should be <UID>:<UID>. */
kern_buf = memdup_user_nul(buf, len); child_str = strchr(buf, ':');
if (IS_ERR(kern_buf)) if (child_str == NULL)
return PTR_ERR(kern_buf); return -EINVAL;
*child_str = '\0';
child_str++;
/* ret = kstrtou32(buf, 0, &parsed_parent);
* Format of |buf| string should be <UID>:<UID>. if (ret)
* Find location of ":" in kern_buf (copied from |buf|). return ret;
*/
first_substring_length = strcspn(kern_buf, separator); ret = kstrtou32(child_str, 0, &parsed_child);
if (first_substring_length == 0 || first_substring_length == len) { if (ret)
ret = -EINVAL; return ret;
goto free_kern;
}
parent_buf = kmemdup_nul(kern_buf, first_substring_length, GFP_KERNEL); rule->src_uid = make_kuid(file->f_cred->user_ns, parsed_parent);
if (!parent_buf) { rule->dst_uid = make_kuid(file->f_cred->user_ns, parsed_child);
ret = -ENOMEM; if (!uid_valid(rule->src_uid) || !uid_valid(rule->dst_uid))
goto free_kern; return -EINVAL;
return 0;
}
static void __release_ruleset(struct rcu_head *rcu)
{
struct setuid_ruleset *pol =
container_of(rcu, struct setuid_ruleset, rcu);
int bucket;
struct setuid_rule *rule;
struct hlist_node *tmp;
hash_for_each_safe(pol->rules, bucket, tmp, rule, next)
kfree(rule);
kfree(pol->policy_str);
kfree(pol);
}
static void release_ruleset(struct setuid_ruleset *pol)
{
call_rcu(&pol->rcu, __release_ruleset);
}
static void insert_rule(struct setuid_ruleset *pol, struct setuid_rule *rule)
{
hash_add(pol->rules, &rule->next, __kuid_val(rule->src_uid));
}
static int verify_ruleset(struct setuid_ruleset *pol)
{
int bucket;
struct setuid_rule *rule, *nrule;
int res = 0;
hash_for_each(pol->rules, bucket, rule, next) {
if (_setuid_policy_lookup(pol, rule->dst_uid, INVALID_UID) ==
SIDPOL_DEFAULT) {
pr_warn("insecure policy detected: uid %d is constrained but transitively unconstrained through uid %d\n",
__kuid_val(rule->src_uid),
__kuid_val(rule->dst_uid));
res = -EINVAL;
/* fix it up */
nrule = kmalloc(sizeof(struct setuid_rule), GFP_KERNEL);
if (!nrule)
return -ENOMEM;
nrule->src_uid = rule->dst_uid;
nrule->dst_uid = rule->dst_uid;
insert_rule(pol, nrule);
}
} }
return res;
}
ret = kstrtol(parent_buf, 0, &parsed_parent); static ssize_t handle_policy_update(struct file *file,
if (ret) const char __user *ubuf, size_t len)
goto free_both; {
struct setuid_ruleset *pol;
char *buf, *p, *end;
int err;
child_buf = kern_buf + first_substring_length + 1; pol = kmalloc(sizeof(struct setuid_ruleset), GFP_KERNEL);
ret = kstrtol(child_buf, 0, &parsed_child); if (!pol)
if (ret) return -ENOMEM;
goto free_both; pol->policy_str = NULL;
hash_init(pol->rules);
*parent = make_kuid(current_user_ns(), parsed_parent); p = buf = memdup_user_nul(ubuf, len);
if (!uid_valid(*parent)) { if (IS_ERR(buf)) {
ret = -EINVAL; err = PTR_ERR(buf);
goto free_both; goto out_free_pol;
} }
pol->policy_str = kstrdup(buf, GFP_KERNEL);
if (pol->policy_str == NULL) {
err = -ENOMEM;
goto out_free_buf;
}
/* policy lines, including the last one, end with \n */
while (*p != '\0') {
struct setuid_rule *rule;
end = strchr(p, '\n');
if (end == NULL) {
err = -EINVAL;
goto out_free_buf;
}
*end = '\0';
rule = kmalloc(sizeof(struct setuid_rule), GFP_KERNEL);
if (!rule) {
err = -ENOMEM;
goto out_free_buf;
}
*child = make_kuid(current_user_ns(), parsed_child); err = parse_policy_line(file, p, rule);
if (!uid_valid(*child)) { if (err)
ret = -EINVAL; goto out_free_rule;
goto free_both;
if (_setuid_policy_lookup(pol, rule->src_uid, rule->dst_uid) ==
SIDPOL_ALLOWED) {
pr_warn("bad policy: duplicate entry\n");
err = -EEXIST;
goto out_free_rule;
}
insert_rule(pol, rule);
p = end + 1;
continue;
out_free_rule:
kfree(rule);
goto out_free_buf;
} }
free_both: err = verify_ruleset(pol);
kfree(parent_buf); /* bogus policy falls through after fixing it up */
free_kern: if (err && err != -EINVAL)
kfree(kern_buf); goto out_free_buf;
return ret;
/*
* Everything looks good, apply the policy and release the old one.
* What we really want here is an xchg() wrapper for RCU, but since that
* doesn't currently exist, just use a spinlock for now.
*/
mutex_lock(&policy_update_lock);
rcu_swap_protected(safesetid_setuid_rules, pol,
lockdep_is_held(&policy_update_lock));
mutex_unlock(&policy_update_lock);
err = len;
out_free_buf:
kfree(buf);
out_free_pol:
release_ruleset(pol);
return err;
} }
static ssize_t safesetid_file_write(struct file *file, static ssize_t safesetid_file_write(struct file *file,
...@@ -104,90 +196,65 @@ static ssize_t safesetid_file_write(struct file *file, ...@@ -104,90 +196,65 @@ static ssize_t safesetid_file_write(struct file *file,
size_t len, size_t len,
loff_t *ppos) loff_t *ppos)
{ {
struct safesetid_file_entry *file_entry = if (!file_ns_capable(file, &init_user_ns, CAP_MAC_ADMIN))
file->f_inode->i_private;
kuid_t parent;
kuid_t child;
int ret;
if (!ns_capable(current_user_ns(), CAP_MAC_ADMIN))
return -EPERM; return -EPERM;
if (*ppos != 0) if (*ppos != 0)
return -EINVAL; return -EINVAL;
switch (file_entry->type) { return handle_policy_update(file, buf, len);
case SAFESETID_WHITELIST_FLUSH:
flush_safesetid_whitelist_entries();
break;
case SAFESETID_WHITELIST_ADD:
ret = parse_safesetid_whitelist_policy(buf, len, &parent,
&child);
if (ret)
return ret;
ret = add_safesetid_whitelist_entry(parent, child);
if (ret)
return ret;
break;
default:
pr_warn("Unknown securityfs file %d\n", file_entry->type);
break;
}
/* Return len on success so caller won't keep trying to write */
return len;
} }
static const struct file_operations safesetid_file_fops = { static ssize_t safesetid_file_read(struct file *file, char __user *buf,
.write = safesetid_file_write, size_t len, loff_t *ppos)
};
static void safesetid_shutdown_securityfs(void)
{ {
int i; ssize_t res = 0;
struct setuid_ruleset *pol;
const char *kbuf;
for (i = 0; i < ARRAY_SIZE(safesetid_files); ++i) { mutex_lock(&policy_update_lock);
struct safesetid_file_entry *entry = pol = rcu_dereference_protected(safesetid_setuid_rules,
&safesetid_files[i]; lockdep_is_held(&policy_update_lock));
securityfs_remove(entry->dentry); if (pol) {
entry->dentry = NULL; kbuf = pol->policy_str;
res = simple_read_from_buffer(buf, len, ppos,
kbuf, strlen(kbuf));
} }
mutex_unlock(&policy_update_lock);
securityfs_remove(safesetid_policy_dir); return res;
safesetid_policy_dir = NULL;
} }
static const struct file_operations safesetid_file_fops = {
.read = safesetid_file_read,
.write = safesetid_file_write,
};
static int __init safesetid_init_securityfs(void) static int __init safesetid_init_securityfs(void)
{ {
int i;
int ret; int ret;
struct dentry *policy_dir;
struct dentry *policy_file;
if (!safesetid_initialized) if (!safesetid_initialized)
return 0; return 0;
safesetid_policy_dir = securityfs_create_dir("safesetid", NULL); policy_dir = securityfs_create_dir("safesetid", NULL);
if (IS_ERR(safesetid_policy_dir)) { if (IS_ERR(policy_dir)) {
ret = PTR_ERR(safesetid_policy_dir); ret = PTR_ERR(policy_dir);
goto error; goto error;
} }
for (i = 0; i < ARRAY_SIZE(safesetid_files); ++i) { policy_file = securityfs_create_file("whitelist_policy", 0600,
struct safesetid_file_entry *entry = policy_dir, NULL, &safesetid_file_fops);
&safesetid_files[i]; if (IS_ERR(policy_file)) {
entry->dentry = securityfs_create_file( ret = PTR_ERR(policy_file);
entry->name, 0200, safesetid_policy_dir, goto error;
entry, &safesetid_file_fops);
if (IS_ERR(entry->dentry)) {
ret = PTR_ERR(entry->dentry);
goto error;
}
} }
return 0; return 0;
error: error:
safesetid_shutdown_securityfs(); securityfs_remove(policy_dir);
return ret; return ret;
} }
fs_initcall(safesetid_init_securityfs); fs_initcall(safesetid_init_securityfs);
...@@ -142,23 +142,19 @@ static void ensure_securityfs_mounted(void) ...@@ -142,23 +142,19 @@ static void ensure_securityfs_mounted(void)
static void write_policies(void) static void write_policies(void)
{ {
static char *policy_str =
"1:2\n"
"1:3\n"
"2:2\n"
"3:3\n";
ssize_t written; ssize_t written;
int fd; int fd;
fd = open(add_whitelist_policy_file, O_WRONLY); fd = open(add_whitelist_policy_file, O_WRONLY);
if (fd < 0) if (fd < 0)
die("cant open add_whitelist_policy file\n"); die("cant open add_whitelist_policy file\n");
written = write(fd, "1:2", strlen("1:2")); written = write(fd, policy_str, strlen(policy_str));
if (written != strlen("1:2")) { if (written != strlen(policy_str)) {
if (written >= 0) {
die("short write to %s\n", add_whitelist_policy_file);
} else {
die("write to %s failed: %s\n",
add_whitelist_policy_file, strerror(errno));
}
}
written = write(fd, "1:3", strlen("1:3"));
if (written != strlen("1:3")) {
if (written >= 0) { if (written >= 0) {
die("short write to %s\n", add_whitelist_policy_file); die("short write to %s\n", add_whitelist_policy_file);
} else { } else {
......
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