GNU Linux-libre 4.14.266-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 unsigned int signalfd_poll(struct file *file, poll_table *wait)
52 {
53         struct signalfd_ctx *ctx = file->private_data;
54         unsigned int 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 |= POLLIN;
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                              siginfo_t const *kinfo)
73 {
74         long err;
75
76         BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128);
77
78         /*
79          * Unused members should be zero ...
80          */
81         err = __clear_user(uinfo, sizeof(*uinfo));
82
83         /*
84          * If you change siginfo_t structure, please be sure
85          * this code is fixed accordingly.
86          */
87         err |= __put_user(kinfo->si_signo, &uinfo->ssi_signo);
88         err |= __put_user(kinfo->si_errno, &uinfo->ssi_errno);
89         err |= __put_user(kinfo->si_code, &uinfo->ssi_code);
90         switch (siginfo_layout(kinfo->si_signo, kinfo->si_code)) {
91         case SIL_KILL:
92                 err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
93                 err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
94                 break;
95         case SIL_TIMER:
96                  err |= __put_user(kinfo->si_tid, &uinfo->ssi_tid);
97                  err |= __put_user(kinfo->si_overrun, &uinfo->ssi_overrun);
98                  err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
99                  err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
100                 break;
101         case SIL_POLL:
102                 err |= __put_user(kinfo->si_band, &uinfo->ssi_band);
103                 err |= __put_user(kinfo->si_fd, &uinfo->ssi_fd);
104                 break;
105         case SIL_FAULT:
106                 err |= __put_user((long) kinfo->si_addr, &uinfo->ssi_addr);
107 #ifdef __ARCH_SI_TRAPNO
108                 err |= __put_user(kinfo->si_trapno, &uinfo->ssi_trapno);
109 #endif
110 #ifdef BUS_MCEERR_AO
111                 /* 
112                  * Other callers might not initialize the si_lsb field,
113                  * so check explicitly for the right codes here.
114                  */
115                 if (kinfo->si_signo == SIGBUS &&
116                     (kinfo->si_code == BUS_MCEERR_AR ||
117                      kinfo->si_code == BUS_MCEERR_AO))
118                         err |= __put_user((short) kinfo->si_addr_lsb,
119                                           &uinfo->ssi_addr_lsb);
120 #endif
121                 break;
122         case SIL_CHLD:
123                 err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
124                 err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
125                 err |= __put_user(kinfo->si_status, &uinfo->ssi_status);
126                 err |= __put_user(kinfo->si_utime, &uinfo->ssi_utime);
127                 err |= __put_user(kinfo->si_stime, &uinfo->ssi_stime);
128                 break;
129         case SIL_RT:
130         default:
131                 /*
132                  * This case catches also the signals queued by sigqueue().
133                  */
134                 err |= __put_user(kinfo->si_pid, &uinfo->ssi_pid);
135                 err |= __put_user(kinfo->si_uid, &uinfo->ssi_uid);
136                 err |= __put_user((long) kinfo->si_ptr, &uinfo->ssi_ptr);
137                 err |= __put_user(kinfo->si_int, &uinfo->ssi_int);
138                 break;
139         }
140
141         return err ? -EFAULT: sizeof(*uinfo);
142 }
143
144 static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, siginfo_t *info,
145                                 int nonblock)
146 {
147         ssize_t ret;
148         DECLARE_WAITQUEUE(wait, current);
149
150         spin_lock_irq(&current->sighand->siglock);
151         ret = dequeue_signal(current, &ctx->sigmask, info);
152         switch (ret) {
153         case 0:
154                 if (!nonblock)
155                         break;
156                 ret = -EAGAIN;
157         default:
158                 spin_unlock_irq(&current->sighand->siglock);
159                 return ret;
160         }
161
162         add_wait_queue(&current->sighand->signalfd_wqh, &wait);
163         for (;;) {
164                 set_current_state(TASK_INTERRUPTIBLE);
165                 ret = dequeue_signal(current, &ctx->sigmask, info);
166                 if (ret != 0)
167                         break;
168                 if (signal_pending(current)) {
169                         ret = -ERESTARTSYS;
170                         break;
171                 }
172                 spin_unlock_irq(&current->sighand->siglock);
173                 schedule();
174                 spin_lock_irq(&current->sighand->siglock);
175         }
176         spin_unlock_irq(&current->sighand->siglock);
177
178         remove_wait_queue(&current->sighand->signalfd_wqh, &wait);
179         __set_current_state(TASK_RUNNING);
180
181         return ret;
182 }
183
184 /*
185  * Returns a multiple of the size of a "struct signalfd_siginfo", or a negative
186  * error code. The "count" parameter must be at least the size of a
187  * "struct signalfd_siginfo".
188  */
189 static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,
190                              loff_t *ppos)
191 {
192         struct signalfd_ctx *ctx = file->private_data;
193         struct signalfd_siginfo __user *siginfo;
194         int nonblock = file->f_flags & O_NONBLOCK;
195         ssize_t ret, total = 0;
196         siginfo_t info;
197
198         count /= sizeof(struct signalfd_siginfo);
199         if (!count)
200                 return -EINVAL;
201
202         siginfo = (struct signalfd_siginfo __user *) buf;
203         do {
204                 ret = signalfd_dequeue(ctx, &info, nonblock);
205                 if (unlikely(ret <= 0))
206                         break;
207                 ret = signalfd_copyinfo(siginfo, &info);
208                 if (ret < 0)
209                         break;
210                 siginfo++;
211                 total += ret;
212                 nonblock = 1;
213         } while (--count);
214
215         return total ? total: ret;
216 }
217
218 #ifdef CONFIG_PROC_FS
219 static void signalfd_show_fdinfo(struct seq_file *m, struct file *f)
220 {
221         struct signalfd_ctx *ctx = f->private_data;
222         sigset_t sigmask;
223
224         sigmask = ctx->sigmask;
225         signotset(&sigmask);
226         render_sigset_t(m, "sigmask:\t", &sigmask);
227 }
228 #endif
229
230 static const struct file_operations signalfd_fops = {
231 #ifdef CONFIG_PROC_FS
232         .show_fdinfo    = signalfd_show_fdinfo,
233 #endif
234         .release        = signalfd_release,
235         .poll           = signalfd_poll,
236         .read           = signalfd_read,
237         .llseek         = noop_llseek,
238 };
239
240 SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
241                 size_t, sizemask, int, flags)
242 {
243         sigset_t sigmask;
244         struct signalfd_ctx *ctx;
245
246         /* Check the SFD_* constants for consistency.  */
247         BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC);
248         BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK);
249
250         if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
251                 return -EINVAL;
252
253         if (sizemask != sizeof(sigset_t) ||
254             copy_from_user(&sigmask, user_mask, sizeof(sigmask)))
255                 return -EINVAL;
256         sigdelsetmask(&sigmask, sigmask(SIGKILL) | sigmask(SIGSTOP));
257         signotset(&sigmask);
258
259         if (ufd == -1) {
260                 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
261                 if (!ctx)
262                         return -ENOMEM;
263
264                 ctx->sigmask = sigmask;
265
266                 /*
267                  * When we call this, the initialization must be complete, since
268                  * anon_inode_getfd() will install the fd.
269                  */
270                 ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx,
271                                        O_RDWR | (flags & (O_CLOEXEC | O_NONBLOCK)));
272                 if (ufd < 0)
273                         kfree(ctx);
274         } else {
275                 struct fd f = fdget(ufd);
276                 if (!f.file)
277                         return -EBADF;
278                 ctx = f.file->private_data;
279                 if (f.file->f_op != &signalfd_fops) {
280                         fdput(f);
281                         return -EINVAL;
282                 }
283                 spin_lock_irq(&current->sighand->siglock);
284                 ctx->sigmask = sigmask;
285                 spin_unlock_irq(&current->sighand->siglock);
286
287                 wake_up(&current->sighand->signalfd_wqh);
288                 fdput(f);
289         }
290
291         return ufd;
292 }
293
294 SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask,
295                 size_t, sizemask)
296 {
297         return sys_signalfd4(ufd, user_mask, sizemask, 0);
298 }
299
300 #ifdef CONFIG_COMPAT
301 COMPAT_SYSCALL_DEFINE4(signalfd4, int, ufd,
302                      const compat_sigset_t __user *,sigmask,
303                      compat_size_t, sigsetsize,
304                      int, flags)
305 {
306         compat_sigset_t ss32;
307         sigset_t tmp;
308         sigset_t __user *ksigmask;
309
310         if (sigsetsize != sizeof(compat_sigset_t))
311                 return -EINVAL;
312         if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
313                 return -EFAULT;
314         sigset_from_compat(&tmp, &ss32);
315         ksigmask = compat_alloc_user_space(sizeof(sigset_t));
316         if (copy_to_user(ksigmask, &tmp, sizeof(sigset_t)))
317                 return -EFAULT;
318
319         return sys_signalfd4(ufd, ksigmask, sizeof(sigset_t), flags);
320 }
321
322 COMPAT_SYSCALL_DEFINE3(signalfd, int, ufd,
323                      const compat_sigset_t __user *,sigmask,
324                      compat_size_t, sigsetsize)
325 {
326         return compat_sys_signalfd4(ufd, sigmask, sigsetsize, 0);
327 }
328 #endif