Skip to content

Commit 70b57bf

Browse files
lmbgregkh
authored andcommitted
bpf, sockmap: Check update requirements after locking
commit 85b8ac0 upstream. 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 <lmb@cloudflare.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Reviewed-by: Jakub Sitnicki <jakub@cloudflare.com> Link: https://lore.kernel.org/bpf/20200207103713.28175-1-lmb@cloudflare.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 8f55393 commit 70b57bf

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
@@ -417,14 +417,16 @@ static int sock_map_update_elem(struct bpf_map *map, void *key,
417417
ret = -EINVAL;
418418
goto out;
419419
}
420-
if (!sock_map_sk_is_suitable(sk) ||
421-
sk->sk_state != TCP_ESTABLISHED) {
420+
if (!sock_map_sk_is_suitable(sk)) {
422421
ret = -EOPNOTSUPP;
423422
goto out;
424423
}
425424

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

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

0 commit comments

Comments
 (0)