Repository navigation
Fix integer overflow in NAPI TypedArray/DataView/Buffer bounds checks - #2334
Open
Medios-lgtm wants to merge 1 commit into
Open
Medios-lgtm wants to merge 1 commit into
Medios-lgtm wants to merge 1 commit into
Conversation
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.
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
napi_create_typedarray,napi_create_dataviewandnode_api_create_buffer_from_arraybuffercheck the requested view against the backingArrayBufferwithbyte_offset + length <= size()evaluated insize_t, then narrowbyte_offsetand the byte length to the view'suint32size_type(JSTypedArrayBase/JSDataViewstoreoffset_/length_asuint32). A caller-suppliedbyte_offsetnearSIZE_MAXwraps the addition, so the check passes and the truncated 32-bit offset is installed asoffset_. The factory returnsnapi_okwith a view whose window sits outside the buffer;setBuffer's ownassert(offset + size <= buf->size())is also fooled because that sum isuint32and wraps, so nothing aborts. A later element access orDataViewget/set then dereferencesdata(runtime) + offset_out of bounds.getTypedArrayBufferinAPI/hermes/extensions/JSIUtils.cppalready validates its ownbyteOffset/byteLengthwith subtraction for exactly this reason; this applies the same non-wrapping form to the three Node-API entry points and guards thelength * elemSizemultiply.Test Plan
Added regression tests to the existing NAPI unit suites that request a view with
byte_offset = SIZE_MAX - 3over a 16-byteArrayBuffer. Before the change each factory returnsnapi_okwithoffset_ == 0xFFFFFFFC; after it they returnnapi_invalid_arg.Debug build with AddressSanitizer.