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 * http://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 <stdint.h>
19
20 #include "expat.h"
21 #include "siphash.h"
22
23 // Macros to convert preprocessor macros to string literals. See
24 // https://gcc.gnu.org/onlinedocs/gcc-3.4.3/cpp/Stringification.html
25 #define xstr(s) str(s)
26 #define str(s) #s
27
28 // The encoder type that we wish to fuzz should come from the compile-time
29 // definition `ENCODING_FOR_FUZZING`. This allows us to have a separate fuzzer
30 // binary for
31 #ifndef ENCODING_FOR_FUZZING
32 # error "ENCODING_FOR_FUZZING was not provided to this fuzz target."
33 #endif
34
35 // 16-byte deterministic hash key.
36 static unsigned char hash_key[16] = "FUZZING IS FUN!";
37
38 static void XMLCALL
start(void * userData,const XML_Char * name,const XML_Char ** atts)39 start(void *userData, const XML_Char *name, const XML_Char **atts) {
40 (void)userData;
41 (void)name;
42 (void)atts;
43 }
44 static void XMLCALL
end(void * userData,const XML_Char * name)45 end(void *userData, const XML_Char *name) {
46 (void)userData;
47 (void)name;
48 }
49
50 static void XMLCALL
may_stop_character_handler(void * userData,const XML_Char * s,int len)51 may_stop_character_handler(void *userData, const XML_Char *s, int len) {
52 XML_Parser parser = (XML_Parser)userData;
53 if (len > 1 && s[0] == 's') {
54 XML_StopParser(parser, s[1] == 'r' ? XML_FALSE : XML_TRUE);
55 }
56 }
57
58 static void
ParseOneInput(XML_Parser p,const uint8_t * data,size_t size)59 ParseOneInput(XML_Parser p, const uint8_t *data, size_t size) {
60 // Set the hash salt using siphash to generate a deterministic hash.
61 struct sipkey *key = sip_keyof(hash_key);
62 XML_SetHashSalt(p, (unsigned long)siphash24(data, size, key));
63 (void)sip24_valid;
64
65 XML_SetUserData(p, p);
66 XML_SetElementHandler(p, start, end);
67 XML_SetCharacterDataHandler(p, may_stop_character_handler);
68 XML_Parse(p, (const XML_Char *)data, size, 0);
69 if (XML_Parse(p, (const XML_Char *)data, size, 1) == XML_STATUS_ERROR) {
70 XML_ErrorString(XML_GetErrorCode(p));
71 }
72 XML_GetCurrentLineNumber(p);
73 if (size % 2) {
74 XML_ParserReset(p, NULL);
75 }
76 }
77
78 int
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)79 LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
80 XML_Parser parentParser = XML_ParserCreate(xstr(ENCODING_FOR_FUZZING));
81 assert(parentParser);
82 ParseOneInput(parentParser, data, size);
83 // not freed yet, but used later and freed then
84
85 XML_Parser namespaceParser = XML_ParserCreateNS(NULL, '!');
86 assert(namespaceParser);
87 ParseOneInput(namespaceParser, data, size);
88 XML_ParserFree(namespaceParser);
89
90 XML_Parser externalEntityParser
91 = XML_ExternalEntityParserCreate(parentParser, "e1", NULL);
92 assert(externalEntityParser);
93 ParseOneInput(externalEntityParser, data, size);
94 XML_ParserFree(externalEntityParser);
95
96 XML_Parser externalDtdParser
97 = XML_ExternalEntityParserCreate(parentParser, NULL, NULL);
98 assert(externalDtdParser);
99 ParseOneInput(externalDtdParser, data, size);
100 XML_ParserFree(externalDtdParser);
101
102 // finally frees this parser which served as parent
103 XML_ParserFree(parentParser);
104 return 0;
105 }
106