]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
binfmt_elf: Dump smaller VMAs first in ELF cores
authorBrian Mak <makb@juniper.net>
Tue, 6 Aug 2024 18:16:02 +0000 (18:16 +0000)
committerKees Cook <kees@kernel.org>
Mon, 12 Aug 2024 18:23:25 +0000 (11:23 -0700)
Large cores may be truncated in some scenarios, such as with daemons
with stop timeouts that are not large enough or lack of disk space. This
impacts debuggability with large core dumps since critical information
necessary to form a usable backtrace, such as stacks and shared library
information, are omitted.

We attempted to figure out which VMAs are needed to create a useful
backtrace, and it turned out to be a non-trivial problem. Instead, we
try simply sorting the VMAs by size, which has the intended effect.

By sorting VMAs by dump size and dumping in that order, we have a
simple, yet effective heuristic.

Signed-off-by: Brian Mak <makb@juniper.net>
Link: https://lore.kernel.org/r/036CD6AE-C560-4FC7-9B02-ADD08E380DC9@juniper.net
Acked-by: "Eric W. Biederman" <ebiederm@xmission.com>
Signed-off-by: Kees Cook <kees@kernel.org>
fs/coredump.c

index 5814a6d781ce16f7a8f48976069b7a386da6650f..53a78b6bbb5b5bf4791bfa0ef47d8cb88e9d8fc2 100644 (file)
@@ -18,6 +18,7 @@
 #include <linux/personality.h>
 #include <linux/binfmts.h>
 #include <linux/coredump.h>
+#include <linux/sort.h>
 #include <linux/sched/coredump.h>
 #include <linux/sched/signal.h>
 #include <linux/sched/task_stack.h>
@@ -1249,6 +1250,18 @@ static void free_vma_snapshot(struct coredump_params *cprm)
        }
 }
 
+static int cmp_vma_size(const void *vma_meta_lhs_ptr, const void *vma_meta_rhs_ptr)
+{
+       const struct core_vma_metadata *vma_meta_lhs = vma_meta_lhs_ptr;
+       const struct core_vma_metadata *vma_meta_rhs = vma_meta_rhs_ptr;
+
+       if (vma_meta_lhs->dump_size < vma_meta_rhs->dump_size)
+               return -1;
+       if (vma_meta_lhs->dump_size > vma_meta_rhs->dump_size)
+               return 1;
+       return 0;
+}
+
 /*
  * Under the mmap_lock, take a snapshot of relevant information about the task's
  * VMAs.
@@ -1311,5 +1324,8 @@ static bool dump_vma_snapshot(struct coredump_params *cprm)
                cprm->vma_data_size += m->dump_size;
        }
 
+       sort(cprm->vma_meta, cprm->vma_count, sizeof(*cprm->vma_meta),
+               cmp_vma_size, NULL);
+
        return true;
 }