From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
To: "Jan Harkes" <jaharkes@cs.cmu.edu>,
coda@cs.cmu.edu, "Andrew Morton" <akpm@linux-foundation.org>,
"David Rheinsberg" <david@readahead.eu>,
"Hugh Dickins" <hughd@google.com>,
"Vlastimil Babka" <vbabka@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
"David Hildenbrand" <david@kernel.org>,
"Jann Horn" <jannh@google.com>, "Alex Shi" <alexs@kernel.org>,
"Johannes Weiner" <hannes@cmpxchg.org>,
"Pedro Falcato" <pfalcato@suse.de>,
"David Rientjes" <rientjes@google.com>,
"Rik van Riel" <riel@redhat.com>,
"Mike Rapoport" <rppt@kernel.org>,
"Suren Baghdasaryan" <surenb@google.com>,
"Michal Hocko" <mhocko@suse.com>,
"Jonathan Corbet" <corbet@lwn.net>,
"Shuah Khan" <skhan@linuxfoundation.org>,
"Randy Dunlap" <rdunlap@infradead.org>,
"Michał Mirosław" <mirq-linux@rere.qmqm.pl>,
"Arnd Bergmann" <arnd@arndb.de>,
"Stephen Rothwell" <sfr@canb.auug.org.au>,
"Muhammad Usama Anjum" <usama.anjum@arm.com>
Cc: codalist@coda.cs.cmu.edu, linux-mm@kvack.org,
Andrei Vagin <avagin@gmail.com>,
linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-fsdevel@vger.kernel.org,
"Lorenzo Stoakes (ARM)" <ljs@kernel.org>,
Chris Mason <mason@kernel.org>
Subject: [PATCH 4/4] fs/proc/task_mmu: write-protect only with ptrace attach
Date: Fri, 27 Nov 2026 19:45:43 +0000 [thread overview]
Message-ID: <20261127-mlock-madvise-mprotect-ai-fixes-v1-4-e46b2b1eb435@kernel.org> (raw)
In-Reply-To: <20261127-mlock-madvise-mprotect-ai-fixes-v1-0-e46b2b1eb435@kernel.org>
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.
Fixes: 52526ca7fdb9 ("fs/proc/task_mmu: implement IOCTL to get and optionally clear info about PTEs")
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
Documentation/admin-guide/mm/pagemap.rst | 4 ++-
fs/proc/task_mmu.c | 47 ++++++++++++++++++++++++++++----
2 files changed, 44 insertions(+), 7 deletions(-)
diff --git a/Documentation/admin-guide/mm/pagemap.rst b/Documentation/admin-guide/mm/pagemap.rst
index 77447c454632..f000296e5ab3 100644
--- a/Documentation/admin-guide/mm/pagemap.rst
+++ b/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.
diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index e3d894ed2b91..19acdddd4b23 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -3120,11 +3120,41 @@ static long pagemap_scan_flush_buffer(struct pagemap_scan_private *p)
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))
@@ -3134,6 +3164,13 @@ static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)
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);
@@ -3155,7 +3192,7 @@ static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)
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);
@@ -3164,7 +3201,7 @@ static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)
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);
@@ -3201,11 +3238,9 @@ static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)
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;
--
2.56.1
next prev parent reply other threads:[~2026-11-27 19:46 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 19:45 [PATCH 0/4] mm: AI-generated fixes for coda, madvise, mprotect and PAGEMAP_SCAN Lorenzo Stoakes (ARM)
2026-11-27 19:45 ` [PATCH 1/4] coda: never point a file's f_mapping at a container's mapping Lorenzo Stoakes (ARM)
2026-11-27 20:44 ` Andrew Morton
2026-11-27 22:05 ` Pedro Falcato
2026-11-27 22:01 ` Pedro Falcato
2026-11-27 19:45 ` [PATCH 2/4] mm/madvise: don't declare OOM for MADV_WILLNEED swapin Lorenzo Stoakes (ARM)
2026-11-27 22:14 ` Pedro Falcato
2026-11-27 19:45 ` [PATCH 3/4] mm/mprotect: fix unpaired notifier start at address 0 Lorenzo Stoakes (ARM)
2026-11-27 22:15 ` Pedro Falcato
2026-11-27 19:45 ` Lorenzo Stoakes (ARM) [this message]
2026-11-27 22:24 ` [PATCH 4/4] fs/proc/task_mmu: write-protect only with ptrace attach Pedro Falcato
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261127-mlock-madvise-mprotect-ai-fixes-v1-4-e46b2b1eb435@kernel.org \
--to=ljs@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=alexs@kernel.org \
--cc=arnd@arndb.de \
--cc=avagin@gmail.com \
--cc=coda@cs.cmu.edu \
--cc=codalist@coda.cs.cmu.edu \
--cc=corbet@lwn.net \
--cc=david@kernel.org \
--cc=david@readahead.eu \
--cc=hannes@cmpxchg.org \
--cc=hughd@google.com \
--cc=jaharkes@cs.cmu.edu \
--cc=jannh@google.com \
--cc=liam@infradead.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mason@kernel.org \
--cc=mhocko@suse.com \
--cc=mirq-linux@rere.qmqm.pl \
--cc=pfalcato@suse.de \
--cc=rdunlap@infradead.org \
--cc=riel@redhat.com \
--cc=rientjes@google.com \
--cc=rppt@kernel.org \
--cc=sfr@canb.auug.org.au \
--cc=skhan@linuxfoundation.org \
--cc=surenb@google.com \
--cc=usama.anjum@arm.com \
--cc=vbabka@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®