BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Michal Luczaj" <mhal@rbox.co>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf v4 2/4] bpf, sockmap: Reject unhashed UDP sockets on sockmap update
Date: Tue, 07 Jul 2026 04:53:38 +0000	[thread overview]
Message-ID: <20260707045338.B30C41F00A3A@smtp.kernel.org> (raw)
In-Reply-To: <20260707-sockmap-lookup-udp-leak-v4-2-f878346f27ab@rbox.co>

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 <mhal@rbox.co>
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.

> diff --git a/net/core/sock_map.c b/net/core/sock_map.c
> --- a/net/core/sock_map.c
> +++ b/net/core/sock_map.c
> @@ -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?

> +	if (sk_is_udp(sk))
> +		return sk_hashed(sk);
>  	if (sk_is_stream_unix(sk))
>  		return (1 << READ_ONCE(sk->sk_state)) & TCPF_ESTABLISHED;
[ ... ]

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260707-sockmap-lookup-udp-leak-v4-0-f878346f27ab@rbox.co?part=2

  reply	other threads:[~2026-07-07  4:53 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-07  4:23 [PATCH bpf v4 0/4] bpf, sockmap: Fix sockmap leaking UDP socks Michal Luczaj
2026-07-07  4:23 ` [PATCH bpf v4 1/4] selftests/bpf: Ensure UDP sockets are bound Michal Luczaj
2026-07-07  4:23 ` [PATCH bpf v4 2/4] bpf, sockmap: Reject unhashed UDP sockets on sockmap update Michal Luczaj
2026-07-07  4:53   ` sashiko-bot [this message]
2026-07-07  4:23 ` [PATCH bpf v4 3/4] selftests/bpf: Adapt sockmap update error handling Michal Luczaj
2026-07-07  4:23 ` [PATCH bpf v4 4/4] selftests/bpf: Fail unbound UDP on sockmap update Michal Luczaj
2026-07-07  4:31   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260707045338.B30C41F00A3A@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=mhal@rbox.co \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox