-
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 regression in randomFill/randomBytes #35135
Conversation
Fixes a segfault when a buffer larger than 2**31-1 is passed in to randomFill/randomFillSync or when a size larger than 2**31-1 is passed in to randomBytes. Signed-off-by: James M Snell <jasnell@gmail.com>
Review requested:
|
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.
Seems fine to me. Requesting more than a few kilobytes of randomness is already questionable (what do you need so much entropy for?), let alone 2 GB.
Co-authored-by: Denys Otrishko <shishugi@gmail.com>
@@ -2691,9 +2691,11 @@ changes: | |||
description: The `buffer` argument may be any `TypedArray` or `DataView`. | |||
--> | |||
|
|||
* `buffer` {Buffer|TypedArray|DataView} Must be supplied. | |||
* `buffer` {Buffer|TypedArray|DataView} Must be supplied. The | |||
size of the provided `buffer` must not be larger than `2**31 - 1`. |
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.
Ditto
size of the provided `buffer` must not be larger than `2**31 - 1`. | |
size of the provided `buffer` must be less or equal to `2**31 - 1`. |
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.
FWIW, I think less than or equal to
would be the way to go here and in the other three examples, but I'm not blocking. Whatever wording is used is fine by me. Can always be changed later.
size of the provided `buffer` must not be larger than `2**31 - 1`. | |
size of the provided `buffer` must be less than or equal to `2**31 - 1`. |
* `offset` {number} **Default:** `0` | ||
* `size` {number} **Default:** `buffer.length - offset` | ||
* `size` {number} **Default:** `buffer.length - offset`. The `size` | ||
must not be larger than `2**31 - 1`. |
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.
must not be larger than `2**31 - 1`. | |
must be less or equal to `2**31 - 1`. |
@@ -2737,9 +2739,11 @@ changes: | |||
description: The `buffer` argument may be any `TypedArray` or `DataView`. | |||
--> | |||
|
|||
* `buffer` {Buffer|TypedArray|DataView} Must be supplied. | |||
* `buffer` {Buffer|TypedArray|DataView} Must be supplied. The size | |||
of the provided `buffer` must not be larger than `2**31 - 1`. |
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.
of the provided `buffer` must not be larger than `2**31 - 1`. | |
of the provided `buffer` must be less or equal to `2**31 - 1`. |
* `offset` {number} **Default:** `0` | ||
* `size` {number} **Default:** `buffer.length - offset` | ||
* `size` {number} **Default:** `buffer.length - offset`. The `size` | ||
must not be larger than `2**31 - 1`. |
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.
must not be larger than `2**31 - 1`. | |
must be less or equal to `2**31 - 1`. |
@@ -5863,7 +5864,10 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) { | |||
const uint32_t size = args[2].As<Uint32>()->Value(); | |||
CHECK_GE(offset + size, offset); // Overflow check. | |||
CHECK_LE(offset + size, Buffer::Length(args[0])); // Bounds check. | |||
Environment* env = Environment::GetCurrent(args); | |||
|
|||
if (size > INT_MAX) |
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.
Isn't it better to do proper validation in JS and only use CHECK_LE
here?
#35093 landed that incorporated most of these changes. |
Fixes a segfault when a buffer larger than
2**31-1
is passed in to randomFill/randomFillSync or when a size larger than2**31-1
is passed in to randomBytes.I opened this one separate because we should decide if this is the way we want to handle this. This PR adds a throw if the size is larger than
2**31-1
. Alternatively, we could allow larger values by splitting it into multiple calls toRAND_bytes
. That said, I'd like to believe that asking for random values larger than2**31-1
is going to be exceedingly rare (I'd really hope), so I think the throw is sufficient here.Related to: #35132
Signed-off-by: James M Snell jasnell@gmail.com
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes