Fix use-after-free of 'name' in parser_regex.c

'name' gets used in the error message. Make sure it only gets freed
afterwards.

This bug was introduced in be0d2fa947 /
https://gitlab.com/apparmor/apparmor/-/merge_requests/727

Fixes coverity CID 254465:  Memory - illegal accesses  (USE_AFTER_FREE)
This commit is contained in:
Christian Boltz 2023-05-29 22:12:41 +02:00
parent d700f87d3e
commit e408d03a5b
Failed to generate hash of commit

View file

@ -486,13 +486,18 @@ static int process_profile_name_xmatch(Profile *prof)
&prof->xmatch_len);
if (ptype == ePatternBasic)
prof->xmatch_len = strlen(name);
if (!prof->attachment)
free(name);
if (ptype == ePatternInvalid) {
PERROR(_("%s: Invalid profile name '%s' - bad regular expression\n"), progname, name);
if (!prof->attachment)
free(name);
return FALSE;
} else if (ptype == ePatternBasic && !(prof->altnames || prof->attachment || prof->xattrs.list)) {
}
if (!prof->attachment)
free(name);
if (ptype == ePatternBasic && !(prof->altnames || prof->attachment || prof->xattrs.list)) {
/* no regex so do not set xmatch */
prof->xmatch = NULL;
prof->xmatch_len = 0;