tcp: fix potential race in tcp_v6_syn_recv_sock()
Subsystems (4)
PATCH 1/1 tcp: fix potential race in tcp_v6_syn_recv_sock()
From: Eric Dumazet <[email protected]>
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.
[florian:
- net/ipv6/tcp_ipv6.c:
- Set newnp->ipv6_fl_list = NULL instead of newinet->ipv6_fl_list = NULL, as ipv6_fl_list is in struct ipv6_pinfo.
- Guarded af_specific assignment with #ifdef CONFIG_TCP_MD5SIG instead of checking CONFIG_TCP_AO.
- Used if (tcp_inet6_sk(sk)->repflow) instead of inet6_test_bit(REPFLOW, sk).
- net/smc/af_smc.c:
- Dropped changes to net/smc/af_smc.c because SMC did not implement
`smc_tcp_syn_recv_sock()` in 5.10.y (added in 5.14 via commit e94a869818ab).]
--- include/net/inet_connection_sock.h | 4 +- include/net/tcp.h | 4 +- net/ipv4/syncookies.c | 2 +- net/ipv4/tcp_fastopen.c | 2 +- net/ipv4/tcp_ipv4.c | 8 ++- net/ipv4/tcp_minisocks.c | 2 +- net/ipv6/tcp_ipv6.c | 98 +++++++++++++----------------- net/mptcp/subflow.c | 6 +- 8 files changed, 62 insertions(+), 64 deletions(-)
include/net/inet_connection_sock.h +3 -1
@@ -41,7 +41,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
@@ -438,7 +438,9 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,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
@@ -201,7 +201,7 @@ struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,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
@@ -262,7 +262,7 @@ static struct sock *tcp_fastopen_create_child(struct sock *sk,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
@@ -1535,7 +1535,9 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,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;
@@ -1591,6 +1593,10 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb, }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
@@ -770,7 +770,7 @@ struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,* 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 -56
114 lines not on this page · raw message
net/mptcp/subflow.c +4 -2
@@ -532,7 +532,9 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,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;
@@ -582,7 +584,7 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk, 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);
Signature · 1 line
2.34.1
PATCH 1/1 tcp: fix potential race in tcp_v6_syn_recv_sock()
From: Eric Dumazet <[email protected]>
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.
[florian:
- net/ipv6/tcp_ipv6.c:
- Set newnp->ipv6_fl_list = NULL instead of newinet->ipv6_fl_list = NULL, as ipv6_fl_list is in struct ipv6_pinfo.
- Guarded af_specific assignment with #ifdef CONFIG_TCP_MD5SIG instead of checking CONFIG_TCP_AO.
- Used if (tcp_inet6_sk(sk)->repflow) instead of inet6_test_bit(REPFLOW, sk).]
--- include/net/inet_connection_sock.h | 4 +- include/net/tcp.h | 4 +- net/ipv4/syncookies.c | 2 +- net/ipv4/tcp_fastopen.c | 2 +- net/ipv4/tcp_ipv4.c | 8 ++- net/ipv4/tcp_minisocks.c | 2 +- net/ipv6/tcp_ipv6.c | 98 +++++++++++++----------------- net/mptcp/subflow.c | 6 +- net/smc/af_smc.c | 6 +- 9 files changed, 66 insertions(+), 66 deletions(-)
include/net/inet_connection_sock.h +3 -1
@@ -41,7 +41,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
@@ -446,7 +446,9 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,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
@@ -201,7 +201,7 @@ struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,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
@@ -251,7 +251,7 @@ static struct sock *tcp_fastopen_create_child(struct sock *sk,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
@@ -1556,7 +1556,9 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,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;
@@ -1612,6 +1614,10 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb, }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
@@ -769,7 +769,7 @@ struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,* 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 -56
114 lines not on this page · raw message
net/mptcp/subflow.c +4 -2
@@ -670,7 +670,9 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,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;
@@ -720,7 +722,7 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk, 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
@@ -76,7 +76,9 @@ static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk,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;
@@ -101,7 +103,7 @@ static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk,/* 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 );
Signature · 1 line
2.34.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [Low] The patch adds a 7th argument (opt_child_init) to the syn_recv_sock function pointer in struct inet_connection_sock_af_ops, but completely fails to update the DCCP subsystem. This breaks the build for DCCP.
Pre-existing issues:
- [Critical] This problem wasn't introduced by this patch, but if route lookup fails when creating an IPv4-mapped IPv6 child socket, the listener's IPv6 resources are erroneously freed.
- [High] This problem wasn't introduced by this patch, but the exact race condition fixed by this patch in TCP is also present in DCCP's IPv6-mapped implementation, which remains unfixed. --
commit eed5a807840b9b1bb7db87da21b6f743e2ad19f8
Author: Eric Dumazet <[email protected]>
tcp: fix potential race in tcp_v6_syn_recv_sock()
This commit addresses a race condition in tcp_v6_syn_recv_sock() where the child socket is visible in the TCP ehash table before its ipv6_pinfo is fully initialized. It moves the initialization code into a new helper tcp_v6_mapped_child_init() and passes it to tcp_v4_syn_recv_sock() to run before ehash insertion.
Quoted 4 lines
include/net/inet_connection_sock.h +0
-0
[ ... ]
include/net/inet_connection_sock.h struct inet_connection_sock_af_ops { · line 46
Quoted 8 lines
include/net/inet_connection_sock.h +3
-1@@ -41,7 +41,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));
[Severity: Low]
Does this change break the build for DCCP? The patch adds the opt_child_init argument to the syn_recv_sock function pointer, but does not appear to update the DCCP subsystem. Calling syn_recv_sock with 6 arguments in net/dccp/minisocks.c:dccp_check_req(), and defining dccp_v4_request_recv_sock() and dccp_v6_request_recv_sock() without the 7th argument will likely cause compiler errors.
[Severity: High]
This is a pre-existing issue, but does the same race condition you fixed here in TCP also affect DCCP?
In net/dccp/ipv6.c:dccp_v6_request_recv_sock(), the IPv6 fields for mapped sockets are initialized after the socket is already created and inserted into the ehash by dccp_v4_request_recv_sock():
newsk = dccp_v4_request_recv_sock(sk, skb, req, dst, req_unhash, own_req);
if (newsk == NULL) return NULL;
newdp6 = (struct dccp6_sock *)newsk;
newinet = inet_sk(newsk);
newinet->pinet6 = &newdp6->inet6;This appears to leave pinet6 pointing to the listener while the child socket is already visible to other CPUs, exposing the same race.
[ ... ]
Quoted 4 lines
net/ipv4/tcp_ipv4.c +0
-0
[ ... ]
net/ipv4/tcp_ipv4.c struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb, · line 1620
Quoted 8 lines
net/ipv4/tcp_ipv4.c +4
-0@@ -1612,6 +1614,10 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb, }sk_setup_caps (newsk, dst); +#if IS_ENABLED (CONFIG_IPV6 ) +if (opt_child_init) +opt_child_init (newsk, sk); +#endif
[Severity: Critical]
This is a pre-existing issue, but can this lead to freeing the listener's IPv6 resources if route lookup fails?
If inet_csk_route_child_sock() fails before opt_child_init() is called in tcp_v4_syn_recv_sock():
if (!dst) {
dst = inet_csk_route_child_sock(sk, newsk, req);
if (!dst)
goto put_and_exit;
}It jumps directly to the put_and_exit label where tcp_done(newsk) is called to destroy the child socket. Since pinet6 has not yet been reassigned by opt_child_init(), it still points to the listener's ipv6_pinfo. This appears to incorrectly free the listener's resources, such as pktoptions and rxpmtu, which could lead to a double-free or use-after-free on the listener.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH 1/1 tcp: fix potential race in tcp_v6_syn_recv_sock()
In reply to Florian FainelliFrom: Eric Dumazet <[email protected]>
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.
[florian:
- net/ipv6/tcp_ipv6.c:
- Set newnp->ipv6_fl_list = NULL instead of newinet->ipv6_fl_list = NULL, as ipv6_fl_list is in struct ipv6_pinfo.
- Guarded af_specific assignment with #ifdef CONFIG_TCP_MD5SIG instead of checking CONFIG_TCP_AO.
- Used if (tcp_inet6_sk(sk)->repflow) instead of inet6_test_bit(REPFLOW, sk).]
--- include/net/inet_connection_sock.h | 4 +- include/net/tcp.h | 4 +- net/ipv4/syncookies.c | 2 +- net/ipv4/tcp_fastopen.c | 2 +- net/ipv4/tcp_ipv4.c | 8 ++- net/ipv4/tcp_minisocks.c | 2 +- net/ipv6/tcp_ipv6.c | 97 +++++++++++++----------------- net/mptcp/subflow.c | 6 +- net/smc/af_smc.c | 6 +- 9 files changed, 66 insertions(+), 65 deletions(-)
include/net/inet_connection_sock.h +3 -1
@@ -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
@@ -461,7 +461,9 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,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
@@ -199,7 +199,7 @@ struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,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
@@ -247,7 +247,7 @@ static struct sock *tcp_fastopen_create_child(struct sock *sk,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
@@ -1566,7 +1566,9 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,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;
@@ -1622,6 +1624,10 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb, }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
@@ -817,7 +817,7 @@ struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,* 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 -55
113 lines not on this page · raw message
net/mptcp/subflow.c +4 -2
@@ -788,7 +788,9 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,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_sock(const struct sock *sk, 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
@@ -119,7 +119,9 @@ static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk,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_sock(const struct sock *sk,/* 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 );
Signature · 1 line
2.34.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
New issues:
- [Low] The DCCP subsystem was not updated to handle the new syn_recv_sock function pointer signature, resulting in a build failure and Control Flow Integrity (CFI) violations.
Pre-existing issues:
- [Critical] Use-After-Free and Double-Free of listener's ipv6_pinfo on the tcp_v4_syn_recv_sock error path.
- [High] The patch leaves the identical ehash race condition completely unfixed in the DCCP subsystem.
- [High] The opt_child_init callback is non-composable and ignored by IPv6, leaving wrapper ULPs (SMC, MPTCP) vulnerable to the same ehash race condition. --
commit 1232c8e7afdc63a365473845ad392043814df4fb
Author: Eric Dumazet <[email protected]>
tcp: fix potential race in tcp_v6_syn_recv_sock()
This commit fixes a race condition where a child socket is inserted into the TCP ehash table while its newinet->pinet6 still points to the listener's IPv6 info. It introduces a new opt_child_init callback parameter to syn_recv_sock to perform the initialization before ehash insertion.
include/net/inet_connection_sock.h struct inet_connection_sock_af_ops { · line 47
Quoted 11 lines
include/net/inet_connection_sock.h +3
-1@@ -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));
[Severity: Low]
Does this break the DCCP subsystem?
The DCCP implementations of syn_recv_sock (dccp_v4_request_recv_sock and dccp_v6_request_recv_sock) were not updated to accept this new 7th parameter. Since dccp_check_req() still passes 6 arguments, this appears to cause a direct compilation error (too few arguments). If compiled with loose warnings and kCFI enabled, it could also cause pointer mismatch panics at runtime.
[Severity: High]
This is a pre-existing issue, but does this leave the identical ehash race condition unfixed in DCCP?
dccp_v6_request_recv_sock() calls dccp_v4_request_recv_sock(), which inserts the socket into the ehash via inet_ehash_nolisten(). Only after it returns does dccp_v6_request_recv_sock() initialize newinet->pinet6. A lockless reader (like sock_diag or BPF) can access the socket from the ehash in this window and observe the listener's pinet6 pointer, which is exactly the race condition this patch fixes for TCP.
[ ... ]
net/ipv4/tcp_ipv4.c struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb, · line 1630
Quoted 11 lines
net/ipv4/tcp_ipv4.c +4
-0@@ -1622,6 +1624,10 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb, }sk_setup_caps (newsk, dst); +#if IS_ENABLED (CONFIG_IPV6 ) +if (opt_child_init) +opt_child_init (newsk, sk); +#endif
[Severity: Critical]
This is a pre-existing issue, but is there a use-after-free of the listener's ipv6_pinfo if routing fails earlier in this function?
If inet_csk_route_child_sock() fails before sk_setup_caps() and this new opt_child_init() call:
if (!dst) {
dst = inet_csk_route_child_sock(sk, newsk, req);
if (!dst)
goto put_and_exit;
}The code jumps to put_and_exit, which destroys the child socket:
put_and_exit:
newinet->inet_opt = NULL; inet_csk_prepare_forced_close(newsk); tcp_done(newsk); goto exit;
Since put_and_exit calls tcp_done() before opt_child_init() has repointed pinet6 to the child's own memory, pinet6 still points to the listener. The destruction path will free the listener's pktoptions, rxpmtu, and opt while the listener is still active, causing a double-free memory corruption.
[ ... ]
Quoted 8 lines
net/ipv6/tcp_ipv6.c +0
-1@@ -1200,60 +1237,10 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff *#endif struct flowi6 fl6; -if (skb->protocol ==htons (ETH_P_IP )) {
[ ... ]
net/ipv6/tcp_ipv6.c static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff * · line 1243
Quoted 4 lines
net/ipv6/tcp_ipv6.c +4
-0+if (skb->protocol ==htons (ETH_P_IP )) +return tcp_v4_syn_recv_sock (sk, skb, req, dst, + req_unhash, own_req, + tcp_v6_mapped_child_init);
[Severity: High]
This is a pre-existing issue, but by overriding opt_child_init with a hardcoded tcp_v6_mapped_child_init callback here, does this leave wrapper ULPs like SMC and MPTCP vulnerable to the same ehash race condition?
Because tcp_v6_syn_recv_sock() ignores the passed opt_child_init callback for IPv6 (and hardcodes it for v4-mapped), wrapper ULPs cannot use it to initialize their state before ehash insertion.
[ ... ]
net/mptcp/subflow.c static struct sock *subflow_syn_recv_sock(const struct sock *sk, · line 839
Quoted 9 lines
net/mptcp/subflow.c +1
-1@@ -834,7 +836,7 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk, 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);
[Severity: High]
This is a pre-existing issue, but since MPTCP mutates ctx->conn via mptcp_sk_clone_init() after syn_recv_sock() returns, does this create a race window where lockless readers can access the listener's state on the newly hashed child?
This seems especially problematic for IPv6 because the passed opt_child_init callback is completely ignored by tcp_v6_syn_recv_sock().
[ ... ]
net/smc/af_smc.c static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk, · line 152
Quoted 12 lines
net/smc/af_smc.c +1
-1@@ -144,7 +146,7 @@ static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk,/* 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 );
[Severity: High]
This is a pre-existing issue, but does clearing the inherited user data via rcu_assign_sk_user_data(child, NULL) here occur too late?
Because this happens after syn_recv_sock() has already hashed the child socket globally, there is a window where lockless readers can access the listener's SMC state on the child socket. Again, this is exacerbated for IPv6 where the opt_child_init callback is ignored.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH 1/1 tcp: fix potential race in tcp_v6_syn_recv_sock()
In reply to Florian FainelliFrom: Eric Dumazet <[email protected]>
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.
[florian:
- net/ipv6/tcp_ipv6.c:
- Set newnp->ipv6_fl_list = NULL instead of newinet->ipv6_fl_list = NULL, as ipv6_fl_list is in struct ipv6_pinfo.
- Guarded af_specific assignment with #ifdef CONFIG_TCP_MD5SIG instead of checking CONFIG_TCP_AO.
- Used if (tcp_inet6_sk(sk)->repflow) instead of inet6_test_bit(REPFLOW, sk).]
--- include/net/inet_connection_sock.h | 4 +- include/net/tcp.h | 4 +- net/ipv4/syncookies.c | 2 +- net/ipv4/tcp_fastopen.c | 2 +- net/ipv4/tcp_ipv4.c | 8 ++- net/ipv4/tcp_minisocks.c | 2 +- net/ipv6/tcp_ipv6.c | 97 +++++++++++++----------------- net/mptcp/subflow.c | 6 +- net/smc/af_smc.c | 6 +- 9 files changed, 66 insertions(+), 65 deletions(-)
include/net/inet_connection_sock.h +3 -1
@@ -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
@@ -468,7 +468,9 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,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
@@ -199,7 +199,7 @@ struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,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
@@ -247,7 +247,7 @@ static struct sock *tcp_fastopen_create_child(struct sock *sk,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
@@ -1509,7 +1509,9 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,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;
@@ -1565,6 +1567,10 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb, }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
@@ -780,7 +780,7 @@ struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,* 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 -55
113 lines not on this page · raw message
net/mptcp/subflow.c +4 -2
@@ -699,7 +699,9 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk,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;
@@ -745,7 +747,7 @@ static struct sock *subflow_syn_recv_sock(const struct sock *sk, 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
@@ -118,7 +118,9 @@ static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk,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;
@@ -143,7 +145,7 @@ static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk,/* 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 );
Signature · 1 line
2.34.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [Low] The patch adds a new argument to the syn_recv_sock function pointer in struct inet_connection_sock_af_ops but fails to update the DCCP subsystem, breaking the build when CONFIG_IP_DCCP is enabled.
Pre-existing issues:
- [Critical] Use-After-Free and double-free of the listener's IPv6 options on routing failure during IPv4-mapped socket creation.
- [High] The DCCP subsystem contains the identical race condition for IPv4-mapped IPv6 sockets that this patch intended to fix for TCP. --
commit 0524a546fd92e10009336d36766fbc7fa6b854ac
Author: Eric Dumazet <[email protected]>
tcp: fix potential race in tcp_v6_syn_recv_sock()
This commit fixes a race condition where a child socket is inserted into the TCP ehash table before its IPv6-specific fields are fully initialized. It introduces an opt_child_init callback in the syn_recv_sock af_ops to allow IPv6 mapped sockets to be initialized before hash insertion.
include/net/inet_connection_sock.h struct inet_connection_sock_af_ops { · line 47
Quoted 12 lines
include/net/inet_connection_sock.h +3
-1@@ -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));
[Severity: Low]
Does this change break the build for the DCCP subsystem?
It looks like dccp_v4_request_recv_sock(), dccp_v6_request_recv_sock(), and their caller dccp_check_req() in net/dccp/minisocks.c were not updated to handle this new 7th argument. This would result in compilation errors when CONFIG_IP_DCCP is enabled.
[Severity: High]
This isn't a bug introduced by this patch, but does the DCCP subsystem suffer from the same race condition this commit fixes for TCP?
In net/dccp/ipv6.c:dccp_v6_request_recv_sock(), the child socket is created and inserted into the ehash table by dccp_v4_request_recv_sock() before its IPv6-specific fields are initialized:
newsk = dccp_v4_request_recv_sock(sk, skb, req, dst,
req_unhash, own_req);
if (newsk == NULL)
return NULL;
newdp6 = (struct dccp6_sock *)newsk;
newinet = inet_sk(newsk);
newinet->pinet6 = &newdp6->inet6;Could concurrent threads access uninitialized IPv6 fields via a lookup, similar to the syzbot crash found in TCP?
[ ... ]
net/ipv4/tcp_ipv4.c struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb, · line 1574
Quoted 13 lines
net/ipv4/tcp_ipv4.c +4
-0@@ -1565,6 +1567,10 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb, }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);
[Severity: Critical]
This is a pre-existing issue, but does this routing failure path in tcp_v4_syn_recv_sock() cause a use-after-free of the listener's IPv6 options?
Just above this block in tcp_v4_syn_recv_sock():
if (!dst) {
dst = inet_csk_route_child_sock(sk, newsk, req);
if (!dst)
goto put_and_exit;
}If inet_csk_route_child_sock() fails, the code jumps to put_and_exit and destroys the child socket. Because opt_child_init() hasn't run yet, newinet->pinet6 still points to the listener's ipv6_pinfo. The child's destructor (inet6_destroy_sock) will then free the listener's options, corrupting the active listener socket.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [Low] The patch adds a 7th argument opt_child_init to the syn_recv_sock callback but fails to update the DCCP subsystem, leading to compilation errors.
Pre-existing issues:
- [Critical] This problem wasn't introduced by this patch, but the error path in tcp_v4_syn_recv_sock() frees the listening socket's IPv6 options via a stale pinet6 pointer.
- [High] This problem wasn't introduced by this patch, but the exact data race condition fixed for TCP IPv6-mapped sockets was left unfixed for DCCP IPv6-mapped sockets. --
commit 9fb42b0dda20f2c1f24b669590d7c23a325895a3
Author: Eric Dumazet <[email protected]>
tcp: fix potential race in tcp_v6_syn_recv_sock()
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. 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.
Quoted 4 lines
include/net/inet_connection_sock.h +0
-0
[ ... ]
include/net/inet_connection_sock.h struct inet_connection_sock_af_ops { · line 49
Quoted 11 lines
include/net/inet_connection_sock.h +3
-1@@ -41,7 +41,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;
[Severity: Low]
Does the DCCP subsystem need to be updated for this new 7th argument?
The syn_recv_sock callback signature now requires 7 arguments, but it looks like dccp_v4_request_recv_sock() and dccp_v6_request_recv_sock() might still be using 6 arguments, and dccp_check_req() in net/dccp/minisocks.c might be missing the 7th argument in its call. Could this cause a build failure when CONFIG_IP_DCCP is enabled due to an arity mismatch?
[Severity: High]
This is a pre-existing issue, but does DCCP have the same race condition for IPv6-mapped sockets that this patch fixes for TCP?
Looking at dccp_v6_request_recv_sock() in net/dccp/ipv6.c:
dccp_v6_request_recv_sock() {
newsk = dccp_v4_request_recv_sock(sk, skb, req, dst, ...);
if (newsk == NULL)
return NULL;
newdp6 = (struct dccp6_sock *)newsk;
newinet = inet_sk(newsk);
newinet->pinet6 = &newdp6->inet6;
newnp = inet6_sk(newsk);
memcpy(newnp, np, sizeof(struct ipv6_pinfo));
...
}It appears that dccp_v4_request_recv_sock() publishes the child socket to the ehash table before dccp_v6_request_recv_sock() initializes its pinet6 pointer. Can a concurrent thread looking up the ehash table access the partially initialized DCCP socket?
Quoted 4 lines
net/ipv4/tcp_ipv4.c +0
-0
[ ... ]
net/ipv4/tcp_ipv4.c struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb, · line 1602
Quoted 11 lines
net/ipv4/tcp_ipv4.c +4
-0@@ -1591,6 +1593,10 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb, }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));
[Severity: Critical]
This isn't a bug introduced by this patch, but does placing the opt_child_init() callback here leave a window for a use-after-free on the listener's IPv6 options in the error path?
When an IPv4 SYN packet is received on an IPv6-mapped TCP listener, tcp_create_openreq_child() bitwise-copies the listener's memory into the new socket, meaning newinet->pinet6 points to the listener's ipv6_pinfo.
If routing fails before reaching this new callback, such as when processing an unroutable spoofed source IP:
tcp_v4_syn_recv_sock() {
newsk = tcp_create_openreq_child(sk, req, skb);
...
if (!dst) {
dst = inet_csk_route_child_sock(sk, newsk, req);
if (!dst)
goto put_and_exit;
}
...
if (opt_child_init)
opt_child_init(newsk, sk);
...
put_and_exit:
newinet->inet_opt = NULL;
inet_csk_prepare_forced_close(newsk);
tcp_done(newsk);
...
}The error path put_and_exit will call tcp_done(newsk) and destroy the child socket. Since opt_child_init() hasn't reassigned pinet6 yet, won't the child socket's destructor free the listener's IPv6 options (pktoptions, rxpmtu, opt) while the listener is still actively using them?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1