Signaling every process inside a container from userspace via /proc is racy against concurrent fork() calls. Add the ROOTNS_KILL ioctl to require an ancestor active PID namespace, walk the root namespace's PID namespace IDR under tasklist_lock, and deliver a validated signal once to each thread group including in-flight forks, aggregating per-process delivery results. Signed-off-by: Jarkko Sakkinen --- include/uapi/linux/rootns.h | 13 ++++++++++ kernel/rootns.c | 52 +++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/include/uapi/linux/rootns.h b/include/uapi/linux/rootns.h index 3fd215185398..acd91d895c63 100644 --- a/include/uapi/linux/rootns.h +++ b/include/uapi/linux/rootns.h @@ -67,4 +67,17 @@ struct rootns_wait_args { */ #define ROOTNS_WAIT _IOWR(ROOTNS_IOCTL_MAGIC, 0x02, struct rootns_wait_args) +/** + * DOC: ROOTNS_KILL + * + * ``ioctl(rootnsfd, ROOTNS_KILL, sig)`` walks the target root namespace's PID + * namespace IDR under ``tasklist_lock`` and delivers ``sig`` (or ``0`` for a + * permission check) once to each member thread group. + * + * Returns ``0`` if at least one thread group was signaled, or ``-1`` with + * ``errno`` set to ``EXDEV`` if the caller's active PID namespace is not an + * ancestor or ``ESRCH`` if the ``rootns`` is not running or has no members. + */ +#define ROOTNS_KILL _IO(ROOTNS_IOCTL_MAGIC, 0x03) + #endif /* _UAPI_LINUX_ROOTNS_H */ diff --git a/kernel/rootns.c b/kernel/rootns.c index 5f03b6ba4a54..ce012a07e982 100644 --- a/kernel/rootns.c +++ b/kernel/rootns.c @@ -154,6 +154,53 @@ static long rootns_wait(struct rootns *rootns, void __user *uarg) return ret; } +static int rootns_kill_members(struct rootns *rootns, int sig) +{ + struct pid_namespace *pid_ns = rootns->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 (!pidns_is_ancestor(pid_ns, task_active_pid_ns(current))) + return -EXDEV; + if (READ_ONCE(rootns->state) != ROOTNS_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 rootns_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { struct rootns *rootns = file->private_data; @@ -163,6 +210,10 @@ static long rootns_ioctl(struct file *file, unsigned int cmd, unsigned long arg) return rootns_get_nsfd(rootns, arg); case ROOTNS_WAIT: return rootns_wait(rootns, (void __user *)arg); + case ROOTNS_KILL: + if (arg > INT_MAX || !valid_signal(arg)) + return -EINVAL; + return rootns_kill_members(rootns, arg); default: return -ENOIOCTLCMD; } @@ -176,6 +227,7 @@ static long rootns_compat_ioctl(struct file *file, unsigned int cmd, case ROOTNS_WAIT: return rootns_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); case ROOTNS_GET_NSFD: + case ROOTNS_KILL: return rootns_ioctl(file, cmd, arg); default: return -ENOIOCTLCMD; -- 2.47.3