* [PATCH v2] RISC-V: KVM: Fix shift-out-of-bounds in make_xfence_request()
@ 2026-04-03 23:20 Jiakai Xu
2026-04-04 18:30 ` Andrew Jones
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Jiakai Xu @ 2026-04-03 23:20 UTC (permalink / raw)
To: kvm-riscv, kvm, linux-kernel, linux-riscv
Cc: Albert Ou, Alexandre Ghiti, Anup Patel, Atish Patra,
Palmer Dabbelt, Paul Walmsley, Jiakai Xu, Jiakai Xu
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@gmail.com>
Signed-off-by: Jiakai Xu <xujiakai2025@iscas.ac.cn>
---
V1 -> V2:
- Dropped 'idx' variable and compared vcpu_id against hbase directly,
as suggested by Andrew Jones.
---
arch/riscv/kvm/tlb.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/arch/riscv/kvm/tlb.c b/arch/riscv/kvm/tlb.c
index ff1aeac4eb8eb..439c20c2775ab 100644
--- a/arch/riscv/kvm/tlb.c
+++ b/arch/riscv/kvm/tlb.c
@@ -338,7 +338,8 @@ static void make_xfence_request(struct kvm *kvm,
bitmap_zero(vcpu_mask, KVM_MAX_VCPUS);
kvm_for_each_vcpu(i, vcpu, kvm) {
if (hbase != -1UL) {
- if (vcpu->vcpu_id < hbase)
+ if (vcpu->vcpu_id < hbase ||
+ vcpu->vcpu_id >= hbase + BITS_PER_LONG)
continue;
if (!(hmask & (1UL << (vcpu->vcpu_id - hbase))))
continue;
--
2.34.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2] RISC-V: KVM: Fix shift-out-of-bounds in make_xfence_request()
2026-04-03 23:20 [PATCH v2] RISC-V: KVM: Fix shift-out-of-bounds in make_xfence_request() Jiakai Xu
@ 2026-04-04 18:30 ` Andrew Jones
2026-04-06 4:25 ` Anup Patel
2026-04-30 3:25 ` patchwork-bot+linux-riscv
2 siblings, 0 replies; 4+ messages in thread
From: Andrew Jones @ 2026-04-04 18:30 UTC (permalink / raw)
To: Jiakai Xu
Cc: kvm-riscv, kvm, linux-kernel, linux-riscv, Albert Ou,
Alexandre Ghiti, Anup Patel, Atish Patra, Palmer Dabbelt,
Paul Walmsley, Jiakai Xu
On Fri, Apr 03, 2026 at 11:20:11PM +0000, Jiakai Xu wrote:
> 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@gmail.com>
> Signed-off-by: Jiakai Xu <xujiakai2025@iscas.ac.cn>
> ---
> V1 -> V2:
> - Dropped 'idx' variable and compared vcpu_id against hbase directly,
> as suggested by Andrew Jones.
> ---
> arch/riscv/kvm/tlb.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/arch/riscv/kvm/tlb.c b/arch/riscv/kvm/tlb.c
> index ff1aeac4eb8eb..439c20c2775ab 100644
> --- a/arch/riscv/kvm/tlb.c
> +++ b/arch/riscv/kvm/tlb.c
> @@ -338,7 +338,8 @@ static void make_xfence_request(struct kvm *kvm,
> bitmap_zero(vcpu_mask, KVM_MAX_VCPUS);
> kvm_for_each_vcpu(i, vcpu, kvm) {
> if (hbase != -1UL) {
> - if (vcpu->vcpu_id < hbase)
> + if (vcpu->vcpu_id < hbase ||
> + vcpu->vcpu_id >= hbase + BITS_PER_LONG)
> continue;
> if (!(hmask & (1UL << (vcpu->vcpu_id - hbase))))
> continue;
> --
> 2.34.1
>
Reviewed-by: Andrew Jones <andrew.jones@oss.qualcomm.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] RISC-V: KVM: Fix shift-out-of-bounds in make_xfence_request()
2026-04-03 23:20 [PATCH v2] RISC-V: KVM: Fix shift-out-of-bounds in make_xfence_request() Jiakai Xu
2026-04-04 18:30 ` Andrew Jones
@ 2026-04-06 4:25 ` Anup Patel
2026-04-30 3:25 ` patchwork-bot+linux-riscv
2 siblings, 0 replies; 4+ messages in thread
From: Anup Patel @ 2026-04-06 4:25 UTC (permalink / raw)
To: Jiakai Xu
Cc: kvm-riscv, kvm, linux-kernel, linux-riscv, Albert Ou,
Alexandre Ghiti, Atish Patra, Palmer Dabbelt, Paul Walmsley,
Jiakai Xu
On Sat, Apr 4, 2026 at 4:50 AM Jiakai Xu <xujiakai2025@iscas.ac.cn> wrote:
>
> 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@gmail.com>
> Signed-off-by: Jiakai Xu <xujiakai2025@iscas.ac.cn>
Queued this patch for Linux-7.1
Thanks,
Anup
> ---
> V1 -> V2:
> - Dropped 'idx' variable and compared vcpu_id against hbase directly,
> as suggested by Andrew Jones.
> ---
> arch/riscv/kvm/tlb.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/arch/riscv/kvm/tlb.c b/arch/riscv/kvm/tlb.c
> index ff1aeac4eb8eb..439c20c2775ab 100644
> --- a/arch/riscv/kvm/tlb.c
> +++ b/arch/riscv/kvm/tlb.c
> @@ -338,7 +338,8 @@ static void make_xfence_request(struct kvm *kvm,
> bitmap_zero(vcpu_mask, KVM_MAX_VCPUS);
> kvm_for_each_vcpu(i, vcpu, kvm) {
> if (hbase != -1UL) {
> - if (vcpu->vcpu_id < hbase)
> + if (vcpu->vcpu_id < hbase ||
> + vcpu->vcpu_id >= hbase + BITS_PER_LONG)
> continue;
> if (!(hmask & (1UL << (vcpu->vcpu_id - hbase))))
> continue;
> --
> 2.34.1
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] RISC-V: KVM: Fix shift-out-of-bounds in make_xfence_request()
2026-04-03 23:20 [PATCH v2] RISC-V: KVM: Fix shift-out-of-bounds in make_xfence_request() Jiakai Xu
2026-04-04 18:30 ` Andrew Jones
2026-04-06 4:25 ` Anup Patel
@ 2026-04-30 3:25 ` patchwork-bot+linux-riscv
2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+linux-riscv @ 2026-04-30 3:25 UTC (permalink / raw)
To: Jiakai Xu
Cc: linux-riscv, kvm-riscv, kvm, linux-kernel, aou, alex, anup,
atish.patra, palmer, pjw, jiakaiPeanut
Hello:
This patch was applied to riscv/linux.git (fixes)
by Anup Patel <anup@brainfault.org>:
On Fri, 3 Apr 2026 23:20:11 +0000 you wrote:
> 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.
>
> [...]
Here is the summary with links:
- [v2] RISC-V: KVM: Fix shift-out-of-bounds in make_xfence_request()
https://git.kernel.org/riscv/c/ddbf9c76c402
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-04-30 3:25 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-03 23:20 [PATCH v2] RISC-V: KVM: Fix shift-out-of-bounds in make_xfence_request() Jiakai Xu
2026-04-04 18:30 ` Andrew Jones
2026-04-06 4:25 ` Anup Patel
2026-04-30 3:25 ` patchwork-bot+linux-riscv
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox