xref: /freebsd/contrib/expat/fuzz/xml_parse_fuzzer.c (revision 2c41f7d8a034ee64a0aaa69a1aa5cb07748a1a32)
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   XML_Parse(p, (const XML_Char *)data, (int)size, 0);
79   if (XML_Parse(p, (const XML_Char *)data, (int)size, 1) == XML_STATUS_ERROR) {
80     XML_ErrorString(XML_GetErrorCode(p));
81   }
82   XML_GetCurrentLineNumber(p);
83   if (size % 2) {
84     XML_ParserReset(p, NULL);
85   }
86 }
87 
88 int
89 LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
90   XML_Parser parentParser = XML_ParserCreate(xstr(ENCODING_FOR_FUZZING));
91   assert(parentParser);
92   ParseOneInput(parentParser, data, size);
93   // not freed yet, but used later and freed then
94 
95   XML_Parser namespaceParser = XML_ParserCreateNS(NULL, '!');
96   assert(namespaceParser);
97   ParseOneInput(namespaceParser, data, size);
98   XML_ParserFree(namespaceParser);
99 
100   XML_Parser externalEntityParser
101       = XML_ExternalEntityParserCreate(parentParser, "e1", NULL);
102   if (externalEntityParser != NULL) {
103     ParseOneInput(externalEntityParser, data, size);
104     XML_ParserFree(externalEntityParser);
105   }
106 
107   XML_Parser externalDtdParser
108       = XML_ExternalEntityParserCreate(parentParser, NULL, NULL);
109   if (externalDtdParser != NULL) {
110     ParseOneInput(externalDtdParser, data, size);
111     XML_ParserFree(externalDtdParser);
112   }
113 
114   // finally frees this parser which served as parent
115   XML_ParserFree(parentParser);
116   return 0;
117 }
118