[PATCH 1/3] KVM: arm64: nv: Fix AT emulation for TCR2_EL1
Dan Carpenter
dan at pereq.eu
Fri Nov 27 07:31:38 PST 2026
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 at vger.kernel.org
Signed-off-by: Dan Carpenter <dan at pereq.eu>
---
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
More information about the linux-arm-kernel
mailing list