epoll: fix race between ep_poll_callback(POLLFREE) and ep_free()/ep_remove()
authorOleg Nesterov <oleg@redhat.com>
Fri, 1 Sep 2017 16:55:33 +0000 (18:55 +0200)
committerBen Hutchings <ben@decadent.org.uk>
Sat, 11 Nov 2017 13:34:37 +0000 (13:34 +0000)
commit47dcfde4a9cc9f0e2dbe3fab9d8fc10ad42f1c00
tree456cd6c1fa9f041bac9b504ac9e99bea8dc1e3f3
parent34e618bfcf9c9a41ed188c74e585e37ebe872eaf
epoll: fix race between ep_poll_callback(POLLFREE) and ep_free()/ep_remove()

commit 138e4ad67afd5c6c318b056b4d17c17f2c0ca5c0 upstream.

The race was introduced by me in commit 971316f0503a ("epoll:
ep_unregister_pollwait() can use the freed pwq->whead").  I did not
realize that nothing can protect eventpoll after ep_poll_callback() sets
->whead = NULL, only whead->lock can save us from the race with
ep_free() or ep_remove().

Move ->whead = NULL to the end of ep_poll_callback() and add the
necessary barriers.

TODO: cleanup the ewake/EPOLLEXCLUSIVE logic, it was confusing even
before this patch.

Hopefully this explains use-after-free reported by syzcaller:

BUG: KASAN: use-after-free in debug_spin_lock_before
...
 _raw_spin_lock_irqsave+0x4a/0x60 kernel/locking/spinlock.c:159
 ep_poll_callback+0x29f/0xff0 fs/eventpoll.c:1148

this is spin_lock(eventpoll->lock),

...
Freed by task 17774:
...
 kfree+0xe8/0x2c0 mm/slub.c:3883
 ep_free+0x22c/0x2a0 fs/eventpoll.c:865

Fixes: 971316f0503a ("epoll: ep_unregister_pollwait() can use the freed pwq->whead")
Reported-by: 范龙飞 <long7573@126.com>
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
[bwh: Backported to 3.2:
 - Use smp_mb() and ACCESS_ONCE() instead of smp_{load_acquire,store_release}()
 - EPOLLEXCLUSIVE is not supported]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
fs/eventpoll.c