Skip to contentSkip to conversation

RISC-V: KVM: Fix out-of-bounds by 1

PATCH Applied Ethan Tidmore 3 participants 4 messages Feb 28 – Mar 24

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

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

Fixes 8f0153ecd3bf1 ("RISC-V: KVM: Add skeleton support for perf")
---
 arch/riscv/kvm/vcpu_pmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kvm/vcpu_pmu.c +1 -1
index 3a4d54aa96d8..51a12f90fb30 100644
@@ -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
maintainer
#1 lore raw
Quoted 8 lines · Ethan Tidmore

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.

Quoted 8 lines

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

Fixes 8f0153ecd3bf1 ("RISC-V: KVM: Add skeleton support for perf")

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
index 3a4d54aa96d8..51a12f90fb30 100644
@@ -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

#2 lore raw
In reply to Ethan Tidmore
Quoted 9 lines · Ethan Tidmore

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 >=.

Quoted 7 lines

Detected by Smatch:
arch/riscv/kvm/vcpu_pmu.c:528 kvm_riscv_vcpu_pmu_ctr_info() error:
buffer overflow 'kvpmu->pmc' 64 <= 64

Fixes 8f0153ecd3bf1 ("RISC-V: KVM: Add skeleton support for perf")

---

Friendly ping.

Thanks,

ET

#3 lore raw
In reply to Ethan Tidmore
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->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.

[...]

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