[PATCH] RISC-V: KVM: Fix shift-out-of-bounds in make_xfence_request()
From: Jiakai Xu
Date: Fri Apr 03 2026 - 02:14:46 EST
The make_xfence_request() function uses a shift operation to check if a
vCPU is in the hart mask:
if (!(hmask & (1UL << (vcpu->vcpu_id - hbase))))
However, when the difference between vcpu_id and hbase
is >= BITS_PER_LONG, the shift operation causes undefined behavior.
This was detected by UBSAN:
UBSAN: shift-out-of-bounds in arch/riscv/kvm/tlb.c:343:23
shift exponent 256 is too large for 64-bit type 'long unsigned int'
Fix this by adding a bounds check before the shift operation.
This bug was found by fuzzing the KVM RISC-V interface.
Fixes: 13acfec2dbcc ("RISC-V: KVM: Add remote HFENCE functions based on VCPU requests")
Signed-off-by: Jiakai Xu <jiakaiPeanut@xxxxxxxxx>
Signed-off-by: Jiakai Xu <xujiakai2025@xxxxxxxxxxx>
---
arch/riscv/kvm/tlb.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/arch/riscv/kvm/tlb.c b/arch/riscv/kvm/tlb.c
index ff1aeac4eb8eb..500e001513a11 100644
--- a/arch/riscv/kvm/tlb.c
+++ b/arch/riscv/kvm/tlb.c
@@ -333,14 +333,17 @@ static void make_xfence_request(struct kvm *kvm,
unsigned long i;
struct kvm_vcpu *vcpu;
unsigned int actual_req = req;
+ unsigned int idx;
DECLARE_BITMAP(vcpu_mask, KVM_MAX_VCPUS);
bitmap_zero(vcpu_mask, KVM_MAX_VCPUS);
kvm_for_each_vcpu(i, vcpu, kvm) {
if (hbase != -1UL) {
- if (vcpu->vcpu_id < hbase)
+ idx = vcpu->vcpu_id - hbase;
+
+ if (idx < 0 || idx >= BITS_PER_LONG)
continue;
- if (!(hmask & (1UL << (vcpu->vcpu_id - hbase))))
+ if (!(hmask & (1UL << idx)))
continue;
}
--
2.34.1