xref: /freebsd/contrib/expat/fuzz/xml_parsebuffer_fuzzer.c (revision 2449fa9c4d8e60cca863396dc3c7d67cbc16359f)
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <assert.h>
18 #include <limits.h> // for INT_MAX
19 #include <stdint.h>
20 #include <string.h>
21 
22 #include "expat.h"
23 #include "siphash.h"
24 
25 // Macros to convert preprocessor macros to string literals. See
26 // https://gcc.gnu.org/onlinedocs/gcc-3.4.3/cpp/Stringification.html
27 #define xstr(s) str(s)
28 #define str(s) #s
29 
30 // The encoder type that we wish to fuzz should come from the compile-time
31 // definition `ENCODING_FOR_FUZZING`. This allows us to have a separate fuzzer
32 // binary for
33 #ifndef ENCODING_FOR_FUZZING
34 #  error "ENCODING_FOR_FUZZING was not provided to this fuzz target."
35 #endif
36 
37 // 16-byte deterministic hash key.
38 static unsigned char hash_key_1[16] = "FUZZING IS FUN?";
39 static unsigned char hash_key_2[16] = "FUZZING IS FUN!";
40 
41 static void XMLCALL
42 start(void *userData, const XML_Char *name, const XML_Char **atts) {
43   (void)userData;
44   (void)name;
45   (void)atts;
46 }
47 static void XMLCALL
48 end(void *userData, const XML_Char *name) {
49   (void)userData;
50   (void)name;
51 }
52 
53 static void XMLCALL
54 may_stop_character_handler(void *userData, const XML_Char *s, int len) {
55   XML_Parser parser = (XML_Parser)userData;
56   if (len > 1 && s[0] == 's') {
57     XML_StopParser(parser, s[1] == 'r' ? XML_FALSE : XML_TRUE);
58   }
59 }
60 
61 static void
62 ParseOneInput(XML_Parser p, const uint8_t *data, size_t size) {
63   // Set the hash salt using siphash to generate a deterministic hash.
64   // The salt is 16 bytes and siphash24 produces 8, so the input is hashed
65   // under two keys.
66   const uint64_t first = siphash24(data, size, sip_keyof(hash_key_1));
67   const uint64_t second = siphash24(data, size, sip_keyof(hash_key_2));
68   uint8_t entropy[16];
69   memcpy(entropy, &first, sizeof(first));
70   memcpy(entropy + sizeof(first), &second, sizeof(second));
71   XML_SetHashSalt16Bytes(p, entropy);
72   (void)sip24_valid;
73 
74   XML_SetUserData(p, p);
75   XML_SetElementHandler(p, start, end);
76   XML_SetCharacterDataHandler(p, may_stop_character_handler);
77   assert(size <= INT_MAX);
78   void *buf = XML_GetBuffer(p, (int)size);
79   assert(buf);
80   memcpy(buf, data, size);
81   XML_ParseBuffer(p, (int)size, 0);
82   buf = XML_GetBuffer(p, (int)size);
83   if (buf == NULL) {
84     return;
85   }
86   memcpy(buf, data, size);
87   if (XML_ParseBuffer(p, (int)size, 1) == XML_STATUS_ERROR) {
88     XML_ErrorString(XML_GetErrorCode(p));
89   }
90   XML_GetCurrentLineNumber(p);
91   if (size % 2) {
92     XML_ParserReset(p, NULL);
93   }
94 }
95 
96 int
97 LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
98   if (size == 0)
99     return 0;
100 
101   XML_Parser parentParser = XML_ParserCreate(xstr(ENCODING_FOR_FUZZING));
102   assert(parentParser);
103   ParseOneInput(parentParser, data, size);
104   // not freed yet, but used later and freed then
105 
106   XML_Parser namespaceParser = XML_ParserCreateNS(NULL, '!');
107   assert(namespaceParser);
108   ParseOneInput(namespaceParser, data, size);
109   XML_ParserFree(namespaceParser);
110 
111   XML_Parser externalEntityParser
112       = XML_ExternalEntityParserCreate(parentParser, "e1", NULL);
113   if (externalEntityParser != NULL) {
114     ParseOneInput(externalEntityParser, data, size);
115     XML_ParserFree(externalEntityParser);
116   }
117 
118   XML_Parser externalDtdParser
119       = XML_ExternalEntityParserCreate(parentParser, NULL, NULL);
120   if (externalDtdParser != NULL) {
121     ParseOneInput(externalDtdParser, data, size);
122     XML_ParserFree(externalDtdParser);
123   }
124 
125   // finally frees this parser which served as parent
126   XML_ParserFree(parentParser);
127   return 0;
128 }
129