Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp620112pxb; Tue, 1 Feb 2022 07:08:29 -0800 (PST) X-Google-Smtp-Source: ABdhPJwLKZSTp2ViDCZSt9rR3NEAGwDKuXv3526bwpSlWq0Sjg2Mx/6agi8mx9aNRh4DYJ8Di+l5 X-Received: by 2002:a17:907:97c9:: with SMTP id js9mr20888308ejc.216.1643728109597; Tue, 01 Feb 2022 07:08:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643728109; cv=none; d=google.com; s=arc-20160816; b=09bUl54wOAcR5gC1IXeEGRpfGxlxtwrykhXT0hfOyXVy3Y7f1G1AY1daFc+K4r1U8x Pbbf9mHss9KICaNPnEb2kz/2GuBd/QPtbuAeIegYT1BFW6i9piEnLEYRd6CTYNLGSnpu EqUpfaF+9Q9vs2pHX0p4lfRWTcAtkrZV+LqAGcemcPXemvbGvBk7UnofaI4CS5eMjZx7 adly6kxC8O6rnuB87W6XVb3Z4Jkw/tMStkrJTNEpqwbgBsF63GiPZX0qSChfOow2onMq SJk32d+l4Ymn/ZKFD4G4Z8CCcXyP+o0hIuo9kef5VpX45hPNpDH7jwGruvzqr7o790gJ wsVw== 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=248U8bGtTSZmWF6/PQLbOxYn3kQGWLbpB6rgtuaj/UQ=; b=PH/1yDq3eDwcoGIKgm8gWdtLzlYYx0a67uhaG3Al4CaSpIM6I66+n+nkRNNZZHQRO0 QJ48Dm0dsFJ5UezWaw0oSJfk1ccpeuCR9UaTi7eAIUn6MFMz7gPdqiQNDAE/cEa42kp3 Udjb7aF/QOCfQxKDvZqxMv+ziz9uNKMuerfs1h02gTvEdP0nCYT+SZPIEewXjUkjUJ6N 1szo7AvdG2pJr0gEJCKveuGLdLI2Fd6qTIb81f06outbpL/Luu7+v2gNOOUrsKp13uAt AcmhS5gDceRIZGUTouNSskkioo/IZC1N21zWsenxZkdoMtMICc2V/NiYYUPyT79fQBub scUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=dlM74cuD; 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 u6si11718829edd.172.2022.02.01.07.08.02; Tue, 01 Feb 2022 07:08:29 -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=dlM74cuD; 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 S1356691AbiA3VYt (ORCPT + 99 others); Sun, 30 Jan 2022 16:24:49 -0500 Received: from mga06.intel.com ([134.134.136.31]:52047 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356511AbiA3VWT (ORCPT ); Sun, 30 Jan 2022 16:22:19 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643577740; x=1675113740; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=numCZyfka0OHXp22LK4zITWZGhKJw0SkYk203jreRnY=; b=dlM74cuDh7xu2MNg3+IJvLIAs2pYaU5sWnV/oj7ayaRERiOVcP8VZIA/ TpTW4QNHGblvwiZF8j6vydljDAgwtO+MAdVLVJ0kx8hjYh1dETkVvbmYe 6uVxz6o8DvdFqSA2/Kx0rhEkdGXj+V96NjC9317uPBunrImEdNRm6bF9v wG9l6pSrkxX9FwjWDekTrQXLsyCQL1l1xrxg3HwOIBKtOw8VabjaH7Soe fjy1UVFLONAFby32xi0ok4irb2BdUOftG5IBsVO3EV9zgOcZo84KImrOF MTGOkMkQphgjosl1TrT+yOf6H7nXroXF2yOSMKcMbV3qoNaCH9Nr/UfsV g==; X-IronPort-AV: E=McAfee;i="6200,9189,10243"; a="308104948" X-IronPort-AV: E=Sophos;i="5.88,329,1635231600"; d="scan'208";a="308104948" 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:22:01 -0800 X-IronPort-AV: E=Sophos;i="5.88,329,1635231600"; d="scan'208";a="536856831" 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:22:01 -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 20/35] mm: Update can_follow_write_pte() for shadow stack Date: Sun, 30 Jan 2022 13:18:23 -0800 Message-Id: <20220130211838.8382-21-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 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 Signed-off-by: Rick Edgecombe Cc: Kees Cook --- Yu-cheng v26: - Instead of passing vm_flags, pass down vma pointer to can_follow_write_*(). Yu-cheng v25: - Split long line into smaller ones. Yu-cheng 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 f0af462ac1e2..95b7d1084c44 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -464,10 +464,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, @@ -510,7 +518,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 3588e9fefbe0..1c7167e6f223 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.17.1