/src/libvslvm/ossfuzz/logical_volume_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libvslvm 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_libvslvm.h" |
31 | | |
32 | | #if !defined( LIBVSLVM_HAVE_BFIO ) |
33 | | |
34 | | /* Opens a handle using a Basic File IO (bfio) handle |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | LIBVSLVM_EXTERN \ |
38 | | int libvslvm_handle_open_file_io_handle( |
39 | | libvslvm_handle_t *handle, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libvslvm_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 | | LIBVSLVM_EXTERN \ |
49 | | int libvslvm_handle_open_physical_volume_files_file_io_pool( |
50 | | libvslvm_handle_t *handle, |
51 | | libbfio_pool_t *file_io_pool, |
52 | | libvslvm_error_t **error ); |
53 | | |
54 | | #endif /* !defined( LIBVSLVM_HAVE_BFIO ) */ |
55 | | |
56 | | int LLVMFuzzerTestOneInput( |
57 | | const uint8_t *data, |
58 | | size_t size ) |
59 | 3.89k | { |
60 | 3.89k | uint8_t buffer[ 512 ]; |
61 | 3.89k | char string[ 64 ]; |
62 | | |
63 | 3.89k | libbfio_handle_t *file_io_handle = NULL; |
64 | 3.89k | libbfio_pool_t *file_io_pool = NULL; |
65 | 3.89k | libvslvm_handle_t *handle = NULL; |
66 | 3.89k | libvslvm_logical_volume_t *logical_volume = NULL; |
67 | 3.89k | libvslvm_volume_group_t *volume_group = NULL; |
68 | 3.89k | off64_t volume_offset = 0; |
69 | 3.89k | size64_t volume_size = 0; |
70 | 3.89k | size_t string_size = 0; |
71 | 3.89k | int entry_index = 0; |
72 | 3.89k | int number_of_logical_volumes = 0; |
73 | 3.89k | int number_of_segments = 0; |
74 | 3.89k | int read_iterator = 0; |
75 | | |
76 | 3.89k | if( libbfio_memory_range_initialize( |
77 | 3.89k | &file_io_handle, |
78 | 3.89k | NULL ) != 1 ) |
79 | 0 | { |
80 | 0 | return( 0 ); |
81 | 0 | } |
82 | 3.89k | if( libbfio_memory_range_set( |
83 | 3.89k | file_io_handle, |
84 | 3.89k | (uint8_t *) data, |
85 | 3.89k | size, |
86 | 3.89k | NULL ) != 1 ) |
87 | 0 | { |
88 | 0 | goto on_error_libbfio; |
89 | 0 | } |
90 | 3.89k | if( libbfio_pool_initialize( |
91 | 3.89k | &file_io_pool, |
92 | 3.89k | 0, |
93 | 3.89k | 0, |
94 | 3.89k | NULL ) != 1 ) |
95 | 0 | { |
96 | 0 | goto on_error_libbfio; |
97 | 0 | } |
98 | 3.89k | if( libvslvm_handle_initialize( |
99 | 3.89k | &handle, |
100 | 3.89k | NULL ) != 1 ) |
101 | 0 | { |
102 | 0 | goto on_error_libbfio; |
103 | 0 | } |
104 | 3.89k | if( libvslvm_handle_open_file_io_handle( |
105 | 3.89k | handle, |
106 | 3.89k | file_io_handle, |
107 | 3.89k | LIBVSLVM_OPEN_READ, |
108 | 3.89k | NULL ) != 1 ) |
109 | 1.84k | { |
110 | 1.84k | goto on_error_libvslvm_handle; |
111 | 1.84k | } |
112 | 2.04k | if( libbfio_pool_append_handle( |
113 | 2.04k | file_io_pool, |
114 | 2.04k | &entry_index, |
115 | 2.04k | file_io_handle, |
116 | 2.04k | LIBBFIO_OPEN_READ, |
117 | 2.04k | NULL ) != 1 ) |
118 | 0 | { |
119 | 0 | goto on_error_libvslvm_handle; |
120 | 0 | } |
121 | | /* The file IO pool takes over management of the file IO handle |
122 | | */ |
123 | 2.04k | file_io_handle = NULL; |
124 | | |
125 | 2.04k | if( libvslvm_handle_open_physical_volume_files_file_io_pool( |
126 | 2.04k | handle, |
127 | 2.04k | file_io_pool, |
128 | 2.04k | NULL ) != 1 ) |
129 | 1.46k | { |
130 | 1.46k | goto on_error_libvslvm_handle; |
131 | 1.46k | } |
132 | 587 | if( libvslvm_handle_get_volume_group( |
133 | 587 | handle, |
134 | 587 | &volume_group, |
135 | 587 | NULL ) == 1 ) |
136 | 587 | { |
137 | 587 | if( libvslvm_volume_group_get_number_of_logical_volumes( |
138 | 587 | volume_group, |
139 | 587 | &number_of_logical_volumes, |
140 | 587 | NULL ) != 1 ) |
141 | 0 | { |
142 | 0 | goto on_error_libvslvm_volume_group; |
143 | 0 | } |
144 | 587 | if( number_of_logical_volumes > 0 ) |
145 | 415 | { |
146 | 415 | if( libvslvm_volume_group_get_logical_volume( |
147 | 415 | volume_group, |
148 | 415 | 0, |
149 | 415 | &logical_volume, |
150 | 415 | NULL ) != 1 ) |
151 | 243 | { |
152 | 243 | goto on_error_libvslvm_volume_group; |
153 | 243 | } |
154 | 172 | if( libvslvm_logical_volume_get_name_size( |
155 | 172 | logical_volume, |
156 | 172 | &string_size, |
157 | 172 | NULL ) != 1 ) |
158 | 0 | { |
159 | 0 | goto on_error_libvslvm_logical_volume; |
160 | 0 | } |
161 | 172 | if( libvslvm_logical_volume_get_name( |
162 | 172 | logical_volume, |
163 | 172 | string, |
164 | 172 | 64, |
165 | 172 | NULL ) != 1 ) |
166 | 1 | { |
167 | 1 | goto on_error_libvslvm_logical_volume; |
168 | 1 | } |
169 | 171 | if( libvslvm_logical_volume_get_identifier_size( |
170 | 171 | logical_volume, |
171 | 171 | &string_size, |
172 | 171 | NULL ) != 1 ) |
173 | 0 | { |
174 | 0 | goto on_error_libvslvm_logical_volume; |
175 | 0 | } |
176 | 171 | if( libvslvm_logical_volume_get_identifier( |
177 | 171 | logical_volume, |
178 | 171 | string, |
179 | 171 | 64, |
180 | 171 | NULL ) != 1 ) |
181 | 0 | { |
182 | 0 | goto on_error_libvslvm_logical_volume; |
183 | 0 | } |
184 | 171 | if( libvslvm_logical_volume_get_number_of_segments( |
185 | 171 | logical_volume, |
186 | 171 | &number_of_segments, |
187 | 171 | NULL ) != 1 ) |
188 | 0 | { |
189 | 0 | goto on_error_libvslvm_logical_volume; |
190 | 0 | } |
191 | 171 | if( libvslvm_logical_volume_get_size( |
192 | 171 | logical_volume, |
193 | 171 | &volume_size, |
194 | 171 | NULL ) != 1 ) |
195 | 0 | { |
196 | 0 | goto on_error_libvslvm_logical_volume; |
197 | 0 | } |
198 | 171 | for( read_iterator = 0; |
199 | 7.53k | read_iterator < 128; |
200 | 7.36k | read_iterator++ ) |
201 | 7.48k | { |
202 | 7.48k | if( volume_offset >= volume_size ) |
203 | 29 | { |
204 | 29 | break; |
205 | 29 | } |
206 | 7.45k | if( libvslvm_logical_volume_read_buffer_at_offset( |
207 | 7.45k | logical_volume, |
208 | 7.45k | buffer, |
209 | 7.45k | 497, |
210 | 7.45k | volume_offset, |
211 | 7.45k | NULL ) == -1 ) |
212 | 90 | { |
213 | 90 | goto on_error_libvslvm_logical_volume; |
214 | 90 | } |
215 | 7.36k | volume_offset += 497; |
216 | 7.36k | } |
217 | 172 | on_error_libvslvm_logical_volume: |
218 | 172 | libvslvm_logical_volume_free( |
219 | 172 | &logical_volume, |
220 | 172 | NULL ); |
221 | 172 | } |
222 | 587 | on_error_libvslvm_volume_group: |
223 | 587 | libvslvm_volume_group_free( |
224 | 587 | &volume_group, |
225 | 587 | NULL ); |
226 | 587 | } |
227 | 587 | libvslvm_handle_close( |
228 | 587 | handle, |
229 | 587 | NULL ); |
230 | | |
231 | 3.89k | on_error_libvslvm_handle: |
232 | 3.89k | libvslvm_handle_free( |
233 | 3.89k | &handle, |
234 | 3.89k | NULL ); |
235 | | |
236 | 3.89k | on_error_libbfio: |
237 | | /* Note that on error the handle still has a reference to file_io_pool |
238 | | * that will be closed. Therefore the file IO pool and handle need to |
239 | | * be freed after closing or freeing the handle. |
240 | | */ |
241 | 3.89k | if( file_io_pool != NULL ) |
242 | 3.89k | { |
243 | 3.89k | libbfio_pool_free( |
244 | 3.89k | &file_io_pool, |
245 | 3.89k | NULL ); |
246 | 3.89k | } |
247 | 3.89k | if( file_io_handle != NULL ) |
248 | 1.84k | { |
249 | 1.84k | libbfio_handle_free( |
250 | 1.84k | &file_io_handle, |
251 | | NULL ); |
252 | 1.84k | } |
253 | 3.89k | return( 0 ); |
254 | 3.89k | } |
255 | | |
256 | | } /* extern "C" */ |
257 | | |