/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 | 460 | { |
60 | 460 | uint8_t uuid[ 16 ]; |
61 | 460 | uint8_t utf8_string[ 64 ]; |
62 | 460 | uint16_t utf16_string[ 64 ]; |
63 | | |
64 | 460 | libbfio_handle_t *file_io_handle = NULL; |
65 | 460 | libbfio_pool_t *file_io_pool = NULL; |
66 | 460 | libfvde_volume_t *volume = NULL; |
67 | 460 | libfvde_volume_group_t *volume_group = NULL; |
68 | 460 | size_t string_size = 0; |
69 | 460 | int entry_index = 0; |
70 | 460 | int number_of_volumes = 0; |
71 | | |
72 | 460 | if( libbfio_memory_range_initialize( |
73 | 460 | &file_io_handle, |
74 | 460 | NULL ) != 1 ) |
75 | 0 | { |
76 | 0 | return( 0 ); |
77 | 0 | } |
78 | 460 | if( libbfio_memory_range_set( |
79 | 460 | file_io_handle, |
80 | 460 | (uint8_t *) data, |
81 | 460 | size, |
82 | 460 | NULL ) != 1 ) |
83 | 0 | { |
84 | 0 | goto on_error_libbfio; |
85 | 0 | } |
86 | 460 | if( libbfio_pool_initialize( |
87 | 460 | &file_io_pool, |
88 | 460 | 0, |
89 | 460 | 0, |
90 | 460 | NULL ) != 1 ) |
91 | 0 | { |
92 | 0 | goto on_error_libbfio; |
93 | 0 | } |
94 | 460 | if( libfvde_volume_initialize( |
95 | 460 | &volume, |
96 | 460 | NULL ) != 1 ) |
97 | 0 | { |
98 | 0 | goto on_error_libbfio; |
99 | 0 | } |
100 | 460 | if( libfvde_volume_open_file_io_handle( |
101 | 460 | volume, |
102 | 460 | file_io_handle, |
103 | 460 | LIBFVDE_OPEN_READ, |
104 | 460 | NULL ) != 1 ) |
105 | 460 | { |
106 | 460 | goto on_error_libfvde_volume; |
107 | 460 | } |
108 | 0 | if( libbfio_pool_append_handle( |
109 | 0 | file_io_pool, |
110 | 0 | &entry_index, |
111 | 0 | file_io_handle, |
112 | 0 | LIBBFIO_OPEN_READ, |
113 | 0 | NULL ) != 1 ) |
114 | 0 | { |
115 | 0 | goto on_error_libfvde_volume; |
116 | 0 | } |
117 | | /* The file IO pool takes over management of the file IO handle |
118 | | */ |
119 | 0 | file_io_handle = NULL; |
120 | |
|
121 | 0 | if( libfvde_volume_open_physical_volume_files_file_io_pool( |
122 | 0 | volume, |
123 | 0 | file_io_pool, |
124 | 0 | NULL ) != 1 ) |
125 | 0 | { |
126 | 0 | goto on_error_libfvde_volume; |
127 | 0 | } |
128 | 0 | if( libfvde_volume_get_volume_group( |
129 | 0 | volume, |
130 | 0 | &volume_group, |
131 | 0 | NULL ) == 1 ) |
132 | 0 | { |
133 | 0 | if( libfvde_volume_group_get_identifier( |
134 | 0 | volume_group, |
135 | 0 | uuid, |
136 | 0 | 64, |
137 | 0 | NULL ) != 1 ) |
138 | 0 | { |
139 | 0 | goto on_error_libfvde_volume_group; |
140 | 0 | } |
141 | 0 | if( libfvde_volume_group_get_utf8_name_size( |
142 | 0 | volume_group, |
143 | 0 | &string_size, |
144 | 0 | NULL ) != 1 ) |
145 | 0 | { |
146 | 0 | goto on_error_libfvde_volume_group; |
147 | 0 | } |
148 | 0 | if( libfvde_volume_group_get_utf8_name( |
149 | 0 | volume_group, |
150 | 0 | utf8_string, |
151 | 0 | 64, |
152 | 0 | NULL ) != 1 ) |
153 | 0 | { |
154 | 0 | goto on_error_libfvde_volume_group; |
155 | 0 | } |
156 | 0 | if( libfvde_volume_group_get_utf16_name_size( |
157 | 0 | volume_group, |
158 | 0 | &string_size, |
159 | 0 | NULL ) != 1 ) |
160 | 0 | { |
161 | 0 | goto on_error_libfvde_volume_group; |
162 | 0 | } |
163 | 0 | if( libfvde_volume_group_get_utf16_name( |
164 | 0 | volume_group, |
165 | 0 | utf16_string, |
166 | 0 | 64, |
167 | 0 | NULL ) != 1 ) |
168 | 0 | { |
169 | 0 | goto on_error_libfvde_volume_group; |
170 | 0 | } |
171 | 0 | if( libfvde_volume_group_get_number_of_physical_volumes( |
172 | 0 | volume_group, |
173 | 0 | &number_of_volumes, |
174 | 0 | NULL ) != 1 ) |
175 | 0 | { |
176 | 0 | goto on_error_libfvde_volume_group; |
177 | 0 | } |
178 | 0 | if( libfvde_volume_group_get_number_of_logical_volumes( |
179 | 0 | volume_group, |
180 | 0 | &number_of_volumes, |
181 | 0 | NULL ) != 1 ) |
182 | 0 | { |
183 | 0 | goto on_error_libfvde_volume_group; |
184 | 0 | } |
185 | 0 | on_error_libfvde_volume_group: |
186 | 0 | libfvde_volume_group_free( |
187 | 0 | &volume_group, |
188 | 0 | NULL ); |
189 | 0 | } |
190 | 0 | libfvde_volume_close( |
191 | 0 | volume, |
192 | 0 | NULL ); |
193 | |
|
194 | 460 | on_error_libfvde_volume: |
195 | 460 | libfvde_volume_free( |
196 | 460 | &volume, |
197 | 460 | NULL ); |
198 | | |
199 | 460 | on_error_libbfio: |
200 | | /* Note that on error the volume still has a reference to file_io_pool |
201 | | * that will be closed. Therefore the file IO pool and handle need to |
202 | | * be freed after closing or freeing the volume. |
203 | | */ |
204 | 460 | if( file_io_pool != NULL ) |
205 | 460 | { |
206 | 460 | libbfio_pool_free( |
207 | 460 | &file_io_pool, |
208 | 460 | NULL ); |
209 | 460 | } |
210 | 460 | if( file_io_handle != NULL ) |
211 | 460 | { |
212 | 460 | libbfio_handle_free( |
213 | 460 | &file_io_handle, |
214 | | NULL ); |
215 | 460 | } |
216 | 460 | return( 0 ); |
217 | 460 | } |
218 | | |
219 | | } /* extern "C" */ |
220 | | |