Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1589234pxb; Thu, 4 Feb 2021 17:48:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJzHjkwwkBUzebRmHMGWYg+c++W/yZDkTfwjFF4LrwCzqrbjIC5FOi8UkBkZpthiH7Z9cUV8 X-Received: by 2002:a17:906:c9d8:: with SMTP id hk24mr1898818ejb.468.1612489724573; Thu, 04 Feb 2021 17:48:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612489724; cv=none; d=google.com; s=arc-20160816; b=n3j8egnd2DsuSCOmoKqUkeaUY53Rlczod/v+QTOvDHsdKpE3gDYgbdbf/bM838Vvnn K2aG1lpmSH+utqxXfZnI1cbmu8iaRA2qFf2pwsSwaesppMIXUbGdrD7q/Ee1II1lUv9j cqMNM1Zix7GkJ8VPNKkLPK4CoXiZDi2DfYCWJbRV1rqMb/FVp8X6UXzU5j0xcjZk1OJa 7/102IrrAn73Oko/7Qxcx60FFIfoQvM6KnMbXCFgv8Ky9QpWPGAWFHaZ4g6YnxDdYIS6 WMawN6Cb/IQtLSS/2uGanYlj47OQ2tc3FFB8KOFeyYiXnikQGM2Oqh0+NtMEjKBkVA97 uQ7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=/KkzilzXEisJddaLgkjR4cjm+2gZx2aIAgczeuG3frI=; b=nPONDfztQdhL+lvFxboXU1dq20QTk4asP+1gItosAeQ8mtMSlvh5sdYC65vm/zz3JX bw3kJNCqkMrKRiiKvaS94UhaX/E79GtF/2APhLWPcOvNfUBH2vyb9afYrk70pz8OliPt pRTXCctwocYK5eM3gNpZ5+G3z0kxkxXvsY44dQLALdVMi4Og0nRqfx4cgXDk1QXimGpQ V48G2WnyVAqJnyAi98PPeElvTEawvJ+3AWv4STz/97sErvSMT7zZ1BK11/+f4YCw6ECl Nugvzrl6y8ngk4sDSZcm9BMC3pJM25i2EgZuq+lZnMu8kFaduUvec/vunR5robd1xOBN j5Yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=SQnBGiEH; 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=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d10si4231264edh.147.2021.02.04.17.48.20; Thu, 04 Feb 2021 17:48:44 -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=@chromium.org header.s=google header.b=SQnBGiEH; 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=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239475AbhBDUaB (ORCPT + 99 others); Thu, 4 Feb 2021 15:30:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46954 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240117AbhBDU2Q (ORCPT ); Thu, 4 Feb 2021 15:28:16 -0500 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BBCF3C06178A for ; Thu, 4 Feb 2021 12:27:36 -0800 (PST) Received: by mail-pl1-x62b.google.com with SMTP id x9so2378179plb.5 for ; Thu, 04 Feb 2021 12:27:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=/KkzilzXEisJddaLgkjR4cjm+2gZx2aIAgczeuG3frI=; b=SQnBGiEH7SyjfxJqdii1BqfZaqzWi1I4ROQ+T9GW6ajXhOT3uDA3RK4TvfKWUbUWhy MIj0IL32FR9WV3RHADy1VD4Q77Q6mNKUCLqwwws+lNkcfFqSMvCOp0kEeObfoGsW907m zujxwsHELU1cNG7gpww149IJuWidCm5N4ZGL8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=/KkzilzXEisJddaLgkjR4cjm+2gZx2aIAgczeuG3frI=; b=ij9NG8xBq1QdXSzjEUQRGRVNtfWfKtDsxfCCYlcgGXTJLd2EBRqPfMSgdq6486jACV LzuGqRWpd4u3hWfFxE6RS5v9FR+xYpLTCm7p2gqRMyeykgtq83LA9ZJwlwzEeRbwvXNd WC1gRdsoMGGbThtLAu/lMHJpcWvNM7EP/ToyQFEztsyWzIo+19RkNnZnlr7vDHciYr5z MloMfQPVsw/d0NBlQS38koWNXZ6U0VnhD0tmC71gqag50LoCt7LRDuCRMMTj4fQybqqx TTYzk0mq89pEql8vImvbA6Wfuzfg7QA0Um91EBb+RZa2K6PlxQ3N+9C0r7Z4Fm0Vzugv LTsA== X-Gm-Message-State: AOAM5335wWGkj+RRg/K5Uaj/+zFs5rBVT6Mc2jjKyqGxUF2yIdm5KhxD 6odGL+eITjD/7nZaapK0fX5C5g== X-Received: by 2002:a17:90a:c902:: with SMTP id v2mr720901pjt.144.1612470456119; Thu, 04 Feb 2021 12:27:36 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id q2sm6995012pfg.190.2021.02.04.12.27.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Feb 2021 12:27:34 -0800 (PST) Date: Thu, 4 Feb 2021 12:27:34 -0800 From: Kees Cook To: Yu-cheng Yu Cc: 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 , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V. Shankar" , Vedvyas Shanbhogue , Dave Martin , Weijiang Yang , Pengfei Xu Subject: Re: [PATCH v19 18/25] mm: Update can_follow_write_pte() for shadow stack Message-ID: <202102041226.D3E2B437@keescook> References: <20210203225547.32221-1-yu-cheng.yu@intel.com> <20210203225547.32221-19-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210203225547.32221-19-yu-cheng.yu@intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Feb 03, 2021 at 02:55:40PM -0800, Yu-cheng Yu wrote: > 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(). Does this need the vma passed down? Should it just pass vm_flags? I suppose it doesn't really matter, though. Reviewed-by: Kees Cook -Kees > > 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 > > -- Kees Cook