From mboxrd@z Thu Jan 1 00:00:00 1970 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 C9BAA16EBF2; Mon, 22 Apr 2024 23:57:02 +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=1713830222; cv=none; b=nd/XZ88avGSF1un8zCMII6YjTG9do6HVRhelXW5jbkYpQvsDqFwaQ8uGesXS3fPeGk/yXPeEZnOiBNKsePSXsdKhQKYz4VaReX2arQVKmK26UvoRvP7F0T0aYx6trUAV9dIQeWLFqYFfoBQH76w2zEUs8D4pdg2/hbKqxtnGNd4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713830222; c=relaxed/simple; bh=2ohdiVvEIOrEQz0rntP/pWeXmB6943ltgofx1Ntyu8Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EkdGjc4kDKPcgOizvLdnl6AbkVEU0yt9Sqd8TqMC99+9+jt4hGtYwzpKeqUembsC7IoV9nDXuNv7ECduof9NNnT/BEO5Sm+W5orTVG0/hct5bYyD7inVIsgl85y8AEY4NEihUT+JDoydnzetKvJZVmzK1Zo3h/RECOfdrTs8Zv8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kyo4zpL8; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="kyo4zpL8" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 22544C113CC; Mon, 22 Apr 2024 23:57:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713830222; bh=2ohdiVvEIOrEQz0rntP/pWeXmB6943ltgofx1Ntyu8Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kyo4zpL8JR9sbDoZV95wzEL5nStsJdjXTZWPZ7ccwYdiH592F5nuLgLoaiA4cYxPg m8tkNRt+3lDOADtaX3JBNmWQ5sd2B3klpt25di9vn44i3XvUHsdnD6co/EkotYnuZT y+MvQbVAG19N0O4O3RTlk2ux10JFdD0G57uMwDGDUImuX8wUgFEVGME4TCn/GsMhn9 leayAbxO/wqj/ENUY9LQlJcsIybKxpVI49E8WQERVn9Y5A3EmRrg2hUKyrX4NdSqOE HlGtehXhsyfBKcu0ukTlGosdVyAF4D/+bYWulCpW5gajK9a1KWS72Q1U6KDLgQsNhy 6K3hlwcPJ9VQw== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Rick Edgecombe , Michael Kelley , Kuppuswamy Sathyanarayanan , "Kirill A . Shutemov" , Wei Liu , Sasha Levin , kys@microsoft.com, haiyangz@microsoft.com, decui@microsoft.com, linux-hyperv@vger.kernel.org Subject: [PATCH AUTOSEL 6.6 23/29] Drivers: hv: vmbus: Leak pages if set_memory_encrypted() fails Date: Mon, 22 Apr 2024 19:17:04 -0400 Message-ID: <20240422231730.1601976-23-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240422231730.1601976-1-sashal@kernel.org> References: <20240422231730.1601976-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.6.28 Content-Transfer-Encoding: 8bit From: Rick Edgecombe [ Upstream commit 03f5a999adba062456c8c818a683beb1b498983a ] In CoCo VMs 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. VMBus code could free decrypted pages if set_memory_encrypted()/decrypted() fails. Leak the pages if this happens. Signed-off-by: Rick Edgecombe Signed-off-by: Michael Kelley Reviewed-by: Kuppuswamy Sathyanarayanan Acked-by: Kirill A. Shutemov Link: https://lore.kernel.org/r/20240311161558.1310-2-mhklinux@outlook.com Signed-off-by: Wei Liu Message-ID: <20240311161558.1310-2-mhklinux@outlook.com> Signed-off-by: Sasha Levin --- drivers/hv/connection.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/drivers/hv/connection.c b/drivers/hv/connection.c index 3cabeeabb1cac..f001ae880e1db 100644 --- a/drivers/hv/connection.c +++ b/drivers/hv/connection.c @@ -237,8 +237,17 @@ int vmbus_connect(void) vmbus_connection.monitor_pages[0], 1); ret |= set_memory_decrypted((unsigned long) vmbus_connection.monitor_pages[1], 1); - if (ret) + if (ret) { + /* + * If set_memory_decrypted() fails, the encryption state + * of the memory is unknown. So leak the memory instead + * of risking returning decrypted memory to the free list. + * For simplicity, always handle both pages the same. + */ + vmbus_connection.monitor_pages[0] = NULL; + vmbus_connection.monitor_pages[1] = NULL; goto cleanup; + } /* * Set_memory_decrypted() will change the memory contents if @@ -337,13 +346,19 @@ void vmbus_disconnect(void) vmbus_connection.int_page = NULL; } - set_memory_encrypted((unsigned long)vmbus_connection.monitor_pages[0], 1); - set_memory_encrypted((unsigned long)vmbus_connection.monitor_pages[1], 1); + if (vmbus_connection.monitor_pages[0]) { + if (!set_memory_encrypted( + (unsigned long)vmbus_connection.monitor_pages[0], 1)) + hv_free_hyperv_page(vmbus_connection.monitor_pages[0]); + vmbus_connection.monitor_pages[0] = NULL; + } - hv_free_hyperv_page(vmbus_connection.monitor_pages[0]); - hv_free_hyperv_page(vmbus_connection.monitor_pages[1]); - vmbus_connection.monitor_pages[0] = NULL; - vmbus_connection.monitor_pages[1] = NULL; + if (vmbus_connection.monitor_pages[1]) { + if (!set_memory_encrypted( + (unsigned long)vmbus_connection.monitor_pages[1], 1)) + hv_free_hyperv_page(vmbus_connection.monitor_pages[1]); + vmbus_connection.monitor_pages[1] = NULL; + } } /* -- 2.43.0