]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
coredump: correctly take tmpfs size into account for compression
authorLuca Boccassi <bluca@debian.org>
Tue, 2 Jul 2024 14:28:47 +0000 (15:28 +0100)
committerLuca Boccassi <luca.boccassi@gmail.com>
Tue, 2 Jul 2024 23:15:03 +0000 (01:15 +0200)
We calculate the amount of uncompressed data we can write by taking the limits
into account and halving it to ensure there's room for switching to compression
on the fly when storing cores on a tmpfs (eg: due read-only rootfs).

But the logic is flawed, as taking into account the size of the tmpfs storage
was applied after the halving, so in practice when an uncompressed core file
was larger than the tmpfs, we fill it and then fail.

Rearrange the logic so that the halving is done after taking into account
the tmpfs size.

src/coredump/coredump.c

index 9a1f3c23fe57f7399e1a3efc613d93213a241b15..843a500d8c08bbfb14cdb276762d50e388596761 100644 (file)
@@ -505,17 +505,21 @@ static int save_external_coredump(
                                                   bus_error_message(&error, r));
                 }
 
+                /* First, ensure we are not going to go over the cgroup limit */
                 max_size = MIN(cgroup_limit, max_size);
-                max_size = LESS_BY(max_size, 1024U) / 2; /* Account for 1KB metadata overhead for compressing */
-                max_size = MAX(PROCESS_SIZE_MIN, max_size); /* Impose a lower minimum */
-
-                /* tmpfs might get full quickly, so check the available space too.
-                 * But don't worry about errors here, failing to access the storage
-                 * location will be better logged when writing to it. */
+                /* tmpfs might get full quickly, so check the available space too. But don't worry about
+                 * errors here, failing to access the storage location will be better logged when writing to
+                 * it. */
                 if (fstatvfs(fd, &sv) >= 0)
                         max_size = MIN((uint64_t)sv.f_frsize * (uint64_t)sv.f_bfree, max_size);
-
-                log_debug("Limiting core file size to %" PRIu64 " bytes due to cgroup memory limits.", max_size);
+                /* Impose a lower minimum, otherwise we will miss the basic headers. */
+                max_size = MAX(PROCESS_SIZE_MIN, max_size);
+                /* Ensure we can always switch to compressing on the fly in case we are running out of space
+                 * by keeping half of the space/memory available, plus 1KB metadata overhead from the
+                 * compression algorithm. */
+                max_size = LESS_BY(max_size, 1024U) / 2;
+
+                log_debug("Limiting core file size to %" PRIu64 " bytes due to cgroup and/or filesystem limits.", max_size);
         }
 
         r = copy_bytes(input_fd, fd, max_size, 0);