From patchwork Thu Apr 21 23:35:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Morton X-Patchwork-Id: 12822487 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 54569C43219 for ; Thu, 21 Apr 2022 23:35:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B65D36B0078; Thu, 21 Apr 2022 19:35:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AC6676B007B; Thu, 21 Apr 2022 19:35:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 96B4D6B007D; Thu, 21 Apr 2022 19:35:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.a.hostedemail.com [64.99.140.24]) by kanga.kvack.org (Postfix) with ESMTP id 752056B0078 for ; Thu, 21 Apr 2022 19:35:45 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 5599D1BF for ; Thu, 21 Apr 2022 23:35:45 +0000 (UTC) X-FDA: 79382495850.11.A6648E7 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf16.hostedemail.com (Postfix) with ESMTP id 663DF180026 for ; Thu, 21 Apr 2022 23:35:43 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 5C0A061EBF; Thu, 21 Apr 2022 23:35:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ADE25C385A5; Thu, 21 Apr 2022 23:35:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1650584143; bh=9JrhIuldGcG+bMHN5M/dS6f6EI/wfPeIxpjhrcP2TPk=; h=Date:To:From:In-Reply-To:Subject:From; b=O4bThR3t0/Ni9wo8SZHZgSbSzGynagqkRTNXiXoFsfTNjbgEzn8xTTREOQmhyrGW7 70ic6pdny/bKtdfjxPvQOIXi4Ryj+xnz9cB81Bt/HDUHeiWPFPpmstIE3C1fo0uDyA 9mOBegFHYLsBGbK8ec9sFleGkn0Sj1fXISP94nfw= Date: Thu, 21 Apr 2022 16:35:43 -0700 To: rppt@linux.vnet.ibm.com,peterx@redhat.com,axelrasmussen@google.com,aarcange@redhat.com,namit@vmware.com,akpm@linux-foundation.org,patches@lists.linux.dev,linux-mm@kvack.org,mm-commits@vger.kernel.org,torvalds@linux-foundation.org,akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20220421163508.66028a9ac2d9fb6ea05b1342@linux-foundation.org> Subject: [patch 04/13] userfaultfd: mark uffd_wp regardless of VM_WRITE flag Message-Id: <20220421233543.ADE25C385A5@smtp.kernel.org> X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 663DF180026 X-Rspam-User: Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=O4bThR3t; spf=pass (imf16.hostedemail.com: domain of akpm@linux-foundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org; dmarc=none X-Stat-Signature: sb98pyfdg79eyr8ruez98zbmz3cga44h X-HE-Tag: 1650584143-44281 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Nadav Amit Subject: userfaultfd: mark uffd_wp regardless of VM_WRITE flag When a PTE is set by UFFD operations such as UFFDIO_COPY, the PTE is currently only marked as write-protected if the VMA has VM_WRITE flag set. This seems incorrect or at least would be unexpected by the users. Consider the following sequence of operations that are being performed on a certain page: mprotect(PROT_READ) UFFDIO_COPY(UFFDIO_COPY_MODE_WP) mprotect(PROT_READ|PROT_WRITE) At this point the user would expect to still get UFFD notification when the page is accessed for write, but the user would not get one, since the PTE was not marked as UFFD_WP during UFFDIO_COPY. Fix it by always marking PTEs as UFFD_WP regardless on the write-permission in the VMA flags. Link: https://lkml.kernel.org/r/20220217211602.2769-1-namit@vmware.com Fixes: 292924b26024 ("userfaultfd: wp: apply _PAGE_UFFD_WP bit") Signed-off-by: Nadav Amit Acked-by: Peter Xu Cc: Axel Rasmussen Cc: Mike Rapoport Cc: Andrea Arcangeli Signed-off-by: Andrew Morton --- mm/userfaultfd.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) --- a/mm/userfaultfd.c~userfaultfd-mark-uffd_wp-regardless-of-vm_write-flag +++ a/mm/userfaultfd.c @@ -72,12 +72,15 @@ int mfill_atomic_install_pte(struct mm_s _dst_pte = pte_mkdirty(_dst_pte); if (page_in_cache && !vm_shared) writable = false; - if (writable) { - if (wp_copy) - _dst_pte = pte_mkuffd_wp(_dst_pte); - else - _dst_pte = pte_mkwrite(_dst_pte); - } + + /* + * Always mark a PTE as write-protected when needed, regardless of + * VM_WRITE, which the user might change. + */ + if (wp_copy) + _dst_pte = pte_mkuffd_wp(_dst_pte); + else if (writable) + _dst_pte = pte_mkwrite(_dst_pte); dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);