Commit

40bb03d3ea6f6a915218508819ecc4d7a7fa260d

pipe: preserve existing fasync readers on writer setup failure

F_SETFL and FIOASYNC decide whether to invoke ->fasync() before
pipe_fasync() takes pipe->mutex. Two enables of one O_RDWR FIFO can
therefore both enter the callback.

fasync_helper() allocates before checking for an existing entry. If the
first enable installs both entries, a second enable can update the
reader entry and then fail the allocation for the writer entry. The
error path removes the pre-existing reader entry, clearing FASYNC while
the writer entry remains registered. __fput() then skips ->fasync(), so a
later pipe notification can dereference the freed file or its f_owner.
An unprivileged memory-pressure reproducer triggers a KASAN
slab-use-after-free in send_sigio() through this path.

Keep the reader helper's result and undo the registration only if this
call added it. The pipe mutex serializes changes to both lists, so a
failed new registration still removes its reader, while a failed
duplicate preserves the existing registrations and FASYNC. Successful
duplicate enables continue to update fa_fd as before.

Fixes: 76398425bb06 ("Move FASYNC bit handling to f_op->fasync()")
Signed-off-by: Kyle Zeng <kylebot@openai.com>
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>

Kyle Zeng <kylebot@openai.com> · 2026-10-02 06:17 UTC · committed by Christian Brauner

Browse files
1 file changed, +4 −2
modified fs/pipe.c
@@ -838,9 +838,11 @@ pipe_fasync(int fd, struct file *filp, int on)
838 838 if (filp->f_mode & FMODE_READ)
839 839 retval = fasync_helper(fd, filp, on, &pipe->fasync_readers);
840 840 if ((filp->f_mode & FMODE_WRITE) && retval >= 0) {
841 + int read_ret = retval;
842 +
841 843 retval = fasync_helper(fd, filp, on, &pipe->fasync_writers);
842 - if (retval < 0 && (filp->f_mode & FMODE_READ))
843 - /* this can happen only if on == T */
844 + /* Undo only a reader entry added by this call. */
845 + if (retval < 0 && read_ret > 0)
844 846 fasync_helper(-1, filp, 0, &pipe->fasync_readers);
845 847 }
846 848 mutex_unlock(&pipe->mutex);