Signaling every process inside a container from userspace via /proc is racy against concurrent fork() calls. Add the rootns_kill() system call 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/linux/syscalls.h | 1 + kernel/rootns.c | 71 ++++++++++++++++++++++++++++++++++++++++ kernel/sys_ni.c | 1 + 3 files changed, 73 insertions(+) diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h index 60e9c4ff71e7..a854d248c8a3 100644 --- a/include/linux/syscalls.h +++ b/include/linux/syscalls.h @@ -1009,6 +1009,7 @@ asmlinkage long sys_lsm_list_modules(u64 __user *ids, u32 __user *size, u32 flag asmlinkage long sys_rootns_create(unsigned int flags); asmlinkage long sys_rootns_wait(int rootnsfd, int __user *status, unsigned int options, struct rusage __user *ru); +asmlinkage long sys_rootns_kill(int rootnsfd, int sig); /* * Architecture-specific system calls diff --git a/kernel/rootns.c b/kernel/rootns.c index 5582ff010ca5..32bb8ef5ed58 100644 --- a/kernel/rootns.c +++ b/kernel/rootns.c @@ -408,3 +408,74 @@ COMPAT_SYSCALL_DEFINE4(rootns_wait, int, rootnsfd, return ret; } #endif + +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; +} + +/** + * sys_rootns_kill - Send a signal to all thread groups in a root namespace + * @rootnsfd: Root namespace control file descriptor + * @sig: Signal number to deliver (or 0 for permission check) + * + * Walk the target root namespace's PID namespace IDR under ``tasklist_lock`` + * and deliver @sig once to each member thread group. + * + * Return: 0 if at least one thread group was signaled, %-EXDEV if the caller's + * active PID namespace is not an ancestor, %-ESRCH if the rootns is not + * running or has no members, or negative error code on failure. + */ +SYSCALL_DEFINE2(rootns_kill, int, rootnsfd, int, sig) +{ + CLASS(fd, f)(rootnsfd); + + if (fd_empty(f)) + return -EBADF; + if (!is_rootns_file(fd_file(f)) || !valid_signal(sig)) + return -EINVAL; + + return rootns_kill_members(fd_file(f)->private_data, sig); +} diff --git a/kernel/sys_ni.c b/kernel/sys_ni.c index f9918d38107b..fba44c49ddbb 100644 --- a/kernel/sys_ni.c +++ b/kernel/sys_ni.c @@ -403,3 +403,4 @@ COND_SYSCALL(uprobe); COND_SYSCALL(rootns_create); COND_SYSCALL(rootns_wait); COND_SYSCALL_COMPAT(rootns_wait); +COND_SYSCALL(rootns_kill); -- 2.47.3