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 v2 1/4] bpf, sockmap: Reject unhashed UDP sockets on sockmap update
Date: Fri, 26 Jun 2026 20:58:14 +0000	[thread overview]
Message-ID: <20260626205814.BAC3C1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260626-sockmap-lookup-udp-leak-v2-1-7e7e201c951a@rbox.co>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [High] Incomplete fix: Memory ordering race allows unbound UDP sockets to be inserted into sockmap, bypassing the sk_hashed() check.

Pre-existing issues:
- [High] The patch addresses a memory leak caused by UDP sockets dynamically acquiring the SOCK_RCU_FREE flag while held by a BPF program, but leaves TCP sockets vulnerable to the exact same state-transition leak.
--

commit f2013e408c7b5bf3e40a884d6c22c888b4372b0b
Author: Michal Luczaj <mhal@rbox.co>

bpf, sockmap: Reject unhashed UDP sockets on sockmap update

This commit rejects unhashed UDP sockets during sockmap updates to prevent
a memory leak. The leak occurred because unbound UDP sockets are refcounted,
but become non-refcounted (SOCK_RCU_FREE) when bound, which skips the
refcount decrement in bpf_sk_release().

> diff --git a/net/core/sock_map.c b/net/core/sock_map.c
> index c60ba6d292f9a..9efbd8ca7db83 100644
> --- 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 does this leave TCP sockets susceptible to
the exact same state-transition leak?

If a TCP socket is held by a BPF program in TCP_ESTABLISHED state and then
transitions to TCP_LISTEN, inet_hash() unconditionally sets SOCK_RCU_FREE:

net/ipv4/inet_hashtables.c:inet_hash() {
    ...
    sock_set_flag(sk, SOCK_RCU_FREE);
    ...
}

This dynamic state change would cause bpf_sk_release() to evaluate
sk_is_refcounted(sk) as false and skip the refcount decrement, leading to
a memory leak similar to the UDP case.

> +	if (sk_is_udp(sk))
> +		return sk_hashed(sk);

[Severity: High]
Could a memory ordering regression here allow unbound UDP sockets to still
bypass this check?

In udp_v4_get_port(), the flag and hash are set:

net/ipv4/udp.c:udp_v4_get_port() {
    ...
    sock_set_flag(sk, SOCK_RCU_FREE);
    sk_add_node_rcu(sk, &hslot->head);
    ...
}

Because these stores lack a memory barrier between them, could sk_hashed(sk)
return true on another CPU while SOCK_RCU_FREE is not yet visible, allowing
the leak to still trigger?

>  	if (sk_is_stream_unix(sk))
>  		return (1 << READ_ONCE(sk->sk_state)) & TCPF_ESTABLISHED;
>  	if (sk_is_vsock(sk) &&

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260626-sockmap-lookup-udp-leak-v2-0-7e7e201c951a@rbox.co?part=1

  parent reply	other threads:[~2026-06-26 20:58 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-26 20:36 [PATCH bpf v2 0/4] bpf, sockmap: Fix sockmap leaking UDP socks Michal Luczaj
2026-06-26 20:36 ` [PATCH bpf v2 1/4] bpf, sockmap: Reject unhashed UDP sockets on sockmap update Michal Luczaj
2026-06-26 20:45   ` Kuniyuki Iwashima
2026-06-26 20:58   ` sashiko-bot [this message]
2026-06-28 21:59     ` Michal Luczaj
2026-06-29 11:37       ` Jakub Sitnicki
2026-06-29 21:40         ` Michal Luczaj
2026-07-01 10:04           ` Jakub Sitnicki
2026-07-01 22:58             ` John Fastabend
2026-07-01 23:19               ` Michal Luczaj
2026-06-29 11:38   ` Jakub Sitnicki
2026-07-01 22:59     ` John Fastabend
2026-06-26 20:36 ` [PATCH bpf v2 2/4] selftests/bpf: Ensure UDP sockets are bound Michal Luczaj
2026-06-26 20:47   ` Kuniyuki Iwashima
2026-06-28 21:58     ` Michal Luczaj
2026-06-26 20:36 ` [PATCH bpf v2 3/4] selftests/bpf: Adapt sockmap update error handling Michal Luczaj
2026-06-26 20:43   ` sashiko-bot
2026-06-28 21:58     ` Michal Luczaj
2026-06-26 20:58   ` Kuniyuki Iwashima
2026-06-28 21:59     ` Michal Luczaj
2026-06-26 21:14   ` bot+bpf-ci
2026-06-26 20:36 ` [PATCH bpf v2 4/4] selftests/bpf: Fail unbound UDP on sockmap update Michal Luczaj
2026-06-26 20:41   ` sashiko-bot
2026-06-28 21:58     ` Michal Luczaj
2026-06-26 21:03   ` Kuniyuki Iwashima
2026-07-01 23:19     ` Michal Luczaj
2026-06-27 17:34 ` [PATCH bpf v2 0/4] bpf, sockmap: Fix sockmap leaking UDP socks sun jian
2026-06-28 22:00   ` Michal Luczaj

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=20260626205814.BAC3C1F000E9@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