Skip to content

Commit 597222f

Browse files
jrfastabborkmann
authored andcommitted
bpf: avoid misuse of psock when TCP_ULP_BPF collides with another ULP
Currently we check sk_user_data is non NULL to determine if the sk exists in a map. However, this is not sufficient to ensure the psock or the ULP ops are not in use by another user, such as kcm or TLS. To avoid this when adding a sock to a map also verify it is of the correct ULP type. Additionally, when releasing a psock verify that it is the TCP_ULP_BPF type before releasing the ULP. The error case where we abort an update due to ULP collision can cause this error path. For example, __sock_map_ctx_update_elem() [...] err = tcp_set_ulp_id(sock, TCP_ULP_BPF) <- collides with TLS if (err) <- so err out here goto out_free [...] out_free: smap_release_sock() <- calling tcp_cleanup_ulp releases the TLS ULP incorrectly. Fixes: 2f857d0 ("bpf: sockmap, remove STRPARSER map_flags and add multi-map support") Signed-off-by: John Fastabend <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 97911e0 commit 597222f

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

kernel/bpf/sockmap.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1462,10 +1462,16 @@ static void smap_destroy_psock(struct rcu_head *rcu)
14621462
schedule_work(&psock->gc_work);
14631463
}
14641464

1465+
static bool psock_is_smap_sk(struct sock *sk)
1466+
{
1467+
return inet_csk(sk)->icsk_ulp_ops == &bpf_tcp_ulp_ops;
1468+
}
1469+
14651470
static void smap_release_sock(struct smap_psock *psock, struct sock *sock)
14661471
{
14671472
if (refcount_dec_and_test(&psock->refcnt)) {
1468-
tcp_cleanup_ulp(sock);
1473+
if (psock_is_smap_sk(sock))
1474+
tcp_cleanup_ulp(sock);
14691475
write_lock_bh(&sock->sk_callback_lock);
14701476
smap_stop_sock(psock, sock);
14711477
write_unlock_bh(&sock->sk_callback_lock);
@@ -1892,6 +1898,10 @@ static int __sock_map_ctx_update_elem(struct bpf_map *map,
18921898
* doesn't update user data.
18931899
*/
18941900
if (psock) {
1901+
if (!psock_is_smap_sk(sock)) {
1902+
err = -EBUSY;
1903+
goto out_progs;
1904+
}
18951905
if (READ_ONCE(psock->bpf_parse) && parse) {
18961906
err = -EBUSY;
18971907
goto out_progs;

0 commit comments

Comments
 (0)