summaryrefslogtreecommitdiff
path: root/arch/riscv/kvm
diff options
context:
space:
mode:
authorJiakai Xu <xujiakai2025@iscas.ac.cn>2026-03-16 01:45:32 +0000
committerAnup Patel <anup@brainfault.org>2026-03-27 20:02:11 +0530
commit99594f75b49edc7046057bca06d892c16967a9b3 (patch)
tree56bfcdadedf00e7a89360a7af2a6bb39eca0552a /arch/riscv/kvm
parentb7c958d7c1eb1cb9b2be7b5ee4129fcd66cec978 (diff)
downloadlwn-99594f75b49edc7046057bca06d892c16967a9b3.tar.gz
lwn-99594f75b49edc7046057bca06d892c16967a9b3.zip
RISC-V: KVM: Fix array out-of-bounds in pmu_ctr_read() and pmu_fw_ctr_read_hi()
When a guest invokes SBI_EXT_PMU_COUNTER_FW_READ or SBI_EXT_PMU_COUNTER_FW_READ_HI on a firmware counter that has not been configured via SBI_EXT_PMU_COUNTER_CFG_MATCH, the pmc->event_idx remains SBI_PMU_EVENT_IDX_INVALID (0xFFFFFFFF). get_event_code() extracts the lower 16 bits, yielding 0xFFFF (65535), which is then used to index into kvpmu->fw_event[]. Since fw_event is only RISCV_KVM_MAX_FW_CTRS (32) entries, this triggers an array-index-out-of-bounds: UBSAN: array-index-out-of-bounds in arch/riscv/kvm/vcpu_pmu.c:255:37 index 65535 is out of range for type 'kvm_fw_event [32]' Add a check for the known unconfigured case (SBI_PMU_EVENT_IDX_INVALID) and a WARN_ONCE guard for any unexpected out-of-bounds event codes, returning -EINVAL in both cases. Fixes: badc386869e2c ("RISC-V: KVM: Support firmware events") Fixes: 08fb07d6dcf71 ("RISC-V: KVM: Support 64 bit firmware counters on RV32") Signed-off-by: Jiakai Xu <xujiakai2025@iscas.ac.cn> Signed-off-by: Jiakai Xu <jiakaiPeanut@gmail.com> Reviewed-by: Andrew Jones <andrew.jones@oss.qualcomm.com> Link: https://lore.kernel.org/r/20260316014533.2312254-2-xujiakai2025@iscas.ac.cn Signed-off-by: Anup Patel <anup@brainfault.org>
Diffstat (limited to 'arch/riscv/kvm')
-rw-r--r--arch/riscv/kvm/vcpu_pmu.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/arch/riscv/kvm/vcpu_pmu.c b/arch/riscv/kvm/vcpu_pmu.c
index f3bf985dcf43..9e9f3302cef8 100644
--- a/arch/riscv/kvm/vcpu_pmu.c
+++ b/arch/riscv/kvm/vcpu_pmu.c
@@ -226,7 +226,14 @@ static int pmu_fw_ctr_read_hi(struct kvm_vcpu *vcpu, unsigned long cidx,
if (pmc->cinfo.type != SBI_PMU_CTR_TYPE_FW)
return -EINVAL;
+ if (pmc->event_idx == SBI_PMU_EVENT_IDX_INVALID)
+ return -EINVAL;
+
fevent_code = get_event_code(pmc->event_idx);
+ if (WARN_ONCE(fevent_code >= SBI_PMU_FW_MAX,
+ "Invalid firmware event code: %d\n", fevent_code))
+ return -EINVAL;
+
pmc->counter_val = kvpmu->fw_event[fevent_code].value;
*out_val = pmc->counter_val >> 32;
@@ -251,7 +258,14 @@ static int pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx,
pmc = &kvpmu->pmc[cidx];
if (pmc->cinfo.type == SBI_PMU_CTR_TYPE_FW) {
+ if (pmc->event_idx == SBI_PMU_EVENT_IDX_INVALID)
+ return -EINVAL;
+
fevent_code = get_event_code(pmc->event_idx);
+ if (WARN_ONCE(fevent_code >= SBI_PMU_FW_MAX,
+ "Invalid firmware event code: %d\n", fevent_code))
+ return -EINVAL;
+
pmc->counter_val = kvpmu->fw_event[fevent_code].value;
} else if (pmc->perf_event) {
pmc->counter_val += perf_event_read_value(pmc->perf_event, &enabled, &running);