Arm provides an AT instruction that translates a virtual address the way the MMU would and returns the result. We emulate this instruction by setting up the state and calling the actual AT instruction. There is a bug in how the TCR2_EL1 permission bits are handled. 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 permission registers PIR_EL1, PIRE0_EL1, POR_EL1 and POR_EL0. If you have the right features and permissions then the return value from the AT instruction has bit that is 0 if the access is allowed or 1 if it would fault. The bug is that the current code assumes that if the VM doesn't have the feature then the host doesn't have it either, and isn't necessarily true. The host can have the features but disable them for a guest. The way the current code is written, if the VM doesn't have the features then KVM doesn't bother to load the TCR2_EL1 or the permission registers, but uses whatever was left there previously. The impact of this information leak is very small. It's hard to imagine how knowing someone else's permission bits is useful to an attacker. Claude suggests that potentially two guests could set up a secret back channel where they communicate by flipping their permissions on and off. :) Fix this by clearing the PIE and POE bits in effective_tcr2() when the VM doesn't have those features, and load the result into TCR2_EL1 whenever the CPU has TCR2, not only when the VM has it. When the AT runs on the guest hypervisor's own registers, in __kvm_at_s1e2() and __kvm_at_s1e01_fast(), use the new load_guest_tcr2() function to load the guest's TCR2_EL2 for the AT and restore the original value afterwards. If the VM doesn't have the PIE and POE features, those bits of TCR2_EL2 are RES0 because kvm_vcpu_apply_reg_masks() clears them. There is a closely related bug in __kvm_at_s1e01_fast(). Here the issue is that KVM is performing an AT command for a guest hypervisor but the POR_EL0 isn't loaded, so we use the VMM's. This bug is triggered when the guest hypervisor runs with HCR_EL2.{E2H,TGE} = {1,1}. Fix this by loading the guest's POR_EL0 before the AT and restoring the VMM's 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") Fixes: 846c993df982 ("KVM: arm64: Add POE save/restore for AT emulation fast-path") Cc: stable@vger.kernel.org Signed-off-by: Dan Carpenter --- v3: Re-write commit message Add a new fix for the guest's POR_EL0 when HCR_EL2.{E2H,TGE} = {1,1} and add a new Fixes tag for that. Btw, Marc the zeros in the load_guest_tcr2() don't come from the reset value, they're actually set in tcr2_el2_feat_map[] so that should re-assure you. :) Update that comment. v2: Restore the original TCR2 at the end as pointed out by Sashiko. Cleanup the code based on Marc's review. Clear the guest's TCR2 in effective_tcr2(). --- arch/arm64/kvm/at.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/arch/arm64/kvm/at.c b/arch/arm64/kvm/at.c index 30d8f062b2c2..893bf88f2f75 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,23 @@ static int handle_at_slow(struct kvm_vcpu *vcpu, u32 op, u64 vaddr, u64 *par) return 0; } +/* + * Load the guest's TCR2_EL2 and 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); + + /* + * TCR2 features are not always exposed to the guests. If they + * aren't exposed then the bits are RES0. + */ + 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 +1421,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, swap_por_el0 = false; bool fail, mmu_cs; + u64 orig_tcr2, orig_por_el0; u64 par; par = SYS_PAR_EL1_F; @@ -1437,8 +1463,9 @@ static u64 __kvm_at_s1e01_fast(struct kvm_vcpu *vcpu, u32 op, u64 vaddr) write_sysreg_el1(vcpu_read_sys_reg(vcpu, TTBR1_EL1), SYS_TTBR1); 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 (cpus_have_final_cap(ARM64_HAS_TCR2)) + write_sysreg_el1(effective_tcr2(vcpu, TR_EL10), SYS_TCR2); if (kvm_has_tcr2(vcpu->kvm)) { - write_sysreg_el1(vcpu_read_sys_reg(vcpu, TCR2_EL1), 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,17 @@ 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); + } + if (!mmu_cs && vcpu_has_nv(vcpu) && kvm_has_s1poe(vcpu->kvm)) { + swap_por_el0 = true; + orig_por_el0 = read_sysreg_s(SYS_POR_EL0); + write_sysreg_s(vcpu_read_sys_reg(vcpu, POR_EL0), SYS_POR_EL0); + } + /* Temporarily switch back to guest context */ write_sysreg_hcr(vcpu->arch.hcr_el2); isb(); @@ -1485,6 +1523,11 @@ 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); + if (swap_por_el0) + write_sysreg_s(orig_por_el0, SYS_POR_EL0); + write_sysreg_hcr(HCR_HOST_VHE_FLAGS); if (mmu_cs) @@ -1544,7 +1587,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 +1597,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 +1623,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 A VMM can offer FEAT_TCR2 to a guest but hide FEAT_S1PIE or FEAT_S1POE, for example to keep the VM migratable. But the complication is that the guest can still write to the PIE, POE and E0POE bits in TCR2_EL1, and when those bits are set the CPU uses the permission registers. So the guest runs with the permissions that another VM left in PIR_EL1, PIRE0_EL1 and POR_EL1, or with the VMM's POR_EL0. By checking which accesses fault, the guest can work out another VM's permission settings. This is a kind of small information leak. Currently, KVM doesn't save or load PIR_EL1, PIRE0_EL1, POR_EL1 and POR_EL0 for a guest where S1PIE or S1POE is hidden. After this patch, KVM switches these registers whenever the CPU has the feature, even for a guest that doesn't have it: PIR_EL1, PIRE0_EL1 and POR_EL1 when the vcpu is loaded and put, and POR_EL0, which the VMM uses, on every guest entry and exit. For a guest that doesn't have the feature, KVM loads 0 instead of the guest's value. A permission of 0 allows no access, so if the guest sets PIE, POE or E0POE anyway, the accesses all fault and the information leak is gone. Fixes: 58627b722ee2 ("KVM: arm64: Make PIR{,E0}_EL1 UNDEF if S1PIE is not advertised to the guest") Fixes: b86c9bea6349 ("KVM: arm64: Save/restore POE registers") Cc: stable@vger.kernel.org Signed-off-by: Dan Carpenter --- v3: Load no-access permissions instead of trapping TCR_EL1 and TCR2_EL1 writes, as Marc suggested. This also covers nested virtualization, so the follow-up patch for it is gone. --- arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h | 40 ++++++++++++++++++++++-------- arch/arm64/kvm/hyp/vhe/sysreg-sr.c | 15 +++++++---- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h b/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h index 4a008c8a696b..ca9c2f6ae5da 100644 --- a/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h +++ b/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h @@ -17,6 +17,7 @@ #include static inline bool ctxt_has_s1poe(struct kvm_cpu_context *ctxt); +static inline u64 ctxt_s1poe_val(struct kvm_cpu_context *ctxt, u64 val); static inline bool ctxt_has_accdata(struct kvm_cpu_context *ctxt); static inline struct kvm_vcpu *ctxt_to_vcpu(struct kvm_cpu_context *ctxt) @@ -60,7 +61,7 @@ static inline void __sysreg_save_common_state(struct kvm_cpu_context *ctxt) *ctxt_mdscr_el1(ctxt) = read_sysreg(mdscr_el1); // POR_EL0 can affect uaccess, so must be saved/restored early. - if (ctxt_has_s1poe(ctxt)) + if (system_supports_poe()) ctxt_sys_reg(ctxt, POR_EL0) = read_sysreg_s(SYS_POR_EL0); } @@ -117,6 +118,21 @@ static inline bool ctxt_has_s1poe(struct kvm_cpu_context *ctxt) return kvm_has_s1poe(kern_hyp_va(vcpu->kvm)); } +/* + * A guest can turn on TCR2_EL1.PIE, POE or E0POE even when the feature is + * hidden from it. Give it permissions of 0, which allow no access, instead + * of whatever another context left in the registers. + */ +static inline u64 ctxt_s1pie_val(struct kvm_cpu_context *ctxt, u64 val) +{ + return ctxt_is_guest(ctxt) && !ctxt_has_s1pie(ctxt) ? 0 : val; +} + +static inline u64 ctxt_s1poe_val(struct kvm_cpu_context *ctxt, u64 val) +{ + return ctxt_is_guest(ctxt) && !ctxt_has_s1poe(ctxt) ? 0 : val; +} + static inline bool ctxt_has_ras(struct kvm_cpu_context *ctxt) { struct kvm_vcpu *vcpu; @@ -161,12 +177,12 @@ static inline void __sysreg_save_el1_state(struct kvm_cpu_context *ctxt) if (ctxt_has_tcrx(ctxt)) { ctxt_sys_reg(ctxt, TCR2_EL1) = read_sysreg_el1(SYS_TCR2); - if (ctxt_has_s1pie(ctxt)) { + if (cpus_have_final_cap(ARM64_HAS_S1PIE)) { ctxt_sys_reg(ctxt, PIR_EL1) = read_sysreg_el1(SYS_PIR); ctxt_sys_reg(ctxt, PIRE0_EL1) = read_sysreg_el1(SYS_PIRE0); } - if (ctxt_has_s1poe(ctxt)) + if (system_supports_poe()) ctxt_sys_reg(ctxt, POR_EL1) = read_sysreg_el1(SYS_POR); } ctxt_sys_reg(ctxt, ESR_EL1) = read_sysreg_el1(SYS_ESR); @@ -222,8 +238,9 @@ static inline void __sysreg_restore_common_state(struct kvm_cpu_context *ctxt) write_sysreg(*ctxt_mdscr_el1(ctxt), mdscr_el1); // POR_EL0 can affect uaccess, so must be saved/restored early. - if (ctxt_has_s1poe(ctxt)) - write_sysreg_s(ctxt_sys_reg(ctxt, POR_EL0), SYS_POR_EL0); + if (system_supports_poe()) + write_sysreg_s(ctxt_s1poe_val(ctxt, ctxt_sys_reg(ctxt, POR_EL0)), + SYS_POR_EL0); } static inline void __sysreg_restore_user_state(struct kvm_cpu_context *ctxt) @@ -263,13 +280,16 @@ static inline void __sysreg_restore_el1_state(struct kvm_cpu_context *ctxt, if (ctxt_has_tcrx(ctxt)) { write_sysreg_el1(ctxt_sys_reg(ctxt, TCR2_EL1), SYS_TCR2); - if (ctxt_has_s1pie(ctxt)) { - write_sysreg_el1(ctxt_sys_reg(ctxt, PIR_EL1), SYS_PIR); - write_sysreg_el1(ctxt_sys_reg(ctxt, PIRE0_EL1), SYS_PIRE0); + if (cpus_have_final_cap(ARM64_HAS_S1PIE)) { + write_sysreg_el1(ctxt_s1pie_val(ctxt, ctxt_sys_reg(ctxt, PIR_EL1)), + SYS_PIR); + write_sysreg_el1(ctxt_s1pie_val(ctxt, ctxt_sys_reg(ctxt, PIRE0_EL1)), + SYS_PIRE0); } - if (ctxt_has_s1poe(ctxt)) - write_sysreg_el1(ctxt_sys_reg(ctxt, POR_EL1), SYS_POR); + if (system_supports_poe()) + write_sysreg_el1(ctxt_s1poe_val(ctxt, ctxt_sys_reg(ctxt, POR_EL1)), + SYS_POR); } write_sysreg_el1(ctxt_sys_reg(ctxt, ESR_EL1), SYS_ESR); write_sysreg_el1(ctxt_sys_reg(ctxt, AFSR0_EL1), SYS_AFSR0); diff --git a/arch/arm64/kvm/hyp/vhe/sysreg-sr.c b/arch/arm64/kvm/hyp/vhe/sysreg-sr.c index 0c4ef1ce32ae..b06d85cb085a 100644 --- a/arch/arm64/kvm/hyp/vhe/sysreg-sr.c +++ b/arch/arm64/kvm/hyp/vhe/sysreg-sr.c @@ -133,15 +133,20 @@ static void __sysreg_restore_vel2_state(struct kvm_vcpu *vcpu) } if (ctxt_has_tcrx(&vcpu->arch.ctxt)) { + struct kvm_cpu_context *ctxt = &vcpu->arch.ctxt; + write_sysreg_el1(__vcpu_sys_reg(vcpu, TCR2_EL2), SYS_TCR2); - if (ctxt_has_s1pie(&vcpu->arch.ctxt)) { - write_sysreg_el1(__vcpu_sys_reg(vcpu, PIR_EL2), SYS_PIR); - write_sysreg_el1(__vcpu_sys_reg(vcpu, PIRE0_EL2), SYS_PIRE0); + if (cpus_have_final_cap(ARM64_HAS_S1PIE)) { + write_sysreg_el1(ctxt_s1pie_val(ctxt, __vcpu_sys_reg(vcpu, PIR_EL2)), + SYS_PIR); + write_sysreg_el1(ctxt_s1pie_val(ctxt, __vcpu_sys_reg(vcpu, PIRE0_EL2)), + SYS_PIRE0); } - if (ctxt_has_s1poe(&vcpu->arch.ctxt)) - write_sysreg_el1(__vcpu_sys_reg(vcpu, POR_EL2), SYS_POR); + if (system_supports_poe()) + write_sysreg_el1(ctxt_s1poe_val(ctxt, __vcpu_sys_reg(vcpu, POR_EL2)), + SYS_POR); } write_sysreg_el1(__vcpu_sys_reg(vcpu, ESR_EL2), SYS_ESR); -- 2.53.0 There is a missing kvm_arm_vmid_update() before the __load_stage2() in __kvm_at_s1e01_fast(). The bug affects nested virtualization on arm64. In KVM a "stage 2" refers to the VM's page table mappings. The __kvm_at_s1e01_fast() function emulates the AT instruction, which translates a virtual address the way the MMU would and returns the result. Each stage 2 has a VMID, which the hardware uses to tag TLB and walk-cache entries, so that one VM never uses another's entries. There are only 2^8 or 2^16 VMIDs, which is quite limited, so vmid.c hands them out by generation. When they run out, a rollover starts a new generation and flushes the TLBs: the VMIDs active on a CPU at that moment are kept, and every other VMID can be given to another VM. The kvm_arm_vmid_update() function checks the VMID's generation, takes a new VMID if it is from an old generation, and marks it as active on this CPU. Without this, the shadow MMU's VMID could be an old one that now belongs to another VM, or a rollover on another CPU could hand it to another VM while we are using it. Either way the AT fills TLB entries tagged with another VM's VMID. To fix this, call kvm_arm_vmid_update() to update the VMID before loading the shadow stage 2. That marks it as active on the CPU. Then at the end of the function, undo that. Restore the original VMID as active while checking for and handling rollover. Fixes: 477e89cabb14 ("KVM: arm64: nv: Add basic emulation of AT S1E{0,1}{R,W}") Cc: stable@vger.kernel.org Signed-off-by: Dan Carpenter --- v2: Unchanged v3: Unchanged --- arch/arm64/kvm/at.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/at.c b/arch/arm64/kvm/at.c index 893bf88f2f75..4a1767704d22 100644 --- a/arch/arm64/kvm/at.c +++ b/arch/arm64/kvm/at.c @@ -1476,6 +1476,7 @@ static u64 __kvm_at_s1e01_fast(struct kvm_vcpu *vcpu, u32 op, u64 vaddr) } } write_sysreg_el1(vcpu_read_sys_reg(vcpu, SCTLR_EL1), SYS_SCTLR); + kvm_arm_vmid_update(&mmu->vmid); __load_stage2(mmu); skip_mmu_switch: @@ -1530,8 +1531,11 @@ static u64 __kvm_at_s1e01_fast(struct kvm_vcpu *vcpu, u32 op, u64 vaddr) write_sysreg_hcr(HCR_HOST_VHE_FLAGS); - if (mmu_cs) + if (mmu_cs) { + kvm_arm_vmid_update(&vcpu->arch.hw_mmu->vmid); + config.vttbr = kvm_get_vttbr(vcpu->arch.hw_mmu); __mmu_config_restore(&config); + } return par; } -- 2.53.0