Skip to contentSkip to conversation

kasan: Disable Software Tag-Based KASAN with GCC

PATCH Applied Will Deacon 4 participants 7 messages Oct 14, 2024 – Oct 18, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kasan: Disable Software Tag-Based KASAN with GCC

Syzbot reports a KASAN failure early during boot on arm64 when building with GCC 12.2.0 and using the Software Tag-Based KASAN mode:

  | BUG: KASAN: invalid-access in smp_build_mpidr_hash arch/arm64/kernel/setup.c:133 [inline]
  | BUG: KASAN: invalid-access in setup_arch+0x984/0xd60 arch/arm64/kernel/setup.c:356
  | Write of size 4 at addr 03ff800086867e00 by task swapper/0
  | Pointer tag: [03], memory tag: [fe]

Initial triage indicates that the report is a false positive and a thorough investigation of the crash by Mark Rutland revealed the root cause to be a bug in GCC:

Quoted 5 lines

When GCC is passed -fsanitize=hwaddress or -fsanitize=kernel-hwaddress it ignores __attribute__((no_sanitize_address)), and instruments functions we require are not instrumented.

Quoted 7 lines

[...]

All versions [of GCC] I tried were broken, from 11.3.0 to 14.2.0 inclusive.

I think we have to disable KASAN_SW_TAGS with GCC until this is fixed

Disable Software Tag-Based KASAN when building with GCC by making CC_HAS_KASAN_SW_TAGS depend on !CC_IS_GCC.

---
 lib/Kconfig.kasan | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

While sweeping up pending fixes and open bug reports, I noticed this one had slipped through the cracks...

lib/Kconfig.kasan +5 -2
index 98016e137b7f..233ab2096924 100644
@@ -22,8 +22,11 @@ config ARCH_DISABLE_KASAN_INLINE
 config CC_HAS_KASAN_GENERIC
 	def_bool $(cc-option, -fsanitize=kernel-address)
 
+# GCC appears to ignore no_sanitize_address when -fsanitize=kernel-hwaddress
+# is passed. See https://bugzilla.kernel.org/show_bug.cgi?id=218854 (and
+# the linked LKML thread) for more details.
 config CC_HAS_KASAN_SW_TAGS
-	def_bool $(cc-option, -fsanitize=kernel-hwaddress)
+	def_bool !CC_IS_GCC && $(cc-option, -fsanitize=kernel-hwaddress)
 
 # This option is only required for software KASAN modes.
 # Old GCC versions do not have proper support for no_sanitize_address.
@@ -98,7 +101,7 @@ config KASAN_SW_TAGS
 	help
 	  Enables Software Tag-Based KASAN.
 
-	  Requires GCC 11+ or Clang.
+	  Requires Clang.
 
 	  Supported only on arm64 CPUs and relies on Top Byte Ignore.
 
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
Acked-by on v1 1/1
#1 lore raw
Quoted 28 lines · Will Deacon

Syzbot reports a KASAN failure early during boot on arm64 when building with GCC 12.2.0 and using the Software Tag-Based KASAN mode:

  | BUG: KASAN: invalid-access in smp_build_mpidr_hash arch/arm64/kernel/setup.c:133 [inline]
  | BUG: KASAN: invalid-access in setup_arch+0x984/0xd60 arch/arm64/kernel/setup.c:356
  | Write of size 4 at addr 03ff800086867e00 by task swapper/0
  | Pointer tag: [03], memory tag: [fe]

Initial triage indicates that the report is a false positive and a thorough investigation of the crash by Mark Rutland revealed the root cause to be a bug in GCC:

Quoted 5 lines

When GCC is passed -fsanitize=hwaddress or -fsanitize=kernel-hwaddress it ignores __attribute__((no_sanitize_address)), and instruments functions we require are not instrumented.

Quoted 7 lines

[...]

All versions [of GCC] I tried were broken, from 11.3.0 to 14.2.0 inclusive.

I think we have to disable KASAN_SW_TAGS with GCC until this is fixed

Disable Software Tag-Based KASAN when building with GCC by making CC_HAS_KASAN_SW_TAGS depend on !CC_IS_GCC.

Quoted 7 lines

Thanks for putting a patch together!

Mark.

Quoted 36 lines
---
 lib/Kconfig.kasan | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

While sweeping up pending fixes and open bug reports, I noticed this one had slipped through the cracks...

lib/Kconfig.kasan +5 -2
index 98016e137b7f..233ab2096924 100644
@@ -22,8 +22,11 @@ config ARCH_DISABLE_KASAN_INLINE
 config CC_HAS_KASAN_GENERIC
 	def_bool $(cc-option, -fsanitize=kernel-address)
 
+# GCC appears to ignore no_sanitize_address when -fsanitize=kernel-hwaddress
+# is passed. See https://bugzilla.kernel.org/show_bug.cgi?id=218854 (and
+# the linked LKML thread) for more details.
 config CC_HAS_KASAN_SW_TAGS
-	def_bool $(cc-option, -fsanitize=kernel-hwaddress)
+	def_bool !CC_IS_GCC && $(cc-option, -fsanitize=kernel-hwaddress)
 
 # This option is only required for software KASAN modes.
 # Old GCC versions do not have proper support for no_sanitize_address.
@@ -98,7 +101,7 @@ config KASAN_SW_TAGS
 	help
 	  Enables Software Tag-Based KASAN.
 
-	  Requires GCC 11+ or Clang.
+	  Requires Clang.
 
 	  Supported only on arm64 CPUs and relies on Top Byte Ignore.
 
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Will Deacon
Quoted 70 lines · Will Deacon

Syzbot reports a KASAN failure early during boot on arm64 when building with GCC 12.2.0 and using the Software Tag-Based KASAN mode:

  | BUG: KASAN: invalid-access in smp_build_mpidr_hash arch/arm64/kernel/setup.c:133 [inline]
  | BUG: KASAN: invalid-access in setup_arch+0x984/0xd60 arch/arm64/kernel/setup.c:356
  | Write of size 4 at addr 03ff800086867e00 by task swapper/0
  | Pointer tag: [03], memory tag: [fe]

Initial triage indicates that the report is a false positive and a thorough investigation of the crash by Mark Rutland revealed the root cause to be a bug in GCC:

Quoted 5 lines

When GCC is passed -fsanitize=hwaddress or -fsanitize=kernel-hwaddress it ignores __attribute__((no_sanitize_address)), and instruments functions we require are not instrumented.

Quoted 7 lines

[...]

All versions [of GCC] I tried were broken, from 11.3.0 to 14.2.0 inclusive.

I think we have to disable KASAN_SW_TAGS with GCC until this is fixed

Disable Software Tag-Based KASAN when building with GCC by making CC_HAS_KASAN_SW_TAGS depend on !CC_IS_GCC.

---
 lib/Kconfig.kasan | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

While sweeping up pending fixes and open bug reports, I noticed this one had slipped through the cracks...

lib/Kconfig.kasan +5 -3
index 98016e137b7f..233ab2096924 100644
@@ -22,8 +22,11 @@ config ARCH_DISABLE_KASAN_INLINE
 config CC_HAS_KASAN_GENERIC
        def_bool $(cc-option, -fsanitize=kernel-address)
 
+# GCC appears to ignore no_sanitize_address when -fsanitize=kernel-hwaddress
+# is passed. See https://bugzilla.kernel.org/show_bug.cgi?id=218854 (and
+# the linked LKML thread) for more details.
 config CC_HAS_KASAN_SW_TAGS
-       def_bool $(cc-option, -fsanitize=kernel-hwaddress)
+       def_bool !CC_IS_GCC && $(cc-option, -fsanitize=kernel-hwaddress)
 
 # This option is only required for software KASAN modes.
 # Old GCC versions do not have proper support for no_sanitize_address.
@@ -98,7 +101,7 @@ config KASAN_SW_TAGS
        help
          Enables Software Tag-Based KASAN.
 
-         Requires GCC 11+ or Clang.
+         Requires Clang.
 
          Supported only on arm64 CPUs and relies on Top Byte Ignore.
 
--
Quoted 2 lines

2.47.0.rc1.288.g06298d1525-goog

Thank you!

author Applied
#3 lore raw
In reply to Will Deacon
Quoted 9 lines · Will Deacon

Syzbot reports a KASAN failure early during boot on arm64 when building with GCC 12.2.0 and using the Software Tag-Based KASAN mode:

  | BUG: KASAN: invalid-access in smp_build_mpidr_hash arch/arm64/kernel/setup.c:133 [inline]
  | BUG: KASAN: invalid-access in setup_arch+0x984/0xd60 arch/arm64/kernel/setup.c:356
  | Write of size 4 at addr 03ff800086867e00 by task swapper/0
  | Pointer tag: [03], memory tag: [fe]

[...]

Applied to arm64 (for-next/fixes), thanks!

[1/1] kasan: Disable Software Tag-Based KASAN with GCC

      https://git.kernel.org/arm64/c/7aed6a2c51ff

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev
#4 lore raw
Quoted 9 lines · Will Deacon' via kasan-dev
Quoted 8 lines · Will Deacon

Syzbot reports a KASAN failure early during boot on arm64 when building with GCC 12.2.0 and using the Software Tag-Based KASAN mode:

  | BUG: KASAN: invalid-access in smp_build_mpidr_hash arch/arm64/kernel/setup.c:133 [inline]
  | BUG: KASAN: invalid-access in setup_arch+0x984/0xd60 arch/arm64/kernel/setup.c:356
  | Write of size 4 at addr 03ff800086867e00 by task swapper/0
  | Pointer tag: [03], memory tag: [fe]
Quoted 6 lines
Quoted 1 line

[...]

Applied to arm64 (for-next/fixes), thanks!

[1/1] kasan: Disable Software Tag-Based KASAN with GCC

      https://git.kernel.org/arm64/c/7aed6a2c51ff

I do not think this is the right fix. Please see alternative below. Please do double-check that the observed splat above is fixed with that.

Thanks,
-- Marco

------ >8 ------

From 23bd83dbff5a9778f34831ed292d5e52b4b0ee18 Mon Sep 17 00:00:00 2001
From: Marco Elver <[email protected]>
Date: Fri, 18 Oct 2024 10:18:24 +0200
Subject: [PATCH] kasan: Fix Software Tag-Based KASAN with GCC

Per [1], -fsanitize=kernel-hwaddress with GCC currently does not disable instrumentation in functions with __attribute__((no_sanitize_address)).

However, __attribute__((no_sanitize("hwaddress"))) does correctly disable instrumentation. Use it instead.

---
 include/linux/compiler-gcc.h | 4 ++++
 1 file changed, 4 insertions(+)
include/linux/compiler-gcc.h +4 -0
index f805adaa316e..cd6f9aae311f 100644
@@ -80,7 +80,11 @@
 #define __noscs __attribute__((__no_sanitize__("shadow-call-stack")))
 #endif
 
+#ifdef __SANITIZE_HWADDRESS__
+#define __no_sanitize_address __attribute__((__no_sanitize__("hwaddress")))
+#else
 #define __no_sanitize_address __attribute__((__no_sanitize_address__))
+#endif
 
 #if defined(__SANITIZE_THREAD__)
 #define __no_sanitize_thread __attribute__((__no_sanitize_thread__))
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
#5 lore raw
Quoted 56 lines · Marco Elver
Quoted 5 lines

Applied to arm64 (for-next/fixes), thanks!

[1/1] kasan: Disable Software Tag-Based KASAN with GCC

      https://git.kernel.org/arm64/c/7aed6a2c51ff

I do not think this is the right fix. Please see alternative below. Please do double-check that the observed splat above is fixed with that.

Thanks,
-- Marco

------ >8 ------

From 23bd83dbff5a9778f34831ed292d5e52b4b0ee18 Mon Sep 17 00:00:00 2001
From: Marco Elver <[email protected]>
Date: Fri, 18 Oct 2024 10:18:24 +0200
Subject: [PATCH] kasan: Fix Software Tag-Based KASAN with GCC

Per [1], -fsanitize=kernel-hwaddress with GCC currently does not disable instrumentation in functions with __attribute__((no_sanitize_address)).

However, __attribute__((no_sanitize("hwaddress"))) does correctly disable instrumentation. Use it instead.

---
 include/linux/compiler-gcc.h | 4 ++++
 1 file changed, 4 insertions(+)
include/linux/compiler-gcc.h +4 -1
index f805adaa316e..cd6f9aae311f 100644
@@ -80,7 +80,11 @@
 #define __noscs __attribute__((__no_sanitize__("shadow-call-stack")))
 #endif
 
+#ifdef __SANITIZE_HWADDRESS__
+#define __no_sanitize_address __attribute__((__no_sanitize__("hwaddress")))
+#else
 #define __no_sanitize_address __attribute__((__no_sanitize_address__))
+#endif
 
 #if defined(__SANITIZE_THREAD__)
 #define __no_sanitize_thread __attribute__((__no_sanitize_thread__))
--
Quoted 1 line

2.47.0.rc1.288.g06298d1525-goog

Tested the change, it does fix the boot-time issue #1 from [1], but #2 and #3 still exist.

However, perhaps, just fixing #1 is already good enough to do a revert of the Will's patch - at least the kernel will boot without false-positive reports.

But I would keep a note that SW_TAGS doesn't work well with GCC until
[1] is fully resolved.

Thanks!

[1] https://bugzilla.kernel.org/show_bug.cgi?id=218854

#6 lore raw
Quoted 55 lines · Andrey Konovalov
Quoted 53 lines · Marco Elver
Quoted 5 lines

Applied to arm64 (for-next/fixes), thanks!

[1/1] kasan: Disable Software Tag-Based KASAN with GCC

      https://git.kernel.org/arm64/c/7aed6a2c51ff

I do not think this is the right fix. Please see alternative below. Please do double-check that the observed splat above is fixed with that.

Thanks,
-- Marco

------ >8 ------

From 23bd83dbff5a9778f34831ed292d5e52b4b0ee18 Mon Sep 17 00:00:00 2001
From: Marco Elver <[email protected]>
Date: Fri, 18 Oct 2024 10:18:24 +0200
Subject: [PATCH] kasan: Fix Software Tag-Based KASAN with GCC

Per [1], -fsanitize=kernel-hwaddress with GCC currently does not disable instrumentation in functions with __attribute__((no_sanitize_address)).

However, __attribute__((no_sanitize("hwaddress"))) does correctly disable instrumentation. Use it instead.

---
 include/linux/compiler-gcc.h | 4 ++++
 1 file changed, 4 insertions(+)
include/linux/compiler-gcc.h +4 -0
index f805adaa316e..cd6f9aae311f 100644
@@ -80,7 +80,11 @@
 #define __noscs __attribute__((__no_sanitize__("shadow-call-stack")))
 #endif
 
+#ifdef __SANITIZE_HWADDRESS__
+#define __no_sanitize_address __attribute__((__no_sanitize__("hwaddress")))
+#else
 #define __no_sanitize_address __attribute__((__no_sanitize_address__))
+#endif
 
Quoted 7 lines
Quoted 4 lines
include/linux/compiler-gcc.h +0 -1
 #if defined(__SANITIZE_THREAD__)
 #define __no_sanitize_thread __attribute__((__no_sanitize_thread__))
--

2.47.0.rc1.288.g06298d1525-goog

Tested the change, it does fix the boot-time issue #1 from [1], but #2 and #3 still exist.

Thanks for testing.
AFAIK #2 and #3 look like false negatives, which are tolerable (not great, but it does not cause serious issues).

Quoted 10 lines

However, perhaps, just fixing #1 is already good enough to do a revert of the Will's patch - at least the kernel will boot without false-positive reports.

But I would keep a note that SW_TAGS doesn't work well with GCC until
[1] is fully resolved.

Thanks!

[1] https://bugzilla.kernel.org/show_bug.cgi?id=218854