/src/libfvde/ossfuzz/logical_volume_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libfvde logical volume type |
3 | | * |
4 | | * Copyright (C) 2014-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 | 3.69k | { |
60 | 3.69k | uint8_t buffer[ 512 ]; |
61 | 3.69k | uint8_t uuid[ 16 ]; |
62 | 3.69k | uint8_t utf8_string[ 64 ]; |
63 | 3.69k | uint16_t utf16_string[ 64 ]; |
64 | | |
65 | 3.69k | libbfio_handle_t *file_io_handle = NULL; |
66 | 3.69k | libbfio_pool_t *file_io_pool = NULL; |
67 | 3.69k | libfvde_volume_t *volume = NULL; |
68 | 3.69k | libfvde_logical_volume_t *logical_volume = NULL; |
69 | 3.69k | libfvde_volume_group_t *volume_group = NULL; |
70 | 3.69k | off64_t volume_offset = 0; |
71 | 3.69k | size64_t volume_size = 0; |
72 | 3.69k | size_t string_size = 0; |
73 | 3.69k | int entry_index = 0; |
74 | 3.69k | int number_of_logical_volumes = 0; |
75 | 3.69k | int read_iterator = 0; |
76 | | |
77 | 3.69k | if( libbfio_memory_range_initialize( |
78 | 3.69k | &file_io_handle, |
79 | 3.69k | NULL ) != 1 ) |
80 | 0 | { |
81 | 0 | return( 0 ); |
82 | 0 | } |
83 | 3.69k | if( libbfio_memory_range_set( |
84 | 3.69k | file_io_handle, |
85 | 3.69k | (uint8_t *) data, |
86 | 3.69k | size, |
87 | 3.69k | NULL ) != 1 ) |
88 | 0 | { |
89 | 0 | goto on_error_libbfio; |
90 | 0 | } |
91 | 3.69k | if( libbfio_pool_initialize( |
92 | 3.69k | &file_io_pool, |
93 | 3.69k | 0, |
94 | 3.69k | 0, |
95 | 3.69k | NULL ) != 1 ) |
96 | 0 | { |
97 | 0 | goto on_error_libbfio; |
98 | 0 | } |
99 | 3.69k | if( libfvde_volume_initialize( |
100 | 3.69k | &volume, |
101 | 3.69k | NULL ) != 1 ) |
102 | 0 | { |
103 | 0 | goto on_error_libbfio; |
104 | 0 | } |
105 | 3.69k | if( libfvde_volume_open_file_io_handle( |
106 | 3.69k | volume, |
107 | 3.69k | file_io_handle, |
108 | 3.69k | LIBFVDE_OPEN_READ, |
109 | 3.69k | NULL ) != 1 ) |
110 | 1.61k | { |
111 | 1.61k | goto on_error_libfvde_volume; |
112 | 1.61k | } |
113 | 2.07k | if( libbfio_pool_append_handle( |
114 | 2.07k | file_io_pool, |
115 | 2.07k | &entry_index, |
116 | 2.07k | file_io_handle, |
117 | 2.07k | LIBBFIO_OPEN_READ, |
118 | 2.07k | 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 | 2.07k | file_io_handle = NULL; |
125 | | |
126 | 2.07k | if( libfvde_volume_open_physical_volume_files_file_io_pool( |
127 | 2.07k | volume, |
128 | 2.07k | file_io_pool, |
129 | 2.07k | NULL ) != 1 ) |
130 | 1.48k | { |
131 | 1.48k | goto on_error_libfvde_volume; |
132 | 1.48k | } |
133 | 588 | if( libfvde_volume_get_volume_group( |
134 | 588 | volume, |
135 | 588 | &volume_group, |
136 | 588 | NULL ) == 1 ) |
137 | 588 | { |
138 | 588 | if( libfvde_volume_group_get_number_of_logical_volumes( |
139 | 588 | volume_group, |
140 | 588 | &number_of_logical_volumes, |
141 | 588 | NULL ) != 1 ) |
142 | 0 | { |
143 | 0 | goto on_error_libfvde_volume_group; |
144 | 0 | } |
145 | 588 | if( number_of_logical_volumes > 0 ) |
146 | 410 | { |
147 | 410 | if( libfvde_volume_group_get_logical_volume_by_index( |
148 | 410 | volume_group, |
149 | 410 | 0, |
150 | 410 | &logical_volume, |
151 | 410 | NULL ) != 1 ) |
152 | 242 | { |
153 | 242 | goto on_error_libfvde_volume_group; |
154 | 242 | } |
155 | 168 | if( libfvde_logical_volume_get_identifier( |
156 | 168 | logical_volume, |
157 | 168 | uuid, |
158 | 168 | 16, |
159 | 168 | NULL ) != 1 ) |
160 | 0 | { |
161 | 0 | goto on_error_libfvde_logical_volume; |
162 | 0 | } |
163 | 168 | if( libfvde_logical_volume_get_utf8_name_size( |
164 | 168 | logical_volume, |
165 | 168 | &string_size, |
166 | 168 | NULL ) != 1 ) |
167 | 1 | { |
168 | 1 | goto on_error_libfvde_logical_volume; |
169 | 1 | } |
170 | 167 | if( libfvde_logical_volume_get_utf8_name( |
171 | 167 | logical_volume, |
172 | 167 | utf8_string, |
173 | 167 | 64, |
174 | 167 | NULL ) != 1 ) |
175 | 0 | { |
176 | 0 | goto on_error_libfvde_logical_volume; |
177 | 0 | } |
178 | 167 | if( libfvde_logical_volume_get_utf16_name_size( |
179 | 167 | logical_volume, |
180 | 167 | &string_size, |
181 | 167 | NULL ) != 1 ) |
182 | 0 | { |
183 | 0 | goto on_error_libfvde_logical_volume; |
184 | 0 | } |
185 | 167 | if( libfvde_logical_volume_get_utf16_name( |
186 | 167 | logical_volume, |
187 | 167 | utf16_string, |
188 | 167 | 64, |
189 | 167 | NULL ) != 1 ) |
190 | 0 | { |
191 | 0 | goto on_error_libfvde_logical_volume; |
192 | 0 | } |
193 | 167 | if( libfvde_logical_volume_get_size( |
194 | 167 | logical_volume, |
195 | 167 | &volume_size, |
196 | 167 | NULL ) != 1 ) |
197 | 0 | { |
198 | 0 | goto on_error_libfvde_logical_volume; |
199 | 0 | } |
200 | 167 | for( read_iterator = 0; |
201 | 6.73k | read_iterator < 128; |
202 | 6.56k | read_iterator++ ) |
203 | 6.68k | { |
204 | 6.68k | if( volume_offset >= volume_size ) |
205 | 33 | { |
206 | 33 | break; |
207 | 33 | } |
208 | 6.65k | if( libfvde_logical_volume_read_buffer_at_offset( |
209 | 6.65k | logical_volume, |
210 | 6.65k | buffer, |
211 | 6.65k | 497, |
212 | 6.65k | volume_offset, |
213 | 6.65k | NULL ) == -1 ) |
214 | 87 | { |
215 | 87 | goto on_error_libfvde_logical_volume; |
216 | 87 | } |
217 | 6.56k | volume_offset += 497; |
218 | 6.56k | } |
219 | 168 | on_error_libfvde_logical_volume: |
220 | 168 | libfvde_logical_volume_free( |
221 | 168 | &logical_volume, |
222 | 168 | NULL ); |
223 | 168 | } |
224 | 588 | on_error_libfvde_volume_group: |
225 | 588 | libfvde_volume_group_free( |
226 | 588 | &volume_group, |
227 | 588 | NULL ); |
228 | 588 | } |
229 | 588 | libfvde_volume_close( |
230 | 588 | volume, |
231 | 588 | NULL ); |
232 | | |
233 | 3.69k | on_error_libfvde_volume: |
234 | 3.69k | libfvde_volume_free( |
235 | 3.69k | &volume, |
236 | 3.69k | NULL ); |
237 | | |
238 | 3.69k | on_error_libbfio: |
239 | | /* Note that on error the volume still has a reference to file_io_pool |
240 | | * that will be closed. Therefore the file IO pool and handle need to |
241 | | * be freed after closing or freeing the volume. |
242 | | */ |
243 | 3.69k | if( file_io_pool != NULL ) |
244 | 3.69k | { |
245 | 3.69k | libbfio_pool_free( |
246 | 3.69k | &file_io_pool, |
247 | 3.69k | NULL ); |
248 | 3.69k | } |
249 | 3.69k | if( file_io_handle != NULL ) |
250 | 1.61k | { |
251 | 1.61k | libbfio_handle_free( |
252 | 1.61k | &file_io_handle, |
253 | | NULL ); |
254 | 1.61k | } |
255 | 3.69k | return( 0 ); |
256 | 3.69k | } |
257 | | |
258 | | } /* extern "C" */ |
259 | | |