Skip to content

Commit 01c2253

Browse files
cgzonespcmoore
authored andcommitted
selinux: make more use of str_read() when loading the policy
Simplify the call sites, and enable future string validation in a single place. Signed-off-by: Christian Göttsche <[email protected]> [PM: subject tweak] Signed-off-by: Paul Moore <[email protected]>
1 parent 7491536 commit 01c2253

File tree

3 files changed

+12
-22
lines changed

3 files changed

+12
-22
lines changed

security/selinux/ss/conditional.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,17 +230,11 @@ int cond_read_bool(struct policydb *p, struct symtab *s, struct policy_file *fp)
230230
goto err;
231231

232232
len = le32_to_cpu(buf[2]);
233-
if (((len == 0) || (len == (u32)-1)))
234-
goto err;
235233

236-
rc = -ENOMEM;
237-
key = kmalloc(len + 1, GFP_KERNEL);
238-
if (!key)
239-
goto err;
240-
rc = next_entry(key, fp, len);
234+
rc = str_read(&key, GFP_KERNEL, fp, len);
241235
if (rc)
242236
goto err;
243-
key[len] = '\0';
237+
244238
rc = symtab_insert(s, key, booldatum);
245239
if (rc)
246240
goto err;

security/selinux/ss/policydb.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ static int context_read_and_validate(struct context *c, struct policydb *p,
10931093
* binary representation file.
10941094
*/
10951095

1096-
static int str_read(char **strp, gfp_t flags, struct policy_file *fp, u32 len)
1096+
int str_read(char **strp, gfp_t flags, struct policy_file *fp, u32 len)
10971097
{
10981098
int rc;
10991099
char *str;
@@ -2473,24 +2473,18 @@ int policydb_read(struct policydb *p, struct policy_file *fp)
24732473
goto bad;
24742474
}
24752475

2476-
rc = -ENOMEM;
2477-
policydb_str = kmalloc(len + 1, GFP_KERNEL);
2478-
if (!policydb_str) {
2479-
pr_err("SELinux: unable to allocate memory for policydb "
2480-
"string of length %d\n",
2481-
len);
2482-
goto bad;
2483-
}
2484-
2485-
rc = next_entry(policydb_str, fp, len);
2476+
rc = str_read(&policydb_str, GFP_KERNEL, fp, len);
24862477
if (rc) {
2487-
pr_err("SELinux: truncated policydb string identifier\n");
2488-
kfree(policydb_str);
2478+
if (rc == -ENOMEM) {
2479+
pr_err("SELinux: unable to allocate memory for policydb string of length %d\n",
2480+
len);
2481+
} else {
2482+
pr_err("SELinux: truncated policydb string identifier\n");
2483+
}
24892484
goto bad;
24902485
}
24912486

24922487
rc = -EINVAL;
2493-
policydb_str[len] = '\0';
24942488
if (strcmp(policydb_str, POLICYDB_STRING)) {
24952489
pr_err("SELinux: policydb string %s does not match "
24962490
"my string %s\n",

security/selinux/ss/policydb.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ static inline char *sym_name(struct policydb *p, unsigned int sym_num,
386386
return p->sym_val_to_name[sym_num][element_nr];
387387
}
388388

389+
extern int str_read(char **strp, gfp_t flags, struct policy_file *fp, u32 len);
390+
389391
extern u16 string_to_security_class(struct policydb *p, const char *name);
390392
extern u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name);
391393

0 commit comments

Comments
 (0)