Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp21601lqz; Fri, 29 Mar 2024 07:24:38 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXnud1wphrMwpZNcKNMYW1Ou54VFDPNbyiiuO4h2aKvXT/OknsqczmiAZb4cBuvcE7WFYVXi8rvAZDFpYI32xnQI/YqyvNRwg+UKz/grw== X-Google-Smtp-Source: AGHT+IEO8rRhhiWx7vSF67lF4xcdzBbqJs/aKrwHaZweXT8GMqmH2x+MC5vv5w4uzJchX817zdLJ X-Received: by 2002:a25:b599:0:b0:dd1:7128:617b with SMTP id q25-20020a25b599000000b00dd17128617bmr2319180ybj.38.1711722277881; Fri, 29 Mar 2024 07:24:37 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711722277; cv=pass; d=google.com; s=arc-20160816; b=Qf2GuzMQd6tZIAVISGY27xZ9vw8R4sqmdeFprpoEq4oWJPbtJQ4PYWXTofmM2zr1jb bXuA0KYkp8XP2gbuIXCqP1qefnVo1OqfNZVvq1YJ6kcaHUWlS2fPlLtqhhc67A4blqyc z/DB9BU1DDjXwMByA+39/HibhSn0NJB9ND7+L8vM7xp9mggvM7euVT+9GZjAaS0mzeEB Lz9aThZb2pONSwGe8GHxLnKRHH+08tWYEWGeA0jRT7oS5+6JNfNoqCEOZQoIGe5mPySD NzGlA5/w+dRnv6/DMvlKp5yiQsKz0V7fee4VGrfYdAKRagQRVPOPyRNgkjOwOiKwXf2V paFg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=XYwjh0jrnn+BrACBYw5XnLft9hILbKU0hcyNKzu3K/E=; fh=43KPihJnOrhvaJgLfA035CbFOOYGx3Xw97T9BoQlRIA=; b=mlEs7fcPrQTqABk5rW6SZhTHeQIGRaqsL9l06B/ExPAVNtPfAQIejHPE7UkUsiPXY0 cMec3QLChPZR3RmDH0Ow+utLqA/nNqRpsOEQfkc4fDbrv7PeTHRlMfoIgX+dxi1WenWW 8aJ0k0GF/stM3yrNkaKh74pWt39BIEgWrbGWa3H2Z1e8FCTVRfggRO4u3JmlsjlfyzMw CX0LXJo3V9gxpe4Z8CBHr4fGEzPGMRRl8ZH3cN5lbNMX1chqNiey4Icgr2HvMquKQnrP ZeKdFFOUO//bCsfT1tP4TmJsthPEDdm5GCrruTFPxNcHgOCWaVRroYRi5kgcKu+6yjbm EEuQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QaNL0ut3; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-124912-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124912-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id pa32-20020a05620a832000b00789ed3aacdcsi3721151qkn.239.2024.03.29.07.24.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Mar 2024 07:24:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-124912-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QaNL0ut3; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-124912-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124912-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 998001C27B3B for ; Fri, 29 Mar 2024 14:24:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AA20229E1D6; Fri, 29 Mar 2024 12:46:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="QaNL0ut3" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 D05CE29E1C5; Fri, 29 Mar 2024 12:46:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711716418; cv=none; b=PN3Q+Y9DtrkEG9xcJ3UnthjlDV+osA4RKvQ16jbY1Ona4T0kVgaQi8cVPhr9fUu9Z5aH0aSQdvB0DhdMN+Aq7d6g/ZvUQZ/F+FnPIBu3nzmYENP8YjLlpiaedjAqB1sDC84PSPWgWlBvX0L3u0KPO2BBdmPVkjGypCDVbc83Vxk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711716418; c=relaxed/simple; bh=k817z/wiq1ZdzswJB0vLMttW1moO1DxXigH8OAcdidA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VJ5GPwFXsooIz0OxfD0VWXxgkolinqC2SDnOcvCdG2H5LlKoDwrUICZJQjH9R1rOGtAuxsKqkG4DbEq+vnHW0cqc7KaUNv/CfKHfwLqXD01pM1Oloe6nW+p3+i/S/e+avyBxh1XnyPHP6Te7wB68z/m6lSI/i7zyRjfbmZqN4GE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QaNL0ut3; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id E4D10C43390; Fri, 29 Mar 2024 12:46:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711716418; bh=k817z/wiq1ZdzswJB0vLMttW1moO1DxXigH8OAcdidA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QaNL0ut39GfeY9cNKBFVDg1JkZr6u0+tgt9hUplhYQXVZnchEkSb9NA77RQa4UXFF Ve3sX28q3dM2j4oq5VwgE3XfNGRb0GOLCDQynpb+o9S5RBbyR+VuLJwJzzWA+Eha3J Qc1bPAGP45gHjuNo6hoX7P3O1n6iwZrAlosdbZCTwuMn3ryuJikpmRlZntEoM4vrcR OurigfXh+6e4eavFjZ5IQoeo71oxtL6L8BYSdAGgLK04WHHbvzfA+77J+knaAjeSjE QpNhsr5K2KfUEL0wT9/WX5MAj4O8Psthk4Uu6yH8jd1cnU0sG+MBeDvuUivf6MgqUS 5U1KruezeiU7g== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Rick Edgecombe , Christoph Hellwig , Sasha Levin , m.szyprowski@samsung.com, iommu@lists.linux.dev Subject: [PATCH AUTOSEL 6.1 28/52] dma-direct: Leak pages on dma_set_decrypted() failure Date: Fri, 29 Mar 2024 08:45:22 -0400 Message-ID: <20240329124605.3091273-28-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240329124605.3091273-1-sashal@kernel.org> References: <20240329124605.3091273-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.1.83 Content-Transfer-Encoding: 8bit From: Rick Edgecombe [ Upstream commit b9fa16949d18e06bdf728a560f5c8af56d2bdcaf ] On TDX it is possible for the untrusted host to cause set_memory_encrypted() or set_memory_decrypted() to fail such that an error is returned and the resulting memory is shared. Callers need to take care to handle these errors to avoid returning decrypted (shared) memory to the page allocator, which could lead to functional or security issues. DMA could free decrypted/shared pages if dma_set_decrypted() fails. This should be a rare case. Just leak the pages in this case instead of freeing them. Signed-off-by: Rick Edgecombe Signed-off-by: Christoph Hellwig Signed-off-by: Sasha Levin --- kernel/dma/direct.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c index 63859a101ed83..d4215739efc71 100644 --- a/kernel/dma/direct.c +++ b/kernel/dma/direct.c @@ -296,7 +296,7 @@ void *dma_direct_alloc(struct device *dev, size_t size, } else { ret = page_address(page); if (dma_set_decrypted(dev, ret, size)) - goto out_free_pages; + goto out_leak_pages; } memset(ret, 0, size); @@ -317,6 +317,8 @@ void *dma_direct_alloc(struct device *dev, size_t size, out_free_pages: __dma_direct_free_pages(dev, page, size); return NULL; +out_leak_pages: + return NULL; } void dma_direct_free(struct device *dev, size_t size, @@ -379,12 +381,11 @@ struct page *dma_direct_alloc_pages(struct device *dev, size_t size, ret = page_address(page); if (dma_set_decrypted(dev, ret, size)) - goto out_free_pages; + goto out_leak_pages; memset(ret, 0, size); *dma_handle = phys_to_dma_direct(dev, page_to_phys(page)); return page; -out_free_pages: - __dma_direct_free_pages(dev, page, size); +out_leak_pages: return NULL; } -- 2.43.0