kasan: prevent recursive instrumentation
Subsystems (3)
PATCH 1/1 kasan: prevent recursive instrumentation
When CONFIG_KASAN is selected, <linux/kasan-checks.h> defines the prototypes for kasan_check_{read,write}(), rather than inline stubs.
This is the case even when it is included by files which are not instrumented by KASAN. Where helpers (e.g. the atomics) are instrumented with explicit calls to kasan_check_{read,write}(), this results in files being unexpectedly instrumented.
This is problematic as it can result in instrumentation in files which cannot safely call these functions, such as within the EFI stub:
[mark@lakrids:~/src/linux]% usekorg 8.1.0 make ARCH=arm64 CROSS_COMPILE=aarch64-linux- -j64 -sdrivers/firmware/efi/libstub/arm-stub.stub.o: In function `atomic_set':/home/mark/src/linux/./include/asm-generic/atomic-instrumented.h:44: undefined reference to __efistub_kasan_check_write' /home/mark/src/linux/./include/asm-generic/atomic-instrumented.h:44:(.init.text+0xa0): relocation truncated to fit: R_AARCH64_CALL26 against undefined symbol __efistub_kasan_check_write' Makefile:1021: recipe for target 'vmlinux' failed
make: *** [vmlinux] Error 1
Let's avoid this by ensuring that uninstrumented files are given the same stub definition of these functions used for !KASAN builds. So that the stub defintions don't conflict with the real definitions in (uninstrumented) common KASAN code, the real definitions are prefixed with underscores, and called from unprefixed macros.
Any compiler-generated instrumentation uses separate __asan_{load,store}_*() entry points, and is not affected by this change.
--- include/linux/kasan-checks.h | 8 +++++--- mm/kasan/common.c | 8 ++++---- scripts/Makefile.kasan | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-)
Hi,
Assuming the KASAN folk are happy with this, I'd like this to be queued in the tip tree, where the arm64 instrumented atomics are already queued.
Thanks,
Mark.
include/linux/kasan-checks.h +5 -3
@@ -2,9 +2,11 @@#ifndef _LINUX_KASAN_CHECKS_H#define _LINUX_KASAN_CHECKS_H -#ifdef CONFIG_KASAN -void kasan_check_read (const volatile void *p,unsigned int size); -void kasan_check_write (const volatile void *p,unsigned int size); +#if defined(CONFIG_KASAN ) && !defined (KASAN_NOSANITIZE ) +void __kasan_check_read (const volatile void *p,unsigned int size); +#define kasan_check_read __kasan_check_read +void __kasan_check_write (const volatile void *p,unsigned int size); +#define kasan_check_write __kasan_check_write#else static inline void kasan_check_read (const volatile void *p,unsigned int size) { }
mm/kasan/common.c +4 -4
@@ -95,17 +95,17 @@ void kasan_disable_current(void) current->kasan_depth--; } -void kasan_check_read (const volatile void *p,unsigned int size) +void __kasan_check_read (const volatile void *p,unsigned int size) {check_memory_region ((unsigned long )p, size, false, _RET_IP_); } -EXPORT_SYMBOL (kasan_check_read); +EXPORT_SYMBOL (__kasan_check_read); -void kasan_check_write (const volatile void *p,unsigned int size) +void __kasan_check_write (const volatile void *p,unsigned int size) {check_memory_region ((unsigned long )p, size, true, _RET_IP_); } -EXPORT_SYMBOL (kasan_check_write); +EXPORT_SYMBOL (__kasan_check_write);#undef memsetvoid *memset (void *addr,int c,size_t len)
scripts/Makefile.kasan +1 -1
@@ -52,5 +52,5 @@ CFLAGS_KASAN := -fsanitize=kernel-hwaddress \ endif # CONFIG_KASAN_SW_TAGS ifdef CONFIG_KASAN -CFLAGS_KASAN_NOSANITIZE := -fno-builtin +CFLAGS_KASAN_NOSANITIZE := -fno-builtin -DKASAN_NOSANITIZE endif
Signature · 1 line
2.11.0
Dmitry Vyukov
reviewerQuoted 3 lines · Mark Rutland
When
CONFIG_KASANis selected, <linux/kasan-checks.h> defines the prototypes for kasan_check_{read,write}(), rather than inline stubs.
Doesn't this do the same as?
https://groups.google.com/d/msg/kasan-dev/NwEdpGgUeac/oXUIRUC6CAAJ
Quoted 103 lines
This is the case even when it is included by files which are not instrumented by KASAN. Where helpers (e.g. the atomics) are instrumented with explicit calls to kasan_check_{read,write}(), this results in files being unexpectedly instrumented.
This is problematic as it can result in instrumentation in files which cannot safely call these functions, such as within the EFI stub:
[mark@lakrids:~/src/linux]% usekorg 8.1.0 make ARCH=arm64 CROSS_COMPILE=aarch64-linux- -j64 -s
drivers/firmware/efi/libstub/arm-stub.stub.o: In function `atomic_set':/home/mark/src/linux/./include/asm-generic/atomic-instrumented.h:44: undefined reference to__efistub_kasan_check_write' /home/mark/src/linux/./include/asm-generic/atomic-instrumented.h:44:(.init.text+0xa0): relocation truncated to fit: R_AARCH64_CALL26 against undefined symbol__efistub_kasan_check_write' Makefile:1021: recipe for target 'vmlinux' failed
make: *** [vmlinux] Error 1Let's avoid this by ensuring that uninstrumented files are given the same stub definition of these functions used for !KASAN builds. So that the stub defintions don't conflict with the real definitions in (uninstrumented) common KASAN code, the real definitions are prefixed with underscores, and called from unprefixed macros.
Any compiler-generated instrumentation uses separate __asan_{load,store}_*() entry points, and is not affected by this change.
Signed-off-by Mark Rutland <[email protected]>Cc Alexander Potapenko <[email protected]>Cc Andrey Ryabinin <[email protected]>Cc Dmitry Vyukov <[email protected]>Cc Ingo Molnar <[email protected]>--- include/linux/kasan-checks.h | 8 +++++--- mm/kasan/common.c | 8 ++++---- scripts/Makefile.kasan | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-)Hi,
Assuming the KASAN folk are happy with this, I'd like this to be queued in the tip tree, where the arm64 instrumented atomics are already queued.
Thanks,
Mark.include/linux/kasan-checks.h +5
-3@@ -2,9 +2,11 @@#ifndef _LINUX_KASAN_CHECKS_H#define _LINUX_KASAN_CHECKS_H -#ifdef CONFIG_KASAN -void kasan_check_read (const volatile void *p,unsigned int size); -void kasan_check_write (const volatile void *p,unsigned int size); +#if defined(CONFIG_KASAN ) && !defined (KASAN_NOSANITIZE ) +void __kasan_check_read (const volatile void *p,unsigned int size); +#define kasan_check_read __kasan_check_read +void __kasan_check_write (const volatile void *p,unsigned int size); +#define kasan_check_write __kasan_check_write#else static inline void kasan_check_read (const volatile void *p,unsigned int size) { }mm/kasan/common.c +4
-4@@ -95,17 +95,17 @@ void kasan_disable_current(void) current->kasan_depth--; } -void kasan_check_read (const volatile void *p,unsigned int size) +void __kasan_check_read (const volatile void *p,unsigned int size) {check_memory_region ((unsigned long )p, size, false, _RET_IP_); } -EXPORT_SYMBOL (kasan_check_read); +EXPORT_SYMBOL (__kasan_check_read); -void kasan_check_write (const volatile void *p,unsigned int size) +void __kasan_check_write (const volatile void *p,unsigned int size) {check_memory_region ((unsigned long )p, size, true, _RET_IP_); } -EXPORT_SYMBOL (kasan_check_write); +EXPORT_SYMBOL (__kasan_check_write);#undef memsetvoid *memset (void *addr,int c,size_t len)scripts/Makefile.kasan +1
-2@@ -52,5 +52,5 @@ CFLAGS_KASAN := -fsanitize=kernel-hwaddress \ endif # CONFIG_KASAN_SW_TAGS ifdef CONFIG_KASAN -CFLAGS_KASAN_NOSANITIZE := -fno-builtin +CFLAGS_KASAN_NOSANITIZE := -fno-builtin -DKASAN_NOSANITIZE endif --2.11.0
Quoted 9 lines · Dmitry Vyukov
Quoted 3 lines · Mark Rutland
When
CONFIG_KASANis selected, <linux/kasan-checks.h> defines the prototypes for kasan_check_{read,write}(), rather than inline stubs.Doesn't this do the same as?
https://groups.google.com/d/msg/kasan-dev/NwEdpGgUeac/oXUIRUC6CAAJ
I had missed that patch -- thanks for the pointer!
Either patch should both fix the issue with the EFI stub, but the __kasan_check*() renaming in this patch also prevents unexpected instrumentation within mm/kasan/common.c, if a call to kasan_check_*() were inlined there.
If you think that robustness is worthwhile, I can spin a v2 making that a bit clearer, and fix the logic to look at __SANITIZE_ADDRESS__ rather than adding the KASAN_NOSANITIZE definition.
AFAICT Arnd's patch isn't queued anywhere just yet.
Thanks,
Mark.
Quoted 103 lines
Quoted 103 lines
This is the case even when it is included by files which are not instrumented by KASAN. Where helpers (e.g. the atomics) are instrumented with explicit calls to kasan_check_{read,write}(), this results in files being unexpectedly instrumented.
This is problematic as it can result in instrumentation in files which cannot safely call these functions, such as within the EFI stub:
[mark@lakrids:~/src/linux]% usekorg 8.1.0 make ARCH=arm64 CROSS_COMPILE=aarch64-linux- -j64 -s
drivers/firmware/efi/libstub/arm-stub.stub.o: In function `atomic_set':/home/mark/src/linux/./include/asm-generic/atomic-instrumented.h:44: undefined reference to__efistub_kasan_check_write' /home/mark/src/linux/./include/asm-generic/atomic-instrumented.h:44:(.init.text+0xa0): relocation truncated to fit: R_AARCH64_CALL26 against undefined symbol__efistub_kasan_check_write' Makefile:1021: recipe for target 'vmlinux' failed
make: *** [vmlinux] Error 1Let's avoid this by ensuring that uninstrumented files are given the same stub definition of these functions used for !KASAN builds. So that the stub defintions don't conflict with the real definitions in (uninstrumented) common KASAN code, the real definitions are prefixed with underscores, and called from unprefixed macros.
Any compiler-generated instrumentation uses separate __asan_{load,store}_*() entry points, and is not affected by this change.
Signed-off-by Mark Rutland <[email protected]>Cc Alexander Potapenko <[email protected]>Cc Andrey Ryabinin <[email protected]>Cc Dmitry Vyukov <[email protected]>Cc Ingo Molnar <[email protected]>--- include/linux/kasan-checks.h | 8 +++++--- mm/kasan/common.c | 8 ++++---- scripts/Makefile.kasan | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-)Hi,
Assuming the KASAN folk are happy with this, I'd like this to be queued in the tip tree, where the arm64 instrumented atomics are already queued.
Thanks,
Mark.include/linux/kasan-checks.h +5
-3@@ -2,9 +2,11 @@#ifndef _LINUX_KASAN_CHECKS_H#define _LINUX_KASAN_CHECKS_H -#ifdef CONFIG_KASAN -void kasan_check_read (const volatile void *p,unsigned int size); -void kasan_check_write (const volatile void *p,unsigned int size); +#if defined(CONFIG_KASAN ) && !defined (KASAN_NOSANITIZE ) +void __kasan_check_read (const volatile void *p,unsigned int size); +#define kasan_check_read __kasan_check_read +void __kasan_check_write (const volatile void *p,unsigned int size); +#define kasan_check_write __kasan_check_write#else static inline void kasan_check_read (const volatile void *p,unsigned int size) { }mm/kasan/common.c +4
-4@@ -95,17 +95,17 @@ void kasan_disable_current(void) current->kasan_depth--; } -void kasan_check_read (const volatile void *p,unsigned int size) +void __kasan_check_read (const volatile void *p,unsigned int size) {check_memory_region ((unsigned long )p, size, false, _RET_IP_); } -EXPORT_SYMBOL (kasan_check_read); +EXPORT_SYMBOL (__kasan_check_read); -void kasan_check_write (const volatile void *p,unsigned int size) +void __kasan_check_write (const volatile void *p,unsigned int size) {check_memory_region ((unsigned long )p, size, true, _RET_IP_); } -EXPORT_SYMBOL (kasan_check_write); +EXPORT_SYMBOL (__kasan_check_write);#undef memsetvoid *memset (void *addr,int c,size_t len)scripts/Makefile.kasan +1
-2@@ -52,5 +52,5 @@ CFLAGS_KASAN := -fsanitize=kernel-hwaddress \ endif # CONFIG_KASAN_SW_TAGS ifdef CONFIG_KASAN -CFLAGS_KASAN_NOSANITIZE := -fno-builtin +CFLAGS_KASAN_NOSANITIZE := -fno-builtin -DKASAN_NOSANITIZE endif --2.11.0
Dmitry Vyukov
reviewerQuoted 19 lines · Mark Rutland
Quoted 9 lines · Dmitry Vyukov
Quoted 3 lines · Mark Rutland
When
CONFIG_KASANis selected, <linux/kasan-checks.h> defines the prototypes for kasan_check_{read,write}(), rather than inline stubs.Doesn't this do the same as?
https://groups.google.com/d/msg/kasan-dev/NwEdpGgUeac/oXUIRUC6CAAJI had missed that patch -- thanks for the pointer!
Either patch should both fix the issue with the EFI stub, but the __kasan_check*() renaming in this patch also prevents unexpected instrumentation within
mm/kasan/common.c, if a call to kasan_check_*() were inlined there.
Quoted 5 lines
If you think that robustness is worthwhile, I can spin a v2 making that a bit clearer, and fix the logic to look at
__SANITIZE_ADDRESS__rather than adding theKASAN_NOSANITIZEdefinition.AFAICT Arnd's patch isn't queued anywhere just yet.
I dunno. I don't have strong preference.
Also not sure if Arnd's patch is not, say, in mm... problems of extremely long patch submission times... patches should take at most days to commit.
Quoted 103 lines
Quoted 103 lines
Quoted 103 lines
This is the case even when it is included by files which are not instrumented by KASAN. Where helpers (e.g. the atomics) are instrumented with explicit calls to kasan_check_{read,write}(), this results in files being unexpectedly instrumented.
This is problematic as it can result in instrumentation in files which cannot safely call these functions, such as within the EFI stub:
[mark@lakrids:~/src/linux]% usekorg 8.1.0 make ARCH=arm64 CROSS_COMPILE=aarch64-linux- -j64 -s
drivers/firmware/efi/libstub/arm-stub.stub.o: In function `atomic_set':/home/mark/src/linux/./include/asm-generic/atomic-instrumented.h:44: undefined reference to__efistub_kasan_check_write' /home/mark/src/linux/./include/asm-generic/atomic-instrumented.h:44:(.init.text+0xa0): relocation truncated to fit: R_AARCH64_CALL26 against undefined symbol__efistub_kasan_check_write' Makefile:1021: recipe for target 'vmlinux' failed
make: *** [vmlinux] Error 1Let's avoid this by ensuring that uninstrumented files are given the same stub definition of these functions used for !KASAN builds. So that the stub defintions don't conflict with the real definitions in (uninstrumented) common KASAN code, the real definitions are prefixed with underscores, and called from unprefixed macros.
Any compiler-generated instrumentation uses separate __asan_{load,store}_*() entry points, and is not affected by this change.
Signed-off-by Mark Rutland <[email protected]>Cc Alexander Potapenko <[email protected]>Cc Andrey Ryabinin <[email protected]>Cc Dmitry Vyukov <[email protected]>Cc Ingo Molnar <[email protected]>--- include/linux/kasan-checks.h | 8 +++++--- mm/kasan/common.c | 8 ++++---- scripts/Makefile.kasan | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-)Hi,
Assuming the KASAN folk are happy with this, I'd like this to be queued in the tip tree, where the arm64 instrumented atomics are already queued.
Thanks,
Mark.include/linux/kasan-checks.h +5
-3@@ -2,9 +2,11 @@#ifndef _LINUX_KASAN_CHECKS_H#define _LINUX_KASAN_CHECKS_H -#ifdef CONFIG_KASAN -void kasan_check_read (const volatile void *p,unsigned int size); -void kasan_check_write (const volatile void *p,unsigned int size); +#if defined(CONFIG_KASAN ) && !defined (KASAN_NOSANITIZE ) +void __kasan_check_read (const volatile void *p,unsigned int size); +#define kasan_check_read __kasan_check_read +void __kasan_check_write (const volatile void *p,unsigned int size); +#define kasan_check_write __kasan_check_write#else static inline void kasan_check_read (const volatile void *p,unsigned int size) { }mm/kasan/common.c +4
-4@@ -95,17 +95,17 @@ void kasan_disable_current(void) current->kasan_depth--; } -void kasan_check_read (const volatile void *p,unsigned int size) +void __kasan_check_read (const volatile void *p,unsigned int size) {check_memory_region ((unsigned long )p, size, false, _RET_IP_); } -EXPORT_SYMBOL (kasan_check_read); +EXPORT_SYMBOL (__kasan_check_read); -void kasan_check_write (const volatile void *p,unsigned int size) +void __kasan_check_write (const volatile void *p,unsigned int size) {check_memory_region ((unsigned long )p, size, true, _RET_IP_); } -EXPORT_SYMBOL (kasan_check_write); +EXPORT_SYMBOL (__kasan_check_write);#undef memsetvoid *memset (void *addr,int c,size_t len)scripts/Makefile.kasan +1
-2@@ -52,5 +52,5 @@ CFLAGS_KASAN := -fsanitize=kernel-hwaddress \ endif # CONFIG_KASAN_SW_TAGS ifdef CONFIG_KASAN -CFLAGS_KASAN_NOSANITIZE := -fno-builtin +CFLAGS_KASAN_NOSANITIZE := -fno-builtin -DKASAN_NOSANITIZE endif --2.11.0
Quoted 24 lines · Dmitry Vyukov
Quoted 23 lines · Mark Rutland
Quoted 9 lines · Dmitry Vyukov
Quoted 3 lines · Mark Rutland
When
CONFIG_KASANis selected, <linux/kasan-checks.h> defines the prototypes for kasan_check_{read,write}(), rather than inline stubs.Doesn't this do the same as?
https://groups.google.com/d/msg/kasan-dev/NwEdpGgUeac/oXUIRUC6CAAJI had missed that patch -- thanks for the pointer!
Either patch should both fix the issue with the EFI stub, but the __kasan_check*() renaming in this patch also prevents unexpected instrumentation within
mm/kasan/common.c, if a call to kasan_check_*() were inlined there.If you think that robustness is worthwhile, I can spin a v2 making that a bit clearer, and fix the logic to look at
__SANITIZE_ADDRESS__rather than adding theKASAN_NOSANITIZEdefinition.
Quoted 6 lines
Quoted 1 line
AFAICT Arnd's patch isn't queued anywhere just yet.
I dunno. I don't have strong preference.
Also not sure if Arnd's patch is not, say, in mm... problems of extremely long patch submission times... patches should take at most days to commit.
I see Arnd's patch is in linux-next today, so it looks like that's solved.
I'll drop this patch unless we see any other problems.
Thanks,
Mark.