Signaling every process inside an nsexec from userspace via /proc is
racy against concurrent fork() calls.

Teach pidfd_send_signal() to dispatch nsexec file descriptors to
nsexec_send_signal() when info is NULL and flags is 0. Require an
ancestor active PID namespace, walk the nsexec's PID namespace IDR
under tasklist_lock, and deliver a validated signal once to each thread
group including in-flight forks by member processes, aggregating
per-process delivery results.

Signed-off-by: Jarkko Sakkinen <[email protected]>
---
 include/linux/nsexec.h | 10 +++++++
 kernel/nsexec.c        | 59 ++++++++++++++++++++++++++++++++++++++++++
 kernel/signal.c        |  4 +++
 3 files changed, 73 insertions(+)

diff --git a/include/linux/nsexec.h b/include/linux/nsexec.h
index e6d772482d1f..6eee61c30f3c 100644
--- a/include/linux/nsexec.h
+++ b/include/linux/nsexec.h
@@ -20,6 +20,7 @@ struct nsproxy;
 struct pid;
 struct pid_namespace;
 struct rusage;
+struct siginfo;
 struct task_struct;
 struct user_namespace;
 struct waitid_info;
@@ -79,6 +80,8 @@ void nsexec_copy(struct pid *pid, struct nsexec *nsexec);
 void nsexec_exit(struct pid_namespace *pid_ns, struct task_struct *tsk);
 long nsexec_waitid(int upid, struct waitid_info *infop, int options,
                    struct rusage *ru);
+int nsexec_send_signal(struct file *file, int sig,
+                       struct siginfo __user *info, unsigned int flags);
 #else
 static inline void nsexec_put(struct nsexec *c)
 {
@@ -103,6 +106,13 @@ static inline long nsexec_waitid(int upid, struct 
waitid_info *infop,
 {
        return -EINVAL;
 }
+
+static inline int nsexec_send_signal(struct file *file, int sig,
+                                     struct siginfo __user *info,
+                                     unsigned int flags)
+{
+       return -EBADF;
+}
 #endif /* CONFIG_NSEXEC */
 
 #endif /* _LINUX_NSEXEC_H */
diff --git a/kernel/nsexec.c b/kernel/nsexec.c
index 1df16f70b456..895cc8c3429d 100644
--- a/kernel/nsexec.c
+++ b/kernel/nsexec.c
@@ -297,6 +297,65 @@ long nsexec_waitid(int upid, struct waitid_info *infop, 
int options,
        return ret;
 }
 
+/**
+ * DOC: pidfd_send_signal
+ *
+ * ``pidfd_send_signal(nsexec_fd, sig, NULL, 0)`` delivers ``sig`` (or ``0`` 
for
+ * a permission and existence probe) once to each member thread group in a
+ * running nsexec. ``info`` must be ``NULL``, ``flags`` must be ``0``, and the
+ * caller must run in an ancestor active PID namespace.
+ */
+int nsexec_send_signal(struct file *file, int sig,
+                       struct siginfo __user *info, unsigned int flags)
+{
+       struct nsexec *nsexec = file->private_data;
+       struct pid_namespace *pid_ns = nsexec->ns->pid_ns_for_children;
+       bool any_success = false;
+       bool any_other = false;
+       bool any_perm = false;
+       int other_err = 0;
+       struct pid *pid;
+       int nr;
+
+       if (info || flags || sig < 0 || !valid_signal(sig))
+               return -EINVAL;
+       if (!pidns_is_ancestor(pid_ns, task_active_pid_ns(current)))
+               return -EXDEV;
+       if (READ_ONCE(nsexec->state) != NSEXEC_RUNNING)
+               return -ESRCH;
+
+       scoped_guard(rcu) {
+               read_lock(&tasklist_lock);
+               idr_for_each_entry(&pid_ns->idr, pid, nr) {
+                       struct task_struct *task = pid_task(pid, PIDTYPE_TGID);
+                       int err;
+
+                       if (!task)
+                               continue;
+
+                       err = group_send_sig_info(sig, SEND_SIG_NOINFO, task,
+                                                 PIDTYPE_MAX);
+                       if (!err) {
+                               any_success = true;
+                       } else if (err == -EPERM) {
+                               any_perm = true;
+                       } else if (err != -ESRCH && !any_other) {
+                               any_other = true;
+                               other_err = err;
+                       }
+               }
+               read_unlock(&tasklist_lock);
+       }
+
+       if (any_success)
+               return 0;
+       if (any_other)
+               return other_err;
+       if (any_perm)
+               return -EPERM;
+       return -ESRCH;
+}
+
 static long nsexec_ioctl(struct file *file, unsigned int cmd, unsigned long 
arg)
 {
        struct nsexec *nsexec = file->private_data;
diff --git a/kernel/signal.c b/kernel/signal.c
index dd770e68faee..217e99c6ac63 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -37,6 +37,7 @@
 #include <linux/capability.h>
 #include <linux/freezer.h>
 #include <linux/pid_namespace.h>
+#include <linux/nsexec.h>
 #include <linux/nsproxy.h>
 #include <linux/user_namespace.h>
 #include <linux/uprobes.h>
@@ -4181,6 +4182,9 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig,
                if (fd_empty(f))
                        return -EBADF;
 
+               if (nsexec_is_file(fd_file(f)))
+                       return nsexec_send_signal(fd_file(f), sig, info, flags);
+
                /* Is this a pidfd? */
                pid = pidfd_to_pid(fd_file(f));
                if (IS_ERR(pid))
-- 
2.47.3


Reply via email to