The last refcnt of the psock can be gone right after sock_map_remove_links(), so sk_psock_stop() could trigger a UAF. The reason why I placed sk_psock_stop() there is to avoid RCU read critical section, and more importantly, some callee of sock_map_remove_links() is supposed to be called with RCU read lock, we can not simply get rid of RCU read lock here. Therefore, the only choice we have is to grab an additional refcnt with sk_psock_get() and put it back after sk_psock_stop().
Unified · Split
| @@ -1521,7 +1521,7 @@ void sock_map_close(struct sock *sk, long timeout) · 1520 unmodified lines | |||
| 1521 | 1521 | ||
| 1522 | lock_sock(sk); | 1522 | lock_sock(sk); |
| 1523 | rcu_read_lock(); | 1523 | rcu_read_lock(); |
| 1524 | psock = sk_psock(sk); | 1524 | psock = sk_psock_get(sk); |
| 1525 | if (unlikely(!psock)) { | 1525 | if (unlikely(!psock)) { |
| 1526 | rcu_read_unlock(); | 1526 | rcu_read_unlock(); |
| 1527 | release_sock(sk); | 1527 | release_sock(sk); |
| @@ -1532,6 +1532,7 @@ void sock_map_close(struct sock *sk, long timeout) · 4 unmodified lines | |||
| 1532 | sock_map_remove_links(sk, psock); | 1532 | sock_map_remove_links(sk, psock); |
| 1533 | rcu_read_unlock(); | 1533 | rcu_read_unlock(); |
| 1534 | sk_psock_stop(psock, true); | 1534 | sk_psock_stop(psock, true); |
| 1535 | sk_psock_put(sk, psock); | ||
| 1535 | release_sock(sk); | 1536 | release_sock(sk); |
| 1536 | saved_close(sk, timeout); | 1537 | saved_close(sk, timeout); |
| 1537 | } | 1538 | } |