bpf, sockmap: Fix sockmap leaking UDP socks
Subsystems (7)
v4 cover 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/[email protected]/
---
Changes in v4:
- selftest: drop redundant if (err) [Sashiko]
- Link to v3: https://patch.msgid.link/[email protected]
Changes in v3:
- selftest: better error handling, ASSERT_*() macros [Sashiko]
- selftest: fix grammar, reorder patches [Kuniyuki]
- Link to v2: https://patch.msgid.link/[email protected]
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/[email protected]
To: Alexei Starovoitov <[email protected]>
To: Daniel Borkmann <[email protected]>
To: Andrii Nakryiko <[email protected]>
To: Eduard Zingerman <[email protected]>
To: Kumar Kartikeya Dwivedi <[email protected]>
To: Martin KaFai Lau <[email protected]>
To: Song Liu <[email protected]>
To: Yonghong Song <[email protected]>
To: Jiri Olsa <[email protected]>
To: Emil Tsalapatis <[email protected]>
To: Shuah Khan <[email protected]>
To: John Fastabend <[email protected]>
To: Jakub Sitnicki <[email protected]>
To: Jiayuan Chen <[email protected]>
To: Eric Dumazet <[email protected]>
To: Kuniyuki Iwashima <[email protected]>
To: Paolo Abeni <[email protected]>
To: Willem de Bruijn <[email protected]>
To: "David S. Miller" <[email protected]>
To: Jakub Kicinski <[email protected]>
To: Simon Horman <[email protected]>
To: Cong Wang <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
Michal Luczaj (4):
selftests/bpf: Ensure UDP sockets are bound
bpf, sockmap: Reject unhashed UDP sockets on sockmap update selftests/bpf: Adapt sockmap update error handling selftests/bpf: Fail unbound UDP on sockmap update
net/core/sock_map.c | 2 ++ .../selftests/bpf/prog_tests/sockmap_basic.c | 6 +++--- .../selftests/bpf/prog_tests/sockmap_listen.c | 21 +++++++++++---------- tools/testing/selftests/bpf/test_maps.c | 13 ++++++------- 4 files changed, 22 insertions(+), 20 deletions(-)
---
base-commit: 17566290dc802b03a8127729b19c5962d63a7947
change-id: 20260617-sockmap-lookup-udp-leak-bc4e5c5481d7
Best regards,
Signature · 1 line
Michal Luczaj <[email protected]>
PATCH v4 1/4 selftests/bpf: Ensure UDP sockets are bound
Update sockmap_basic tests to bind sockets before they are used. This accommodates the recent change in sockmap that rejects unbound UDP sockets.
--- tools/testing/selftests/bpf/prog_tests/sockmap_basic.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/sockmap_basic.c +3 -3
@@ -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);
Signature · 1 line
2.55.0
PATCH v4 2/4 bpf, sockmap: Reject unhashed UDP sockets on sockmap update
In reply to Michal LuczajUDP sockets get SOCK_RCU_FREE set when (auto-)bound. This means sk_is_refcounted(unbound) = true, while sk_is_refcounted(bound) = false.
Because sockmap accepts unbound UDP sockets, a BPF program can increment a socket's refcount via lookup. If the socket is subsequently bound, the transition from unbound to bound causes bpf_sk_release() to skip the decrement of the refcount, causing a memory leak.
unreferenced object 0xffff88810bc2eb40 (size 1984):
comm "test_progs", pid 2451, jiffies 4295320596 hex dump (first 32 bytes):
7f 00 00 01 7f 00 00 01 d2 04 1b b7 04 d2 00 00 ................
02 00 01 40 00 00 00 00 00 00 00 00 00 00 00 00 ...@............
backtrace (crc bdee079d):
kmem_cache_alloc_noprof+0x557/0x660
sk_prot_alloc+0x69/0x240
sk_alloc+0x30/0x460
inet_create+0x2ce/0xf80
__sock_create+0x25b/0x5c0
__sys_socket+0x119/0x1d0
__x64_sys_socket+0x72/0xd0
do_syscall_64+0xa1/0x5f0
entry_SYSCALL_64_after_hwframe+0x76/0x7eInstead of special-casing for refcounted sockets, reject unhashed UDP sockets during sockmap updates, as there is no benefit to supporting those. This effectively reverts the commit under Fixes, with two exceptions:
1. sock_map_sk_state_allowed() maintains a fall-through return true.
2. In the spirit of commit b8b8315e39ff ("bpf, sockmap: Remove unhash handler for BPF sockmap usage"), the proto::unhash BPF handler is not reintroduced.
Historical note: this issue is related to commit 67312adc96b5 ("bpf: reject unhashed sockets in bpf_sk_assign").
--- net/core/sock_map.c | 2 ++ 1 file changed, 2 insertions(+)
net/core/sock_map.c +2 -0
@@ -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) &&
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf v4 2/4] bpf, sockmap: Reject unhashed UDP sockets on sockmap update
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] The patch prevents a refcount leak for UDP sockets by rejecting unhashed sockets, but an identical reference count leak can be triggered for TCP sockets because sk_is_refcounted() relies on the mutable SOCK_RCU_FREE flag. --
--- Patch [2]: [PATCH bpf v4 2/4] bpf, sockmap: Reject unhashed UDP sockets on sockmap update --- commit efe688dfced966a49066087e860ab73696a8f75a
Author: Michal Luczaj <[email protected]> Date: Tue Jul 7 04:23:57 2026 +0000
bpf, sockmap: Reject unhashed UDP sockets on sockmap update
This patch addresses a socket reference count leak by preventing unhashed UDP sockets from being updated in sockmaps. It removes the ability for BPF programs to increment a refcount on an unbound socket that subsequently gets bound and skips the refcount decrement upon release.
net/core/sock_map.c static bool sock_map_sk_state_allowed(const struct sock *sk) · line 544
Quoted 7 lines
net/core/sock_map.c +0
-0@@ -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 );
[Severity: High]
This is a pre-existing issue, but can the exact same reference count leak happen for TCP sockets because sk_is_refcounted() relies on the mutable SOCK_RCU_FREE flag?
If a user creates a TCP socket, connects it (entering TCP_ESTABLISHED), and adds it to a sockmap, a BPF program can call bpf_map_lookup_elem() on the sockmap. This increments the socket's refcount because SOCK_RCU_FREE is not set on established sockets.
If a user thread concurrently calls connect(AF_UNSPEC) to disconnect the socket (moving it to TCP_CLOSE and removing it from the map), and then calls listen() on the same socket, inet_hash() unconditionally sets the SOCK_RCU_FREE flag.
When the BPF program subsequently calls bpf_sk_release(sk):
net/core/filter.c:bpf_sk_release() {
...
if (sk && sk_is_refcounted(sk))
sock_gen_put(sk);
...
}sk_is_refcounted(sk) will now return false because SOCK_RCU_FREE is set, causing bpf_sk_release() to skip the decrement and permanently leak the acquired reference.
Could this sequence be abused to cause memory exhaustion, and is there a way to safely track the acquired references without relying on a flag that might change mid-flight?
Quoted 4 lines
net/core/sock_map.c +2
-0+if (sk_is_udp (sk)) +return sk_hashed (sk);if (sk_is_stream_unix (sk))return (1 <<READ_ONCE (sk->sk_state)) &TCPF_ESTABLISHED ;
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf v4 2/4] bpf, sockmap: Reject unhashed UDP sockets on sockmap update
Quoted 50 lines · sashiko-bot
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] The patch prevents a refcount leak for UDP sockets by rejecting unhashed sockets, but an identical reference count leak can be triggered for TCP sockets becausesk_is_refcounted()relies on the mutableSOCK_RCU_FREEflag. ----- Patch [2]: [PATCH bpf v4 2/4] bpf, sockmap: Reject unhashed UDP sockets on sockmap update --- commit
efe688dfced966a49066087e860ab73696a8f75aAuthor: Michal Luczaj <[email protected]> Date: Tue Jul 7 04:23:57 2026 +0000bpf, sockmap: Reject unhashed UDP sockets on sockmap update
This patch addresses a socket reference count leak by preventing unhashed UDP sockets from being updated in sockmaps. It removes the ability for BPF programs to increment a refcount on an unbound socket that subsequently gets bound and skips the refcount decrement upon release.
net/core/sock_map.c static bool sock_map_sk_state_allowed(const struct sock *sk) · line 544
Quoted 7 lines
net/core/sock_map.c +0
-0@@ -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 );[Severity: High]
This is a pre-existing issue, but can the exact same reference count leak happen for TCP sockets becausesk_is_refcounted()relies on the mutableSOCK_RCU_FREEflag?If a user creates a TCP socket, connects it (entering
TCP_ESTABLISHED), and adds it to a sockmap, a BPF program can callbpf_map_lookup_elem()on the sockmap. This increments the socket's refcount becauseSOCK_RCU_FREEis not set on established sockets.If a user thread concurrently calls
connect(AF_UNSPEC)to disconnect the socket (moving it toTCP_CLOSEand removing it from the map), and then callslisten()on the same socket,inet_hash()unconditionally sets theSOCK_RCU_FREEflag.When the BPF program subsequently calls
bpf_sk_release(sk):net/core/filter.c:bpf_sk_release() { ... if (sk && sk_is_refcounted(sk)) sock_gen_put(sk); ... }
Quoted 7 lines
sk_is_refcounted(sk)will now return false becauseSOCK_RCU_FREEis set, causingbpf_sk_release()to skip the decrement and permanently leak the acquired reference.Could this sequence be abused to cause memory exhaustion, and is there a way to safely track the acquired references without relying on a flag that might change mid-flight?
Could sockmap/net experts comment on whether this case needs fixing too?
Quoted 6 lines
Quoted 5 lines
net/core/sock_map.c +2
-0+if (sk_is_udp (sk)) +return sk_hashed (sk);if (sk_is_stream_unix (sk))return (1 <<READ_ONCE (sk->sk_state)) &TCPF_ESTABLISHED ;[ ... ]
Re: [PATCH bpf v4 2/4] bpf, sockmap: Reject unhashed UDP sockets on sockmap update
Quoted 55 lines · Kumar Kartikeya Dwivedi
Quoted 54 lines · sashiko-bot
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] The patch prevents a refcount leak for UDP sockets by rejecting unhashed sockets, but an identical reference count leak can be triggered for TCP sockets becausesk_is_refcounted()relies on the mutableSOCK_RCU_FREEflag. ----- Patch [2]: [PATCH bpf v4 2/4] bpf, sockmap: Reject unhashed UDP sockets on sockmap update --- commit
efe688dfced966a49066087e860ab73696a8f75aAuthor: Michal Luczaj <[email protected]> Date: Tue Jul 7 04:23:57 2026 +0000bpf, sockmap: Reject unhashed UDP sockets on sockmap update
This patch addresses a socket reference count leak by preventing unhashed UDP sockets from being updated in sockmaps. It removes the ability for BPF programs to increment a refcount on an unbound socket that subsequently gets bound and skips the refcount decrement upon release.
net/core/sock_map.c static bool sock_map_sk_state_allowed(const struct sock *sk) · line 544
Quoted 7 lines
net/core/sock_map.c +0
-0@@ -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 );[Severity: High]
This is a pre-existing issue, but can the exact same reference count leak happen for TCP sockets becausesk_is_refcounted()relies on the mutableSOCK_RCU_FREEflag?If a user creates a TCP socket, connects it (entering
TCP_ESTABLISHED), and adds it to a sockmap, a BPF program can callbpf_map_lookup_elem()on the sockmap. This increments the socket's refcount becauseSOCK_RCU_FREEis not set on established sockets.If a user thread concurrently calls
connect(AF_UNSPEC)to disconnect the socket (moving it toTCP_CLOSEand removing it from the map), and then callslisten()on the same socket,inet_hash()unconditionally sets theSOCK_RCU_FREEflag.When the BPF program subsequently calls
bpf_sk_release(sk):net/core/filter.c:bpf_sk_release() { ... if (sk && sk_is_refcounted(sk)) sock_gen_put(sk); ... }
sk_is_refcounted(sk)will now return false becauseSOCK_RCU_FREEis set, causingbpf_sk_release()to skip the decrement and permanently leak the acquired reference.
Quoted 5 lines
Quoted 3 lines
Could this sequence be abused to cause memory exhaustion, and is there a way to safely track the acquired references without relying on a flag that might change mid-flight?
Could sockmap/net experts comment on whether this case needs fixing too?
I'm not an expert, but I think Sashiko is right. Please see
https://lore.kernel.org/bpf/[email protected]/
I'll take care of it in a follow up series.
thanks,
Michal
PATCH v4 3/4 selftests/bpf: Adapt sockmap update error handling
In reply to Michal LuczajUpdate sockmap_listen to accommodate the recent change in sockmap that rejects unbound UDP sockets.
TCP: Reject unbound and bound (unless established or listening).
UDP: Accept only bound sockets.
While at it, migrate to ASSERT_* and enforce reverse xmas tree.
--- .../selftests/bpf/prog_tests/sockmap_listen.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-)
tools/testing/selftests/bpf/prog_tests/sockmap_listen.c +11 -10
@@ -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),
Signature · 1 line
2.55.0
PATCH v4 4/4 selftests/bpf: Fail unbound UDP on sockmap update
In reply to Michal Luczajsockmap now rejects unbound UDP sockets. Adjust test_maps. While at it, check socket()'s return value.
This effectively reverts commit c39aa2159974 ("bpf, selftests: Fix test_maps now that sockmap supports UDP").
--- tools/testing/selftests/bpf/test_maps.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
tools/testing/selftests/bpf/test_maps.c +6 -7
@@ -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++) {
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf v4 4/4] selftests/bpf: Fail unbound UDP on sockmap update
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Usage of the deprecated CHECK() macro in new patch code. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Usage of the deprecated CHECK() macro in new patch code.
--
--- Patch [4]: [PATCH bpf v4 4/4] selftests/bpf: Fail unbound UDP on sockmap update --- commit 97dab8be7b651b7cede0184ca7321b97bf080f13
Author: Michal Luczaj <[email protected]>
selftests/bpf: Fail unbound UDP on sockmap update
This commit adjusts test_maps to reflect that sockmap now rejects unbound UDP sockets. It adds a check for the socket() return value and effectively reverts an earlier commit.
Quoted 6 lines
tools/testing/selftests/bpf/test_maps.c +0
-0@@ -759,16 +759,15 @@ static void test_sockmap(unsigned int tasks, void *data) goto out_sockmap; }
tools/testing/selftests/bpf/test_maps.c static void test_sockmap(unsigned int tasks, void *data) · line 764
Quoted 10 lines
tools/testing/selftests/bpf/test_maps.c +2
-6-/* 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);
[Severity: Low]
This isn't a bug, but could we use ASSERT_OK_FD() or ASSERT_GE() instead of the deprecated CHECK() macro here?
The BPF subsystem coding guidelines prefer using the modern ASSERT_* family of macros for new tests and updates.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
This series was applied to bpf/bpf.git (master) by Kumar Kartikeya Dwivedi <[email protected]>:
Hello:
This series was applied to bpf/bpf.git (master)
by Kumar Kartikeya Dwivedi <[email protected]>:
Quoted 9 lines · On Tue, 07 Jul 2026 06:23:55 +0200 you wrote:
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/[email protected]/Signed-off-by Michal Luczaj <[email protected]>[...]
Here is the summary with links:
- [bpf,v4,1/4] selftests/bpf: Ensure UDP sockets are bound
https://git.kernel.org/bpf/bpf/c/fe3ff273767e - [bpf,v4,2/4] bpf, sockmap: Reject unhashed UDP sockets on sockmap update https://git.kernel.org/bpf/bpf/c/66efd3368ae1 - [bpf,v4,3/4] selftests/bpf: Adapt sockmap update error handling https://git.kernel.org/bpf/bpf/c/30581eda4a07 - [bpf,v4,4/4] selftests/bpf: Fail unbound UDP on sockmap update https://git.kernel.org/bpf/bpf/c/203b06932777
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html