Skip to contentSkip to conversation

tracing/fprobe: Do not let KASAN check the $stackN read

PATCH In review Yifan Feng 2 participants 2 messages Nov 18 – Nov 22

Subsystems (2)
lore
Share & tools
lore

Yifan Feng

author
#0 lore raw

PATCH 1/1 tracing/fprobe: Do not let KASAN check the $stackN read

ftrace_regs_get_kernel_stack_nth() reads the Nth entry of the kernel stack for the $stackN argument of fprobe and tracepoint probe events. That entry can be in the redzone of another stack frame, so with CONFIG_KASAN_STACK every hit of such an event is reported:

  # echo 'f:fp_read vfs_read $stack10' >> dynamic_events
  # echo 1 > events/fprobes/fp_read/enable
  BUG: KASAN: stack-out-of-bounds in process_fetch_insn+0xac9/0xcb0

Use READ_ONCE_NOCHECK(), as arm64 does in regs_get_kernel_stack_nth() for kprobe events.

Fixes 0566cefe73b9 ("tracing/fprobe: Enable fprobe events with CONFIG_DYNAMIC_FTRACE_WITH_ARGS")

---
Tested on v7.4-rc3 in QEMU with KASAN: 6 reports before, 0 after. Found and written with the help of an AI coding assistant.

 include/linux/ftrace.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/ftrace.h +1 -1
index 1c2056a1a68b..bdb6ee6bc3b9 100644
@@ -270,7 +270,7 @@ ftrace_regs_get_kernel_stack_nth(struct ftrace_regs *fregs, unsigned int nth)
 	stackp = (unsigned long *)ftrace_regs_get_stack_pointer(fregs);
 	if (((unsigned long)(stackp + nth) & ~(THREAD_SIZE - 1)) ==
 	    ((unsigned long)stackp & ~(THREAD_SIZE - 1)))
-		return *(stackp + nth);
+		return READ_ONCE_NOCHECK(*(stackp + nth));
 
 	return 0;
 }

base-commit: 63fdb511c27afcebce797e3db580b3534ddfa639

Signature · 1 line
2.39.3 (Apple Git-146)
#1 lore raw
Quoted 9 lines · Yifan Feng

ftrace_regs_get_kernel_stack_nth() reads the Nth entry of the kernel stack for the $stackN argument of fprobe and tracepoint probe events. That entry can be in the redzone of another stack frame, so with CONFIG_KASAN_STACK every hit of such an event is reported:

  # echo 'f:fp_read vfs_read $stack10' >> dynamic_events
  # echo 1 > events/fprobes/fp_read/enable
  BUG: KASAN: stack-out-of-bounds in process_fetch_insn+0xac9/0xcb0
Quoted 5 lines

Use READ_ONCE_NOCHECK(), as arm64 does in regs_get_kernel_stack_nth() for kprobe events.

Fixes 0566cefe73b9 ("tracing/fprobe: Enable fprobe events with CONFIG_DYNAMIC_FTRACE_WITH_ARGS")

Looks good to me. Let me pick this to fixes branch.

Thanks!

Quoted 26 lines

---
Tested on v7.4-rc3 in QEMU with KASAN: 6 reports before, 0 after. Found and written with the help of an AI coding assistant.

 include/linux/ftrace.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/ftrace.h +1 -1
index 1c2056a1a68b..bdb6ee6bc3b9 100644
@@ -270,7 +270,7 @@ ftrace_regs_get_kernel_stack_nth(struct ftrace_regs *fregs, unsigned int nth)
 	stackp = (unsigned long *)ftrace_regs_get_stack_pointer(fregs);
 	if (((unsigned long)(stackp + nth) & ~(THREAD_SIZE - 1)) ==
 	    ((unsigned long)stackp & ~(THREAD_SIZE - 1)))
-		return *(stackp + nth);
+		return READ_ONCE_NOCHECK(*(stackp + nth));
 
 	return 0;
 }

base-commit: 63fdb511c27afcebce797e3db580b3534ddfa639

Signature · 1 line
2.39.3 (Apple Git-146)
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>