All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kyle Zeng <kylebot@openai.com>
To: linux-mm@kvack.org
Cc: Andrew Morton <akpm@linux-foundation.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	Lorenzo Stoakes <ljs@kernel.org>,
	Vlastimil Babka <vbabka@kernel.org>, Jann Horn <jannh@google.com>,
	Pedro Falcato <pfalcato@suse.de>, Kyle Zeng <kylebot@openai.com>,
	stable@vger.kernel.org
Subject: [PATCH] fs/proc/task_mmu: pin the mm during PAGEMAP_SCAN
Date: Fri, 23 Oct 2026 20:56:46 -0700	[thread overview]
Message-ID: <20261024035646.68348-1-kylebot@openai.com> (raw)

A pagemap file holds an mm_count reference, not an mm_users reference.
Unlike pagemap_read(), do_pagemap_scan() does not acquire a live-MM
reference before walking the page tables. Its mmap read lock does not
exclude the first phase of exit_mmap(), which also unmaps VMAs under the
read lock.

A process retaining a pagemap descriptor can therefore race the final
mmput() and access resources that have already been released. In
particular, a concurrent last userfaultfd close can free a context still
reachable through a dying MM's VMAs, and unmap_vmas() can free a shared
HugeTLB VMA's lock while the page walker is entering down_read(). KASAN
reports both a userfaultfd context read-after-free and a HugeTLB rwsem
write-after-free with unprivileged, read-only PAGEMAP_SCAN requests.

Acquire mmget_not_zero() after validating the ioctl arguments and keep
the reference for the entire scan, including across mmap lock drops and
MMU-notifier operations. Return -ESRCH if the MM is already dead, as
PROCMAP_QUERY does. Balance the reference on the bounce-buffer allocation
failure path and at the common exit. The pagemap file continues to hold
only its existing mm_count reference between operations.

Fixes: 52526ca7fdb9 ("fs/proc/task_mmu: implement IOCTL to get and optionally clear info about PTEs")
Cc: <stable@vger.kernel.org>
Signed-off-by: Kyle Zeng <kylebot@openai.com>
---
I have a reproducer for this issue.

 fs/proc/task_mmu.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 5c54aebe2118..d2cee8dcba58 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -3235,11 +3235,14 @@ static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)
 	if (ret)
 		return ret;
 
+	if (!mmget_not_zero(mm))
+		return -ESRCH;
+
 	p.masks_of_interest = p.arg.category_mask | p.arg.category_anyof_mask |
 			      p.arg.return_mask;
 	ret = pagemap_scan_init_bounce_buffer(&p);
 	if (ret)
-		return ret;
+		goto out_mm;
 
 	for (walk_start = p.arg.start; walk_start < p.arg.end;
 			walk_start = p.arg.walk_end) {
@@ -3294,6 +3297,8 @@ static long do_pagemap_scan(struct mm_struct *mm, unsigned long uarg)
 		ret = -EFAULT;
 
 	kfree(p.vec_buf);
+out_mm:
+	mmput(mm);
 	return ret;
 }
 


             reply	other threads:[~2026-10-24  3:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-24  3:56 Kyle Zeng [this message]
2026-10-27 14:03 ` [PATCH] fs/proc/task_mmu: pin the mm during PAGEMAP_SCAN David Hildenbrand (Arm)

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=20261024035646.68348-1-kylebot@openai.com \
    --to=kylebot@openai.com \
    --cc=akpm@linux-foundation.org \
    --cc=jannh@google.com \
    --cc=liam@infradead.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=pfalcato@suse.de \
    --cc=stable@vger.kernel.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.