Skip to content

Commit 2f51508

Browse files
committed
tls: fix TLSX_ALPN_GetSize word16 overflow (F-2128)
Match the TLSX_SNI_GetSize pattern: use a word32 accumulator and return 0 if the aggregate size exceeds WOLFSSL_MAX_16BIT, so a large number of ALPN entries can no longer silently wrap the length computation.
1 parent fb64844 commit 2f51508

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

src/tls.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,16 +1809,20 @@ static void TLSX_ALPN_FreeAll(ALPN *list, void* heap)
18091809
static word16 TLSX_ALPN_GetSize(ALPN *list)
18101810
{
18111811
ALPN* alpn;
1812-
word16 length = OPAQUE16_LEN; /* list length */
1812+
word32 length = OPAQUE16_LEN; /* list length */
18131813

18141814
while ((alpn = list)) {
18151815
list = alpn->next;
18161816

18171817
length++; /* protocol name length is on one byte */
1818-
length += (word16)XSTRLEN(alpn->protocol_name);
1818+
length += (word32)XSTRLEN(alpn->protocol_name);
1819+
1820+
if (length > WOLFSSL_MAX_16BIT) {
1821+
return 0;
1822+
}
18191823
}
18201824

1821-
return length;
1825+
return (word16)length;
18221826
}
18231827

18241828
/** Writes the ALPN objects of a list in a buffer. */
@@ -14838,9 +14842,16 @@ static int TLSX_GetSize(TLSX* list, byte* semaphore, byte msgType,
1483814842
isRequest);
1483914843
break;
1484014844

14841-
case TLSX_APPLICATION_LAYER_PROTOCOL:
14842-
length += ALPN_GET_SIZE((ALPN*)extension->data);
14845+
case TLSX_APPLICATION_LAYER_PROTOCOL: {
14846+
word16 alpnSz = ALPN_GET_SIZE((ALPN*)extension->data);
14847+
/* 0 on non-empty list means 16-bit overflow. */
14848+
if (alpnSz == 0 && extension->data != NULL) {
14849+
ret = LENGTH_ERROR;
14850+
break;
14851+
}
14852+
length += alpnSz;
1484314853
break;
14854+
}
1484414855
#if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
1484514856
case TLSX_SIGNATURE_ALGORITHMS:
1484614857
length += SA_GET_SIZE(extension->data);

0 commit comments

Comments
 (0)