Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: linux-fsdevel@vger.kernel.org, Jann Horn <jannh@google.com>
Cc: Oleg Nesterov <oleg@redhat.com>,
	 Alexander Viro <viro@zeniv.linux.org.uk>,
	Jan Kara <jack@suse.cz>,  Jiwoo Kim <arpe1618@gmail.com>,
	Chen Linxuan <me@black-desk.cn>,
	 linux-mm@kvack.org,
	"Christian Brauner (Amutable)" <brauner@kernel.org>
Subject: [PATCH 3/3] pidfd: restrict pidfd_get_task() to the caller's pid namespace
Date: Fri, 20 Nov 2026 14:42:11 +0100	[thread overview]
Message-ID: <20261120-work-pidfd-pidns_visible-v1-3-0e82d08469ae@kernel.org> (raw)
In-Reply-To: <20261120-work-pidfd-pidns_visible-v1-0-0e82d08469ae@kernel.org>

Similar to the pidfs ioctls and pidfd_getfd() let's require that the
target of pidfd_get_task() is visible in the caller's active pid
namespace. The callers of pidfd_get_task() are process_madvise() and
process_mrelease() and both act on the target's address space.

Currently, a task can receive a pidfd for a process that it can't see
in its own pid namespace, e.g., via SCM_RIGHTS or by inheriting it
across setns() and fork(). process_madvise() only gates access on
ptrace_may_access() and process_mrelease() has no permission check at
all beyond requiring that the target is already dying. So both work on
processes the caller can't even name in its own pid namespace. That
doesn't make a lot of sense and it's inconsistent with the rest of the
pidfd api which refuses to hand out resources of invisible tasks.

Return -EREMOTE just like PIDFD_GET_INFO, the namespace and task path
ioctls, and pidfd_getfd() do. It's distinct from all errors that
process_madvise() and process_mrelease() return today, so userspace can
tell that the process exists but lives outside of its pid namespace.

PIDFD_SELF_THREAD and PIDFD_SELF_THREAD_GROUP aren't affected. They
refer to the caller itself or to its thread-group leader and
copy_process() refuses CLONE_THREAD if the new thread would end up in a
different pid namespace than the caller. So the check is only done for
real pidfds.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 kernel/pid.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/kernel/pid.c b/kernel/pid.c
index 72b98d0f35ab1..530aa93d6fdb5 100644
--- a/kernel/pid.c
+++ b/kernel/pid.c
@@ -636,9 +636,11 @@ struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags)
  *
  * Return the task associated with @pidfd. The function takes a reference on
  * the returned task. The caller is responsible for releasing that reference.
+ * The task must be visible in the caller's pid namespace.
  *
  * Return: On success, the task_struct associated with the pidfd.
- *	   On error, a negative errno number will be returned.
+ *	   On error, a negative errno number will be returned. If the task
+ *	   isn't visible in the caller's pid namespace -EREMOTE is returned.
  */
 struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags)
 {
@@ -660,6 +662,10 @@ struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags)
 		pid = pidfd_get_pid(pidfd, &f_flags);
 		if (IS_ERR(pid))
 			return ERR_CAST(pid);
+		if (!pid_in_current_pidns(pid)) {
+			put_pid(pid);
+			return ERR_PTR(-EREMOTE);
+		}
 		type = PIDTYPE_TGID;
 		break;
 	}

-- 
2.53.0



      parent reply	other threads:[~2026-11-20 13:42 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 13:42 [PATCH 0/3] pidfd: use one helper for pid namespace visibility checks Christian Brauner
2026-11-20 13:42 ` [PATCH 1/3] pid: move pid_in_current_pidns() into kernel/pid.c Christian Brauner
2026-11-20 14:10   ` Alexander Mikhalitsyn
2026-11-20 13:42 ` [PATCH 2/3] pidfd: convert remaining visibility checks to pid_in_current_pidns() Christian Brauner
2026-11-20 14:10   ` Alexander Mikhalitsyn
2026-11-20 13:42 ` Christian Brauner [this message]

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=20261120-work-pidfd-pidns_visible-v1-3-0e82d08469ae@kernel.org \
    --to=brauner@kernel.org \
    --cc=arpe1618@gmail.com \
    --cc=jack@suse.cz \
    --cc=jannh@google.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=me@black-desk.cn \
    --cc=oleg@redhat.com \
    --cc=viro@zeniv.linux.org.uk \
    /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