Skip to content

Commit 78ae7df

Browse files
thejhmicah-morton
authored andcommitted
LSM: SafeSetID: refactor policy parsing
In preparation for changing the policy parsing logic, refactor the line parsing logic to be less verbose and move it into a separate function. Signed-off-by: Jann Horn <jannh@google.com> Signed-off-by: Micah Morton <mortonm@chromium.org>
1 parent 8068866 commit 78ae7df

1 file changed

Lines changed: 33 additions & 51 deletions

File tree

‎security/safesetid/securityfs.c‎

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -33,68 +33,50 @@ static struct safesetid_file_entry safesetid_files[] = {
3333

3434
/*
3535
* In the case the input buffer contains one or more invalid UIDs, the kuid_t
36-
* variables pointed to by 'parent' and 'child' will get updated but this
36+
* variables pointed to by @parent and @child will get updated but this
3737
* function will return an error.
38+
* Contents of @buf may be modified.
3839
*/
39-
static int parse_safesetid_whitelist_policy(const char __user *buf,
40-
size_t len,
41-
kuid_t *parent,
42-
kuid_t *child)
40+
static int parse_policy_line(
41+
struct file *file, char *buf, kuid_t *parent, kuid_t *child)
4342
{
44-
char *kern_buf;
45-
char *parent_buf;
46-
char *child_buf;
47-
const char separator[] = ":";
43+
char *child_str;
4844
int ret;
49-
size_t first_substring_length;
50-
long parsed_parent;
51-
long parsed_child;
45+
u32 parsed_parent, parsed_child;
5246

53-
/* Duplicate string from user memory and NULL-terminate */
54-
kern_buf = memdup_user_nul(buf, len);
55-
if (IS_ERR(kern_buf))
56-
return PTR_ERR(kern_buf);
57-
58-
/*
59-
* Format of |buf| string should be <UID>:<UID>.
60-
* Find location of ":" in kern_buf (copied from |buf|).
61-
*/
62-
first_substring_length = strcspn(kern_buf, separator);
63-
if (first_substring_length == 0 || first_substring_length == len) {
64-
ret = -EINVAL;
65-
goto free_kern;
66-
}
67-
68-
parent_buf = kmemdup_nul(kern_buf, first_substring_length, GFP_KERNEL);
69-
if (!parent_buf) {
70-
ret = -ENOMEM;
71-
goto free_kern;
72-
}
47+
/* Format of |buf| string should be <UID>:<UID>. */
48+
child_str = strchr(buf, ':');
49+
if (child_str == NULL)
50+
return -EINVAL;
51+
*child_str = '\0';
52+
child_str++;
7353

74-
ret = kstrtol(parent_buf, 0, &parsed_parent);
54+
ret = kstrtou32(buf, 0, &parsed_parent);
7555
if (ret)
76-
goto free_both;
56+
return ret;
7757

78-
child_buf = kern_buf + first_substring_length + 1;
79-
ret = kstrtol(child_buf, 0, &parsed_child);
58+
ret = kstrtou32(child_str, 0, &parsed_child);
8059
if (ret)
81-
goto free_both;
60+
return ret;
8261

8362
*parent = make_kuid(current_user_ns(), parsed_parent);
84-
if (!uid_valid(*parent)) {
85-
ret = -EINVAL;
86-
goto free_both;
87-
}
88-
8963
*child = make_kuid(current_user_ns(), parsed_child);
90-
if (!uid_valid(*child)) {
91-
ret = -EINVAL;
92-
goto free_both;
93-
}
64+
if (!uid_valid(*parent) || !uid_valid(*child))
65+
return -EINVAL;
9466

95-
free_both:
96-
kfree(parent_buf);
97-
free_kern:
67+
return 0;
68+
}
69+
70+
static int parse_safesetid_whitelist_policy(
71+
struct file *file, const char __user *buf, size_t len,
72+
kuid_t *parent, kuid_t *child)
73+
{
74+
char *kern_buf = memdup_user_nul(buf, len);
75+
int ret;
76+
77+
if (IS_ERR(kern_buf))
78+
return PTR_ERR(kern_buf);
79+
ret = parse_policy_line(file, kern_buf, parent, child);
9880
kfree(kern_buf);
9981
return ret;
10082
}
@@ -121,8 +103,8 @@ static ssize_t safesetid_file_write(struct file *file,
121103
flush_safesetid_whitelist_entries();
122104
break;
123105
case SAFESETID_WHITELIST_ADD:
124-
ret = parse_safesetid_whitelist_policy(buf, len, &parent,
125-
&child);
106+
ret = parse_safesetid_whitelist_policy(file, buf, len,
107+
&parent, &child);
126108
if (ret)
127109
return ret;
128110

0 commit comments

Comments
 (0)