From: Chen Linxuan via B4 Relay <devnull+me.black-desk.cn@kernel.org>
To: Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
Andrew Morton <akpm@linux-foundation.org>,
David Hildenbrand <david@kernel.org>,
Lorenzo Stoakes <ljs@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
Vlastimil Babka <vbabka@kernel.org>,
Mike Rapoport <rppt@kernel.org>,
Suren Baghdasaryan <surenb@google.com>,
Michal Hocko <mhocko@suse.com>, Ingo Molnar <mingo@redhat.com>,
Peter Zijlstra <peterz@infradead.org>,
Juri Lelli <juri.lelli@redhat.com>,
Vincent Guittot <vincent.guittot@linaro.org>,
Dietmar Eggemann <dietmar.eggemann@arm.com>,
Steven Rostedt <rostedt@goodmis.org>,
Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
Valentin Schneider <vschneid@redhat.com>,
K Prateek Nayak <kprateek.nayak@amd.com>,
Kees Cook <kees@kernel.org>,
John Johansen <john.johansen@canonical.com>,
Georgia Garcia <georgia.garcia@canonical.com>,
Paul Moore <paul@paul-moore.com>,
James Morris <jmorris@namei.org>,
"Serge E. Hallyn" <serge@hallyn.com>
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-mm@kvack.org, apparmor@lists.ubuntu.com,
linux-security-module@vger.kernel.org,
Chen Linxuan <me@black-desk.cn>
Subject: [PATCH RFC 2/3] pidfd: Use scoped cleanup for task access
Date: Thu, 20 Aug 2026 13:45:40 +0800 [thread overview]
Message-ID: <20260820-pidfd-get-paths-v1-2-ac3eee4003d5@black-desk.cn> (raw)
In-Reply-To: <20260820-pidfd-get-paths-v1-0-ac3eee4003d5@black-desk.cn>
From: Chen Linxuan <me@black-desk.cn>
Split namespace acquisition from namespace fd creation and represent a
privileged target task as a scoped class that owns both the task
reference and exec_update_lock. Use the class for namespace lookups so
ptrace access checks and task state reads remain tied to the same exec
critical section, while open_namespace() stays outside the lock.
Assisted-by: codex:glm-5.3
Signed-off-by: Chen Linxuan <me@black-desk.cn>
---
fs/pidfs.c | 123 ++++++++++++++++++++++++++++++++++++++-----------------------
1 file changed, 77 insertions(+), 46 deletions(-)
diff --git a/fs/pidfs.c b/fs/pidfs.c
index a6a643f15d08..39e1e7ad9b2b 100644
--- a/fs/pidfs.c
+++ b/fs/pidfs.c
@@ -527,62 +527,71 @@ static bool pidfs_ioctl_valid(unsigned int cmd)
return false;
}
-static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+static inline void pidfd_put_task_locked(struct task_struct *task)
{
- struct task_struct *task __free(put_task) = NULL;
- struct nsproxy *nsp __free(put_nsproxy) = NULL;
- struct ns_common *ns_common = NULL;
- int error;
-
- if (!pidfs_ioctl_valid(cmd))
- return -ENOIOCTLCMD;
-
- if (cmd == FS_IOC_GETVERSION) {
- if (!arg)
- return -EINVAL;
-
- __u32 __user *argp = (__u32 __user *)arg;
- return put_user(file_inode(file)->i_generation, argp);
+ if (!IS_ERR_OR_NULL(task)) {
+ up_read(&task->signal->exec_update_lock);
+ put_task_struct(task);
}
+}
- /* Extensible IOCTL that does not open namespace FDs, take a shortcut */
- if (_IOC_NR(cmd) == _IOC_NR(PIDFD_GET_INFO))
- return pidfd_info(file, cmd, arg);
+/*
+ * Return @pid's task with @task's exec_update_lock held. The ptrace check
+ * and the callers' task state lookup must be performed while the lock is held
+ * so that they cannot race with a concurrent execve().
+ */
+static struct task_struct *pidfd_get_task_locked(struct pid *pid,
+ unsigned long arg)
+{
+ struct task_struct *task = get_pid_task(pid, PIDTYPE_PID);
+ int error;
- task = get_pid_task(pidfd_pid(file), PIDTYPE_PID);
if (!task)
- return -ESRCH;
+ return ERR_PTR(-ESRCH);
- if (arg)
- return -EINVAL;
+ if (arg) {
+ put_task_struct(task);
+ return ERR_PTR(-EINVAL);
+ }
- /*
- * We're trying to open a file descriptor to the namespace so perform a
- * filesystem cred ptrace check. Hold @task's exec_update_lock for the
- * duration of the ptrace check and the namespace lookup so that the
- * credentials used for the access decision match those of @task at the
- * time its namespace is read, preventing a concurrent execve() from
- * swapping the task's credentials in between the check and the use. We
- * mirror nsfs behavior.
- */
error = down_read_killable(&task->signal->exec_update_lock);
- if (error)
- return error;
+ if (error) {
+ put_task_struct(task);
+ return ERR_PTR(error);
+ }
if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
- error = -EACCES;
- goto out_unlock;
+ up_read(&task->signal->exec_update_lock);
+ put_task_struct(task);
+ return ERR_PTR(-EACCES);
}
+ return task;
+}
+
+DEFINE_CLASS(pidfd_task_locked, struct task_struct *,
+ pidfd_put_task_locked(_T),
+ pidfd_get_task_locked(pid, arg),
+ struct pid *pid, unsigned long arg)
+
+static struct ns_common *pidfd_get_namespace(struct pid *pid,
+ unsigned int cmd,
+ unsigned long arg)
+{
+ struct nsproxy *nsp __free(put_nsproxy) = NULL;
+ struct ns_common *ns_common = NULL;
+
+ CLASS(pidfd_task_locked, task)(pid, arg);
+ if (IS_ERR(task))
+ return ERR_CAST(task);
+
scoped_guard(task_lock, task) {
nsp = task->nsproxy;
if (nsp)
get_nsproxy(nsp);
}
- if (!nsp) {
- error = -ESRCH; /* just pretend it didn't exist */
- goto out_unlock;
- }
+ if (!nsp)
+ return ERR_PTR(-ESRCH); /* just pretend it didn't exist */
switch (cmd) {
/* Namespaces that hang of nsproxy. */
@@ -664,16 +673,38 @@ static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
#endif
break;
default:
- error = -ENOIOCTLCMD;
+ return ERR_PTR(-ENOIOCTLCMD);
+ }
+
+ if (!ns_common)
+ return ERR_PTR(-EOPNOTSUPP);
+
+ return ns_common;
+}
+
+static long pidfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+ struct ns_common *ns_common = NULL;
+
+ if (!pidfs_ioctl_valid(cmd))
+ return -ENOIOCTLCMD;
+
+ if (cmd == FS_IOC_GETVERSION) {
+ if (!arg)
+ return -EINVAL;
+
+ __u32 __user *argp = (__u32 __user *)arg;
+
+ return put_user(file_inode(file)->i_generation, argp);
}
- if (!error && !ns_common)
- error = -EOPNOTSUPP;
+ /* Extensible IOCTL that does not open namespace FDs, take a shortcut */
+ if (_IOC_NR(cmd) == _IOC_NR(PIDFD_GET_INFO))
+ return pidfd_info(file, cmd, arg);
-out_unlock:
- up_read(&task->signal->exec_update_lock);
- if (error)
- return error;
+ ns_common = pidfd_get_namespace(pidfd_pid(file), cmd, arg);
+ if (IS_ERR(ns_common))
+ return PTR_ERR(ns_common);
/* open_namespace() unconditionally consumes the reference */
return open_namespace(ns_common);
--
2.53.0
next prev parent reply other threads:[~2026-08-20 5:45 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-20 5:45 [PATCH RFC 0/3] pidfd: add task path ioctls Chen Linxuan via B4 Relay
2026-08-20 5:45 ` [PATCH RFC 1/3] fs: Introduce task path helpers Chen Linxuan via B4 Relay
2026-08-20 5:45 ` Chen Linxuan via B4 Relay [this message]
2026-08-20 5:45 ` [PATCH RFC 3/3] pidfd: Add task path ioctls Chen Linxuan via B4 Relay
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=20260820-pidfd-get-paths-v1-2-ac3eee4003d5@black-desk.cn \
--to=devnull+me.black-desk.cn@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=apparmor@lists.ubuntu.com \
--cc=brauner@kernel.org \
--cc=bsegall@google.com \
--cc=david@kernel.org \
--cc=dietmar.eggemann@arm.com \
--cc=georgia.garcia@canonical.com \
--cc=jack@suse.cz \
--cc=jmorris@namei.org \
--cc=john.johansen@canonical.com \
--cc=juri.lelli@redhat.com \
--cc=kees@kernel.org \
--cc=kprateek.nayak@amd.com \
--cc=liam@infradead.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-security-module@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=me@black-desk.cn \
--cc=mgorman@suse.de \
--cc=mhocko@suse.com \
--cc=mingo@redhat.com \
--cc=paul@paul-moore.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=rppt@kernel.org \
--cc=serge@hallyn.com \
--cc=surenb@google.com \
--cc=vbabka@kernel.org \
--cc=vincent.guittot@linaro.org \
--cc=viro@zeniv.linux.org.uk \
--cc=vschneid@redhat.com \
/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