From: Alexander Mikhalitsyn <alex@amutable.com>
To: Jiwoo Kim <arpe1618@gmail.com>, Christian Brauner <brauner@kernel.org>
Cc: Sargun Dhillon <sargun@sargun.me>, Arnd Bergmann <arnd@arndb.de>,
Jann Horn <jannh@google.com>, Chen Linxuan <me@black-desk.cn>,
linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] pidfd: restrict pidfd_getfd() to the caller's pid namespace
Date: Thu, 19 Nov 2026 14:42:16 +0100 [thread overview]
Message-ID: <3e4103cbcf7b76535e2f957762174515ef00f08d.camel@amutable.com> (raw)
In-Reply-To: <20261117152332.1221-1-arpe1618@gmail.com>
On Wed, 2026-11-18 at 00:23 +0900, Jiwoo Kim wrote:
> pidfds can cross pid namespace boundaries: a task can obtain one for
> a
> process outside of its own pid namespace hierarchy through
> SO_PASSPIDFD
> or SO_PEERPIDFD, via SCM_RIGHTS, or through file descriptor
> inheritance.
>
> Commit ee8323fdc827 ("pidfd: restrict task access ioctls to the
> caller's
> pid namespace") made the pidfs namespace ioctls fail with -EREMOTE
> unless the target is visible in the caller's active pid namespace, as
> PIDFD_GET_INFO already does, following the rule that operations which
> grant access to additional resources of the target require this [1].
>
Hi Jiwoo,
> pidfd_getfd() grants access to the target's open files, but it only
> performs the ptrace access check, which does not take the pid
> namespace
> relationship between the caller and the target into account. With the
> caller in a child pid namespace and in the same user namespace as the
> target, PIDFD_GET_INFO and PIDFD_GET_UTS_NAMESPACE fail with -EREMOTE
> for such a pidfd, while pidfd_getfd() on the same pidfd returns a
> duplicate of the target's file descriptor.
It it an interesting point, because pid namespace itself it not a
security boundary. While user namespace and capabilities are mechanisms
to manage permissions.
Currently, you can do something like this on your machine:
# Terminal 0
echo $$ # note this PID we will use it in "Terminal 1"
# Terminal 1
sudo unshare -f -p
echo hello > /proc/<PID>/fd/0
and this will actually print hello in the Terminal 0, while the process
doing that lives in nested pid namespace. But this is not a problem
because this guy is a root:
# Terminal 1
cat /proc/self/status | grep CapEff
So please understand me right, I'm not saying that your point is not
valid, but I'm not sure what was the intention behind pid_getfd() API
in the first place and we might want to have semantics and model close
to the one procfs has.
Speaking about other cases, where we *do* have this check:
- for exportfs operations it completely makes sense, because filehandle
contains an inode number and user space may accidentally (due to the
memory corruption or purposefully craft it). When I call
open_by_handle_at I *do* expect to get something from my own PID
namespace.
- pidfd_ioctl since ee8323fdc827c580425644db605a4be776cace57 ("pidfd:
restrict task access ioctls to the caller's pid namespace") doesn't
allow to open namespace files, while procfs has no problem with this
(it works you can try)
- pidfd_info() also forbids cross pidns information retrieval and for
part of it functionallity we can do it through procfs, but for exit
info or coredump we can't
To conclude, it looks like we have a little bit of inconsistencies
around here :-) [ But please, don't go and change anything in procfs as
we might break some tricky use-cases like CRIU and SCM_RIGHTS users. ]
IMHO.
Kind regards,
Alex
>
> Apply the same rule to pidfd_getfd() and fail with -EREMOTE unless
> the
> target is visible in the caller's active pid namespace. Callers in
> ancestor pid namespaces are unaffected.
>
> This was found with an AI coding assistant, which also drafted this
> patch and its changelog. The problem was reproduced in a QEMU guest,
> and the fix was tested there.
>
> Link:
> https://lore.kernel.org/all/20260925-gewischt-auftrag-tierzucht-c153cb3f9f74@brauner/
> [1]
> Fixes: 8649c322f75c ("pid: Implement pidfd_getfd syscall")
> Cc: stable@vger.kernel.org
> Signed-off-by: Jiwoo Kim <arpe1618@gmail.com>
> ---
> -EREMOTE is a new error for pidfd_getfd(). The check is done before
> the
> task lookup, so, as with PIDFD_GET_INFO, a reaped target outside of
> the
> caller's pid namespace hierarchy gives -EREMOTE rather than -ESRCH,
> and
> the check comes before the ptrace access check.
>
> setns() also accepts such pidfds without this check, but it requires
> CAP_SYS_ADMIN over the namespaces being joined, so it is left alone
> here.
>
> A reproducer is available on request.
>
> kernel/pid.c | 14 +++++++++++---
> 1 file changed, 11 insertions(+), 3 deletions(-)
>
> diff --git a/kernel/pid.c b/kernel/pid.c
> index adf721606768..4f9a29eb29de 100644
> --- a/kernel/pid.c
> +++ b/kernel/pid.c
> @@ -893,6 +893,13 @@ static int pidfd_getfd(struct pid *pid, int fd)
> struct file *file;
> int ret;
>
> + /*
> + * Require the target to be visible in the caller's pid
> namespace,
> + * like the pidfs ioctls that grant access to its resources.
> + */
> + if (!pid_vnr(pid))
> + return -EREMOTE;
> +
> task = get_pid_task(pid, PIDTYPE_PID);
> if (!task)
> return -ESRCH;
> @@ -917,9 +924,10 @@ static int pidfd_getfd(struct pid *pid, int fd)
> *
> * This syscall gets a copy of a file descriptor from another
> process
> * based on the pidfd, and file descriptor number. It requires that
> - * the calling process has the ability to ptrace the process
> represented
> - * by the pidfd. The process which is having its file descriptor
> copied
> - * is otherwise unaffected.
> + * the process represented by the pidfd is visible in the caller's
> pid
> + * namespace and that the calling process has the ability to ptrace
> it.
> + * The process which is having its file descriptor copied is
> otherwise
> + * unaffected.
> *
> * Return: On success, a cloexec file descriptor is returned.
> * On error, a negative errno number will be returned.
>
> base-commit: 44cc27050381e92de64cc3d7836376eda4d92a5f
next prev parent reply other threads:[~2026-11-19 13:42 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 15:23 [PATCH] pidfd: restrict pidfd_getfd() to the caller's pid namespace Jiwoo Kim
2026-11-17 16:20 ` Bradley Morgan
2026-11-19 13:42 ` Alexander Mikhalitsyn [this message]
2026-11-19 13:54 ` Jiwoo Kim
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=3e4103cbcf7b76535e2f957762174515ef00f08d.camel@amutable.com \
--to=alex@amutable.com \
--cc=arnd@arndb.de \
--cc=arpe1618@gmail.com \
--cc=brauner@kernel.org \
--cc=jannh@google.com \
--cc=linux-api@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=me@black-desk.cn \
--cc=sargun@sargun.me \
/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