/src/libewf/ossfuzz/file_entry_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libewf file entry type |
3 | | * |
4 | | * Copyright (C) 2006-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_libewf.h" |
31 | | |
32 | | #if !defined( LIBEWF_HAVE_BFIO ) |
33 | | |
34 | | /* Opens a set of EWF file(s) using a Basic File IO (bfio) pool |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | LIBEWF_EXTERN \ |
38 | | int libewf_handle_open_file_io_pool( |
39 | | libewf_handle_t *handle, |
40 | | libbfio_pool_t *file_io_pool, |
41 | | int access_flags, |
42 | | libewf_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBEWF_HAVE_BFIO ) */ |
45 | | |
46 | | static void get_file_entry_values( |
47 | | libewf_file_entry_t *file_entry ) |
48 | 0 | { |
49 | 0 | uint8_t buffer[ 512 ]; |
50 | 0 | uint8_t string[ 64 ]; |
51 | 0 | libewf_extent_t *extent = NULL; |
52 | 0 | int64_t posix_time = 0; |
53 | 0 | size64_t extent_size = 0; |
54 | 0 | size64_t size64_value = 0; |
55 | 0 | size_t string_size = 0; |
56 | 0 | uint64_t identifier = 0; |
57 | 0 | off64_t extent_offset = 0; |
58 | 0 | off64_t duplicate_offset = 0; |
59 | 0 | uint8_t type_8bit = 0; |
60 | 0 | uint32_t extent_flags = 0; |
61 | 0 | uint32_t flags = 0; |
62 | 0 | int number_of_sub_file_entries = 0; |
63 | 0 | int number_of_extents = 0; |
64 | 0 | int number_of_attributes = 0; |
65 | 0 | int number_of_access_control_entries = 0; |
66 | |
|
67 | 0 | if( file_entry == NULL ) |
68 | 0 | { |
69 | 0 | return; |
70 | 0 | } |
71 | 0 | libewf_file_entry_get_identifier( |
72 | 0 | file_entry, |
73 | 0 | &identifier, |
74 | 0 | NULL ); |
75 | 0 | libewf_file_entry_get_type( |
76 | 0 | file_entry, |
77 | 0 | &type_8bit, |
78 | 0 | NULL ); |
79 | 0 | libewf_file_entry_get_flags( |
80 | 0 | file_entry, |
81 | 0 | &flags, |
82 | 0 | NULL ); |
83 | 0 | libewf_file_entry_get_duplicate_media_data_offset( |
84 | 0 | file_entry, |
85 | 0 | &duplicate_offset, |
86 | 0 | NULL ); |
87 | 0 | if( libewf_file_entry_get_utf8_name_size( |
88 | 0 | file_entry, |
89 | 0 | &string_size, |
90 | 0 | NULL ) == 1 ) |
91 | 0 | { |
92 | 0 | if( string_size > 64 ) |
93 | 0 | { |
94 | 0 | string_size = 64; |
95 | 0 | } |
96 | 0 | libewf_file_entry_get_utf8_name( |
97 | 0 | file_entry, |
98 | 0 | string, |
99 | 0 | string_size, |
100 | 0 | NULL ); |
101 | 0 | } |
102 | 0 | if( libewf_file_entry_get_utf16_name_size( |
103 | 0 | file_entry, |
104 | 0 | &string_size, |
105 | 0 | NULL ) == 1 ) |
106 | 0 | { |
107 | 0 | if( string_size > 64 ) |
108 | 0 | { |
109 | 0 | string_size = 64; |
110 | 0 | } |
111 | 0 | libewf_file_entry_get_utf16_name( |
112 | 0 | file_entry, |
113 | 0 | (uint16_t *) string, |
114 | 0 | string_size, |
115 | 0 | NULL ); |
116 | 0 | } |
117 | 0 | libewf_file_entry_get_utf8_short_name_size( |
118 | 0 | file_entry, |
119 | 0 | &string_size, |
120 | 0 | NULL ); |
121 | 0 | libewf_file_entry_get_utf8_short_name( |
122 | 0 | file_entry, |
123 | 0 | string, |
124 | 0 | 64, |
125 | 0 | NULL ); |
126 | 0 | libewf_file_entry_get_utf16_short_name_size( |
127 | 0 | file_entry, |
128 | 0 | &string_size, |
129 | 0 | NULL ); |
130 | 0 | libewf_file_entry_get_utf16_short_name( |
131 | 0 | file_entry, |
132 | 0 | (uint16_t *) string, |
133 | 0 | 64, |
134 | 0 | NULL ); |
135 | 0 | libewf_file_entry_get_size( |
136 | 0 | file_entry, |
137 | 0 | &size64_value, |
138 | 0 | NULL ); |
139 | 0 | libewf_file_entry_get_creation_time( |
140 | 0 | file_entry, |
141 | 0 | &posix_time, |
142 | 0 | NULL ); |
143 | 0 | libewf_file_entry_get_modification_time( |
144 | 0 | file_entry, |
145 | 0 | &posix_time, |
146 | 0 | NULL ); |
147 | 0 | libewf_file_entry_get_access_time( |
148 | 0 | file_entry, |
149 | 0 | &posix_time, |
150 | 0 | NULL ); |
151 | 0 | libewf_file_entry_get_entry_modification_time( |
152 | 0 | file_entry, |
153 | 0 | &posix_time, |
154 | 0 | NULL ); |
155 | 0 | libewf_file_entry_get_deletion_time( |
156 | 0 | file_entry, |
157 | 0 | &posix_time, |
158 | 0 | NULL ); |
159 | 0 | libewf_file_entry_get_utf8_hash_value_md5( |
160 | 0 | file_entry, |
161 | 0 | string, |
162 | 0 | 64, |
163 | 0 | NULL ); |
164 | 0 | libewf_file_entry_get_utf8_hash_value_sha1( |
165 | 0 | file_entry, |
166 | 0 | string, |
167 | 0 | 64, |
168 | 0 | NULL ); |
169 | 0 | libewf_file_entry_read_buffer( |
170 | 0 | file_entry, |
171 | 0 | buffer, |
172 | 0 | 497, |
173 | 0 | NULL ); |
174 | 0 | if( libewf_file_entry_get_number_of_sub_file_entries( |
175 | 0 | file_entry, |
176 | 0 | &number_of_sub_file_entries, |
177 | 0 | NULL ) == 1 && number_of_sub_file_entries > 0 ) |
178 | 0 | { |
179 | 0 | libewf_file_entry_t *sub_file_entry = NULL; |
180 | |
|
181 | 0 | if( libewf_file_entry_get_sub_file_entry( |
182 | 0 | file_entry, |
183 | 0 | 0, |
184 | 0 | &sub_file_entry, |
185 | 0 | NULL ) == 1 && sub_file_entry != NULL ) |
186 | 0 | { |
187 | 0 | get_file_entry_values( |
188 | 0 | sub_file_entry ); |
189 | 0 | libewf_file_entry_free( |
190 | 0 | &sub_file_entry, |
191 | 0 | NULL ); |
192 | 0 | } |
193 | 0 | libewf_file_entry_get_sub_file_entry_by_utf8_name( |
194 | 0 | file_entry, |
195 | 0 | (uint8_t *) "case_data", |
196 | 0 | 9, |
197 | 0 | &sub_file_entry, |
198 | 0 | NULL ); |
199 | |
|
200 | 0 | if( sub_file_entry != NULL ) |
201 | 0 | { |
202 | 0 | libewf_file_entry_free( |
203 | 0 | &sub_file_entry, |
204 | 0 | NULL ); |
205 | 0 | } |
206 | 0 | } |
207 | 0 | if( libewf_file_entry_get_number_of_extents( |
208 | 0 | file_entry, |
209 | 0 | &number_of_extents, |
210 | 0 | NULL ) == 1 && number_of_extents > 0 ) |
211 | 0 | { |
212 | 0 | if( libewf_file_entry_get_extent( |
213 | 0 | file_entry, |
214 | 0 | 0, |
215 | 0 | &extent, |
216 | 0 | NULL ) == 1 && extent != NULL ) |
217 | 0 | { |
218 | 0 | libewf_extent_get_values( |
219 | 0 | extent, |
220 | 0 | &extent_offset, |
221 | 0 | &extent_size, |
222 | 0 | &extent_flags, |
223 | 0 | NULL ); |
224 | 0 | libewf_extent_free( |
225 | 0 | &extent, |
226 | 0 | NULL ); |
227 | 0 | } |
228 | 0 | } |
229 | 0 | libewf_file_entry_get_number_of_attributes( |
230 | 0 | file_entry, |
231 | 0 | &number_of_attributes, |
232 | 0 | NULL ); |
233 | 0 | libewf_file_entry_get_number_of_access_control_entries( |
234 | 0 | file_entry, |
235 | 0 | &number_of_access_control_entries, |
236 | 0 | NULL ); |
237 | 0 | } |
238 | | |
239 | | int LLVMFuzzerTestOneInput( |
240 | | const uint8_t *data, |
241 | | size_t size ) |
242 | 1.71k | { |
243 | 1.71k | libbfio_handle_t *file_io_handle = NULL; |
244 | 1.71k | libbfio_pool_t *file_io_pool = NULL; |
245 | 1.71k | libewf_handle_t *handle = NULL; |
246 | 1.71k | libewf_file_entry_t *file_entry = NULL; |
247 | 1.71k | size_t file_entry_path_size = 0; |
248 | 1.71k | int entry_index = 0; |
249 | | |
250 | 1.71k | if( libbfio_memory_range_initialize( |
251 | 1.71k | &file_io_handle, |
252 | 1.71k | NULL ) != 1 ) |
253 | 0 | { |
254 | 0 | return( 0 ); |
255 | 0 | } |
256 | 1.71k | if( libbfio_memory_range_set( |
257 | 1.71k | file_io_handle, |
258 | 1.71k | (uint8_t *) data, |
259 | 1.71k | size, |
260 | 1.71k | NULL ) != 1 ) |
261 | 0 | { |
262 | 0 | goto on_error_libbfio; |
263 | 0 | } |
264 | 1.71k | if( libbfio_pool_initialize( |
265 | 1.71k | &file_io_pool, |
266 | 1.71k | 0, |
267 | 1.71k | 0, |
268 | 1.71k | NULL ) != 1 ) |
269 | 0 | { |
270 | 0 | goto on_error_libbfio; |
271 | 0 | } |
272 | 1.71k | if( libbfio_pool_append_handle( |
273 | 1.71k | file_io_pool, |
274 | 1.71k | &entry_index, |
275 | 1.71k | file_io_handle, |
276 | 1.71k | LIBBFIO_OPEN_READ, |
277 | 1.71k | NULL ) != 1 ) |
278 | 0 | { |
279 | 0 | goto on_error_libbfio; |
280 | 0 | } |
281 | | /* The file IO pool takes over management of the file IO handle |
282 | | */ |
283 | 1.71k | file_io_handle = NULL; |
284 | | |
285 | 1.71k | if( libewf_handle_initialize( |
286 | 1.71k | &handle, |
287 | 1.71k | NULL ) != 1 ) |
288 | 0 | { |
289 | 0 | goto on_error_libbfio; |
290 | 0 | } |
291 | 1.71k | if( libewf_handle_open_file_io_pool( |
292 | 1.71k | handle, |
293 | 1.71k | file_io_pool, |
294 | 1.71k | LIBEWF_OPEN_READ, |
295 | 1.71k | NULL ) != 1 ) |
296 | 1.32k | { |
297 | 1.32k | goto on_error_libewf; |
298 | 1.32k | } |
299 | 393 | if( libewf_handle_get_root_file_entry( |
300 | 393 | handle, |
301 | 393 | &file_entry, |
302 | 393 | NULL ) == 1 && file_entry != NULL ) |
303 | 0 | { |
304 | 0 | get_file_entry_values( |
305 | 0 | file_entry ); |
306 | 0 | libewf_file_entry_free( |
307 | 0 | &file_entry, |
308 | 0 | NULL ); |
309 | 0 | } |
310 | | /* Use a bounded slice of the input as a path to explore the |
311 | | * lookup of the file entry by name. |
312 | | */ |
313 | 393 | file_entry_path_size = size; |
314 | | |
315 | 393 | if( file_entry_path_size > 24 ) |
316 | 384 | { |
317 | 384 | file_entry_path_size = 24; |
318 | 384 | } |
319 | 393 | if( file_entry_path_size > 0 ) |
320 | 393 | { |
321 | 393 | libewf_handle_get_file_entry_by_utf8_path( |
322 | 393 | handle, |
323 | 393 | data, |
324 | 393 | file_entry_path_size, |
325 | 393 | &file_entry, |
326 | 393 | NULL ); |
327 | | |
328 | 393 | if( file_entry != NULL ) |
329 | 0 | { |
330 | 0 | get_file_entry_values( |
331 | 0 | file_entry ); |
332 | 0 | libewf_file_entry_free( |
333 | 0 | &file_entry, |
334 | 0 | NULL ); |
335 | 0 | } |
336 | 393 | } |
337 | 393 | libewf_handle_close( |
338 | 393 | handle, |
339 | 393 | NULL ); |
340 | | |
341 | 1.71k | on_error_libewf: |
342 | 1.71k | libewf_handle_free( |
343 | 1.71k | &handle, |
344 | 1.71k | NULL ); |
345 | | |
346 | 1.71k | on_error_libbfio: |
347 | | /* Note that on error the handle still has a reference to file_io_pool |
348 | | * that will be closed. Therefore the file IO pool and handle need to |
349 | | * be freed after closing or freeing the handle. |
350 | | */ |
351 | 1.71k | if( file_io_pool != NULL ) |
352 | 1.71k | { |
353 | 1.71k | libbfio_pool_free( |
354 | 1.71k | &file_io_pool, |
355 | 1.71k | NULL ); |
356 | 1.71k | } |
357 | 1.71k | if( file_io_handle != NULL ) |
358 | 0 | { |
359 | 0 | libbfio_handle_free( |
360 | 0 | &file_io_handle, |
361 | | NULL ); |
362 | 0 | } |
363 | 1.71k | return( 0 ); |
364 | 1.71k | } |
365 | | |
366 | | } /* extern "C" */ |