/src/libfwevt/ossfuzz/template_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libfwevt template type |
3 | | * |
4 | | * Copyright (C) 2011-2026, Joachim Metz <joachim.metz@gmail.com> |
5 | | * |
6 | | * Refer to AUTHORS for acknowledgements. |
7 | | * |
8 | | * This program is free software: you can redistribute it and/or modify |
9 | | * it under the terms of the GNU Lesser General Public License as published by |
10 | | * the Free Software Foundation, either version 3 of the License, or |
11 | | * (at your option) any later version. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public License |
19 | | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
20 | | */ |
21 | | |
22 | | #include <stddef.h> |
23 | | #include <stdint.h> |
24 | | |
25 | | #include <stdlib.h> |
26 | | |
27 | | /* Note that some of the OSS-Fuzz engines use C++ |
28 | | */ |
29 | | extern "C" { |
30 | | |
31 | | #include "ossfuzz_libfwevt.h" |
32 | | |
33 | | static const int codepages[ 3 ] = { |
34 | | 1252, |
35 | | 932, |
36 | | 65001 |
37 | | }; |
38 | | |
39 | | static const size_t max_string_size = 8 * 1024 * 1024; |
40 | | |
41 | | int LLVMFuzzerTestOneInput( |
42 | | const uint8_t *data, |
43 | | size_t size ) |
44 | 4.01k | { |
45 | 4.01k | libfwevt_template_item_t *item = NULL; |
46 | 4.01k | libfwevt_template_t *wevt_template = NULL; |
47 | 4.01k | libfwevt_xml_document_t *xml_document = NULL; |
48 | 4.01k | uint8_t *utf8_name = NULL; |
49 | 4.01k | uint8_t *utf8_string = NULL; |
50 | 4.01k | uint8_t guid_data[ 16 ] = { 0 }; |
51 | 4.01k | uint8_t input_data_type = 0; |
52 | 4.01k | size_t codepage_index = 0; |
53 | 4.01k | size_t utf8_name_size = 0; |
54 | 4.01k | size_t utf8_string_size = 0; |
55 | 4.01k | int number_of_items = 0; |
56 | | |
57 | | /* Note the template read function can only be applied once |
58 | | * therefore a separate template instance is used per codepage |
59 | | */ |
60 | 4.01k | for( codepage_index = 0; |
61 | 16.0k | codepage_index < ( sizeof( codepages ) / sizeof( codepages[ 0 ] ) ); |
62 | 12.0k | codepage_index++ ) |
63 | 12.0k | { |
64 | 12.0k | if( libfwevt_template_initialize( |
65 | 12.0k | &wevt_template, |
66 | 12.0k | NULL ) != 1 ) |
67 | 0 | { |
68 | 0 | continue; |
69 | 0 | } |
70 | 12.0k | if( libfwevt_template_set_ascii_codepage( |
71 | 12.0k | wevt_template, |
72 | 12.0k | codepages[ codepage_index ], |
73 | 12.0k | NULL ) != 1 |
74 | 12.0k | || libfwevt_template_read( |
75 | 12.0k | wevt_template, |
76 | 12.0k | data, |
77 | 12.0k | size, |
78 | 12.0k | 0, |
79 | 12.0k | NULL ) != 1 ) |
80 | 1.31k | { |
81 | 1.31k | goto on_error_wevt_template; |
82 | 1.31k | } |
83 | 10.7k | if( libfwevt_xml_document_initialize( |
84 | 10.7k | &xml_document, |
85 | 10.7k | NULL ) == 1 ) |
86 | 10.7k | { |
87 | 10.7k | if( libfwevt_template_read_xml_document( |
88 | 10.7k | wevt_template, |
89 | 10.7k | xml_document, |
90 | 10.7k | NULL ) == 1 ) |
91 | 6.00k | { |
92 | 6.00k | if( libfwevt_xml_document_get_utf8_xml_string_size( |
93 | 6.00k | xml_document, |
94 | 6.00k | &utf8_string_size, |
95 | 6.00k | NULL ) == 1 |
96 | 5.59k | && ( utf8_string_size > 0 ) |
97 | 5.59k | && ( utf8_string_size < max_string_size ) ) |
98 | 5.59k | { |
99 | 5.59k | utf8_string = (uint8_t *) malloc( utf8_string_size ); |
100 | | |
101 | 5.59k | if( utf8_string != NULL ) |
102 | 5.59k | { |
103 | 5.59k | libfwevt_xml_document_get_utf8_xml_string( |
104 | 5.59k | xml_document, |
105 | 5.59k | utf8_string, |
106 | 5.59k | utf8_string_size, |
107 | 5.59k | NULL ); |
108 | 5.59k | free( utf8_string ); |
109 | | |
110 | 5.59k | utf8_string = NULL; |
111 | 5.59k | } |
112 | 5.59k | } |
113 | 6.00k | } |
114 | 10.7k | if( libfwevt_template_get_number_of_items( |
115 | 10.7k | wevt_template, |
116 | 10.7k | &number_of_items, |
117 | 10.7k | NULL ) == 1 |
118 | 10.7k | && ( number_of_items > 0 ) ) |
119 | 1.40k | { |
120 | 1.40k | if( libfwevt_template_get_item_by_index( |
121 | 1.40k | wevt_template, |
122 | 1.40k | 0, |
123 | 1.40k | &item, |
124 | 1.40k | NULL ) == 1 ) |
125 | 1.40k | { |
126 | 1.40k | libfwevt_template_item_get_input_data_type( |
127 | 1.40k | item, |
128 | 1.40k | &input_data_type, |
129 | 1.40k | NULL ); |
130 | | |
131 | 1.40k | if( libfwevt_template_item_get_utf8_name_size( |
132 | 1.40k | item, |
133 | 1.40k | &utf8_name_size, |
134 | 1.40k | NULL ) == 1 |
135 | 243 | && ( utf8_name_size > 0 ) |
136 | 237 | && ( utf8_name_size < max_string_size ) ) |
137 | 237 | { |
138 | 237 | utf8_name = (uint8_t *) malloc( utf8_name_size ); |
139 | | |
140 | 237 | if( utf8_name != NULL ) |
141 | 237 | { |
142 | 237 | libfwevt_template_item_get_utf8_name( |
143 | 237 | item, |
144 | 237 | utf8_name, |
145 | 237 | utf8_name_size, |
146 | 237 | NULL ); |
147 | 237 | free( utf8_name ); |
148 | | |
149 | 237 | utf8_name = NULL; |
150 | 237 | } |
151 | 237 | } |
152 | 1.40k | } |
153 | 1.40k | } |
154 | 10.7k | if( libfwevt_template_get_identifier( |
155 | 10.7k | wevt_template, |
156 | 10.7k | guid_data, |
157 | 10.7k | 16, |
158 | 10.7k | NULL ) != 1 ) |
159 | 0 | { |
160 | 0 | goto on_error_xml_document; |
161 | 0 | } |
162 | 10.7k | } |
163 | 10.7k | goto on_error_xml_document; |
164 | | |
165 | 10.7k | on_error_xml_document: |
166 | 10.7k | if( xml_document != NULL ) |
167 | 10.7k | { |
168 | 10.7k | libfwevt_xml_document_free( |
169 | 10.7k | &xml_document, |
170 | 10.7k | NULL ); |
171 | 10.7k | } |
172 | 12.0k | on_error_wevt_template: |
173 | 12.0k | if( wevt_template != NULL ) |
174 | 12.0k | { |
175 | 12.0k | libfwevt_template_free( |
176 | 12.0k | &wevt_template, |
177 | 12.0k | NULL ); |
178 | 12.0k | } |
179 | 12.0k | } |
180 | 4.01k | return( 0 ); |
181 | 4.01k | } |
182 | | |
183 | | } /* extern "C" */ |