Re: [PATCH] pidfd: restrict pidfd_getfd() to the caller's pid namespace

From: Chen Linxuan

Date: Fri Nov 20 2026 - 04:49:35 EST


On Thu Nov 19, 2026 at 9:54 PM CST, Jiwoo Kim wrote:
> On Thu, 2026-11-19 at 14:42 +0100, Alexander Mikhalitsyn wrote:
>> It it an interesting point, because pid namespace itself it not a
>> security boundary. While user namespace and capabilities are mechanisms
>> to manage permissions.
>
> Hi Alex,
>
> thanks for looking at this.
>
> Agreed, and the patch does not try to make it one. I followed the rule
> Christian suggested in [1], requiring pid_vnr(pid) != 0 for operations
> that grant access to additional resources of the target, which
> ee8323fdc827 then applied to the namespace ioctls. pidfd_getfd() hands
> out one of the target's open files, so it looked like it belongs to the
> same group.
>
>> Currently, you can do something like this on your machine:
> [...]
>> 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:
>
> Right. As far as I can see this also works for an unprivileged task of
> the same uid, as long as it can reach the outer task through a procfs
> instance of the outer pid namespace. So pidfd_getfd() currently behaves
> like procfs here, and the patch would make it stricter than procfs, in
> line with the pidfd ioctls and PIDFD_GET_INFO instead.
>
> The case I had in mind is the one Jann described in [2]: a task in a
> child pid namespace that gets a pidfd for an outer task over a socket.
> When it has no procfs view of the outer task, as in a container with its
> own procfs, that pidfd is its only handle to it. There, the ioctls refuse
> the pidfd with -EREMOTE, while pidfd_getfd() returns the target's file.
>
>> 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.
>
> Understood. If pidfd_getfd() should keep following the procfs model, I'm
> fine with dropping this patch; the inconsistency would then be on the
> ioctl side. I'll leave that to Christian.
>
>> 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. ]
>
> No plans to touch procfs.
>
> [1] https://lore.kernel.org/all/20260925-gewischt-auftrag-tierzucht-c153cb3f9f74@brauner/
> [2] https://lore.kernel.org/all/CAG48ez1T5-tUHVK_iHsgUtWa-inVdrZxA3E-ZCW0iENJvasKfg@xxxxxxxxxxxxxx/
>
> Thanks,
> Jiwoo Kim

pidfds provide process access without requiring procfs to be mounted, but that
does not mean they must expose every capability that procfs may expose in a
particular mount configuration. It is reasonable for pidfd operations to apply
stricter checks.

The existing handling of the pidfd namespace ioctls follows this principle. Even
when procfs may be able to access a target, PIDFD_GET_INFO and the namespace
ioctls require the target to be visible in the caller's active pid namespace.
This does not treat pid namespaces as independent security boundaries. It is a
conservative restriction to ensure that pidfd interfaces do not gain additional
access merely by bypassing procfs.

The original pidfd_getfd() commit, 8649c322f75c, used the following rationale:

> This syscall allows for the retrieval of file descriptors from other
> processes, based on their pidfd. This is possible using ptrace, and injection
> of parasitic code to inject code which leverages SCM_RIGHTS to move file
> descriptors between a tracee and a tracer.

The syscall documentation in the same commit says:

> 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.

That rationale assumes that the caller can actually ptrace the target, rather
than merely pass ptrace_may_access() for a task referenced by a pidfd. If the
target is outside the caller's pid namespace, the caller generally cannot
address and ptrace it through the ordinary pid-based interface. Allowing
pidfd_getfd() solely because a pidfd was transferred through SCM_RIGHTS,
SO_PASSPIDFD, SO_PEERPIDFD, or fd inheritance would therefore give pidfd an
operation beyond the ptrace-based justification in the original commit.