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

Add fuzz test to handle length math issues with Messages and Headers #1273

Closed
wants to merge 13 commits into from

Conversation

pmeredit
Copy link
Contributor

This was mostly generated by Devin. The initial fuzz test was bad, but all the infrastructure for fuzzing is good. The test I modified (which was 4 loc?) did uncover an actual bug still present in the driver, which is exciting, and I am now confident that all our math ops are good.

@pmeredit
Copy link
Contributor Author

It was really nice for Devin to handle all the cfg attributes here


fuzz_target!(|data: &[u8]| {
if data.len() < Header::LENGTH {
return;
}
if let Ok(header) = Header::from_slice(data) {
if header.length < 0 {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was particularly bad. Ok, the length is negative because it's random. This wasn't testing any actual driver code 😂

@@ -158,12 +158,14 @@ fn num_decimal_digits(mut n: usize) -> usize {

/// Read a document's raw BSON bytes from the provided reader.
pub(crate) fn read_document_bytes<R: Read>(mut reader: R) -> Result<Vec<u8>> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are the actual bugs found by fuzzing

Copy link
Contributor

Choose a reason for hiding this comment

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

we should be getting a warning when we use risky as <numtype> conversions as of #1045; I wonder why these ones weren't caught

@@ -80,6 +80,7 @@ impl Action for ParseConnectionString {
"srvMaxHosts and loadBalanced=true cannot both be present",
));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did a quick pass for any other as usize issues and added comments after I was certain they were safe

@@ -1,7 +1,13 @@
#[cfg(test)]
pub(crate) mod test;

#[cfg(feature = "fuzzing")]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

allow missing just so we don't see warning when we run the fuzz test

@pmeredit
Copy link
Contributor Author

@isabelatkinson @abr-egn I notice that Devin added a bunch of Arbitrary implementations that are not actually used. Question is, do we remove them, or do we actually use them?

Comment on lines +3 to +6
use mongodb::cmap::conn::wire::{
header::{Header, OpCode},
message::Message,
};
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer not to add any of this to the public API, even if it's behind a feature flag. Can we move the contents of fuzz to within src/test and put any fuzzing additions to the message/header code behind cfg(test)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, this isn't possible because of the way cargo fuzz works (I wish it were, it's an issue with how it uses LLVM's fuzzing support, I think). However, you did bring to my attention that I was accidentally leaking some of this publicly when not fuzzing, so I have fixed that up.

@@ -158,12 +158,14 @@ fn num_decimal_digits(mut n: usize) -> usize {

/// Read a document's raw BSON bytes from the provided reader.
pub(crate) fn read_document_bytes<R: Read>(mut reader: R) -> Result<Vec<u8>> {
Copy link
Contributor

Choose a reason for hiding this comment

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

we should be getting a warning when we use risky as <numtype> conversions as of #1045; I wonder why these ones weren't caught

@pmeredit
Copy link
Contributor Author

@isabelatkinson @abr-egn maybe our best bet here is to not check in the fuzzer, but just commit the bug fix?

@isabelatkinson
Copy link
Contributor

isabelatkinson commented Dec 31, 2024

maybe our best bet here is to not check in the fuzzer, but just commit the bug fix

@pmeredit That would be my preference; maybe we can leave the changes on a branch in case we want to revisit?

@pmeredit
Copy link
Contributor Author

maybe our best bet here is to not check in the fuzzer, but just commit the bug fix

@pmeredit That would be my preference; maybe we can leave the changes on a branch in case we want to revisit?

SGTM! I'll just make a new PR and leave this branch/push this branch to origin (if I can)

@pmeredit pmeredit closed this Dec 31, 2024
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