Skip to contentSkip to conversation

KVM: arm64: Prevent the host from using an smc with imm16 != 0

PATCH Applied Sebastian Ene 3 participants 4 messages Mar 30 – Apr 2

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

PATCH v3 1/1 KVM: arm64: Prevent the host from using an smc with imm16 != 0

The ARM Service Calling Convention (SMCCC) specifies that the function identifier and parameters should be passed in registers, leaving the 16-bit immediate field un-handled in pKVM when an SMC instruction is trapped.
Since the HVC is a private interface between EL2 and the host, enforce the host kernel running under pKVM to use an immediate value of 0 only when using SMCs to make it clear for non-compliant software talking to Trustzone that we only use SMCCC.

---
v2 -> current:
- move the ESR decoding of the imm16 in the handle_host_smc where it was supposed to be
- updated the commit message to include the reason behind while we are not doing for HVCs as well
- updated the commit message to clarify that pKVM is not handling the imm16

v1 -> v2:
- Dropped injecting an UNDEF and return an error instead (SMCCC_RET_NOT_SUPPORTED)
- Used the mask ESR_ELx_xVC_IMM_MASK instead of masking with U16_MAX
- Updated the title of the commit message from:
"[PATCH] KVM: arm64: Inject UNDEF when host is executing an

    smc with imm16 != 0"

Link to v2:
https://lore.kernel.org/all/[email protected]/
Link to v1:
https://lore.kernel.org/all/[email protected]/

---
 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +7 -0
index e7790097db93..461cf5cb5ac7 100644
@@ -676,8 +676,14 @@ static void default_host_smc_handler(struct kvm_cpu_context *host_ctxt)
 static void handle_host_smc(struct kvm_cpu_context *host_ctxt)
 {
 	DECLARE_REG(u64, func_id, host_ctxt, 0);
+	u64 esr = read_sysreg_el2(SYS_ESR);
 	bool handled;
 
+	if (esr & ESR_ELx_xVC_IMM_MASK) {
+		cpu_reg(host_ctxt, 0) = SMCCC_RET_NOT_SUPPORTED;
+		goto exit_skip_instr;
+	}
+
 	func_id &= ~ARM_SMCCC_CALL_HINTS;
 
 	handled = kvm_host_psci_handler(host_ctxt, func_id);
@@ -686,6 +692,7 @@ static void handle_host_smc(struct kvm_cpu_context *host_ctxt)
 	if (!handled)
 		default_host_smc_handler(host_ctxt);
 
+exit_skip_instr:
 	/* SMC was trapped, move ELR past the current PC. */
 	kvm_skip_host_instr();
 }
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
Reviewed-by on v3 1/1
#1 lore raw
Quoted 10 lines · Sebastian Ene

The ARM Service Calling Convention (SMCCC) specifies that the function identifier and parameters should be passed in registers, leaving the 16-bit immediate field un-handled in pKVM when an SMC instruction is trapped.
Since the HVC is a private interface between EL2 and the host, enforce the host kernel running under pKVM to use an immediate value of 0 only when using SMCs to make it clear for non-compliant software talking to Trustzone that we only use SMCCC.

Quoted 56 lines

---
v2 -> current:
- move the ESR decoding of the imm16 in the handle_host_smc where it was supposed to be
- updated the commit message to include the reason behind while we are not doing for HVCs as well
- updated the commit message to clarify that pKVM is not handling the imm16

v1 -> v2:
- Dropped injecting an UNDEF and return an error instead (SMCCC_RET_NOT_SUPPORTED)
- Used the mask ESR_ELx_xVC_IMM_MASK instead of masking with U16_MAX
- Updated the title of the commit message from:
"[PATCH] KVM: arm64: Inject UNDEF when host is executing an

    smc with imm16 != 0"

Link to v2:
https://lore.kernel.org/all/[email protected]/
Link to v1:
https://lore.kernel.org/all/[email protected]/

---
 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +7 -0
index e7790097db93..461cf5cb5ac7 100644
@@ -676,8 +676,14 @@ static void default_host_smc_handler(struct kvm_cpu_context *host_ctxt)
 static void handle_host_smc(struct kvm_cpu_context *host_ctxt)
 {
 	DECLARE_REG(u64, func_id, host_ctxt, 0);
+	u64 esr = read_sysreg_el2(SYS_ESR);
 	bool handled;
 
+	if (esr & ESR_ELx_xVC_IMM_MASK) {
+		cpu_reg(host_ctxt, 0) = SMCCC_RET_NOT_SUPPORTED;
+		goto exit_skip_instr;
+	}
+
 	func_id &= ~ARM_SMCCC_CALL_HINTS;
 
 	handled = kvm_host_psci_handler(host_ctxt, func_id);
@@ -686,6 +692,7 @@ static void handle_host_smc(struct kvm_cpu_context *host_ctxt)
 	if (!handled)
 		default_host_smc_handler(host_ctxt);
 
+exit_skip_instr:
 	/* SMC was trapped, move ELR past the current PC. */
 	kvm_skip_host_instr();
 }
Signature · 1 line
2.53.0.1018.g2bb0e51243-goog
#2 lore raw
Quoted 11 lines · Vincent Donnefort
Quoted 9 lines · Sebastian Ene

The ARM Service Calling Convention (SMCCC) specifies that the function identifier and parameters should be passed in registers, leaving the 16-bit immediate field un-handled in pKVM when an SMC instruction is trapped.
Since the HVC is a private interface between EL2 and the host, enforce the host kernel running under pKVM to use an immediate value of 0 only when using SMCs to make it clear for non-compliant software talking to Trustzone that we only use SMCCC.

Quoted 2 lines
Quoted 1 line

Hello,

Quoted 2 lines

Thanks for reviewing,
Sebastian

Quoted 56 lines
Quoted 56 lines

---
v2 -> current:
- move the ESR decoding of the imm16 in the handle_host_smc where it was supposed to be
- updated the commit message to include the reason behind while we are not doing for HVCs as well
- updated the commit message to clarify that pKVM is not handling the imm16

v1 -> v2:
- Dropped injecting an UNDEF and return an error instead (SMCCC_RET_NOT_SUPPORTED)
- Used the mask ESR_ELx_xVC_IMM_MASK instead of masking with U16_MAX
- Updated the title of the commit message from:
"[PATCH] KVM: arm64: Inject UNDEF when host is executing an

    smc with imm16 != 0"

Link to v2:
https://lore.kernel.org/all/[email protected]/
Link to v1:
https://lore.kernel.org/all/[email protected]/

---
 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +7 -1
index e7790097db93..461cf5cb5ac7 100644
@@ -676,8 +676,14 @@ static void default_host_smc_handler(struct kvm_cpu_context *host_ctxt)
 static void handle_host_smc(struct kvm_cpu_context *host_ctxt)
 {
      DECLARE_REG(u64, func_id, host_ctxt, 0);
+     u64 esr = read_sysreg_el2(SYS_ESR);
      bool handled;
 
+     if (esr & ESR_ELx_xVC_IMM_MASK) {
+             cpu_reg(host_ctxt, 0) = SMCCC_RET_NOT_SUPPORTED;
+             goto exit_skip_instr;
+     }
+
      func_id &= ~ARM_SMCCC_CALL_HINTS;
 
      handled = kvm_host_psci_handler(host_ctxt, func_id);
@@ -686,6 +692,7 @@ static void handle_host_smc(struct kvm_cpu_context *host_ctxt)
      if (!handled)
              default_host_smc_handler(host_ctxt);
 
+exit_skip_instr:
      /* SMC was trapped, move ELR past the current PC. */
      kvm_skip_host_instr();
 }
--

2.53.0.1018.g2bb0e51243-goog

maintainer Applied
#3 lore raw
In reply to Sebastian Ene
Quoted 10 lines · Sebastian Ene

The ARM Service Calling Convention (SMCCC) specifies that the function identifier and parameters should be passed in registers, leaving the 16-bit immediate field un-handled in pKVM when an SMC instruction is trapped.
Since the HVC is a private interface between EL2 and the host, enforce the host kernel running under pKVM to use an immediate value of 0 only when using SMCs to make it clear for non-compliant software talking to Trustzone that we only use SMCCC.

[...]

Applied to next, thanks!

[1/1] KVM: arm64: Prevent the host from using an smc with imm16 != 0

      commit: cf6348af645bd8e38758114e6afcc406c5bb515f

Cheers,

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