Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

20250109-memory-errors #8349

Merged
merged 3 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/crl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,7 @@ static int DupX509_CRL(WOLFSSL_X509_CRL *dupl, const WOLFSSL_X509_CRL* crl)
if (dupl->monitors[0].path != NULL) {
XFREE(dupl->monitors[0].path, dupl->heap,
DYNAMIC_TYPE_CRL_MONITOR);
dupl->monitors[0].path = NULL;
}
return MEMORY_E;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -7991,7 +7991,7 @@ int wolfSSL_i2d_PKCS8_PKEY(WOLFSSL_PKCS8_PRIV_KEY_INFO* key, unsigned char** pp)
return WOLFSSL_FATAL_ERROR;
len = (int)keySz;

if (pp == NULL)
if ((pp == NULL) || (len == 0))
return len;

if (*pp == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion src/tls13.c
Original file line number Diff line number Diff line change
Expand Up @@ -6960,7 +6960,7 @@ int DoTls13ClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
echX = TLSX_Find(ssl->extensions, TLSX_ECH);

if (echX == NULL)
return WOLFSSL_FATAL_ERROR;
ERROR_OUT(WOLFSSL_FATAL_ERROR, exit_dch);

((WOLFSSL_ECH*)echX->data)->aad = input + HANDSHAKE_HEADER_SZ;
((WOLFSSL_ECH*)echX->data)->aadLen = helloSz;
Expand Down
1 change: 0 additions & 1 deletion src/x509.c
Original file line number Diff line number Diff line change
Expand Up @@ -1690,7 +1690,6 @@ WOLFSSL_v3_ext_method* wolfSSL_X509V3_EXT_get(WOLFSSL_X509_EXTENSION* ex)
WOLFSSL_MSG("Failed to get nid from passed extension object");
return NULL;
}
XMEMSET(&method, 0, sizeof(WOLFSSL_v3_ext_method));
switch (nid) {
case WC_NID_basic_constraints:
break;
Expand Down
68 changes: 39 additions & 29 deletions tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -33411,7 +33411,7 @@ static int test_wc_dilithium_check_key(void)
&privCheckKeyLen, pubCheckKey, &pubCheckKeyLen), 0);

/* Modify hash. */
if (pubCheckKey != NULL) {
if ((pubCheckKey != NULL) && EXPECT_SUCCESS()) {
pubCheckKey[0] ^= 0x80;
ExpectIntEQ(wc_dilithium_import_key(NULL, 0, NULL, 0, NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
Expand Down Expand Up @@ -78037,7 +78037,6 @@ static int test_wolfSSL_d2i_OCSP_CERTID(void)
{
EXPECT_DECLS;
#if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
WOLFSSL_OCSP_CERTID* certId;
WOLFSSL_OCSP_CERTID* certIdGood;
WOLFSSL_OCSP_CERTID* certIdBad;
const unsigned char* rawCertIdPtr;
Expand All @@ -78056,40 +78055,49 @@ static int test_wolfSSL_d2i_OCSP_CERTID(void)

/* If the cert ID is NULL the function should allocate it and copy the
* data to it. */
certId = NULL;
ExpectNotNull(certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
sizeof(rawCertId)));
ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
if (certId != NULL) {
XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
{
WOLFSSL_OCSP_CERTID* certId = NULL;
ExpectNotNull(certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
sizeof(rawCertId)));
ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
if (certId != NULL) {
XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
}
}

/* If the cert ID is not NULL the function will just copy the data to it. */
ExpectNotNull(certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
DYNAMIC_TYPE_TMP_BUFFER));
ExpectNotNull(certId);
ExpectNotNull(XMEMSET(certId, 0, sizeof(*certId)));

/* Reset rawCertIdPtr since it was push forward in the previous call. */
rawCertIdPtr = &rawCertId[0];
ExpectNotNull(certIdGood = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
sizeof(rawCertId)));
ExpectPtrEq(certIdGood, certId);
ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
if (certId != NULL) {
XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
certId = NULL;
{
WOLFSSL_OCSP_CERTID* certId = NULL;
ExpectNotNull(certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
DYNAMIC_TYPE_TMP_BUFFER));
ExpectNotNull(certId);
if (certId != NULL)
XMEMSET(certId, 0, sizeof(*certId));

/* Reset rawCertIdPtr since it was push forward in the previous call. */
rawCertIdPtr = &rawCertId[0];
ExpectNotNull(certIdGood = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
sizeof(rawCertId)));
ExpectPtrEq(certIdGood, certId);
ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
if (certId != NULL) {
XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
certId = NULL;
}
}

/* The below tests should fail when passed bad parameters. NULL should
* always be returned. */
ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr,
sizeof(rawCertId)));
ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL,
sizeof(rawCertId)));
ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0));
{
WOLFSSL_OCSP_CERTID* certId = NULL;
ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr,
sizeof(rawCertId)));
ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL,
sizeof(rawCertId)));
ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0));
}
#endif
return EXPECT_RESULT();
}
Expand Down Expand Up @@ -84988,6 +84996,7 @@ static int test_wolfSSL_PEM_X509_INFO_read_bio(void)

ExpectIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
subject = NULL;
X509_INFO_free(info);
info = NULL;

Expand All @@ -84997,6 +85006,7 @@ static int test_wolfSSL_PEM_X509_INFO_read_bio(void)

ExpectIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
subject = NULL;
X509_INFO_free(info);
ExpectNull(info = sk_X509_INFO_pop(sk));

Expand Down
31 changes: 22 additions & 9 deletions wolfcrypt/benchmark/benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -1725,7 +1725,9 @@ static const char* bench_result_words3[][5] = {
#endif

#ifdef LINUX_RUSAGE_UTIME
static void check_for_excessive_stime(const char *desc,
static void check_for_excessive_stime(const char *algo,
int strength,
const char *desc,
const char *desc_extra);
#endif

Expand Down Expand Up @@ -2518,7 +2520,7 @@ static void bench_stats_sym_finish(const char* desc, int useDeviceID,
#endif

#ifdef LINUX_RUSAGE_UTIME
check_for_excessive_stime(desc, "");
check_for_excessive_stime(desc, 0, "", "");
#endif

/* calculate actual bytes */
Expand Down Expand Up @@ -2744,7 +2746,7 @@ static void bench_stats_asym_finish_ex(const char* algo, int strength,
total = current_time(0) - start;

#ifdef LINUX_RUSAGE_UTIME
check_for_excessive_stime(desc, desc_extra);
check_for_excessive_stime(algo, strength, desc, desc_extra);
#endif

#ifdef GENERATE_MACHINE_PARSEABLE_REPORT
Expand Down Expand Up @@ -14672,7 +14674,9 @@ void bench_sphincsKeySign(byte level, byte optim)
(double)rusage.ru_utime.tv_usec / MILLION_VALUE;
}

static void check_for_excessive_stime(const char *desc,
static void check_for_excessive_stime(const char *algo,
int strength,
const char *desc,
const char *desc_extra)
{
double start_utime = (double)base_rusage.ru_utime.tv_sec +
Expand All @@ -14685,11 +14689,20 @@ void bench_sphincsKeySign(byte level, byte optim)
(double)cur_rusage.ru_stime.tv_usec / MILLION_VALUE;
double stime_utime_ratio =
(cur_stime - start_stime) / (cur_utime - start_utime);
if (stime_utime_ratio > .1)
printf("%swarning, "
"excessive system time ratio for %s%s (" FLT_FMT_PREC "%%).\n",
err_prefix, desc, desc_extra,
FLT_FMT_PREC_ARGS(3, stime_utime_ratio * 100.0));
if (stime_utime_ratio > .1) {
if (strength > 0) {
printf("%swarning, "
"excessive system time ratio for %s-%d-%s%s (" FLT_FMT_PREC "%%).\n",
err_prefix, algo, strength, desc, desc_extra,
FLT_FMT_PREC_ARGS(3, stime_utime_ratio * 100.0));
}
else {
printf("%swarning, "
"excessive system time ratio for %s%s%s (" FLT_FMT_PREC "%%).\n",
err_prefix, algo, desc, desc_extra,
FLT_FMT_PREC_ARGS(3, stime_utime_ratio * 100.0));
}
}
}

#elif defined(WOLFSSL_LINUXKM)
Expand Down
34 changes: 22 additions & 12 deletions wolfcrypt/src/asn.c
Original file line number Diff line number Diff line change
Expand Up @@ -7429,7 +7429,7 @@ int wc_CreatePKCS8Key(byte* out, word32* outSz, byte* key, word32 keySz,
/* Get the size of the DER encoding. */
ret = SizeASN_Items(pkcs8KeyASN, dataASN, pkcs8KeyASN_Length-1, &sz);
}
if (ret == 0) {
if ((ret == 0) || (ret == WC_NO_ERR_TRACE(LENGTH_ONLY_E))) {
/* Always return the calculated size. */
*outSz = (word32)sz;
}
Expand Down Expand Up @@ -28804,6 +28804,13 @@ int SetNameEx(byte* output, word32 outputSz, CertName* name, void* heap)
ret = 0;
}

if (items == 0) {
/* if zero items, short-circuit return to avoid frivolous zero-size
* allocations.
*/
return 0;
}

/* Allocate dynamic data items. */
dataASN = (ASNSetData*)XMALLOC(items * sizeof(ASNSetData), heap,
DYNAMIC_TYPE_TMP_BUFFER);
Expand Down Expand Up @@ -34171,23 +34178,26 @@ static int EccSpecifiedECDomainDecode(const byte* input, word32 inSz,
}
#endif /* WOLFSSL_ECC_CURVE_STATIC */

if ((ret == 0) && (curveSz)) {
*curveSz = curve->size;
}

if (key) {
/* Store parameter set in key. */
if ((ret == 0) && (wc_ecc_set_custom_curve(key, curve) < 0)) {
ret = ASN_PARSE_E;
}
if (ret == 0) {
/* The parameter set was allocated.. */
key->deallocSet = 1;
if (wc_ecc_set_custom_curve(key, curve) < 0) {
ret = ASN_PARSE_E;
}
else {
/* The parameter set was allocated.. */
key->deallocSet = 1;
/* Don't deallocate below. */
curve = NULL;
}
}
}

if ((ret == 0) && (curveSz)) {
*curveSz = curve->size;
}

if ((ret != 0) && (curve != NULL)) {
/* Failed to set parameters so free parameter set. */
if (curve != NULL) { /* NOLINT(clang-analyzer-unix.Malloc) */
wc_ecc_free_curve(curve, heap);
}

Expand Down
54 changes: 30 additions & 24 deletions wolfcrypt/src/dh.c
Original file line number Diff line number Diff line change
Expand Up @@ -2036,19 +2036,21 @@ static int wc_DhAgree_Sync(DhKey* key, byte* agree, word32* agreeSz,
#ifndef WOLFSSL_SP_NO_2048
if (mp_count_bits(&key->p) == 2048) {
if (mp_init(y) != MP_OKAY)
return MP_INIT_E;
ret = MP_INIT_E;

SAVE_VECTOR_REGISTERS(ret = _svr_ret;);
if (ret == 0) {
SAVE_VECTOR_REGISTERS(ret = _svr_ret;);

if (ret == 0 && mp_read_unsigned_bin(y, otherPub, pubSz) != MP_OKAY)
ret = MP_READ_E;
if (ret == 0 && mp_read_unsigned_bin(y, otherPub, pubSz) != MP_OKAY)
ret = MP_READ_E;

if (ret == 0)
ret = sp_DhExp_2048(y, priv, privSz, &key->p, agree, agreeSz);
if (ret == 0)
ret = sp_DhExp_2048(y, priv, privSz, &key->p, agree, agreeSz);

mp_clear(y);
mp_clear(y);

RESTORE_VECTOR_REGISTERS();
RESTORE_VECTOR_REGISTERS();
}

/* make sure agree is > 1 (SP800-56A, 5.7.1.1) */
if ((ret == 0) &&
Expand All @@ -2070,19 +2072,21 @@ static int wc_DhAgree_Sync(DhKey* key, byte* agree, word32* agreeSz,
#ifndef WOLFSSL_SP_NO_3072
if (mp_count_bits(&key->p) == 3072) {
if (mp_init(y) != MP_OKAY)
return MP_INIT_E;
ret = MP_INIT_E;

SAVE_VECTOR_REGISTERS(ret = _svr_ret;);
if (ret == 0) {
SAVE_VECTOR_REGISTERS(ret = _svr_ret;);

if (ret == 0 && mp_read_unsigned_bin(y, otherPub, pubSz) != MP_OKAY)
ret = MP_READ_E;
if (ret == 0 && mp_read_unsigned_bin(y, otherPub, pubSz) != MP_OKAY)
ret = MP_READ_E;

if (ret == 0)
ret = sp_DhExp_3072(y, priv, privSz, &key->p, agree, agreeSz);
if (ret == 0)
ret = sp_DhExp_3072(y, priv, privSz, &key->p, agree, agreeSz);

mp_clear(y);
mp_clear(y);

RESTORE_VECTOR_REGISTERS();
RESTORE_VECTOR_REGISTERS();
}

/* make sure agree is > 1 (SP800-56A, 5.7.1.1) */
if ((ret == 0) &&
Expand All @@ -2104,19 +2108,21 @@ static int wc_DhAgree_Sync(DhKey* key, byte* agree, word32* agreeSz,
#ifdef WOLFSSL_SP_4096
if (mp_count_bits(&key->p) == 4096) {
if (mp_init(y) != MP_OKAY)
return MP_INIT_E;
ret = MP_INIT_E;

SAVE_VECTOR_REGISTERS(ret = _svr_ret;);
if (ret == 0) {
SAVE_VECTOR_REGISTERS(ret = _svr_ret;);

if (ret == 0 && mp_read_unsigned_bin(y, otherPub, pubSz) != MP_OKAY)
ret = MP_READ_E;
if (ret == 0 && mp_read_unsigned_bin(y, otherPub, pubSz) != MP_OKAY)
ret = MP_READ_E;

if (ret == 0)
ret = sp_DhExp_4096(y, priv, privSz, &key->p, agree, agreeSz);
if (ret == 0)
ret = sp_DhExp_4096(y, priv, privSz, &key->p, agree, agreeSz);

mp_clear(y);
mp_clear(y);

RESTORE_VECTOR_REGISTERS();
RESTORE_VECTOR_REGISTERS();
}

/* make sure agree is > 1 (SP800-56A, 5.7.1.1) */
if ((ret == 0) &&
Expand Down
Loading
Loading