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

Fix panic on bad length for SVCB record #1467

Merged
merged 2 commits into from Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -24,6 +24,12 @@ All notes should be prepended with the location of the change, e.g. `(proto)` or

- (proto) Panic when name exceeds maximal domain name length during display #1447

## 0.20.2

### Fixed

- (proto) Panic on bad length in SVCB for record length #1465

## 0.20.1

### Added
Expand Down
21 changes: 19 additions & 2 deletions crates/proto/src/rr/rdata/svcb.rs
Expand Up @@ -1032,7 +1032,11 @@ pub fn read(decoder: &mut BinDecoder<'_>, rdata_length: Restrict<u16>) -> ProtoR
let svc_priority = decoder.read_u16()?.unverified(/*any u16 is valid*/);
let target_name = Name::read(decoder)?;

let mut remainder_len = rdata_length.map(|len| len as usize - (decoder.index() - start_index)).unverified(/*valid len*/);
let mut remainder_len = rdata_length
.map(|len| len as usize)
.checked_sub(decoder.index() - start_index)
.map_err(|len| format!("Bad length for RDATA of SVCB: {}", len))?
.unverified(); // valid len
let mut svc_params: Vec<(SvcParamKey, SvcParamValue)> = Vec::new();

// must have at least 4 bytes left for the key and the length
Expand All @@ -1053,7 +1057,11 @@ pub fn read(decoder: &mut BinDecoder<'_>, rdata_length: Restrict<u16>) -> ProtoR
}

svc_params.push((key, value));
remainder_len = rdata_length.map(|len| len as usize - (decoder.index() - start_index)).unverified(/*valid len*/);
remainder_len = rdata_length
.map(|len| len as usize)
.checked_sub(decoder.index() - start_index)
.map_err(|len| format!("Bad length for RDATA of SVCB: {}", len))?
.unverified(); // valid len
}

Ok(SVCB {
Expand Down Expand Up @@ -1211,4 +1219,13 @@ mod tests {
],
));
}

#[test]
fn test_no_panic() {
const BUF: &[u8] = &[
255, 121, 0, 0, 0, 0, 40, 255, 255, 160, 160, 0, 0, 0, 64, 0, 1, 255, 158, 0, 0, 0, 8,
0, 0, 7, 7, 0, 0, 0, 0, 0, 0, 0,
];
assert!(crate::op::Message::from_vec(&BUF).is_err());
}
}