[PW_SID:1067079] RISC-V: KVM: Fix array out-of-bounds in firmware counter reads#1616
[PW_SID:1067079] RISC-V: KVM: Fix array out-of-bounds in firmware counter reads#1616linux-riscv-bot wants to merge 2 commits intoworkflow__riscv__fixesfrom
Conversation
…_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: badc386 ("RISC-V: KVM: Support firmware events") Fixes: 08fb07d ("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> Signed-off-by: Linux RISC-V bot <linux.riscv.bot@gmail.com>
The current sbi_pmu_test attempts to read firmware counters without configuring them first with SBI_EXT_PMU_COUNTER_CFG_MATCH. Previously this did not fail because KVM incorrectly allowed the read and accessed fw_event[] with an out-of-bounds index when the counter was unconfigured. After fixing that bug, the read now correctly returns SBI_ERR_INVALID_PARAM, causing the selftest to fail. Update the test to configure a firmware event before reading the counter. Also add a negative test to ensure that attempting to read an unconfigured firmware counter fails gracefully. 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> Reviewed-by: Nutty Liu <nutty.liu@hotmail.com> Signed-off-by: Linux RISC-V bot <linux.riscv.bot@gmail.com>
|
Patch 1: "[v5,1/2] RISC-V: KVM: Fix array out-of-bounds in pmu_ctr_read() and pmu_fw_ctr_read_hi()" |
|
Patch 1: "[v5,1/2] RISC-V: KVM: Fix array out-of-bounds in pmu_ctr_read() and pmu_fw_ctr_read_hi()" |
|
Patch 1: "[v5,1/2] RISC-V: KVM: Fix array out-of-bounds in pmu_ctr_read() and pmu_fw_ctr_read_hi()" |
|
Patch 1: "[v5,1/2] RISC-V: KVM: Fix array out-of-bounds in pmu_ctr_read() and pmu_fw_ctr_read_hi()" |
|
Patch 1: "[v5,1/2] RISC-V: KVM: Fix array out-of-bounds in pmu_ctr_read() and pmu_fw_ctr_read_hi()" |
|
Patch 1: "[v5,1/2] RISC-V: KVM: Fix array out-of-bounds in pmu_ctr_read() and pmu_fw_ctr_read_hi()" |
|
Patch 1: "[v5,1/2] RISC-V: KVM: Fix array out-of-bounds in pmu_ctr_read() and pmu_fw_ctr_read_hi()" |
|
Patch 1: "[v5,1/2] RISC-V: KVM: Fix array out-of-bounds in pmu_ctr_read() and pmu_fw_ctr_read_hi()" |
|
Patch 1: "[v5,1/2] RISC-V: KVM: Fix array out-of-bounds in pmu_ctr_read() and pmu_fw_ctr_read_hi()" |
|
Patch 1: "[v5,1/2] RISC-V: KVM: Fix array out-of-bounds in pmu_ctr_read() and pmu_fw_ctr_read_hi()" |
|
Patch 1: "[v5,1/2] RISC-V: KVM: Fix array out-of-bounds in pmu_ctr_read() and pmu_fw_ctr_read_hi()" |
|
Patch 1: "[v5,1/2] RISC-V: KVM: Fix array out-of-bounds in pmu_ctr_read() and pmu_fw_ctr_read_hi()" |
|
Patch 2: "[v5,2/2] RISC-V: KVM: selftests: Fix firmware counter read in sbi_pmu_test" |
|
Patch 2: "[v5,2/2] RISC-V: KVM: selftests: Fix firmware counter read in sbi_pmu_test" |
|
Patch 2: "[v5,2/2] RISC-V: KVM: selftests: Fix firmware counter read in sbi_pmu_test" |
|
Patch 2: "[v5,2/2] RISC-V: KVM: selftests: Fix firmware counter read in sbi_pmu_test" |
|
Patch 2: "[v5,2/2] RISC-V: KVM: selftests: Fix firmware counter read in sbi_pmu_test" |
|
Patch 2: "[v5,2/2] RISC-V: KVM: selftests: Fix firmware counter read in sbi_pmu_test" |
|
Patch 2: "[v5,2/2] RISC-V: KVM: selftests: Fix firmware counter read in sbi_pmu_test" |
|
Patch 2: "[v5,2/2] RISC-V: KVM: selftests: Fix firmware counter read in sbi_pmu_test" |
|
Patch 2: "[v5,2/2] RISC-V: KVM: selftests: Fix firmware counter read in sbi_pmu_test" |
|
Patch 2: "[v5,2/2] RISC-V: KVM: selftests: Fix firmware counter read in sbi_pmu_test" |
|
Patch 2: "[v5,2/2] RISC-V: KVM: selftests: Fix firmware counter read in sbi_pmu_test" |
|
Patch 2: "[v5,2/2] RISC-V: KVM: selftests: Fix firmware counter read in sbi_pmu_test" |
PR for series 1067079 applied to workflow__riscv__fixes
Name: RISC-V: KVM: Fix array out-of-bounds in firmware counter reads
URL: https://patchwork.kernel.org/project/linux-riscv/list/?series=1067079
Version: 5