Skip to content

Commit

Permalink
selinux: make more use of str_read() when loading the policy
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
cgzones authored and pcmoore committed Jan 8, 2025
1 parent 7491536 commit 01c2253
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
10 changes: 2 additions & 8 deletions security/selinux/ss/conditional.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,11 @@ int cond_read_bool(struct policydb *p, struct symtab *s, struct policy_file *fp)
goto err;

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

rc = -ENOMEM;
key = kmalloc(len + 1, GFP_KERNEL);
if (!key)
goto err;
rc = next_entry(key, fp, len);
rc = str_read(&key, GFP_KERNEL, fp, len);
if (rc)
goto err;
key[len] = '\0';

rc = symtab_insert(s, key, booldatum);
if (rc)
goto err;
Expand Down
22 changes: 8 additions & 14 deletions security/selinux/ss/policydb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ static int context_read_and_validate(struct context *c, struct policydb *p,
* binary representation file.
*/

static int str_read(char **strp, gfp_t flags, struct policy_file *fp, u32 len)
int str_read(char **strp, gfp_t flags, struct policy_file *fp, u32 len)
{
int rc;
char *str;
Expand Down Expand Up @@ -2473,24 +2473,18 @@ int policydb_read(struct policydb *p, struct policy_file *fp)
goto bad;
}

rc = -ENOMEM;
policydb_str = kmalloc(len + 1, GFP_KERNEL);
if (!policydb_str) {
pr_err("SELinux: unable to allocate memory for policydb "
"string of length %d\n",
len);
goto bad;
}

rc = next_entry(policydb_str, fp, len);
rc = str_read(&policydb_str, GFP_KERNEL, fp, len);
if (rc) {
pr_err("SELinux: truncated policydb string identifier\n");
kfree(policydb_str);
if (rc == -ENOMEM) {
pr_err("SELinux: unable to allocate memory for policydb string of length %d\n",
len);
} else {
pr_err("SELinux: truncated policydb string identifier\n");
}
goto bad;
}

rc = -EINVAL;
policydb_str[len] = '\0';
if (strcmp(policydb_str, POLICYDB_STRING)) {
pr_err("SELinux: policydb string %s does not match "
"my string %s\n",
Expand Down
2 changes: 2 additions & 0 deletions security/selinux/ss/policydb.h
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ static inline char *sym_name(struct policydb *p, unsigned int sym_num,
return p->sym_val_to_name[sym_num][element_nr];
}

extern int str_read(char **strp, gfp_t flags, struct policy_file *fp, u32 len);

extern u16 string_to_security_class(struct policydb *p, const char *name);
extern u32 string_to_av_perm(struct policydb *p, u16 tclass, const char *name);

Expand Down

0 comments on commit 01c2253

Please sign in to comment.