kasan: support stack instrumentation for tag-based mode
Subsystems (9)
v2 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.
Changes v1 -> v2:
- Add efi patch to undefined reference to `efi_enter_virtual_mode'.
- Reorder attributes for start_kernel().
Andrey Konovalov (5):
kasan: don't tag stacks allocated with pagealloc
efi: provide empty efi_enter_virtual_mode implementation 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 +- include/linux/efi.h | 4 ++++ init/main.c | 2 +- kernel/fork.c | 3 ++- lib/test_kasan.c | 2 +- scripts/Makefile.kasan | 3 ++- 6 files changed, 11 insertions(+), 5 deletions(-)
Signature · 1 line
2.28.0.163.g6104cc2f0b6-goog
PATCH v2 1/5 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
@@ -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 ;
@@ -307,6 +307,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
PATCH v2 2/5 efi: provide empty efi_enter_virtual_mode implementation
In reply to Andrey KonovalovWhen CONFIG_EFI is not enabled, we might get an undefined reference to efi_enter_virtual_mode() error, if this efi_enabled() call isn't inlined into start_kernel(). This happens in particular, if start_kernel() is annodated with __no_sanitize_address.
--- include/linux/efi.h | 4 ++++ 1 file changed, 4 insertions(+)
include/linux/efi.h +4 -0
@@ -606,7 +606,11 @@ extern void *efi_get_pal_addr (void);extern void efi_map_pal_code (void );extern void efi_memmap_walk (efi_freemem_callback_t callback,void *arg);extern void efi_gettimeofday (struct timespec64 *ts); +#ifdef CONFIG_EFI extern void efi_enter_virtual_mode (void );/* switch EFI to virtual mode, if possible */ +#else +static inline void efi_enter_virtual_mode (void ) {} +#endif #ifdef CONFIG_X86 extern efi_status_t efi_query_variable_store(u32 attributes, unsigned long size,
Signature · 1 line
2.28.0.163.g6104cc2f0b6-goog
Re: [PATCH v2 2/5] efi: provide empty efi_enter_virtual_mode implementation
Quoted 8 lines · Andrey Konovalov
When
CONFIG_EFIis not enabled, we might get an undefined reference toefi_enter_virtual_mode()error, if thisefi_enabled()call isn't inlined intostart_kernel(). This happens in particular, ifstart_kernel()is annodated with__no_sanitize_address.Reported-by kernel test robot <[email protected]>Signed-off-by Andrey Konovalov <[email protected]>
Quoted 23 lines
--- include/linux/efi.h | 4 ++++ 1 file changed, 4 insertions(+)include/linux/efi.h +4
-1@@ -606,7 +606,11 @@ extern void *efi_get_pal_addr (void);extern void efi_map_pal_code (void );extern void efi_memmap_walk (efi_freemem_callback_t callback,void *arg);extern void efi_gettimeofday (struct timespec64 *ts); +#ifdef CONFIG_EFI extern void efi_enter_virtual_mode (void );/* switch EFI to virtual mode, if possible */ +#else +static inline void efi_enter_virtual_mode (void ) {} +#endif #ifdef CONFIG_X86 extern efi_status_t efi_query_variable_store(u32 attributes, unsigned long size, --2.28.0.163.g6104cc2f0b6-goog
PATCH v2 3/5 kasan, arm64: don't instrument functions that enable kasan
In reply to Andrey KonovalovThis 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
@@ -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
@@ -827,7 +827,7 @@ void __init __weak arch_call_rest_init(void)rest_init (); } -asmlinkage __visible void __init start_kernel(void) +asmlinkage __visible void __init __no_sanitize_address start_kernel(void) {char *command_line;char *after_dashes;
Signature · 1 line
2.28.0.163.g6104cc2f0b6-goog
Re: [PATCH v2 3/5] kasan, arm64: don't instrument functions that enable kasan
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'ssetup_arch()as a precaution (it doesn't have any stack variables right now).Signed-off-by Andrey Konovalov <[email protected]>
I thought I acked this already. Either way:
Re: [PATCH v2 3/5] kasan, arm64: don't instrument functions that enable kasan
Quoted 16 lines · Catalin Marinas
Quoted 14 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.Disable KASAN instrumentation for
start_kernel(). Also disable it for arm64'ssetup_arch()as a precaution (it doesn't have any stack variables right now).
Quoted 5 lines
Quoted 1 line
Signed-off-by Andrey Konovalov <[email protected]>I thought I acked this already. Either way:
Acked-by Catalin Marinas <[email protected]>
Sorry, I forgot to include that into v2. Thanks!
PATCH v2 4/5 kasan: allow enabling stack tagging for tag-based mode
In reply to Andrey KonovalovUse 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
@@ -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
PATCH v2 5/5 kasan: adjust kasan_stack_oob for tag-based mode
In reply to Andrey KonovalovUse 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
@@ -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