Skip to content

Commit 661e814

Browse files
brauneropsiff
authored andcommitted
eventpoll: move epi_fget() up
mainline inclusion from mainline-v7.1-rc1 category: bugfix CVE: CVE-2026-46242 [ Upstream commit 86e87059e6d1fd5115a31949726450ed03c1073b ] We'll need it when removing files so move it up. No functional change. Link: https://patch.msgid.link/20260423-work-epoll-uaf-v1-5-2470f9eec0f5@kernel.org Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org> Stable-dep-of: a6dc643c6931 ("eventpoll: fix ep_remove struct eventpoll / struct file UAF") [file_ref_get(&file->f_ref) from original commit left as atomic_long_inc_not_zero(&file->f_count) due to v6.12.y missing commit 90ee6ed ("fs: port files to file_ref") and its dependent commit 08ef26e ("fs: add file_ref")] Signed-off-by: Quentin Schulz <quentin.schulz@cherry.de> Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
1 parent d058696 commit 661e814

1 file changed

Lines changed: 28 additions & 28 deletions

File tree

fs/eventpoll.c

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,34 @@ static void ep_free(struct eventpoll *ep)
714714
kfree_rcu(ep, rcu);
715715
}
716716

717+
/*
718+
* The ffd.file pointer may be in the process of being torn down due to
719+
* being closed, but we may not have finished eventpoll_release() yet.
720+
*
721+
* Normally, even with the atomic_long_inc_not_zero, the file may have
722+
* been free'd and then gotten re-allocated to something else (since
723+
* files are not RCU-delayed, they are SLAB_TYPESAFE_BY_RCU).
724+
*
725+
* But for epoll, users hold the ep->mtx mutex, and as such any file in
726+
* the process of being free'd will block in eventpoll_release_file()
727+
* and thus the underlying file allocation will not be free'd, and the
728+
* file re-use cannot happen.
729+
*
730+
* For the same reason we can avoid a rcu_read_lock() around the
731+
* operation - 'ffd.file' cannot go away even if the refcount has
732+
* reached zero (but we must still not call out to ->poll() functions
733+
* etc).
734+
*/
735+
static struct file *epi_fget(const struct epitem *epi)
736+
{
737+
struct file *file;
738+
739+
file = epi->ffd.file;
740+
if (!atomic_long_inc_not_zero(&file->f_count))
741+
file = NULL;
742+
return file;
743+
}
744+
717745
/*
718746
* Called with &file->f_lock held,
719747
* returns with it released
@@ -885,34 +913,6 @@ static __poll_t __ep_eventpoll_poll(struct file *file, poll_table *wait, int dep
885913
return res;
886914
}
887915

888-
/*
889-
* The ffd.file pointer may be in the process of being torn down due to
890-
* being closed, but we may not have finished eventpoll_release() yet.
891-
*
892-
* Normally, even with the atomic_long_inc_not_zero, the file may have
893-
* been free'd and then gotten re-allocated to something else (since
894-
* files are not RCU-delayed, they are SLAB_TYPESAFE_BY_RCU).
895-
*
896-
* But for epoll, users hold the ep->mtx mutex, and as such any file in
897-
* the process of being free'd will block in eventpoll_release_file()
898-
* and thus the underlying file allocation will not be free'd, and the
899-
* file re-use cannot happen.
900-
*
901-
* For the same reason we can avoid a rcu_read_lock() around the
902-
* operation - 'ffd.file' cannot go away even if the refcount has
903-
* reached zero (but we must still not call out to ->poll() functions
904-
* etc).
905-
*/
906-
static struct file *epi_fget(const struct epitem *epi)
907-
{
908-
struct file *file;
909-
910-
file = epi->ffd.file;
911-
if (!atomic_long_inc_not_zero(&file->f_count))
912-
file = NULL;
913-
return file;
914-
}
915-
916916
/*
917917
* Differs from ep_eventpoll_poll() in that internal callers already have
918918
* the ep->mtx so we need to start from depth=1, such that mutex_lock_nested()

0 commit comments

Comments
 (0)