[PATCH v2 3/4] KVM: arm64: nv: Clear hidden TCR2 bits for nested guests
Dan Carpenter
dan at pereq.eu
Thu Nov 19 05:25:36 PST 2026
The previous patch added traps for TCR2_EL1 writes on VMs that have
FEAT_TCR2 but not FEAT_S1PIE or FEAT_S1POE. This patch extends that
fix to cover nested virtualization.
The bug is that a KVM can hide the PIE and POE bits in TCR2_EL1 but a VM
can still write to them. It obviously makes no sense to hide a register
from the VM but still allow writes. The implication of this bug is a
small information leak because a VM can determine the permission bits of
another VM's page tables.
The previous patch trapped writes to TCR2_EL1 but nested virtualization
is more complicated because we have to deal with TCR2_EL2 as well. A
trapped TCR2_EL1 write has to go to either TCR2_EL2 or TCR2_EL1 depending
on who made it. With nested virtualization the hidden bits can reach the
hardware in three ways:
- A guest hypervisor at vEL2 writes its TCR2_EL2.
- An L2 writes its own TCR2_EL1.
- An L1 writes its L2's TCR2_EL1 in the VNCR page, which KVM loads as is.
To fix this, remove the vcpu_has_nv() check in __compute_hfgwtr() so that
these writes are trapped too. When the guest hypervisor is at vEL2,
update TCR2_EL2 and TCR_EL2 instead of L2's registers. Also clear the
hidden bits in __sysreg_restore_el1_state() when it loads a guest's
TCR2_EL1.
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 at vger.kernel.org
Signed-off-by: Dan Carpenter <dan at pereq.eu>
---
v2: This is a new patch
---
arch/arm64/kvm/config.c | 2 +-
arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h | 10 +++++++++-
arch/arm64/kvm/sys_regs.c | 19 ++++++++++++++++++-
3 files changed, 28 insertions(+), 3 deletions(-)
diff --git a/arch/arm64/kvm/config.c b/arch/arm64/kvm/config.c
index f756f7c4fdf8..8bde0a5eafb6 100644
--- a/arch/arm64/kvm/config.c
+++ b/arch/arm64/kvm/config.c
@@ -1898,7 +1898,7 @@ static void __compute_hfgwtr(struct kvm_vcpu *vcpu)
*vcpu_fgt(vcpu, HFGWTR_EL2) |= HFGWTR_EL2_TCR_EL1;
/* The CPU acts on hidden TCR2_EL1 bits, so trap writes to clear them */
- if (!vcpu_has_nv(vcpu) && kvm_has_tcr2(vcpu->kvm) &&
+ if (kvm_has_tcr2(vcpu->kvm) &&
(!kvm_has_s1pie(vcpu->kvm) || !kvm_has_s1poe(vcpu->kvm)))
*vcpu_fgt(vcpu, HFGWTR_EL2) |= HFGWTR_EL2_TCR_EL1;
}
diff --git a/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h b/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h
index 4a008c8a696b..d6e8bab14ed3 100644
--- a/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h
+++ b/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h
@@ -261,7 +261,15 @@ static inline void __sysreg_restore_el1_state(struct kvm_cpu_context *ctxt,
write_sysreg_el1(ctxt_sys_reg(ctxt, TTBR0_EL1), SYS_TTBR0);
write_sysreg_el1(ctxt_sys_reg(ctxt, TTBR1_EL1), SYS_TTBR1);
if (ctxt_has_tcrx(ctxt)) {
- write_sysreg_el1(ctxt_sys_reg(ctxt, TCR2_EL1), SYS_TCR2);
+ u64 tcr2 = ctxt_sys_reg(ctxt, TCR2_EL1);
+
+ if (ctxt_is_guest(ctxt)) {
+ if (!ctxt_has_s1pie(ctxt))
+ tcr2 &= ~TCR2_EL1_PIE;
+ if (!ctxt_has_s1poe(ctxt))
+ tcr2 &= ~(TCR2_EL1_POE | TCR2_EL1_E0POE);
+ }
+ write_sysreg_el1(tcr2, SYS_TCR2);
if (ctxt_has_s1pie(ctxt)) {
write_sysreg_el1(ctxt_sys_reg(ctxt, PIR_EL1), SYS_PIR);
diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
index 3899702d438b..e4a3beac6ec9 100644
--- a/arch/arm64/kvm/sys_regs.c
+++ b/arch/arm64/kvm/sys_regs.c
@@ -624,6 +624,23 @@ static bool access_tcr2_el1(struct kvm_vcpu *vcpu,
if (!kvm_has_s1poe(vcpu->kvm))
p->regval &= ~(TCR2_EL1_POE | TCR2_EL1_E0POE);
+ if (is_hyp_ctxt(vcpu) && vcpu_el2_e2h_is_set(vcpu)) {
+ vcpu_write_sys_reg(vcpu, p->regval, TCR2_EL2);
+ return true;
+ }
+
+ return access_vm_reg(vcpu, p, r);
+}
+
+static bool access_tcr_el1(struct kvm_vcpu *vcpu,
+ struct sys_reg_params *p,
+ const struct sys_reg_desc *r)
+{
+ if (is_hyp_ctxt(vcpu) && vcpu_el2_e2h_is_set(vcpu)) {
+ vcpu_write_sys_reg(vcpu, p->regval, TCR_EL2);
+ return true;
+ }
+
return access_vm_reg(vcpu, p, r);
}
@@ -3512,7 +3529,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
{ SYS_DESC(SYS_SMCR_EL1), undef_access },
{ SYS_DESC(SYS_TTBR0_EL1), access_vm_reg, reset_unknown, TTBR0_EL1 },
{ SYS_DESC(SYS_TTBR1_EL1), access_vm_reg, reset_unknown, TTBR1_EL1 },
- { SYS_DESC(SYS_TCR_EL1), access_vm_reg, reset_val, TCR_EL1, 0 },
+ { SYS_DESC(SYS_TCR_EL1), access_tcr_el1, reset_val, TCR_EL1, 0 },
{ SYS_DESC(SYS_TCR2_EL1), access_tcr2_el1, reset_val, TCR2_EL1, 0,
.visibility = tcr2_visibility },
--
2.53.0
More information about the linux-arm-kernel
mailing list