linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/4] kasan: support stack instrumentation for tag-based mode
@ 2020-07-31 13:20 Andrey Konovalov
  2020-07-31 13:20 ` [PATCH 1/4] kasan: don't tag stacks allocated with pagealloc Andrey Konovalov
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Andrey Konovalov @ 2020-07-31 13:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Andrey Ryabinin, Alexander Potapenko, Dmitry Vyukov, Marco Elver,
	kasan-dev, linux-mm, linux-kernel, Walter Wu, Elena Petrova,
	Vincenzo Frascino, Catalin Marinas, Andrey Konovalov

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(-)

-- 
2.28.0.163.g6104cc2f0b6-goog



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/4] kasan: don't tag stacks allocated with pagealloc
  2020-07-31 13:20 [PATCH 0/4] kasan: support stack instrumentation for tag-based mode Andrey Konovalov
@ 2020-07-31 13:20 ` Andrey Konovalov
  2020-07-31 13:20 ` [PATCH 2/4] kasan, arm64: don't instrument functions that enable kasan Andrey Konovalov
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Andrey Konovalov @ 2020-07-31 13:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Andrey Ryabinin, Alexander Potapenko, Dmitry Vyukov, Marco Elver,
	kasan-dev, linux-mm, linux-kernel, Walter Wu, Elena Petrova,
	Vincenzo Frascino, Catalin Marinas, Andrey Konovalov

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.

Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
---
 kernel/fork.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/kernel/fork.c b/kernel/fork.c
index d03c9586d342..9cea2265e677 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -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;
 }
-- 
2.28.0.163.g6104cc2f0b6-goog



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/4] kasan, arm64: don't instrument functions that enable kasan
  2020-07-31 13:20 [PATCH 0/4] kasan: support stack instrumentation for tag-based mode Andrey Konovalov
  2020-07-31 13:20 ` [PATCH 1/4] kasan: don't tag stacks allocated with pagealloc Andrey Konovalov
@ 2020-07-31 13:20 ` Andrey Konovalov
  2020-07-31 14:21   ` Catalin Marinas
  2020-07-31 13:20 ` [PATCH 3/4] kasan: allow enabling stack tagging for tag-based mode Andrey Konovalov
  2020-07-31 13:20 ` [PATCH 4/4] kasan: adjust kasan_stack_oob " Andrey Konovalov
  3 siblings, 1 reply; 6+ messages in thread
From: Andrey Konovalov @ 2020-07-31 13:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Andrey Ryabinin, Alexander Potapenko, Dmitry Vyukov, Marco Elver,
	kasan-dev, linux-mm, linux-kernel, Walter Wu, Elena Petrova,
	Vincenzo Frascino, Catalin Marinas, 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).

Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
---
 arch/arm64/kernel/setup.c | 2 +-
 init/main.c               | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c
index c793276ec7ad..87e81d29e6fb 100644
--- a/arch/arm64/kernel/setup.c
+++ b/arch/arm64/kernel/setup.c
@@ -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;
diff --git a/init/main.c b/init/main.c
index 2d74985e09b1..c73a16ff213e 100644
--- a/init/main.c
+++ b/init/main.c
@@ -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;
-- 
2.28.0.163.g6104cc2f0b6-goog



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/4] kasan: allow enabling stack tagging for tag-based mode
  2020-07-31 13:20 [PATCH 0/4] kasan: support stack instrumentation for tag-based mode Andrey Konovalov
  2020-07-31 13:20 ` [PATCH 1/4] kasan: don't tag stacks allocated with pagealloc Andrey Konovalov
  2020-07-31 13:20 ` [PATCH 2/4] kasan, arm64: don't instrument functions that enable kasan Andrey Konovalov
@ 2020-07-31 13:20 ` Andrey Konovalov
  2020-07-31 13:20 ` [PATCH 4/4] kasan: adjust kasan_stack_oob " Andrey Konovalov
  3 siblings, 0 replies; 6+ messages in thread
From: Andrey Konovalov @ 2020-07-31 13:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Andrey Ryabinin, Alexander Potapenko, Dmitry Vyukov, Marco Elver,
	kasan-dev, linux-mm, linux-kernel, Walter Wu, Elena Petrova,
	Vincenzo Frascino, Catalin Marinas, 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

Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
---
 scripts/Makefile.kasan | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/scripts/Makefile.kasan b/scripts/Makefile.kasan
index 03757cc60e06..f4beee1b0013 100644
--- a/scripts/Makefile.kasan
+++ b/scripts/Makefile.kasan
@@ -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
-- 
2.28.0.163.g6104cc2f0b6-goog



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 4/4] kasan: adjust kasan_stack_oob for tag-based mode
  2020-07-31 13:20 [PATCH 0/4] kasan: support stack instrumentation for tag-based mode Andrey Konovalov
                   ` (2 preceding siblings ...)
  2020-07-31 13:20 ` [PATCH 3/4] kasan: allow enabling stack tagging for tag-based mode Andrey Konovalov
@ 2020-07-31 13:20 ` Andrey Konovalov
  3 siblings, 0 replies; 6+ messages in thread
From: Andrey Konovalov @ 2020-07-31 13:20 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Andrey Ryabinin, Alexander Potapenko, Dmitry Vyukov, Marco Elver,
	kasan-dev, linux-mm, linux-kernel, Walter Wu, Elena Petrova,
	Vincenzo Frascino, Catalin Marinas, Andrey Konovalov

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

Suggested-by: Walter Wu <walter-zh.wu@mediatek.com>
Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
---
 lib/test_kasan.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/test_kasan.c b/lib/test_kasan.c
index f362f2662938..53e953bb1d1d 100644
--- a/lib/test_kasan.c
+++ b/lib/test_kasan.c
@@ -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");
-- 
2.28.0.163.g6104cc2f0b6-goog



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/4] kasan, arm64: don't instrument functions that enable kasan
  2020-07-31 13:20 ` [PATCH 2/4] kasan, arm64: don't instrument functions that enable kasan Andrey Konovalov
@ 2020-07-31 14:21   ` Catalin Marinas
  0 siblings, 0 replies; 6+ messages in thread
From: Catalin Marinas @ 2020-07-31 14:21 UTC (permalink / raw)
  To: Andrey Konovalov
  Cc: Andrew Morton, Andrey Ryabinin, Alexander Potapenko,
	Dmitry Vyukov, Marco Elver, kasan-dev, linux-mm, linux-kernel,
	Walter Wu, Elena Petrova, Vincenzo Frascino

On Fri, Jul 31, 2020 at 03:20:39PM +0200, Andrey Konovalov wrote:
> 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).
> 
> Signed-off-by: Andrey Konovalov <andreyknvl@google.com>

For arm64:

Acked-by: Catalin Marinas <catalin.marinas@arm.com>


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-07-31 14:21 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-31 13:20 [PATCH 0/4] kasan: support stack instrumentation for tag-based mode Andrey Konovalov
2020-07-31 13:20 ` [PATCH 1/4] kasan: don't tag stacks allocated with pagealloc Andrey Konovalov
2020-07-31 13:20 ` [PATCH 2/4] kasan, arm64: don't instrument functions that enable kasan Andrey Konovalov
2020-07-31 14:21   ` Catalin Marinas
2020-07-31 13:20 ` [PATCH 3/4] kasan: allow enabling stack tagging for tag-based mode Andrey Konovalov
2020-07-31 13:20 ` [PATCH 4/4] kasan: adjust kasan_stack_oob " Andrey Konovalov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).