SEV re-initialization fixes
Subsystems (4)
v2 cover SEV re-initialization fixes
From: "Tycho Andersen (AMD)" <[email protected]>
Here is a v2 of the HSAVE_PA clearing fixes. Changes are:
* return ENODEV instead EINVAL for the re-init cases
* note ABI breakage in patch 3's commit log
* CC stable on all the patches
v1 is here: https://lore.kernel.org/all/[email protected]/
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: 7fd2df204f342fc17d1a0bfcd474b24232fb0f32
Signature · 1 line
2.54.0
PATCH v2 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/sevioctls (e.g.,SEV_PDH_GEN) and zero outMSR_VM_HSAVE_PAglobally? 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.
--- drivers/crypto/ccp/sev-dev.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/crypto/ccp/sev-dev.c +2 -5
@@ -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.54.0
PATCH v2 2/4 crypto/ccp: Do not initialize SNP for ioctl(SNP_COMMIT)
In reply to Tycho AndersenFrom: "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/sevioctls (e.g.,SEV_PDH_GEN) and zero outMSR_VM_HSAVE_PAglobally? 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.
--- drivers/crypto/ccp/sev-dev.c | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-)
drivers/crypto/ccp/sev-dev.c +1 -12
@@ -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.54.0
PATCH v2 3/4 crypto/ccp: Do not initialize SNP for ioctl(SNP_VLEK_LOAD)
In reply to Tycho AndersenFrom: "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/sevioctls (e.g.,SEV_PDH_GEN) and zero outMSR_VM_HSAVE_PAglobally? 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.
This is an ABI break: before, this was a no-op and almost certainly a mistake by userspace, and now it returns -ENODEV. ABI compatibility could be maintained here by simply returning 0 in the check instead.
--- drivers/crypto/ccp/sev-dev.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-)
drivers/crypto/ccp/sev-dev.c +4 -13
@@ -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 ;
@@ -2491,6 +2490,9 @@ static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable)if (!writable)return -EPERM ; +if (!sev->snp_initialized) +return -ENODEV ; +if (copy_from_user (&input,u64_to_user_ptr (argp->data),sizeof (input)))return -EFAULT ;
@@ -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.54.0
Re: [PATCH v2 3/4] crypto/ccp: Do not initialize SNP for ioctl(SNP_VLEK_LOAD)
Quoted 22 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/sevioctls (e.g.,SEV_PDH_GEN) and zero outMSR_VM_HSAVE_PAglobally? 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_LOADnote: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.
This is an ABI break: before, this was a no-op and almost certainly a mistake by userspace, and now it returns
-ENODEV. ABI compatibility could be maintained here by simply returning 0 in the check instead.
Quoted 6 lines
Fixes ceac7fb89e8d ("crypto: ccp - Ensure implicit SEV/SNP init and shutdown in ioctls")Reported-by SashikoAssisted-by Gemini:gemini-3.1-pro-previewCC <[email protected]>Signed-off-by Tycho Andersen (AMD) <[email protected]>
Quoted 48 lines
--- drivers/crypto/ccp/sev-dev.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-)drivers/crypto/ccp/sev-dev.c +4
-13@@ -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 ;@@ -2491,6 +2490,9 @@ static int sev_ioctl_do_snp_vlek_load(struct sev_issue_cmd *argp, bool writable)if (!writable)return -EPERM ; +if (!sev->snp_initialized) +return -ENODEV ; +if (copy_from_user (&input,u64_to_user_ptr (argp->data),sizeof (input)))return -EFAULT ;@@ -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;
PATCH v2 4/4 crypto/ccp: Do not initialize SNP for ioctl(SNP_CONFIG)
In reply to Tycho AndersenFrom: "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/sevioctls (e.g.,SEV_PDH_GEN) and zero outMSR_VM_HSAVE_PAglobally? 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
--- drivers/crypto/ccp/sev-dev.c | 33 ++++----------------------------- 1 file changed, 4 insertions(+), 29 deletions(-)
drivers/crypto/ccp/sev-dev.c +4 -29
@@ -1727,21 +1727,6 @@ static int sev_move_to_init_state(struct sev_issue_cmd *argp, bool *shutdown_reqreturn 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 writableif (!writable)return -EPERM ; +if (!sev->snp_initialized) +return -ENODEV ; +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.54.0
Re: [PATCH v2 4/4] crypto/ccp: Do not initialize SNP for ioctl(SNP_CONFIG)
Quoted 24 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/sevioctls (e.g.,SEV_PDH_GEN) and zero outMSR_VM_HSAVE_PAglobally? 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'sHSAVE_PAfor non-zero data before re-initializing
* once initialization has failed, continue to refuse to initialize until the ccp module is unloaded
Quoted 6 lines
Fixes ceac7fb89e8d ("crypto: ccp - Ensure implicit SEV/SNP init and shutdown in ioctls")Reported-by SashikoAssisted-by Gemini:gemini-3.1-pro-previewCC <[email protected]>Signed-off-by Tycho Andersen (AMD) <[email protected]>
Quoted 65 lines
--- drivers/crypto/ccp/sev-dev.c | 33 ++++----------------------------- 1 file changed, 4 insertions(+), 29 deletions(-)drivers/crypto/ccp/sev-dev.c +4
-29@@ -1727,21 +1727,6 @@ static int sev_move_to_init_state(struct sev_issue_cmd *argp, bool *shutdown_reqreturn 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 writableif (!writable)return -EPERM ; +if (!sev->snp_initialized) +return -ENODEV ; +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)
Herbert Xu
maintainerQuoted 20 lines · Tycho Andersen
From: "Tycho Andersen (AMD)" <[email protected]>
Here is a v2 of the
HSAVE_PAclearing fixes. Changes are:* return ENODEV instead EINVAL for the re-init cases
* note ABI breakage in patch 3's commit log
* CC stable on all the patchesv1 is here: https://lore.kernel.org/all/[email protected]/
Thanks,
Tycho
Tycho Andersen (AMD) (4):
crypto/ccp: Do not initialize SNP for SEV ioctlscrypto/ccp: Do not initialize SNP forioctl(SNP_COMMIT)crypto/ccp: Do not initialize SNP forioctl(SNP_VLEK_LOAD)crypto/ccp: Do not initialize SNP forioctl(SNP_CONFIG)
Quoted 7 lines
drivers/crypto/ccp/sev-dev.c | 70 ++++++------------------------------ 1 file changed, 11 insertions(+), 59 deletions(-)base-commit:
7fd2df204f342fc17d1a0bfcd474b24232fb0f32Signature · 1 line
2.54.0
All applied. Thanks.
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt