From patchwork Tue Oct 17 20:25:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 154499 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp4381121vqb; Tue, 17 Oct 2023 13:26:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHLUS/QsRXz+SWotIVGAETcOkoVKGx8DCBCTnVo8uVCmBrOr5M9IBHKXO/tGCIYc6cVOHEZ X-Received: by 2002:a17:90b:1b44:b0:274:60c7:e15a with SMTP id nv4-20020a17090b1b4400b0027460c7e15amr3541958pjb.4.1697574397171; Tue, 17 Oct 2023 13:26:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697574397; cv=none; d=google.com; s=arc-20160816; b=H5nOBGxzrKePO4qzxY5S3q3gXlCRUffjqUp8m9oNueXz9slLcjLOQRzytegxazFlb0 hTAVgZwQ9zmIo4XrAO9WMPVHIwluj9Vr6LMEyiA9hFwrMTQ6+Qdo6Cwxew9UNoO09p+T 2HeMlDNVEIaEFxv1zZ6hJoIA0K9f0eUm7TWJcLO+Dnueqmr3qqQPH4E9AF3xwTXThkWp 52+HGGJcNhUQ/+GNOha4A/uiYb2w+8pjVpgvIkUc53TtsuEgLI/ClaJ5z0I72/l38Ik/ hQJxJozcKz/3+JzcXafd/RO3fu251fHTUFInF4sR9JUtgtpCHpeMpVvUIXmL/2huOpBu gA4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=XBsimUVzuD3nEV3NZdlfEVSzq/vsyq4vsE/zMpmgFSA=; fh=SYvEGz8seZYs1sK9WnZjLPVTgNFsGd9AM19ibQrXG3o=; b=Ptd68HKHhjPACeN9zKYMk+dypm6ocp+pSnXOgqfYvQgleqQeaBLVlHA/ioGQuMUlLS 762ajmmgEaG8cR+e9+xpAwgUTsTToDGIsEtbxisOcBrGXT9jfPrgf8UZtLrcIIMh/iHj EHt4BggBEXNZo3yqhfXuXaSxH2qRrz97I1nzcwbG0EGqbTprKwwWEmCI3dnbx4qfDi38 vJUSt26XD3m+tkBSSQvKU8GEIBT7twK707MJHPAaQgh+chDkqUpfOgN6KF/i5UYK9i1S Sc9EBGVyd0GffFfU74FWCeHCpKaB1vcMWgpk4b2sd4ceJ1v0v5hU0aTiidbSo/CFKlRs INuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ibyLT5Me; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id v8-20020a17090a6b0800b00262f937bf93si2466409pjj.77.2023.10.17.13.26.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 13:26:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ibyLT5Me; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id AA33E8088A95; Tue, 17 Oct 2023 13:26:31 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235121AbjJQUZz (ORCPT + 21 others); Tue, 17 Oct 2023 16:25:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344429AbjJQUZh (ORCPT ); Tue, 17 Oct 2023 16:25:37 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2023FF; Tue, 17 Oct 2023 13:25:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1697574336; x=1729110336; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DeK9M9T8xLg82lbSzsd9PGh1edTcS0OzTNpNIQkWt+o=; b=ibyLT5MepQhobD67/052nfhe6pYBEQu/Oy3Bz4PQaXwxoNNmUsmzruXB 2JPoCZAuETRn+kV2/hlOIGcSyYiXnaUswIaSF2sLKUwmiDjLAOg1dmWPo Sqq1GjHNS8STK5wZMyEe1a07nKuHWJSYa5ngdAihkYtDajbUVsnMg3VF3 yjGWnGMICqR3JPuwu+uYdamomDxnCuokp3b9kAgRM9lCmpGbYWuuu3OIf LRvwnG3p7I/8VTAz9aLtOi4nOFsAdtPqgtNxEDHHHfcqH8JHIS6lwBX1Y 1ZmhTDMShiAWHq5g6mdmSDRWwF8ydRUtrOjByStvvBPm9jASHivRKQ5xe g==; X-IronPort-AV: E=McAfee;i="6600,9927,10866"; a="7429573" X-IronPort-AV: E=Sophos;i="6.03,233,1694761200"; d="scan'208";a="7429573" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Oct 2023 13:25:36 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10866"; a="900040468" X-IronPort-AV: E=Sophos;i="6.03,233,1694761200"; d="scan'208";a="900040468" Received: from rtdinh-mobl1.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.212.150.155]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Oct 2023 13:23:32 -0700 From: Rick Edgecombe To: x86@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com, luto@kernel.org, peterz@infradead.org, kirill.shutemov@linux.intel.com, elena.reshetova@intel.com, isaku.yamahata@intel.com, seanjc@google.com, Michael Kelley , thomas.lendacky@amd.com, decui@microsoft.com, sathyanarayanan.kuppuswamy@linux.intel.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org Cc: rick.p.edgecombe@intel.com, "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , linux-hyperv@vger.kernel.org Subject: [RFC 07/10] hv: Use free_decrypted_pages() Date: Tue, 17 Oct 2023 13:25:02 -0700 Message-Id: <20231017202505.340906-8-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231017202505.340906-1-rick.p.edgecombe@intel.com> References: <20231017202505.340906-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 17 Oct 2023 13:26:31 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780035771058718688 X-GMAIL-MSGID: 1780035771058718688 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. Hyperv could free decrypted/shared pages if set_memory_decrypted() fails. Use the recently added free_decrypted_pages() to avoid this. Only compile tested. Cc: "K. Y. Srinivasan" Cc: Haiyang Zhang Cc: Wei Liu Cc: Dexuan Cui Cc: linux-hyperv@vger.kernel.org Signed-off-by: Rick Edgecombe --- drivers/hv/channel.c | 7 ++++--- drivers/hv/connection.c | 13 +++++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/drivers/hv/channel.c b/drivers/hv/channel.c index 56f7e06c673e..1ad8f7fabe06 100644 --- a/drivers/hv/channel.c +++ b/drivers/hv/channel.c @@ -153,9 +153,10 @@ void vmbus_free_ring(struct vmbus_channel *channel) hv_ringbuffer_cleanup(&channel->inbound); if (channel->ringbuffer_page) { - __free_pages(channel->ringbuffer_page, - get_order(channel->ringbuffer_pagecount - << PAGE_SHIFT)); + int order = get_order(channel->ringbuffer_pagecount << PAGE_SHIFT); + unsigned long addr = (unsigned long)page_address(channel->ringbuffer_page); + + free_decrypted_pages(addr, order); channel->ringbuffer_page = NULL; } } diff --git a/drivers/hv/connection.c b/drivers/hv/connection.c index 3cabeeabb1ca..cffad9b139d3 100644 --- a/drivers/hv/connection.c +++ b/drivers/hv/connection.c @@ -315,6 +315,7 @@ int vmbus_connect(void) void vmbus_disconnect(void) { + int ret; /* * First send the unload request to the host. */ @@ -337,11 +338,15 @@ 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); + ret = set_memory_encrypted((unsigned long)vmbus_connection.monitor_pages[0], 1); + ret |= set_memory_encrypted((unsigned long)vmbus_connection.monitor_pages[1], 1); - hv_free_hyperv_page(vmbus_connection.monitor_pages[0]); - hv_free_hyperv_page(vmbus_connection.monitor_pages[1]); + if (!ret) { + hv_free_hyperv_page(vmbus_connection.monitor_pages[0]); + hv_free_hyperv_page(vmbus_connection.monitor_pages[1]); + } else { + WARN_ONCE(1, "Failed to re-encrypt memory before freeing, leaking pages!\n"); + } vmbus_connection.monitor_pages[0] = NULL; vmbus_connection.monitor_pages[1] = NULL; }