]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
sctp: validate cookie AUTH state before use
authorJérémy Jean <Jeremy.Jean@oss.cyber.gouv.fr>
Tue, 4 Aug 2026 20:00:42 +0000 (20:00 +0000)
committerJakub Kicinski <kuba@kernel.org>
Fri, 7 Aug 2026 22:02:32 +0000 (15:02 -0700)
When cookie authentication is disabled, COOKIE_ECHO restores fixed-size
AUTH fields directly from peer-controlled cookie bytes.  A forged RANDOM
length, HMAC list, or CHUNKS list can then reach association consumers
with lengths or identifiers that were never validated against the local
backing arrays.

A forged RANDOM length can cause out-of-bounds reads during key-vector
construction.  A forged HMAC identifier also caused a 32-byte write past
a zero-length AUTH chunk, providing a primitive for a local privilege
escalation chain.

Validate the cookie's RANDOM, HMACS, and CHUNKS parameters at the cookie
trust boundary before copying them into the association.  Reject invalid
types, malformed lengths, unsupported HMAC identifiers, HMAC lists
without SHA1, and forbidden chunk ids.

Fixes: bbd0d59809f9 ("[SCTP]: Implement the receive and verification of AUTH chunk")
Fixes: 1f485649f529 ("[SCTP]: Implement SCTP-AUTH internals")
Signed-off-by: Jérémy Jean <Jeremy.Jean@oss.cyber.gouv.fr>
Acked-by: Xin Long <lucien.xin@gmail.com>
Link: https://patch.msgid.link/20260804200042.2412009-1-Jeremy.Jean@oss.cyber.gouv.fr
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
include/net/sctp/auth.h
net/sctp/auth.c
net/sctp/sm_make_chunk.c

index 6f2cd562b1de6e51982bc21a35fb26ea1a643ba8..74b3790e2a3d5d7293ca8e85be7e448e6b96923c 100644 (file)
@@ -22,6 +22,7 @@ struct sctp_endpoint;
 struct sctp_association;
 struct sctp_authkey;
 struct sctp_hmacalgo;
+struct sctp_cookie;
 
 /* Defines an HMAC algorithm supported by SCTP chunk authentication */
 struct sctp_hmac {
@@ -72,6 +73,8 @@ struct sctp_shared_key *sctp_auth_get_shkey(
 int sctp_auth_asoc_copy_shkeys(const struct sctp_endpoint *ep,
                                struct sctp_association *asoc,
                                gfp_t gfp);
+bool sctp_auth_verify_cookie_params(const struct sctp_endpoint *ep,
+                                   const struct sctp_cookie *cookie);
 const struct sctp_hmac *sctp_auth_get_hmac(__u16 hmac_id);
 const struct sctp_hmac *
 sctp_auth_asoc_get_hmac(const struct sctp_association *asoc);
index c901d373af803cdc92ba99d96a3d2eec0f69fcd1..cc4229ee116d1f61161de0704a92c2cc71d84697 100644 (file)
@@ -377,6 +377,81 @@ nomem:
        return -ENOMEM;
 }
 
+static bool sctp_auth_chunk_id_forbidden(__u8 chunk_id)
+{
+       switch (chunk_id) {
+       case SCTP_CID_INIT:
+       case SCTP_CID_INIT_ACK:
+       case SCTP_CID_SHUTDOWN_COMPLETE:
+       case SCTP_CID_AUTH:
+               return true;
+       default:
+               return false;
+       }
+}
+
+/* Verify AUTH parameters copied from a state cookie before they are restored
+ * into an association.  When cookie authentication is disabled these fields
+ * are peer-controlled, so they must satisfy the same constraints as locally
+ * generated AUTH parameters.
+ */
+bool sctp_auth_verify_cookie_params(const struct sctp_endpoint *ep,
+                                   const struct sctp_cookie *cookie)
+{
+       const struct sctp_paramhdr *random;
+       const struct sctp_hmac_algo_param *hmacs;
+       const struct sctp_chunks_param *chunks;
+       u16 hmacs_len, chunks_len;
+       u16 n_hmacs, n_chunks, i;
+       bool has_sha1 = false;
+
+       if (sctp_sk(ep->base.sk)->cookie_auth_enable || !ep->auth_enable)
+               return true;
+
+       random = (const struct sctp_paramhdr *)cookie->auth_random;
+       if (random->type != SCTP_PARAM_RANDOM ||
+           ntohs(random->length) != sizeof(*random) + SCTP_AUTH_RANDOM_LENGTH)
+               return false;
+
+       hmacs = (const struct sctp_hmac_algo_param *)cookie->auth_hmacs;
+       hmacs_len = ntohs(hmacs->param_hdr.length);
+       if (hmacs->param_hdr.type != SCTP_PARAM_HMAC_ALGO ||
+           hmacs_len < sizeof(struct sctp_paramhdr) +
+                       sizeof(hmacs->hmac_ids[0]) ||
+           hmacs_len > sizeof(cookie->auth_hmacs) ||
+           (hmacs_len - sizeof(struct sctp_paramhdr)) %
+                       sizeof(hmacs->hmac_ids[0]))
+               return false;
+
+       n_hmacs = (hmacs_len - sizeof(struct sctp_paramhdr)) /
+                 sizeof(hmacs->hmac_ids[0]);
+       for (i = 0; i < n_hmacs; i++) {
+               u16 hmac_id = ntohs(hmacs->hmac_ids[i]);
+
+               if (!sctp_hmac_supported(hmac_id))
+                       return false;
+               if (hmac_id == SCTP_AUTH_HMAC_ID_SHA1)
+                       has_sha1 = true;
+       }
+       if (!has_sha1)
+               return false;
+
+       chunks = (const struct sctp_chunks_param *)cookie->auth_chunks;
+       chunks_len = ntohs(chunks->param_hdr.length);
+       if (chunks->param_hdr.type != SCTP_PARAM_CHUNKS ||
+           chunks_len < sizeof(struct sctp_paramhdr) ||
+           chunks_len > sizeof(cookie->auth_chunks))
+               return false;
+
+       n_chunks = chunks_len - sizeof(struct sctp_paramhdr);
+       for (i = 0; i < n_chunks; i++) {
+               if (sctp_auth_chunk_id_forbidden(chunks->chunks[i]))
+                       return false;
+       }
+
+       return true;
+}
+
 
 /* Public interface to create the association shared key.
  * See code above for the algorithm.
index e25612e9d0821fa6dcb06a375cbcec11315c1365..236e25abc7a428872ba386391827df555da266ba 100644 (file)
@@ -1852,6 +1852,9 @@ struct sctp_association *sctp_unpack_cookie(
        /* Set up our peer's port number.  */
        retval->peer.port = ntohs(chunk->sctp_hdr->source);
 
+       if (!sctp_auth_verify_cookie_params(ep, bear_cookie))
+               goto malformed;
+
        /* Populate the association from the cookie.  */
        memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));