From a73c74db66a14540eb043bf9535c0d9c37804062 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 22 Jun 2023 16:48:48 +0200 Subject: [PATCH] coredump: use loop_read() for reading coredump into memory Fixes: #26748 --- src/basic/io-util.c | 3 +-- src/coredump/coredump.c | 11 ++++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/basic/io-util.c b/src/basic/io-util.c index 6f6fb8068c4..bf1aab9e388 100644 --- a/src/basic/io-util.c +++ b/src/basic/io-util.c @@ -54,8 +54,7 @@ ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) { assert(fd >= 0); - /* If called with nbytes == 0, let's call read() at least - * once, to validate the operation */ + /* If called with nbytes == 0, let's call read() at least once, to validate the operation */ if (nbytes > (size_t) SSIZE_MAX) return -EINVAL; diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index 847ac531dfb..940c0def61e 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -604,14 +604,15 @@ static int allocate_journal_field(int fd, size_t size, char **ret, size_t *ret_s return log_warning_errno(errno, "Failed to seek: %m"); field = malloc(9 + size); - if (!field) { - log_warning("Failed to allocate memory for coredump, coredump will not be stored."); - return -ENOMEM; - } + if (!field) + return log_warning_errno(SYNTHETIC_ERRNO(ENOMEM), + "Failed to allocate memory for coredump, coredump will not be stored."); memcpy(field, "COREDUMP=", 9); - n = read(fd, field + 9, size); + /* NB: simple read() would fail for overly large coredumps, since read() on Linux can only deal with + * 0x7ffff000 bytes max. Hence call things in a loop. */ + n = loop_read(fd, field + 9, size, /* do_poll= */ false); if (n < 0) return log_error_errno((int) n, "Failed to read core data: %m"); if ((size_t) n < size)