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, ¤t->sighand->signalfd_wqh, wait); 57 58 spin_lock_irq(¤t->sighand->siglock); 59 if (next_signal(¤t->pending, &ctx->sigmask) || 60 next_signal(¤t->signal->shared_pending, 61 &ctx->sigmask)) 62 events |= EPOLLIN; 63 spin_unlock_irq(¤t->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 case SIL_FAULT_PERF_EVENT: 108 /* 109 * Fall through to the SIL_FAULT case. SIL_FAULT_BNDERR, 110 * SIL_FAULT_PKUERR, and SIL_FAULT_PERF_EVENT are only 111 * generated by faults that deliver them synchronously to 112 * userspace. In case someone injects one of these signals 113 * and signalfd catches it treat it as SIL_FAULT. 114 */ 115 case SIL_FAULT: 116 new.ssi_addr = (long) kinfo->si_addr; 117 break; 118 case SIL_FAULT_TRAPNO: 119 new.ssi_addr = (long) kinfo->si_addr; 120 new.ssi_trapno = kinfo->si_trapno; 121 break; 122 case SIL_FAULT_MCEERR: 123 new.ssi_addr = (long) kinfo->si_addr; 124 new.ssi_addr_lsb = (short) kinfo->si_addr_lsb; 125 break; 126 case SIL_CHLD: 127 new.ssi_pid = kinfo->si_pid; 128 new.ssi_uid = kinfo->si_uid; 129 new.ssi_status = kinfo->si_status; 130 new.ssi_utime = kinfo->si_utime; 131 new.ssi_stime = kinfo->si_stime; 132 break; 133 case SIL_RT: 134 /* 135 * This case catches also the signals queued by sigqueue(). 136 */ 137 new.ssi_pid = kinfo->si_pid; 138 new.ssi_uid = kinfo->si_uid; 139 new.ssi_ptr = (long) kinfo->si_ptr; 140 new.ssi_int = kinfo->si_int; 141 break; 142 case SIL_SYS: 143 new.ssi_call_addr = (long) kinfo->si_call_addr; 144 new.ssi_syscall = kinfo->si_syscall; 145 new.ssi_arch = kinfo->si_arch; 146 break; 147 } 148 149 if (copy_to_user(uinfo, &new, sizeof(struct signalfd_siginfo))) 150 return -EFAULT; 151 152 return sizeof(*uinfo); 153 } 154 155 static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, kernel_siginfo_t *info, 156 int nonblock) 157 { 158 ssize_t ret; 159 DECLARE_WAITQUEUE(wait, current); 160 161 spin_lock_irq(¤t->sighand->siglock); 162 ret = dequeue_signal(current, &ctx->sigmask, info); 163 switch (ret) { 164 case 0: 165 if (!nonblock) 166 break; 167 ret = -EAGAIN; 168 fallthrough; 169 default: 170 spin_unlock_irq(¤t->sighand->siglock); 171 return ret; 172 } 173 174 add_wait_queue(¤t->sighand->signalfd_wqh, &wait); 175 for (;;) { 176 set_current_state(TASK_INTERRUPTIBLE); 177 ret = dequeue_signal(current, &ctx->sigmask, info); 178 if (ret != 0) 179 break; 180 if (signal_pending(current)) { 181 ret = -ERESTARTSYS; 182 break; 183 } 184 spin_unlock_irq(¤t->sighand->siglock); 185 schedule(); 186 spin_lock_irq(¤t->sighand->siglock); 187 } 188 spin_unlock_irq(¤t->sighand->siglock); 189 190 remove_wait_queue(¤t->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 */ 201 static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count, 202 loff_t *ppos) 203 { 204 struct signalfd_ctx *ctx = file->private_data; 205 struct signalfd_siginfo __user *siginfo; 206 int nonblock = file->f_flags & O_NONBLOCK; 207 ssize_t ret, total = 0; 208 kernel_siginfo_t info; 209 210 count /= sizeof(struct signalfd_siginfo); 211 if (!count) 212 return -EINVAL; 213 214 siginfo = (struct signalfd_siginfo __user *) buf; 215 do { 216 ret = signalfd_dequeue(ctx, &info, nonblock); 217 if (unlikely(ret <= 0)) 218 break; 219 ret = signalfd_copyinfo(siginfo, &info); 220 if (ret < 0) 221 break; 222 siginfo++; 223 total += ret; 224 nonblock = 1; 225 } while (--count); 226 227 return total ? total: ret; 228 } 229 230 #ifdef CONFIG_PROC_FS 231 static void signalfd_show_fdinfo(struct seq_file *m, struct file *f) 232 { 233 struct signalfd_ctx *ctx = f->private_data; 234 sigset_t sigmask; 235 236 sigmask = ctx->sigmask; 237 signotset(&sigmask); 238 render_sigset_t(m, "sigmask:\t", &sigmask); 239 } 240 #endif 241 242 static const struct file_operations signalfd_fops = { 243 #ifdef CONFIG_PROC_FS 244 .show_fdinfo = signalfd_show_fdinfo, 245 #endif 246 .release = signalfd_release, 247 .poll = signalfd_poll, 248 .read = signalfd_read, 249 .llseek = noop_llseek, 250 }; 251 252 static int do_signalfd4(int ufd, sigset_t *mask, int flags) 253 { 254 struct signalfd_ctx *ctx; 255 256 /* Check the SFD_* constants for consistency. */ 257 BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC); 258 BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK); 259 260 if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK)) 261 return -EINVAL; 262 263 sigdelsetmask(mask, sigmask(SIGKILL) | sigmask(SIGSTOP)); 264 signotset(mask); 265 266 if (ufd == -1) { 267 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); 268 if (!ctx) 269 return -ENOMEM; 270 271 ctx->sigmask = *mask; 272 273 /* 274 * When we call this, the initialization must be complete, since 275 * anon_inode_getfd() will install the fd. 276 */ 277 ufd = anon_inode_getfd("[signalfd]", &signalfd_fops, ctx, 278 O_RDWR | (flags & (O_CLOEXEC | O_NONBLOCK))); 279 if (ufd < 0) 280 kfree(ctx); 281 } else { 282 struct fd f = fdget(ufd); 283 if (!f.file) 284 return -EBADF; 285 ctx = f.file->private_data; 286 if (f.file->f_op != &signalfd_fops) { 287 fdput(f); 288 return -EINVAL; 289 } 290 spin_lock_irq(¤t->sighand->siglock); 291 ctx->sigmask = *mask; 292 spin_unlock_irq(¤t->sighand->siglock); 293 294 wake_up(¤t->sighand->signalfd_wqh); 295 fdput(f); 296 } 297 298 return ufd; 299 } 300 301 SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask, 302 size_t, sizemask, int, flags) 303 { 304 sigset_t mask; 305 306 if (sizemask != sizeof(sigset_t)) 307 return -EINVAL; 308 if (copy_from_user(&mask, user_mask, sizeof(mask))) 309 return -EFAULT; 310 return do_signalfd4(ufd, &mask, flags); 311 } 312 313 SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask, 314 size_t, sizemask) 315 { 316 sigset_t mask; 317 318 if (sizemask != sizeof(sigset_t)) 319 return -EINVAL; 320 if (copy_from_user(&mask, user_mask, sizeof(mask))) 321 return -EFAULT; 322 return do_signalfd4(ufd, &mask, 0); 323 } 324 325 #ifdef CONFIG_COMPAT 326 static long do_compat_signalfd4(int ufd, 327 const compat_sigset_t __user *user_mask, 328 compat_size_t sigsetsize, int flags) 329 { 330 sigset_t mask; 331 332 if (sigsetsize != sizeof(compat_sigset_t)) 333 return -EINVAL; 334 if (get_compat_sigset(&mask, user_mask)) 335 return -EFAULT; 336 return do_signalfd4(ufd, &mask, flags); 337 } 338 339 COMPAT_SYSCALL_DEFINE4(signalfd4, int, ufd, 340 const compat_sigset_t __user *, user_mask, 341 compat_size_t, sigsetsize, 342 int, flags) 343 { 344 return do_compat_signalfd4(ufd, user_mask, sigsetsize, flags); 345 } 346 347 COMPAT_SYSCALL_DEFINE3(signalfd, int, ufd, 348 const compat_sigset_t __user *, user_mask, 349 compat_size_t, sigsetsize) 350 { 351 return do_compat_signalfd4(ufd, user_mask, sigsetsize, 0); 352 } 353 #endif 354