Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a panic with an invalid name section #3509

Merged
merged 1 commit into from
Nov 5, 2021

Conversation

alexcrichton
Copy link
Member

This commit fixes a panic which can happen on a module with an invalid
name section where one of the functions named has the index u32::MAX.
Previously Wasmtime would create a new FuncIndex with the indices
found in the name section but the sentinel u32::MAX causes a panic.

Cranelift otherwise limits the number of functions through wasmparser
which has a hard limit (lower than u32::MAX) so this commit applies a
fix of only recording function names for function indices that are
actually present in the module.

This commit fixes a panic which can happen on a module with an invalid
name section where one of the functions named has the index `u32::MAX`.
Previously Wasmtime would create a new `FuncIndex` with the indices
found in the name section but the sentinel `u32::MAX` causes a panic.

Cranelift otherwise limits the number of functions through `wasmparser`
which has a hard limit (lower than `u32::MAX`) so this commit applies a
fix of only recording function names for function indices that are
actually present in the module.
Copy link
Contributor

@pchickey pchickey left a comment

Choose a reason for hiding this comment

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

Nice!

@alexcrichton alexcrichton merged commit 6be0f82 into bytecodealliance:main Nov 5, 2021
@alexcrichton alexcrichton deleted the fix-panic branch November 5, 2021 20:09
@ulan
Copy link
Contributor

ulan commented Nov 16, 2021

@alexcrichton: do you know if this bug has any security impact?

@alexcrichton
Copy link
Member Author

@ulan this means that if arbitrary input is fed into Module::new then it can cause Module::new to panic, which can be a form of denial-of-service I believe, but I think that's the impact of this.

@ulan
Copy link
Contributor

ulan commented Nov 16, 2021

Thanks @alexcrichton. Sorry, I forgot to mention that in a test that I tired the panic happens only in the debug mode. Could it be that the assertion is enabled only in debug mode? If so, could the bug lead to something exploitable in release mode?

In any case, do you think it is worthwhile to merge the fix back in the last two released versions?

@alexcrichton
Copy link
Member Author

Oh right sorry now I remember what I thought when I was originally thinking about this. Indeed yeah this is a debug assert so the assert doesn't happen in release mode, which means my previous comment is not actually correct because most embeddings are built in release mode.

I remember now though that my conclusion was that this would cause no issues. Nothing bad will happen for bad name section indices in reality and this was mostly just a debug check we needed to fixup. In that sense no need for security backports or anything.

@ulan
Copy link
Contributor

ulan commented Nov 16, 2021

Happy to hear that! Thanks @alexcrichton.

alexcrichton added a commit to alexcrichton/wasmtime that referenced this pull request Mar 15, 2024
Move these up to Wasmtime's misc testsuite to get translated and
instantiated by Wasmtime.

Note that the max-function-index-in-name-section test was removed here
as that's tested by the support added in bytecodealliance#3509.
github-merge-queue bot pushed a commit that referenced this pull request Mar 16, 2024
* Move remaining `*.wat` tests out of cranelift-wasm/wasmtests

Move these up to Wasmtime's misc testsuite to get translated and
instantiated by Wasmtime.

Note that the max-function-index-in-name-section test was removed here
as that's tested by the support added in #3509.

* Remove cranelift-wasm test for name section

This is pretty thoroughly tested elsewhere in Wasmtime that we respect
the name section, for example many of the trap tests assert that the
name of the function comes from the text format.

* Move reachability tests out of cranelift-wasm

Instead add them to the disassembly test suite to ensure we don't
generate dead code. Additionally this has a lot of coverage via fuzzing
too.

* Move more tests out of cranelift-wasm

Move them into `tests/disas` so we can easily see the CLIF.
jameysharp added a commit to jameysharp/wasmtime that referenced this pull request Mar 16, 2024
The cranelift/wasm/wasmtests suite verifies that the given WebAssembly
modules compile without errors using cranelift-wasm's DummyEnvironment.

That environment is not particularly representative of how we actually
compile WebAssembly modules in Wasmtime, so it's better to perform these
tests in Wasmtime itself.

The rust_fannkuch test can move to Wasmtime's misc test suite because it
can be both compiled and instantiated without any extra setup. This
tests more than was tested in cranelift-wasm, which didn't try to
instantiate the module. By moving it to a wast test we could also choose
to add assertions about the execution of this module, but I don't think
that's necessary.

I'm just deleting the embenchen tests because they can't be instantiated
without setting up a bunch of imports, and it's not clear to me that
testing that they can be compiled offers any value for helping us find
bugs or regressions.

Finally, I'm also deleting the test case which was extracted from a fuzz
bug in 2019, before Cranelift was merged into the Wasmtime repo:
bytecodealliance/cranelift#1306

That test is interesting, but running it here only tests code in
DummyEnvironment, not anything reachable from Wasmtime. There was a
report of exactly the same bug in Wasmtime two years later, and the fix
included tests in tests/misc_testsuite/empty.wast to cover this case.
bytecodealliance#3509

The `empty.wast` test is better than this one because it has minimal and
well-commented binary modules which I've verified still trigger the same
bug if the fix is reverted. By contrast, since this older test was
produced by a fuzzer, it is larger than necessary and the end of its
name section is malformed and unparseable.
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.

3 participants