xref: /linux/fs/signalfd.c (revision 5d7f3375cd574fc83f0cd30839b21455c0e79ae2)
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 void refine_sigmask(struct signalfd_ctx *ctx, sigset_t *sigmask)
52 {
53 	struct k_sigaction *k = current->sighand->action;
54 	int n;
55 
56 	*sigmask = ctx->sigmask;
57 	for (n = 1; n <= _NSIG; ++n, ++k) {
58 		if (k->sa.sa_flags & SA_IMMUTABLE)
59 			sigaddset(sigmask, n);
60 	}
61 }
62 
63 static __poll_t signalfd_poll(struct file *file, poll_table *wait)
64 {
65 	struct signalfd_ctx *ctx = file->private_data;
66 	__poll_t events = 0;
67 	sigset_t sigmask;
68 
69 	poll_wait(file, &current->sighand->signalfd_wqh, wait);
70 
71 	spin_lock_irq(&current->sighand->siglock);
72 	refine_sigmask(ctx, &sigmask);
73 	if (next_signal(&current->pending, &sigmask) ||
74 	    next_signal(&current->signal->shared_pending, &sigmask))
75 		events |= EPOLLIN;
76 	spin_unlock_irq(&current->sighand->siglock);
77 
78 	return events;
79 }
80 
81 /*
82  * Copied from copy_siginfo_to_user() in kernel/signal.c
83  */
84 static int signalfd_copyinfo(struct iov_iter *to, kernel_siginfo_t const *kinfo)
85 {
86 	struct signalfd_siginfo new;
87 
88 	BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128);
89 
90 	/*
91 	 * Unused members should be zero ...
92 	 */
93 	memset(&new, 0, sizeof(new));
94 
95 	/*
96 	 * If you change siginfo_t structure, please be sure
97 	 * this code is fixed accordingly.
98 	 */
99 	new.ssi_signo = kinfo->si_signo;
100 	new.ssi_errno = kinfo->si_errno;
101 	new.ssi_code  = kinfo->si_code;
102 	switch (siginfo_layout(kinfo->si_signo, kinfo->si_code)) {
103 	case SIL_KILL:
104 		new.ssi_pid = kinfo->si_pid;
105 		new.ssi_uid = kinfo->si_uid;
106 		break;
107 	case SIL_TIMER:
108 		new.ssi_tid = kinfo->si_tid;
109 		new.ssi_overrun = kinfo->si_overrun;
110 		new.ssi_ptr = (long) kinfo->si_ptr;
111 		new.ssi_int = kinfo->si_int;
112 		break;
113 	case SIL_POLL:
114 		new.ssi_band = kinfo->si_band;
115 		new.ssi_fd   = kinfo->si_fd;
116 		break;
117 	case SIL_FAULT_BNDERR:
118 	case SIL_FAULT_PKUERR:
119 	case SIL_FAULT_PERF_EVENT:
120 		/*
121 		 * Fall through to the SIL_FAULT case.  SIL_FAULT_BNDERR,
122 		 * SIL_FAULT_PKUERR, and SIL_FAULT_PERF_EVENT are only
123 		 * generated by faults that deliver them synchronously to
124 		 * userspace.  In case someone injects one of these signals
125 		 * and signalfd catches it treat it as SIL_FAULT.
126 		 */
127 	case SIL_FAULT:
128 		new.ssi_addr = (long) kinfo->si_addr;
129 		break;
130 	case SIL_FAULT_TRAPNO:
131 		new.ssi_addr = (long) kinfo->si_addr;
132 		new.ssi_trapno = kinfo->si_trapno;
133 		break;
134 	case SIL_FAULT_MCEERR:
135 		new.ssi_addr = (long) kinfo->si_addr;
136 		new.ssi_addr_lsb = (short) kinfo->si_addr_lsb;
137 		break;
138 	case SIL_CHLD:
139 		new.ssi_pid    = kinfo->si_pid;
140 		new.ssi_uid    = kinfo->si_uid;
141 		new.ssi_status = kinfo->si_status;
142 		new.ssi_utime  = kinfo->si_utime;
143 		new.ssi_stime  = kinfo->si_stime;
144 		break;
145 	case SIL_RT:
146 		/*
147 		 * This case catches also the signals queued by sigqueue().
148 		 */
149 		new.ssi_pid = kinfo->si_pid;
150 		new.ssi_uid = kinfo->si_uid;
151 		new.ssi_ptr = (long) kinfo->si_ptr;
152 		new.ssi_int = kinfo->si_int;
153 		break;
154 	case SIL_SYS:
155 		new.ssi_call_addr = (long) kinfo->si_call_addr;
156 		new.ssi_syscall   = kinfo->si_syscall;
157 		new.ssi_arch      = kinfo->si_arch;
158 		break;
159 	}
160 
161 	if (!copy_to_iter_full(&new, sizeof(struct signalfd_siginfo), to))
162 		return -EFAULT;
163 
164 	return sizeof(struct signalfd_siginfo);
165 }
166 
167 static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, kernel_siginfo_t *info,
168 				int nonblock)
169 {
170 	enum pid_type type;
171 	DECLARE_WAITQUEUE(wait, current);
172 	sigset_t sigmask;
173 	ssize_t ret;
174 
175 	spin_lock_irq(&current->sighand->siglock);
176 	refine_sigmask(ctx, &sigmask);
177 	ret = dequeue_signal(&sigmask, info, &type);
178 	switch (ret) {
179 	case 0:
180 		if (!nonblock)
181 			break;
182 		ret = -EAGAIN;
183 		fallthrough;
184 	default:
185 		spin_unlock_irq(&current->sighand->siglock);
186 		return ret;
187 	}
188 
189 	add_wait_queue(&current->sighand->signalfd_wqh, &wait);
190 	for (;;) {
191 		set_current_state(TASK_INTERRUPTIBLE);
192 		ret = dequeue_signal(&sigmask, info, &type);
193 		if (ret != 0)
194 			break;
195 		if (signal_pending(current)) {
196 			ret = -ERESTARTSYS;
197 			break;
198 		}
199 		spin_unlock_irq(&current->sighand->siglock);
200 		schedule();
201 		spin_lock_irq(&current->sighand->siglock);
202 		refine_sigmask(ctx, &sigmask);
203 	}
204 	spin_unlock_irq(&current->sighand->siglock);
205 
206 	remove_wait_queue(&current->sighand->signalfd_wqh, &wait);
207 	__set_current_state(TASK_RUNNING);
208 
209 	return ret;
210 }
211 
212 /*
213  * Returns a multiple of the size of a "struct signalfd_siginfo", or a negative
214  * error code. The "count" parameter must be at least the size of a
215  * "struct signalfd_siginfo".
216  */
217 static ssize_t signalfd_read_iter(struct kiocb *iocb, struct iov_iter *to)
218 {
219 	struct file *file = iocb->ki_filp;
220 	struct signalfd_ctx *ctx = file->private_data;
221 	size_t count = iov_iter_count(to);
222 	ssize_t ret, total = 0;
223 	kernel_siginfo_t info;
224 	bool nonblock;
225 
226 	count /= sizeof(struct signalfd_siginfo);
227 	if (!count)
228 		return -EINVAL;
229 
230 	nonblock = file->f_flags & O_NONBLOCK || iocb->ki_flags & IOCB_NOWAIT;
231 	do {
232 		ret = signalfd_dequeue(ctx, &info, nonblock);
233 		if (unlikely(ret <= 0))
234 			break;
235 		ret = signalfd_copyinfo(to, &info);
236 		if (ret < 0)
237 			break;
238 		total += ret;
239 		nonblock = 1;
240 	} while (--count);
241 
242 	return total ? total: ret;
243 }
244 
245 #ifdef CONFIG_PROC_FS
246 static void signalfd_show_fdinfo(struct seq_file *m, struct file *f)
247 {
248 	struct signalfd_ctx *ctx = f->private_data;
249 	sigset_t sigmask;
250 
251 	sigmask = ctx->sigmask;
252 	signotset(&sigmask);
253 	render_sigset_t(m, "sigmask:\t", &sigmask);
254 }
255 #endif
256 
257 static const struct file_operations signalfd_fops = {
258 #ifdef CONFIG_PROC_FS
259 	.show_fdinfo	= signalfd_show_fdinfo,
260 #endif
261 	.release	= signalfd_release,
262 	.poll		= signalfd_poll,
263 	.read_iter	= signalfd_read_iter,
264 	.llseek		= noop_llseek,
265 };
266 
267 static int do_signalfd4(int ufd, sigset_t *mask, int flags)
268 {
269 	/* Check the SFD_* constants for consistency.  */
270 	BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC);
271 	BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK);
272 
273 	if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
274 		return -EINVAL;
275 
276 	sigdelsetmask(mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
277 	signotset(mask);
278 
279 	if (ufd == -1) {
280 		int fd;
281 		struct signalfd_ctx *ctx __free(kfree) = NULL;
282 
283 		ctx = kmalloc_obj(*ctx);
284 		if (!ctx)
285 			return -ENOMEM;
286 
287 		ctx->sigmask = *mask;
288 
289 		fd = FD_ADD(flags & O_CLOEXEC,
290 			    anon_inode_getfile_fmode(
291 				    "[signalfd]", &signalfd_fops, ctx,
292 				    O_RDWR | (flags & O_NONBLOCK), FMODE_NOWAIT));
293 		if (fd >= 0)
294 			retain_and_null_ptr(ctx);
295 		return fd;
296 	} else {
297 		struct signalfd_ctx *ctx;
298 
299 		CLASS(fd, f)(ufd);
300 		if (fd_empty(f))
301 			return -EBADF;
302 		ctx = fd_file(f)->private_data;
303 		if (fd_file(f)->f_op != &signalfd_fops)
304 			return -EINVAL;
305 		spin_lock_irq(&current->sighand->siglock);
306 		ctx->sigmask = *mask;
307 		spin_unlock_irq(&current->sighand->siglock);
308 
309 		wake_up(&current->sighand->signalfd_wqh);
310 	}
311 
312 	return ufd;
313 }
314 
315 SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
316 		size_t, sizemask, int, flags)
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, flags);
325 }
326 
327 SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask,
328 		size_t, sizemask)
329 {
330 	sigset_t mask;
331 
332 	if (sizemask != sizeof(sigset_t))
333 		return -EINVAL;
334 	if (copy_from_user(&mask, user_mask, sizeof(mask)))
335 		return -EFAULT;
336 	return do_signalfd4(ufd, &mask, 0);
337 }
338 
339 #ifdef CONFIG_COMPAT
340 static long do_compat_signalfd4(int ufd,
341 			const compat_sigset_t __user *user_mask,
342 			compat_size_t sigsetsize, int flags)
343 {
344 	sigset_t mask;
345 
346 	if (sigsetsize != sizeof(compat_sigset_t))
347 		return -EINVAL;
348 	if (get_compat_sigset(&mask, user_mask))
349 		return -EFAULT;
350 	return do_signalfd4(ufd, &mask, flags);
351 }
352 
353 COMPAT_SYSCALL_DEFINE4(signalfd4, int, ufd,
354 		     const compat_sigset_t __user *, user_mask,
355 		     compat_size_t, sigsetsize,
356 		     int, flags)
357 {
358 	return do_compat_signalfd4(ufd, user_mask, sigsetsize, flags);
359 }
360 
361 COMPAT_SYSCALL_DEFINE3(signalfd, int, ufd,
362 		     const compat_sigset_t __user *, user_mask,
363 		     compat_size_t, sigsetsize)
364 {
365 	return do_compat_signalfd4(ufd, user_mask, sigsetsize, 0);
366 }
367 #endif
368