Skip to content

Commit 280ddde

Browse files
committedAug 30, 2017
Fix Ipv4Packet::payload{,_mut}() returning overly long buffers.
These functions only skipped the header, but what they should have done is cut the packet at its total length and skip the header. Otherwise, if the buffer the IP packet is constructed from contains padding, such as Ethernet packets, the payload would be too long.
1 parent 1ece71a commit 280ddde

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed
 

Diff for: ‎src/wire/ipv4.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Packet<&'a T> {
248248
/// Return a pointer to the payload.
249249
#[inline]
250250
pub fn payload(&self) -> &'a [u8] {
251-
let range = self.header_len() as usize;
251+
let range = self.header_len() as usize..self.total_len() as usize;
252252
let data = self.buffer.as_ref();
253-
&data[range..]
253+
&data[range]
254254
}
255255
}
256256

@@ -381,7 +381,7 @@ impl<'a, T: AsRef<[u8]> + AsMut<[u8]> + ?Sized> Packet<&'a mut T> {
381381
/// Return a mutable pointer to the payload.
382382
#[inline]
383383
pub fn payload_mut(&mut self) -> &mut [u8] {
384-
let range = self.header_len() as usize..;
384+
let range = self.header_len() as usize..self.total_len() as usize;
385385
let data = self.buffer.as_mut();
386386
&mut data[range]
387387
}
@@ -611,6 +611,18 @@ mod test {
611611
assert_eq!(&packet.into_inner()[..], &PACKET_BYTES[..]);
612612
}
613613

614+
#[test]
615+
fn test_overlong() {
616+
let mut bytes = vec![];
617+
bytes.extend(&PACKET_BYTES[..]);
618+
bytes.push(0);
619+
620+
assert_eq!(Packet::new(&bytes).payload().len(),
621+
PAYLOAD_BYTES.len());
622+
assert_eq!(Packet::new(&mut bytes).payload_mut().len(),
623+
PAYLOAD_BYTES.len());
624+
}
625+
614626
static REPR_PACKET_BYTES: [u8; 24] =
615627
[0x45, 0x00, 0x00, 0x18,
616628
0x00, 0x00, 0x40, 0x00,

0 commit comments

Comments
 (0)
Please sign in to comment.