Skip to content

Commit 3b5bbe7

Browse files
committed
pidfd: prevent creation of pidfds for kthreads
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: 32fcb42 ("pid: add pidfd_open()") Cc: [email protected] Signed-off-by: Christian Brauner <[email protected]>
1 parent 889ced4 commit 3b5bbe7

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

kernel/fork.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,11 +2053,24 @@ static int __pidfd_prepare(struct pid *pid, unsigned int flags, struct file **re
20532053
*/
20542054
int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret)
20552055
{
2056-
bool thread = flags & PIDFD_THREAD;
2057-
2058-
if (!pid || !pid_has_task(pid, thread ? PIDTYPE_PID : PIDTYPE_TGID))
2056+
if (!pid)
20592057
return -EINVAL;
20602058

2059+
scoped_guard(rcu) {
2060+
struct task_struct *tsk;
2061+
2062+
if (flags & PIDFD_THREAD)
2063+
tsk = pid_task(pid, PIDTYPE_PID);
2064+
else
2065+
tsk = pid_task(pid, PIDTYPE_TGID);
2066+
if (!tsk)
2067+
return -EINVAL;
2068+
2069+
/* Don't create pidfds for kernel threads for now. */
2070+
if (tsk->flags & PF_KTHREAD)
2071+
return -EINVAL;
2072+
}
2073+
20612074
return __pidfd_prepare(pid, flags, ret);
20622075
}
20632076

@@ -2403,6 +2416,12 @@ __latent_entropy struct task_struct *copy_process(
24032416
if (clone_flags & CLONE_PIDFD) {
24042417
int flags = (clone_flags & CLONE_THREAD) ? PIDFD_THREAD : 0;
24052418

2419+
/* Don't create pidfds for kernel threads for now. */
2420+
if (args->kthread) {
2421+
retval = -EINVAL;
2422+
goto bad_fork_free_pid;
2423+
}
2424+
24062425
/* Note that no task has been attached to @pid yet. */
24072426
retval = __pidfd_prepare(pid, flags, &pidfile);
24082427
if (retval < 0)

0 commit comments

Comments
 (0)