Skip to content

Commit

Permalink
crypto: fix checkPrime crash with large buffers
Browse files Browse the repository at this point in the history
Fixes: #56512
  • Loading branch information
santigimeno committed Jan 11, 2025
1 parent ad68d08 commit 5c3164a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/crypto/crypto_random.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ Maybe<void> CheckPrimeTraits::AdditionalConfig(
ArrayBufferOrViewContents<unsigned char> candidate(args[offset]);

params->candidate = BignumPointer(candidate.data(), candidate.size());
if (params->candidate.get() == nullptr) {
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 @@ -12,6 +12,7 @@ const {
generatePrimeSync,
checkPrime,
checkPrimeSync,
randomBytes,
} = require('crypto');

const { Worker } = require('worker_threads');
Expand Down Expand Up @@ -254,6 +255,18 @@ for (const checks of [-(2 ** 31), -1, 2 ** 31, 2 ** 32 - 1, 2 ** 32, 2 ** 50]) {
});
}

{
const bytes = randomBytes(67108864);
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

0 comments on commit 5c3164a

Please sign in to comment.