Skip to contentSkip to conversation

SEV re-initialization fixes

PATCH Superseded Tycho Andersen 2 participants 11 messages Apr 27 – Apr 29

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover SEV re-initialization fixes

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

Here is a series targeted at fixing the bug where HSAVE_PA is cleared after other VMs are running that Sashiko pointed out.

The first three patches are relatively straightforward, I think :). The last one is an ABI break from how things used to work.

Thanks,

Tycho

Tycho Andersen (AMD) (4):
crypto/ccp: Do not initialize SNP for SEV ioctls crypto/ccp: Do not initialize SNP for ioctl(SNP_COMMIT) crypto/ccp: Do not initialize SNP for ioctl(SNP_VLEK_LOAD) crypto/ccp: Do not initialize SNP for ioctl(SNP_CONFIG)

 drivers/crypto/ccp/sev-dev.c | 70 ++++++------------------------------
 1 file changed, 11 insertions(+), 59 deletions(-)

base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731

Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/4 crypto/ccp: Do not initialize SNP for SEV ioctls

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

Sashiko notes:

Quoted 5 lines

if SEV initialization fails and KVM is actively running normal VMs, could a userspace process trigger this code path via /dev/sev ioctls (e.g., SEV_PDH_GEN) and zero out MSR_VM_HSAVE_PA globally? Would the next VMRUN execution for an active VM trigger a general protection fault and crash the host?

sev_move_to_init_state() is called for ioctls requiring only SEV firmware:
SEV_PEK_GEN, SEV_PDH_GEN, SEV_PEK_CSR, SEV_PEK_CERT_IMPORT, and SEV_PDH_CERT_EXPORT. After the firmware command, it does SEV_SHUTDOWN on the SEV firmware. Since these commands do not require SNP to be initialized, skip it by calling __sev_platform_init_locked() which only initializes the SEV firmware. This way SNP is not Initialized at all, and HSAVE_PA is not cleared.

The previous code saved any SEV initialization firmware error to init_args.error and then threw it away and hardcoded the return value of INVALID_PLATFORM_STATE regardless of the real firmware error. This patch changes it to surface the underlying error, which is hopefully both more useful and doesn't cause any problems.

Note that it is still safe to call __sev_firmware_shutdown() directly: it calls __sev_snp_shutdown_locked(), which skips SNP shutdown if SNP was not initialized.

Fixes ceac7fb89e8d ("crypto: ccp - Ensure implicit SEV/SNP init and shutdown in ioctls")
Reported-by Sashiko
Assisted-by Gemini:gemini-3.1-pro-preview
---
 drivers/crypto/ccp/sev-dev.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/crypto/ccp/sev-dev.c +2 -5
index d1e9e0ac63b6..6891b90bbb88 100644
@@ -1716,14 +1716,11 @@ static int sev_get_platform_state(int *state, int *error)
 
 static int sev_move_to_init_state(struct sev_issue_cmd *argp, bool *shutdown_required)
 {
-	struct sev_platform_init_args init_args = {0};
 	int rc;
 
-	rc = _sev_platform_init_locked(&init_args);
-	if (rc) {
-		argp->error = SEV_RET_INVALID_PLATFORM_STATE;
+	rc = __sev_platform_init_locked(&argp->error);
+	if (rc)
 		return rc;
-	}
 
 	*shutdown_required = true;
 
Signature · 1 line
2.53.0
#4 lore raw

PATCH 2/4 crypto/ccp: Do not initialize SNP for ioctl(SNP_COMMIT)

In reply to Tycho Andersen

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

Sashiko notes:

Quoted 5 lines

if SEV initialization fails and KVM is actively running normal VMs, could a userspace process trigger this code path via /dev/sev ioctls (e.g., SEV_PDH_GEN) and zero out MSR_VM_HSAVE_PA globally? Would the next VMRUN execution for an active VM trigger a general protection fault and crash the host?

The SNP_COMMIT command does not require the firmware to be in any particular state. Skip initializing it if it was previously uninitialized.

The SEV-SNP firmware specification doc 56860 does not mention SNP_COMMIT in Table 5 as a command that is allowed in the UNINIT state, but it is in fact allowed and a future documentation update will reflect that.

Fixes ceac7fb89e8d ("crypto: ccp - Ensure implicit SEV/SNP init and shutdown in ioctls")
Reported-by Sashiko
Assisted-by Gemini:gemini-3.1-pro-preview
---
 drivers/crypto/ccp/sev-dev.c | 13 +------------
 1 file changed, 1 insertion(+), 12 deletions(-)
drivers/crypto/ccp/sev-dev.c +1 -12
index 6891b90bbb88..572f06368d4b 100644
@@ -2437,24 +2437,13 @@ static int sev_ioctl_do_snp_platform_status(struct sev_issue_cmd *argp)
 
 static int sev_ioctl_do_snp_commit(struct sev_issue_cmd *argp)
 {
-	struct sev_device *sev = psp_master->sev_data;
 	struct sev_data_snp_commit buf;
-	bool shutdown_required = false;
-	int ret, error;
-
-	if (!sev->snp_initialized) {
-		ret = snp_move_to_init_state(argp, &shutdown_required);
-		if (ret)
-			return ret;
-	}
+	int ret;
 
 	buf.len = sizeof(buf);
 
 	ret = __sev_do_cmd_locked(SEV_CMD_SNP_COMMIT, &buf, &argp->error);
 
-	if (shutdown_required)
-		__sev_snp_shutdown_locked(&error, false);
-
 	return ret;
 }
 
Signature · 1 line
2.53.0
#6 lore raw

PATCH 3/4 crypto/ccp: Do not initialize SNP for ioctl(SNP_VLEK_LOAD)

In reply to Tycho Andersen

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

Sashiko notes:

Quoted 5 lines

if SEV initialization fails and KVM is actively running normal VMs, could a userspace process trigger this code path via /dev/sev ioctls (e.g., SEV_PDH_GEN) and zero out MSR_VM_HSAVE_PA globally? Would the next VMRUN execution for an active VM trigger a general protection fault and crash the host?

The SEV firmware docs for SNP_VLEK_LOAD note:

Quoted 1 line

On SNP_SHUTDOWN, the VLEK is deleted.

That is, the initialization/shutdown wrapper here is pointless, because the firmware immediately throws away the key anyway. Instead, refuse to do anything if SNP has not been previously initialized.

Fixes ceac7fb89e8d ("crypto: ccp - Ensure implicit SEV/SNP init and shutdown in ioctls")
Reported-by Sashiko
Assisted-by Gemini:gemini-3.1-pro-preview
---
 drivers/crypto/ccp/sev-dev.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)
drivers/crypto/ccp/sev-dev.c +4 -13
index 572f06368d4b..e8c3ac6d989a 100644
@@ -2481,9 +2481,8 @@ static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable)
 {
 	struct sev_device *sev = psp_master->sev_data;
 	struct sev_user_data_snp_vlek_load input;
-	bool shutdown_required = false;
-	int ret, error;
 	void *blob;
+	int ret;
 
 	if (!argp->data)
 		return -EINVAL;
@@ -2497,6 +2496,9 @@ static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable)
 	if (input.len != sizeof(input) || input.vlek_wrapped_version != 0)
 		return -EINVAL;
 
+	if (!sev->snp_initialized)
+		return -EINVAL;
+
 	blob = psp_copy_user_blob(input.vlek_wrapped_address,
 				  sizeof(struct sev_user_data_snp_wrapped_vlek_hashstick));
 	if (IS_ERR(blob))
@@ -2504,18 +2506,7 @@ static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable)
 
 	input.vlek_wrapped_address = __psp_pa(blob);
 
-	if (!sev->snp_initialized) {
-		ret = snp_move_to_init_state(argp, &shutdown_required);
-		if (ret)
-			goto cleanup;
-	}
-
 	ret = __sev_do_cmd_locked(SEV_CMD_SNP_VLEK_LOAD, &input, &argp->error);
-
-	if (shutdown_required)
-		__sev_snp_shutdown_locked(&error, false);
-
-cleanup:
 	kfree(blob);
 
 	return ret;
Signature · 1 line
2.53.0
#9 lore raw

PATCH 4/4 crypto/ccp: Do not initialize SNP for ioctl(SNP_CONFIG)

In reply to Tycho Andersen

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

Sashiko notes:

Quoted 5 lines

if SEV initialization fails and KVM is actively running normal VMs, could a userspace process trigger this code path via /dev/sev ioctls (e.g., SEV_PDH_GEN) and zero out MSR_VM_HSAVE_PA globally? Would the next VMRUN execution for an active VM trigger a general protection fault and crash the host?

Refuse to re-try initialization if SNP is not already initialized for SNP_CONFIG.

This is technically an ABI break: before if SNP initialization failed it could be transparently retriggered by this ioctl, and if no VMs were running, everything worked fine. Hopefully this is enough of a corner case that nobody will notice, but someone does, there are a few options:

* do something like symbol_get() for kvm and refuse to initialize if KVM is loaded
* check each cpu's HSAVE_PA for non-zero data before re-initializing
* once initialization has failed, continue to refuse to initialize until the ccp module is unloaded

Fixes ceac7fb89e8d ("crypto: ccp - Ensure implicit SEV/SNP init and shutdown in ioctls")
Reported-by Sashiko
Assisted-by Gemini:gemini-3.1-pro-preview
---
 drivers/crypto/ccp/sev-dev.c | 33 ++++-----------------------------
 1 file changed, 4 insertions(+), 29 deletions(-)
drivers/crypto/ccp/sev-dev.c +4 -29
index e8c3ac6d989a..5b113908a4f9 100644
@@ -1727,21 +1727,6 @@ static int sev_move_to_init_state(struct sev_issue_cmd *argp, bool *shutdown_req
 	return 0;
 }
 
-static int snp_move_to_init_state(struct sev_issue_cmd *argp, bool *shutdown_required)
-{
-	int error, rc;
-
-	rc = __sev_snp_init_locked(&error, 0);
-	if (rc) {
-		argp->error = SEV_RET_INVALID_PLATFORM_STATE;
-		return rc;
-	}
-
-	*shutdown_required = true;
-
-	return 0;
-}
-
 static int sev_ioctl_do_reset(struct sev_issue_cmd *argp, bool writable)
 {
 	int state, rc;
@@ -2451,8 +2436,6 @@ static int sev_ioctl_do_snp_set_config(struct sev_issue_cmd *argp, bool writable
 {
 	struct sev_device *sev = psp_master->sev_data;
 	struct sev_user_data_snp_config config;
-	bool shutdown_required = false;
-	int ret, error;
 
 	if (!argp->data)
 		return -EINVAL;
@@ -2460,21 +2443,13 @@ static int sev_ioctl_do_snp_set_config(struct sev_issue_cmd *argp, bool writable
 	if (!writable)
 		return -EPERM;
 
+	if (!sev->snp_initialized)
+		return -EINVAL;
+
 	if (copy_from_user(&config, (void __user *)argp->data, sizeof(config)))
 		return -EFAULT;
 
-	if (!sev->snp_initialized) {
-		ret = snp_move_to_init_state(argp, &shutdown_required);
-		if (ret)
-			return ret;
-	}
-
-	ret = __sev_do_cmd_locked(SEV_CMD_SNP_CONFIG, &config, &argp->error);
-
-	if (shutdown_required)
-		__sev_snp_shutdown_locked(&error, false);
-
-	return ret;
+	return __sev_do_cmd_locked(SEV_CMD_SNP_CONFIG, &config, &argp->error);
 }
 
 static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable)
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH v1 1/4] crypto/ccp: Do not initialize SNP for SEV ioctls

In reply to Tycho Andersen
Quoted 28 lines · Tycho Andersen

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

Sashiko notes:

Quoted 5 lines

if SEV initialization fails and KVM is actively running normal VMs, could a userspace process trigger this code path via /dev/sev ioctls (e.g., SEV_PDH_GEN) and zero out MSR_VM_HSAVE_PA globally? Would the next VMRUN execution for an active VM trigger a general protection fault and crash the host?

sev_move_to_init_state() is called for ioctls requiring only SEV firmware:
SEV_PEK_GEN, SEV_PDH_GEN, SEV_PEK_CSR, SEV_PEK_CERT_IMPORT, and SEV_PDH_CERT_EXPORT. After the firmware command, it does SEV_SHUTDOWN on the SEV firmware. Since these commands do not require SNP to be initialized, skip it by calling __sev_platform_init_locked() which only initializes the SEV firmware. This way SNP is not Initialized at all, and HSAVE_PA is not cleared.

The previous code saved any SEV initialization firmware error to init_args.error and then threw it away and hardcoded the return value of INVALID_PLATFORM_STATE regardless of the real firmware error. This patch changes it to surface the underlying error, which is hopefully both more useful and doesn't cause any problems.

Note that it is still safe to call __sev_firmware_shutdown() directly: it calls __sev_snp_shutdown_locked(), which skips SNP shutdown if SNP was not initialized.

Quoted 5 lines
Fixes ceac7fb89e8d ("crypto: ccp - Ensure implicit SEV/SNP init and shutdown in ioctls")
Reported-by Sashiko
Assisted-by Gemini:gemini-3.1-pro-preview

I have a similar patch that I hadn't gotten out that added an argument to _sev_platform_init_locked() to skip/prevent SNP initialization. I wonder if adding something to sev_platform_init_args would be better? This could then be expanded to prevent SNP initialization if the KVM sev_snp module parameter was set to false.

But for a fix, this is probably simpler. It does skip some of the checks that _sev_platform_init_locked() has, but I think all of the checks that matter are performed for the paths that call sev_move_to_init_state().

Should this go to stable?

Quoted 25 lines
---
 drivers/crypto/ccp/sev-dev.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/crypto/ccp/sev-dev.c +2 -5
index d1e9e0ac63b6..6891b90bbb88 100644
@@ -1716,14 +1716,11 @@ static int sev_get_platform_state(int *state, int *error)
 
 static int sev_move_to_init_state(struct sev_issue_cmd *argp, bool *shutdown_required)
 {
-	struct sev_platform_init_args init_args = {0};
 	int rc;
 
-	rc = _sev_platform_init_locked(&init_args);
-	if (rc) {
-		argp->error = SEV_RET_INVALID_PLATFORM_STATE;
+	rc = __sev_platform_init_locked(&argp->error);
+	if (rc)
 		return rc;
-	}
 
 	*shutdown_required = true;
 
maintainer Reviewed-by on v1 2/4
#5 lore raw

Re: [PATCH v1 2/4] crypto/ccp: Do not initialize SNP for ioctl(SNP_COMMIT)

In reply to Tycho Andersen
Quoted 17 lines · Tycho Andersen

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

Sashiko notes:

Quoted 5 lines

if SEV initialization fails and KVM is actively running normal VMs, could a userspace process trigger this code path via /dev/sev ioctls (e.g., SEV_PDH_GEN) and zero out MSR_VM_HSAVE_PA globally? Would the next VMRUN execution for an active VM trigger a general protection fault and crash the host?

The SNP_COMMIT command does not require the firmware to be in any particular state. Skip initializing it if it was previously uninitialized.

The SEV-SNP firmware specification doc 56860 does not mention SNP_COMMIT in Table 5 as a command that is allowed in the UNINIT state, but it is in fact allowed and a future documentation update will reflect that.

Quoted 5 lines
Fixes ceac7fb89e8d ("crypto: ccp - Ensure implicit SEV/SNP init and shutdown in ioctls")
Reported-by Sashiko
Assisted-by Gemini:gemini-3.1-pro-preview

Stable?

Quoted 34 lines
---
 drivers/crypto/ccp/sev-dev.c | 13 +------------
 1 file changed, 1 insertion(+), 12 deletions(-)
drivers/crypto/ccp/sev-dev.c +1 -12
index 6891b90bbb88..572f06368d4b 100644
@@ -2437,24 +2437,13 @@ static int sev_ioctl_do_snp_platform_status(struct sev_issue_cmd *argp)
 
 static int sev_ioctl_do_snp_commit(struct sev_issue_cmd *argp)
 {
-	struct sev_device *sev = psp_master->sev_data;
 	struct sev_data_snp_commit buf;
-	bool shutdown_required = false;
-	int ret, error;
-
-	if (!sev->snp_initialized) {
-		ret = snp_move_to_init_state(argp, &shutdown_required);
-		if (ret)
-			return ret;
-	}
+	int ret;
 
 	buf.len = sizeof(buf);
 
 	ret = __sev_do_cmd_locked(SEV_CMD_SNP_COMMIT, &buf, &argp->error);
 
-	if (shutdown_required)
-		__sev_snp_shutdown_locked(&error, false);
-
 	return ret;
 }
 
maintainer
#7 lore raw

Re: [PATCH v1 3/4] crypto/ccp: Do not initialize SNP for ioctl(SNP_VLEK_LOAD)

In reply to Tycho Andersen
Quoted 18 lines · Tycho Andersen

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

Sashiko notes:

Quoted 5 lines

if SEV initialization fails and KVM is actively running normal VMs, could a userspace process trigger this code path via /dev/sev ioctls (e.g., SEV_PDH_GEN) and zero out MSR_VM_HSAVE_PA globally? Would the next VMRUN execution for an active VM trigger a general protection fault and crash the host?

The SEV firmware docs for SNP_VLEK_LOAD note:

Quoted 1 line

On SNP_SHUTDOWN, the VLEK is deleted.

That is, the initialization/shutdown wrapper here is pointless, because the firmware immediately throws away the key anyway. Instead, refuse to do anything if SNP has not been previously initialized.

Quoted 5 lines
Fixes ceac7fb89e8d ("crypto: ccp - Ensure implicit SEV/SNP init and shutdown in ioctls")
Reported-by Sashiko
Assisted-by Gemini:gemini-3.1-pro-preview

Stable?

Minor comments below.

Quoted 20 lines
---
 drivers/crypto/ccp/sev-dev.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)
drivers/crypto/ccp/sev-dev.c +1 -2
index 572f06368d4b..e8c3ac6d989a 100644
@@ -2481,9 +2481,8 @@ static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable)
 {
 	struct sev_device *sev = psp_master->sev_data;
 	struct sev_user_data_snp_vlek_load input;
-	bool shutdown_required = false;
-	int ret, error;
 	void *blob;
+	int ret;
 
 	if (!argp->data)
 		return -EINVAL;

drivers/crypto/ccp/sev-dev.c static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable) · line 2501

Quoted 6 lines
drivers/crypto/ccp/sev-dev.c +3 -0
@@ -2497,6 +2496,9 @@ static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable)
 	if (input.len != sizeof(input) || input.vlek_wrapped_version != 0)
 		return -EINVAL;
 
+	if (!sev->snp_initialized)
+		return -EINVAL;
+

Should this be moved up to avoid the copy_from_user()?

And should something other than -EINVAL be used, maybe -ENODEV, to help distinguish the error a bit?

Thanks,
Tom

Quoted 22 lines
drivers/crypto/ccp/sev-dev.c +0 -11
 	blob = psp_copy_user_blob(input.vlek_wrapped_address,
 				  sizeof(struct sev_user_data_snp_wrapped_vlek_hashstick));
 	if (IS_ERR(blob))
@@ -2504,18 +2506,7 @@ static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable)
 
 	input.vlek_wrapped_address = __psp_pa(blob);
 
-	if (!sev->snp_initialized) {
-		ret = snp_move_to_init_state(argp, &shutdown_required);
-		if (ret)
-			goto cleanup;
-	}
-
 	ret = __sev_do_cmd_locked(SEV_CMD_SNP_VLEK_LOAD, &input, &argp->error);
-
-	if (shutdown_required)
-		__sev_snp_shutdown_locked(&error, false);
-
-cleanup:
 	kfree(blob);
 
 	return ret;
maintainer
#10 lore raw

Re: [PATCH v1 4/4] crypto/ccp: Do not initialize SNP for ioctl(SNP_CONFIG)

In reply to Tycho Andersen
Quoted 10 lines · Tycho Andersen

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

Sashiko notes:

Quoted 5 lines

if SEV initialization fails and KVM is actively running normal VMs, could a userspace process trigger this code path via /dev/sev ioctls (e.g., SEV_PDH_GEN) and zero out MSR_VM_HSAVE_PA globally? Would the next VMRUN execution for an active VM trigger a general protection fault and crash the host?

Quoted 7 lines

Refuse to re-try initialization if SNP is not already initialized for SNP_CONFIG.

This is technically an ABI break: before if SNP initialization failed it could be transparently retriggered by this ioctl, and if no VMs were running, everything worked fine. Hopefully this is enough of a corner case that nobody will notice, but someone does, there are a few options:

Isn't this the same with patch #3? Not sure why it is only called out for this one.

Quoted 52 lines

* do something like symbol_get() for kvm and refuse to initialize if KVM is loaded
* check each cpu's HSAVE_PA for non-zero data before re-initializing
* once initialization has failed, continue to refuse to initialize until the ccp module is unloaded

Fixes ceac7fb89e8d ("crypto: ccp - Ensure implicit SEV/SNP init and shutdown in ioctls")
Reported-by Sashiko
Assisted-by Gemini:gemini-3.1-pro-preview
---
 drivers/crypto/ccp/sev-dev.c | 33 ++++-----------------------------
 1 file changed, 4 insertions(+), 29 deletions(-)
drivers/crypto/ccp/sev-dev.c +0 -17
index e8c3ac6d989a..5b113908a4f9 100644
@@ -1727,21 +1727,6 @@ static int sev_move_to_init_state(struct sev_issue_cmd *argp, bool *shutdown_req
 	return 0;
 }
 
-static int snp_move_to_init_state(struct sev_issue_cmd *argp, bool *shutdown_required)
-{
-	int error, rc;
-
-	rc = __sev_snp_init_locked(&error, 0);
-	if (rc) {
-		argp->error = SEV_RET_INVALID_PLATFORM_STATE;
-		return rc;
-	}
-
-	*shutdown_required = true;
-
-	return 0;
-}
-
 static int sev_ioctl_do_reset(struct sev_issue_cmd *argp, bool writable)
 {
 	int state, rc;
@@ -2451,8 +2436,6 @@ static int sev_ioctl_do_snp_set_config(struct sev_issue_cmd *argp, bool writable
 {
 	struct sev_device *sev = psp_master->sev_data;
 	struct sev_user_data_snp_config config;
-	bool shutdown_required = false;
-	int ret, error;
 
 	if (!argp->data)
 		return -EINVAL;

drivers/crypto/ccp/sev-dev.c static int sev_ioctl_do_snp_set_config(struct sev_issue_cmd *argp, bool writable · line 2447

Quoted 5 lines
drivers/crypto/ccp/sev-dev.c +2 -0
@@ -2460,21 +2443,13 @@ static int sev_ioctl_do_snp_set_config(struct sev_issue_cmd *argp, bool writable
 	if (!writable)
 		return -EPERM;
 
+	if (!sev->snp_initialized)
+		return -EINVAL;

Maybe -ENODEV to distinguish this situation?

Thanks,
Tom

Quoted 20 lines
drivers/crypto/ccp/sev-dev.c +2 -12
+
 	if (copy_from_user(&config, (void __user *)argp->data, sizeof(config)))
 		return -EFAULT;
 
-	if (!sev->snp_initialized) {
-		ret = snp_move_to_init_state(argp, &shutdown_required);
-		if (ret)
-			return ret;
-	}
-
-	ret = __sev_do_cmd_locked(SEV_CMD_SNP_CONFIG, &config, &argp->error);
-
-	if (shutdown_required)
-		__sev_snp_shutdown_locked(&error, false);
-
-	return ret;
+	return __sev_do_cmd_locked(SEV_CMD_SNP_CONFIG, &config, &argp->error);
 }
 
 static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable)
#3 lore raw

Re: [PATCH v1 1/4] crypto/ccp: Do not initialize SNP for SEV ioctls

In reply to Tom Lendacky
Quoted 35 lines · Tom Lendacky
Quoted 28 lines · Tycho Andersen

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

Sashiko notes:

Quoted 5 lines

if SEV initialization fails and KVM is actively running normal VMs, could a userspace process trigger this code path via /dev/sev ioctls (e.g., SEV_PDH_GEN) and zero out MSR_VM_HSAVE_PA globally? Would the next VMRUN execution for an active VM trigger a general protection fault and crash the host?

sev_move_to_init_state() is called for ioctls requiring only SEV firmware:
SEV_PEK_GEN, SEV_PDH_GEN, SEV_PEK_CSR, SEV_PEK_CERT_IMPORT, and SEV_PDH_CERT_EXPORT. After the firmware command, it does SEV_SHUTDOWN on the SEV firmware. Since these commands do not require SNP to be initialized, skip it by calling __sev_platform_init_locked() which only initializes the SEV firmware. This way SNP is not Initialized at all, and HSAVE_PA is not cleared.

The previous code saved any SEV initialization firmware error to init_args.error and then threw it away and hardcoded the return value of INVALID_PLATFORM_STATE regardless of the real firmware error. This patch changes it to surface the underlying error, which is hopefully both more useful and doesn't cause any problems.

Note that it is still safe to call __sev_firmware_shutdown() directly: it calls __sev_snp_shutdown_locked(), which skips SNP shutdown if SNP was not initialized.

Quoted 5 lines
Fixes ceac7fb89e8d ("crypto: ccp - Ensure implicit SEV/SNP init and shutdown in ioctls")
Reported-by Sashiko
Assisted-by Gemini:gemini-3.1-pro-preview
Quoted 5 lines

I have a similar patch that I hadn't gotten out that added an argument to _sev_platform_init_locked() to skip/prevent SNP initialization. I wonder if adding something to sev_platform_init_args would be better? This could then be expanded to prevent SNP initialization if the KVM sev_snp module parameter was set to false.

Yeah, I will also need additional params to init_args here:
https://lore.kernel.org/all/[email protected]/
so I think adding it there makes sense.

Quoted 5 lines

But for a fix, this is probably simpler. It does skip some of the checks that _sev_platform_init_locked() has, but I think all of the checks that matter are performed for the paths that call sev_move_to_init_state().

Should this go to stable?

Yes, they all should as you point out, I'll add that for v2.

Quoted 1 line

Thanks,

Tycho

#8 lore raw

Re: [PATCH v1 3/4] crypto/ccp: Do not initialize SNP for ioctl(SNP_VLEK_LOAD)

In reply to Tom Lendacky
Quoted 19 lines · Tom Lendacky
Quoted 19 lines
drivers/crypto/ccp/sev-dev.c +1 -2
index 572f06368d4b..e8c3ac6d989a 100644
@@ -2481,9 +2481,8 @@ static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable)
 {
 	struct sev_device *sev = psp_master->sev_data;
 	struct sev_user_data_snp_vlek_load input;
-	bool shutdown_required = false;
-	int ret, error;
 	void *blob;
+	int ret;
 
 	if (!argp->data)
 		return -EINVAL;
@@ -2497,6 +2496,9 @@ static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable)
 	if (input.len != sizeof(input) || input.vlek_wrapped_version != 0)
 		return -EINVAL;
 
Quoted 5 lines

drivers/crypto/ccp/sev-dev.c static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable) · line 2501

Quoted 3 lines
drivers/crypto/ccp/sev-dev.c +3 -0
+	if (!sev->snp_initialized)
+		return -EINVAL;
+

Should this be moved up to avoid the copy_from_user()?

Yep, I can do that.

Quoted 2 lines

And should something other than -EINVAL be used, maybe -ENODEV, to help distinguish the error a bit?

As you noted in patch 4, this is an ABI break as well. We could return 0 here and make it not an ABI break. Given that any use of this is almost certainly a bug, though, I think -ENODEV is good, I'll change it to that.

Tycho