From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-182.mta0.migadu.com (out-182.mta0.migadu.com [91.218.175.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 46F2539061D for ; Tue, 2 Jun 2026 18:17:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780424247; cv=none; b=TKgCAV4W1jgyLvxA0qNr9IqdO1KVWUslsbeM0AWgde2uZuqAoCVYFMesVt1/wYZ7YWSpFcKvMhzLOeb6Zx58v4bk+3wCa8nMm+5ELGhbYVJ8cB7kCi4cS3KuUoeroWn/WdF3ewdG15xrShQupX0So62PToEF4xTrQVsP7vPJAhQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780424247; c=relaxed/simple; bh=lKrbErFL33y0hvZLmkIS71x1sxFvJ5v/SI1TpWYA9xA=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=i/eEFufLxbfoWVg3mpfWTGrLRYSiIlUITgZdY6toxl8uFkHv+dSk9FhxRMQLS8xHA1vdKT/gEWeOzsVXPifn2Fah4/p5mZsSn18IagoygYngqQxGhUAJy9ZzByxl8P39V42ipPKF2e0pbx9gBd856cAbxi0sHI1UAQxOxDahefA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=BB8/dpvx; arc=none smtp.client-ip=91.218.175.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="BB8/dpvx" Message-ID: <9f134eb4-1c64-47c4-8c4d-feb3c75072e6@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1780424234; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KwNAHhnYjbAwxb3CujRs87uODn/mS6bwBRlR99Zbexw=; b=BB8/dpvxkI7ABBOHbbeUMG9d/OHBj/j+bxj8pt9mKsrjGzrzANg73shTc6xoqaD/+aPP0Y vjKYgCbcV3WOG7slgBbAE1+AzCYXkkR0MeM4lA+LI99GrQz1UM1pgr8frkxXrK2lpQb7mG ZbKZwEmW1VvzZbsEBSBae6FKovSch1A= Date: Tue, 2 Jun 2026 11:17:06 -0700 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [PATCH v2 4/4] crypto: ccp: Fix memory leak in SEV INIT_EX path To: Tom Lendacky , Sean Christopherson , Paolo Bonzini , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Gonda , Brijesh Singh , Youngjae Lee , Ashish Kalra , Michael Roth , John Allen , Herbert Xu Cc: clm@meta.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, stable@vger.kernel.org, Atish Patra , Sashiko References: <20260601-sev_snp_fixes-v2-0-611891b28a86@meta.com> <20260601-sev_snp_fixes-v2-4-611891b28a86@meta.com> Content-Language: en-US X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Atish Patra In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT On 6/2/26 7:54 AM, Tom Lendacky wrote: > On 6/1/26 18:04, Atish Patra wrote: >> From: Atish Patra >> >> allocated pages in _init_ext_path are never freed and sev_init_ex_buffer >> is left pointing at the leaked memory in case of any failures during the >> function.. >> >> Fix by adding an error path that frees the pages and clears >> sev_init_ex_buffer. Make sure we only free the memory if the failure >> happens before the conversion. Otherwise, we may end up trying to free >> up converted pages in case of reclaim failure. rmp_mark_pages_firmware >> failures should be rare enough to avoid more code complexity to track >> down which pages were reclaimed/leaked vs which are not. >> >> Fixes: 7364a6fbca45 ("crypto: ccp: Handle non-volatile INIT_EX data when SNP is enabled") >> >> Reported-by: Sashiko >> Signed-off-by: Atish Patra > Not sure the goto's are the best, but they do the job - just a personal > preference for me here. > > The new comment below is a bit verbose, I would think it is sufficient to > just say something like "Pages can be in an inconsistent state, don't > release them back to the system" or such. Sure. I will update the comment. > It might be nice in the future if we can identify if the reclaim was > successful and use that for determining whether the pages are safe to > freed... but the failure chance should be practically zero, so I'm not > sure it is worth it. Yes. I had started that path but was not sure if the code churn is worth it. I can send it as a separate patch and we can take a call if you are think it's worth. > > Reviewed-by: Tom Lendacky > >> --- >> drivers/crypto/ccp/sev-dev.c | 16 ++++++++++++++-- >> 1 file changed, 14 insertions(+), 2 deletions(-) >> >> diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c >> index 3d4793e8e34b..8566f164430b 100644 >> --- a/drivers/crypto/ccp/sev-dev.c >> +++ b/drivers/crypto/ccp/sev-dev.c >> @@ -1550,7 +1550,7 @@ static int __sev_platform_init_handle_init_ex_path(struct sev_device *sev) >> >> rc = sev_read_init_ex_file(); >> if (rc) >> - return rc; >> + goto err_free; >> >> /* If SEV-SNP is initialized, transition to firmware page. */ >> if (sev->snp_initialized) { >> @@ -1559,11 +1559,23 @@ static int __sev_platform_init_handle_init_ex_path(struct sev_device *sev) >> npages = 1UL << get_order(NV_LENGTH); >> if (rmp_mark_pages_firmware(__pa(sev_init_ex_buffer), npages, true)) { >> dev_err(sev->dev, "SEV: INIT_EX NV memory page state change failed.\n"); >> - return -ENOMEM; >> + rc = -ENOMEM; >> + /* >> + * Don't free on conversion failure: the rollback may >> + * have left pages firmware-owned, and a high-order >> + * block can't be partially freed. >> + */ >> + goto err_reset; >> } >> } >> >> return 0; >> + >> +err_free: >> + __free_pages(page, get_order(NV_LENGTH)); >> +err_reset: >> + sev_init_ex_buffer = NULL; >> + return rc; >> } >> >> static int __sev_platform_init_locked(int *error) >>