From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-189.mta1.migadu.com (out-189.mta1.migadu.com [95.215.58.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4ACBE1B983F for ; Fri, 17 Apr 2026 02:44:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.189 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776393861; cv=none; b=Ckoehz6NnvpOa4djDKlgZVjKiX18re4qNnVOVOFCcVYZ38zd2gBLKtLhYwgNNvU5H0Vhbym2+bh2/EclmTv5/+xMhA0j8FGHHR4jDscLaFLh6ZKpp8+iUJfbWcCtmaN04r5JXeGjFCf3FsFtSU+kr3JU6Hg8JuAiahBhW0qjB8g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776393861; c=relaxed/simple; bh=jzABcSaSqpM9aDL5CV4XBeoe9jOOv4UbXdZhfHL2GsU=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Pc+Up8UPuknJW8YmZreHBC0BjmlmdVZoEycTDQf1A+r9l4DmxBpmFWR84pWgMwQYq9C/2fqG90CByX+om4Uk6Ajp1OyQEMS/Ba6uhh3T4oJd43hyYGTttVxBTQirQC9J+4O4LRQfPMJEsNCI7yOW7IgWH3yeISJiR7GgOK+SJ7I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=FxJPuUx2; arc=none smtp.client-ip=95.215.58.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="FxJPuUx2" Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1776393857; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GhyqLmxrfpiB9Kg4xW6jUc4n1FOcmNDlAeTxwpau2IY=; b=FxJPuUx2aMLHRTnvt7uIlBLwZx1dW2wbeQSbTSxz+aicICgaowi+CYcFNEqX+KX0dIGxE/ IC/sm8Ai5sN/JL9Bw6NXB9l8+SSwUfYZ8JrXeS2xwXWMG4A9dHoyIKE1DljqwwjD8PQo1V f6/dXP96dK2wK9D3G3srrFGs+/6BpYU= Date: Fri, 17 Apr 2026 10:43:53 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [PATCH bpf v2 1/2] bpf: Reject TCP_NODELAY in TCP header option callbacks To: KaFai Wan , martin.lau@linux.dev, daniel@iogearbox.net, john.fastabend@gmail.com, sdf@fomichev.me, ast@kernel.org, andrii@kernel.org, eddyz87@gmail.com, memxor@gmail.com, song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, shuah@kernel.org, jiayuan.chen@linux.dev, bpf@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: Quan Sun <2022090917019@std.uestc.edu.cn>, Yinhao Hu , Kaiyan Mei References: <20260416112308.1820332-1-kafai.wan@linux.dev> <20260416112308.1820332-2-kafai.wan@linux.dev> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Jiayuan Chen In-Reply-To: <20260416112308.1820332-2-kafai.wan@linux.dev> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT On 4/16/26 7:23 PM, KaFai Wan wrote: > A BPF_SOCK_OPS program can enable > BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG and then call > bpf_setsockopt(TCP_NODELAY) from BPF_SOCK_OPS_HDR_OPT_LEN_CB or > BPF_SOCK_OPS_WRITE_HDR_OPT_CB. > > In these callbacks, bpf_setsockopt(TCP_NODELAY) can reach > __tcp_sock_set_nodelay(), which can call tcp_push_pending_frames(). > > From BPF_SOCK_OPS_HDR_OPT_LEN_CB, tcp_push_pending_frames() can call > tcp_current_mss(), which calls tcp_established_options() and re-enters > bpf_skops_hdr_opt_len(). > > BPF_SOCK_OPS_HDR_OPT_LEN_CB > -> bpf_setsockopt(TCP_NODELAY) > -> tcp_push_pending_frames() > -> tcp_current_mss() > -> tcp_established_options() > -> bpf_skops_hdr_opt_len() > -> BPF_SOCK_OPS_HDR_OPT_LEN_CB > > From BPF_SOCK_OPS_WRITE_HDR_OPT_CB, tcp_push_pending_frames() can call > tcp_write_xmit(), which calls tcp_transmit_skb(). That path recomputes > header option length through tcp_established_options() and > bpf_skops_hdr_opt_len() before re-entering bpf_skops_write_hdr_opt(). > > BPF_SOCK_OPS_WRITE_HDR_OPT_CB > -> bpf_setsockopt(TCP_NODELAY) > -> tcp_push_pending_frames() > -> tcp_write_xmit() > -> tcp_transmit_skb() > -> tcp_established_options() > -> bpf_skops_hdr_opt_len() > -> bpf_skops_write_hdr_opt() > -> BPF_SOCK_OPS_WRITE_HDR_OPT_CB > > This leads to unbounded recursion and can overflow the kernel stack. > > Reject TCP_NODELAY with -EOPNOTSUPP in bpf_sock_ops_setsockopt() > when bpf_setsockopt() is called from > BPF_SOCK_OPS_HDR_OPT_LEN_CB or BPF_SOCK_OPS_WRITE_HDR_OPT_CB. > > Reported-by: Quan Sun <2022090917019@std.uestc.edu.cn> > Reported-by: Yinhao Hu > Reported-by: Kaiyan Mei > Closes: https://lore.kernel.org/bpf/d1d523c9-6901-4454-a183-94462b8f3e4e@std.uestc.edu.cn/ > Fixes: 7e41df5dbba2 ("bpf: Add a few optnames to bpf_setsockopt") > Signed-off-by: KaFai Wan > --- > net/core/filter.c | 5 +++++ > 1 file changed, 5 insertions(+) > > diff --git a/net/core/filter.c b/net/core/filter.c > index fcfcb72663ca..911ff04bca5a 100644 > --- a/net/core/filter.c > +++ b/net/core/filter.c > @@ -5833,6 +5833,11 @@ BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock, > if (!is_locked_tcp_sock_ops(bpf_sock)) > return -EOPNOTSUPP; > > + if ((bpf_sock->op == BPF_SOCK_OPS_HDR_OPT_LEN_CB || > + bpf_sock->op == BPF_SOCK_OPS_WRITE_HDR_OPT_CB) && > + IS_ENABLED(CONFIG_INET) && level == SOL_TCP && optname == TCP_NODELAY) > + return -EOPNOTSUPP; > + > return _bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen); > } > A simple comment is recommended: /* TCP_NODELAY triggers tcp_push_pending_frames() and re-enters these callbacks. */ Also like Martin pointed before, BPF_SOCK_OPS_HDR_OPT_LEN_CB / BPF_SOCK_OPS_WRITE_HDR_OPT_CB can only be produced under CONFIG_INET so IS_ENABLED(CONFIG_INET) is dead.