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 v3 2/2] bpf: Fix reference leak in bpf_sk_assign()
Date: Wed, 02 Sep 2026 18:38:40 +0000	[thread overview]
Message-ID: <20260902183840.9756B1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260902-sockmap-lookup-tcp-leak-v3-2-b998b3d49d07@rbox.co>

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

Pre-existing issues:
- [Critical] bpf_sk_assign_tcp_reqsk() stores an unreferenced pointer to a TCP_LISTEN socket inside a request socket. If the packet is queued via NF_QUEUE, the listener socket can be freed, causing a Use-After-Free when the packet is reinjected and stolen by tcp_v4_rcv().
--

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

bpf: Fix reference leak in bpf_sk_assign()

This patch aims to fix a reference leak in bpf_sk_assign() by freezing the
decision on whether a socket is refcounted at assign time. It introduces a
new destructor sock_pfree_refcounted() and modifies __nf_queue() to bump
references for prefetched sockets without references.

> diff --git a/net/netfilter/nf_queue.c b/net/netfilter/nf_queue.c
> index 73363ceedebe3..e2e9739c00ac5 100644
> --- a/net/netfilter/nf_queue.c
> +++ b/net/netfilter/nf_queue.c
> @@ -195,16 +195,16 @@ static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state,
>  		break;
>  	}
>  
> -	if (skb_sk_is_prefetched(skb)) {
> -		struct sock *sk = skb->sk;
> -
> -		if (!sk_is_refcounted(sk)) {
> -			if (!refcount_inc_not_zero(&sk->sk_refcnt))
> -				return -ENOTCONN;
> +	/*
> +	 * If the skb was prefetched without taking sock's ref, bump it.
> +	 * Skip sock_pfree-dtor'ed skbs coming from bpf_sk_assign_tcp_reqsk().
> +	 */
> +	if (skb_sk_is_prefetched_noref(skb) && sk_fullsock(skb->sk)) {
[Severity: Critical]
This isn't a bug introduced by this patch, but does maintaining this behavior
for request sockets leave an unreferenced listener socket exposed to a
use-after-free?

In bpf_sk_assign_tcp_reqsk(), a request socket is allocated without taking a
reference on the listener socket (attach_listener = false), and the
unreferenced listener is stored in req->rsk_listener.

Because sk_fullsock() evaluates to false for request sockets, this check
bypasses bumping the reference count for the SKB when it is handed to
netfilter queue. While the SKB is held in userspace, it escapes the RCU
read-side critical section. If the listener socket is closed and freed
during this time, what happens when the packet is reinjected?

It appears that when tcp_v4_rcv() processes the reinjected packet, it calls
skb_steal_sock() which extracts the freed req->rsk_listener, clears the
destructor (bypassing sock_pfree), and returns the freed socket for use,
triggering a use-after-free.

Could this be triggered by a user with privileges to set up a TC BPF rule
and netfilter queue?

> +		if (!refcount_inc_not_zero(&skb->sk->sk_refcnt))
> +			return -ENOTCONN;
>  
> -			/* drop refcount on skb_orphan */
> -			skb->destructor = sock_edemux;
> -		}
> +		/* drop refcount on skb_orphan */
> +		skb->destructor = sock_edemux;
>  	}
>  
>  	entry = kmalloc(sizeof(*entry) + route_key_size, GFP_ATOMIC);

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260902-sockmap-lookup-tcp-leak-v3-0-b998b3d49d07@rbox.co?part=2

  reply	other threads:[~2026-09-02 18:38 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-02 17:52 [PATCH bpf v3 0/2] bpf: Fix socket leaks around connect(AF_UNSPEC)+listen() Michal Luczaj
2026-09-02 17:52 ` [PATCH bpf v3 1/2] bpf: Unconditionally take socket references in lookup helpers Michal Luczaj
2026-09-02 17:52 ` [PATCH bpf v3 2/2] bpf: Fix reference leak in bpf_sk_assign() Michal Luczaj
2026-09-02 18:38   ` sashiko-bot [this message]
2026-09-02 18:58 ` [PATCH bpf v3 0/2] bpf: Fix socket leaks around connect(AF_UNSPEC)+listen() Kuniyuki Iwashima
2026-09-02 22:52   ` Jakub Kicinski
2026-09-02 23:04     ` Kuniyuki Iwashima
2026-09-02 23:06       ` Kuniyuki Iwashima

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=20260902183840.9756B1F000E9@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