Skip to content

Commit 5c486cb

Browse files
authored
Merge pull request #7371 from douzzer/20240327-tls-int-overflows
20240327-tls-int-overflows
2 parents 3f3dd47 + 038be95 commit 5c486cb

2 files changed

Lines changed: 25 additions & 10 deletions

File tree

src/internal.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10566,8 +10566,7 @@ static WC_INLINE int GrowOutputBuffer(WOLFSSL* ssl, int size)
1056610566
#else
1056710567
const byte align = WOLFSSL_GENERAL_ALIGNMENT;
1056810568
#endif
10569-
int newSz = size + ssl->buffers.outputBuffer.idx +
10570-
ssl->buffers.outputBuffer.length;
10569+
word32 newSz;
1057110570

1057210571
#if WOLFSSL_GENERAL_ALIGNMENT > 0
1057310572
/* the encrypted data will be offset from the front of the buffer by
@@ -10578,7 +10577,15 @@ static WC_INLINE int GrowOutputBuffer(WOLFSSL* ssl, int size)
1057810577
align *= 2;
1057910578
#endif
1058010579

10581-
tmp = (byte*)XMALLOC(newSz + align, ssl->heap, DYNAMIC_TYPE_OUT_BUFFER);
10580+
if (! WC_SAFE_SUM_WORD32(ssl->buffers.outputBuffer.idx,
10581+
ssl->buffers.outputBuffer.length, newSz))
10582+
return BUFFER_E;
10583+
if (! WC_SAFE_SUM_WORD32(newSz, (word32)size, newSz))
10584+
return BUFFER_E;
10585+
if (! WC_SAFE_SUM_WORD32(newSz, align, newSz))
10586+
return BUFFER_E;
10587+
tmp = (byte*)XMALLOC(newSz, ssl->heap, DYNAMIC_TYPE_OUT_BUFFER);
10588+
newSz -= align;
1058210589
WOLFSSL_MSG("growing output buffer");
1058310590

1058410591
if (tmp == NULL)
@@ -28389,7 +28396,7 @@ static int SigAlgoCachesMsgs(int sigAlgo)
2838928396
}
2839028397

2839128398
static int HashSkeData(WOLFSSL* ssl, enum wc_HashType hashType,
28392-
const byte* data, int sz, byte sigAlgo)
28399+
const byte* data, word32 sz, byte sigAlgo)
2839328400
{
2839428401
int ret = 0;
2839528402
int digest_sz = wc_HashGetDigestSize(hashType);
@@ -28399,11 +28406,16 @@ static int HashSkeData(WOLFSSL* ssl, enum wc_HashType hashType,
2839928406
}
2840028407

2840128408
if (ret == 0) {
28409+
word32 new_size = SEED_LEN;
2840228410
/* buffer for signature */
28403-
ssl->buffers.sig.buffer = (byte*)XMALLOC(SEED_LEN + sz, ssl->heap,
28404-
DYNAMIC_TYPE_SIGNATURE);
28405-
if (ssl->buffers.sig.buffer == NULL) {
28411+
if (! WC_SAFE_SUM_WORD32(new_size, sz, new_size))
2840628412
ret = MEMORY_E;
28413+
else {
28414+
ssl->buffers.sig.buffer = (byte*)XMALLOC(new_size, ssl->heap,
28415+
DYNAMIC_TYPE_SIGNATURE);
28416+
if (ssl->buffers.sig.buffer == NULL) {
28417+
ret = MEMORY_E;
28418+
}
2840728419
}
2840828420
}
2840928421
if (ret == 0) {
@@ -30439,14 +30451,14 @@ static int DoServerKeyExchange(WOLFSSL* ssl, const byte* input,
3043930451
ERROR_OUT(NOT_COMPILED_IN, exit_dske);
3044030452
#else
3044130453
enum wc_HashType hashType;
30442-
word16 verifySz;
30454+
word32 verifySz;
3044330455
byte sigAlgo;
3044430456

3044530457
if (ssl->options.usingAnon_cipher) {
3044630458
break;
3044730459
}
3044830460

30449-
verifySz = (word16)(args->idx - args->begin);
30461+
verifySz = (args->idx - args->begin);
3045030462
if (verifySz > MAX_DH_SZ) {
3045130463
ERROR_OUT(BUFFER_ERROR, exit_dske);
3045230464
}
@@ -33382,7 +33394,7 @@ static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
3338233394
#if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
3338333395
word32 exportSz;
3338433396
#endif
33385-
int sendSz;
33397+
word32 sendSz;
3338633398
int inputSz;
3338733399
} SskeArgs;
3338833400

wolfssl/wolfcrypt/types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,9 @@ typedef struct w64wrapper {
430430

431431
#define XELEM_CNT(x) (sizeof((x))/sizeof(*(x)))
432432

433+
#define WC_SAFE_SUM_WORD32(in1, in2, out) ((in2) <= 0xffffffffU - (in1) ? \
434+
((out) = (in1) + (in2), 1) : ((out) = 0xffffffffU, 0))
435+
433436
/* idea to add global alloc override by Moises Guimaraes */
434437
/* default to libc stuff */
435438
/* XREALLOC is used once in normal math lib, not in fast math lib */

0 commit comments

Comments
 (0)