Skip to content

buffer: validate copyArrayBuffer offsets against buffer length#63904

Open
iliaal wants to merge 1 commit into
nodejs:mainfrom
iliaal:buffer-copyarraybuffer-bounds
Open

buffer: validate copyArrayBuffer offsets against buffer length#63904
iliaal wants to merge 1 commit into
nodejs:mainfrom
iliaal:buffer-copyarraybuffer-bounds

Conversation

@iliaal

@iliaal iliaal commented Jun 14, 2026

Copy link
Copy Markdown

CopyArrayBuffer() computed byteLength - offset in unsigned arithmetic before its CHECK_GE bounds check. An offset greater than the buffer length wrapped the subtraction to a near-SIZE_MAX value, so the check passed and memcpy() copied out of bounds.

The only in-tree caller is the Web Streams BYOB reader, which validates the offsets in JS via canCopyArrayBuffer() before calling. The binding is also reachable directly through process.binding('buffer').copyArrayBuffer(...), which bypasses that guard, so the offsets reach C++ unchecked. Only in-process (trusted) code can reach it, so this is a robustness fix, not a vulnerability.

Validate the destination and source ranges in C++ and throw ERR_OUT_OF_RANGE, as the other range checks in node_buffer.cc already do.

Reproduction before the change (in-process JS, no flags):

const b = process.binding('buffer');
b.copyArrayBuffer(new ArrayBuffer(1024), 1025, new ArrayBuffer(8), 0, 8);
// writes 8 bytes past the destination, corrupting the adjacent heap chunk

After the change the call throws ERR_OUT_OF_RANGE.

@nodejs-github-bot nodejs-github-bot added buffer Issues and PRs related to the buffer subsystem. c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Jun 14, 2026

@Renegade334 Renegade334 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The caller validates the arguments here, we do not provide guardrails for invoking C++ internals from userland.

That being said, an additional assertion to cover the invariant you've mentioned (eg. CHECK_LE(destination_offset, destination_byte_length) wouldn't go amiss.

@iliaal iliaal force-pushed the buffer-copyarraybuffer-bounds branch from f277b81 to 1c547e6 Compare June 14, 2026 13:56

@Renegade334 Renegade334 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM 👍

@iliaal

iliaal commented Jun 14, 2026

Copy link
Copy Markdown
Author

Agreed. Switched to assertions and dropped the THROW_ERR_OUT_OF_RANGE path:

CHECK_LE(destination_offset, destination_byte_length);
CHECK_LE(source_offset, source_byte_length);
CHECK_GE(destination_byte_length - destination_offset, bytes_to_copy);
CHECK_GE(source_byte_length - source_offset, bytes_to_copy);

The CHECK_LEs run before the subtractions so they can't underflow. Also dropped the test, since it covered the now-removed catchable error. Pushed as 1c547e6c.

@Renegade334

Copy link
Copy Markdown
Member

Please run git commit --amend --no-edit --signoff to add the Signed-off-by trailer to your commit message.

CopyArrayBuffer() computed `byteLength - offset` in unsigned arithmetic
before its CHECK_GE bounds check. An offset greater than the buffer
length wrapped the subtraction to a near-SIZE_MAX value, so the check
passed and memcpy() copied out of bounds.

process.binding('buffer').copyArrayBuffer() is an internal, trusted
binding; the only in-tree caller, the Web Streams BYOB reader, already
validates the offsets in JS. Assert the offsets are within bounds with
CHECK_LE before the subtractions so the invariant holds regardless of
caller, matching the CHECK-based style already used here.

Signed-off-by: Ilia Alshanetsky <ilia@ilia.ws>
@iliaal iliaal force-pushed the buffer-copyarraybuffer-bounds branch from 1c547e6 to bd8f96e Compare June 14, 2026 14:03
@iliaal

iliaal commented Jun 14, 2026

Copy link
Copy Markdown
Author

Done

@Renegade334 Renegade334 added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. request-ci Add this label to start a Jenkins CI on a PR. labels Jun 14, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jun 14, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. buffer Issues and PRs related to the buffer subsystem. c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants