Skip to contentSkip to conversation

KVM: arm64: nv: trap forwarding bugs

PATCH Applied Dan Carpenter 2 participants 5 messages Nov 18 – Nov 19

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover KVM: arm64: nv: trap forwarding bugs

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(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/3 KVM: arm64: nv: Forward PIR_EL2, PIRE0_EL2 and POR_EL2 traps

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")
---
 arch/arm64/kvm/emulate-nested.c | 3 +++
 1 file changed, 3 insertions(+)
arch/arm64/kvm/emulate-nested.c +3 -0
index 2bec81668361..bf4b8a44b9c4 100644
@@ -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),
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/3 KVM: arm64: nv: Forward CNTP_TVAL_EL02 and CNTV_TVAL_EL02 traps

In reply to Dan Carpenter

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}")
---
 arch/arm64/kvm/emulate-nested.c | 2 ++
 1 file changed, 2 insertions(+)
arch/arm64/kvm/emulate-nested.c +2 -0
index bf4b8a44b9c4..9e0c15e6be47 100644
@@ -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),
Signature · 1 line
2.53.0
#3 lore raw

PATCH 3/3 KVM: arm64: nv: Report EL2 registers with no trap forwarding

In reply to Dan Carpenter

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.

---
 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(-)
arch/arm64/include/asm/kvm_host.h +1 -1
index 8efb21b2d729..0614c3b3a40d 100644
@@ -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);
arch/arm64/kvm/emulate-nested.c +4 -1
index 9e0c15e6be47..39136a89d74b 100644
@@ -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);
arch/arm64/kvm/sys_regs.c +21 -2
index ef4554656ddf..afd7e76bedff 100644
@@ -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;
 }
Signature · 1 line
2.53.0
maintainer Applied
#4 lore raw
In reply to Dan Carpenter
Quoted 11 lines · Dan Carpenter

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

[...]

Applied to fixes, thanks!

[1/3] KVM: arm64: nv: Forward PIR_EL2, PIRE0_EL2 and POR_EL2 traps

      commit: 88fa0c91640ce163190531bf3e975c73c77d7c7b
[2/3] KVM: arm64: nv: Forward CNTP_TVAL_EL02 and CNTV_TVAL_EL02 traps
      commit: fb32e91ac511101f528fa2f33cb1904b3796f5c3
[3/3] KVM: arm64: nv: Report EL2 registers with no trap forwarding
      commit: 2fec206f37582b9295185a86e43a08ac74e149d2

Cheers,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.