Skip to contentSkip to conversation

kasan: Add KUnit config and fix missing instrumentation in load_unaligned_zeropad()

PATCH New Bill Wendling 2 participants 9 messages Sep 11

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

cover kasan: Add KUnit config and fix missing instrumentation in load_unaligned_zeropad()

This series makes the KASAN KUnit test suite easy to run via kunit.py and fixes a real detection gap it uncovered along the way.

Patch 1 adds mm/kasan/.kunitconfig so the suite can be run with:

    ./tools/testing/kunit/kunit.py run --arch=x86_64 --kunitconfig mm/kasan

Getting CONFIG_KASAN_KUNIT_TEST to actually build required pulling in CONFIG_SCHED_TRACER (CONFIG_FTRACE alone doesn't select the invisible TRACEPOINTS symbol it depends on), plus a larger CONFIG_LOG_BUF_SHIFT to stop KASAN's verbose reports from overflowing the printk buffer under QEMU.

The migration from read_word_at_a_time() to load_unaligned_zeropad() lost instrumentation, because the load_unaligned_zeropad() functions are implemented as inline asm with an exception table entry on every architecture, so the compiler never sees the memory access and can't instrument it for KASAN/KCSAN. This meant sized_strscpy() silently bypassed KASAN when reading its source buffer word-at-a-time, and the same gap turned out to be open at every other call site in the tree.

Rather than patching each call site individually, patch 2 renames x86's load_unaligned_zeropad() to arch_load_unaligned_zeropad() and adds a single common, always-inlined wrapper that instruments the read before calling through.

Patches 3-7 convert the remaining architectures (arm64, arm, riscv, s390, powerpc) that implement load_unaligned_zeropad() to the same pattern, so each can be reviewed independently by its arch maintainer.

Verified via objdump that the wrapper adds no calls or relocations when KASAN and KCSAN are both disabled.

Bill Wendling (7):
kasan: Add .kunitconfig for KUnit tests x86/word-at-a-time: Instrument load_unaligned_zeropad() arm64/word-at-a-time: Instrument load_unaligned_zeropad() arm/word-at-a-time: Instrument load_unaligned_zeropad() riscv/word-at-a-time: Instrument load_unaligned_zeropad() s390/word-at-a-time: Instrument load_unaligned_zeropad() powerpc/word-at-a-time: Instrument load_unaligned_zeropad()

 arch/arm/include/asm/word-at-a-time.h         |  4 ++-
 arch/arm64/include/asm/word-at-a-time.h       |  4 ++-
 arch/powerpc/include/asm/word-at-a-time.h     |  4 ++-
 arch/riscv/include/asm/word-at-a-time.h       |  4 ++-
 arch/s390/include/asm/word-at-a-time.h        |  4 ++-
 arch/x86/include/asm/word-at-a-time.h         |  4 ++-
 .../asm-generic/word-at-a-time-instrumented.h | 25 +++++++++++++++++++
 mm/kasan/.kunitconfig                         | 15 +++++++++++
 8 files changed, 58 insertions(+), 6 deletions(-)
 create mode 100644 include/asm-generic/word-at-a-time-instrumented.h
 create mode 100644 mm/kasan/.kunitconfig
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/7 kasan: Add .kunitconfig for KUnit tests

Add a .kunitconfig file for the KASAN test suite so that it can easily be run via kunit.py:

    ./tools/testing/kunit/kunit.py run --arch=x86_64 --kunitconfig mm/kasan

CONFIG_KASAN_KUNIT_TEST depends on TRACEPOINTS, which is an invisible Kconfig symbol without a prompt. CONFIG_FTRACE alone only unlocks the tracer submenu and does not select TRACEPOINTS by itself, so also enable CONFIG_SCHED_TRACER=y, which selects GENERIC_TRACER -> TRACING -> TRACEPOINTS. This follows the same pattern used by security/landlock/.kunitconfig. Verified with:

make KCONFIG_ALLCONFIG=mm/kasan/.kunitconfig allnoconfig

that CONFIG_TRACEPOINTS=y and CONFIG_KASAN_KUNIT_TEST=y both resolve.

In addition, set CONFIG_LOG_BUF_SHIFT=20 to prevent printk buffer overflows on serial consoles when running under QEMU, as KASAN generates extensive reports across its test cases.

Assisted-by Claude:claude-sonnet-5

---
Cc: Andrey Ryabinin <[email protected]>
Cc: Alexander Potapenko <[email protected]>
Cc: Andrey Konovalov <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: Vincenzo Frascino <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: "Mickaël Salaün" <[email protected]>
Cc: "Günther Noack" <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 mm/kasan/.kunitconfig | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
 create mode 100644 mm/kasan/.kunitconfig
mm/kasan/.kunitconfig new file +15 -0
new file mode 100644
index 000000000000..58a7448fa352
@@ -0,0 +1,15 @@
+CONFIG_KUNIT=y
+CONFIG_KASAN=y
+CONFIG_KASAN_KUNIT_TEST=y
+
+# Additional dependencies.
+# CONFIG_KASAN_KUNIT_TEST depends on TRACEPOINTS, an invisible Kconfig
+# symbol with no prompt. CONFIG_FTRACE alone only unlocks the tracer
+# submenu; CONFIG_SCHED_TRACER is needed to actually select
+# GENERIC_TRACER -> TRACING -> TRACEPOINTS.
+CONFIG_FTRACE=y
+CONFIG_SCHED_TRACER=y
+
+# Prevent printk buffer overflows on serial consoles when running under
+# QEMU, since KASAN generates extensive reports across its test cases.
+CONFIG_LOG_BUF_SHIFT=20
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/7 x86/word-at-a-time: Instrument load_unaligned_zeropad()

In reply to Bill Wendling

load_unaligned_zeropad() is implemented as inline asm plus an exception table entry on every architecture, so the compiler never sees the actual memory access and cannot emit KASAN/KCSAN instrumentation for it.

Resurrect the instrumentation that was lost when migrating from read_word_at_a_time() by renaming load_unaligned_zeropad() to arch_load_unaligned_zeropad() and adding a common instrumented load_unaligned_zeropad() in the new asm-generic/word-at-a-time-instrumented.h, pulled in from the bottom of asm/word-at-a-time.h. This mirrors the existing arch_atomic_*()/atomic_*() split in linux/atomic/atomic-instrumented.h, where the arch_* version is the raw, uninstrumented primitive and the plain name is the instrumented one everyone should call.

The wrapper is __always_inline, so it compiles down to exactly the same inline asm as before plus instrument_read() -- confirmed via objdump that no calls or relocations to load_unaligned_zeropad(), instrument_read(), or the KASAN/KCSAN check functions remain when KASAN and KCSAN are both disabled.

Convert x86 to the new split here. Follow-up patches convert the remaining architectures (arm64, arm, riscv, s390, powerpc) that implement load_unaligned_zeropad().

Fixes d94c12bd97d5 ("string: Add load_unaligned_zeropad() code path to sized_strscpy()")
Assisted-by Claude:claude-sonnet-5

---
Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: "H. Peter Anvin" <[email protected]>
Cc: Arnd Bergmann <[email protected]>
Cc: Paul Walmsley <[email protected]>
Cc: Palmer Dabbelt <[email protected]>
Cc: Albert Ou <[email protected]>
Cc: Alexandre Ghiti <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: Catalin Marinas <[email protected]>
Cc: Peter Collingbourne <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 arch/x86/include/asm/word-at-a-time.h         |  4 ++-
 .../asm-generic/word-at-a-time-instrumented.h | 25 +++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)
 create mode 100644 include/asm-generic/word-at-a-time-instrumented.h
arch/x86/include/asm/word-at-a-time.h +3 -1
index 422a47746657..483d27dc96b2 100644
@@ -67,7 +67,7 @@ static inline unsigned long find_zero(unsigned long mask)
  * and the next page not being mapped, take the exception and
  * return zeroes in the non-existing part.
  */
-static inline unsigned long load_unaligned_zeropad(const void *addr)
+static inline unsigned long arch_load_unaligned_zeropad(const void *addr)
 {
 	unsigned long ret;
 
@@ -81,4 +81,6 @@ static inline unsigned long load_unaligned_zeropad(const void *addr)
 	return ret;
 }
 
+#include <asm-generic/word-at-a-time-instrumented.h>
+
 #endif /* _ASM_WORD_AT_A_TIME_H */
include/asm-generic/word-at-a-time-instrumented.h new file +25 -0
new file mode 100644
index 000000000000..8a66d53bf538
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_GENERIC_WORD_AT_A_TIME_INSTRUMENTED_H
+#define _ASM_GENERIC_WORD_AT_A_TIME_INSTRUMENTED_H
+
+#include <linux/instrumented.h>
+
+/*
+ * Each arch's asm/word-at-a-time.h defines arch_load_unaligned_zeropad()
+ * as inline asm plus an exception table entry, so the compiler never sees
+ * the actual memory access and cannot emit KASAN/KCSAN instrumentation
+ * for it. Provide the common, instrumented load_unaligned_zeropad() that
+ * every caller uses by wrapping the arch version with instrument_read().
+ *
+ * This is marked __always_inline, and arch_load_unaligned_zeropad() is
+ * itself a tiny inline-asm-only function, so this compiles down to the
+ * same inline asm as before plus instrument_read() (which itself compiles
+ * to nothing when KASAN and KCSAN are both disabled) -- no actual call.
+ */
+static __always_inline unsigned long load_unaligned_zeropad(const void *addr)
+{
+	instrument_read(addr, 1);
+	return arch_load_unaligned_zeropad(addr);
+}
+
+#endif /* _ASM_GENERIC_WORD_AT_A_TIME_INSTRUMENTED_H */
Signature · 1 line
2.47.3
#3 lore raw

PATCH 3/7 arm64/word-at-a-time: Instrument load_unaligned_zeropad()

In reply to Bill Wendling

Convert arm64 to the arch_load_unaligned_zeropad() / load_unaligned_zeropad() split introduced for x86 in the previous patch, so KASAN/KCSAN also see reads through load_unaligned_zeropad() on this architecture (e.g. under CONFIG_KASAN_SW_TAGS).

Assisted-by Claude:claude-sonnet-5

---
Cc: Catalin Marinas <[email protected]>
Cc: Will Deacon <[email protected]>
Cc: Mark Rutland <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 arch/arm64/include/asm/word-at-a-time.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm64/include/asm/word-at-a-time.h +3 -1
index 824ca6987a51..656e3274ed7a 100644
@@ -47,7 +47,7 @@ static inline unsigned long zero_bytemask(unsigned long bits)
  * and the next page not being mapped, take the exception and
  * return zeroes in the non-existing part.
  */
-static inline unsigned long load_unaligned_zeropad(const void *addr)
+static inline unsigned long arch_load_unaligned_zeropad(const void *addr)
 {
 	unsigned long ret;
 
@@ -66,4 +66,6 @@ static inline unsigned long load_unaligned_zeropad(const void *addr)
 	return ret;
 }
 
+#include <asm-generic/word-at-a-time-instrumented.h>
+
 #endif /* __ASM_WORD_AT_A_TIME_H */
Signature · 1 line
2.47.3
#4 lore raw

PATCH 4/7 arm/word-at-a-time: Instrument load_unaligned_zeropad()

In reply to Bill Wendling

Convert arm to the arch_load_unaligned_zeropad() / load_unaligned_zeropad() split introduced for x86 earlier in this series, so KASAN/KCSAN also see reads through load_unaligned_zeropad() on this architecture.

Assisted-by Claude:claude-sonnet-5

---
Cc: Russell King <[email protected]>
Cc: "Russell King (Oracle)" <[email protected]>
Cc: Liyuan Pang <[email protected]>
Cc: Xie Yuanbin <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 arch/arm/include/asm/word-at-a-time.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm/include/asm/word-at-a-time.h +3 -1
index 5023f98d8293..ef0002d16dc1 100644
@@ -65,7 +65,7 @@ static inline unsigned long find_zero(unsigned long mask)
  * and the next page not being mapped, take the exception and
  * return zeroes in the non-existing part.
  */
-static inline unsigned long load_unaligned_zeropad(const void *addr)
+static inline unsigned long arch_load_unaligned_zeropad(const void *addr)
 {
 	unsigned long ret, tmp;
 
@@ -96,5 +96,7 @@ static inline unsigned long load_unaligned_zeropad(const void *addr)
 	return ret;
 }
 
+#include <asm-generic/word-at-a-time-instrumented.h>
+
 #endif	/* DCACHE_WORD_ACCESS */
 #endif /* __ASM_ARM_WORD_AT_A_TIME_H */
Signature · 1 line
2.47.3
#5 lore raw

PATCH 5/7 riscv/word-at-a-time: Instrument load_unaligned_zeropad()

In reply to Bill Wendling

Convert riscv to the arch_load_unaligned_zeropad() / load_unaligned_zeropad() split introduced for x86 earlier in this series, so KASAN/KCSAN also see reads through load_unaligned_zeropad() on this architecture.

Assisted-by Claude:claude-sonnet-5

---
Cc: Paul Walmsley <[email protected]>
Cc: Palmer Dabbelt <[email protected]>
Cc: Albert Ou <[email protected]>
Cc: Alexandre Ghiti <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 arch/riscv/include/asm/word-at-a-time.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/riscv/include/asm/word-at-a-time.h +3 -1
index 3802cda71ab7..a4294b50c918 100644
@@ -56,7 +56,7 @@ static inline unsigned long find_zero(unsigned long mask)
  * and the next page not being mapped, take the exception and
  * return zeroes in the non-existing part.
  */
-static inline unsigned long load_unaligned_zeropad(const void *addr)
+static inline unsigned long arch_load_unaligned_zeropad(const void *addr)
 {
 	unsigned long ret;
 
@@ -71,6 +71,8 @@ static inline unsigned long load_unaligned_zeropad(const void *addr)
 	return ret;
 }
 
+#include <asm-generic/word-at-a-time-instrumented.h>
+
 #endif	/* CONFIG_DCACHE_WORD_ACCESS */
 
 #endif /* _ASM_RISCV_WORD_AT_A_TIME_H */
Signature · 1 line
2.47.3
#6 lore raw

PATCH 6/7 s390/word-at-a-time: Instrument load_unaligned_zeropad()

In reply to Bill Wendling

Convert s390 to the arch_load_unaligned_zeropad() / load_unaligned_zeropad() split introduced for x86 earlier in this series, so KASAN/KCSAN also see reads through load_unaligned_zeropad() on this architecture.

Assisted-by Claude:claude-sonnet-5

---
Cc: Heiko Carstens <[email protected]>
Cc: Vasily Gorbik <[email protected]>
Cc: Alexander Gordeev <[email protected]>
Cc: Christian Borntraeger <[email protected]>
Cc: Sven Schnelle <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 arch/s390/include/asm/word-at-a-time.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/s390/include/asm/word-at-a-time.h +3 -1
index eaa19dee7699..6a055bee5ba0 100644
@@ -48,7 +48,7 @@ static inline unsigned long zero_bytemask(unsigned long data)
  * and the next page not being mapped, take the exception and
  * return zeroes in the non-existing part.
  */
-static inline unsigned long load_unaligned_zeropad(const void *addr)
+static inline unsigned long arch_load_unaligned_zeropad(const void *addr)
 {
 	unsigned long data;
 
@@ -62,4 +62,6 @@ static inline unsigned long load_unaligned_zeropad(const void *addr)
 	return data;
 }
 
+#include <asm-generic/word-at-a-time-instrumented.h>
+
 #endif /* _ASM_WORD_AT_A_TIME_H */
Signature · 1 line
2.47.3
#8 lore raw

PATCH 7/7 powerpc/word-at-a-time: Instrument load_unaligned_zeropad()

In reply to Bill Wendling

Convert powerpc to the arch_load_unaligned_zeropad() / load_unaligned_zeropad() split introduced for x86 earlier in this series, so KASAN/KCSAN also see reads through load_unaligned_zeropad() on this architecture.

Assisted-by Claude:claude-sonnet-5

---
Cc: Madhavan Srinivasan <[email protected]>
Cc: Michael Ellerman <[email protected]>
Cc: Nicholas Piggin <[email protected]>
Cc: "Christophe Leroy (CS GROUP)" <[email protected]>
Cc: "Ritesh Harjani (IBM)" <[email protected]>
Cc: Shrikanth Hegde <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 arch/powerpc/include/asm/word-at-a-time.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/word-at-a-time.h +3 -1
index 54653a863414..9a09620ca776 100644
@@ -164,7 +164,7 @@ static inline unsigned long prep_zero_mask(unsigned long a, unsigned long bits,
 #define FIXUP_SECTION ".fixup"
 #endif
 
-static inline unsigned long load_unaligned_zeropad(const void *addr)
+static inline unsigned long arch_load_unaligned_zeropad(const void *addr)
 {
 	unsigned long ret, offset, tmp;
 
@@ -203,4 +203,6 @@ static inline unsigned long load_unaligned_zeropad(const void *addr)
 
 #undef FIXUP_SECTION
 
+#include <asm-generic/word-at-a-time-instrumented.h>
+
 #endif /* _ASM_WORD_AT_A_TIME_H */
Signature · 1 line
2.47.3
#7 lore raw

Re: [PATCH 6/7] s390/word-at-a-time: Instrument load_unaligned_zeropad()

In reply to Bill Wendling
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines

Convert s390 to the arch_load_unaligned_zeropad() / load_unaligned_zeropad() split introduced for x86 earlier in this series, so KASAN/KCSAN also see reads through load_unaligned_zeropad() on this architecture.

Assisted-by Claude:claude-sonnet-5

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6