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: e434584216 ("lib: sbi_pmu: Implement SBI PMU event info function")
Signed-off-by: liutong <liutong@iscas.ac.cn>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260910052629.3359493-3-liutong@iscas.ac.cn
Signed-off-by: Anup Patel <anup@brainfault.org>
This commit is contained in:
liutong authored and Anup Patel committed 2026-10-26 10:28:28 +05:30
1 parent a14e4d24a1
commit f45b9433ed
1 file changed
+6 -1
+6 -1
View File
@@ -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