From: "Emil Tsalapatis" <emil@etsalapatis.com>
To: "Michal Luczaj" <mhal@rbox.co>,
"Eric Dumazet" <edumazet@google.com>,
"Kuniyuki Iwashima" <kuniyu@google.com>,
"Paolo Abeni" <pabeni@redhat.com>,
"Willem de Bruijn" <willemb@google.com>,
"John Fastabend" <john.fastabend@gmail.com>,
"Jakub Sitnicki" <jakub@cloudflare.com>,
"Jiayuan Chen" <jiayuan.chen@linux.dev>,
"David S. Miller" <davem@davemloft.net>,
"Jakub Kicinski" <kuba@kernel.org>,
"Simon Horman" <horms@kernel.org>,
"Daniel Borkmann" <daniel@iogearbox.net>,
"Stanislav Fomichev" <sdf@fomichev.me>,
"Martin KaFai Lau" <martin.lau@linux.dev>,
"Alexei Starovoitov" <ast@kernel.org>,
"Andrii Nakryiko" <andrii@kernel.org>,
"Eduard Zingerman" <eddyz87@gmail.com>,
"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
"Song Liu" <song@kernel.org>,
"Yonghong Song" <yonghong.song@linux.dev>,
"Jiri Olsa" <jolsa@kernel.org>,
"Emil Tsalapatis" <emil@etsalapatis.com>,
"Joe Stringer" <joe@wand.net.nz>
Cc: <netdev@vger.kernel.org>, <bpf@vger.kernel.org>,
<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH bpf 2/3] bpf: Extract shared reqsk-to-listener upgrade
Date: Wed, 29 Jul 2026 16:30:10 -0400 [thread overview]
Message-ID: <DKBC8NVXBTW8.C20199U1ITBD@etsalapatis.com> (raw)
In-Reply-To: <20260723-sockmap-lookup-tcp-leak-v1-2-1bbdc58ce514@rbox.co>
On Thu Jul 23, 2026 at 7:33 AM EDT, Michal Luczaj wrote:
> __bpf_sk_lookup() and bpf_sk_lookup() duplicate the same sk_to_full_sk()
> reqsk-to-listener upgrade. Extract it into a helper.
>
> Leave the currently unreachable WARN_ONCE as a defensive assert.
>
> No functional change.
Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>
>
> Signed-off-by: Michal Luczaj <mhal@rbox.co>
> ---
> net/core/filter.c | 57 ++++++++++++++++++++++++-------------------------------
> 1 file changed, 25 insertions(+), 32 deletions(-)
>
> diff --git a/net/core/filter.c b/net/core/filter.c
> index b446aa8be5c3..403aba3ce891 100644
> --- a/net/core/filter.c
> +++ b/net/core/filter.c
> @@ -7074,6 +7074,27 @@ __bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
> return sk;
> }
>
> +static struct sock *
> +bpf_sk_lookup_full_sk(struct sock *sk)
> +{
> + struct sock *sk2 = sk_to_full_sk(sk);
> +
> + /* sk_to_full_sk() may return sk->rsk_listener, make sure the original
> + * sk sock refcnt is decremented to prevent a request_sock leak.
> + */
> + if (sk2 != sk) {
> + sock_gen_put(sk);
> + /* Ensure there is no need to bump sk2 refcnt. */
> + if (unlikely(sk2 && !sock_flag(sk2, SOCK_RCU_FREE))) {
> + WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
> + return NULL;
> + }
> + sk = sk2;
> + }
> +
> + return sk;
> +}
> +
> static struct sock *
> __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
> struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
> @@ -7083,22 +7104,8 @@ __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
> ifindex, proto, netns_id, flags,
> sdif);
>
> - if (sk) {
> - struct sock *sk2 = sk_to_full_sk(sk);
> -
> - /* sk_to_full_sk() may return (sk)->rsk_listener, so make sure the original sk
> - * sock refcnt is decremented to prevent a request_sock leak.
> - */
> - if (sk2 != sk) {
> - sock_gen_put(sk);
> - /* Ensure there is no need to bump sk2 refcnt */
> - if (unlikely(sk2 && !sock_flag(sk2, SOCK_RCU_FREE))) {
> - WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
> - return NULL;
> - }
> - sk = sk2;
> - }
> - }
> + if (sk)
> + sk = bpf_sk_lookup_full_sk(sk);
>
> return sk;
> }
> @@ -7129,22 +7136,8 @@ bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
> struct sock *sk = bpf_skc_lookup(skb, tuple, len, proto, netns_id,
> flags);
>
> - if (sk) {
> - struct sock *sk2 = sk_to_full_sk(sk);
> -
> - /* sk_to_full_sk() may return (sk)->rsk_listener, so make sure the original sk
> - * sock refcnt is decremented to prevent a request_sock leak.
> - */
> - if (sk2 != sk) {
> - sock_gen_put(sk);
> - /* Ensure there is no need to bump sk2 refcnt */
> - if (unlikely(sk2 && !sock_flag(sk2, SOCK_RCU_FREE))) {
> - WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
> - return NULL;
> - }
> - sk = sk2;
> - }
> - }
> + if (sk)
> + sk = bpf_sk_lookup_full_sk(sk);
>
> return sk;
> }
next prev parent reply other threads:[~2026-07-29 20:30 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-23 11:33 [PATCH bpf 0/3] bpf: Fix socket leaks around connect(AF_UNSPEC)+listen() Michal Luczaj
2026-07-23 11:33 ` [PATCH bpf 1/3] bpf, sockmap: Use sock_hold() instead of refcount_inc_not_zero() in lookup Michal Luczaj
2026-07-25 0:00 ` John Fastabend
2026-07-25 18:07 ` Michal Luczaj
2026-07-29 20:29 ` Emil Tsalapatis
2026-07-23 11:33 ` [PATCH bpf 2/3] bpf: Extract shared reqsk-to-listener upgrade Michal Luczaj
2026-07-29 20:30 ` Emil Tsalapatis [this message]
2026-07-23 11:33 ` [PATCH bpf 3/3] bpf: Unconditionally take socket references in lookup helpers Michal Luczaj
2026-07-23 15:25 ` bot+bpf-ci
2026-07-29 20:32 ` Emil Tsalapatis
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=DKBC8NVXBTW8.C20199U1ITBD@etsalapatis.com \
--to=emil@etsalapatis.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=eddyz87@gmail.com \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=jakub@cloudflare.com \
--cc=jiayuan.chen@linux.dev \
--cc=joe@wand.net.nz \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=mhal@rbox.co \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sdf@fomichev.me \
--cc=song@kernel.org \
--cc=willemb@google.com \
--cc=yonghong.song@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