From patchwork Tue Oct 17 20:25:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13426030 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4979BCDB474 for ; Tue, 17 Oct 2023 20:25:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C7D8B80070; Tue, 17 Oct 2023 16:25:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C08CB8006D; Tue, 17 Oct 2023 16:25:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9BD7280070; Tue, 17 Oct 2023 16:25:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 7A6548006D for ; Tue, 17 Oct 2023 16:25:41 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 5A88DA0E61 for ; Tue, 17 Oct 2023 20:25:41 +0000 (UTC) X-FDA: 81356084082.24.1FFD24F Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by imf04.hostedemail.com (Postfix) with ESMTP id 58BEC4001A for ; Tue, 17 Oct 2023 20:25:39 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=bAwYhPFD; spf=pass (imf04.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.198.163.7 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1697574339; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=cQn22U+CaFNzglZA9drV6DWVhDvn+L3lzTGWP/p2/NY=; b=RWMMPPpv91qWCGoKqQPLyGtibwSP9jZtlsI6iiF1XldMJ00ZnNSmx/lQX4Hp+A0NaR+5ut W5D9LBhMEFFAY6vaMF2FXVSGP0fmpATh4XJ8hUjw25zZ5cUi+F3edhvZ4h/OI4PNBAakhQ TK2zV6gjwaxVDqeoPM6pORRNgvLpHUw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1697574339; a=rsa-sha256; cv=none; b=LnBwfTuRVOCEOHfq9RBjxU5f5uDDetlt2cIcySFtcepfJEch3x0Hfrf7CfL87WDGClDRsT uQSze9S1L2lR4f45bm4a7nCtzloXCO3ipiT2cfWy9SUeXCgUAiHopC2M4WSxOCHnLWgE1g k7jPu2YwNCI39VVQZPlDnPdmD7RZpjA= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=bAwYhPFD; spf=pass (imf04.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.198.163.7 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1697574339; x=1729110339; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=acK3P8RbFbLJ3Y/qsHvg8A7W0ea+PDycu9fLk45ERVg=; b=bAwYhPFDDn2o4/g5Fs2i2s5um2j26EQWQIyBaZJ2aaH4c46DESSmKzjG m8qgnDnrDqfr9zfj/plTmMMnQ9zukuTursZT/jQ40fKZMUrMtz/at8UjB 2l4v9tWYfqrSphZ+M3Cau2AbRLqI7cmk5duZAgPeY3kh5iehrPes69QO8 jBjP+2X9WgAAlW4iqXjd8rIbUUlrF8lLVOJR7htbOzfKiGP3cWF0qEI1e 21/snBiup9feaslOPp3z2p9HInLQpMahJKFB49EzoOfCu4atkY9+5wd+O Yaa8lCqGFJg1pF8Gipjp5bndl2kLgoWjnStIFm212TwtidEDXcPnrKg6U w==; X-IronPort-AV: E=McAfee;i="6600,9927,10866"; a="7429609" X-IronPort-AV: E=Sophos;i="6.03,233,1694761200"; d="scan'208";a="7429609" 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:39 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10866"; a="900040478" X-IronPort-AV: E=Sophos;i="6.03,233,1694761200"; d="scan'208";a="900040478" 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:35 -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 10/10] uio_hv_generic: Don't free decrypted memory Date: Tue, 17 Oct 2023 13:25:05 -0700 Message-Id: <20231017202505.340906-11-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-Rspamd-Queue-Id: 58BEC4001A X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 96r5ur58fdkuo9nsarjy1c578fan7cpo X-HE-Tag: 1697574339-6556 X-HE-Meta: U2FsdGVkX19MXZT2vczyIwKen3sFRd0b+2/a7t0qtIRjHXcBLeIEiRrj1Lh/714uyErRoqTP71b1VDmOccQqfCaFXkRczqKwD3ZSVsckq3G6H8+0JdOyKDsfO9Bg7nTaXoM42atZZqnPhhmZkZXcYvHP/5st8eCACjQnwJfta7aKzF+2GviDAw12T81jhfhLugIV+Kejw5JiTZ0c+Pukk5jEEJemIHo8p25YxnrSVu0OV0hdwr28whlJRlnLDIPJChx+BW2yFVEjTRcKjPjcKA46wCyEXzE0hMWD0xJU+1wW48bzkgQ24eNFn/6O+rMdF6cB7VxfUFOlQMp1BQNMTI5kh5zx3LFL6QOLVEQb0jPhJJ56NHxpa0+263klrLGMO9IEHiALhAjPf7VupSf3ef4h/T/OfuDuUhdu2G5hEJGpXkt1EAGMMKy9uHN6ziT9dZbwKXnaPwYWKR3V6g3nH+IEaElCFc4ItsGwQVOUrbzJO7EdzSec9UWJJtoxPnCJ8nE5qPAdoczHSHr4Wm1wio5DjLIcP5b0h9+wicJSi5uuU2SwvE+dJpdgo//OgIugvycSpx4a/bpTT3aAMHDpTIeF9ef5FU8jKxQZz4eQWe0/bOE8HL5LAkLSfZELY0UqU7/TdZDrYKUX3qY/cW6WHZMo3Dgd9JusS1CGmkyXsRlveKbF/hU0A9NcdiVV4apldJV9w8eOtwjqNBoLde2KezWM+7Lc47tYd3yWLXPF74lq/jfeUd+2O3m0pZJV/lcVd0hKQ2BWS45mcYEIoCjQqm3m0q3l0cIlR6lJksLSPikZPZ5F1RI2MhhO2XqAlIFsDHwDEMldwS3XQR+D2qqjPawtmPL2qkcijXeyYoYdjMF6ipZiVhJ6irpysL9oqi7K70MX/o7O0bS2svmF0oDbId3hbO6QLVB2p3E/QABfkvM3hVfVY8/weXsvT8PRA+uN4XC75PsYQJ4JD9ut3Dd h8ToxExn XZTo4squvNv8Vjv37ZzsVrkGrxh4nqQqwAzUrosTa0fUzeAiKHSQ/J1KxlsX5YgymjNO+F7dDK/xe3izy8fZbHWxMJQzNUDLYwq5Gbnkz3K+RHA7jlDXzUzOCuyzbzuvMdwgImWjJ2dkC+siL/RoJWkn53lKzScmzACLz0WdYMD9GrCwZGZ7oBHOFJw+/aAzoRkeFvjUWOdzsq/m6wzMarkK33bVrKy4Gs7qtC2uAbslBEWIkq0vpyORR4vk7aJIt8YVbgX3L9F+zgqaB1jU9ZhDcxT0unT9oDqbR X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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. uio_hv_generic could free decrypted/shared pages if set_memory_decrypted() fails. Check the decrypted field in the gpadl before freeing in order to not leak the memory. 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/uio/uio_hv_generic.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/uio/uio_hv_generic.c b/drivers/uio/uio_hv_generic.c index 20d9762331bd..6be3462b109f 100644 --- a/drivers/uio/uio_hv_generic.c +++ b/drivers/uio/uio_hv_generic.c @@ -181,12 +181,14 @@ hv_uio_cleanup(struct hv_device *dev, struct hv_uio_private_data *pdata) { if (pdata->send_gpadl.gpadl_handle) { vmbus_teardown_gpadl(dev->channel, &pdata->send_gpadl); - vfree(pdata->send_buf); + if (!pdata->send_gpadl.decrypted) + vfree(pdata->send_buf); } if (pdata->recv_gpadl.gpadl_handle) { vmbus_teardown_gpadl(dev->channel, &pdata->recv_gpadl); - vfree(pdata->recv_buf); + if (!pdata->recv_gpadl.decrypted) + vfree(pdata->recv_buf); } } @@ -295,7 +297,8 @@ hv_uio_probe(struct hv_device *dev, ret = vmbus_establish_gpadl(channel, pdata->recv_buf, RECV_BUFFER_SIZE, &pdata->recv_gpadl); if (ret) { - vfree(pdata->recv_buf); + if (!pdata->recv_gpadl.decrypted) + vfree(pdata->recv_buf); goto fail_close; } @@ -317,7 +320,8 @@ hv_uio_probe(struct hv_device *dev, ret = vmbus_establish_gpadl(channel, pdata->send_buf, SEND_BUFFER_SIZE, &pdata->send_gpadl); if (ret) { - vfree(pdata->send_buf); + if (!pdata->send_gpadl.decrypted) + vfree(pdata->send_buf); goto fail_close; }