/src/libfwsi/ossfuzz/item_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libfwsi item type |
3 | | * |
4 | | * Copyright (C) 2010-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 | | #include <stdlib.h> |
25 | | |
26 | | extern "C" { |
27 | | |
28 | | #include "ossfuzz_libfwsi.h" |
29 | | |
30 | | } /* extern "C" */ |
31 | | |
32 | | /* Retrieves a codepage based on the input |
33 | | * The input is mapped onto one of the supported codepages such that |
34 | | * the fuzz target exercises the codepage specific data conversion path. |
35 | | * The last entry is an unsupported codepage to exercise the validation path |
36 | | */ |
37 | | static int ossfuzz_libfwsi_get_codepage( |
38 | | const uint8_t *data, |
39 | | size_t size ) |
40 | 3.46k | { |
41 | 3.46k | static const int codepages[ 16 ] = { |
42 | 3.46k | LIBFWSI_CODEPAGE_US_ASCII, |
43 | 3.46k | LIBFWSI_CODEPAGE_WINDOWS_874, |
44 | 3.46k | LIBFWSI_CODEPAGE_WINDOWS_932, |
45 | 3.46k | LIBFWSI_CODEPAGE_WINDOWS_936, |
46 | 3.46k | LIBFWSI_CODEPAGE_WINDOWS_949, |
47 | 3.46k | LIBFWSI_CODEPAGE_WINDOWS_950, |
48 | 3.46k | LIBFWSI_CODEPAGE_WINDOWS_1250, |
49 | 3.46k | LIBFWSI_CODEPAGE_WINDOWS_1251, |
50 | 3.46k | LIBFWSI_CODEPAGE_WINDOWS_1252, |
51 | 3.46k | LIBFWSI_CODEPAGE_WINDOWS_1253, |
52 | 3.46k | LIBFWSI_CODEPAGE_WINDOWS_1254, |
53 | 3.46k | LIBFWSI_CODEPAGE_WINDOWS_1255, |
54 | 3.46k | LIBFWSI_CODEPAGE_WINDOWS_1256, |
55 | 3.46k | LIBFWSI_CODEPAGE_WINDOWS_1257, |
56 | 3.46k | LIBFWSI_CODEPAGE_WINDOWS_1258, |
57 | 3.46k | LIBFWSI_CODEPAGE_ISO_LATIN_1 |
58 | 3.46k | }; |
59 | | |
60 | 3.46k | if( size == 0 ) |
61 | 0 | { |
62 | 0 | return( LIBFWSI_CODEPAGE_US_ASCII ); |
63 | 0 | } |
64 | 3.46k | return( codepages[ data[ 0 ] % 16 ] ); |
65 | 3.46k | } |
66 | | |
67 | | typedef int (ossfuzz_libfwsi_utf8_size_getter_t)( |
68 | | libfwsi_item_t *item, |
69 | | size_t *utf8_string_size, |
70 | | libfwsi_error_t **error ); |
71 | | |
72 | | typedef int (ossfuzz_libfwsi_utf8_name_getter_t)( |
73 | | libfwsi_item_t *item, |
74 | | uint8_t *utf8_string, |
75 | | size_t utf8_string_size, |
76 | | libfwsi_error_t **error ); |
77 | | |
78 | | typedef int (ossfuzz_libfwsi_utf16_size_getter_t)( |
79 | | libfwsi_item_t *item, |
80 | | size_t *utf16_string_size, |
81 | | libfwsi_error_t **error ); |
82 | | |
83 | | typedef int (ossfuzz_libfwsi_utf16_name_getter_t)( |
84 | | libfwsi_item_t *item, |
85 | | uint16_t *utf16_string, |
86 | | size_t utf16_string_size, |
87 | | libfwsi_error_t **error ); |
88 | | |
89 | | /* Calls the UTF-8 name or location getters |
90 | | */ |
91 | | static void ossfuzz_libfwsi_call_utf8_string_getters( |
92 | | libfwsi_item_t *item, |
93 | | ossfuzz_libfwsi_utf8_size_getter_t utf8_size_getter, |
94 | | ossfuzz_libfwsi_utf8_name_getter_t utf8_name_getter ) |
95 | 19.3k | { |
96 | 19.3k | uint8_t *utf8_string = NULL; |
97 | 19.3k | size_t utf8_size = 0; |
98 | | |
99 | 19.3k | if( utf8_size_getter( |
100 | 19.3k | item, |
101 | 19.3k | &utf8_size, |
102 | 19.3k | NULL ) == 1 ) |
103 | 4.24k | { |
104 | 4.24k | utf8_string = (uint8_t *) malloc( |
105 | 4.24k | utf8_size ); |
106 | | |
107 | 4.24k | if( utf8_string != NULL ) |
108 | 4.24k | { |
109 | 4.24k | utf8_name_getter( |
110 | 4.24k | item, |
111 | 4.24k | utf8_string, |
112 | 4.24k | utf8_size, |
113 | 4.24k | NULL ); |
114 | | |
115 | 4.24k | free( |
116 | 4.24k | utf8_string ); |
117 | | |
118 | 4.24k | utf8_string = NULL; |
119 | 4.24k | } |
120 | 4.24k | } |
121 | 19.3k | } |
122 | | |
123 | | /* Calls the UTF-16 name or location getters |
124 | | */ |
125 | | static void ossfuzz_libfwsi_call_utf16_string_getters( |
126 | | libfwsi_item_t *item, |
127 | | ossfuzz_libfwsi_utf16_size_getter_t utf16_size_getter, |
128 | | ossfuzz_libfwsi_utf16_name_getter_t utf16_name_getter ) |
129 | 19.3k | { |
130 | 19.3k | uint16_t *utf16_string = NULL; |
131 | 19.3k | size_t utf16_size = 0; |
132 | | |
133 | 19.3k | if( utf16_size_getter( |
134 | 19.3k | item, |
135 | 19.3k | &utf16_size, |
136 | 19.3k | NULL ) == 1 ) |
137 | 4.24k | { |
138 | 4.24k | utf16_string = (uint16_t *) malloc( |
139 | 4.24k | utf16_size * sizeof( uint16_t ) ); |
140 | | |
141 | 4.24k | if( utf16_string != NULL ) |
142 | 4.24k | { |
143 | 4.24k | utf16_name_getter( |
144 | 4.24k | item, |
145 | 4.24k | utf16_string, |
146 | 4.24k | utf16_size, |
147 | 4.24k | NULL ); |
148 | | |
149 | 4.24k | free( |
150 | 4.24k | utf16_string ); |
151 | | |
152 | 4.24k | utf16_string = NULL; |
153 | 4.24k | } |
154 | 4.24k | } |
155 | 19.3k | } |
156 | | |
157 | | /* Retrieves the values of a root folder item |
158 | | */ |
159 | | static void ossfuzz_libfwsi_walk_root_folder_item( |
160 | | libfwsi_item_t *root_folder ) |
161 | 3.22k | { |
162 | 3.22k | uint8_t shell_folder_identifier[ 16 ] = { 0 }; |
163 | | |
164 | 3.22k | libfwsi_root_folder_get_shell_folder_identifier( |
165 | 3.22k | root_folder, |
166 | 3.22k | shell_folder_identifier, |
167 | 3.22k | sizeof( shell_folder_identifier ), |
168 | 3.22k | NULL ); |
169 | 3.22k | } |
170 | | |
171 | | /* Retrieves the values of a volume item |
172 | | */ |
173 | | static void ossfuzz_libfwsi_walk_volume_item( |
174 | | libfwsi_item_t *volume ) |
175 | 3.22k | { |
176 | 3.22k | uint8_t identifier[ 16 ] = { 0 }; |
177 | | |
178 | 3.22k | ossfuzz_libfwsi_call_utf8_string_getters( |
179 | 3.22k | volume, |
180 | 3.22k | &libfwsi_volume_get_utf8_name_size, |
181 | 3.22k | &libfwsi_volume_get_utf8_name ); |
182 | | |
183 | 3.22k | ossfuzz_libfwsi_call_utf16_string_getters( |
184 | 3.22k | volume, |
185 | 3.22k | &libfwsi_volume_get_utf16_name_size, |
186 | 3.22k | &libfwsi_volume_get_utf16_name ); |
187 | | |
188 | 3.22k | libfwsi_volume_get_identifier( |
189 | 3.22k | volume, |
190 | 3.22k | identifier, |
191 | 3.22k | sizeof( identifier ), |
192 | 3.22k | NULL ); |
193 | | |
194 | 3.22k | libfwsi_volume_get_shell_folder_identifier( |
195 | 3.22k | volume, |
196 | 3.22k | identifier, |
197 | 3.22k | sizeof( identifier ), |
198 | 3.22k | NULL ); |
199 | 3.22k | } |
200 | | |
201 | | /* Retrieves the values of a file entry item |
202 | | */ |
203 | | static void ossfuzz_libfwsi_walk_file_entry_item( |
204 | | libfwsi_item_t *file_entry ) |
205 | 3.22k | { |
206 | 3.22k | uint32_t file_size = 0; |
207 | 3.22k | uint32_t fat_date_time = 0; |
208 | 3.22k | uint32_t file_attribute_flags = 0; |
209 | | |
210 | 3.22k | libfwsi_file_entry_get_file_size( |
211 | 3.22k | file_entry, |
212 | 3.22k | &file_size, |
213 | 3.22k | NULL ); |
214 | | |
215 | 3.22k | libfwsi_file_entry_get_modification_time( |
216 | 3.22k | file_entry, |
217 | 3.22k | &fat_date_time, |
218 | 3.22k | NULL ); |
219 | | |
220 | 3.22k | libfwsi_file_entry_get_file_attribute_flags( |
221 | 3.22k | file_entry, |
222 | 3.22k | &file_attribute_flags, |
223 | 3.22k | NULL ); |
224 | | |
225 | 3.22k | ossfuzz_libfwsi_call_utf8_string_getters( |
226 | 3.22k | file_entry, |
227 | 3.22k | &libfwsi_file_entry_get_utf8_name_size, |
228 | 3.22k | &libfwsi_file_entry_get_utf8_name ); |
229 | | |
230 | 3.22k | ossfuzz_libfwsi_call_utf16_string_getters( |
231 | 3.22k | file_entry, |
232 | 3.22k | &libfwsi_file_entry_get_utf16_name_size, |
233 | 3.22k | &libfwsi_file_entry_get_utf16_name ); |
234 | 3.22k | } |
235 | | |
236 | | /* Retrieves the values of a network location item |
237 | | */ |
238 | | static void ossfuzz_libfwsi_walk_network_location_item( |
239 | | libfwsi_item_t *network_location ) |
240 | 3.22k | { |
241 | 3.22k | ossfuzz_libfwsi_call_utf8_string_getters( |
242 | 3.22k | network_location, |
243 | 3.22k | &libfwsi_network_location_get_utf8_location_size, |
244 | 3.22k | &libfwsi_network_location_get_utf8_location ); |
245 | | |
246 | 3.22k | ossfuzz_libfwsi_call_utf16_string_getters( |
247 | 3.22k | network_location, |
248 | 3.22k | &libfwsi_network_location_get_utf16_location_size, |
249 | 3.22k | &libfwsi_network_location_get_utf16_location ); |
250 | | |
251 | 3.22k | ossfuzz_libfwsi_call_utf8_string_getters( |
252 | 3.22k | network_location, |
253 | 3.22k | &libfwsi_network_location_get_utf8_description_size, |
254 | 3.22k | &libfwsi_network_location_get_utf8_description ); |
255 | | |
256 | 3.22k | ossfuzz_libfwsi_call_utf16_string_getters( |
257 | 3.22k | network_location, |
258 | 3.22k | &libfwsi_network_location_get_utf16_description_size, |
259 | 3.22k | &libfwsi_network_location_get_utf16_description ); |
260 | | |
261 | 3.22k | ossfuzz_libfwsi_call_utf8_string_getters( |
262 | 3.22k | network_location, |
263 | 3.22k | &libfwsi_network_location_get_utf8_comments_size, |
264 | 3.22k | &libfwsi_network_location_get_utf8_comments ); |
265 | | |
266 | 3.22k | ossfuzz_libfwsi_call_utf16_string_getters( |
267 | 3.22k | network_location, |
268 | 3.22k | &libfwsi_network_location_get_utf16_comments_size, |
269 | 3.22k | &libfwsi_network_location_get_utf16_comments ); |
270 | 3.22k | } |
271 | | |
272 | | /* Retrieves the values of a compressed folder item |
273 | | */ |
274 | | static void ossfuzz_libfwsi_walk_compressed_folder_item( |
275 | | libfwsi_item_t *compressed_folder ) |
276 | 3.22k | { |
277 | 3.22k | ossfuzz_libfwsi_call_utf8_string_getters( |
278 | 3.22k | compressed_folder, |
279 | 3.22k | &libfwsi_compressed_folder_get_utf8_name_size, |
280 | 3.22k | &libfwsi_compressed_folder_get_utf8_name ); |
281 | | |
282 | 3.22k | ossfuzz_libfwsi_call_utf16_string_getters( |
283 | 3.22k | compressed_folder, |
284 | 3.22k | &libfwsi_compressed_folder_get_utf16_name_size, |
285 | 3.22k | &libfwsi_compressed_folder_get_utf16_name ); |
286 | 3.22k | } |
287 | | |
288 | | /* Retrieves the values of a control panel item |
289 | | */ |
290 | | static void ossfuzz_libfwsi_walk_control_panel_item_item( |
291 | | libfwsi_item_t *control_panel_item ) |
292 | 3.22k | { |
293 | 3.22k | uint8_t identifier[ 16 ] = { 0 }; |
294 | | |
295 | 3.22k | libfwsi_control_panel_item_get_identifier( |
296 | 3.22k | control_panel_item, |
297 | 3.22k | identifier, |
298 | 3.22k | sizeof( identifier ), |
299 | 3.22k | NULL ); |
300 | 3.22k | } |
301 | | |
302 | | /* Retrieves the values of a control panel category item |
303 | | */ |
304 | | static void ossfuzz_libfwsi_walk_control_panel_category_item( |
305 | | libfwsi_item_t *control_panel_category ) |
306 | 3.22k | { |
307 | 3.22k | uint32_t identifier = 0; |
308 | | |
309 | 3.22k | libfwsi_control_panel_category_get_identifier( |
310 | 3.22k | control_panel_category, |
311 | 3.22k | &identifier, |
312 | 3.22k | NULL ); |
313 | 3.22k | } |
314 | | |
315 | | /* Retrieves the values of a users property view item |
316 | | */ |
317 | | static void ossfuzz_libfwsi_walk_users_property_view_item( |
318 | | libfwsi_item_t *users_property_view ) |
319 | 3.22k | { |
320 | 3.22k | uint8_t *property_store_data = NULL; |
321 | 3.22k | uint8_t identifier[ 16 ] = { 0 }; |
322 | 3.22k | size_t property_store_data_size = 0; |
323 | | |
324 | 3.22k | libfwsi_users_property_view_get_known_folder_identifier( |
325 | 3.22k | users_property_view, |
326 | 3.22k | identifier, |
327 | 3.22k | sizeof( identifier ), |
328 | 3.22k | NULL ); |
329 | | |
330 | 3.22k | libfwsi_users_property_view_get_property_store_data_size( |
331 | 3.22k | users_property_view, |
332 | 3.22k | &property_store_data_size, |
333 | 3.22k | NULL ); |
334 | | |
335 | 3.22k | if( property_store_data_size > 0 ) |
336 | 5 | { |
337 | 5 | property_store_data = (uint8_t *) malloc( |
338 | 5 | property_store_data_size ); |
339 | | |
340 | 5 | if( property_store_data != NULL ) |
341 | 5 | { |
342 | 5 | libfwsi_users_property_view_copy_property_store_data( |
343 | 5 | users_property_view, |
344 | 5 | property_store_data, |
345 | 5 | property_store_data_size, |
346 | 5 | NULL ); |
347 | | |
348 | 5 | free( |
349 | 5 | property_store_data ); |
350 | | |
351 | 5 | property_store_data = NULL; |
352 | 5 | } |
353 | 5 | } |
354 | 3.22k | } |
355 | | |
356 | | /* Retrieves the values of an item |
357 | | */ |
358 | | static void ossfuzz_libfwsi_walk_item( |
359 | | libfwsi_item_t *item ) |
360 | 3.22k | { |
361 | 3.22k | libfwsi_extension_block_t *extension_block = NULL; |
362 | 3.22k | uint8_t identifier[ 16 ] = { 0 }; |
363 | 3.22k | uint8_t class_type = 0; |
364 | 3.22k | uint32_t signature = 0; |
365 | 3.22k | size_t data_size = 0; |
366 | 3.22k | int type = 0; |
367 | 3.22k | int number_of_extension_blocks = 0; |
368 | 3.22k | int extension_block_index = 0; |
369 | | |
370 | 3.22k | libfwsi_item_get_type( |
371 | 3.22k | item, |
372 | 3.22k | &type, |
373 | 3.22k | NULL ); |
374 | | |
375 | 3.22k | libfwsi_item_get_class_type( |
376 | 3.22k | item, |
377 | 3.22k | &class_type, |
378 | 3.22k | NULL ); |
379 | | |
380 | 3.22k | libfwsi_item_get_signature( |
381 | 3.22k | item, |
382 | 3.22k | &signature, |
383 | 3.22k | NULL ); |
384 | | |
385 | 3.22k | libfwsi_item_get_data_size( |
386 | 3.22k | item, |
387 | 3.22k | &data_size, |
388 | 3.22k | NULL ); |
389 | | |
390 | 3.22k | libfwsi_item_get_delegate_folder_identifier( |
391 | 3.22k | item, |
392 | 3.22k | identifier, |
393 | 3.22k | sizeof( identifier ), |
394 | 3.22k | NULL ); |
395 | | |
396 | 3.22k | libfwsi_item_get_number_of_extension_blocks( |
397 | 3.22k | item, |
398 | 3.22k | &number_of_extension_blocks, |
399 | 3.22k | NULL ); |
400 | | |
401 | 3.22k | for( extension_block_index = 0; |
402 | 3.32k | extension_block_index < number_of_extension_blocks; |
403 | 3.22k | extension_block_index++ ) |
404 | 102 | { |
405 | 102 | if( libfwsi_item_get_extension_block( |
406 | 102 | item, |
407 | 102 | extension_block_index, |
408 | 102 | &extension_block, |
409 | 102 | NULL ) == 1 ) |
410 | 102 | { |
411 | 102 | libfwsi_extension_block_get_signature( |
412 | 102 | extension_block, |
413 | 102 | &signature, |
414 | 102 | NULL ); |
415 | | |
416 | 102 | libfwsi_extension_block_get_data_size( |
417 | 102 | extension_block, |
418 | 102 | &data_size, |
419 | 102 | NULL ); |
420 | | |
421 | 102 | extension_block = NULL; |
422 | 102 | } |
423 | 102 | } |
424 | 3.22k | ossfuzz_libfwsi_walk_compressed_folder_item( |
425 | 3.22k | item ); |
426 | | |
427 | 3.22k | ossfuzz_libfwsi_walk_control_panel_category_item( |
428 | 3.22k | item ); |
429 | | |
430 | 3.22k | ossfuzz_libfwsi_walk_control_panel_item_item( |
431 | 3.22k | item ); |
432 | | |
433 | 3.22k | ossfuzz_libfwsi_walk_file_entry_item( |
434 | 3.22k | item ); |
435 | | |
436 | 3.22k | ossfuzz_libfwsi_walk_network_location_item( |
437 | 3.22k | item ); |
438 | | |
439 | 3.22k | ossfuzz_libfwsi_walk_root_folder_item( |
440 | 3.22k | item ); |
441 | | |
442 | 3.22k | ossfuzz_libfwsi_walk_users_property_view_item( |
443 | 3.22k | item ); |
444 | | |
445 | 3.22k | ossfuzz_libfwsi_walk_volume_item( |
446 | 3.22k | item ); |
447 | 3.22k | } |
448 | | |
449 | | extern "C" { |
450 | | |
451 | | int LLVMFuzzerTestOneInput( |
452 | | const uint8_t *data, |
453 | | size_t size ) |
454 | 9.96k | { |
455 | 9.96k | libfwsi_item_t *item = NULL; |
456 | | |
457 | 9.96k | if( libfwsi_item_initialize( |
458 | 9.96k | &item, |
459 | 9.96k | NULL ) != 1 ) |
460 | 0 | { |
461 | 0 | return( 0 ); |
462 | 0 | } |
463 | 9.96k | if( libfwsi_item_copy_from_byte_stream( |
464 | 9.96k | item, |
465 | 9.96k | data, |
466 | 9.96k | size, |
467 | 9.96k | ossfuzz_libfwsi_get_codepage( |
468 | 9.96k | data, |
469 | 9.96k | size ), |
470 | 9.96k | NULL ) == 1 ) |
471 | 6.59k | { |
472 | 6.59k | ossfuzz_libfwsi_walk_item( |
473 | 6.59k | item ); |
474 | 6.59k | } |
475 | 9.96k | libfwsi_item_free( |
476 | 9.96k | &item, |
477 | 9.96k | NULL ); |
478 | | |
479 | 9.96k | return( 0 ); |
480 | 9.96k | } |
481 | | |
482 | | } /* extern "C" */ |