Blame SOURCES/0003-gcore-fix-memory-allocation-failure-during-processin.patch

37748b
From 4cb65a0d9168778d120920418b968d05da10989f Mon Sep 17 00:00:00 2001
37748b
From: HATAYAMA Daisuke <d.hatayama@fujitsu.com>
37748b
Date: Fri, 25 Feb 2022 04:59:48 -0500
37748b
Subject: [PATCH 3/8] gcore: fix memory allocation failure during processing
37748b
 NT_AUXV note
37748b
37748b
For crash dumps generated using kernel-4.18.0-365.el8 or later on
37748b
CentOS stream 8, crash gcore command fails as follows:
37748b
37748b
    crash> gcore -v 7 -f 128 10604
37748b
    gcore: Opening file core.10604.test-dumpfilter ...
37748b
    gcore: done.
37748b
    gcore: Writing ELF header ...
37748b
    gcore:  done.
37748b
    gcore: Retrieving and writing note information ...
37748b
    gcore: zero-size memory allocation! (called from 7fd558ce1e05)
37748b
    Failed.
37748b
37748b
This memory allocation failure occurs in fill_auxv_note() that creates
37748b
NT_AUXV note due to saved_auxv entries of size and offset tables are
37748b
somehow 0.
37748b
37748b
This is because during the merge of the upstream kernel commit
37748b
1c33bb0507508af24fd754dd7123bd8e997fab2f (x86/elf: Support a new ELF
37748b
aux vector AT_MINSIGSTKSZ), location of saved_auxv of struct mm_struct
37748b
has been moved as workaround in order to avoid kABI breakage.
37748b
37748b
Fix this by using RHEL-specific location for saved_auxv if there is
37748b
member rh_reserved_saved_auxv in struct mm_struct.
37748b
37748b
Signed-off-by: HATAYAMA Daisuke <d.hatayama@fujitsu.com>
37748b
Signed-off-by: Lianbo Jiang <lijiang@redhat.com>
37748b
---
37748b
 src/libgcore/gcore_coredump.c | 54 +++++++++++++++++++++++++++++------
37748b
 1 file changed, 46 insertions(+), 8 deletions(-)
37748b
37748b
diff --git a/src/libgcore/gcore_coredump.c b/src/libgcore/gcore_coredump.c
37748b
index 6f57b21b62b6..c14cc116e951 100644
37748b
--- a/src/libgcore/gcore_coredump.c
37748b
+++ b/src/libgcore/gcore_coredump.c
37748b
@@ -18,6 +18,10 @@
37748b
 
37748b
 static struct elf_note_info *elf_note_info_init(void);
37748b
 
37748b
+static void get_auxv_size_addr(struct task_context *tc,
37748b
+			       size_t *size,
37748b
+			       ulong *addr);
37748b
+
37748b
 static void fill_prstatus_note(struct elf_note_info *info,
37748b
 			       struct task_context *tc,
37748b
 			       struct memelfnote *memnote);
37748b
@@ -923,18 +927,49 @@ compat_fill_prstatus_note(struct elf_note_info *info,
37748b
 
37748b
 #endif /* GCORE_ARCH_COMPAT */
37748b
 
37748b
+static void get_auxv_size_addr(struct task_context *tc,
37748b
+			       size_t *psize,
37748b
+			       ulong *paddr)
37748b
+{
37748b
+	size_t size;
37748b
+	ulong addr;
37748b
+
37748b
+	if (MEMBER_EXISTS("mm_struct", "rh_reserved_saved_auxv")) {
37748b
+		ulong mm_rh;
37748b
+
37748b
+		size = MEMBER_SIZE("mm_struct_rh", "saved_auxv");
37748b
+		readmem(task_mm(tc->task, FALSE) + MEMBER_OFFSET("mm_struct", "mm_rh"),
37748b
+			KVADDR,
37748b
+			&mm_rh,
37748b
+			sizeof(mm_rh),
37748b
+			"mm_struct mm_rh",
37748b
+			gcore_verbose_error_handle());
37748b
+		addr = mm_rh + MEMBER_OFFSET("mm_struct_rh", "saved_auxv");
37748b
+	} else {
37748b
+		size = MEMBER_SIZE("mm_struct", "saved_auxv");
37748b
+		addr = task_mm(tc->task, FALSE) +
37748b
+			MEMBER_OFFSET("mm_struct", "saved_auxv");
37748b
+	}
37748b
+
37748b
+	*psize = size;
37748b
+	*paddr = addr;
37748b
+}
37748b
+
37748b
 static void
37748b
 fill_auxv_note(struct elf_note_info *info, struct task_context *tc,
37748b
 	       struct memelfnote *memnote)
37748b
 {
37748b
 	ulong *auxv;
37748b
+	ulong addr;
37748b
+	size_t size;
37748b
 	int i;
37748b
 
37748b
-	auxv = (ulong *)GETBUF(MEMBER_SIZE("mm_struct", "saved_auxv"));
37748b
+	get_auxv_size_addr(tc, &size, &addr);
37748b
 
37748b
-	readmem(task_mm(tc->task, FALSE) +
37748b
-		MEMBER_OFFSET("mm_struct", "saved_auxv"), KVADDR, auxv,
37748b
-		MEMBER_SIZE("mm_struct", "saved_auxv"), "fill_auxv_note",
37748b
+	auxv = (ulong *)GETBUF(size);
37748b
+
37748b
+	readmem(addr, KVADDR, auxv,
37748b
+		size, "fill_auxv_note",
37748b
 		gcore_verbose_error_handle());
37748b
 
37748b
 	i = 0;
37748b
@@ -954,13 +989,16 @@ compat_fill_auxv_note(struct elf_note_info *info,
37748b
 		      struct memelfnote *memnote)
37748b
 {
37748b
 	uint32_t *auxv;
37748b
+	ulong addr;
37748b
+	size_t size;
37748b
 	int i;
37748b
 
37748b
-	auxv = (uint32_t *)GETBUF(MEMBER_SIZE("mm_struct", "saved_auxv"));
37748b
+	get_auxv_size_addr(tc, &size, &addr);
37748b
+
37748b
+	auxv = (uint32_t *)GETBUF(size);
37748b
 
37748b
-	readmem(task_mm(tc->task, FALSE) +
37748b
-		MEMBER_OFFSET("mm_struct", "saved_auxv"), KVADDR, auxv,
37748b
-		MEMBER_SIZE("mm_struct", "saved_auxv"), "fill_auxv_note32",
37748b
+	readmem(addr, KVADDR, auxv,
37748b
+		size, "fill_auxv_note32",
37748b
 		gcore_verbose_error_handle());
37748b
 
37748b
 	i = 0;
37748b
-- 
37748b
2.37.1
37748b