Skip to content

mm/kasan: fixes and __asan_(un)poison_memory_region() - #22278

Merged
GUIDINGLI merged 4 commits into
apache:masterfrom
huojianchaoyy:nx-mm-kasan-fixes-v1
Nov 25, 2026
Merged

GUIDINGLI merged 4 commits into
apache:masterfrom
huojianchaoyy:nx-mm-kasan-fixes-v1

Conversation

@huojianchaoyy

Copy link
Copy Markdown

Note: Please adhere to Contributing Guidelines.

Summary

KASAN changes from Xiaomi Vela, split out of #22206 as asked in its review.

  • kasan: fix link error: an empty __sanitizer_annotate_double_ended_contiguous_container(), which libc++ (<deque>) calls when built with the sanitizer.
  • kasan_report: fix spacing: the shadow memory dump fits in 80 columns.
  • mm/kasan: expose compiler-rt _asan(un)poison_memory_region: __asan_poison_memory_region() and __asan_unpoison_memory_region() on top of kasan_poison() / kasan_unpoison(), and a new <sanitizer/asan_interface.h>, so code written for user-space ASan links on a KASAN target.
  • mm/kasan: default enable DISABLE_READS_CHECK: KASAN instruments writes only by default.

Impact

  • CONFIG_MM_KASAN_DISABLE_READS_CHECK defaults to y: KASAN checks writes only unless it is turned off.
  • New API: __asan_poison_memory_region() / __asan_unpoison_memory_region() and <sanitizer/asan_interface.h>.

Testing

Host: Ubuntu 22.04 x86_64, GCC 13.4.0 (sim, aarch64-none-elf), CMake 3.23 + Ninja 1.10. Base: master 25b649e.

./tools/checkpatch.sh -c -u -m -g HEAD~4..HEAD passes.

sim:ostest (KASAN and UBSAN): a 320 s run completes one ostest pass with no KASAN/UBSAN report; apart from the delays printed by hrtimer_test, the output is the same as master's. CONFIG_MM_KASAN_DISABLE_READS_CHECK=y in that build.

qemu-armv8a:sw_tags builds. In both builds hook.c.o defines __asan_poison_memory_region, __asan_unpoison_memory_region and __sanitizer_annotate_double_ended_contiguous_container.

Not tested: linking libc++ <deque> with KASAN, real hardware.

chirping78 and others added 4 commits November 24, 2026 20:05
ld.lld: error: undefined symbol: __sanitizer_annotate_double_ended_contiguous_container
>>> referenced by deque:987 (include/libcxx/deque:987)

Signed-off-by: chenxiaoyi <chenxiaoyi@xiaomi.com>
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 <p-liuyanfeng9@xiaomi.com>
Code written against user-space AddressSanitizer (for example the mbedtls
test framework) includes <sanitizer/asan_interface.h> 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 <makejian@xiaomi.com>
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 <guanyi3@xiaomi.com>
@github-actions github-actions Bot added Area: Memory Management Memory Management issues Size: M The size of the change in this PR is medium labels Nov 24, 2026
@github-actions

Copy link
Copy Markdown

@GUIDINGLI
GUIDINGLI merged commit 9f68a4c into apache:master Nov 25, 2026
58 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Memory Management Memory Management issues Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants