/src/libolecf/ossfuzz/item_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libolecf item type |
3 | | * |
4 | | * Copyright (C) 2008-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_libbfio.h" |
32 | | #include "ossfuzz_libolecf.h" |
33 | | |
34 | | #if !defined( LIBOLECF_HAVE_BFIO ) |
35 | | |
36 | | /* Opens a file using a Basic File IO (bfio) handle |
37 | | * Returns 1 if successful or -1 on error |
38 | | */ |
39 | | LIBOLECF_EXTERN \ |
40 | | int libolecf_file_open_file_io_handle( |
41 | | libolecf_file_t *file, |
42 | | libbfio_handle_t *file_io_handle, |
43 | | int access_flags, |
44 | | libolecf_error_t **error ); |
45 | | |
46 | | #endif /* !defined( LIBOLECF_HAVE_BFIO ) */ |
47 | | |
48 | | /* Limits the size of the name and query buffers |
49 | | */ |
50 | 1.57k | #define FUZZER_DATA_SIZE_LIMIT ( (size_t) 0x10000 ) |
51 | 624 | #define FUZZER_PATH_SIZE_LIMIT ( (size_t) 0x100 ) |
52 | | |
53 | | /* Limits the number of sub items to query |
54 | | */ |
55 | 771 | #define FUZZER_SUB_ITEM_LIMIT 8 |
56 | | |
57 | | int LLVMFuzzerTestOneInput( |
58 | | const uint8_t *data, |
59 | | size_t size ) |
60 | 1.18k | { |
61 | 1.18k | libolecf_item_t *lookup_item = NULL; |
62 | 1.18k | uint8_t *utf8_name = NULL; |
63 | 1.18k | uint16_t *utf16_name = NULL; |
64 | | |
65 | 1.18k | libbfio_handle_t *file_io_handle = NULL; |
66 | 1.18k | libolecf_file_t *file = NULL; |
67 | 1.18k | libolecf_item_t *item = NULL; |
68 | 1.18k | libolecf_item_t *sub_item = NULL; |
69 | 1.18k | uint8_t item_type = 0; |
70 | 1.18k | uint32_t item_size = 0; |
71 | 1.18k | uint64_t filetime = 0; |
72 | 1.18k | size_t utf8_name_size = 0; |
73 | 1.18k | size_t utf16_name_size = 0; |
74 | 1.18k | size_t name_buffer_size = 0; |
75 | 1.18k | size_t query_size = 0; |
76 | 1.18k | size_t unicode_query_size = 0; |
77 | 1.18k | int number_of_sub_items = 0; |
78 | 1.18k | int result = 0; |
79 | 1.18k | int sub_item_index = 0; |
80 | 1.18k | int sub_number_of_sub_items = 0; |
81 | | |
82 | 1.18k | if( libbfio_memory_range_initialize( |
83 | 1.18k | &file_io_handle, |
84 | 1.18k | NULL ) != 1 ) |
85 | 0 | { |
86 | 0 | return( 0 ); |
87 | 0 | } |
88 | 1.18k | if( libbfio_memory_range_set( |
89 | 1.18k | file_io_handle, |
90 | 1.18k | (uint8_t *) data, |
91 | 1.18k | size, |
92 | 1.18k | NULL ) != 1 ) |
93 | 0 | { |
94 | 0 | goto on_error_libbfio; |
95 | 0 | } |
96 | 1.18k | if( libolecf_file_initialize( |
97 | 1.18k | &file, |
98 | 1.18k | NULL ) != 1 ) |
99 | 0 | { |
100 | 0 | goto on_error_libbfio; |
101 | 0 | } |
102 | 1.18k | if( libolecf_file_open_file_io_handle( |
103 | 1.18k | file, |
104 | 1.18k | file_io_handle, |
105 | 1.18k | LIBOLECF_OPEN_READ, |
106 | 1.18k | NULL ) != 1 ) |
107 | 835 | { |
108 | 835 | goto on_error_libolecf_file; |
109 | 835 | } |
110 | 347 | if( libolecf_file_get_root_item( |
111 | 347 | file, |
112 | 347 | &item, |
113 | 347 | NULL ) == 1 ) |
114 | 312 | { |
115 | 312 | libolecf_item_get_type( |
116 | 312 | item, |
117 | 312 | &item_type, |
118 | 312 | NULL ); |
119 | 312 | libolecf_item_get_size( |
120 | 312 | item, |
121 | 312 | &item_size, |
122 | 312 | NULL ); |
123 | 312 | libolecf_item_get_creation_time( |
124 | 312 | item, |
125 | 312 | &filetime, |
126 | 312 | NULL ); |
127 | 312 | libolecf_item_get_modification_time( |
128 | 312 | item, |
129 | 312 | &filetime, |
130 | 312 | NULL ); |
131 | 312 | result = libolecf_item_get_utf8_name_size( |
132 | 312 | item, |
133 | 312 | &utf8_name_size, |
134 | 312 | NULL ); |
135 | 312 | if( ( result == 1 ) |
136 | 207 | && ( utf8_name_size > 0 ) ) |
137 | 206 | { |
138 | 206 | name_buffer_size = utf8_name_size; |
139 | | |
140 | 206 | if( name_buffer_size > FUZZER_DATA_SIZE_LIMIT ) |
141 | 0 | { |
142 | 0 | name_buffer_size = FUZZER_DATA_SIZE_LIMIT; |
143 | 0 | } |
144 | 206 | utf8_name = (uint8_t *) malloc( name_buffer_size ); |
145 | | |
146 | 206 | if( utf8_name != NULL ) |
147 | 206 | { |
148 | 206 | if( libolecf_item_get_utf8_name( |
149 | 206 | item, |
150 | 206 | utf8_name, |
151 | 206 | name_buffer_size, |
152 | 206 | NULL ) == 1 ) |
153 | 206 | { |
154 | 206 | utf8_name[ name_buffer_size - 1 ] = 0; |
155 | | |
156 | | /* Use the value as a sink to keep it live |
157 | | */ |
158 | 206 | filetime = utf8_name[ 0 ]; |
159 | 206 | } |
160 | 206 | free( utf8_name ); |
161 | 206 | utf8_name = NULL; |
162 | 206 | } |
163 | 206 | } |
164 | 312 | result = libolecf_item_get_utf16_name_size( |
165 | 312 | item, |
166 | 312 | &utf16_name_size, |
167 | 312 | NULL ); |
168 | 312 | if( ( result == 1 ) |
169 | 207 | && ( utf16_name_size > 0 ) ) |
170 | 206 | { |
171 | 206 | name_buffer_size = utf16_name_size; |
172 | | |
173 | 206 | if( name_buffer_size > FUZZER_DATA_SIZE_LIMIT ) |
174 | 0 | { |
175 | 0 | name_buffer_size = FUZZER_DATA_SIZE_LIMIT; |
176 | 0 | } |
177 | | /* The size is the number of UTF-16 characters, |
178 | | * including the end of string character |
179 | | */ |
180 | 206 | utf16_name = (uint16_t *) malloc( name_buffer_size * sizeof( uint16_t ) ); |
181 | | |
182 | 206 | if( utf16_name != NULL ) |
183 | 206 | { |
184 | 206 | if( libolecf_item_get_utf16_name( |
185 | 206 | item, |
186 | 206 | utf16_name, |
187 | 206 | name_buffer_size, |
188 | 206 | NULL ) == 1 ) |
189 | 206 | { |
190 | 206 | utf16_name[ name_buffer_size - 1 ] = 0; |
191 | | |
192 | | /* Use the value as a sink to keep it live |
193 | | */ |
194 | 206 | filetime = (uint64_t) utf16_name[ 0 ]; |
195 | 206 | } |
196 | 206 | free( utf16_name ); |
197 | 206 | utf16_name = NULL; |
198 | 206 | } |
199 | 206 | } |
200 | 312 | if( libolecf_item_get_number_of_sub_items( |
201 | 312 | item, |
202 | 312 | &number_of_sub_items, |
203 | 312 | NULL ) == 1 ) |
204 | 312 | { |
205 | | /* Query the sub items |
206 | | */ |
207 | 312 | for( sub_item_index = 0; |
208 | 1.04k | sub_item_index < number_of_sub_items; |
209 | 736 | sub_item_index++ ) |
210 | 771 | { |
211 | 771 | utf8_name_size = 0; |
212 | 771 | utf16_name_size = 0; |
213 | | |
214 | 771 | if( sub_item_index >= FUZZER_SUB_ITEM_LIMIT ) |
215 | 35 | { |
216 | 35 | break; |
217 | 35 | } |
218 | 736 | if( libolecf_item_get_sub_item( |
219 | 736 | item, |
220 | 736 | sub_item_index, |
221 | 736 | &sub_item, |
222 | 736 | NULL ) != 1 ) |
223 | 0 | { |
224 | 0 | break; |
225 | 0 | } |
226 | 736 | libolecf_item_get_type( |
227 | 736 | sub_item, |
228 | 736 | &item_type, |
229 | 736 | NULL ); |
230 | 736 | libolecf_item_get_size( |
231 | 736 | sub_item, |
232 | 736 | &item_size, |
233 | 736 | NULL ); |
234 | 736 | libolecf_item_get_creation_time( |
235 | 736 | sub_item, |
236 | 736 | &filetime, |
237 | 736 | NULL ); |
238 | 736 | libolecf_item_get_modification_time( |
239 | 736 | sub_item, |
240 | 736 | &filetime, |
241 | 736 | NULL ); |
242 | 736 | result = libolecf_item_get_utf8_name_size( |
243 | 736 | sub_item, |
244 | 736 | &utf8_name_size, |
245 | 736 | NULL ); |
246 | 736 | if( ( result == 1 ) |
247 | 590 | && ( utf8_name_size > 0 ) ) |
248 | 582 | { |
249 | 582 | name_buffer_size = utf8_name_size; |
250 | | |
251 | 582 | if( name_buffer_size > FUZZER_DATA_SIZE_LIMIT ) |
252 | 0 | { |
253 | 0 | name_buffer_size = FUZZER_DATA_SIZE_LIMIT; |
254 | 0 | } |
255 | 582 | utf8_name = (uint8_t *) malloc( name_buffer_size ); |
256 | | |
257 | 582 | if( utf8_name != NULL ) |
258 | 582 | { |
259 | 582 | if( libolecf_item_get_utf8_name( |
260 | 582 | sub_item, |
261 | 582 | utf8_name, |
262 | 582 | name_buffer_size, |
263 | 582 | NULL ) == 1 ) |
264 | 582 | { |
265 | 582 | utf8_name[ name_buffer_size - 1 ] = 0; |
266 | | |
267 | | /* Use the value as a sink |
268 | | * to keep it live |
269 | | */ |
270 | 582 | filetime = utf8_name[ 0 ]; |
271 | 582 | } |
272 | 582 | free( utf8_name ); |
273 | 582 | utf8_name = NULL; |
274 | 582 | } |
275 | 582 | } |
276 | 736 | result = libolecf_item_get_utf16_name_size( |
277 | 736 | sub_item, |
278 | 736 | &utf16_name_size, |
279 | 736 | NULL ); |
280 | 736 | if( ( result == 1 ) |
281 | 590 | && ( utf16_name_size > 0 ) ) |
282 | 582 | { |
283 | 582 | name_buffer_size = utf16_name_size; |
284 | | |
285 | 582 | if( name_buffer_size > FUZZER_DATA_SIZE_LIMIT ) |
286 | 0 | { |
287 | 0 | name_buffer_size = FUZZER_DATA_SIZE_LIMIT; |
288 | 0 | } |
289 | | /* The size is the number of UTF-16 |
290 | | * characters, including the end of |
291 | | * string character |
292 | | */ |
293 | 582 | utf16_name = (uint16_t *) malloc( name_buffer_size * sizeof( uint16_t ) ); |
294 | | |
295 | 582 | if( utf16_name != NULL ) |
296 | 582 | { |
297 | 582 | if( libolecf_item_get_utf16_name( |
298 | 582 | sub_item, |
299 | 582 | utf16_name, |
300 | 582 | name_buffer_size, |
301 | 582 | NULL ) == 1 ) |
302 | 582 | { |
303 | 582 | utf16_name[ name_buffer_size - 1 ] = 0; |
304 | | |
305 | | /* Use the value as a sink |
306 | | * to keep it live |
307 | | */ |
308 | 582 | filetime = (uint64_t) utf16_name[ 0 ]; |
309 | 582 | } |
310 | 582 | free( utf16_name ); |
311 | 582 | utf16_name = NULL; |
312 | 582 | } |
313 | 582 | } |
314 | | /* Query the number of sub items |
315 | | */ |
316 | 736 | sub_number_of_sub_items = 0; |
317 | 736 | libolecf_item_get_number_of_sub_items( |
318 | 736 | sub_item, |
319 | 736 | &sub_number_of_sub_items, |
320 | 736 | NULL ); |
321 | 736 | libolecf_item_free( |
322 | 736 | &sub_item, |
323 | 736 | NULL ); |
324 | 736 | } |
325 | | /* Attempt to retrieve a sub item out of range |
326 | | * to trigger the error handler |
327 | | */ |
328 | 312 | sub_item = NULL; |
329 | 312 | libolecf_item_get_sub_item( |
330 | 312 | item, |
331 | 312 | number_of_sub_items, |
332 | 312 | &sub_item, |
333 | 312 | NULL ); |
334 | 312 | if( sub_item != NULL ) |
335 | 0 | { |
336 | 0 | libolecf_item_free( |
337 | 0 | &sub_item, |
338 | 0 | NULL ); |
339 | 0 | } |
340 | 312 | } |
341 | | /* Look up sub items by name and path using the fuzzer |
342 | | * data as the query strings |
343 | | */ |
344 | 312 | if( ( data != NULL ) |
345 | 312 | && ( size > 0 ) ) |
346 | 312 | { |
347 | 312 | query_size = size; |
348 | | |
349 | 312 | if( query_size > FUZZER_PATH_SIZE_LIMIT ) |
350 | 312 | { |
351 | 312 | query_size = FUZZER_PATH_SIZE_LIMIT; |
352 | 312 | } |
353 | 312 | unicode_query_size = query_size / 2; |
354 | 312 | lookup_item = NULL; |
355 | 312 | libolecf_item_get_sub_item_by_utf8_name( |
356 | 312 | item, |
357 | 312 | data, |
358 | 312 | query_size, |
359 | 312 | &lookup_item, |
360 | 312 | NULL ); |
361 | 312 | if( lookup_item != NULL ) |
362 | 0 | { |
363 | 0 | libolecf_item_free( |
364 | 0 | &lookup_item, |
365 | 0 | NULL ); |
366 | 0 | } |
367 | 312 | lookup_item = NULL; |
368 | 312 | libolecf_item_get_sub_item_by_utf8_path( |
369 | 312 | item, |
370 | 312 | data, |
371 | 312 | query_size, |
372 | 312 | &lookup_item, |
373 | 312 | NULL ); |
374 | 312 | if( lookup_item != NULL ) |
375 | 0 | { |
376 | 0 | libolecf_item_free( |
377 | 0 | &lookup_item, |
378 | 0 | NULL ); |
379 | 0 | } |
380 | 312 | lookup_item = NULL; |
381 | 312 | if( unicode_query_size > 0 ) |
382 | 312 | { |
383 | 312 | libolecf_item_get_sub_item_by_utf16_name( |
384 | 312 | item, |
385 | 312 | (const uint16_t *) data, |
386 | 312 | unicode_query_size, |
387 | 312 | &lookup_item, |
388 | 312 | NULL ); |
389 | 312 | if( lookup_item != NULL ) |
390 | 0 | { |
391 | 0 | libolecf_item_free( |
392 | 0 | &lookup_item, |
393 | 0 | NULL ); |
394 | 0 | } |
395 | 312 | lookup_item = NULL; |
396 | 312 | libolecf_item_get_sub_item_by_utf16_path( |
397 | 312 | item, |
398 | 312 | (const uint16_t *) data, |
399 | 312 | unicode_query_size, |
400 | 312 | &lookup_item, |
401 | 312 | NULL ); |
402 | 312 | if( lookup_item != NULL ) |
403 | 0 | { |
404 | 0 | libolecf_item_free( |
405 | 0 | &lookup_item, |
406 | 0 | NULL ); |
407 | 0 | } |
408 | 312 | } |
409 | 312 | } |
410 | 312 | libolecf_item_free( |
411 | 312 | &item, |
412 | 312 | NULL ); |
413 | 312 | } |
414 | 347 | libolecf_file_close( |
415 | 347 | file, |
416 | 347 | NULL ); |
417 | | |
418 | 1.18k | on_error_libolecf_file: |
419 | 1.18k | libolecf_file_free( |
420 | 1.18k | &file, |
421 | 1.18k | NULL ); |
422 | | |
423 | 1.18k | on_error_libbfio: |
424 | 1.18k | libbfio_handle_free( |
425 | 1.18k | &file_io_handle, |
426 | 1.18k | NULL ); |
427 | | |
428 | 1.18k | return( 0 ); |
429 | 1.18k | } |
430 | | |
431 | | } /* extern "C" */ |