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