From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.229.8 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796060737; b=BOB07DtJfxlJpfpIYZHXI7vFOSP9BdmW6dIfoW/woKt8zBhxt3gsxiYxhQEIlGq7QA5q F0WfuaFHOCt9cskpM3U7KA44MG1N/w+Df7iwREWoNn3plU7V7n758cBSH8EEafr4tALGT AWT+2xnmi4hmAlHTB+BPpQuhQCKx6eaKxerTJlna7FWQKOPDZAH6HHZ6u6cZ9YlXSqWRA eEBzTtYh0loP68GjMTx7ZJVb8yNVlNTtZsE4AYrsaowvuNVk1NL192702tPd5g8+zwVM5 bcGhyRjBDgGMbL7cbr6YoRiEkP1JvMpPMjRq3sk/8nmOj73ZNpm6rQjElxGLgIOtV7g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796060737; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; bh=SnrAQzmNWg3DKJmGX5Je9obNlNeZMkwQObGtmACpU6s=; b=UeDZ5ecR3Mte+E79lAtrS2rWOY75tZL9IY8UemXXrZknJaeSVUZPDLoQxroJmGwFspOt tFPh6hJy2983wtGXnIG3enZ/sSdETLl/BTNOApw1dNYCLk6G8Jab6pNM+gCIBJ3DkKOQR P34uzOHhspcsy2rtCAm+DAX3MySgOgEl7WoORGFPH1C+xIDLdwhuPseBrSyLXmemS/YlL R1IhVz29TYh1ZGwHjKIzKNo/EcDnsesYcze1ZbdSoaKiqCib5efV/eXXJcHsKdcH8UVwI HI0RinvhhJ6RfARjmsUzGG8m8tzSv8B0zgz/RMQF+VFa+kYicbbIm/e22SzDZW0fJmQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=EquImFHB; arc=none smtp.remote-ip=74.125.229.8 Received: from mail-dy2-f8.google.com (mail-dy2-f8.google.com [74.125.229.8]) (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 AD605606A325 for ; Mon, 30 Nov 2026 17:45:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AD605606A325 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=EquImFHB Received: by mail-dy2-f8.google.com with SMTP id 5a478bee46e88-3831d564bcfso3745763eec.1 for ; Mon, 30 Nov 2026 09:45:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796060736; x=1796665536; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=SnrAQzmNWg3DKJmGX5Je9obNlNeZMkwQObGtmACpU6s=; b=EquImFHBjAnhHIbW+gFtbLx5RsenQF3ynqS5bnvxOXMKeZ1MqqyGdxinIXfkIpfzf7 LIfpmMmSvEAE+QQGXBhGG3V6TZE5g8u1IJI+w0LRfIPjV1Nbg9sLhE0Y9PLdq3nrRSts h1g5NNirU4Rfzqo92D0QkILt9d/2KFJNRI+lslu1Xlugo55zgo2ZB8TgymGHwzq6G5Fv XrSSsobPR43Eja/amIqKODt4wagy7MLipEFta8SfpjyrAJ7YMan4LzwkvdNuHSZOVzKN waZlCl4eLfXwXeb9V5GaaYODnUOBOgx7fLE5FyMbdJV3iCB0XQ3+egtu5UJVZa6xECo/ Tdcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796060736; x=1796665536; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=SnrAQzmNWg3DKJmGX5Je9obNlNeZMkwQObGtmACpU6s=; b=cNQNhy5Myjo+WqZSdCGlOB+AHC9rUBOBbanXc91QIakDL1lxMzKc8pEBOR2/apmZQS 5+6w5yhn/sjRLTpCYqVFqwTqdSH6UIdyvsQZuRulCF+d4i61B94lxj/gQ4EbbQWTTIZs h1d2XmG2HvsW0onQWysOtMK44R1KOQywcJGkVfl+kcIVcSlgHffLn41Diu6Cs6WbsbDF cI8an4RR5aAmvU7jwNO6Dx5DXP2+XVF2BnUXVBDxSiHhqP1x+dDsWtUqv91aJeMvGddZ sDsRHe3MhLlBdI7QkeCX/oo1xTM6jg3C/WbBxRTYycBK69mq+pjM5FEOlLbbI6EgUjrD oFWQ== X-Forwarded-Encrypted: i=1; AL+hErH6z1BkbFNhqZvCDsd5HAUPSDNNwTH66BVPabTMJBRYVnKE9+MP+AqxOvzJTc0bjh6DFL2Vz/Q=@vger.kernel.org X-Gm-Message-State: APuU4Z+5PTK0c4B7escx57edk9w7PWPS0AHoZm6XCkXB8HAbUPSfQWVb iqTSjWEpmopuNwaVJHzaCktDC/rc+iomZdDpEQ3D4MYJ+3zrEJ15Djhc X-Gm-Gg: AZFdTZeXjb4+ii3idgVS7Dhb0VxZq94gLwkYIf3hM4s2VnY5jUAU7dKlsl7op99ZutZ N8nOSasHUDovS6HOnnich8JjzSIx8u3Noe//dBRjQwK3HLY0ITU5/xEQpHs2mHsnvEAgb/vgtWt 03oukeHM+Dtn8bnXIsuiZZHXJ7WzT/NZukhRjTm/r86k/qEFAbgTwPk/h167Crc5EbypNNvJEoR 3pz7mGkFBlXx6YWLOgkkUhfhdmOdhZANFG9IGU+MTrJSmYtpRS24QxxCNGuJ9dv9wx91DaXKeTA BDQPqzACt0ILdGvqUSGdC7yjQVIh55oYuJzb3UK6SXhzmPoPiewlcma3+GYlHtUmaowAtPwGvmn 7VvB7EOXwY86GlR3zDjGu3wp2V2DN2za+jMylGMoo/ZZblqDspLPesG4+Tirm3Wn2UAs/qLzF2c X95cEGJ6bhgQJ6wTeha7iszofVTT4tFo04G81Fey9L//6Ww2QxI5UeeECuEbKpEmwY6lPb0wNi0 HY7PFFgeIWlqHUuHSr1raTjmdk5gGFlH1wMLCgCtw5BWPOImIUXOWSl72tpKC5O+2nNKNZVlCny DiHpoXrr2JmyKfe4eoB9NeA= X-Received: by 2002:a05:7300:d509:b0:385:ac93:91f4 with SMTP id 5a478bee46e88-385ac939fc3mr42741471eec.26.1796060735329; Mon, 30 Nov 2026 09:45:35 -0800 (PST) Received: from localhost ([2a03:2880:9ff:42::]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-386992b404bsm85518022eec.1.2026.11.30.09.45.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 09:45:34 -0800 (PST) Date: Mon, 30 Nov 2026 09:45:23 -0800 From: Stanislav Fomichev To: Jiayuan Chen Cc: bpf@vger.kernel.org, Eric Dumazet , Neal Cardwell , Kuniyuki Iwashima , "David S. Miller" , Jakub Kicinski , Paolo Abeni , Simon Horman , Matthieu Baerts , Mat Martineau , Geliang Tang , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Shuah Khan , Xu Kuohai , Stanislav Fomichev , netdev@vger.kernel.org, mptcp@lists.linux.dev, linux-kselftest@vger.kernel.org Subject: Re: [PATCH bpf v3 1/2] bpf, lsm: fix bpf_setsockopt() on a half set up child in inet_csk_clone Message-ID: References: <20261130041156.295524-1-jiayuan.chen@linux.dev> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20261130041156.295524-1-jiayuan.chen@linux.dev> On 11/30, Jiayuan Chen wrote: > 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: > > 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 > > > 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: > > 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 > ... > > > 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 > --- > 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. */ Don't think the comments above are providing anything useful. > + 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 */ This one maybe reword to '/* bpf_setsockopt from security_inet_csk_clone */'? (unless MPTCP people have some ideas how to make it work properly)