Skip to contentSkip to conversation

Revoke supported SEV VM types

PATCH Superseded Tycho Andersen 2 participants 11 messages Mar 3 – Mar 12

Subsystems (7)
x86 KVM Kselftest Crypto API +3 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Revoke supported SEV VM types

From: "Tycho Andersen (AMD)" <[email protected]>

Recent SEV firmware [1] does not support SEV-ES VMs when SNP is enabled. Sean suggested [2] adding an API so that userspace can check for this condition, so do that. Also introduce and use SNP_VERIFY_MITIGATION to determine whether it is present or not.

[1]: https://www.amd.com/en/resources/product-security/bulletin/amd-sb-3023.html
[2]: https://lore.kernel.org/all/[email protected]/

Tycho Andersen (AMD) (5):
kvm/sev: don't expose unusable VM types crypto/ccp: introduce SNP_VERIFY_MITIGATION crypto/ccp: export firmware supported vm types kvm/sev: mask off firmware unsupported vm types selftests/kvm: teach sev_*_test about revoking VM types

 arch/x86/kvm/svm/sev.c                        | 16 +++-
 drivers/crypto/ccp/sev-dev.c                  | 84 +++++++++++++++++++
 include/linux/psp-sev.h                       | 56 +++++++++++++
 .../selftests/kvm/x86/sev_init2_tests.c       | 14 ++--
 .../selftests/kvm/x86/sev_migrate_tests.c     |  2 +-
 .../selftests/kvm/x86/sev_smoke_test.c        |  4 +-
 6 files changed, 162 insertions(+), 14 deletions(-)

base-commit: 11439c4635edd669ae435eec308f4ab8a0804808

Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/5 kvm/sev: don't expose unusable VM types

From: "Tycho Andersen (AMD)" <[email protected]>

Commit 0aa6b90ef9d7 ("KVM: SVM: Add support for allowing zero SEV ASIDs") made it possible to make it impossible to use SEV VMs by not allocating them any ASIDs.

Commit 6c7c620585c6 ("KVM: SEV: Add SEV-SNP CipherTextHiding support") did the same thing for SEV-ES.

Do not export KVM_X86_SEV(_ES)_VM as exported types if in either of these situations, so that userspace can use them to determine what is actually supported by the current kernel configuration.

Also move the buildup to a local variable so it is easier to add additional masking in future patches.

---
 arch/x86/kvm/svm/sev.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/sev.c +11 -3
index 3f9c1aa39a0a..f941d48626d3 100644
@@ -2957,18 +2957,26 @@ void sev_vm_destroy(struct kvm *kvm)
 
 void __init sev_set_cpu_caps(void)
 {
+	int supported_vm_types = 0;
+
 	if (sev_enabled) {
 		kvm_cpu_cap_set(X86_FEATURE_SEV);
-		kvm_caps.supported_vm_types |= BIT(KVM_X86_SEV_VM);
+
+		if (min_sev_asid <= max_sev_asid)
+			supported_vm_types |= BIT(KVM_X86_SEV_VM);
 	}
 	if (sev_es_enabled) {
 		kvm_cpu_cap_set(X86_FEATURE_SEV_ES);
-		kvm_caps.supported_vm_types |= BIT(KVM_X86_SEV_ES_VM);
+
+		if (min_sev_es_asid <= max_sev_es_asid)
+			supported_vm_types |= BIT(KVM_X86_SEV_ES_VM);
 	}
 	if (sev_snp_enabled) {
 		kvm_cpu_cap_set(X86_FEATURE_SEV_SNP);
-		kvm_caps.supported_vm_types |= BIT(KVM_X86_SNP_VM);
+		supported_vm_types |= BIT(KVM_X86_SNP_VM);
 	}
+
+	kvm_caps.supported_vm_types |= supported_vm_types;
 }
 
 static bool is_sev_snp_initialized(void)
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 1/5] kvm/sev: don't expose unusable VM types

KVM: SEV:

Quoted 10 lines · Tycho Andersen

From: "Tycho Andersen (AMD)" <[email protected]>

Commit 0aa6b90ef9d7 ("KVM: SVM: Add support for allowing zero SEV ASIDs") made it possible to make it impossible to use SEV VMs by not allocating them any ASIDs.

Commit 6c7c620585c6 ("KVM: SEV: Add SEV-SNP CipherTextHiding support") did the same thing for SEV-ES.

Do not export KVM_X86_SEV(_ES)_VM as exported types if in either of these

                                       ^^^^^^^^
                                       supported
Quoted 18 lines

situations, so that userspace can use them to determine what is actually supported by the current kernel configuration.

Also move the buildup to a local variable so it is easier to add additional masking in future patches.

---
 arch/x86/kvm/svm/sev.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/sev.c +0 -0
index 3f9c1aa39a0a..f941d48626d3 100644

arch/x86/kvm/svm/sev.c void sev_vm_destroy(struct kvm *kvm) · line 2960

Quoted 4 lines
arch/x86/kvm/svm/sev.c +1 -0
@@ -2957,18 +2957,26 @@ void sev_vm_destroy(struct kvm *kvm)
 
 void __init sev_set_cpu_caps(void)
 {
+	int supported_vm_types = 0;

This should be a u32.

Quoted 28 lines
arch/x86/kvm/svm/sev.c +10 -3
+
 	if (sev_enabled) {
 		kvm_cpu_cap_set(X86_FEATURE_SEV);
-		kvm_caps.supported_vm_types |= BIT(KVM_X86_SEV_VM);
+
+		if (min_sev_asid <= max_sev_asid)
+			supported_vm_types |= BIT(KVM_X86_SEV_VM);
 	}
 	if (sev_es_enabled) {
 		kvm_cpu_cap_set(X86_FEATURE_SEV_ES);
-		kvm_caps.supported_vm_types |= BIT(KVM_X86_SEV_ES_VM);
+
+		if (min_sev_es_asid <= max_sev_es_asid)
+			supported_vm_types |= BIT(KVM_X86_SEV_ES_VM);
 	}
 	if (sev_snp_enabled) {
 		kvm_cpu_cap_set(X86_FEATURE_SEV_SNP);
-		kvm_caps.supported_vm_types |= BIT(KVM_X86_SNP_VM);
+		supported_vm_types |= BIT(KVM_X86_SNP_VM);
 	}
+
+	kvm_caps.supported_vm_types |= supported_vm_types;
 }
 
 static bool is_sev_snp_initialized(void)
Signature · 1 line
2.53.0
#3 lore raw

PATCH 2/5 crypto/ccp: introduce SNP_VERIFY_MITIGATION

In reply to Tycho Andersen

From: "Tycho Andersen (AMD)" <[email protected]>

These are all documented in the SEV FW document ID 56860.

These are based on the previous patch in the link, though moved out of uapi.

---
 drivers/crypto/ccp/sev-dev.c |  1 +
 include/linux/psp-sev.h      | 47 ++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)
drivers/crypto/ccp/sev-dev.c +1 -0
index 096f993974d1..9eba3fe1a27f 100644
@@ -222,6 +222,7 @@ static int sev_cmd_buffer_len(int cmd)
 	case SEV_CMD_GUEST_STATUS:		return sizeof(struct sev_data_guest_status);
 	case SEV_CMD_DBG_DECRYPT:		return sizeof(struct sev_data_dbg);
 	case SEV_CMD_DBG_ENCRYPT:		return sizeof(struct sev_data_dbg);
+	case SEV_CMD_SNP_VERIFY_MITIGATION:	return sizeof(struct sev_data_snp_verify_mitigation);
 	case SEV_CMD_SEND_START:		return sizeof(struct sev_data_send_start);
 	case SEV_CMD_SEND_UPDATE_DATA:		return sizeof(struct sev_data_send_update_data);
 	case SEV_CMD_SEND_UPDATE_VMSA:		return sizeof(struct sev_data_send_update_vmsa);
include/linux/psp-sev.h +47 -0

65 lines not on this page · raw message

Signature · 1 line
2.53.0
#4 lore raw

PATCH 3/5 crypto/ccp: export firmware supported vm types

In reply to Tycho Andersen

From: "Tycho Andersen (AMD)" <[email protected]>

In some configurations, the firmware does not support all VM types. Do an SNP_VERIFY_MITIGATION to determine if the mitigation for CVE-2025-48514 is active, and if so, turn off the SEV_ES bit.

---
 drivers/crypto/ccp/sev-dev.c | 83 ++++++++++++++++++++++++++++++++++++
 include/linux/psp-sev.h      |  9 ++++
 2 files changed, 92 insertions(+)
drivers/crypto/ccp/sev-dev.c +83 -0
index 9eba3fe1a27f..79610617a38d 100644
@@ -2930,3 +2930,86 @@ void sev_pci_exit(void)
 
 	sev_firmware_shutdown(sev);
 }
+
+static int snp_verify_mitigation(struct sev_device *sev, u64 vector, u64 *verified)
+{
+	struct sev_data_snp_verify_mitigation data = {0};
+	struct snp_verify_mitigation_dst *dst;
+	struct page *p;
+	int rc, error = 0;
+
+	if (!sev->snp_plat_status.feature_info ||
+	    !(sev->snp_feat_info_0.ecx & SNP_VERIFY_MITIGATION_SUPPORTED)) {
+		return -EOPNOTSUPP;
+	}
+
+	p = __snp_alloc_firmware_pages(GFP_KERNEL, 0, true);
+	if (!p)
+		return -ENOMEM;
+	dst = page_address(p);
+
+	data.length = sizeof(data);
+	data.subcommand = SNP_MIT_SUBCMD_REQ_VERIFY;
+	data.vector = vector;
+	data.dst_paddr_en = 1;
+	data.dst_paddr = __psp_pa(dst);
+
+	rc = sev_do_cmd(SEV_CMD_SNP_VERIFY_MITIGATION, &data, &error);
+	if (rc < 0) {
+		if (error)
+			dev_err(sev->dev, "VERIFY_MITIGATION error %d\n", error);
+		goto reclaim_pages;
+	}
+
+	rc = -EIO;
+	if (dst->mit_failure_status) {
+		dev_err(sev->dev, "VERIFY_MITIGATION failure status %d\n", dst->mit_failure_status);
+		goto reclaim_pages;
+	}
+
+	*verified = dst->mit_verified_vector;
+	rc = 0;
+
+reclaim_pages:
+	__snp_free_firmware_pages(p, 0, true);
+	return rc;
+}
+
+int sev_firmware_supported_vm_types(void)
+{
+	int rc, supported_vm_types = 0;
+	struct sev_device *sev;
+	u64 verified = 0;
+
+	if (!psp_master || !psp_master->sev_data)
+		return supported_vm_types;
+	sev = psp_master->sev_data;
+
+	supported_vm_types |= BIT(KVM_X86_SEV_VM);
+	supported_vm_types |= BIT(KVM_X86_SEV_ES_VM);
+
+	if (!sev->snp_initialized)
+		return supported_vm_types;
+
+	supported_vm_types |= BIT(KVM_X86_SNP_VM);
+
+	rc = snp_verify_mitigation(sev, SNP_MIT_VEC_CVE_2025_48514, &verified);
+	if (rc < 0) {
+		/*
+		 * Older firmware that doesn't support VERIFY_MITIGATION won't
+		 * have the mitigation for this CVE, so all types are supported.
+		 */
+		if (rc == -EOPNOTSUPP)
+			return supported_vm_types;
+		dev_err(sev->dev, "Unable to determine supported vm types: %d\n", rc);
+		return supported_vm_types;
+	}
+
+	/* This mitigation disables SEV-ES guests when present */
+	if (verified & SNP_MIT_VEC_CVE_2025_48514)
+		supported_vm_types &= ~BIT(KVM_X86_SEV_ES_VM);
+
+	return supported_vm_types;
+
+}
+EXPORT_SYMBOL_FOR_MODULES(sev_firmware_supported_vm_types, "kvm-amd");
include/linux/psp-sev.h +9 -0
index 2b4b56632b4e..07ce49b31ba2 100644
@@ -611,6 +611,12 @@ struct sev_data_snp_verify_mitigation {
 #define SNP_MIT_SUBCMD_REQ_STATUS	0x0
 #define SNP_MIT_SUBCMD_REQ_VERIFY	0x1
 
+/*
+ * For CVE-2025-48514 defined in AMD-SB-3023
+ * https://www.amd.com/en/resources/product-security/bulletin/amd-sb-3023.html
+ */
+#define SNP_MIT_VEC_CVE_2025_48514		BIT(3)
+
 /**
  * struct snp_verify_mitigation_dst - mitigation result vectors
  *
@@ -1092,6 +1098,7 @@ void snp_free_firmware_page(void *addr);
 void sev_platform_shutdown(void);
 bool sev_is_snp_ciphertext_hiding_supported(void);
 u64 sev_get_snp_policy_bits(void);
+int sev_firmware_supported_vm_types(void);
 
 #else	/* !CONFIG_CRYPTO_DEV_SP_PSP */
 
@@ -1135,6 +1142,8 @@ static inline void sev_platform_shutdown(void) { }
 
 static inline bool sev_is_snp_ciphertext_hiding_supported(void) { return false; }
 
+static inline int sev_firmware_supported_vm_types(void) { return 0; }
+
 #endif	/* CONFIG_CRYPTO_DEV_SP_PSP */
 
 #endif	/* __PSP_SEV_H__ */
Signature · 1 line
2.53.0
#5 lore raw

Re: [PATCH 3/5] crypto/ccp: export firmware supported vm types

include/linux/psp-sev.h struct sev_data_snp_verify_mitigation { · line 618

Quoted 5 lines · Tycho Andersen
include/linux/psp-sev.h +5 -0
+/*
+ * For CVE-2025-48514 defined in AMD-SB-3023
+ * https://www.amd.com/en/resources/product-security/bulletin/amd-sb-3023.html
+ */
+#define SNP_MIT_VEC_CVE_2025_48514		BIT(3)

It turns out that the public security bulletin is wrong, and this is not the right way to check for this. I will respin the series with a fix.

Tycho

#6 lore raw

PATCH 4/5 kvm/sev: mask off firmware unsupported vm types

In reply to Tycho Andersen

From: "Tycho Andersen (AMD)" <[email protected]>

In some configurations not all VM types are supported by the firmware. Reflect this information in the supported_vm_types that KVM exports.

---
 arch/x86/kvm/svm/sev.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kvm/svm/sev.c +2 -0
index f941d48626d3..eeae39af63a9 100644
@@ -2976,6 +2976,8 @@ void __init sev_set_cpu_caps(void)
 		supported_vm_types |= BIT(KVM_X86_SNP_VM);
 	}
 
+	supported_vm_types &= sev_firmware_supported_vm_types();
+
 	kvm_caps.supported_vm_types |= supported_vm_types;
 }
 
Signature · 1 line
2.53.0
#7 lore raw

Re: [PATCH 4/5] kvm/sev: mask off firmware unsupported vm types

KVM: SEV:

Quoted 17 lines · Tycho Andersen

From: "Tycho Andersen (AMD)" <[email protected]>

In some configurations not all VM types are supported by the firmware. Reflect this information in the supported_vm_types that KVM exports.

---
 arch/x86/kvm/svm/sev.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kvm/svm/sev.c +0 -0
index f941d48626d3..eeae39af63a9 100644

arch/x86/kvm/svm/sev.c void __init sev_set_cpu_caps(void) · line 2981

Quoted 6 lines
arch/x86/kvm/svm/sev.c +2 -0
@@ -2976,6 +2976,8 @@ void __init sev_set_cpu_caps(void)
 		supported_vm_types |= BIT(KVM_X86_SNP_VM);
 	}
 
+	supported_vm_types &= sev_firmware_supported_vm_types();
+
 	kvm_caps.supported_vm_types |= supported_vm_types;

To save one whole line (two, counting whitespace!), and to guard against future changes, I vote for:

	kvm_caps.supported_vm_types |= supported_vm_types &
				       sev_firmware_supported_vm_types();

or if parentheses would make it clearer:

	kvm_caps.supported_vm_types |= (supported_vm_types &
				        sev_firmware_supported_vm_types());

I spent a silly amount of time fiddling with the code to try and avoid the local variable, and failed.

#8 lore raw

PATCH 5/5 selftests/kvm: teach sev_*_test about revoking VM types

In reply to Tycho Andersen

From: "Tycho Andersen (AMD)" <[email protected]>

Instead of using CPUID, use the VM type bit to determine support, since those now reflect the correct status of support by the kernel and firmware configurations.

---
 tools/testing/selftests/kvm/x86/sev_init2_tests.c  | 14 ++++++--------
 .../testing/selftests/kvm/x86/sev_migrate_tests.c  |  2 +-
 tools/testing/selftests/kvm/x86/sev_smoke_test.c   |  4 ++--
 3 files changed, 9 insertions(+), 11 deletions(-)
tools/testing/selftests/kvm/x86/sev_init2_tests.c +6 -8
index b238615196ad..97bd036b4f1c 100644
@@ -136,16 +136,14 @@ int main(int argc, char *argv[])
 		    kvm_check_cap(KVM_CAP_VM_TYPES), 1 << KVM_X86_SEV_VM);
 
 	TEST_REQUIRE(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SEV_VM));
-	have_sev_es = kvm_cpu_has(X86_FEATURE_SEV_ES);
+	have_sev_es = kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SEV_ES_VM);
 
-	TEST_ASSERT(have_sev_es == !!(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SEV_ES_VM)),
-		    "sev-es: KVM_CAP_VM_TYPES (%x) does not match cpuid (checking %x)",
-		    kvm_check_cap(KVM_CAP_VM_TYPES), 1 << KVM_X86_SEV_ES_VM);
+	TEST_ASSERT(!have_sev_es || kvm_cpu_has(X86_FEATURE_SEV_ES),
+		    "sev-es: SEV_ES_VM supported without SEV_ES in CPUID");
 
-	have_snp = kvm_cpu_has(X86_FEATURE_SEV_SNP);
-	TEST_ASSERT(have_snp == !!(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SNP_VM)),
-		    "sev-snp: KVM_CAP_VM_TYPES (%x) indicates SNP support (bit %d), but CPUID does not",
-		    kvm_check_cap(KVM_CAP_VM_TYPES), KVM_X86_SNP_VM);
+	have_snp = kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SNP_VM);
+	TEST_ASSERT(!have_snp || kvm_cpu_has(X86_FEATURE_SEV_SNP),
+		    "sev-snp: SNP_VM supported without SEV_SNP in CPUID");
 
 	test_vm_types();
 
tools/testing/selftests/kvm/x86/sev_migrate_tests.c +1 -1
index 0a6dfba3905b..3f2c3b00e3bc 100644
@@ -376,7 +376,7 @@ int main(int argc, char *argv[])
 
 	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SEV));
 
-	have_sev_es = kvm_cpu_has(X86_FEATURE_SEV_ES);
+	have_sev_es = kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SEV_ES_VM);
 
 	if (kvm_has_cap(KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM)) {
 		test_sev_migrate_from(/* es= */ false);
tools/testing/selftests/kvm/x86/sev_smoke_test.c +2 -2
index 86ad1c7d068f..16ec940de5ac 100644
@@ -219,10 +219,10 @@ int main(int argc, char *argv[])
 
 	test_sev_smoke(guest_sev_code, KVM_X86_SEV_VM, 0);
 
-	if (kvm_cpu_has(X86_FEATURE_SEV_ES))
+	if (kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SEV_ES_VM))
 		test_sev_smoke(guest_sev_es_code, KVM_X86_SEV_ES_VM, SEV_POLICY_ES);
 
-	if (kvm_cpu_has(X86_FEATURE_SEV_SNP))
+	if (kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SNP_VM))
 		test_sev_smoke(guest_snp_code, KVM_X86_SNP_VM, snp_default_policy());
 
 	return 0;
Signature · 1 line
2.53.0
#9 lore raw

Re: [PATCH 5/5] selftests/kvm: teach sev_*_test about revoking VM types

KVM: selftests:

(though selftests/kvm is totally fine, but since you need to respin anyways...)

#10 lore raw
In reply to Tycho Andersen
Quoted 17 lines · Tycho Andersen

From: "Tycho Andersen (AMD)" <[email protected]>

Recent SEV firmware [1] does not support SEV-ES VMs when SNP is enabled. Sean suggested [2] adding an API so that userspace can check for this condition, so do that. Also introduce and use SNP_VERIFY_MITIGATION to determine whether it is present or not.

[1]: https://www.amd.com/en/resources/product-security/bulletin/amd-sb-3023.html
[2]: https://lore.kernel.org/all/[email protected]/

Tycho Andersen (AMD) (5):
kvm/sev: don't expose unusable VM types crypto/ccp: introduce SNP_VERIFY_MITIGATION crypto/ccp: export firmware supported vm types kvm/sev: mask off firmware unsupported vm types selftests/kvm: teach sev_*_test about revoking VM types

Quoted 7 lines
 arch/x86/kvm/svm/sev.c                        | 16 +++-
 drivers/crypto/ccp/sev-dev.c                  | 84 +++++++++++++++++++
 include/linux/psp-sev.h                       | 56 +++++++++++++
 .../selftests/kvm/x86/sev_init2_tests.c       | 14 ++--
 .../selftests/kvm/x86/sev_migrate_tests.c     |  2 +-
 .../selftests/kvm/x86/sev_smoke_test.c        |  4 +-
 6 files changed, 162 insertions(+), 14 deletions(-)

Other than a few nits, this LGTM. Even though the sev-dev.c changes are far more extensive, I would prefer to take the KVM changes through kvm-x86 due to the effective change in KVM's ABI. I'd be happy to carry the whole thing, or use a stable topic branch as a base (patch 1 can easily become patch 3).