Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp673916pxb; Wed, 3 Feb 2021 15:02:03 -0800 (PST) X-Google-Smtp-Source: ABdhPJwsxMHAZCCnKfZia6JFjN2au13UM+aQKhlVcHF4v7XW0EL2/c7K6B4dLVZCof1bwPYr1toP X-Received: by 2002:a17:906:c1d9:: with SMTP id bw25mr5315311ejb.452.1612393323667; Wed, 03 Feb 2021 15:02:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612393323; cv=none; d=google.com; s=arc-20160816; b=daHYJ8VG6AullOjCAcI+h5Zmyrdw1uWB7wCVuLgvNh+AHOt0vK7T8feUWlOy7VndQt RdIWN7BkpUrpCavEqU/My2O/P3IQF7QubrKBLmVFAvMrRQwJro/cvlEpjfO6mTkmpqNf +6TDF2Ge/vvs39p0tY4G4TvAvQKlTKW0eCc/JYAlMsNagUBTx/hq6Vsd0LXkqD3+vRPG 0hksUxXtbVzzYCmfkZTfnLkQWOXqlE4WkRfsX6X9Fb/sWy4eO9HyXg9OHKsN2iy2VBRH 1tzxyerSY9y+yRRaJ6QVKaXoA56YX5jfD+oIfsGn8WmCqdzh+0MQ2N4GaKwVPa9txP4Y zhRA== 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 :ironport-sdr:ironport-sdr; bh=9i0aYU4lT6Yzl15wrcHTJUHMu/1JGviqPN9NDBaWpHE=; b=Z4QFVDXS/BZAn2NeGy+iCBp/Bz9QOyMHraeiQPtUclGzrwwllhi8BwR5NO+/rA2+7/ 6L+JyhhG/8lC0UHpgFOAFqDiuTShZ6NZNjIWyYAd1Dz7lHjVypWTNvZnNYkJKmMk45BV NkRpXClO2GNj+kj6pqxuzFlV2hlwCNZ4wRuUMxFd1Me7PLLM+dOa4KwvKwGj+YnKIScU lQSwoxgHwrsHuo8hr2y36tkSsvDt1kT1lOOTzTrLQ4Pn+9UMKGT6wY5LD4k0Vz9VFG4u r5h9dipbNGtgcq8jNaN6I3+aa/k9nGrFE5RTz0pRRxW1o/30xPPWyRDhOyrhIjq96v/m ncfw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g19si2297108ejz.742.2021.02.03.15.01.37; Wed, 03 Feb 2021 15:02:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233430AbhBCW7w (ORCPT + 99 others); Wed, 3 Feb 2021 17:59:52 -0500 Received: from mga06.intel.com ([134.134.136.31]:30220 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233232AbhBCW5y (ORCPT ); Wed, 3 Feb 2021 17:57:54 -0500 IronPort-SDR: BZIea/fgpDa7mgooeCYdLCJ9LBWuMEVUGkI7l6/iire8xlVkEF5qkCwlCZs8dGhzrVRtaK5xzG ZFpzv5u58Tvg== X-IronPort-AV: E=McAfee;i="6000,8403,9884"; a="242642360" X-IronPort-AV: E=Sophos;i="5.79,399,1602572400"; d="scan'208";a="242642360" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2021 14:56:26 -0800 IronPort-SDR: upGbwf1A8nUYk19fGAnkOJkBYo4KvsEvxiUr2GQsY4mdbvncP8FCvTjJHS/pqhgf3DovCkIUlo SBaL8OD5bJWg== X-IronPort-AV: E=Sophos;i="5.79,399,1602572400"; d="scan'208";a="507921109" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2021 14:56:25 -0800 From: Yu-cheng Yu To: x86@kernel.org, "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H.J. Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V. Shankar" , Vedvyas Shanbhogue , Dave Martin , Weijiang Yang , Pengfei Xu Cc: Yu-cheng Yu Subject: [PATCH v19 11/25] x86/mm: Update ptep_set_wrprotect() and pmdp_set_wrprotect() for transition from _PAGE_DIRTY to _PAGE_COW Date: Wed, 3 Feb 2021 14:55:33 -0800 Message-Id: <20210203225547.32221-12-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210203225547.32221-1-yu-cheng.yu@intel.com> References: <20210203225547.32221-1-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When Shadow Stack is introduced, [R/O + _PAGE_DIRTY] PTE is reserved for shadow stack. Copy-on-write PTEs have [R/O + _PAGE_COW]. When a PTE goes from [R/W + _PAGE_DIRTY] to [R/O + _PAGE_COW], it could become a transient shadow stack PTE in two cases: The first case is that some processors can start a write but end up seeing a read-only 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. The second case is that when _PAGE_DIRTY is replaced with _PAGE_COW non- atomically, a transient shadow stack PTE can be created as a result. Thus, prevent that with cmpxchg. Dave Hansen, Jann Horn, Andy Lutomirski, and Peter Zijlstra provided many insights to the issue. Jann Horn provided the cmpxchg solution. Signed-off-by: Yu-cheng Yu --- arch/x86/include/asm/pgtable.h | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 4a7fd3e5b7df..2144a25ca2a3 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -1229,6 +1229,24 @@ 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) { + /* + * If Shadow Stack is enabled, pte_wrprotect() moves _PAGE_DIRTY + * to _PAGE_COW (see comments at pte_wrprotect()). + * When a thread reads a RW=1, Dirty=0 PTE and before changing it + * to RW=0, Dirty=0, another thread could have written to the page + * and the PTE is RW=1, Dirty=1 now. Use try_cmpxchg() to detect + * PTE changes and update old_pte, then try again. + */ + if (cpu_feature_enabled(X86_FEATURE_SHSTK)) { + pte_t old_pte, new_pte; + + old_pte = READ_ONCE(*ptep); + do { + new_pte = pte_wrprotect(old_pte); + } while (!try_cmpxchg(&ptep->pte, &old_pte.pte, new_pte.pte)); + + return; + } clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte); } @@ -1286,6 +1304,24 @@ 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) { + /* + * If Shadow Stack is enabled, pmd_wrprotect() moves _PAGE_DIRTY + * to _PAGE_COW (see comments at pmd_wrprotect()). + * When a thread reads a RW=1, Dirty=0 PMD and before changing it + * to RW=0, Dirty=0, another thread could have written to the page + * and the PMD is RW=1, Dirty=1 now. Use try_cmpxchg() to detect + * PMD changes and update old_pmd, then try again. + */ + if (cpu_feature_enabled(X86_FEATURE_SHSTK)) { + pmd_t old_pmd, new_pmd; + + old_pmd = READ_ONCE(*pmdp); + do { + new_pmd = pmd_wrprotect(old_pmd); + } while (!try_cmpxchg((pmdval_t *)pmdp, (pmdval_t *)&old_pmd, pmd_val(new_pmd))); + + return; + } clear_bit(_PAGE_BIT_RW, (unsigned long *)pmdp); } -- 2.21.0