From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.47 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796103008; b=00l9p4+KmD9p3kW6K048+dBhVe16V18sI+ud9Sz7TRcjkMms92IDwVeqgWn0Va85+Dfz SCGrmLUgpxFqvHEunMhr5eTcmVIX7ls0qO79uf3wytDh9/GSqTY7RcOvelwR6ZQJCafFZ hIZedRyNUFJEuQ6EMRBBgfjzA0GziErN2iBGn8MfZR2le1F20PygnH0foGL4hpiFCg5if Mv86OPQouqjYhAe9A9IZhepk2O86mU8jyThUOJdLCOiUo5HyK/rDuviCat1QkIJ8e6l0Y seBxS/+5TBRCB5oyrQ4ZlqU1/erQfmoB5pI54rlCpOhHY6HUjnLVCLNtPbmhOLlAt3g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796103008; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=TTGfsqH1JcjlbPK4svPq/LveUAZmomuOSbVslUPFfGQ=; b=mJ5NOgWMNxc6Y0e6XP/c0g6i05ipi+pajZOPcbfrKbUrMGChA3R2P/pUZZ5uApzwaiCL UU4ARHFfyyPfPLQ2J7KytflrNHGXV3ZWdDzkccF3u3SQNfVQ5Y1S6Ex099F9AOaf6tyoC RYAmykbsgq3oQ9R9DB2HFvr9kP8u4jmKZOUBke0lL/au74pj9kCF57kI2IiyDmBklai0I 6TEsqbJp2ncBsbtqZJB9hQgdPiZk3LBbraXak0u8KxgrDnBcseuF+riKprVIo4q60g8wa d0oSLmkeUiglCqqrY6VOJi93PFUhlz3hWntSoenPnXsebVAZm1Iiam3s0acgKcXa/Kg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=0Ca8f+2U; arc=none smtp.remote-ip=74.125.82.47 Received: from mail-dl1-f47.google.com (mail-dl1-f47.google.com [74.125.82.47]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 26B08606A334 for ; Tue, 1 Dec 2026 05:30:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 26B08606A334 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=0Ca8f+2U Received: by mail-dl1-f47.google.com with SMTP id a92af1059eb24-1b5b910c838so1410611c88.1 for ; Mon, 30 Nov 2026 21:30:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796103007; x=1796707807; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=TTGfsqH1JcjlbPK4svPq/LveUAZmomuOSbVslUPFfGQ=; b=0Ca8f+2UKtYVs6aA/ISa4q/oOxD2BQicIMhHusxGc8KIdHDD6sZQJkFs4uU/86+U72 tbVzN2VEaeCBhhNyngoq2XJXjCyFUQxrvqpEksFLYvimSHD2NWebiXoP9X65V2jmhJ/5 Tld6q07Pey8AqahiMPSZjT7WpzaJgiky1Q/I8JchqENubd28Ilg/SIaR3ZaLMx12ciGJ 1Hu6lPiZH6YfFQeJru883WxvIokl0B+O+a2icCva98RxRXOgBIiEPxIyMTCBeWpY5ylb s2EB25FyiDCh58RiwL70rRJTQ2umfVp4UvaTYJ/DPx95XQv5v0a8s48xwO+OpOl24J+n d9nA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796103007; x=1796707807; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=TTGfsqH1JcjlbPK4svPq/LveUAZmomuOSbVslUPFfGQ=; b=ls+oxMtBeZEyKqz4BqGRbCcooPbyPQMSJhYz0ld+uqHp6RcMERpgLJbdNsGB2MRjtB k7t9JgA5WglRuJZUtj2YsQrA9N/IsYkhIFu3e9yMsZDRF3GBKIgx2bqmbwzsyu59n4Az gkHdnt5W37ivMI8syH2zLmLWI3zr9sS4On4U3OLLcL8W5+rswH+1QYyyUt0/CVZC2zgq +juxD/1Kksr4auy/wF20nFNtkiLw1OAyiybmS+XPljDRa5riz9Ahfc+++RjwU5zy7VMT /h1xPKjcJcw10fKF4wmp6BgBDLaTiWaI63XGPlnfl67Dek5why8v6BI2Thw+H96YXmed ZU1g== X-Forwarded-Encrypted: i=1; AL+hErEotIiO12uJzznEUQFL5zuRgEQpjbBLnGs1A9XfXzUcfQiV1OR1yleS8yBjhSMrb0kU5PY/bzsCHBxj+Cc=@vger.kernel.org X-Gm-Message-State: APuU4Z82CFIB1rurniiSHVZngAiHmJJtSa9VqQulUi7x9zUv+G2H9Cha Q0HjM4YTmg5DbCW4eFQWJnAr318q6ev+5LHrkGqwwFf/PG7xnV1X7ZJY X-Gm-Gg: AZFdTZfZ0OTqlu+QVUSGXz9V38rJPgVUVAqGdvLxzs60zSK8FCUV/oq4YoE9n3TTdw+ DeRvjPY9+vJkI+TsqZaNl9P+air21rfi22zpx9kzo9Q3c3sGnaSyN426WYxID8VGdA47Lf2flzm 5HkaVcDy7UIfFsUeFDv1WAIF28Gl7464UTxFttJ5BfsbJ1DxzBOfnqQC7ClYKdp8umpySi4ZZeN IRSzoeHKUNgrDnE1KQzZZxctkGgYpHO/YPAlmIv3OxCmUI0pvE+pYpGbglmEPimcnhWQ0U2Jo2K B+xMaWTaS4vBLLTNZe+fNx4CNSoIuMk7ycQ0mLubR2sMx3QbGchntEm1NtMOP7JClmSeuLOynl+ wPjKDB6IUVkxdmRVj7COd3SYLITXFJxqaW9A9iJJN0/PYInB+qIcupnoJTS0RBBC8y23vBo/JTB 17E4wUEfJaV4rYsbjuOkmy47WjgR+9cRVEOTUFH9JuZv3XgZEyVziMFCqY68QmcG/QqE+YbL+2M +RQtRwoQDfREbelAXtB/uqjodqhfpGAiTgpU7Rt5MvDhVhGQzaiI9fr+jBmBL3bG6z7SRTPhIRe O0T7IwUMlEnWTECQ8K8Mk4y0oeWU1U17Uf6qXOove0K0OTnlSznWsiUD3CP0sIg= X-Received: by 2002:a05:7300:1486:b0:369:d8e1:7bb6 with SMTP id 5a478bee46e88-38570e24c35mr48239067eec.25.1796103006978; Mon, 30 Nov 2026 21:30:06 -0800 (PST) Received: from 10-86-27-207.ban-spse ([165.204.217.251]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-385672c6f31sm103101247eec.15.2026.11.30.21.30.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 21:30:06 -0800 (PST) From: Gokul K To: Ashish Kalra , Tom Lendacky , John Allen , Herbert Xu , "David S . Miller" , linux-crypto@vger.kernel.org Cc: Sean Christopherson , David Rientjes , Peter Gonda , Jacky Li , Atish Patra , linux-kernel@vger.kernel.org, Tom Lendacky Subject: [PATCH v2] crypto: ccp - Do not hand a short INIT_EX file to the firmware Date: Tue, 1 Dec 2026 10:59:47 +0530 Message-ID: <20261201052947.2306233-1-gokul02k@gmail.com> X-Mailer: git-send-email 2.54.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 dates from commit d8da2da21fdb ("crypto: ccp - Initialize PSP when reading psp data file failed"), which stopped a failed read from failing the init, so that the PSP is still initialized, but left whatever had been read in the buffer. Before it, a short read returned -EIO. 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: d8da2da21fdb ("crypto: ccp - Initialize PSP when reading psp data file failed") Reported-by: Sean Christopherson Closes: https://lore.kernel.org/all/abSWm4wyY91I4zf8@google.com/ Tested-by: Tom Lendacky Signed-off-by: Gokul K --- Changes in v2: - Fixes: now names d8da2da21fdb. 3d725965f836 still returned -EIO on a short read; d8da2da21fdb is the commit that dropped it (sashiko-bot). The commit message says so in a new paragraph. - Shortened the comment as Tom suggested, saying INIT_EX rather than SNP_INIT_EX, since this buffer is passed to SEV_CMD_INIT_EX. - Kept Tom's Tested-by: the code is unchanged apart from that comment. - Cc Jacky Li, author of d8da2da21fdb. v1: https://lore.kernel.org/all/20261126092001.2099312-1-gokul02k@gmail.com/ Tom Lendacky tested v1. I built v2 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. I am happy to follow up on the write side. drivers/crypto/ccp/sev-dev.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index f833cb7e4da3..e3554449c477 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -302,14 +302,22 @@ 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); + /* + * A full file wasn't read, resulting in a buffer containing + * invalid state, so let INIT_EX start with a zeroed buffer, + * similar to when the file doesn't exist. + */ + 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