Skip to content

Commit 4d5339c

Browse files
torvaldswinzkh
authored andcommitted
UPSTREAM: eventpoll: don't decrement ep refcount while still holding the ep mutex
commit 8c2e52ebbe885c7eeaabd3b7ddcdc1246fc400d2 upstream. Jann Horn points out that epoll is decrementing the ep refcount and then doing a mutex_unlock(&ep->mtx); afterwards. That's very wrong, because it can lead to a use-after-free. That pattern is actually fine for the very last reference, because the code in question will delay the actual call to "ep_free(ep)" until after it has unlocked the mutex. But it's wrong for the much subtler "next to last" case when somebody *else* may also be dropping their reference and free the ep while we're still using the mutex. Note that this is true even if that other user is also using the same ep mutex: mutexes, unlike spinlocks, can not be used for object ownership, even if they guarantee mutual exclusion. A mutex "unlock" operation is not atomic, and as one user is still accessing the mutex as part of unlocking it, another user can come in and get the now released mutex and free the data structure while the first user is still cleaning up. See our mutex documentation in Documentation/locking/mutex-design.rst, in particular the section [1] about semantics: "mutex_unlock() may access the mutex structure even after it has internally released the lock already - so it's not safe for another context to acquire the mutex and assume that the mutex_unlock() context is not using the structure anymore" So if we drop our ep ref before the mutex unlock, but we weren't the last one, we may then unlock the mutex, another user comes in, drops _their_ reference and releases the 'ep' as it now has no users - all while the mutex_unlock() is still accessing it. Fix this by simply moving the ep refcount dropping to outside the mutex: the refcount itself is atomic, and doesn't need mutex protection (that's the whole _point_ of refcounts: unlike mutexes, they are inherently about object lifetimes). Bug: 432751421 Bug: 447040629 Reported-by: Jann Horn <[email protected]> Link: https://docs.kernel.org/locking/mutex-design.html#semantics [1] Cc: Alexander Viro <[email protected]> Cc: Christian Brauner <[email protected]> Cc: Jan Kara <[email protected]> Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> (cherry picked from commit 6dee745bd0aec9d399df674256e7b1ecdb615444) Signed-off-by: Lee Jones <[email protected]> Change-Id: If3ca29987319bd31d794f621cdae863923b65c00
1 parent 8cf4040 commit 4d5339c

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

fs/eventpoll.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -775,22 +775,22 @@ static bool __ep_remove(struct eventpoll *ep, struct epitem *epi, bool force)
775775
call_rcu(&epi->rcu, epi_rcu_free);
776776

777777
percpu_counter_dec(&ep->user->epoll_watches);
778-
return ep_refcount_dec_and_test(ep);
778+
return true;
779779
}
780780

781781
/*
782782
* ep_remove variant for callers owing an additional reference to the ep
783783
*/
784784
static void ep_remove_safe(struct eventpoll *ep, struct epitem *epi)
785785
{
786-
WARN_ON_ONCE(__ep_remove(ep, epi, false));
786+
if (__ep_remove(ep, epi, false))
787+
WARN_ON_ONCE(ep_refcount_dec_and_test(ep));
787788
}
788789

789790
static void ep_clear_and_put(struct eventpoll *ep)
790791
{
791792
struct rb_node *rbp, *next;
792793
struct epitem *epi;
793-
bool dispose;
794794

795795
/* We need to release all tasks waiting for these file */
796796
if (waitqueue_active(&ep->poll_wait))
@@ -823,10 +823,8 @@ static void ep_clear_and_put(struct eventpoll *ep)
823823
cond_resched();
824824
}
825825

826-
dispose = ep_refcount_dec_and_test(ep);
827826
mutex_unlock(&ep->mtx);
828-
829-
if (dispose)
827+
if (ep_refcount_dec_and_test(ep))
830828
ep_free(ep);
831829
}
832830

@@ -1006,7 +1004,7 @@ void eventpoll_release_file(struct file *file)
10061004
dispose = __ep_remove(ep, epi, true);
10071005
mutex_unlock(&ep->mtx);
10081006

1009-
if (dispose)
1007+
if (dispose && ep_refcount_dec_and_test(ep))
10101008
ep_free(ep);
10111009
goto again;
10121010
}

0 commit comments

Comments
 (0)