mirror of
https://gitlab.com/apparmor/apparmor
synced 2025-09-01 14:55:10 +00:00
libapparmor: Don't leak memory after a realloc(3) failure
realloc() returns NULL when it fails. Using the same pointer to specify the buffer to reallocate *and* to store realloc()'s return value will result in a leak of the previously allocated buffer upon error. These issues were discovered by cppcheck. Note that 'buffer' in write_policy_fd_to_iface() has the autofree attribute so it must not be manually freed if the realloc(3) fails as it'll be automatically freed. Signed-off-by: Tyler Hicks <tyhicks@canonical.com> Acked-by: Seth Arnold <seth.arnold@canonical.com>
This commit is contained in:
@@ -288,10 +288,15 @@ int aa_getprocattr(pid_t tid, const char *attr, char **label, char **mode)
|
|||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
char *tmp;
|
||||||
|
|
||||||
size <<= 1;
|
size <<= 1;
|
||||||
buffer = realloc(buffer, size);
|
tmp = realloc(buffer, size);
|
||||||
if (!buffer)
|
if (!tmp) {
|
||||||
|
free(buffer);
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
buffer = tmp;
|
||||||
memset(buffer, 0, size);
|
memset(buffer, 0, size);
|
||||||
|
|
||||||
rc = aa_getprocattr_raw(tid, attr, buffer, size, mode);
|
rc = aa_getprocattr_raw(tid, attr, buffer, size, mode);
|
||||||
@@ -645,10 +650,15 @@ int aa_getpeercon(int fd, char **label, char **mode)
|
|||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
char *tmp;
|
||||||
|
|
||||||
last_size = size;
|
last_size = size;
|
||||||
buffer = realloc(buffer, size);
|
tmp = realloc(buffer, size);
|
||||||
if (!buffer)
|
if (!tmp) {
|
||||||
|
free(buffer);
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
buffer = tmp;
|
||||||
memset(buffer, 0, size);
|
memset(buffer, 0, size);
|
||||||
|
|
||||||
rc = aa_getpeercon_raw(fd, buffer, &size, mode);
|
rc = aa_getpeercon_raw(fd, buffer, &size, mode);
|
||||||
|
@@ -159,13 +159,15 @@ static int write_policy_fd_to_iface(aa_kernel_interface *kernel_interface,
|
|||||||
|
|
||||||
do {
|
do {
|
||||||
if (asize - size == 0) {
|
if (asize - size == 0) {
|
||||||
buffer = realloc(buffer, chunksize);
|
char *tmp = realloc(buffer, chunksize);
|
||||||
|
|
||||||
asize = chunksize;
|
asize = chunksize;
|
||||||
chunksize <<= 1;
|
chunksize <<= 1;
|
||||||
if (!buffer) {
|
if (!tmp) {
|
||||||
errno = ENOMEM;
|
errno = ENOMEM;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
buffer = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
rsize = read(fd, buffer + size, asize - size);
|
rsize = read(fd, buffer + size, asize - size);
|
||||||
|
Reference in New Issue
Block a user