Skip to contentSkip to conversation

KVM: arm64: Fixes for SMC64 SYSTEM_RESET2 calls

PATCH Applied Oliver Upton 2 participants 5 messages Mar 22, 2022 – Mar 23, 2022

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

v2 cover KVM: arm64: Fixes for SMC64 SYSTEM_RESET2 calls

This series addresses a couple of issues with how KVM exposes SMC64 calls to its guest. It is currently possible for an AArch32 guest to discover the SMC64 SYSTEM_RESET2 function (via PSCI_1_0_FN_PSCI_FEATURES) and even make a call to it. SMCCC does not allow for 64 bit calls to be made from a 32 bit state.

Patch 1 cleans up the way we filter SMC64 calls in PSCI. Using a switch with case statements for each possibly-filtered function is asking for trouble. Instead, pivot off of the bit that indicates the desired calling convention. This plugs the PSCI_FEATURES hole for SYSTEM_RESET2.

Patch 2 adds a check to the PSCI v1.x call handler in KVM, bailing out early if the guest is not allowed to use a particular function. This closes the door on calls to 64-bit SYSTEM_RESET2 from AArch32.

Lastly, patch 3 is a nit to remove a superfluous check in the hopes of avoiding trouble the next time we raise KVM's PSCI version.

Applies on top of kvmarm/next at commit:

21ea45784275 ("KVM: arm64: fix typos in comments")

v1: http://lore.kernel.org/r/[email protected]

v1 -> v2:
- Collect Acks and Reviews (Reiji, Will)
- Hoist SMC64 filtering all the way up to kvm_psci_call() (Reiji)

Oliver Upton (3):
KVM: arm64: Generally disallow SMC64 for AArch32 guests
KVM: arm64: Actually prevent SMC64 SYSTEM_RESET2 from AArch32
KVM: arm64: Drop unneeded minor version check from PSCI v1.x handler

 arch/arm64/kvm/psci.c | 31 ++++++++++++++-----------------
 1 file changed, 14 insertions(+), 17 deletions(-)
Signature · 1 line
2.35.1.894.gb6a874cedc-goog
#1 lore raw

PATCH v2 1/3 KVM: arm64: Generally disallow SMC64 for AArch32 guests

The only valid calling SMC calling convention from an AArch32 state is SMC32. Disallow any PSCI function that sets the SMC64 function ID bit when called from AArch32 rather than comparing against known SMC64 PSCI functions.

Note that without this change KVM advertises the SMC64 flavor of SYSTEM_RESET2 to AArch32 guests.

Fixes d43583b890e7 ("KVM: arm64: Expose PSCI SYSTEM_RESET2 call to the guest")
---
 arch/arm64/kvm/psci.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
arch/arm64/kvm/psci.c +5 -9
index a433c3eac9b7..cd3ee947485f 100644
@@ -216,15 +216,11 @@ static void kvm_psci_narrow_to_32bit(struct kvm_vcpu *vcpu)
 
 static unsigned long kvm_psci_check_allowed_function(struct kvm_vcpu *vcpu, u32 fn)
 {
-	switch(fn) {
-	case PSCI_0_2_FN64_CPU_SUSPEND:
-	case PSCI_0_2_FN64_CPU_ON:
-	case PSCI_0_2_FN64_AFFINITY_INFO:
-		/* Disallow these functions for 32bit guests */
-		if (vcpu_mode_is_32bit(vcpu))
-			return PSCI_RET_NOT_SUPPORTED;
-		break;
-	}
+	/*
+	 * Prevent 32 bit guests from calling 64 bit PSCI functions.
+	 */
+	if ((fn & PSCI_0_2_64BIT) && vcpu_mode_is_32bit(vcpu))
+		return PSCI_RET_NOT_SUPPORTED;
 
 	return 0;
 }
Signature · 1 line
2.35.1.894.gb6a874cedc-goog
#2 lore raw

PATCH v2 2/3 KVM: arm64: Actually prevent SMC64 SYSTEM_RESET2 from AArch32

In reply to Oliver Upton

The SMCCC does not allow the SMC64 calling convention to be used from AArch32. While KVM checks to see if the calling convention is allowed in PSCI_1_0_FN_PSCI_FEATURES, it does not actually prevent calls to unadvertised PSCI v1.0+ functions.

Hoist the check to see if the requested function is allowed into kvm_psci_call(), thereby preventing SMC64 calls from AArch32 for all PSCI versions.

Fixes d43583b890e7 ("KVM: arm64: Expose PSCI SYSTEM_RESET2 call to the guest")
---
 arch/arm64/kvm/psci.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
arch/arm64/kvm/psci.c +9 -5
index cd3ee947485f..d24ccc77500b 100644
@@ -232,10 +232,6 @@ static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
 	unsigned long val;
 	int ret = 1;
 
-	val = kvm_psci_check_allowed_function(vcpu, psci_fn);
-	if (val)
-		goto out;
-
 	switch (psci_fn) {
 	case PSCI_0_2_FN_PSCI_VERSION:
 		/*
@@ -303,7 +299,6 @@ static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
 		break;
 	}
 
-out:
 	smccc_set_retval(vcpu, val, 0, 0, 0);
 	return ret;
 }
@@ -423,6 +418,15 @@ static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
  */
 int kvm_psci_call(struct kvm_vcpu *vcpu)
 {
+	u32 psci_fn = smccc_get_function(vcpu);
+	unsigned long val;
+
+	val = kvm_psci_check_allowed_function(vcpu, psci_fn);
+	if (val) {
+		smccc_set_retval(vcpu, val, 0, 0, 0);
+		return 1;
+	}
+
 	switch (kvm_psci_version(vcpu)) {
 	case KVM_ARM_PSCI_1_1:
 		return kvm_psci_1_x_call(vcpu, 1);
Signature · 1 line
2.35.1.894.gb6a874cedc-goog
#3 lore raw

PATCH v2 3/3 KVM: arm64: Drop unneeded minor version check from PSCI v1.x handler

In reply to Oliver Upton

We already sanitize the guest's PSCI version when it is being written by userspace, rejecting unsupported version numbers. Additionally, the 'minor' parameter to kvm_psci_1_x_call() is a constant known at compile time for all callsites.

Though it is benign, the additional check against the
PSCI kvm_psci_1_x_call() is unnecessary and likely to be missed the next time KVM raises its maximum PSCI version. Drop the check altogether and rely on sanitization when the PSCI version is set by userspace.

No functional change intended.

---
 arch/arm64/kvm/psci.c | 3 ---
 1 file changed, 3 deletions(-)
arch/arm64/kvm/psci.c +0 -3
index d24ccc77500b..0b1b588f1f9b 100644
@@ -310,9 +310,6 @@ static int kvm_psci_1_x_call(struct kvm_vcpu *vcpu, u32 minor)
 	unsigned long val;
 	int ret = 1;
 
-	if (minor > 1)
-		return -EINVAL;
-
 	switch(psci_fn) {
 	case PSCI_0_2_FN_PSCI_VERSION:
 		val = minor == 0 ? KVM_ARM_PSCI_1_0 : KVM_ARM_PSCI_1_1;
Signature · 1 line
2.35.1.894.gb6a874cedc-goog
maintainer Applied
#4 lore raw
In reply to Oliver Upton
Quoted 6 lines · Oliver Upton

This series addresses a couple of issues with how KVM exposes SMC64 calls to its guest. It is currently possible for an AArch32 guest to discover the SMC64 SYSTEM_RESET2 function (via PSCI_1_0_FN_PSCI_FEATURES) and even make a call to it. SMCCC does not allow for 64 bit calls to be made from a 32 bit state.

Quoted 6 lines

Patch 1 cleans up the way we filter SMC64 calls in PSCI. Using a switch with case statements for each possibly-filtered function is asking for trouble. Instead, pivot off of the bit that indicates the desired calling convention. This plugs the PSCI_FEATURES hole for SYSTEM_RESET2.

[...]

Applied to fixes, thanks!

[1/3] KVM: arm64: Generally disallow SMC64 for AArch32 guests

      commit: 905ec3226f8150f73838a36cb79ba79e1d789e8e
[2/3] KVM: arm64: Actually prevent SMC64 SYSTEM_RESET2 from AArch32
      commit: 3e1b3dbad320e1532fdf09e5b80e35d62a0fd82b
[3/3] KVM: arm64: Drop unneeded minor version check from PSCI v1.x handler
      commit: 8872d9b3e35a0ecb80d6413bba403d4aaf49af63

Cheers,

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