Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp6865742rwd; Tue, 6 Jun 2023 03:08:09 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5jSE/RadTR7KerlCsE7A9m+YdH100927o9UI529Q77osRKLE5MQKfNIhQR6XLRwdZX65Jm X-Received: by 2002:a05:6358:bea6:b0:125:4dd9:cf23 with SMTP id di38-20020a056358bea600b001254dd9cf23mr2301628rwb.12.1686046088954; Tue, 06 Jun 2023 03:08:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686046088; cv=none; d=google.com; s=arc-20160816; b=R0EqawxESkUp2VzX2tuI4qpOTI7xFkiAaqxQ0YXBfFYAQlrMn8uDy40OeP6EiFehLW JHlkOuW1hUu8keodK3olVygo/xJlr90I6QP7XwQy5AVERMHg23DXnqfgAD9yrP/rke9L UB5PHDBulD2myKHfFVlOLV6bDBd89Y4rZx8ZFSlaEsxfpAeD1wspziy7nP34pky+CiIE ygQsyZBvAEnGhpWj5WTz+aMqyMkp6tup+RHYOvBd1yExQk4RckZ2/mrHyCBieWIL4L6z Tn+X+lIrvE6WOnpZUlRL+ODsUjVKawYtNAL7tkCVtgeeSiIDEiJCY8aGjhCXIJqWLhNJ X2iA== 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=RKyLtInM9xkQMcxdlhjJHh4pPvd9swjjqqaLnuUhyHs=; b=hyUKysgtrJ7g1rPJ9tSwVGuzn5ftAcESuDeWuwdCrzVsYKhIkH5DF7ox8kxctIb2vs DioMDtByKGAS4Fbz9ybu+4tVXsD+foDMS7C65bkH6SJ15GuD/5hX7AQoi2gJuU5iYazN hibEdKpaBRbM1rj4QCBVTsbtahaOJyMAX/qR8bvDHr9nNy/I6R0ynguGJBI7i61r0Juv Sr26549Il2xJdnRd5hmjtQNVns0kl1fC/0BtY4osDrboJIj4mQ2NgiZEJGGjLdkL6hMS +ZadiX5+7eFwLUvog7gQTL1YODcfvMNdXqbUkEP8IBvZ8vfSzXxi//fW5Lg8qwoadlJ0 3suA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ciV7fRbr; 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 v11-20020a17090ac90b00b00258996c09e4si8908682pjt.22.2023.06.06.03.07.57; Tue, 06 Jun 2023 03:08:08 -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=ciV7fRbr; 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 S237470AbjFFJ4p (ORCPT + 99 others); Tue, 6 Jun 2023 05:56:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236148AbjFFJ4f (ORCPT ); Tue, 6 Jun 2023 05:56:35 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 73565E73 for ; Tue, 6 Jun 2023 02:56:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1686045394; x=1717581394; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7RLFRqvdRV4EeGWD2/bOkXceiG/5lKBnbgdL4IoAVVE=; b=ciV7fRbrLWFPhM/KEYth/Vr28YZKwzcAPL6k7UWq14J93o9Zl0Ekpo4C Xwbxvv4V1/gyZ0cxgJNBC4j9x/ZNBrYtUbvygNIrRFt0spYfMXFQ9GZJv uNumEtDdLWp+q2Ua0igR8cV+2PBw1NLiEUw4tbA7oopeeXq7zF10l+z52 85x8ljd3socjSIK8QheAdwvUyJ7RpEM0Lt9PDtVto/IiW8xHje67b6uok jtaUG1j7515hFrXqsv51mJ1dHrxKUYmocUnWb0G1AK4wDZDPLjgEgYHt2 DPu6fD17W2tzuSijvfVEC8TjB9uOnirNgDvYWm6kSaJhVHz3USnVfYt26 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10732"; a="422445929" X-IronPort-AV: E=Sophos;i="6.00,219,1681196400"; d="scan'208";a="422445929" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Jun 2023 02:56:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10732"; a="738720781" X-IronPort-AV: E=Sophos;i="6.00,219,1681196400"; d="scan'208";a="738720781" Received: from rgraefe-mobl1.ger.corp.intel.com (HELO box.shutemov.name) ([10.252.58.173]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Jun 2023 02:56:29 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id B154A10D02A; Tue, 6 Jun 2023 12:56:26 +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" Subject: [PATCHv3 2/3] x86/tdx: Fix race between set_memory_encrypted() and load_unaligned_zeropad() Date: Tue, 6 Jun 2023 12:56:21 +0300 Message-Id: <20230606095622.1939-3-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230606095622.1939-1-kirill.shutemov@linux.intel.com> References: <20230606095622.1939-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 an 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. load_unaligned_zeropad() can touch memory that is not owned by the caller, but just happens to be next after the owned memory. This load_unaligned_zeropad() behavior makes it important when the kernel asks VMM to convert a GPA from shared to private or back. The kernel must never have a page mapped into direct mapping (and aliases) as private when the GPA is already converted to shared or when the GPA is not yet converted to private. guest.enc_status_change_prepare() is called before adjusting direct mapping and therefore is responsible for converting the memory to private. guest.enc_status_change_finish() is 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 properly converted. 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") Reviewed-by: Kuppuswamy Sathyanarayanan --- arch/x86/coco/tdx/tdx.c | 64 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index e146b599260f..f6213a10de3a 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,43 @@ 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 an 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. + * + * load_unaligned_zeropad() can touch memory that is not owned by the + * caller, but just happens to be next after the owned memory. This + * load_unaligned_zeropad() behavior makes it important when the kernel + * asks VMM to convert a GPA from shared to private or back. The kernel + * must never have a page mapped into direct mapping (and aliases) as + * private when the GPA is already converted to shared or when the GPA + * is not yet converted to private. + * + * guest.enc_status_change_prepare() is called before adjusting direct + * mapping and therefore is responsible for converting the memory to + * private. + * + * guest.enc_status_change_finish() is 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 properly + * converted. 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