Linux Security Modules development
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: Jann Horn <jannh@google.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	 linux-fsdevel@vger.kernel.org,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	 linux-perf-users@vger.kernel.org, bpf@vger.kernel.org,
	 selinux@vger.kernel.org, Kees Cook <kees@kernel.org>,
	linux-mm@kvack.org,  linux-security-module@vger.kernel.org,
	apparmor@lists.ubuntu.com,  David Hildenbrand <david@kernel.org>,
	 "Christian Brauner (Amutable)" <brauner@kernel.org>
Subject: [PATCH RFC 17/19] proc: look up map_files vma without exec_update_lock held
Date: Tue, 24 Nov 2026 23:58:40 +0100	[thread overview]
Message-ID: <20261124-work-exec-update_lock-v1-17-e489035395b8@kernel.org> (raw)
In-Reply-To: <20261124-work-exec-update_lock-v1-0-e489035395b8@kernel.org>

Since commit 6255da28d4bb ("proc: protect ptrace_may_access() with
exec_update_lock (FD links)") the links in /proc/<pid>/map_files are
resolved with exec_update_lock of the task held. To find the file
map_files_get_link() looks up the vma and for that it waits for the mmap
lock of the mm.

So whoever reads such a link makes an exec of the task wait for the mmap
lock of its old mm although exec_mmap() doesn't take that lock anymore.
And everyone else who wants to look at the task queues up behind the
exec.

The lock is only needed for the access check and to get at the mm the
check was made for. So only do that with the lock held and look up the
vma afterwards.

The links don't go through call_proc_get_link() anymore so they don't
need ->proc_get_link().

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/proc/base.c | 68 ++++++++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 52 insertions(+), 16 deletions(-)

diff --git a/fs/proc/base.c b/fs/proc/base.c
index 7685660c36241..b28bd6c57e1d8 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -2228,18 +2228,40 @@ static const struct dentry_operations tid_map_files_dentry_operations = {
 	.d_delete	= pid_delete_dentry,
 };
 
-static int map_files_get_link(struct dentry *dentry, struct path *path,
-			      struct task_struct *task)
+/* Check the access and pin the mm with exec_update_lock held. */
+static struct mm_struct *map_files_mm(struct inode *inode)
+{
+	struct task_struct *task __free(put_task) = NULL;
+	struct mm_struct *mm;
+	int err;
+
+	task = get_proc_task(inode);
+	if (!task)
+		return ERR_PTR(-ENOENT);
+
+	err = down_read_killable(&task->signal->exec_update_lock);
+	if (err)
+		return ERR_PTR(err);
+	if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
+		mm = get_task_mm(task) ?: ERR_PTR(-ENOENT);
+	else
+		mm = ERR_PTR(-EACCES);
+	up_read(&task->signal->exec_update_lock);
+	return mm;
+}
+
+/* The vma lookup waits for the mmap lock so do it without exec_update_lock. */
+static int map_files_get_link(struct dentry *dentry, struct inode *inode,
+			      struct path *path)
 {
 	unsigned long vm_start, vm_end;
 	struct vm_area_struct *vma;
 	struct mm_struct *mm;
 	int rc;
 
-	rc = -ENOENT;
-	mm = get_task_mm(task);
-	if (!mm)
-		goto out;
+	mm = map_files_mm(inode);
+	if (IS_ERR(mm))
+		return PTR_ERR(mm);
 
 	rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
 	if (rc)
@@ -2260,7 +2282,6 @@ static int map_files_get_link(struct dentry *dentry, struct path *path,
 
 out_mmput:
 	mmput(mm);
-out:
 	return rc;
 }
 
@@ -2280,17 +2301,36 @@ proc_map_files_get_link(struct dentry *dentry,
 			struct inode *inode,
 		        struct delayed_call *done)
 {
+	struct path path;
+	int error;
+
 	if (!checkpoint_restore_ns_capable(&init_user_ns))
 		return ERR_PTR(-EPERM);
 
-	return proc_pid_get_link(dentry, inode, done);
+	if (!dentry)
+		return ERR_PTR(-ECHILD);
+	error = map_files_get_link(dentry, inode, &path);
+	if (!error)
+		error = nd_jump_link(&path);
+	return ERR_PTR(error);
+}
+
+static int proc_map_files_readlink(struct dentry *dentry, char __user *buffer,
+				   int buflen)
+{
+	struct path path;
+	int error;
+
+	error = map_files_get_link(dentry, d_inode(dentry), &path);
+	if (!error) {
+		error = do_proc_readlink(&path, buffer, buflen);
+		path_put(&path);
+	}
+	return error;
 }
 
-/*
- * Identical to proc_pid_link_inode_operations except for get_link()
- */
 static const struct inode_operations proc_map_files_link_inode_operations = {
-	.readlink	= proc_pid_readlink,
+	.readlink	= proc_map_files_readlink,
 	.get_link	= proc_map_files_get_link,
 	.setattr	= proc_nochmod_setattr,
 };
@@ -2300,7 +2340,6 @@ proc_map_files_instantiate(struct dentry *dentry,
 			   struct task_struct *task, const void *ptr)
 {
 	fmode_t mode = (fmode_t)(unsigned long)ptr;
-	struct proc_inode *ei;
 	struct inode *inode;
 
 	inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
@@ -2309,9 +2348,6 @@ proc_map_files_instantiate(struct dentry *dentry,
 	if (!inode)
 		return ERR_PTR(-ENOENT);
 
-	ei = PROC_I(inode);
-	ei->op.proc_get_link = map_files_get_link;
-
 	inode->i_op = &proc_map_files_link_inode_operations;
 	inode->i_size = 64;
 

-- 
2.53.0


  parent reply	other threads:[~2026-11-24 23:00 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 22:58 [PATCH RFC 00/19] exec: move sleeping work out of exec_update_lock Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 01/19] proc: copy namespace link after dropping exec_update_lock Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 02/19] kcmp: look up epoll target without exec_update_lock held Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 03/19] perf: copy counter values after dropping ctx->mutex Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 04/19] perf: copy ioctl arguments without ctx->mutex held Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 05/19] selinux: close inherited files that can't be replaced Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 06/19] exec: move unshare_sighand() before exec_mmap() Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 07/19] selftests/exec: test exec with a task sleeping in a page fault Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 08/19] selftests/exec: test exec with a shared signal table Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 09/19] lsm: add security_bprm_revalidate_files() Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 10/19] bpf: mark bprm_revalidate_files as sleepable Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 11/19] selinux: revalidate inherited files before taking exec_update_lock Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 12/19] apparmor: " Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 13/19] exec: drop exec_update_lock after the credentials are committed Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 14/19] exec: switch time namespace after dropping exec_update_lock Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 15/19] exec: don't take the mmap lock of the old mm Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 16/19] exit: don't take the mmap lock in exit_mm() Christian Brauner
2026-11-24 22:58 ` Christian Brauner [this message]
2026-11-24 23:09   ` [PATCH RFC 17/19] proc: look up map_files vma without exec_update_lock held sashiko-bot
2026-11-24 22:58 ` [PATCH RFC 18/19] vfork: wake up the parent once the child has left the mm Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 19/19] selftests/exec: test when a vfork() parent is woken up Christian Brauner
2026-11-24 23:15 ` [PATCH RFC 00/19] exec: move sleeping work out of exec_update_lock sashiko-bot

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=20261124-work-exec-update_lock-v1-17-e489035395b8@kernel.org \
    --to=brauner@kernel.org \
    --cc=acme@kernel.org \
    --cc=apparmor@lists.ubuntu.com \
    --cc=bpf@vger.kernel.org \
    --cc=david@kernel.org \
    --cc=jannh@google.com \
    --cc=kees@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=selinux@vger.kernel.org \
    --cc=torvalds@linux-foundation.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