Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp4529183rwd; Tue, 30 May 2023 06:44:00 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4Jqs1r8ic8ydqNkAt0TLlP5qLCEsrRcbIajApbGTnH79WKLBb2yy7m7iLy0Uh6AXmTS7wx X-Received: by 2002:a05:6a20:a108:b0:10c:6956:a23c with SMTP id q8-20020a056a20a10800b0010c6956a23cmr10884269pzk.25.1685454240000; Tue, 30 May 2023 06:44:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685454239; cv=none; d=google.com; s=arc-20160816; b=uESXkJb9SOJlyHGRXRp2QpPZflEy+aL5ElPBfqqsozMksuiBrRURABWR8n71pIvs38 LjuTmcbSNPX++3sqaxiwQQc/RtaOmeMDDa0y1I6GtqC4xW7jK024hc5SkEjj+OXbQDVA RgxKATyOiODDQ49fAhoIVorKqGnhXe2OnFDx+/re0SsGl9Ly8JaDuhx2tWfVcL60+iys rePS1V9vVoVwpwcCfpoirAnqZjlJe2jDcDeHJQ/YpF67EObseu/Ejcp5I+IwrmKdIVWB FhHaSFFVOoBEwQTldBKl4b8nVF/L6iDgjCx7RyLMi0IRFyFtmFuGlf4K6crvMEiA6V5d ebKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=4myFnj9ndGqnaiGeDhyYP7OesbdejV5qJnTGcrpJ6BI=; b=fukjb1LvSMp3T9oCytJ9oCxnQkXD6nQ8swHj8SjvWIopPcVOmweSNdIt2RdPi26dye I6f9yxPL/HHR+fb4FHJENBQWLCR4qOoyPx/CPtRjriGnYOZJOS09eoeYJrQvWlJNDpbW hXqkZmIPYzxzHUq44bof9hSPJe8ZB8bTDRJsPWFHZtAI9NUZtyLhTY3mdGCxXEEIqxVE GZSh0cnXJj/gjt8A5qpAJSxRnvQ5IrL6hz00ykz/e81Z3xL1uohhFPoZnQUiFZ3SA48+ btzv7Z1BSt5/KTVQgc1k+Os0Pw4Wy3xpEUyyc52kYJpdKlK1DHwrThYLvnPjZVNFChBu FebQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=WX3qukUt; 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 z135-20020a63338d000000b0052866b25be5si10619430pgz.746.2023.05.30.06.43.46; Tue, 30 May 2023 06:43:59 -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=WX3qukUt; 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 S232635AbjE3NWW (ORCPT + 99 others); Tue, 30 May 2023 09:22:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231806AbjE3NWU (ORCPT ); Tue, 30 May 2023 09:22:20 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52CA792; Tue, 30 May 2023 06:22:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685452938; x=1716988938; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=4aGVqSdvgTPuU35q1D5R0JGk/giyk93IXrt6KSMzrvU=; b=WX3qukUtyoSjiaHL/VBOZGDvMBtlichIyy6Dbu6mmnY31KaCE391a7cI biFY6HhA0euHONuaWOCpmvDuTUcK460YhFPnOnasLj86BSIDeDrcLDvWJ 39QJ1kRxN/R+fQyGar4SafysPzzhqBgWT+uwy4w1tIu21ajIaHX+l5Zvo GTpHw32gdZaSMEKjnqMPZg0gtkfpu9fOx+MJVqhd18HteeL329+i51+uM cCUNkD/pgObw4gSwc8GkrcKOQVKacnPu/TUrGk7mdC2gGw4aL5vurSmfF N3s6kuIPcUIBbNoXUeb5J9h438V6uOGVnVNhqN7MUEQ93sI6bHGsATpPn A==; X-IronPort-AV: E=McAfee;i="6600,9927,10726"; a="383166691" X-IronPort-AV: E=Sophos;i="6.00,204,1681196400"; d="scan'208";a="383166691" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 May 2023 06:22:17 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10726"; a="656853818" X-IronPort-AV: E=Sophos;i="6.00,204,1681196400"; d="scan'208";a="656853818" Received: from ndesilva-mobl1.amr.corp.intel.com (HELO [10.209.101.99]) ([10.209.101.99]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 May 2023 06:22:17 -0700 Message-ID: <2d8ae093-dd9e-ea49-d6f9-3fdb2fb84e4d@linux.intel.com> Date: Tue, 30 May 2023 06:22:17 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0 Thunderbird/102.11.0 Subject: Re: [PATCHv2 2/3] x86/tdx: Fix race between set_memory_encrypted() and load_unaligned_zeropad() Content-Language: en-US To: Tom Lendacky , "Kirill A. Shutemov" Cc: "Kirill A. Shutemov" , dave.hansen@intel.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, decui@microsoft.com, rick.p.edgecombe@intel.com, seanjc@google.com, x86@kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org References: <20230526120225.31936-1-kirill.shutemov@linux.intel.com> <20230526120225.31936-3-kirill.shutemov@linux.intel.com> <19d9977f-bf07-2948-1b45-456af9c09e2f@linux.intel.com> <20230530005736.ndwza2i5n7wrki5h@box.shutemov.name> <9d679d3f-8db9-80ef-292a-9ca5b860579b@amd.com> From: Sathyanarayanan Kuppuswamy In-Reply-To: <9d679d3f-8db9-80ef-292a-9ca5b860579b@amd.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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 Hi, On 5/30/23 5:57 AM, Tom Lendacky wrote: > On 5/29/23 19:57, Kirill A. Shutemov wrote: >> On Fri, May 26, 2023 at 03:10:56PM -0700, Sathyanarayanan Kuppuswamy wrote: >>> >>> >>> On 5/26/23 5:02 AM, Kirill A. Shutemov wrote: >>>> 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. >>> >>> /s/to/to be ? >> >> Yep, my bad. >> >>>> 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. >>> >>> I am wondering whether this issue exist in the AMD code? >>> >>> IMO, you can add some info on the window in set_memory_encrypted() >>> where this race exists. >> >> I don't think AMD affected by load_unaligned_zeropad() the same way as >> Intel does. But I'm not sure. >> >> Tom, do you have any comments? > > Right, shouldn't be an issue for SNP. Thanks for confirming. > > Thanks, > Tom > >> >>> >>>> >>>> guest.enc_status_change_prepare() called before adjusting direct mapping >>>> and therefore it is responsible for converting the memory to private. >>>> >>>> guest.enc_status_change_finish() 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. >>> >>>> Rest looks good to me. Reviewed-by: Kuppuswamy Sathyanarayanan >>>> 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..59cc13e41aa6 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; >>> >>> I think you don't need to change the order here. >> >> I wanted to emphasise that the comment is for _prepare/_finish callbacks >> and I hoped re-order would help with this. >> >>>> -    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_status_change_finish() 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"); >>>>   } >>> >>> --  >>> Sathyanarayanan Kuppuswamy >>> Linux Kernel Developer >> -- Sathyanarayanan Kuppuswamy Linux Kernel Developer