/src/libmdmp/ossfuzz/file_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libmdmp file type |
3 | | * |
4 | | * Copyright (C) 2014-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 <stdio.h> |
25 | | |
26 | | /* Note that some of the OSS-Fuzz engines use C++ |
27 | | */ |
28 | | extern "C" { |
29 | | |
30 | | #include "ossfuzz_libbfio.h" |
31 | | #include "ossfuzz_libmdmp.h" |
32 | | |
33 | | #if !defined( LIBMDMP_HAVE_BFIO ) |
34 | | |
35 | | /* Determines if a file contains a MDMP file signature using a Basic File IO (bfio) handle |
36 | | * Returns 1 if true, 0 if not or -1 on error |
37 | | */ |
38 | | LIBMDMP_EXTERN \ |
39 | | int libmdmp_check_file_signature_file_io_handle( |
40 | | libbfio_handle_t *file_io_handle, |
41 | | libmdmp_error_t **error ); |
42 | | |
43 | | /* Opens a file using a Basic File IO (bfio) handle |
44 | | * Returns 1 if successful or -1 on error |
45 | | */ |
46 | | LIBMDMP_EXTERN \ |
47 | | int libmdmp_file_open_file_io_handle( |
48 | | libmdmp_file_t *file, |
49 | | libbfio_handle_t *file_io_handle, |
50 | | int access_flags, |
51 | | libmdmp_error_t **error ); |
52 | | |
53 | | /* Retrieves the stream data file io handle |
54 | | * Returns 1 if successful -1 on error |
55 | | */ |
56 | | LIBMDMP_EXTERN \ |
57 | | int libmdmp_stream_get_data_file_io_handle( |
58 | | libmdmp_stream_t *stream, |
59 | | libbfio_handle_t **file_io_handle, |
60 | | libmdmp_error_t **error ); |
61 | | |
62 | | #endif /* !defined( LIBMDMP_HAVE_BFIO ) */ |
63 | | |
64 | | /* The maximum number of streams to exercise per input, |
65 | | * to bound the execution time |
66 | | */ |
67 | 310 | #define MAXIMUM_NUMBER_OF_STREAMS 10 |
68 | | |
69 | | /* The size of the read buffer used to exercise the streams |
70 | | */ |
71 | 9.11k | #define READ_BUFFER_SIZE 4096 |
72 | | |
73 | | /* Exercises the stream functions |
74 | | */ |
75 | | static void fuzz_stream( |
76 | | libmdmp_stream_t *stream, |
77 | | size_t size ) |
78 | 1.82k | { |
79 | 1.82k | uint8_t read_buffer[ READ_BUFFER_SIZE ]; |
80 | | |
81 | 1.82k | libbfio_handle_t *stream_file_io_handle = NULL; |
82 | 1.82k | size64_t stream_file_io_handle_size = 0; |
83 | 1.82k | size64_t stream_size = 0; |
84 | 1.82k | uint32_t stream_type = 0; |
85 | 1.82k | off64_t stream_offset = 0; |
86 | 1.82k | int result = 0; |
87 | | |
88 | 1.82k | libmdmp_stream_get_type( |
89 | 1.82k | stream, |
90 | 1.82k | &stream_type, |
91 | 1.82k | NULL ); |
92 | | |
93 | 1.82k | libmdmp_stream_get_size( |
94 | 1.82k | stream, |
95 | 1.82k | &stream_size, |
96 | 1.82k | NULL ); |
97 | | |
98 | 1.82k | libmdmp_stream_get_offset( |
99 | 1.82k | stream, |
100 | 1.82k | &stream_offset, |
101 | 1.82k | NULL ); |
102 | | |
103 | 1.82k | libmdmp_stream_get_start_offset( |
104 | 1.82k | stream, |
105 | 1.82k | &stream_offset, |
106 | 1.82k | NULL ); |
107 | | |
108 | 1.82k | libmdmp_stream_read_buffer( |
109 | 1.82k | stream, |
110 | 1.82k | read_buffer, |
111 | 1.82k | READ_BUFFER_SIZE, |
112 | 1.82k | NULL ); |
113 | | |
114 | 1.82k | libmdmp_stream_get_offset( |
115 | 1.82k | stream, |
116 | 1.82k | &stream_offset, |
117 | 1.82k | NULL ); |
118 | | |
119 | 1.82k | libmdmp_stream_seek_offset( |
120 | 1.82k | stream, |
121 | 1.82k | 0, |
122 | 1.82k | SEEK_SET, |
123 | 1.82k | NULL ); |
124 | | |
125 | 1.82k | libmdmp_stream_seek_offset( |
126 | 1.82k | stream, |
127 | 1.82k | 1, |
128 | 1.82k | SEEK_CUR, |
129 | 1.82k | NULL ); |
130 | | |
131 | 1.82k | libmdmp_stream_seek_offset( |
132 | 1.82k | stream, |
133 | 1.82k | 0, |
134 | 1.82k | SEEK_END, |
135 | 1.82k | NULL ); |
136 | | |
137 | 1.82k | libmdmp_stream_read_buffer_at_offset( |
138 | 1.82k | stream, |
139 | 1.82k | read_buffer, |
140 | 1.82k | READ_BUFFER_SIZE, |
141 | 1.82k | ( off64_t ) ( size + 1 ), |
142 | 1.82k | NULL ); |
143 | | |
144 | 1.82k | result = libmdmp_stream_get_data_file_io_handle( |
145 | 1.82k | stream, |
146 | 1.82k | &stream_file_io_handle, |
147 | 1.82k | NULL ); |
148 | | |
149 | 1.82k | if( ( result == 1 ) |
150 | 1.82k | && ( stream_file_io_handle != NULL ) ) |
151 | 1.82k | { |
152 | 1.82k | libbfio_handle_exists( |
153 | 1.82k | stream_file_io_handle, |
154 | 1.82k | NULL ); |
155 | | |
156 | 1.82k | libbfio_handle_get_size( |
157 | 1.82k | stream_file_io_handle, |
158 | 1.82k | &stream_file_io_handle_size, |
159 | 1.82k | NULL ); |
160 | | |
161 | 1.82k | libbfio_handle_seek_offset( |
162 | 1.82k | stream_file_io_handle, |
163 | 1.82k | 0, |
164 | 1.82k | SEEK_SET, |
165 | 1.82k | NULL ); |
166 | | |
167 | 1.82k | libbfio_handle_seek_offset( |
168 | 1.82k | stream_file_io_handle, |
169 | 1.82k | 1, |
170 | 1.82k | SEEK_CUR, |
171 | 1.82k | NULL ); |
172 | | |
173 | 1.82k | libbfio_handle_seek_offset( |
174 | 1.82k | stream_file_io_handle, |
175 | 1.82k | 0, |
176 | 1.82k | SEEK_END, |
177 | 1.82k | NULL ); |
178 | | |
179 | 1.82k | libbfio_handle_read_buffer( |
180 | 1.82k | stream_file_io_handle, |
181 | 1.82k | read_buffer, |
182 | 1.82k | READ_BUFFER_SIZE, |
183 | 1.82k | NULL ); |
184 | | |
185 | 1.82k | libbfio_handle_read_buffer_at_offset( |
186 | 1.82k | stream_file_io_handle, |
187 | 1.82k | read_buffer, |
188 | 1.82k | READ_BUFFER_SIZE, |
189 | 1.82k | ( off64_t ) ( size + 1 ), |
190 | 1.82k | NULL ); |
191 | | |
192 | 1.82k | libbfio_handle_write_buffer_at_offset( |
193 | 1.82k | stream_file_io_handle, |
194 | 1.82k | read_buffer, |
195 | 1.82k | READ_BUFFER_SIZE, |
196 | 1.82k | 0, |
197 | 1.82k | NULL ); |
198 | | |
199 | 1.82k | libbfio_handle_free( |
200 | 1.82k | &stream_file_io_handle, |
201 | 1.82k | NULL ); |
202 | 1.82k | } |
203 | 1.82k | } |
204 | | |
205 | | /* Exercises the file functions |
206 | | */ |
207 | | void fuzz_file( |
208 | | libmdmp_file_t *file, |
209 | | size_t size ) |
210 | 310 | { |
211 | 310 | uint8_t read_buffer[ 128 ]; |
212 | | |
213 | 310 | static const uint32_t requested_stream_types[ 8 ] = { |
214 | 310 | 0x00000003, /* Thread list stream */ |
215 | 310 | 0x00000004, /* Exception stream */ |
216 | 310 | 0x00000006, /* System process info stream */ |
217 | 310 | 0x00000007, /* Thread contexts stream */ |
218 | 310 | 0x00010000, /* Module list stream */ |
219 | 310 | 0x00100002, /* Memory 64 list stream */ |
220 | 310 | 0x00100003, /* Memory ranges list stream */ |
221 | 310 | 0xffffffff |
222 | 310 | }; |
223 | 310 | libmdmp_stream_t *stream = NULL; |
224 | 310 | ssize_t read_count = 0; |
225 | 310 | int number_of_streams = 0; |
226 | 310 | int maximum_number_of_streams = MAXIMUM_NUMBER_OF_STREAMS; |
227 | 310 | int requested_stream_type_index = 0; |
228 | 310 | int stream_index = 0; |
229 | | |
230 | 310 | libmdmp_file_get_number_of_streams( |
231 | 310 | file, |
232 | 310 | &number_of_streams, |
233 | 310 | NULL ); |
234 | | |
235 | 310 | if( number_of_streams > maximum_number_of_streams ) |
236 | 116 | { |
237 | 116 | number_of_streams = maximum_number_of_streams; |
238 | 116 | } |
239 | 310 | for( stream_index = 0; |
240 | 2.13k | stream_index < number_of_streams; |
241 | 1.82k | stream_index++ ) |
242 | 1.82k | { |
243 | 1.82k | stream = NULL; |
244 | | |
245 | 1.82k | if( libmdmp_file_get_stream( |
246 | 1.82k | file, |
247 | 1.82k | stream_index, |
248 | 1.82k | &stream, |
249 | 1.82k | NULL ) != 1 ) |
250 | 0 | { |
251 | 0 | break; |
252 | 0 | } |
253 | 1.82k | fuzz_stream( |
254 | 1.82k | stream, |
255 | 1.82k | size ); |
256 | | |
257 | 1.82k | if( stream != NULL ) |
258 | 1.82k | { |
259 | 1.82k | libmdmp_stream_free( |
260 | 1.82k | &stream, |
261 | 1.82k | NULL ); |
262 | 1.82k | } |
263 | 1.82k | } |
264 | 310 | libmdmp_file_signal_abort( |
265 | 310 | file, |
266 | 310 | NULL ); |
267 | | |
268 | 310 | for( requested_stream_type_index = 0; |
269 | 2.79k | requested_stream_type_index < (int) ( sizeof( requested_stream_types ) / sizeof( uint32_t ) ); |
270 | 2.48k | requested_stream_type_index++ ) |
271 | 2.48k | { |
272 | 2.48k | stream = NULL; |
273 | 2.48k | read_count = 0; |
274 | | |
275 | | /* The return value can be 1 if a stream was found, |
276 | | * 0 if no such stream or -1 on error |
277 | | */ |
278 | 2.48k | if( libmdmp_file_get_stream_by_type( |
279 | 2.48k | file, |
280 | 2.48k | requested_stream_types[ requested_stream_type_index ], |
281 | 2.48k | &stream, |
282 | 2.48k | NULL ) != 1 ) |
283 | 2.20k | { |
284 | 2.20k | continue; |
285 | 2.20k | } |
286 | 271 | if( stream != NULL ) |
287 | 271 | { |
288 | 271 | read_count = libmdmp_stream_read_buffer( |
289 | 271 | stream, |
290 | 271 | read_buffer, |
291 | 271 | sizeof( read_buffer ), |
292 | 271 | NULL ); |
293 | | |
294 | 271 | if( read_count > 0 ) |
295 | 92 | { |
296 | | /* Prevent the buffer from being flagged as unused |
297 | | */ |
298 | 92 | ( void ) read_buffer[ 0 ]; |
299 | 92 | } |
300 | 271 | libmdmp_stream_free( |
301 | 271 | &stream, |
302 | 271 | NULL ); |
303 | 271 | } |
304 | 271 | } |
305 | 310 | } |
306 | | |
307 | | int LLVMFuzzerTestOneInput( |
308 | | const uint8_t *data, |
309 | | size_t size ) |
310 | 2.38k | { |
311 | 2.38k | libbfio_handle_t *file_io_handle = NULL; |
312 | 2.38k | libmdmp_file_t *file = NULL; |
313 | 2.38k | libmdmp_error_t *error = NULL; |
314 | 2.38k | char error_string[ 257 ]; |
315 | 2.38k | int codepage = 0; |
316 | | |
317 | 2.38k | libmdmp_notify_set_verbose( |
318 | 2.38k | 0 ); |
319 | | |
320 | 2.38k | libmdmp_get_version(); |
321 | | |
322 | 2.38k | libmdmp_get_access_flags_read(); |
323 | | |
324 | 2.38k | libmdmp_set_codepage( |
325 | 2.38k | 1252, |
326 | 2.38k | NULL ); |
327 | | |
328 | 2.38k | libmdmp_get_codepage( |
329 | 2.38k | &codepage, |
330 | 2.38k | NULL ); |
331 | | |
332 | 2.38k | libmdmp_set_codepage( |
333 | 2.38k | 0, |
334 | 2.38k | NULL ); |
335 | | |
336 | 2.38k | if( libbfio_memory_range_initialize( |
337 | 2.38k | &file_io_handle, |
338 | 2.38k | NULL ) != 1 ) |
339 | 0 | { |
340 | 0 | return( 0 ); |
341 | 0 | } |
342 | 2.38k | if( libbfio_memory_range_set( |
343 | 2.38k | file_io_handle, |
344 | 2.38k | (uint8_t *) data, |
345 | 2.38k | size, |
346 | 2.38k | NULL ) != 1 ) |
347 | 0 | { |
348 | 0 | goto on_error_libbfio; |
349 | 0 | } |
350 | 2.38k | libmdmp_check_file_signature_file_io_handle( |
351 | 2.38k | file_io_handle, |
352 | 2.38k | NULL ); |
353 | | |
354 | 2.38k | if( libmdmp_file_initialize( |
355 | 2.38k | &file, |
356 | 2.38k | NULL ) != 1 ) |
357 | 0 | { |
358 | 0 | goto on_error_libbfio; |
359 | 0 | } |
360 | 2.38k | if( libmdmp_file_open_file_io_handle( |
361 | 2.38k | file, |
362 | 2.38k | file_io_handle, |
363 | 2.38k | LIBMDMP_OPEN_READ, |
364 | 2.38k | &error ) != 1 ) |
365 | 1.67k | { |
366 | 1.67k | libmdmp_error_sprint( |
367 | 1.67k | error, |
368 | 1.67k | error_string, |
369 | 1.67k | sizeof( error_string ) ); |
370 | | |
371 | 1.67k | libmdmp_error_backtrace_sprint( |
372 | 1.67k | error, |
373 | 1.67k | error_string, |
374 | 1.67k | sizeof( error_string ) ); |
375 | | |
376 | 1.67k | libmdmp_error_fprint( |
377 | 1.67k | error, |
378 | 1.67k | NULL ); |
379 | | |
380 | 1.67k | libmdmp_error_backtrace_fprint( |
381 | 1.67k | error, |
382 | 1.67k | NULL ); |
383 | | |
384 | 1.67k | libmdmp_error_free( |
385 | 1.67k | &error ); |
386 | | |
387 | 1.67k | goto on_error_libmdmp; |
388 | 1.67k | } |
389 | 714 | fuzz_file( |
390 | 714 | file, |
391 | 714 | size ); |
392 | | |
393 | 714 | libmdmp_file_close( |
394 | 714 | file, |
395 | 714 | NULL ); |
396 | | |
397 | 2.38k | on_error_libmdmp: |
398 | 2.38k | libmdmp_file_free( |
399 | 2.38k | &file, |
400 | 2.38k | NULL ); |
401 | | |
402 | 2.38k | on_error_libbfio: |
403 | 2.38k | libbfio_handle_free( |
404 | 2.38k | &file_io_handle, |
405 | 2.38k | NULL ); |
406 | | |
407 | 2.38k | return( 0 ); |
408 | 2.38k | } |
409 | | |
410 | | } /* extern "C" */ |