Skip to contentSkip to conversation

sctp: validate INIT in COOKIE-ECHO when auth disabled

PATCH Superseded Xin Long 2 participants 7 messages Jun 16 – Jun 18

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Xin Long

author maintainer
#0 lore raw

cover sctp: validate INIT in COOKIE-ECHO when auth disabled

This series fixes a security gap in SCTP's COOKIE-ECHO handling when cookie authentication is disabled.

Currently, INIT chunks embedded in cookies are not re-verified after unpacking, creating a vulnerability when cookie_auth_enable=0. This series first refactors error handling, then adds the missing validation.

Xin Long (2):
sctp: factor out INIT verification failure handling
sctp: add INIT verification after cookie unpacking

 net/sctp/sm_make_chunk.c |   2 +-
 net/sctp/sm_statefuns.c  | 200 +++++++++++++++++++--------------------
 2 files changed, 99 insertions(+), 103 deletions(-)
Signature · 1 line
2.47.1
author maintainer
#1 lore raw

PATCH 1/2 sctp: factor out INIT verification failure handling

Extract the duplicated INIT/INIT-ACK error handling logic into a new helper, sctp_abort_on_init_err().

Several state functions open-code the same pattern after sctp_verify_init() fails: construct an ABORT with error causes if available, send it when allocation succeeds, or fall back to T-bit ABORT handling when no error chunk is present. INIT-ACK handling also includes additional teardown logic for malformed packets.

Move this logic into sctp_abort_on_init_err() to reduce duplication and centralize INIT/INIT-ACK failure handling.

No functional change intended. The helper will be used in a subsequent patch.

---
 net/sctp/sm_statefuns.c | 171 +++++++++++++++++-----------------------
 1 file changed, 72 insertions(+), 99 deletions(-)
net/sctp/sm_statefuns.c +72 -99
index 9b23c11cbb9e..544f308ee527 100644
@@ -68,6 +68,14 @@ static void sctp_send_stale_cookie_err(struct net *net,
 				       const struct sctp_chunk *chunk,
 				       struct sctp_cmd_seq *commands,
 				       struct sctp_chunk *err_chunk);
+static enum sctp_disposition sctp_abort_on_init_err(
+					struct net *net,
+					const struct sctp_endpoint *ep,
+					const struct sctp_association *asoc,
+					const struct sctp_chunk *chunk,
+					void *arg,
+					struct sctp_cmd_seq *commands,
+					struct sctp_chunk *err_chunk);
 static enum sctp_disposition sctp_sf_do_5_2_6_stale(
 					struct net *net,
 					const struct sctp_endpoint *ep,
@@ -325,7 +333,6 @@ enum sctp_disposition sctp_sf_do_5_1B_init(struct net *net,
 	struct sctp_chunk *chunk = arg, *repl, *err_chunk;
 	struct sctp_unrecognized_param *unk_param;
 	struct sctp_association *new_asoc;
-	struct sctp_packet *packet;
 	int len;
 
 	/* 6.10 Bundling
@@ -375,32 +382,9 @@ enum sctp_disposition sctp_sf_do_5_1B_init(struct net *net,
 	err_chunk = NULL;
 	if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
 			      (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
-			      &err_chunk)) {
-		/* This chunk contains fatal error. It is to be discarded.
-		 * Send an ABORT, with causes if there is any.
-		 */
-		if (err_chunk) {
-			packet = sctp_abort_pkt_new(net, ep, asoc, arg,
-					(__u8 *)(err_chunk->chunk_hdr) +
-					sizeof(struct sctp_chunkhdr),
-					ntohs(err_chunk->chunk_hdr->length) -
-					sizeof(struct sctp_chunkhdr));
-
-			sctp_chunk_free(err_chunk);
-
-			if (packet) {
-				sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
-						SCTP_PACKET(packet));
-				SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
-				return SCTP_DISPOSITION_CONSUME;
-			} else {
-				return SCTP_DISPOSITION_NOMEM;
-			}
-		} else {
-			return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
-						    commands);
-		}
-	}
+			      &err_chunk))
+		return sctp_abort_on_init_err(net, ep, asoc, chunk, arg,
+					      commands, err_chunk);
 
 	/* Grab the INIT header.  */
 	chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
@@ -525,7 +509,6 @@ enum sctp_disposition sctp_sf_do_5_1C_ack(struct net *net,
 	struct sctp_init_chunk *initchunk;
 	struct sctp_chunk *chunk = arg;
 	struct sctp_chunk *err_chunk;
-	struct sctp_packet *packet;
 
 	if (!sctp_vtag_verify(chunk, asoc))
 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
@@ -548,50 +531,9 @@ enum sctp_disposition sctp_sf_do_5_1C_ack(struct net *net,
 	err_chunk = NULL;
 	if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
 			      (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
-			      &err_chunk)) {
-
-		enum sctp_error error = SCTP_ERROR_NO_RESOURCE;
-
-		/* This chunk contains fatal error. It is to be discarded.
-		 * Send an ABORT, with causes.  If there are no causes,
-		 * then there wasn't enough memory.  Just terminate
-		 * the association.
-		 */
-		if (err_chunk) {
-			packet = sctp_abort_pkt_new(net, ep, asoc, arg,
-					(__u8 *)(err_chunk->chunk_hdr) +
-					sizeof(struct sctp_chunkhdr),
-					ntohs(err_chunk->chunk_hdr->length) -
-					sizeof(struct sctp_chunkhdr));
-
-			sctp_chunk_free(err_chunk);
-
-			if (packet) {
-				sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
-						SCTP_PACKET(packet));
-				SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
-				error = SCTP_ERROR_INV_PARAM;
-			}
-		}
-
-		/* SCTP-AUTH, Section 6.3:
-		 *    It should be noted that if the receiver wants to tear
-		 *    down an association in an authenticated way only, the
-		 *    handling of malformed packets should not result in
-		 *    tearing down the association.
-		 *
-		 * This means that if we only want to abort associations
-		 * in an authenticated way (i.e AUTH+ABORT), then we
-		 * can't destroy this association just because the packet
-		 * was malformed.
-		 */
-		if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
-			return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
-
-		SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
-		return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
-						asoc, chunk->transport);
-	}
+			      &err_chunk))
+		return sctp_abort_on_init_err(net, ep, asoc, chunk, arg,
+					      commands, err_chunk);
 
 	/* Tag the variable length parameters.  Note that we never
 	 * convert the parameters in an INIT chunk.
@@ -1522,7 +1464,6 @@ static enum sctp_disposition sctp_sf_do_unexpected_init(
 	struct sctp_unrecognized_param *unk_param;
 	struct sctp_association *new_asoc;
 	enum sctp_disposition retval;
-	struct sctp_packet *packet;
 	int len;
 
 	/* 6.10 Bundling
@@ -1566,31 +1507,9 @@ static enum sctp_disposition sctp_sf_do_unexpected_init(
 	err_chunk = NULL;
 	if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
 			      (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
-			      &err_chunk)) {
-		/* This chunk contains fatal error. It is to be discarded.
-		 * Send an ABORT, with causes if there is any.
-		 */
-		if (err_chunk) {
-			packet = sctp_abort_pkt_new(net, ep, asoc, arg,
-					(__u8 *)(err_chunk->chunk_hdr) +
-					sizeof(struct sctp_chunkhdr),
-					ntohs(err_chunk->chunk_hdr->length) -
-					sizeof(struct sctp_chunkhdr));
-
-			if (packet) {
-				sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
-						SCTP_PACKET(packet));
-				SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
-				retval = SCTP_DISPOSITION_CONSUME;
-			} else {
-				retval = SCTP_DISPOSITION_NOMEM;
-			}
-			goto cleanup;
-		} else {
-			return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
-						    commands);
-		}
-	}
+			      &err_chunk))
+		return sctp_abort_on_init_err(net, ep, asoc, chunk, arg,
+					      commands, err_chunk);
 
 	/*
 	 * Other parameters for the endpoint SHOULD be copied from the
@@ -1691,7 +1610,6 @@ static enum sctp_disposition sctp_sf_do_unexpected_init(
 nomem_retval:
 	if (new_asoc)
 		sctp_association_free(new_asoc);
-cleanup:
 	if (err_chunk)
 		sctp_chunk_free(err_chunk);
 	return retval;
@@ -6485,6 +6403,61 @@ static void sctp_send_stale_cookie_err(struct net *net,
 	}
 }
 
+static enum sctp_disposition sctp_abort_on_init_err(
+					struct net *net,
+					const struct sctp_endpoint *ep,
+					const struct sctp_association *asoc,
+					const struct sctp_chunk *chunk,
+					void *arg,
+					struct sctp_cmd_seq *commands,
+					struct sctp_chunk *err_chunk)
+{
+	enum sctp_error error = SCTP_ERROR_NO_RESOURCE;
+	struct sctp_packet *packet;
+	struct sctp_chunkhdr *ch;
+
+	if (!err_chunk)
+		return sctp_sf_tabort_8_4_8(net, ep, asoc, SCTP_ST_CHUNK(0),
+					    arg, commands);
+
+	ch = err_chunk->chunk_hdr;
+	packet = sctp_abort_pkt_new(net, ep, asoc, arg,
+				    (__u8 *)ch + sizeof(*ch),
+				    ntohs(ch->length) - sizeof(*ch));
+
+	sctp_chunk_free(err_chunk);
+
+	if (packet) {
+		sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
+				SCTP_PACKET(packet));
+		SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
+		error = SCTP_ERROR_INV_PARAM;
+	}
+
+	if (chunk->chunk_hdr->type != SCTP_CID_INIT_ACK) {
+		if (!packet)
+			return SCTP_DISPOSITION_NOMEM;
+		return SCTP_DISPOSITION_CONSUME;
+	}
+	/* SCTP-AUTH, Section 6.3:
+	 *    It should be noted that if the receiver wants to tear
+	 *    down an association in an authenticated way only, the
+	 *    handling of malformed packets should not result in
+	 *    tearing down the association.
+	 *
+	 * This means that if we only want to abort associations
+	 * in an authenticated way (i.e AUTH+ABORT), then we
+	 * can't destroy this association just because the packet
+	 * was malformed.
+	 */
+	if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
+		return sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg,
+					commands);
+
+	SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
+	return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
+				      asoc, chunk->transport);
+}
 
 /* Process a data chunk */
 static int sctp_eat_data(const struct sctp_association *asoc,
Signature · 1 line
2.47.1
author maintainer
#4 lore raw

PATCH 2/2 sctp: add INIT verification after cookie unpacking

In reply to Xin Long

In SCTP handshake, the INIT chunk is initially processed by the server and embedded into the cookie carried in INIT-ACK. The client then returns this cookie via COOKIE-ECHO, where the server unpacks it and reconstructs the original INIT chunk.

When cookie authentication is enabled, the cookie contents are protected against tampering, so reusing the unpacked INIT without re-verification is safe.

However, when cookie authentication is disabled, the reconstructed INIT can no longer be trusted. In this case, the INIT must be explicitly validated after unpacking to avoid processing potentially tampered data.

Add sctp_verify_init() checks after cookie unpacking in COOKIE-ECHO processing paths (sctp_sf_do_5_1D_ce() and sctp_sf_do_5_2_4_dupcook()) when cookie_auth_enable is disabled. On failure, the association is freed and an ABORT is generated via sctp_abort_on_init_err().

Also update sctp_verify_init() to validate parameter bounds against the actual peer_init length rather than chunk->chunk_end, since peer_init may not span the full chunk buffer in COOKIE-ECHO.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 net/sctp/sm_make_chunk.c |  2 +-
 net/sctp/sm_statefuns.c  | 29 ++++++++++++++++++++++++++---
 2 files changed, 27 insertions(+), 4 deletions(-)
net/sctp/sm_make_chunk.c +1 -1
index 41958b8e59fd..21b9eb1c02e9 100644
@@ -2298,7 +2298,7 @@ int sctp_verify_init(struct net *net, const struct sctp_endpoint *ep,
 	 * VIOLATION error.  We build the ERROR chunk here and let the normal
 	 * error handling code build and send the packet.
 	 */
-	if (param.v != (void *)chunk->chunk_end)
+	if (param.v != (void *)peer_init + ntohs(peer_init->chunk_hdr.length))
 		return sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
 
 	/* The only missing mandatory param possible today is
net/sctp/sm_statefuns.c +26 -3
index 544f308ee527..a11a18678279 100644
@@ -647,10 +647,10 @@ enum sctp_disposition sctp_sf_do_5_1D_ce(struct net *net,
 					 struct sctp_cmd_seq *commands)
 {
 	struct sctp_ulpevent *ev, *ai_ev = NULL, *auth_ev = NULL;
+	struct sctp_chunk *err_chk_p = NULL;
 	struct sctp_association *new_asoc;
 	struct sctp_init_chunk *peer_init;
 	struct sctp_chunk *chunk = arg;
-	struct sctp_chunk *err_chk_p;
 	struct sctp_chunk *repl;
 	struct sock *sk;
 	int error = 0;
@@ -725,6 +725,17 @@ enum sctp_disposition sctp_sf_do_5_1D_ce(struct net *net,
 		}
 	}
 
+	peer_init = (struct sctp_init_chunk *)(chunk->subh.cookie_hdr + 1);
+	if (!sctp_sk(sk)->cookie_auth_enable &&
+	    !sctp_verify_init(net, ep, asoc, peer_init->chunk_hdr.type,
+			      peer_init, chunk, &err_chk_p)) {
+		sctp_association_free(new_asoc);
+		return sctp_abort_on_init_err(net, ep, asoc, chunk, arg,
+					      commands, err_chk_p);
+	}
+	if (err_chk_p)
+		sctp_chunk_free(err_chk_p);
+
 	if (security_sctp_assoc_request(new_asoc, chunk->head_skb ?: chunk->skb)) {
 		sctp_association_free(new_asoc);
 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
@@ -738,7 +749,6 @@ enum sctp_disposition sctp_sf_do_5_1D_ce(struct net *net,
 	/* This is a brand-new association, so these are not yet side
 	 * effects--it is safe to run them here.
 	 */
-	peer_init = (struct sctp_init_chunk *)(chunk->subh.cookie_hdr + 1);
 	if (!sctp_process_init(new_asoc, chunk,
 			       &chunk->subh.cookie_hdr->c.peer_addr,
 			       peer_init, GFP_ATOMIC))
@@ -2128,10 +2138,11 @@ enum sctp_disposition sctp_sf_do_5_2_4_dupcook(
 					void *arg,
 					struct sctp_cmd_seq *commands)
 {
+	struct sctp_chunk *err_chk_p = NULL;
 	struct sctp_association *new_asoc;
+	struct sctp_init_chunk *peer_init;
 	struct sctp_chunk *chunk = arg;
 	enum sctp_disposition retval;
-	struct sctp_chunk *err_chk_p;
 	int error = 0;
 	char action;
 
@@ -2200,6 +2211,18 @@ enum sctp_disposition sctp_sf_do_5_2_4_dupcook(
 	switch (action) {
 	case 'A': /* Association restart. */
 	case 'B': /* Collision case B. */
+		peer_init = (struct sctp_init_chunk *)
+				(chunk->subh.cookie_hdr + 1);
+		if (!sctp_sk(ep->base.sk)->cookie_auth_enable &&
+		    !sctp_verify_init(net, ep, asoc, peer_init->chunk_hdr.type,
+				      peer_init, chunk, &err_chk_p)) {
+			sctp_association_free(new_asoc);
+			return sctp_abort_on_init_err(net, ep, asoc, chunk, arg,
+						      commands, err_chk_p);
+		}
+		if (err_chk_p)
+			sctp_chunk_free(err_chk_p);
+		fallthrough;
 	case 'D': /* Collision case D. */
 		/* Update socket peer label if first association. */
 		if (security_sctp_assoc_request((struct sctp_association *)asoc,
Signature · 1 line
2.47.1
reviewer
#2 lore raw

Re: [PATCH net 1/2] sctp: factor out INIT verification failure handling

In reply to Xin Long

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://netdev-ai.bots.linux.dev/sashiko/
---
sctp: factor out INIT verification failure handling

This refactor moves the duplicated INIT/INIT-ACK error handling out of sctp_sf_do_5_1B_init(), sctp_sf_do_5_1C_ack(), and sctp_sf_do_unexpected_init() into a new helper sctp_abort_on_init_err(). The commit message states "No functional change intended."

Quoted 4 lines
net/sctp/sm_statefuns.c +0 -0
index 9b23c11cbb9e..544f308ee527 100644

[ ... ]

Quoted 41 lines
net/sctp/sm_statefuns.c +0 -37
@@ -548,50 +531,9 @@ enum sctp_disposition sctp_sf_do_5_1C_ack(struct net *net,
 	err_chunk = NULL;
 	if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
 			      (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
-			      &err_chunk)) {
-
-		enum sctp_error error = SCTP_ERROR_NO_RESOURCE;
-
-		/* This chunk contains fatal error. It is to be discarded.
-		 * Send an ABORT, with causes.  If there are no causes,
-		 * then there wasn't enough memory.  Just terminate
-		 * the association.
-		 */
-		if (err_chunk) {
-			packet = sctp_abort_pkt_new(net, ep, asoc, arg,
-					(__u8 *)(err_chunk->chunk_hdr) +
-					sizeof(struct sctp_chunkhdr),
-					ntohs(err_chunk->chunk_hdr->length) -
-					sizeof(struct sctp_chunkhdr));
-
-			sctp_chunk_free(err_chunk);
-
-			if (packet) {
-				sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
-						SCTP_PACKET(packet));
-				SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
-				error = SCTP_ERROR_INV_PARAM;
-			}
-		}
-
-		/* SCTP-AUTH, Section 6.3:
-		 *    It should be noted that if the receiver wants to tear
-		 *    down an association in an authenticated way only, the
-		 *    handling of malformed packets should not result in
-		 *    tearing down the association.
-		 *
-		 * This means that if we only want to abort associations
-		 * in an authenticated way (i.e AUTH+ABORT), then we
-		 * can't destroy this association just because the packet
-		 * was malformed.
-		 */

net/sctp/sm_statefuns.c enum sctp_disposition sctp_sf_do_5_1C_ack(struct net *net, · line 536

Quoted 10 lines
net/sctp/sm_statefuns.c +3 -7
-		if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
-			return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
-
-		SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
-		return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
-						asoc, chunk->transport);
-	}
+			      &err_chunk))
+		return sctp_abort_on_init_err(net, ep, asoc, chunk, arg,
+					      commands, err_chunk);

[High]
In the original sctp_sf_do_5_1C_ack() the "if (err_chunk) { ... }" block is non-controlling: when err_chunk is NULL the code skips the block, falls through with error == SCTP_ERROR_NO_RESOURCE, consults sctp_auth_recv_cid(SCTP_CID_ABORT, asoc) and either pdiscards or runs sctp_stop_t1_and_abort() to tear the half-open association down.

[High]
In the new helper, can the INIT-ACK + NULL err_chunk case still reach that path? Looking at sctp_abort_on_init_err():

if (!err_chunk)

          return sctp_sf_tabort_8_4_8(net, ep, asoc, SCTP_ST_CHUNK(0),
                                      arg, commands);
  ...
  if (chunk->chunk_hdr->type != SCTP_CID_INIT_ACK) {
          ...
  }
  /* SCTP-AUTH, Section 6.3 ... */
  if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
          return sctp_sf_pdiscard(...);
  SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
                                asoc, chunk->transport);

[High]
The !err_chunk early return runs before the INIT-ACK type check, so when sctp_verify_init() returns 0 with *errp left as NULL on an INIT-ACK, control now goes to sctp_sf_tabort_8_4_8() rather than the auth-check + sctp_stop_t1_and_abort() path.

[High]
Is the NULL-err_chunk case actually reachable on an INIT-ACK? In sctp_process_inv_mandatory() and sctp_process_inv_paramlength() in net/sctp/sm_make_chunk.c, *errp is allocated via sctp_make_op_error_space() or sctp_make_violation_paramlen() and the function returns 0 unconditionally:

  if (!*errp)
          *errp = sctp_make_op_error_space(asoc, chunk, 0);
  if (*errp)
          sctp_init_cause(...);
  return 0;

So an allocation failure on a malformed INIT-ACK reaches the helper with err_chunk == NULL.

[High]
On that path, does the new code skip several actions that the original performed? sctp_sf_tabort_8_4_8() emits an OOTB ABORT and pdiscards; it does not stop T1-init, does not call SCTP_CMD_SET_SK_ERR(ECONNREFUSED), does not issue SCTP_CMD_INIT_FAILED, does not transition the asoc to SCTP_STATE_CLOSED, and does not increment SCTP_MIB_ABORTEDS.

[High]
As a result, would connect(2) wait for T1-init to exhaust its retransmissions instead of receiving ECONNREFUSED promptly, and would SCTP_MIB_ABORTEDS no longer be incremented (with SCTP_MIB_IN_PKT_DISCARDS being incremented in its place)?

[High]
Does this also bypass the SCTP-AUTH section 6.3 check quoted in the comment? Previously, when AUTH-ABORT was negotiated and err_chunk was NULL, sctp_auth_recv_cid() caused sctp_sf_pdiscard() to be returned and no ABORT was emitted on the wire. In the new path, sctp_sf_tabort_8_4_8() sends an unauthenticated OOTB ABORT regardless of AUTH negotiation.

[High]
Finally, does the disposition return value also change from SCTP_DISPOSITION_ABORT to SCTP_DISPOSITION_CONSUME on this path?

[High]
Would moving the chunk->chunk_hdr->type check ahead of the !err_chunk early return restore the original semantics for INIT-ACK while keeping the sctp_sf_tabort_8_4_8() fallback for INIT?

[ ... ]

Quoted 59 lines
net/sctp/sm_statefuns.c +55 -0
@@ -6485,6 +6403,61 @@ static void sctp_send_stale_cookie_err(struct net *net,
 	}
 }
 
+static enum sctp_disposition sctp_abort_on_init_err(
+					struct net *net,
+					const struct sctp_endpoint *ep,
+					const struct sctp_association *asoc,
+					const struct sctp_chunk *chunk,
+					void *arg,
+					struct sctp_cmd_seq *commands,
+					struct sctp_chunk *err_chunk)
+{
+	enum sctp_error error = SCTP_ERROR_NO_RESOURCE;
+	struct sctp_packet *packet;
+	struct sctp_chunkhdr *ch;
+
+	if (!err_chunk)
+		return sctp_sf_tabort_8_4_8(net, ep, asoc, SCTP_ST_CHUNK(0),
+					    arg, commands);
+
+	ch = err_chunk->chunk_hdr;
+	packet = sctp_abort_pkt_new(net, ep, asoc, arg,
+				    (__u8 *)ch + sizeof(*ch),
+				    ntohs(ch->length) - sizeof(*ch));
+
+	sctp_chunk_free(err_chunk);
+
+	if (packet) {
+		sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
+				SCTP_PACKET(packet));
+		SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
+		error = SCTP_ERROR_INV_PARAM;
+	}
+
+	if (chunk->chunk_hdr->type != SCTP_CID_INIT_ACK) {
+		if (!packet)
+			return SCTP_DISPOSITION_NOMEM;
+		return SCTP_DISPOSITION_CONSUME;
+	}
+	/* SCTP-AUTH, Section 6.3:
+	 *    It should be noted that if the receiver wants to tear
+	 *    down an association in an authenticated way only, the
+	 *    handling of malformed packets should not result in
+	 *    tearing down the association.
+	 *
+	 * This means that if we only want to abort associations
+	 * in an authenticated way (i.e AUTH+ABORT), then we
+	 * can't destroy this association just because the packet
+	 * was malformed.
+	 */
+	if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
+		return sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg,
+					commands);
+
+	SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
+	return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
+				      asoc, chunk->transport);
+}
reviewer
#5 lore raw

Re: [PATCH net 2/2] sctp: add INIT verification after cookie unpacking

In reply to Xin Long

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://netdev-ai.bots.linux.dev/sashiko/
---
Now I have enough verified information to produce the report.

commit 9d74027bfd0cb22a103cca034883f0fcb4e2535a
Author: Xin Long <[email protected]>

sctp: add INIT verification after cookie unpacking

This commit adds sctp_verify_init() checks after cookie unpacking in the COOKIE-ECHO processing paths when cookie_auth_enable is disabled. It also changes the bounds comparison inside sctp_verify_init() from chunk->chunk_end to peer_init + ntohs(peer_init->chunk_hdr.length).

net/sctp/sm_make_chunk.c int sctp_verify_init(struct net *net, const struct sctp_endpoint *ep, · line 2302

Quoted 11 lines
net/sctp/sm_make_chunk.c +1 -1
index 41958b8e59fd..21b9eb1c02e9 100644
@@ -2298,7 +2298,7 @@ int sctp_verify_init(struct net *net, const struct sctp_endpoint *ep,
 	 * VIOLATION error.  We build the ERROR chunk here and let the normal
 	 * error handling code build and send the packet.
 	 */
-	if (param.v != (void *)chunk->chunk_end)
+	if (param.v != (void *)peer_init + ntohs(peer_init->chunk_hdr.length))
 		return sctp_process_inv_paramlength(asoc, param.p, chunk, errp);

[High]
Can this new comparison reject INIT chunks whose final parameter has a length that is not a multiple of 4?

sctp_walk_params() advances pos.v by SCTP_PAD4(ntohs(pos.p->length)) on every iteration:

  include/net/sctp/sctp.h:
  #define _sctp_walk_params(pos, chunk, end)\
  for (pos.v = (u8 *)(chunk + 1);\
       ...
       pos.v += SCTP_PAD4(ntohs(pos.p->length)))

so when the final parameter has an unpadded length L that is not 4-byte aligned, pos.v ends at peer_init + offset_of_last_param + SCTP_PAD4(L), which exceeds peer_init + ntohs(peer_init->chunk_hdr.length) by 1..3.

The previous comparison used chunk->chunk_end, which inqueue.c computes with the padding included:

  net/sctp/inqueue.c:
  chunk->chunk_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));

so the old check accepted unaligned-length final parameters; the new check returns 0 from sctp_verify_init(), and the callers then invoke sctp_abort_on_init_err() and emit an ABORT.

Note that this change is in sctp_verify_init() itself, so it affects every caller, not just the new COOKIE-ECHO sites: sctp_sf_do_5_1B_init, sctp_sf_do_5_1C_ack, sctp_sf_do_unexpected_init, sctp_sf_do_5_1D_ce and sctp_sf_do_5_2_4_dupcook all reach this comparison.

Realistic triggers include:

- Locally-generated INITs constructed by sctp_make_init() when both auth_enable and asconf_enable are set: with the default chunk IDs ASCONF + ASCONF_ACK the AUTH-CHUNKS parameter has length 6 and is typically the last parameter, leaving chunk_hdr.length unaligned.

- Peer INITs containing any final parameter with non-4-aligned length, e.g. SCTP_PARAM_SUPPORTED_ADDRESS_TYPES with 1 or 3 types last, AUTH-CHUNKS with N=1,2,3 entries last, or any unknown TLV.

Even in the COOKIE-ECHO context the new bound seems too strict, since sctp_pack_cookie() copies the INIT verbatim with memcpy(cookie + 1, init_chunk->chunk_hdr, ntohs(...->length)) followed immediately by raw_addrs, so unpacked cookies recovered from peers that sent an INIT with an unaligned-length final parameter would also be spuriously rejected.

Would comparing against peer_init + SCTP_PAD4(ntohs(peer_init-> chunk_hdr.length)) preserve the long-standing semantics of chunk_end while still validating the cookie-derived peer_init bounds?

Quoted 1 line
net/sctp/sm_statefuns.c +0 -0

[ ... ]

author maintainer
#3 lore raw

Re: [PATCH net 1/2] sctp: factor out INIT verification failure handling

In reply to Simon Horman
Quoted 139 lines · Simon Horman

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://netdev-ai.bots.linux.dev/sashiko/
---
sctp: factor out INIT verification failure handling

This refactor moves the duplicated INIT/INIT-ACK error handling out of sctp_sf_do_5_1B_init(), sctp_sf_do_5_1C_ack(), and sctp_sf_do_unexpected_init() into a new helper sctp_abort_on_init_err(). The commit message states "No functional change intended."

Quoted 4 lines
net/sctp/sm_statefuns.c +0 -0
index 9b23c11cbb9e..544f308ee527 100644

[ ... ]

Quoted 41 lines
net/sctp/sm_statefuns.c +0 -37
@@ -548,50 +531,9 @@ enum sctp_disposition sctp_sf_do_5_1C_ack(struct net *net,
      err_chunk = NULL;
      if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
                            (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
-                           &err_chunk)) {
-
-             enum sctp_error error = SCTP_ERROR_NO_RESOURCE;
-
-             /* This chunk contains fatal error. It is to be discarded.
-              * Send an ABORT, with causes.  If there are no causes,
-              * then there wasn't enough memory.  Just terminate
-              * the association.
-              */
-             if (err_chunk) {
-                     packet = sctp_abort_pkt_new(net, ep, asoc, arg,
-                                     (__u8 *)(err_chunk->chunk_hdr) +
-                                     sizeof(struct sctp_chunkhdr),
-                                     ntohs(err_chunk->chunk_hdr->length) -
-                                     sizeof(struct sctp_chunkhdr));
-
-                     sctp_chunk_free(err_chunk);
-
-                     if (packet) {
-                             sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
-                                             SCTP_PACKET(packet));
-                             SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
-                             error = SCTP_ERROR_INV_PARAM;
-                     }
-             }
-
-             /* SCTP-AUTH, Section 6.3:
-              *    It should be noted that if the receiver wants to tear
-              *    down an association in an authenticated way only, the
-              *    handling of malformed packets should not result in
-              *    tearing down the association.
-              *
-              * This means that if we only want to abort associations
-              * in an authenticated way (i.e AUTH+ABORT), then we
-              * can't destroy this association just because the packet
-              * was malformed.
-              */

net/sctp/sm_statefuns.c enum sctp_disposition sctp_sf_do_5_1C_ack(struct net *net, · line 536

Quoted 10 lines
net/sctp/sm_statefuns.c +3 -7
-             if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
-                     return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
-
-             SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
-             return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
-                                             asoc, chunk->transport);
-     }
+                           &err_chunk))
+             return sctp_abort_on_init_err(net, ep, asoc, chunk, arg,
+                                           commands, err_chunk);

[High]
In the original sctp_sf_do_5_1C_ack() the "if (err_chunk) { ... }" block is non-controlling: when err_chunk is NULL the code skips the block, falls through with error == SCTP_ERROR_NO_RESOURCE, consults sctp_auth_recv_cid(SCTP_CID_ABORT, asoc) and either pdiscards or runs sctp_stop_t1_and_abort() to tear the half-open association down.

[High]
In the new helper, can the INIT-ACK + NULL err_chunk case still reach that path? Looking at sctp_abort_on_init_err():

if (!err_chunk)

          return sctp_sf_tabort_8_4_8(net, ep, asoc, SCTP_ST_CHUNK(0),
                                      arg, commands);
  ...
  if (chunk->chunk_hdr->type != SCTP_CID_INIT_ACK) {
          ...
  }
  /* SCTP-AUTH, Section 6.3 ... */
  if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
          return sctp_sf_pdiscard(...);
  SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
  return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
                                asoc, chunk->transport);

[High]
The !err_chunk early return runs before the INIT-ACK type check, so when sctp_verify_init() returns 0 with *errp left as NULL on an INIT-ACK, control now goes to sctp_sf_tabort_8_4_8() rather than the auth-check + sctp_stop_t1_and_abort() path.

[High]
Is the NULL-err_chunk case actually reachable on an INIT-ACK? In sctp_process_inv_mandatory() and sctp_process_inv_paramlength() in net/sctp/sm_make_chunk.c, *errp is allocated via sctp_make_op_error_space() or sctp_make_violation_paramlen() and the function returns 0 unconditionally:

  if (!*errp)
          *errp = sctp_make_op_error_space(asoc, chunk, 0);
  if (*errp)
          sctp_init_cause(...);
  return 0;

So an allocation failure on a malformed INIT-ACK reaches the helper with err_chunk == NULL.

[High]
On that path, does the new code skip several actions that the original performed? sctp_sf_tabort_8_4_8() emits an OOTB ABORT and pdiscards; it does not stop T1-init, does not call SCTP_CMD_SET_SK_ERR(ECONNREFUSED), does not issue SCTP_CMD_INIT_FAILED, does not transition the asoc to SCTP_STATE_CLOSED, and does not increment SCTP_MIB_ABORTEDS.

[High]
As a result, would connect(2) wait for T1-init to exhaust its retransmissions instead of receiving ECONNREFUSED promptly, and would SCTP_MIB_ABORTEDS no longer be incremented (with SCTP_MIB_IN_PKT_DISCARDS being incremented in its place)?

[High]
Does this also bypass the SCTP-AUTH section 6.3 check quoted in the comment? Previously, when AUTH-ABORT was negotiated and err_chunk was NULL, sctp_auth_recv_cid() caused sctp_sf_pdiscard() to be returned and no ABORT was emitted on the wire. In the new path, sctp_sf_tabort_8_4_8() sends an unauthenticated OOTB ABORT regardless of AUTH negotiation.

Quoted 9 lines

[High]
Finally, does the disposition return value also change from SCTP_DISPOSITION_ABORT to SCTP_DISPOSITION_CONSUME on this path?

[High]
Would moving the chunk->chunk_hdr->type check ahead of the !err_chunk early return restore the original semantics for INIT-ACK while keeping the sctp_sf_tabort_8_4_8() fallback for INIT?

All above is talking about one regression, the (!err_chunk) check should be after (chunk->chunk_hdr->type != SCTP_CID_INIT_ACK) check.

This is a good catch, and I will fix it on v2.

Thanks.

Quoted 61 lines

[ ... ]

Quoted 59 lines
net/sctp/sm_statefuns.c +55 -0
@@ -6485,6 +6403,61 @@ static void sctp_send_stale_cookie_err(struct net *net,
      }
 }
 
+static enum sctp_disposition sctp_abort_on_init_err(
+                                     struct net *net,
+                                     const struct sctp_endpoint *ep,
+                                     const struct sctp_association *asoc,
+                                     const struct sctp_chunk *chunk,
+                                     void *arg,
+                                     struct sctp_cmd_seq *commands,
+                                     struct sctp_chunk *err_chunk)
+{
+     enum sctp_error error = SCTP_ERROR_NO_RESOURCE;
+     struct sctp_packet *packet;
+     struct sctp_chunkhdr *ch;
+
+     if (!err_chunk)
+             return sctp_sf_tabort_8_4_8(net, ep, asoc, SCTP_ST_CHUNK(0),
+                                         arg, commands);
+
+     ch = err_chunk->chunk_hdr;
+     packet = sctp_abort_pkt_new(net, ep, asoc, arg,
+                                 (__u8 *)ch + sizeof(*ch),
+                                 ntohs(ch->length) - sizeof(*ch));
+
+     sctp_chunk_free(err_chunk);
+
+     if (packet) {
+             sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
+                             SCTP_PACKET(packet));
+             SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
+             error = SCTP_ERROR_INV_PARAM;
+     }
+
+     if (chunk->chunk_hdr->type != SCTP_CID_INIT_ACK) {
+             if (!packet)
+                     return SCTP_DISPOSITION_NOMEM;
+             return SCTP_DISPOSITION_CONSUME;
+     }
+     /* SCTP-AUTH, Section 6.3:
+      *    It should be noted that if the receiver wants to tear
+      *    down an association in an authenticated way only, the
+      *    handling of malformed packets should not result in
+      *    tearing down the association.
+      *
+      * This means that if we only want to abort associations
+      * in an authenticated way (i.e AUTH+ABORT), then we
+      * can't destroy this association just because the packet
+      * was malformed.
+      */
+     if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
+             return sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg,
+                                     commands);
+
+     SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
+     return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
+                                   asoc, chunk->transport);
+}
author maintainer
#6 lore raw

Re: [PATCH net 2/2] sctp: add INIT verification after cookie unpacking

In reply to Simon Horman
Quoted 79 lines · Simon Horman

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://netdev-ai.bots.linux.dev/sashiko/
---
Now I have enough verified information to produce the report.

commit 9d74027bfd0cb22a103cca034883f0fcb4e2535a
Author: Xin Long <[email protected]>

sctp: add INIT verification after cookie unpacking

This commit adds sctp_verify_init() checks after cookie unpacking in the COOKIE-ECHO processing paths when cookie_auth_enable is disabled. It also changes the bounds comparison inside sctp_verify_init() from chunk->chunk_end to peer_init + ntohs(peer_init->chunk_hdr.length).

net/sctp/sm_make_chunk.c int sctp_verify_init(struct net *net, const struct sctp_endpoint *ep, · line 2302

Quoted 11 lines
net/sctp/sm_make_chunk.c +1 -1
index 41958b8e59fd..21b9eb1c02e9 100644
@@ -2298,7 +2298,7 @@ int sctp_verify_init(struct net *net, const struct sctp_endpoint *ep,
       * VIOLATION error.  We build the ERROR chunk here and let the normal
       * error handling code build and send the packet.
       */
-     if (param.v != (void *)chunk->chunk_end)
+     if (param.v != (void *)peer_init + ntohs(peer_init->chunk_hdr.length))
              return sctp_process_inv_paramlength(asoc, param.p, chunk, errp);

[High]
Can this new comparison reject INIT chunks whose final parameter has a length that is not a multiple of 4?

sctp_walk_params() advances pos.v by SCTP_PAD4(ntohs(pos.p->length)) on every iteration:

  include/net/sctp/sctp.h:
  #define _sctp_walk_params(pos, chunk, end)\
  for (pos.v = (u8 *)(chunk + 1);\
       ...
       pos.v += SCTP_PAD4(ntohs(pos.p->length)))

so when the final parameter has an unpadded length L that is not 4-byte aligned, pos.v ends at peer_init + offset_of_last_param + SCTP_PAD4(L), which exceeds peer_init + ntohs(peer_init->chunk_hdr.length) by 1..3.

The previous comparison used chunk->chunk_end, which inqueue.c computes with the padding included:

  net/sctp/inqueue.c:
  chunk->chunk_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));

so the old check accepted unaligned-length final parameters; the new check returns 0 from sctp_verify_init(), and the callers then invoke sctp_abort_on_init_err() and emit an ABORT.

Note that this change is in sctp_verify_init() itself, so it affects every caller, not just the new COOKIE-ECHO sites: sctp_sf_do_5_1B_init, sctp_sf_do_5_1C_ack, sctp_sf_do_unexpected_init, sctp_sf_do_5_1D_ce and sctp_sf_do_5_2_4_dupcook all reach this comparison.

Realistic triggers include:

- Locally-generated INITs constructed by sctp_make_init() when both auth_enable and asconf_enable are set: with the default chunk IDs ASCONF + ASCONF_ACK the AUTH-CHUNKS parameter has length 6 and is typically the last parameter, leaving chunk_hdr.length unaligned.

- Peer INITs containing any final parameter with non-4-aligned length, e.g. SCTP_PARAM_SUPPORTED_ADDRESS_TYPES with 1 or 3 types last, AUTH-CHUNKS with N=1,2,3 entries last, or any unknown TLV.

Even in the COOKIE-ECHO context the new bound seems too strict, since sctp_pack_cookie() copies the INIT verbatim with memcpy(cookie + 1, init_chunk->chunk_hdr, ntohs(...->length)) followed immediately by raw_addrs, so unpacked cookies recovered from peers that sent an INIT with an unaligned-length final parameter would also be spuriously rejected.

Quoted 4 lines

Would comparing against peer_init + SCTP_PAD4(ntohs(peer_init-> chunk_hdr.length)) preserve the long-standing semantics of chunk_end while still validating the cookie-derived peer_init bounds?

There's no problem here, as INIT or INIT_ACK chunk's length is always a multiple of 4, unless it's an abnormal one. We don't need SCTP_PAD4() for this check.

Thanks.