sixlowpan_packet.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. ) {
  44. let mut buffer = vec![0; iphc_repr.buffer_len()];
  45. let mut iphc_frame = SixlowpanIphcPacket::new_unchecked(&mut buffer[..]);
  46. iphc_repr.emit(&mut iphc_frame);
  47. let payload = frame.payload();
  48. match iphc_repr.next_header {
  49. SixlowpanNextHeader::Compressed => {
  50. if let Ok(p) = SixlowpanNhcPacket::dispatch(payload) {
  51. match p {
  52. SixlowpanNhcPacket::ExtHeader => {
  53. if let Ok(frame) =
  54. SixlowpanExtHeaderPacket::new_checked(payload)
  55. {
  56. if let Ok(repr) = SixlowpanExtHeaderRepr::parse(&frame)
  57. {
  58. let mut buffer = vec![0; repr.buffer_len()];
  59. let mut ext_header_frame =
  60. SixlowpanExtHeaderPacket::new_unchecked(
  61. &mut buffer[..],
  62. );
  63. repr.emit(&mut ext_header_frame);
  64. }
  65. }
  66. }
  67. SixlowpanNhcPacket::UdpHeader => {
  68. if let Ok(frame) =
  69. SixlowpanUdpNhcPacket::new_checked(payload)
  70. {
  71. if let Ok(repr) = SixlowpanUdpNhcRepr::parse(
  72. &frame,
  73. &iphc_repr.src_addr,
  74. &iphc_repr.dst_addr,
  75. ) {
  76. let mut buffer = vec![
  77. 0;
  78. repr.header_len()
  79. + frame.payload().len()
  80. ];
  81. let mut udp_packet =
  82. SixlowpanUdpNhcPacket::new_unchecked(
  83. &mut buffer[..],
  84. );
  85. repr.emit(
  86. &mut udp_packet,
  87. &iphc_repr.src_addr,
  88. &iphc_repr.dst_addr,
  89. frame.payload().len(),
  90. |b| b.copy_from_slice(frame.payload()),
  91. );
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98. SixlowpanNextHeader::Uncompressed(proto) => match proto {
  99. IpProtocol::HopByHop => {
  100. if let Ok(frame) = Ipv6HopByHopHeader::new_checked(payload) {
  101. if let Ok(repr) = Ipv6HopByHopRepr::parse(&frame) {
  102. let mut buffer = vec![0; repr.buffer_len()];
  103. let mut hop_by_hop_frame =
  104. Ipv6HopByHopHeader::new_unchecked(&mut buffer[..]);
  105. repr.emit(&mut hop_by_hop_frame);
  106. }
  107. }
  108. }
  109. IpProtocol::Icmp => {
  110. if let Ok(frame) = Icmpv4Packet::new_checked(payload) {
  111. if let Ok(repr) =
  112. Icmpv4Repr::parse(&frame, &ChecksumCapabilities::default())
  113. {
  114. let mut buffer = vec![0; repr.buffer_len()];
  115. let mut icmpv4_packet =
  116. Icmpv4Packet::new_unchecked(&mut buffer[..]);
  117. repr.emit(
  118. &mut icmpv4_packet,
  119. &ChecksumCapabilities::default(),
  120. );
  121. }
  122. }
  123. }
  124. IpProtocol::Igmp => {
  125. if let Ok(frame) = IgmpPacket::new_checked(payload) {
  126. if let Ok(repr) = IgmpRepr::parse(&frame) {
  127. let mut buffer = vec![0; repr.buffer_len()];
  128. let mut frame = IgmpPacket::new_unchecked(&mut buffer[..]);
  129. repr.emit(&mut frame);
  130. }
  131. }
  132. }
  133. IpProtocol::Tcp => {
  134. if let Ok(frame) = TcpPacket::new_checked(payload) {
  135. if let Ok(repr) = TcpRepr::parse(
  136. &frame,
  137. &iphc_repr.src_addr.into_address(),
  138. &iphc_repr.dst_addr.into_address(),
  139. &ChecksumCapabilities::default(),
  140. ) {
  141. let mut buffer = vec![0; repr.buffer_len()];
  142. let mut frame = TcpPacket::new_unchecked(&mut buffer[..]);
  143. repr.emit(
  144. &mut frame,
  145. &iphc_repr.src_addr.into_address(),
  146. &iphc_repr.dst_addr.into_address(),
  147. &ChecksumCapabilities::default(),
  148. );
  149. }
  150. }
  151. }
  152. IpProtocol::Udp => {
  153. if let Ok(frame) = UdpPacket::new_checked(payload) {
  154. if let Ok(repr) = UdpRepr::parse(
  155. &frame,
  156. &iphc_repr.src_addr.into_address(),
  157. &iphc_repr.dst_addr.into_address(),
  158. &ChecksumCapabilities::default(),
  159. ) {
  160. let mut buffer =
  161. vec![0; repr.header_len() + frame.payload().len()];
  162. let mut packet = UdpPacket::new_unchecked(&mut buffer[..]);
  163. repr.emit(
  164. &mut packet,
  165. &iphc_repr.src_addr.into_address(),
  166. &iphc_repr.dst_addr.into_address(),
  167. frame.payload().len(),
  168. |b| b.copy_from_slice(frame.payload()),
  169. &ChecksumCapabilities::default(),
  170. );
  171. }
  172. }
  173. }
  174. IpProtocol::Ipv6Route => {
  175. if let Ok(frame) = Ipv6RoutingHeader::new_checked(payload) {
  176. if let Ok(repr) = Ipv6RoutingRepr::parse(&frame) {
  177. let mut buffer = vec![0; repr.buffer_len()];
  178. let mut packet = Ipv6RoutingHeader::new(&mut buffer[..]);
  179. repr.emit(&mut packet);
  180. }
  181. }
  182. }
  183. IpProtocol::Ipv6Frag => {
  184. if let Ok(frame) = Ipv6FragmentHeader::new_checked(payload) {
  185. if let Ok(repr) = Ipv6FragmentRepr::parse(&frame) {
  186. let mut buffer = vec![0; repr.buffer_len()];
  187. let mut frame =
  188. Ipv6FragmentHeader::new_unchecked(&mut buffer[..]);
  189. repr.emit(&mut frame);
  190. }
  191. }
  192. }
  193. IpProtocol::Icmpv6 => {
  194. if let Ok(packet) = Icmpv6Packet::new_checked(payload) {
  195. if let Ok(repr) = Icmpv6Repr::parse(
  196. &iphc_repr.src_addr.into_address(),
  197. &iphc_repr.dst_addr.into_address(),
  198. &packet,
  199. &ChecksumCapabilities::default(),
  200. ) {
  201. let mut buffer = vec![0; repr.buffer_len()];
  202. let mut packet =
  203. Icmpv6Packet::new_unchecked(&mut buffer[..]);
  204. repr.emit(
  205. &iphc_repr.src_addr.into_address(),
  206. &iphc_repr.dst_addr.into_address(),
  207. &mut packet,
  208. &ChecksumCapabilities::default(),
  209. );
  210. }
  211. }
  212. }
  213. IpProtocol::Ipv6NoNxt => (),
  214. IpProtocol::Ipv6Opts => {
  215. if let Ok(packet) = Ipv6Option::new_checked(payload) {
  216. if let Ok(repr) = Ipv6OptionRepr::parse(&packet) {
  217. let mut buffer = vec![0; repr.buffer_len()];
  218. let mut packet = Ipv6Option::new_unchecked(&mut buffer[..]);
  219. repr.emit(&mut packet);
  220. }
  221. }
  222. }
  223. IpProtocol::Unknown(_) => (),
  224. },
  225. };
  226. let mut buffer = vec![0; iphc_repr.buffer_len()];
  227. let mut frame = SixlowpanIphcPacket::new_unchecked(&mut buffer[..]);
  228. iphc_repr.emit(&mut frame);
  229. }
  230. };
  231. }
  232. Err(_) => (),
  233. }
  234. });