sixlowpan_packet.rs 13 KB

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