sctp: fix a vtag verification failure caused by stale INITs
Subsystems (4)
Xin Long
maintainercover sctp: fix a vtag verification failure caused by stale INITs
Similar to Scenario B in commit 8e56b063c865 ( netfilter: handle the connecting collision properly in nf_conntrack_proto_sctp"):
Scenario B: INIT_ACK is delayed until the peer completes its own handshake
192.168.1.2 > 192.168.1.1: sctp (1) [INIT] [init tag: 3922216408]
192.168.1.1 > 192.168.1.2: sctp (1) [INIT] [init tag: 144230885]
192.168.1.2 > 192.168.1.1: sctp (1) [INIT ACK] [init tag: 3922216408]
192.168.1.1 > 192.168.1.2: sctp (1) [COOKIE ECHO]
192.168.1.2 > 192.168.1.1: sctp (1) [COOKIE ACK]192.168.1.1 > 192.168.1.2: sctp (1) [INIT ACK] [init tag: 3914796021] *
There is another case:
Scenario F: INIT is delayed until the peer completes its own handshake
192.168.1.2 > 192.168.1.1: sctp (1) [INIT] [init tag: 3922216408] (OVS upcall)
192.168.1.1 > 192.168.1.2: sctp (1) [INIT] [init tag: 144230885]
192.168.1.2 > 192.168.1.1: sctp (1) [INIT ACK] [init tag: 3922216408]
192.168.1.1 > 192.168.1.2: sctp (1) [COOKIE ECHO]
192.168.1.2 > 192.168.1.1: sctp (1) [COOKIE ACK] 192.168.1.2 > 192.168.1.1: sctp (1) [INIT] [init tag: 3922216408] (delayed)
192.168.1.1 > 192.168.1.2: sctp (1) [INIT ACK] [init tag: 3914796021] *
In this case, the delayed INIT (e.g. due to OVS upcall) is recorded by conntrack, which prevents vtag verification from dropping the unexpected INIT-ACK in nf_conntrack_sctp_packet():
vtag = ct->proto.sctp.vtag[!dir];
if (!ct->proto.sctp.init[!dir] && vtag && vtag != ih->init_tag)
goto out_unlock;This happens because ct->proto.sctp.init[!dir] is set by the delayed INIT, even though it is stale.
Fix this in two parts:
- In netfilter: Do not record INITs whose init_tag matches the peer vtag, as they carry no new handshake state in the 1st patch.
- In SCTP: Prevent endpoints from responding to such INITs with INIT-ACK, ensuring correctness even when middleboxes lack the netfilter fix in the 2nd patch.
A follow-up selftest for this scenario will be posted in a separate patch by Yi Chen.
Xin Long (2):
netfilter: skip recording stale or retransmitted INIT
sctp: discard stale INIT after handshake completion
net/netfilter/nf_conntrack_proto_sctp.c | 10 +++++++--- net/sctp/sm_statefuns.c | 6 ++++++ 2 files changed, 13 insertions(+), 3 deletions(-)
Signature · 1 line
2.47.1
Xin Long
maintainerPATCH 1/2 netfilter: skip recording stale or retransmitted INIT
An INIT whose init_tag matches the peer's vtag does not provide new state information. It indicates either:
- a stale INIT (after INIT-ACK has already been seen on the same side), or
- a retransmitted INIT (after INIT has already been recorded on the same side).
In both cases, the INIT must not update ct->proto.sctp.init[] state, since it does not advance the handshake tracking and may otherwise corrupt INIT/INIT-ACK validation logic.
Allow INIT processing only when the conntrack entry is newly created (SCTP_CONNTRACK_NONE), or when the init_tag differs from the stored peer vtag.
Note it skips the check for the ct with old_state SCTP_CONNTRACK_NONE in nf_conntrack_sctp_packet(), as it is just created in sctp_new() where it set ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = ih->init_tag.
--- net/netfilter/nf_conntrack_proto_sctp.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
net/netfilter/nf_conntrack_proto_sctp.c +7 -3
@@ -466,9 +466,13 @@ int nf_conntrack_sctp_packet(struct nf_conn *ct,if (!ih) goto out_unlock; -if (ct->proto.sctp.init[dir] && ct->proto.sctp.init[!dir]) - ct->proto.sctp.init[!dir] =0 ; - ct->proto.sctp.init[dir] =1 ; +/* Do not record INIT matching peer vtag (stale or retransmitted INIT). */ +if (old_state ==SCTP_CONNTRACK_NONE || + ct->proto.sctp.vtag[!dir] != ih->init_tag) { +if (ct->proto.sctp.init[dir] && ct->proto.sctp.init[!dir]) + ct->proto.sctp.init[!dir] =0 ; + ct->proto.sctp.init[dir] =1 ; + }pr_debug ("Setting vtag %x for dir %d\n" , ih->init_tag, !dir); ct->proto.sctp.vtag[!dir] = ih->init_tag;
Signature · 1 line
2.47.1
Florian Westphal
maintainerRe: [PATCH net 1/2] netfilter: skip recording stale or retransmitted INIT
Quoted 5 lines · Xin Long
net/netfilter/nf_conntrack_proto_sctp.c +0
-0
net/netfilter/nf_conntrack_proto_sctp.c int nf_conntrack_sctp_packet(struct nf_conn *ct, · line 471
Quoted 9 lines
net/netfilter/nf_conntrack_proto_sctp.c +3
-3@@ -466,9 +466,13 @@ int nf_conntrack_sctp_packet(struct nf_conn *ct,if (!ih) goto out_unlock; -if (ct->proto.sctp.init[dir] && ct->proto.sctp.init[!dir]) - ct->proto.sctp.init[!dir] =0 ; - ct->proto.sctp.init[dir] =1 ; +/* Do not record INIT matching peer vtag (stale or retransmitted INIT). */ +if (old_state ==SCTP_CONNTRACK_NONE || + ct->proto.sctp.vtag[!dir] != ih->init_tag) {
Should ct->proto.sctp.vtag[!dir] == ih->init_tag case also
set ignore = true?
Xin Long
maintainerRe: [PATCH net 1/2] netfilter: skip recording stale or retransmitted INIT
Quoted 11 lines · Florian Westphal
Quoted 8 lines · Xin Long
net/netfilter/nf_conntrack_proto_sctp.c +0
-0@@ -466,9 +466,13 @@ int nf_conntrack_sctp_packet(struct nf_conn *ct,if (!ih) goto out_unlock;
Quoted 9 lines
net/netfilter/nf_conntrack_proto_sctp.c int nf_conntrack_sctp_packet(struct nf_conn *ct, · line 471
Quoted 6 lines
net/netfilter/nf_conntrack_proto_sctp.c +3
-3-if (ct->proto.sctp.init[dir] && ct->proto.sctp.init[!dir]) - ct->proto.sctp.init[!dir] =0 ; - ct->proto.sctp.init[dir] =1 ; +/* Do not record INIT matching peer vtag (stale or retransmitted INIT). */ +if (old_state ==SCTP_CONNTRACK_NONE || + ct->proto.sctp.vtag[!dir] != ih->init_tag) {Should ct->proto.sctp.vtag[!dir] ==
ih->init_tagcase also
set ignore = true?
It should for a stale INIT, but not for a retransmitted one. At this point, though, we don't reliably distinguish between the two.
Also, as this patch only aims to prevent updating ct->proto.sctp.init[] (introduced in 8e56b063c865) in this scenario, it’s safer to avoid changing other behavior.
Thanks.
Re: [PATCH net 1/2] netfilter: skip recording stale or retransmitted INIT
Quoted 21 lines · Xin Long
Quoted 10 lines · Florian Westphal
Quoted 8 lines · Xin Long
net/netfilter/nf_conntrack_proto_sctp.c +0
-0@@ -466,9 +466,13 @@ int nf_conntrack_sctp_packet(struct nf_conn *ct,if (!ih) goto out_unlock;Quoted 9 lines
net/netfilter/nf_conntrack_proto_sctp.c int nf_conntrack_sctp_packet(struct nf_conn *ct, · line 471
Quoted 6 lines
net/netfilter/nf_conntrack_proto_sctp.c +3
-3-if (ct->proto.sctp.init[dir] && ct->proto.sctp.init[!dir]) - ct->proto.sctp.init[!dir] =0 ; - ct->proto.sctp.init[dir] =1 ; +/* Do not record INIT matching peer vtag (stale or retransmitted INIT). */ +if (old_state ==SCTP_CONNTRACK_NONE || + ct->proto.sctp.vtag[!dir] != ih->init_tag) {Should ct->proto.sctp.vtag[!dir] ==
ih->init_tagcase also
set ignore = true?
Quoted 6 lines
It should for a stale INIT, but not for a retransmitted one. At this point, though, we don't reliably distinguish between the two.
Also, as this patch only aims to prevent updating ct->proto.sctp.init[] (introduced in
8e56b063c865) in this scenario, it’s safer to avoid changing other behavior.
Alright. I'm fine with this going in via net directly:
Re: [PATCH net 1/2] netfilter: skip recording stale or retransmitted INIT
In reply to Xin LongQuoted 15 lines · Xin Long
An INIT whose
init_tagmatches the peer's vtag does not provide new state information. It indicates either:- a stale INIT (after INIT-ACK has already been seen on the same side), or
- a retransmitted INIT (after INIT has already been recorded on the same side).In both cases, the INIT must not update ct->proto.sctp.init[] state, since it does not advance the handshake tracking and may otherwise corrupt INIT/INIT-ACK validation logic.
Allow INIT processing only when the conntrack entry is newly created (
SCTP_CONNTRACK_NONE), or when theinit_tagdiffers from the stored peer vtag.
Quoted 6 lines
Note it skips the check for the ct with
old_stateSCTP_CONNTRACK_NONEinnf_conntrack_sctp_packet(), as it is just created insctp_new()where it set ct->proto.sctp.vtag[IP_CT_DIR_REPLY] =ih->init_tag.Fixes 9fb9cbb1082d ("[NETFILTER]: Add nf_conntrack subsystem.")Signed-off-by Xin Long <[email protected]>
Xin Long
maintainerPATCH 2/2 sctp: discard stale INIT after handshake completion
In reply to Xin LongAfter an association reaches ESTABLISHED, the peer’s init_tag is already known from the handshake. Any subsequent INIT with the same init_tag is not a valid restart, but a delayed or duplicate INIT.
Drop such INIT chunks in sctp_sf_do_unexpected_init() instead of processing them as new association attempts.
--- net/sctp/sm_statefuns.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/sctp/sm_statefuns.c +6 -0
@@ -1556,6 +1556,12 @@ static enum sctp_disposition sctp_sf_do_unexpected_init(/* Tag the variable length parameters. */ chunk->param_hdr.v =skb_pull (chunk->skb,sizeof (struct sctp_inithdr )); +if (asoc->state >=SCTP_STATE_ESTABLISHED ) { +/* Discard INIT matching peer vtag after handshake completion (stale INIT). */ +if (chunk->subh.init_hdr->init_tag == asoc->peer.i.init_tag) +return sctp_sf_pdiscard (net, ep, asoc, type, arg, commands); + } +/* Verify the INIT chunk before processing it. */ err_chunk =NULL ; if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Signature · 1 line
2.47.1
Re: [PATCH net 2/2] sctp: discard stale INIT after handshake completion
Quoted 9 lines · Xin Long
After an association reaches ESTABLISHED, the peer’s
init_tagis already known from the handshake. Any subsequent INIT with the sameinit_tagis not a valid restart, but a delayed or duplicate INIT.Drop such INIT chunks in
sctp_sf_do_unexpected_init()instead of processing them as new association attempts.Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Signed-off-by Xin Long <[email protected]>
Xin Long
maintainerRe: [PATCH net 2/2] sctp: discard stale INIT after handshake completion
In reply to Xin LongQuoted 8 lines
This is an AI-generated review of your patch from:
https://sashiko.dev/#/patchset/cover.1775847557.git.lucien.xin%40gmail.com
Quoted 4 lines
net/sctp/sm_statefuns.c +0
-0@@ -1556,6 +1556,12 @@ static enum sctp_disposition sctp_sf_do_unexpected_init(/* Tag the variable length parameters. */ chunk->param_hdr.v =skb_pull (chunk->skb,sizeof (struct sctp_inithdr ));
Quoted 11 lines
net/sctp/sm_statefuns.c static enum sctp_disposition sctp_sf_do_unexpected_init( · line 1561
Quoted 3 lines
net/sctp/sm_statefuns.c +3
-0+if (asoc->state >=SCTP_STATE_ESTABLISHED ) { +/* Discard INIT matching peer vtag after handshake completion (stale INIT). */ +if (chunk->subh.init_hdr->init_tag == asoc->peer.i.init_tag)Does this code compare a network-byte-order value directly with a host-byte-order value?
Looking at the structures,chunk->subh.init_hdr->init_tagis a__be32extracted directly from the packet, whileasoc->peer.i.init_tagis a__u32. During handshake initialization,asoc->peer.i.init_tagis populated usingntohl().
On little-endian architectures, will this comparison always fail and allow the stale INIT chunks to pass through instead of discarding them?
Will post v2 to fix this.
Thanks.