Linux cryptographic layer development
 help / color / mirror / Atom feed
* [PATCH] crypto: ccp - Do not hand a short INIT_EX file to the firmware
@ 2026-11-26  9:20 Gokul K
  2026-11-26  9:29 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Gokul K @ 2026-11-26  9:20 UTC (permalink / raw)
  To: Ashish Kalra, Tom Lendacky, John Allen, Herbert Xu,
	David S . Miller, linux-crypto
  Cc: Sean Christopherson, David Rientjes, Peter Gonda, Atish Patra,
	linux-kernel

sev_read_init_ex_file() reads the INIT_EX non-volatile state from
init_ex_path into sev_init_ex_buffer. When the read returns anything other
than NV_LENGTH it logs the fact and then returns 0, so the caller carries
on and __sev_init_ex_locked() passes the buffer to the PSP as the
platform's saved state.

For a truncated file that buffer holds a prefix of the old contents
followed by the zeroes the page was allocated with, which is not a state
the firmware ever wrote. A negative return from kernel_read() is treated
the same way, and then the buffer holds nothing but zeroes while the code
still reports success.

This is not what the interface promises. Documentation/virt/kvm/x86/
amd-memory-encryption.rst says of init_ex_path:

  If the file specified by ``init_ex_path`` does not exist or is invalid,
  the OS will create or override the file with PSP non-volatile storage.

A file that cannot be read in full is invalid, so discard what was read
and let INIT_EX run from a zeroed buffer, exactly as it does when the file
does not exist yet. The firmware then repopulates the buffer and
sev_write_init_ex_file_if_required() writes it back, which is the
"override" half of the documented behavior.

Doing this for a negative kernel_read() return means a transient read
error costs the platform its persisted state rather than failing the
init. That is the deliberate trade: the documented contract allows the
file to be overridden, and feeding the firmware a buffer it did not write
is worse than starting over.

This was found while looking at the INIT_EX failure reported by Sean
Christopherson, where an interrupted writeback leaves the platform
unusable. It addresses only the case of a file that is short or unreadable
on the next init. An interrupted write leaves a full-length file holding a
mix of old and new bytes, which a length check cannot detect, so the
reported failure is not fully fixed by this patch.

Fixes: 3d725965f836 ("crypto: ccp - Add SEV_INIT_EX support")
Reported-by: Sean Christopherson <seanjc@google.com>
Closes: https://lore.kernel.org/all/abSWm4wyY91I4zf8@google.com/
Signed-off-by: Gokul K <gokul02k@gmail.com>
---
Compile-tested only.  I have AMD hardware but not a working SEV INIT_EX
setup, so the path this touches has not been exercised on a machine.  Built
at W=1 with CONFIG_CRYPTO_DEV_SP_PSP=y, and confirmed sev-dev.o is not built
with it =n.

What this does not cover, and I would rather say so up front than have it
found in review: the failure in the report starts with an interrupted
kernel_write() in sev_write_init_ex_file().  That file is opened without
O_TRUNC, so a partial overwrite leaves a full-length file whose head is new
and whose tail is stale.  Such a file passes the length check this patch
adds and still reaches the firmware.  Making the write side leave either
nothing or something detectably invalid behind is a separate change, and I
did not want to bundle an untested design decision with this one.

I also have no explanation yet for the widespread memory corruption in the
report.  A zeroed or stale NV buffer should not produce corrupted page
tables, so I assume something else is going on there and this patch does
not claim to address it.

If anyone with an INIT_EX setup is willing to run the reproducer from the
report against this, I would appreciate it, and I am happy to follow up on
the write side.

 drivers/crypto/ccp/sev-dev.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c
index f833cb7e4da3..6d7ea028d24f 100644
--- a/drivers/crypto/ccp/sev-dev.c
+++ b/drivers/crypto/ccp/sev-dev.c
@@ -302,14 +302,23 @@ static int sev_read_init_ex_file(void)
 	}
 
 	nread = kernel_read(fp, sev_init_ex_buffer, NV_LENGTH, NULL);
+	filp_close(fp, NULL);
+
 	if (nread != NV_LENGTH) {
 		dev_info(sev->dev,
 			"SEV: could not read %u bytes to non volatile memory area, ret %ld\n",
 			NV_LENGTH, nread);
+		/*
+		 * Whatever was read is not what the firmware last wrote, so
+		 * none of it can be handed back to the firmware. Discard it
+		 * and let INIT_EX start from a zeroed buffer, which is what
+		 * happens when the file does not exist yet.
+		 */
+		memset(sev_init_ex_buffer, 0, NV_LENGTH);
+		return 0;
 	}
 
 	dev_dbg(sev->dev, "SEV: read %ld bytes from NV file\n", nread);
-	filp_close(fp, NULL);
 
 	return 0;
 }
-- 
2.54.0



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] crypto: ccp - Do not hand a short INIT_EX file to the firmware
  2026-11-26  9:20 [PATCH] crypto: ccp - Do not hand a short INIT_EX file to the firmware Gokul K
@ 2026-11-26  9:29 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-26  9:29 UTC (permalink / raw)
  To: Gokul K, Ashish Kalra, Tom Lendacky, John Allen, Herbert Xu,
	David S . Miller, linux-crypto
  Cc: Sean Christopherson, David Rientjes, Peter Gonda, Atish Patra

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] crypto: ccp: inaccurate Fixes trailer for
  sev_read_init_ex_file() short read
--

commit c74101dd406e137499f025e32571e23c036489af
Author: Gokul K <gokul02k@gmail.com>

> crypto: ccp - Do not hand a short INIT_EX file to the firmware
>
> sev_read_init_ex_file() reads the INIT_EX non-volatile state from
> init_ex_path into sev_init_ex_buffer. When the read returns anything other
> than NV_LENGTH it logs the fact and then returns 0, so the caller carries
> on and __sev_init_ex_locked() passes the buffer to the PSP as the
> platform's saved state.
>
> For a truncated file that buffer holds a prefix of the old contents
> followed by the zeroes the page was allocated with, which is not a state
> the firmware ever wrote. A negative return from kernel_read() is treated
> the same way, and then the buffer holds nothing but zeroes while the code
> still reports success.
>
> This is not what the interface promises. Documentation/virt/kvm/x86/
> amd-memory-encryption.rst says of init_ex_path:
>
>   If the file specified by ``init_ex_path`` does not exist or is invalid,
>   the OS will create or override the file with PSP non-volatile storage.
>
> A file that cannot be read in full is invalid, so discard what was read
> and let INIT_EX run from a zeroed buffer, exactly as it does when the file
> does not exist yet. The firmware then repopulates the buffer and
> sev_write_init_ex_file_if_required() writes it back, which is the
> "override" half of the documented behavior.
>
> Doing this for a negative kernel_read() return means a transient read
> error costs the platform its persisted state rather than failing the
> init. That is the deliberate trade: the documented contract allows the
> file to be overridden, and feeding the firmware a buffer it did not write
> is worse than starting over.
>
> This was found while looking at the INIT_EX failure reported by Sean
> Christopherson, where an interrupted writeback leaves the platform
> unusable. It addresses only the case of a file that is short or unreadable
> on the next init. An interrupted write leaves a full-length file holding a
> mix of old and new bytes, which a length check cannot detect, so the
> reported failure is not fully fixed by this patch.
>
> Fixes: 3d725965f836 ("crypto: ccp - Add SEV_INIT_EX support")
> Reported-by: Sean Christopherson <seanjc@google.com>
> Closes: https://lore.kernel.org/all/abSWm4wyY91I4zf8@google.com/
> Signed-off-by: Gokul K <gokul02k@gmail.com>

[Severity: Low]
In commit 3d725965f836 ("crypto: ccp - Add SEV_INIT_EX support"),
sev_read_init_ex_file() returned -EIO when kernel_read() did not return
NV_LENGTH, so __sev_init_ex_locked() aborted before issuing
SEV_CMD_INIT_EX to the firmware.

Returning 0 on a short or failed read while leaving partial contents in
sev_init_ex_buffer was introduced by commit d8da2da21fdb ("crypto: ccp -
Initialize PSP when reading psp data file failed"). Should the Fixes: tag
cite that commit instead:

  Fixes: d8da2da21fdb ("crypto: ccp - Initialize PSP when reading psp data file failed")

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126092001.2099312-1-gokul02k@gmail.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-26  9:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26  9:20 [PATCH] crypto: ccp - Do not hand a short INIT_EX file to the firmware Gokul K
2026-11-26  9:29 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox