🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #24392

[PATCH bpf v3 1/2] bpf, lsm: fix bpf_setsockopt() on a half set up child in inet_csk_clone

#24392
From: Jiayuan Chen
Date: Mon, 30 Nov 2026 04:11
146 lines
14487 bytes
security_inet_csk_clone() is called at the end of inet_csk_clone_lock().
The child is locked, which is why the hook is in
bpf_lsm_locked_sockopt_hooks, but it is not set up yet. So
bpf_setsockopt() from a lsm_cgroup prog is wrong there for:

 - SO_KEEPALIVE, TCP_KEEPIDLE: arm the keepalive timer before
   tcp_init_xmit_timers() sets it up.
 - IPV6_TCLASS: writes to the ipv6_pinfo of the listener.
 - TCP_CONGESTION: puts icsk->icsk_ca_ops, which the child holds no
   reference on.

For example, SO_KEEPALIVE ends up with:

  Oops: general protection fault, probably for non-canonical address ...
  KASAN: maybe wild-memory-access in range [0xdead000000000128-...]
  RIP: 0010:__run_timers+0x5d2/0xab0
  Call Trace:
   <IRQ>
   run_timer_base+0x107/0x190
   run_timer_softirq+0x1a/0x40
   handle_softirqs+0x251/0xa70
   __irq_exit_rcu+0x1a8/0x260
   irq_exit_rcu+0xe/0x30
   sysvec_apic_timer_interrupt+0xc2/0x110
   </IRQ>

Options considered:
 1. Remove the hook from bpf_lsm_locked_sockopt_hooks, as commit
    5416c9aea832 ("bpf: Fix the kernel crash caused by bpf_setsockopt().")
    did for socket_sock_rcv_skb. Not done, setting options like
    SO_PRIORITY on the child is what the hook is used for and it works.
 2. Reject these options in the hook. Not done, it is a deny list
    that has to follow every change of the child set up.
 3. Call the hook after the child is set up.

Go with 3, as V1 suggested. Call the hook right before
__inet_inherit_port() in tcp_v4_syn_recv_sock() and
tcp_v6_syn_recv_sock(), the two callers of tcp_create_openreq_child().
The child is still locked and not in bhash and ehash yet. Options
like SO_MARK and IP_TOS now drop the dst the child just got,
tcp_init_transfer() looks it up again.

MPTCP is not covered by the move. A passive subflow inherits
sk_write_space = subflow_write_space() from the listener, but its
subflow->conn is only set in subflow_syn_recv_sock() after the child
is in ehash, too late for the hook. SO_SNDBUF and TCP_NOTSENT_LOWAT
call sk->sk_write_space(), and subflow_write_space() derefs the NULL
conn. There is no msk to wake up yet, so return early there.

A bpf CC hits the same NULL without the lsm hook, set_state() is
called from tcp_ca_openreq_child():

  Oops: general protection fault, probably for non-canonical address ...
  KASAN: null-ptr-deref in range [0x00000000000008c0-0x00000000000008c7]
  RIP: 0010:subflow_write_space+0x142/0xb80
  Call Trace:
   <IRQ>
   do_tcp_setsockopt+0x18e0/0x2500
   sol_tcp_sockopt+0x415/0xaa0
   __bpf_setsockopt+0xd8/0x180
   _bpf_setsockopt+0x77/0x130
   bpf_sk_setsockopt_nodelay+0x41/0x60
   bpf_prog_47b652bba9e6db3d_repro_set_state+0x40/0x46
   bpf__tcp_congestion_ops_set_state+0x4c/0xac
   tcp_set_ca_state+0xaf/0x250
   tcp_ca_openreq_child+0x126/0x460
   tcp_v4_syn_recv_sock+0x5b7/0x1240
   subflow_syn_recv_sock+0x2ae/0x1570
   tcp_check_req+0x531/0x2a30
   tcp_v4_rcv+0x142d/0x3e00
   ...
   </IRQ>

Fixes: 9113d7e48e91 ("bpf: expose bpf_{g,s}etsockopt to lsm cgroup")
Fixes: eb18b49ea758 ("bpf: tcp: Allow bpf-tcp-cc to call bpf_(get|set)sockopt") # MPTCP NULL deref
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
v1 -> v2: Both Stanislav and Alexei asked whether we can keep those options supported by moving
          the hook rather than disabled them.
v1 : https://lore.kernel.org/bpf/20261125070330.16625-1-jiayuan.chen@linux.dev/
---
 net/ipv4/inet_connection_sock.c | 2 --
 net/ipv4/tcp_ipv4.c             | 3 +++
 net/ipv6/tcp_ipv6.c             | 3 +++
 net/mptcp/subflow.c             | 4 ++++
 4 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
index 9a3b05edb62b..759f8267f805 100644
--- a/net/ipv4/inet_connection_sock.c
+++ b/net/ipv4/inet_connection_sock.c
@@ -1276,8 +1276,6 @@ struct sock *inet_csk_clone_lock(const struct sock *sk,
 
 	inet_clone_ulp(req, newsk, priority);
 
-	security_inet_csk_clone(newsk, req);
-
 	return newsk;
 }
 
diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
index 2fc92a7884fc..d03b50ba41f5 100644
--- a/net/ipv4/tcp_ipv4.c
+++ b/net/ipv4/tcp_ipv4.c
@@ -1761,6 +1761,9 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
 		goto put_and_exit; /* OOM, release back memory */
 #endif
 
+	/* The child is set up, but is not in bhash and ehash yet. */
+	security_inet_csk_clone(newsk, req);
+
 	if (__inet_inherit_port(sk, newsk) < 0)
 		goto put_and_exit;
 	*own_req = inet_ehash_nolisten(newsk, req_to_sk(req_unhash),
diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c
index 5c7ed38f1f56..d704d9820b83 100644
--- a/net/ipv6/tcp_ipv6.c
+++ b/net/ipv6/tcp_ipv6.c
@@ -1516,6 +1516,9 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff *
 		goto put_and_exit; /* OOM */
 #endif
 
+	/* The child is set up, but is not in bhash and ehash yet. */
+	security_inet_csk_clone(newsk, req);
+
 	if (__inet_inherit_port(sk, newsk) < 0)
 		goto put_and_exit;
 	*own_req = inet_ehash_nolisten(newsk, req_to_sk(req_unhash),
diff --git a/net/mptcp/subflow.c b/net/mptcp/subflow.c
index 00677fb3efcd..a0fd3ce812c8 100644
--- a/net/mptcp/subflow.c
+++ b/net/mptcp/subflow.c
@@ -1602,6 +1602,10 @@ static void subflow_write_space(struct sock *ssk)
 {
 	struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
 
+	/* bpf_setsockopt() on a passive subflow that has no msk yet */
+	if (!sk)
+		return;
+
 	mptcp_propagate_sndbuf(sk, ssk);
 	mptcp_write_space(sk);
 }
-- 
2.43.0
Technical Details
Message-ID: <20261130041156.295524-1-jiayuan.chen@linux.dev>
Path: mbox-mo1.pugleaf.net!news-wdc.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-373865-linux-kernel-netdev=dewy.ru

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-wdc.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-373865-linux-kernel-netdev=dewy.ru
From: jiayuan.chen@linux.dev (Jiayuan Chen)
Newsgroups: linux.kernel.netdev
Subject: [PATCH bpf v3 1/2] bpf, lsm: fix bpf_setsockopt() on a half set up child in inet_csk_clone
Date: Mon, 30 Nov 2026 12:11:42 +0800
Organization: Usenet Network
Message-ID: <20261130041156.295524-1-jiayuan.chen@linux.dev>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="1329936"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:zurJ6CwO3KuYSNPUfOMdHPm6onk= sha256:gLH4b/NcCkdFcdxw0fADLntn1d99HkEwOdWAqVnwc5U=
	sha1:t7mpGxL7csab2Xg4WVBvNgKox4w= sha256:Asdh8ZiLkNiJj8sYrMY+MR1iI6vLVs/udO89h65XT8Y=
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 AF679125FC1; Mon, 30 Nov 2026 07:12:35 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -2.151
X-Spam-Status: No, score=-2.151 tagged_above=-9999 required=6.31
	tests=[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 B9_XP_2pCosG;
	Mon, 30 Nov 2026 07:12:31 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-373865-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 8B6F1125F93; Mon, 30 Nov 2026 07:12:21 +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 BE2FB23C5A; Mon, 30 Nov 2026 04:12:18 +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=1796011936;
	b=xtRK+oy6ia1yrVias9/v3aV8ByyRx3q5oOwibyZNP9vLDEgJpnufzbvOaywaKTmFQTKO
	 LbaaXN54JlZAJKNLe1licKCd1czF/3Fy2pXJqPqF4bmbxD2JyI+iL8hr+qZaj/FkhMEkd
	 D/QxDiuigTKVXdBpFIDewM3Vira8M+oNfR18GckNN2yLHF3XpK6A4y6znmn7HhlgBsXjy
	 t5SFQIgiATrUtLh/kGI8meeQTZzJjkjbiigzyCfYE2nyjyOxdfJESun/D/zJJaWe8+row
	 D9odutRkVXJ6u4Uile3OKSOzJIRSE9DHitbZwBIjkGlIl9labKqzHbOeqGtRxs7c4+g==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796011936;
	h=X-Original-To:Received:DMARC-Filter:X-Envelope-To:DKIM-Signature:
	 X-Envelope-To:Received:X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:
	 Subject:Date:Message-ID:X-Mailer:X-Mailing-List:List-Id:List-Post:
	 List-Help:List-Subscribe:List-Unsubscribe:List-Owner:Precedence:
	 MIME-Version:Content-Transfer-Encoding;
	bh=gL2eji2Fpv/7VKV8c7Oit8JtTRYnjzctzB+TmLzrC74=;
	b=BVwy+6FdgBC2x/gtc2KxhCJSNiYtrq826tbrV9ejIgx/itlpZ+vgT2sF70PxW6XqVE10
	 mF5HfmbDwztZ8MHwx+It0lSuXoncOm6sVwuverYLlqj7teP0GRY50zRzsOCWdvRzlxVXt
	 V3AxwYLJBAS8LpmiLcEJIaYKIB+nXNMCgfMdIHjU2uqZsUMpUkw4vW7ab6iysONiKUcUI
	 jhlmZMKSjlJkNbbmzaAQGmPXnvc48Hehw20DMZWo71ynW/NG8LG2GX4IBCeo2xqy2Xl+m
	 S4gXJO1wvbs3vfICDlnAmBfCnFxMoRfpk/QZT46MVLbztH1qZmgTJeOI+947W0BHpug==
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=EZGWd15Y;
	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 0E1CB606A32F;
	Mon, 30 Nov 2026 04:12:16 +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=EZGWd15Y
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.14
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1796011935;
	b=rJY+qOUXz55ofEiMc0uVt9duvNvbIYhGAlXzfrxhIxScAP0o3kKBr5ahas81ZaEs7023
	 5mJH6R4j9MSZKQlIZ28QBFRNTpb0iHR357GyP9L0jpkhw6BXxOMAJcYaI680bo4PPMcrM
	 o742EHYXhzpso2NX9gp0Y8+qNbBFXZosdKcsfd5Q3i65tLRc4Kq3wUmRSVFjMDLAy9XoZ
	 RIAtOz04JmO+tkg9RVF53acV5BzEfDdoJN0gHt1j0KsH/drh0QQfhbO04pfzmJdpEyOGr
	 YhlwAzgWWJi9D3l4ETSZeDcMv4TXHH+VWq31Q+v5G2Ey40kNJYeUgd1lOixAmAsdA4g==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796011935;
	h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received:
	 X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID:
	 X-Mailer:MIME-Version:Content-Transfer-Encoding;
	bh=gL2eji2Fpv/7VKV8c7Oit8JtTRYnjzctzB+TmLzrC74=;
	b=n9IbxkYRBMc7Edtz46vWhCNdQ5JFEthjtd/zZBaPv3jaOAcTX/+4D8Tlei101d4l49yN
	 17KT8p3BBVghzVGvAunSnvgOHRfsUwgpBNZZpCdZS1erGZ4RXoCf8uoM6RUGVKsKqwU8v
	 LnUqH47onyyGPlLNyot6mY4KP5b/Ec6W9CAGHBus1l1hFFqHrw92weDL0M1xFdTQNHRzC
	 VK71fhELTdz1wN0AacXYGLdbuX2pwgm5cXaFG27OvDfaaiXQpEpAP3l8nvU9spCBGSHoV
	 YqJTjUsAm5ba1wNTkBvn8OS4zLxlE40EazePCrgbZa6oS8k0/5bJ0r467oj7B1opKkQ==
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=EZGWd15Y;
	arc=none smtp.remote-ip=95.215.58.14
X-Received: from mta1.migadu.com (out-14.mta1.migadu.com [95.215.58.14])
	(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 77E7A606A325
	for <netdev@vger.kernel.org>; Mon, 30 Nov 2026 04:12:14 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 77E7A606A325
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=MiYuhYt6sA/ttZV3otbGuB2Bl6EU5Vy+SMhznHH22Xk=;
 c=simple/simple; d=linux.dev;
 h=from:to:subject:date:message-id:mime-version:content-type; s=key1;
 t=1796011932; v=1; x=1796616732;
 b=EZGWd15YGS7opYLg7bODSEohtZkorio5Hgh7cApH6pKtT6FqZoI9gL4DfyPDO8JiMDfTWGm/
 6YGQI9EYS3QIuLgRUdGTmVVCO1cFjwTAzJpoEpDM1Y/HfvAYEa2o6riOq/wh73fap7TW9ycFOyH
 RTK6M8iJ1LFzQd3/EjRAiy+w=
X-Envelope-To: netdev@vger.kernel.org
X-Received: by smtp.migadu.com with ESMTPS id 62f28bbe57346f07;
	Mon, 30 Nov 2026 04:12:11 +0000
X-Mizu-Trace-ID: 62f28bbe57346f07
X-Migadu-Flow: FLOW_OUT
X-Cc: sdf.kernel@gmail.com,
	Jiayuan Chen <jiayuan.chen@linux.dev>,
	Eric Dumazet <edumazet@kernel.org>,
	Neal Cardwell <ncardwell@google.com>,
	Kuniyuki Iwashima <kuniyu@google.com>,
	"David S. Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>,
	Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	Matthieu Baerts <matttbe@kernel.org>,
	Mat Martineau <martineau@kernel.org>,
	Geliang Tang <geliang@kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Ihor Solodrai <ihor.solodrai@linux.dev>,
	Shuah Khan <shuah@kernel.org>,
	Xu Kuohai <xukuohai@huawei.com>,
	Stanislav Fomichev <sdf@fomichev.me>,
	netdev@vger.kernel.org,
	mptcp@lists.linux.dev,
	linux-kselftest@vger.kernel.org
X-Mailer: git-send-email 2.43.0
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