VirtualBox

source: vbox/trunk/src/libs/libslirp-4.8.0/fuzzing/slirp_fuzz_udp6_data.c@ 105529

Last change on this file since 105529 was 105529, checked in by vboxsync, 8 months ago

libs/libslirp: libslirp 4.8.0 fixed exports. bugref:10268

  • Property svn:eol-style set to native
File size: 4.3 KB
Line 
1#include <glib.h>
2#include <stdlib.h>
3#include <stdio.h>
4#include "../src/libslirp.h"
5#include "../src/ip6.h"
6#include "helper.h"
7#include "slirp_base_fuzz.h"
8
9#ifdef CUSTOM_MUTATOR
10extern size_t LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize);
11size_t LLVMFuzzerCustomMutator(uint8_t *Data, size_t Size, size_t MaxSize, unsigned int Seed);
12
13/// This is a custom mutator, this allows us to mutate only specific parts of
14/// the input and fix the checksum so the packet isn't rejected for bad reasons.
15extern size_t LLVMFuzzerCustomMutator(uint8_t *Data, size_t Size,
16 size_t MaxSize, unsigned int Seed)
17{
18 size_t current_size = Size;
19 uint8_t *Data_ptr = Data;
20 uint8_t *ip_data;
21 bool mutated = false;
22
23 pcap_hdr_t *hdr = (void *)Data_ptr;
24 pcaprec_hdr_t *rec = NULL;
25
26 if (current_size < sizeof(pcap_hdr_t)) {
27 return 0;
28 }
29
30 Data_ptr += sizeof(*hdr);
31 current_size -= sizeof(*hdr);
32
33 if (hdr->magic_number == 0xd4c3b2a1) {
34 g_debug("FIXME: byteswap fields");
35 return 0;
36 } /* else assume native pcap file */
37 if (hdr->network != 1) {
38 return 0;
39 }
40
41 for ( ; current_size > sizeof(*rec); Data_ptr += rec->incl_len, current_size -= rec->incl_len) {
42 rec = (void *)Data_ptr;
43 Data_ptr += sizeof(*rec);
44 current_size -= sizeof(*rec);
45
46 if (rec->incl_len != rec->orig_len) {
47 return 0;
48 }
49 if (rec->incl_len > current_size) {
50 return 0;
51 }
52 if (rec->incl_len < 14 + 1) {
53 return 0;
54 }
55
56 ip_data = Data_ptr + 14;
57
58 if (rec->incl_len >= 14 + 24) {
59 struct in6_addr *ipsource = (struct in6_addr *) (ip_data + 8);
60
61 // This an answer, which we will produce, so don't receive
62 if (in6_equal(ipsource, &ip6_host) || in6_equal(ipsource, &ip6_dns))
63 continue;
64 }
65
66 // Exclude packets that are not UDP from the mutation strategy
67 if (ip_data[6] != IPPROTO_UDP)
68 continue;
69
70 uint8_t Data_to_mutate[MaxSize];
71 uint8_t ip_hl_in_bytes = sizeof(struct ip6); /* ip header length */
72
73 // Fixme : don't use ip_hl_in_bytes inside the fuzzing code, maybe use the
74 // rec->incl_len and manually calculate the size.
75 if (ip_hl_in_bytes > rec->incl_len - 14)
76 return 0;
77
78 uint8_t *start_of_udp = ip_data + ip_hl_in_bytes;
79 uint8_t udp_header_size = 8;
80 uint16_t udp_size = ntohs(*(uint16_t *)(ip_data + 4));
81 uint16_t udp_data_size = (udp_size - (uint16_t)udp_header_size);
82
83 // The size inside the packet can't be trusted, if it is too big it can
84 // lead to heap overflows in the fuzzing code.
85 // Fixme : don't use udp_size inside the fuzzing code, maybe use the
86 // rec->incl_len and manually calculate the size.
87 if (udp_data_size > MaxSize || udp_data_size > rec->incl_len - 14 - ip_hl_in_bytes - udp_header_size)
88 return 0;
89
90 // Copy interesting data to the `Data_to_mutate` array
91 // here we want to fuzz everything in the udp packet
92 memset(Data_to_mutate, 0, MaxSize);
93 memcpy(Data_to_mutate, start_of_udp, udp_size);
94
95 // Call to libfuzzer's mutation function.
96 // Pass the whole UDP packet, mutate it and then fix checksum value
97 // so the packet isn't rejected.
98 // The new size of the data is returned by LLVMFuzzerMutate.
99 // Fixme: allow to change the size of the UDP packet, this will require
100 // to fix the size before calculating the new checksum and change
101 // how the Data_ptr is advanced.
102 // Most offsets bellow should be good for when the switch will be
103 // done to avoid overwriting new/mutated data.
104 LLVMFuzzerMutate(Data_to_mutate + udp_header_size, udp_data_size, udp_data_size);
105
106 // Drop checksum
107 // Stricto sensu, UDPv6 makes checksums mandatory, but libslirp doesn't
108 // check that actually
109 *(uint16_t *)(Data_to_mutate + 6) = 0;
110
111 // Copy the mutated data back to the `Data` array
112 memcpy(start_of_udp, Data_to_mutate, udp_size);
113
114 mutated = true;
115 }
116
117 if (!mutated)
118 return 0;
119
120 return Size;
121}
122#endif // CUSTOM_MUTATOR
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette