From: sashiko-bot@kernel.org
To: "Shuai Xue" <xueshuai@linux.alibaba.com>,
palmer@dabbelt.com, pjw@kernel.org, aou@eecs.berkeley.edu
Cc: alex@ghiti.fr, oleg@redhat.com, rostedt@goodmis.org,
mhiramat@kernel.org, mark.rutland@arm.com, peterz@infradead.org,
mingo@redhat.com, acme@kernel.org, namhyung@kernel.org,
alexander.shishkin@linux.intel.com, jolsa@kernel.org,
irogers@google.com, adrian.hunter@intel.com,
james.clark@linaro.org, jpoimboe@kernel.org, jikos@kernel.org,
mbenes@suse.cz, pmladek@suse.com, joe.lawrence@redhat.com,
shuah@kernel.org, mpdesouza@suse.com, martin@kaiser.cx,
kmehltretter@gmail.com, oliver.yang@linux.alibaba.com,
zhuo.song@linux.alibaba.com, jkchen@linux.alibaba.com,
linux-trace-kernel@vger.kernel.org,
linux-perf-users@vger.kernel.org, live-patching@vger.kernel.org
Subject: Re: [PATCH v7 09/10] riscv: Kconfig: enable HAVE_RELIABLE_STACKTRACE and HAVE_LIVEPATCH
Date: Tue, 10 Nov 2026 02:10:22 +0000 [thread overview]
Message-ID: <sashiko-outbox-206030@kernel.org> (raw)
In-Reply-To: <20261110013750.36800-10-xueshuai@linux.alibaba.com>
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] riscv: module: fault and UAF in apply_relocate_add() for klp
relocs
- [Medium] riscv: stacktrace: ftrace_caller() omits frames in reliable
unwind
- [Medium] riscv: stacktrace: uninitialized frame read in
return_to_handler()
- [Medium] riscv: stacktrace: arch_rethook_trampoline() omits caller
frame
--
commit 57947c1aaf895b44b13bb74dddecd8c57f82bb66
Author: Wang Han <wanghan@linux.alibaba.com>
riscv: Kconfig: enable HAVE_RELIABLE_STACKTRACE and HAVE_LIVEPATCH
This commit selects HAVE_RELIABLE_STACKTRACE and HAVE_LIVEPATCH on 64-bit
RISC-V configurations with FRAME_POINTER enabled, and sources
kernel/livepatch/Kconfig.
> diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
> index e7891a2a89da6..4512f67bbfd98 100644
> --- a/arch/riscv/Kconfig
> +++ b/arch/riscv/Kconfig
> @@ -186,6 +186,7 @@ config RISCV
> select HAVE_KRETPROBES
> # https://github.com/ClangBuiltLinux/linux/issues/1881
> select HAVE_LD_DEAD_CODE_DATA_ELIMINATION if !LD_IS_LLD
> + select HAVE_LIVEPATCH if FRAME_POINTER && 64BIT
[Severity: High]
Can klp_apply_object_relocs() -> apply_relocate_add() trigger a store page
fault and use-after-free when applying late .klp.rela.<module>.* relocations?
During load_module(), complete_formation() marks module text and rodata
(including .plt, .got, and .got.plt) read-only under CONFIG_STRICT_MODULE_RWX
before klp_enable_patch() or klp_module_coming() runs. When
apply_relocate_add() is later called on a module with mod->state !=
MODULE_STATE_UNFORMED, the relocation handlers in arch/riscv/kernel/module.c
(such as riscv_insn_rmw()) and module_emit_got_entry() /
module_emit_plt_entry() in arch/riscv/kernel/module-sections.c write directly
to read-only module memory instead of using text patching helpers and
flushing the instruction cache:
arch/riscv/kernel/module.c:riscv_insn_rmw() {
...
parcel[0] = cpu_to_le16(insn);
parcel[1] = cpu_to_le16(insn >> 16);
return 0;
}
In addition, module_frob_arch_sections() caches pointers into the temporary
info->sechdrs buffer in mod->arch.plt.shdr, mod->arch.got.shdr, and
mod->arch.got_plt.shdr, which free_copy() frees via vfree() at the end of
load_module():
arch/riscv/kernel/module-sections.c:module_frob_arch_sections() {
...
for (i = 0; i < ehdr->e_shnum; i++) {
if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt"))
mod->arch.plt.shdr = sechdrs + i;
else if (!strcmp(secstrings + sechdrs[i].sh_name, ".got"))
mod->arch.got.shdr = sechdrs + i;
else if (!strcmp(secstrings + sechdrs[i].sh_name, ".got.plt"))
mod->arch.got_plt.shdr = sechdrs + i;
}
...
}
When late livepatch relocations emit GOT or PLT entries,
module_emit_got_entry() and module_emit_plt_entry() dereference these freed
shdr pointers (e.g. got_sec->shdr->sh_addr), and re-applying relocations when
a target module is reloaded increments num_entries past max_entries and hits
BUG_ON().
> select HAVE_MOVE_PMD
> select HAVE_MOVE_PUD
> select HAVE_PAGE_SIZE_4KB
> @@ -195,6 +196,7 @@ config RISCV
> select HAVE_PERF_USER_STACK_DUMP
> select HAVE_POSIX_CPU_TIMERS_TASK_WORK
> select HAVE_REGS_AND_STACK_ACCESS_API
> + select HAVE_RELIABLE_STACKTRACE if FRAME_POINTER && 64BIT
[Severity: Medium]
Does arch_stack_walk_reliable() silently omit the traced function and its
caller when unwinding a task inside an ftrace callback or direct trampoline?
With CONFIG_DYNAMIC_FTRACE_WITH_ARGS, the patchable entry of a traced
function jumps to ftrace_caller before the function sets up its stack frame.
At that point, t0 holds the traced function's PC, ra holds the return address
in its caller, and s0 still holds the caller's frame pointer. In
arch/riscv/kernel/mcount-dyn.S, SAVE_ABI_REGS saves t0, x1, and x8 into
struct __arch_ftrace_regs without constructing frame records on the stack or
updating s0:
arch/riscv/kernel/mcount-dyn.S:SAVE_ABI_REGS {
addi sp, sp, -FREGS_SIZE_ON_STACK
REG_S t0, FREGS_EPC(sp)
REG_S x1, FREGS_RA(sp)
REG_S x8, FREGS_S0(sp)
...
}
When ftrace_caller calls op->func, op->func saves the unchanged s0 in its
frame record. Unwinding that frame record in kunwind_next_frame_record()
advances directly to the caller's caller with state->source ==
KUNWIND_SOURCE_FRAME, which arch_reliable_kunwind_consume_entry() accepts as
reliable. During a livepatch transition, klp_check_stack() can miss that a
preempted or blocked task is executing inside a patched function or its
caller.
[Severity: Medium]
Can kunwind_next_frame_record() read uninitialized stack memory when
unwinding through return_to_handler()?
In arch/riscv/kernel/mcount.S, SAVE_RET_ABI_STATE allocates
FREGS_SIZE_ON_STACK bytes, saves ra and s0 at low offsets from sp, and sets
s0 to sp + FREGS_SIZE_ON_STACK before return_to_handler calls
ftrace_return_to_handler():
arch/riscv/kernel/mcount.S:SAVE_RET_ABI_STATE {
addi sp, sp, -FREGS_SIZE_ON_STACK
REG_S ra, FREGS_RA(sp)
REG_S s0, FREGS_S0(sp)
REG_S a0, FREGS_A0(sp)
REG_S a1, FREGS_A1(sp)
addi s0, sp, FREGS_SIZE_ON_STACK
}
The 16 bytes at s0 - 16 (sp + FREGS_SIZE_ON_STACK - 16) are never
initialized. When unwinding a task inside ftrace_return_to_handler(),
kunwind_next_frame_record() restores fp = sp + FREGS_SIZE_ON_STACK and on the
next step reads fp - sizeof(*record) as a struct frame_record, either
triggering WARN_ON_ONCE(tsk == current) in kunwind_next_frame_record_meta()
if those bytes happen to be zero or following a bogus frame pointer and
skipping live frames while returning 0 (reliable).
[Severity: Medium]
Does unwinding from inside arch_rethook_trampoline_callback() omit the caller
of the probed function while still reporting a reliable stack trace?
When a kretprobed function returns to arch_rethook_trampoline(), its epilogue
has already restored s0 to its caller's frame pointer, while the return
address in the caller is held in current->rethooks. In
arch/riscv/kernel/probes/rethook_trampoline.S, arch_rethook_trampoline
allocates PT_SIZE_ON_STACK without initializing S_STACKFRAME
(FRAME_META_TYPE_PT_REGS) or updating s0 before calling
arch_rethook_trampoline_callback():
arch/riscv/kernel/probes/rethook_trampoline.S:arch_rethook_trampoline() {
addi sp, sp, -(PT_SIZE_ON_STACK)
save_all_base_regs
move a0, sp /* pt_regs */
call arch_rethook_trampoline_callback
...
}
When kunwind unwinds arch_rethook_trampoline_callback()'s frame record,
state->common.pc is inside arch_rethook_trampoline (past the entry address
checked by is_kretprobe_trampoline()) and state->common.fp is the caller's
frame pointer. The next unwind step reads the frame record of the caller's
caller, skipping the caller of the probed function and returning 0
(reliable).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261110013750.36800-1-xueshuai@linux.alibaba.com?part=9
next prev parent reply other threads:[~2026-11-10 2:10 UTC|newest]
Thread overview: 39+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-14 9:26 [PATCH v6 0/7] riscv: Add reliable stack unwinding for livepatch Shuai Xue
2026-09-14 9:26 ` [PATCH v6 1/7] riscv: stacktrace: Add frame record metadata Shuai Xue
2026-09-14 9:40 ` sashiko-bot
2026-09-14 9:26 ` [PATCH v6 2/7] riscv: stacktrace: disable KASAN and KCOV instrumentation for stacktrace.o Shuai Xue
2026-09-14 9:32 ` sashiko-bot
2026-09-14 9:26 ` [PATCH v6 3/7] riscv: ftrace: always preserve s0 in dynamic ftrace register frame Shuai Xue
2026-09-14 9:32 ` sashiko-bot
2026-09-14 9:26 ` [PATCH v6 4/7] riscv: stacktrace: introduce stack-bound tracking helpers Shuai Xue
2026-09-14 9:33 ` sashiko-bot
2026-09-14 9:26 ` [PATCH v6 5/7] riscv: stacktrace: switch to frame-pointer based unwinder Shuai Xue
2026-09-14 9:39 ` sashiko-bot
2026-10-09 18:59 ` Karl Mehltretter
2026-10-10 3:38 ` Shuai Xue
2026-10-13 20:18 ` Martin Kaiser
2026-10-14 13:09 ` Shuai Xue
2026-09-14 9:26 ` [PATCH v6 6/7] riscv: Kconfig: enable HAVE_RELIABLE_STACKTRACE and HAVE_LIVEPATCH Shuai Xue
2026-09-14 9:38 ` sashiko-bot
2026-09-14 9:26 ` [PATCH v6 7/7] selftests/livepatch: Add RISC-V syscall wrapper prefix Shuai Xue
2026-09-14 9:38 ` sashiko-bot
2026-09-29 7:28 ` [PATCH v6 0/7] riscv: Add reliable stack unwinding for livepatch Martin Kaiser
2026-10-08 11:42 ` Shuai Xue
2026-10-12 8:44 ` Martin Kaiser
2026-11-10 1:37 ` [PATCH v7 00/10] " Shuai Xue
2026-11-10 1:37 ` [PATCH v7 01/10] riscv: stacktrace: do not report the walker's own frame from arch_stack_walk() Shuai Xue
2026-11-10 1:52 ` sashiko-bot
2026-11-10 1:37 ` [PATCH v7 02/10] riscv: Add KUnit test for stack trace start frames Shuai Xue
2026-11-10 1:50 ` sashiko-bot
2026-11-10 1:37 ` [PATCH v7 03/10] riscv: ftrace: use the frame pointer as the function graph return-address cookie Shuai Xue
2026-11-10 1:37 ` [PATCH v7 04/10] riscv: stacktrace: Add frame record metadata Shuai Xue
2026-11-10 1:54 ` sashiko-bot
2026-11-10 1:37 ` [PATCH v7 05/10] riscv: stacktrace: disable KASAN and KCOV instrumentation for stacktrace.o Shuai Xue
2026-11-10 1:37 ` [PATCH v7 06/10] riscv: ftrace: always preserve s0 in dynamic ftrace register frame Shuai Xue
2026-11-10 1:37 ` [PATCH v7 07/10] riscv: stacktrace: introduce stack-bound tracking helpers Shuai Xue
2026-11-10 1:37 ` [PATCH v7 08/10] riscv: stacktrace: switch to frame-pointer based unwinder Shuai Xue
2026-11-10 2:20 ` sashiko-bot
2026-11-10 1:37 ` [PATCH v7 09/10] riscv: Kconfig: enable HAVE_RELIABLE_STACKTRACE and HAVE_LIVEPATCH Shuai Xue
2026-11-10 2:10 ` sashiko-bot [this message]
2026-11-10 1:37 ` [PATCH v7 10/10] selftests/livepatch: Add RISC-V syscall wrapper prefix Shuai Xue
2026-11-10 6:35 ` [PATCH v7 00/10] riscv: Add reliable stack unwinding for livepatch Karl Mehltretter
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-206030@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alex@ghiti.fr \
--cc=alexander.shishkin@linux.intel.com \
--cc=aou@eecs.berkeley.edu \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=jikos@kernel.org \
--cc=jkchen@linux.alibaba.com \
--cc=joe.lawrence@redhat.com \
--cc=jolsa@kernel.org \
--cc=jpoimboe@kernel.org \
--cc=kmehltretter@gmail.com \
--cc=linux-perf-users@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=live-patching@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=martin@kaiser.cx \
--cc=mbenes@suse.cz \
--cc=mhiramat@kernel.org \
--cc=mingo@redhat.com \
--cc=mpdesouza@suse.com \
--cc=namhyung@kernel.org \
--cc=oleg@redhat.com \
--cc=oliver.yang@linux.alibaba.com \
--cc=palmer@dabbelt.com \
--cc=peterz@infradead.org \
--cc=pjw@kernel.org \
--cc=pmladek@suse.com \
--cc=rostedt@goodmis.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=shuah@kernel.org \
--cc=xueshuai@linux.alibaba.com \
--cc=zhuo.song@linux.alibaba.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox