Skip to content

Commit 181083f

Browse files
committed
Reject certain malformed IPv4 packets.
Reported independently, but testcase found via cargo-fuzz.
1 parent 0818612 commit 181083f

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/wire/ipv4.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ impl<T: AsRef<[u8]>> Packet<T> {
184184

185185
/// Ensure that no accessor method will panic if called.
186186
/// Returns `Err(Error::Truncated)` if the buffer is too short.
187+
/// Returns `Err(Error::Malformed)` if the header length is greater
188+
/// than total length.
187189
///
188190
/// The result of this check is invalidated by calling [set_header_len]
189191
/// and [set_total_len].
@@ -196,6 +198,8 @@ impl<T: AsRef<[u8]>> Packet<T> {
196198
Err(Error::Truncated)
197199
} else if len < self.header_len() as usize {
198200
Err(Error::Truncated)
201+
} else if self.header_len() as u16 > self.total_len() {
202+
Err(Error::Malformed)
199203
} else if len < self.total_len() as usize {
200204
Err(Error::Truncated)
201205
} else {
@@ -739,6 +743,13 @@ mod test {
739743
assert_eq!(Repr::parse(&packet, &ChecksumCapabilities::default()), Err(Error::Malformed));
740744
}
741745

746+
#[test]
747+
fn test_parse_total_len_less_than_header_len() {
748+
let mut bytes = vec![0; 40];
749+
bytes[0] = 0x09;
750+
assert_eq!(Packet::new_checked(&mut bytes), Err(Error::Malformed));
751+
}
752+
742753
#[test]
743754
fn test_emit() {
744755
let repr = packet_repr();

0 commit comments

Comments
 (0)