diff options
author | Frantisek Sumsal <frantisek@sumsal.cz> | 2023-05-24 13:29:52 +0200 |
---|---|---|
committer | Frantisek Sumsal <frantisek@sumsal.cz> | 2023-05-24 21:59:10 +0200 |
commit | f392dfb5a1286184189233a84f6d6871bd4f7ade (patch) | |
tree | 5ff4a2348b7157f6049544f7c30c2899e8652352 /src/busctl | |
parent | sd-journal: propagate errors from ordered_hashmap_*() (diff) | |
download | systemd-f392dfb5a1286184189233a84f6d6871bd4f7ade.tar.xz systemd-f392dfb5a1286184189233a84f6d6871bd4f7ade.zip |
tree-wide: check memstream buffer after closing the handle
When closing the FILE handle attached to a memstream, it may attempt to
do a realloc() that may fail during OOM situations, in which case we are
left with the buffer pointer pointing to NULL and buffer size > 0. For
example:
```
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
void *realloc(void *ptr, size_t size) {
return NULL;
}
int main(int argc, char *argv[])
{
FILE *f;
char *buf;
size_t sz = 0;
f = open_memstream(&buf, &sz);
if (!f)
return -ENOMEM;
fputs("Hello", f);
fflush(f);
printf("buf: 0x%lx, sz: %lu, errno: %d\n",
(unsigned long) buf, sz, errno);
fclose(f);
printf("buf: 0x%lx, sz: %lu, errno: %d\n",
(unsigned long) buf, sz, errno);
return 0;
}
```
```
$ gcc -o main main.c
$ ./main
buf: 0x74d4a0, sz: 5, errno: 0
buf: 0x0, sz: 5, errno: 0
```
This might do unexpected things if the underlying code expects a valid
pointer to the memstream buffer after closing the handle.
Found by Nallocfuzz.
Diffstat (limited to 'src/busctl')
-rw-r--r-- | src/busctl/busctl.c | 3 |
1 files changed, 3 insertions, 0 deletions
diff --git a/src/busctl/busctl.c b/src/busctl/busctl.c index 965ded9675..4d69aee5eb 100644 --- a/src/busctl/busctl.c +++ b/src/busctl/busctl.c @@ -1072,6 +1072,9 @@ static int introspect(int argc, char **argv, void *userdata) { mf = safe_fclose(mf); + if (!buf) + return bus_log_parse_error(ENOMEM); + z = set_get(members, &((Member) { .type = "property", .interface = m->interface, |