Skip to content

Commit 332fa4a

Browse files
bjorn-rivosavpatel
authored andcommitted
riscv: kvm: Fix out-of-bounds array access
In kvm_riscv_vcpu_sbi_init() the entry->ext_idx can contain an out-of-bound index. This is used as a special marker for the base extensions, that cannot be disabled. However, when traversing the extensions, that special marker is not checked prior indexing the array. Add an out-of-bounds check to the function. Fixes: 56d8a38 ("RISC-V: KVM: Allow some SBI extensions to be disabled by default") Signed-off-by: Björn Töpel <bjorn@rivosinc.com> Reviewed-by: Anup Patel <anup@brainfault.org> Link: https://lore.kernel.org/r/20241104191503.74725-1-bjorn@kernel.org Signed-off-by: Anup Patel <anup@brainfault.org>
1 parent 60821fb commit 332fa4a

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

arch/riscv/kvm/vcpu_sbi.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,19 +486,22 @@ void kvm_riscv_vcpu_sbi_init(struct kvm_vcpu *vcpu)
486486
struct kvm_vcpu_sbi_context *scontext = &vcpu->arch.sbi_context;
487487
const struct kvm_riscv_sbi_extension_entry *entry;
488488
const struct kvm_vcpu_sbi_extension *ext;
489-
int i;
489+
int idx, i;
490490

491491
for (i = 0; i < ARRAY_SIZE(sbi_ext); i++) {
492492
entry = &sbi_ext[i];
493493
ext = entry->ext_ptr;
494+
idx = entry->ext_idx;
495+
496+
if (idx < 0 || idx >= ARRAY_SIZE(scontext->ext_status))
497+
continue;
494498

495499
if (ext->probe && !ext->probe(vcpu)) {
496-
scontext->ext_status[entry->ext_idx] =
497-
KVM_RISCV_SBI_EXT_STATUS_UNAVAILABLE;
500+
scontext->ext_status[idx] = KVM_RISCV_SBI_EXT_STATUS_UNAVAILABLE;
498501
continue;
499502
}
500503

501-
scontext->ext_status[entry->ext_idx] = ext->default_disabled ?
504+
scontext->ext_status[idx] = ext->default_disabled ?
502505
KVM_RISCV_SBI_EXT_STATUS_DISABLED :
503506
KVM_RISCV_SBI_EXT_STATUS_ENABLED;
504507
}

0 commit comments

Comments
 (0)