sixlowpan_udp_header.rs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #![no_main]
  2. use libfuzzer_sys::fuzz_target;
  3. use smoltcp::wire::{Ipv6Address, SixlowpanUdpPacket, SixlowpanUdpRepr};
  4. #[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, arbitrary::Arbitrary)]
  5. pub struct AddressFuzzer(pub [u8; 16]);
  6. impl From<AddressFuzzer> for Ipv6Address {
  7. fn from(val: AddressFuzzer) -> Self {
  8. Ipv6Address(val.0)
  9. }
  10. }
  11. #[derive(Debug, arbitrary::Arbitrary)]
  12. struct SixlowpanUdpPacketFuzzer<'a> {
  13. data: &'a [u8],
  14. src_addr: AddressFuzzer,
  15. dst_addr: AddressFuzzer,
  16. checksum: Option<u16>,
  17. }
  18. fuzz_target!(|fuzz: SixlowpanUdpPacketFuzzer| {
  19. if let Ok(ref frame) = SixlowpanUdpPacket::new_checked(fuzz.data) {
  20. if let Ok(repr) = SixlowpanUdpRepr::parse(
  21. frame,
  22. &fuzz.src_addr.into(),
  23. &fuzz.dst_addr.into(),
  24. fuzz.checksum,
  25. ) {
  26. let payload = frame.payload();
  27. let mut buffer = vec![0; repr.header_len() + payload.len()];
  28. let mut frame = SixlowpanUdpPacket::new_unchecked(&mut buffer[..]);
  29. repr.emit(
  30. &mut frame,
  31. &fuzz.src_addr.into(),
  32. &fuzz.dst_addr.into(),
  33. payload.len(),
  34. |b| b.copy_from_slice(payload),
  35. );
  36. }
  37. };
  38. });