mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-07-22 03:27:30 -04:00
Merge branch 'bpf-sockmap-fix-sockmap-leaking-udp-socks'
Michal Luczaj says: ==================== bpf, sockmap: Fix sockmap leaking UDP socks Fix for UDP sockets getting leaked during sockmap lookup/release. Accompanied by selftests updates. Two Sashiko's concerns to be addressed separately: https://lore.kernel.org/bpf/20260626205814.BAC3C1F000E9@smtp.kernel.org/ Signed-off-by: Michal Luczaj <mhal@rbox.co> --- Changes in v4: - selftest: drop redundant `if (err)` [Sashiko] - Link to v3: https://patch.msgid.link/20260702-sockmap-lookup-udp-leak-v3-0-ff8de8782468@rbox.co Changes in v3: - selftest: better error handling, ASSERT_*() macros [Sashiko] - selftest: fix grammar, reorder patches [Kuniyuki] - Link to v2: https://patch.msgid.link/20260626-sockmap-lookup-udp-leak-v2-0-7e7e201c951a@rbox.co Changes in v2: - selftest: drop the original, adapt old tests - fix: change approach to rejecting unbound UDP [Kuniyuki] - Link to v1: https://patch.msgid.link/20260623-sockmap-lookup-udp-leak-v1-0-05804f9308e4@rbox.co To: Alexei Starovoitov <ast@kernel.org> To: Daniel Borkmann <daniel@iogearbox.net> To: Andrii Nakryiko <andrii@kernel.org> To: Eduard Zingerman <eddyz87@gmail.com> To: Kumar Kartikeya Dwivedi <memxor@gmail.com> To: Martin KaFai Lau <martin.lau@linux.dev> To: Song Liu <song@kernel.org> To: Yonghong Song <yonghong.song@linux.dev> To: Jiri Olsa <jolsa@kernel.org> To: Emil Tsalapatis <emil@etsalapatis.com> To: Shuah Khan <shuah@kernel.org> To: John Fastabend <john.fastabend@gmail.com> To: Jakub Sitnicki <jakub@cloudflare.com> To: Jiayuan Chen <jiayuan.chen@linux.dev> To: Eric Dumazet <edumazet@google.com> To: Kuniyuki Iwashima <kuniyu@google.com> To: Paolo Abeni <pabeni@redhat.com> To: Willem de Bruijn <willemb@google.com> To: "David S. Miller" <davem@davemloft.net> To: Jakub Kicinski <kuba@kernel.org> To: Simon Horman <horms@kernel.org> To: Cong Wang <cong.wang@bytedance.com> Cc: bpf@vger.kernel.org Cc: linux-kselftest@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org --- ==================== Link: https://patch.msgid.link/20260707-sockmap-lookup-udp-leak-v4-0-f878346f27ab@rbox.co Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
This commit is contained in:
@@ -542,6 +542,8 @@ static bool sock_map_sk_state_allowed(const struct sock *sk)
|
||||
{
|
||||
if (sk_is_tcp(sk))
|
||||
return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
|
||||
if (sk_is_udp(sk))
|
||||
return sk_hashed(sk);
|
||||
if (sk_is_stream_unix(sk))
|
||||
return (1 << READ_ONCE(sk->sk_state)) & TCPF_ESTABLISHED;
|
||||
if (sk_is_vsock(sk) &&
|
||||
|
||||
@@ -853,7 +853,7 @@ static void test_sockmap_many_socket(void)
|
||||
return;
|
||||
}
|
||||
|
||||
udp = xsocket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0);
|
||||
udp = socket_loopback(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK);
|
||||
if (udp < 0) {
|
||||
close(dgram);
|
||||
close(tcp);
|
||||
@@ -922,7 +922,7 @@ static void test_sockmap_many_maps(void)
|
||||
return;
|
||||
}
|
||||
|
||||
udp = xsocket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0);
|
||||
udp = socket_loopback(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK);
|
||||
if (udp < 0) {
|
||||
close(dgram);
|
||||
close(tcp);
|
||||
@@ -993,7 +993,7 @@ static void test_sockmap_same_sock(void)
|
||||
return;
|
||||
}
|
||||
|
||||
udp = xsocket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0);
|
||||
udp = socket_loopback(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK);
|
||||
if (udp < 0) {
|
||||
close(dgram);
|
||||
close(tcp);
|
||||
|
||||
@@ -53,8 +53,8 @@ static void test_insert_opened(struct test_sockmap_listen *skel __always_unused,
|
||||
int family, int sotype, int mapfd)
|
||||
{
|
||||
u32 key = 0;
|
||||
u64 value;
|
||||
int err, s;
|
||||
u64 value;
|
||||
|
||||
s = xsocket(family, sotype, 0);
|
||||
if (s == -1)
|
||||
@@ -63,11 +63,8 @@ static void test_insert_opened(struct test_sockmap_listen *skel __always_unused,
|
||||
errno = 0;
|
||||
value = s;
|
||||
err = bpf_map_update_elem(mapfd, &key, &value, BPF_NOEXIST);
|
||||
if (sotype == SOCK_STREAM) {
|
||||
if (!err || errno != EOPNOTSUPP)
|
||||
FAIL_ERRNO("map_update: expected EOPNOTSUPP");
|
||||
} else if (err)
|
||||
FAIL_ERRNO("map_update: expected success");
|
||||
ASSERT_ERR(err, "map_update");
|
||||
ASSERT_EQ(errno, EOPNOTSUPP, "errno");
|
||||
xclose(s);
|
||||
}
|
||||
|
||||
@@ -77,8 +74,8 @@ static void test_insert_bound(struct test_sockmap_listen *skel __always_unused,
|
||||
struct sockaddr_storage addr;
|
||||
socklen_t len = 0;
|
||||
u32 key = 0;
|
||||
u64 value;
|
||||
int err, s;
|
||||
u64 value;
|
||||
|
||||
init_addr_loopback(family, &addr, &len);
|
||||
|
||||
@@ -93,8 +90,12 @@ static void test_insert_bound(struct test_sockmap_listen *skel __always_unused,
|
||||
errno = 0;
|
||||
value = s;
|
||||
err = bpf_map_update_elem(mapfd, &key, &value, BPF_NOEXIST);
|
||||
if (!err || errno != EOPNOTSUPP)
|
||||
FAIL_ERRNO("map_update: expected EOPNOTSUPP");
|
||||
if (sotype == SOCK_STREAM) {
|
||||
ASSERT_ERR(err, "map_update");
|
||||
ASSERT_EQ(errno, EOPNOTSUPP, "errno");
|
||||
} else {
|
||||
ASSERT_OK(err, "map_update");
|
||||
}
|
||||
close:
|
||||
xclose(s);
|
||||
}
|
||||
@@ -1289,7 +1290,7 @@ static void test_ops(struct test_sockmap_listen *skel, struct bpf_map *map,
|
||||
/* insert */
|
||||
TEST(test_insert_invalid),
|
||||
TEST(test_insert_opened),
|
||||
TEST(test_insert_bound, SOCK_STREAM),
|
||||
TEST(test_insert_bound),
|
||||
TEST(test_insert),
|
||||
/* delete */
|
||||
TEST(test_delete_after_insert),
|
||||
|
||||
@@ -759,16 +759,15 @@ static void test_sockmap(unsigned int tasks, void *data)
|
||||
goto out_sockmap;
|
||||
}
|
||||
|
||||
/* Test update with unsupported UDP socket */
|
||||
/* Test update with unsupported unbound UDP socket */
|
||||
udp = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
i = 0;
|
||||
err = bpf_map_update_elem(fd, &i, &udp, BPF_ANY);
|
||||
if (err) {
|
||||
printf("Failed socket update SOCK_DGRAM '%i:%i'\n",
|
||||
i, udp);
|
||||
CHECK(udp < 0, "socket(AF_INET, SOCK_DGRAM)", "errno:%d\n", errno);
|
||||
err = bpf_map_update_elem(fd, &(int){0}, &udp, BPF_ANY);
|
||||
close(udp);
|
||||
if (!err) {
|
||||
printf("Unexpectedly succeeded unbound UDP update '0:%i'\n", udp);
|
||||
goto out_sockmap;
|
||||
}
|
||||
close(udp);
|
||||
|
||||
/* Test update without programs */
|
||||
for (i = 0; i < 6; i++) {
|
||||
|
||||
Reference in New Issue
Block a user