Debian Codegraph @

fuzz/lint.c

raw
1 /*
2 * lint.c: a libFuzzer target to test the xmllint executable.
3 *
4 * See Copyright for the status of this software.
5 */
6
7 #include <fcntl.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <unistd.h>
11
12 #include <libxml/catalog.h>
13 #include <libxml/parser.h>
14 #include <libxml/xmlerror.h>
15 #include <libxml/xmlmemory.h>
16
17 #include "private/lint.h"
18
19 #include "fuzz.h"
20
21 /*
22 * Untested options:
23 *
24 * --memory: Requires temp file
25 *
26 * --catalogs: Requires XML catalogs
27 *
28 * --dtdvalid:
29 * --dtdvalidfpi: Requires an external DTD
30 *
31 * --output: Writes to disk
32 *
33 * --path: Requires cooperation with resource loader
34 *
35 * --relaxng:
36 * --schema:
37 * --schematron: Requires schemas
38 *
39 * --shell: We could pipe fuzz data to stdin but this is probably
40 * not worth it.
41 */
42
43 static const char *const switches[] = {
44 "--auto",
45 "--c14n",
46 "--c14n11",
47 "--compress",
48 "--copy",
49 "--debug",
50 NULL,
51 "--dropdtd",
52 "--dtdattr",
53 "--exc-c14n",
54 "--format",
55 NULL,
56 "--huge",
57 "--insert",
58 "--loaddtd",
59 "--load-trace",
60 NULL,
61 "--noblanks",
62 "--nocdata",
63 "--nocompact",
64 "--nodefdtd",
65 "--nodict",
66 "--noenc",
67 "--noent",
68 "--nofixup-base-uris",
69 "--nonet",
70 "--noout",
71 "--nowarning",
72 NULL,
73 "--noxincludenode",
74 "--nsclean",
75 "--oldxml10",
76 "--pedantic",
77 "--postvalid",
78 "--push",
79 "--pushsmall",
80 "--quiet",
81 "--recover",
82 "--repeat",
83 "--sax1",
84 NULL,
85 "--timing",
86 "--valid",
87 "--version",
88 "--walker",
89 "--xinclude",
90 "--xmlout"
91 };
92 static const size_t numSwitches = sizeof(switches) / sizeof(switches[0]);
93
94 struct {
95 const char **argv;
96 size_t argi;
97 } vars;
98
99 static void
100 pushArg(const char *str) {
101 vars.argv[vars.argi++] = str;
102 }
103
104 int
105 LLVMFuzzerInitialize(int *argc ATTRIBUTE_UNUSED,
106 char ***argv ATTRIBUTE_UNUSED) {
107 int fd;
108
109 /* Redirect stdout to /dev/null */
110 fd = open("/dev/null", O_WRONLY);
111 if (fd == -1) {
112 perror("/dev/null");
113 abort();
114 }
115 if (dup2(fd, STDOUT_FILENO) == -1) {
116 perror("dup2");
117 abort();
118 }
119 close(fd);
120
121 return 0;
122 }
123
124 int
125 LLVMFuzzerTestOneInput(const char *data, size_t size) {
126 char maxmemBuf[20];
127 char maxAmplBuf[20];
128 char prettyBuf[20];
129 const char *sval, *docBuffer, *docUrl;
130 size_t ssize, docSize, i;
131 unsigned uval;
132 int ival;
133
134 if (xmlMemUsed() != 0) {
135 fprintf(stderr, "Undetected leak in previous iteration\n");
136 abort();
137 }
138
139 vars.argv = malloc((numSwitches + 5 + 6 * 2) * sizeof(vars.argv[0]));
140 vars.argi = 0;
141 pushArg("xmllint"),
142 pushArg("--nocatalogs");
143
144 xmlFuzzDataInit(data, size);
145
146 for (i = 0; i < numSwitches; i++) {
147 if (i % 32 == 0)
148 uval = xmlFuzzReadInt(4);
149 if ((uval & 1) && (switches[i] != NULL))
150 pushArg(switches[i]);
151 uval >>= 1;
152 }
153
154 /*
155 * Use four main parsing modes with equal probability
156 */
157 switch (uval & 3) {
158 case 0:
159 /* XML parser */
160 break;
161 case 1:
162 /* HTML parser */
163 pushArg("--html");
164 break;
165 case 2:
166 /* XML reader */
167 pushArg("--stream");
168 break;
169 case 3:
170 /* SAX parser */
171 pushArg("--sax");
172 break;
173 }
174
175 uval = xmlFuzzReadInt(4);
176 if (uval > 0) {
177 if (size <= (INT_MAX - 2000) / 20)
178 uval %= size * 20 + 2000;
179 else
180 uval %= INT_MAX;
181 snprintf(maxmemBuf, 20, "%u", uval);
182 pushArg("--maxmem");
183 pushArg(maxmemBuf);
184 }
185
186 ival = xmlFuzzReadInt(1);
187 if (ival >= 1 && ival <= 5) {
188 snprintf(maxAmplBuf, 20, "%d", ival);
189 pushArg("--max-ampl");
190 pushArg(maxAmplBuf);
191 }
192
193 ival = xmlFuzzReadInt(1);
194 if (ival != 0) {
195 snprintf(prettyBuf, 20, "%d", ival % 4);
196 pushArg("--pretty");
197 pushArg(prettyBuf);
198 }
199
200 sval = xmlFuzzReadString(&ssize);
201 if (ssize > 0) {
202 pushArg("--encode");
203 pushArg(sval);
204 }
205
206 sval = xmlFuzzReadString(&ssize);
207 if (ssize > 0) {
208 pushArg("--pattern");
209 pushArg(sval);
210 }
211
212 sval = xmlFuzzReadString(&ssize);
213 if (ssize > 0) {
214 pushArg("--xpath");
215 pushArg(sval);
216 }
217
218 xmlFuzzReadEntities();
219 docBuffer = xmlFuzzMainEntity(&docSize);
220 docUrl = xmlFuzzMainUrl();
221 if (docBuffer == NULL || docUrl[0] == '-')
222 goto exit;
223 pushArg(docUrl);
224
225 pushArg(NULL);
226
227 xmlSetGenericErrorFunc(NULL, xmlFuzzErrorFunc);
228 #ifdef LIBXML_CATALOG_ENABLED
229 xmlCatalogSetDefaults(XML_CATA_ALLOW_NONE);
230 #endif
231
232 xmllintMain(vars.argi - 1, vars.argv, stdout, xmlFuzzResourceLoader);
233
234 xmlMemSetup(free, malloc, realloc, xmlMemStrdup);
235
236 exit:
237 xmlFuzzDataCleanup();
238 free(vars.argv);
239 return(0);
240 }
241
242 size_t
243 LLVMFuzzerCustomMutator(char *data, size_t size, size_t maxSize,
244 unsigned seed) {
245 static const xmlFuzzChunkDesc chunks[] = {
246 { 8, XML_FUZZ_PROB_ONE / 10 }, /* switches */
247 { 4, XML_FUZZ_PROB_ONE / 10 }, /* maxmem */
248 { 1, XML_FUZZ_PROB_ONE / 100 }, /* maxAmpl */
249 { 1, XML_FUZZ_PROB_ONE / 100 }, /* pretty */
250 { 0, 0 }
251 };
252
253 return xmlFuzzMutateChunks(chunks, data, size, maxSize, seed,
254 LLVMFuzzerMutate);
255 }
256