]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
pidfd: prevent creation of pidfds for kthreads
authorChristian Brauner <brauner@kernel.org>
Wed, 31 Jul 2024 10:01:12 +0000 (12:01 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 29 Aug 2024 15:35:44 +0000 (17:35 +0200)
commit 3b5bbe798b2451820e74243b738268f51901e7d0 upstream.

It's currently possible to create pidfds for kthreads but it is unclear
what that is supposed to mean. Until we have use-cases for it and we
figured out what behavior we want block the creation of pidfds for
kthreads.

Link: https://lore.kernel.org/r/20240731-gleis-mehreinnahmen-6bbadd128383@brauner
Fixes: 32fcb426ec00 ("pid: add pidfd_open()")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
kernel/fork.c

index 99076dbe27d83f93d704882d4f5c09ae103af303..5ac4558d480162a63a97584bafdcec3ba1bd7f97 100644 (file)
@@ -2069,11 +2069,24 @@ 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;
-
-       if (!pid || !pid_has_task(pid, thread ? PIDTYPE_PID : PIDTYPE_TGID))
+       if (!pid)
                return -EINVAL;
 
+       scoped_guard(rcu) {
+               struct task_struct *tsk;
+
+               if (flags & PIDFD_THREAD)
+                       tsk = pid_task(pid, PIDTYPE_PID);
+               else
+                       tsk = pid_task(pid, PIDTYPE_TGID);
+               if (!tsk)
+                       return -EINVAL;
+
+               /* Don't create pidfds for kernel threads for now. */
+               if (tsk->flags & PF_KTHREAD)
+                       return -EINVAL;
+       }
+
        return __pidfd_prepare(pid, flags, ret);
 }
 
@@ -2419,6 +2432,12 @@ __latent_entropy struct task_struct *copy_process(
        if (clone_flags & CLONE_PIDFD) {
                int flags = (clone_flags & CLONE_THREAD) ? PIDFD_THREAD : 0;
 
+               /* Don't create pidfds for kernel threads for now. */
+               if (args->kthread) {
+                       retval = -EINVAL;
+                       goto bad_fork_free_pid;
+               }
+
                /* Note that no task has been attached to @pid yet. */
                retval = __pidfd_prepare(pid, flags, &pidfile);
                if (retval < 0)