RISC-V: KVM: Fix out-of-bounds by 1
Subsystems (3)
PATCH 1/1 RISC-V: KVM: Fix out-of-bounds by 1
The array kvpmu->pmc is defined as:
struct kvm_pmc pmc[RISCV_KVM_MAX_COUNTERS];
So, accessing it with index RISCV_KVM_MAX_COUNTERS would be out-of-bounds by 1.
Change index check from > to >=.
Detected by Smatch:arch/riscv/kvm/vcpu_pmu.c:528 kvm_riscv_vcpu_pmu_ctr_info() error:
buffer overflow 'kvpmu->pmc' 64 <= 64
--- arch/riscv/kvm/vcpu_pmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kvm/vcpu_pmu.c +1 -1
@@ -520,7 +520,7 @@ int kvm_riscv_vcpu_pmu_ctr_info(struct kvm_vcpu *vcpu, unsigned long cidx, {struct kvm_pmu *kvpmu =vcpu_to_pmu (vcpu); -if (cidx >RISCV_KVM_MAX_COUNTERS || cidx ==1 ) { +if (cidx >=RISCV_KVM_MAX_COUNTERS || cidx ==1 ) { retdata->err_val =SBI_ERR_INVALID_PARAM ;return 0 ; }
Signature · 1 line
2.53.0
Anup Patel
maintainerQuoted 8 lines · Ethan Tidmore
The array
kvpmu->pmcis defined as:struct
kvm_pmcpmc[RISCV_KVM_MAX_COUNTERS];So, accessing it with index
RISCV_KVM_MAX_COUNTERSwould be out-of-bounds by 1.
Quoted 8 lines
Change index check from > to >=.
Detected by Smatch:
arch/riscv/kvm/vcpu_pmu.c:528kvm_riscv_vcpu_pmu_ctr_info()error:
buffer overflow 'kvpmu->pmc' 64 <= 64Fixes 8f0153ecd3bf1 ("RISC-V: KVM: Add skeleton support for perf")Signed-off-by Ethan Tidmore <[email protected]>
Radim had already sent a similar which was merged.
Refer, https://lore.kernel.org/r/[email protected]
Regards,
Anup
Quoted 20 lines
--- arch/riscv/kvm/vcpu_pmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/riscv/kvm/vcpu_pmu.c +1
-2@@ -520,7 +520,7 @@ int kvm_riscv_vcpu_pmu_ctr_info(struct kvm_vcpu *vcpu, unsigned long cidx, {struct kvm_pmu *kvpmu =vcpu_to_pmu (vcpu); -if (cidx >RISCV_KVM_MAX_COUNTERS || cidx ==1 ) { +if (cidx >=RISCV_KVM_MAX_COUNTERS || cidx ==1 ) { retdata->err_val =SBI_ERR_INVALID_PARAM ;return 0 ; } --2.53.0
Quoted 9 lines · Ethan Tidmore
The array
kvpmu->pmcis defined as:struct
kvm_pmcpmc[RISCV_KVM_MAX_COUNTERS];So, accessing it with index
RISCV_KVM_MAX_COUNTERSwould be out-of-bounds by 1.Change index check from > to >=.
Quoted 7 lines
Detected by Smatch:
arch/riscv/kvm/vcpu_pmu.c:528kvm_riscv_vcpu_pmu_ctr_info()error:
buffer overflow 'kvpmu->pmc' 64 <= 64Fixes 8f0153ecd3bf1 ("RISC-V: KVM: Add skeleton support for perf")Signed-off-by Ethan Tidmore <[email protected]>---
Friendly ping.
Thanks,
ET
This patch was applied to riscv/linux.git (for-next) by Anup Patel <[email protected]>:
Hello:
This patch was applied to riscv/linux.git (for-next) by Anup Patel <[email protected]>:
Quoted 8 lines · On Sat, 28 Feb 2026 09:22:26 -0600 you wrote:
The array
kvpmu->pmcis defined as:struct
kvm_pmcpmc[RISCV_KVM_MAX_COUNTERS];So, accessing it with index
RISCV_KVM_MAX_COUNTERSwould be out-of-bounds by 1.[...]
Here is the summary with links:
- RISC-V: KVM: Fix out-of-bounds by 1
https://git.kernel.org/riscv/c/5c1bb0787111
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html