Skip to content

Commit 7db8c3c

Browse files
tobluxjrjohansen
authored andcommitted
apparmor: replace sprintf with snprintf in aa_new_learning_profile
Replace unbounded sprintf() calls with snprintf() to prevent potential buffer overflows in aa_new_learning_profile(). While the current code works correctly, snprintf() is safer and follows secure coding best practices. No functional changes. Signed-off-by: Thorsten Blum <[email protected]> Signed-off-by: John Johansen <[email protected]>
1 parent 8f0b4cc commit 7db8c3c

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

security/apparmor/policy.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -697,24 +697,27 @@ struct aa_profile *aa_new_learning_profile(struct aa_profile *parent, bool hat,
697697
struct aa_profile *p, *profile;
698698
const char *bname;
699699
char *name = NULL;
700+
size_t name_sz;
700701

701702
AA_BUG(!parent);
702703

703704
if (base) {
704-
name = kmalloc(strlen(parent->base.hname) + 8 + strlen(base),
705-
gfp);
705+
name_sz = strlen(parent->base.hname) + 8 + strlen(base);
706+
name = kmalloc(name_sz, gfp);
706707
if (name) {
707-
sprintf(name, "%s//null-%s", parent->base.hname, base);
708+
snprintf(name, name_sz, "%s//null-%s",
709+
parent->base.hname, base);
708710
goto name;
709711
}
710712
/* fall through to try shorter uniq */
711713
}
712714

713-
name = kmalloc(strlen(parent->base.hname) + 2 + 7 + 8, gfp);
715+
name_sz = strlen(parent->base.hname) + 2 + 7 + 8;
716+
name = kmalloc(name_sz, gfp);
714717
if (!name)
715718
return NULL;
716-
sprintf(name, "%s//null-%x", parent->base.hname,
717-
atomic_inc_return(&parent->ns->uniq_null));
719+
snprintf(name, name_sz, "%s//null-%x", parent->base.hname,
720+
atomic_inc_return(&parent->ns->uniq_null));
718721

719722
name:
720723
/* lookup to see if this is a dup creation */

0 commit comments

Comments
 (0)