This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit bfb5e5d13e78e5aae44184570aa5a41a585508c7 Author: xuxingliang <[email protected]> AuthorDate: Mon Feb 10 11:42:02 2025 +0800 gdbstub: avoid sanitize functions to read/write memory 1. The GDB stub reads and writes any memory GDB asks for, so KASAN must not check its accesses: build lib_gdbstub.c with -fno-sanitize=kernel-address when CONFIG_MM_KASAN is enabled. 2. memcpy() is still checked by KASAN even when the caller is not instrumented: the C version is instrumented, and the arch version is checked by hand in arch_libc.c. Use gdb_memcpy(), a plain byte copy under KASAN, to access the memory of the target. 3. running_regs() can be NULL in an interrupt handler that does not save the context, e.g. with exception_direct(); use the TCB registers then instead of dereferencing NULL. Signed-off-by: xuxingliang <[email protected]> --- libs/libc/gdbstub/CMakeLists.txt | 6 ++++++ libs/libc/gdbstub/Make.defs | 4 ++++ libs/libc/gdbstub/lib_gdbstub.c | 39 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/libs/libc/gdbstub/CMakeLists.txt b/libs/libc/gdbstub/CMakeLists.txt index 244156628a0..9dee19d8f8b 100644 --- a/libs/libc/gdbstub/CMakeLists.txt +++ b/libs/libc/gdbstub/CMakeLists.txt @@ -22,4 +22,10 @@ if(CONFIG_LIB_GDBSTUB) target_sources(c PRIVATE lib_gdbstub.c) + + if(CONFIG_MM_KASAN) + set_source_files_properties( + lib_gdbstub.c DIRECTORY .. PROPERTIES COMPILE_FLAGS + -fno-sanitize=kernel-address) + endif() endif() diff --git a/libs/libc/gdbstub/Make.defs b/libs/libc/gdbstub/Make.defs index 922601726ae..637781a4b85 100644 --- a/libs/libc/gdbstub/Make.defs +++ b/libs/libc/gdbstub/Make.defs @@ -26,6 +26,10 @@ ifeq ($(CONFIG_LIB_GDBSTUB),y) CSRCS += lib_gdbstub.c +ifneq ($(CONFIG_MM_KASAN),) +lib_gdbstub.c_CFLAGS += -fno-sanitize=kernel-address +endif + # Add the userfs directory to the build DEPPATH += --dep-path gdbstub diff --git a/libs/libc/gdbstub/lib_gdbstub.c b/libs/libc/gdbstub/lib_gdbstub.c index 964eea08a54..5d29757e9dc 100644 --- a/libs/libc/gdbstub/lib_gdbstub.c +++ b/libs/libc/gdbstub/lib_gdbstub.c @@ -783,6 +783,39 @@ static ssize_t gdb_hex2bin(FAR void *buf, size_t buf_len, return data_len / 2; } +/**************************************************************************** + * Name: gdb_memcpy + * + * Description: + * Copy data from src to dest. + * + * Input Parameters: + * dest - The destination buffer. + * src - The source buffer. + * len - The length of the data to copy. + * + * Returned Value: + * The destination buffer. + * + ****************************************************************************/ + +static FAR void *gdb_memcpy(FAR void *dest, FAR const void *src, size_t len) +{ +#ifdef CONFIG_MM_KASAN + FAR char *d = dest; + FAR const char *s = src; + + while (len--) + { + *d++ = *s++; + } + + return dest; +#else + return memcpy(dest, src, len); +#endif +} + /**************************************************************************** * Name: gdb_count_repeat * @@ -882,7 +915,7 @@ static ssize_t gdb_get_memory(FAR struct gdb_state_s *state, else { ret = MIN(len, buf_len); - memcpy(buf, (FAR const void *)addr, ret); + gdb_memcpy(buf, (FAR const void *)addr, ret); } } @@ -925,7 +958,7 @@ static ssize_t gdb_put_memory(FAR struct gdb_state_s *state, else { ret = MIN(len, buf_len); - memcpy((FAR void *)addr, buf, ret); + gdb_memcpy((FAR void *)addr, buf, ret); } } @@ -1049,7 +1082,7 @@ static void gdb_update_regcache(FAR struct gdb_state_s *state) reg = (FAR uint8_t *)tcb->xcp.regs; if (state->pid == _SCHED_GETTID()) { - if (up_interrupt_context()) + if (up_interrupt_context() && running_regs()) { reg = (FAR uint8_t *)running_regs(); }
