/src/libsmraw/ossfuzz/handle_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libsmraw handle type |
3 | | * |
4 | | * Copyright (C) 2010-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 <stdlib.h> |
25 | | #include <string.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_libsmraw.h" |
33 | | |
34 | | #if !defined( LIBSMRAW_HAVE_BFIO ) |
35 | | |
36 | | /* Opens a set of storage media RAW files using a Basic File IO (bfio) pool |
37 | | * Returns 1 if successful or -1 on error |
38 | | */ |
39 | | LIBSMRAW_EXTERN \ |
40 | | int libsmraw_handle_open_file_io_pool( |
41 | | libsmraw_handle_t *handle, |
42 | | libbfio_pool_t *file_io_pool, |
43 | | int access_flags, |
44 | | libsmraw_error_t **error ); |
45 | | |
46 | | #endif /* !defined( LIBSMRAW_HAVE_BFIO ) */ |
47 | | |
48 | | int LLVMFuzzerTestOneInput( |
49 | | const uint8_t *data, |
50 | | size_t size ) |
51 | 146 | { |
52 | 146 | uint8_t buffer[ 512 ]; |
53 | | |
54 | 146 | libbfio_handle_t *file_io_handle = NULL; |
55 | 146 | libbfio_pool_t *file_io_pool = NULL; |
56 | 146 | libsmraw_handle_t *handle = NULL; |
57 | 146 | uint8_t *buffer_write = NULL; |
58 | 146 | libbfio_handle_t *file_io_handle_write = NULL; |
59 | 146 | libbfio_pool_t *file_io_pool_write = NULL; |
60 | 146 | libsmraw_handle_t *handle_write = NULL; |
61 | 146 | off64_t media_offset = 0; |
62 | 146 | size64_t media_size = 0; |
63 | 146 | ssize_t read_count = 0; |
64 | 146 | int entry_index = 0; |
65 | 146 | int entry_index_write = 0; |
66 | 146 | int read_iterator = 0; |
67 | 146 | int write_iterator = 0; |
68 | | |
69 | 146 | if( libbfio_memory_range_initialize( |
70 | 146 | &file_io_handle, |
71 | 146 | NULL ) != 1 ) |
72 | 0 | { |
73 | 0 | return( 0 ); |
74 | 0 | } |
75 | 146 | if( libbfio_memory_range_set( |
76 | 146 | file_io_handle, |
77 | 146 | (uint8_t *) data, |
78 | 146 | size, |
79 | 146 | NULL ) != 1 ) |
80 | 0 | { |
81 | 0 | goto on_error_libbfio; |
82 | 0 | } |
83 | 146 | if( libbfio_pool_initialize( |
84 | 146 | &file_io_pool, |
85 | 146 | 0, |
86 | 146 | 0, |
87 | 146 | NULL ) != 1 ) |
88 | 0 | { |
89 | 0 | goto on_error_libbfio; |
90 | 0 | } |
91 | 146 | if( libbfio_pool_append_handle( |
92 | 146 | file_io_pool, |
93 | 146 | &entry_index, |
94 | 146 | file_io_handle, |
95 | 146 | LIBBFIO_OPEN_READ, |
96 | 146 | NULL ) != 1 ) |
97 | 0 | { |
98 | 0 | goto on_error_libbfio; |
99 | 0 | } |
100 | | /* The file IO pool takes over management of the file IO handle |
101 | | */ |
102 | 146 | file_io_handle = NULL; |
103 | | |
104 | 146 | if( libsmraw_handle_initialize( |
105 | 146 | &handle, |
106 | 146 | NULL ) != 1 ) |
107 | 0 | { |
108 | 0 | goto on_error_libbfio; |
109 | 0 | } |
110 | 146 | if( libsmraw_handle_open_file_io_pool( |
111 | 146 | handle, |
112 | 146 | file_io_pool, |
113 | 146 | LIBSMRAW_OPEN_READ, |
114 | 146 | NULL ) != 1 ) |
115 | 0 | { |
116 | 0 | goto on_error_libsmraw; |
117 | 0 | } |
118 | 146 | if( libsmraw_handle_get_media_size( |
119 | 146 | handle, |
120 | 146 | &media_size, |
121 | 146 | NULL ) != 1 ) |
122 | 0 | { |
123 | 0 | goto on_error_libsmraw; |
124 | 0 | } |
125 | 146 | for( read_iterator = 0; |
126 | 7.53k | read_iterator < 128; |
127 | 7.39k | read_iterator++ ) |
128 | 7.49k | { |
129 | 7.49k | if( media_offset >= media_size ) |
130 | 102 | { |
131 | 102 | break; |
132 | 102 | } |
133 | 7.39k | if( libsmraw_handle_read_buffer_at_offset( |
134 | 7.39k | handle, |
135 | 7.39k | buffer, |
136 | 7.39k | 497, |
137 | 7.39k | media_offset, |
138 | 7.39k | NULL ) == -1 ) |
139 | 0 | { |
140 | 0 | goto on_error_libsmraw; |
141 | 0 | } |
142 | 7.39k | media_offset += 497; |
143 | 7.39k | } |
144 | 146 | for( read_iterator = 0; |
145 | 3.17k | read_iterator < 128; |
146 | 3.03k | read_iterator++ ) |
147 | 3.15k | { |
148 | 3.15k | read_count = libsmraw_handle_read_buffer( |
149 | 3.15k | handle, |
150 | 3.15k | buffer, |
151 | 3.15k | 497, |
152 | 3.15k | NULL ); |
153 | | |
154 | 3.15k | if( read_count == -1 ) |
155 | 0 | { |
156 | 0 | goto on_error_libsmraw; |
157 | 0 | } |
158 | 3.15k | if( read_count == 0 ) |
159 | 125 | { |
160 | 125 | break; |
161 | 125 | } |
162 | 3.15k | } |
163 | 146 | if( libsmraw_handle_seek_offset( |
164 | 146 | handle, |
165 | 146 | 0, |
166 | 146 | SEEK_SET, |
167 | 146 | NULL ) == -1 ) |
168 | 0 | { |
169 | 0 | goto on_error_libsmraw; |
170 | 0 | } |
171 | 146 | if( libsmraw_handle_get_offset( |
172 | 146 | handle, |
173 | 146 | &media_offset, |
174 | 146 | NULL ) != 1 ) |
175 | 0 | { |
176 | 0 | goto on_error_libsmraw; |
177 | 0 | } |
178 | 146 | if( libsmraw_handle_signal_abort( |
179 | 146 | handle, |
180 | 146 | NULL ) != 1 ) |
181 | 0 | { |
182 | 0 | goto on_error_libsmraw; |
183 | 0 | } |
184 | 146 | libsmraw_handle_close( |
185 | 146 | handle, |
186 | 146 | NULL ); |
187 | | |
188 | 146 | if( libbfio_memory_range_initialize( |
189 | 146 | &file_io_handle_write, |
190 | 146 | NULL ) != 1 ) |
191 | 0 | { |
192 | 0 | goto on_error_libbfio; |
193 | 0 | } |
194 | | |
195 | | /* Use a copy of the input data so that the fuzzer's input buffer |
196 | | * is not modified when written data is stored in the file IO handle |
197 | | */ |
198 | 146 | buffer_write = (uint8_t *) calloc( 1, size + 1 ); |
199 | | |
200 | 146 | if( buffer_write == NULL ) |
201 | 0 | { |
202 | 0 | goto on_error_libbfio_write; |
203 | 0 | } |
204 | 146 | memcpy( |
205 | 146 | buffer_write, |
206 | 146 | data, |
207 | 146 | size ); |
208 | | |
209 | 146 | if( libbfio_memory_range_set( |
210 | 146 | file_io_handle_write, |
211 | 146 | buffer_write, |
212 | 146 | size, |
213 | 146 | NULL ) != 1 ) |
214 | 0 | { |
215 | 0 | goto on_error_libbfio_write; |
216 | 0 | } |
217 | 146 | if( libbfio_pool_initialize( |
218 | 146 | &file_io_pool_write, |
219 | 146 | 0, |
220 | 146 | 0, |
221 | 146 | NULL ) != 1 ) |
222 | 0 | { |
223 | 0 | goto on_error_libbfio_write; |
224 | 0 | } |
225 | 146 | if( libbfio_pool_append_handle( |
226 | 146 | file_io_pool_write, |
227 | 146 | &entry_index_write, |
228 | 146 | file_io_handle_write, |
229 | 146 | LIBBFIO_OPEN_READ_WRITE, |
230 | 146 | NULL ) != 1 ) |
231 | 0 | { |
232 | 0 | goto on_error_libbfio_write; |
233 | 0 | } |
234 | | /* The file IO pool takes over management of the file IO handle |
235 | | */ |
236 | 146 | file_io_handle_write = NULL; |
237 | | |
238 | 146 | if( libsmraw_handle_initialize( |
239 | 146 | &handle_write, |
240 | 146 | NULL ) != 1 ) |
241 | 0 | { |
242 | 0 | goto on_error_libsmraw_write; |
243 | 0 | } |
244 | 146 | if( libsmraw_handle_open_file_io_pool( |
245 | 146 | handle_write, |
246 | 146 | file_io_pool_write, |
247 | 146 | LIBSMRAW_OPEN_READ_WRITE, |
248 | 146 | NULL ) != 1 ) |
249 | 0 | { |
250 | 0 | goto on_error_libsmraw_write; |
251 | 0 | } |
252 | 146 | if( libsmraw_handle_get_media_size( |
253 | 146 | handle_write, |
254 | 146 | &media_size, |
255 | 146 | NULL ) != 1 ) |
256 | 0 | { |
257 | 0 | goto on_error_libsmraw_write; |
258 | 0 | } |
259 | 146 | media_offset = 0; |
260 | | |
261 | 146 | for( write_iterator = 0; |
262 | 7.43k | write_iterator < 128; |
263 | 7.28k | write_iterator++ ) |
264 | 7.39k | { |
265 | 7.39k | if( media_offset >= media_size ) |
266 | 0 | { |
267 | 0 | break; |
268 | 0 | } |
269 | 7.39k | if( libsmraw_handle_write_buffer_at_offset( |
270 | 7.39k | handle_write, |
271 | 7.39k | buffer, |
272 | 7.39k | 497, |
273 | 7.39k | media_offset, |
274 | 7.39k | NULL ) == -1 ) |
275 | 103 | { |
276 | 103 | goto on_error_libsmraw_write; |
277 | 103 | } |
278 | 7.28k | media_offset += 497; |
279 | 7.28k | } |
280 | 43 | if( media_offset > 0 ) |
281 | 43 | { |
282 | 43 | read_count = libsmraw_handle_read_buffer_at_offset( |
283 | 43 | handle_write, |
284 | 43 | buffer, |
285 | 43 | 497, |
286 | 43 | 0, |
287 | 43 | NULL ); |
288 | | |
289 | 43 | if( read_count == -1 ) |
290 | 0 | { |
291 | 0 | goto on_error_libsmraw_write; |
292 | 0 | } |
293 | 43 | } |
294 | 43 | libsmraw_handle_close( |
295 | 43 | handle_write, |
296 | 43 | NULL ); |
297 | | |
298 | 146 | on_error_libsmraw_write: |
299 | 146 | libsmraw_handle_free( |
300 | 146 | &handle_write, |
301 | 146 | NULL ); |
302 | | |
303 | 146 | on_error_libsmraw: |
304 | 146 | libsmraw_handle_free( |
305 | 146 | &handle, |
306 | 146 | NULL ); |
307 | | |
308 | 146 | on_error_libbfio_write: |
309 | | /* Note that on error the handle still has a reference to file_io_pool |
310 | | * that will be closed. Therefore the file IO pool and handle need to |
311 | | * be freed after closing or freeing the handle. |
312 | | */ |
313 | 146 | if( file_io_pool_write != NULL ) |
314 | 146 | { |
315 | 146 | libbfio_pool_free( |
316 | 146 | &file_io_pool_write, |
317 | 146 | NULL ); |
318 | 146 | } |
319 | 146 | if( file_io_handle_write != NULL ) |
320 | 0 | { |
321 | 0 | libbfio_handle_free( |
322 | 0 | &file_io_handle_write, |
323 | 0 | NULL ); |
324 | 0 | } |
325 | 146 | if( buffer_write != NULL ) |
326 | 146 | { |
327 | 146 | free( |
328 | 146 | buffer_write ); |
329 | 146 | } |
330 | | |
331 | 146 | on_error_libbfio: |
332 | | /* Note that on error the handle still has a reference to file_io_pool |
333 | | * that will be closed. Therefore the file IO pool and handle need to |
334 | | * be freed after closing or freeing the handle. |
335 | | */ |
336 | 146 | if( file_io_pool != NULL ) |
337 | 146 | { |
338 | 146 | libbfio_pool_free( |
339 | 146 | &file_io_pool, |
340 | 146 | NULL ); |
341 | 146 | } |
342 | 146 | if( file_io_handle != NULL ) |
343 | 0 | { |
344 | 0 | libbfio_handle_free( |
345 | 0 | &file_io_handle, |
346 | | NULL ); |
347 | 0 | } |
348 | 146 | return( 0 ); |
349 | 146 | } |
350 | | |
351 | | } /* extern "C" */ |