From f6abdaf25dfe0608277fc634f3927c1f184ede6d Mon Sep 17 00:00:00 2001 From: Nickolai Zeldovich Date: Sun, 11 Oct 2026 18:21:14 -0400 Subject: [PATCH] lib: sbi_pmu: Do not return uninitialized stack data on FW read errors sbi_ecall_pmu_handler() passes the uninitialized local temp to sbi_pmu_ctr_fw_read() and copies it into out->value unconditionally. sbi_pmu_ctr_fw_read() only writes *cval on success, so every error return of SBI_EXT_PMU_COUNTER_FW_READ (e.g. a hardware or inactive counter index) hands an M-mode stack word to the supervisor in a1. SBI_EXT_PMU_COUNTER_FW_READ_HI does the same with the upper half of temp on RV32. Initialize temp to zero so that the error paths return zero in sbiret.value; the success paths are unchanged. Fixes: 1664d0efce0b ("lib: sbi_pmu: Replace sbi_pmu_ctr_read() with sbi_pmu_ctr_fw_read()") Fixes: 51951d9e9af8 ("lib: sbi_pmu: Implement sbi_pmu_counter_fw_read_hi") Signed-off-by: Nickolai Zeldovich Assisted-By: Claude Fable 5.1 Reviewed-by: Anup Patel Link: https://lore.kernel.org/r/20261011222115.769450-1-nickolai@csail.mit.edu Signed-off-by: Anup Patel --- lib/sbi/sbi_ecall_pmu.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/sbi/sbi_ecall_pmu.c b/lib/sbi/sbi_ecall_pmu.c index 41bb1624..f2ce103c 100644 --- a/lib/sbi/sbi_ecall_pmu.c +++ b/lib/sbi/sbi_ecall_pmu.c @@ -22,7 +22,7 @@ static int sbi_ecall_pmu_handler(unsigned long extid, unsigned long funcid, struct sbi_ecall_return *out) { int ret = 0; - uint64_t temp; + uint64_t temp = 0; switch (funcid) { case SBI_EXT_PMU_NUM_COUNTERS: @@ -62,7 +62,6 @@ static int sbi_ecall_pmu_handler(unsigned long extid, unsigned long funcid, #endif break; case SBI_EXT_PMU_COUNTER_START: - #if __riscv_xlen == 32 temp = ((uint64_t)regs->a4 << 32) | regs->a3; #else