🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #19221

Re: [PATCH bpf-next 3/8] bpf: allow bpf_sk_assign on tc egress with refcounted sockets

#19221
From: bot+bpf-ci
Date: Thu, 19 Nov 2026 15:19
158 lines
14274 bytes
--===============8478692278203113852==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

> bpf: allow bpf_sk_assign on tc egress with refcounted sockets

TL;DR:
- bpf_sk_assign() on egress may hit BUG_ON in skb_orphan() for test-run skbs
  with skb->sk but no destructor
- skb_do_redirect() may keep a borrowed listener via syncookie reqsk, a
  possible use-after-free; also possible socket double-put

> diff --git a/net/core/filter.c b/net/core/filter.c
> --- a/net/core/filter.c
> +++ b/net/core/filter.c
> @@ -2559,14 +2559,17 @@ int skb_do_redirect(struct sk_buff *skb)
>  	if (unlikely(!dev))
>  		goto out_drop;
>
> +#ifdef CONFIG_INET
>  	/*
> -	 * Prefetched sockets may borrow a listener under RCU. Orphan them
> -	 * before redirects that can outlive this section. Peer ingress
> -	 * stays in the same RCU section.
> +	 * Prefetched listeners may be borrowed under RCU. Drop borrowed
> +	 * assignments before redirects that can outlive this section.
> +	 * Peer ingress stays in the same RCU section.
>  	 */
> -	if (skb_sk_is_prefetched(skb) &&
> +	if (skb->destructor == sock_pfree &&
> +	    !sk_is_refcounted(skb->sk) &&
>  	    (!(flags & BPF_F_PEER) || (flags & BPF_F_EGRESS)))
>  		skb_orphan(skb);
> +#endif

Can this leave a borrowed listener behind a redirect?

bpf_sk_assign_tcp_reqsk() installs a syncookie request sock whose
rsk_listener is a borrowed SOCK_RCU_FREE listener:

    req = inet_reqsk_alloc(ops, sk, false);   /* no ref on sk */
    req->rsk_listener = sk;
    skb->sk = req_to_sk(req);
    skb->destructor = sock_pfree;

skb->sk is a TCP_NEW_SYN_RECV request sock, so sk_is_refcounted(skb->sk)
is true and the new condition is false.  The skb then keeps the reqsk
across bpf_redirect(ifindex, BPF_F_INGRESS) and non-peer egress
redirects.  For example:

    sch_handle_ingress()
      skb_do_redirect()                    /* no orphan */
        __bpf_redirect_common()
          __bpf_rx_skb()
            dev_forward_skb_nomtu()
              netif_rx_internal()          /* queued past the RCU section */

Later tcp_v4_rcv() -> __inet_lookup_skb() -> inet_steal_sock() ->
skb_steal_sock() takes the syncookie branch:

    *refcounted = false;
    sk = req->rsk_listener;
    req->rsk_listener = NULL;
    return sk;

and inet_steal_sock() dereferences that listener.  If the listener was
closed and its SOCK_RCU_FREE grace period elapsed while the skb sat in
the backlog or a qdisc, is this a use-after-free?

The updated comment says borrowed listener assignments are dropped, and the
commit message describes "sock_pfree + !sk_is_refcounted" as "the true
no-refcount case from ingress", but the reqsk case above is not covered.

Separately, refcounted sock_pfree assignments made at ingress now survive
non-peer redirects into egress.  complete_skb() and
chcr_ktls_copy_record_in_skb() treat any destructor other than
sock_efree/sock_pfree_ref as charged to sk_wmem_alloc, and chcr copies
sock_pfree to nskb without sock_hold().  Would that double-put the socket
when both skbs are freed?  The commit message says only sock_pfree_ref
sockets are meant to be preserved; would testing
skb->destructor == sock_pfree alone, as before, keep that and close both
problems?

[ ... ]

> @@ -8115,21 +8118,26 @@ static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = {
>
>  BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags)
>  {
> +	bool egress = !skb_at_tc_ingress(skb);
> +	bool refcounted;
> +
>  	if (!sk || flags != 0)
>  		return -EINVAL;
> -	if (!skb_at_tc_ingress(skb))
> +	if (egress && (!sk_fullsock(sk) || !sk_is_refcounted(sk) ||
> +		       skb_is_decrypted(skb)))
>  		return -EOPNOTSUPP;
> +	refcounted = egress || sk_is_refcounted(sk);
>  	if (unlikely(dev_net(skb->dev) != sock_net(sk)))
>  		return -ENETUNREACH;
>  	if (sk_unhashed(sk))
>  		return -EOPNOTSUPP;
> -	if (sk_is_refcounted(sk) &&
> +	if (refcounted &&
>  	    unlikely(!refcount_inc_not_zero_acquire(&sk->sk_refcnt)))
>  		return -ENOENT;
>
>  	skb_orphan(skb);
>  	skb->sk = sk;
> -	skb->destructor = sock_pfree;
> +	skb->destructor = egress ? sock_pfree_ref : sock_pfree;

Can skb_orphan() hit BUG_ON(skb->sk) here?

Removing the !skb_at_tc_ingress() rejection lets every non-ingress skb
reach skb_orphan(), including skbs that have skb->sk set and no
destructor.  bpf_prog_test_run_skb() builds exactly such an skb:

    sk = sk_alloc(net, AF_UNSPEC, GFP_USER, &bpf_dummy_proto, 1);
    skb = slab_build_skb(data);
    skb->sk = sk;          /* no destructor */

tc_at_ingress is zero there and convert___skb_to_skb() never sets it, so
skb_at_tc_ingress() is false and the new egress branch is taken.  A tc
program that does bpf_sk_lookup_tcp(skb, &tuple, ..., BPF_F_CURRENT_NETNS,
0) on an established socket and then bpf_sk_assign(skb, sk, 0) passes
sk_fullsock(), sk_is_refcounted(), skb_is_decrypted(), the netns check and
sk_unhashed(), and refcount_inc_not_zero_acquire() succeeds.  Then:

    static __always_inline void skb_orphan(struct sk_buff *skb)
    {
        if (skb->destructor) {
            ...
        } else {
            BUG_ON(skb->sk);   /* skb->sk == dummy sk */
        }
    }

Before this patch the same program got -EOPNOTSUPP and never reached
skb_orphan().  The selftest added later in the series (probe_timewait in
sk_assign_netkit) goes through bpf_prog_test_run_opts() but only avoids
the BUG because its socket is in TIME_WAIT and fails sk_fullsock().  A
later patch in the series that accepts listeners does not add a guard
either.

Could the egress branch reject skbs that have skb->sk set but no
destructor (or the test-run context) before calling skb_orphan()?


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/44548126067
--===============8478692278203113852==--
Technical Details
Message-ID: <a4c31b033613fa51016f2cf42f90e3f2c61d0e9a8c1012c20e5074a0e92bbe95@mail.kernel.org>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-367109-linux-kernel-netdev=dewy.ru
References: <20261119142231.2612515-4-razor@blackwall.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-367109-linux-kernel-netdev=dewy.ru
From: bot+bpf-ci@kernel.org
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH bpf-next 3/8] bpf: allow bpf_sk_assign on tc egress with refcounted sockets
Date: Thu, 19 Nov 2026 15:19:14 +0000 (UTC)
Organization: Usenet Network
Message-ID: <a4c31b033613fa51016f2cf42f90e3f2c61d0e9a8c1012c20e5074a0e92bbe95@mail.kernel.org>
References: <20261119142231.2612515-4-razor@blackwall.org>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="===============8478692278203113852=="
Injection-Info: usenet.network;
	logging-data="2909793"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:237abQRLs3RfLY+lgbTqZTddB4M= sha256:gWNJUeu1bjMQ6Oq/WF2rxjH+oGbniAniL9DGtGwbsrU=
	sha1:T3K9pmXnFUamp88bUT2hJAX0FMI= sha256:gyZQLeYfhaHIpDdNLUL0mNXMOsPsD4H+kcjeNWZBF1Q=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 5607A1156BD; Thu, 19 Nov 2026 18:19:43 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.401
X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id C01rKCY0tLMV;
	Thu, 19 Nov 2026 18:19:39 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-367109-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 1CEAC1156B1; Thu, 19 Nov 2026 18:19:29 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id 10125265893; Thu, 19 Nov 2026 15:19:25 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1795101559;
	b=DYzeXCGvsTEKHV+v7wkpqUdxCP5fRb+oRazwNDIpwg3h+tjlkxvtloVuWDVbruEDf4Yf
	 5gkkQeyaymbD+j6KIkvPXxePO8kncWBSj30Mri6IPWr7472pS6wqUZ5wYF9SsRd/UxQXN
	 x9/1AitD9KRAr4K8Xh38FLbFTR0nJ0fEwulETZ1VPv0PVVnenhSEv+r2y6FZ0VonpDMio
	 tyj/vGYWRpHqZ89LXz2tmzIuG3R5OdfcNGh971sKGiu6LK7VFPK0gGZGwNgYB1ZG0zC05
	 zDXk04iNzwh4hVtOflKpIpFKo+EE4MzswjamgsoJPZEBUFmGwrmD41+l0w1izY/84Sg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795101559;
	h=X-Original-To:Received:Received:DKIM-Signature:Content-Type:
	 X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:Message-Id:
	 In-Reply-To:References:Subject:From:To:Cc:Date;
	bh=M7sCKL1sfvK10u19rDG/6Mu1LXARQdWjOTJLt9g16i4=;
	b=s492iDUtcduhmqNilF0cd19T+8iXh/DVT1GxaBpsAHl7JVneiRNed621rhZmqEAVyMpI
	 QBghQX69kjgnpmnZIo8evjsaZUTPu8IyrKqTYaBk1R3SMHIUHwX2uopnCuwedGxqDlmgD
	 Jvg3TbAJGUtSeL6tJq76qWPachsQ6QPfHlEAaKPfsgCsmLYfCZRLujyLi5+j4HSBcB2qL
	 aBWr39MfsXiIBJieiLS6xzwwidPShtBL7SPJbfQBRxrCKyeebDxBi/VIM6oPgplRUeNoe
	 qF3Afzm+j63yXodJ2Mo0oNJzfodBXU7HvrouL6I1yDHhRTVyhfhiPiZ6zhaV4BiKxCA==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Lg0JDFKB;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 0BCFD606A32F;
	Thu, 19 Nov 2026 15:19:19 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Lg0JDFKB
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1795101558;
	b=x7jQKlbqGoGmBLKvZrrTr+nX/ZEU/ORHtgwR7kPQU+7lJxxi+5PwKhCnNgsGFpOkXGVj
	 iTK7omWs3inAcTfzb4uQfDKTGATJnq3TqH6gl/KIY+bdiKtRpr58nHIukcUrdH/vNC7Fp
	 uJTlUY/7YQAxa18cGVPWEjnhddhjAf5L/0Vmrc4g8u9z3NSP76yyzalUtAzCp59GdYxeP
	 gfxhd0bO3JdexHNU+Ef/GW0NJzCFYRq8B2If3bduIJ2kQ5khG4f57d42PdjaLkdgDNeY6
	 cmGCG7Iz8/kDhoJrMQ5nF7lemLRJve4+Y0ItzDngGHbOk2hQrpKP54rDZJlaNP9brLA==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795101558;
	h=Received:DKIM-Signature:Content-Type:MIME-Version:Message-Id:
	 In-Reply-To:References:Subject:From:To:Cc:Date;
	bh=M7sCKL1sfvK10u19rDG/6Mu1LXARQdWjOTJLt9g16i4=;
	b=Mr3JZ8zLrnad+prTfHnpr5q13ZrZonK2rBu6xQsAQMY2EgP25/xOUByoOlHxq+8q0aa5
	 MVtliO68KGeCb4IwtAGH7flGiwQX5XZ+tfnEprzHwLz4yrXJwUSpA29Fb8otzFOLfQG9k
	 oXD9HG49fPGsl4DJj9WdWUGuWF1TKCNP7maEbOuwdLxFDKv3TokFlx99+o6Hlc4MGoP+/
	 NjUC8IW8wm/FsL/oiaudi1RCJBP7sg8HRmC7b1hFFBFS+MEgumGOSMTxya4jNIfzgXQnE
	 xrpGgVKf04l1f4LBSJjx7OyVOIt0ORHPSSkFdoR72tChEa3U2mN9TPzOf4jRDToJyDA==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Lg0JDFKB;
	arc=none smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2CB63606A325;
	Thu, 19 Nov 2026 15:19:17 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 504201F000C5;
	Thu, 19 Nov 2026 15:19:14 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1795101557;
	bh=M7sCKL1sfvK10u19rDG/6Mu1LXARQdWjOTJLt9g16i4=;
	h=In-Reply-To:References:Subject:From:To:Cc:Date;
	b=Lg0JDFKBjuwS7FakiWE1sm+uH5LVfHqt63AR+fjt5IVyYJ2cgez6hfUzxDeIh04L6
	 hbMhpZCT54lfwJfrUTZlAQ++RJ/rtanPH4dpkuickkE2ZGrD8XP9qWrOM0tTpRL1XT
	 5Bb/bARJ7Z4sxepYk2N2p8btO2l9C/F7EPPTtDdRL9LKfxsji0p5JXtgeOLLpCZnQi
	 jgLqNP2LdUVnEo42oH+zP4c/RR8REdDAegwgM74OczLswvr+9kt3pLcPQv8wGTnl3G
	 Qxt2x79tZ3M1iYtmVb5sCxZcRnvkBN7+zuhwnQWCieevMT2Dtfb2i//4rQdvSHefXU
	 RLO6rdJ9vhBlQ==
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list
X-In-Reply-To: <20261119142231.2612515-4-razor@blackwall.org>
X-Cc: daniel@iogearbox.net,razor@blackwall.org,ayush.sawal@chelsio.com,andrew+netdev@lunn.ch,davem@davemloft.net,edumazet@kernel.org,kuba@kernel.org,pabeni@redhat.com,ast@kernel.org,andrii@kernel.org,eddyz87@gmail.com,memxor@gmail.com,martin.lau@linux.dev,song@kernel.org,yonghong.song@linux.dev,jolsa@kernel.org,emil@etsalapatis.com,ihor.solodrai@linux.dev,john.fastabend@gmail.com,sdf@fomichev.me,horms@kernel.org,kuniyu@google.com,willemb@google.com,jhs@mojatatu.com,jiri@resnulli.us,sd@queasysnail.net,paul@isovalent.com,alasdair.mcwilliam@isovalent.com,netdev@vger.kernel.org,ast@kernel.org,andrii@kernel.org,daniel@iogearbox.net,martin.lau@kernel.org,eddyz87@gmail.com,yonghong.song@linux.dev,mason@kernel.org,ihor.solodrai@linux.dev