Blame SOURCES/0002-dmidecode-Don-t-use-memcpy-on-dev-mem-on-arm64.patch

abf46d
From 82497fa02d60757c2cfa645cf89a79abb1435273 Mon Sep 17 00:00:00 2001
abf46d
From: Jean Delvare <jdelvare@suse.de>
abf46d
Date: Fri, 16 Nov 2018 11:18:25 +0100
abf46d
Subject: [PATCH 2/5] dmidecode: Don't use memcpy on /dev/mem on arm64
abf46d
abf46d
On arm64, calling memcpy on /dev/mem will cause a bus error if the
abf46d
start and the end of the buffer are not aligned on a 64-bit boundary.
abf46d
Using option --no-sysfs triggers this.
abf46d
abf46d
Use a slow manual byte-by-byte copy in that case, to prevent the bus
abf46d
error. This is only a fallback path (at least on Linux) and not
abf46d
performance-critical anyway, as it is a one-time operation and DMI
abf46d
tables are usually not too large.
abf46d
abf46d
This fixes bug #55026:
abf46d
https://savannah.nongnu.org/bugs/index.php?55026
abf46d
abf46d
Signed-off-by: Jean Delvare <jdelvare@suse.de>
abf46d
Signed-off-by: Lianbo Jiang <lijiang@redhat.com>
abf46d
---
abf46d
 config.h |  5 +++++
abf46d
 util.c   | 14 +++++++++++++-
abf46d
 2 files changed, 18 insertions(+), 1 deletion(-)
abf46d
abf46d
diff --git a/config.h b/config.h
abf46d
index e39091fde502..423735537fa8 100644
abf46d
--- a/config.h
abf46d
+++ b/config.h
abf46d
@@ -26,4 +26,9 @@
abf46d
 #define ALIGNMENT_WORKAROUND
abf46d
 #endif
abf46d
 
abf46d
+/* Avoid unaligned memcpy on /dev/mem */
abf46d
+#ifdef __aarch64__
abf46d
+#define USE_SLOW_MEMCPY
abf46d
+#endif
abf46d
+
abf46d
 #endif
abf46d
diff --git a/util.c b/util.c
abf46d
index eeffdae8e536..04aaadd5b913 100644
abf46d
--- a/util.c
abf46d
+++ b/util.c
abf46d
@@ -155,6 +155,18 @@ void *read_file(off_t base, size_t *max_len, const char *filename)
abf46d
 	return p;
abf46d
 }
abf46d
 
abf46d
+static void safe_memcpy(void *dest, const void *src, size_t n)
abf46d
+{
abf46d
+#ifdef USE_SLOW_MEMCPY
abf46d
+	size_t i;
abf46d
+
abf46d
+	for (i = 0; i < n; i++)
abf46d
+		*((u8 *)dest + i) = *((const u8 *)src + i);
abf46d
+#else
abf46d
+	memcpy(dest, src, n);
abf46d
+#endif
abf46d
+}
abf46d
+
abf46d
 /*
abf46d
  * Copy a physical memory chunk into a memory buffer.
abf46d
  * This function allocates memory.
abf46d
@@ -214,7 +226,7 @@ void *mem_chunk(off_t base, size_t len, const char *devmem)
abf46d
 	if (mmp == MAP_FAILED)
abf46d
 		goto try_read;
abf46d
 
abf46d
-	memcpy(p, (u8 *)mmp + mmoffset, len);
abf46d
+	safe_memcpy(p, (u8 *)mmp + mmoffset, len);
abf46d
 
abf46d
 	if (munmap(mmp, mmoffset + len) == -1)
abf46d
 	{
abf46d
-- 
abf46d
2.17.1
abf46d