/src/xmlsec/apps/oss-fuzz/xmlsec_target.c
Line | Count | Source |
1 | | #include <stdint.h> |
2 | | #include <stddef.h> |
3 | | #include <limits.h> |
4 | | |
5 | | #include <libxml/parser.h> |
6 | | #include <libxml/xmlerror.h> |
7 | | |
8 | | #include <xmlsec/buffer.h> |
9 | | #include <xmlsec/parser.h> |
10 | | #include <xmlsec/xmlsec.h> |
11 | | |
12 | | int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size); |
13 | | |
14 | 2.69M | static void ignore(void* ctx, const char* msg, ...) { |
15 | | /* Error handler to avoid error message spam from the libxml parser. */ |
16 | 2.69M | (void)ctx; |
17 | 2.69M | (void)msg; |
18 | 2.69M | } |
19 | | |
20 | | static int g_initialized = 0; |
21 | | /* Set when do_init() fails, so a failed one-time init is not retried on |
22 | | * every input. */ |
23 | | static int g_init_failed = 0; |
24 | | |
25 | 1 | static int do_init(void) { |
26 | 1 | int options; |
27 | | |
28 | 1 | xmlInitParser(); |
29 | | |
30 | 1 | if (xmlSecInit() < 0) { |
31 | 0 | return -1; |
32 | 0 | } |
33 | 1 | if (xmlSecCheckVersion() != 1) { |
34 | 0 | xmlSecShutdown(); |
35 | 0 | return -1; |
36 | 0 | } |
37 | | |
38 | | /* Silence libxml2 error spam. */ |
39 | 1 | xmlSetGenericErrorFunc(NULL, &ignore); |
40 | | |
41 | | /* xmlSecParseMemory() parses with the process-wide default options, which |
42 | | * include XML_PARSE_HUGE (see src/parser.c). Fuzz inputs are small, so the |
43 | | * relaxation is unnecessary; on libxml2 < 2.13.0 it disables the parser |
44 | | * depth limit, risking stack exhaustion on deeply nested fuzz input. */ |
45 | 1 | options = xmlSecParserGetDefaultOptions(); |
46 | 1 | options &= ~XML_PARSE_HUGE; |
47 | 1 | options |= XML_PARSE_PEDANTIC | XML_PARSE_NONET; |
48 | 1 | xmlSecParserSetDefaultOptions(options); |
49 | | |
50 | | /* done */ |
51 | 1 | return 0; |
52 | 1 | } |
53 | | |
54 | 21.5k | int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
55 | 21.5k | xmlSecBufferPtr buf; |
56 | 21.5k | xmlDocPtr doc; |
57 | | |
58 | 21.5k | if (!g_initialized) { |
59 | 1 | g_init_failed = (do_init() < 0); |
60 | 1 | g_initialized = 1; |
61 | 1 | } |
62 | | /* A zero-size buffer never allocates data, so xmlSecBufferGetData() would |
63 | | * return NULL; skip empty inputs like the sibling targets do. Also skip |
64 | | * inputs too large to fit in the int length expected by |
65 | | * xmlCreateMemoryParserCtxt(), which xmlSecParseMemory() uses. */ |
66 | 21.5k | if (g_init_failed || size == 0 || size > (size_t)INT_MAX) { |
67 | 0 | return 0; |
68 | 0 | } |
69 | 21.5k | buf = xmlSecBufferCreate(size); |
70 | 21.5k | if (buf == NULL) { |
71 | 0 | return 0; |
72 | 0 | } |
73 | 21.5k | if (xmlSecBufferSetData(buf, data, size) < 0) { |
74 | 0 | xmlSecBufferDestroy(buf); |
75 | 0 | return 0; |
76 | 0 | } |
77 | 21.5k | doc = xmlSecParseMemory(xmlSecBufferGetData(buf), |
78 | 21.5k | xmlSecBufferGetSize(buf), 0); |
79 | | |
80 | 21.5k | if (doc != NULL) { |
81 | 87 | xmlFreeDoc(doc); |
82 | 87 | } |
83 | 21.5k | xmlSecBufferDestroy(buf); |
84 | 21.5k | return 0; |
85 | 21.5k | } |