Commit f794a59
authored
Avoid out-of-bounds imports read in wasm_instance_new (#2797)
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.1 parent 2a8dc8b commit f794a59
1 file changed
Lines changed: 10 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
741 | 741 | | |
742 | 742 | | |
743 | 743 | | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
744 | 753 | | |
745 | | - | |
| 754 | + | |
746 | 755 | | |
747 | 756 | | |
748 | 757 | | |
| |||
0 commit comments