Skip to content

Bounds-check function index in name section local handlers - #2787

Merged
sbc100 merged 1 commit into
WebAssembly:mainfrom
aizu-m:local-name-func-index-bounds
Jul 6, 2026
Merged

sbc100 merged 1 commit into
WebAssembly:mainfrom
aizu-m:local-name-func-index-bounds

Conversation

@aizu-m

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

Copy link
Copy Markdown
Contributor

ASAN, calling ReadBinaryIr with read_debug_names=true and stop_on_first_error=false on a crafted module:

binary-reader-ir.cc:1721: AddressSanitizer: container-overflow
READ of size 8 in BinaryReaderIR::OnLocalNameLocalCount

Found while going over the name-section handlers. OnLocalNameLocalCount and OnLocalName index module_->funcs[func_index] using the function index taken from the name section's local subsection. The reader only checks function_index < NumTotalFuncs(), i.e. the declared count num_func_imports_ + num_function_signatures_. With stop_on_first_error off, a truncated function section (declares two funcs, but the second signature LEB runs past the section end) leaves funcs one short of that count, so a local name for the missing function reads past the vector. OnLocalNameLocalCount had a debug-only assert; OnLocalName had no check at all.

Every sibling handler here already does the index >= size() check, and OnFunctionBodyCount already guards this same stop_on_first_error case for the code path. Added the matching check to both local-name handlers, plus a regression test.

@sbc100

sbc100 commented Jul 6, 2026

Copy link
Copy Markdown
Member

I'm not sure we want to support stop_on_first_error in most configurations (for example ReadBinaryIr) since it seems like an endless source of such issues, and means we are doing more checking that we need otherwise do on the normal code path.

See the discussion in #2778

@sbc100
sbc100 merged commit 52a5fed into WebAssembly:main Jul 6, 2026
17 checks passed
@aizu-m

aizu-m commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

Agreed. I went through the callers after reading the #2778 thread: every in-tree ReadBinaryIr user already runs with stop_on_first_error=true (wasm2wat sets it explicitly, the wasm2wat fuzzer and the emscripten helpers get the default). objdump is the only tool that still wants false, and it goes through its own reader, not the IR one. So dropping collect-all-errors mode from the IR path wouldn't cost any tool, just the option surface. Can put that together as a follow-up if that's the direction you'd like. Thanks for merging this one.

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