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