Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp4831816pxy; Tue, 27 Apr 2021 13:46:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyqIIsD+dPCPOKvKcUrTv+nItWFN5r3TFiwHIjOy/ySf+84hy32ii03tPiVs2ExMNFCuIkX X-Received: by 2002:a63:d942:: with SMTP id e2mr23729001pgj.117.1619556397633; Tue, 27 Apr 2021 13:46:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619556397; cv=none; d=google.com; s=arc-20160816; b=olJ5I3CnocgRMBpEcKaDdjnKukvEB/LM7/l0/6rOzrwXQkhnN1psTYG7/2C2yzqBJa tywXCtsW4a2i8TCcO51RkHKKgUZmlhtzz81gd1UWKWCsrSDR9nSITkCRNeS0ABetp665 IbCSX5cMFcgfHPh8RGDSRHVGsfRQpTe7qC9Pyr0Y70cnl+OrF+0sTwBDXojJvjpjNX/6 xQmGuwGF2McYgD4QE6Xl9cHQHNPQkWFhOMZOhEWR3OhjpGrQGWluNh02iHnvUAY4YVXf erPqkmvhYyeV/9lI080jk4ei4uRaMLq/eTv7KX5ytVdhaAMdZ9nb3Mea3gIJU80pJMRf e0Eg== 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=w7HGHOrqop+mnK/3p7OWCouWtBgZkOSB5+TKCKy7Z4c=; b=DMEyXlEnZdjlKQnAWZUIYQlPXzvcZeH7VbioCb7Dno9eC4kYJPvF3+patozyoDqm04 e4ilUryWMOEhiCy7bQ07bFITN1gOPCbTxgt6YkMGO22siFQnGJQ4FUayfCg1kpIpVW3T HOpLjmO9iEmTj42q/G7rxd8Dvs38NP9f1rT2WSYHaXMJiF7COMuONgizHmcmtn/Z8zti xHyEK6s2sSlPt4AX9f1c/savdpGUu7bGCjm6vU4O34EUsEL2KA9XAsvjFS0+2c7+B/KX dLzu0mANdxSMlZoepcr+lVAcdQIuwu54bz41pBLtLGrrZxCas63ekjLbjW3NCOH5TWvX tsBw== 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 w16si1068592pll.248.2021.04.27.13.46.25; Tue, 27 Apr 2021 13:46:37 -0700 (PDT) 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 S239234AbhD0Upl (ORCPT + 99 others); Tue, 27 Apr 2021 16:45:41 -0400 Received: from mga05.intel.com ([192.55.52.43]:31779 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239075AbhD0UpT (ORCPT ); Tue, 27 Apr 2021 16:45:19 -0400 IronPort-SDR: JHh6iZnp6fe7VB8sv+Nmu45my5AViKH/jhbxDo42Z4UC5dsyRxWLzE6WFD/2dC7te9HygKdRbf sXD5Q+x5GNdQ== X-IronPort-AV: E=McAfee;i="6200,9189,9967"; a="281922470" X-IronPort-AV: E=Sophos;i="5.82,255,1613462400"; d="scan'208";a="281922470" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Apr 2021 13:44:14 -0700 IronPort-SDR: rSbJOHYWKU44KGErZE0rybPPRTg8QpnuigGhKK94jTQw0kWk+G1SMwD+BpPauzXKgkBonA1TEU 3QrunAR6FGIQ== X-IronPort-AV: E=Sophos;i="5.82,255,1613462400"; d="scan'208";a="465623486" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Apr 2021 13:44:13 -0700 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 , Haitao Huang Cc: Yu-cheng Yu , "Kirill A . Shutemov" Subject: [PATCH v26 12/30] x86/mm: Update ptep_set_wrprotect() and pmdp_set_wrprotect() for transition from _PAGE_DIRTY to _PAGE_COW Date: Tue, 27 Apr 2021 13:42:57 -0700 Message-Id: <20210427204315.24153-13-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210427204315.24153-1-yu-cheng.yu@intel.com> References: <20210427204315.24153-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 Reviewed-by: Kees Cook Reviewed-by: Kirill A. Shutemov --- 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 e1739f590ca6..46d9394b884f 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -1306,6 +1306,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); } @@ -1350,6 +1368,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