From 491741df0d480155b42e86746badc151aac8ab7e Mon Sep 17 00:00:00 2001 From: chenxiaoyi Date: Wed, 11 Mar 2026 16:04:48 +0800 Subject: [PATCH 1/4] kasan: fix link error ld.lld: error: undefined symbol: __sanitizer_annotate_double_ended_contiguous_container >>> referenced by deque:987 (include/libcxx/deque:987) Signed-off-by: chenxiaoyi --- mm/kasan/hook.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mm/kasan/hook.c b/mm/kasan/hook.c index a38a165c9398a..d7eef2680827a 100644 --- a/mm/kasan/hook.c +++ b/mm/kasan/hook.c @@ -386,6 +386,14 @@ void __sanitizer_annotate_contiguous_container(FAR const void *beg, /* Shut up compiler complaints */ } +void __sanitizer_annotate_double_ended_contiguous_container( + FAR const void *beg, FAR const void *end, + FAR const void *old_beg, FAR const void *old_end, + FAR const void *new_beg, FAR const void *new_end) +{ + /* Shut up compiler complaints */ +} + void __asan_report_load_n_noabort(FAR void *addr, size_t size) { kasan_report(addr, size, false, return_address(0)); From d66d6b3d95e837f5dc70f21c4684d374d3aa9fbf Mon Sep 17 00:00:00 2001 From: Yanfeng Liu Date: Fri, 24 Oct 2025 10:57:15 +0800 Subject: [PATCH 2/4] kasan_report: fix spacing Current kasan dump exceeds 80 cols thus is not easy with standard terminal. This makes it fit in 80 cols. Signed-off-by: Yanfeng Liu --- mm/kasan/hook.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/kasan/hook.c b/mm/kasan/hook.c index d7eef2680827a..7230a557a73ca 100644 --- a/mm/kasan/hook.c +++ b/mm/kasan/hook.c @@ -146,7 +146,7 @@ static void kasan_show_memory(FAR const uint8_t *addr, size_t size, _alert("Shadow bytes around the buggy address:\n"); for (p = start; p < end; p += 16) { - int ret = sprintf(buffer, " %p: ", p); + int ret = sprintf(buffer, "%p: ", p); int i; for (i = 0; i < 16; i++) From 874bd943353b8e87e9c89154e043f37e38f324d7 Mon Sep 17 00:00:00 2001 From: makejian Date: Thu, 2 Jul 2026 10:25:35 +0800 Subject: [PATCH 3/4] mm/kasan: expose compiler-rt __asan_(un)poison_memory_region Code written against user-space AddressSanitizer (for example the mbedtls test framework) includes and calls __asan_poison_memory_region()/__asan_unpoison_memory_region() to enforce memory access policies. On a KASAN target NuttX does not link the compiler-rt user-space ASan runtime, so these standard entry points were undefined and such code failed to link. Provide them from mm/kasan alongside the other compiler-rt __asan_* entry points already implemented in hook.c, bridging to the in-kernel KASAN shadow via kasan_poison()/kasan_unpoison(). Add a matching public header include/sanitizer/asan_interface.h so the standard include path resolves on a target build. They are excluded for the SIM mode, where the host compiler-rt runtime already defines these symbols and kasan_poison() forwards to them. Signed-off-by: makejian --- include/sanitizer/asan_interface.h | 80 ++++++++++++++++++++++++++++++ mm/kasan/hook.c | 51 +++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 include/sanitizer/asan_interface.h diff --git a/include/sanitizer/asan_interface.h b/include/sanitizer/asan_interface.h new file mode 100644 index 0000000000000..3bec781da5d16 --- /dev/null +++ b/include/sanitizer/asan_interface.h @@ -0,0 +1,80 @@ +/**************************************************************************** + * include/sanitizer/asan_interface.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/* AddressSanitizer public interface. + * + * This is the same header the host compiler-rt runtime provides so that code + * written against user-space ASan (for example the mbedtls test framework) + * can include and call + * __asan_(un)poison_memory_region() unchanged on a NuttX target. NuttX does + * not link the compiler-rt user-space ASan runtime; on a KASAN target these + * entry points are provided by mm/kasan and bridge to the in-kernel KASAN + * shadow. The signatures match the upstream compiler-rt header. + */ + +#ifndef __INCLUDE_SANITIZER_ASAN_INTERFACE_H +#define __INCLUDE_SANITIZER_ASAN_INTERFACE_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef __cplusplus +extern "C" +{ +#endif + +/**************************************************************************** + * Name: __asan_poison_memory_region + * + * Description: + * Mark the memory region [addr, addr+size) as unaddressable, so that any + * instrumented access to it triggers a KASAN report until unpoisoned. + * + ****************************************************************************/ + +void __asan_poison_memory_region(FAR void const volatile *addr, size_t size); + +/**************************************************************************** + * Name: __asan_unpoison_memory_region + * + * Description: + * Mark the memory region [addr, addr+size) as addressable again. + * + ****************************************************************************/ + +void __asan_unpoison_memory_region(FAR void const volatile *addr, + size_t size); + +#ifdef __cplusplus +} +#endif + +#endif /* __INCLUDE_SANITIZER_ASAN_INTERFACE_H */ diff --git a/mm/kasan/hook.c b/mm/kasan/hook.c index 7230a557a73ca..d069ebe5ca5bf 100644 --- a/mm/kasan/hook.c +++ b/mm/kasan/hook.c @@ -25,6 +25,8 @@ #include #include +#include + #include #include #include @@ -378,6 +380,55 @@ void __asan_unregister_globals(void *ptr, size_t size) /* Shut up compiler complaints */ } +/**************************************************************************** + * Name: __asan_poison_memory_region + * + * Description: + * Compiler-rt AddressSanitizer public interface. User-space code built for + * ASan (for example the mbedtls test framework) includes + * and calls this to mark a region as + * unaddressable. NuttX has no compiler-rt user-space ASan runtime, so on a + * KASAN target this bridges the standard entry point to the in-kernel + * KASAN shadow. Regions not registered with KASAN are ignored by + * kasan_poison(). + * + * Input Parameters: + * addr - range start address + * size - range size + * + * Returned Value: + * None. + * + ****************************************************************************/ + +void __asan_poison_memory_region(FAR void const volatile *addr, size_t size) +{ + kasan_poison((FAR const void *)addr, size); +} + +/**************************************************************************** + * Name: __asan_unpoison_memory_region + * + * Description: + * Compiler-rt AddressSanitizer public interface, the counterpart of + * __asan_poison_memory_region(). Marks a region as addressable again by + * bridging to the in-kernel KASAN shadow. + * + * Input Parameters: + * addr - range start address + * size - range size + * + * Returned Value: + * None. + * + ****************************************************************************/ + +void __asan_unpoison_memory_region(FAR void const volatile *addr, + size_t size) +{ + kasan_unpoison((FAR const void *)addr, size); +} + void __sanitizer_annotate_contiguous_container(FAR const void *beg, FAR const void *end, FAR const void *old_mid, From 1a1f65111237d64010d372fb1a727f87f28a844f Mon Sep 17 00:00:00 2001 From: guanyi3 Date: Fri, 24 Jul 2026 10:08:14 +0800 Subject: [PATCH 4/4] mm/kasan: default enable DISABLE_READS_CHECK Change CONFIG_MM_KASAN_DISABLE_READS_CHECK default from n to y. This disables read-side instrumentation (--param asan-instrument-reads=0) by default, significantly reducing KASAN CPU overhead on hot paths like UART serial I/O. Write-side checks remain active to catch use-after-free and buffer overflows on write. Most memory corruption bugs (UAF, heap overflow, stack overflow) are caught by write-side checks. Read-side checks add substantial overhead with relatively lower detection value in embedded RTOS environments. Signed-off-by: guanyi3 --- mm/kasan/Kconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/kasan/Kconfig b/mm/kasan/Kconfig index cdf9d86d54cd3..04160df970fda 100644 --- a/mm/kasan/Kconfig +++ b/mm/kasan/Kconfig @@ -111,7 +111,7 @@ config MM_KASAN_DISABLE_NULL_POINTER_CHECK config MM_KASAN_DISABLE_READS_CHECK bool "Disable reads check" - default n + default y ---help--- This option disables kasan reads check. It speeds up performance compared with default read/write check. Only disable it when you are