Skip to contentSkip to conversation

kasan: support stack instrumentation for tag-based mode

PATCH Superseded Andrey Konovalov 2 participants 6 messages Jul 31, 2020

Subsystems (8)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover kasan: support stack instrumentation for tag-based mode

This goes on top of Walter's patch titled "kasan: fix KASAN unit tests for tag-based KASAN" (already in mm tree).

Bugzilla link: https://bugzilla.kernel.org/show_bug.cgi?id=203497

Thanks to Walter Wu for debugging and testing.

Andrey Konovalov (4):
kasan: don't tag stacks allocated with pagealloc kasan, arm64: don't instrument functions that enable kasan
kasan: allow enabling stack tagging for tag-based mode
kasan: adjust kasan_stack_oob for tag-based mode

 arch/arm64/kernel/setup.c | 2 +-
 init/main.c               | 2 +-
 kernel/fork.c             | 3 ++-
 lib/test_kasan.c          | 2 +-
 scripts/Makefile.kasan    | 3 ++-
 5 files changed, 7 insertions(+), 5 deletions(-)
Signature · 1 line
2.28.0.163.g6104cc2f0b6-goog
#1 lore raw

PATCH 1/4 kasan: don't tag stacks allocated with pagealloc

This patch prepares Software Tag-Based KASAN for stack tagging support.

With Tag-Based KASAN when kernel stacks are allocated via pagealloc (which happens when CONFIG_VMAP_STACK is not enabled), they get tagged. KASAN instrumentation doesn't expect the sp register to be tagged, and this leads to false-positive reports.

Fix by resetting the tag of kernel stack pointers after allocation.

---
 kernel/fork.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/fork.c +2 -1
index d03c9586d342..9cea2265e677 100644
@@ -261,7 +261,7 @@ static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node)
 					     THREAD_SIZE_ORDER);
 
 	if (likely(page)) {
-		tsk->stack = page_address(page);
+		tsk->stack = kasan_reset_tag(page_address(page));
 		return tsk->stack;
 	}
 	return NULL;
@@ -302,6 +302,7 @@ static unsigned long *alloc_thread_stack_node(struct task_struct *tsk,
 {
 	unsigned long *stack;
 	stack = kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node);
+	stack = kasan_reset_tag(stack);
 	tsk->stack = stack;
 	return stack;
 }
Signature · 1 line
2.28.0.163.g6104cc2f0b6-goog
#2 lore raw

PATCH 2/4 kasan, arm64: don't instrument functions that enable kasan

In reply to Andrey Konovalov

This patch prepares Software Tag-Based KASAN for stack tagging support.

With stack tagging enabled, KASAN tags stack variable in each function in its prologue. In start_kernel() stack variables get tagged before KASAN is enabled via setup_arch()->kasan_init(). As the result the tags for start_kernel()'s stack variables end up in the temporary shadow memory. Later when KASAN gets enabled, switched to normal shadow, and starts checking tags, this leads to false-positive reports, as proper tags are missing in normal shadow.

Disable KASAN instrumentation for start_kernel(). Also disable it for arm64's setup_arch() as a precaution (it doesn't have any stack variables right now).

---
 arch/arm64/kernel/setup.c | 2 +-
 init/main.c               | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kernel/setup.c +1 -1
index c793276ec7ad..87e81d29e6fb 100644
@@ -276,7 +276,7 @@ arch_initcall(reserve_memblock_reserved_regions);
 
 u64 __cpu_logical_map[NR_CPUS] = { [0 ... NR_CPUS-1] = INVALID_HWID };
 
-void __init setup_arch(char **cmdline_p)
+void __init __no_sanitize_address setup_arch(char **cmdline_p)
 {
 	init_mm.start_code = (unsigned long) _text;
 	init_mm.end_code   = (unsigned long) _etext;
init/main.c +1 -1
index 2d74985e09b1..c73a16ff213e 100644
@@ -829,7 +829,7 @@ void __init __weak arch_call_rest_init(void)
 	rest_init();
 }
 
-asmlinkage __visible void __init start_kernel(void)
+asmlinkage __visible __no_sanitize_address void __init start_kernel(void)
 {
 	char *command_line;
 	char *after_dashes;
Signature · 1 line
2.28.0.163.g6104cc2f0b6-goog
#4 lore raw

PATCH 3/4 kasan: allow enabling stack tagging for tag-based mode

In reply to Andrey Konovalov

Use CONFIG_KASAN_STACK to enable stack tagging.

Note, that HWASAN short granules [1] are disabled. Supporting those will require more kernel changes.

[1] https://clang.llvm.org/docs/HardwareAssistedAddressSanitizerDesign.html

---
 scripts/Makefile.kasan | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
scripts/Makefile.kasan +2 -1
index 03757cc60e06..f4beee1b0013 100644
@@ -44,7 +44,8 @@ else
 endif
 
 CFLAGS_KASAN := -fsanitize=kernel-hwaddress \
-		-mllvm -hwasan-instrument-stack=0 \
+		-mllvm -hwasan-instrument-stack=$(CONFIG_KASAN_STACK) \
+		-mllvm -hwasan-use-short-granules=0 \
 		$(instrumentation_flags)
 
 endif # CONFIG_KASAN_SW_TAGS
Signature · 1 line
2.28.0.163.g6104cc2f0b6-goog
#5 lore raw

PATCH 4/4 kasan: adjust kasan_stack_oob for tag-based mode

In reply to Andrey Konovalov

Use OOB_TAG_OFF as access offset to land the access into the next granule.

---
 lib/test_kasan.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/test_kasan.c +1 -1
index f362f2662938..53e953bb1d1d 100644
@@ -488,7 +488,7 @@ static noinline void __init kasan_global_oob(void)
 static noinline void __init kasan_stack_oob(void)
 {
 	char stack_array[10];
-	volatile int i = 0;
+	volatile int i = OOB_TAG_OFF;
 	char *p = &stack_array[ARRAY_SIZE(stack_array) + i];
 
 	pr_info("out-of-bounds on stack\n");
Signature · 1 line
2.28.0.163.g6104cc2f0b6-goog
maintainer Acked-by on v1 2/4
#3 lore raw

Re: [PATCH 2/4] kasan, arm64: don't instrument functions that enable kasan

In reply to Andrey Konovalov
Quoted 10 lines · Andrey Konovalov

This patch prepares Software Tag-Based KASAN for stack tagging support.

With stack tagging enabled, KASAN tags stack variable in each function in its prologue. In start_kernel() stack variables get tagged before KASAN is enabled via setup_arch()->kasan_init(). As the result the tags for start_kernel()'s stack variables end up in the temporary shadow memory. Later when KASAN gets enabled, switched to normal shadow, and starts checking tags, this leads to false-positive reports, as proper tags are missing in normal shadow.

Quoted 5 lines

Disable KASAN instrumentation for start_kernel(). Also disable it for arm64's setup_arch() as a precaution (it doesn't have any stack variables right now).

For arm64: