Skip to content

Avoid out-of-bounds imports read in wasm_instance_new#2797

Merged
sbc100 merged 2 commits into
WebAssembly:mainfrom
aizu-m:instance-new-imports-bounds
Jul 14, 2026
Merged

Avoid out-of-bounds imports read in wasm_instance_new#2797
sbc100 merged 2 commits into
WebAssembly:mainfrom
aizu-m:instance-new-imports-bounds

Conversation

@aizu-m

@aizu-m aizu-m commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

ASan, instantiating a 2-import module through the wasm C API with a 1-element imports vector:

==ERROR: AddressSanitizer: heap-buffer-overflow
READ of size 8 ... in wasm_instance_new interp-wasm-c-api.cc:746
0 bytes after 8-byte region [...] (the caller's imports array)

Spotted while running modules through libwasm with a fixed imports list. wasm_instance_new copies the imports into a RefVec by looping i over the module's declared import count and reading imports->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::Instantiate already 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 by imports->size lets the short case fall through to that existing trap instead. Valid and over-supplied lists build the same RefVec as before.

Comment thread src/interp/interp-wasm-c-api.cc Outdated
// 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++) {

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.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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
sbc100 merged commit f794a59 into WebAssembly:main Jul 14, 2026
17 checks passed
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.

2 participants