Skip to content

Commit f2d2440

Browse files
committed
Fix Coverity (D)TLS fragmentation size checks
Add MAX_RECORD_SIZE-based bounds checks in SendHandshakeMsg and Dtls13SendFragmentedInternal to prevent negative/overflowed fragment sizes from reaching memcpy/BuildMessage/DtlsMsgPoolSave.
1 parent e70e7cb commit f2d2440

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/dtls13.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,8 @@ static int Dtls13SendOneFragmentRtx(WOLFSSL* ssl,
978978
static int Dtls13SendFragmentedInternal(WOLFSSL* ssl)
979979
{
980980
int fragLength, rlHeaderLength;
981-
int remainingSize, maxFragment;
981+
word32 remainingSize;
982+
int maxFragment;
982983
int recordLength, outputSz;
983984
byte isEncrypted;
984985
byte* output;
@@ -988,16 +989,19 @@ static int Dtls13SendFragmentedInternal(WOLFSSL* ssl)
988989
(enum HandShakeType)ssl->dtls13FragHandshakeType);
989990
rlHeaderLength = Dtls13GetRlHeaderLength(ssl, isEncrypted);
990991
maxFragment = wolfssl_local_GetMaxPlaintextSize(ssl);
991-
992+
if (maxFragment <= DTLS_HANDSHAKE_HEADER_SZ ||
993+
maxFragment > MAX_RECORD_SIZE ||
994+
ssl->dtls13FragOffset > ssl->dtls13MessageLength) {
995+
Dtls13FreeFragmentsBuffer(ssl);
996+
return BUFFER_E;
997+
}
992998
remainingSize = ssl->dtls13MessageLength - ssl->dtls13FragOffset;
993999

9941000
while (remainingSize > 0) {
9951001

9961002
fragLength = maxFragment - DTLS_HANDSHAKE_HEADER_SZ;
997-
998-
if (fragLength > remainingSize) {
999-
fragLength = remainingSize;
1000-
}
1003+
if (fragLength > (int)remainingSize)
1004+
fragLength = (int)remainingSize;
10011005

10021006
recordLength = fragLength + rlHeaderLength + DTLS_HANDSHAKE_HEADER_SZ;
10031007
outputSz = wolfssl_local_GetRecordSize(ssl,
@@ -1041,7 +1045,7 @@ static int Dtls13SendFragmentedInternal(WOLFSSL* ssl)
10411045
}
10421046

10431047
ssl->dtls13FragOffset += fragLength;
1044-
remainingSize -= fragLength;
1048+
remainingSize -= (word32)fragLength;
10451049
}
10461050

10471051
/* we sent all fragments */

src/internal.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10766,6 +10766,8 @@ static int SendHandshakeMsg(WOLFSSL* ssl, byte* input, word32 inputSz,
1076610766
maxFrag -= DTLS_HANDSHAKE_HEADER_SZ;
1076710767
}
1076810768
#endif
10769+
if (maxFrag <= 0 || maxFrag > MAX_RECORD_SIZE)
10770+
return BUFFER_E;
1076910771

1077010772
/* Make sure input is not the ssl output buffer as this
1077110773
* function doesn't handle that */
@@ -10801,6 +10803,8 @@ static int SendHandshakeMsg(WOLFSSL* ssl, byte* input, word32 inputSz,
1080110803
fragSz = inputSz - ssl->fragOffset;
1080210804

1080310805
/* check for available size */
10806+
if (fragSz > (word32)MAX_RECORD_SIZE)
10807+
return BUFFER_E;
1080410808
outputSz = headerSz + (int)fragSz;
1080510809
if (IsEncryptionOn(ssl, 1))
1080610810
outputSz += cipherExtraData(ssl);
@@ -10816,6 +10820,8 @@ static int SendHandshakeMsg(WOLFSSL* ssl, byte* input, word32 inputSz,
1081610820
int dataSz = (int)fragSz;
1081710821
#ifdef WOLFSSL_DTLS
1081810822
if (ssl->options.dtls) {
10823+
if (fragSz + DTLS_HANDSHAKE_HEADER_SZ > (word32)MAX_RECORD_SIZE)
10824+
return BUFFER_E;
1081910825
data -= DTLS_HANDSHAKE_HEADER_SZ;
1082010826
dataSz += DTLS_HANDSHAKE_HEADER_SZ;
1082110827
AddHandShakeHeader(data, inputSz, ssl->fragOffset, fragSz,

0 commit comments

Comments
 (0)