/src/libmsiecf/ossfuzz/file_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libmsiecf file type |
3 | | * |
4 | | * Copyright (C) 2009-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 | | /* Note that some of the OSS-Fuzz engines use C++ |
26 | | */ |
27 | | extern "C" { |
28 | | |
29 | | #include "ossfuzz_libbfio.h" |
30 | | #include "ossfuzz_libmsiecf.h" |
31 | | |
32 | | #if !defined( LIBMSIECF_HAVE_BFIO ) |
33 | | |
34 | | /* Determines if a file contains a MSIECF file signature using a Basic File IO (bfio) handle |
35 | | * Returns 1 if true, 0 if not or -1 on error |
36 | | */ |
37 | | LIBMSIECF_EXTERN \ |
38 | | int libmsiecf_check_file_signature_file_io_handle( |
39 | | libbfio_handle_t *file_io_handle, |
40 | | libmsiecf_error_t **error ); |
41 | | |
42 | | /* Opens a file using a Basic File IO (bfio) handle |
43 | | * Returns 1 if successful or -1 on error |
44 | | */ |
45 | | LIBMSIECF_EXTERN \ |
46 | | int libmsiecf_file_open_file_io_handle( |
47 | | libmsiecf_file_t *file, |
48 | | libbfio_handle_t *file_io_handle, |
49 | | int access_flags, |
50 | | libmsiecf_error_t **error ); |
51 | | |
52 | | #endif /* !defined( LIBMSIECF_HAVE_BFIO ) */ |
53 | | |
54 | | /* Returns one of the codepages supported by libmsiecf_file_set_ascii_codepage |
55 | | * based on the first byte of the provided input |
56 | | */ |
57 | | int |
58 | | fuzzing_supported_codepage( |
59 | | const uint8_t *data, |
60 | | size_t size ) |
61 | 3.85k | { |
62 | | /* The values accepted by libmsiecf_file_set_ascii_codepage |
63 | | */ |
64 | 3.85k | int codepages[15] = { |
65 | 3.85k | LIBMSIECF_CODEPAGE_ASCII, |
66 | 3.85k | LIBMSIECF_CODEPAGE_WINDOWS_874, |
67 | 3.85k | LIBMSIECF_CODEPAGE_WINDOWS_932, |
68 | 3.85k | LIBMSIECF_CODEPAGE_WINDOWS_936, |
69 | 3.85k | LIBMSIECF_CODEPAGE_WINDOWS_949, |
70 | 3.85k | LIBMSIECF_CODEPAGE_WINDOWS_950, |
71 | 3.85k | LIBMSIECF_CODEPAGE_WINDOWS_1250, |
72 | 3.85k | LIBMSIECF_CODEPAGE_WINDOWS_1251, |
73 | 3.85k | LIBMSIECF_CODEPAGE_WINDOWS_1252, |
74 | 3.85k | LIBMSIECF_CODEPAGE_WINDOWS_1253, |
75 | 3.85k | LIBMSIECF_CODEPAGE_WINDOWS_1254, |
76 | 3.85k | LIBMSIECF_CODEPAGE_WINDOWS_1255, |
77 | 3.85k | LIBMSIECF_CODEPAGE_WINDOWS_1256, |
78 | 3.85k | LIBMSIECF_CODEPAGE_WINDOWS_1257, |
79 | 3.85k | LIBMSIECF_CODEPAGE_WINDOWS_1258 }; |
80 | | |
81 | 3.85k | if( size == 0 ) |
82 | 0 | { |
83 | 0 | return( codepages[0] ); |
84 | 0 | } |
85 | 3.85k | return( codepages[data[0] % 15] ); |
86 | 3.85k | } |
87 | | |
88 | | int LLVMFuzzerTestOneInput( |
89 | | const uint8_t *data, |
90 | | size_t size ) |
91 | 2.00k | { |
92 | 2.00k | char cache_directory_name[16] = {}; |
93 | 2.00k | char error_string[1024] = {}; |
94 | 2.00k | libbfio_handle_t *file_io_handle = NULL; |
95 | 2.00k | libmsiecf_file_t *file = NULL; |
96 | 2.00k | libmsiecf_item_t *item = NULL; |
97 | 2.00k | size64_t unallocated_block_size = 0; |
98 | 2.00k | off64_t unallocated_block_offset = 0; |
99 | 2.00k | uint8_t major_version = 0; |
100 | 2.00k | uint8_t minor_version = 0; |
101 | 2.00k | int ascii_codepage = 0; |
102 | 2.00k | int number_of_cache_directories = 0; |
103 | 2.00k | int number_of_items = 0; |
104 | 2.00k | int number_of_recovered_items = 0; |
105 | 2.00k | int number_of_unallocated_blocks = 0; |
106 | 2.00k | int index = 0; |
107 | 2.00k | int result = 0; |
108 | | |
109 | | /* Test the library support functions |
110 | | */ |
111 | 2.00k | libmsiecf_get_version(); |
112 | | |
113 | 2.00k | libmsiecf_get_access_flags_read(); |
114 | | |
115 | 2.00k | libmsiecf_get_codepage( |
116 | 2.00k | &ascii_codepage, |
117 | 2.00k | NULL ); |
118 | | |
119 | 2.00k | libmsiecf_get_codepage( |
120 | 2.00k | NULL, |
121 | 2.00k | NULL ); |
122 | | |
123 | | /* 0x12345 is not a supported codepage |
124 | | */ |
125 | 2.00k | libmsiecf_set_codepage( |
126 | 2.00k | 0x12345, |
127 | 2.00k | NULL ); |
128 | | |
129 | 2.00k | ascii_codepage = fuzzing_supported_codepage( |
130 | 2.00k | data, |
131 | 2.00k | size ); |
132 | | |
133 | 2.00k | libmsiecf_set_codepage( |
134 | 2.00k | ascii_codepage, |
135 | 2.00k | NULL ); |
136 | | |
137 | 2.00k | if( libbfio_memory_range_initialize( |
138 | 2.00k | &file_io_handle, |
139 | 2.00k | NULL ) != 1 ) |
140 | 0 | { |
141 | 0 | return( 0 ); |
142 | 0 | } |
143 | 2.00k | if( libbfio_memory_range_set( |
144 | 2.00k | file_io_handle, |
145 | 2.00k | (uint8_t *) data, |
146 | 2.00k | size, |
147 | 2.00k | NULL ) != 1 ) |
148 | 0 | { |
149 | 0 | goto on_error_libbfio; |
150 | 0 | } |
151 | 2.00k | if( libmsiecf_file_initialize( |
152 | 2.00k | &file, |
153 | 2.00k | NULL ) != 1 ) |
154 | 0 | { |
155 | 0 | goto on_error_libbfio; |
156 | 0 | } |
157 | 2.00k | libmsiecf_check_file_signature_file_io_handle( |
158 | 2.00k | NULL, |
159 | 2.00k | NULL ); |
160 | | |
161 | 2.00k | if( file_io_handle != NULL ) |
162 | 2.00k | { |
163 | 2.00k | libmsiecf_check_file_signature_file_io_handle( |
164 | 2.00k | file_io_handle, |
165 | 2.00k | NULL ); |
166 | 2.00k | } |
167 | 2.00k | libmsiecf_file_get_size( |
168 | 2.00k | file, |
169 | 2.00k | NULL, |
170 | 2.00k | NULL ); |
171 | | |
172 | 2.00k | libmsiecf_file_get_ascii_codepage( |
173 | 2.00k | file, |
174 | 2.00k | NULL, |
175 | 2.00k | NULL ); |
176 | | |
177 | 2.00k | libmsiecf_file_get_format_version( |
178 | 2.00k | file, |
179 | 2.00k | NULL, |
180 | 2.00k | NULL, |
181 | 2.00k | NULL ); |
182 | | |
183 | 2.00k | libmsiecf_file_get_number_of_unallocated_blocks( |
184 | 2.00k | file, |
185 | 2.00k | NULL, |
186 | 2.00k | NULL ); |
187 | | |
188 | 2.00k | libmsiecf_file_get_unallocated_block( |
189 | 2.00k | file, |
190 | 2.00k | 0, |
191 | 2.00k | NULL, |
192 | 2.00k | NULL, |
193 | 2.00k | NULL ); |
194 | | |
195 | 2.00k | libmsiecf_file_get_number_of_cache_directories( |
196 | 2.00k | file, |
197 | 2.00k | NULL, |
198 | 2.00k | NULL ); |
199 | | |
200 | 2.00k | libmsiecf_file_get_cache_directory_name( |
201 | 2.00k | file, |
202 | 2.00k | 0, |
203 | 2.00k | NULL, |
204 | 2.00k | sizeof( cache_directory_name ), |
205 | 2.00k | NULL ); |
206 | | |
207 | 2.00k | libmsiecf_file_get_number_of_items( |
208 | 2.00k | file, |
209 | 2.00k | NULL, |
210 | 2.00k | NULL ); |
211 | | |
212 | 2.00k | libmsiecf_file_get_item_by_index( |
213 | 2.00k | file, |
214 | 2.00k | 0, |
215 | 2.00k | NULL, |
216 | 2.00k | NULL ); |
217 | | |
218 | 2.00k | libmsiecf_file_get_number_of_recovered_items( |
219 | 2.00k | file, |
220 | 2.00k | NULL, |
221 | 2.00k | NULL ); |
222 | | |
223 | 2.00k | libmsiecf_file_get_recovered_item_by_index( |
224 | 2.00k | file, |
225 | 2.00k | 0, |
226 | 2.00k | NULL, |
227 | 2.00k | NULL ); |
228 | | |
229 | 2.00k | libmsiecf_file_signal_abort( |
230 | 2.00k | NULL, |
231 | 2.00k | NULL ); |
232 | | |
233 | 2.00k | libmsiecf_file_signal_abort( |
234 | 2.00k | file, |
235 | 2.00k | NULL ); |
236 | | |
237 | 2.00k | libmsiecf_file_open_file_io_handle( |
238 | 2.00k | NULL, |
239 | 2.00k | file_io_handle, |
240 | 2.00k | LIBMSIECF_OPEN_READ, |
241 | 2.00k | NULL ); |
242 | | |
243 | 2.00k | libmsiecf_file_open_file_io_handle( |
244 | 2.00k | file, |
245 | 2.00k | NULL, |
246 | 2.00k | LIBMSIECF_OPEN_READ, |
247 | 2.00k | NULL ); |
248 | | |
249 | 2.00k | libmsiecf_file_open_file_io_handle( |
250 | 2.00k | file, |
251 | 2.00k | file_io_handle, |
252 | 2.00k | 0, |
253 | 2.00k | NULL ); |
254 | | |
255 | | /* LIBMSIECF_OPEN_READ_WRITE contains the unsupported write access flag |
256 | | */ |
257 | 2.00k | libmsiecf_file_open_file_io_handle( |
258 | 2.00k | file, |
259 | 2.00k | file_io_handle, |
260 | 2.00k | LIBMSIECF_OPEN_READ_WRITE, |
261 | 2.00k | NULL ); |
262 | | |
263 | 2.00k | libmsiecf_file_close( |
264 | 2.00k | NULL, |
265 | 2.00k | NULL ); |
266 | | |
267 | 2.00k | libmsiecf_file_close( |
268 | 2.00k | file, |
269 | 2.00k | NULL ); |
270 | | |
271 | | /* Test the pre-open error path |
272 | | */ |
273 | 2.00k | libmsiecf_file_get_size( |
274 | 2.00k | file, |
275 | 2.00k | &unallocated_block_size, |
276 | 2.00k | NULL ); |
277 | 2.00k | if( libmsiecf_file_open_file_io_handle( |
278 | 2.00k | file, |
279 | 2.00k | file_io_handle, |
280 | 2.00k | LIBMSIECF_OPEN_READ, |
281 | 2.00k | NULL ) != 1 ) |
282 | 1.28k | { |
283 | 1.28k | goto on_error_libmsiecf; |
284 | 1.28k | } |
285 | | /* Test the "file IO handle already set" error branch |
286 | | */ |
287 | 722 | libmsiecf_file_open_file_io_handle( |
288 | 722 | file, |
289 | 722 | file_io_handle, |
290 | 722 | LIBMSIECF_OPEN_READ, |
291 | 722 | NULL ); |
292 | | |
293 | | /* Test the file functions with an opened file |
294 | | */ |
295 | 722 | libmsiecf_file_get_size( |
296 | 722 | file, |
297 | 722 | NULL, |
298 | 722 | NULL ); |
299 | 722 | libmsiecf_file_get_ascii_codepage( |
300 | 722 | file, |
301 | 722 | NULL, |
302 | 722 | NULL ); |
303 | 722 | libmsiecf_file_get_format_version( |
304 | 722 | file, |
305 | 722 | NULL, |
306 | 722 | NULL, |
307 | 722 | NULL ); |
308 | 722 | libmsiecf_file_get_number_of_unallocated_blocks( |
309 | 722 | file, |
310 | 722 | NULL, |
311 | 722 | NULL ); |
312 | 722 | libmsiecf_file_get_unallocated_block( |
313 | 722 | file, |
314 | 722 | 0, |
315 | 722 | NULL, |
316 | 722 | NULL, |
317 | 722 | NULL ); |
318 | 722 | libmsiecf_file_get_number_of_cache_directories( |
319 | 722 | file, |
320 | 722 | NULL, |
321 | 722 | NULL ); |
322 | 722 | libmsiecf_file_get_cache_directory_name( |
323 | 722 | file, |
324 | 722 | 0, |
325 | 722 | NULL, |
326 | 722 | sizeof( cache_directory_name ), |
327 | 722 | NULL ); |
328 | 722 | libmsiecf_file_get_number_of_items( |
329 | 722 | file, |
330 | 722 | NULL, |
331 | 722 | NULL ); |
332 | 722 | libmsiecf_file_get_item_by_index( |
333 | 722 | file, |
334 | 722 | 0, |
335 | 722 | NULL, |
336 | 722 | NULL ); |
337 | 722 | libmsiecf_file_get_number_of_recovered_items( |
338 | 722 | file, |
339 | 722 | NULL, |
340 | 722 | NULL ); |
341 | 722 | libmsiecf_file_get_recovered_item_by_index( |
342 | 722 | file, |
343 | 722 | 0, |
344 | 722 | NULL, |
345 | 722 | NULL ); |
346 | | |
347 | 722 | libmsiecf_file_get_size( |
348 | 722 | file, |
349 | 722 | &unallocated_block_size, |
350 | 722 | NULL ); |
351 | 722 | libmsiecf_file_get_ascii_codepage( |
352 | 722 | file, |
353 | 722 | &ascii_codepage, |
354 | 722 | NULL ); |
355 | | |
356 | | /* 0x12345 is not a supported codepage |
357 | | */ |
358 | 722 | libmsiecf_file_set_ascii_codepage( |
359 | 722 | file, |
360 | 722 | 0x12345, |
361 | 722 | NULL ); |
362 | | |
363 | 722 | ascii_codepage = fuzzing_supported_codepage( |
364 | 722 | data, |
365 | 722 | size ); |
366 | | |
367 | 722 | libmsiecf_file_set_ascii_codepage( |
368 | 722 | file, |
369 | 722 | ascii_codepage, |
370 | 722 | NULL ); |
371 | 722 | libmsiecf_file_get_ascii_codepage( |
372 | 722 | file, |
373 | 722 | &ascii_codepage, |
374 | 722 | NULL ); |
375 | 722 | libmsiecf_file_get_format_version( |
376 | 722 | file, |
377 | 722 | &major_version, |
378 | 722 | &minor_version, |
379 | 722 | NULL ); |
380 | 722 | libmsiecf_file_get_number_of_items( |
381 | 722 | file, |
382 | 722 | &number_of_items, |
383 | 722 | NULL ); |
384 | 722 | libmsiecf_file_get_number_of_recovered_items( |
385 | 722 | file, |
386 | 722 | &number_of_recovered_items, |
387 | 722 | NULL ); |
388 | | |
389 | 722 | if( number_of_items > 0 ) |
390 | 711 | { |
391 | 711 | result = libmsiecf_file_get_item_by_index( |
392 | 711 | file, |
393 | 711 | 0, |
394 | 711 | &item, |
395 | 711 | NULL ); |
396 | | |
397 | 711 | if( result == 1 ) |
398 | 153 | { |
399 | 153 | libmsiecf_file_get_item_by_index( |
400 | 153 | file, |
401 | 153 | 0, |
402 | 153 | &item, |
403 | 153 | NULL ); |
404 | | |
405 | 153 | libmsiecf_item_free( |
406 | 153 | &item, |
407 | 153 | NULL ); |
408 | 153 | } |
409 | 711 | } |
410 | 722 | result = libmsiecf_file_get_number_of_unallocated_blocks( |
411 | 722 | file, |
412 | 722 | &number_of_unallocated_blocks, |
413 | 722 | NULL ); |
414 | | |
415 | 722 | if( ( result == 1 ) |
416 | 722 | && ( number_of_unallocated_blocks > 0 ) ) |
417 | 640 | { |
418 | | /* Iterate over a limited number of unallocated blocks |
419 | | */ |
420 | 640 | for( index = 0; |
421 | 24.8k | index < number_of_unallocated_blocks && index < 64; |
422 | 24.1k | index += 1 ) |
423 | 24.1k | { |
424 | 24.1k | libmsiecf_file_get_unallocated_block( |
425 | 24.1k | file, |
426 | 24.1k | index, |
427 | 24.1k | &unallocated_block_offset, |
428 | 24.1k | &unallocated_block_size, |
429 | 24.1k | NULL ); |
430 | 24.1k | } |
431 | | /* Test the out-of-range error branch |
432 | | */ |
433 | 640 | if( number_of_unallocated_blocks < 2147483647 ) |
434 | 640 | { |
435 | 640 | index = number_of_unallocated_blocks + 1; |
436 | 640 | } |
437 | 0 | else |
438 | 0 | { |
439 | 0 | index = 2147483646; |
440 | 0 | } |
441 | 640 | libmsiecf_file_get_unallocated_block( |
442 | 640 | file, |
443 | 640 | index, |
444 | 640 | &unallocated_block_offset, |
445 | 640 | &unallocated_block_size, |
446 | 640 | NULL ); |
447 | 640 | } |
448 | 722 | result = libmsiecf_file_get_number_of_cache_directories( |
449 | 722 | file, |
450 | 722 | &number_of_cache_directories, |
451 | 722 | NULL ); |
452 | | |
453 | 722 | if( ( result == 1 ) |
454 | 722 | && ( number_of_cache_directories > 0 ) ) |
455 | 306 | { |
456 | | /* Iterate over a limited number of cache directories |
457 | | */ |
458 | 306 | for( index = 0; |
459 | 1.25k | index < number_of_cache_directories && index < 8; |
460 | 953 | index += 1 ) |
461 | 953 | { |
462 | 953 | libmsiecf_file_get_cache_directory_name( |
463 | 953 | file, |
464 | 953 | index, |
465 | 953 | cache_directory_name, |
466 | 953 | sizeof( cache_directory_name ), |
467 | 953 | NULL ); |
468 | 953 | } |
469 | | /* Test the out-of-range error branch |
470 | | */ |
471 | 306 | if( number_of_cache_directories < 2147483647 ) |
472 | 306 | { |
473 | 306 | index = number_of_cache_directories + 1; |
474 | 306 | } |
475 | 0 | else |
476 | 0 | { |
477 | 0 | index = 2147483646; |
478 | 0 | } |
479 | 306 | libmsiecf_file_get_cache_directory_name( |
480 | 306 | file, |
481 | 306 | index, |
482 | 306 | cache_directory_name, |
483 | 306 | sizeof( cache_directory_name ), |
484 | 306 | NULL ); |
485 | 306 | } |
486 | 722 | libmsiecf_file_signal_abort( |
487 | 722 | file, |
488 | 722 | NULL ); |
489 | | |
490 | 722 | libmsiecf_file_close( |
491 | 722 | file, |
492 | 722 | NULL ); |
493 | | |
494 | 2.00k | on_error_libmsiecf: |
495 | 2.00k | if( file != NULL ) |
496 | 2.00k | { |
497 | 2.00k | libmsiecf_file_free( |
498 | 2.00k | &file, |
499 | 2.00k | NULL ); |
500 | 2.00k | } |
501 | | |
502 | 2.00k | on_error_libbfio: |
503 | 2.00k | if( file_io_handle != NULL ) |
504 | 2.00k | { |
505 | 2.00k | libbfio_handle_free( |
506 | 2.00k | &file_io_handle, |
507 | | NULL ); |
508 | 2.00k | } |
509 | 2.00k | return( 0 ); |
510 | 2.00k | } |
511 | | |
512 | | } /* extern "C" */ |