Skip to content

Commit 7506e06

Browse files
committed
Prevent arithmetic on NULL pointer if the scratch space is too small
If the scratch space is too small when calling `secp256k1_ecmult_strauss_batch()`, the `state.pre_a` allocation will fail and the pointer will be `NULL`. This causes `state.pre_a_lam` to be computed from the `NULL` pointer. It is also possible that the first allocation to fail is for `state.ps`, which will cause the failure to occur when in `secp256k1_ecmult_strauss_wnaf()`. The issue has been detected by UBSAN using Clang 10: ``` CC=clang \ CFLAGS="-fsanitize=undefined -fno-omit-frame-pointer" \ LDFLAGS="-fsanitize=undefined -fno-omit-frame-pointer" \ ../configure UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 make check ```
1 parent ac05f61 commit 7506e06

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/ecmult_impl.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -595,11 +595,11 @@ static int secp256k1_ecmult_strauss_batch(const secp256k1_callback* error_callba
595595
scalars = (secp256k1_scalar*)secp256k1_scratch_alloc(error_callback, scratch, n_points * sizeof(secp256k1_scalar));
596596
state.prej = (secp256k1_gej*)secp256k1_scratch_alloc(error_callback, scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_gej));
597597
state.zr = (secp256k1_fe*)secp256k1_scratch_alloc(error_callback, scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_fe));
598-
state.pre_a = (secp256k1_ge*)secp256k1_scratch_alloc(error_callback, scratch, n_points * 2 * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_ge));
599-
state.pre_a_lam = state.pre_a + n_points * ECMULT_TABLE_SIZE(WINDOW_A);
598+
state.pre_a = (secp256k1_ge*)secp256k1_scratch_alloc(error_callback, scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_ge));
599+
state.pre_a_lam = (secp256k1_ge*)secp256k1_scratch_alloc(error_callback, scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_ge));
600600
state.ps = (struct secp256k1_strauss_point_state*)secp256k1_scratch_alloc(error_callback, scratch, n_points * sizeof(struct secp256k1_strauss_point_state));
601601

602-
if (points == NULL || scalars == NULL || state.prej == NULL || state.zr == NULL || state.pre_a == NULL) {
602+
if (points == NULL || scalars == NULL || state.prej == NULL || state.zr == NULL || state.pre_a == NULL || state.pre_a_lam == NULL || state.ps == NULL) {
603603
secp256k1_scratch_apply_checkpoint(error_callback, scratch, scratch_checkpoint);
604604
return 0;
605605
}

0 commit comments

Comments
 (0)