Skip to contentSkip to conversation

tcp: initialize missing request_sock fields in syncookie path

PATCH Superseded Thaison Phan 3 participants 9 messages Nov 14 – Nov 16

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover tcp: initialize missing request_sock fields in syncookie path

Initialize missing request_sock fields for TCP connections accepted via SYN cookies, where cookie_tcp_reqsk_alloc() and bpf_sk_assign_tcp_reqsk() allocate without __GFP_ZERO:

- Patch 1 initializes ireq->ir_rmt_addr and ireq->ir_loc_addr to LOOPBACK4_IPV6 in cookie_v6_check(), matching tcp_v6_init_req().
- Patch 2 initializes the AccECN request_sock fields in cookie_tcp_reqsk_init() and sets syn_ect_snt in cookie_{v4,v6}_check(), matching tcp_openreq_init() and tcp_{v4,v6}_send_synack().

An AI coding assistant helped audit the request_sock initialization paths, write the QEMU reproducer, and draft the changelogs.

Thaison Phan (2):
tcp: initialize ir_rmt_addr and ir_loc_addr in cookie_v6_check()
tcp: accecn: initialize AccECN fields in syncookie path

 net/ipv4/syncookies.c | 5 +++++
 net/ipv6/syncookies.c | 4 ++++
 2 files changed, 9 insertions(+)

base-commit: 19ec8416a76c08326fe5f814410a1b43046e63be

Signature · 1 line
2.56.0.611.g1030f0b940-goog
#1 lore raw

PATCH 1/2 tcp: initialize ir_rmt_addr and ir_loc_addr in cookie_v6_check()

Commit a3a128f611a9 ("inet: consolidate inet_csk_clone_lock()") moved the LOOPBACK4_IPV6 initialization for passive IPv6 TCP sockets from tcp_v6_syn_recv_sock() to tcp_v6_init_req().

IPv6 SYN cookie connections allocate a fresh request_sock without __GFP_ZERO and call tcp_v6_syn_recv_sock() without tcp_v6_init_req(), leaving ireq->ir_rmt_addr and ireq->ir_loc_addr uninitialized in cookie_v6_check(). inet_csk_clone_lock() then copies uninitialized slab bytes into sk_daddr, sk_rcv_saddr, and inet_saddr, exposing them to tracepoints and bpf_sock and causing inet_daddr != LOOPBACK4_IPV6 checks (such as IP_OPTIONS in do_ip_setsockopt()) to misclassify native IPv6 child sockets as IPv4-mapped.

Initialize ireq->ir_rmt_addr and ireq->ir_loc_addr to LOOPBACK4_IPV6 in cookie_v6_check() to match tcp_v6_init_req().

Found in CI testing on a KASAN kernel; reproduced without kernel modifications in a KASAN VM (tcp_syncookies=2) and tested against the reproducer and packetdrill syncookie selftests.

Fixes a3a128f611a9 ("inet: consolidate inet_csk_clone_lock()")
---
 net/ipv6/syncookies.c | 3 +++
 1 file changed, 3 insertions(+)
net/ipv6/syncookies.c +3 -0
index b581cb1ee2e8..b33011b71fff 100644
@@ -17,6 +17,7 @@
 #include <net/ipv6.h>
 #include <net/tcp.h>
 #include <net/tcp_ecn.h>
+#include <net/transp_v6.h>
 
 #define COOKIEBITS 24	/* Upper bits store count */
 #define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1)
@@ -205,6 +206,8 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
 
 	ireq->ir_v6_rmt_addr = ipv6_hdr(skb)->saddr;
 	ireq->ir_v6_loc_addr = ipv6_hdr(skb)->daddr;
+	ireq->ir_rmt_addr = LOOPBACK4_IPV6;
+	ireq->ir_loc_addr = LOOPBACK4_IPV6;
 
 	if (security_inet_conn_request(sk, skb, req)) {
 		SKB_DR_SET(reason, SECURITY_HOOK);
Signature · 1 line
2.56.0.611.g1030f0b940-goog
maintainer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH net 1/2] tcp: initialize ir_rmt_addr and ir_loc_addr in cookie_v6_check()

Quoted 14 lines · Thaison Phan

Commit a3a128f611a9 ("inet: consolidate inet_csk_clone_lock()") moved the LOOPBACK4_IPV6 initialization for passive IPv6 TCP sockets from tcp_v6_syn_recv_sock() to tcp_v6_init_req().

IPv6 SYN cookie connections allocate a fresh request_sock without __GFP_ZERO and call tcp_v6_syn_recv_sock() without tcp_v6_init_req(), leaving ireq->ir_rmt_addr and ireq->ir_loc_addr uninitialized in cookie_v6_check(). inet_csk_clone_lock() then copies uninitialized slab bytes into sk_daddr, sk_rcv_saddr, and inet_saddr, exposing them to tracepoints and bpf_sock and causing inet_daddr != LOOPBACK4_IPV6 checks (such as IP_OPTIONS in do_ip_setsockopt()) to misclassify native IPv6 child sockets as IPv4-mapped.

Quoted 7 lines

Initialize ireq->ir_rmt_addr and ireq->ir_loc_addr to LOOPBACK4_IPV6 in cookie_v6_check() to match tcp_v6_init_req().

Found in CI testing on a KASAN kernel; reproduced without kernel modifications in a KASAN VM (tcp_syncookies=2) and tested against the reproducer and packetdrill syncookie selftests.

Nice catch, but KASAN does not report uninitialized reads.

Was this KMSAN, or did you notice garbage in the inet_sock_set_state tracepoint output?

Please say which in the changelog, and include the report or the trace line if you have one.

Quoted 19 lines
Fixes a3a128f611a9 ("inet: consolidate inet_csk_clone_lock()")
---
 net/ipv6/syncookies.c | 3 +++
 1 file changed, 3 insertions(+)
net/ipv6/syncookies.c +1 -0
index b581cb1ee2e8..b33011b71fff 100644
@@ -17,6 +17,7 @@
 #include <net/ipv6.h>
 #include <net/tcp.h>
 #include <net/tcp_ecn.h>
+#include <net/transp_v6.h>
 
 #define COOKIEBITS 24  /* Upper bits store count */
 #define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1)

net/ipv6/syncookies.c struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb) · line 210

Quoted 5 lines
net/ipv6/syncookies.c +2 -0
@@ -205,6 +206,8 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
 
        ireq->ir_v6_rmt_addr = ipv6_hdr(skb)->saddr;
        ireq->ir_v6_loc_addr = ipv6_hdr(skb)->daddr;
+       ireq->ir_rmt_addr = LOOPBACK4_IPV6;
+       ireq->ir_loc_addr = LOOPBACK4_IPV6;

With the changelog clarified:

pw-bot: cr

#3 lore raw

Re: [PATCH net 1/2] tcp: initialize ir_rmt_addr and ir_loc_addr in cookie_v6_check()

Quoted 6 lines · Eric Dumazet

Nice catch, but KASAN does not report uninitialized reads. Was this KMSAN, or did you notice garbage in the inet_sock_set_state tracepoint output?

Please say which in the changelog, and include the report or the trace line if you have one.

I should have explained KASAN's relation to the bug better. It was not caught by a KASAN/KMSAN report itself, but originally noticed via an out-of-tree socket hook observing inet_sk(sk)->inet_saddr != LOOPBACK4_IPV6 on KASAN test runs, and reproduced upstream via the inet_sock_set_state tracepoint.

When KASAN is enabled, SLUB_RCU_DEBUG is enabled by default, introducing an RCU grace period before freeing the request_sock from the initial SYN. Without that grace period, cookie_v6_check() typically reuses the warm request_sock whose ir_rmt_addr and ir_loc_addr were already set to LOOPBACK4_IPV6. With the grace period, cookie_v6_check() gets a cold slab object instead, e.g. (with slub_debug=P, expecting 127.0.0.6):

  ... sock:inet_sock_set_state: family=AF_INET6 protocol=IPPROTO_TCP \
      sport=59839 dport=41050 \
      saddr=90.90.90.90 daddr=90.90.90.90 \
      saddrv6=::1 daddrv6=::1 \
      oldstate=TCP_LISTEN newstate=TCP_SYN_RECV

I'll clarify this and include the trace output in the v2 changelog.

#4 lore raw

PATCH 2/2 tcp: accecn: initialize AccECN fields in syncookie path

In reply to Thaison Phan

cookie_tcp_reqsk_alloc() and bpf_sk_assign_tcp_reqsk() allocate struct request_sock without __GFP_ZERO, leaving saw_accecn_opt, accecn_fail_mode, syn_ect_rcv, and syn_ect_snt uninitialized. When accecn_ok is negotiated in cookie_{v4,v6}_check(), tcp_ecn_openreq_child() reads all four fields to initialize the child socket's AccECN state.

In addition, because the temporary request_sock used to send a cookie SYN-ACK is immediately freed, treq->syn_ect_snt is lost. If the listener marks ECT (e.g. DCTCP), leaving syn_ect_snt at 0 causes tcp_accecn_third_ack() to flag TCP_ACCECN_ACE_FAIL_RECV when the peer reflects ECT, disabling ECT for the rest of the connection.

Initialize the AccECN fields in cookie_tcp_reqsk_init() to match tcp_openreq_init(), and set syn_ect_snt from the listener's TOS/tclass in cookie_{v4,v6}_check() to match tcp_{v4,v6}_send_synack().

Found by code inspection while auditing request_sock initialization in the syncookie path; reproduced without kernel modifications in a KASAN VM (tcp_syncookies=2, tcp_ecn=3) and tested against the reproducer and packetdrill AccECN/syncookie selftests.

Fixes 3cae34274c79 ("tcp: accecn: AccECN negotiation")
Fixes b40671b5ee58 ("tcp: accecn: AccECN option failure handling")
---
 net/ipv4/syncookies.c | 5 +++++
 net/ipv6/syncookies.c | 1 +
 2 files changed, 6 insertions(+)
net/ipv4/syncookies.c +5 -0
index 4e0ff3b82062..5747f9a5a6d1 100644
@@ -290,6 +290,10 @@ static int cookie_tcp_reqsk_init(struct sock *sk, struct sk_buff *skb,
 	 */
 	treq->txhash = treq->snt_isn;
 	treq->syn_tos = TCP_SKB_CB(skb)->ip_dsfield;
+	treq->saw_accecn_opt = TCP_ACCECN_OPT_NOT_SEEN;
+	treq->accecn_fail_mode = 0;
+	treq->syn_ect_rcv = 0;
+	treq->syn_ect_snt = 0;
 
 #if IS_ENABLED(CONFIG_MPTCP)
 	treq->is_mptcp = sk_is_mptcp(sk);
@@ -502,6 +506,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
 		ireq->rcv_wscale = rcv_wscale;
 	ireq->ecn_ok &= cookie_ecn_ok(net, &rt->dst);
 	treq->accecn_ok = ireq->ecn_ok && cookie_accecn_ok(th);
+	treq->syn_ect_snt = READ_ONCE(inet_sk(sk)->tos) & INET_ECN_MASK;
 
 	ret = tcp_get_cookie_sock(sk, skb, req, &rt->dst);
 	/* ip_queue_xmit() depends on our flow being setup
net/ipv6/syncookies.c +1 -0
index b33011b71fff..7435ebcc366c 100644
@@ -276,6 +276,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
 		ireq->rcv_wscale = rcv_wscale;
 	ireq->ecn_ok &= cookie_ecn_ok(net, dst);
 	tcp_rsk(req)->accecn_ok = ireq->ecn_ok && cookie_accecn_ok(th);
+	tcp_rsk(req)->syn_ect_snt = READ_ONCE(np->tclass) & INET_ECN_MASK;
 
 	ret = tcp_get_cookie_sock(sk, skb, req, dst);
 	if (!ret) {
Signature · 1 line
2.56.0.611.g1030f0b940-goog
maintainer Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH net 2/2] tcp: accecn: initialize AccECN fields in syncookie path

Quoted 8 lines · Thaison Phan

cookie_tcp_reqsk_alloc() and bpf_sk_assign_tcp_reqsk() allocate struct request_sock without __GFP_ZERO, leaving saw_accecn_opt, accecn_fail_mode, syn_ect_rcv, and syn_ect_snt uninitialized. When accecn_ok is negotiated in cookie_{v4,v6}_check(), tcp_ecn_openreq_child() reads all four fields to initialize the child socket's AccECN state.

Right.

Quoted 5 lines

In addition, because the temporary request_sock used to send a cookie SYN-ACK is immediately freed, treq->syn_ect_snt is lost. If the listener marks ECT (e.g. DCTCP), leaving syn_ect_snt at 0 causes tcp_accecn_third_ack() to flag TCP_ACCECN_ACE_FAIL_RECV when the peer reflects ECT, disabling ECT for the rest of the connection.

This is a different bug than the uninitialized fields above, with a different impact. I would prefer two patches, one per bug, but since both Fixes: commits landed in the same release I will not insist.

Quoted 9 lines

Initialize the AccECN fields in cookie_tcp_reqsk_init() to match tcp_openreq_init(), and set syn_ect_snt from the listener's TOS/tclass in cookie_{v4,v6}_check() to match tcp_{v4,v6}_send_synack().

Found by code inspection while auditing request_sock initialization in the syncookie path; reproduced without kernel modifications in a KASAN VM (tcp_syncookies=2, tcp_ecn=3) and tested against the reproducer and packetdrill AccECN/syncookie selftests.

Same remark as for the LOOPBACK4_IPV6 patch: KASAN does not catch uninitialized bitfields. Please say how the failure was observed (KMSAN, tcpi_accecn_fail_mode in TCP_INFO on the child, ...).

Quoted 14 lines
Fixes 3cae34274c79 ("tcp: accecn: AccECN negotiation")
Fixes b40671b5ee58 ("tcp: accecn: AccECN option failure handling")
---
 net/ipv4/syncookies.c | 5 +++++
 net/ipv6/syncookies.c | 1 +
 2 files changed, 6 insertions(+)
net/ipv4/syncookies.c +0 -0
index 4e0ff3b82062..5747f9a5a6d1 100644

net/ipv4/syncookies.c static int cookie_tcp_reqsk_init(struct sock *sk, struct sk_buff *skb, · line 296

Quoted 7 lines
net/ipv4/syncookies.c +4 -0
@@ -290,6 +290,10 @@ static int cookie_tcp_reqsk_init(struct sock *sk, struct sk_buff *skb,
         */
        treq->txhash = treq->snt_isn;
        treq->syn_tos = TCP_SKB_CB(skb)->ip_dsfield;
+       treq->saw_accecn_opt = TCP_ACCECN_OPT_NOT_SEEN;
+       treq->accecn_fail_mode = 0;
+       treq->syn_ect_rcv = 0;
+       treq->syn_ect_snt = 0;

I think syn_ect_snt is always overwritten in cookie_{v4,v6}_check() before being read, this line is not needed?

Quoted 24 lines
net/ipv4/syncookies.c +1 -0
 #if IS_ENABLED(CONFIG_MPTCP)
        treq->is_mptcp = sk_is_mptcp(sk);
@@ -502,6 +506,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
                ireq->rcv_wscale = rcv_wscale;
        ireq->ecn_ok &= cookie_ecn_ok(net, &rt->dst);
        treq->accecn_ok = ireq->ecn_ok && cookie_accecn_ok(th);
+       treq->syn_ect_snt = READ_ONCE(inet_sk(sk)->tos) & INET_ECN_MASK;
 
        ret = tcp_get_cookie_sock(sk, skb, req, &rt->dst);
        /* ip_queue_xmit() depends on our flow being setup
net/ipv6/syncookies.c +1 -1
index b33011b71fff..7435ebcc366c 100644
@@ -276,6 +276,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
                ireq->rcv_wscale = rcv_wscale;
        ireq->ecn_ok &= cookie_ecn_ok(net, dst);
        tcp_rsk(req)->accecn_ok = ireq->ecn_ok && cookie_accecn_ok(th);
+       tcp_rsk(req)->syn_ect_snt = READ_ONCE(np->tclass) & INET_ECN_MASK;
 
        ret = tcp_get_cookie_sock(sk, skb, req, dst);
        if (!ret) {
--
Quoted 2 lines

2.56.0.611.g1030f0b940-goog

Thanks.

#6 lore raw

Re: [PATCH net 2/2] tcp: accecn: initialize AccECN fields in syncookie path

Quoted 4 lines · Eric Dumazet

This is a different bug than the uninitialized fields above, with a different impact. I would prefer two patches, one per bug, but since both Fixes: commits landed in the same release I will not insist.

Makes sense. I'll make sure to split independent fixes into separate patches in the future.

Quoted 3 lines

Same remark as for the LOOPBACK4_IPV6 patch: KASAN does not catch uninitialized bitfields. Please say how the failure was observed (KMSAN, tcpi_accecn_fail_mode in TCP_INFO on the child, ...).

This was spotted by code inspection (with AI assistance) comparing tcp_openreq_init() and cookie_tcp_reqsk_init() while investigating the LOOPBACK4_IPV6 bug. We then verified it on a kernel with SLUB_RCU_DEBUG=y and slub_debug=P (preventing reuse of the warm request_sock from the initial SYN), observing bogus tcpi_accecn_fail_mode and tcpi_accecn_opt_seen values in TCP_INFO on the accepted child socket (as well as tcpi_accecn_fail_mode=0x2 when the listener sends ECT(0) on the SYN-ACK).

I'll update the v2 changelog with these details.

Quoted 2 lines

I think syn_ect_snt is always overwritten in cookie_{v4,v6}_check() before being read, this line is not needed?

Agreed, cookie_{v4,v6}_check() always overwrite syn_ect_snt before tcp_get_cookie_sock() reads it. I'll drop that line in v2.

#7 lore raw

RE: [PATCH net 2/2] tcp: accecn: initialize AccECN fields in syncookie path

In reply to Thaison Phan
Quoted 7 lines

-----Original Message-----
From: Thaison Phan <[email protected]>
Sent: Saturday, November 14, 2026 5:13 PM
To: Eric Dumazet <[email protected]>; Neal Cardwell <[email protected]>; Kuniyuki Iwashima <[email protected]>; David S . Miller <[email protected]>; Jakub Kicinski <[email protected]>; Paolo Abeni <[email protected]>; Ilpo Järvinen <[email protected]>; Olivier Tilmans (Nokia) <[email protected]>; Chia-Yu Chang (Nokia) <[email protected]>
Cc: Simon Horman <[email protected]>; Stan Iliev <[email protected]>; [email protected]; [email protected]; Thaison Phan <[email protected]>
Subject: [PATCH net 2/2] tcp: accecn: initialize AccECN fields in syncookie path

Quoted 6 lines

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

CAUTION: This is an external email. Please be very careful when clicking links or opening attachments. See the URL nok.it/ext for additional information.

If the listener marks ECT (e.g. DCTCP), leaving syn_ect_snt at 0 causes tcp_accecn_third_ack() to flag TCP_ACCECN_ACE_FAIL_RECV when the peer reflects ECT, disabling ECT for the rest of the connection.

Hi,

Thanks for the fix, the only change I propose is to clarify the above message into:

"For AccECN connections where the listener marks ECT on the SYN-ACK, leaving syn_ect_snt as 0 causes tcp_accecn_third_ack() to see a mismatch between the expected Not-ECT and the peer's reflected ECT in the ACE field, erroneously flagging TCP_ACCECN_ACE_FAIL_RECV in accecn_fail_mode. Refer to Table 4 of RFC 9768 for more details."

Because RFC3168 ECN does not use this syn_ect_snt.

Thanks.
Chia-Yu

#8 lore raw

Re: [PATCH net 2/2] tcp: accecn: initialize AccECN fields in syncookie path

Quoted 9 lines · Chia-Yu Chang (Nokia)

Thanks for the fix, the only change I propose is to clarify the above message into:

"For AccECN connections where the listener marks ECT on the SYN-ACK, leaving syn_ect_snt as 0 causes tcp_accecn_third_ack() to see a mismatch between the expected Not-ECT and the peer's reflected ECT in the ACE field, erroneously flagging TCP_ACCECN_ACE_FAIL_RECV in accecn_fail_mode.
Refer to Table 4 of RFC 9768 for more details."

Because RFC3168 ECN does not use this syn_ect_snt.

Thanks for reviewing. That wording is clearer and more precise.

I sent out v2 [1] yesterday before seeing your note, so once the 24-hour repost window passes I will send a v3 incorporating your clarification (assuming v2 hasn't already been picked up by then).

[1] https://lore.kernel.org/netdev/[email protected]/