summaryrefslogtreecommitdiff
path: root/kernel/fork.c
diff options
context:
space:
mode:
authorChristian Brauner <brauner@kernel.org>2025-04-04 16:06:55 +0200
committerChristian Brauner <brauner@kernel.org>2025-04-07 09:38:24 +0200
commit1e940fff94374d04b6c34f896ed9fbad3d2fb706 (patch)
treeb8b36d1b847d960dfb7bb9d76b4e477b65ebd656 /kernel/fork.c
parent0af2f6be1b4281385b618cb86ad946eded089ac8 (diff)
parent4fc3f73c16dae0211f31a963eedfb921f8366f57 (diff)
Merge patch series "pidfd: improve uapi when task isn't found"
Christian Brauner <brauner@kernel.org> says: We currently report EINVAL whenever a struct pid has no tasked attached anymore thereby conflating two concepts: (1) The task has already been reaped. (2) The caller requested a pidfd for a thread-group leader but the pid actually references a struct pid that isn't used as a thread-group leader. This is causing issues for non-threaded workloads as in [1] where they expect ESRCH to be reported, not EINVAL. I think that's a very resonable assumption. This patch tries to allow userspace to distinguish between (1) and (2). This is racy of course but that shouldn't matter. * patches from https://lore.kernel.org/r/20250403-work-pidfd-fixes-v1-0-a123b6ed6716@kernel.org: selftest/pidfd: add test for thread-group leader pidfd open for thread pidfd: improve uapi when task isn't found pidfd: remove unneeded NULL check from pidfd_prepare() selftests/pidfd: adapt to recent changes Link: https://lore.kernel.org/r/20250403-work-pidfd-fixes-v1-0-a123b6ed6716@kernel.org Signed-off-by: Christian Brauner <brauner@kernel.org>
Diffstat (limited to 'kernel/fork.c')
-rw-r--r--kernel/fork.c23
1 files changed, 20 insertions, 3 deletions
diff --git a/kernel/fork.c b/kernel/fork.c
index c4b26cd8998b..4a2080b968c8 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -2108,10 +2108,27 @@ static int __pidfd_prepare(struct pid *pid, unsigned int flags, struct file **re
*/
int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret)
{
- bool thread = flags & PIDFD_THREAD;
+ int err = 0;
- if (!pid || !pid_has_task(pid, thread ? PIDTYPE_PID : PIDTYPE_TGID))
- return -EINVAL;
+ if (!(flags & PIDFD_THREAD)) {
+ /*
+ * If this is struct pid isn't used as a thread-group
+ * leader pid but the caller requested to create a
+ * thread-group leader pidfd then report ENOENT to the
+ * caller as a hint.
+ */
+ if (!pid_has_task(pid, PIDTYPE_TGID))
+ err = -ENOENT;
+ }
+
+ /*
+ * If this wasn't a thread-group leader struct pid or the task
+ * got reaped in the meantime report -ESRCH to userspace.
+ */
+ if (!pid_has_task(pid, PIDTYPE_PID))
+ err = -ESRCH;
+ if (err)
+ return err;
return __pidfd_prepare(pid, flags, ret);
}