-
Notifications
You must be signed in to change notification settings - Fork 126
bpf: Fix use-after-free of sockmap #8686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Upstream branch: 812f770 |
014f72f
to
4d1d6fc
Compare
Upstream branch: b02f072 |
8e5f91a
to
9de4b50
Compare
4d1d6fc
to
e14a0d8
Compare
Upstream branch: f3f8649 |
9de4b50
to
f341117
Compare
e14a0d8
to
503edd4
Compare
Upstream branch: ae0a457 |
f341117
to
17d38a2
Compare
503edd4
to
7e623dc
Compare
Upstream branch: f4edc66 |
17d38a2
to
eb23d26
Compare
7e623dc
to
156a3ac
Compare
Upstream branch: 6ca2162 |
eb23d26
to
39b13a4
Compare
156a3ac
to
50dba77
Compare
Upstream branch: a259804 |
39b13a4
to
e937709
Compare
50dba77
to
ce294a5
Compare
Upstream branch: 79db658 |
e937709
to
418c46b
Compare
3c64a8d
to
e2c83e6
Compare
3dc28f3
to
35ab59d
Compare
Upstream branch: 9aa8fe2 |
e2c83e6
to
6464620
Compare
35ab59d
to
bf339f7
Compare
Upstream branch: 9aa8fe2 |
6464620
to
6d59848
Compare
bf339f7
to
9809c6b
Compare
Upstream branch: aa918db |
6d59848
to
760a00f
Compare
9809c6b
to
0a09ea9
Compare
The sk->sk_socket is not locked or referenced, and during the call to skb_send_sock(), there is a race condition with the release of sk_socket. All types of sockets(tcp/udp/unix/vsock) will be affected. Race conditions: ''' CPU0 CPU1 skb_send_sock sendmsg_unlocked sock_sendmsg sock_sendmsg_nosec close(fd): ... ops->release() sock_map_close() sk_socket->ops = NULL free(socket) sock->ops->sendmsg ^ panic here ''' Based on the fact that we already wait for the workqueue to finish in sock_map_close() if psock is held, we simply increase the psock reference count to avoid race conditions. ''' void sock_map_close() { ... if (likely(psock)) { ... psock = sk_psock_get(sk); if (unlikely(!psock)) goto no_psock; <=== Control usually jumps here via goto ... cancel_delayed_work_sync(&psock->work); <=== not executed sk_psock_put(sk, psock); ... } ''' The panic I catched: ''' Workqueue: events sk_psock_backlog RIP: 0010:sock_sendmsg+0x21d/0x440 RAX: 0000000000000000 RBX: ffffc9000521fad8 RCX: 0000000000000001 ... Call Trace: <TASK> ? die_addr+0x40/0xa0 ? exc_general_protection+0x14c/0x230 ? asm_exc_general_protection+0x26/0x30 ? sock_sendmsg+0x21d/0x440 ? sock_sendmsg+0x3e0/0x440 ? __pfx_sock_sendmsg+0x10/0x10 __skb_send_sock+0x543/0xb70 sk_psock_backlog+0x247/0xb80 ... ''' Reported-by: Michal Luczaj <[email protected]> Fixes: 799aa7f ("skmsg: Avoid lock_sock() in sk_psock_backlog()") Signed-off-by: Jiayuan Chen <[email protected]>
There are potential concurrency issues, as shown below. ''' CPU0 CPU1 sk_psock_verdict_data_ready: socket *sock = sk->sk_socket if (!sock) return close(fd): ... ops->release() if (!sock->ops) return sock->ops = NULL rcu_call(sock) free(sock) READ_ONCE(sock->ops) ^ use 'sock' after free ''' RCU is not applicable to Unix sockets read path, because the Unix socket implementation itself assumes it's always in process context and heavily uses mutex_lock, so, we can't call read_skb within rcu lock. Incrementing the psock reference count would not help either, since sock_map_close() does not wait for data_ready() to complete its execution. While we don't utilize sk_socket here, implementing read_skb at the sock layer instead of socket layer might be architecturally preferable ? However, deferring this optimization as current fix adequately addresses the immediate issue. Fixes: c638291 ("af_unix: Implement ->psock_update_sk_prot()") Reported-by: [email protected] Closes: https://lore.kernel.org/bpf/[email protected]/ Signed-off-by: Jiayuan Chen <[email protected]>
Add edge case tests for sockmap. Signed-off-by: Jiayuan Chen <[email protected]>
Upstream branch: 4e82c87 |
760a00f
to
d11c45e
Compare
At least one diff in series https://patchwork.kernel.org/project/netdevbpf/list/?series=938820 expired. Closing PR. |
Pull request for series with
subject: bpf: Fix use-after-free of sockmap
version: 3
url: https://patchwork.kernel.org/project/netdevbpf/list/?series=944583