[PATCH v2] lib: sbi_pmu: fix integer overflow and zero-address in event_get_info
liutong
liutong at iscas.ac.cn
Thu Jul 30 09:02:52 PDT 2026
sbi_pmu_event_get_info() computes the shared memory size as
num_events * sizeof(struct sbi_pmu_event_info) without checking for
integer overflow. A sufficiently large num_events causes the product to
wrap around, making the domain check pass on a truncated size while the
loop iterates with the original count.
This allows S-mode to trigger out-of-bounds writes in M-mode memory.
A zero physical address is also not rejected, which would cause M-mode
to write to address 0 and hang the hart.
Add bounds checking on num_events before the multiplication and reject
zero shmem addresses early.
Fixes: e4345842168b ("lib: sbi_pmu: Implement SBI PMU event info function")
Signed-off-by: liutong <liutong at iscas.ac.cn>
---
Changes in v2:
- Added Fixes tag
lib/sbi/sbi_pmu.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/lib/sbi/sbi_pmu.c b/lib/sbi/sbi_pmu.c
index a0f6d2fa..3a2321cc 100644
--- a/lib/sbi/sbi_pmu.c
+++ b/lib/sbi/sbi_pmu.c
@@ -1061,7 +1061,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;
struct sbi_pmu_event_info *einfo;
struct sbi_pmu_hart_state *phs = pmu_thishart_state_ptr();
@@ -1076,6 +1076,14 @@ 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 (!shmem_phys_lo)
+ return SBI_ERR_INVALID_ADDRESS;
+
+ if (num_events > ((unsigned long)-1) / 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
--
2.34.1
More information about the opensbi
mailing list