Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.180.54 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796213174; b=JwgcS2F/wRKJT5bbQT+k34n8ERjfSLtU3/zIvjWvExOkukkDZ3Lml5Vj9Ys7Y02hiys0 MMkDOxD0tPe9JkQf/4qV4VbeqGaZKepemgR0PWjf1DB5ia8QUS9AxFY1RprN8EE5BEN5+ UckIb97yO3udmtuxuNFg164SxIoXe5YC2vraoNKN9mfkLGJc0vMEJeTxmsWhCpPU4PGlb pd68ZwExSv9ciNz/xzx3unmQvaEkf9u5TxZqkn4Oxnsf9WOvoscwm0Z3Q3ek7k+/ZIX/e ZPz2RuCRYxrEn/VtHLA3/CbBGH2mNBCRK0Xu0NywEr7T9thaKfXVNwQwnJT+A6cHIkQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796213174; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding: X-ZohoMailClient; bh=LRO+yWZaK1u203UsRTTVvK+cYZFMIKOhGWH6mqZXEh4=; b=2kZdTAoi/PO733s8Zo/feYKnf13mArZZ751ZA4DvflRGu+4ZsmByZ/ZUooUjABi7i81G idBUjCJ7mW8Mlq9L7M8/OEoSMWfGrqKrMHziwiC0eBsrjl+r1a7EMCa2XHmxLh2PfktfM yPvl61evq4X4PXWMf16ovMZhIOwaxLmP2pupFoZQtVh2PRjC8f7OHbUVnxfl9+P67oMbI QKJdjkHqIUYEqBjAFgUwdmHu/jfRHxJcF7bDNS0S5A1aMBujilcbX2bVSFNkWUv5aSUZx rmEI6E5MFis1LFLMrxQ2TAOJaN6r/H9Xr32FtdMjRKIWRR/XCKi0QtidmgXefwMPjPQ== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=mpiricsoftware.com; spf=pass smtp.mailfrom=mpiricsoftware.com; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.180.54 Received: from sender6-of-o54.zoho.com (sender6-of-o54.zoho.com [165.173.180.54]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BF029606A698 for ; Wed, 2 Dec 2026 12:06:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BF029606A698 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mpiricsoftware.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mpiricsoftware.com ARC-Seal: i=1; a=rsa-sha256; t=1796213168; cv=none; d=zohomail.com; s=zohoarc; b=MKZwPn54QM8/7th24qTLUid/PsoRv5G4uN/xliJjghKfqzxHTpQimbmsvav8bJ8O2cGKv92IuGbXpg61A8KFX9AsnuwZFFEQa0/vQ29sgs6m7cDK732RS9TYJiFEBwwmPBoGnV+ZvqA2d5zRIZRFM457AuEEF1Dk1Ju0FaueNCg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1796213168; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=LRO+yWZaK1u203UsRTTVvK+cYZFMIKOhGWH6mqZXEh4=; b=oHIAt2+C8dIOZfZKo/lF5rLQDKpWJqF5wwXHBFzURhsH9s2T/DCew2zk7UXtI0aj6imKgYgBnM3G03ADdZFaqxbqw3q+w42dZLOR7swiChphZzUPXM64Rwv5jmMs7PPY+hPo8B+KVKVWYex65ulY3Sy94L26RDbjr+3ji9GK3p0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=mpiricsoftware.com; spf=pass smtp.mailfrom=kalpan.jani@mpiricsoftware.com; dmarc=pass header.from= DKIM-Signature: a=rsa-sha256; b=U09fSBJk50NMvqzgFDmo6pS68n+3b9gsqTpkEki1i3pEyS0G6qHfV01iBk/bQy8AvY1ZrGdDddt8bqj7ShDfxmdn2j+XM5Y05EU3fEoMkl0JbX1KeSYNqGMY4S2U8G2dCLW64YJRwtwiQlG4cQ7ywB339LDnwuhKb/siA+u0pgs=; c=relaxed/relaxed; s=mpiric; d=mpiricsoftware.com; v=1; bh=LRO+yWZaK1u203UsRTTVvK+cYZFMIKOhGWH6mqZXEh4=; h=Content-Description:Content-ID:Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Owner:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Resent-Sender:Resent-Cc:Resent-Date:Resent-To:Reply-To:Resent-Message-ID:Resent-From:Sender:Subject:To:Date:From:Message-Id:Reply-To:Subject:To:Cc; Received: by smtp.zohomail.com with SMTPS id 1796213164096423.25064407628315; Wed, 2 Dec 2026 04:06:04 -0800 (PST) From: Kalpan Jani To: mptcp@lists.linux.dev Cc: shardul.b@mpiricsoftware.com, janak@mpiric.us, kalpanjani009@gmail.com, Kalpan Jani Subject: [PATCH mptcp-next v8] mptcp: bpf: fix NULL deref and UAF in bpf_mptcp_sock_from_subflow() Date: Wed, 2 Dec 2026 17:35:52 +0530 Message-ID: <20261202120552.1973095-1-kalpan.jani@mpiricsoftware.com> X-Mailer: git-send-email 2.43.0 X-Mailing-List: mptcp@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-ZohoMailClient: External bpf_mptcp_sock_from_subflow() is reachable from BPF programs via bpf_skc_to_mptcp_sock() on an arbitrary socket, without the subflow socket lock held. It assumes sk_is_mptcp(sk) implies a valid subflow context whose ->conn points to a live parent mptcp_sock. That invariant does not hold in three windows: - Fallback: subflow_ulp_fallback() clears icsk_ulp_data via rcu_assign_pointer(..., NULL) before clearing tcp_sk(sk)->is_mptcp, so a concurrent reader can observe is_mptcp == 1 with a NULL context. - Init: subflow_ulp_init() sets is_mptcp = 1 while ->conn is still NULL; ->conn is only assigned later in subflow_syn_recv_sock() or mptcp_subflow_create_socket(). On CONFIG_DEBUG_NET, mptcp_sk() dereferences its argument in a WARN_ON(), so mptcp_sk(NULL) faults. - Teardown: lockless readers can access the subflow context and parent socket while teardown is in progress. Dropping the subflow-owned parent reference before an RCU grace period can leave readers with an invalid parent socket. Load the context with rcu_dereference_check() and the parent socket with smp_load_acquire(). Publish ->conn with smp_store_release() in the MP_CAPABLE, MP_JOIN and outgoing subflow paths. Defer releasing the subflow-owned parent socket reference until the subflow context has passed an RCU grace period. Retire contexts through a shared RCU callback, including contexts released through delegated NAPI processing. Keep the parent reference while an orphaned, unaccepted subflow context is retained for later cleanup. Give the parent msk RCU-grace lifetime by setting SOCK_RCU_FREE in the shared __mptcp_init_sock(), covering both active and passive paths, and dropping the reset in mptcp_sk_clone_init(). This ensures a reader that obtains the parent under the applicable RCU protection does not access freed memory. Keep bpf_skc_to_mptcp_sock() unavailable to sleepable tracing programs, which cannot rely on the required RCU protection. Fixes: 3bc253c2e652 ("bpf: Add bpf_skc_to_mptcp_sock_proto") Closes: https://github.com/multipath-tcp/mptcp_net-next/issues/622 Signed-off-by: Kalpan Jani --- Changes since v7: - Remove the generic needs_rcu metadata from struct bpf_func_proto and the associated verifier changes. - Extend rcu_dereference_check() to allow access when either the subflow socket lock or RCU read-side protection is held. - Defer releasing the subflow-owned parent socket reference until after an RCU grace period. - Introduce a shared RCU callback to free subflow contexts and release parent socket references, including delegated NAPI cleanup. - Retain the parent socket reference while an orphaned, unaccepted subflow context is kept for later cleanup. - Remove SOCK_RCU_FREE from subflow TCP sockets while retaining it on the parent MPTCP socket. - Clear icsk_ulp_data and the MPTCP subflow flag when retiring the context. - Restrict bpf_skc_to_mptcp_sock() from sleepable tracing programs. - Update the subflow context comment to document RCU access by BPF readers. v7: https://lore.kernel.org/all/20261123084836.1704274-1-kalpan.jani@mpiricsoftware.com/ --- kernel/trace/bpf_trace.c | 2 +- net/mptcp/bpf.c | 16 ++++++++++++++-- net/mptcp/protocol.c | 8 ++++++-- net/mptcp/protocol.h | 15 ++++++++++++--- net/mptcp/subflow.c | 38 +++++++++++++++++++++++++++----------- 5 files changed, 60 insertions(+), 19 deletions(-) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 1f35a18ef399..eedc0371548c 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1765,7 +1765,7 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) case BPF_FUNC_skc_to_unix_sock: return &bpf_skc_to_unix_sock_proto; case BPF_FUNC_skc_to_mptcp_sock: - return &bpf_skc_to_mptcp_sock_proto; + return prog->sleepable ? NULL : &bpf_skc_to_mptcp_sock_proto; case BPF_FUNC_sk_storage_get: return &bpf_sk_storage_get_tracing_proto; case BPF_FUNC_sk_storage_delete: diff --git a/net/mptcp/bpf.c b/net/mptcp/bpf.c index 74648ca1e99b..fa05394a8480 100644 --- a/net/mptcp/bpf.c +++ b/net/mptcp/bpf.c @@ -193,8 +193,20 @@ static struct bpf_struct_ops bpf_mptcp_sched_ops = { struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk) { - if (sk && sk_fullsock(sk) && sk_is_tcp(sk) && sk_is_mptcp(sk)) - return mptcp_sk(mptcp_subflow_ctx(sk)->conn); + struct mptcp_subflow_context *ctx; + struct sock *conn; + + if (sk && sk_fullsock(sk) && sk_is_tcp(sk) && sk_is_mptcp(sk)) { + ctx = rcu_dereference_check(inet_csk(sk)->icsk_ulp_data, + lockdep_sock_is_held(sk) || + rcu_read_lock_any_held()); + if (ctx) { + /* Pair with the release stores publishing ctx->conn. */ + conn = smp_load_acquire(&ctx->conn); + if (conn) + return mptcp_sk(conn); + } + } return NULL; } diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c index b9c6e5a615c8..b4da188c52d5 100644 --- a/net/mptcp/protocol.c +++ b/net/mptcp/protocol.c @@ -3472,6 +3472,11 @@ static void __mptcp_init_sock(struct sock *sk) mptcp_pm_data_init(msk); spin_lock_init(&msk->fallback_lock); + /* the returned parent msk may be handed to lockless readers + * (bpf_skc_to_mptcp_sock()); free it after an RCU grace period + */ + sock_set_flag(sk, SOCK_RCU_FREE); + /* re-use the csk retrans timer for MPTCP-level retrans */ timer_setup(&sk->mptcp_retransmit_timer, mptcp_retransmit_timer, 0); timer_setup(&msk->sk.mptcp_tout_timer, mptcp_tout_timer, 0); @@ -4054,7 +4059,6 @@ struct sock *mptcp_sk_clone_init(const struct sock *sk, /* passive msk is created after the first/MPC subflow */ msk->subflow_id = 2; - sock_reset_flag(nsk, SOCK_RCU_FREE); security_inet_csk_clone(nsk, req); /* this can't race with mptcp_close(), as the msk is @@ -5112,7 +5116,7 @@ static int mptcp_napi_poll(struct napi_struct *napi, int budget) sock_put(ssk); if (status & BIT(MPTCP_DELEGATE_DEAD)) - kfree_rcu(subflow, rcu); + call_rcu(&subflow->rcu, mptcp_subflow_free_ctx_rcu); if (++work_done == budget) return budget; diff --git a/net/mptcp/protocol.h b/net/mptcp/protocol.h index 7cdd0a7e8296..5d93ea6e8898 100644 --- a/net/mptcp/protocol.h +++ b/net/mptcp/protocol.h @@ -667,7 +667,7 @@ mptcp_subflow_ctx(const struct sock *sk) { const struct inet_connection_sock *icsk = inet_csk(sk); - /* Use RCU on icsk_ulp_data only for sock diag code */ + /* Lockless readers, such as BPF and sock diag, use RCU. */ return (__force struct mptcp_subflow_context *)icsk->icsk_ulp_data; } @@ -822,14 +822,23 @@ mptcp_subflow_delegated_next(struct mptcp_delegated_action *delegated) return ret; } +void mptcp_subflow_free_ctx_rcu(struct rcu_head *head); + static inline void mptcp_subflow_free_ctx(struct mptcp_subflow_context *subflow) { + struct sock *ssk = subflow->tcp_sock; + struct inet_connection_sock *icsk = inet_csk(ssk); long old = set_mask_bits(&subflow->delegated_status, 0, BIT(MPTCP_DELEGATE_DEAD)); - /* a scheduled subflow is owned by mptcp_napi_poll(), which will free it */ + if (rcu_access_pointer(icsk->icsk_ulp_data) == subflow) { + rcu_assign_pointer(icsk->icsk_ulp_data, NULL); + WRITE_ONCE(tcp_sk(ssk)->is_mptcp, 0); + } + + /* A scheduled subflow is retired by mptcp_napi_poll(). */ if (!(old & BIT(MPTCP_DELEGATE_SCHEDULED))) - kfree_rcu(subflow, rcu); + call_rcu(&subflow->rcu, mptcp_subflow_free_ctx_rcu); } void __mptcp_inherit_memcg(struct sock *sk, struct sock *ssk, gfp_t gfp); diff --git a/net/mptcp/subflow.c b/net/mptcp/subflow.c index 85695fe00cc3..2402fc87fd09 100644 --- a/net/mptcp/subflow.c +++ b/net/mptcp/subflow.c @@ -829,6 +829,18 @@ static void subflow_ulp_fallback(struct sock *sk, mptcp_subflow_ops_undo_override(sk); } +void mptcp_subflow_free_ctx_rcu(struct rcu_head *head) +{ + struct mptcp_subflow_context *ctx = + container_of(head, struct mptcp_subflow_context, rcu); + struct sock *conn = READ_ONCE(ctx->conn); + + if (conn) + sock_put(conn); + + kfree(ctx); +} + void mptcp_subflow_drop_ctx(struct sock *ssk) { struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(ssk); @@ -836,12 +848,9 @@ void mptcp_subflow_drop_ctx(struct sock *ssk) if (!ctx) return; - list_del(&mptcp_subflow_ctx(ssk)->node); - if (inet_csk(ssk)->icsk_ulp_ops) { + list_del(&ctx->node); + if (inet_csk(ssk)->icsk_ulp_ops) subflow_ulp_fallback(ssk, ctx); - if (ctx->conn) - sock_put(ctx->conn); - } mptcp_subflow_free_ctx(ctx); } @@ -936,6 +945,7 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk, if (child && *own_req) { struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child); + struct sock *conn; tcp_rsk(req)->drop_req = false; @@ -966,14 +976,16 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk, ctx->setsockopt_seq = listener->setsockopt_seq; if (ctx->mp_capable) { - ctx->conn = mptcp_sk_clone_init(listener->conn, &mp_opt, child, req); - if (!ctx->conn) { + conn = mptcp_sk_clone_init(listener->conn, &mp_opt, child, req); + if (!conn) { fallback = true; goto fallback; } + /* Publish the initialized parent socket to lockless readers. */ + smp_store_release(&ctx->conn, conn); ctx->subflow_id = 1; - owner = mptcp_sk(ctx->conn); + owner = mptcp_sk(conn); if (mp_opt.deny_join_id0) WRITE_ONCE(owner->pm.remote_deny_join_id0, true); @@ -1008,7 +1020,8 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk, /* move the msk reference ownership to the subflow */ WRITE_ONCE(subflow_req->msk, NULL); - ctx->conn = (struct sock *)owner; + /* Publish the parent socket to lockless readers. */ + smp_store_release(&ctx->conn, (struct sock *)owner); if (subflow_use_different_sport(owner, sk)) { pr_debug("ack inet_sport=%d %d\n", @@ -1936,7 +1949,8 @@ int mptcp_subflow_create_socket(struct sock *sk, unsigned short family, *new_sock = sf; sock_hold(sk); - subflow->conn = sk; + /* Publish the parent socket after taking its reference. */ + smp_store_release(&subflow->conn, sk); mptcp_subflow_ops_override(sf->sk); return 0; @@ -1957,6 +1971,7 @@ static struct mptcp_subflow_context *subflow_create_ctx(struct sock *sk, if (!ctx) return NULL; + /* Publish the context for lockless BPF readers. */ rcu_assign_pointer(icsk->icsk_ulp_data, ctx); INIT_LIST_HEAD(&ctx->node); INIT_LIST_HEAD(&ctx->delegated_node); @@ -2145,9 +2160,10 @@ static void subflow_ulp_release(struct sock *ssk) if (!release && !test_and_set_bit(MPTCP_WORK_CLOSE_SUBFLOW, &mptcp_sk(sk)->flags)) mptcp_schedule_work(sk); - sock_put(sk); + } + mptcp_subflow_tcp_fallback(ssk, ctx); mptcp_subflow_ops_undo_override(ssk); if (release) mptcp_subflow_free_ctx(ctx); -- 2.43.0