Skip to content

Commit 85b8ac0

Browse files
lmbborkmann
authored andcommitted
bpf, sockmap: Check update requirements after locking
It's currently possible to insert sockets in unexpected states into a sockmap, due to a TOCTTOU when updating the map from a syscall. sock_map_update_elem checks that sk->sk_state == TCP_ESTABLISHED, locks the socket and then calls sock_map_update_common. At this point, the socket may have transitioned into another state, and the earlier assumptions don't hold anymore. Crucially, it's conceivable (though very unlikely) that a socket has become unhashed. This breaks the sockmap's assumption that it will get a callback via sk->sk_prot->unhash. Fix this by checking the (fixed) sk_type and sk_protocol without the lock, followed by a locked check of sk_state. Unfortunately it's not possible to push the check down into sock_(map|hash)_update_common, since BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB run before the socket has transitioned from TCP_SYN_RECV into TCP_ESTABLISHED. Fixes: 604326b ("bpf, sockmap: convert to generic sk_msg interface") Signed-off-by: Lorenz Bauer <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Reviewed-by: Jakub Sitnicki <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent b9e4272 commit 85b8ac0

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

net/core/sock_map.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -416,14 +416,16 @@ static int sock_map_update_elem(struct bpf_map *map, void *key,
416416
ret = -EINVAL;
417417
goto out;
418418
}
419-
if (!sock_map_sk_is_suitable(sk) ||
420-
sk->sk_state != TCP_ESTABLISHED) {
419+
if (!sock_map_sk_is_suitable(sk)) {
421420
ret = -EOPNOTSUPP;
422421
goto out;
423422
}
424423

425424
sock_map_sk_acquire(sk);
426-
ret = sock_map_update_common(map, idx, sk, flags);
425+
if (sk->sk_state != TCP_ESTABLISHED)
426+
ret = -EOPNOTSUPP;
427+
else
428+
ret = sock_map_update_common(map, idx, sk, flags);
427429
sock_map_sk_release(sk);
428430
out:
429431
fput(sock->file);
@@ -739,14 +741,16 @@ static int sock_hash_update_elem(struct bpf_map *map, void *key,
739741
ret = -EINVAL;
740742
goto out;
741743
}
742-
if (!sock_map_sk_is_suitable(sk) ||
743-
sk->sk_state != TCP_ESTABLISHED) {
744+
if (!sock_map_sk_is_suitable(sk)) {
744745
ret = -EOPNOTSUPP;
745746
goto out;
746747
}
747748

748749
sock_map_sk_acquire(sk);
749-
ret = sock_hash_update_common(map, key, sk, flags);
750+
if (sk->sk_state != TCP_ESTABLISHED)
751+
ret = -EOPNOTSUPP;
752+
else
753+
ret = sock_hash_update_common(map, key, sk, flags);
750754
sock_map_sk_release(sk);
751755
out:
752756
fput(sock->file);

0 commit comments

Comments
 (0)