/src/libfvde/ossfuzz/volume_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libfvde volume 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 | | /* Determines if a file contains a FVDE volume signature using a Basic File IO (bfio) handle |
35 | | * Returns 1 if true, 0 if not or -1 on error |
36 | | */ |
37 | | LIBFVDE_EXTERN \ |
38 | | int libfvde_check_volume_signature_file_io_handle( |
39 | | libbfio_handle_t *file_io_handle, |
40 | | libfvde_error_t **error ); |
41 | | |
42 | | /* Opens a volume using a Basic File IO (bfio) handle |
43 | | * Returns 1 if successful or -1 on error |
44 | | */ |
45 | | LIBFVDE_EXTERN \ |
46 | | int libfvde_volume_open_file_io_handle( |
47 | | libfvde_volume_t *volume, |
48 | | libbfio_handle_t *file_io_handle, |
49 | | int access_flags, |
50 | | libfvde_error_t **error ); |
51 | | |
52 | | /* Reads the encrypted root plist file (EncryptedRoot.plist) using a Basic File IO (bfio) handle |
53 | | * Returns 1 if successful or -1 on error |
54 | | */ |
55 | | LIBFVDE_EXTERN \ |
56 | | int libfvde_volume_read_encrypted_root_plist_file_io_handle( |
57 | | libfvde_volume_t *volume, |
58 | | libbfio_handle_t *file_io_handle, |
59 | | libfvde_error_t **error ); |
60 | | |
61 | | /* Opens the physical volume files |
62 | | * This function assumes the physical volume files are in same order as defined by the metadata |
63 | | * Returns 1 if successful or -1 on error |
64 | | */ |
65 | | LIBFVDE_EXTERN \ |
66 | | int libfvde_volume_open_physical_volume_files_file_io_pool( |
67 | | libfvde_volume_t *volume, |
68 | | libbfio_pool_t *file_io_pool, |
69 | | libfvde_error_t **error ); |
70 | | |
71 | | #endif /* !defined( LIBFVDE_HAVE_BFIO ) */ |
72 | | |
73 | | int LLVMFuzzerTestOneInput( |
74 | | const uint8_t *data, |
75 | | size_t size ) |
76 | 1.45k | { |
77 | 1.45k | uint8_t uuid[ 16 ]; |
78 | 1.45k | uint8_t volume_master_key[ 16 ] = { |
79 | 1.45k | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10 |
80 | 1.45k | }; |
81 | | |
82 | 1.45k | libbfio_handle_t *file_io_handle = NULL; |
83 | 1.45k | libbfio_pool_t *file_io_pool = NULL; |
84 | 1.45k | libfvde_volume_t *volume = NULL; |
85 | 1.45k | libfvde_volume_group_t *volume_group = NULL; |
86 | 1.45k | uint32_t encryption_method = 0; |
87 | 1.45k | size64_t volume_size = 0; |
88 | 1.45k | off64_t volume_offset = 0; |
89 | 1.45k | int entry_index = 0; |
90 | | |
91 | 1.45k | if( libbfio_memory_range_initialize( |
92 | 1.45k | &file_io_handle, |
93 | 1.45k | NULL ) != 1 ) |
94 | 0 | { |
95 | 0 | return( 0 ); |
96 | 0 | } |
97 | 1.45k | if( libbfio_memory_range_set( |
98 | 1.45k | file_io_handle, |
99 | 1.45k | (uint8_t *) data, |
100 | 1.45k | size, |
101 | 1.45k | NULL ) != 1 ) |
102 | 0 | { |
103 | 0 | goto on_error_libbfio; |
104 | 0 | } |
105 | 1.45k | libfvde_check_volume_signature_file_io_handle( |
106 | 1.45k | file_io_handle, |
107 | 1.45k | NULL ); |
108 | | |
109 | 1.45k | if( libbfio_pool_initialize( |
110 | 1.45k | &file_io_pool, |
111 | 1.45k | 0, |
112 | 1.45k | 0, |
113 | 1.45k | NULL ) != 1 ) |
114 | 0 | { |
115 | 0 | goto on_error_libbfio; |
116 | 0 | } |
117 | 1.45k | if( libfvde_volume_initialize( |
118 | 1.45k | &volume, |
119 | 1.45k | NULL ) != 1 ) |
120 | 0 | { |
121 | 0 | goto on_error_libbfio; |
122 | 0 | } |
123 | 1.45k | if( libfvde_volume_open_file_io_handle( |
124 | 1.45k | volume, |
125 | 1.45k | file_io_handle, |
126 | 1.45k | LIBFVDE_OPEN_READ, |
127 | 1.45k | NULL ) != 1 ) |
128 | 1.45k | { |
129 | 1.45k | goto on_error_libfvde_volume; |
130 | 1.45k | } |
131 | 0 | libfvde_volume_read_encrypted_root_plist_file_io_handle( |
132 | 0 | volume, |
133 | 0 | file_io_handle, |
134 | 0 | NULL ); |
135 | |
|
136 | 0 | if( libbfio_pool_append_handle( |
137 | 0 | file_io_pool, |
138 | 0 | &entry_index, |
139 | 0 | file_io_handle, |
140 | 0 | LIBBFIO_OPEN_READ, |
141 | 0 | NULL ) != 1 ) |
142 | 0 | { |
143 | 0 | goto on_error_libfvde_volume; |
144 | 0 | } |
145 | | /* The file IO pool takes over management of the file IO handle |
146 | | */ |
147 | 0 | file_io_handle = NULL; |
148 | |
|
149 | 0 | if( libfvde_volume_open_physical_volume_files_file_io_pool( |
150 | 0 | volume, |
151 | 0 | file_io_pool, |
152 | 0 | NULL ) != 1 ) |
153 | 0 | { |
154 | 0 | goto on_error_libfvde_volume; |
155 | 0 | } |
156 | 0 | libfvde_volume_is_locked( |
157 | 0 | volume, |
158 | 0 | NULL ); |
159 | |
|
160 | 0 | libfvde_volume_get_size( |
161 | 0 | volume, |
162 | 0 | &volume_size, |
163 | 0 | NULL ); |
164 | |
|
165 | 0 | libfvde_volume_get_offset( |
166 | 0 | volume, |
167 | 0 | &volume_offset, |
168 | 0 | NULL ); |
169 | |
|
170 | 0 | libfvde_volume_seek_offset( |
171 | 0 | volume, |
172 | 0 | 0, |
173 | 0 | 0, |
174 | 0 | NULL ); |
175 | |
|
176 | 0 | libfvde_volume_read_buffer_at_offset( |
177 | 0 | volume, |
178 | 0 | uuid, |
179 | 0 | 16, |
180 | 0 | 0, |
181 | 0 | NULL ); |
182 | |
|
183 | 0 | libfvde_volume_set_utf8_password( |
184 | 0 | volume, |
185 | 0 | (uint8_t *) "password", |
186 | 0 | 8, |
187 | 0 | NULL ); |
188 | |
|
189 | 0 | libfvde_volume_set_keys( |
190 | 0 | volume, |
191 | 0 | volume_master_key, |
192 | 0 | 16, |
193 | 0 | NULL ); |
194 | |
|
195 | 0 | libfvde_volume_unlock( |
196 | 0 | volume, |
197 | 0 | NULL ); |
198 | |
|
199 | 0 | libfvde_volume_get_encryption_method( |
200 | 0 | volume, |
201 | 0 | &encryption_method, |
202 | 0 | NULL ); |
203 | |
|
204 | 0 | libfvde_volume_get_logical_volume_group_identifier( |
205 | 0 | volume, |
206 | 0 | uuid, |
207 | 0 | 16, |
208 | 0 | NULL ); |
209 | |
|
210 | 0 | libfvde_volume_get_physical_volume_size( |
211 | 0 | volume, |
212 | 0 | &volume_size, |
213 | 0 | NULL ); |
214 | |
|
215 | 0 | libfvde_volume_get_physical_volume_identifier( |
216 | 0 | volume, |
217 | 0 | uuid, |
218 | 0 | 16, |
219 | 0 | NULL ); |
220 | |
|
221 | 0 | libfvde_volume_get_physical_volume_encryption_method( |
222 | 0 | volume, |
223 | 0 | &encryption_method, |
224 | 0 | NULL ); |
225 | |
|
226 | 0 | libfvde_volume_get_logical_volume_size( |
227 | 0 | volume, |
228 | 0 | &volume_size, |
229 | 0 | NULL ); |
230 | |
|
231 | 0 | libfvde_volume_get_logical_volume_identifier( |
232 | 0 | volume, |
233 | 0 | uuid, |
234 | 0 | 16, |
235 | 0 | NULL ); |
236 | |
|
237 | 0 | libfvde_volume_get_logical_volume_encryption_method( |
238 | 0 | volume, |
239 | 0 | &encryption_method, |
240 | 0 | NULL ); |
241 | |
|
242 | 0 | libfvde_volume_signal_abort( |
243 | 0 | volume, |
244 | 0 | NULL ); |
245 | |
|
246 | 0 | if( libfvde_volume_get_volume_group( |
247 | 0 | volume, |
248 | 0 | &volume_group, |
249 | 0 | NULL ) == 1 ) |
250 | 0 | { |
251 | 0 | libfvde_volume_group_free( |
252 | 0 | &volume_group, |
253 | 0 | NULL ); |
254 | 0 | } |
255 | 0 | libfvde_volume_close( |
256 | 0 | volume, |
257 | 0 | NULL ); |
258 | |
|
259 | 1.45k | on_error_libfvde_volume: |
260 | 1.45k | libfvde_volume_free( |
261 | 1.45k | &volume, |
262 | 1.45k | NULL ); |
263 | | |
264 | 1.45k | on_error_libbfio: |
265 | | /* Note that on error the volume still has a reference to file_io_pool |
266 | | * that will be closed. Therefore the file IO pool and handle need to |
267 | | * be freed after closing or freeing the volume. |
268 | | */ |
269 | 1.45k | if( file_io_pool != NULL ) |
270 | 1.45k | { |
271 | 1.45k | libbfio_pool_free( |
272 | 1.45k | &file_io_pool, |
273 | 1.45k | NULL ); |
274 | 1.45k | } |
275 | 1.45k | if( file_io_handle != NULL ) |
276 | 1.45k | { |
277 | 1.45k | libbfio_handle_free( |
278 | 1.45k | &file_io_handle, |
279 | | NULL ); |
280 | 1.45k | } |
281 | 1.45k | return( 0 ); |
282 | 1.45k | } |
283 | | |
284 | | } /* extern "C" */ |
285 | | |