From patchwork Fri Jun 16 19:17:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 109328 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1566625vqr; Fri, 16 Jun 2023 12:26:27 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7eUG+zFtsSUCvHPufHVj+36B05VUHw5UbMo4n481G/29vUnIOt6aumM1TWHQu4+KCwPf+b X-Received: by 2002:a05:6a00:1ace:b0:65c:9e3e:889b with SMTP id f14-20020a056a001ace00b0065c9e3e889bmr2993467pfv.6.1686943587088; Fri, 16 Jun 2023 12:26:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686943587; cv=none; d=google.com; s=arc-20160816; b=UoeZI0GM+yTv+gX/X3CbsSsyB3SzfN2k9CoSi0f9NHO5AStyvk9V5EvHWVFIRCo/pE IPsdQEmc8DrfI3mA9OZgiifhB/xaC0gPeVbkfuguQXAxcYdcoS77oSds56ad1mf3LFyY Y5EUvnzMg9faU52A+xGY2pMnuq7TQEPf1GLc2Vk7qvqGhJke4jBlnV2t6P9fUcdJAH4A +M942f7WO9q7efIFqtO0HSMR6+bDNzgligXtBeZSIRQ9nG+2F3USl1unSM8z3qAVRtHU hRPGRzXp3bUC4ALsEbEjj1gA+XpS7VKddQ9em2BOzE8Jk5jqWVDJfmz+PRt7fzQL+0QP SgwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:cc:subject:to:reply-to:sender:from :dkim-signature:dkim-signature:date; bh=BYKFeatUBoOPBvwxlIsSnk4Tb5+E9gS0xg4pcEolCM8=; b=j/tJcfPyDgpHnFd8bfXx1dUBJMmHduSiXnOGzcFvjW6Hff8h1SwYEcwIqN+AvOZ/3c I32ld9/nkfHPjz3kdLrK/oScrVB8QAGsB9bojAqOTEp3AQYVs6nX0xc6xzHaHX/O8zed 8h3m8V6hXB2O8FIRjJwwqTl8ncPvVd4rvEfZOTvbncvnYCaaKrFj1Upj8t7ANCusBGpd BBDiApgEGyDMNd4PS0VeATiMTcrejO7Xbv39bsr6pGj3XLdAMH8UUYosowbrmeoeX94l ybOChrSwMsAay1FYsku4k0eOugxsCIAdBnkG4fVGisME6hR5HLVvXKi+l/D6UXnel1yu 5Owg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=s3zyMKQs; dkim=neutral (no key) header.i=@linutronix.de header.b=lzJpGUNQ; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p7-20020a637f47000000b005533c55dc3esi1328587pgn.573.2023.06.16.12.26.15; Fri, 16 Jun 2023 12:26:27 -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=@linutronix.de header.s=2020 header.b=s3zyMKQs; dkim=neutral (no key) header.i=@linutronix.de header.b=lzJpGUNQ; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346158AbjFPTTF (ORCPT + 99 others); Fri, 16 Jun 2023 15:19:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48112 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346005AbjFPTRL (ORCPT ); Fri, 16 Jun 2023 15:17:11 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3573F35A7; Fri, 16 Jun 2023 12:17:05 -0700 (PDT) Date: Fri, 16 Jun 2023 19:17:03 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686943023; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=BYKFeatUBoOPBvwxlIsSnk4Tb5+E9gS0xg4pcEolCM8=; b=s3zyMKQs5qadjoAxqNan5FqTshvHBULaLv1BvKrLCYWiEYSiD1wvLdl8lRvYVuI0I/TE9d o6CwInak3jw3rAcA0LCznFqR3q4TfLTY+RoWcbZXIon1AnPvocrWYeuYO8QPNH5H/nI+6L 1Sab8SVKOHo3hJfi367hrKDBEtVruAFCLNmDXmChjOp2ErrcKBWo9LzUGBc+KWqyISWBZy 6/8V6dKUHW4eiS8mumt11CbejYty19PfYroSLBuhJ9LsI4/MjRRDIYMBLP7xnzHV2YmtjC XdfhM9IqUc3Vn97Jx0vE6pI3aXGuEHZJ8wWfzhEO7pjCaCwJ68tM1HMlaHf/yQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686943023; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=BYKFeatUBoOPBvwxlIsSnk4Tb5+E9gS0xg4pcEolCM8=; b=lzJpGUNQyzbZ6/cHLFrXn2+pzq3vHq7wgh8sduZt7KxquLNy6a0Nmh7gU5j01NgNuiDUzD Z5hWBBtyMHrLW3Cw== From: "tip-bot2 for Rick Edgecombe" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/shstk] x86/mm: Update ptep/pmdp_set_wrprotect() for _PAGE_SAVED_DIRTY Cc: "Yu-cheng Yu" , Rick Edgecombe , Dave Hansen , "Mike Rapoport (IBM)" , Pengfei Xu , John Allen , Kees Cook , x86@kernel.org, linux-kernel@vger.kernel.org MIME-Version: 1.0 Message-ID: <168694302336.404.16379064029653101571.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768888558676674796?= X-GMAIL-MSGID: =?utf-8?q?1768888558676674796?= The following commit has been merged into the x86/shstk branch of tip: Commit-ID: 75c1d1854306f4c978105bafe3ec1e030548cec5 Gitweb: https://git.kernel.org/tip/75c1d1854306f4c978105bafe3ec1e030548cec5 Author: Rick Edgecombe AuthorDate: Mon, 12 Jun 2023 17:10:37 -07:00 Committer: Dave Hansen CommitterDate: Thu, 15 Jun 2023 16:31:33 -07:00 x86/mm: Update ptep/pmdp_set_wrprotect() for _PAGE_SAVED_DIRTY When shadow stack is in use, Write=0,Dirty=1 PTE are preserved for shadow stack. Copy-on-write PTEs then have Write=0,SavedDirty=1. When a PTE goes from Write=1,Dirty=1 to Write=0,SavedDirty=1, it could become a transient shadow stack PTE in two cases: 1. Some processors can start a write but end up seeing a Write=0 PTE by the time they get to the Dirty bit, creating a transient shadow stack PTE. However, this will not occur on processors supporting shadow stack, and a TLB flush is not necessary. 2. When _PAGE_DIRTY is replaced with _PAGE_SAVED_DIRTY non-atomically, a transient shadow stack PTE can be created as a result. Prevent the second case when doing a write protection and Dirty->SavedDirty shift at the same time with a CMPXCHG loop. The first case Note, in the PAE case CMPXCHG will need to operate on 8 byte, but try_cmpxchg() will not use CMPXCHG8B, so it cannot operate on a full PAE PTE. However the exiting logic is not operating on a full 8 byte region either, and relies on the fact that the Write bit is in the first 4 bytes when doing the clear_bit(). Since both the Dirty, SavedDirty and Write bits are in the first 4 bytes, casting to a long will be similar to the existing behavior which also casts to a long. Dave Hansen, Jann Horn, Andy Lutomirski, and Peter Zijlstra provided many insights to the issue. Jann Horn provided the CMPXCHG solution. Co-developed-by: Yu-cheng Yu Signed-off-by: Yu-cheng Yu Signed-off-by: Rick Edgecombe Signed-off-by: Dave Hansen Acked-by: Mike Rapoport (IBM) Tested-by: Pengfei Xu Tested-by: John Allen Tested-by: Kees Cook Link: https://lore.kernel.org/all/20230613001108.3040476-12-rick.p.edgecombe%40intel.com --- arch/x86/include/asm/pgtable.h | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index a1883d8..13fdad2 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -1189,7 +1189,17 @@ static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm, static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep) { - clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte); + /* + * Avoid accidentally creating shadow stack PTEs + * (Write=0,Dirty=1). Use cmpxchg() to prevent races with + * the hardware setting Dirty=1. + */ + pte_t old_pte, new_pte; + + old_pte = READ_ONCE(*ptep); + do { + new_pte = pte_wrprotect(old_pte); + } while (!try_cmpxchg((long *)&ptep->pte, (long *)&old_pte, *(long *)&new_pte)); } #define flush_tlb_fix_spurious_fault(vma, address, ptep) do { } while (0) @@ -1241,7 +1251,17 @@ static inline pud_t pudp_huge_get_and_clear(struct mm_struct *mm, static inline void pmdp_set_wrprotect(struct mm_struct *mm, unsigned long addr, pmd_t *pmdp) { - clear_bit(_PAGE_BIT_RW, (unsigned long *)pmdp); + /* + * Avoid accidentally creating shadow stack PTEs + * (Write=0,Dirty=1). Use cmpxchg() to prevent races with + * the hardware setting Dirty=1. + */ + pmd_t old_pmd, new_pmd; + + old_pmd = READ_ONCE(*pmdp); + do { + new_pmd = pmd_wrprotect(old_pmd); + } while (!try_cmpxchg((long *)pmdp, (long *)&old_pmd, *(long *)&new_pmd)); } #ifndef pmdp_establish