Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp622468pxb; Tue, 1 Feb 2022 07:10:43 -0800 (PST) X-Google-Smtp-Source: ABdhPJwremfVf+U6w7zTNTYdnwQ/h3L7Qq2ghfNVlhCGG2KcC3v/vWlkMetm8t5zLe11qHZnCJ2o X-Received: by 2002:a17:907:9808:: with SMTP id ji8mr12966937ejc.12.1643728242817; Tue, 01 Feb 2022 07:10:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643728242; cv=none; d=google.com; s=arc-20160816; b=YVKnyegyvyBNvbaCRlHm8cEFvLPgyBKUa7PRQ/e93xgRERhhQixEHLYuOTgkHgiIAb 377MdM7s7fXHY/daxb3jCcr6bokZEbzGS9/T/n5btZcBQv7yIY6xikgvR8RmRtz8BH/j cQZvqFYVoY/EwyuAtuR0QbhgAqEnCXuKZObS3QsjCL0wlVAYpqJpGpP1oP6hxZB1QfRd QJ5QZJtJieyhcA+Ogx1vilvyx6e4v0KEdwTNnpdwMH7SEka9MJBR6TtgoOcgk2ZGVD9C qSrmR8O9Ngpu4xhyBQsSjsDnsTS5eP8K2sJFnTEC6Dl9Vrd739k/YGReUSb2QIhtWbuQ 9Gkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=T3ELRmDMfooD2OFG0Vzht0b2AuwJuq4oAK8Ai7cctu4=; b=x0cIK2pQ1HJktLEWzQ5tZ6wHRfC5ytR7k/iQLhqsV5J0/zdCK4Zy8Kej4DBdhLRNbf J+VHqNOqj4ydM1FM7bSsNFyh1AQbBhaTjV/DwARtMMnhwcxEJAjaOtiL+k6X/3JbZmPn r3a8+FCB8THkc0VqCNqAqvasFwn+GJ/GpoOX2bdL+QNIXMRIVO2b3H3aYT5J0ZlT6oOf BENi3ixGGcSl1xfn8VG83pDF/79SOzeS518n9qKVtJOhk/rdJ53MQfdnyZD16BpCx64x Dp6XsT5R6Lq+z0jojdD+ff7LQF3r9mpLGySub/16p5VnC8F9ZvqqE1B29qUh4UWdcNuO ksQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=bKLtdut3; 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=pass (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 r13si9219545eda.492.2022.02.01.07.10.16; Tue, 01 Feb 2022 07:10:42 -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; dkim=pass header.i=@intel.com header.s=Intel header.b=bKLtdut3; 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=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356885AbiA3VXz (ORCPT + 99 others); Sun, 30 Jan 2022 16:23:55 -0500 Received: from mga06.intel.com ([134.134.136.31]:52029 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356510AbiA3VV7 (ORCPT ); Sun, 30 Jan 2022 16:21:59 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643577719; x=1675113719; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=/0hTOGjoC4CyAc1QDDk6aPdkNAiEc470qCgFvhPkCgI=; b=bKLtdut3Ogdq4+oOSpJgsqPOgg+rV6mDdSuKtqY+S0ltre7UIHQK56w8 Ai74Vr12IOm4fdRTQFyyt1OY3zFmCpv/PYtDLHieYQObCrLYt+uNeBoT4 gX9lnQLs99NElKyFqpXa3KZECxlbkMuzVlDyfhybYN8KmX9OpnMyiVySS 9TYf/Ov+L7MyGLi212ag/YNkpcH/ZHwyavEnYkW09rDaDh+pfEzI7WdyW g1Cb1MgsCLsO5z8SYWRlfCkVtZkutTvsbjqkMi+GbSihPwvh26rtBxqC1 suV3tm8ouH3xnDJSrEVpvLjZKz/UvkKg9qvvtsuKi17YXuQgiZY9ZaK5v w==; X-IronPort-AV: E=McAfee;i="6200,9189,10243"; a="308104936" X-IronPort-AV: E=Sophos;i="5.88,329,1635231600"; d="scan'208";a="308104936" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jan 2022 13:21:59 -0800 X-IronPort-AV: E=Sophos;i="5.88,329,1635231600"; d="scan'208";a="536856798" Received: from avmallar-mobl1.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.209.123.171]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Jan 2022 13:21:58 -0800 From: Rick Edgecombe 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" , Dave Martin , Weijiang Yang , "Kirill A . Shutemov" , joao.moreira@intel.com, John Allen , kcc@google.com, eranian@google.com Cc: rick.p.edgecombe@intel.com, Yu-cheng Yu Subject: [PATCH 16/35] x86/mm: Update maybe_mkwrite() for shadow stack Date: Sun, 30 Jan 2022 13:18:19 -0800 Message-Id: <20220130211838.8382-17-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220130211838.8382-1-rick.p.edgecombe@intel.com> References: <20220130211838.8382-1-rick.p.edgecombe@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yu-cheng Yu When serving a page fault, maybe_mkwrite() makes a PTE writable if its vma has VM_WRITE. A shadow stack vma has VM_SHADOW_STACK. Its PTEs have _PAGE_DIRTY, but not _PAGE_WRITE. In fork(), _PAGE_DIRTY is cleared to cause copy-on-write, and in the page fault handler, _PAGE_DIRTY is restored and the shadow stack page is writable again. Introduce an x86 version of maybe_mkwrite(), which sets proper PTE bits according to VM flags. Apply the same changes to maybe_pmd_mkwrite(). Signed-off-by: Yu-cheng Yu Reviewed-by: Kirill A. Shutemov Signed-off-by: Rick Edgecombe Cc: Kees Cook --- Yu-cheng v29: - Remove likely()'s. arch/x86/include/asm/pgtable.h | 6 ++++++ arch/x86/mm/pgtable.c | 20 ++++++++++++++++++++ include/linux/mm.h | 2 ++ mm/huge_memory.c | 2 ++ 4 files changed, 30 insertions(+) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index e1061b9cba6a..36166bdd0b98 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -282,6 +282,9 @@ static inline int pmd_trans_huge(pmd_t pmd) return (pmd_val(pmd) & (_PAGE_PSE|_PAGE_DEVMAP)) == _PAGE_PSE; } +#define maybe_pmd_mkwrite maybe_pmd_mkwrite +extern pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma); + #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD static inline int pud_trans_huge(pud_t pud) { @@ -1660,6 +1663,9 @@ static inline bool arch_faults_on_old_pte(void) return false; } +#define maybe_mkwrite maybe_mkwrite +extern pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma); + #endif /* __ASSEMBLY__ */ #endif /* _ASM_X86_PGTABLE_H */ diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index 3481b35cb4ec..c22c8e9c37e8 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -610,6 +610,26 @@ int pmdp_clear_flush_young(struct vm_area_struct *vma, } #endif +pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_WRITE) + pte = pte_mkwrite(pte); + else if (vma->vm_flags & VM_SHADOW_STACK) + pte = pte_mkwrite_shstk(pte); + return pte; +} + +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) +{ + if (vma->vm_flags & VM_WRITE) + pmd = pmd_mkwrite(pmd); + else if (vma->vm_flags & VM_SHADOW_STACK) + pmd = pmd_mkwrite_shstk(pmd); + return pmd; +} +#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ + /** * reserve_top_address - reserves a hole in the top of kernel address space * @reserve - size of hole to reserve diff --git a/include/linux/mm.h b/include/linux/mm.h index 311c6018d503..b3cb3a17037b 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -955,12 +955,14 @@ void free_compound_page(struct page *page); * pte_mkwrite. But get_user_pages can cause write faults for mappings * that do not have writing enabled, when used by access_process_vm. */ +#ifndef maybe_mkwrite static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma) { if (likely(vma->vm_flags & VM_WRITE)) pte = pte_mkwrite(pte); return pte; } +#endif vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page); void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr); diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 406a3c28c026..2adedcfca00b 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -491,12 +491,14 @@ static int __init setup_transparent_hugepage(char *str) } __setup("transparent_hugepage=", setup_transparent_hugepage); +#ifndef maybe_pmd_mkwrite pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) { if (likely(vma->vm_flags & VM_WRITE)) pmd = pmd_mkwrite(pmd); return pmd; } +#endif #ifdef CONFIG_MEMCG static inline struct deferred_split *get_deferred_split_queue(struct page *page) -- 2.17.1