Skip to content
Closed
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
crypto: fix checkPrime crash with large buffers
Fixes: #56512
  • Loading branch information
santigimeno committed Jan 13, 2025
commit 27746115311f9e724a6064656838582192b400a6
5 changes: 5 additions & 0 deletions src/crypto/crypto_random.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ Maybe<void> CheckPrimeTraits::AdditionalConfig(
ArrayBufferOrViewContents<unsigned char> candidate(args[offset]);

params->candidate = BignumPointer(candidate.data(), candidate.size());
if (!params->candidate) {
ThrowCryptoError(
Environment::GetCurrent(args), ERR_get_error(), "BignumPointer");
return Nothing<void>();
}

CHECK(args[offset + 1]->IsInt32()); // Checks
params->checks = args[offset + 1].As<Int32>()->Value();
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-crypto-prime.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,19 @@ for (const checks of [-(2 ** 31), -1, 2 ** 31, 2 ** 32 - 1, 2 ** 32, 2 ** 50]) {
});
}

{
const bytes = Buffer.alloc(67108864);
bytes[0] = 0x1;
assert.throws(() => checkPrime(bytes, common.mustNotCall()), {
code: 'ERR_OSSL_BN_BIGNUM_TOO_LONG',
message: /bignum too long/
});
assert.throws(() => checkPrimeSync(bytes), {
code: 'ERR_OSSL_BN_BIGNUM_TOO_LONG',
message: /bignum too long/
});
}

assert(!checkPrimeSync(Buffer.from([0x1])));
assert(checkPrimeSync(Buffer.from([0x2])));
assert(checkPrimeSync(Buffer.from([0x3])));
Expand Down