Skip to content

Commit

Permalink
selinux: free str on error in str_read()
Browse files Browse the repository at this point in the history
In [see "Fixes:"] I missed the fact that str_read() may give back an
allocated pointer even if it returns an error, causing a potential
memory leak in filename_trans_read_one(). Fix this by making the
function free the allocated string whenever it returns a non-zero value,
which also makes its behavior more obvious and prevents repeating the
same mistake in the future.

Reported-by: coverity-bot <keescook+coverity-bot@chromium.org>
Addresses-Coverity-ID: 1461665 ("Resource leaks")
Fixes: c3a2761 ("selinux: optimize storage of filename transitions")
Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Paul Moore <paul@paul-moore.com>
  • Loading branch information
Ondrej Mosnacek authored and Paul Moore committed Apr 15, 2020
1 parent c753924 commit af15f14
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions security/selinux/ss/policydb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1035,14 +1035,14 @@ static int str_read(char **strp, gfp_t flags, void *fp, u32 len)
if (!str)
return -ENOMEM;

/* it's expected the caller should free the str */
*strp = str;

rc = next_entry(str, fp, len);
if (rc)
if (rc) {
kfree(str);
return rc;
}

str[len] = '\0';
*strp = str;
return 0;
}

Expand Down

0 comments on commit af15f14

Please sign in to comment.