From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796150906; b=QVUlrkFT44qZZ2RlzFSF4tHpHf3GPi2udt2TsRNok2kBPHU0Qw11fQGz628HWlQWN68p wvZ+G5ZMfUqBMjdNKEjUceZVCEYsLd3U0G/oUvxAkfwU5eOWZgBWhsYS8B5sYJ+ePyfvV oXtTvMlc0dszgsxhtvgdef4vO4SiqhCJOZrtSlbUNzGM/XftAwk8RRt+ZL+LvmqbAHbjb kUFzfSuhDiWeOPqLEWe4ODiJKkq6AkDu0i//jxS5gyd2mJxfHauMAQr7Tb9IUrgNtkEm6 JyG9rjAllyEkV/B/9/5KKjMxJswDYYOOLSuJutasWgGUqobmAOu9w6+rqBGqBt2rxwg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796150906; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=mho9Lb53SnwpvkQKZrnR0MdrU3bfFRn0/bklnOj0LzU=; b=12xvdbjdkCuGLRySi76WpAJznMRLPVzxoeyU7ziK/zdpoL9PCRVRmHn5kY+PRZxkDLof eP35FY+rlDnlUziPPt+AkvldxINQhWHcp7zrewJj4t9ytqbZvIlMr87YHi7dYwNW2Zh1o OmRk8MCGASJ1UoxDgun/N36kAJN7g4Xk06cTq8w4QucBuDOnjzkhZt6nqB4G68Lwjtgm8 x6UDZyAtNFUwzR0QOhGTr4rKUzsaJBPizSmOYMHEP2dbrpYcxqNiTymewod1LsYmj+3MZ qjNKNXh71/nMqYHjSIet4WHCjc2vYH/63/W7UqCnR+K6PgkM87GanW1pPjIvENqWvww== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=r20UvguE; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C5B06606A320 for ; Tue, 1 Dec 2026 18:48:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=r20UvguE Received: by smtp.kernel.org (Postfix) with ESMTPSA id 958AE1F000C1; Tue, 1 Dec 2026 18:48:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796150906; bh=mho9Lb53SnwpvkQKZrnR0MdrU3bfFRn0/bklnOj0LzU=; h=Date:To:From:In-Reply-To:References:Subject; b=r20UvguEzejA1zuKSXrdaWoF3ZzyNlaeET5pARFjDNTcIZ1NgXcTFstgKxWN3sBsr ex1Qf7qguLPXbvY+UHwQ7KhqITaRlgrPLbHFVUHQY5xis3FdKhNbMvZprqUfxvGZTh l1TiFhYlKi6IhHORQuarrIeH5I+xfMLXSkm670MU= Date: Tue, 01 Dec 2026 10:48:26 -0800 To: mm-commits@vger.kernel.org,vbabka@kernel.org,usama.anjum@arm.com, surenb@google.com,skhan@linuxfoundation.org,sfr@canb.auug.org.au, rppt@kernel.org,rientjes@google.com,riel@redhat.com,rdunlap@infradead.org, pfalcato@suse.de,mirq-linux@rere.qmqm.pl,mhocko@suse.com,mason@kernel.org, liam@infradead.org,jannh@google.com,jaharkes@cs.cmu.edu,hughd@google.com, hannes@cmpxchg.org,david@readahead.eu,david@kernel.org,corbet@lwn.net, avagin@gmail.com,arnd@arndb.de,alexs@kernel.org,ljs@kernel.org, akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261127-mlock-madvise-mprotect-ai-fixes-v1-4-e46b2b1eb435@kernel.org> References: <20261127-mlock-madvise-mprotect-ai-fixes-v1-4-e46b2b1eb435@kernel.org> Subject: + fs-proc-task_mmu-write-protect-only-with-ptrace-attach.patch added to mm-new branch Message-Id: <20261201184826.958AE1F000C1@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list The patch titled Subject: fs/proc/task_mmu: write-protect only with ptrace attach has been added to the -mm mm-new branch. Its filename is fs-proc-task_mmu-write-protect-only-with-ptrace-attach.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/fs-proc-task_mmu-write-protect-only-with-ptrace-attach.patch This patch will later appear in the mm-new branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Note, mm-new is a provisional staging ground for work-in-progress patches, and acceptance into mm-new is a notification for others take notice and to finish up reviews. Please do not hesitate to respond to review feedback and post updated versions to replace or incrementally fixup patches in mm-new. The mm-new branch of mm.git is not included in linux-next If a few days of testing in mm-new is successful, the patch will me moved into mm.git's mm-unstable branch, which is included in linux-next Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: "Lorenzo Stoakes (ARM)" Subject: fs/proc/task_mmu: write-protect only with ptrace attach Date: Fri, 27 Nov 2026 19:45:43 +0000 The abomination known as PM_SCAN_WP_MATCHING, which makes PAGEMAP_SCAN not scan but mutate for eldritch uffd-wp purposes, also lacks a permission check when scanning the mm of another process. It defaults to PTRACE_MODE_READ, which is insufficient for this accursed operation. Therefore actually check for permissions correctly, and also make sure the mm is as expected when doing so. As it stands, a task may currently manipulate page tables of the remote process, write-protecting uffd-wp marked pages, allocating page tables and splitting THP, and invoking MMU notifiers as needed. The check is made at the time of the ioctl against its caller, as lock_trace() does for other /proc files, rather than at open. A tracker which passes READ but not ATTACH on another process now gets EACCES for PM_SCAN_WP_MATCHING, whether it opened the pagemap file itself or was handed the descriptor. Also update the pagemap documentation to reflect the change. Link: https://patch.msgid.link/20261127-mlock-madvise-mprotect-ai-fixes-v1-4-e46b2b1eb435@kernel.org Fixes: 52526ca7fdb9 ("fs/proc/task_mmu: implement IOCTL to get and optionally clear info about PTEs") Signed-off-by: Lorenzo Stoakes (ARM) Signed-off-by: Andrew Morton Reported-by: Chris Mason Reviewed-by: Pedro Falcato Reviewed-by: David Hildenbrand (Arm) Cc: Jan Harkes Cc: David Rheinsberg Cc: Hugh Dickins Cc: Vlastimil Babka Cc: Liam R. Howlett Cc: Jann Horn Cc: Alex Shi Cc: Johannes Weiner Cc: David Rientjes Cc: Rik van Riel Cc: Mike Rapoport Cc: Suren Baghdasaryan Cc: Michal Hocko Cc: Jonathan Corbet Cc: Shuah Khan Cc: Randy Dunlap Cc: Michał Mirosław Cc: Arnd Bergmann Cc: Stephen Rothwell Cc: Muhammad Usama Anjum Cc: Andrei Vagin Documentation/admin-guide/mm/pagemap.rst | 4 + fs/proc/task_mmu.c | 47 ++++++++++++++++++--- 2 files changed, 44 insertions(+), 7 deletions(-) --- a/Documentation/admin-guide/mm/pagemap.rst~fs-proc-task_mmu-write-protect-only-with-ptrace-attach +++ a/Documentation/admin-guide/mm/pagemap.rst @@ -240,7 +240,9 @@ in this IOCTL: - Write-protect the pages. The ``PM_SCAN_WP_MATCHING`` is used to write-protect the pages of interest. The ``PM_SCAN_CHECK_WPASYNC`` aborts the operation if non-Async Write Protected pages are found. The ``PM_SCAN_WP_MATCHING`` can be - used with or without ``PM_SCAN_CHECK_WPASYNC``. + used with or without ``PM_SCAN_CHECK_WPASYNC``. On the pagemap file of another + process, ``PM_SCAN_WP_MATCHING`` requires that the caller is allowed to attach + to that process with ptrace. Otherwise the IOCTL fails with ``EACCES``. - Both of those operations can be combined into one atomic operation where we can get and write protect the pages as well. --- a/fs/proc/task_mmu.c~fs-proc-task_mmu-write-protect-only-with-ptrace-attach +++ a/fs/proc/task_mmu.c @@ -3164,11 +3164,41 @@ static long pagemap_scan_flush_buffer(st return n; } -static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg) +/* + * Ensure the caller has PTRACE_MODE_ATTACH_FSCREDS permission for wp scan, and + * that the file's mm still matches the process's. + */ +static int pagemap_scan_check_wp(struct file *file, struct mm_struct *mm) +{ + struct task_struct *task; + struct mm_struct *task_mm; + bool same_mm; + + if (mm == current->mm) + return 0; + + task = get_proc_task(file_inode(file)); + if (!task) + return -ESRCH; + + task_mm = mm_access(task, PTRACE_MODE_ATTACH_FSCREDS); + put_task_struct(task); + if (IS_ERR(task_mm)) + return PTR_ERR(task_mm); + + same_mm = task_mm == mm; + mmput(task_mm); + + return same_mm ? 0 : -EACCES; +} + +static long do_pagemap_scan(struct file *file, unsigned long uarg) { struct pagemap_scan_private p = {0}; + struct mm_struct *mm = file->private_data; unsigned long walk_start; size_t n_ranges_out = 0; + bool scan_wp; int ret; if (!mmget_not_zero(mm)) @@ -3178,6 +3208,13 @@ static long do_pagemap_scan(struct mm_st if (ret) goto out_mmput; + scan_wp = p.arg.flags & PM_SCAN_WP_MATCHING; + if (scan_wp) { + ret = pagemap_scan_check_wp(file, mm); + if (ret) + goto out_mmput; + } + p.masks_of_interest = p.arg.category_mask | p.arg.category_anyof_mask | p.arg.return_mask; ret = pagemap_scan_init_bounce_buffer(&p); @@ -3199,7 +3236,7 @@ static long do_pagemap_scan(struct mm_st break; /* Protection change for the range is going to happen. */ - if (p.arg.flags & PM_SCAN_WP_MATCHING) { + if (scan_wp) { mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_VMA, 0, mm, walk_start, p.arg.end); mmu_notifier_invalidate_range_start(&range); @@ -3209,7 +3246,7 @@ static long do_pagemap_scan(struct mm_st ret = walk_page_range(mm, walk_start, p.arg.end, &pagemap_scan_ops, &p); - if (p.arg.flags & PM_SCAN_WP_MATCHING) + if (scan_wp) mmu_notifier_invalidate_range_end(&range); mmap_read_unlock(mm); @@ -3246,11 +3283,9 @@ out_mmput: static long do_pagemap_cmd(struct file *file, unsigned int cmd, unsigned long arg) { - struct mm_struct *mm = file->private_data; - switch (cmd) { case PAGEMAP_SCAN: - return do_pagemap_scan(mm, arg); + return do_pagemap_scan(file, arg); default: return -ENOIOCTLCMD; _ Patches currently in -mm which might be from ljs@kernel.org are mm-msync-return-enomem-if-page-aligned-length-overflows.patch mm-vma-drop-mm-asynchronously-in-vma_start_read.patch mm-vma-predicate-vma_assert_stabilised-on-config_debug_vm.patch mm-only-assert-mmap-locks-if-config_debug_vm-or-lockdep-is-set.patch mm-only-assert-vma-locks-if-config_debug_vm-is-set.patch mm-convert-warn_on_onces-to-vm_warn_on_once-in-vma-lock-code.patch mm-madvise-zap-markers-from-guard-regions.patch mm-mmap-free-gathered-pages-before-mmf_oom_skip.patch percpu-fix-contig-hints-that-are-too-low.patch percpu-search-the-whole-reserved-chunk.patch xen-privcmd-convert-mmap-hook-to-mmap_prepare.patch kvm-ppc-book3s-convert-spapr-tce-mmap-hook-to-mmap_prepare.patch mshv_vtl-convert-mmap-hooks-to-mmap_prepare.patch x86-sgx-convert-vepc-mmap-hook-to-mmap_prepare.patch udf-convert-mmap-hook-to-mmap_prepare.patch selinuxfs-convert-mmap-hooks-to-mmap_prepare.patch xen-convert-xenstore-interface-mmap-hooks-to-mmap_prepare.patch soc-aspeed-convert-lpc-and-p2a-control-mmap-hooks-to-mmap_prepare.patch soc-qcom-rmtfs_mem-convert-mmap-hook-to-mmap_prepare.patch tee-convert-shm-mmap-hook-to-mmap_prepare.patch misc-bcm-vk-convert-mmap-hook-to-mmap_prepare.patch ptp-vmclock-convert-mmap-hook-to-mmap_prepare.patch vhost-vdpa-convert-mmap-hook-to-mmap_prepare.patch mm-vma-error-out-on-invalid-bounds-in-unmapped_area_topdown.patch mm-vma-lock-vmas-before-taking-mm_all_locks_mutex.patch mm-filemap-convert-fault-and-readahead-logic-to-use-vma_flags_t.patch mm-madvise-convert-madvise_vma_behavior-to-use-vma_flags_t.patch mm-convert-miscellaneous-vma-flag-tests-to-use-vma_flags_t.patch fs-proc-task_mmu-convert-vma-flag-tests-to-use-vma_flags_t.patch fs-coredump-convert-vma_dump_size-to-use-vma_flags_t.patch uprobes-convert-vma-flag-tests-to-use-vma_flags_t.patch perf-convert-vma-flag-tests-to-use-vma_flags_t.patch selinux-convert-vma-flag-tests-to-use-vma_flags_t.patch apparmor-convert-vma-flag-tests-to-use-vma_flags_t.patch ima-convert-vma-flag-tests-to-use-vma_flags_t.patch ipe-convert-vma-flag-tests-to-use-vma_flags_t.patch coda-never-point-a-files-f_mapping-at-a-containers-mapping.patch fs-proc-task_mmu-write-protect-only-with-ptrace-attach.patch