Repository navigation
Description
I plan on laying out some foundational fuzzing infrastructure for Wasmtime in the next few weeks. I'd like to use this issue as a kind of meta issue to keep track of this work. I'd also appreciate feedback on the plan from anyone with experience fuzzing or domain knowledge of a particular thing we plan on fuzzing.
Goals
-
Find bugs!
- Bugs that we wouldn't otherwise find until our users hit them.
- Bugs that are hard to manually write test cases for, or that you wouldn't even think of testing for.
-
Make bugs (fuzzer-found or otherwise) easier to debug via automatic test case reduction.
Strategy
Breadth not Depth
At least initially, let's build out a few different fuzzing approaches enough that they start identifying bugs, but not spend a ton of time building bespoke tools tailored for exactly the problems we have at hand.
My assumptions are that
- we have low-hanging fruit available, since we haven't done a ton of fuzzing for a bunch of corners yet, and
- different fuzzing approaches tend to uncover different sets of bugs.
Therefore, by making a bunch of different just-good-enough fuzzers, we will repeatedly discover new, unique low-hanging fruit bugs.
Additionally, this gives us a nice foundation that we can spring board off of in the future when we decide to go deeper in any particular direction.
Decouple Generators and Oracles
A generator creates test cases (usually given an RNG or a random byte stream input). An oracle determines if executing a test case uncovered a bug. In general, it is good software engineering to separate concerns, but separating these two parts specifically allows us to:
- reuse oracles during automatic test case reduction (a la
creduce), and - swap out existing, off-the-shelf generators with more intelligent, custom generators the future.
Implementation
In general, I recommend that we use libFuzzer to drive our fuzzing. It is coverage-guided, which means it can find interesting code paths more quickly than testing purely random inputs will. It also has a nice Rust interface in the form of cargo-fuzz.
Any custom generators we create should take libFuzzer-provided input bytes and then re-interpret that as a sequence of random values to drive choices inside the generator. This lets us combine the benefits of smart, structure-aware generators with those of coverage-guided fuzzing. We can implement this by implementing our custom generators in terms of the arbitrary crate's Arbitrary trait.
As far as test case reduction goes, when a generator is creating Wasm files, it should be relatively easy to use binaryen's wasm-reduce on the Wasm file, or use creduce on the WAT disassembly. We can, however, do some small things to make the process turnkey:
- Write glue scripts for running
wasm-reduceand/orcreduceon a Wasm test case with any of our various oracles
For generators that are creating custom in-memory data structures by implementing the Arbitrary trait, test case reduction requires we implement some custom logic. The Arbitrary trait supports defining a custom shrink method that takes &self and returns an iterator of smaller instances of Self. We can use this to create custom test case reduction for each of our custom test case generators.
Finally, any custom generator we create (and any generator we wrap that supports turning the generation of individual test case features on/off) should support swarm testing. Swarm testing is where we randomly turn on/off the generation of various test case features (such as, should a generator create Wasm test cases that use call_indirect or not?) so that we are more likely to generate pathological test cases where bugs are more likely to be found. This is relatively easy implement and should yield
Fuzzing Wasmtime's Embedding API
This is a case where, unfortunately, we can't really use existing off-the-shelf solutions.
Generators
- Build a custom generator that creates a sequence of API calls. It shouldn't perform the calls, just describe them. This generator should have some smarts about knowing how to generate valid API calls.
Oracles
- Interpret API call descriptions and perform the actual API call. Find unexpected panics, assertion failures, and segfaults.
Wasm Execution Fuzzing
We should fuzz our execution of Wasm. Yes, Cranelift has some fuzzing in SpiderMonkey, but we should also make sure that all of our Wasmtime-specific JIT'ing machinery is well fuzzed, as well as our WASI implementation and sandboxing.
Generators
-
Use
wasm-opt -ttfto generate random, valid Wasm files. -
Write a custom generator that creates Wasm files that make sequences of WASI syscalls.
Oracles
-
Execute the file and ensure Wasmtime doesn't panic, fail any
assert!(..)s, or segfault regardless if executing the Wasm traps. -
stracethe process or something and ensure it doesn't do any syscalls outside the preopened directory given to the WASI sandbox or something? -
Differential fuzzing where we compare the observable results of execution between:
- Cranelift without optimizations
- Cranelift with opt=speed
- Cranelift with opt=size
- Cranelift with opt=speed_and_size
- Cranelift with a warm code cache
- Cranelift with a cold code cache
- Lightbeam
More Stuff to Explore in the Future
-
- Alternatively, we could MacGyver some custom code coverage scheme via instrumenting Wasm files with Walrus instead of doing this inside Cranelift at the clif level.
-
Create test case generators and oracles for our Wasm interface types support? What would be involved here is not super clear to me yet.
Questions
-
Should the fuzzing corpus be committed into the git repo? Or perhaps should it be a separate repo that we include as a git submodule?
-
What work here should we prioritize?
- In particular, what variants would be most valuable to compare / most likely to uncover high-priority bugs in differential fuzzing of Wasm execution?
-
Is there anything here you think we should not implement?
-
Are there any other WASI-targeted oracles we can create? The
straceidea is pretty half-baked right now. I'd appreciate some more ideas from folks more involved in the WASI side of things than I am...
Activity
acfoltzer commentedon Nov 21, 2019
In Lucet, I wrote a simple fuzzing script that uses Csmith-generated C programs: https://github.com/bytecodealliance/lucet/blob/master/lucet-wasi-fuzz/src/main.rs
The approach is to run each program via Lucet on WASI:
Then compare the
stdoutagainst a native oracle:It's pretty bare-bones, other than the ability to run a creduce loop when a failure is found, but it should be possible to hook it up to libfuzzer and wasmtime.
kubkon commentedon Nov 21, 2019
@fitzgen I haven't done a lot of fuzzing in the past, but I'll be more than happy to learn on the job and help out any way I can in testing out our WASI implementation. @acfoltzer lemme know if you need any help in potentially reusing your Lucet fuzzing harness in Wasmtime!
alexcrichton commentedon Nov 21, 2019
For interface types specifically I suspect that the generator won't be too too different than what wasm generator we might have, unless we heavily base it on
wasm-optin which case we'd have to write our own fuzz case generator.For an oracle I think our best bet will be to have someone entirely disconnected from the wasmtime interface types work to write an interpreter, and then we'd compare the two implementations against each other. I suspect we'd discover bugs in both, but I don't think we have much of an oracle otherwise right now.
acfoltzer commentedon Nov 21, 2019
Thanks, @kubkon! I'm actually not going to have time to work on this for a few weeks at least, so if you're feeling eager, don't worry about jumping in and pinging me if you need any support.
sunfishcode commentedon Nov 21, 2019
On the topic of oracles, the
straceidea is appealing, as it doesn't require admin privileges and doesn't depend on cooperation from the VM. Ideally we'd write our ownptraceutility rather than literally usingstrace, so that we can catch sandbox violations when they happen, which protects the host system better and gives fuzzers a better picture of what's happening.Another option is to use
LD_PRELOADto interpose between the application and libc, which ought to be faster thanptrace, and simpler to implement, though it would depend on applications being dynamically linked to libc.fitzgen commentedon Nov 21, 2019
Good point that there are a few different tools we have at our disposal to observe syscalls. There is probably some eBPF APIs and
perftools we could use too.I would lean towards whatever is both
Unless I'm mistaken,
LD_PRELOADwouldn't work well for the latter, since we would have to manually implement overwriting a symbol for every libc API we wanted to observe.Create a new `wasmtime-fuzzing` crate
Split our existing fuzz targets into separate generators and oracles
Split our existing fuzz targets into separate generators and oracles
Split our existing fuzz targets into separate generators and oracles
jfoote commentedon Nov 22, 2019
This is a good idea. We did something similar for a cranelift fuzz target. One downside to this approach is that the fuzz target cannot be seeded with a distilled corpus of valid-ish Wasm modules (since the input is a bitstring). Likewise, corpuses that are accumulated as fuzzers run will not be readily recyclable between generators that consume bitstrings (IIUC).
These are not good reasons not to take this approach, but something to consider for future work. Overall this looks great. I like the equivalence checking idea.
fitzgen commentedon Nov 25, 2019
FYI, some discussion about this over here: rust-fuzz/cargo-fuzz#194
pventuzelo commentedon Nov 26, 2019
Hi guys, i'm planning to do some fuzzing on lightbeam in the next weeks ;)
Just to give you a bit of context about me, I'm the guy behind webassembly-security.com and I'm teaching WebAssembly security and Rust security. I'm focused on fuzzing and vulnerability research on both WebAssembly (module & VM) and Rust code, so don't hesitate to ping me if needed ;)
I agree with @jfoote, regarding using binaryen
translate_to_fuzzfor fuzzing. Main issue will be crash replay because I think (need to be verify) binaryen is not consistent on wasm generation (i.e. same input can generate 2 different module). Also, generated wasm section are often the same in the final wasm module, meaning some part of the VM/parser will be difficult to reach@fitzgen Regarding where to store fuzzing corpus, i would suggest a specific repo or server not link to this one to prevent user to download all those files accidentally. Also, corpus need to be minimize before being pushed in this storage repo.
In general, you should have one fuzz target per APIs and per backends since corpus will evolved differently depending of the code triggered.
fitzgen commentedon Nov 26, 2019
wasm-opt -ttfwill generate the same output given the same input; it is deterministic.fitzgen commentedon Nov 27, 2019
I've set up a repo for the libFuzzer corpora here: https://github.com/bytecodealliance/wasmtime-libfuzzer-corpus
5 remaining items
fitzgen commentedon Dec 18, 2019
Great -- thanks! I don't think there is any giant rush here, so if this gets bumped to after the holidays, that seems 100% OK with me :)
fitzgen commentedon Dec 20, 2019
This is done, and part of the new
cargo fuzz0.6.0 release.jfoote commentedon Jan 16, 2020
Quick update here: I was able to link the oss-fuzz build environment libfuzzer library (
libFuzzingEngine.a) into the wasmtime/fuzzcompilefuzz target after patching rust-fuzz/libfuzzer to select c++ std lib based on an env var. Executing the binary for a few seconds yields the expected results; it seems to work.Building with asan (the default) is OK, but specifying
sanitizer=memoryyields a linking error. I fiddled with the bug a little and suspect an incompatibility in the instrumenting/linking used inlibFuzzingEngine.aand what rustc/libfuzzer-sys are using, but I did not root-cause it.The other sanitizer that oss-fuzz can optionally build with is ubsan, but it is not supported by our toolchain here at this time AFAIK.
My recommendation (and plan at this point, unless directed otherwise) is to ignore the sanitizer flag supplied by oss-fuzz, set the fuzz target configs to use only asan for good measure, and proceed to write a build script for the wasmtime/fuzz targets. I'll then make a PR to oss-fuzz after rust-fuzz/libfuzzer#56 is merged to get the conversation started.
Add initial differential fuzzing
Add initial differential fuzzing
Add initial differential fuzzing
Update libfuzzer-sys dependency version number
jfoote commentedon Jan 20, 2020
Hello @fitzgen! I have the strawman PR for the wasmtime oss-fuzz integration staged. Before we move forward with that, can you take a look at the project acceptance PR diff (jfoote/oss-fuzz@06542db) and see if it looks OK to you?
Basically I set myself as the maintainer for now and added an email alias for you as well as [email protected]. Those addresses are used to get notifications when the fuzzers find something or the build breaks. Note that if aliases listed there have associated google accounts they will get access to the oss-fuzz dashboard and bug tracker. Should we add anyone else initially?
cloneback to upstreamfitzgen commentedon Jan 21, 2020
@jfoote looks great! 👍 I left a couple comments on the draft text. Everything else looks ready to go!
jfoote commentedon Feb 7, 2020
Quick update for posterity and onlookers: we've successfully integrated the wasmtime fuzz targets with oss-fuzz, with the caveats outlined in the comments and referenced PRs above. Thanks to @fitzgen and @alexcrichton for making this happen!
Fix cranelift_preopt panic
Hyperion101010 commentedon Apr 4, 2020
@fitzgen sir this was a gsoc2020 project idea, I worked in the application period and submitted a proposal. Given the time I had at I hand i wasn't able to get complete idea about the different vulnerabilities like ABI abstractions, Heap and Stack safety. I want to voluntarily contribute for the idea, but couldn't do the same before I clear out some doubts.
I would like to start understanding the fuzzing process more closely and contributing by writing fuzzers perhaps. During the application process I wrote mails for the project details, but I never got any reply which is completely fine given the situation we have now.
Is there any way we can do a conversation for the doubts I have, I see that there used to be a IRC channel for wasmtime one year ago, but now they migrated to Matrix which unfortunately doesn't has any such channel. If you are available on any channel of Mozilla/(other open source org) please let me know.
Good day!
bjorn3 commentedon Apr 4, 2020
https://bytecodealliance.zulipchat.com/ is the primary discussion channel.
bjorn3 commentedon Feb 3, 2021
I think this can be closed.