Article View: linux.kernel.netdev
Article #9617Re: [PATCH bpf v2 07/15] bpf, sockmap: Check the target of an egress redirect before each send
From: Jiayuan Chen
Date: Mon, 02 Nov 2026 12:32
Date: Mon, 02 Nov 2026 12:32
99 lines
12670 bytes
12670 bytes
On 11/2/26 3:21 PM, Alexei Starovoitov wrote:
> From: Chris Mason <mason@kernel.org>
>
> tcp_sendmsg_locked() expects a caller that has the socket open. It uses
> sk->sk_socket and the wait queue sk->sk_wq. For an egress redirect of an
> sk_msg prog the sender calls it on the target without a reference to the
> target's file. Both pointers are NULL before accept() and after close().
> close() can occur while the sender sleeps. Then the kernel crashes.
>
> Fail the redirect when the target has no struct socket, or when the sender
> has to wait for the connection. The error is EINVAL. After close() it is
> EPIPE, as today. sk_stream_wait_connect() calls remove_wait_queue() without
> a test of SOCK_DEAD. sk_stream_wait_memory() has this test, but it can
> still use sk_wq after the sleep. This patch does not change that. The
> target can change while the sender sleeps there. Thus do both checks before
> each call of tcp_sendmsg_locked(). Clear MSG_FASTOPEN, so that the sender
> does not call the connect code of the target, which uses both pointers.
>
> Today the sender can send before accept() and waits in SYN_SENT. send()
> with MSG_FASTOPEN sends the data. Today it fails with EISCONN. When the
> redirect of a cork fails, send() returns success and the kernel drops the
> data, as today.
>
> The Fixes tags are for the two checks and for MSG_FASTOPEN, in this order.
>
> Fixes: 4f738adba30a ("bpf: create tcp_bpf_ulp allowing BPF to monitor socket TX/RX data")
> Fixes: b8b8315e39ff ("bpf, sockmap: Remove unhash handler for BPF sockmap usage")
> Fixes: c5c37af6ecad ("tcp: Convert do_tcp_sendpages() to use MSG_SPLICE_PAGES")
> Signed-off-by: Chris Mason <mason@kernel.org>
> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
> ---
> net/ipv4/tcp_bpf.c | 24 +++++++++++++++++++++++-
> 1 file changed, 23 insertions(+), 1 deletion(-)
>
> diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c
> index fd443350f9b8..ef2e15de0fe3 100644
> --- a/net/ipv4/tcp_bpf.c
> +++ b/net/ipv4/tcp_bpf.c
> @@ -88,6 +88,24 @@ static int bpf_tcp_ingress(struct sock *sk, struct sk_psock *psock,
> return ret;
> }
>
> +/*
> + * The target of a redirect belongs to somebody else. A child that nobody has
> + * accepted yet, and an orphan, have no struct socket and no wait queue.
> + * tcp_sendmsg_locked() needs both. See sendmsg_locked() in skbuff.c.
> + * The sender holds no reference to the file of the target, which can be closed
> + * whenever tcp_sendmsg_locked() sleeps. sk_stream_wait_connect() and
> + * inet_wait_for_connect() go back to the wait queue when they wake up, so the
> + * sender must not wait for a connect.
> + */
> +static bool tcp_bpf_redir_allowed(const struct sock *sk)
> +{
> + if (!sk->sk_socket)
> + return false;
> + if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))
> + return tcp_passive_fastopen(sk);
> + return true;
> +}
> +
> static int tcp_bpf_push(struct sock *sk, struct sk_msg *msg, u32 apply_bytes,
> int flags, bool uncharge)
> {
> @@ -109,6 +127,10 @@ static int tcp_bpf_push(struct sock *sk, struct sk_msg *msg, u32 apply_bytes,
> page = sg_page(sge);
>
> retry:
> + /* Only a redirect sends data which is not charged to sk */
> + if (!uncharge && unlikely(!tcp_bpf_redir_allowed(sk)))
> + return sock_flag(sk, SOCK_DEAD) ? -EPIPE : -EINVAL;
> +
Reviewed-by: Jiayuan Chen <jiayuan.chen@linux.dev>
I can trigger it by redirecting data to a non-accept sk.
==================================================================
BUG: KASAN: null-ptr-deref in tcp_sendmsg_locked+0xfb8/0x4550
Write of size 8 at addr 0000000000000008 by task repro/488
Tainted: [W]=WARN
Call Trace:
<TASK>
dump_stack_lvl+0x91/0xf0
print_report+0x442/0x630
kasan_report+0xf3/0x130
kasan_check_range+0x11c/0x200
__kasan_check_write+0x14/0x30
tcp_sendmsg_locked+0xfb8/0x4550
tcp_bpf_push+0x3d0/0xbc0
tcp_bpf_sendmsg_redir+0x639/0x1050
tcp_bpf_sendmsg+0xa32/0x1b90
inet_sendmsg+0xdf/0x100
__sys_sendto+0x3ed/0x4f0
__x64_sys_sendto+0xe0/0x1b0
x64_sys_call+0x1375/0x27e0
do_syscall_64+0xe5/0x610
Technical Details
Message-ID:
<69e66c9c-f2ab-41a7-972d-d1562a6387b8@linux.dev>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-354661-linux-kernel-netdev=dewy.ruReferences:
<20261102072112.4160195-1-alexei.starovoitov@gmail.com> <20261102072112.4160195-8-alexei.starovoitov@gmail.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-354661-linux-kernel-netdev=dewy.ru From: jiayuan.chen@linux.dev (Jiayuan Chen) Newsgroups: linux.kernel.netdev Subject: Re: [PATCH bpf v2 07/15] bpf, sockmap: Check the target of an egress redirect before each send Date: Mon, 2 Nov 2026 20:32:24 +0800 Organization: Usenet Network Message-ID: <69e66c9c-f2ab-41a7-972d-d1562a6387b8@linux.dev> References: <20261102072112.4160195-1-alexei.starovoitov@gmail.com> <20261102072112.4160195-8-alexei.starovoitov@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="2920765"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:kL4nXpMio57pioulpSpoYJ0sof0= sha256:lXWCNul9rD37YHqZYcaedQ2my2dRaCgP6XDZPMS1/PU= sha1:RxsFvY/QnnlAwj2jl42+zYqNjOI= sha256:jK+Sx3G2WhycsMI255HQEuN/756KA5cJGTuwRjsK0uk= 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 B6F101124EF; Mon, 2 Nov 2026 15:35:25 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -2.051 X-Spam-Status: No, score=-2.051 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, HK_RANDOM_FROM=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 (1024-bit key) header.d=linux.dev 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 i_E9c1Ofxu57; Mon, 2 Nov 2026 15:35:17 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-354661-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=linux.dev X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114]) by mail.dewy.ru (Postfix) with ESMTPS id B70D41124D1; Mon, 2 Nov 2026 15:35:08 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by tor.lore.kernel.org (Postfix) with ESMTP id 00D512EB9C; Mon, 2 Nov 2026 12:32:46 +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=1793622758; b=2HYTg6bZBkzvdvHcmsYkbFKk/xwFhZcqkzxgQSgkFL7qhQ5DxuApZ5kFQAXV4IcgtJ6G Rmd9PNqnHoCtXtU+WE0SA74JCZLNsLhlGzCmt8wc7SE6Y4WID5A/jPOm9Y7MjO9uB6wgl PMa/1l9AGMwQfHozl3PqdvhBxreGq1k3w5UpjLCwDfvADInDDJVIQnM6p/np8JJTjgzKR vH3EiE6UWN7xnHkkxESbWu0JWJZ1+q43/0VFvBkE2s8NSSg9VrI8wVozazGfqfk9KtzL5 wEftd/tleH2GLZFG7z7p16qxVFvYoulQVTW9iuJCw27hFCJgXn4cwehrMzoaYqW2JSg== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793622758; h=X-Original-To:Received:DMARC-Filter:X-Envelope-To:DKIM-Signature: X-Envelope-To:Received:X-Mizu-Trace-ID:X-Migadu-Flow:Message-ID:Date: X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe: List-Unsubscribe:List-Owner:Precedence:MIME-Version:User-Agent: Subject:To:Cc:References:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=w6jqiUBD5ibgJFs0ugALEEDB7b768frs2A7MQ2xTHQM=; b=nsLFSJIe2UB0Ov3J6w8acC3trAhLdvj/3+kr2bKaTrcxEnVN1YYSIUop+h44u64lqCfr rRqYC0A9ePpJM7s1XsIAalgIfGjO58tR0ILoVFj2BMaSjHLgmxZqmO5XwFaSB5sOTGq69 pKTi4JBq/mxYhbtXmdlBVdqox3TB7cj27RvsgZ/Kx7q15P7uUEPuWtOTh1QbDo1pVhK4n RA5uRXDFgEFXntIMUcS1IULh49pg+Xuf3ifdeI1zbm//Fpq2hTLchDsVtQEuB8+9hq7zx TmqiqpiAl1DrlO5Cfz64fE4c3tCW0yLhDINfqmZ60glHSVS+pAKQsnwAfVfS0xhU3YA== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=cL9ESvBY; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; 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 6B484606A33D; Mon, 2 Nov 2026 12:32:38 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=cL9ESvBY X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.152 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793622757; b=Oixmi1pxpENcY6boWOrIWfUGN18mmPSlumP/dfATS5/nSsm2yaNDxMyki9jKW7aRHAaH pioxkbeOFj515OrVe8vBawZAiH0p66zdosb0SA4+bTyC76bdLBtEnnsASnzkkEpSqujHJ KIFSclhrdEfpiFuGfwV+9p6I/IYn+v22+qstzdSBKmJiROq5COePhOpkiiCAGlj6wyvko jI4B0eQuncQpGLm9Ea5MANwRTDL1zZyQfJPc0+e6s5kplyUpELIa1eQurfnaeEr2aEmsX j3x1+jwuxmIbV0lIZkNpI2zQfIoLu06qxvnG1Rf3d7QGGGlNCOu/3KlpCFmGDPjt2Wg== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793622757; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=w6jqiUBD5ibgJFs0ugALEEDB7b768frs2A7MQ2xTHQM=; b=KNOTSnrcCwb4kvbaLYkL6fguRQ3FLiysMOmWdAkxvez023iXsLk94sIicDKZN1Ye/iPs d4Yf4JsebcawIPOqSESEgQPh54KlNC4sO4pwJITQOo1WvF71OXloQ8n+GFfvhlAYfoW5K hr7Hy9bhKbmJH4h4UJMlsA1GuD1gtgQzuDzQ/KjMToxH3mzH8Q5dhS1jdnLtsuJj9m5kL pCMPmt2cZiCc4Ri+Q45ZoEhb5+35D1N9mzQA9b4ogaUlQAU1POpEizb4YR8wRaWc720Pz 33CNEhpHTg+Bni8exKh3xOUceDlFzwMq8jdxOdGszvVpQNf1Zdgt5ao4XF+zoR8qh4A== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=cL9ESvBY; arc=none smtp.remote-ip=95.215.58.152 X-Received: from mta1.migadu.com (out-152.mta1.migadu.com [95.215.58.152]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7653F606A337 for <netdev@vger.kernel.org>; Mon, 2 Nov 2026 12:32:36 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7653F606A337 X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Envelope-To: netdev@vger.kernel.org X-DKIM-Signature: a=rsa-sha256; bh=SiiP3R6iGc96rRdu87RGjK9ajivgC01xWOp9SC3ZPUw=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1793622754; v=1; x=1794227554; b=cL9ESvBYAL63kpZebLoC7cMqDXiUTe+XViIZ37bDaSZ1Q0a0JYe1iNSmZibWd9qWMknh82ox +sJiP+jk4U4LQOo4MKjH4kFkR/e3J8KhK1a8fMaCfur/n4+t6U8TYvgWeqAbtbaXIgi/RG7n2Nn IgCgEQpNcYUnpO4gLchGeCIA= X-Envelope-To: netdev@vger.kernel.org X-Received: by smtp.migadu.com with ESMTPS id b39a93dc725c8c73; Mon, 02 Nov 2026 12:32:34 +0000 X-Mizu-Trace-ID: b39a93dc725c8c73 X-Migadu-Flow: FLOW_OUT 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-User-Agent: Mozilla Thunderbird X-Cc: daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com, memxor@gmail.com, john.fastabend@gmail.com, jakub@cloudflare.com, davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, kuniyu@google.com, willemb@google.com, matttbe@kernel.org, mptcp@lists.linux.dev, mason@kernel.org, josef@toxicpanda.com X-In-Reply-To: <20261102072112.4160195-8-alexei.starovoitov@gmail.com>