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

RUST-713 Fix underflow on BSON array and binary deserialization #244

Merged
merged 3 commits into from
Mar 24, 2021
Merged
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
15 changes: 15 additions & 0 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ fn deserialize_array<R: Read + ?Sized>(reader: &mut R, utf8_lossy: bool) -> Resu
let mut arr = Array::new();
let length = read_i32(reader)?;

if !(MIN_BSON_DOCUMENT_SIZE..=MAX_BSON_SIZE).contains(&length) {
return Err(Error::invalid_length(
length as usize,
&format!("array length must be between {} and {}", MIN_BSON_DOCUMENT_SIZE, MAX_BSON_SIZE).as_str(),
));
}

ensure_read_exactly(
reader,
(length as usize) - 4,
Expand Down Expand Up @@ -224,6 +231,14 @@ pub(crate) fn deserialize_bson_kvp<R: Read + ?Sized>(
if let BinarySubtype::BinaryOld = subtype {
let data_len = read_i32(reader)?;

if !(0..=(MAX_BSON_SIZE - 4)).contains(&data_len) {
return Err(Error::invalid_length(
data_len as usize,
&format!("0x02 length must be between 0 and {}", MAX_BSON_SIZE - 4).as_str(),
));

}

if data_len + 4 != len {
return Err(Error::invalid_length(
data_len as usize,
Expand Down