[PATCH v4 1/7] x86/kasan: generate KASAN_SHADOW_OFFSET in Makefile

Andrey Ryabinin a.ryabinin at samsung.com
Fri Jul 24 09:41:53 PDT 2015


ARM64 has several different address space layouts and its
going to have one more at least. Different address space layouts
have different shadow offsets, so every new layout require adding
new default value for CONFIG_KASAN_SHADOW_OFFSET.
It's possible to generate KASAN_SHADOW_OFFSET in Makefile, so
the shadow address for every possible layout will be auto-generated.

However, we should do this in x86 too, because generic code
depend on having CONFIG_KASAN_SHADOW_OFFSET.
There is no functional changes here.

Signed-off-by: Andrey Ryabinin <a.ryabinin at samsung.com>
---
 arch/x86/Kconfig             |  5 -----
 arch/x86/Makefile            |  2 ++
 arch/x86/include/asm/kasan.h | 21 +++++++++++++--------
 include/linux/kasan.h        |  1 -
 scripts/Makefile.kasan       |  2 +-
 5 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index b3a1a5d..6d6dd6f 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -255,11 +255,6 @@ config ARCH_SUPPORTS_OPTIMIZED_INLINING
 config ARCH_SUPPORTS_DEBUG_PAGEALLOC
 	def_bool y
 
-config KASAN_SHADOW_OFFSET
-	hex
-	depends on KASAN
-	default 0xdffffc0000000000
-
 config HAVE_INTEL_TXT
 	def_bool y
 	depends on INTEL_IOMMU && ACPI
diff --git a/arch/x86/Makefile b/arch/x86/Makefile
index 118e6de..c666989 100644
--- a/arch/x86/Makefile
+++ b/arch/x86/Makefile
@@ -39,6 +39,8 @@ ifdef CONFIG_X86_NEED_RELOCS
         LDFLAGS_vmlinux := --emit-relocs
 endif
 
+KASAN_SHADOW_OFFSET := 0xdffffc0000000000
+
 ifeq ($(CONFIG_X86_32),y)
         BITS := 32
         UTS_MACHINE := i386
diff --git a/arch/x86/include/asm/kasan.h b/arch/x86/include/asm/kasan.h
index 74a2a8d..88881f6 100644
--- a/arch/x86/include/asm/kasan.h
+++ b/arch/x86/include/asm/kasan.h
@@ -1,17 +1,22 @@
 #ifndef _ASM_X86_KASAN_H
 #define _ASM_X86_KASAN_H
 
-/*
- * Compiler uses shadow offset assuming that addresses start
- * from 0. Kernel addresses don't start from 0, so shadow
- * for kernel really starts from compiler's shadow offset +
- * 'kernel address space start' >> KASAN_SHADOW_SCALE_SHIFT
- */
-#define KASAN_SHADOW_START      (KASAN_SHADOW_OFFSET + \
-					(0xffff800000000000ULL >> 3))
+#define KASAN_SHADOW_START      (0xffffec0000000000ULL)
 /* 47 bits for kernel address -> (47 - 3) bits for shadow */
 #define KASAN_SHADOW_END        (KASAN_SHADOW_START + (1ULL << (47 - 3)))
 
+/*
+ * This value is used to map an address to the corresponding shadow
+ * address by the following formula:
+ *	shadow_addr = (address >> 3) + KASAN_SHADOW_OFFSET;
+ *
+ * (1 << 61) shadow addresses - [KASAN_SHADOW_OFFSET,KASAN_SHADOW_END]
+ * cover all 64-bits of virtual addresses. So KASAN_SHADOW_OFFSET
+ * should satisfy the following equation:
+ *      KASAN_SHADOW_OFFSET = KASAN_SHADOW_END - (1ULL << 61)
+ */
+#define KASAN_SHADOW_OFFSET (KASAN_SHADOW_END - (1UL << (64 - 3)))
+
 #ifndef __ASSEMBLY__
 
 #ifdef CONFIG_KASAN
diff --git a/include/linux/kasan.h b/include/linux/kasan.h
index 5486d77..6fb1c7d 100644
--- a/include/linux/kasan.h
+++ b/include/linux/kasan.h
@@ -10,7 +10,6 @@ struct vm_struct;
 #ifdef CONFIG_KASAN
 
 #define KASAN_SHADOW_SCALE_SHIFT 3
-#define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL)
 
 #include <asm/kasan.h>
 #include <linux/sched.h>
diff --git a/scripts/Makefile.kasan b/scripts/Makefile.kasan
index 3f874d2..19d9a61 100644
--- a/scripts/Makefile.kasan
+++ b/scripts/Makefile.kasan
@@ -8,7 +8,7 @@ endif
 CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
 
 CFLAGS_KASAN := $(call cc-option, -fsanitize=kernel-address \
-		-fasan-shadow-offset=$(CONFIG_KASAN_SHADOW_OFFSET) \
+		-fasan-shadow-offset=$(KASAN_SHADOW_OFFSET) \
 		--param asan-stack=1 --param asan-globals=1 \
 		--param asan-instrumentation-with-call-threshold=$(call_threshold))
 
-- 
2.4.5




More information about the linux-arm-kernel mailing list