Avoid out-of-bounds imports read in wasm_instance_new#2797
Merged
Conversation
sbc100
reviewed
Jul 13, 2026
| // than the caller supplied cannot walk off the end of the imports array; | ||
| // the short list is then rejected by Instance::Instantiate below. | ||
| size_t import_count = module->As<Module>()->import_types().size(); | ||
| for (size_t i = 0; i < import_count && i < imports->size; i++) { |
Member
There was a problem hiding this comment.
IIRC that number of imports supplied should match the number expected by the module. Maybe we can instead error out here if they are not equal?
Contributor
Author
There was a problem hiding this comment.
Makes sense. Changed it to an explicit equality check that traps with "wrong number of imports provided" and returns null before touching the array. One behavioural note: an over-supplied list used to be silently accepted, and now errors too. Reran the ASan repro (2-import module, 1 supplied): clean trap, no over-read. All the C API examples still pass.
sbc100
approved these changes
Jul 14, 2026
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.
ASan, instantiating a 2-import module through the wasm C API with a 1-element imports vector:
Spotted while running modules through libwasm with a fixed imports list.
wasm_instance_newcopies the imports into aRefVecby loopingiover the module's declared import count and readingimports->data[i]. Nothing checks the caller actually supplied that many, so a module whose import section declares more imports than the embedder passes reads past the end of the imports array and then derefs the wild pointer through->I->self().Instance::Instantiatealready rejects a short list with a "not enough imports!" trap, but it runs after this loop, so the over-read happens first. Bounding the loop byimports->sizelets the short case fall through to that existing trap instead. Valid and over-supplied lists build the sameRefVecas before.