Skip to content

Commit

Permalink
Fix panic on bad length for SVCB record
Browse files Browse the repository at this point in the history
  • Loading branch information
bluejekyll committed Apr 20, 2021
1 parent e25bcf5 commit a098420
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -23,6 +23,11 @@ All notes should be prepended with the location of the change, e.g. `(proto)` or
### Fixed

- (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

## 0.20.1

Expand Down
13 changes: 11 additions & 2 deletions crates/proto/src/rr/rdata/svcb.rs
Expand Up @@ -1032,7 +1032,7 @@ 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 +1053,7 @@ 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 +1211,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());
}
}

0 comments on commit a098420

Please sign in to comment.