[PATCH 2/3] KVM: arm64: Load no-access permissions when S1PIE or S1POE is hidden

Dan Carpenter dan at pereq.eu
Fri Nov 27 07:31:39 PST 2026


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 at vger.kernel.org
Signed-off-by: Dan Carpenter <dan at pereq.eu>
---
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 <asm/kvm_mmu.h>
 
 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




More information about the linux-arm-kernel mailing list