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