Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

epoll: be better about file lifetimes

epoll can call out to vfs_poll() with a file pointer that may race with
the last 'fput()'. That would make f_count go down to zero, and while
the ep->mtx locking means that the resulting file pointer tear-down will
be blocked until the poll returns, it means that f_count is already
dead, and any use of it won't actually get a reference to the file any
more: it's dead regardless.

Make sure we have a valid ref on the file pointer before we call down to
vfs_poll() from the epoll routines.

Link: https://lore.kernel.org/lkml/0000000000002d631f0615918f1e@google.com/
Reported-by: syzbot+045b454ab35fd82a35fb@syzkaller.appspotmail.com
Reviewed-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

+37 -1
+37 -1
fs/eventpoll.c
··· 980 980 } 981 981 982 982 /* 983 + * The ffd.file pointer may be in the process of being torn down due to 984 + * being closed, but we may not have finished eventpoll_release() yet. 985 + * 986 + * Normally, even with the atomic_long_inc_not_zero, the file may have 987 + * been free'd and then gotten re-allocated to something else (since 988 + * files are not RCU-delayed, they are SLAB_TYPESAFE_BY_RCU). 989 + * 990 + * But for epoll, users hold the ep->mtx mutex, and as such any file in 991 + * the process of being free'd will block in eventpoll_release_file() 992 + * and thus the underlying file allocation will not be free'd, and the 993 + * file re-use cannot happen. 994 + * 995 + * For the same reason we can avoid a rcu_read_lock() around the 996 + * operation - 'ffd.file' cannot go away even if the refcount has 997 + * reached zero (but we must still not call out to ->poll() functions 998 + * etc). 999 + */ 1000 + static struct file *epi_fget(const struct epitem *epi) 1001 + { 1002 + struct file *file; 1003 + 1004 + file = epi->ffd.file; 1005 + if (!atomic_long_inc_not_zero(&file->f_count)) 1006 + file = NULL; 1007 + return file; 1008 + } 1009 + 1010 + /* 983 1011 * Differs from ep_eventpoll_poll() in that internal callers already have 984 1012 * the ep->mtx so we need to start from depth=1, such that mutex_lock_nested() 985 1013 * is correctly annotated. ··· 1015 987 static __poll_t ep_item_poll(const struct epitem *epi, poll_table *pt, 1016 988 int depth) 1017 989 { 1018 - struct file *file = epi->ffd.file; 990 + struct file *file = epi_fget(epi); 1019 991 __poll_t res; 992 + 993 + /* 994 + * We could return EPOLLERR | EPOLLHUP or something, but let's 995 + * treat this more as "file doesn't exist, poll didn't happen". 996 + */ 997 + if (!file) 998 + return 0; 1020 999 1021 1000 pt->_key = epi->event.events; 1022 1001 if (!is_file_epoll(file)) 1023 1002 res = vfs_poll(file, pt); 1024 1003 else 1025 1004 res = __ep_eventpoll_poll(file, pt, depth); 1005 + fput(file); 1026 1006 return res & epi->event.events; 1027 1007 } 1028 1008