Skip to content

Commit 8e14b42

Browse files
cgzonesgregkh
authored andcommitted
selinux: make more use of str_read() when loading the policy
[ Upstream commit 01c2253 ] Simplify the call sites, and enable future string validation in a single place. Signed-off-by: Christian Göttsche <cgzones@googlemail.com> [PM: subject tweak] Signed-off-by: Paul Moore <paul@paul-moore.com> (cherry picked from commit 01c2253) Signed-off-by: Wentao Guan <guanwentao@uniontech.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 778fdda commit 8e14b42

3 files changed

Lines changed: 12 additions & 22 deletions

File tree

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
@@ -1107,7 +1107,7 @@ static int context_read_and_validate(struct context *c, struct policydb *p,
11071107
* binary representation file.
11081108
*/
11091109

1110-
static int str_read(char **strp, gfp_t flags, struct policy_file *fp, u32 len)
1110+
int str_read(char **strp, gfp_t flags, struct policy_file *fp, u32 len)
11111111
{
11121112
int rc;
11131113
char *str;
@@ -2499,24 +2499,18 @@ int policydb_read(struct policydb *p, struct policy_file *fp)
24992499
goto bad;
25002500
}
25012501

2502-
rc = -ENOMEM;
2503-
policydb_str = kmalloc(len + 1, GFP_KERNEL);
2504-
if (!policydb_str) {
2505-
pr_err("SELinux: unable to allocate memory for policydb "
2506-
"string of length %d\n",
2507-
len);
2508-
goto bad;
2509-
}
2510-
2511-
rc = next_entry(policydb_str, fp, len);
2502+
rc = str_read(&policydb_str, GFP_KERNEL, fp, len);
25122503
if (rc) {
2513-
pr_err("SELinux: truncated policydb string identifier\n");
2514-
kfree(policydb_str);
2504+
if (rc == -ENOMEM) {
2505+
pr_err("SELinux: unable to allocate memory for policydb string of length %d\n",
2506+
len);
2507+
} else {
2508+
pr_err("SELinux: truncated policydb string identifier\n");
2509+
}
25152510
goto bad;
25162511
}
25172512

25182513
rc = -EINVAL;
2519-
policydb_str[len] = '\0';
25202514
if (strcmp(policydb_str, POLICYDB_STRING)) {
25212515
pr_err("SELinux: policydb string %s does not match "
25222516
"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)