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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
|
/*
* Copyright (c) 2024-2025 Pablo Neira Ayuso <pablo@netfilter.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 (or any
* later) as published by the Free Software Foundation.
*/
/* Funded through the NGI0 Entrust established by NLnet (https://nlnet.nl)
* with support from the European Commission's Next Generation Internet
* programme.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include "fuzz.h"
static const char *fuzz_type_name[] = {
[FUZZ_DEL_TABLE] = "deltable",
[FUZZ_DEL_BASECHAIN] = "delbasechain",
[FUZZ_DEL_CHAIN] = "delchain",
[FUZZ_DEL_RULE] = "delrule",
[FUZZ_DEL_SET] = "delset",
[FUZZ_FLUSH_SET] = "flushset",
[FUZZ_DEL_ELEM] = "delelem",
[FUZZ_DEL_OBJ] = "delobj",
[FUZZ_DUP] = "dup",
[FUZZ_REVERSE_COMMIT] = "reverse-commit",
[FUZZ_REVERSE_ABORT] = "reverse-abort",
[FUZZ_TABLE_DORMANT] = "table-dormant",
[FUZZ_TABLE_WAKEUP] = "table-wakeup",
[FUZZ_SWAP] = "swap",
[FUZZ_BOGUS] = "bogus",
};
void print_fuzz_modes(void)
{
int i;
for (i = 0; i < FUZZ_MAX; i++)
printf("\t%s\n", fuzz_type_name[i]);
}
const char *fuzz_type_to_name(uint32_t type)
{
if (type >= FUZZ_MAX)
return "unknown";
return fuzz_type_name[type];
}
int fuzz_type_to_value(const char *str)
{
int i;
for (i = 0; i < FUZZ_MAX; i++) {
if (!strcmp(fuzz_type_name[i], str))
return i;
}
return -1;
}
struct line_args {
char n1[1024];
char n2[1024];
char n3[1024];
char n4[1024];
char n5[1024];
char n6[1024];
char n7[1024];
char n8[1024];
char n9[1024];
char n10[1024];
char n11[1024];
char n12[1024];
char n13[1024];
char n14[1024];
char n15[1024];
};
#define BUF_SIZE 1024000
struct fuzz_state_ctx {
char buf[1024000];
uint32_t len;
char saved_line[1024];
uint32_t state;
uint32_t line;
uint32_t line_from;
struct line_args saved_args;
uint32_t more;
bool found;
bool found2;
bool done;
};
static void add_line(struct fuzz_state_ctx *ctx, const char *line)
{
snprintf(&ctx->buf[ctx->len], BUF_SIZE - ctx->len, "%s", line);
ctx->len += strlen(line);
}
static void reverse_cmd(struct fuzz_state_ctx *ctx, const char *line, int type)
{
struct line_args *args = &ctx->saved_args;
int ret;
add_line(ctx, line);
if (!strcmp(line, "commit();\n") ||
!strcmp(line, "abort();\n"))
return;
if (ctx->done) {
ctx->more++;
return;
}
switch (type) {
case FUZZ_DEL_TABLE:
case FUZZ_TABLE_DORMANT:
case FUZZ_TABLE_WAKEUP:
if (sscanf(line, "add_table(%[^,],%[^,],%[^,],%[^,],%[^)]);",
args->n1, args->n2, args->n3, args->n4, args->n5) == 5) {
ctx->found2 = 1;
}
break;
case FUZZ_DEL_BASECHAIN:
if (sscanf(line, "add_basechain(%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^)]);",
args->n1, args->n2, args->n3, args->n4, args->n5, args->n6, args->n7, args->n8, args->n9, args->n10, args->n11, args->n12, args->n13) == 13) {
ctx->found2 = 1;
}
break;
case FUZZ_DEL_CHAIN:
if (sscanf(line, "add_chain(%[^,],%[^,],%[^,],%[^,],%[^)]);",
args->n1, args->n2, args->n3, args->n4, args->n5) == 5) {
ctx->found2 = 1;
}
break;
case FUZZ_DEL_RULE:
if (sscanf(line, "add_rule(%[^,],%[^,],%[^,],%[^,],%[^)]);",
args->n1, args->n2, args->n3, args->n4, args->n5) == 5) {
ctx->found2 = 1;
}
break;
case FUZZ_DEL_SET:
case FUZZ_FLUSH_SET:
if (sscanf(line, "add_set(%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^)]);",
args->n1, args->n2, args->n3, args->n4, args->n5, args->n6, args->n7, args->n8, args->n9, args->n10, args->n11, args->n12, args->n13, args->n14, args->n15) == 15) {
ctx->found2 = 1;
}
break;
case FUZZ_DEL_OBJ:
if (sscanf(line, "add_obj_counter(%[^,],%[^,],%[^,],%[^,],%[^)]);",
args->n1, args->n2, args->n3, args->n4, args->n5) == 5) {
ctx->found2 = 1;
} else if (sscanf(line, "add_obj_quota(%[^,],%[^,],%[^,],%[^,],%[^,],%[^)]);",
args->n1, args->n2, args->n3, args->n4, args->n5, args->n6) == 6) {
ctx->found2 = 1;
} else if (sscanf(line, "add_obj_connlimit(%[^,],%[^,],%[^,],%[^,],%[^)]);",
args->n1, args->n2, args->n3, args->n4, args->n5) == 5) {
ctx->found2 = 1;
} else if (sscanf(line, "add_obj_secmark(%[^,],%[^,],%[^,],%[^)]);",
args->n1, args->n2, args->n3, args->n4) == 4) {
ctx->found2 = 1;
} else if (sscanf(line, "add_obj_synproxy(%[^,],%[^,],%[^,],%[^,],%[^,],%[^)]);",
args->n1, args->n2, args->n3, args->n4, args->n5, args->n6) == 6) {
ctx->found2 = 1;
} else if (sscanf(line, "add_obj_ct_timeout(%[^,],%[^,],%[^,],%[^,],%[^,],%[^)]);",
args->n1, args->n2, args->n3, args->n4, args->n5, args->n6) == 6) {
ctx->found2 = 1;
} else if (sscanf(line, "add_obj_ct_helper(%[^,],%[^,],%[^,],%[^,],%[^,],%[^)]);",
args->n1, args->n2, args->n3, args->n4, args->n5, args->n6) == 6) {
ctx->found2 = 1;
} else if (sscanf(line, "add_obj_ct_expect(%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^)]);",
args->n1, args->n2, args->n3, args->n4, args->n5, args->n6, args->n7, args->n8) == 8) {
ctx->found2 = 1;
} else if (sscanf(line, "add_obj_limit(%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^)]);",
args->n1, args->n2, args->n3, args->n4, args->n5, args->n6, args->n7, args->n8) == 8) {
ctx->found2 = 1;
} else if (sscanf(line, "add_obj_tunnel(%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^)]);",
args->n1, args->n2, args->n3, args->n4, args->n5, args->n6, args->n7, args->n8, args->n9, args->n10, args->n11, args->n12, args->n13, args->n14) == 14) {
ctx->found2 = 1;
}
break;
}
if (!ctx->found2)
return;
switch (type) {
case FUZZ_DEL_TABLE:
ctx->len += snprintf(&ctx->buf[ctx->len], BUF_SIZE - ctx->len, "del_table(%s,%s, NULL);\n", args->n1, args->n2);
break;
case FUZZ_DEL_BASECHAIN:
ctx->len += snprintf(&ctx->buf[ctx->len], BUF_SIZE - ctx->len, "del_basechain(%s,%s, NULL, NULL, NULL, NULL, NULL, NULL);\n", args->n1, args->n2);
break;
case FUZZ_DEL_CHAIN:
ctx->len += snprintf(&ctx->buf[ctx->len], BUF_SIZE - ctx->len, "del_chain(%s);\n", args->n1);
break;
case FUZZ_DEL_RULE:
ctx->len += snprintf(&ctx->buf[ctx->len], BUF_SIZE - ctx->len, "del_rule(%s,%s, NULL);\n", args->n1, args->n2);
break;
case FUZZ_DEL_SET:
ctx->len += snprintf(&ctx->buf[ctx->len], BUF_SIZE - ctx->len, "del_set(%s, NULL);\n", args->n1);
break;
case FUZZ_FLUSH_SET:
ctx->len += snprintf(&ctx->buf[ctx->len], BUF_SIZE - ctx->len, "flush_set(%s, NULL);\n", args->n1);
break;
case FUZZ_TABLE_DORMANT:
ctx->len += snprintf(&ctx->buf[ctx->len], BUF_SIZE - ctx->len, "add_table(%s,%s, \"0x1\",%s,%s);\n", args->n1, args->n2, args->n4, args->n5);
break;
case FUZZ_TABLE_WAKEUP:
ctx->len += snprintf(&ctx->buf[ctx->len], BUF_SIZE - ctx->len, "add_table(%s,%s, \"0x0\",%s,%s);\n", args->n1, args->n2, args->n4, args->n5);
break;
case FUZZ_DEL_OBJ:
ctx->len += snprintf(&ctx->buf[ctx->len], BUF_SIZE - ctx->len, "del_obj_unknown(%s, NULL, NULL, \"1\");\n", args->n1);
break;
}
ctx->line_from = ctx->line;
ctx->done = 1;
}
static void del_elem(struct fuzz_state_ctx *ctx, const char *line)
{
struct line_args args = {};
add_line(ctx, line);
if (sscanf(line, "add_elem(%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^)]);",
args.n1, args.n2, args.n3, args.n4, args.n5, args.n6, args.n7, args.n8, args.n9) != 9)
return;
if (ctx->found) {
ctx->more++;
return;
}
ctx->len += snprintf(&ctx->buf[ctx->len], BUF_SIZE - ctx->len, "del_elem(%s, NULL, NULL);\n", args.n1);
ctx->line_from = ctx->line;
ctx->found = 1;
}
static void dup_line(struct fuzz_state_ctx *ctx, const char *line)
{
add_line(ctx, line);
if (!strcmp(line, "commit();\n") ||
!strcmp(line, "abort();\n"))
return;
if (ctx->found) {
ctx->more++;
return;
}
snprintf(&ctx->buf[ctx->len], BUF_SIZE - ctx->len, "%s", line);
ctx->len += strlen(line);
ctx->line_from = ctx->line;
ctx->found = 1;
}
static void reverse_abort(struct fuzz_state_ctx *ctx, const char *line)
{
if (strcmp(line, "abort();\n")) {
add_line(ctx, line);
return;
}
if (ctx->found) {
add_line(ctx, line);
ctx->more++;
return;
}
ctx->len += snprintf(&ctx->buf[ctx->len], BUF_SIZE - ctx->len, "commit();\n");
ctx->line_from = ctx->line;
ctx->found = 1;
}
static void reverse_commit(struct fuzz_state_ctx *ctx, const char *line)
{
if (strcmp(line, "commit();\n")) {
add_line(ctx, line);
return;
}
if (ctx->found) {
add_line(ctx, line);
ctx->more++;
return;
}
ctx->len += snprintf(&ctx->buf[ctx->len], BUF_SIZE - ctx->len, "abort();\n");
ctx->line_from = ctx->line;
ctx->found = 1;
}
static void swap(struct fuzz_state_ctx *ctx, const char *line)
{
if (!strcmp(line, "commit();\n") ||
!strcmp(line, "abort();\n")) {
if (ctx->done) {
if (!ctx->found) {
add_line(ctx, ctx->saved_line);
ctx->found = 1;
}
}
add_line(ctx, line);
return;
}
if (!strncmp(line, "add_table(", strlen("add_table(")) ||
!strncmp(line, "set_table(", strlen("set_table("))) {
add_line(ctx, line);
return;
}
if (ctx->done) {
add_line(ctx, line);
if (!ctx->found) {
add_line(ctx, ctx->saved_line);
ctx->found = 1;
}
ctx->more++;
return;
}
snprintf(ctx->saved_line, sizeof(ctx->saved_line), "%s", line);
ctx->line_from = ctx->line;
ctx->done = 1;
}
static void bogus(struct fuzz_state_ctx *ctx, const char *line)
{
struct line_args args = {};
add_line(ctx, line);
if (!strcmp(line, "commit();\n") ||
!strcmp(line, "abort();\n"))
return;
if (ctx->done) {
ctx->more++;
return;
}
ctx->len += snprintf(&ctx->buf[ctx->len], BUF_SIZE - ctx->len, "del_table(NFPROTO_IPV4, \"bogus123456unknown\", NULL);\n");
ctx->line_from = ctx->line;
ctx->done = 1;
}
static void fuzz_step_cb(struct fuzz_state_ctx *ctx, const char *line)
{
if (ctx->line++ < ctx->line_from) {
add_line(ctx, line);
return;
}
switch (ctx->state) {
case FUZZ_DEL_TABLE:
case FUZZ_DEL_BASECHAIN:
case FUZZ_DEL_CHAIN:
case FUZZ_DEL_RULE:
case FUZZ_DEL_SET:
case FUZZ_FLUSH_SET:
case FUZZ_DEL_OBJ:
case FUZZ_TABLE_DORMANT:
case FUZZ_TABLE_WAKEUP:
reverse_cmd(ctx, line, ctx->state);
break;
case FUZZ_DEL_ELEM:
del_elem(ctx, line);
break;
case FUZZ_DUP:
dup_line(ctx, line);
break;
case FUZZ_REVERSE_COMMIT:
reverse_commit(ctx, line);
break;
case FUZZ_REVERSE_ABORT:
reverse_abort(ctx, line);
break;
case FUZZ_SWAP:
swap(ctx, line);
break;
case FUZZ_BOGUS:
bogus(ctx, line);
break;
}
}
static FILE *fuzz_step(struct fuzz_state_ctx *ctx, FILE *fp,
void (*fuzz_step_cb)(struct fuzz_state_ctx *ctx, const char *line))
{
const char *line;
char buf[1024];
FILE *new_fp;
ctx->line = 0;
ctx->len = 0;
ctx->more = 0;
ctx->found = 0;
ctx->done = 0;
line = fgets(buf, sizeof(buf), fp);
while (line) {
fuzz_step_cb(ctx, line);
line = fgets(buf, sizeof(buf), fp);
}
new_fp = fmemopen(ctx->buf, ctx->len, "r");
if (!new_fp) {
fprintf(stderr, "cannot open memfile\n");
return NULL;
}
return new_fp;
}
static int fuzz_print(FILE *fp)
{
const char *line;
char buf[1024];
FILE *fp2;
line = fgets(buf, sizeof(buf), fp);
while (line) {
fprintf(stdout, "%s", line);
line = fgets(buf, sizeof(buf), fp);
}
rewind(fp);
return 0;
}
int fuzz_loop(struct fuzz_ctx *ctx, FILE *fp, int (*fuzz_cb)(FILE *fp))
{
struct fuzz_state_ctx state_ctx = {
.state = ctx->type[ctx->index],
};
struct timeval tv_cur, tv_diff;
FILE *new_fp;
int ret = 0;
if (ctx->index >= ctx->num_types) {
if (ctx->debug)
printf("<<<< fuzz_loop backtrack STACK limit reached\n");
ctx->index--;
return 0;
}
if (ctx->debug) {
printf(">>>> fuzz_loop at index %d in state=%x\n",
ctx->index, ctx->type[ctx->index]);
}
while (1) {
new_fp = fuzz_step(&state_ctx, fp, fuzz_step_cb);
if (!new_fp)
break;
ctx->fuzz_steps++;
ctx->total_fuzz_steps++;
if (ctx->fuzz_steps % 100 == 0) {
gettimeofday(&tv_cur, NULL);
timersub(&tv_cur, &ctx->tv_start, &tv_diff);
printf("... fuzz steps: %u in %u.%u seconds\n",
ctx->fuzz_steps, tv_diff.tv_sec, tv_diff.tv_usec);
}
if (ctx->debug)
fuzz_print(new_fp);
if (fuzz_cb(new_fp) < 0) {
ret = -1;
fclose(new_fp);
break;
}
if (ctx->index < ctx->num_types) {
ctx->index++;
if (fuzz_loop(ctx, new_fp, fuzz_cb) < 0) {
ret = -1;
fclose(new_fp);
break;
}
}
fclose(new_fp);
if (!state_ctx.more) {
if (ctx->debug) {
printf("<<<< fuzz_loop backtrack NO MORE at index %u in state=%x\n",
ctx->index, ctx->type[ctx->index]);
}
if (ctx->index > 0)
ctx->index--;
break;
} else {
if (ctx->debug) {
printf("==== still more tries at index %u in state=%x\n",
ctx->index, ctx->type[ctx->index]);
}
}
rewind(fp);
}
return ret;
}
int fuzz(struct fuzz_ctx *ctx, FILE *fp, int (*fuzz_cb)(FILE *fp))
{
struct timeval tv_cur;
int ret;
gettimeofday(&ctx->tv_start, NULL);
ctx->fuzz_steps = 0;
ret = fuzz_loop(ctx, fp, fuzz_cb);
gettimeofday(&tv_cur, NULL);
timersub(&tv_cur, &ctx->tv_start, &ctx->tv_delta);
return ret;
}
|