mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] x86/unwind/orc: Read the CS slot without KASAN checking
@ 2026-12-03  3:54 Zhao Gongyi
  2026-12-03  4:08 ` Josh Poimboeuf
  0 siblings, 1 reply; 2+ messages in thread
From: Zhao Gongyi @ 2026-12-03  3:54 UTC (permalink / raw)
  To: jpoimboe, peterz; +Cc: tglx, mingo, bp, dave.hansen, x86, hpa, linux-kernel

The end-of-stack check in unwind_next_frame() calls user_mode(), leaving
the recovered CS read instrumented by KASAN. Commit e504e74cc3a2
("x86/unwind/orc: Disable KASAN checking in the ORC unwinder, part 2")
converted other recovered register reads to READ_ONCE_NOCHECK(), but
left this read unchanged. Best-effort unwinding can visit poisoned slots
within a valid stack range.

Reading /proc/<pid>/stack has reported a two-byte stack-out-of-bounds
read at this check:

  BUG: KASAN: stack-out-of-bounds in user_mode [inline]
  unwind_next_frame
  arch_stack_walk
  stack_trace_save_tsk
  proc_pid_stack

Read the CS slot with READ_ONCE_NOCHECK() as well. Use the word-sized csx
member to satisfy the macro's size requirement, and test its low two bits
to preserve the x86-64 user_mode() predicate. The existing stack range
checks remain in place.

Fixes: ee9f8fce9964 ("x86/unwind: Add the ORC unwinder")
Signed-off-by: Zhao Gongyi <zhaogongyi@bytedance.com>
Link: https://syzkaller.appspot.com/bug?extid=d081d4f4ee906c359975
---
Changes since v1:
- Restore the original end-of-stack comment as requested by Josh.
  The code change is unchanged.

v1: https://lore.kernel.org/r/20261130125820.37165-1-zhaogongyi@bytedance.com
Review: https://lore.kernel.org/r/axChL0Zz1JvUSIGf@jpoimboe

Testing:
- Built baseline and patched unwind_orc.o on d430c96a291a with GCC 12.2,
  KASAN_GENERIC, KASAN_STACK and UNWINDER_ORC; no compiler diagnostics.
  Disassembly confirmed the nocheck CS read, NULL check and low-bit test.
- Built and booted baseline and patched kernels on 8bf5d77a8edacc under
  x86-64 QEMU/KVM with identical embedded configs and KASAN stack
  instrumentation. Five sampled proc-stack dump symbol sequences matched
  between the two kernels.
- In a 300-second QEMU/KVM run per kernel with the same workload (48
  churn threads, 12 readers), the baseline logged a two-byte KASAN
  stack-out-of-bounds read in unwind_next_frame() via proc_pid_stack();
  the patched kernel logged no KASAN report during that run.
- The target task and the ORC entry that produced the recovered regs
  pointer were not captured, so why the read hit a redzone is not
  diagnosed here.

 arch/x86/kernel/unwind_orc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kernel/unwind_orc.c b/arch/x86/kernel/unwind_orc.c
index 6407bc9..7c755e7 100644
--- a/arch/x86/kernel/unwind_orc.c
+++ b/arch/x86/kernel/unwind_orc.c
@@ -495,7 +495,7 @@ bool unwind_next_frame(struct unwind_state *state)
 	guard(rcu)();
 
 	/* End-of-stack check for user tasks: */
-	if (state->regs && user_mode(state->regs))
+	if (state->regs && (READ_ONCE_NOCHECK(state->regs->csx) & 3))
 		goto the_end;
 
 	/*

base-commit: d430c96a291a9c2e7f9a279e8bf9ec252d18c4a9
-- 
2.39.5 (Apple Git-154)

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] x86/unwind/orc: Read the CS slot without KASAN checking
  2026-12-03  3:54 [PATCH v2] x86/unwind/orc: Read the CS slot without KASAN checking Zhao Gongyi
@ 2026-12-03  4:08 ` Josh Poimboeuf
  0 siblings, 0 replies; 2+ messages in thread
From: Josh Poimboeuf @ 2026-12-03  4:08 UTC (permalink / raw)
  To: Zhao Gongyi; +Cc: peterz, tglx, mingo, bp, dave.hansen, x86, hpa, linux-kernel

On Thu, Dec 03, 2026 at 11:54:54AM +0800, Zhao Gongyi wrote:
> The end-of-stack check in unwind_next_frame() calls user_mode(), leaving
> the recovered CS read instrumented by KASAN. Commit e504e74cc3a2
> ("x86/unwind/orc: Disable KASAN checking in the ORC unwinder, part 2")
> converted other recovered register reads to READ_ONCE_NOCHECK(), but
> left this read unchanged. Best-effort unwinding can visit poisoned slots
> within a valid stack range.
> 
> Reading /proc/<pid>/stack has reported a two-byte stack-out-of-bounds
> read at this check:
> 
>   BUG: KASAN: stack-out-of-bounds in user_mode [inline]
>   unwind_next_frame
>   arch_stack_walk
>   stack_trace_save_tsk
>   proc_pid_stack
> 
> Read the CS slot with READ_ONCE_NOCHECK() as well. Use the word-sized csx
> member to satisfy the macro's size requirement, and test its low two bits
> to preserve the x86-64 user_mode() predicate. The existing stack range
> checks remain in place.
> 
> Fixes: ee9f8fce9964 ("x86/unwind: Add the ORC unwinder")
> Signed-off-by: Zhao Gongyi <zhaogongyi@bytedance.com>
> Link: https://syzkaller.appspot.com/bug?extid=d081d4f4ee906c359975

Acked-by: Josh Poimboeuf <jpoimboe@kernel.org>

-- 
Josh

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-03  4:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-03  3:54 [PATCH v2] x86/unwind/orc: Read the CS slot without KASAN checking Zhao Gongyi
2026-12-03  4:08 ` Josh Poimboeuf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®