sixlowpan_packet.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #![no_main]
  2. use libfuzzer_sys::fuzz_target;
  3. use smoltcp::{phy::ChecksumCapabilities, wire::*};
  4. #[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, arbitrary::Arbitrary)]
  5. pub enum AddressFuzzer {
  6. Absent,
  7. Short([u8; 2]),
  8. Extended([u8; 8]),
  9. }
  10. impl From<AddressFuzzer> for Ieee802154Address {
  11. fn from(val: AddressFuzzer) -> Self {
  12. match val {
  13. AddressFuzzer::Absent => Ieee802154Address::Absent,
  14. AddressFuzzer::Short(b) => Ieee802154Address::Short(b),
  15. AddressFuzzer::Extended(b) => Ieee802154Address::Extended(b),
  16. }
  17. }
  18. }
  19. #[derive(Debug, arbitrary::Arbitrary)]
  20. struct SixlowpanPacketFuzzer<'a> {
  21. data: &'a [u8],
  22. ll_src_addr: Option<AddressFuzzer>,
  23. ll_dst_addr: Option<AddressFuzzer>,
  24. }
  25. fuzz_target!(|fuzz: SixlowpanPacketFuzzer| {
  26. match SixlowpanPacket::dispatch(fuzz.data) {
  27. Ok(SixlowpanPacket::FragmentHeader) => {
  28. if let Ok(frame) = SixlowpanFragPacket::new_checked(fuzz.data) {
  29. if let Ok(repr) = SixlowpanFragRepr::parse(&frame) {
  30. let mut buffer = vec![0; repr.buffer_len()];
  31. let mut frame = SixlowpanFragPacket::new_unchecked(&mut buffer[..]);
  32. repr.emit(&mut frame);
  33. }
  34. }
  35. }
  36. Ok(SixlowpanPacket::IphcHeader) => {
  37. if let Ok(frame) = SixlowpanIphcPacket::new_checked(fuzz.data) {
  38. if let Ok(iphc_repr) = SixlowpanIphcRepr::parse(
  39. &frame,
  40. fuzz.ll_src_addr.map(Into::into),
  41. fuzz.ll_dst_addr.map(Into::into),
  42. ) {
  43. let mut buffer = vec![0; iphc_repr.buffer_len()];
  44. let mut iphc_frame = SixlowpanIphcPacket::new_unchecked(&mut buffer[..]);
  45. iphc_repr.emit(&mut iphc_frame);
  46. let payload = frame.payload();
  47. match iphc_repr.next_header {
  48. SixlowpanNextHeader::Compressed => {
  49. if let Ok(p) = SixlowpanNhcPacket::dispatch(payload) {
  50. match p {
  51. SixlowpanNhcPacket::ExtHeader => {
  52. if let Ok(frame) =
  53. SixlowpanExtHeaderPacket::new_checked(payload)
  54. {
  55. if let Ok(repr) = SixlowpanExtHeaderRepr::parse(&frame)
  56. {
  57. let mut buffer = vec![0; repr.buffer_len()];
  58. let mut ext_header_frame =
  59. SixlowpanExtHeaderPacket::new_unchecked(
  60. &mut buffer[..],
  61. );
  62. repr.emit(&mut ext_header_frame);
  63. }
  64. }
  65. }
  66. SixlowpanNhcPacket::UdpHeader => {
  67. if let Ok(frame) =
  68. SixlowpanUdpNhcPacket::new_checked(payload)
  69. {
  70. if let Ok(repr) = SixlowpanUdpNhcRepr::parse(
  71. &frame,
  72. &iphc_repr.src_addr,
  73. &iphc_repr.dst_addr,
  74. ) {
  75. let mut buffer = vec![
  76. 0;
  77. repr.header_len()
  78. + frame.payload().len()
  79. ];
  80. let mut udp_packet =
  81. SixlowpanUdpNhcPacket::new_unchecked(
  82. &mut buffer[..],
  83. );
  84. repr.emit(
  85. &mut udp_packet,
  86. &iphc_repr.src_addr,
  87. &iphc_repr.dst_addr,
  88. frame.payload().len(),
  89. |b| b.copy_from_slice(frame.payload()),
  90. );
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. SixlowpanNextHeader::Uncompressed(proto) => match proto {
  98. IpProtocol::HopByHop => {
  99. if let Ok(frame) = Ipv6HopByHopHeader::new_checked(payload) {
  100. if let Ok(repr) = Ipv6HopByHopRepr::parse(&frame) {
  101. let mut buffer = vec![0; repr.buffer_len()];
  102. let mut hop_by_hop_frame =
  103. Ipv6HopByHopHeader::new_unchecked(&mut buffer[..]);
  104. repr.emit(&mut hop_by_hop_frame);
  105. }
  106. }
  107. }
  108. IpProtocol::Icmp => {
  109. if let Ok(frame) = Icmpv4Packet::new_checked(payload) {
  110. if let Ok(repr) =
  111. Icmpv4Repr::parse(&frame, &ChecksumCapabilities::default())
  112. {
  113. let mut buffer = vec![0; repr.buffer_len()];
  114. let mut icmpv4_packet =
  115. Icmpv4Packet::new_unchecked(&mut buffer[..]);
  116. repr.emit(
  117. &mut icmpv4_packet,
  118. &ChecksumCapabilities::default(),
  119. );
  120. }
  121. }
  122. }
  123. IpProtocol::Igmp => {
  124. if let Ok(frame) = IgmpPacket::new_checked(payload) {
  125. if let Ok(repr) = IgmpRepr::parse(&frame) {
  126. let mut buffer = vec![0; repr.buffer_len()];
  127. let mut frame = IgmpPacket::new_unchecked(&mut buffer[..]);
  128. repr.emit(&mut frame);
  129. }
  130. }
  131. }
  132. IpProtocol::Tcp => {
  133. if let Ok(frame) = TcpPacket::new_checked(payload) {
  134. if let Ok(repr) = TcpRepr::parse(
  135. &frame,
  136. &iphc_repr.src_addr.into_address(),
  137. &iphc_repr.dst_addr.into_address(),
  138. &ChecksumCapabilities::default(),
  139. ) {
  140. let mut buffer = vec![0; repr.buffer_len()];
  141. let mut frame = TcpPacket::new_unchecked(&mut buffer[..]);
  142. repr.emit(
  143. &mut frame,
  144. &iphc_repr.src_addr.into_address(),
  145. &iphc_repr.dst_addr.into_address(),
  146. &ChecksumCapabilities::default(),
  147. );
  148. }
  149. }
  150. }
  151. IpProtocol::Udp => {
  152. if let Ok(frame) = UdpPacket::new_checked(payload) {
  153. if let Ok(repr) = UdpRepr::parse(
  154. &frame,
  155. &iphc_repr.src_addr.into_address(),
  156. &iphc_repr.dst_addr.into_address(),
  157. &ChecksumCapabilities::default(),
  158. ) {
  159. let mut buffer =
  160. vec![0; repr.header_len() + frame.payload().len()];
  161. let mut packet = UdpPacket::new_unchecked(&mut buffer[..]);
  162. repr.emit(
  163. &mut packet,
  164. &iphc_repr.src_addr.into_address(),
  165. &iphc_repr.dst_addr.into_address(),
  166. frame.payload().len(),
  167. |b| b.copy_from_slice(frame.payload()),
  168. &ChecksumCapabilities::default(),
  169. );
  170. }
  171. }
  172. }
  173. IpProtocol::Ipv6Route => {
  174. if let Ok(frame) = Ipv6RoutingHeader::new_checked(payload) {
  175. if let Ok(repr) = Ipv6RoutingRepr::parse(&frame) {
  176. let mut buffer = vec![0; repr.buffer_len()];
  177. let mut packet = Ipv6RoutingHeader::new(&mut buffer[..]);
  178. repr.emit(&mut packet);
  179. }
  180. }
  181. }
  182. IpProtocol::Ipv6Frag => {
  183. if let Ok(frame) = Ipv6FragmentHeader::new_checked(payload) {
  184. if let Ok(repr) = Ipv6FragmentRepr::parse(&frame) {
  185. let mut buffer = vec![0; repr.buffer_len()];
  186. let mut frame =
  187. Ipv6FragmentHeader::new_unchecked(&mut buffer[..]);
  188. repr.emit(&mut frame);
  189. }
  190. }
  191. }
  192. IpProtocol::Icmpv6 => {
  193. if let Ok(packet) = Icmpv6Packet::new_checked(payload) {
  194. if let Ok(repr) = Icmpv6Repr::parse(
  195. &iphc_repr.src_addr.into_address(),
  196. &iphc_repr.dst_addr.into_address(),
  197. &packet,
  198. &ChecksumCapabilities::default(),
  199. ) {
  200. let mut buffer = vec![0; repr.buffer_len()];
  201. let mut packet =
  202. Icmpv6Packet::new_unchecked(&mut buffer[..]);
  203. repr.emit(
  204. &iphc_repr.src_addr.into_address(),
  205. &iphc_repr.dst_addr.into_address(),
  206. &mut packet,
  207. &ChecksumCapabilities::default(),
  208. );
  209. }
  210. }
  211. }
  212. IpProtocol::Ipv6NoNxt => (),
  213. IpProtocol::Ipv6Opts => {
  214. if let Ok(packet) = Ipv6Option::new_checked(payload) {
  215. if let Ok(repr) = Ipv6OptionRepr::parse(&packet) {
  216. let mut buffer = vec![0; repr.buffer_len()];
  217. let mut packet = Ipv6Option::new_unchecked(&mut buffer[..]);
  218. repr.emit(&mut packet);
  219. }
  220. }
  221. }
  222. IpProtocol::Unknown(_) => (),
  223. },
  224. };
  225. let mut buffer = vec![0; iphc_repr.buffer_len()];
  226. let mut frame = SixlowpanIphcPacket::new_unchecked(&mut buffer[..]);
  227. iphc_repr.emit(&mut frame);
  228. }
  229. };
  230. }
  231. Err(_) => (),
  232. }
  233. });