Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp683037pxb; Wed, 3 Feb 2021 15:17:32 -0800 (PST) X-Google-Smtp-Source: ABdhPJxpfvzccIoz7aoscXh+8a7VOkF+AUOtBeB3lXurzLrHiHOVXe7wIuN6+CB1J8AjZuJ/ERKX X-Received: by 2002:a17:906:654d:: with SMTP id u13mr5543860ejn.530.1612394252173; Wed, 03 Feb 2021 15:17:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612394252; cv=none; d=google.com; s=arc-20160816; b=R7DSk6TzK58VvyCOnfHfS6Bwg2FwGi9q0Jy2DioaAxLgtmiKATWXsgd8nYC+WamneD h13ytoivPiDu0PqrHpm2GVTZmP1d2CthRAZhMNDFXdJ+WJFQTJIBRyMonsz3flBsUeEs nmT+DjaVx1vOv5YD5uPbsFOjmGPDvgK8oVm4qfLjncSL36CElG/0cNwcv0N1Ok6Jdxw7 rvvMprF0GAb7WpuWaO//s9vB3S+YdrFWgD9NZt6aziEL++FliIFsMuIsT9WapdEgkByB 26+v6n9FX4Cr76kKizIRnNp1fRaYtIjVU9j7hpG3k+4aOSJSg6nytlmroBwJtm1Y/vOv zAcg== 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=5H8hjxlkdvPb45bcVkkKV4tSUYptTJIYikrbUINq/tA=; b=wTd1wATeMjQZpO3mG7uaReLQcm4vwdm/Ew8JItS0mcirNGxnvilpp0hhG6UDthdf8i 1bmgOUKxBNxqgZo24YhnoqIGHPgRH+rsuXZ5SFPcgd0ez9XUolUJJjH7c3vP5pwQbtif 79DHR14BA5mQnNInItfNpZKhxBZ6LI0PogcJQzmnZk8bNT5duiT7reMCJkp0hIiShdjz Wag7ITzbliyOgsUb4LVQu9k8DQGtBIZVC5/tnTcYmyQfRRpiwYCULtS+e7RnMZawS0l+ zQstBVK5dznUCY/0zWqzi64w5ofi07CRc1HOARNkwsvA0Xla7L+XU39PbyrFhALto57k Nq0A== 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 h10si2221263ejy.307.2021.02.03.15.17.07; Wed, 03 Feb 2021 15:17:32 -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 S233484AbhBCXPO (ORCPT + 99 others); Wed, 3 Feb 2021 18:15:14 -0500 Received: from mga06.intel.com ([134.134.136.31]:30210 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233338AbhBCW64 (ORCPT ); Wed, 3 Feb 2021 17:58:56 -0500 IronPort-SDR: 7ScCRB79TYVYOk+3XDeIRqRu5jDMQg14p5vKghoJdJyfDDFCWgZS3/OOENQv68xanl4vXEDkay VHJFKYvllQNw== X-IronPort-AV: E=McAfee;i="6000,8403,9884"; a="242642371" X-IronPort-AV: E=Sophos;i="5.79,399,1602572400"; d="scan'208";a="242642371" 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:30 -0800 IronPort-SDR: ZATXDVE4oU1cXivQHr/R3pV/sj5RxVs6GFYbtgGXsSadbA/fwUasLyOAJAL1Ug6JaHRqiOs94L gwGHC490f+0g== X-IronPort-AV: E=Sophos;i="5.79,399,1602572400"; d="scan'208";a="507921140" 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:30 -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 18/25] mm: Update can_follow_write_pte() for shadow stack Date: Wed, 3 Feb 2021 14:55:40 -0800 Message-Id: <20210203225547.32221-19-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 Can_follow_write_pte() ensures a read-only page is COWed by checking the FOLL_COW flag, and uses pte_dirty() to validate the flag is still valid. Like a writable data page, a shadow stack page is writable, and becomes read-only during copy-on-write, but it is always dirty. Thus, in the can_follow_write_pte() check, it belongs to the writable page case and should be excluded from the read-only page pte_dirty() check. Apply the same changes to can_follow_write_pmd(). Signed-off-by: Yu-cheng Yu --- mm/gup.c | 8 +++++--- mm/huge_memory.c | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index e4c224cd9661..66ab67626f57 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -357,10 +357,12 @@ static int follow_pfn_pte(struct vm_area_struct *vma, unsigned long address, * FOLL_FORCE can write to even unwritable pte's, but only * after we've gone through a COW cycle and they are dirty. */ -static inline bool can_follow_write_pte(pte_t pte, unsigned int flags) +static inline bool can_follow_write_pte(pte_t pte, unsigned int flags, + struct vm_area_struct *vma) { return pte_write(pte) || - ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte)); + ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pte_dirty(pte) && + !arch_shadow_stack_mapping(vma->vm_flags)); } static struct page *follow_page_pte(struct vm_area_struct *vma, @@ -403,7 +405,7 @@ static struct page *follow_page_pte(struct vm_area_struct *vma, } if ((flags & FOLL_NUMA) && pte_protnone(pte)) goto no_page; - if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags)) { + if ((flags & FOLL_WRITE) && !can_follow_write_pte(pte, flags, vma)) { pte_unmap_unlock(ptep, ptl); return NULL; } diff --git a/mm/huge_memory.c b/mm/huge_memory.c index bfec65c9308b..eb64e2b56bc9 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1337,10 +1337,12 @@ vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf, pmd_t orig_pmd) * FOLL_FORCE can write to even unwritable pmd's, but only * after we've gone through a COW cycle and they are dirty. */ -static inline bool can_follow_write_pmd(pmd_t pmd, unsigned int flags) +static inline bool can_follow_write_pmd(pmd_t pmd, unsigned int flags, + struct vm_area_struct *vma) { return pmd_write(pmd) || - ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pmd_dirty(pmd)); + ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pmd_dirty(pmd) && + !arch_shadow_stack_mapping(vma->vm_flags)); } struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, @@ -1353,7 +1355,7 @@ struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, assert_spin_locked(pmd_lockptr(mm, pmd)); - if (flags & FOLL_WRITE && !can_follow_write_pmd(*pmd, flags)) + if (flags & FOLL_WRITE && !can_follow_write_pmd(*pmd, flags, vma)) goto out; /* Avoid dumping huge zero page */ -- 2.21.0