wait4() and waitid() only allow a parent or subreaper to collect exit
status, so an external manager holding a run file descriptor cannot
wait on a run init process forked by another process or observe
exit status multiple times across observers.

Add P_RUNFD to waitid() and wire kernel_waitid() to runfd_waitid()
to wait freezably and interruptibly on a run file descriptor until
the run reaches RUNFD_DEAD and populate siginfo_t with init's
caller-visible TGID, munged UID, and exit status without consuming it.

Co-developed-by: David Howells <[email protected]>
Signed-off-by: David Howells <[email protected]>
Signed-off-by: Jarkko Sakkinen <[email protected]>
---
 include/linux/runfd.h     | 10 +++++
 include/uapi/linux/wait.h |  1 +
 kernel/exit.c             |  4 ++
 kernel/runfd.c            | 83 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 98 insertions(+)

diff --git a/include/linux/runfd.h b/include/linux/runfd.h
index 8f18afee7d5b..eb2e8db5844f 100644
--- a/include/linux/runfd.h
+++ b/include/linux/runfd.h
@@ -19,8 +19,10 @@ struct file;
 struct nsproxy;
 struct pid;
 struct pid_namespace;
+struct rusage;
 struct task_struct;
 struct user_namespace;
+struct waitid_info;
 
 /**
  * enum runfd_state - run lifecycle states
@@ -75,6 +77,8 @@ void runfd_put(struct run *c);
 bool runfd_is_file(struct file *file);
 void runfd_copy(struct pid *pid, struct run *run);
 void runfd_exit(struct pid_namespace *pid_ns, struct task_struct *tsk);
+long runfd_waitid(int upid, struct waitid_info *infop, int options,
+                 struct rusage *ru);
 #else
 static inline void runfd_put(struct run *c)
 {
@@ -93,6 +97,12 @@ static inline void runfd_exit(struct pid_namespace *pid_ns,
                              struct task_struct *tsk)
 {
 }
+
+static inline long runfd_waitid(int upid, struct waitid_info *infop,
+                               int options, struct rusage *ru)
+{
+       return -EINVAL;
+}
 #endif /* CONFIG_RUNFD */
 
 #endif /* _LINUX_RUNFD_H */
diff --git a/include/uapi/linux/wait.h b/include/uapi/linux/wait.h
index 85b809fc9f11..fc36f4e12d6e 100644
--- a/include/uapi/linux/wait.h
+++ b/include/uapi/linux/wait.h
@@ -18,6 +18,7 @@
 #define P_PID          1
 #define P_PGID         2
 #define P_PIDFD                3
+#define P_RUNFD        4
 
 
 #endif /* _UAPI_LINUX_WAIT_H */
diff --git a/kernel/exit.c b/kernel/exit.c
index d5e7bc2af804..e8ec67d307b4 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -71,6 +71,7 @@
 #include <linux/unwind_deferred.h>
 #include <linux/uaccess.h>
 #include <linux/pidfs.h>
+#include <linux/runfd.h>
 
 #include <uapi/linux/wait.h>
 
@@ -1803,6 +1804,9 @@ static long kernel_waitid(int which, pid_t upid, struct 
waitid_info *infop,
        struct wait_opts wo;
        long ret;
 
+       if (which == P_RUNFD)
+               return runfd_waitid(upid, infop, options, ru);
+
        ret = kernel_waitid_prepare(&wo, which, upid, infop, options, ru);
        if (ret)
                return ret;
diff --git a/kernel/runfd.c b/kernel/runfd.c
index 45e5709dbc0e..52a07a108705 100644
--- a/kernel/runfd.c
+++ b/kernel/runfd.c
@@ -17,6 +17,7 @@
 #include <linux/wait.h>
 #include <uapi/linux/wait.h>
 #include <linux/file.h>
+#include <linux/freezer.h>
 #include <linux/fs.h>
 #include <linux/anon_inodes.h>
 #include <linux/runfd.h>
@@ -42,6 +43,8 @@
 #include <linux/idr.h>
 #include <uapi/linux/runfd.h>
 
+#include "exit.h"
+
 void runfd_put(struct run *c)
 {
        if (!c || !refcount_dec_and_test(&c->usage))
@@ -215,6 +218,86 @@ static long runfd_share_nsfd(struct run *c, unsigned long 
arg)
        return 0;
 }
 
+/**
+ * DOC: P_RUNFD
+ *
+ * ``waitid(P_RUNFD, run_fd, infop, options, NULL)`` waits freezably until
+ * ``run_fd`` reaches ``RUNFD_DEAD`` (unless ``WNOHANG`` or ``O_NONBLOCK`` is
+ * set) and writes init's exit information into ``infop`` without consuming it.
+ * ``options`` must include ``WEXITED`` and may include ``WNOHANG`` and
+ * ``WNOWAIT``; ``rusage`` must be ``NULL``, and the caller must run in an
+ * ancestor active PID namespace.
+ */
+long runfd_waitid(int upid, struct waitid_info *infop, int options,
+                 struct rusage *ru)
+{
+       struct pid_namespace *pid_ns;
+       unsigned int f_flags;
+       struct run *run;
+       bool nonblock;
+       int status;
+       kuid_t uid;
+       long ret;
+
+       if (upid < 0 || ru)
+               return -EINVAL;
+       if (options & ~(WEXITED | WNOHANG | WNOWAIT))
+               return -EINVAL;
+       if (!(options & WEXITED))
+               return -EINVAL;
+
+       CLASS(fd, f)(upid);
+       if (fd_empty(f) || !runfd_is_file(fd_file(f)))
+               return -EBADF;
+
+       f_flags = READ_ONCE(fd_file(f)->f_flags);
+       nonblock = (options & WNOHANG) || (f_flags & O_NONBLOCK);
+       run = fd_file(f)->private_data;
+       pid_ns = task_active_pid_ns(current);
+
+       if (!pidns_is_ancestor(run->ns->pid_ns_for_children, pid_ns))
+               return -ECHILD;
+       for (;;) {
+               enum runfd_state state;
+
+               scoped_guard(spinlock, &run->lock) {
+                       state = run->state;
+                       if (state == RUNFD_INIT)
+                               return -ECHILD;
+                       if (state == RUNFD_DEAD) {
+                               ret = pid_nr_ns(run->init_pid, pid_ns);
+                               if (ret <= 0)
+                                       return -ECHILD;
+                               status = run->exit_code;
+                               uid = run->init_uid;
+                       }
+               }
+               if (state == RUNFD_DEAD)
+                       break;
+               if (nonblock)
+                       return (options & WNOHANG) ? 0 : -EAGAIN;
+
+               ret = wait_event_freezable(run->waitq,
+                                          READ_ONCE(run->state) ==
+                                          RUNFD_DEAD ||
+                                          READ_ONCE(run->state) ==
+                                          RUNFD_INIT);
+               if (ret)
+                       return ret;
+       }
+
+       if ((status & 0x7f) == 0) {
+               infop->cause = CLD_EXITED;
+               infop->status = status >> 8;
+       } else {
+               infop->cause = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
+               infop->status = status & 0x7f;
+       }
+       infop->pid = ret;
+       infop->uid = from_kuid_munged(current_user_ns(), uid);
+       return ret;
+}
+
 static long runfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
        struct run *run = file->private_data;
-- 
2.47.3


Reply via email to