* [PATCH] KVM: arm64: Sanitise host vCPU fields in flush_hyp_vcpu()
@ 2026-06-04 11:18 Hyunwoo Kim
2026-06-04 13:01 ` Fuad Tabba
0 siblings, 1 reply; 3+ messages in thread
From: Hyunwoo Kim @ 2026-06-04 11:18 UTC (permalink / raw)
To: maz, oupton, joey.gouly, seiden, suzuki.poulose, yuzenghui,
catalin.marinas, will
Cc: linux-arm-kernel, kvmarm, imv4bel
flush_hyp_vcpu() copies the host vCPU context and vGIC state into the
hyp's private vCPU on every run. ctxt_to_vcpu() expects a guest context
to have a NULL __hyp_running_vcpu, which is only ever set on the host
context, so that it resolves the vCPU via container_of(). The vGIC list
register save and restore expect used_lrs to stay within the number of
implemented list registers. While this is generally the case,
flush_hyp_vcpu() copies both fields verbatim from the host vCPU and
enforces neither expectation.
Fix by clearing __hyp_running_vcpu and clamping used_lrs after the copy.
Fixes: be66e67f1750 ("KVM: arm64: Use the pKVM hyp vCPU structure in handle___kvm_vcpu_run()")
Signed-off-by: Hyunwoo Kim <imv4bel@gmail.com>
---
arch/arm64/kvm/hyp/nvhe/hyp-main.c | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
index 06db299c37a89..ef9318ff0c25e 100644
--- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c
+++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
@@ -7,6 +7,7 @@
#include <hyp/adjust_pc.h>
#include <hyp/switch.h>
+#include <asm/arch_gicv3.h>
#include <asm/pgtable-types.h>
#include <asm/kvm_asm.h>
#include <asm/kvm_emulate.h>
@@ -128,6 +129,9 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
hyp_vcpu->vcpu.arch.ctxt = host_vcpu->arch.ctxt;
+ /* A guest context must keep a NULL __hyp_running_vcpu. */
+ hyp_vcpu->vcpu.arch.ctxt.__hyp_running_vcpu = NULL;
+
hyp_vcpu->vcpu.arch.mdcr_el2 = host_vcpu->arch.mdcr_el2;
hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWI | HCR_TWE);
hyp_vcpu->vcpu.arch.hcr_el2 |= READ_ONCE(host_vcpu->arch.hcr_el2) &
@@ -139,6 +143,13 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3 = host_vcpu->arch.vgic_cpu.vgic_v3;
+ /* Bound the host-provided used_lrs by the implemented list registers. */
+ if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
+ hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3.used_lrs =
+ min_t(unsigned int,
+ hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3.used_lrs,
+ (read_gicreg(ICH_VTR_EL2) & 0xf) + 1);
+
hyp_vcpu->vcpu.arch.pid = host_vcpu->arch.pid;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] KVM: arm64: Sanitise host vCPU fields in flush_hyp_vcpu()
2026-06-04 11:18 [PATCH] KVM: arm64: Sanitise host vCPU fields in flush_hyp_vcpu() Hyunwoo Kim
@ 2026-06-04 13:01 ` Fuad Tabba
2026-06-04 13:35 ` Hyunwoo Kim
0 siblings, 1 reply; 3+ messages in thread
From: Fuad Tabba @ 2026-06-04 13:01 UTC (permalink / raw)
To: Hyunwoo Kim
Cc: maz, oupton, joey.gouly, seiden, suzuki.poulose, yuzenghui,
catalin.marinas, will, linux-arm-kernel, kvmarm
Hi Hyunwoo,
On Thu, 4 Jun 2026 at 12:18, Hyunwoo Kim <imv4bel@gmail.com> wrote:
>
> flush_hyp_vcpu() copies the host vCPU context and vGIC state into the
> hyp's private vCPU on every run. ctxt_to_vcpu() expects a guest context
> to have a NULL __hyp_running_vcpu, which is only ever set on the host
> context, so that it resolves the vCPU via container_of(). The vGIC list
> register save and restore expect used_lrs to stay within the number of
> implemented list registers. While this is generally the case,
> flush_hyp_vcpu() copies both fields verbatim from the host vCPU and
> enforces neither expectation.
>
> Fix by clearing __hyp_running_vcpu and clamping used_lrs after the copy.
Nice catch, both fixes are correct.
Please split this into two patches, one per field. They are independent
bugs that just happen to share a Fixes: tag and the function. Both are
host -> EL2, so worth stating that in the commit messages.
Otherwise this looks right to me.
Cheers,
/fuad
>
> Fixes: be66e67f1750 ("KVM: arm64: Use the pKVM hyp vCPU structure in handle___kvm_vcpu_run()")
> Signed-off-by: Hyunwoo Kim <imv4bel@gmail.com>
> ---
> arch/arm64/kvm/hyp/nvhe/hyp-main.c | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
> index 06db299c37a89..ef9318ff0c25e 100644
> --- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c
> +++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
> @@ -7,6 +7,7 @@
> #include <hyp/adjust_pc.h>
> #include <hyp/switch.h>
>
> +#include <asm/arch_gicv3.h>
> #include <asm/pgtable-types.h>
> #include <asm/kvm_asm.h>
> #include <asm/kvm_emulate.h>
> @@ -128,6 +129,9 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
>
> hyp_vcpu->vcpu.arch.ctxt = host_vcpu->arch.ctxt;
>
> + /* A guest context must keep a NULL __hyp_running_vcpu. */
> + hyp_vcpu->vcpu.arch.ctxt.__hyp_running_vcpu = NULL;
> +
> hyp_vcpu->vcpu.arch.mdcr_el2 = host_vcpu->arch.mdcr_el2;
> hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWI | HCR_TWE);
> hyp_vcpu->vcpu.arch.hcr_el2 |= READ_ONCE(host_vcpu->arch.hcr_el2) &
> @@ -139,6 +143,13 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
>
> hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3 = host_vcpu->arch.vgic_cpu.vgic_v3;
>
> + /* Bound the host-provided used_lrs by the implemented list registers. */
> + if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
> + hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3.used_lrs =
> + min_t(unsigned int,
> + hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3.used_lrs,
> + (read_gicreg(ICH_VTR_EL2) & 0xf) + 1);
> +
> hyp_vcpu->vcpu.arch.pid = host_vcpu->arch.pid;
> }
>
> --
> 2.43.0
>
>
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] KVM: arm64: Sanitise host vCPU fields in flush_hyp_vcpu()
2026-06-04 13:01 ` Fuad Tabba
@ 2026-06-04 13:35 ` Hyunwoo Kim
0 siblings, 0 replies; 3+ messages in thread
From: Hyunwoo Kim @ 2026-06-04 13:35 UTC (permalink / raw)
To: Fuad Tabba
Cc: maz, oupton, joey.gouly, seiden, suzuki.poulose, yuzenghui,
catalin.marinas, will, linux-arm-kernel, kvmarm, imv4bel
On Thu, Jun 04, 2026 at 02:01:17PM +0100, Fuad Tabba wrote:
> Hi Hyunwoo,
>
> On Thu, 4 Jun 2026 at 12:18, Hyunwoo Kim <imv4bel@gmail.com> wrote:
> >
> > flush_hyp_vcpu() copies the host vCPU context and vGIC state into the
> > hyp's private vCPU on every run. ctxt_to_vcpu() expects a guest context
> > to have a NULL __hyp_running_vcpu, which is only ever set on the host
> > context, so that it resolves the vCPU via container_of(). The vGIC list
> > register save and restore expect used_lrs to stay within the number of
> > implemented list registers. While this is generally the case,
> > flush_hyp_vcpu() copies both fields verbatim from the host vCPU and
> > enforces neither expectation.
> >
> > Fix by clearing __hyp_running_vcpu and clamping used_lrs after the copy.
>
> Nice catch, both fixes are correct.
Thanks for the review.
>
> Please split this into two patches, one per field. They are independent
> bugs that just happen to share a Fixes: tag and the function. Both are
> host -> EL2, so worth stating that in the commit messages.
I'll split this into two patches and resend it as a series.
>
> Otherwise this looks right to me.
>
> Cheers,
> /fuad
>
>
> >
> > Fixes: be66e67f1750 ("KVM: arm64: Use the pKVM hyp vCPU structure in handle___kvm_vcpu_run()")
> > Signed-off-by: Hyunwoo Kim <imv4bel@gmail.com>
> > ---
> > arch/arm64/kvm/hyp/nvhe/hyp-main.c | 11 +++++++++++
> > 1 file changed, 11 insertions(+)
> >
> > diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
> > index 06db299c37a89..ef9318ff0c25e 100644
> > --- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c
> > +++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
> > @@ -7,6 +7,7 @@
> > #include <hyp/adjust_pc.h>
> > #include <hyp/switch.h>
> >
> > +#include <asm/arch_gicv3.h>
> > #include <asm/pgtable-types.h>
> > #include <asm/kvm_asm.h>
> > #include <asm/kvm_emulate.h>
> > @@ -128,6 +129,9 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
> >
> > hyp_vcpu->vcpu.arch.ctxt = host_vcpu->arch.ctxt;
> >
> > + /* A guest context must keep a NULL __hyp_running_vcpu. */
> > + hyp_vcpu->vcpu.arch.ctxt.__hyp_running_vcpu = NULL;
> > +
> > hyp_vcpu->vcpu.arch.mdcr_el2 = host_vcpu->arch.mdcr_el2;
> > hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWI | HCR_TWE);
> > hyp_vcpu->vcpu.arch.hcr_el2 |= READ_ONCE(host_vcpu->arch.hcr_el2) &
> > @@ -139,6 +143,13 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
> >
> > hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3 = host_vcpu->arch.vgic_cpu.vgic_v3;
> >
> > + /* Bound the host-provided used_lrs by the implemented list registers. */
> > + if (static_branch_unlikely(&kvm_vgic_global_state.gicv3_cpuif))
> > + hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3.used_lrs =
> > + min_t(unsigned int,
> > + hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3.used_lrs,
> > + (read_gicreg(ICH_VTR_EL2) & 0xf) + 1);
> > +
> > hyp_vcpu->vcpu.arch.pid = host_vcpu->arch.pid;
> > }
> >
> > --
> > 2.43.0
> >
> >
Best regards,
Hyunwoo Kim
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-06-04 13:35 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-04 11:18 [PATCH] KVM: arm64: Sanitise host vCPU fields in flush_hyp_vcpu() Hyunwoo Kim
2026-06-04 13:01 ` Fuad Tabba
2026-06-04 13:35 ` Hyunwoo Kim
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox