| 1 |
/* |
| 2 |
* valid.c: a libFuzzer target to test DTD validation. |
| 3 |
* |
| 4 |
* See Copyright for the status of this software. |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <libxml/catalog.h> |
| 8 |
#include <libxml/parser.h> |
| 9 |
#include <libxml/tree.h> |
| 10 |
#include <libxml/xmlerror.h> |
| 11 |
#include "fuzz.h" |
| 12 |
|
| 13 |
int |
| 14 |
LLVMFuzzerInitialize(int *argc ATTRIBUTE_UNUSED, |
| 15 |
char ***argv ATTRIBUTE_UNUSED) { |
| 16 |
xmlFuzzMemSetup(); |
| 17 |
xmlInitParser(); |
| 18 |
#ifdef LIBXML_CATALOG_ENABLED |
| 19 |
xmlInitializeCatalog(); |
| 20 |
xmlCatalogSetDefaults(XML_CATA_ALLOW_NONE); |
| 21 |
#endif |
| 22 |
|
| 23 |
return 0; |
| 24 |
} |
| 25 |
|
| 26 |
int |
| 27 |
LLVMFuzzerTestOneInput(const char *data, size_t size) { |
| 28 |
xmlParserCtxtPtr ctxt; |
| 29 |
xmlDocPtr doc; |
| 30 |
const char *docBuffer, *docUrl; |
| 31 |
size_t failurePos, docSize; |
| 32 |
int opts; |
| 33 |
|
| 34 |
xmlFuzzDataInit(data, size); |
| 35 |
opts = (int) xmlFuzzReadInt(4); |
| 36 |
opts |= XML_PARSE_DTDVALID; |
| 37 |
failurePos = xmlFuzzReadInt(4) % (size + 100); |
| 38 |
|
| 39 |
xmlFuzzReadEntities(); |
| 40 |
docBuffer = xmlFuzzMainEntity(&docSize); |
| 41 |
docUrl = xmlFuzzMainUrl(); |
| 42 |
if (docBuffer == NULL) |
| 43 |
goto exit; |
| 44 |
|
| 45 |
/* Pull parser */ |
| 46 |
|
| 47 |
xmlFuzzInjectFailure(failurePos); |
| 48 |
ctxt = xmlNewParserCtxt(); |
| 49 |
if (ctxt != NULL) { |
| 50 |
xmlCtxtSetErrorHandler(ctxt, xmlFuzzSErrorFunc, NULL); |
| 51 |
xmlCtxtSetResourceLoader(ctxt, xmlFuzzResourceLoader, NULL); |
| 52 |
doc = xmlCtxtReadMemory(ctxt, docBuffer, docSize, docUrl, NULL, opts); |
| 53 |
xmlFuzzCheckFailureReport("xmlCtxtReadMemory", |
| 54 |
ctxt->errNo == XML_ERR_NO_MEMORY, |
| 55 |
ctxt->errNo == XML_IO_EIO); |
| 56 |
xmlFreeDoc(doc); |
| 57 |
xmlFreeParserCtxt(ctxt); |
| 58 |
} |
| 59 |
|
| 60 |
/* Post validation */ |
| 61 |
|
| 62 |
xmlFuzzInjectFailure(failurePos); |
| 63 |
ctxt = xmlNewParserCtxt(); |
| 64 |
if (ctxt != NULL) { |
| 65 |
xmlCtxtSetErrorHandler(ctxt, xmlFuzzSErrorFunc, NULL); |
| 66 |
xmlCtxtSetResourceLoader(ctxt, xmlFuzzResourceLoader, NULL); |
| 67 |
doc = xmlCtxtReadMemory(ctxt, docBuffer, docSize, docUrl, NULL, |
| 68 |
opts & ~XML_PARSE_DTDVALID); |
| 69 |
xmlFuzzCheckFailureReport("xmlCtxtReadMemory", |
| 70 |
doc == NULL && ctxt->errNo == XML_ERR_NO_MEMORY, |
| 71 |
doc == NULL && ctxt->errNo == XML_IO_EIO); |
| 72 |
if (doc != NULL) { |
| 73 |
int valid = xmlCtxtValidateDocument(ctxt, doc); |
| 74 |
|
| 75 |
xmlFuzzCheckFailureReport("xmlCtxtValidateDocument", |
| 76 |
!valid && ctxt->errNo == XML_ERR_NO_MEMORY, |
| 77 |
!valid && ctxt->errNo == XML_IO_EIO); |
| 78 |
} |
| 79 |
xmlFreeDoc(doc); |
| 80 |
xmlFreeParserCtxt(ctxt); |
| 81 |
} |
| 82 |
|
| 83 |
/* Push parser */ |
| 84 |
|
| 85 |
#ifdef LIBXML_PUSH_ENABLED |
| 86 |
{ |
| 87 |
static const size_t maxChunkSize = 128; |
| 88 |
size_t consumed, chunkSize; |
| 89 |
|
| 90 |
xmlFuzzInjectFailure(failurePos); |
| 91 |
/* |
| 92 |
* FIXME: xmlCreatePushParserCtxt can still report OOM errors |
| 93 |
* to stderr. |
| 94 |
*/ |
| 95 |
xmlSetGenericErrorFunc(NULL, xmlFuzzErrorFunc); |
| 96 |
ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, docUrl); |
| 97 |
xmlSetGenericErrorFunc(NULL, NULL); |
| 98 |
if (ctxt != NULL) { |
| 99 |
xmlCtxtSetErrorHandler(ctxt, xmlFuzzSErrorFunc, NULL); |
| 100 |
xmlCtxtSetResourceLoader(ctxt, xmlFuzzResourceLoader, NULL); |
| 101 |
xmlCtxtUseOptions(ctxt, opts); |
| 102 |
|
| 103 |
for (consumed = 0; consumed < docSize; consumed += chunkSize) { |
| 104 |
chunkSize = docSize - consumed; |
| 105 |
if (chunkSize > maxChunkSize) |
| 106 |
chunkSize = maxChunkSize; |
| 107 |
xmlParseChunk(ctxt, docBuffer + consumed, chunkSize, 0); |
| 108 |
} |
| 109 |
|
| 110 |
xmlParseChunk(ctxt, NULL, 0, 1); |
| 111 |
xmlFuzzCheckFailureReport("xmlParseChunk", |
| 112 |
ctxt->errNo == XML_ERR_NO_MEMORY, |
| 113 |
ctxt->errNo == XML_IO_EIO); |
| 114 |
xmlFreeDoc(ctxt->myDoc); |
| 115 |
xmlFreeParserCtxt(ctxt); |
| 116 |
} |
| 117 |
} |
| 118 |
#endif |
| 119 |
|
| 120 |
exit: |
| 121 |
xmlFuzzInjectFailure(0); |
| 122 |
xmlFuzzDataCleanup(); |
| 123 |
xmlResetLastError(); |
| 124 |
return(0); |
| 125 |
} |
| 126 |
|
| 127 |
size_t |
| 128 |
LLVMFuzzerCustomMutator(char *data, size_t size, size_t maxSize, |
| 129 |
unsigned seed) { |
| 130 |
static const xmlFuzzChunkDesc chunks[] = { |
| 131 |
{ 4, XML_FUZZ_PROB_ONE / 10 }, /* opts */ |
| 132 |
{ 4, XML_FUZZ_PROB_ONE / 10 }, /* failurePos */ |
| 133 |
{ 0, 0 } |
| 134 |
}; |
| 135 |
|
| 136 |
return xmlFuzzMutateChunks(chunks, data, size, maxSize, seed, |
| 137 |
LLVMFuzzerMutate); |
| 138 |
} |
| 139 |
|