Skip to content

Fix integer overflow in NAPI TypedArray/DataView/Buffer bounds checks - #2334

Open
Medios-lgtm wants to merge 1 commit into
meta:static_hfrom
Medios-lgtm:napi-view-bounds-overflow
Open

Medios-lgtm wants to merge 1 commit into
meta:static_hfrom
Medios-lgtm:napi-view-bounds-overflow

Conversation

@Medios-lgtm

Copy link
Copy Markdown
Contributor

Summary

napi_create_typedarray, napi_create_dataview and node_api_create_buffer_from_arraybuffer check the requested view against the backing ArrayBuffer with byte_offset + length <= size() evaluated in size_t, then narrow byte_offset and the byte length to the view's uint32 size_type (JSTypedArrayBase/JSDataView store offset_/length_ as uint32). A caller-supplied byte_offset near SIZE_MAX wraps the addition, so the check passes and the truncated 32-bit offset is installed as offset_. The factory returns napi_ok with a view whose window sits outside the buffer; setBuffer's own assert(offset + size <= buf->size()) is also fooled because that sum is uint32 and wraps, so nothing aborts. A later element access or DataView get/set then dereferences data(runtime) + offset_ out of bounds.

getTypedArrayBuffer in API/hermes/extensions/JSIUtils.cpp already validates its own byteOffset/byteLength with subtraction for exactly this reason; this applies the same non-wrapping form to the three Node-API entry points and guards the length * elemSize multiply.

Test Plan

Added regression tests to the existing NAPI unit suites that request a view with byte_offset = SIZE_MAX - 3 over a 16-byte ArrayBuffer. Before the change each factory returns napi_ok with offset_ == 0xFFFFFFFC; after it they return napi_invalid_arg.

$ cmake --build cmake-build-asan --target NapiTests
$ ./cmake-build-asan/unittests/napi/NapiTests
[==========] 1060 tests from 22 test cases ran. (1840 ms total)
[  PASSED  ] 1060 tests.

Debug build with AddressSanitizer.

napi_create_typedarray, napi_create_dataview and
node_api_create_buffer_from_arraybuffer checked the requested view
against the backing ArrayBuffer with byte_offset + length computed in
size_t, then narrowed byte_offset and the byte length to the view's
uint32 size_type. A byte_offset near SIZE_MAX wrapped the addition so
the check passed, and the truncated offset was installed as the view's
offset_, yielding a view outside the buffer that reads or writes out of
bounds on element/DataView access. Validate with division and
subtraction instead, matching getTypedArrayBuffer in
extensions/JSIUtils.cpp.
@meta-cla meta-cla Bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Nov 21, 2026

This branch was successfully deployed

1 active deployment
npm-publish — 6f2d3a87 Deployed Nov 21, 2026 by Medios-lgtm via publish #1787
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Do not delete this pull request or issue due to inactivity.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant