Skip to content

Commit 3915152

Browse files
santigimenoaduh95
authored andcommitted
crypto: fix checkPrime crash with large buffers
Fixes: #56512 PR-URL: #56559 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 6af5053 commit 3915152

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/crypto/crypto_random.cc

+5
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ Maybe<void> CheckPrimeTraits::AdditionalConfig(
176176
ArrayBufferOrViewContents<unsigned char> candidate(args[offset]);
177177

178178
params->candidate = BignumPointer(candidate.data(), candidate.size());
179+
if (!params->candidate) {
180+
ThrowCryptoError(
181+
Environment::GetCurrent(args), ERR_get_error(), "BignumPointer");
182+
return Nothing<void>();
183+
}
179184

180185
CHECK(args[offset + 1]->IsInt32()); // Checks
181186
params->checks = args[offset + 1].As<Int32>()->Value();

test/parallel/test-crypto-prime.js

+13
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,19 @@ for (const checks of [-(2 ** 31), -1, 2 ** 31, 2 ** 32 - 1, 2 ** 32, 2 ** 50]) {
254254
});
255255
}
256256

257+
{
258+
const bytes = Buffer.alloc(67108864);
259+
bytes[0] = 0x1;
260+
assert.throws(() => checkPrime(bytes, common.mustNotCall()), {
261+
code: 'ERR_OSSL_BN_BIGNUM_TOO_LONG',
262+
message: /bignum too long/
263+
});
264+
assert.throws(() => checkPrimeSync(bytes), {
265+
code: 'ERR_OSSL_BN_BIGNUM_TOO_LONG',
266+
message: /bignum too long/
267+
});
268+
}
269+
257270
assert(!checkPrimeSync(Buffer.from([0x1])));
258271
assert(checkPrimeSync(Buffer.from([0x2])));
259272
assert(checkPrimeSync(Buffer.from([0x3])));

0 commit comments

Comments
 (0)