cf4a81
From 09288f2058c8066a2729588ec668eed3e1140028 Mon Sep 17 00:00:00 2001
cf4a81
From: Takao Indoh <indou.takao@jp.fujitsu.com>
cf4a81
Date: Thu, 26 Oct 2017 20:33:01 +0900
cf4a81
Subject: [PATCH 4/4] [PATCH v3 4/4] sadump: Fix a KASLR problem of sadump
cf4a81
 while kdump is working
cf4a81
cf4a81
In the calc_kaslr_offset(), kaslr_offset and phys_base are calculated
cf4a81
using IDTR and CR3, but this solution does not work in the following
cf4a81
cases.
cf4a81
cf4a81
1) If the dump is captured on early stage of kernel boot, IDTR points
cf4a81
   early IDT table(early_idts) instead of normal IDT(idt_table).
cf4a81
2) If the dump is captured whle kdump is working, IDTR points IDT table
cf4a81
   of 2nd kernel, not 1st kernel.
cf4a81
cf4a81
This patch fixes the case 2). Current implementation does not support
cf4a81
the case 1), need enhancement in the future. This patch gets kernel boot
cf4a81
parameter from "saved_command_line" and check if "elfcorehdr=" is
cf4a81
included in the parameter. If it's included, we are in the 2nd kernel.
cf4a81
Retrieve vmcoreinfo from address of "elfcorehdr=" and get kaslr_offset
cf4a81
and phys_base from vmcoreinfo.
cf4a81
cf4a81
Signed-off-by: Takao Indoh <indou.takao@jp.fujitsu.com>
cf4a81
---
cf4a81
 makedumpfile.c |   1 +
cf4a81
 makedumpfile.h |   1 +
cf4a81
 sadump_info.c  | 271 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cf4a81
 3 files changed, 273 insertions(+)
cf4a81
cf4a81
diff --git a/makedumpfile.c b/makedumpfile.c
cf4a81
index 41438a344574..6d5fc8b95415 100644
cf4a81
--- a/makedumpfile-1.6.2/makedumpfile.c
cf4a81
+++ b/makedumpfile-1.6.2/makedumpfile.c
cf4a81
@@ -1556,6 +1556,7 @@ get_symbol_info(void)
cf4a81
 
cf4a81
 	SYMBOL_INIT(divide_error, "divide_error");
cf4a81
 	SYMBOL_INIT(idt_table, "idt_table");
cf4a81
+	SYMBOL_INIT(saved_command_line, "saved_command_line");
cf4a81
 
cf4a81
 	return TRUE;
cf4a81
 }
cf4a81
diff --git a/makedumpfile.h b/makedumpfile.h
cf4a81
index 5f814e7bc3c1..db753792bca6 100644
cf4a81
--- a/makedumpfile-1.6.2/makedumpfile.h
cf4a81
+++ b/makedumpfile-1.6.2/makedumpfile.h
cf4a81
@@ -1602,6 +1602,7 @@ struct symbol_table {
cf4a81
 	unsigned long long	kexec_crash_image;
cf4a81
 	unsigned long long	divide_error;
cf4a81
 	unsigned long long	idt_table;
cf4a81
+	unsigned long long	saved_command_line;
cf4a81
 
cf4a81
 	/*
cf4a81
 	 * symbols on ppc64 arch
cf4a81
diff --git a/sadump_info.c b/sadump_info.c
cf4a81
index 29ccef881370..148d4baaa538 100644
cf4a81
--- a/makedumpfile-1.6.2/sadump_info.c
cf4a81
+++ b/makedumpfile-1.6.2/sadump_info.c
cf4a81
@@ -1059,6 +1059,241 @@ get_vec0_addr(ulong idtr)
cf4a81
 }
cf4a81
 
cf4a81
 /*
cf4a81
+ * Parse a string of [size[KMG]@]offset[KMG]
cf4a81
+ * Import from Linux kernel(lib/cmdline.c)
cf4a81
+ */
cf4a81
+static ulong memparse(char *ptr, char **retptr)
cf4a81
+{
cf4a81
+	char *endptr;
cf4a81
+
cf4a81
+	unsigned long long ret = strtoull(ptr, &endptr, 0);
cf4a81
+
cf4a81
+	switch (*endptr) {
cf4a81
+	case 'E':
cf4a81
+	case 'e':
cf4a81
+		ret <<= 10;
cf4a81
+	case 'P':
cf4a81
+	case 'p':
cf4a81
+		ret <<= 10;
cf4a81
+	case 'T':
cf4a81
+	case 't':
cf4a81
+		ret <<= 10;
cf4a81
+	case 'G':
cf4a81
+	case 'g':
cf4a81
+		ret <<= 10;
cf4a81
+	case 'M':
cf4a81
+	case 'm':
cf4a81
+		ret <<= 10;
cf4a81
+	case 'K':
cf4a81
+	case 'k':
cf4a81
+		ret <<= 10;
cf4a81
+		endptr++;
cf4a81
+	default:
cf4a81
+		break;
cf4a81
+	}
cf4a81
+
cf4a81
+	if (retptr)
cf4a81
+		*retptr = endptr;
cf4a81
+
cf4a81
+	return ret;
cf4a81
+}
cf4a81
+
cf4a81
+/*
cf4a81
+ * Find "elfcorehdr=" in the boot parameter of kernel and return the address
cf4a81
+ * of elfcorehdr.
cf4a81
+ */
cf4a81
+static ulong
cf4a81
+get_elfcorehdr(ulong cr3)
cf4a81
+{
cf4a81
+	char cmdline[BUFSIZE], *ptr;
cf4a81
+	ulong cmdline_vaddr;
cf4a81
+	ulong cmdline_paddr;
cf4a81
+	ulong buf_vaddr, buf_paddr;
cf4a81
+	char *end;
cf4a81
+	ulong elfcorehdr_addr = 0, elfcorehdr_size = 0;
cf4a81
+
cf4a81
+	if (SYMBOL(saved_command_line) == NOT_FOUND_SYMBOL) {
cf4a81
+		ERRMSG("Can't get the symbol of saved_command_line.\n");
cf4a81
+		return 0;
cf4a81
+	}
cf4a81
+	cmdline_vaddr = SYMBOL(saved_command_line);
cf4a81
+	if ((cmdline_paddr = vtop4_x86_64_pagetable(cmdline_vaddr, cr3)) == NOT_PADDR)
cf4a81
+		return 0;
cf4a81
+
cf4a81
+	DEBUG_MSG("sadump: cmdline vaddr: %lx\n", cmdline_vaddr);
cf4a81
+	DEBUG_MSG("sadump: cmdline paddr: %lx\n", cmdline_paddr);
cf4a81
+
cf4a81
+	if (!readmem(PADDR, cmdline_paddr, &buf_vaddr, sizeof(ulong)))
cf4a81
+		return 0;
cf4a81
+
cf4a81
+	if ((buf_paddr = vtop4_x86_64_pagetable(buf_vaddr, cr3)) == NOT_PADDR)
cf4a81
+		return 0;
cf4a81
+
cf4a81
+	DEBUG_MSG("sadump: cmdline buf vaddr: %lx\n", buf_vaddr);
cf4a81
+	DEBUG_MSG("sadump: cmdline buf paddr: %lx\n", buf_paddr);
cf4a81
+
cf4a81
+	memset(cmdline, 0, BUFSIZE);
cf4a81
+	if (!readmem(PADDR, buf_paddr, cmdline, BUFSIZE))
cf4a81
+		return 0;
cf4a81
+
cf4a81
+	ptr = strstr(cmdline, "elfcorehdr=");
cf4a81
+	if (!ptr)
cf4a81
+		return 0;
cf4a81
+
cf4a81
+	DEBUG_MSG("sadump: 2nd kernel detected.\n");
cf4a81
+
cf4a81
+	ptr += strlen("elfcorehdr=");
cf4a81
+	elfcorehdr_addr = memparse(ptr, &end;;
cf4a81
+	if (*end == '@') {
cf4a81
+		elfcorehdr_size = elfcorehdr_addr;
cf4a81
+		elfcorehdr_addr = memparse(end + 1, &end;;
cf4a81
+	}
cf4a81
+
cf4a81
+	DEBUG_MSG("sadump: elfcorehdr_addr: %lx\n", elfcorehdr_addr);
cf4a81
+	DEBUG_MSG("sadump: elfcorehdr_size: %lx\n", elfcorehdr_size);
cf4a81
+
cf4a81
+	return elfcorehdr_addr;
cf4a81
+}
cf4a81
+
cf4a81
+/*
cf4a81
+ * Get vmcoreinfo from elfcorehdr.
cf4a81
+ * Some codes are imported from Linux kernel(fs/proc/vmcore.c)
cf4a81
+ */
cf4a81
+static int
cf4a81
+get_vmcoreinfo_in_kdump_kernel(ulong elfcorehdr, ulong *addr, int *len)
cf4a81
+{
cf4a81
+	unsigned char e_ident[EI_NIDENT];
cf4a81
+	Elf64_Ehdr ehdr;
cf4a81
+	Elf64_Phdr phdr;
cf4a81
+	Elf64_Nhdr nhdr;
cf4a81
+	ulong ptr;
cf4a81
+	ulong nhdr_offset = 0;
cf4a81
+	int i;
cf4a81
+
cf4a81
+	if (!readmem(PADDR, elfcorehdr, e_ident, EI_NIDENT))
cf4a81
+		return FALSE;
cf4a81
+
cf4a81
+	if (e_ident[EI_CLASS] != ELFCLASS64) {
cf4a81
+		ERRMSG("Only ELFCLASS64 is supportd\n");
cf4a81
+		return FALSE;
cf4a81
+	}
cf4a81
+
cf4a81
+	if (!readmem(PADDR, elfcorehdr, &ehdr, sizeof(ehdr)))
cf4a81
+		return FALSE;
cf4a81
+
cf4a81
+	/* Sanity Check */
cf4a81
+	if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
cf4a81
+		(ehdr.e_type != ET_CORE) ||
cf4a81
+		ehdr.e_ident[EI_CLASS] != ELFCLASS64 ||
cf4a81
+		ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
cf4a81
+		ehdr.e_version != EV_CURRENT ||
cf4a81
+		ehdr.e_ehsize != sizeof(Elf64_Ehdr) ||
cf4a81
+		ehdr.e_phentsize != sizeof(Elf64_Phdr) ||
cf4a81
+		ehdr.e_phnum == 0) {
cf4a81
+		ERRMSG("Invalid elf header\n");
cf4a81
+		return FALSE;
cf4a81
+	}
cf4a81
+
cf4a81
+	ptr = elfcorehdr + ehdr.e_phoff;
cf4a81
+	for (i = 0; i < ehdr.e_phnum; i++) {
cf4a81
+		ulong offset;
cf4a81
+		char name[16];
cf4a81
+
cf4a81
+		if (!readmem(PADDR, ptr, &phdr, sizeof(phdr)))
cf4a81
+			return FALSE;
cf4a81
+
cf4a81
+		ptr += sizeof(phdr);
cf4a81
+		if (phdr.p_type != PT_NOTE)
cf4a81
+			continue;
cf4a81
+
cf4a81
+		offset = phdr.p_offset;
cf4a81
+		if (!readmem(PADDR, offset, &nhdr, sizeof(nhdr)))
cf4a81
+			return FALSE;
cf4a81
+
cf4a81
+		offset += divideup(sizeof(Elf64_Nhdr), sizeof(Elf64_Word))*
cf4a81
+			  sizeof(Elf64_Word);
cf4a81
+		memset(name, 0, sizeof(name));
cf4a81
+		if (!readmem(PADDR, offset, name, sizeof(name)))
cf4a81
+			return FALSE;
cf4a81
+
cf4a81
+		if(!strcmp(name, "VMCOREINFO")) {
cf4a81
+			nhdr_offset = offset;
cf4a81
+			break;
cf4a81
+		}
cf4a81
+	}
cf4a81
+
cf4a81
+	if (!nhdr_offset)
cf4a81
+		return FALSE;
cf4a81
+
cf4a81
+	*addr = nhdr_offset +
cf4a81
+		divideup(nhdr.n_namesz, sizeof(Elf64_Word))*
cf4a81
+		sizeof(Elf64_Word);
cf4a81
+	*len = nhdr.n_descsz;
cf4a81
+
cf4a81
+	DEBUG_MSG("sadump: vmcoreinfo addr: %lx\n", *addr);
cf4a81
+	DEBUG_MSG("sadump: vmcoreinfo len:  %d\n", *len);
cf4a81
+
cf4a81
+	return TRUE;
cf4a81
+}
cf4a81
+
cf4a81
+/*
cf4a81
+ * Check if current kaslr_offset/phys_base is for 1st kernel or 2nd kernel.
cf4a81
+ * If we are in 2nd kernel, get kaslr_offset/phys_base from vmcoreinfo.
cf4a81
+ *
cf4a81
+ * 1. Get command line and try to retrieve "elfcorehdr=" boot parameter
cf4a81
+ * 2. If "elfcorehdr=" is not found in command line, we are in 1st kernel.
cf4a81
+ *    There is nothing to do.
cf4a81
+ * 3. If "elfcorehdr=" is found, we are in 2nd kernel. Find vmcoreinfo
cf4a81
+ *    using "elfcorehdr=" and retrieve kaslr_offset/phys_base from vmcoreinfo.
cf4a81
+ */
cf4a81
+int
cf4a81
+get_kaslr_offset_from_vmcoreinfo(ulong cr3, ulong *kaslr_offset,
cf4a81
+				 ulong *phys_base)
cf4a81
+{
cf4a81
+	ulong elfcorehdr_addr = 0;
cf4a81
+	ulong vmcoreinfo_addr;
cf4a81
+	int vmcoreinfo_len;
cf4a81
+	char *buf, *pos;
cf4a81
+	int ret = FALSE;
cf4a81
+
cf4a81
+	elfcorehdr_addr = get_elfcorehdr(cr3);
cf4a81
+	if (!elfcorehdr_addr)
cf4a81
+		return FALSE;
cf4a81
+
cf4a81
+	if (!get_vmcoreinfo_in_kdump_kernel(elfcorehdr_addr, &vmcoreinfo_addr,
cf4a81
+					    &vmcoreinfo_len))
cf4a81
+		return FALSE;
cf4a81
+
cf4a81
+	if (!vmcoreinfo_len)
cf4a81
+		return FALSE;
cf4a81
+
cf4a81
+	DEBUG_MSG("sadump: Find vmcoreinfo in kdump memory\n");
cf4a81
+
cf4a81
+	if (!(buf = malloc(vmcoreinfo_len))) {
cf4a81
+		ERRMSG("Can't allocate vmcoreinfo buffer.\n");
cf4a81
+		return FALSE;
cf4a81
+	}
cf4a81
+
cf4a81
+	if (!readmem(PADDR, vmcoreinfo_addr, buf, vmcoreinfo_len))
cf4a81
+		goto finish;
cf4a81
+
cf4a81
+	pos = strstr(buf, STR_NUMBER("phys_base"));
cf4a81
+	if (!pos)
cf4a81
+		goto finish;
cf4a81
+	*phys_base  = strtoull(pos + strlen(STR_NUMBER("phys_base")), NULL, 0);
cf4a81
+
cf4a81
+	pos = strstr(buf, STR_KERNELOFFSET);
cf4a81
+	if (!pos)
cf4a81
+		goto finish;
cf4a81
+	*kaslr_offset = strtoull(pos + strlen(STR_KERNELOFFSET), NULL, 16);
cf4a81
+	ret = TRUE;
cf4a81
+
cf4a81
+finish:
cf4a81
+	free(buf);
cf4a81
+	return ret;
cf4a81
+}
cf4a81
+
cf4a81
+/*
cf4a81
  * Calculate kaslr_offset and phys_base
cf4a81
  *
cf4a81
  * kaslr_offset:
cf4a81
@@ -1106,6 +1341,26 @@ get_vec0_addr(ulong idtr)
cf4a81
  *
cf4a81
  * Note that the address (A) cannot be used instead of (E) because (A) is
cf4a81
  * not direct map address, it's a fixed map address.
cf4a81
+ *
cf4a81
+ * This solution works in most every case, but does not work in the
cf4a81
+ * following case.
cf4a81
+ *
cf4a81
+ * 1) If the dump is captured on early stage of kernel boot, IDTR points
cf4a81
+ *    early IDT table(early_idts) instead of normal IDT(idt_table).
cf4a81
+ * 2) If the dump is captured whle kdump is working, IDTR points
cf4a81
+ *    IDT table of 2nd kernel, not 1st kernel.
cf4a81
+ *
cf4a81
+ * Current implementation does not support the case 1), need
cf4a81
+ * enhancement in the future. For the case 2), get kaslr_offset and
cf4a81
+ * phys_base as follows.
cf4a81
+ *
cf4a81
+ * 1) Get kaslr_offset and phys_base using the above solution.
cf4a81
+ * 2) Get kernel boot parameter from "saved_command_line"
cf4a81
+ * 3) If "elfcorehdr=" is not included in boot parameter, we are in the
cf4a81
+ *    first kernel, nothing to do any more.
cf4a81
+ * 4) If "elfcorehdr=" is included in boot parameter, we are in the 2nd
cf4a81
+ *    kernel. Retrieve vmcoreinfo from address of "elfcorehdr=" and
cf4a81
+ *    get kaslr_offset and phys_base from vmcoreinfo.
cf4a81
  */
cf4a81
 int
cf4a81
 calc_kaslr_offset(void)
cf4a81
@@ -1116,6 +1371,7 @@ calc_kaslr_offset(void)
cf4a81
 	int apicid;
cf4a81
 	unsigned long divide_error_vmcore, divide_error_vmlinux;
cf4a81
 	unsigned long kaslr_offset, phys_base;
cf4a81
+	unsigned long kaslr_offset_kdump, phys_base_kdump;
cf4a81
 
cf4a81
 	memset(&zero, 0, sizeof(zero));
cf4a81
 	for (apicid = 0; apicid < sh->nr_cpus; ++apicid) {
cf4a81
@@ -1161,6 +1417,21 @@ calc_kaslr_offset(void)
cf4a81
 	if (!get_symbol_info())
cf4a81
 		return FALSE;
cf4a81
 
cf4a81
+	/*
cf4a81
+	 * Check if current kaslr_offset/phys_base is for 1st kernel or 2nd
cf4a81
+	 * kernel. If we are in 2nd kernel, get kaslr_offset/phys_base
cf4a81
+	 * from vmcoreinfo
cf4a81
+	 */
cf4a81
+	if (get_kaslr_offset_from_vmcoreinfo(cr3, &kaslr_offset_kdump,
cf4a81
+					    &phys_base_kdump)) {
cf4a81
+		info->kaslr_offset = kaslr_offset_kdump;
cf4a81
+		info->phys_base = phys_base_kdump;
cf4a81
+
cf4a81
+		/* Reload symbol */
cf4a81
+		if (!get_symbol_info())
cf4a81
+			return FALSE;
cf4a81
+	}
cf4a81
+
cf4a81
 	DEBUG_MSG("sadump: kaslr_offset=%lx\n", info->kaslr_offset);
cf4a81
 	DEBUG_MSG("sadump: phys_base=%lx\n", info->phys_base);
cf4a81
 
cf4a81
-- 
cf4a81
2.5.5
cf4a81