mirror of
https://github.com/morgan9e/systemd
synced 2026-04-14 08:25:20 +09:00
Merge pull request #27770 from mrc0mmand/more-nallocfuzz-shenanigans
A couple of fixes for potential issues during OOM situations
This commit is contained in:
@@ -330,8 +330,7 @@ static int parse_env_file_push(
|
||||
|
||||
if (streq(key, k)) {
|
||||
va_end(aq);
|
||||
free(*v);
|
||||
*v = value;
|
||||
free_and_replace(*v, value);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "alloc-util.h"
|
||||
#include "escape.h"
|
||||
#include "extract-word.h"
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
#include "gunicode.h"
|
||||
#include "locale-util.h"
|
||||
@@ -603,9 +604,9 @@ char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) {
|
||||
STATE_CSI,
|
||||
STATE_CSO,
|
||||
} state = STATE_OTHER;
|
||||
char *obuf = NULL;
|
||||
_cleanup_free_ char *obuf = NULL;
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
size_t osz = 0, isz, shift[2] = {}, n_carriage_returns = 0;
|
||||
FILE *f;
|
||||
|
||||
assert(ibuf);
|
||||
assert(*ibuf);
|
||||
@@ -713,11 +714,13 @@ char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]) {
|
||||
}
|
||||
}
|
||||
|
||||
if (fflush_and_check(f) < 0) {
|
||||
fclose(f);
|
||||
return mfree(obuf);
|
||||
}
|
||||
fclose(f);
|
||||
if (fflush_and_check(f) < 0)
|
||||
return NULL;
|
||||
|
||||
f = safe_fclose(f);
|
||||
|
||||
if (!obuf)
|
||||
return NULL;
|
||||
|
||||
free_and_replace(*ibuf, obuf);
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -95,6 +95,9 @@ int manager_get_dump_string(Manager *m, char **patterns, char **ret) {
|
||||
|
||||
f = safe_fclose(f);
|
||||
|
||||
if (!dump)
|
||||
return -ENOMEM;
|
||||
|
||||
*ret = TAKE_PTR(dump);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -707,6 +707,9 @@ static int compose_open_fds(pid_t pid, char **open_fds) {
|
||||
if (errno > 0)
|
||||
return -errno;
|
||||
|
||||
if (!buffer)
|
||||
return -ENOMEM;
|
||||
|
||||
*open_fds = TAKE_PTR(buffer);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -1811,6 +1811,10 @@ static int format_journal_url(
|
||||
return r;
|
||||
|
||||
f = safe_fclose(f);
|
||||
|
||||
if (!url)
|
||||
return -ENOMEM;
|
||||
|
||||
*ret_url = TAKE_PTR(url);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -268,6 +268,10 @@ int introspect_finish(struct introspect *i, char **ret) {
|
||||
return r;
|
||||
|
||||
i->f = safe_fclose(i->f);
|
||||
|
||||
if (!i->introspection)
|
||||
return -ENOMEM;
|
||||
|
||||
*ret = TAKE_PTR(i->introspection);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -824,6 +824,7 @@ int bus_match_parse(
|
||||
|
||||
char *bus_match_to_string(struct bus_match_component *components, size_t n_components) {
|
||||
_cleanup_free_ char *buffer = NULL;
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
size_t size = 0;
|
||||
int r;
|
||||
|
||||
@@ -832,7 +833,7 @@ char *bus_match_to_string(struct bus_match_component *components, size_t n_compo
|
||||
|
||||
assert(components);
|
||||
|
||||
FILE *f = open_memstream_unlocked(&buffer, &size);
|
||||
f = open_memstream_unlocked(&buffer, &size);
|
||||
if (!f)
|
||||
return NULL;
|
||||
|
||||
@@ -855,9 +856,11 @@ char *bus_match_to_string(struct bus_match_component *components, size_t n_compo
|
||||
}
|
||||
|
||||
r = fflush_and_check(f);
|
||||
safe_fclose(f);
|
||||
if (r < 0)
|
||||
return NULL;
|
||||
|
||||
f = safe_fclose(f);
|
||||
|
||||
return TAKE_PTR(buffer);
|
||||
}
|
||||
|
||||
|
||||
@@ -145,7 +145,9 @@ static int finish_item(
|
||||
char *payload, size_t payload_size) {
|
||||
|
||||
_cleanup_free_ CatalogItem *i = NULL;
|
||||
_cleanup_free_ char *prev = NULL, *combined = NULL;
|
||||
_cleanup_free_ char *combined = NULL;
|
||||
char *prev;
|
||||
int r;
|
||||
|
||||
assert(h);
|
||||
assert(payload);
|
||||
@@ -168,19 +170,24 @@ static int finish_item(
|
||||
if (!combined)
|
||||
return log_oom();
|
||||
|
||||
if (ordered_hashmap_update(h, i, combined) < 0)
|
||||
return log_oom();
|
||||
combined = NULL;
|
||||
r = ordered_hashmap_update(h, i, combined);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
TAKE_PTR(combined);
|
||||
free(prev);
|
||||
} else {
|
||||
/* A new item */
|
||||
combined = memdup(payload, payload_size + 1);
|
||||
if (!combined)
|
||||
return log_oom();
|
||||
|
||||
if (ordered_hashmap_put(h, i, combined) < 0)
|
||||
return log_oom();
|
||||
i = NULL;
|
||||
combined = NULL;
|
||||
r = ordered_hashmap_put(h, i, combined);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
TAKE_PTR(i);
|
||||
TAKE_PTR(combined);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -1199,30 +1199,31 @@ void link_dump(Link *link, FILE *f) {
|
||||
|
||||
int network_format(Network *network, char **ret) {
|
||||
_cleanup_free_ char *s = NULL;
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
size_t sz = 0;
|
||||
int r;
|
||||
|
||||
assert(network);
|
||||
assert(ret);
|
||||
|
||||
{
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
f = open_memstream_unlocked(&s, &sz);
|
||||
if (!f)
|
||||
return -ENOMEM;
|
||||
|
||||
f = open_memstream_unlocked(&s, &sz);
|
||||
if (!f)
|
||||
return -ENOMEM;
|
||||
network_dump(network, f);
|
||||
|
||||
network_dump(network, f);
|
||||
/* Add terminating 0, so that the output buffer is a valid string. */
|
||||
fputc('\0', f);
|
||||
|
||||
/* Add terminating 0, so that the output buffer is a valid string. */
|
||||
fputc('\0', f);
|
||||
|
||||
r = fflush_and_check(f);
|
||||
}
|
||||
r = fflush_and_check(f);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
assert(s);
|
||||
f = safe_fclose(f);
|
||||
|
||||
if (!s)
|
||||
return -ENOMEM;
|
||||
|
||||
*ret = TAKE_PTR(s);
|
||||
assert(sz > 0);
|
||||
return (int) sz - 1;
|
||||
@@ -1230,30 +1231,31 @@ int network_format(Network *network, char **ret) {
|
||||
|
||||
int netdev_format(NetDev *netdev, char **ret) {
|
||||
_cleanup_free_ char *s = NULL;
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
size_t sz = 0;
|
||||
int r;
|
||||
|
||||
assert(netdev);
|
||||
assert(ret);
|
||||
|
||||
{
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
f = open_memstream_unlocked(&s, &sz);
|
||||
if (!f)
|
||||
return -ENOMEM;
|
||||
|
||||
f = open_memstream_unlocked(&s, &sz);
|
||||
if (!f)
|
||||
return -ENOMEM;
|
||||
netdev_dump(netdev, f);
|
||||
|
||||
netdev_dump(netdev, f);
|
||||
/* Add terminating 0, so that the output buffer is a valid string. */
|
||||
fputc('\0', f);
|
||||
|
||||
/* Add terminating 0, so that the output buffer is a valid string. */
|
||||
fputc('\0', f);
|
||||
|
||||
r = fflush_and_check(f);
|
||||
}
|
||||
r = fflush_and_check(f);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
assert(s);
|
||||
f = safe_fclose(f);
|
||||
|
||||
if (!s)
|
||||
return -ENOMEM;
|
||||
|
||||
*ret = TAKE_PTR(s);
|
||||
assert(sz > 0);
|
||||
return (int) sz - 1;
|
||||
@@ -1261,30 +1263,31 @@ int netdev_format(NetDev *netdev, char **ret) {
|
||||
|
||||
int link_format(Link *link, char **ret) {
|
||||
_cleanup_free_ char *s = NULL;
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
size_t sz = 0;
|
||||
int r;
|
||||
|
||||
assert(link);
|
||||
assert(ret);
|
||||
|
||||
{
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
f = open_memstream_unlocked(&s, &sz);
|
||||
if (!f)
|
||||
return -ENOMEM;
|
||||
|
||||
f = open_memstream_unlocked(&s, &sz);
|
||||
if (!f)
|
||||
return -ENOMEM;
|
||||
link_dump(link, f);
|
||||
|
||||
link_dump(link, f);
|
||||
/* Add terminating 0, so that the output buffer is a valid string. */
|
||||
fputc('\0', f);
|
||||
|
||||
/* Add terminating 0, so that the output buffer is a valid string. */
|
||||
fputc('\0', f);
|
||||
|
||||
r = fflush_and_check(f);
|
||||
}
|
||||
r = fflush_and_check(f);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
assert(s);
|
||||
f = safe_fclose(f);
|
||||
|
||||
if (!s)
|
||||
return -ENOMEM;
|
||||
|
||||
*ret = TAKE_PTR(s);
|
||||
assert(sz > 0);
|
||||
return (int) sz - 1;
|
||||
|
||||
@@ -847,6 +847,9 @@ int manager_get_dump_string(Manager *m, char **ret) {
|
||||
|
||||
f = safe_fclose(f);
|
||||
|
||||
if (!dump)
|
||||
return -ENOMEM;
|
||||
|
||||
*ret = TAKE_PTR(dump);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -299,6 +299,11 @@ static int dump_kill_candidates(OomdCGroupContext **sorted, int n, int dump_unti
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
f = safe_fclose(f);
|
||||
|
||||
if (!dump)
|
||||
return -ENOMEM;
|
||||
|
||||
return log_dump(LOG_INFO, dump);
|
||||
}
|
||||
|
||||
|
||||
@@ -867,10 +867,14 @@ static int dnssec_rrset_serialize_sig(
|
||||
}
|
||||
|
||||
r = fflush_and_check(f);
|
||||
f = safe_fclose(f); /* sig_data may be reallocated when f is closed. */
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
f = safe_fclose(f); /* sig_data may be reallocated when f is closed. */
|
||||
|
||||
if (!sig_data)
|
||||
return -ENOMEM;
|
||||
|
||||
*ret_sig_data = TAKE_PTR(sig_data);
|
||||
*ret_sig_size = sig_size;
|
||||
return 0;
|
||||
|
||||
@@ -1222,19 +1222,20 @@ int dns_resource_record_to_wire_format(DnsResourceRecord *rr, bool canonical) {
|
||||
return 0;
|
||||
|
||||
r = dns_packet_append_rr(&packet, rr, 0, &start, &rds);
|
||||
if (r < 0)
|
||||
if (r < 0) {
|
||||
dns_packet_unref(&packet);
|
||||
return r;
|
||||
}
|
||||
|
||||
assert(start == 0);
|
||||
assert(packet._data);
|
||||
|
||||
free(rr->wire_format);
|
||||
rr->wire_format = packet._data;
|
||||
rr->wire_format = TAKE_PTR(packet._data);
|
||||
rr->wire_format_size = packet.size;
|
||||
rr->wire_format_rdata_offset = rds;
|
||||
rr->wire_format_canonical = canonical;
|
||||
|
||||
packet._data = NULL;
|
||||
dns_packet_unref(&packet);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -519,6 +519,11 @@ static int manager_sigusr1(sd_event_source *s, const struct signalfd_siginfo *si
|
||||
if (fflush_and_check(f) < 0)
|
||||
return log_oom();
|
||||
|
||||
f = safe_fclose(f);
|
||||
|
||||
if (!buffer)
|
||||
return -ENOMEM;
|
||||
|
||||
log_dump(LOG_INFO, buffer);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -628,6 +628,9 @@ static int method_dump_memory_state_by_fd(sd_bus_message *message, void *userdat
|
||||
|
||||
dump_file = safe_fclose(dump_file);
|
||||
|
||||
if (!dump)
|
||||
return -ENOMEM;
|
||||
|
||||
fd = acquire_data_fd(dump, dump_size, 0);
|
||||
if (fd < 0)
|
||||
return fd;
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "alloc-util.h"
|
||||
#include "calendarspec.h"
|
||||
#include "errno-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
#include "macro.h"
|
||||
#include "parse-util.h"
|
||||
@@ -336,9 +337,9 @@ static void format_chain(FILE *f, int space, const CalendarComponent *c, bool us
|
||||
}
|
||||
|
||||
int calendar_spec_to_string(const CalendarSpec *c, char **p) {
|
||||
char *buf = NULL;
|
||||
_cleanup_free_ char *buf = NULL;
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
size_t sz = 0;
|
||||
FILE *f;
|
||||
int r;
|
||||
|
||||
assert(c);
|
||||
@@ -383,14 +384,15 @@ int calendar_spec_to_string(const CalendarSpec *c, char **p) {
|
||||
}
|
||||
|
||||
r = fflush_and_check(f);
|
||||
fclose(f);
|
||||
|
||||
if (r < 0) {
|
||||
free(buf);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
*p = buf;
|
||||
f = safe_fclose(f);
|
||||
|
||||
if (!buf)
|
||||
return -ENOMEM;
|
||||
|
||||
*p = TAKE_PTR(buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -621,8 +621,13 @@ static int parse_core(int fd, const char *executable, char **ret, JsonVariant **
|
||||
return log_warning_errno(r, "Could not parse core file, flushing file buffer failed: %m");
|
||||
|
||||
c.f = safe_fclose(c.f);
|
||||
|
||||
if (!buf)
|
||||
return log_oom();
|
||||
|
||||
*ret = TAKE_PTR(buf);
|
||||
}
|
||||
|
||||
if (ret_package_metadata)
|
||||
*ret_package_metadata = TAKE_PTR(package_metadata);
|
||||
|
||||
@@ -735,8 +740,13 @@ static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **r
|
||||
return log_warning_errno(r, "Could not parse ELF file, flushing file buffer failed: %m");
|
||||
|
||||
c.f = safe_fclose(c.f);
|
||||
|
||||
if (!buf)
|
||||
return log_oom();
|
||||
|
||||
*ret = TAKE_PTR(buf);
|
||||
}
|
||||
|
||||
if (ret_package_metadata)
|
||||
*ret_package_metadata = TAKE_PTR(elf_metadata);
|
||||
|
||||
|
||||
@@ -2537,6 +2537,9 @@ int table_format(Table *t, char **ret) {
|
||||
|
||||
f = safe_fclose(f);
|
||||
|
||||
if (!buf)
|
||||
return -ENOMEM;
|
||||
|
||||
*ret = TAKE_PTR(buf);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -1769,6 +1769,7 @@ static int json_format(FILE *f, JsonVariant *v, JsonFormatFlags flags, const cha
|
||||
|
||||
int json_variant_format(JsonVariant *v, JsonFormatFlags flags, char **ret) {
|
||||
_cleanup_free_ char *s = NULL;
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
size_t sz = 0;
|
||||
int r;
|
||||
|
||||
@@ -1781,26 +1782,26 @@ int json_variant_format(JsonVariant *v, JsonFormatFlags flags, char **ret) {
|
||||
if (flags & JSON_FORMAT_OFF)
|
||||
return -ENOEXEC;
|
||||
|
||||
{
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
f = open_memstream_unlocked(&s, &sz);
|
||||
if (!f)
|
||||
return -ENOMEM;
|
||||
|
||||
f = open_memstream_unlocked(&s, &sz);
|
||||
if (!f)
|
||||
return -ENOMEM;
|
||||
|
||||
r = json_variant_dump(v, flags, f, NULL);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
/* Add terminating 0, so that the output buffer is a valid string. */
|
||||
fputc('\0', f);
|
||||
|
||||
r = fflush_and_check(f);
|
||||
}
|
||||
r = json_variant_dump(v, flags, f, NULL);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
assert(s);
|
||||
/* Add terminating 0, so that the output buffer is a valid string. */
|
||||
fputc('\0', f);
|
||||
|
||||
r = fflush_and_check(f);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
f = safe_fclose(f);
|
||||
|
||||
if (!s)
|
||||
return -ENOMEM;
|
||||
|
||||
*ret = TAKE_PTR(s);
|
||||
assert(sz > 0);
|
||||
return (int) sz - 1;
|
||||
|
||||
@@ -284,7 +284,7 @@ int specifier_architecture(char specifier, const void *data, const char *root, c
|
||||
* installation. */
|
||||
|
||||
static int parse_os_release_specifier(const char *root, const char *id, char **ret) {
|
||||
char *v = NULL;
|
||||
_cleanup_free_ char *v = NULL;
|
||||
int r;
|
||||
|
||||
assert(ret);
|
||||
@@ -293,7 +293,7 @@ static int parse_os_release_specifier(const char *root, const char *id, char **r
|
||||
if (r >= 0)
|
||||
/* parse_os_release() calls parse_env_file() which only sets the return value for
|
||||
* entries found. Let's make sure we set the return value in all cases. */
|
||||
*ret = v;
|
||||
*ret = TAKE_PTR(v);
|
||||
|
||||
/* Translate error for missing os-release file to EUNATCH. */
|
||||
return r == -ENOENT ? -EUNATCH : r;
|
||||
|
||||
Reference in New Issue
Block a user