Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions fuzz/fuzz_targets/header_length.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
#![no_main]
use libfuzzer_sys::fuzz_target;
use mongodb::cmap::conn::wire::header::Header;
use mongodb::cmap::conn::wire::{header::Header, message::Message};

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 😂

panic!("Negative length detected: {}", header.length);
}
if header.length as usize > std::usize::MAX - Header::LENGTH {
panic!("Integer overflow detected in length calculation");
}
let total_size = Header::LENGTH + header.length as usize;
if total_size > data.len() {
return;
let data = &data[Header::LENGTH..];
if let Ok(message) = Message::read_from_slice(data, header) {
let _ = message;
}
}
});
2 changes: 2 additions & 0 deletions src/cmap/conn/wire/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ impl Header {
#[cfg(not(feature = "fuzzing"))]
pub(crate) const LENGTH: usize = 4 * std::mem::size_of::<i32>();

// generates a Header from a randomly generated slice of bytes, as long as the slice is at least
// 16 bytes long this is used for fuzzing
#[cfg(feature = "fuzzing")]
pub fn from_slice(data: &[u8]) -> Result<Self> {
if data.len() < Self::LENGTH {
Expand Down