Signaling every process inside a container from userspace via /proc is racy against concurrent fork() calls. Add the rootns_kill() system call to walk the root namespace's PID namespace IDR under RCU and deliver a validated signal once to each thread group, aggregating per-process delivery results. Signed-off-by: Jarkko Sakkinen --- include/linux/syscalls.h | 1 + kernel/rootns.c | 65 ++++++++++++++++++++++++++++++++++++++++ kernel/sys_ni.c | 1 + 3 files changed, 67 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 8a1254db69df..f59b2fe9910d 100644 --- a/kernel/rootns.c +++ b/kernel/rootns.c @@ -21,6 +21,7 @@ #include #include #include +#include #include /* @@ -390,3 +391,67 @@ 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 (READ_ONCE(rootns->state) != ROOTNS_RUNNING) + return -ESRCH; + + scoped_guard(rcu) { + idr_for_each_entry(&pid_ns->idr, pid, nr) { + int err; + + if (!pid_task(pid, PIDTYPE_TGID)) + continue; + + err = kill_pid(pid, sig, 0); + if (!err) { + any_success = true; + } else if (err == -EPERM) { + any_perm = true; + } else if (err != -ESRCH && !any_other) { + any_other = true; + other_err = err; + } + } + } + + 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 RCU and deliver + * @sig once to each member thread group. + * + * Return: 0 if at least one thread group was signaled, %-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