Blob Blame History Raw
From c6992684b51ba9604d50016f36c9139bf54ae03b Mon Sep 17 00:00:00 2001
From: Kazuhito Hagio <k-hagio@ab.jp.nec.com>
Date: Tue, 17 Dec 2019 11:12:50 -0500
Subject: [PATCH 1/3] [PATCH] Generalize get_kaslr_offset_arm64() for other
 architectures

Generalize the get_kaslr_offset_arm64() and rename it to
get_kaslr_offset_general() for use by other architectures
supporting KASLR.

Also, modify the confusing comment in the function and its
x86_64 version.

Signed-off-by: Kazuhito Hagio <k-hagio@ab.jp.nec.com>
---
 arch/arm64.c   | 61 --------------------------------------------------
 arch/x86_64.c  |  8 +++----
 makedumpfile.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++
 makedumpfile.h |  4 ++--
 4 files changed, 66 insertions(+), 67 deletions(-)

diff --git a/makedumpfile-1.6.6/arch/arm64.c b/makedumpfile-1.6.6/arch/arm64.c
index 3516b340adfd..43164ccc32d4 100644
--- a/makedumpfile-1.6.6/arch/arm64.c
+++ b/makedumpfile-1.6.6/arch/arm64.c
@@ -205,67 +205,6 @@ get_phys_base_arm64(void)
 	return FALSE;
 }
 
-unsigned long
-get_kaslr_offset_arm64(unsigned long vaddr)
-{
-	unsigned int i;
-	char buf[BUFSIZE_FGETS], *endp;
-	static unsigned long _text = NOT_FOUND_SYMBOL;
-	static unsigned long _end = NOT_FOUND_SYMBOL;
-
-	if (!info->kaslr_offset && info->file_vmcoreinfo) {
-		if (fseek(info->file_vmcoreinfo, 0, SEEK_SET) < 0) {
-			ERRMSG("Can't seek the vmcoreinfo file(%s). %s\n",
-					info->name_vmcoreinfo, strerror(errno));
-			return FALSE;
-		}
-
-		while (fgets(buf, BUFSIZE_FGETS, info->file_vmcoreinfo)) {
-			i = strlen(buf);
-			if (!i)
-				break;
-			if (buf[i - 1] == '\n')
-				buf[i - 1] = '\0';
-			if (strncmp(buf, STR_KERNELOFFSET,
-					strlen(STR_KERNELOFFSET)) == 0) {
-				info->kaslr_offset =
-					strtoul(buf+strlen(STR_KERNELOFFSET),&endp,16);
-				DEBUG_MSG("info->kaslr_offset: %lx\n", info->kaslr_offset);
-			}
-		}
-	}
-	if (!info->kaslr_offset)
-		return 0;
-
-	if (_text == NOT_FOUND_SYMBOL) {
-		/*
-		 * Currently, the return value of this function is used in
-		 * resolve_config_entry() only, and in that case, we must
-		 * have a vmlinux.
-		 */
-		if (info->name_vmlinux) {
-			_text = get_symbol_addr("_text");
-			_end = get_symbol_addr("_end");
-		}
-		DEBUG_MSG("_text: %lx, _end: %lx\n", _text, _end);
-		if (_text == NOT_FOUND_SYMBOL || _end == NOT_FOUND_SYMBOL) {
-			ERRMSG("Cannot determine _text and _end address\n");
-			return FALSE;
-		}
-	}
-
-	if (_text <= vaddr && vaddr <= _end) {
-		DEBUG_MSG("info->kaslr_offset: %lx\n", info->kaslr_offset);
-		return info->kaslr_offset;
-	} else {
-		/*
-		 * TODO: we need to check if it is vmalloc/vmmemmap/module
-		 * address, we will have different offset
-		 */
-		return 0;
-	}
-}
-
 ulong
 get_stext_symbol(void)
 {
diff --git a/makedumpfile-1.6.6/arch/x86_64.c b/makedumpfile-1.6.6/arch/x86_64.c
index 876644f932be..7a2c05c36809 100644
--- a/makedumpfile-1.6.6/arch/x86_64.c
+++ b/makedumpfile-1.6.6/arch/x86_64.c
@@ -77,14 +77,14 @@ get_kaslr_offset_x86_64(unsigned long vaddr)
 	else
 		kernel_image_size = KERNEL_IMAGE_SIZE_KASLR_ORIG;
 
+	/*
+	 * Returns the kaslr offset only if the vaddr needs it to be added,
+	 * i.e. only kernel text address for now.  Otherwise returns 0.
+	 */
 	if (vaddr >= __START_KERNEL_map &&
 			vaddr < __START_KERNEL_map + kernel_image_size)
 		return info->kaslr_offset;
 	else
-		/*
-		 * TODO: we need to check if it is vmalloc/vmmemmap/module
-		 * address, we will have different offset
-		 */
 		return 0;
 }
 
diff --git a/makedumpfile-1.6.6/makedumpfile.c b/makedumpfile-1.6.6/makedumpfile.c
index 7586d7c001d4..332b804cd756 100644
--- a/makedumpfile-1.6.6/makedumpfile.c
+++ b/makedumpfile-1.6.6/makedumpfile.c
@@ -3945,6 +3945,66 @@ free_for_parallel()
 	}
 }
 
+unsigned long
+get_kaslr_offset_general(unsigned long vaddr)
+{
+	unsigned int i;
+	char buf[BUFSIZE_FGETS], *endp;
+	static unsigned long _text = NOT_FOUND_SYMBOL;
+	static unsigned long _end = NOT_FOUND_SYMBOL;
+
+	if (!info->kaslr_offset && info->file_vmcoreinfo) {
+		if (fseek(info->file_vmcoreinfo, 0, SEEK_SET) < 0) {
+			ERRMSG("Can't seek the vmcoreinfo file(%s). %s\n",
+				info->name_vmcoreinfo, strerror(errno));
+			return FALSE;
+		}
+
+		while (fgets(buf, BUFSIZE_FGETS, info->file_vmcoreinfo)) {
+			i = strlen(buf);
+			if (!i)
+				break;
+			if (buf[i - 1] == '\n')
+				buf[i - 1] = '\0';
+			if (strncmp(buf, STR_KERNELOFFSET,
+					strlen(STR_KERNELOFFSET)) == 0) {
+				info->kaslr_offset = strtoul(buf +
+					strlen(STR_KERNELOFFSET), &endp, 16);
+				DEBUG_MSG("info->kaslr_offset: %lx\n",
+					info->kaslr_offset);
+			}
+		}
+	}
+	if (!info->kaslr_offset)
+		return 0;
+
+	if (_text == NOT_FOUND_SYMBOL) {
+		/*
+		 * Currently, the return value of this function is used in
+		 * resolve_config_entry() only, and in that case, we must
+		 * have a vmlinux.
+		 */
+		if (info->name_vmlinux) {
+			_text = get_symbol_addr("_text");
+			_end = get_symbol_addr("_end");
+		}
+		DEBUG_MSG("_text: %lx, _end: %lx\n", _text, _end);
+		if (_text == NOT_FOUND_SYMBOL || _end == NOT_FOUND_SYMBOL) {
+			ERRMSG("Cannot determine _text and _end address\n");
+			return FALSE;
+		}
+	}
+
+	/*
+	 * Returns the kaslr offset only if the vaddr needs it to be added,
+	 * i.e. only kernel text address for now.  Otherwise returns 0.
+	 */
+	if (_text <= vaddr && vaddr <= _end)
+		return info->kaslr_offset;
+	else
+		return 0;
+}
+
 int
 find_kaslr_offsets()
 {
diff --git a/makedumpfile-1.6.6/makedumpfile.h b/makedumpfile-1.6.6/makedumpfile.h
index ac11e906b5b7..067fa483aa01 100644
--- a/makedumpfile-1.6.6/makedumpfile.h
+++ b/makedumpfile-1.6.6/makedumpfile.h
@@ -964,6 +964,7 @@ typedef unsigned long pgd_t;
 static inline int stub_true() { return TRUE; }
 static inline int stub_true_ul(unsigned long x) { return TRUE; }
 static inline int stub_false() { return FALSE; }
+unsigned long get_kaslr_offset_general(unsigned long vaddr);
 #define paddr_to_vaddr_general(X) ((X) + PAGE_OFFSET)
 
 #ifdef __aarch64__
@@ -973,7 +974,6 @@ unsigned long long vaddr_to_paddr_arm64(unsigned long vaddr);
 int get_versiondep_info_arm64(void);
 int get_xen_basic_info_arm64(void);
 int get_xen_info_arm64(void);
-unsigned long get_kaslr_offset_arm64(unsigned long vaddr);
 #define paddr_to_vaddr_arm64(X) (((X) - info->phys_base) | PAGE_OFFSET)
 
 #define find_vmemmap()		stub_false()
@@ -982,7 +982,7 @@ unsigned long get_kaslr_offset_arm64(unsigned long vaddr);
 #define get_phys_base()		get_phys_base_arm64()
 #define get_machdep_info()	get_machdep_info_arm64()
 #define get_versiondep_info()	get_versiondep_info_arm64()
-#define get_kaslr_offset(X)	get_kaslr_offset_arm64(X)
+#define get_kaslr_offset(X)	get_kaslr_offset_general(X)
 #define get_xen_basic_info_arch(X) get_xen_basic_info_arm64(X)
 #define get_xen_info_arch(X) get_xen_info_arm64(X)
 #define is_phys_addr(X)		stub_true_ul(X)
-- 
2.17.1