From f45b9433ed03588edf7c5a9a803c67b2c9d09aee Mon Sep 17 00:00:00 2001 From: liutong Date: Thu, 10 Sep 2026 05:26:27 +0000 Subject: [PATCH] lib: sbi_pmu: fix integer overflow and zero-address in event_get_info sbi_pmu_event_get_info() computes the shared memory size as num_events * sizeof(struct sbi_pmu_event_info) before num_events is validated. The product can wrap, in which case the domain check runs against a truncated size while the loop below still iterates over the original count, that is, past the region that was checked. Check num_events against the largest value that cannot wrap, and reject a zero address, both before the multiplication. Fixes: e4345842168b ("lib: sbi_pmu: Implement SBI PMU event info function") Signed-off-by: liutong Reviewed-by: Anup Patel Link: https://lore.kernel.org/r/20260910052629.3359493-3-liutong@iscas.ac.cn Signed-off-by: Anup Patel --- lib/sbi/sbi_pmu.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/sbi/sbi_pmu.c b/lib/sbi/sbi_pmu.c index bf43ad89..54877ee6 100644 --- a/lib/sbi/sbi_pmu.c +++ b/lib/sbi/sbi_pmu.c @@ -1080,7 +1080,7 @@ int sbi_pmu_ctr_get_info(uint32_t cidx, unsigned long *ctr_info) int sbi_pmu_event_get_info(unsigned long shmem_phys_lo, unsigned long shmem_phys_hi, unsigned long num_events, unsigned long flags) { - unsigned long shmem_size = num_events * sizeof(struct sbi_pmu_event_info); + unsigned long shmem_size; int i, j, event_type, ret = 0; struct sbi_pmu_event_info *einfo; struct sbi_pmu_hart_state *phs = pmu_thishart_state_ptr(); @@ -1095,6 +1095,11 @@ int sbi_pmu_event_get_info(unsigned long shmem_phys_lo, unsigned long shmem_phys if (!num_events || (shmem_phys_lo & 0xF)) return SBI_ERR_INVALID_PARAM; + if (num_events > (-1UL / sizeof(struct sbi_pmu_event_info))) + return SBI_ERR_INVALID_PARAM; + + shmem_size = num_events * sizeof(struct sbi_pmu_event_info); + /* * On RV32, the M-mode can only access the first 4GB of * the physical address space because M-mode does not have