]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame_incremental - fs/signalfd.c
Linux 6.16-rc4
[thirdparty/kernel/stable.git] / fs / signalfd.c
... / ...
CommitLineData
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * fs/signalfd.c
4 *
5 * Copyright (C) 2003 Linus Torvalds
6 *
7 * Mon Mar 5, 2007: Davide Libenzi <davidel@xmailserver.org>
8 * Changed ->read() to return a siginfo strcture instead of signal number.
9 * Fixed locking in ->poll().
10 * Added sighand-detach notification.
11 * Added fd re-use in sys_signalfd() syscall.
12 * Now using anonymous inode source.
13 * Thanks to Oleg Nesterov for useful code review and suggestions.
14 * More comments and suggestions from Arnd Bergmann.
15 * Sat May 19, 2007: Davi E. M. Arnaut <davi@haxent.com.br>
16 * Retrieve multiple signals with one read() call
17 * Sun Jul 15, 2007: Davide Libenzi <davidel@xmailserver.org>
18 * Attach to the sighand only during read() and poll().
19 */
20
21#include <linux/file.h>
22#include <linux/poll.h>
23#include <linux/init.h>
24#include <linux/fs.h>
25#include <linux/sched.h>
26#include <linux/slab.h>
27#include <linux/kernel.h>
28#include <linux/signal.h>
29#include <linux/list.h>
30#include <linux/anon_inodes.h>
31#include <linux/signalfd.h>
32#include <linux/syscalls.h>
33#include <linux/proc_fs.h>
34#include <linux/compat.h>
35
36void signalfd_cleanup(struct sighand_struct *sighand)
37{
38 wake_up_pollfree(&sighand->signalfd_wqh);
39}
40
41struct signalfd_ctx {
42 sigset_t sigmask;
43};
44
45static int signalfd_release(struct inode *inode, struct file *file)
46{
47 kfree(file->private_data);
48 return 0;
49}
50
51static __poll_t signalfd_poll(struct file *file, poll_table *wait)
52{
53 struct signalfd_ctx *ctx = file->private_data;
54 __poll_t events = 0;
55
56 poll_wait(file, &current->sighand->signalfd_wqh, wait);
57
58 spin_lock_irq(&current->sighand->siglock);
59 if (next_signal(&current->pending, &ctx->sigmask) ||
60 next_signal(&current->signal->shared_pending,
61 &ctx->sigmask))
62 events |= EPOLLIN;
63 spin_unlock_irq(&current->sighand->siglock);
64
65 return events;
66}
67
68/*
69 * Copied from copy_siginfo_to_user() in kernel/signal.c
70 */
71static int signalfd_copyinfo(struct iov_iter *to, kernel_siginfo_t const *kinfo)
72{
73 struct signalfd_siginfo new;
74
75 BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128);
76
77 /*
78 * Unused members should be zero ...
79 */
80 memset(&new, 0, sizeof(new));
81
82 /*
83 * If you change siginfo_t structure, please be sure
84 * this code is fixed accordingly.
85 */
86 new.ssi_signo = kinfo->si_signo;
87 new.ssi_errno = kinfo->si_errno;
88 new.ssi_code = kinfo->si_code;
89 switch (siginfo_layout(kinfo->si_signo, kinfo->si_code)) {
90 case SIL_KILL:
91 new.ssi_pid = kinfo->si_pid;
92 new.ssi_uid = kinfo->si_uid;
93 break;
94 case SIL_TIMER:
95 new.ssi_tid = kinfo->si_tid;
96 new.ssi_overrun = kinfo->si_overrun;
97 new.ssi_ptr = (long) kinfo->si_ptr;
98 new.ssi_int = kinfo->si_int;
99 break;
100 case SIL_POLL:
101 new.ssi_band = kinfo->si_band;
102 new.ssi_fd = kinfo->si_fd;
103 break;
104 case SIL_FAULT_BNDERR:
105 case SIL_FAULT_PKUERR:
106 case SIL_FAULT_PERF_EVENT:
107 /*
108 * Fall through to the SIL_FAULT case. SIL_FAULT_BNDERR,
109 * SIL_FAULT_PKUERR, and SIL_FAULT_PERF_EVENT are only
110 * generated by faults that deliver them synchronously to
111 * userspace. In case someone injects one of these signals
112 * and signalfd catches it treat it as SIL_FAULT.
113 */
114 case SIL_FAULT:
115 new.ssi_addr = (long) kinfo->si_addr;
116 break;
117 case SIL_FAULT_TRAPNO:
118 new.ssi_addr = (long) kinfo->si_addr;
119 new.ssi_trapno = kinfo->si_trapno;
120 break;
121 case SIL_FAULT_MCEERR:
122 new.ssi_addr = (long) kinfo->si_addr;
123 new.ssi_addr_lsb = (short) kinfo->si_addr_lsb;
124 break;
125 case SIL_CHLD:
126 new.ssi_pid = kinfo->si_pid;
127 new.ssi_uid = kinfo->si_uid;
128 new.ssi_status = kinfo->si_status;
129 new.ssi_utime = kinfo->si_utime;
130 new.ssi_stime = kinfo->si_stime;
131 break;
132 case SIL_RT:
133 /*
134 * This case catches also the signals queued by sigqueue().
135 */
136 new.ssi_pid = kinfo->si_pid;
137 new.ssi_uid = kinfo->si_uid;
138 new.ssi_ptr = (long) kinfo->si_ptr;
139 new.ssi_int = kinfo->si_int;
140 break;
141 case SIL_SYS:
142 new.ssi_call_addr = (long) kinfo->si_call_addr;
143 new.ssi_syscall = kinfo->si_syscall;
144 new.ssi_arch = kinfo->si_arch;
145 break;
146 }
147
148 if (!copy_to_iter_full(&new, sizeof(struct signalfd_siginfo), to))
149 return -EFAULT;
150
151 return sizeof(struct signalfd_siginfo);
152}
153
154static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, kernel_siginfo_t *info,
155 int nonblock)
156{
157 enum pid_type type;
158 ssize_t ret;
159 DECLARE_WAITQUEUE(wait, current);
160
161 spin_lock_irq(&current->sighand->siglock);
162 ret = dequeue_signal(&ctx->sigmask, info, &type);
163 switch (ret) {
164 case 0:
165 if (!nonblock)
166 break;
167 ret = -EAGAIN;
168 fallthrough;
169 default:
170 spin_unlock_irq(&current->sighand->siglock);
171 return ret;
172 }
173
174 add_wait_queue(&current->sighand->signalfd_wqh, &wait);
175 for (;;) {
176 set_current_state(TASK_INTERRUPTIBLE);
177 ret = dequeue_signal(&ctx->sigmask, info, &type);
178 if (ret != 0)
179 break;
180 if (signal_pending(current)) {
181 ret = -ERESTARTSYS;
182 break;
183 }
184 spin_unlock_irq(&current->sighand->siglock);
185 schedule();
186 spin_lock_irq(&current->sighand->siglock);
187 }
188 spin_unlock_irq(&current->sighand->siglock);
189
190 remove_wait_queue(&current->sighand->signalfd_wqh, &wait);
191 __set_current_state(TASK_RUNNING);
192
193 return ret;
194}
195
196/*
197 * Returns a multiple of the size of a "struct signalfd_siginfo", or a negative
198 * error code. The "count" parameter must be at least the size of a
199 * "struct signalfd_siginfo".
200 */
201static ssize_t signalfd_read_iter(struct kiocb *iocb, struct iov_iter *to)
202{
203 struct file *file = iocb->ki_filp;
204 struct signalfd_ctx *ctx = file->private_data;
205 size_t count = iov_iter_count(to);
206 ssize_t ret, total = 0;
207 kernel_siginfo_t info;
208 bool nonblock;
209
210 count /= sizeof(struct signalfd_siginfo);
211 if (!count)
212 return -EINVAL;
213
214 nonblock = file->f_flags & O_NONBLOCK || iocb->ki_flags & IOCB_NOWAIT;
215 do {
216 ret = signalfd_dequeue(ctx, &info, nonblock);
217 if (unlikely(ret <= 0))
218 break;
219 ret = signalfd_copyinfo(to, &info);
220 if (ret < 0)
221 break;
222 total += ret;
223 nonblock = 1;
224 } while (--count);
225
226 return total ? total: ret;
227}
228
229#ifdef CONFIG_PROC_FS
230static void signalfd_show_fdinfo(struct seq_file *m, struct file *f)
231{
232 struct signalfd_ctx *ctx = f->private_data;
233 sigset_t sigmask;
234
235 sigmask = ctx->sigmask;
236 signotset(&sigmask);
237 render_sigset_t(m, "sigmask:\t", &sigmask);
238}
239#endif
240
241static const struct file_operations signalfd_fops = {
242#ifdef CONFIG_PROC_FS
243 .show_fdinfo = signalfd_show_fdinfo,
244#endif
245 .release = signalfd_release,
246 .poll = signalfd_poll,
247 .read_iter = signalfd_read_iter,
248 .llseek = noop_llseek,
249};
250
251static int do_signalfd4(int ufd, sigset_t *mask, int flags)
252{
253 struct signalfd_ctx *ctx;
254
255 /* Check the SFD_* constants for consistency. */
256 BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC);
257 BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK);
258
259 if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
260 return -EINVAL;
261
262 sigdelsetmask(mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
263 signotset(mask);
264
265 if (ufd == -1) {
266 struct file *file;
267
268 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
269 if (!ctx)
270 return -ENOMEM;
271
272 ctx->sigmask = *mask;
273
274 ufd = get_unused_fd_flags(flags & O_CLOEXEC);
275 if (ufd < 0) {
276 kfree(ctx);
277 return ufd;
278 }
279
280 file = anon_inode_getfile_fmode("[signalfd]", &signalfd_fops,
281 ctx, O_RDWR | (flags & O_NONBLOCK),
282 FMODE_NOWAIT);
283 if (IS_ERR(file)) {
284 put_unused_fd(ufd);
285 kfree(ctx);
286 return PTR_ERR(file);
287 }
288 fd_install(ufd, file);
289 } else {
290 CLASS(fd, f)(ufd);
291 if (fd_empty(f))
292 return -EBADF;
293 ctx = fd_file(f)->private_data;
294 if (fd_file(f)->f_op != &signalfd_fops)
295 return -EINVAL;
296 spin_lock_irq(&current->sighand->siglock);
297 ctx->sigmask = *mask;
298 spin_unlock_irq(&current->sighand->siglock);
299
300 wake_up(&current->sighand->signalfd_wqh);
301 }
302
303 return ufd;
304}
305
306SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
307 size_t, sizemask, int, flags)
308{
309 sigset_t mask;
310
311 if (sizemask != sizeof(sigset_t))
312 return -EINVAL;
313 if (copy_from_user(&mask, user_mask, sizeof(mask)))
314 return -EFAULT;
315 return do_signalfd4(ufd, &mask, flags);
316}
317
318SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask,
319 size_t, sizemask)
320{
321 sigset_t mask;
322
323 if (sizemask != sizeof(sigset_t))
324 return -EINVAL;
325 if (copy_from_user(&mask, user_mask, sizeof(mask)))
326 return -EFAULT;
327 return do_signalfd4(ufd, &mask, 0);
328}
329
330#ifdef CONFIG_COMPAT
331static long do_compat_signalfd4(int ufd,
332 const compat_sigset_t __user *user_mask,
333 compat_size_t sigsetsize, int flags)
334{
335 sigset_t mask;
336
337 if (sigsetsize != sizeof(compat_sigset_t))
338 return -EINVAL;
339 if (get_compat_sigset(&mask, user_mask))
340 return -EFAULT;
341 return do_signalfd4(ufd, &mask, flags);
342}
343
344COMPAT_SYSCALL_DEFINE4(signalfd4, int, ufd,
345 const compat_sigset_t __user *, user_mask,
346 compat_size_t, sigsetsize,
347 int, flags)
348{
349 return do_compat_signalfd4(ufd, user_mask, sigsetsize, flags);
350}
351
352COMPAT_SYSCALL_DEFINE3(signalfd, int, ufd,
353 const compat_sigset_t __user *, user_mask,
354 compat_size_t, sigsetsize)
355{
356 return do_compat_signalfd4(ufd, user_mask, sigsetsize, 0);
357}
358#endif