Skip to contentSkip to conversation

tcp: initialize missing request_sock fields in syncookie path

PATCH Applied Thaison Phan 3 participants 5 messages Nov 18 – Nov 20

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

v4 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() for kernel-generated cookies (!req->syncookie), 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.

v4:
- Patch 2: restore treq->syn_ect_snt = 0 in cookie_tcp_reqsk_init() and guard syn_ect_snt updates in cookie_{v4,v6}_check() with !req->syncookie for BPF custom syncookies; document in changelog (Sashiko, Eric Dumazet)
- Patch 2: add Reviewed-by tag from Matthieu Baerts
v3: https://lore.kernel.org/netdev/[email protected]/
- Patch 2: clarify syn_ect_snt / tcp_accecn_third_ack() description for AccECN connections and reference RFC 9768 Table 4
(Chia-Yu Chang)
v2: https://lore.kernel.org/netdev/[email protected]/
- Patch 1: clarify how KASAN (via SLUB_RCU_DEBUG) exposed the bug, explain how it was observed, and include the inet_sock_set_state trace line (Eric Dumazet)
- Patch 2: drop redundant treq->syn_ect_snt = 0 in cookie_tcp_reqsk_init() and clarify how the bug was found and observed via TCP_INFO (Eric Dumazet)
- Add Eric Dumazet's Reviewed-by tags
v1: https://lore.kernel.org/netdev/[email protected]/

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 | 7 +++++++
 net/ipv6/syncookies.c | 6 ++++++
 2 files changed, 13 insertions(+)

base-commit: 2a324a58a084c5cfd7e5a2f55e0786bee170a4a5

Signature · 1 line
2.56.1.831.g720f3f5937-goog
#1 lore raw

PATCH v4 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.

This was originally noticed via an out-of-tree socket hook observing inet_sk(sk)->inet_saddr != LOOPBACK4_IPV6 on KASAN test runs. KASAN enables SLUB_RCU_DEBUG by default, introducing an RCU grace period that prevents cookie_v6_check() from reusing the warm request_sock initialized during the initial SYN. Upstream, with tcp_syncookies=2, SLUB_RCU_DEBUG=y, and slub_debug=P, sock:inet_sock_set_state shows slab poison instead of 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

Initialize ireq->ir_rmt_addr and ireq->ir_loc_addr to LOOPBACK4_IPV6 in cookie_v6_check() to match tcp_v6_init_req(). Tested against a QEMU reproducer checking inet_sock_set_state and IP_OPTIONS, as well as the packetdrill syncookie selftests.

Fixes a3a128f611a9 ("inet: consolidate inet_csk_clone_lock()")

---
v4:
- no changes
v3: https://lore.kernel.org/netdev/[email protected]/
- no changes
v2: https://lore.kernel.org/netdev/[email protected]/
- clarify how KASAN (via SLUB_RCU_DEBUG) exposed the uninitialized request_sock, how the issue was observed, and include the inet_sock_set_state trace line (Eric Dumazet)
- add Reviewed-by tag from Eric Dumazet
v1: https://lore.kernel.org/netdev/[email protected]/

 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.1.831.g720f3f5937-goog
#2 lore raw

PATCH v4 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. 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.

Initialize the AccECN fields in cookie_tcp_reqsk_init() to match tcp_openreq_init(). For kernel-generated cookies (!req->syncookie), set syn_ect_snt from the listener's TOS/tclass in cookie_{v4,v6}_check() to match tcp_{v4,v6}_send_synack(); for BPF custom syncookies (req->syncookie), leave syn_ect_snt at 0 (Not-ECT) since the SYN-ACK was built by the BPF program rather than the kernel and bpf_tcp_req_attrs does not report the SYN-ACK ECN codepoint.

Found by AI-assisted code inspection comparing tcp_openreq_init() and cookie_tcp_reqsk_init() while investigating the cookie_v6_check() LOOPBACK4_IPV6 bug. Reproduced in a QEMU VM with tcp_syncookies=2, tcp_ecn=3, SLUB_RCU_DEBUG=y, and slub_debug=P, observing bogus tcpi_accecn_fail_mode and tcpi_accecn_opt_seen values in TCP_INFO on the accepted child socket (and tcpi_accecn_fail_mode=0x2 on ECT(0) listeners), and tested with packetdrill AccECN and syncookie selftests.

Fixes 3cae34274c79 ("tcp: accecn: AccECN negotiation")
Fixes b40671b5ee58 ("tcp: accecn: AccECN option failure handling")

---
v4:
- restore treq->syn_ect_snt = 0 in cookie_tcp_reqsk_init() and guard syn_ect_snt updates in cookie_{v4,v6}_check() with !req->syncookie for BPF custom syncookies; document in changelog (Sashiko,
Eric Dumazet)
- add Reviewed-by tag from Matthieu Baerts
v3: https://lore.kernel.org/netdev/[email protected]/
- clarify syn_ect_snt / tcp_accecn_third_ack() description for AccECN connections and reference RFC 9768 Table 4 (Chia-Yu Chang)
v2: https://lore.kernel.org/netdev/[email protected]/
- drop redundant treq->syn_ect_snt = 0 in cookie_tcp_reqsk_init() (Eric Dumazet)
- clarify how the bug was discovered and observed via TCP_INFO
(Eric Dumazet)
- add Reviewed-by tag from Eric Dumazet
v1: https://lore.kernel.org/netdev/[email protected]/

 net/ipv4/syncookies.c | 7 +++++++
 net/ipv6/syncookies.c | 3 +++
 2 files changed, 10 insertions(+)
net/ipv4/syncookies.c +7 -0
index 4e0ff3b82062..89169c82431c 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,9 @@ 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);
+	/* SYN-ACK was not sent by the kernel if req->syncookie is set. */
+	if (!req->syncookie)
+		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 +3 -0
index b33011b71fff..e8d45a627ca2 100644
@@ -276,6 +276,9 @@ 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);
+	/* SYN-ACK was not sent by the kernel if req->syncookie is set. */
+	if (!req->syncookie)
+		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.1.831.g720f3f5937-goog
maintainer Reviewed-by on the v4 series
#3 lore raw
In reply to Thaison Phan
Quoted 12 lines · Thaison Phan

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() for kernel-generated cookies (!req->syncookie), matching tcp_openreq_init() and tcp_{v4,v6}_send_synack().

Quoted 3 lines

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

Quoted 33 lines

v4:
- Patch 2: restore treq->syn_ect_snt = 0 in cookie_tcp_reqsk_init() and guard syn_ect_snt updates in cookie_{v4,v6}_check() with !req->syncookie for BPF custom syncookies; document in changelog (Sashiko, Eric Dumazet)
- Patch 2: add Reviewed-by tag from Matthieu Baerts
v3: https://lore.kernel.org/netdev/[email protected]/
- Patch 2: clarify syn_ect_snt / tcp_accecn_third_ack() description for AccECN connections and reference RFC 9768 Table 4
(Chia-Yu Chang)
v2: https://lore.kernel.org/netdev/[email protected]/
- Patch 1: clarify how KASAN (via SLUB_RCU_DEBUG) exposed the bug, explain how it was observed, and include the inet_sock_set_state trace line (Eric Dumazet)
- Patch 2: drop redundant treq->syn_ect_snt = 0 in cookie_tcp_reqsk_init() and clarify how the bug was found and observed via TCP_INFO (Eric Dumazet)
- Add Eric Dumazet's Reviewed-by tags
v1: https://lore.kernel.org/netdev/[email protected]/

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 | 7 +++++++
 net/ipv6/syncookies.c | 6 ++++++
 2 files changed, 13 insertions(+)

base-commit: 2a324a58a084c5cfd7e5a2f55e0786bee170a4a5 --
2.56.1.831.g720f3f5937-goog

#4 lore raw
In reply to Thaison Phan
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 4 lines · On Wed, 18 Nov 2026 21:09:31 +0000 you wrote:

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:

Quoted 8 lines

- 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() for kernel-generated cookies (!req->syncookie), matching tcp_openreq_init() and tcp_{v4,v6}_send_synack().

[...]

Here is the summary with links:
- [net,v4,1/2] tcp: initialize ir_rmt_addr and ir_loc_addr in cookie_v6_check()

    https://git.kernel.org/netdev/net/c/7411fb192907
  - [net,v4,2/2] tcp: accecn: initialize AccECN fields in syncookie path
    https://git.kernel.org/netdev/net/c/ce28b1a4d137

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html