Skip to content

Commit 8d8e90c

Browse files
stusmallzonyitoo
authored andcommitted
Fix for #116 (#117)
* Add simple fuzzer and one failing test case * Fix for #116
1 parent 31a74d9 commit 8d8e90c

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

fuzz/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
target
3+
corpus
4+
artifacts

fuzz/Cargo.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
[package]
3+
name = "bson-fuzz"
4+
version = "0.0.1"
5+
authors = ["Automatically generated"]
6+
publish = false
7+
8+
[package.metadata]
9+
cargo-fuzz = true
10+
11+
[dependencies.bson]
12+
path = ".."
13+
[dependencies.libfuzzer-sys]
14+
git = "https://github.com/rust-fuzz/libfuzzer-sys.git"
15+
16+
# Prevent this from interfering with workspaces
17+
[workspace]
18+
members = ["."]
19+
20+
[[bin]]
21+
name = "fuzz_target_1"
22+
path = "fuzz_targets/fuzz_target_1.rs"
23+
24+
[[bin]]
25+
name = "decode"
26+
path = "fuzz_targets/decode.rs"

fuzz/fuzz_targets/decode.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![no_main]
2+
#[macro_use] extern crate libfuzzer_sys;
3+
extern crate bson;
4+
5+
use bson::decode_document;
6+
use std::io::Cursor;
7+
8+
fuzz_target!(|buf: &[u8]| {
9+
let _ = decode_document(&mut Cursor::new(&buf[..]));
10+
});

src/decoder/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ use spec::{self, BinarySubtype};
3939

4040
use serde::de::Deserialize;
4141

42+
const MAX_BSON_SIZE: i32 = 16 * 1024 * 1024;
43+
4244
fn read_string<R: Read + ?Sized>(reader: &mut R, utf8_lossy: bool) -> DecoderResult<String> {
4345
let len = reader.read_i32::<LittleEndian>()?;
4446

@@ -170,6 +172,9 @@ fn decode_bson<R: Read + ?Sized>(reader: &mut R, tag: u8, utf8_lossy: bool) -> D
170172
Some(Array) => decode_array(reader, utf8_lossy).map(Bson::Array),
171173
Some(Binary) => {
172174
let len = read_i32(reader)?;
175+
if len < 0 || len > MAX_BSON_SIZE {
176+
return Err(DecoderError::InvalidLength(len as usize, format!("Invalid binary length of {}", len)));
177+
}
173178
let subtype = BinarySubtype::from(reader.read_u8()?);
174179
let mut data = Vec::with_capacity(len as usize);
175180
reader.take(len as u64).read_to_end(&mut data)?;

tests/modules/encoder_decoder.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,10 @@ fn test_decode_multiply_overflows_issue64() {
310310

311311
assert!(decode_document(&mut Cursor::new(&buffer[..])).is_err());
312312
}
313+
314+
315+
#[test]
316+
fn test_illegal_size(){
317+
let buffer = [0x06, 0xcc, 0xf9, 0x0a, 0x05, 0x00, 0x00, 0x03, 0x00, 0xff, 0xff];
318+
assert!(decode_document(&mut Cursor::new(&buffer[..])).is_err());
319+
}

0 commit comments

Comments
 (0)