Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/3] KVM: arm64: nv: trap forwarding bugs
@ 2026-11-18 15:29 Dan Carpenter
  2026-11-18 15:29 ` [PATCH 1/3] KVM: arm64: nv: Forward PIR_EL2, PIRE0_EL2 and POR_EL2 traps Dan Carpenter
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Dan Carpenter @ 2026-11-18 15:29 UTC (permalink / raw)
  To: Marc Zyngier
  Cc: Catalin Marinas, Fuad Tabba, Joey Gouly, kvmarm, linux-arm-kernel,
	Mark Rutland, Oliver Upton, Steffen Eiden, Suzuki K Poulose,
	Will Deacon, Zenghui Yu, Karthik Bhargavan, Clément Blaudeau

This was detected as part of our Formal Analysis work at Pereq.

Regular KVM and nested virt traps have been getting out of sync.  Fix a
couple bugs and add a boot time check to prevent future bugs.

Dan Carpenter (3):
  KVM: arm64: nv: Forward PIR_EL2, PIRE0_EL2 and POR_EL2 traps
  KVM: arm64: nv: Forward CNTP_TVAL_EL02 and CNTV_TVAL_EL02 traps
  KVM: arm64: nv: Report EL2 registers with no trap forwarding

 arch/arm64/include/asm/kvm_host.h |  2 +-
 arch/arm64/kvm/emulate-nested.c   | 10 +++++++++-
 arch/arm64/kvm/sys_regs.c         | 23 +++++++++++++++++++++--
 3 files changed, 31 insertions(+), 4 deletions(-)

-- 
2.53.0



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] KVM: arm64: nv: Forward PIR_EL2, PIRE0_EL2 and POR_EL2 traps
  2026-11-18 15:29 [PATCH 0/3] KVM: arm64: nv: trap forwarding bugs Dan Carpenter
@ 2026-11-18 15:29 ` Dan Carpenter
  2026-11-18 15:30 ` [PATCH 2/3] KVM: arm64: nv: Forward CNTP_TVAL_EL02 and CNTV_TVAL_EL02 traps Dan Carpenter
  2026-11-18 15:30 ` [PATCH 3/3] KVM: arm64: nv: Report EL2 registers with no trap forwarding Dan Carpenter
  2 siblings, 0 replies; 4+ messages in thread
From: Dan Carpenter @ 2026-11-18 15:29 UTC (permalink / raw)
  To: Marc Zyngier
  Cc: Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
	Mark Rutland, linux-arm-kernel, kvmarm, Karthik Bhargavan,
	Clément Blaudeau

These registers were added to KVM, but not to the encoding_to_cgt[]
table which decides when a trap is forwarded to a nested hypervisor.

The result is that if an L1 hypervisor runs its L2 with HCR_EL2.NV set
(meaning the L2 is itself a hypervisor), and the L2 accesses these
registers, KVM handles the access itself and it goes to the L1's own
registers instead of being emulated on L1.  This is quite a serious bug
because it lets the L2 change the permissions of the L1's own stage 1
translations.  It could mark everything as no-access and cause a crash
in L1 or mark everything as read-write-execute which weakens the
protections in L1.

Add the three registers with CGT_HCR_NV, MAIR_EL2 and AMAIR_EL2.

Fixes: 874ae1d48e60 ("KVM: arm64: Handle PIR{,E0}_EL2 traps")
Fixes: 5970e9903f03 ("KVM: arm64: Add basic support for POR_EL2")
Cc: stable@vger.kernel.org
Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
 arch/arm64/kvm/emulate-nested.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/arch/arm64/kvm/emulate-nested.c b/arch/arm64/kvm/emulate-nested.c
index 2bec81668361..bf4b8a44b9c4 100644
--- a/arch/arm64/kvm/emulate-nested.c
+++ b/arch/arm64/kvm/emulate-nested.c
@@ -889,6 +889,9 @@ static const struct encoding_to_trap_config encoding_to_cgt[] __initconst = {
 	SR_TRAP(SYS_PMSCR_EL2,		CGT_HCR_NV),
 	SR_TRAP(SYS_MAIR_EL2,		CGT_HCR_NV),
 	SR_TRAP(SYS_AMAIR_EL2,		CGT_HCR_NV),
+	SR_TRAP(SYS_PIR_EL2,		CGT_HCR_NV),
+	SR_TRAP(SYS_PIRE0_EL2,		CGT_HCR_NV),
+	SR_TRAP(SYS_POR_EL2,		CGT_HCR_NV),
 	SR_TRAP(SYS_MPAMHCR_EL2,	CGT_HCR_NV),
 	SR_TRAP(SYS_MPAMVPMV_EL2,	CGT_HCR_NV),
 	SR_TRAP(SYS_MPAM2_EL2,		CGT_HCR_NV),
-- 
2.53.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/3] KVM: arm64: nv: Forward CNTP_TVAL_EL02 and CNTV_TVAL_EL02 traps
  2026-11-18 15:29 [PATCH 0/3] KVM: arm64: nv: trap forwarding bugs Dan Carpenter
  2026-11-18 15:29 ` [PATCH 1/3] KVM: arm64: nv: Forward PIR_EL2, PIRE0_EL2 and POR_EL2 traps Dan Carpenter
@ 2026-11-18 15:30 ` Dan Carpenter
  2026-11-18 15:30 ` [PATCH 3/3] KVM: arm64: nv: Report EL2 registers with no trap forwarding Dan Carpenter
  2 siblings, 0 replies; 4+ messages in thread
From: Dan Carpenter @ 2026-11-18 15:30 UTC (permalink / raw)
  To: Marc Zyngier
  Cc: Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
	Mark Rutland, linux-arm-kernel, kvmarm, Karthik Bhargavan,
	Clément Blaudeau

There are a couple timer related entries messing in the nested
virtualization traps.

With nested virtualization you can run a hypervisor in L1 and then you
can run another hypervisor under that one in L2 and a guest under that
in L3.  Without these then if L2 tries to read or write L3's timers it
instead reads and writes to its own timers.

Fixes: c271269e3570 ("KVM: arm64: nv: Add trap routing for CNTHCTL_EL2.EL1{NVPCT,NVVCT,TVT,TVCT}")
Cc: stable@vger.kernel.org
Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
 arch/arm64/kvm/emulate-nested.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/arch/arm64/kvm/emulate-nested.c b/arch/arm64/kvm/emulate-nested.c
index bf4b8a44b9c4..9e0c15e6be47 100644
--- a/arch/arm64/kvm/emulate-nested.c
+++ b/arch/arm64/kvm/emulate-nested.c
@@ -942,8 +942,10 @@ static const struct encoding_to_trap_config encoding_to_cgt[] __initconst = {
 		      sys_reg(3, 5, 14, 1, 0), CGT_HCR_NV),
 	SR_TRAP(SYS_CNTP_CTL_EL02,	CGT_CNTHCTL_EL1NVPCT),
 	SR_TRAP(SYS_CNTP_CVAL_EL02,	CGT_CNTHCTL_EL1NVPCT),
+	SR_TRAP(SYS_CNTP_TVAL_EL02,	CGT_HCR_NV),
 	SR_TRAP(SYS_CNTV_CTL_EL02,	CGT_CNTHCTL_EL1NVVCT),
 	SR_TRAP(SYS_CNTV_CVAL_EL02,	CGT_CNTHCTL_EL1NVVCT),
+	SR_TRAP(SYS_CNTV_TVAL_EL02,	CGT_HCR_NV),
 	SR_TRAP(OP_AT_S1E2R,		CGT_HCR_NV),
 	SR_TRAP(OP_AT_S1E2W,		CGT_HCR_NV),
 	SR_TRAP(OP_AT_S12E1R,		CGT_HCR_NV),
-- 
2.53.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 3/3] KVM: arm64: nv: Report EL2 registers with no trap forwarding
  2026-11-18 15:29 [PATCH 0/3] KVM: arm64: nv: trap forwarding bugs Dan Carpenter
  2026-11-18 15:29 ` [PATCH 1/3] KVM: arm64: nv: Forward PIR_EL2, PIRE0_EL2 and POR_EL2 traps Dan Carpenter
  2026-11-18 15:30 ` [PATCH 2/3] KVM: arm64: nv: Forward CNTP_TVAL_EL02 and CNTV_TVAL_EL02 traps Dan Carpenter
@ 2026-11-18 15:30 ` Dan Carpenter
  2 siblings, 0 replies; 4+ messages in thread
From: Dan Carpenter @ 2026-11-18 15:30 UTC (permalink / raw)
  To: Marc Zyngier
  Cc: Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
	Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
	Mark Rutland, linux-arm-kernel, kvmarm, Karthik Bhargavan,
	Clément Blaudeau

One of the recurring issues we run into is that people add a trap to
KVM but they forget to add a matching trap for nested virtualization.
Add a boot time warning to print a warning for missing traps.

Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
 arch/arm64/include/asm/kvm_host.h |  2 +-
 arch/arm64/kvm/emulate-nested.c   |  5 ++++-
 arch/arm64/kvm/sys_regs.c         | 23 +++++++++++++++++++++--
 3 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 8efb21b2d729..0614c3b3a40d 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -1379,7 +1379,7 @@ void kvm_reset_sys_regs(struct kvm_vcpu *vcpu);
 int __init kvm_sys_reg_table_init(void);
 struct sys_reg_desc;
 int __init populate_sysreg_config(const struct sys_reg_desc *sr,
-				  unsigned int idx);
+				  unsigned int idx, bool needs_nv_trap);
 int __init populate_nv_trap_config(void);
 
 void kvm_calculate_traps(struct kvm_vcpu *vcpu);
diff --git a/arch/arm64/kvm/emulate-nested.c b/arch/arm64/kvm/emulate-nested.c
index 9e0c15e6be47..39136a89d74b 100644
--- a/arch/arm64/kvm/emulate-nested.c
+++ b/arch/arm64/kvm/emulate-nested.c
@@ -2434,7 +2434,7 @@ int __init populate_nv_trap_config(void)
 }
 
 int __init populate_sysreg_config(const struct sys_reg_desc *sr,
-				  unsigned int idx)
+				  unsigned int idx, bool needs_nv_trap)
 {
 	union trap_config tc;
 	u32 encoding;
@@ -2452,6 +2452,9 @@ int __init populate_sysreg_config(const struct sys_reg_desc *sr,
 	encoding = sys_reg(sr->Op0, sr->Op1, sr->CRn, sr->CRm, sr->Op2);
 	tc = get_trap_config(encoding);
 
+	if (needs_nv_trap && !tc.val)
+		kvm_err("sysreg %s has no NV trap configuration\n", sr->name);
+
 	if (tc.sri) {
 		kvm_err("sysreg %s (%d) duplicate entry (%d)\n",
 			sr->name, idx - 1, tc.sri);
diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
index ef4554656ddf..afd7e76bedff 100644
--- a/arch/arm64/kvm/sys_regs.c
+++ b/arch/arm64/kvm/sys_regs.c
@@ -5978,6 +5978,23 @@ int kvm_vcpu_finalize_sys_regs(struct kvm_vcpu *vcpu)
 	return 0;
 }
 
+/* Keep the nested virtualization traps in sync with the KVM traps. */
+static bool __init needs_nv_trap(const struct sys_reg_desc *r)
+{
+	/* op1 4 is the EL2 encodings, op1 5 the _EL12 and _EL02 aliases */
+	if (r->Op1 != 4 && r->Op1 != 5)
+		return false;
+
+	if (!r->access ||
+	    r->access == bad_vncr_trap ||
+	    r->access == trap_raz_wi ||
+	    r->access == bad_redir_trap ||
+	    r->access == undef_access)
+		return false;
+
+	return true;
+}
+
 int __init kvm_sys_reg_table_init(void)
 {
 	const struct sys_reg_desc *gicv3_regs, *gicv5_regs;
@@ -6010,10 +6027,12 @@ int __init kvm_sys_reg_table_init(void)
 	check_feature_map();
 
 	for (i = 0; !ret && i < ARRAY_SIZE(sys_reg_descs); i++)
-		ret = populate_sysreg_config(sys_reg_descs + i, i);
+		ret = populate_sysreg_config(sys_reg_descs + i, i,
+					     needs_nv_trap(sys_reg_descs + i));
 
 	for (i = 0; !ret && i < ARRAY_SIZE(sys_insn_descs); i++)
-		ret = populate_sysreg_config(sys_insn_descs + i, i);
+		ret = populate_sysreg_config(sys_insn_descs + i, i,
+					     needs_nv_trap(sys_insn_descs + i));
 
 	return ret;
 }
-- 
2.53.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-18 15:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 15:29 [PATCH 0/3] KVM: arm64: nv: trap forwarding bugs Dan Carpenter
2026-11-18 15:29 ` [PATCH 1/3] KVM: arm64: nv: Forward PIR_EL2, PIRE0_EL2 and POR_EL2 traps Dan Carpenter
2026-11-18 15:30 ` [PATCH 2/3] KVM: arm64: nv: Forward CNTP_TVAL_EL02 and CNTV_TVAL_EL02 traps Dan Carpenter
2026-11-18 15:30 ` [PATCH 3/3] KVM: arm64: nv: Report EL2 registers with no trap forwarding Dan Carpenter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox