Skip to content

Commit fabe0c0

Browse files
authored
Merge pull request #9646 from rlm2002/coverity
20260112 Coverity: update macros and add length checks
2 parents 16e45f9 + b4344c1 commit fabe0c0

File tree

4 files changed

+58
-25
lines changed

4 files changed

+58
-25
lines changed

src/tls.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6972,8 +6972,10 @@ int TLSX_SupportedVersions_Parse(const WOLFSSL* ssl, const byte* input,
69726972
int set = 0;
69736973

69746974
/* Must contain a length and at least one version. */
6975-
if (length < OPAQUE8_LEN + OPAQUE16_LEN || (length & 1) != 1)
6975+
if (length < OPAQUE8_LEN + OPAQUE16_LEN || (length & 1) != 1
6976+
|| length > MAX_SV_EXT_LEN) {
69766977
return BUFFER_ERROR;
6978+
}
69776979

69786980
len = *input;
69796981

@@ -9963,10 +9965,13 @@ int TLSX_KeyShare_Parse_ClientHello(const WOLFSSL* ssl,
99639965
if (length < OPAQUE16_LEN)
99649966
return BUFFER_ERROR;
99659967

9966-
/* ClientHello contains zero or more key share entries. */
9968+
/* ClientHello contains zero or more key share entries. Limits extension
9969+
* length to 2^16-1 and subtracting 4 bytes for header size per RFC 8446 */
99679970
ato16(input, &len);
9968-
if (len != length - OPAQUE16_LEN)
9971+
if ((len != length - OPAQUE16_LEN) ||
9972+
length > (MAX_EXT_DATA_LEN - HELLO_EXT_SZ)) {
99699973
return BUFFER_ERROR;
9974+
}
99709975
offset += OPAQUE16_LEN;
99719976

99729977
while (offset < (int)length) {

wolfcrypt/test/test.c

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20187,10 +20187,9 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_bank_test(void)
2018720187
byte outbuf1[16], outbuf2[16];
2018820188
int i;
2018920189

20190-
WC_ALLOC_VAR_EX(bank, struct wc_rng_bank, 1, HEAP_HINT,
20190+
WC_CALLOC_VAR_EX(bank, struct wc_rng_bank, 1, HEAP_HINT,
2019120191
DYNAMIC_TYPE_TMP_BUFFER,
2019220192
return WC_TEST_RET_ENC_EC(MEMORY_E));
20193-
XMEMSET(bank, 0, sizeof(*bank));
2019420193

2019520194
#ifdef WC_DRBG_BANKREF
2019620195
WC_ALLOC_VAR_EX(rng, WC_RNG, 1, HEAP_HINT,
@@ -52298,44 +52297,67 @@ static wc_test_ret_t sakke_kat_derive_test(SakkeKey* key, ecc_point* rsk)
5229852297
return WC_TEST_RET_ENC_EC(ret);
5229952298
if (iTableLen != 0) {
5230052299
iTable = (byte*)XMALLOC(iTableLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
52301-
if (iTable == NULL)
52302-
return WC_TEST_RET_ENC_ERRNO;
52300+
if (iTable == NULL) {
52301+
ret = WC_TEST_RET_ENC_ERRNO;
52302+
goto out;
52303+
}
5230352304
ret = wc_GenerateSakkePointITable(key, iTable, &iTableLen);
52304-
if (ret != 0)
52305-
return WC_TEST_RET_ENC_EC(ret);
52305+
if (ret != 0) {
52306+
ret = WC_TEST_RET_ENC_EC(ret);
52307+
goto out;
52308+
}
5230652309
}
5230752310
len = 0;
5230852311
ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
52309-
if (ret != WC_NO_ERR_TRACE(LENGTH_ONLY_E))
52310-
return WC_TEST_RET_ENC_EC(ret);
52312+
if (ret != WC_NO_ERR_TRACE(LENGTH_ONLY_E)) {
52313+
ret = WC_TEST_RET_ENC_EC(ret);
52314+
goto out;
52315+
}
5231152316
if (len > 0) {
5231252317
table = (byte*)XMALLOC(len, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
52313-
if (table == NULL)
52314-
return WC_TEST_RET_ENC_ERRNO;
52318+
if (table == NULL) {
52319+
ret = WC_TEST_RET_ENC_ERRNO;
52320+
goto out;
52321+
}
5231552322
ret = wc_GenerateSakkeRskTable(key, rsk, table, &len);
52316-
if (ret != 0)
52317-
return WC_TEST_RET_ENC_EC(ret);
52323+
if (ret != 0) {
52324+
ret = WC_TEST_RET_ENC_EC(ret);
52325+
goto out;
52326+
}
5231852327
}
5231952328

5232052329
ret = wc_SetSakkeRsk(key, rsk, table, len);
52321-
if (ret != 0)
52322-
return WC_TEST_RET_ENC_EC(ret);
52330+
if (ret != 0) {
52331+
ret = WC_TEST_RET_ENC_EC(ret);
52332+
goto out;
52333+
}
5232352334

5232452335
XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
5232552336
ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
5232652337
auth, sizeof(auth));
52327-
if (ret != 0)
52328-
return WC_TEST_RET_ENC_EC(ret);
52329-
if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
52330-
return WC_TEST_RET_ENC_NC;
52338+
if (ret != 0) {
52339+
ret = WC_TEST_RET_ENC_EC(ret);
52340+
goto out;
52341+
}
52342+
if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0) {
52343+
ret = WC_TEST_RET_ENC_NC;
52344+
goto out;
52345+
}
5233152346

5233252347
/* Don't reference table that is about to be freed. */
5233352348
ret = wc_ClearSakkePointITable(key);
52334-
if (ret != 0)
52335-
return WC_TEST_RET_ENC_EC(ret);
52349+
if (ret != 0) {
52350+
ret = WC_TEST_RET_ENC_EC(ret);
52351+
}
52352+
52353+
out:
5233652354
/* Dispose of tables */
5233752355
XFREE(iTable, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
5233852356
XFREE(table, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
52357+
/* return error code if encountered */
52358+
if (ret != 0) {
52359+
return ret;
52360+
}
5233952361

5234052362
/* Make sure the key public key is exportable - convert to Montgomery form
5234152363
* in Validation.

wolfssl/internal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,12 @@ enum Misc {
16681668
MAX_REQUEST_SZ = 256, /* Maximum cert req len (no auth yet */
16691669
SESSION_FLUSH_COUNT = 256, /* Flush session cache unless user turns off */
16701670
TLS_MAX_PAD_SZ = 255, /* Max padding in TLS */
1671+
MAX_EXT_DATA_LEN = 65535,
1672+
/* Max extension data length <0..2^16-1> RFC 8446
1673+
* Section 4.2 */
1674+
MAX_SV_EXT_LEN = 255,
1675+
/* Max supported_versions extension length
1676+
* <2..254> RFC 8446 Section 4.2.1.*/
16711677

16721678
#if defined(HAVE_NULL_CIPHER) && defined(WOLFSSL_TLS13)
16731679
#if defined(WOLFSSL_SHA384) && WC_MAX_SYM_KEY_SIZE < 48

wolfssl/wolfcrypt/types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -912,9 +912,9 @@ enum {
912912
WC_DO_NOTHING
913913
#define WC_VAR_OK(VAR_NAME) 1
914914
#define WC_CALLOC_VAR(VAR_NAME, VAR_TYPE, VAR_SIZE, HEAP) \
915-
XMEMSET(VAR_NAME, 0, sizeof(var))
915+
XMEMSET(VAR_NAME, 0, sizeof(VAR_TYPE))
916916
#define WC_CALLOC_VAR_EX(VAR_NAME, VAR_TYPE, VAR_SIZE, HEAP, TY, ONFAIL)\
917-
WC_DO_NOTHING
917+
XMEMSET(VAR_NAME, 0, sizeof(VAR_TYPE))
918918
#define WC_FREE_VAR(VAR_NAME, HEAP) WC_DO_NOTHING \
919919
/* nothing to free, its stack */
920920
#define WC_FREE_VAR_EX(VAR_NAME, HEAP, TYPE) WC_DO_NOTHING

0 commit comments

Comments
 (0)