-
Notifications
You must be signed in to change notification settings - Fork 30.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
crypto: fix checkPrime crash with large buffers #56559
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ const { | |
generatePrimeSync, | ||
checkPrime, | ||
checkPrimeSync, | ||
randomBytes, | ||
} = require('crypto'); | ||
|
||
const { Worker } = require('worker_threads'); | ||
|
@@ -254,6 +255,18 @@ for (const checks of [-(2 ** 31), -1, 2 ** 31, 2 ** 32 - 1, 2 ** 32, 2 ** 50]) { | |
}); | ||
} | ||
|
||
{ | ||
const bytes = randomBytes(67108864); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is generating this large of a random number each time necessary? Should be possible to generate a value statically once that trips this error reliably. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean storing the number into a file in fixtures and use that in the test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't need to be in fixtures (can just be defined inline here) but yes that would work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll do it in a file: it's 64M. Do we really want to have such a large file in the repo though? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that's better than slowing down the CI every run by generating 64M random bytes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That said, couldn't it just be 64M of zeroes? It doesn't actually need to be random or a prime if I understand correctly. |
||
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]))); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A simplified bool check should work here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hadn't noticed the
bool
operator was implemented. Thanks!