Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] arm64: stacktrace: broken unwind of synthetic pt_regs in
kunwind_next()
--
commit 68ea7139e10984e52da92dcf88b763bce8dfdff8
Author: Dylan Hatch [off-list ref]
unwind: arm64: use .lr_unwind to unwind interrupt frames
This commit adds kunwind_next_regs_lr_unwind() to unwind across pt_regs
exception boundaries using .lr_unwind metadata, falling back to
frame-pointer unwinding and marking the stacktrace unreliable on failure.
quoted hunk ↗ jump to hunk
diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c
index 3ebcf8c53fb04..842555a4a3fe2 100644
--- a/arch/arm64/kernel/stacktrace.c
+++ b/arch/arm64/kernel/stacktrace.c
[ ... ]
quoted hunk ↗ jump to hunk
@@ -244,6 +249,65 @@ kunwind_next_frame_record(struct kunwind_state *state)
return 0;
}
+#ifdef CONFIG_ARM64_LR_UNWIND
+
+/*
+ * Unwind from a pt_regs according to .lr_unwind info.
+ */
+static __always_inline int
+kunwind_next_regs_lr_unwind(struct kunwind_state *state)
+{
+ struct pt_regs *regs = state->regs;
+ enum lr_unwind_mode mode;
+ unsigned int sp_off;
+ unsigned long fp;
+ int err;
+
+ if (WARN_ON_ONCE(state->source != KUNWIND_SOURCE_REGS_PC))
+ return -EINVAL;
+ if (WARN_ON_ONCE(!regs))
+ return -EINVAL;
+
+ state->regs = NULL;
+
+ err = lr_unwind_find(state->common.pc, &mode, &sp_off);
+ if (err) {
+ /* Fallback to FP based unwinder */
+ state->unreliable = true;
+ return kunwind_next_frame_record(state);
+ }
+
+ switch (mode) {
+ case LR_UNWIND_LIVE:
+ if (state->common.fp & 0x7)
+ return -EINVAL;
+ state->common.pc = regs->regs[30];
+ state->source = KUNWIND_SOURCE_REGS_LR;
+ return 0;
+ case LR_UNWIND_FP:
+ return kunwind_next_frame_record(state);\n> + case LR_UNWIND_SP:
+ if (check_add_overflow(regs->sp, (unsigned long)sp_off, &fp))
+ return -EINVAL;
+ state->common.fp = fp;
+ return kunwind_next_frame_record(state);
[Severity: High]
Does kunwind_next_regs_lr_unwind() mis-unwind synthetic pt_regs passed via
arch_stack_walk() -> kunwind_init_from_regs()?
kunwind_init_from_regs() sets state->source = KUNWIND_SOURCE_REGS_PC for
any caller-supplied pt_regs, not just on-stack exception frames. Software
perf events, tracepoints, and BPF stack helpers pass a pt_regs filled by
perf_arch_fetch_caller_regs():
arch/arm64/include/asm/perf_event.h:perf_arch_fetch_caller_regs() {
(regs)->pc = (__ip); \
(regs)->regs[29] = (unsigned long) __builtin_frame_address(0); \
(regs)->sp = current_stack_pointer; \
(regs)->pstate = PSR_MODE_EL1h; \
}
Here regs->pc is CALLER_ADDR0 (in the caller frame), while regs->sp and
regs->regs[29] belong to the inner frame and regs->regs[30] is unset.
When lr_unwind_find(regs->pc) returns LR_UNWIND_SP, adding the caller's
sp_off to the inner frame's regs->sp points state->common.fp into the
middle of the stack frame, causing kunwind_next_frame_record() to treat
arbitrary stack words as {fp, lr} (leaking stack contents into perf or BPF
callchains or hitting WARN_ON_ONCE(tsk == current) in
kunwind_next_frame_record_meta() if both words are zero).
Similarly, arch_ftrace_fill_perf_regs() leaves regs->regs[30] unset (so
LR_UNWIND_LIVE at fentry sets pc to 0 and drops the caller frame) and at
fexit copies an uninitialized fregs->sp from return_to_handler into
regs->sp.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117043504.1021063-1-dylanbhatch@google.com?part=12