Skip to contentSkip to conversation

RISC-V: KVM: fix stack overrun when loading vlenb

PATCH In review Radim Krčmář 4 participants 4 messages Aug 5, 2025 – Aug 22, 2025

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

PATCH 1/1 RISC-V: KVM: fix stack overrun when loading vlenb

The userspace load can put up to 2048 bits into an xlen bit stack buffer. We want only xlen bits, so check the size beforehand.

Fixes 2fa290372dfe ("RISC-V: KVM: add 'vlenb' Vector CSR")
---
 arch/riscv/kvm/vcpu_vector.c | 2 ++
 1 file changed, 2 insertions(+)
arch/riscv/kvm/vcpu_vector.c +2 -0
index a5f88cb717f3..05f3cc2d8e31 100644
@@ -182,6 +182,8 @@ int kvm_riscv_vcpu_set_reg_vector(struct kvm_vcpu *vcpu,
 		struct kvm_cpu_context *cntx = &vcpu->arch.guest_context;
 		unsigned long reg_val;
 
+		if (reg_size != sizeof(reg_val))
+			return -EINVAL;
 		if (copy_from_user(&reg_val, uaddr, reg_size))
 			return -EFAULT;
 		if (reg_val != cntx->vector.vlenb)
Signature · 1 line
2.50.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Radim Krčmář

The userspace load can put up to 2048 bits into an xlen bit stack buffer. We want only xlen bits, so check the size beforehand.

Fixes 2fa290372dfe ("RISC-V: KVM: add 'vlenb' Vector CSR")

---

Quoted 16 lines
  arch/riscv/kvm/vcpu_vector.c | 2 ++
  1 file changed, 2 insertions(+)
arch/riscv/kvm/vcpu_vector.c +2 -0
index a5f88cb717f3..05f3cc2d8e31 100644
@@ -182,6 +182,8 @@ int kvm_riscv_vcpu_set_reg_vector(struct kvm_vcpu *vcpu,
  		struct kvm_cpu_context *cntx = &vcpu->arch.guest_context;
  		unsigned long reg_val;
 
+		if (reg_size != sizeof(reg_val))
+			return -EINVAL;
  		if (copy_from_user(&reg_val, uaddr, reg_size))
  			return -EFAULT;
  		if (reg_val != cntx->vector.vlenb)
Reviewed-by on v1 1/1
#2 lore raw
In reply to Radim Krčmář
Quoted 7 lines · Radim Krčmář

The userspace load can put up to 2048 bits into an xlen bit stack buffer. We want only xlen bits, so check the size beforehand.

Fixes 2fa290372dfe ("RISC-V: KVM: add 'vlenb' Vector CSR")

---

Thanks,
Nutty

Quoted 16 lines
  arch/riscv/kvm/vcpu_vector.c | 2 ++
  1 file changed, 2 insertions(+)
arch/riscv/kvm/vcpu_vector.c +2 -0
index a5f88cb717f3..05f3cc2d8e31 100644
@@ -182,6 +182,8 @@ int kvm_riscv_vcpu_set_reg_vector(struct kvm_vcpu *vcpu,
  		struct kvm_cpu_context *cntx = &vcpu->arch.guest_context;
  		unsigned long reg_val;
 
+		if (reg_size != sizeof(reg_val))
+			return -EINVAL;
  		if (copy_from_user(&reg_val, uaddr, reg_size))
  			return -EFAULT;
  		if (reg_val != cntx->vector.vlenb)
Signature · 3 lines
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
maintainer
#3 lore raw
In reply to Radim Krčmář
Quoted 7 lines · Radim Krčmář

The userspace load can put up to 2048 bits into an xlen bit stack buffer. We want only xlen bits, so check the size beforehand.

Fixes 2fa290372dfe ("RISC-V: KVM: add 'vlenb' Vector CSR")

Queued this as a fix for Linux-6.17

Thanks,
Anup

Quoted 20 lines
---
 arch/riscv/kvm/vcpu_vector.c | 2 ++
 1 file changed, 2 insertions(+)
arch/riscv/kvm/vcpu_vector.c +2 -1
index a5f88cb717f3..05f3cc2d8e31 100644
@@ -182,6 +182,8 @@ int kvm_riscv_vcpu_set_reg_vector(struct kvm_vcpu *vcpu,
                struct kvm_cpu_context *cntx = &vcpu->arch.guest_context;
                unsigned long reg_val;
 
+               if (reg_size != sizeof(reg_val))
+                       return -EINVAL;
                if (copy_from_user(&reg_val, uaddr, reg_size))
                        return -EFAULT;
                if (reg_val != cntx->vector.vlenb)
--

2.50.0