KVM: arm64: Sanitise host vCPU fields copied in flush_hyp_vcpu()
Subsystems (3)
v3 cover KVM: arm64: Sanitise host vCPU fields copied in flush_hyp_vcpu()
flush_hyp_vcpu() copies the host vCPU context and vGIC state into the hyp's private vCPU on every run. This series sanitises two fields that it currently copies verbatim (host -> EL2): __hyp_running_vcpu is cleared in the guest context, and used_lrs is bounded by the number of implemented list registers.
Changes in v3:
- 2/2: replicate kvm_vgic_global_state.nr_lr into hyp_gicv3_nr_lr once at init (guarded by gicv3_cpuif), instead of reading ICH_VTR_EL2 on every entry behind a gicv3_cpuif gate. (Marc)
- v2: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- split into two patches, one per field, per review.
- v1: https://lore.kernel.org/all/aiFe-CXo-XVTFz1g@v4bel/
Hyunwoo Kim (2):
KVM: arm64: Clear __hyp_running_vcpu when flushing the pKVM hyp vCPU
KVM: arm64: Bound used_lrs when flushing the pKVM hyp vCPU
arch/arm64/include/asm/kvm_hyp.h | 1 + arch/arm64/kvm/arm.c | 2 ++ arch/arm64/kvm/hyp/nvhe/hyp-main.c | 12 ++++++++++++ 3 files changed, 15 insertions(+)
Signature · 1 line
2.43.0
PATCH v3 1/2 KVM: arm64: Clear __hyp_running_vcpu when flushing the pKVM hyp vCPU
flush_hyp_vcpu() copies the host vCPU context 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(). While this is generally the case, flush_hyp_vcpu() copies the context verbatim and does not enforce this, so a value provided by the host is dereferenced at EL2 (host -> EL2).
Fix by clearing __hyp_running_vcpu after the copy.
--- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 3 +++ 1 file changed, 3 insertions(+)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +3 -0
@@ -128,6 +128,9 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) hyp_vcpu->vcpu.arch.ctxt = host_vcpu->arch.ctxt; +/* __hyp_running_vcpu must be NULL in a guest context. */ + 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) &
Signature · 1 line
2.43.0
PATCH v3 2/2 KVM: arm64: Bound used_lrs when flushing the pKVM hyp vCPU
In reply to Hyunwoo Kimflush_hyp_vcpu() copies the host vGIC state into the hyp's private vCPU on every run. The vGIC list register save and restore use used_lrs as their loop bound and expect it to stay within the number of implemented list registers. While this is generally the case, flush_hyp_vcpu() copies vgic_v3 verbatim and does not enforce this, so a value provided by the host is used at EL2 to index vgic_lr[] and access ICH_LR<n>_EL2 (host -> EL2).
Fix by clamping used_lrs to the number of implemented list registers after the copy, as the trusted path already does in vgic_flush_lr_state(). The number of implemented list registers is constant after init, so it is replicated once from kvm_vgic_global_state.nr_lr into hyp_gicv3_nr_lr rather than read on every entry.
--- arch/arm64/include/asm/kvm_hyp.h | 1 + arch/arm64/kvm/arm.c | 2 ++ arch/arm64/kvm/hyp/nvhe/hyp-main.c | 9 +++++++++ 3 files changed, 12 insertions(+)
arch/arm64/include/asm/kvm_hyp.h +1 -0
@@ -157,5 +157,6 @@ extern unsigned long kvm_nvhe_sym(__icache_flags);extern unsigned int kvm_nvhe_sym (kvm_arm_vmid_bits );extern unsigned int kvm_nvhe_sym (kvm_host_sve_max_vl );extern unsigned long kvm_nvhe_sym (hyp_nr_cpus ); +extern unsigned int kvm_nvhe_sym (hyp_gicv3_nr_lr );#endif /* __ARM64_KVM_HYP_H__ */
arch/arm64/kvm/arm.c +2 -0
@@ -2426,6 +2426,8 @@ static int __init init_subsystems(void)switch (err) {case 0 : vgic_present = true; +if (static_branch_unlikely (&kvm_vgic_global_state.gicv3_cpuif)) +kvm_nvhe_sym (hyp_gicv3_nr_lr) = kvm_vgic_global_state.nr_lr;break ;case -ENODEV :case -ENXIO :
arch/arm64/kvm/hyp/nvhe/hyp-main.c +9 -0
@@ -24,6 +24,9 @@ DEFINE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params); +/* Number of implemented GICv3 LRs. Used by flush_hyp_vcpu(). */ +unsigned int hyp_gicv3_nr_lr; +void __kvm_hyp_host_forward_smc (struct kvm_cpu_context *host_ctxt);static void __hyp_sve_save_guest (struct kvm_vcpu *vcpu)
@@ -142,6 +145,12 @@ 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 used_lrs by the number of implemented list registers. */ + hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3.used_lrs = + min_t(unsigned int, + hyp_vcpu->vcpu.arch.vgic_cpu.vgic_v3.used_lrs, + hyp_gicv3_nr_lr); + hyp_vcpu->vcpu.arch.pid = host_vcpu->arch.pid; }
Signature · 1 line
2.43.0
Quoted 6 lines · Hyunwoo Kim
flush_hyp_vcpu()copies the host vCPU context and vGIC state into the hyp's private vCPU on every run. This series sanitises two fields that it currently copies verbatim (host -> EL2):__hyp_running_vcpuis cleared in the guest context, andused_lrsis bounded by the number of implemented list registers.
From my side I think you've addressed Marc's feedback. For the latest series:
Cheers,
/fuad
Quoted 23 lines
Changes in v3:
- 2/2: replicate kvm_vgic_global_state.nr_lr intohyp_gicv3_nr_lronce at init (guarded bygicv3_cpuif), instead of readingICH_VTR_EL2on every entry behind agicv3_cpuifgate. (Marc)
- v2: https://lore.kernel.org/all/[email protected]/Changes in v2:
- split into two patches, one per field, per review.
- v1: https://lore.kernel.org/all/aiFe-CXo-XVTFz1g@v4bel/Hyunwoo Kim (2):
KVM: arm64: Clear__hyp_running_vcpuwhen flushing the pKVM hyp vCPU
KVM: arm64: Boundused_lrswhen flushing the pKVM hyp vCPUarch/arm64/include/asm/kvm_hyp.h | 1 + arch/arm64/kvm/arm.c | 2 ++ arch/arm64/kvm/hyp/nvhe/hyp-main.c | 12 ++++++++++++ 3 files changed, 15 insertions(+)--
2.43.0