All of lore.kernel.org
 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 v3 2/4] bpf, sockmap: Reject unhashed UDP sockets on sockmap update
Date: Wed, 01 Jul 2026 23:55:50 +0000	[thread overview]
Message-ID: <20260701235552.2B0AA1F00A3F@smtp.kernel.org> (raw)
In-Reply-To: <20260702-sockmap-lookup-udp-leak-v3-2-ff8de8782468@rbox.co>

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

Pre-existing issues:
- [High] The refcount leak fixed for UDP sockets by this patch still exists for TCP sockets (and via bpf_sk_lookup_tcp) because bpf_sk_release relies on the mutable SOCK_RCU_FREE flag.
--

commit 120f1940dc9e4cc498f383925d488cf77882caea
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 when a socket transitions from unbound to bound. This transition
causes bpf_sk_release() to skip the refcount decrement due to the SOCK_RCU_FREE
flag being set.

> 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 isn't a bug introduced by this patch, but does the same refcount leak
still exist for TCP sockets?

A BPF program can acquire a reference to a TCP socket in the TCP_ESTABLISHED
state (e.g., via bpf_sk_lookup_tcp or bpf_map_lookup_elem on a sockmap) because
TCP_ESTABLISHED sockets do not have the SOCK_RCU_FREE flag set.

If a user-space thread concurrently calls connect(AF_UNSPEC) to transition the
socket to TCP_CLOSE, followed by listen(), inet_hash() will dynamically set the
SOCK_RCU_FREE flag on the socket.

When the BPF program finishes and calls bpf_sk_release():

net/core/filter.c:bpf_sk_release() {
	if (sk && sk_is_refcounted(sk))
		sock_gen_put(sk);
	return 0;
}

Because the flag was just set, sk_is_refcounted(sk) will dynamically evaluate
to false, skipping sock_gen_put(sk) and permanently leaking the acquired
reference.

> +	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/20260702-sockmap-lookup-udp-leak-v3-0-ff8de8782468@rbox.co?part=2

  reply	other threads:[~2026-07-01 23:55 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-01 23:28 [PATCH bpf v3 0/4] bpf, sockmap: Fix sockmap leaking UDP socks Michal Luczaj
2026-07-01 23:28 ` [PATCH bpf v3 1/4] selftests/bpf: Ensure UDP sockets are bound Michal Luczaj
2026-07-02 10:35   ` Jakub Sitnicki
2026-07-01 23:28 ` [PATCH bpf v3 2/4] bpf, sockmap: Reject unhashed UDP sockets on sockmap update Michal Luczaj
2026-07-01 23:55   ` sashiko-bot [this message]
2026-07-02  7:30     ` Michal Luczaj
2026-07-01 23:28 ` [PATCH bpf v3 3/4] selftests/bpf: Adapt sockmap update error handling Michal Luczaj
2026-07-02  0:00   ` bot+bpf-ci
2026-07-02  7:30     ` Michal Luczaj
2026-07-02 10:52   ` Jakub Sitnicki
2026-07-01 23:28 ` [PATCH bpf v3 4/4] selftests/bpf: Fail unbound UDP on sockmap update Michal Luczaj
2026-07-01 23:34   ` sashiko-bot
2026-07-02  7:30     ` Michal Luczaj
2026-07-02 11:01   ` Jakub Sitnicki

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=20260701235552.2B0AA1F00A3F@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.