FAILED: patch "[PATCH] tcp: do not coalesce segments after FIN" failed to apply to 6.12-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 6.12-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.12.y
git checkout FETCH_HEAD
git cherry-pick -x 1c46780dec48893b8c25fd5e33a3127e8dc39dd4
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026112553-cresting-chimp-b365@gregkh' --subject-prefix 'PATCH 6.12.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 1c46780dec48893b8c25fd5e33a3127e8dc39dd4 Mon Sep 17 00:00:00 2001
From: Hyunwoo Kim <[email protected]>
Date: Fri, 6 Nov 2026 21:25:21 +0900
Subject: [PATCH] tcp: do not coalesce segments after FIN
A FIN consumes a sequence number. If another FIN segment is coalesced into an skb that already carries FIN, end_seq advances to account for the additional FIN, but tcp_flags still contains only one FIN bit.
When the preceding gap is filled and tcp_ofo_queue() processes the coalesced skb, rcv_nxt advances to the skb's end_seq. When the receive path consumes all data in the skb, copied_seq advances by the data length and one sequence number for FIN. If three or more FIN segments have been coalesced into one skb, tcp_inq() can still return a positive value after the receive queue becomes empty and the SOCK_DONE adjustment is applied.
TCP_ZEROCOPY_RECEIVE is another consumer affected by this state. tcp_zerocopy_receive() takes its length from tcp_inq() and follows skb->next without checking for the end of the receive queue.
Software kTLS receive is one consumer affected by this state. When strp->copy_mode is false, tls_strp_msg_consume() calls the TCP helper tcp_read_done() to consume the current TLS record. In the reproducer, tcp_read_done() consumed the record data and one FIN before freeing the receive skb.
When kTLS looked for the next record, tcp_inq() reported at least TLS_HEADER_SIZE bytes even though the receive queue was empty. tcp_recv_skb() returned NULL, so tls_strp_load_anchor_with_queue() hit WARN_ON_ONCE() and returned without updating the anchor.
Since tcp_inq() had reported at least TLS_HEADER_SIZE, tls_strp_read_sock() proceeded to tls_strp_check_queue_ok(). skb_shinfo(strp->anchor)->frag_list still pointed to the freed receive skb. tls_strp_check_queue_ok() then read TCP_SKB_CB(first)->seq through the stale pointer, causing a use-after-free.
The reproducer followed this call path:
tls_rx_rec_done()
tls_strp_msg_consume()
tcp_read_done() // frees the receive skb
tls_strp_check_rcv()
tls_strp_read_sock()
tls_strp_load_anchor_with_queue() // returns after WARN_ON_ONCE()
tcp_recv_skb() == NULL
tls_strp_check_queue_ok()
TCP_SKB_CB(first)->seq // reads the freed receive skb
KASAN reported:
BUG: KASAN: slab-use-after-free in tls_strp_check_queue_ok.isra.0+0x5f/0x100 Read of size 4 at addr ffff888103169ca8 by task ktls-server/71 ... Call Trace: <TASK> ... tls_strp_check_queue_ok.isra.0+0x5f/0x100 tls_strp_check_rcv+0x2a9/0x400 ... tls_sw_recvmsg+0x3ce/0xa10 ... sock_recvmsg+0xea/0x100 __sys_recvfrom+0x142/0x1f0 ... __x64_sys_recvfrom+0x76/0x90 do_syscall_64+0xdd/0x4a0 entry_SYSCALL_64_after_hwframe+0x77/0x7f ... </TASK> ... Freed by task 71: ... napi_skb_cache_put+0x24/0xf0 skb_attempt_defer_free+0x183/0x3b0 tcp_read_done+0x2d4/0x340 tls_strp_msg_consume+0x45/0x80 tls_sw_recvmsg+0x3c4/0xa10 ...
In tcp_try_coalesce(), skip coalescing when the destination skb already carries FIN. tcp_add_backlog() calls skb_try_coalesce() directly instead of using this helper, so add the same check there. Since both paths check only the destination skb, a final segment carrying data and FIN can still be coalesced into an skb without FIN when the other coalescing checks pass.
net/ipv4/tcp_input.c +4 -0
@@ -5290,6 +5290,10 @@ static bool tcp_try_coalesce(struct sock *sk,if (!tcp_skb_can_collapse_rx (to, from))return false; +/* Nothing follows a FIN. */ +if (unlikely (TCP_SKB_CB (to)->tcp_flags &TCPHDR_FIN )) +return false; +if (!skb_try_coalesce (to, from, fragstolen, &delta))return false;
net/ipv4/tcp_ipv4.c +2 -0
@@ -1957,6 +1957,8 @@ enum skb_drop_reason tcp_add_backlog(struct sock *sk, struct sk_buff *skb) ((TCP_SKB_CB (tail)->tcp_flags ^TCP_SKB_CB (skb)->tcp_flags) & (TCPHDR_ECE |TCPHDR_CWR |TCPHDR_AE )) || +/* Nothing follows a FIN, see tcp_try_coalesce() */ + (TCP_SKB_CB (tail)->tcp_flags &TCPHDR_FIN ) || !tcp_skb_can_collapse_rx (tail, skb) || thtail->doff != th->doff || memcmp(thtail + 1, th + 1, hdrlen - sizeof(*th)) ||
Sasha Levin
maintainerPATCH 1/4 tcp: use BIT() macro in include/net/tcp.h
From: Chia-Yu Chang <[email protected]>
[ Upstream commit 0114a91da67263c56612ff19a6aa0992466e7756 ]
Use BIT() macro for TCP flags field and TCP congestion control flags that will be used by the congestion control algorithm.
No functional changes.
Stable-dep-of: 1c46780dec48 ("tcp: do not coalesce segments after FIN")
--- include/net/tcp.h | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-)
include/net/tcp.h +11 -10
@@ -26,6 +26,7 @@#include <linux/kref.h> #include <linux/ktime.h> #include <linux/indirect_call_wrapper.h> +#include <linux/bits.h> #include <net/inet_connection_sock.h> #include <net/inet_timewait_sock.h>
@@ -911,14 +912,14 @@ static inline u32 tcp_rsk_tsval(const struct tcp_request_sock *treq)#define tcp_flag_byte (th) (((u_int8_t *)th)[13]) -#define TCPHDR_FIN 0x01 -#define TCPHDR_SYN 0x02 -#define TCPHDR_RST 0x04 -#define TCPHDR_PSH 0x08 -#define TCPHDR_ACK 0x10 -#define TCPHDR_URG 0x20 -#define TCPHDR_ECE 0x40 -#define TCPHDR_CWR 0x80 +#define TCPHDR_FIN BIT(0) +#define TCPHDR_SYN BIT(1) +#define TCPHDR_RST BIT(2) +#define TCPHDR_PSH BIT(3) +#define TCPHDR_ACK BIT(4) +#define TCPHDR_URG BIT(5) +#define TCPHDR_ECE BIT(6) +#define TCPHDR_CWR BIT(7)#define TCPHDR_SYN_ECN (TCPHDR_SYN | TCPHDR_ECE | TCPHDR_CWR)
@@ -1110,9 +1111,9 @@ enum tcp_ca_ack_event_flags {#define TCP_CA_UNSPEC 0/* Algorithm can be set on socket without CAP_NET_ADMIN privileges */ -#define TCP_CONG_NON_RESTRICTED 0x1 +#define TCP_CONG_NON_RESTRICTED BIT(0)/* Requires ECN/ECT set on all packets */ -#define TCP_CONG_NEEDS_ECN 0x2 +#define TCP_CONG_NEEDS_ECN BIT(1)#define TCP_CONG_MASK (TCP_CONG_NON_RESTRICTED | TCP_CONG_NEEDS_ECN)union tcp_cc_info ;
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/4 tcp: extend TCP flags to allow AE bit/ACE field
From: Ilpo Järvinen <[email protected]>
[ Upstream commit 2c2f08d31d2f6cd5621b121d596986bd54da679a ]
With AccECN, there's one additional TCP flag to be used (AE) and ACE field that overloads the definition of AE, CWR, and ECE flags. As tcp_flags was previously only 1 byte, the byte-order stuff needs to be added to it's handling.
Stable-dep-of: 1c46780dec48 ("tcp: do not coalesce segments after FIN")
--- include/net/tcp.h | 11 +++++++++-- include/uapi/linux/tcp.h | 9 ++++++--- net/ipv4/tcp_ipv4.c | 2 +- net/ipv4/tcp_output.c | 8 ++++---- net/ipv6/tcp_ipv6.c | 2 +- net/netfilter/nf_log_syslog.c | 8 +++++--- 6 files changed, 26 insertions(+), 14 deletions(-)
include/net/tcp.h +9 -2
@@ -920,7 +920,14 @@ static inline u32 tcp_rsk_tsval(const struct tcp_request_sock *treq)#define TCPHDR_URG BIT(5)#define TCPHDR_ECE BIT(6)#define TCPHDR_CWR BIT(7) - +#define TCPHDR_AE BIT(8) +#define TCPHDR_FLAGS_MASK (TCPHDR_FIN | TCPHDR_SYN | TCPHDR_RST | \ + TCPHDR_PSH | TCPHDR_ACK | TCPHDR_URG | \ + TCPHDR_ECE | TCPHDR_CWR | TCPHDR_AE) +#define tcp_flags_ntohs (th) (ntohs(*(__be16 *)&tcp_flag_word(th)) & \ + TCPHDR_FLAGS_MASK) + +#define TCPHDR_ACE (TCPHDR_ECE | TCPHDR_CWR | TCPHDR_AE)#define TCPHDR_SYN_ECN (TCPHDR_SYN | TCPHDR_ECE | TCPHDR_CWR)/* State flags for sacked in struct tcp_skb_cb */
@@ -958,7 +965,7 @@ struct tcp_skb_cb {u16 tcp_gso_size; }; }; -__u8 tcp_flags;/* TCP header flags. (tcp[13]) */ +__u16 tcp_flags;/* TCP header flags (tcp[12-13])*/ __u8 sacked;/* State flags for SACK. */ __u8 ip_dsfield;/* IPv4 tos or IPv6 dsfield */
include/uapi/linux/tcp.h +6 -3
@@ -28,7 +28,8 @@ struct tcphdr {__be32 seq;__be32 ack_seq;#if defined(__LITTLE_ENDIAN_BITFIELD) - __u16 res1:4, + __u16 ae:1, + res1:3, doff:4, fin:1, syn:1,
@@ -40,7 +41,8 @@ struct tcphdr { cwr:1 ;#elif defined(__BIG_ENDIAN_BITFIELD) __u16 doff:4, - res1:4, + res1:3, + ae:1, cwr:1, ece:1, urg:1,
@@ -70,6 +72,7 @@ union tcp_word_hdr {#define tcp_flag_word (tp) (((union tcp_word_hdr *)(tp))->words[3])enum { +TCP_FLAG_AE =__constant_cpu_to_be32 (0x01000000 ),TCP_FLAG_CWR =__constant_cpu_to_be32 (0x00800000 ),TCP_FLAG_ECE =__constant_cpu_to_be32 (0x00400000 ),TCP_FLAG_URG =__constant_cpu_to_be32 (0x00200000 ),
@@ -78,7 +81,7 @@ enum {TCP_FLAG_RST =__constant_cpu_to_be32 (0x00040000 ),TCP_FLAG_SYN =__constant_cpu_to_be32 (0x00020000 ),TCP_FLAG_FIN =__constant_cpu_to_be32 (0x00010000 ), -TCP_RESERVED_BITS =__constant_cpu_to_be32 (0x0F000000 ), +TCP_RESERVED_BITS =__constant_cpu_to_be32 (0x0E000000 ), TCP_DATA_OFFSET = __constant_cpu_to_be32(0xF0000000) };
net/ipv4/tcp_ipv4.c +1 -1
@@ -2167,7 +2167,7 @@ static void tcp_v4_fill_cb(struct sk_buff *skb, const struct iphdr *iph,TCP_SKB_CB (skb)->end_seq = (TCP_SKB_CB (skb)->seq + th->syn + th->fin + skb->len - th->doff *4 );TCP_SKB_CB (skb)->ack_seq =ntohl (th->ack_seq); -TCP_SKB_CB (skb)->tcp_flags =tcp_flag_byte (th); +TCP_SKB_CB (skb)->tcp_flags =tcp_flags_ntohs (th);TCP_SKB_CB (skb)->ip_dsfield =ipv4_get_dsfield (iph);TCP_SKB_CB (skb)->sacked =0 ; TCP_SKB_CB(skb)->has_rxtstamp =
net/ipv4/tcp_output.c +4 -4
@@ -403,7 +403,7 @@ static void tcp_ecn_send(struct sock *sk, struct sk_buff *skb,/* Constructs common control bits of non-data skb. If SYN/FIN is present, * auto increment end seqno. */ -static void tcp_init_nondata_skb (struct sk_buff *skb,u32 seq,u8 flags) +static void tcp_init_nondata_skb (struct sk_buff *skb,u32 seq,u16 flags) { skb->ip_summed =CHECKSUM_PARTIAL ;
@@ -1387,7 +1387,7 @@ static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, th->seq =htonl (tcb->seq); th->ack_seq =htonl (rcv_nxt); *(((__be16 *)th) +6 ) =htons (((tcp_header_size >>2 ) <<12 ) | - tcb->tcp_flags); + (tcb->tcp_flags &TCPHDR_FLAGS_MASK )); th->check =0 ; th->urg_ptr =0 ;
@@ -1610,8 +1610,8 @@ int tcp_fragment(struct sock *sk, enum tcp_queue tcp_queue,struct sk_buff *buff;int old_factor;long limit; +u16 flags;int nlen; -u8 flags;if (WARN_ON (len > skb->len))return -EINVAL ;
@@ -2166,7 +2166,7 @@ static int tso_fragment(struct sock *sk, struct sk_buff *skb, unsigned int len, {int nlen = skb->len - len;struct sk_buff *buff; -u8 flags; +u16 flags;/* All of a TSO frame must be composed of paged data. */ DEBUG_NET_WARN_ON_ONCE (skb->len != skb->data_len);
net/ipv6/tcp_ipv6.c +1 -1
@@ -1717,7 +1717,7 @@ static void tcp_v6_fill_cb(struct sk_buff *skb, const struct ipv6hdr *hdr,TCP_SKB_CB (skb)->end_seq = (TCP_SKB_CB (skb)->seq + th->syn + th->fin + skb->len - th->doff*4 );TCP_SKB_CB (skb)->ack_seq =ntohl (th->ack_seq); -TCP_SKB_CB (skb)->tcp_flags =tcp_flag_byte (th); +TCP_SKB_CB (skb)->tcp_flags =tcp_flags_ntohs (th);TCP_SKB_CB (skb)->ip_dsfield =ipv6_get_dsfield (hdr);TCP_SKB_CB (skb)->sacked =0 ; TCP_SKB_CB(skb)->has_rxtstamp =
net/netfilter/nf_log_syslog.c +5 -3
@@ -219,7 +219,9 @@ nf_log_dump_tcp_header(struct nf_log_buf *m,/* Max length: 9 "RES=0x3C " */ nf_log_buf_add (m,"RES=0x%02x " , (u_int8_t)(ntohl (tcp_flag_word (th) &TCP_RESERVED_BITS ) >>22 )); -/* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */ +/* Max length: 35 "AE CWR ECE URG ACK PSH RST SYN FIN " */ +if (th->ae) +nf_log_buf_add (m,"AE " );if (th->cwr)nf_log_buf_add (m,"CWR " );if (th->ece)
@@ -519,7 +521,7 @@ dump_ipv4_packet(struct net *net, struct nf_log_buf *m,/* Proto Max log string length */ /* IP: 40+46+6+11+127 = 230 */ -/* TCP: 10+max(25,20+30+13+9+32+11+127) = 252 */ +/* TCP: 10+max(25,20+30+13+9+35+11+127) = 255 */ /* UDP: 10+max(25,20) = 35 */ /* UDPLITE: 14+max(25,20) = 39 */ /* ICMP: 11+max(25, 18+25+max(19,14,24+3+n+10,3+n+10)) = 91+n */
@@ -529,7 +531,7 @@ dump_ipv4_packet(struct net *net, struct nf_log_buf *m,/* (ICMP allows recursion one level deep) */ /* maxlen = IP + ICMP + IP + max(TCP,UDP,ICMP,unknown) */ -/* maxlen = 230+ 91 + 230 + 252 = 803 */ +/* maxlen = 230+ 91 + 230 + 255 = 806 */ }static noinline_for_stack void
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/4 tcp: AccECN support to tcp_add_backlog
In reply to Sasha LevinFrom: Ilpo Järvinen <[email protected]>
[ Upstream commit d722762c4eaa918d3d2f78b41cca3f480a616d65 ]
AE flag needs to be preserved for AccECN.
Stable-dep-of: 1c46780dec48 ("tcp: do not coalesce segments after FIN")
--- net/ipv4/tcp_ipv4.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
net/ipv4/tcp_ipv4.c +2 -1
@@ -2059,7 +2059,8 @@ bool tcp_add_backlog(struct sock *sk, struct sk_buff *skb, !((TCP_SKB_CB (tail)->tcp_flags &TCP_SKB_CB (skb)->tcp_flags) &TCPHDR_ACK ) || ((TCP_SKB_CB (tail)->tcp_flags ^ -TCP_SKB_CB (skb)->tcp_flags) & (TCPHDR_ECE |TCPHDR_CWR )) || +TCP_SKB_CB (skb)->tcp_flags) & + (TCPHDR_ECE |TCPHDR_CWR |TCPHDR_AE )) || !tcp_skb_can_collapse_rx (tail, skb) || thtail->doff != th->doff || memcmp(thtail + 1, th + 1, hdrlen - sizeof(*th)))
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 4/4 tcp: do not coalesce segments after FIN
In reply to Sasha LevinFrom: Hyunwoo Kim <[email protected]>
[ Upstream commit 1c46780dec48893b8c25fd5e33a3127e8dc39dd4 ]
A FIN consumes a sequence number. If another FIN segment is coalesced into an skb that already carries FIN, end_seq advances to account for the additional FIN, but tcp_flags still contains only one FIN bit.
When the preceding gap is filled and tcp_ofo_queue() processes the coalesced skb, rcv_nxt advances to the skb's end_seq. When the receive path consumes all data in the skb, copied_seq advances by the data length and one sequence number for FIN. If three or more FIN segments have been coalesced into one skb, tcp_inq() can still return a positive value after the receive queue becomes empty and the SOCK_DONE adjustment is applied.
TCP_ZEROCOPY_RECEIVE is another consumer affected by this state. tcp_zerocopy_receive() takes its length from tcp_inq() and follows skb->next without checking for the end of the receive queue.
Software kTLS receive is one consumer affected by this state. When strp->copy_mode is false, tls_strp_msg_consume() calls the TCP helper tcp_read_done() to consume the current TLS record. In the reproducer, tcp_read_done() consumed the record data and one FIN before freeing the receive skb.
When kTLS looked for the next record, tcp_inq() reported at least TLS_HEADER_SIZE bytes even though the receive queue was empty. tcp_recv_skb() returned NULL, so tls_strp_load_anchor_with_queue() hit WARN_ON_ONCE() and returned without updating the anchor.
Since tcp_inq() had reported at least TLS_HEADER_SIZE, tls_strp_read_sock() proceeded to tls_strp_check_queue_ok(). skb_shinfo(strp->anchor)->frag_list still pointed to the freed receive skb. tls_strp_check_queue_ok() then read TCP_SKB_CB(first)->seq through the stale pointer, causing a use-after-free.
The reproducer followed this call path:
tls_rx_rec_done()
tls_strp_msg_consume()
tcp_read_done() // frees the receive skb
tls_strp_check_rcv()
tls_strp_read_sock()
tls_strp_load_anchor_with_queue() // returns after WARN_ON_ONCE()
tcp_recv_skb() == NULL
tls_strp_check_queue_ok()
TCP_SKB_CB(first)->seq // reads the freed receive skb
KASAN reported:
BUG: KASAN: slab-use-after-free in tls_strp_check_queue_ok.isra.0+0x5f/0x100 Read of size 4 at addr ffff888103169ca8 by task ktls-server/71 ... Call Trace: <TASK> ... tls_strp_check_queue_ok.isra.0+0x5f/0x100 tls_strp_check_rcv+0x2a9/0x400 ... tls_sw_recvmsg+0x3ce/0xa10 ... sock_recvmsg+0xea/0x100 __sys_recvfrom+0x142/0x1f0 ... __x64_sys_recvfrom+0x76/0x90 do_syscall_64+0xdd/0x4a0 entry_SYSCALL_64_after_hwframe+0x77/0x7f ... </TASK> ... Freed by task 71: ... napi_skb_cache_put+0x24/0xf0 skb_attempt_defer_free+0x183/0x3b0 tcp_read_done+0x2d4/0x340 tls_strp_msg_consume+0x45/0x80 tls_sw_recvmsg+0x3c4/0xa10 ...
In tcp_try_coalesce(), skip coalescing when the destination skb already carries FIN. tcp_add_backlog() calls skb_try_coalesce() directly instead of using this helper, so add the same check there. Since both paths check only the destination skb, a final segment carrying data and FIN can still be coalesced into an skb without FIN when the other coalescing checks pass.
--- net/ipv4/tcp_input.c | 4 ++++ net/ipv4/tcp_ipv4.c | 2 ++ 2 files changed, 6 insertions(+)
net/ipv4/tcp_input.c +4 -0
@@ -4943,6 +4943,10 @@ static bool tcp_try_coalesce(struct sock *sk,if (!tcp_skb_can_collapse_rx (to, from))return false; +/* Nothing follows a FIN. */ +if (unlikely (TCP_SKB_CB (to)->tcp_flags &TCPHDR_FIN )) +return false; +if (!skb_try_coalesce (to, from, fragstolen, &delta))return false;
net/ipv4/tcp_ipv4.c +2 -0
@@ -2061,6 +2061,8 @@ bool tcp_add_backlog(struct sock *sk, struct sk_buff *skb, ((TCP_SKB_CB (tail)->tcp_flags ^TCP_SKB_CB (skb)->tcp_flags) & (TCPHDR_ECE |TCPHDR_CWR |TCPHDR_AE )) || +/* Nothing follows a FIN, see tcp_try_coalesce() */ + (TCP_SKB_CB (tail)->tcp_flags &TCPHDR_FIN ) || !tcp_skb_can_collapse_rx (tail, skb) || thtail->doff != th->doff || memcmp(thtail + 1, th + 1, hdrlen - sizeof(*th)))
Signature · 1 line
2.53.0