summaryrefslogtreecommitdiffstats
path: root/fs/signalfd.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2026-08-30 09:22:00 -0700
committerLinus Torvalds <torvalds@linux-foundation.org>2026-08-30 09:22:00 -0700
commit034dd340b08be1f2f0477ad16131d609f9dbd53c (patch)
tree2536f4b2d7893ccd916b5abdf3c454ad6edc03c2 /fs/signalfd.c
downloadlinux-stable-034dd340b08be1f2f0477ad16131d609f9dbd53c.tar.gz
linux-stable-034dd340b08be1f2f0477ad16131d609f9dbd53c.zip
Merge tag 'trace-v7.3-2' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-tracegrafted
Pull tracing fixes from Steven Rostedt: - Fix error output of boot instance creation failure Currently if a boot instance creation fails, instead of printing out the name of the instance that failed, it prints "(null)". That is because it prints "cur_str" that had already been processed by strsep(). Print the saved name instead. While at it, print the error code of the failure. - Fix use-after-free for same named historgrams Histograms can be named so that they can be used in multiple events. But if the named histogram has a variable attached, the second event that uses the named histogram which duplicates it and needs to free the original after duplication leaves the old variable in place and still visible. If another histogram uses than variable, it will use the stale one which will try to reference the freed duplicate histogram and crash the kernel. Free the duplicate variables along with the duplicated histogram data. - Check return value of kthread_run() in event self test The events self tests uses a kthread for testing but does not check if it succeeded in creating a kthread. If the kthread creation were to fail, the code will still try to call kthread_stop() on the error returned. - Fix race between reading trace_pipe and updating subbuffer size If a user is reading the trace_pipe file at the same time they update the ring buffer sub-buffer size, can cause the trace_pipe read to read stale data. Add trace_access_lock() around updating the ring buffer sub-buffer size. - Fix eventfs_inode on failure path in creation of the events directory In the creation of the "events" directory, if after allocating the eventfs_inode a failure is detected, it calls cleanup_ei() which calls free_ei(). The free_ei() will test if eventfs_inode being freed has no children. It is a bug if it does. But on the failure case of the creation of the "events" directory, the children lists have not yet been initialized and the free will trigger a warning because list_empty() on an uninitialized list returns false. Move the initialization into init_ei() where it makes more sense and makes sure that a created eventfs_inode has its lists initialized upon creation. - Check return value of kthread_run() in ftrace direct sample code The sample code that shows how to use the ftrace direct calls does not test the return of kthread_run() to see if it succeeds. Return a failure if the kthread_run() doesn't succeed. - Clear user events state on fork in case of alloc failure On fork, the child gets a pointer to the parent's user events state. It makes a copy of it then updates the child's pointer to it. But if the allocation fails, the duplication function leaves the child with a pointer to its parent's descriptor. When the child cleans up its data, it will free the parent's descriptor while the parent is still using it. In the duplication function, set the child's user_event_mm to NULL before testing if the allocation succeeded, and when it exits it will not free the parent's descriptor. - Fix retry exhaustion in simple ring buffer reader swap simple_ring_buffer_swap_reader_page() starts with retry set to 8 and post-decrements it only after a failed link replacement. On the final attempt, a successful replacement leaves retry at zero, while a failed replacement leaves it at -1. But the check for success expects the retry value to be non-zero and exits with an error on zero. This is the opposite result. Fix it. - Fail nicely when the remote swap_reader_page() returns an error Currently, if the swap_reader_page() of a remote buffer fails, it triggers a WARN_ON_ONCE() and continues normally. Instead, have it exit with an error and a pr_warn() print instead of a full WARNING. * tag 'trace-v7.3-2' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace: ring-buffer: Stop remote reader update when page swap fails tracing: Fix retry exhaustion in simple ring buffer reader swap tracing/user_events: Clear copied tracing state before fork duplication samples/ftrace: Fix kthread_stop() on ERR_PTR in ftrace-direct-multi-modify samples/ftrace: Fix kthread_stop() on ERR_PTR in ftrace-direct-modify eventfs: Initialize ei->children and ei->list in init_ei() tracing: Fix use-after-free in trace_pipe read on sub-buffer order change tracing: Fix crash passing ERR_PTR to kthread_stop() tracing: Fix use-after-free with same-name named triggers tracing: Fix logged instance name on creation failure
Diffstat (limited to 'fs/signalfd.c')
-rw-r--r--fs/signalfd.c351
1 files changed, 351 insertions, 0 deletions
diff --git a/fs/signalfd.c b/fs/signalfd.c
new file mode 100644
index 000000000..dff53745e
--- /dev/null
+++ b/fs/signalfd.c
@@ -0,0 +1,351 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * fs/signalfd.c
+ *
+ * Copyright (C) 2003 Linus Torvalds
+ *
+ * Mon Mar 5, 2007: Davide Libenzi <davidel@xmailserver.org>
+ * Changed ->read() to return a siginfo strcture instead of signal number.
+ * Fixed locking in ->poll().
+ * Added sighand-detach notification.
+ * Added fd re-use in sys_signalfd() syscall.
+ * Now using anonymous inode source.
+ * Thanks to Oleg Nesterov for useful code review and suggestions.
+ * More comments and suggestions from Arnd Bergmann.
+ * Sat May 19, 2007: Davi E. M. Arnaut <davi@haxent.com.br>
+ * Retrieve multiple signals with one read() call
+ * Sun Jul 15, 2007: Davide Libenzi <davidel@xmailserver.org>
+ * Attach to the sighand only during read() and poll().
+ */
+
+#include <linux/file.h>
+#include <linux/poll.h>
+#include <linux/init.h>
+#include <linux/fs.h>
+#include <linux/sched.h>
+#include <linux/slab.h>
+#include <linux/kernel.h>
+#include <linux/signal.h>
+#include <linux/list.h>
+#include <linux/anon_inodes.h>
+#include <linux/signalfd.h>
+#include <linux/syscalls.h>
+#include <linux/proc_fs.h>
+#include <linux/compat.h>
+
+void signalfd_cleanup(struct sighand_struct *sighand)
+{
+ wake_up_pollfree(&sighand->signalfd_wqh);
+}
+
+struct signalfd_ctx {
+ sigset_t sigmask;
+};
+
+static int signalfd_release(struct inode *inode, struct file *file)
+{
+ kfree(file->private_data);
+ return 0;
+}
+
+static __poll_t signalfd_poll(struct file *file, poll_table *wait)
+{
+ struct signalfd_ctx *ctx = file->private_data;
+ __poll_t events = 0;
+
+ poll_wait(file, &current->sighand->signalfd_wqh, wait);
+
+ spin_lock_irq(&current->sighand->siglock);
+ if (next_signal(&current->pending, &ctx->sigmask) ||
+ next_signal(&current->signal->shared_pending,
+ &ctx->sigmask))
+ events |= EPOLLIN;
+ spin_unlock_irq(&current->sighand->siglock);
+
+ return events;
+}
+
+/*
+ * Copied from copy_siginfo_to_user() in kernel/signal.c
+ */
+static int signalfd_copyinfo(struct iov_iter *to, kernel_siginfo_t const *kinfo)
+{
+ struct signalfd_siginfo new;
+
+ BUILD_BUG_ON(sizeof(struct signalfd_siginfo) != 128);
+
+ /*
+ * Unused members should be zero ...
+ */
+ memset(&new, 0, sizeof(new));
+
+ /*
+ * If you change siginfo_t structure, please be sure
+ * this code is fixed accordingly.
+ */
+ new.ssi_signo = kinfo->si_signo;
+ new.ssi_errno = kinfo->si_errno;
+ new.ssi_code = kinfo->si_code;
+ switch (siginfo_layout(kinfo->si_signo, kinfo->si_code)) {
+ case SIL_KILL:
+ new.ssi_pid = kinfo->si_pid;
+ new.ssi_uid = kinfo->si_uid;
+ break;
+ case SIL_TIMER:
+ new.ssi_tid = kinfo->si_tid;
+ new.ssi_overrun = kinfo->si_overrun;
+ new.ssi_ptr = (long) kinfo->si_ptr;
+ new.ssi_int = kinfo->si_int;
+ break;
+ case SIL_POLL:
+ new.ssi_band = kinfo->si_band;
+ new.ssi_fd = kinfo->si_fd;
+ break;
+ case SIL_FAULT_BNDERR:
+ case SIL_FAULT_PKUERR:
+ case SIL_FAULT_PERF_EVENT:
+ /*
+ * Fall through to the SIL_FAULT case. SIL_FAULT_BNDERR,
+ * SIL_FAULT_PKUERR, and SIL_FAULT_PERF_EVENT are only
+ * generated by faults that deliver them synchronously to
+ * userspace. In case someone injects one of these signals
+ * and signalfd catches it treat it as SIL_FAULT.
+ */
+ case SIL_FAULT:
+ new.ssi_addr = (long) kinfo->si_addr;
+ break;
+ case SIL_FAULT_TRAPNO:
+ new.ssi_addr = (long) kinfo->si_addr;
+ new.ssi_trapno = kinfo->si_trapno;
+ break;
+ case SIL_FAULT_MCEERR:
+ new.ssi_addr = (long) kinfo->si_addr;
+ new.ssi_addr_lsb = (short) kinfo->si_addr_lsb;
+ break;
+ case SIL_CHLD:
+ new.ssi_pid = kinfo->si_pid;
+ new.ssi_uid = kinfo->si_uid;
+ new.ssi_status = kinfo->si_status;
+ new.ssi_utime = kinfo->si_utime;
+ new.ssi_stime = kinfo->si_stime;
+ break;
+ case SIL_RT:
+ /*
+ * This case catches also the signals queued by sigqueue().
+ */
+ new.ssi_pid = kinfo->si_pid;
+ new.ssi_uid = kinfo->si_uid;
+ new.ssi_ptr = (long) kinfo->si_ptr;
+ new.ssi_int = kinfo->si_int;
+ break;
+ case SIL_SYS:
+ new.ssi_call_addr = (long) kinfo->si_call_addr;
+ new.ssi_syscall = kinfo->si_syscall;
+ new.ssi_arch = kinfo->si_arch;
+ break;
+ }
+
+ if (!copy_to_iter_full(&new, sizeof(struct signalfd_siginfo), to))
+ return -EFAULT;
+
+ return sizeof(struct signalfd_siginfo);
+}
+
+static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, kernel_siginfo_t *info,
+ int nonblock)
+{
+ enum pid_type type;
+ ssize_t ret;
+ DECLARE_WAITQUEUE(wait, current);
+
+ spin_lock_irq(&current->sighand->siglock);
+ ret = dequeue_signal(&ctx->sigmask, info, &type);
+ switch (ret) {
+ case 0:
+ if (!nonblock)
+ break;
+ ret = -EAGAIN;
+ fallthrough;
+ default:
+ spin_unlock_irq(&current->sighand->siglock);
+ return ret;
+ }
+
+ add_wait_queue(&current->sighand->signalfd_wqh, &wait);
+ for (;;) {
+ set_current_state(TASK_INTERRUPTIBLE);
+ ret = dequeue_signal(&ctx->sigmask, info, &type);
+ if (ret != 0)
+ break;
+ if (signal_pending(current)) {
+ ret = -ERESTARTSYS;
+ break;
+ }
+ spin_unlock_irq(&current->sighand->siglock);
+ schedule();
+ spin_lock_irq(&current->sighand->siglock);
+ }
+ spin_unlock_irq(&current->sighand->siglock);
+
+ remove_wait_queue(&current->sighand->signalfd_wqh, &wait);
+ __set_current_state(TASK_RUNNING);
+
+ return ret;
+}
+
+/*
+ * Returns a multiple of the size of a "struct signalfd_siginfo", or a negative
+ * error code. The "count" parameter must be at least the size of a
+ * "struct signalfd_siginfo".
+ */
+static ssize_t signalfd_read_iter(struct kiocb *iocb, struct iov_iter *to)
+{
+ struct file *file = iocb->ki_filp;
+ struct signalfd_ctx *ctx = file->private_data;
+ size_t count = iov_iter_count(to);
+ ssize_t ret, total = 0;
+ kernel_siginfo_t info;
+ bool nonblock;
+
+ count /= sizeof(struct signalfd_siginfo);
+ if (!count)
+ return -EINVAL;
+
+ nonblock = file->f_flags & O_NONBLOCK || iocb->ki_flags & IOCB_NOWAIT;
+ do {
+ ret = signalfd_dequeue(ctx, &info, nonblock);
+ if (unlikely(ret <= 0))
+ break;
+ ret = signalfd_copyinfo(to, &info);
+ if (ret < 0)
+ break;
+ total += ret;
+ nonblock = 1;
+ } while (--count);
+
+ return total ? total: ret;
+}
+
+#ifdef CONFIG_PROC_FS
+static void signalfd_show_fdinfo(struct seq_file *m, struct file *f)
+{
+ struct signalfd_ctx *ctx = f->private_data;
+ sigset_t sigmask;
+
+ sigmask = ctx->sigmask;
+ signotset(&sigmask);
+ render_sigset_t(m, "sigmask:\t", &sigmask);
+}
+#endif
+
+static const struct file_operations signalfd_fops = {
+#ifdef CONFIG_PROC_FS
+ .show_fdinfo = signalfd_show_fdinfo,
+#endif
+ .release = signalfd_release,
+ .poll = signalfd_poll,
+ .read_iter = signalfd_read_iter,
+ .llseek = noop_llseek,
+};
+
+static int do_signalfd4(int ufd, sigset_t *mask, int flags)
+{
+ /* Check the SFD_* constants for consistency. */
+ BUILD_BUG_ON(SFD_CLOEXEC != O_CLOEXEC);
+ BUILD_BUG_ON(SFD_NONBLOCK != O_NONBLOCK);
+
+ if (flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))
+ return -EINVAL;
+
+ sigdelsetmask(mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
+ signotset(mask);
+
+ if (ufd == -1) {
+ int fd;
+ struct signalfd_ctx *ctx __free(kfree) = NULL;
+
+ ctx = kmalloc_obj(*ctx);
+ if (!ctx)
+ return -ENOMEM;
+
+ ctx->sigmask = *mask;
+
+ fd = FD_ADD(flags & O_CLOEXEC,
+ anon_inode_getfile_fmode(
+ "[signalfd]", &signalfd_fops, ctx,
+ O_RDWR | (flags & O_NONBLOCK), FMODE_NOWAIT));
+ if (fd >= 0)
+ retain_and_null_ptr(ctx);
+ return fd;
+ } else {
+ struct signalfd_ctx *ctx;
+
+ CLASS(fd, f)(ufd);
+ if (fd_empty(f))
+ return -EBADF;
+ ctx = fd_file(f)->private_data;
+ if (fd_file(f)->f_op != &signalfd_fops)
+ return -EINVAL;
+ spin_lock_irq(&current->sighand->siglock);
+ ctx->sigmask = *mask;
+ spin_unlock_irq(&current->sighand->siglock);
+
+ wake_up(&current->sighand->signalfd_wqh);
+ }
+
+ return ufd;
+}
+
+SYSCALL_DEFINE4(signalfd4, int, ufd, sigset_t __user *, user_mask,
+ size_t, sizemask, int, flags)
+{
+ sigset_t mask;
+
+ if (sizemask != sizeof(sigset_t))
+ return -EINVAL;
+ if (copy_from_user(&mask, user_mask, sizeof(mask)))
+ return -EFAULT;
+ return do_signalfd4(ufd, &mask, flags);
+}
+
+SYSCALL_DEFINE3(signalfd, int, ufd, sigset_t __user *, user_mask,
+ size_t, sizemask)
+{
+ sigset_t mask;
+
+ if (sizemask != sizeof(sigset_t))
+ return -EINVAL;
+ if (copy_from_user(&mask, user_mask, sizeof(mask)))
+ return -EFAULT;
+ return do_signalfd4(ufd, &mask, 0);
+}
+
+#ifdef CONFIG_COMPAT
+static long do_compat_signalfd4(int ufd,
+ const compat_sigset_t __user *user_mask,
+ compat_size_t sigsetsize, int flags)
+{
+ sigset_t mask;
+
+ if (sigsetsize != sizeof(compat_sigset_t))
+ return -EINVAL;
+ if (get_compat_sigset(&mask, user_mask))
+ return -EFAULT;
+ return do_signalfd4(ufd, &mask, flags);
+}
+
+COMPAT_SYSCALL_DEFINE4(signalfd4, int, ufd,
+ const compat_sigset_t __user *, user_mask,
+ compat_size_t, sigsetsize,
+ int, flags)
+{
+ return do_compat_signalfd4(ufd, user_mask, sigsetsize, flags);
+}
+
+COMPAT_SYSCALL_DEFINE3(signalfd, int, ufd,
+ const compat_sigset_t __user *, user_mask,
+ compat_size_t, sigsetsize)
+{
+ return do_compat_signalfd4(ufd, user_mask, sigsetsize, 0);
+}
+#endif