/src/libexe/ossfuzz/file_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libexe file 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 | | /* Note that some of the OSS-Fuzz engines use C++ |
26 | | */ |
27 | | extern "C" { |
28 | | |
29 | | #include "ossfuzz_libbfio.h" |
30 | | #include "ossfuzz_libexe.h" |
31 | | |
32 | | #if !defined( LIBEXE_HAVE_BFIO ) |
33 | | |
34 | | /* Opens a file using a Basic File IO (bfio) handle |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | LIBEXE_EXTERN \ |
38 | | int libexe_file_open_file_io_handle( |
39 | | libexe_file_t *file, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libexe_error_t **error ); |
43 | | |
44 | | /* Determines if a file contains an EXE file signature using a Basic File IO (bfio) handle |
45 | | * Returns 1 if true, 0 if not or -1 on error |
46 | | */ |
47 | | LIBEXE_EXTERN \ |
48 | | int libexe_check_file_signature_file_io_handle( |
49 | | libbfio_handle_t *file_io_handle, |
50 | | libexe_error_t **error ); |
51 | | |
52 | | /* Retrieves the section data file io handle |
53 | | * Returns 1 if successful or -1 on error |
54 | | */ |
55 | | LIBEXE_EXTERN \ |
56 | | int libexe_section_get_data_file_io_handle( |
57 | | libexe_section_t *section, |
58 | | libbfio_handle_t **file_io_handle, |
59 | | libexe_error_t **error ); |
60 | | |
61 | | #endif /* !defined( LIBEXE_HAVE_BFIO ) */ |
62 | | |
63 | | /* The ASCII codepages that are supported by libexe_file_set_ascii_codepage |
64 | | */ |
65 | | static const int supported_ascii_codepages[ 15 ] = |
66 | | { |
67 | | LIBEXE_CODEPAGE_ASCII, |
68 | | LIBEXE_CODEPAGE_WINDOWS_874, |
69 | | LIBEXE_CODEPAGE_WINDOWS_932, |
70 | | LIBEXE_CODEPAGE_WINDOWS_936, |
71 | | LIBEXE_CODEPAGE_WINDOWS_949, |
72 | | LIBEXE_CODEPAGE_WINDOWS_950, |
73 | | LIBEXE_CODEPAGE_WINDOWS_1250, |
74 | | LIBEXE_CODEPAGE_WINDOWS_1251, |
75 | | LIBEXE_CODEPAGE_WINDOWS_1252, |
76 | | LIBEXE_CODEPAGE_WINDOWS_1253, |
77 | | LIBEXE_CODEPAGE_WINDOWS_1254, |
78 | | LIBEXE_CODEPAGE_WINDOWS_1255, |
79 | | LIBEXE_CODEPAGE_WINDOWS_1256, |
80 | | LIBEXE_CODEPAGE_WINDOWS_1257, |
81 | | LIBEXE_CODEPAGE_WINDOWS_1258 |
82 | | }; |
83 | | |
84 | | /* The maximum number of sections to process for a specific input |
85 | | */ |
86 | 3.20k | #define FILE_FUZZER_MAXIMUM_NUMBER_OF_SECTIONS 8 |
87 | | |
88 | | /* The maximum size of a name buffer |
89 | | */ |
90 | 17.0k | #define FILE_FUZZER_MAX_NAME_SIZE 256 |
91 | | |
92 | | /* Retrieves a read size (buffer size) from the input |
93 | | */ |
94 | | size_t file_fuzzer_get_read_size( |
95 | | const uint8_t *data, |
96 | | size_t size ) |
97 | 3.11k | { |
98 | 3.11k | if( size == 0 ) |
99 | 0 | { |
100 | 0 | return( 1 ); |
101 | 0 | } |
102 | 3.11k | return( 1 + ( size % FILE_FUZZER_MAX_NAME_SIZE ) ); |
103 | 3.11k | } |
104 | | |
105 | | /* Retrieves a seek offset from the input |
106 | | */ |
107 | | off64_t file_fuzzer_get_seek_offset( |
108 | | const uint8_t *data, |
109 | | size_t size ) |
110 | 3.11k | { |
111 | 3.11k | off64_t seek_offset = 0; |
112 | 3.11k | int number_of_bytes = 0; |
113 | 3.11k | int index = 0; |
114 | | |
115 | 3.11k | if( size > 4 ) |
116 | 3.11k | { |
117 | 3.11k | number_of_bytes = 4; |
118 | 3.11k | } |
119 | 0 | else |
120 | 0 | { |
121 | 0 | number_of_bytes = (int) size; |
122 | 0 | } |
123 | 3.11k | for( index = 0; |
124 | 15.5k | index < number_of_bytes; |
125 | 12.4k | index++ ) |
126 | 12.4k | { |
127 | 12.4k | seek_offset = ( seek_offset << 8 ) | (off64_t) data[ size - 1 - index ]; |
128 | 12.4k | } |
129 | 3.11k | return( seek_offset ); |
130 | 3.11k | } |
131 | | |
132 | | /* Fuzzes a section |
133 | | */ |
134 | | void file_fuzzer_fuzz_section( |
135 | | libexe_section_t *section, |
136 | | const uint8_t *data, |
137 | | size_t size, |
138 | | char *name ) |
139 | 3.11k | { |
140 | 3.11k | uint16_t utf16_name[ 16 ] = { 0 }; |
141 | | |
142 | 3.11k | libbfio_handle_t *section_file_io_handle = NULL; |
143 | 3.11k | off64_t offset = 0; |
144 | 3.11k | off64_t seek_offset = 0; |
145 | 3.11k | off64_t start_offset = 0; |
146 | 3.11k | size64_t size_64bit = 0; |
147 | 3.11k | size_t name_size = 0; |
148 | 3.11k | size_t read_size = 0; |
149 | 3.11k | uint32_t virtual_address = 0; |
150 | 3.11k | int result = 0; |
151 | | |
152 | 3.11k | name_size = FILE_FUZZER_MAX_NAME_SIZE; |
153 | | |
154 | 3.11k | if( libexe_section_get_name_size( |
155 | 3.11k | section, |
156 | 3.11k | &name_size, |
157 | 3.11k | NULL ) == 1 ) |
158 | 3.11k | { |
159 | 3.11k | if( name_size > FILE_FUZZER_MAX_NAME_SIZE ) |
160 | 0 | { |
161 | 0 | name_size = FILE_FUZZER_MAX_NAME_SIZE; |
162 | 0 | } |
163 | 3.11k | libexe_section_get_name( |
164 | 3.11k | section, |
165 | 3.11k | name, |
166 | 3.11k | name_size, |
167 | 3.11k | NULL ); |
168 | 3.11k | } |
169 | 3.11k | name_size = FILE_FUZZER_MAX_NAME_SIZE; |
170 | | |
171 | 3.11k | if( libexe_section_get_utf8_name_size( |
172 | 3.11k | section, |
173 | 3.11k | &name_size, |
174 | 3.11k | NULL ) == 1 ) |
175 | 3.11k | { |
176 | 3.11k | if( name_size > FILE_FUZZER_MAX_NAME_SIZE ) |
177 | 0 | { |
178 | 0 | name_size = FILE_FUZZER_MAX_NAME_SIZE; |
179 | 0 | } |
180 | 3.11k | libexe_section_get_utf8_name( |
181 | 3.11k | section, |
182 | 3.11k | (uint8_t *) name, |
183 | 3.11k | name_size, |
184 | 3.11k | NULL ); |
185 | 3.11k | } |
186 | 3.11k | libexe_section_get_utf16_name_size( |
187 | 3.11k | section, |
188 | 3.11k | &name_size, |
189 | 3.11k | NULL ); |
190 | | |
191 | 3.11k | libexe_section_get_utf16_name( |
192 | 3.11k | section, |
193 | 3.11k | utf16_name, |
194 | 3.11k | 16, |
195 | 3.11k | NULL ); |
196 | | |
197 | 3.11k | libexe_section_get_size( |
198 | 3.11k | section, |
199 | 3.11k | &size_64bit, |
200 | 3.11k | NULL ); |
201 | | |
202 | 3.11k | libexe_section_get_start_offset( |
203 | 3.11k | section, |
204 | 3.11k | &start_offset, |
205 | 3.11k | NULL ); |
206 | | |
207 | 3.11k | libexe_section_get_virtual_address( |
208 | 3.11k | section, |
209 | 3.11k | &virtual_address, |
210 | 3.11k | NULL ); |
211 | | |
212 | 3.11k | read_size = file_fuzzer_get_read_size( |
213 | 3.11k | data, |
214 | 3.11k | size ); |
215 | | |
216 | 3.11k | libexe_section_read_buffer( |
217 | 3.11k | section, |
218 | 3.11k | (uint8_t *) name, |
219 | 3.11k | read_size, |
220 | 3.11k | NULL ); |
221 | | |
222 | 3.11k | seek_offset = file_fuzzer_get_seek_offset( |
223 | 3.11k | data, |
224 | 3.11k | size ); |
225 | | |
226 | 3.11k | libexe_section_seek_offset( |
227 | 3.11k | section, |
228 | 3.11k | seek_offset, |
229 | 3.11k | size > 2 ? (int) ( data[ 1 ] % 3 ) : 0, |
230 | 3.11k | NULL ); |
231 | | |
232 | 3.11k | libexe_section_get_offset( |
233 | 3.11k | section, |
234 | 3.11k | &offset, |
235 | 3.11k | NULL ); |
236 | | |
237 | 3.11k | libexe_section_read_buffer_at_offset( |
238 | 3.11k | section, |
239 | 3.11k | (uint8_t *) name, |
240 | 3.11k | read_size, |
241 | 3.11k | seek_offset, |
242 | 3.11k | NULL ); |
243 | | |
244 | 3.11k | result = libexe_section_get_data_file_io_handle( |
245 | 3.11k | section, |
246 | 3.11k | §ion_file_io_handle, |
247 | 3.11k | NULL ); |
248 | | |
249 | 3.11k | if( ( result == 1 ) |
250 | 3.11k | && ( section_file_io_handle != NULL ) ) |
251 | 3.11k | { |
252 | 3.11k | libbfio_handle_open( |
253 | 3.11k | section_file_io_handle, |
254 | 3.11k | LIBBFIO_OPEN_READ, |
255 | 3.11k | NULL ); |
256 | | |
257 | 3.11k | libbfio_handle_exists( |
258 | 3.11k | section_file_io_handle, |
259 | 3.11k | NULL ); |
260 | | |
261 | 3.11k | libbfio_handle_is_open( |
262 | 3.11k | section_file_io_handle, |
263 | 3.11k | NULL ); |
264 | | |
265 | 3.11k | libbfio_handle_get_size( |
266 | 3.11k | section_file_io_handle, |
267 | 3.11k | &size_64bit, |
268 | 3.11k | NULL ); |
269 | | |
270 | 3.11k | libbfio_handle_read_buffer( |
271 | 3.11k | section_file_io_handle, |
272 | 3.11k | (uint8_t *) name, |
273 | 3.11k | read_size, |
274 | 3.11k | NULL ); |
275 | | |
276 | 3.11k | libbfio_handle_seek_offset( |
277 | 3.11k | section_file_io_handle, |
278 | 3.11k | seek_offset, |
279 | 3.11k | 0, |
280 | 3.11k | NULL ); |
281 | | |
282 | 3.11k | libbfio_handle_read_buffer_at_offset( |
283 | 3.11k | section_file_io_handle, |
284 | 3.11k | (uint8_t *) name, |
285 | 3.11k | read_size, |
286 | 3.11k | seek_offset, |
287 | 3.11k | NULL ); |
288 | | |
289 | 3.11k | libbfio_handle_close( |
290 | 3.11k | section_file_io_handle, |
291 | 3.11k | NULL ); |
292 | | |
293 | 3.11k | libbfio_handle_free( |
294 | 3.11k | §ion_file_io_handle, |
295 | 3.11k | NULL ); |
296 | 3.11k | } |
297 | 3.11k | libexe_section_free( |
298 | 3.11k | §ion, |
299 | 3.11k | NULL ); |
300 | 3.11k | } |
301 | | |
302 | | /* Fuzzes the sections of a file |
303 | | */ |
304 | | static void file_fuzzer_fuzz_sections( |
305 | | libexe_file_t *file, |
306 | | const uint8_t *data, |
307 | | size_t size, |
308 | | char *name ) |
309 | 759 | { |
310 | 759 | char candidate_name[ 9 ] = { 0 }; |
311 | 759 | libexe_section_t *section = NULL; |
312 | 759 | libexe_section_t *section_by_name = NULL; |
313 | 759 | int number_of_sections = 0; |
314 | 759 | int section_index = 0; |
315 | 759 | size_t candidate_name_length = 0; |
316 | 759 | size_t name_size = 0; |
317 | | |
318 | 759 | for( candidate_name_length = 0; |
319 | 6.83k | candidate_name_length < 8; |
320 | 6.07k | candidate_name_length++ ) |
321 | 6.07k | { |
322 | 6.07k | candidate_name[ candidate_name_length ] = (char) ( size > 0 ? data[ ( (size_t) candidate_name_length * size ) / 8 ] : 0 ); |
323 | 6.07k | } |
324 | 759 | candidate_name[ 8 ] = 0; |
325 | | |
326 | 759 | libexe_file_get_section_by_name( |
327 | 759 | file, |
328 | 759 | candidate_name, |
329 | 759 | 8, |
330 | 759 | §ion_by_name, |
331 | 759 | NULL ); |
332 | | |
333 | 759 | if( section_by_name != NULL ) |
334 | 2 | { |
335 | 2 | libexe_section_free( |
336 | 2 | §ion_by_name, |
337 | 2 | NULL ); |
338 | 2 | } |
339 | 759 | if( libexe_file_get_number_of_sections( |
340 | 759 | file, |
341 | 759 | &number_of_sections, |
342 | 759 | NULL ) != 1 ) |
343 | 0 | { |
344 | 0 | return; |
345 | 0 | } |
346 | 759 | if( number_of_sections <= 0 ) |
347 | 7 | { |
348 | 7 | return; |
349 | 7 | } |
350 | 752 | section = NULL; |
351 | | |
352 | 752 | if( libexe_file_get_section_by_index( |
353 | 752 | file, |
354 | 752 | 0, |
355 | 752 | §ion, |
356 | 752 | NULL ) == 1 ) |
357 | 752 | { |
358 | 752 | if( section != NULL ) |
359 | 752 | { |
360 | 752 | name_size = FILE_FUZZER_MAX_NAME_SIZE; |
361 | | |
362 | 752 | if( libexe_section_get_name_size( |
363 | 752 | section, |
364 | 752 | &name_size, |
365 | 752 | NULL ) == 1 ) |
366 | 752 | { |
367 | 752 | if( name_size > 1 ) |
368 | 687 | { |
369 | 687 | if( libexe_section_get_name( |
370 | 687 | section, |
371 | 687 | name, |
372 | 687 | FILE_FUZZER_MAX_NAME_SIZE, |
373 | 687 | NULL ) == 1 ) |
374 | 687 | { |
375 | 687 | section_by_name = NULL; |
376 | | |
377 | 687 | libexe_file_get_section_by_name( |
378 | 687 | file, |
379 | 687 | name, |
380 | 687 | ( size_t ) ( name_size - 1 ), |
381 | 687 | §ion_by_name, |
382 | 687 | NULL ); |
383 | | |
384 | 687 | if( section_by_name != NULL ) |
385 | 687 | { |
386 | 687 | libexe_section_free( |
387 | 687 | §ion_by_name, |
388 | 687 | NULL ); |
389 | 687 | } |
390 | 687 | } |
391 | 687 | } |
392 | 752 | } |
393 | 752 | libexe_section_free( |
394 | 752 | §ion, |
395 | 752 | NULL ); |
396 | 752 | } |
397 | 752 | } |
398 | 752 | for( section_index = 0; |
399 | 3.86k | section_index < number_of_sections |
400 | 3.20k | && section_index < FILE_FUZZER_MAXIMUM_NUMBER_OF_SECTIONS; |
401 | 3.11k | section_index++ ) |
402 | 3.11k | { |
403 | 3.11k | section = NULL; |
404 | | |
405 | 3.11k | if( libexe_file_get_section_by_index( |
406 | 3.11k | file, |
407 | 3.11k | section_index, |
408 | 3.11k | §ion, |
409 | 3.11k | NULL ) != 1 ) |
410 | 0 | { |
411 | 0 | continue; |
412 | 0 | } |
413 | 3.11k | if( section == NULL ) |
414 | 0 | { |
415 | 0 | continue; |
416 | 0 | } |
417 | 3.11k | file_fuzzer_fuzz_section( |
418 | 3.11k | section, |
419 | 3.11k | data, |
420 | 3.11k | size, |
421 | 3.11k | name ); |
422 | 3.11k | } |
423 | 752 | } |
424 | | |
425 | | int LLVMFuzzerTestOneInput( |
426 | | const uint8_t *data, |
427 | | size_t size ) |
428 | 1.51k | { |
429 | 1.51k | char name[ FILE_FUZZER_MAX_NAME_SIZE ] = { 0 }; |
430 | | |
431 | 1.51k | libbfio_handle_t *signature_file_io_handle = NULL; |
432 | 1.51k | libbfio_handle_t *file_io_handle = NULL; |
433 | 1.51k | libexe_file_t *file = NULL; |
434 | 1.51k | int access_flags_read = 0; |
435 | 1.51k | int ascii_codepage = 0; |
436 | 1.51k | int codepage = 0; |
437 | 1.51k | int codepage_index = 0; |
438 | | |
439 | 1.51k | access_flags_read = libexe_get_access_flags_read(); |
440 | | |
441 | 1.51k | libexe_set_codepage( |
442 | 1.51k | 0, |
443 | 1.51k | NULL ); |
444 | | |
445 | 1.51k | libexe_get_codepage( |
446 | 1.51k | &codepage, |
447 | 1.51k | NULL ); |
448 | | |
449 | 1.51k | if( libbfio_memory_range_initialize( |
450 | 1.51k | &signature_file_io_handle, |
451 | 1.51k | NULL ) != 1 ) |
452 | 0 | { |
453 | 0 | return( 0 ); |
454 | 0 | } |
455 | 1.51k | if( libbfio_memory_range_set( |
456 | 1.51k | signature_file_io_handle, |
457 | 1.51k | (uint8_t *) data, |
458 | 1.51k | size, |
459 | 1.51k | NULL ) != 1 ) |
460 | 0 | { |
461 | 0 | goto on_error_signature; |
462 | 0 | } |
463 | 1.51k | libexe_check_file_signature_file_io_handle( |
464 | 1.51k | signature_file_io_handle, |
465 | 1.51k | NULL ); |
466 | | |
467 | 1.51k | if( libbfio_memory_range_initialize( |
468 | 1.51k | &file_io_handle, |
469 | 1.51k | NULL ) != 1 ) |
470 | 0 | { |
471 | 0 | goto on_error_signature; |
472 | 0 | } |
473 | 1.51k | if( libbfio_memory_range_set( |
474 | 1.51k | file_io_handle, |
475 | 1.51k | (uint8_t *) data, |
476 | 1.51k | size, |
477 | 1.51k | NULL ) != 1 ) |
478 | 0 | { |
479 | 0 | goto on_error_libbfio; |
480 | 0 | } |
481 | 1.51k | if( libexe_file_initialize( |
482 | 1.51k | &file, |
483 | 1.51k | NULL ) != 1 ) |
484 | 0 | { |
485 | 0 | goto on_error_libbfio; |
486 | 0 | } |
487 | 1.51k | if( libexe_file_open_file_io_handle( |
488 | 1.51k | file, |
489 | 1.51k | file_io_handle, |
490 | 1.51k | LIBEXE_OPEN_READ, |
491 | 1.51k | NULL ) == 1 ) |
492 | 759 | { |
493 | 759 | libexe_file_get_ascii_codepage( |
494 | 759 | file, |
495 | 759 | &ascii_codepage, |
496 | 759 | NULL ); |
497 | | |
498 | 759 | codepage_index = size > 0 ? ( int ) ( data[ 0 ] % 15 ) : 0; |
499 | | |
500 | 759 | libexe_file_set_ascii_codepage( |
501 | 759 | file, |
502 | 759 | supported_ascii_codepages[ codepage_index ], |
503 | 759 | NULL ); |
504 | | |
505 | 759 | file_fuzzer_fuzz_sections( |
506 | 759 | file, |
507 | 759 | data, |
508 | 759 | size, |
509 | 759 | name ); |
510 | | |
511 | 759 | libexe_file_signal_abort( |
512 | 759 | file, |
513 | 759 | NULL ); |
514 | 759 | } |
515 | 1.51k | libexe_file_close( |
516 | 1.51k | file, |
517 | 1.51k | NULL ); |
518 | | |
519 | 1.51k | on_error_libbfio: |
520 | 1.51k | libexe_file_free( |
521 | 1.51k | &file, |
522 | 1.51k | NULL ); |
523 | | |
524 | 1.51k | on_error_signature: |
525 | 1.51k | libbfio_handle_free( |
526 | 1.51k | &signature_file_io_handle, |
527 | 1.51k | NULL ); |
528 | | |
529 | 1.51k | libbfio_handle_free( |
530 | 1.51k | &file_io_handle, |
531 | 1.51k | NULL ); |
532 | | |
533 | 1.51k | return( 0 ); |
534 | 1.51k | } |
535 | | |
536 | | } /* extern "C" */ |