Skip to content

Fix fast array out-of-bounds access in Array.prototype.fill and copyWithin after resize - #5322

Open
Perennos wants to merge 1 commit into
jerryscript-project:masterfrom
Perennos:fix-array-fill-copywithin-resize
Open

Perennos wants to merge 1 commit into
jerryscript-project:masterfrom
Perennos:fix-array-fill-copywithin-resize

Conversation

@Perennos

Copy link
Copy Markdown

Fixes #5283
Fixes #5282

Array.prototype.fill and Array.prototype.copyWithin compute their index range from the array length before calling valueOf() on user-supplied arguments. If that callback shrinks a fast array, the fast paths use the stale range against the smaller buffer: fill writes past the end (heap out-of-bounds write) and copyWithin reads past the end (heap out-of-bounds read).

Both bugs share the same cause, so one patch fixes them:

  • fill: clamp the end of the range to the fast array's current length.
  • copyWithin: the fast path previously checked only the target range; it now checks the source range as well and falls back to the generic path if either is out of bounds.

Tests: added tests/jerry/regression-test-issue-5283.js and tests/jerry/regression-test-issue-5282.js from the issue reproducers. Both abort (assertion failure) on an unpatched debug build and pass with the fix. python3 tools/run-tests.py --jerry-tests --build-debug passes with the fix (1231/1231), including the two new tests and the existing array-prototype-fill.js.

AI-assisted: drafted with an AI coding assistant; reproduced and tested locally before submitting.

A valueOf() callback on the start or end argument can shrink the array
after the length and the range have been computed. The fast array paths
then accessed the shrunk buffer with the stale range.

fill: clamp the end of the range to the current array length.
copyWithin: validate the source range as well as the target range before
using the fast path, otherwise fall back to the generic path.

Fixes jerryscript-project#5283
Fixes jerryscript-project#5282

Signed-off-by: Alexander Marzouca <333385283+Perennos@users.noreply.github.com>
JerryScript-DCO-1.0-Signed-off-by: Alexander Marzouca 333385283+Perennos@users.noreply.github.com

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Heap OOB Write in Array.prototype.fill via start.valueOf() array shrink Heap OOB Read in Array.prototype.copyWithin via TOCTOU

1 participant