From patchwork Thu Jun 1 18:25:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kirill A. Shutemov" X-Patchwork-Id: 102132 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp541115vqr; Thu, 1 Jun 2023 11:46:44 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6gENaX4yNgs8dX7g9PCYPn4sUz21a+52o/6nwDhdayyBEFOH7NGnisoF01Qmqs0QoW9yit X-Received: by 2002:a17:90b:370a:b0:254:ce9e:53 with SMTP id mg10-20020a17090b370a00b00254ce9e0053mr174302pjb.23.1685645204384; Thu, 01 Jun 2023 11:46:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685645204; cv=none; d=google.com; s=arc-20160816; b=czAsdN1h6+cMeWGOzYBy2BQke0g3cr1EuFhl4W7wTuX4mR7QJJ8ptR5ILsmXsEDqO4 ymnCY1kNJWzd3QQQx5Eaw6gmDN5uA98MLs/1pBF581Su/7+mnvfk/HsbpegeEJU76lkE B+aDOc7tKxc2vLGUc5/riXaTE8czviLosB3Vcomre6XL7Lsw2ZHwy+GBo6V1sOFxmxx7 5zDBU2Ku5miiBgvdQN8ZzCIlFxtGLFjZxtpD58IXKSh1Db5V+gEQhoJGdlaUOdyXqcsI abcl5YC+PjS6fxoqj0EHM6pBRE0sLUNb8wuhcq264Ear+pyi8xhuybpXZE3CiPQHN0jb 0qag== 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=oGRDwK5r+gHSrZclkxM5aTLGsgT2GEXWwSoW4pfzbqM=; b=mu91RY4N37jZkMYTXvRu3weJL1cQnHophojR3yRAQ+c6/IlsRJpmdp+iP2VWSzM9W7 rnXytkBoNrqKyo4N7CJeJ8FMJx9WAP2kSbItEnWucG/73zP/YpV3QreYWWzR5VC3L/qe sfE9zolZlfnCb0c99WH77YkSXhOjGU/NE4/tou4jz3zQKPOdLl8gASHmojsgdxhRh9fz 6g1oLsM+Cmtk/xoB2TgWEMzRPaTRNcKWaMAiOYDqfLy8o0tluwimNHbwZM57zR4Yb/91 iUNmdVxBrLqlNUrJdAvCUH1MD39PShj95ewo54iX6DqKzqAQbtZu2PYqVAT/dfJ3DWy4 ruIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=DVmri9in; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y63-20020a638a42000000b00534874dd5f4si2783662pgd.441.2023.06.01.11.46.32; Thu, 01 Jun 2023 11:46:44 -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=DVmri9in; 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 S232626AbjFAS0h (ORCPT + 99 others); Thu, 1 Jun 2023 14:26:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232364AbjFAS03 (ORCPT ); Thu, 1 Jun 2023 14:26:29 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB14B1A8; Thu, 1 Jun 2023 11:26:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685643974; x=1717179974; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=aHz7WO7wShdd679ArCfVvIALDE990d9+23AGlVsKS9M=; b=DVmri9inLMCZYCA2TTAEto9jBbCrI9Yq8z8B4zAbVsY80vRl7178Hjyl ZROtH0u3yCJslpjmRRdmAuR2ywyu8ZOchY0pY6Ew/f/g2XidVHotUCSXb A+FDjNKq46tS8++G9H56LD/u9fjmLay5OVMKG66t560EqppIl1f3Hl5PK xg5JN/RlNv8pQdCnb2F+csAH0MmrPCC0329w8DuBbjjQUN/J5GshnaaCM mW7AaTS10ObvzZjtO5jheYmsvzSBG6/wNIiOfifAfyeuUmaHJrzsgL8iB ESan9sb73jNMv/g/NP2xzh1CUmSrl4IatfTEiSHCgn8/4ujFRUjCv3/CI g==; X-IronPort-AV: E=McAfee;i="6600,9927,10728"; a="383920618" X-IronPort-AV: E=Sophos;i="6.00,210,1681196400"; d="scan'208";a="383920618" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Jun 2023 11:26:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10728"; a="772539957" X-IronPort-AV: E=Sophos;i="6.00,210,1681196400"; d="scan'208";a="772539957" Received: from fmaislin-mobl1.ger.corp.intel.com (HELO box.shutemov.name) ([10.252.47.213]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Jun 2023 11:26:04 -0700 Received: by box.shutemov.name (Postfix, from userid 1000) id 8C4C910B6AD; Thu, 1 Jun 2023 21:25:52 +0300 (+03) From: "Kirill A. Shutemov" To: Borislav Petkov , Andy Lutomirski , Dave Hansen , Sean Christopherson , Andrew Morton , Joerg Roedel , Ard Biesheuvel Cc: Andi Kleen , Kuppuswamy Sathyanarayanan , David Rientjes , Vlastimil Babka , Tom Lendacky , Thomas Gleixner , Peter Zijlstra , Paolo Bonzini , Ingo Molnar , Dario Faggioli , Mike Rapoport , David Hildenbrand , Mel Gorman , marcelo.cerri@canonical.com, tim.gardner@canonical.com, khalid.elmously@canonical.com, philip.cox@canonical.com, aarcange@redhat.com, peterx@redhat.com, x86@kernel.org, linux-mm@kvack.org, linux-coco@lists.linux.dev, linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" , Dave Hansen Subject: [PATCHv13 6/9] efi/unaccepted: Avoid load_unaligned_zeropad() stepping into unaccepted memory Date: Thu, 1 Jun 2023 21:25:40 +0300 Message-Id: <20230601182543.19036-7-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230601182543.19036-1-kirill.shutemov@linux.intel.com> References: <20230601182543.19036-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767527105534631534?= X-GMAIL-MSGID: =?utf-8?q?1767527105534631534?= load_unaligned_zeropad() can lead to unwanted loads across page boundaries. The unwanted loads are typically harmless. But, they might be made to totally unrelated or even unmapped memory. load_unaligned_zeropad() relies on exception fixup (#PF, #GP and now #VE) to recover from these unwanted loads. But, this approach does not work for unaccepted memory. For TDX, a load from unaccepted memory will not lead to a recoverable exception within the guest. The guest will exit to the VMM where the only recourse is to terminate the guest. There are two parts to fix this issue and comprehensively avoid access to unaccepted memory. Together these ensure that an extra "guard" page is accepted in addition to the memory that needs to be used. 1. Implicitly extend the range_contains_unaccepted_memory(start, end) checks up to end+unit_size if 'end' is aligned on a unit_size boundary. 2. Implicitly extend accept_memory(start, end) to end+unit_size if 'end' is aligned on a unit_size boundary. Side note: This leads to something strange. Pages which were accepted at boot, marked by the firmware as accepted and will never _need_ to be accepted might be on unaccepted_pages list This is a cue to ensure that the next page is accepted before 'page' can be used. This is an actual, real-world problem which was discovered during TDX testing. Signed-off-by: Kirill A. Shutemov Reviewed-by: Dave Hansen Reviewed-by: Ard Biesheuvel Reviewed-by: Tom Lendacky --- drivers/firmware/efi/unaccepted_memory.c | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/drivers/firmware/efi/unaccepted_memory.c b/drivers/firmware/efi/unaccepted_memory.c index bb91c41f76fb..3d1ca60916dd 100644 --- a/drivers/firmware/efi/unaccepted_memory.c +++ b/drivers/firmware/efi/unaccepted_memory.c @@ -37,6 +37,34 @@ void accept_memory(phys_addr_t start, phys_addr_t end) start -= unaccepted->phys_base; end -= unaccepted->phys_base; + /* + * load_unaligned_zeropad() can lead to unwanted loads across page + * boundaries. The unwanted loads are typically harmless. But, they + * might be made to totally unrelated or even unmapped memory. + * load_unaligned_zeropad() relies on exception fixup (#PF, #GP and now + * #VE) to recover from these unwanted loads. + * + * But, this approach does not work for unaccepted memory. For TDX, a + * load from unaccepted memory will not lead to a recoverable exception + * within the guest. The guest will exit to the VMM where the only + * recourse is to terminate the guest. + * + * There are two parts to fix this issue and comprehensively avoid + * access to unaccepted memory. Together these ensure that an extra + * "guard" page is accepted in addition to the memory that needs to be + * used: + * + * 1. Implicitly extend the range_contains_unaccepted_memory(start, end) + * checks up to end+unit_size if 'end' is aligned on a unit_size + * boundary. + * + * 2. Implicitly extend accept_memory(start, end) to end+unit_size if + * 'end' is aligned on a unit_size boundary. (immediately following + * this comment) + */ + if (!(end % unit_size)) + end += unit_size; + /* Make sure not to overrun the bitmap */ if (end > unaccepted->size * unit_size * BITS_PER_BYTE) end = unaccepted->size * unit_size * BITS_PER_BYTE; @@ -84,6 +112,13 @@ bool range_contains_unaccepted_memory(phys_addr_t start, phys_addr_t end) start -= unaccepted->phys_base; end -= unaccepted->phys_base; + /* + * Also consider the unaccepted state of the *next* page. See fix #1 in + * the comment on load_unaligned_zeropad() in accept_memory(). + */ + if (!(end % unit_size)) + end += unit_size; + /* Make sure not to overrun the bitmap */ if (end > unaccepted->size * unit_size * BITS_PER_BYTE) end = unaccepted->size * unit_size * BITS_PER_BYTE;