Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1639695rwd; Thu, 25 May 2023 16:03:29 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ79Xnzu/JeYyYRBdrFfVRwIPrtqcqX/3tas1baHxqAHW/5sh8kHOwH7cSLYfTp5HdDkBT0K X-Received: by 2002:a05:6a20:918d:b0:104:4b5f:1d0a with SMTP id v13-20020a056a20918d00b001044b5f1d0amr26532098pzd.53.1685055809433; Thu, 25 May 2023 16:03:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685055809; cv=none; d=google.com; s=arc-20160816; b=UnyotgPHgSTdasLPi+GdvO0qklYsCcQVNHZ0tNEaps70xGiZtaOocMmksTkIr/Dnnc aAsZRNuRj6vhTj/1CNGjzaCfwr8oDpDm6Q8V8GFtr/ck8RKuUm65I5t4ARZ/EGBp7UVk PV6XEuGT3OKAL4Otp0pb+Lcmh4t5N2iPq/F82Hs0aijlRIarMmxQwWtFvf+Se06ZfeK4 tIc6gv9rsLoadF82UQcnBUiJxYCPXXvFmuTFCwA0GWJrxhaz3kWN+gXOoqqlcX0WGuG7 hiaVITeV8hmobQjJw90jKPMzVQ578T7ApxrLksCwQYbnsvqGhEC3cl7utyBkpqS4H5fZ o8rw== 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=ng/X1boMr3k4pHt37jzb7Hc40RXHruVwjF9W24RwiZc=; b=OGR/FRqZe+MiWReqppT2WqDeFOan6PmuHB8W/ukPRTAzK6wLN0qM/I5OavqYldyMFV sVYkzNCRJMnvBtpq4IZ1TDLW96gNXyJkEi+IJOhm0d72ri6V+MDhtzlBisPVTK2Ix+9j 2zGbQ9mexGFlYJ+BBH9nWWM9Jly65VnTXAkpILTnfuL/xcbfIYcX0U8Cp4qO+UgklJwH eDpHNw/cnI3hld+owGJ+XB0RRpfFPg5jCI3oWt2Ajyyq7POnCdroVZiLgeiXfVHe5nPV UTutk5s3fe/vqlZZ4Yhit9SCzJ8PhCqUIhBlhnZXJcbSc+0UHQUvNY+W9AkdmWiZxEcQ yftQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=cqRUXBBj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t67-20020a625f46000000b006367f3bb8a9si2442193pfb.249.2023.05.25.16.03.16; Thu, 25 May 2023 16:03:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=cqRUXBBj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234071AbjEYXAh (ORCPT + 99 others); Thu, 25 May 2023 19:00:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231964AbjEYXAe (ORCPT ); Thu, 25 May 2023 19:00:34 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5602C1A4; Thu, 25 May 2023 16:00:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685055629; x=1716591629; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=h2+VFs78zM5hSi1cfMqUT06G+rYbm2kFes9haAPQVuw=; b=cqRUXBBjb63SJJmn8igmdSehy07ho/a9Mt41eqBCGUbE0iUxOQP+xZNZ U2veR+y53Jom6HLWQxQeBdhvIq2i+fraaORiAVrBAqmGVtsIzo7k7POci hIMXXIbKrJbcY5fxGEik4BZCEg78RmGsxbWFJXDo8NEfcOoARrfz7cNrW z/m8G2ETAOsHi4xgXvW/TiLOHb3UP7SXg3WRIJypwK8AGkaLk07maMvG8 KfT+V9mr7HMACAOyGRwITP4r1kriC3IbB35IYc5xr/3hfEXO2e4wJFjMm hsngfhtrpd88k20/OERjXd23lJ0yEHtHWjkCncSyRjJRwUXnf1uzW/Tp4 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10721"; a="440404189" X-IronPort-AV: E=Sophos;i="6.00,192,1681196400"; d="scan'208";a="440404189" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 May 2023 15:59:16 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10721"; a="737995684" X-IronPort-AV: E=Sophos;i="6.00,192,1681196400"; d="scan'208";a="737995684" Received: from daubin-mobl.amr.corp.intel.com (HELO box.shutemov.name) ([10.249.39.107]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 May 2023 15:59:00 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id C2DA710DC17; Fri, 26 May 2023 01:58:57 +0300 (+03) From: "Kirill A. Shutemov" To: dave.hansen@intel.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de Cc: decui@microsoft.com, rick.p.edgecombe@intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, seanjc@google.com, thomas.lendacky@amd.com, x86@kernel.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" , stable@vger.kernel.org Subject: [PATCH 2/2] x86/tdx: Fix race between set_memory_encrypted() and load_unaligned_zeropad() Date: Fri, 26 May 2023 01:58:47 +0300 Message-Id: <20230525225847.28592-3-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230525225847.28592-1-kirill.shutemov@linux.intel.com> References: <20230525225847.28592-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Touching privately mapped GPA that is not properly converted to private with MapGPA and accepted leads to unrecoverable exit to VMM. load_unaligned_zeropad() can touch memory that is not owned by the caller, but just happened to next after the owned memory. This load_unaligned_zeropad() behaviour makes it important when kernel asks VMM to convert a GPA from shared to private or back. Kernel must never have a page mapped into direct mapping (and aliases) as private when the GPA is already converted to shared or when GPA is not yet converted to private. guest.enc_status_change_prepare() called before adjusting direct mapping and therefore it is responsible for converting the memory to private. guest.enc_tlb_flush_required() called after adjusting direct mapping and it converts the memory to shared. It is okay to have a shared mapping of memory that is not converted properly. handle_mmio() knows how to deal with load_unaligned_zeropad() stepping on it. Signed-off-by: Kirill A. Shutemov Fixes: 7dbde7631629 ("x86/mm/cpa: Add support for TDX shared memory") Cc: stable@vger.kernel.org --- arch/x86/coco/tdx/tdx.c | 56 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index e146b599260f..84525df750d4 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -840,6 +840,30 @@ static bool tdx_enc_status_changed(unsigned long vaddr, int numpages, bool enc) return true; } +static bool tdx_enc_status_change_prepare(unsigned long vaddr, int numpages, + bool enc) +{ + /* + * Only handle shared->private conversion here. + * See the comment in tdx_early_init(). + */ + if (enc) + return tdx_enc_status_changed(vaddr, numpages, enc); + return true; +} + +static bool tdx_enc_status_change_finish(unsigned long vaddr, int numpages, + bool enc) +{ + /* + * Only handle private->shared conversion here. + * See the comment in tdx_early_init(). + */ + if (!enc) + return tdx_enc_status_changed(vaddr, numpages, enc); + return true; +} + void __init tdx_early_init(void) { u64 cc_mask; @@ -867,9 +891,35 @@ void __init tdx_early_init(void) */ physical_mask &= cc_mask - 1; - x86_platform.guest.enc_cache_flush_required = tdx_cache_flush_required; - x86_platform.guest.enc_tlb_flush_required = tdx_tlb_flush_required; - x86_platform.guest.enc_status_change_finish = tdx_enc_status_changed; + /* + * Touching privately mapped GPA that is not properly converted to + * private with MapGPA and accepted leads to unrecoverable exit + * to VMM. + * + * load_unaligned_zeropad() can touch memory that is not owned by + * the caller, but just happened to next after the owned memory. + * This load_unaligned_zeropad() behaviour makes it important when + * kernel asks VMM to convert a GPA from shared to private or back. + * Kernel must never have a page mapped into direct mapping (and + * aliases) as private when the GPA is already converted to shared or + * when GPA is not yet converted to private. + * + * guest.enc_status_change_prepare() called before adjusting direct + * mapping and therefore it is responsible for converting the memory + * to private. + * + * guest.enc_tlb_flush_required() called after adjusting direct mapping + * and it converts the memory to shared. + * + * It is okay to have a shared mapping of memory that is not converted + * properly. handle_mmio() knows how to deal with load_unaligned_zeropad() + * stepping on it. + */ + x86_platform.guest.enc_status_change_prepare = tdx_enc_status_change_prepare; + x86_platform.guest.enc_status_change_finish = tdx_enc_status_change_finish; + + x86_platform.guest.enc_cache_flush_required = tdx_cache_flush_required; + x86_platform.guest.enc_tlb_flush_required = tdx_tlb_flush_required; pr_info("Guest detected\n"); } -- 2.39.3