Arm provides an AT instruction that translates a virtual address the way the MMU would and returns the result. There is a bug in the way this instruction is emulated for nested virtualization because of how TCR2_EL1 is handled. The bug is fairly minor. The TCR2_EL1 register has bits which turn on permission indirection (PIE) and permission overlays (POE). When these bits are set, the AT instruction reads the permissions stored in PIR_EL1 and POR_EL1. If you have the right features and permissions then in the return value from the AT instruction we set one bit to either 1 or 0 depending on whether the access would be allowed or whether it would fault. However, the bug is that just because the host has TCR2 enabled, it doesn't mean the VM has it. In the current code KVM doesn't load these registers for a VM which doesn't have the features, instead the AT runs with the host's HCRX_EL2, which has TCR2En set. So the AT uses the TCR2_EL1, PIR_EL1 and POR_EL1 values another VM left on the CPU, or possibly the VMM's POR_EL0. Likewise, for a VM which has TCR2 but not PIE or POE, the L2's TCR2_EL1 is loaded unmasked. The impact of this bug is that we leak a single bit of data to the L1, but in real life it's hard to imagine how it is useful to an attacker. Fix this by clearing the PIE and POE bits in effective_tcr2() when the VM doesn't have those features, and use it for the L2's TCR2_EL1. When the AT runs on the guest hypervisor's own registers, in __kvm_at_s1e2() and in __kvm_at_s1e01_fast(), load the guest's TCR2_EL2 for the AT and restore the live value afterwards. Fixes: 477e89cabb14 ("KVM: arm64: nv: Add basic emulation of AT S1E{0,1}{R,W}") Fixes: 23e7a34c8397 ("KVM: arm64: Add AT fast-path support for S1PIE") Cc: stable@vger.kernel.org Signed-off-by: Dan Carpenter --- v2: Restore original TCR2 values at the end of the AT command Cleanup the code based on Marc's review. Add some comments --- arch/arm64/kvm/at.c | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/arch/arm64/kvm/at.c b/arch/arm64/kvm/at.c index 30d8f062b2c2..9dfabd0586e2 100644 --- a/arch/arm64/kvm/at.c +++ b/arch/arm64/kvm/at.c @@ -91,11 +91,18 @@ static enum trans_regime compute_translation_regime(struct kvm_vcpu *vcpu, u32 o static u64 effective_tcr2(struct kvm_vcpu *vcpu, enum trans_regime regime) { if (regime == TR_EL10) { + u64 val; + if (vcpu_has_nv(vcpu) && !(__vcpu_sys_reg(vcpu, HCRX_EL2) & HCRX_EL2_TCR2En)) return 0; - return vcpu_read_sys_reg(vcpu, TCR2_EL1); + val = vcpu_read_sys_reg(vcpu, TCR2_EL1); + if (!kvm_has_s1pie(vcpu->kvm)) + val &= ~TCR2_EL1_PIE; + if (!kvm_has_s1poe(vcpu->kvm)) + val &= ~(TCR2_EL1_POE | TCR2_EL1_E0POE); + return val; } return vcpu_read_sys_reg(vcpu, TCR2_EL2); @@ -1384,6 +1391,24 @@ static int handle_at_slow(struct kvm_vcpu *vcpu, u32 op, u64 vaddr, u64 *par) return 0; } +/* + * TCR2 features are always exposed to the guests. Clear the ones + * which are not exposed on this VM. Return the original TCR2 so we + * can restore it after running the AT command. + */ +static u64 load_guest_tcr2(struct kvm_vcpu *vcpu) +{ + u64 orig = read_sysreg_el1(SYS_TCR2); + + /* + * This relies on the reset value of TCR2_EL2 being 0, and not + * being modified when FEAT_TCR2 is not exposed to the guest. + */ + write_sysreg_el1(vcpu_read_sys_reg(vcpu, TCR2_EL2), SYS_TCR2); + + return orig; +} + /* * Return the PAR_EL1 value as the result of a valid translation. * @@ -1397,7 +1422,9 @@ static u64 __kvm_at_s1e01_fast(struct kvm_vcpu *vcpu, u32 op, u64 vaddr) { struct mmu_config config; struct kvm_s2_mmu *mmu; + bool swap_tcr2 = false; bool fail, mmu_cs; + u64 orig_tcr2; u64 par; par = SYS_PAR_EL1_F; @@ -1438,7 +1465,7 @@ static u64 __kvm_at_s1e01_fast(struct kvm_vcpu *vcpu, u32 op, u64 vaddr) write_sysreg_el1(vcpu_read_sys_reg(vcpu, TCR_EL1), SYS_TCR); write_sysreg_el1(vcpu_read_sys_reg(vcpu, MAIR_EL1), SYS_MAIR); if (kvm_has_tcr2(vcpu->kvm)) { - write_sysreg_el1(vcpu_read_sys_reg(vcpu, TCR2_EL1), SYS_TCR2); + write_sysreg_el1(effective_tcr2(vcpu, TR_EL10), SYS_TCR2); if (kvm_has_s1pie(vcpu->kvm)) { write_sysreg_el1(vcpu_read_sys_reg(vcpu, PIR_EL1), SYS_PIR); write_sysreg_el1(vcpu_read_sys_reg(vcpu, PIRE0_EL1), SYS_PIRE0); @@ -1452,6 +1479,12 @@ static u64 __kvm_at_s1e01_fast(struct kvm_vcpu *vcpu, u32 op, u64 vaddr) __load_stage2(mmu); skip_mmu_switch: + if (!mmu_cs && vcpu_has_nv(vcpu) && + cpus_have_final_cap(ARM64_HAS_TCR2)) { + swap_tcr2 = true; + orig_tcr2 = load_guest_tcr2(vcpu); + } + /* Temporarily switch back to guest context */ write_sysreg_hcr(vcpu->arch.hcr_el2); isb(); @@ -1485,6 +1518,9 @@ static u64 __kvm_at_s1e01_fast(struct kvm_vcpu *vcpu, u32 op, u64 vaddr) if (!fail) par = read_sysreg_par(); + if (swap_tcr2) + write_sysreg_el1(orig_tcr2, SYS_TCR2); + write_sysreg_hcr(HCR_HOST_VHE_FLAGS); if (mmu_cs) @@ -1544,7 +1580,7 @@ int __kvm_at_s1e2(struct kvm_vcpu *vcpu, u32 op, u64 vaddr) * switching context behind everybody's back, disable interrupts... */ scoped_guard(write_lock_irqsave, &vcpu->kvm->mmu_lock) { - u64 val, hcr; + u64 val, hcr, orig_tcr2; bool fail; val = hcr = read_sysreg(hcr_el2); @@ -1554,6 +1590,9 @@ int __kvm_at_s1e2(struct kvm_vcpu *vcpu, u32 op, u64 vaddr) if (!vcpu_el2_e2h_is_set(vcpu)) val |= HCR_EL2_NV | HCR_EL2_NV1; + if (cpus_have_final_cap(ARM64_HAS_TCR2)) + orig_tcr2 = load_guest_tcr2(vcpu); + write_sysreg_hcr(val); isb(); @@ -1577,6 +1616,9 @@ int __kvm_at_s1e2(struct kvm_vcpu *vcpu, u32 op, u64 vaddr) if (!fail) par = read_sysreg_par(); + if (cpus_have_final_cap(ARM64_HAS_TCR2)) + write_sysreg_el1(orig_tcr2, SYS_TCR2); + write_sysreg_hcr(hcr); isb(); } -- 2.53.0