/src/libfsapfs/ossfuzz/volume_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libfsapfs volume type |
3 | | * |
4 | | * Copyright (C) 2018-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_libfsapfs.h" |
31 | | |
32 | | #if !defined( LIBFSAPFS_HAVE_BFIO ) |
33 | | |
34 | | /* Opens a container using a Basic File IO (bfio) handle |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | LIBFSAPFS_EXTERN \ |
38 | | int libfsapfs_container_open_file_io_handle( |
39 | | libfsapfs_container_t *container, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libfsapfs_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBFSAPFS_HAVE_BFIO ) */ |
45 | | |
46 | | int LLVMFuzzerTestOneInput( |
47 | | const uint8_t *data, |
48 | | size_t size ) |
49 | 3.10k | { |
50 | 3.10k | uint8_t utf8_string[ 64 ]; |
51 | 3.10k | uint8_t volume_identifier[ 16 ]; |
52 | 3.10k | uint16_t utf16_path[ 1 ] = { 0x002f }; |
53 | 3.10k | uint16_t utf16_string[ 32 ]; |
54 | 3.10k | uint16_t utf16_password[ 11 ] = { 'f', 's', 'a', 'p', 'f', 's', '-', 'T', 'E', 'S', 'T' }; |
55 | 3.10k | uint16_t utf16_recovery_password[ 20 ] = { 'f', 's', 'a', 'p', 'f', 's', '-', 'r', 'e', 'c', 'o', 'v', |
56 | 3.10k | 'e', 'r', 'y', '-', 'T', 'E', 'S', 'T' }; |
57 | | |
58 | 3.10k | libbfio_handle_t *file_io_handle = NULL; |
59 | 3.10k | libfsapfs_container_t *container = NULL; |
60 | 3.10k | libfsapfs_file_entry_t *file_entry = NULL; |
61 | 3.10k | libfsapfs_volume_t *volume = NULL; |
62 | 3.10k | size64_t volume_size = 0; |
63 | 3.10k | size_t utf8_string_size = 0; |
64 | 3.10k | size_t utf16_string_size = 0; |
65 | 3.10k | uint64_t compatible_features_flags = 0; |
66 | 3.10k | uint64_t incompatible_features_flags = 0; |
67 | 3.10k | uint64_t next_file_entry_identifier = 0; |
68 | 3.10k | uint64_t read_only_compatible_features_flags = 0; |
69 | 3.10k | int is_locked = 0; |
70 | 3.10k | int number_of_snapshots = 0; |
71 | 3.10k | int number_of_volumes = 0; |
72 | 3.10k | int result = 0; |
73 | | |
74 | 3.10k | if( libbfio_memory_range_initialize( |
75 | 3.10k | &file_io_handle, |
76 | 3.10k | NULL ) != 1 ) |
77 | 0 | { |
78 | 0 | return( 0 ); |
79 | 0 | } |
80 | 3.10k | if( libbfio_memory_range_set( |
81 | 3.10k | file_io_handle, |
82 | 3.10k | (uint8_t *) data, |
83 | 3.10k | size, |
84 | 3.10k | NULL ) != 1 ) |
85 | 0 | { |
86 | 0 | goto on_error_libbfio; |
87 | 0 | } |
88 | 3.10k | if( libfsapfs_container_initialize( |
89 | 3.10k | &container, |
90 | 3.10k | NULL ) != 1 ) |
91 | 0 | { |
92 | 0 | goto on_error_libbfio; |
93 | 0 | } |
94 | 3.10k | if( libfsapfs_container_open_file_io_handle( |
95 | 3.10k | container, |
96 | 3.10k | file_io_handle, |
97 | 3.10k | LIBFSAPFS_OPEN_READ, |
98 | 3.10k | NULL ) != 1 ) |
99 | 1.29k | { |
100 | 1.29k | goto on_error_libfsapfs_container; |
101 | 1.29k | } |
102 | 1.81k | if( libfsapfs_container_get_number_of_volumes( |
103 | 1.81k | container, |
104 | 1.81k | &number_of_volumes, |
105 | 1.81k | NULL ) != 1 ) |
106 | 0 | { |
107 | 0 | goto on_error_libfsapfs_container; |
108 | 0 | } |
109 | 1.81k | if( number_of_volumes > 0 ) |
110 | 1.81k | { |
111 | 1.81k | result = libfsapfs_container_get_volume_by_index( |
112 | 1.81k | container, |
113 | 1.81k | 0, |
114 | 1.81k | &volume, |
115 | 1.81k | NULL ); |
116 | | |
117 | 1.81k | if( result == -1 ) |
118 | 1.79k | { |
119 | 1.79k | goto on_error_libfsapfs_container; |
120 | 1.79k | } |
121 | 20 | if( result == 1 ) |
122 | 20 | { |
123 | 20 | if( libfsapfs_volume_get_features_flags( |
124 | 20 | volume, |
125 | 20 | &compatible_features_flags, |
126 | 20 | &incompatible_features_flags, |
127 | 20 | &read_only_compatible_features_flags, |
128 | 20 | NULL ) != 1 ) |
129 | 0 | { |
130 | 0 | goto on_error_libfsapfs_volume; |
131 | 0 | } |
132 | 20 | if( libfsapfs_volume_get_size( |
133 | 20 | volume, |
134 | 20 | &volume_size, |
135 | 20 | NULL ) != 1 ) |
136 | 20 | { |
137 | 20 | goto on_error_libfsapfs_volume; |
138 | 20 | } |
139 | 0 | if( libfsapfs_volume_get_identifier( |
140 | 0 | volume, |
141 | 0 | volume_identifier, |
142 | 0 | 16, |
143 | 0 | NULL ) != 1 ) |
144 | 0 | { |
145 | 0 | goto on_error_libfsapfs_volume; |
146 | 0 | } |
147 | 0 | if( libfsapfs_volume_get_utf8_name_size( |
148 | 0 | volume, |
149 | 0 | &utf8_string_size, |
150 | 0 | NULL ) == -1 ) |
151 | 0 | { |
152 | 0 | goto on_error_libfsapfs_volume; |
153 | 0 | } |
154 | 0 | if( utf8_string_size > 0 ) |
155 | 0 | { |
156 | 0 | if( libfsapfs_volume_get_utf8_name( |
157 | 0 | volume, |
158 | 0 | utf8_string, |
159 | 0 | 64, |
160 | 0 | NULL ) == -1 ) |
161 | 0 | { |
162 | 0 | goto on_error_libfsapfs_volume; |
163 | 0 | } |
164 | 0 | } |
165 | 0 | if( libfsapfs_volume_get_utf16_name_size( |
166 | 0 | volume, |
167 | 0 | &utf16_string_size, |
168 | 0 | NULL ) == -1 ) |
169 | 0 | { |
170 | 0 | goto on_error_libfsapfs_volume; |
171 | 0 | } |
172 | 0 | if( utf16_string_size > 0 ) |
173 | 0 | { |
174 | 0 | if( libfsapfs_volume_get_utf16_name( |
175 | 0 | volume, |
176 | 0 | utf16_string, |
177 | 0 | 32, |
178 | 0 | NULL ) == -1 ) |
179 | 0 | { |
180 | 0 | goto on_error_libfsapfs_volume; |
181 | 0 | } |
182 | 0 | } |
183 | 0 | is_locked = libfsapfs_volume_is_locked( |
184 | 0 | volume, |
185 | 0 | NULL ); |
186 | |
|
187 | 0 | if( is_locked == -1 ) |
188 | 0 | { |
189 | 0 | goto on_error_libfsapfs_volume; |
190 | 0 | } |
191 | 0 | if( is_locked == 1 ) |
192 | 0 | { |
193 | 0 | if( libfsapfs_volume_set_utf8_password( |
194 | 0 | volume, |
195 | 0 | (uint8_t *) "fsapfs-TEST", |
196 | 0 | 11, |
197 | 0 | NULL ) == -1 ) |
198 | 0 | { |
199 | 0 | goto on_error_libfsapfs_volume; |
200 | 0 | } |
201 | 0 | if( libfsapfs_volume_set_utf16_password( |
202 | 0 | volume, |
203 | 0 | utf16_password, |
204 | 0 | 11, |
205 | 0 | NULL ) == -1 ) |
206 | 0 | { |
207 | 0 | goto on_error_libfsapfs_volume; |
208 | 0 | } |
209 | 0 | if( libfsapfs_volume_set_utf8_recovery_password( |
210 | 0 | volume, |
211 | 0 | (uint8_t *) "fsapfs-recovery-TEST", |
212 | 0 | 20, |
213 | 0 | NULL ) == -1 ) |
214 | 0 | { |
215 | 0 | goto on_error_libfsapfs_volume; |
216 | 0 | } |
217 | 0 | if( libfsapfs_volume_set_utf16_recovery_password( |
218 | 0 | volume, |
219 | 0 | utf16_recovery_password, |
220 | 0 | 20, |
221 | 0 | NULL ) == -1 ) |
222 | 0 | { |
223 | 0 | goto on_error_libfsapfs_volume; |
224 | 0 | } |
225 | 0 | if( libfsapfs_volume_unlock( |
226 | 0 | volume, |
227 | 0 | NULL ) == -1 ) |
228 | 0 | { |
229 | 0 | goto on_error_libfsapfs_volume; |
230 | 0 | } |
231 | 0 | } |
232 | 0 | if( libfsapfs_volume_get_next_file_entry_identifier( |
233 | 0 | volume, |
234 | 0 | &next_file_entry_identifier, |
235 | 0 | NULL ) != 1 ) |
236 | 0 | { |
237 | 0 | goto on_error_libfsapfs_volume; |
238 | 0 | } |
239 | 0 | result = libfsapfs_volume_get_root_directory( |
240 | 0 | volume, |
241 | 0 | &file_entry, |
242 | 0 | NULL ); |
243 | |
|
244 | 0 | if( result == -1 ) |
245 | 0 | { |
246 | 0 | goto on_error_libfsapfs_volume; |
247 | 0 | } |
248 | 0 | if( result == 1 ) |
249 | 0 | { |
250 | 0 | libfsapfs_file_entry_free( |
251 | 0 | &file_entry, |
252 | 0 | NULL ); |
253 | 0 | } |
254 | 0 | result = libfsapfs_volume_get_file_entry_by_identifier( |
255 | 0 | volume, |
256 | 0 | 1, |
257 | 0 | &file_entry, |
258 | 0 | NULL ); |
259 | |
|
260 | 0 | if( result == -1 ) |
261 | 0 | { |
262 | 0 | goto on_error_libfsapfs_volume; |
263 | 0 | } |
264 | 0 | if( result == 1 ) |
265 | 0 | { |
266 | 0 | libfsapfs_file_entry_free( |
267 | 0 | &file_entry, |
268 | 0 | NULL ); |
269 | 0 | } |
270 | 0 | result = libfsapfs_volume_get_file_entry_by_utf8_path( |
271 | 0 | volume, |
272 | 0 | (uint8_t *) "/", |
273 | 0 | 1, |
274 | 0 | &file_entry, |
275 | 0 | NULL ); |
276 | |
|
277 | 0 | if( result == -1 ) |
278 | 0 | { |
279 | 0 | goto on_error_libfsapfs_volume; |
280 | 0 | } |
281 | 0 | if( result == 1 ) |
282 | 0 | { |
283 | 0 | libfsapfs_file_entry_free( |
284 | 0 | &file_entry, |
285 | 0 | NULL ); |
286 | 0 | } |
287 | 0 | result = libfsapfs_volume_get_file_entry_by_utf16_path( |
288 | 0 | volume, |
289 | 0 | utf16_path, |
290 | 0 | 1, |
291 | 0 | &file_entry, |
292 | 0 | NULL ); |
293 | |
|
294 | 0 | if( result == -1 ) |
295 | 0 | { |
296 | 0 | goto on_error_libfsapfs_volume; |
297 | 0 | } |
298 | 0 | if( result == 1 ) |
299 | 0 | { |
300 | 0 | libfsapfs_file_entry_free( |
301 | 0 | &file_entry, |
302 | 0 | NULL ); |
303 | 0 | } |
304 | 0 | if( libfsapfs_volume_get_number_of_snapshots( |
305 | 0 | volume, |
306 | 0 | &number_of_snapshots, |
307 | 0 | NULL ) != 1 ) |
308 | 0 | { |
309 | 0 | goto on_error_libfsapfs_volume; |
310 | 0 | } |
311 | 0 | } |
312 | 20 | on_error_libfsapfs_volume: |
313 | 20 | libfsapfs_volume_free( |
314 | 20 | &volume, |
315 | 20 | NULL ); |
316 | 20 | } |
317 | 21 | libfsapfs_container_close( |
318 | 21 | container, |
319 | 21 | NULL ); |
320 | | |
321 | 3.10k | on_error_libfsapfs_container: |
322 | 3.10k | libfsapfs_container_free( |
323 | 3.10k | &container, |
324 | 3.10k | NULL ); |
325 | | |
326 | 3.10k | on_error_libbfio: |
327 | 3.10k | libbfio_handle_free( |
328 | 3.10k | &file_io_handle, |
329 | 3.10k | NULL ); |
330 | | |
331 | 3.10k | return( 0 ); |
332 | 3.10k | } |
333 | | |
334 | | } /* extern "C" */ |