Skip to contentSkip to conversation

tcp: fix potential race in tcp_v6_syn_recv_sock()

PATCH New Michael Pfeifroth 1 participant 1 message Sep 8

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tcp: fix potential race in tcp_v6_syn_recv_sock()

[ Upstream commit 858d2a4f67ff69e645a43487ef7ea7f28f06deae ]

Code in tcp_v6_syn_recv_sock() after the call to tcp_v4_syn_recv_sock() is done too late.

After tcp_v4_syn_recv_sock(), the child socket is already visible from TCP ehash table and other cpus might use it.

Since newinet->pinet6 is still pointing to the listener ipv6_pinfo bad things can happen as syzbot found.

Move the problematic code in tcp_v6_mapped_child_init() and call this new helper from tcp_v4_syn_recv_sock() before the ehash insertion.

This allows the removal of one tcp_sync_mss(), since tcp_v4_syn_recv_sock() will call it with the correct context.

Backport notes (6.12.y):
The mainline commit does not apply cleanly to 6.12 because ipv6_fl_list was later moved from struct ipv6_pinfo into struct inet_sock. On 6.12 ipv6_fl_list still lives inside ipv6_pinfo, so the memcpy of the parent ipv6_pinfo into the child covers it, and the new helper must therefore keep the ipv6_fl_list initialization in the post-memcpy block (as done here). No other logic changes.

CVE-2026-43198

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Reported-by [email protected]

[mpf: backport to 6.12.y; keep ipv6_fl_list initialization in the
post-memcpy block since ipv6_fl_list still lives in struct ipv6_pinfo on 6.12 (mainline moved it into struct inet_sock after 6.12)]

---

include/net/inet_connection_sock.h +3 -1
Index: b/include/net/inet_connection_sock.h
===================================================================
@@ -42,7 +42,9 @@ struct inet_connection_sock_af_ops {
 				      struct request_sock *req,
 				      struct dst_entry *dst,
 				      struct request_sock *req_unhash,
-				      bool *own_req);
+				      bool *own_req,
+				      void (*opt_child_init)(struct sock *newsk,
+							     const struct sock *sk));
 	u16	    net_header_len;
 	u16	    net_frag_header_len;
 	u16	    sockaddr_len;
include/net/tcp.h +3 -1
Index: b/include/net/tcp.h
===================================================================
@@ -460,7 +460,9 @@ struct sock *tcp_v4_syn_recv_sock(const
 				  struct request_sock *req,
 				  struct dst_entry *dst,
 				  struct request_sock *req_unhash,
-				  bool *own_req);
+				  bool *own_req,
+				  void (*opt_child_init)(struct sock *newsk,
+							 const struct sock *sk));
 int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb);
 int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
 int tcp_connect(struct sock *sk);
net/ipv4/syncookies.c +1 -1
Index: b/net/ipv4/syncookies.c
===================================================================
@@ -199,7 +199,7 @@ struct sock *tcp_get_cookie_sock(struct
 	bool own_req;
 
 	child = icsk->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
-						 NULL, &own_req);
+						 NULL, &own_req, NULL);
 	if (child) {
 		refcount_set(&req->rsk_refcnt, 1);
 		tcp_sk(child)->tsoffset = tsoff;
net/ipv4/tcp_fastopen.c +1 -1
Index: b/net/ipv4/tcp_fastopen.c
===================================================================
@@ -247,7 +247,7 @@ static struct sock *tcp_fastopen_create_
 	bool own_req;
 
 	child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL,
-							 NULL, &own_req);
+							 NULL, &own_req, NULL);
 	if (!child)
 		return NULL;
 
net/ipv4/tcp_ipv4.c +7 -1
Index: b/net/ipv4/tcp_ipv4.c
===================================================================
@@ -1564,7 +1564,9 @@ struct sock *tcp_v4_syn_recv_sock(const
 				  struct request_sock *req,
 				  struct dst_entry *dst,
 				  struct request_sock *req_unhash,
-				  bool *own_req)
+				  bool *own_req,
+				  void (*opt_child_init)(struct sock *newsk,
+							 const struct sock *sk))
 {
 	struct inet_request_sock *ireq;
 	bool found_dup_sk = false;
@@ -1620,6 +1622,10 @@ struct sock *tcp_v4_syn_recv_sock(const
 	}
 	sk_setup_caps(newsk, dst);
 
+#if IS_ENABLED(CONFIG_IPV6)
+	if (opt_child_init)
+		opt_child_init(newsk, sk);
+#endif
 	tcp_ca_openreq_child(newsk, dst);
 
 	tcp_sync_mss(newsk, dst_mtu(dst));
net/ipv4/tcp_minisocks.c +1 -1
Index: b/net/ipv4/tcp_minisocks.c
===================================================================
@@ -798,7 +798,7 @@ struct sock *tcp_check_req(struct sock *
 	 * socket is created, wait for troubles.
 	 */
 	child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL,
-							 req, &own_req);
+							 req, &own_req, NULL);
 	if (!child)
 		goto listen_overflow;
 
net/ipv6/tcp_ipv6.c +42 -54

112 lines not on this page · raw message

net/mptcp/subflow.c +4 -2
Index: b/net/mptcp/subflow.c
===================================================================
@@ -788,7 +788,9 @@ static struct sock *subflow_syn_recv_soc
 					  struct request_sock *req,
 					  struct dst_entry *dst,
 					  struct request_sock *req_unhash,
-					  bool *own_req)
+					  bool *own_req,
+					  void (*opt_child_init)(struct sock *newsk,
+								 const struct sock *sk))
 {
 	struct mptcp_subflow_context *listener = mptcp_subflow_ctx(sk);
 	struct mptcp_subflow_request_sock *subflow_req;
@@ -834,7 +836,7 @@ static struct sock *subflow_syn_recv_soc
 
 create_child:
 	child = listener->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
-						     req_unhash, own_req);
+						     req_unhash, own_req, opt_child_init);
 
 	if (child && *own_req) {
 		struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(child);
net/smc/af_smc.c +4 -2
Index: b/net/smc/af_smc.c
===================================================================
@@ -119,7 +119,9 @@ static struct sock *smc_tcp_syn_recv_soc
 					  struct request_sock *req,
 					  struct dst_entry *dst,
 					  struct request_sock *req_unhash,
-					  bool *own_req)
+					  bool *own_req,
+					  void (*opt_child_init)(struct sock *newsk,
+								 const struct sock *sk))
 {
 	struct smc_sock *smc;
 	struct sock *child;
@@ -144,7 +146,7 @@ static struct sock *smc_tcp_syn_recv_soc
 
 	/* passthrough to original syn recv sock fct */
 	child = smc->ori_af_ops->syn_recv_sock(sk, skb, req, dst, req_unhash,
-					       own_req);
+					       own_req, opt_child_init);
 	/* child must not inherit smc or its ops */
 	if (child) {
 		rcu_assign_sk_user_data(child, NULL);