-
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathfuzz.c
More file actions
297 lines (247 loc) · 9.09 KB
/
Copy pathfuzz.c
File metadata and controls
297 lines (247 loc) · 9.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
reliable
Copyright © 2017 - 2026, Más Bandwidth LLC
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// This fuzzer drives two endpoints exchanging real traffic across an unreliable
// link, so it exercises the full send -> fragment -> reassemble -> ack path (the
// interesting parser and reassembly code), not just receive() of random bytes.
//
// On top of valid traffic it layers adversarial behaviour: packet loss,
// reordering, duplication, single-bit corruption of otherwise-valid packets, and
// injection of fully random packets. Run it under AddressSanitizer / UBSan to
// catch memory errors and undefined behaviour:
//
// clang -g -O1 -fsanitize=address,undefined -DRELIABLE_DEBUG -I. reliable.c fuzz.c -o fuzz && ./fuzz 1000000 <seed>
//
// Usage: fuzz [num_iterations] [seed] (num_iterations <= 0 runs until Ctrl-C)
#include "reliable.h"
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <assert.h>
#include <signal.h>
#include <inttypes.h>
#include <time.h>
#define MAX_PACKET_BYTES (16*1024)
static volatile int quit = 0;
void interrupt_handler( int signal )
{
(void) signal;
quit = 1;
}
int random_int( int a, int b )
{
assert( a <= b );
int result = a + rand() % ( b - a + 1 );
assert( result >= a );
assert( result <= b );
return result;
}
double global_time = 100.0;
// Simulated network link: packets a transmit callback hands us are copied into a
// bounded queue, then flushed (with reordering / loss / duplication / corruption)
// into the destination endpoint once per iteration.
#define MAX_QUEUE 8192
struct queued_packet_t
{
uint8_t * data;
int bytes;
};
struct link_t
{
struct queued_packet_t packets[MAX_QUEUE];
int num_packets;
};
static void link_queue( struct link_t * link, uint8_t * data, int bytes )
{
if ( link->num_packets == MAX_QUEUE || bytes <= 0 )
return;
uint8_t * copy = (uint8_t*) malloc( bytes );
assert( copy );
memcpy( copy, data, bytes );
link->packets[link->num_packets].data = copy;
link->packets[link->num_packets].bytes = bytes;
link->num_packets++;
}
static void link_clear( struct link_t * link )
{
int i;
for ( i = 0; i < link->num_packets; ++i )
free( link->packets[i].data );
link->num_packets = 0;
}
// Flush the link into an endpoint, applying loss, reordering, duplication and
// single-bit corruption so the parser sees plausible-but-broken packets.
static void link_flush( struct link_t * link, struct reliable_endpoint_t * to )
{
// fisher-yates shuffle for reordering
int i;
for ( i = link->num_packets - 1; i > 0; --i )
{
int j = rand() % ( i + 1 );
struct queued_packet_t tmp = link->packets[i];
link->packets[i] = link->packets[j];
link->packets[j] = tmp;
}
for ( i = 0; i < link->num_packets; ++i )
{
uint8_t * data = link->packets[i].data;
int bytes = link->packets[i].bytes;
if ( random_int( 0, 99 ) < 10 ) // 10% packet loss
continue;
if ( random_int( 0, 99 ) < 20 ) // 20% single-bit corruption
data[random_int( 0, bytes - 1 )] ^= (uint8_t) ( 1 << random_int( 0, 7 ) );
int copies = random_int( 0, 99 ) < 5 ? 2 : 1; // 5% duplication
int c;
for ( c = 0; c < copies; ++c )
reliable_endpoint_receive_packet( to, data, bytes );
}
link_clear( link );
}
struct fuzz_context_t
{
struct reliable_endpoint_t * client;
struct reliable_endpoint_t * server;
struct link_t client_to_server;
struct link_t server_to_client;
};
static struct fuzz_context_t ctx;
void test_transmit_packet_function( void * context, uint64_t id, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
{
(void) context;
(void) sequence;
if ( id == 0 )
link_queue( &ctx.client_to_server, packet_data, packet_bytes );
else
link_queue( &ctx.server_to_client, packet_data, packet_bytes );
}
int test_process_packet_function( void * context, uint64_t id, uint16_t sequence, uint8_t * packet_data, int packet_bytes )
{
(void) context;
(void) id;
(void) sequence;
(void) packet_data;
(void) packet_bytes;
return 1;
}
static struct reliable_endpoint_t * make_endpoint( uint64_t id, const char * name )
{
struct reliable_config_t config;
reliable_default_config( &config );
reliable_copy_string( config.name, name, sizeof( config.name ) );
config.context = &ctx;
config.id = id;
config.transmit_packet_function = &test_transmit_packet_function;
config.process_packet_function = &test_process_packet_function;
return reliable_endpoint_create( &config, global_time );
}
void fuzz_initialize()
{
reliable_init();
memset( &ctx, 0, sizeof( ctx ) );
ctx.client = make_endpoint( 0, "client" );
ctx.server = make_endpoint( 1, "server" );
}
void fuzz_shutdown()
{
printf( "\nshutdown\n" );
link_clear( &ctx.client_to_server );
link_clear( &ctx.server_to_client );
reliable_endpoint_destroy( ctx.client );
reliable_endpoint_destroy( ctx.server );
reliable_term();
}
static void send_random_packet( struct reliable_endpoint_t * endpoint )
{
static uint8_t packet_data[MAX_PACKET_BYTES];
int packet_bytes = random_int( 1, MAX_PACKET_BYTES );
int i;
for ( i = 0; i < packet_bytes; ++i )
packet_data[i] = (uint8_t) rand();
reliable_endpoint_send_packet( endpoint, packet_data, packet_bytes );
}
static void inject_random_packet( struct reliable_endpoint_t * endpoint )
{
static uint8_t packet_data[MAX_PACKET_BYTES];
int packet_bytes = random_int( 1, MAX_PACKET_BYTES );
int i;
for ( i = 0; i < packet_bytes; ++i )
packet_data[i] = (uint8_t) rand();
reliable_endpoint_receive_packet( endpoint, packet_data, packet_bytes );
}
void fuzz_iteration( double time )
{
// real traffic in both directions (most large enough to fragment)
send_random_packet( ctx.client );
send_random_packet( ctx.server );
// deliver queued traffic across the lossy / reordering / corrupting link
link_flush( &ctx.client_to_server, ctx.server );
link_flush( &ctx.server_to_client, ctx.client );
// inject fully random packets straight into receive (the classic fuzz path)
inject_random_packet( ctx.client );
inject_random_packet( ctx.server );
reliable_endpoint_update( ctx.client, time );
reliable_endpoint_update( ctx.server, time );
reliable_endpoint_clear_acks( ctx.client );
reliable_endpoint_clear_acks( ctx.server );
}
int main( int argc, char ** argv )
{
printf( "[fuzz]\n" );
int num_iterations = -1;
unsigned int seed = (unsigned int) time( NULL );
if ( argc >= 2 )
num_iterations = atoi( argv[1] );
if ( argc >= 3 )
seed = (unsigned int) strtoul( argv[2], NULL, 10 );
printf( "seed = %u\n", seed );
srand( seed );
fuzz_initialize();
signal( SIGINT, interrupt_handler );
double delta_time = 0.1;
if ( num_iterations > 0 )
{
int i;
for ( i = 0; i < num_iterations; ++i )
{
if ( quit )
break;
if ( ( i % 1000 ) == 0 )
{
printf( "." );
fflush( stdout );
}
fuzz_iteration( global_time );
global_time += delta_time;
}
}
else
{
int i = 0;
while ( !quit )
{
if ( ( i++ % 1000 ) == 0 )
{
printf( "." );
fflush( stdout );
}
fuzz_iteration( global_time );
global_time += delta_time;
}
}
fuzz_shutdown();
return 0;
}