Debian Codegraph @

fuzz/genSeed.c

raw
1 /*
2 * xmlSeed.c: Generate the XML seed corpus for fuzzing.
3 *
4 * See Copyright for the status of this software.
5 */
6
7 #include <stdio.h>
8 #include <string.h>
9 #include <glob.h>
10 #include <libgen.h>
11 #include <sys/stat.h>
12
13 #ifdef _WIN32
14 #include <direct.h>
15 #else
16 #include <unistd.h>
17 #endif
18
19 #include <libxml/parser.h>
20 #include <libxml/parserInternals.h>
21 #include <libxml/HTMLparser.h>
22 #include <libxml/xinclude.h>
23 #include <libxml/xmlschemas.h>
24 #include "fuzz.h"
25
26 #define PATH_SIZE 500
27 #define SEED_BUF_SIZE 16384
28 #define EXPR_SIZE 4500
29
30 #define FLAG_READER (1 << 0)
31 #define FLAG_LINT (1 << 1)
32 #define FLAG_PUSH_CHUNK_SIZE (1 << 2)
33
34 typedef int
35 (*fileFunc)(const char *base, FILE *out);
36
37 typedef int
38 (*mainFunc)(const char *arg);
39
40 static struct {
41 FILE *out;
42 xmlHashTablePtr entities; /* Maps URLs to xmlFuzzEntityInfos */
43 xmlExternalEntityLoader oldLoader;
44 fileFunc processFile;
45 const char *fuzzer;
46 int counter;
47 char cwd[PATH_SIZE];
48 int flags;
49 } globalData;
50
51 #if defined(HAVE_SCHEMA_FUZZER) || \
52 defined(HAVE_XML_FUZZER)
53 /*
54 * A custom resource loader that writes all external DTDs or entities to a
55 * single file in the format expected by xmlFuzzResourceLoader.
56 */
57 static xmlParserErrors
58 fuzzResourceRecorder(void *data ATTRIBUTE_UNUSED, const char *URL,
59 const char *ID ATTRIBUTE_UNUSED,
60 xmlResourceType type ATTRIBUTE_UNUSED,
61 xmlParserInputFlags flags,
62 xmlParserInputPtr *out) {
63 *out = NULL;
64
65 if (globalData.entities == NULL ||
66 xmlHashLookup(globalData.entities, BAD_CAST URL) == NULL) {
67 data = xmlSlurpFile(URL, NULL);
68
69 if (globalData.entities == NULL)
70 globalData.entities = xmlHashCreate(4);
71
72 xmlHashAddEntry(globalData.entities, (const xmlChar *) URL, data);
73 }
74
75 return(xmlNewInputFromUrl(URL, flags, out));
76 }
77
78 static void
79 fuzzRecorderInit(FILE *out) {
80 globalData.out = out;
81 globalData.entities = xmlHashCreate(8);
82 globalData.oldLoader = xmlGetExternalEntityLoader();
83 }
84
85 static void
86 fuzzRecorderWriteAndFree(void *entry, const xmlChar *file) {
87 char *data = entry;
88 xmlFuzzWriteString(globalData.out, (const char *) file);
89 xmlFuzzWriteString(globalData.out, data);
90 xmlFree(data);
91 }
92
93 static void
94 fuzzRecorderWrite(const char *file) {
95 xmlHashRemoveEntry(globalData.entities, (const xmlChar *) file,
96 fuzzRecorderWriteAndFree);
97 }
98
99 static void
100 fuzzRecorderCleanup(void) {
101 /* Write remaining entities (in random order). */
102 xmlHashFree(globalData.entities, fuzzRecorderWriteAndFree);
103 globalData.out = NULL;
104 globalData.entities = NULL;
105 globalData.oldLoader = NULL;
106 }
107 #endif
108
109 #ifdef HAVE_XML_FUZZER
110 static int
111 processXml(const char *docFile, FILE *out) {
112 int opts = XML_PARSE_NOENT | XML_PARSE_DTDLOAD;
113 xmlParserCtxtPtr ctxt;
114 xmlDocPtr doc;
115
116 if (globalData.flags & FLAG_LINT) {
117 /* Switches */
118 xmlFuzzWriteInt(out, 0, 4);
119 xmlFuzzWriteInt(out, 0, 4);
120 /* maxmem */
121 xmlFuzzWriteInt(out, 0, 4);
122 /* max-ampl */
123 xmlFuzzWriteInt(out, 0, 1);
124 /* pretty */
125 xmlFuzzWriteInt(out, 0, 1);
126 /* encode */
127 xmlFuzzWriteString(out, "");
128 /* pattern */
129 xmlFuzzWriteString(out, "");
130 /* xpath */
131 xmlFuzzWriteString(out, "");
132 } else {
133 /* Parser options. */
134 xmlFuzzWriteInt(out, opts, 4);
135 /* Max allocations. */
136 xmlFuzzWriteInt(out, 0, 4);
137
138 if (globalData.flags & FLAG_PUSH_CHUNK_SIZE) {
139 /* Chunk size for push parser */
140 xmlFuzzWriteInt(out, 256, 4);
141 }
142
143 if (globalData.flags & FLAG_READER) {
144 /* Initial reader program with a couple of OP_READs */
145 xmlFuzzWriteString(out, "\x01\x01\x01\x01\x01\x01\x01\x01");
146 }
147 }
148
149 fuzzRecorderInit(out);
150
151 ctxt = xmlNewParserCtxt();
152 xmlCtxtSetErrorHandler(ctxt, xmlFuzzSErrorFunc, NULL);
153 xmlCtxtSetResourceLoader(ctxt, fuzzResourceRecorder, NULL);
154 doc = xmlCtxtReadFile(ctxt, docFile, NULL, opts);
155 #ifdef LIBXML_XINCLUDE_ENABLED
156 {
157 xmlXIncludeCtxtPtr xinc = xmlXIncludeNewContext(doc);
158
159 xmlXIncludeSetErrorHandler(xinc, xmlFuzzSErrorFunc, NULL);
160 xmlXIncludeSetResourceLoader(xinc, fuzzResourceRecorder, NULL);
161 xmlXIncludeSetFlags(xinc, opts);
162 xmlXIncludeProcessNode(xinc, (xmlNodePtr) doc);
163 xmlXIncludeFreeContext(xinc);
164 }
165 #endif
166 xmlFreeDoc(doc);
167 xmlFreeParserCtxt(ctxt);
168
169 fuzzRecorderWrite(docFile);
170 fuzzRecorderCleanup();
171
172 return(0);
173 }
174 #endif
175
176 #ifdef HAVE_HTML_FUZZER
177 static int
178 processHtml(const char *docFile, FILE *out) {
179 char buf[SEED_BUF_SIZE];
180 FILE *file;
181 size_t size;
182
183 /* Parser options. */
184 xmlFuzzWriteInt(out, 0, 4);
185 /* Max allocations. */
186 xmlFuzzWriteInt(out, 0, 4);
187
188 /* Copy file */
189 file = fopen(docFile, "rb");
190 if (file == NULL) {
191 fprintf(stderr, "couldn't open %s\n", docFile);
192 return(0);
193 }
194 do {
195 size = fread(buf, 1, SEED_BUF_SIZE, file);
196 if (size > 0)
197 fwrite(buf, 1, size, out);
198 } while (size == SEED_BUF_SIZE);
199 fclose(file);
200
201 return(0);
202 }
203 #endif
204
205 #if defined(HAVE_HTML_FUZZER) || \
206 defined(HAVE_XML_FUZZER)
207 static int
208 processPattern(const char *pattern) {
209 glob_t globbuf;
210 int ret = 0;
211 int res;
212 size_t i;
213
214 res = glob(pattern, 0, NULL, &globbuf);
215 if (res == GLOB_NOMATCH)
216 return(0);
217 if (res != 0) {
218 fprintf(stderr, "couldn't match pattern %s\n", pattern);
219 return(-1);
220 }
221
222 for (i = 0; i < globbuf.gl_pathc; i++) {
223 struct stat statbuf;
224 char outPath[PATH_SIZE];
225 char *dirBuf = NULL;
226 char *baseBuf = NULL;
227 const char *path, *dir, *base;
228 FILE *out = NULL;
229 int dirChanged = 0;
230 size_t size;
231
232 path = globbuf.gl_pathv[i];
233
234 if ((stat(path, &statbuf) != 0) || (!S_ISREG(statbuf.st_mode)))
235 continue;
236
237 dirBuf = (char *) xmlCharStrdup(path);
238 baseBuf = (char *) xmlCharStrdup(path);
239 if ((dirBuf == NULL) || (baseBuf == NULL)) {
240 fprintf(stderr, "memory allocation failed\n");
241 ret = -1;
242 goto error;
243 }
244 dir = dirname(dirBuf);
245 base = basename(baseBuf);
246
247 size = snprintf(outPath, sizeof(outPath), "seed/%s/%s",
248 globalData.fuzzer, base);
249 if (size >= PATH_SIZE) {
250 fprintf(stderr, "creating path failed\n");
251 ret = -1;
252 goto error;
253 }
254 out = fopen(outPath, "wb");
255 if (out == NULL) {
256 fprintf(stderr, "couldn't open %s for writing\n", outPath);
257 ret = -1;
258 goto error;
259 }
260 if (chdir(dir) != 0) {
261 fprintf(stderr, "couldn't chdir to %s\n", dir);
262 ret = -1;
263 goto error;
264 }
265 dirChanged = 1;
266 if (globalData.processFile(base, out) != 0)
267 ret = -1;
268
269 error:
270 if (out != NULL)
271 fclose(out);
272 xmlFree(dirBuf);
273 xmlFree(baseBuf);
274 if ((dirChanged) && (chdir(globalData.cwd) != 0)) {
275 fprintf(stderr, "couldn't chdir to %s\n", globalData.cwd);
276 ret = -1;
277 break;
278 }
279 }
280
281 globfree(&globbuf);
282 return(ret);
283 }
284 #endif
285
286 #if defined(HAVE_SCHEMA_FUZZER)
287 static int
288 processSchema(const char *xsdFile, const char *xmlFile, FILE *out) {
289 xmlSchemaPtr schema;
290 xmlSchemaParserCtxtPtr pctxt;
291
292 /* Max allocations. */
293 xmlFuzzWriteInt(out, 0, 4);
294
295 fuzzRecorderInit(out);
296
297 pctxt = xmlSchemaNewParserCtxt(xsdFile);
298 xmlSchemaSetParserStructuredErrors(pctxt, xmlFuzzSErrorFunc, NULL);
299 xmlSchemaSetResourceLoader(pctxt, fuzzResourceRecorder, NULL);
300 schema = xmlSchemaParse(pctxt);
301 xmlSchemaFreeParserCtxt(pctxt);
302
303 if (schema != NULL) {
304 xmlSchemaValidCtxtPtr vctxt;
305 xmlParserCtxtPtr ctxt;
306 xmlDocPtr doc;
307
308 ctxt = xmlNewParserCtxt();
309 xmlCtxtSetErrorHandler(ctxt, xmlFuzzSErrorFunc, NULL);
310 xmlCtxtSetResourceLoader(ctxt, fuzzResourceRecorder, NULL);
311 doc = xmlCtxtReadFile(ctxt, xmlFile, NULL, XML_PARSE_NOENT);
312 xmlFreeParserCtxt(ctxt);
313
314 vctxt = xmlSchemaNewValidCtxt(schema);
315 xmlSchemaSetValidStructuredErrors(vctxt, xmlFuzzSErrorFunc, NULL);
316 xmlSchemaValidateDoc(vctxt, doc);
317 xmlSchemaFreeValidCtxt(vctxt);
318
319 xmlFreeDoc(doc);
320 xmlSchemaFree(schema);
321 }
322
323 fuzzRecorderWrite(xsdFile);
324 fuzzRecorderWrite(xmlFile);
325 fuzzRecorderCleanup();
326
327 return(0);
328 }
329
330 static int
331 processSchemaPattern(const char *pattern) {
332 glob_t globbuf;
333 int ret = 0;
334 int res;
335 size_t i;
336
337 res = glob(pattern, 0, NULL, &globbuf);
338 if (res == GLOB_NOMATCH)
339 return(0);
340 if (res != 0) {
341 fprintf(stderr, "couldn't match pattern %s\n", pattern);
342 return(-1);
343 }
344
345 for (i = 0; i < globbuf.gl_pathc; i++) {
346 glob_t globbuf2;
347 struct stat statbuf;
348 char xmlPattern[PATH_SIZE];
349 char *dirBuf = NULL;
350 char *baseBuf = NULL;
351 const char *path, *dir, *base;
352 size_t size, dirLen, baseLen, len, j;
353
354 path = globbuf.gl_pathv[i];
355
356 if ((stat(path, &statbuf) != 0) || (!S_ISREG(statbuf.st_mode)))
357 continue;
358
359 dirBuf = (char *) xmlCharStrdup(path);
360 baseBuf = (char *) xmlCharStrdup(path);
361 if ((dirBuf == NULL) || (baseBuf == NULL)) {
362 fprintf(stderr, "memory allocation failed\n");
363 ret = -1;
364 goto error;
365 }
366 dir = dirname(dirBuf);
367 dirLen = strlen(dir);
368 base = basename(baseBuf);
369 baseLen = strlen(base);
370
371 len = strlen(path);
372 if (len <= 5)
373 continue;
374 /* Strip .xsl or _0.xsd suffix */
375 if (len > 6 && path[len - 6] == '_')
376 len -= 6;
377 else
378 len -= 4;
379 size = snprintf(xmlPattern, sizeof(xmlPattern), "%.*s_*.xml",
380 (int) len, path);
381 if (size >= PATH_SIZE) {
382 fprintf(stderr, "creating path failed\n");
383 ret = -1;
384 goto error;
385 }
386
387 res = glob(xmlPattern, 0, NULL, &globbuf2);
388 if (res == GLOB_NOMATCH)
389 goto error;
390 if (res != 0) {
391 fprintf(stderr, "couldn't match pattern %s\n", xmlPattern);
392 ret = -1;
393 goto error;
394 }
395
396 for (j = 0; j < globbuf2.gl_pathc; j++) {
397 char outPath[PATH_SIZE];
398 const char *xmlFile;
399 FILE *out = NULL;
400
401 xmlFile = globbuf2.gl_pathv[j];
402
403 len = strlen(xmlFile);
404 if (len < dirLen + 7)
405 continue;
406 if (len >= 6 && xmlFile[len - 6] == '_')
407 size = snprintf(outPath, sizeof(outPath), "seed/%s/%.*s_%c",
408 globalData.fuzzer, (int) baseLen - 4, base,
409 xmlFile[len - 5]);
410 else
411 size = snprintf(outPath, sizeof(outPath), "seed/%s/%.*s",
412 globalData.fuzzer, (int) baseLen - 4, base);
413
414 if (size >= PATH_SIZE) {
415 fprintf(stderr, "creating path failed\n");
416 ret = -1;
417 continue;
418 }
419 out = fopen(outPath, "wb");
420 if (out == NULL) {
421 fprintf(stderr, "couldn't open %s for writing\n", outPath);
422 ret = -1;
423 continue;
424 }
425
426 if (chdir(dir) != 0) {
427 fprintf(stderr, "couldn't chdir to %s\n", dir);
428 ret = -1;
429 } else {
430 if (processSchema(base, xmlFile + dirLen + 1, out) != 0)
431 ret = -1;
432 }
433
434 fclose(out);
435
436 if (chdir(globalData.cwd) != 0) {
437 fprintf(stderr, "couldn't chdir to %s\n", globalData.cwd);
438 ret = -1;
439 break;
440 }
441 }
442
443 globfree(&globbuf2);
444
445 error:
446 xmlFree(dirBuf);
447 xmlFree(baseBuf);
448 }
449
450 globfree(&globbuf);
451 return(ret);
452 }
453 #endif
454
455 #ifdef HAVE_XPATH_FUZZER
456 static int
457 processXPath(const char *testDir, const char *prefix, const char *name,
458 const char *data, const char *subdir, int xptr) {
459 char pattern[PATH_SIZE];
460 glob_t globbuf;
461 size_t i, size;
462 int ret = 0, res;
463
464 size = snprintf(pattern, sizeof(pattern), "%s/%s/%s*",
465 testDir, subdir, prefix);
466 if (size >= PATH_SIZE)
467 return(-1);
468 res = glob(pattern, 0, NULL, &globbuf);
469 if (res == GLOB_NOMATCH)
470 return(0);
471 if (res != 0) {
472 fprintf(stderr, "couldn't match pattern %s\n", pattern);
473 return(-1);
474 }
475
476 for (i = 0; i < globbuf.gl_pathc; i++) {
477 char *path = globbuf.gl_pathv[i];
478 struct stat statbuf;
479 FILE *in;
480 char expr[EXPR_SIZE];
481
482 if ((stat(path, &statbuf) != 0) || (!S_ISREG(statbuf.st_mode)))
483 continue;
484
485 in = fopen(path, "rb");
486 if (in == NULL) {
487 ret = -1;
488 continue;
489 }
490
491 while (fgets(expr, EXPR_SIZE, in) != NULL) {
492 char outPath[PATH_SIZE];
493 FILE *out;
494 int j;
495
496 for (j = 0; expr[j] != 0; j++)
497 if (expr[j] == '\r' || expr[j] == '\n')
498 break;
499 expr[j] = 0;
500
501 size = snprintf(outPath, sizeof(outPath), "seed/xpath/%s-%d",
502 name, globalData.counter);
503 if (size >= PATH_SIZE) {
504 ret = -1;
505 continue;
506 }
507 out = fopen(outPath, "wb");
508 if (out == NULL) {
509 ret = -1;
510 continue;
511 }
512
513 /* Max allocations. */
514 xmlFuzzWriteInt(out, 0, 4);
515
516 if (xptr) {
517 xmlFuzzWriteString(out, expr);
518 } else {
519 char xptrExpr[EXPR_SIZE+100];
520
521 /* Wrap XPath expressions as XPointer */
522 snprintf(xptrExpr, sizeof(xptrExpr), "xpointer(%s)", expr);
523 xmlFuzzWriteString(out, xptrExpr);
524 }
525
526 xmlFuzzWriteString(out, data);
527
528 fclose(out);
529 globalData.counter++;
530 }
531
532 fclose(in);
533 }
534
535 globfree(&globbuf);
536
537 return(ret);
538 }
539
540 static int
541 processXPathDir(const char *testDir) {
542 char pattern[PATH_SIZE];
543 glob_t globbuf;
544 size_t i, size;
545 int ret = 0;
546
547 globalData.counter = 1;
548 if (processXPath(testDir, "", "expr", "<d></d>", "expr", 0) != 0)
549 ret = -1;
550
551 size = snprintf(pattern, sizeof(pattern), "%s/docs/*", testDir);
552 if (size >= PATH_SIZE)
553 return(1);
554 if (glob(pattern, 0, NULL, &globbuf) != 0)
555 return(1);
556
557 for (i = 0; i < globbuf.gl_pathc; i++) {
558 char *path = globbuf.gl_pathv[i];
559 char *data;
560 const char *docFile;
561
562 data = xmlSlurpFile(path, NULL);
563 if (data == NULL) {
564 ret = -1;
565 continue;
566 }
567 docFile = basename(path);
568
569 globalData.counter = 1;
570 if (processXPath(testDir, docFile, docFile, data, "tests", 0) != 0)
571 ret = -1;
572 if (processXPath(testDir, docFile, docFile, data, "xptr", 1) != 0)
573 ret = -1;
574 if (processXPath(testDir, docFile, docFile, data, "xptr-xp1", 1) != 0)
575 ret = -1;
576
577 xmlFree(data);
578 }
579
580 globfree(&globbuf);
581
582 return(ret);
583 }
584 #endif
585
586 int
587 main(int argc, const char **argv) {
588 mainFunc processArg = NULL;
589 const char *fuzzer;
590 int ret = 0;
591 int i;
592
593 if (argc < 3) {
594 fprintf(stderr, "usage: seed [FUZZER] [PATTERN...]\n");
595 return(1);
596 }
597
598 fuzzer = argv[1];
599 if (strcmp(fuzzer, "html") == 0) {
600 #ifdef HAVE_HTML_FUZZER
601 processArg = processPattern;
602 globalData.flags |= FLAG_PUSH_CHUNK_SIZE;
603 globalData.processFile = processHtml;
604 #endif
605 } else if (strcmp(fuzzer, "lint") == 0) {
606 #ifdef HAVE_LINT_FUZZER
607 processArg = processPattern;
608 globalData.flags |= FLAG_LINT;
609 globalData.processFile = processXml;
610 #endif
611 } else if (strcmp(fuzzer, "reader") == 0) {
612 #ifdef HAVE_READER_FUZZER
613 processArg = processPattern;
614 globalData.flags |= FLAG_READER;
615 globalData.processFile = processXml;
616 #endif
617 } else if (strcmp(fuzzer, "schema") == 0) {
618 #ifdef HAVE_SCHEMA_FUZZER
619 processArg = processSchemaPattern;
620 #endif
621 } else if (strcmp(fuzzer, "valid") == 0) {
622 #ifdef HAVE_VALID_FUZZER
623 processArg = processPattern;
624 globalData.processFile = processXml;
625 #endif
626 } else if (strcmp(fuzzer, "xinclude") == 0) {
627 #ifdef HAVE_XINCLUDE_FUZZER
628 processArg = processPattern;
629 globalData.processFile = processXml;
630 #endif
631 } else if (strcmp(fuzzer, "xml") == 0) {
632 #ifdef HAVE_XML_FUZZER
633 processArg = processPattern;
634 globalData.flags |= FLAG_PUSH_CHUNK_SIZE;
635 globalData.processFile = processXml;
636 #endif
637 } else if (strcmp(fuzzer, "xpath") == 0) {
638 #ifdef HAVE_XPATH_FUZZER
639 processArg = processXPathDir;
640 #endif
641 } else {
642 fprintf(stderr, "unknown fuzzer %s\n", fuzzer);
643 return(1);
644 }
645 globalData.fuzzer = fuzzer;
646
647 if (getcwd(globalData.cwd, PATH_SIZE) == NULL) {
648 fprintf(stderr, "couldn't get current directory\n");
649 return(1);
650 }
651
652 if (processArg != NULL)
653 for (i = 2; i < argc; i++)
654 processArg(argv[i]);
655
656 return(ret);
657 }
658