Skip to content

Commit d0a3b3d

Browse files
mmhalgregkh
authored andcommitted
bpf: Fix bpf_sk_select_reuseport() memory leak
[ Upstream commit b3af609 ] 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 <mhal@rbox.co> Reviewed-by: Martin KaFai Lau <martin.lau@kernel.org> Link: https://patch.msgid.link/20250110-reuseport-memleak-v1-1-fa1ddab0adfe@rbox.co Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 0752481 commit d0a3b3d

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

net/core/filter.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11109,42 +11109,48 @@ BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
1110911109
bool is_sockarray = map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY;
1111011110
struct sock_reuseport *reuse;
1111111111
struct sock *selected_sk;
11112+
int err;
1111211113

1111311114
selected_sk = map->ops->map_lookup_elem(map, key);
1111411115
if (!selected_sk)
1111511116
return -ENOENT;
1111611117

1111711118
reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
1111811119
if (!reuse) {
11119-
/* Lookup in sock_map can return TCP ESTABLISHED sockets. */
11120-
if (sk_is_refcounted(selected_sk))
11121-
sock_put(selected_sk);
11122-
1112311120
/* reuseport_array has only sk with non NULL sk_reuseport_cb.
1112411121
* The only (!reuse) case here is - the sk has already been
1112511122
* unhashed (e.g. by close()), so treat it as -ENOENT.
1112611123
*
1112711124
* Other maps (e.g. sock_map) do not provide this guarantee and
1112811125
* the sk may never be in the reuseport group to begin with.
1112911126
*/
11130-
return is_sockarray ? -ENOENT : -EINVAL;
11127+
err = is_sockarray ? -ENOENT : -EINVAL;
11128+
goto error;
1113111129
}
1113211130

1113311131
if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
1113411132
struct sock *sk = reuse_kern->sk;
1113511133

11136-
if (sk->sk_protocol != selected_sk->sk_protocol)
11137-
return -EPROTOTYPE;
11138-
else if (sk->sk_family != selected_sk->sk_family)
11139-
return -EAFNOSUPPORT;
11140-
11141-
/* Catch all. Likely bound to a different sockaddr. */
11142-
return -EBADFD;
11134+
if (sk->sk_protocol != selected_sk->sk_protocol) {
11135+
err = -EPROTOTYPE;
11136+
} else if (sk->sk_family != selected_sk->sk_family) {
11137+
err = -EAFNOSUPPORT;
11138+
} else {
11139+
/* Catch all. Likely bound to a different sockaddr. */
11140+
err = -EBADFD;
11141+
}
11142+
goto error;
1114311143
}
1114411144

1114511145
reuse_kern->selected_sk = selected_sk;
1114611146

1114711147
return 0;
11148+
error:
11149+
/* Lookup in sock_map can return TCP ESTABLISHED sockets. */
11150+
if (sk_is_refcounted(selected_sk))
11151+
sock_put(selected_sk);
11152+
11153+
return err;
1114811154
}
1114911155

1115011156
static const struct bpf_func_proto sk_select_reuseport_proto = {

0 commit comments

Comments
 (0)