Skip to content
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

Closed
wants to merge 4 commits into from
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
14 changes: 9 additions & 5 deletions doc/api/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -2631,7 +2631,7 @@ changes:
`ERR_INVALID_CALLBACK`.
-->

* `size` {number}
* `size` {number} The `size` must be less or equal to `2**31 - 1`.
* `callback` {Function}
* `err` {Error}
* `buf` {Buffer}
Expand Down Expand Up @@ -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`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Suggested change
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`.

Copy link
Member

@Trott Trott Sep 16, 2020

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.

Suggested change
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`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
must not be larger than `2**31 - 1`.
must be less or equal to `2**31 - 1`.

* Returns: {Buffer|TypedArray|DataView} The object passed as `buffer` argument.

Synchronous version of [`crypto.randomFill()`][].
Expand Down Expand Up @@ -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`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
must not be larger than `2**31 - 1`.
must be less or equal to `2**31 - 1`.

* `callback` {Function} `function(err, buf) {}`.

This function is similar to [`crypto.randomBytes()`][] but requires the first
Expand Down
6 changes: 5 additions & 1 deletion src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5855,6 +5855,7 @@ struct RandomBytesJob : public CryptoJob {


void RandomBytes(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK(args[0]->IsArrayBufferView()); // buffer; wrap object retains ref.
CHECK(args[1]->IsUint32()); // offset
CHECK(args[2]->IsUint32()); // size
Expand All @@ -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)
Copy link
Member

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?

return THROW_ERR_OUT_OF_RANGE(env, "buffer is too large");

std::unique_ptr<RandomBytesJob> job(new RandomBytesJob(env));
job->data = reinterpret_cast<unsigned char*>(Buffer::Data(args[0])) + offset;
job->size = size;
Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-crypto-random-regression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

const { Buffer } = require('buffer');
const assert = require('assert');

const {
randomFill,
randomFillSync,
randomBytes
} = require('crypto');

let kData;
try {
kData = Buffer.alloc(2 ** 31 + 1);
} catch {
common.skip('not enough memory');
}

assert.throws(() => randomFill(kData, common.mustNotCall()), {
code: 'ERR_OUT_OF_RANGE'
});

assert.throws(() => randomFillSync(kData), {
code: 'ERR_OUT_OF_RANGE'
});

assert.throws(() => randomBytes(2 ** 31 + 1), {
code: 'ERR_OUT_OF_RANGE'
});