/src/libfsfat/ossfuzz/file_entry_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libfsfat file_entry type |
3 | | * |
4 | | * Copyright (C) 2021-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_libfsfat.h" |
31 | | |
32 | | #if !defined( LIBFSFAT_HAVE_BFIO ) |
33 | | |
34 | | /* Opens a volume using a Basic File IO (bfio) handle |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | LIBFSFAT_EXTERN \ |
38 | | int libfsfat_volume_open_file_io_handle( |
39 | | libfsfat_volume_t *volume, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libfsfat_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBFSFAT_HAVE_BFIO ) */ |
45 | | |
46 | | int LLVMFuzzerTestOneInput( |
47 | | const uint8_t *data, |
48 | | size_t size ) |
49 | 1.16k | { |
50 | 1.16k | uint8_t buffer[ 512 ]; |
51 | 1.16k | uint8_t utf8_name[ 512 ]; |
52 | 1.16k | uint16_t utf16_name[ 512 ]; |
53 | 1.16k | uint16_t utf16_string[ 2 ]; |
54 | | |
55 | 1.16k | libbfio_handle_t *file_io_handle = NULL; |
56 | 1.16k | libfsfat_file_entry_t *file_entry = NULL; |
57 | 1.16k | libfsfat_file_entry_t *root_directory = NULL; |
58 | 1.16k | libfsfat_volume_t *volume = NULL; |
59 | 1.16k | uint64_t identifier = 0; |
60 | 1.16k | uint64_t fat_timestamp = 0; |
61 | 1.16k | uint32_t extent_flags = 0; |
62 | 1.16k | uint16_t file_attribute_flags = 0; |
63 | 1.16k | size64_t file_entry_size = 0; |
64 | 1.16k | size_t utf8_name_size = 0; |
65 | 1.16k | size_t utf16_name_size = 0; |
66 | 1.16k | off64_t extent_offset = 0; |
67 | 1.16k | off64_t read_offset = 0; |
68 | 1.16k | int number_of_sub_file_entries = 0; |
69 | 1.16k | int number_of_extents = 0; |
70 | 1.16k | int sub_file_entry_index = 0; |
71 | 1.16k | int read_iterator = 0; |
72 | 1.16k | int result = 0; |
73 | | |
74 | 1.16k | utf16_string[ 0 ] = (uint16_t) '.'; |
75 | 1.16k | utf16_string[ 1 ] = (uint16_t) '.'; |
76 | | |
77 | 1.16k | if( libbfio_memory_range_initialize( |
78 | 1.16k | &file_io_handle, |
79 | 1.16k | NULL ) != 1 ) |
80 | 0 | { |
81 | 0 | return( 0 ); |
82 | 0 | } |
83 | 1.16k | if( libbfio_memory_range_set( |
84 | 1.16k | file_io_handle, |
85 | 1.16k | (uint8_t *) data, |
86 | 1.16k | size, |
87 | 1.16k | NULL ) != 1 ) |
88 | 0 | { |
89 | 0 | goto on_error_libbfio; |
90 | 0 | } |
91 | 1.16k | if( libfsfat_volume_initialize( |
92 | 1.16k | &volume, |
93 | 1.16k | NULL ) != 1 ) |
94 | 0 | { |
95 | 0 | goto on_error_libbfio; |
96 | 0 | } |
97 | 1.16k | if( libfsfat_volume_open_file_io_handle( |
98 | 1.16k | volume, |
99 | 1.16k | file_io_handle, |
100 | 1.16k | LIBFSFAT_OPEN_READ, |
101 | 1.16k | NULL ) != 1 ) |
102 | 1.07k | { |
103 | 1.07k | goto on_error_libfsfat_volume; |
104 | 1.07k | } |
105 | 97 | if( libfsfat_volume_get_root_directory( |
106 | 97 | volume, |
107 | 97 | &root_directory, |
108 | 97 | NULL ) != 1 ) |
109 | 0 | { |
110 | 0 | goto on_error_libfsfat_volume; |
111 | 0 | } |
112 | 97 | if( libfsfat_file_entry_get_identifier( |
113 | 97 | root_directory, |
114 | 97 | &identifier, |
115 | 97 | NULL ) != 1 ) |
116 | 0 | { |
117 | 0 | goto on_error_libfsfat_root_directory; |
118 | 0 | } |
119 | 97 | if( libfsfat_file_entry_get_access_time( |
120 | 97 | root_directory, |
121 | 97 | &fat_timestamp, |
122 | 97 | NULL ) == -1 ) |
123 | 0 | { |
124 | 0 | goto on_error_libfsfat_root_directory; |
125 | 0 | } |
126 | 97 | if( libfsfat_file_entry_get_creation_time( |
127 | 97 | root_directory, |
128 | 97 | &fat_timestamp, |
129 | 97 | NULL ) == -1 ) |
130 | 0 | { |
131 | 0 | goto on_error_libfsfat_root_directory; |
132 | 0 | } |
133 | 97 | if( libfsfat_file_entry_get_modification_time( |
134 | 97 | root_directory, |
135 | 97 | &fat_timestamp, |
136 | 97 | NULL ) == -1 ) |
137 | 0 | { |
138 | 0 | goto on_error_libfsfat_root_directory; |
139 | 0 | } |
140 | 97 | if( libfsfat_file_entry_get_file_attribute_flags( |
141 | 97 | root_directory, |
142 | 97 | &file_attribute_flags, |
143 | 97 | NULL ) == -1 ) |
144 | 0 | { |
145 | 0 | goto on_error_libfsfat_root_directory; |
146 | 0 | } |
147 | 97 | if( libfsfat_file_entry_get_utf8_name_size( |
148 | 97 | root_directory, |
149 | 97 | &utf8_name_size, |
150 | 97 | NULL ) == -1 ) |
151 | 0 | { |
152 | 0 | goto on_error_libfsfat_root_directory; |
153 | 0 | } |
154 | 97 | if( utf8_name_size > 0 ) |
155 | 0 | { |
156 | 0 | if( libfsfat_file_entry_get_utf8_name( |
157 | 0 | root_directory, |
158 | 0 | utf8_name, |
159 | 0 | 512, |
160 | 0 | NULL ) == -1 ) |
161 | 0 | { |
162 | 0 | goto on_error_libfsfat_root_directory; |
163 | 0 | } |
164 | 0 | } |
165 | 97 | if( libfsfat_file_entry_get_utf16_name_size( |
166 | 97 | root_directory, |
167 | 97 | &utf16_name_size, |
168 | 97 | NULL ) == -1 ) |
169 | 0 | { |
170 | 0 | goto on_error_libfsfat_root_directory; |
171 | 0 | } |
172 | 97 | if( utf16_name_size > 0 ) |
173 | 0 | { |
174 | 0 | if( libfsfat_file_entry_get_utf16_name( |
175 | 0 | root_directory, |
176 | 0 | utf16_name, |
177 | 0 | 512, |
178 | 0 | NULL ) == -1 ) |
179 | 0 | { |
180 | 0 | goto on_error_libfsfat_root_directory; |
181 | 0 | } |
182 | 0 | } |
183 | 97 | if( libfsfat_file_entry_get_size( |
184 | 97 | root_directory, |
185 | 97 | &file_entry_size, |
186 | 97 | NULL ) != 1 ) |
187 | 0 | { |
188 | 0 | goto on_error_libfsfat_root_directory; |
189 | 0 | } |
190 | 97 | if( libfsfat_file_entry_get_number_of_extents( |
191 | 97 | root_directory, |
192 | 97 | &number_of_extents, |
193 | 97 | NULL ) != 1 ) |
194 | 97 | { |
195 | 97 | goto on_error_libfsfat_root_directory; |
196 | 97 | } |
197 | 0 | if( number_of_extents > 0 ) |
198 | 0 | { |
199 | 0 | if( libfsfat_file_entry_get_extent_by_index( |
200 | 0 | root_directory, |
201 | 0 | 0, |
202 | 0 | &extent_offset, |
203 | 0 | &file_entry_size, |
204 | 0 | &extent_flags, |
205 | 0 | NULL ) != 1 ) |
206 | 0 | { |
207 | 0 | goto on_error_libfsfat_root_directory; |
208 | 0 | } |
209 | 0 | } |
210 | 0 | if( libfsfat_file_entry_get_number_of_sub_file_entries( |
211 | 0 | root_directory, |
212 | 0 | &number_of_sub_file_entries, |
213 | 0 | NULL ) != 1 ) |
214 | 0 | { |
215 | 0 | goto on_error_libfsfat_root_directory; |
216 | 0 | } |
217 | 0 | for( sub_file_entry_index = 0; |
218 | 0 | ( sub_file_entry_index < number_of_sub_file_entries ) |
219 | 0 | && ( sub_file_entry_index < 8 ); |
220 | 0 | sub_file_entry_index++ ) |
221 | 0 | { |
222 | | /* Limit the number of sub file entries to read |
223 | | */ |
224 | 0 | if( libfsfat_file_entry_get_sub_file_entry_by_index( |
225 | 0 | root_directory, |
226 | 0 | sub_file_entry_index, |
227 | 0 | &file_entry, |
228 | 0 | NULL ) != 1 ) |
229 | 0 | { |
230 | 0 | goto on_error_libfsfat_file_entry; |
231 | 0 | } |
232 | 0 | if( libfsfat_file_entry_get_identifier( |
233 | 0 | file_entry, |
234 | 0 | &identifier, |
235 | 0 | NULL ) != 1 ) |
236 | 0 | { |
237 | 0 | goto on_error_libfsfat_file_entry; |
238 | 0 | } |
239 | 0 | if( libfsfat_file_entry_get_access_time( |
240 | 0 | file_entry, |
241 | 0 | &fat_timestamp, |
242 | 0 | NULL ) == -1 ) |
243 | 0 | { |
244 | 0 | goto on_error_libfsfat_file_entry; |
245 | 0 | } |
246 | 0 | if( libfsfat_file_entry_get_creation_time( |
247 | 0 | file_entry, |
248 | 0 | &fat_timestamp, |
249 | 0 | NULL ) == -1 ) |
250 | 0 | { |
251 | 0 | goto on_error_libfsfat_file_entry; |
252 | 0 | } |
253 | 0 | if( libfsfat_file_entry_get_modification_time( |
254 | 0 | file_entry, |
255 | 0 | &fat_timestamp, |
256 | 0 | NULL ) == -1 ) |
257 | 0 | { |
258 | 0 | goto on_error_libfsfat_file_entry; |
259 | 0 | } |
260 | 0 | if( libfsfat_file_entry_get_file_attribute_flags( |
261 | 0 | file_entry, |
262 | 0 | &file_attribute_flags, |
263 | 0 | NULL ) == -1 ) |
264 | 0 | { |
265 | 0 | goto on_error_libfsfat_file_entry; |
266 | 0 | } |
267 | 0 | if( libfsfat_file_entry_get_utf8_name_size( |
268 | 0 | file_entry, |
269 | 0 | &utf8_name_size, |
270 | 0 | NULL ) == -1 ) |
271 | 0 | { |
272 | 0 | goto on_error_libfsfat_file_entry; |
273 | 0 | } |
274 | 0 | if( utf8_name_size > 0 ) |
275 | 0 | { |
276 | 0 | if( libfsfat_file_entry_get_utf8_name( |
277 | 0 | file_entry, |
278 | 0 | utf8_name, |
279 | 0 | 512, |
280 | 0 | NULL ) == -1 ) |
281 | 0 | { |
282 | 0 | goto on_error_libfsfat_file_entry; |
283 | 0 | } |
284 | 0 | } |
285 | 0 | if( libfsfat_file_entry_get_utf16_name_size( |
286 | 0 | file_entry, |
287 | 0 | &utf16_name_size, |
288 | 0 | NULL ) == -1 ) |
289 | 0 | { |
290 | 0 | goto on_error_libfsfat_file_entry; |
291 | 0 | } |
292 | 0 | if( utf16_name_size > 0 ) |
293 | 0 | { |
294 | 0 | if( libfsfat_file_entry_get_utf16_name( |
295 | 0 | file_entry, |
296 | 0 | utf16_name, |
297 | 0 | 512, |
298 | 0 | NULL ) == -1 ) |
299 | 0 | { |
300 | 0 | goto on_error_libfsfat_file_entry; |
301 | 0 | } |
302 | 0 | } |
303 | 0 | if( libfsfat_file_entry_get_size( |
304 | 0 | file_entry, |
305 | 0 | &file_entry_size, |
306 | 0 | NULL ) != 1 ) |
307 | 0 | { |
308 | 0 | goto on_error_libfsfat_file_entry; |
309 | 0 | } |
310 | 0 | if( libfsfat_file_entry_get_number_of_extents( |
311 | 0 | file_entry, |
312 | 0 | &number_of_extents, |
313 | 0 | NULL ) != 1 ) |
314 | 0 | { |
315 | 0 | goto on_error_libfsfat_file_entry; |
316 | 0 | } |
317 | 0 | if( number_of_extents > 0 ) |
318 | 0 | { |
319 | 0 | if( libfsfat_file_entry_get_extent_by_index( |
320 | 0 | file_entry, |
321 | 0 | 0, |
322 | 0 | &extent_offset, |
323 | 0 | &file_entry_size, |
324 | 0 | &extent_flags, |
325 | 0 | NULL ) != 1 ) |
326 | 0 | { |
327 | 0 | goto on_error_libfsfat_file_entry; |
328 | 0 | } |
329 | 0 | } |
330 | 0 | if( file_entry_size > 0 ) |
331 | 0 | { |
332 | 0 | if( libfsfat_file_entry_read_buffer( |
333 | 0 | file_entry, |
334 | 0 | buffer, |
335 | 0 | 512, |
336 | 0 | NULL ) == -1 ) |
337 | 0 | { |
338 | 0 | goto on_error_libfsfat_file_entry; |
339 | 0 | } |
340 | 0 | for( read_iterator = 0; |
341 | 0 | read_iterator < 128; |
342 | 0 | read_iterator++ ) |
343 | 0 | { |
344 | 0 | if( (off64_t) read_offset >= (off64_t) file_entry_size ) |
345 | 0 | { |
346 | 0 | break; |
347 | 0 | } |
348 | 0 | if( libfsfat_file_entry_read_buffer_at_offset( |
349 | 0 | file_entry, |
350 | 0 | buffer, |
351 | 0 | 497, |
352 | 0 | (off64_t) read_offset, |
353 | 0 | NULL ) == -1 ) |
354 | 0 | { |
355 | 0 | goto on_error_libfsfat_file_entry; |
356 | 0 | } |
357 | 0 | read_offset += 497; |
358 | 0 | } |
359 | 0 | if( libfsfat_file_entry_seek_offset( |
360 | 0 | file_entry, |
361 | 0 | 0, |
362 | 0 | SEEK_SET, |
363 | 0 | NULL ) == -1 ) |
364 | 0 | { |
365 | 0 | goto on_error_libfsfat_file_entry; |
366 | 0 | } |
367 | 0 | if( libfsfat_file_entry_get_offset( |
368 | 0 | file_entry, |
369 | 0 | &read_offset, |
370 | 0 | NULL ) == -1 ) |
371 | 0 | { |
372 | 0 | goto on_error_libfsfat_file_entry; |
373 | 0 | } |
374 | 0 | } |
375 | 0 | if( libfsfat_file_entry_free( |
376 | 0 | &file_entry, |
377 | 0 | NULL ) != 1 ) |
378 | 0 | { |
379 | 0 | goto on_error_libfsfat_root_directory; |
380 | 0 | } |
381 | 0 | } |
382 | 0 | result = libfsfat_file_entry_get_sub_file_entry_by_utf8_name( |
383 | 0 | root_directory, |
384 | 0 | (const uint8_t *) "..", |
385 | 0 | 2, |
386 | 0 | &file_entry, |
387 | 0 | NULL ); |
388 | |
|
389 | 0 | if( result == -1 ) |
390 | 0 | { |
391 | 0 | goto on_error_libfsfat_root_directory; |
392 | 0 | } |
393 | 0 | if( result == 1 ) |
394 | 0 | { |
395 | 0 | if( libfsfat_file_entry_free( |
396 | 0 | &file_entry, |
397 | 0 | NULL ) != 1 ) |
398 | 0 | { |
399 | 0 | goto on_error_libfsfat_root_directory; |
400 | 0 | } |
401 | 0 | } |
402 | 0 | result = libfsfat_file_entry_get_sub_file_entry_by_utf16_name( |
403 | 0 | root_directory, |
404 | 0 | utf16_string, |
405 | 0 | 2, |
406 | 0 | &file_entry, |
407 | 0 | NULL ); |
408 | |
|
409 | 0 | if( result == -1 ) |
410 | 0 | { |
411 | 0 | goto on_error_libfsfat_root_directory; |
412 | 0 | } |
413 | 0 | if( result == 1 ) |
414 | 0 | { |
415 | 0 | if( libfsfat_file_entry_free( |
416 | 0 | &file_entry, |
417 | 0 | NULL ) != 1 ) |
418 | 0 | { |
419 | 0 | goto on_error_libfsfat_root_directory; |
420 | 0 | } |
421 | 0 | } |
422 | 0 | on_error_libfsfat_file_entry: |
423 | 0 | if( file_entry != NULL ) |
424 | 0 | { |
425 | 0 | if( libfsfat_file_entry_free( |
426 | 0 | &file_entry, |
427 | 0 | NULL ) != 1 ) |
428 | 0 | { |
429 | 0 | goto on_error_libfsfat_root_directory; |
430 | 0 | } |
431 | 0 | } |
432 | 97 | on_error_libfsfat_root_directory: |
433 | 97 | if( libfsfat_file_entry_free( |
434 | 97 | &root_directory, |
435 | 97 | NULL ) != 1 ) |
436 | 0 | { |
437 | 0 | goto on_error_libfsfat_volume; |
438 | 0 | } |
439 | 97 | libfsfat_volume_close( |
440 | 97 | volume, |
441 | 97 | NULL ); |
442 | | |
443 | 1.16k | on_error_libfsfat_volume: |
444 | 1.16k | libfsfat_volume_free( |
445 | 1.16k | &volume, |
446 | 1.16k | NULL ); |
447 | | |
448 | 1.16k | on_error_libbfio: |
449 | 1.16k | libbfio_handle_free( |
450 | 1.16k | &file_io_handle, |
451 | 1.16k | NULL ); |
452 | | |
453 | 1.16k | return( 0 ); |
454 | 1.16k | } |
455 | | |
456 | | } /* extern "C" */ |