From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3643CC433B4 for ; Fri, 9 Apr 2021 15:31:51 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id AA55E610F9 for ; Fri, 9 Apr 2021 15:31:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AA55E610F9 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=shutemov.name Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 0660E6B006E; Fri, 9 Apr 2021 11:31:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F31816B0071; Fri, 9 Apr 2021 11:31:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D5F156B0072; Fri, 9 Apr 2021 11:31:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0176.hostedemail.com [216.40.44.176]) by kanga.kvack.org (Postfix) with ESMTP id B0EC06B006E for ; Fri, 9 Apr 2021 11:31:49 -0400 (EDT) Received: from smtpin32.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 645B8184A0277 for ; Fri, 9 Apr 2021 15:31:49 +0000 (UTC) X-FDA: 78013218738.32.8937581 Received: from mail-lf1-f47.google.com (mail-lf1-f47.google.com [209.85.167.47]) by imf20.hostedemail.com (Postfix) with ESMTP id B51B2D2 for ; Fri, 9 Apr 2021 15:31:45 +0000 (UTC) Received: by mail-lf1-f47.google.com with SMTP id n8so10395956lfh.1 for ; Fri, 09 Apr 2021 08:31:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov-name.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=N/anldwvTE8JiwYoi+CFb0zDLJgVaul+of7NRgtf6uM=; b=L/g+dUL3VxLisPEfWcvegwi75OSGO8+Y2ESkFHKHtdm/FJ3KwHf3WyBvNQuBQUe6rK gmCszXDYq7J1ZtnXhsTfuZEwEIgDDO7ZIaqqN7x8SJZ+mKkxZV0a7YD856zUPVH4rC0f ws0pP0X7wcOUy1pWWfUdYeSsuH0EG1b3K5SkFuD0SUcOXg3wOKJmopOi3jZqWAOVNeC7 3eJh1iLvmnlJPS6EDZe9uoFw8pCHYnYMAzmVg/ApXr/iLJJe0F6iS3GVGKtWo2i+PYEZ vHuJ1D+eXoE46MtRGDezJVhqeUXXA/J/AuP4CvMG+vWWExbT8HwY/VYnAaqolfnmOInP Bpow== 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=N/anldwvTE8JiwYoi+CFb0zDLJgVaul+of7NRgtf6uM=; b=iB/9y3DMVIO/EL3W9dK1aF9XoumiJq6hbKe/NGpzhnn6/ZKk0sgz6FOLQ2CNPOya1/ IRoi5+IWAaN2r2V1miRAMUNsqtMKBl7h7aDYomxNWEpaadcJog89nC1AidgC+5OinkIa Q02NoJr/1gU15YYJUHJ1oUAbru9menU1LsBTYDLWFfgN7ULEEVZQQqHwNQwEdJNuziv6 aYZfKwwfhfCO8nrkJL248KP6Ol5o6Z9P7fvw8zRVIk8wqbHk4c6r5nU4z2Aa0hT39YmU HJRTqWDVPi2Pm4F9dyEmElqXQH9HJCMHJzkkDOwuKIa9uJ1AaKMt3DxRg78BAB4jjBUw G/VQ== X-Gm-Message-State: AOAM530vPIPASVGPg9bu9buD8dCABMa8NmNGFv/p+HIwfj8wkNw4W+au oE45A9kjYki0AAL/fbI2+/ujPA== X-Google-Smtp-Source: ABdhPJwYX6z9aGP1PXbuviqGTBPyv46KjTBYphTybXFc7ge+WtCz+ZQSUUKUTI90Zz3uE7HXZu0jww== X-Received: by 2002:ac2:53af:: with SMTP id j15mr3242187lfh.74.1617982307528; Fri, 09 Apr 2021 08:31:47 -0700 (PDT) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id s23sm298175lfs.246.2021.04.09.08.31.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Apr 2021 08:31:47 -0700 (PDT) Received: by box.localdomain (Postfix, from userid 1000) id 98ED8102498; Fri, 9 Apr 2021 18:31:46 +0300 (+03) Date: Fri, 9 Apr 2021 18:31:46 +0300 From: "Kirill A. Shutemov" 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 , 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 , Haitao Huang , "Kirill A . Shutemov" Subject: Re: [PATCH v24 19/30] mm: Update can_follow_write_pte() for shadow stack Message-ID: <20210409153146.ib2xsn7n2q4ixpde@box.shutemov.name> References: <20210401221104.31584-1-yu-cheng.yu@intel.com> <20210401221104.31584-20-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210401221104.31584-20-yu-cheng.yu@intel.com> X-Stat-Signature: qcudgx5q3h9sdfxaie88tu88ahr4mu3t X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: B51B2D2 Received-SPF: none (shutemov.name>: No applicable sender policy available) receiver=imf20; identity=mailfrom; envelope-from=""; helo=mail-lf1-f47.google.com; client-ip=209.85.167.47 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1617982305-2812 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, Apr 01, 2021 at 03:10:53PM -0700, 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(). > > Signed-off-by: Yu-cheng Yu > Reviewed-by: Kees Cook > Cc: Kirill A. Shutemov > --- > v24: > - Change arch_shadow_stack_mapping() to is_shadow_stack_mapping(). > > 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 e40579624f10..c313cc988865 100644 > --- a/mm/gup.c > +++ b/mm/gup.c > @@ -356,10 +356,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) && > + !is_shadow_stack_mapping(vma->vm_flags)); It's getting too ugly. I think it deserve to be rewritten. What about: 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; ? -- Kirill A. Shutemov