-
Notifications
You must be signed in to change notification settings - Fork 28
Add sections about fuzzing APIs with callbacks #53
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # Writing Oracles that Access Data | ||
|
|
||
| Rust requires that a reference should point to a valid value, as defined in [The Rust Reference](https://doc.rust-lang.org/reference/behavior-considered-undefined.html#r-undefined.validity.reference-box): | ||
|
|
||
| > A reference or `Box<T>` must be aligned and non-null, it cannot be dangling, and it must point to a valid value. | ||
|
|
||
| As a result, a high-quality harness should validate **every reference** obtained from the target library. There are two main categories to obtain data from the target library: either from the API's return value, or in the parameters of callbacks. | ||
|
|
||
| It's very flexible to design APIs with callbacks in Rust, while it's not easy to write good fuzzing harnesses for those. | ||
|
|
||
| ```rust,ignore | ||
| pub fn api_with_callback(user_data: &[u8], callback: impl Fn(&[u32])) { | ||
| let dangling_data_ptr: *mut u32 = process_user_data(user_data); | ||
| let data_len: usize = HARDCODED_VALUE; | ||
| let data = unsafe { std::slice::from_raw_parts(dangling_data_ptr, data_len) }; | ||
| callback(data); | ||
| } | ||
| ``` | ||
|
|
||
| In the above example, creating slice from dangling pointer is definitely a UB. However, current fuzzing solutions are often equipped only with address sanitizer, which will detect violations only if an invalid memory is **accessed**. As a result, the creation of such a slice will not be catched by the address sanitizer, and the effectiveness depends on the quality of fuzzing harnesses. | ||
|
|
||
| ```rust,ignore | ||
| // Bad harness | ||
| fuzz_target!(|data: &[u8]| { | ||
| api_with_callback(data, |lib_data| {}); | ||
| }); | ||
|
|
||
| // Good harness | ||
| fuzz_target!(|data: &[u8]| { | ||
| api_with_callback(data, |lib_data| { | ||
| lib_data.iter().for_each(|byte_ref| { | ||
| core::hint::black_box(*byte_ref); | ||
| }); | ||
| }); | ||
| }); | ||
| ``` | ||
|
|
||
| In the good harness above, each byte of `lib_data` is accessed (and the [`black_box`](https://doc.rust-lang.org/std/hint/fn.black_box.html) is used to avoid the access being optimized out), and any invalid memory accesses will be catched by address sanitizers, leading to effective bug detection. | ||
|
|
||
| As described above, the reference data can be obtained either from the API's return value, or in the parameters of callbacks. As long as a reference is obtained from the target library, such a reference should be checked in the fuzzing harness to catch unsoundness. Beyond manuanlly writing checking patterns, crates like [touched](https://crates.io/crates/touched) provide convenient utilities for this purpose. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.