Skip to content

Commit b31d3f7

Browse files
tobluxjrjohansen
authored andcommitted
apparmor: Replace sprintf/strcpy with scnprintf/strscpy in aa_policy_init
strcpy() is deprecated and sprintf() does not perform bounds checking either. Although an overflow is unlikely, it's better to proactively avoid it by using the safer strscpy() and scnprintf(), respectively. Additionally, unify memory allocation for 'hname' to simplify and improve aa_policy_init(). Closes: KSPP/linux#88 Reviewed-by: Serge Hallyn <[email protected]> Signed-off-by: Thorsten Blum <[email protected]> Signed-off-by: John Johansen <[email protected]>
1 parent 7db8c3c commit b31d3f7

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

security/apparmor/lib.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -478,19 +478,17 @@ bool aa_policy_init(struct aa_policy *policy, const char *prefix,
478478
const char *name, gfp_t gfp)
479479
{
480480
char *hname;
481+
size_t hname_sz;
481482

483+
hname_sz = (prefix ? strlen(prefix) + 2 : 0) + strlen(name) + 1;
482484
/* freed by policy_free */
483-
if (prefix) {
484-
hname = aa_str_alloc(strlen(prefix) + strlen(name) + 3, gfp);
485-
if (hname)
486-
sprintf(hname, "%s//%s", prefix, name);
487-
} else {
488-
hname = aa_str_alloc(strlen(name) + 1, gfp);
489-
if (hname)
490-
strcpy(hname, name);
491-
}
485+
hname = aa_str_alloc(hname_sz, gfp);
492486
if (!hname)
493487
return false;
488+
if (prefix)
489+
scnprintf(hname, hname_sz, "%s//%s", prefix, name);
490+
else
491+
strscpy(hname, name, hname_sz);
494492
policy->hname = hname;
495493
/* base.name is a substring of fqname */
496494
policy->name = basename(policy->hname);

0 commit comments

Comments
 (0)