Skip to content

Commit e1fb1dc

Browse files
committed
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: Oleg Nesterov <[email protected]> Link: systemd/systemd#31093 [1] Link: https://lore.kernel.org/r/20240210-chihuahua-hinzog-3945b6abd44a@brauner Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Christian Brauner <[email protected]>
1 parent 81b9d8a commit e1fb1dc

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

include/uapi/linux/pidfd.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@
1010
#define PIDFD_NONBLOCK O_NONBLOCK
1111
#define PIDFD_THREAD O_EXCL
1212

13+
/* Flags for pidfd_send_signal(). */
14+
#define PIDFD_SIGNAL_THREAD (1UL << 0)
15+
#define PIDFD_SIGNAL_THREAD_GROUP (1UL << 1)
16+
#define PIDFD_SIGNAL_PROCESS_GROUP (1UL << 2)
17+
1318
#endif /* _UAPI_LINUX_PIDFD_H */

kernel/signal.c

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,16 +1905,19 @@ int send_sig_fault_trapno(int sig, int code, void __user *addr, int trapno,
19051905
return send_sig_info(info.si_signo, &info, t);
19061906
}
19071907

1908-
int kill_pgrp(struct pid *pid, int sig, int priv)
1908+
static int kill_pgrp_info(int sig, struct kernel_siginfo *info, struct pid *pgrp)
19091909
{
19101910
int ret;
1911-
19121911
read_lock(&tasklist_lock);
1913-
ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1912+
ret = __kill_pgrp_info(sig, info, pgrp);
19141913
read_unlock(&tasklist_lock);
1915-
19161914
return ret;
19171915
}
1916+
1917+
int kill_pgrp(struct pid *pid, int sig, int priv)
1918+
{
1919+
return kill_pgrp_info(sig, __si_special(priv), pid);
1920+
}
19181921
EXPORT_SYMBOL(kill_pgrp);
19191922

19201923
int kill_pid(struct pid *pid, int sig, int priv)
@@ -3873,6 +3876,10 @@ static struct pid *pidfd_to_pid(const struct file *file)
38733876
return tgid_pidfd_to_pid(file);
38743877
}
38753878

3879+
#define PIDFD_SEND_SIGNAL_FLAGS \
3880+
(PIDFD_SIGNAL_THREAD | PIDFD_SIGNAL_THREAD_GROUP | \
3881+
PIDFD_SIGNAL_PROCESS_GROUP)
3882+
38763883
/**
38773884
* sys_pidfd_send_signal - Signal a process through a pidfd
38783885
* @pidfd: file descriptor of the process
@@ -3897,7 +3904,11 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig,
38973904
enum pid_type type;
38983905

38993906
/* Enforce flags be set to 0 until we add an extension. */
3900-
if (flags)
3907+
if (flags & ~PIDFD_SEND_SIGNAL_FLAGS)
3908+
return -EINVAL;
3909+
3910+
/* Ensure that only a single signal scope determining flag is set. */
3911+
if (hweight32(flags & PIDFD_SEND_SIGNAL_FLAGS) > 1)
39013912
return -EINVAL;
39023913

39033914
f = fdget(pidfd);
@@ -3915,10 +3926,24 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig,
39153926
if (!access_pidfd_pidns(pid))
39163927
goto err;
39173928

3918-
if (f.file->f_flags & PIDFD_THREAD)
3929+
switch (flags) {
3930+
case 0:
3931+
/* Infer scope from the type of pidfd. */
3932+
if (f.file->f_flags & PIDFD_THREAD)
3933+
type = PIDTYPE_PID;
3934+
else
3935+
type = PIDTYPE_TGID;
3936+
break;
3937+
case PIDFD_SIGNAL_THREAD:
39193938
type = PIDTYPE_PID;
3920-
else
3939+
break;
3940+
case PIDFD_SIGNAL_THREAD_GROUP:
39213941
type = PIDTYPE_TGID;
3942+
break;
3943+
case PIDFD_SIGNAL_PROCESS_GROUP:
3944+
type = PIDTYPE_PGID;
3945+
break;
3946+
}
39223947

39233948
if (info) {
39243949
ret = copy_siginfo_from_user_any(&kinfo, info);
@@ -3931,14 +3956,17 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig,
39313956

39323957
/* Only allow sending arbitrary signals to yourself. */
39333958
ret = -EPERM;
3934-
if ((task_pid(current) != pid) &&
3959+
if ((task_pid(current) != pid || type > PIDTYPE_TGID) &&
39353960
(kinfo.si_code >= 0 || kinfo.si_code == SI_TKILL))
39363961
goto err;
39373962
} else {
39383963
prepare_kill_siginfo(sig, &kinfo, type);
39393964
}
39403965

3941-
ret = kill_pid_info_type(sig, &kinfo, pid, type);
3966+
if (type == PIDTYPE_PGID)
3967+
ret = kill_pgrp_info(sig, &kinfo, pid);
3968+
else
3969+
ret = kill_pid_info_type(sig, &kinfo, pid, type);
39423970
err:
39433971
fdput(f);
39443972
return ret;

0 commit comments

Comments
 (0)