Skip to content

Commit b3af609

Browse files
mmhalkuba-moo
authored andcommitted
bpf: Fix bpf_sk_select_reuseport() memory leak
As pointed out in the original comment, lookup in sockmap can return a TCP ESTABLISHED socket. Such TCP socket may have had SO_ATTACH_REUSEPORT_EBPF set before it was ESTABLISHED. In other words, a non-NULL sk_reuseport_cb does not imply a non-refcounted socket. Drop sk's reference in both error paths. unreferenced object 0xffff888101911800 (size 2048): comm "test_progs", pid 44109, jiffies 4297131437 hex dump (first 32 bytes): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 80 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ backtrace (crc 9336483b): __kmalloc_noprof+0x3bf/0x560 __reuseport_alloc+0x1d/0x40 reuseport_alloc+0xca/0x150 reuseport_attach_prog+0x87/0x140 sk_reuseport_attach_bpf+0xc8/0x100 sk_setsockopt+0x1181/0x1990 do_sock_setsockopt+0x12b/0x160 __sys_setsockopt+0x7b/0xc0 __x64_sys_setsockopt+0x1b/0x30 do_syscall_64+0x93/0x180 entry_SYSCALL_64_after_hwframe+0x76/0x7e Fixes: 64d8529 ("bpf: Allow bpf_map_lookup_elem for SOCKMAP and SOCKHASH") Signed-off-by: Michal Luczaj <[email protected]> Reviewed-by: Martin KaFai Lau <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 8c7a6ef commit b3af609

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

net/core/filter.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11251,42 +11251,48 @@ BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
1125111251
bool is_sockarray = map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY;
1125211252
struct sock_reuseport *reuse;
1125311253
struct sock *selected_sk;
11254+
int err;
1125411255

1125511256
selected_sk = map->ops->map_lookup_elem(map, key);
1125611257
if (!selected_sk)
1125711258
return -ENOENT;
1125811259

1125911260
reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
1126011261
if (!reuse) {
11261-
/* Lookup in sock_map can return TCP ESTABLISHED sockets. */
11262-
if (sk_is_refcounted(selected_sk))
11263-
sock_put(selected_sk);
11264-
1126511262
/* reuseport_array has only sk with non NULL sk_reuseport_cb.
1126611263
* The only (!reuse) case here is - the sk has already been
1126711264
* unhashed (e.g. by close()), so treat it as -ENOENT.
1126811265
*
1126911266
* Other maps (e.g. sock_map) do not provide this guarantee and
1127011267
* the sk may never be in the reuseport group to begin with.
1127111268
*/
11272-
return is_sockarray ? -ENOENT : -EINVAL;
11269+
err = is_sockarray ? -ENOENT : -EINVAL;
11270+
goto error;
1127311271
}
1127411272

1127511273
if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
1127611274
struct sock *sk = reuse_kern->sk;
1127711275

11278-
if (sk->sk_protocol != selected_sk->sk_protocol)
11279-
return -EPROTOTYPE;
11280-
else if (sk->sk_family != selected_sk->sk_family)
11281-
return -EAFNOSUPPORT;
11282-
11283-
/* Catch all. Likely bound to a different sockaddr. */
11284-
return -EBADFD;
11276+
if (sk->sk_protocol != selected_sk->sk_protocol) {
11277+
err = -EPROTOTYPE;
11278+
} else if (sk->sk_family != selected_sk->sk_family) {
11279+
err = -EAFNOSUPPORT;
11280+
} else {
11281+
/* Catch all. Likely bound to a different sockaddr. */
11282+
err = -EBADFD;
11283+
}
11284+
goto error;
1128511285
}
1128611286

1128711287
reuse_kern->selected_sk = selected_sk;
1128811288

1128911289
return 0;
11290+
error:
11291+
/* Lookup in sock_map can return TCP ESTABLISHED sockets. */
11292+
if (sk_is_refcounted(selected_sk))
11293+
sock_put(selected_sk);
11294+
11295+
return err;
1129011296
}
1129111297

1129211298
static const struct bpf_func_proto sk_select_reuseport_proto = {

0 commit comments

Comments
 (0)