Commit e1fb1dc0 authored by Christian Brauner's avatar Christian Brauner

pidfd: allow to override signal scope in pidfd_send_signal()

Right now we determine the scope of the signal based on the type of
pidfd. There are use-cases where it's useful to override the scope of
the signal. For example in [1]. Add flags to determine the scope of the
signal:

(1) PIDFD_SIGNAL_THREAD: send signal to specific thread reference by @pidfd
(2) PIDFD_SIGNAL_THREAD_GROUP: send signal to thread-group of @pidfd
(2) PIDFD_SIGNAL_PROCESS_GROUP: send signal to process-group of @pidfd

Since we now allow specifying PIDFD_SEND_PROCESS_GROUP for
pidfd_send_signal() to send signals to process groups we need to adjust
the check restricting si_code emulation by userspace to account for
PIDTYPE_PGID.
Reviewed-by: default avatarOleg Nesterov <oleg@redhat.com>
Link: https://github.com/systemd/systemd/issues/31093 [1]
Link: https://lore.kernel.org/r/20240210-chihuahua-hinzog-3945b6abd44a@brauner
Link: https://lore.kernel.org/r/20240214123655.GB16265@redhat.comSigned-off-by: default avatarChristian Brauner <brauner@kernel.org>
parent 81b9d8ac
...@@ -10,4 +10,9 @@ ...@@ -10,4 +10,9 @@
#define PIDFD_NONBLOCK O_NONBLOCK #define PIDFD_NONBLOCK O_NONBLOCK
#define PIDFD_THREAD O_EXCL #define PIDFD_THREAD O_EXCL
/* Flags for pidfd_send_signal(). */
#define PIDFD_SIGNAL_THREAD (1UL << 0)
#define PIDFD_SIGNAL_THREAD_GROUP (1UL << 1)
#define PIDFD_SIGNAL_PROCESS_GROUP (1UL << 2)
#endif /* _UAPI_LINUX_PIDFD_H */ #endif /* _UAPI_LINUX_PIDFD_H */
...@@ -1905,16 +1905,19 @@ int send_sig_fault_trapno(int sig, int code, void __user *addr, int trapno, ...@@ -1905,16 +1905,19 @@ int send_sig_fault_trapno(int sig, int code, void __user *addr, int trapno,
return send_sig_info(info.si_signo, &info, t); return send_sig_info(info.si_signo, &info, t);
} }
int kill_pgrp(struct pid *pid, int sig, int priv) static int kill_pgrp_info(int sig, struct kernel_siginfo *info, struct pid *pgrp)
{ {
int ret; int ret;
read_lock(&tasklist_lock); read_lock(&tasklist_lock);
ret = __kill_pgrp_info(sig, __si_special(priv), pid); ret = __kill_pgrp_info(sig, info, pgrp);
read_unlock(&tasklist_lock); read_unlock(&tasklist_lock);
return ret; return ret;
} }
int kill_pgrp(struct pid *pid, int sig, int priv)
{
return kill_pgrp_info(sig, __si_special(priv), pid);
}
EXPORT_SYMBOL(kill_pgrp); EXPORT_SYMBOL(kill_pgrp);
int kill_pid(struct pid *pid, int sig, int priv) int kill_pid(struct pid *pid, int sig, int priv)
...@@ -3873,6 +3876,10 @@ static struct pid *pidfd_to_pid(const struct file *file) ...@@ -3873,6 +3876,10 @@ static struct pid *pidfd_to_pid(const struct file *file)
return tgid_pidfd_to_pid(file); return tgid_pidfd_to_pid(file);
} }
#define PIDFD_SEND_SIGNAL_FLAGS \
(PIDFD_SIGNAL_THREAD | PIDFD_SIGNAL_THREAD_GROUP | \
PIDFD_SIGNAL_PROCESS_GROUP)
/** /**
* sys_pidfd_send_signal - Signal a process through a pidfd * sys_pidfd_send_signal - Signal a process through a pidfd
* @pidfd: file descriptor of the process * @pidfd: file descriptor of the process
...@@ -3897,7 +3904,11 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig, ...@@ -3897,7 +3904,11 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig,
enum pid_type type; enum pid_type type;
/* Enforce flags be set to 0 until we add an extension. */ /* Enforce flags be set to 0 until we add an extension. */
if (flags) if (flags & ~PIDFD_SEND_SIGNAL_FLAGS)
return -EINVAL;
/* Ensure that only a single signal scope determining flag is set. */
if (hweight32(flags & PIDFD_SEND_SIGNAL_FLAGS) > 1)
return -EINVAL; return -EINVAL;
f = fdget(pidfd); f = fdget(pidfd);
...@@ -3915,10 +3926,24 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig, ...@@ -3915,10 +3926,24 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig,
if (!access_pidfd_pidns(pid)) if (!access_pidfd_pidns(pid))
goto err; goto err;
if (f.file->f_flags & PIDFD_THREAD) switch (flags) {
case 0:
/* Infer scope from the type of pidfd. */
if (f.file->f_flags & PIDFD_THREAD)
type = PIDTYPE_PID;
else
type = PIDTYPE_TGID;
break;
case PIDFD_SIGNAL_THREAD:
type = PIDTYPE_PID; type = PIDTYPE_PID;
else break;
case PIDFD_SIGNAL_THREAD_GROUP:
type = PIDTYPE_TGID; type = PIDTYPE_TGID;
break;
case PIDFD_SIGNAL_PROCESS_GROUP:
type = PIDTYPE_PGID;
break;
}
if (info) { if (info) {
ret = copy_siginfo_from_user_any(&kinfo, info); ret = copy_siginfo_from_user_any(&kinfo, info);
...@@ -3931,14 +3956,17 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig, ...@@ -3931,14 +3956,17 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig,
/* Only allow sending arbitrary signals to yourself. */ /* Only allow sending arbitrary signals to yourself. */
ret = -EPERM; ret = -EPERM;
if ((task_pid(current) != pid) && if ((task_pid(current) != pid || type > PIDTYPE_TGID) &&
(kinfo.si_code >= 0 || kinfo.si_code == SI_TKILL)) (kinfo.si_code >= 0 || kinfo.si_code == SI_TKILL))
goto err; goto err;
} else { } else {
prepare_kill_siginfo(sig, &kinfo, type); prepare_kill_siginfo(sig, &kinfo, type);
} }
ret = kill_pid_info_type(sig, &kinfo, pid, type); if (type == PIDTYPE_PGID)
ret = kill_pgrp_info(sig, &kinfo, pid);
else
ret = kill_pid_info_type(sig, &kinfo, pid, type);
err: err:
fdput(f); fdput(f);
return ret; return ret;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment