sctp: handle wrapped and duplicate RECONF responses
Subsystems (3)
v3 cover sctp: handle wrapped and duplicate RECONF responses
Fix response sequence zero lookup first, then make RECONF response handling idempotent with an outstanding-request bitmask.
Jun Yang (2):
sctp: distinguish sequence zero from wildcard in reconf lookup
sctp: fix stream->outcnt underflow on duplicate RECONF responses
include/net/sctp/structs.h | 2 +- net/sctp/stream.c | 46 ++++++++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 15 deletions(-)
---
Changes since v2:
- Split the sequence-zero lookup fix into patch 1 as requested.
v2: https://lore.kernel.org/netdev/[email protected]/
--
2.55.0
PATCH v3 1/2 sctp: distinguish sequence zero from wildcard in reconf lookup
Zero is a valid response sequence after strreset_outseq wraps, but sctp_chunk_lookup_strreset_param() currently treats it as a wildcard.
Add match_seq so response lookups match zero exactly while the one type-only lookup can still ignore the sequence.
--- net/sctp/stream.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
net/sctp/stream.c +6 -5
@@ -482,7 +482,7 @@ out:static struct sctp_paramhdr *sctp_chunk_lookup_strreset_param (struct sctp_association *asoc,__be32 resp_seq, -__be16 type) +__be16 type,bool match_seq) {struct sctp_chunk *chunk = asoc->strreset_chunk;struct sctp_reconf_chunk *hdr;
@@ -499,7 +499,7 @@ static struct sctp_paramhdr *sctp_chunk_lookup_strreset_param(*/ struct sctp_strreset_tsnreq *req = param.v; -if ((!resp_seq || req->request_seq == resp_seq) && +if ((!match_seq || req->request_seq == resp_seq) && (!type || type == req->param_hdr.type))return param.v; }
@@ -564,7 +564,7 @@ struct sctp_chunk *sctp_process_strreset_outreq(if (asoc->strreset_chunk) {if (!sctp_chunk_lookup_strreset_param ( asoc, outreq->response_seq, -SCTP_PARAM_RESET_IN_REQUEST )) { +SCTP_PARAM_RESET_IN_REQUEST , true)) {/* same process with outstanding isn't 0 */ result =SCTP_STRRESET_ERR_IN_PROGRESS ; goto out;
@@ -816,7 +816,7 @@ struct sctp_chunk *sctp_process_strreset_addstrm_out(if (asoc->strreset_chunk) {if (!sctp_chunk_lookup_strreset_param ( - asoc,0 ,SCTP_PARAM_RESET_ADD_IN_STREAMS )) { + asoc,0 ,SCTP_PARAM_RESET_ADD_IN_STREAMS , false)) {/* same process with outstanding isn't 0 */ result =SCTP_STRRESET_ERR_IN_PROGRESS ; goto out;
@@ -927,7 +927,8 @@ struct sctp_chunk *sctp_process_strreset_resp(struct sctp_paramhdr *req;__u32 result; - req =sctp_chunk_lookup_strreset_param (asoc, resp->response_seq,0 ); + req =sctp_chunk_lookup_strreset_param (asoc, resp->response_seq,0 , + true);if (!req)return NULL ;
Signature · 1 line
2.55.0
Re: [PATCH net v3 1/2] sctp: distinguish sequence zero from wildcard in reconf lookup
Quoted 66 lines · Jun Yang
Zero is a valid response sequence after
strreset_outseqwraps, butsctp_chunk_lookup_strreset_param()currently treats it as a wildcard.Add
match_seqso response lookups match zero exactly while the one type-only lookup can still ignore the sequence.Fixes 50a41591f110 ("sctp: implement receiver-side procedures for the Add Outgoing Streams Request Parameter")Suggested-by Simon Horman <[email protected]>Signed-off-by Jun Yang <[email protected]>--- net/sctp/stream.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)net/sctp/stream.c +6
-6@@ -482,7 +482,7 @@ out:static struct sctp_paramhdr *sctp_chunk_lookup_strreset_param (struct sctp_association *asoc,__be32 resp_seq, -__be16 type) +__be16 type,bool match_seq) {struct sctp_chunk *chunk = asoc->strreset_chunk;struct sctp_reconf_chunk *hdr;@@ -499,7 +499,7 @@ static struct sctp_paramhdr *sctp_chunk_lookup_strreset_param(*/ struct sctp_strreset_tsnreq *req = param.v; -if ((!resp_seq || req->request_seq == resp_seq) && +if ((!match_seq || req->request_seq == resp_seq) && (!type || type == req->param_hdr.type))return param.v; }@@ -564,7 +564,7 @@ struct sctp_chunk *sctp_process_strreset_outreq(if (asoc->strreset_chunk) {if (!sctp_chunk_lookup_strreset_param ( asoc, outreq->response_seq, -SCTP_PARAM_RESET_IN_REQUEST )) { +SCTP_PARAM_RESET_IN_REQUEST , true)) {/* same process with outstanding isn't 0 */ result =SCTP_STRRESET_ERR_IN_PROGRESS ; goto out;@@ -816,7 +816,7 @@ struct sctp_chunk *sctp_process_strreset_addstrm_out(if (asoc->strreset_chunk) {if (!sctp_chunk_lookup_strreset_param ( - asoc,0 ,SCTP_PARAM_RESET_ADD_IN_STREAMS )) { + asoc,0 ,SCTP_PARAM_RESET_ADD_IN_STREAMS , false)) {/* same process with outstanding isn't 0 */ result =SCTP_STRRESET_ERR_IN_PROGRESS ; goto out;@@ -927,7 +927,8 @@ struct sctp_chunk *sctp_process_strreset_resp(struct sctp_paramhdr *req;__u32 result; - req =sctp_chunk_lookup_strreset_param (asoc, resp->response_seq,0 ); + req =sctp_chunk_lookup_strreset_param (asoc, resp->response_seq,0 , + true);if (!req)return NULL ; --
Quoted 2 lines
2.55.0
The issue reported by Sashiko [1] is mainly a changelog improvement. It’s not a big deal to me.
[1] https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260821091440.6496-1-junvyyang%40tencent.com
PATCH v3 2/2 sctp: fix stream->outcnt underflow on duplicate RECONF responses
In reply to Jun YangA cached RECONF chunk may contain more than one request parameter. A duplicate response can therefore find and process the same ADD_OUT request again while another parameter is still outstanding, rolling back outcnt twice and possibly underflowing it.
Track outstanding request types as bits and clear each bit after its first response. Later responses for the same request are then ignored.
---
v3:
- Split the response_seq == 0 lookup fix into patch 1 (Simon Horman).
- Keep the request bit helper local to stream.c.
v2:
- Track outstanding requests by type instead of sequence (Xin Long).
- Drop the redundant arithmetic guard (Xin Long).
v1: https://lore.kernel.org/netdev/[email protected]/
include/net/sctp/structs.h | 2 +- net/sctp/stream.c | 39 +++++++++++++++++++++++++++----------- 2 files changed, 29 insertions(+), 12 deletions(-)
include/net/sctp/structs.h +1 -1
@@ -2057,7 +2057,7 @@ struct sctp_association { force_delay:1 ;__u8 strreset_enable; -__u8 strreset_outstanding;/* request param count on the fly */ +__u8 strreset_outstanding;/* request param bitmask on the fly */ __u32 strreset_outseq;/* Update after receiving response */ __u32 strreset_inseq;/* Update after receiving request */
net/sctp/stream.c +28 -11
@@ -22,6 +22,9 @@#include <net/sctp/sm.h> #include <net/sctp/stream_sched.h> +#define SCTP_STRRESET_BIT (type) \ + BIT(ntohs(type) - ntohs(SCTP_PARAM_RESET_OUT_REQUEST)) +static void sctp_stream_shrink_out (struct sctp_stream *stream,__u16 outcnt) {struct sctp_association *asoc;
@@ -372,7 +375,9 @@ int sctp_send_reset_streams(struct sctp_association *asoc, goto out; } - asoc->strreset_outstanding = out + in; + asoc->strreset_outstanding = + (out ?SCTP_STRRESET_BIT (SCTP_PARAM_RESET_OUT_REQUEST ) :0 ) | + (in ?SCTP_STRRESET_BIT (SCTP_PARAM_RESET_IN_REQUEST ) :0 ); out:return retval;
@@ -417,7 +422,8 @@ int sctp_send_reset_assoc(struct sctp_association *asoc)return retval; } - asoc->strreset_outstanding =1 ; + asoc->strreset_outstanding = +SCTP_STRRESET_BIT (SCTP_PARAM_RESET_TSN_REQUEST );return 0 ; }
@@ -474,7 +480,9 @@ int sctp_send_add_streams(struct sctp_association *asoc, goto out; } - asoc->strreset_outstanding = !!out + !!in; + asoc->strreset_outstanding = + (out ?SCTP_STRRESET_BIT (SCTP_PARAM_RESET_ADD_OUT_STREAMS ) :0 ) | + (in ?SCTP_STRRESET_BIT (SCTP_PARAM_RESET_ADD_IN_STREAMS ) :0 ); out:return retval;
@@ -564,13 +572,16 @@ struct sctp_chunk *sctp_process_strreset_outreq(if (asoc->strreset_chunk) {if (!sctp_chunk_lookup_strreset_param ( asoc, outreq->response_seq, -SCTP_PARAM_RESET_IN_REQUEST , true)) { +SCTP_PARAM_RESET_IN_REQUEST , true) || + !(asoc->strreset_outstanding & +SCTP_STRRESET_BIT (SCTP_PARAM_RESET_IN_REQUEST ))) {/* same process with outstanding isn't 0 */ result =SCTP_STRRESET_ERR_IN_PROGRESS ; goto out; } - asoc->strreset_outstanding--; + asoc->strreset_outstanding &= + ~SCTP_STRRESET_BIT (SCTP_PARAM_RESET_IN_REQUEST ); asoc->strreset_outseq++;if (!asoc->strreset_outstanding) {
@@ -669,7 +680,8 @@ struct sctp_chunk *sctp_process_strreset_inreq(SCTP_SO (stream, i)->state =SCTP_STREAM_CLOSED ; asoc->strreset_chunk = chunk; - asoc->strreset_outstanding =1 ; + asoc->strreset_outstanding = +SCTP_STRRESET_BIT (SCTP_PARAM_RESET_OUT_REQUEST );sctp_chunk_hold (asoc->strreset_chunk); result =SCTP_STRRESET_PERFORMED ;
@@ -816,13 +828,16 @@ struct sctp_chunk *sctp_process_strreset_addstrm_out(if (asoc->strreset_chunk) {if (!sctp_chunk_lookup_strreset_param ( - asoc,0 ,SCTP_PARAM_RESET_ADD_IN_STREAMS , false)) { + asoc,0 ,SCTP_PARAM_RESET_ADD_IN_STREAMS , false) || + !(asoc->strreset_outstanding & +SCTP_STRRESET_BIT (SCTP_PARAM_RESET_ADD_IN_STREAMS ))) {/* same process with outstanding isn't 0 */ result =SCTP_STRRESET_ERR_IN_PROGRESS ; goto out; } - asoc->strreset_outstanding--; + asoc->strreset_outstanding &= + ~SCTP_STRRESET_BIT (SCTP_PARAM_RESET_ADD_IN_STREAMS ); asoc->strreset_outseq++;if (!asoc->strreset_outstanding) {
@@ -899,7 +914,8 @@ struct sctp_chunk *sctp_process_strreset_addstrm_in( goto out; asoc->strreset_chunk = chunk; - asoc->strreset_outstanding =1 ; + asoc->strreset_outstanding = +SCTP_STRRESET_BIT (SCTP_PARAM_RESET_ADD_OUT_STREAMS );sctp_chunk_hold (asoc->strreset_chunk); stream->outcnt = outcnt;
@@ -929,7 +945,8 @@ struct sctp_chunk *sctp_process_strreset_resp( req =sctp_chunk_lookup_strreset_param (asoc, resp->response_seq,0 , true); -if (!req) +if (!req || !(asoc->strreset_outstanding & +SCTP_STRRESET_BIT (req->type)))return NULL ; result =ntohl (resp->result);
@@ -1079,7 +1096,7 @@ struct sctp_chunk *sctp_process_strreset_resp( nums, 0, GFP_ATOMIC); } - asoc->strreset_outstanding--; + asoc->strreset_outstanding &= ~SCTP_STRRESET_BIT (req->type); asoc->strreset_outseq++;/* remove everything for this reconf request */
Signature · 1 line
2.55.0
Xin Long
maintainerRe: [PATCH net v3 2/2] sctp: fix stream->outcnt underflow on duplicate RECONF responses
Quoted 49 lines · Jun Yang
A cached RECONF chunk may contain more than one request parameter. A duplicate response can therefore find and process the same
ADD_OUTrequest again while another parameter is still outstanding, rolling back outcnt twice and possibly underflowing it.Track outstanding request types as bits and clear each bit after its first response. Later responses for the same request are then ignored.
Fixes 11ae76e67a17 ("sctp: implement receiver-side procedures for the Reconf Response Parameter")Reported-by TencentOS Corvus AI <[email protected]>Suggested-by Xin Long <[email protected]>Assisted-by tencentos-corvus-ai:kimi-k3Signed-off-by Jun Yang <[email protected]>---
v3:
- Split theresponse_seq== 0 lookup fix into patch 1 (Simon Horman).
- Keep the request bit helper local tostream.c.v2:
- Track outstanding requests by type instead of sequence (Xin Long).
- Drop the redundant arithmetic guard (Xin Long).v1: https://lore.kernel.org/netdev/[email protected]/
include/net/sctp/structs.h | 2 +- net/sctp/stream.c | 39 +++++++++++++++++++++++++++----------- 2 files changed, 29 insertions(+), 12 deletions(-)include/net/sctp/structs.h +1
-1@@ -2057,7 +2057,7 @@ struct sctp_association { force_delay:1 ;__u8 strreset_enable; -__u8 strreset_outstanding;/* request param count on the fly */ +__u8 strreset_outstanding;/* request param bitmask on the fly */ __u32 strreset_outseq;/* Update after receiving response */ __u32 strreset_inseq;/* Update after receiving request */ net/sctp/stream.c +0
-0
net/sctp/stream.c line 27
Quoted 6 lines
net/sctp/stream.c +3
-0@@ -22,6 +22,9 @@#include <net/sctp/sm.h> #include <net/sctp/stream_sched.h> +#define SCTP_STRRESET_BIT (type) \ + BIT(ntohs(type) - ntohs(SCTP_PARAM_RESET_OUT_REQUEST)) +
Hi, Jun Yang,
Any reason for removing SCTP_STRRESET_RET/CLEAR/TEST()? Each of these macros is used at least twice below, so removing them increases the number of lines of code.
Note: The pre-existing issue reported in sashiko [1] is false positive, as a peer can't answers an IN_REQUEST or ADD_IN_STREAMS with SCTP_STRRESET_PERFORMED according to rfc6525.
[1] https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260821091440.6496-1-junvyyang%40tencent.com
Thanks.
Quoted 115 lines
net/sctp/stream.c +25
-12static void sctp_stream_shrink_out (struct sctp_stream *stream,__u16 outcnt) {struct sctp_association *asoc;@@ -372,7 +375,9 @@ int sctp_send_reset_streams(struct sctp_association *asoc, goto out; } - asoc->strreset_outstanding = out + in; + asoc->strreset_outstanding = + (out ?SCTP_STRRESET_BIT (SCTP_PARAM_RESET_OUT_REQUEST ) :0 ) | + (in ?SCTP_STRRESET_BIT (SCTP_PARAM_RESET_IN_REQUEST ) :0 ); out:return retval;@@ -417,7 +422,8 @@ int sctp_send_reset_assoc(struct sctp_association *asoc)return retval; } - asoc->strreset_outstanding =1 ; + asoc->strreset_outstanding = +SCTP_STRRESET_BIT (SCTP_PARAM_RESET_TSN_REQUEST );return 0 ; }@@ -474,7 +480,9 @@ int sctp_send_add_streams(struct sctp_association *asoc, goto out; } - asoc->strreset_outstanding = !!out + !!in; + asoc->strreset_outstanding = + (out ?SCTP_STRRESET_BIT (SCTP_PARAM_RESET_ADD_OUT_STREAMS ) :0 ) | + (in ?SCTP_STRRESET_BIT (SCTP_PARAM_RESET_ADD_IN_STREAMS ) :0 ); out:return retval;@@ -564,13 +572,16 @@ struct sctp_chunk *sctp_process_strreset_outreq(if (asoc->strreset_chunk) {if (!sctp_chunk_lookup_strreset_param ( asoc, outreq->response_seq, -SCTP_PARAM_RESET_IN_REQUEST , true)) { +SCTP_PARAM_RESET_IN_REQUEST , true) || + !(asoc->strreset_outstanding & +SCTP_STRRESET_BIT (SCTP_PARAM_RESET_IN_REQUEST ))) {/* same process with outstanding isn't 0 */ result =SCTP_STRRESET_ERR_IN_PROGRESS ; goto out; } - asoc->strreset_outstanding--; + asoc->strreset_outstanding &= + ~SCTP_STRRESET_BIT (SCTP_PARAM_RESET_IN_REQUEST ); asoc->strreset_outseq++;if (!asoc->strreset_outstanding) {@@ -669,7 +680,8 @@ struct sctp_chunk *sctp_process_strreset_inreq(SCTP_SO (stream, i)->state =SCTP_STREAM_CLOSED ; asoc->strreset_chunk = chunk; - asoc->strreset_outstanding =1 ; + asoc->strreset_outstanding = +SCTP_STRRESET_BIT (SCTP_PARAM_RESET_OUT_REQUEST );sctp_chunk_hold (asoc->strreset_chunk); result =SCTP_STRRESET_PERFORMED ;@@ -816,13 +828,16 @@ struct sctp_chunk *sctp_process_strreset_addstrm_out(if (asoc->strreset_chunk) {if (!sctp_chunk_lookup_strreset_param ( - asoc,0 ,SCTP_PARAM_RESET_ADD_IN_STREAMS , false)) { + asoc,0 ,SCTP_PARAM_RESET_ADD_IN_STREAMS , false) || + !(asoc->strreset_outstanding & +SCTP_STRRESET_BIT (SCTP_PARAM_RESET_ADD_IN_STREAMS ))) {/* same process with outstanding isn't 0 */ result =SCTP_STRRESET_ERR_IN_PROGRESS ; goto out; } - asoc->strreset_outstanding--; + asoc->strreset_outstanding &= + ~SCTP_STRRESET_BIT (SCTP_PARAM_RESET_ADD_IN_STREAMS ); asoc->strreset_outseq++;if (!asoc->strreset_outstanding) {@@ -899,7 +914,8 @@ struct sctp_chunk *sctp_process_strreset_addstrm_in( goto out; asoc->strreset_chunk = chunk; - asoc->strreset_outstanding =1 ; + asoc->strreset_outstanding = +SCTP_STRRESET_BIT (SCTP_PARAM_RESET_ADD_OUT_STREAMS );sctp_chunk_hold (asoc->strreset_chunk); stream->outcnt = outcnt;@@ -929,7 +945,8 @@ struct sctp_chunk *sctp_process_strreset_resp( req =sctp_chunk_lookup_strreset_param (asoc, resp->response_seq,0 , true); -if (!req) +if (!req || !(asoc->strreset_outstanding & +SCTP_STRRESET_BIT (req->type)))return NULL ; result =ntohl (resp->result);@@ -1079,7 +1096,7 @@ struct sctp_chunk *sctp_process_strreset_resp( nums, 0, GFP_ATOMIC); } - asoc->strreset_outstanding--; + asoc->strreset_outstanding &= ~SCTP_STRRESET_BIT (req->type); asoc->strreset_outseq++;/* remove everything for this reconf request */ --2.55.0