GNU Linux-libre 5.4.257-gnu1
[releases.git] / fs / signalfd.c
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
36 void signalfd_cleanup(struct sighand_struct *sighand)
37 {
38         wake_up_pollfree(&sighand->signalfd_wqh);
39 }
40
41 struct signalfd_ctx {
42         sigset_t sigmask;
43 };
44
45 static int signalfd_release(struct inode *inode, struct file *file)
46 {
47         kfree(file->private_data);
48         return 0;
49 }
50
51 static __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  */
71 static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo,
72                              kernel_siginfo_t const *kinfo)
73 {
74         struct signalfd_siginfo new;
75
76         BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128);
77
78         /*
79          * Unused members should be zero ...
80          */
81         memset(&new, 0, sizeof(new));
82
83         /*
84          * If you change siginfo_t structure, please be sure
85          * this code is fixed accordingly.
86          */
87         new.ssi_signo = kinfo->si_signo;
88         new.ssi_errno = kinfo->si_errno;
89         new.ssi_code  = kinfo->si_code;
90         switch (siginfo_layout(kinfo->si_signo, kinfo->si_code)) {
91         case SIL_KILL:
92                 new.ssi_pid = kinfo->si_pid;
93                 new.ssi_uid = kinfo->si_uid;
94                 break;
95         case SIL_TIMER:
96                 new.ssi_tid = kinfo->si_tid;
97                 new.ssi_overrun = kinfo->si_overrun;
98                 new.ssi_ptr = (long) kinfo->si_ptr;
99                 new.ssi_int = kinfo->si_int;
100                 break;
101         case SIL_POLL:
102                 new.ssi_band = kinfo->si_band;
103                 new.ssi_fd   = kinfo->si_fd;
104                 break;
105         case SIL_FAULT_BNDERR:
106         case SIL_FAULT_PKUERR:
107                 /*
108                  * Fall through to the SIL_FAULT case.  Both SIL_FAULT_BNDERR
109                  * and SIL_FAULT_PKUERR are only generated by faults that
110                  * deliver them synchronously to userspace.  In case someone
111                  * injects one of these signals and signalfd catches it treat
112                  * it as SIL_FAULT.
113                  */
114         case SIL_FAULT:
115                 new.ssi_addr = (long) kinfo->si_addr;
116 #ifdef __ARCH_SI_TRAPNO
117                 new.ssi_trapno = kinfo->si_trapno;
118 #endif
119                 break;
120         case SIL_FAULT_MCEERR:
121                 new.ssi_addr = (long) kinfo->si_addr;
122 #ifdef __ARCH_SI_TRAPNO
123                 new.ssi_trapno = kinfo->si_trapno;
124 #endif
125                 new.ssi_addr_lsb = (short) kinfo->si_addr_lsb;
126                 break;
127         case SIL_CHLD:
128                 new.ssi_pid    = kinfo->si_pid;
129                 new.ssi_uid    = kinfo->si_uid;
130                 new.ssi_status = kinfo->si_status;
131                 new.ssi_utime  = kinfo->si_utime;
132                 new.ssi_stime  = kinfo->si_stime;
133                 break;
134         case SIL_RT:
135                 /*
136                  * This case catches also the signals queued by sigqueue().
137                  */
138                 new.ssi_pid = kinfo->si_pid;
139                 new.ssi_uid = kinfo->si_uid;
140                 new.ssi_ptr = (long) kinfo->si_ptr;
141                 new.ssi_int = kinfo->si_int;
142                 break;
143         case SIL_SYS:
144                 new.ssi_call_addr = (long) kinfo->si_call_addr;
145                 new.ssi_syscall   = kinfo->si_syscall;
146                 new.ssi_arch      = kinfo->si_arch;
147                 break;
148         }
149
150         if (copy_to_user(uinfo, &new, sizeof(struct signalfd_siginfo)))
151                 return -EFAULT;
152
153         return sizeof(*uinfo);
154 }
155
156 static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, kernel_siginfo_t *info,
157                                 int nonblock)
158 {
159         ssize_t ret;
160         DECLARE_WAITQUEUE(wait, current);
161
162         spin_lock_irq(&current->sighand->siglock);
163         ret = dequeue_signal(current, &ctx->sigmask, info);
164         switch (ret) {
165         case 0:
166                 if (!nonblock)
167                         break;
168                 ret = -EAGAIN;
169                 /* fall through */
170         default:
171                 spin_unlock_irq(&current->sighand->siglock);
172                 return ret;
173         }
174
175         add_wait_queue(&current->sighand->signalfd_wqh, &wait);
176         for (;;) {
177                 set_current_state(TASK_INTERRUPTIBLE);
178                 ret = dequeue_signal(current, &ctx->sigmask, info);
179                 if (ret != 0)
180                         break;
181                 if (signal_pending(current)) {
182                         ret = -ERESTARTSYS;
183                         break;
184                 }
185                 spin_unlock_irq(&current->sighand->siglock);
186                 schedule();
187                 spin_lock_irq(&current->sighand->siglock);
188         }
189         spin_unlock_irq(&current->sighand->siglock);
190
191         remove_wait_queue(&current->sighand->signalfd_wqh, &wait);
192         __set_current_state(TASK_RUNNING);
193
194         return ret;
195 }
196
197 /*
198  * Returns a multiple of the size of a "struct signalfd_siginfo", or a negative
199  * error code. The "count" parameter must be at least the size of a
200  * "struct signalfd_siginfo".
201  */
202 static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,
203                              loff_t *ppos)
204 {
205         struct signalfd_ctx *ctx = file->private_data;
206         struct signalfd_siginfo __user *siginfo;
207         int nonblock = file->f_flags & O_NONBLOCK;
208         ssize_t ret, total = 0;
209         kernel_siginfo_t info;
210
211         count /= sizeof(struct signalfd_siginfo);
212         if (!count)
213                 return -EINVAL;
214
215         siginfo = (struct signalfd_siginfo __user *) buf;
216         do {
217                 ret = signalfd_dequeue(ctx, &info, nonblock);
218                 if (unlikely(ret <= 0))
219                         break;
220                 ret = signalfd_copyinfo(siginfo, &info);
221                 if (ret < 0)
222                         break;
223                 siginfo++;
224                 total += ret;
225                 nonblock = 1;
226         } while (--count);
227
228         return total ? total: ret;
229 }
230
231 #ifdef CONFIG_PROC_FS
232 static void signalfd_show_fdinfo(struct seq_file *m, struct file *f)
233 {
234         struct signalfd_ctx *ctx = f->private_data;
235         sigset_t sigmask;
236
237         sigmask = ctx->sigmask;
238         signotset(&sigmask);
239         render_sigset_t(m, "sigmask:\t", &sigmask);
240 }
241 #endif
242
243 static const struct file_operations signalfd_fops = {
244 #ifdef CONFIG_PROC_FS
245         .show_fdinfo    = signalfd_show_fdinfo,
246 #endif
247         .release        = signalfd_release,
248         .poll           = signalfd_poll,
249         .read           = signalfd_read,
250         .llseek         = noop_llseek,
251         .may_pollfree   = true,
252 };
253
254 static int do_signalfd4(int ufd, sigset_t *mask, int flags)
255 {
256         struct signalfd_ctx *ctx;
257
258         /* Check the SFD_* constants for consistency.  */
259         BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC);
260         BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK);
261
262         if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
263                 return -EINVAL;
264
265         sigdelsetmask(mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
266         signotset(mask);
267
268         if (ufd == -1) {
269                 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
270                 if (!ctx)
271                         return -ENOMEM;
272
273                 ctx->sigmask = *mask;
274
275                 /*
276                  * When we call this, the initialization must be complete, since
277                  * anon_inode_getfd() will install the fd.
278                  */
279                 ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx,
280                                        O_RDWR | (flags & (O_CLOEXEC | O_NONBLOCK)));
281                 if (ufd < 0)
282                         kfree(ctx);
283         } else {
284                 struct fd f = fdget(ufd);
285                 if (!f.file)
286                         return -EBADF;
287                 ctx = f.file->private_data;
288                 if (f.file->f_op != &signalfd_fops) {
289                         fdput(f);
290                         return -EINVAL;
291                 }
292                 spin_lock_irq(&current->sighand->siglock);
293                 ctx->sigmask = *mask;
294                 spin_unlock_irq(&current->sighand->siglock);
295
296                 wake_up(&current->sighand->signalfd_wqh);
297                 fdput(f);
298         }
299
300         return ufd;
301 }
302
303 SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
304                 size_t, sizemask, int, flags)
305 {
306         sigset_t mask;
307
308         if (sizemask != sizeof(sigset_t))
309                 return -EINVAL;
310         if (copy_from_user(&mask, user_mask, sizeof(mask)))
311                 return -EFAULT;
312         return do_signalfd4(ufd, &mask, flags);
313 }
314
315 SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask,
316                 size_t, sizemask)
317 {
318         sigset_t mask;
319
320         if (sizemask != sizeof(sigset_t))
321                 return -EINVAL;
322         if (copy_from_user(&mask, user_mask, sizeof(mask)))
323                 return -EFAULT;
324         return do_signalfd4(ufd, &mask, 0);
325 }
326
327 #ifdef CONFIG_COMPAT
328 static long do_compat_signalfd4(int ufd,
329                         const compat_sigset_t __user *user_mask,
330                         compat_size_t sigsetsize, int flags)
331 {
332         sigset_t mask;
333
334         if (sigsetsize != sizeof(compat_sigset_t))
335                 return -EINVAL;
336         if (get_compat_sigset(&mask, user_mask))
337                 return -EFAULT;
338         return do_signalfd4(ufd, &mask, flags);
339 }
340
341 COMPAT_SYSCALL_DEFINE4(signalfd4, int, ufd,
342                      const compat_sigset_t __user *, user_mask,
343                      compat_size_t, sigsetsize,
344                      int, flags)
345 {
346         return do_compat_signalfd4(ufd, user_mask, sigsetsize, flags);
347 }
348
349 COMPAT_SYSCALL_DEFINE3(signalfd, int, ufd,
350                      const compat_sigset_t __user *, user_mask,
351                      compat_size_t, sigsetsize)
352 {
353         return do_compat_signalfd4(ufd, user_mask, sigsetsize, 0);
354 }
355 #endif