Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp1688280pxb; Fri, 20 Aug 2021 11:22:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwGfHxIVxIUypg0nev6PtmBHWvmGbNroOu/ny0W6+G4N47YA7CaTl+eOi1gCxSPgSzvjbTZ X-Received: by 2002:a92:c6d1:: with SMTP id v17mr14799033ilm.302.1629483722852; Fri, 20 Aug 2021 11:22:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629483722; cv=none; d=google.com; s=arc-20160816; b=bUc4mtapYzlxaTUYfAVb+CU7o1Aq8yscV1lYezY/kEdx5NM6VKUX4FjRWfcyWKlcyb rJGqmZwzibGwFdwl3D0DxBWKXDU+dPVDaeWeccO1xyKtORbt9eVg6WfDETO4XF0mzwrK kIWJ5x3uxWtRSQ7idIyE0c9GoR1yLT8zcaWj/Cxn1TYUNz6YRKtwKZzB3iJzAnL9U41p P1Zexs7NUNmYtCvHcHoLeOvDdWZ6HYa6aJWwXqiDyYJ0LY3Qenn3Fn395I8My40Z5xWb snQQc5oHDA1cBC3QFRSLMhbW7Sp1GXRS7HLEx8FWsk55WIC8VJY35v/BXzg/3JHjkedT SRnQ== 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; bh=545eqO8HmwjylNHheZuq0aFT2WIUBbYr7ej0klPGNFA=; b=uq51Xa9X7wcj7H1t4TIrNOurXP60z3qPlv6UJgNIC/ZwsciB39UID8l7JfOumi+OVy ax+O/gFt9sLvhl+X0j86MUCZ/MiSRscgnfQ8/k6MNo65rfnwbBWlL3IbWkzKohx45ANx 7YmnxSNsXYSwtNFf+644P0K+/RsQaYMReGiCsWkAE7LcL0NyCMXViL/WEUlMETM9f99q KCAeLb7Og4JqDdOLyzngkhuqFpJLyURWs5NaspNBf4MFrbLI/J/N29GFZfpuXgBVR91t BG3C4dDcCmTX54/saOkfmpRB31GGu0rIcsru2uesqrS7MOIuhfywXIq0iCNAIt1dg7c0 UF1w== 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 i13si6965321ilk.122.2021.08.20.11.21.50; Fri, 20 Aug 2021 11:22:02 -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 S238052AbhHTSVm (ORCPT + 99 others); Fri, 20 Aug 2021 14:21:42 -0400 Received: from mga05.intel.com ([192.55.52.43]:22426 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237713AbhHTSUn (ORCPT ); Fri, 20 Aug 2021 14:20:43 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10082"; a="302407830" X-IronPort-AV: E=Sophos;i="5.84,338,1620716400"; d="scan'208";a="302407830" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Aug 2021 11:18:50 -0700 X-IronPort-AV: E=Sophos;i="5.84,338,1620716400"; d="scan'208";a="533074748" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Aug 2021 11:18:49 -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" , Dave Martin , Weijiang Yang , Pengfei Xu , Haitao Huang , Rick P Edgecombe Cc: Yu-cheng Yu , "Kirill A . Shutemov" Subject: [PATCH v29 20/32] mm: Update can_follow_write_pte() for shadow stack Date: Fri, 20 Aug 2021 11:11:49 -0700 Message-Id: <20210820181201.31490-21-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210820181201.31490-1-yu-cheng.yu@intel.com> References: <20210820181201.31490-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(). While at it, also split the long line into smaller ones. Signed-off-by: Yu-cheng Yu Reviewed-by: Kirill A. Shutemov Cc: Kees Cook --- v26: - Instead of passing vm_flags, pass down vma pointer to can_follow_write_*(). v25: - Split long line into smaller ones. v24: - Change arch_shadow_stack_mapping() to is_shadow_stack_mapping(). --- mm/gup.c | 16 ++++++++++++---- mm/huge_memory.c | 16 ++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index b94717977d17..02dfba3141e7 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -478,10 +478,18 @@ 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)); + if (pte_write(pte)) + return true; + if ((flags & (FOLL_FORCE | FOLL_COW)) != (FOLL_FORCE | FOLL_COW)) + return false; + if (!pte_dirty(pte)) + return false; + if (is_shadow_stack_mapping(vma->vm_flags)) + return false; + return true; } static struct page *follow_page_pte(struct vm_area_struct *vma, @@ -524,7 +532,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 b9a6fc7af693..d35acb59dde9 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1346,10 +1346,18 @@ vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf) * 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)); + if (pmd_write(pmd)) + return true; + if ((flags & (FOLL_FORCE | FOLL_COW)) != (FOLL_FORCE | FOLL_COW)) + return false; + if (!pmd_dirty(pmd)) + return false; + if (is_shadow_stack_mapping(vma->vm_flags)) + return false; + return true; } struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, @@ -1362,7 +1370,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