/src/libfvde/ossfuzz/volume_group_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libfvde volume group type |
3 | | * |
4 | | * Copyright (C) 2011-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_libfvde.h" |
31 | | |
32 | | #if !defined( LIBFVDE_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 | | LIBFVDE_EXTERN \ |
38 | | int libfvde_volume_open_file_io_handle( |
39 | | libfvde_volume_t *volume, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libfvde_error_t **error ); |
43 | | |
44 | | /* Opens the physical volume files |
45 | | * This function assumes the physical volume files are in same order as defined by the metadata |
46 | | * Returns 1 if successful or -1 on error |
47 | | */ |
48 | | LIBFVDE_EXTERN \ |
49 | | int libfvde_volume_open_physical_volume_files_file_io_pool( |
50 | | libfvde_volume_t *volume, |
51 | | libbfio_pool_t *file_io_pool, |
52 | | libfvde_error_t **error ); |
53 | | |
54 | | #endif /* !defined( LIBFVDE_HAVE_BFIO ) */ |
55 | | |
56 | | int LLVMFuzzerTestOneInput( |
57 | | const uint8_t *data, |
58 | | size_t size ) |
59 | 739 | { |
60 | 739 | uint8_t uuid[ 16 ]; |
61 | 739 | uint8_t utf8_string[ 64 ]; |
62 | 739 | uint16_t utf16_string[ 64 ]; |
63 | | |
64 | 739 | libbfio_handle_t *file_io_handle = NULL; |
65 | 739 | libbfio_pool_t *file_io_pool = NULL; |
66 | 739 | libfvde_volume_t *volume = NULL; |
67 | 739 | libfvde_volume_group_t *volume_group = NULL; |
68 | 739 | libfvde_physical_volume_t *physical_volume = NULL; |
69 | 739 | libfvde_logical_volume_t *logical_volume = NULL; |
70 | 739 | uint32_t encryption_method = 0; |
71 | 739 | size64_t volume_size = 0; |
72 | 739 | size_t string_size = 0; |
73 | 739 | int entry_index = 0; |
74 | 739 | int number_of_volumes = 0; |
75 | 739 | int volume_index = 0; |
76 | | |
77 | 739 | if( libbfio_memory_range_initialize( |
78 | 739 | &file_io_handle, |
79 | 739 | NULL ) != 1 ) |
80 | 0 | { |
81 | 0 | return( 0 ); |
82 | 0 | } |
83 | 739 | if( libbfio_memory_range_set( |
84 | 739 | file_io_handle, |
85 | 739 | (uint8_t *) data, |
86 | 739 | size, |
87 | 739 | NULL ) != 1 ) |
88 | 0 | { |
89 | 0 | goto on_error_libbfio; |
90 | 0 | } |
91 | 739 | if( libbfio_pool_initialize( |
92 | 739 | &file_io_pool, |
93 | 739 | 0, |
94 | 739 | 0, |
95 | 739 | NULL ) != 1 ) |
96 | 0 | { |
97 | 0 | goto on_error_libbfio; |
98 | 0 | } |
99 | 739 | if( libfvde_volume_initialize( |
100 | 739 | &volume, |
101 | 739 | NULL ) != 1 ) |
102 | 0 | { |
103 | 0 | goto on_error_libbfio; |
104 | 0 | } |
105 | 739 | if( libfvde_volume_open_file_io_handle( |
106 | 739 | volume, |
107 | 739 | file_io_handle, |
108 | 739 | LIBFVDE_OPEN_READ, |
109 | 739 | NULL ) != 1 ) |
110 | 739 | { |
111 | 739 | goto on_error_libfvde_volume; |
112 | 739 | } |
113 | 0 | if( libbfio_pool_append_handle( |
114 | 0 | file_io_pool, |
115 | 0 | &entry_index, |
116 | 0 | file_io_handle, |
117 | 0 | LIBBFIO_OPEN_READ, |
118 | 0 | NULL ) != 1 ) |
119 | 0 | { |
120 | 0 | goto on_error_libfvde_volume; |
121 | 0 | } |
122 | | /* The file IO pool takes over management of the file IO handle |
123 | | */ |
124 | 0 | file_io_handle = NULL; |
125 | |
|
126 | 0 | if( libfvde_volume_open_physical_volume_files_file_io_pool( |
127 | 0 | volume, |
128 | 0 | file_io_pool, |
129 | 0 | NULL ) != 1 ) |
130 | 0 | { |
131 | 0 | goto on_error_libfvde_volume; |
132 | 0 | } |
133 | 0 | if( libfvde_volume_get_volume_group( |
134 | 0 | volume, |
135 | 0 | &volume_group, |
136 | 0 | NULL ) == 1 ) |
137 | 0 | { |
138 | 0 | if( libfvde_volume_group_get_identifier( |
139 | 0 | volume_group, |
140 | 0 | uuid, |
141 | 0 | 64, |
142 | 0 | NULL ) != 1 ) |
143 | 0 | { |
144 | 0 | goto on_error_libfvde_volume_group; |
145 | 0 | } |
146 | 0 | if( libfvde_volume_group_get_utf8_name_size( |
147 | 0 | volume_group, |
148 | 0 | &string_size, |
149 | 0 | NULL ) != 1 ) |
150 | 0 | { |
151 | 0 | goto on_error_libfvde_volume_group; |
152 | 0 | } |
153 | 0 | if( libfvde_volume_group_get_utf8_name( |
154 | 0 | volume_group, |
155 | 0 | utf8_string, |
156 | 0 | 64, |
157 | 0 | NULL ) != 1 ) |
158 | 0 | { |
159 | 0 | goto on_error_libfvde_volume_group; |
160 | 0 | } |
161 | 0 | if( libfvde_volume_group_get_utf16_name_size( |
162 | 0 | volume_group, |
163 | 0 | &string_size, |
164 | 0 | NULL ) != 1 ) |
165 | 0 | { |
166 | 0 | goto on_error_libfvde_volume_group; |
167 | 0 | } |
168 | 0 | if( libfvde_volume_group_get_utf16_name( |
169 | 0 | volume_group, |
170 | 0 | utf16_string, |
171 | 0 | 64, |
172 | 0 | NULL ) != 1 ) |
173 | 0 | { |
174 | 0 | goto on_error_libfvde_volume_group; |
175 | 0 | } |
176 | 0 | if( libfvde_volume_group_get_number_of_physical_volumes( |
177 | 0 | volume_group, |
178 | 0 | &number_of_volumes, |
179 | 0 | NULL ) != 1 ) |
180 | 0 | { |
181 | 0 | goto on_error_libfvde_volume_group; |
182 | 0 | } |
183 | 0 | for( volume_index = 0; |
184 | 0 | volume_index < number_of_volumes; |
185 | 0 | volume_index++ ) |
186 | 0 | { |
187 | 0 | if( libfvde_volume_group_get_physical_volume_by_index( |
188 | 0 | volume_group, |
189 | 0 | volume_index, |
190 | 0 | &physical_volume, |
191 | 0 | NULL ) != 1 ) |
192 | 0 | { |
193 | 0 | break; |
194 | 0 | } |
195 | 0 | if( libfvde_physical_volume_get_identifier( |
196 | 0 | physical_volume, |
197 | 0 | uuid, |
198 | 0 | 16, |
199 | 0 | NULL ) != 1 ) |
200 | 0 | { |
201 | 0 | libfvde_physical_volume_free( |
202 | 0 | &physical_volume, |
203 | 0 | NULL ); |
204 | |
|
205 | 0 | break; |
206 | 0 | } |
207 | 0 | libfvde_physical_volume_get_encryption_method( |
208 | 0 | physical_volume, |
209 | 0 | &encryption_method, |
210 | 0 | NULL ); |
211 | |
|
212 | 0 | libfvde_physical_volume_get_size( |
213 | 0 | physical_volume, |
214 | 0 | &volume_size, |
215 | 0 | NULL ); |
216 | |
|
217 | 0 | if( libfvde_physical_volume_free( |
218 | 0 | &physical_volume, |
219 | 0 | NULL ) != 1 ) |
220 | 0 | { |
221 | 0 | break; |
222 | 0 | } |
223 | 0 | } |
224 | 0 | if( libfvde_volume_group_get_number_of_logical_volumes( |
225 | 0 | volume_group, |
226 | 0 | &number_of_volumes, |
227 | 0 | NULL ) != 1 ) |
228 | 0 | { |
229 | 0 | goto on_error_libfvde_volume_group; |
230 | 0 | } |
231 | 0 | for( volume_index = 0; |
232 | 0 | volume_index < number_of_volumes; |
233 | 0 | volume_index++ ) |
234 | 0 | { |
235 | 0 | if( libfvde_volume_group_get_logical_volume_by_index( |
236 | 0 | volume_group, |
237 | 0 | volume_index, |
238 | 0 | &logical_volume, |
239 | 0 | NULL ) != 1 ) |
240 | 0 | { |
241 | 0 | break; |
242 | 0 | } |
243 | 0 | if( libfvde_logical_volume_get_identifier( |
244 | 0 | logical_volume, |
245 | 0 | uuid, |
246 | 0 | 16, |
247 | 0 | NULL ) != 1 ) |
248 | 0 | { |
249 | 0 | libfvde_logical_volume_free( |
250 | 0 | &logical_volume, |
251 | 0 | NULL ); |
252 | |
|
253 | 0 | break; |
254 | 0 | } |
255 | 0 | libfvde_logical_volume_get_utf8_name_size( |
256 | 0 | logical_volume, |
257 | 0 | &string_size, |
258 | 0 | NULL ); |
259 | |
|
260 | 0 | libfvde_logical_volume_get_utf8_name( |
261 | 0 | logical_volume, |
262 | 0 | utf8_string, |
263 | 0 | 64, |
264 | 0 | NULL ); |
265 | |
|
266 | 0 | libfvde_logical_volume_get_utf16_name_size( |
267 | 0 | logical_volume, |
268 | 0 | &string_size, |
269 | 0 | NULL ); |
270 | |
|
271 | 0 | libfvde_logical_volume_get_utf16_name( |
272 | 0 | logical_volume, |
273 | 0 | utf16_string, |
274 | 0 | 64, |
275 | 0 | NULL ); |
276 | |
|
277 | 0 | libfvde_logical_volume_get_size( |
278 | 0 | logical_volume, |
279 | 0 | &volume_size, |
280 | 0 | NULL ); |
281 | |
|
282 | 0 | if( libfvde_logical_volume_free( |
283 | 0 | &logical_volume, |
284 | 0 | NULL ) != 1 ) |
285 | 0 | { |
286 | 0 | break; |
287 | 0 | } |
288 | 0 | } |
289 | 0 | on_error_libfvde_volume_group: |
290 | 0 | libfvde_volume_group_free( |
291 | 0 | &volume_group, |
292 | 0 | NULL ); |
293 | 0 | } |
294 | 0 | libfvde_volume_close( |
295 | 0 | volume, |
296 | 0 | NULL ); |
297 | |
|
298 | 739 | on_error_libfvde_volume: |
299 | 739 | libfvde_volume_free( |
300 | 739 | &volume, |
301 | 739 | NULL ); |
302 | | |
303 | 739 | on_error_libbfio: |
304 | | /* Note that on error the volume still has a reference to file_io_pool |
305 | | * that will be closed. Therefore the file IO pool and handle need to |
306 | | * be freed after closing or freeing the volume. |
307 | | */ |
308 | 739 | if( file_io_pool != NULL ) |
309 | 739 | { |
310 | 739 | libbfio_pool_free( |
311 | 739 | &file_io_pool, |
312 | 739 | NULL ); |
313 | 739 | } |
314 | 739 | if( file_io_handle != NULL ) |
315 | 739 | { |
316 | 739 | libbfio_handle_free( |
317 | 739 | &file_io_handle, |
318 | | NULL ); |
319 | 739 | } |
320 | 739 | return( 0 ); |
321 | 739 | } |
322 | | |
323 | | } /* extern "C" */ |
324 | | |