Skip to content

Commit fbd9acb

Browse files
thejhmicah-morton
authored andcommitted
LSM: SafeSetID: add read handler
For debugging a running system, it is very helpful to be able to see what policy the system is using. Add a read handler that can dump out a copy of the loaded policy. Signed-off-by: Jann Horn <jannh@google.com> Signed-off-by: Micah Morton <mortonm@chromium.org>
1 parent 03638e6 commit fbd9acb

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

‎security/safesetid/lsm.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct setuid_rule {
4141

4242
struct setuid_ruleset {
4343
DECLARE_HASHTABLE(rules, SETID_HASH_BITS);
44+
char *policy_str;
4445
struct rcu_head rcu;
4546
};
4647

‎security/safesetid/securityfs.c‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#include "lsm.h"
2121

22-
static DEFINE_SPINLOCK(policy_update_lock);
22+
static DEFINE_MUTEX(policy_update_lock);
2323

2424
/*
2525
* In the case the input buffer contains one or more invalid UIDs, the kuid_t
@@ -67,6 +67,7 @@ static void __release_ruleset(struct rcu_head *rcu)
6767

6868
hash_for_each_safe(pol->rules, bucket, tmp, rule, next)
6969
kfree(rule);
70+
kfree(pol->policy_str);
7071
kfree(pol);
7172
}
7273

@@ -85,13 +86,19 @@ static ssize_t handle_policy_update(struct file *file,
8586
pol = kmalloc(sizeof(struct setuid_ruleset), GFP_KERNEL);
8687
if (!pol)
8788
return -ENOMEM;
89+
pol->policy_str = NULL;
8890
hash_init(pol->rules);
8991

9092
p = buf = memdup_user_nul(ubuf, len);
9193
if (IS_ERR(buf)) {
9294
err = PTR_ERR(buf);
9395
goto out_free_pol;
9496
}
97+
pol->policy_str = kstrdup(buf, GFP_KERNEL);
98+
if (pol->policy_str == NULL) {
99+
err = -ENOMEM;
100+
goto out_free_buf;
101+
}
95102

96103
/* policy lines, including the last one, end with \n */
97104
while (*p != '\0') {
@@ -135,10 +142,10 @@ static ssize_t handle_policy_update(struct file *file,
135142
* What we really want here is an xchg() wrapper for RCU, but since that
136143
* doesn't currently exist, just use a spinlock for now.
137144
*/
138-
spin_lock(&policy_update_lock);
145+
mutex_lock(&policy_update_lock);
139146
rcu_swap_protected(safesetid_setuid_rules, pol,
140147
lockdep_is_held(&policy_update_lock));
141-
spin_unlock(&policy_update_lock);
148+
mutex_unlock(&policy_update_lock);
142149
err = len;
143150

144151
out_free_buf:
@@ -162,7 +169,27 @@ static ssize_t safesetid_file_write(struct file *file,
162169
return handle_policy_update(file, buf, len);
163170
}
164171

172+
static ssize_t safesetid_file_read(struct file *file, char __user *buf,
173+
size_t len, loff_t *ppos)
174+
{
175+
ssize_t res = 0;
176+
struct setuid_ruleset *pol;
177+
const char *kbuf;
178+
179+
mutex_lock(&policy_update_lock);
180+
pol = rcu_dereference_protected(safesetid_setuid_rules,
181+
lockdep_is_held(&policy_update_lock));
182+
if (pol) {
183+
kbuf = pol->policy_str;
184+
res = simple_read_from_buffer(buf, len, ppos,
185+
kbuf, strlen(kbuf));
186+
}
187+
mutex_unlock(&policy_update_lock);
188+
return res;
189+
}
190+
165191
static const struct file_operations safesetid_file_fops = {
192+
.read = safesetid_file_read,
166193
.write = safesetid_file_write,
167194
};
168195

@@ -181,7 +208,7 @@ static int __init safesetid_init_securityfs(void)
181208
goto error;
182209
}
183210

184-
policy_file = securityfs_create_file("whitelist_policy", 0200,
211+
policy_file = securityfs_create_file("whitelist_policy", 0600,
185212
policy_dir, NULL, &safesetid_file_fops);
186213
if (IS_ERR(policy_file)) {
187214
ret = PTR_ERR(policy_file);

0 commit comments

Comments
 (0)