Skip to content

Commit 0dd6aa3

Browse files
committed
Prevent ECC tmp key leak and UB in wc_ecc_mulmod_ex
ecc_key_tmp_final was guarded by `if (err == MP_OKAY)`, leaking key->t1/t2 (and x/y/z under ALT_ECC_SIZE) whenever an allocation or mulmod step after ecc_key_tmp_init failed. Simply removing the guard is unsafe here: unlike wc_ecc_mulmod_ex2 (whose arg checks `return` directly), this function XMALLOC'd `key` before the arg checks and used `goto exit`, so a bad-arg call would hand uninitialized memory to ecc_key_tmp_final and XFREE garbage pointers. Defer the XMALLOC until after the arg/range checks so `key` is NULL on the early-error paths, then call ecc_key_tmp_final unconditionally to plug the leak on the late-error paths.
1 parent d7a34d4 commit 0dd6aa3

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

wolfcrypt/src/ecc.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3733,7 +3733,7 @@ int wc_ecc_mulmod_ex(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a,
37333733
#endif
37343734
int i, err;
37353735
#ifdef WOLFSSL_SMALL_STACK_CACHE
3736-
ecc_key *key = (ecc_key *)XMALLOC(sizeof(*key), heap, DYNAMIC_TYPE_ECC);
3736+
ecc_key *key = NULL;
37373737
#endif
37383738
mp_digit mp;
37393739

@@ -3753,6 +3753,7 @@ int wc_ecc_mulmod_ex(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a,
37533753
}
37543754

37553755
#ifdef WOLFSSL_SMALL_STACK_CACHE
3756+
key = (ecc_key *)XMALLOC(sizeof(*key), heap, DYNAMIC_TYPE_ECC);
37563757
if (key == NULL) {
37573758
err = MP_MEM;
37583759
goto exit;
@@ -3815,8 +3816,7 @@ int wc_ecc_mulmod_ex(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a,
38153816
if (key) {
38163817
if (R)
38173818
R->key = NULL;
3818-
if (err == MP_OKAY)
3819-
ecc_key_tmp_final(key, heap);
3819+
ecc_key_tmp_final(key, heap);
38203820
XFREE(key, heap, DYNAMIC_TYPE_ECC);
38213821
}
38223822
#endif /* WOLFSSL_SMALL_STACK_CACHE */

0 commit comments

Comments
 (0)