linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] pidfd: prevent creation of pidfds for kthreads
@ 2024-07-31 10:01 Christian Brauner
  2024-07-31 14:51 ` Oleg Nesterov
  2024-08-18  3:58 ` Eric Biggers
  0 siblings, 2 replies; 13+ messages in thread
From: Christian Brauner @ 2024-07-31 10:01 UTC (permalink / raw)
  To: linux-fsdevel, linux-kernel
  Cc: Christian Brauner, Oleg Nesterov, Aleksa Sarai, Tycho Andersen,
	Daan De Meyer, Tejun Heo, stable

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.

Fixes: 32fcb426ec00 ("pid: add pidfd_open()")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
 kernel/fork.c | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/kernel/fork.c b/kernel/fork.c
index cc760491f201..18bdc87209d0 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -2053,11 +2053,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);
 }
 
@@ -2403,6 +2416,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)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2024-08-23  6:12 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-31 10:01 [PATCH] pidfd: prevent creation of pidfds for kthreads Christian Brauner
2024-07-31 14:51 ` Oleg Nesterov
2024-08-01  6:58   ` Christian Brauner
2024-08-01  8:01     ` Oleg Nesterov
2024-08-01 13:48       ` Christian Brauner
2024-08-01 13:59         ` Oleg Nesterov
2024-08-18  3:58 ` Eric Biggers
2024-08-19  8:41   ` Christian Brauner
2024-08-20 19:34     ` Eric Biggers
2024-08-21  7:41       ` Christian Brauner
2024-08-21  7:47         ` Daan De Meyer
2024-08-23  5:23     ` Linux regression tracking (Thorsten Leemhuis)
2024-08-23  6:12       ` Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).