/src/libphdi/ossfuzz/handle_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libphdi handle type |
3 | | * |
4 | | * Copyright (C) 2015-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_libphdi.h" |
31 | | |
32 | | #if !defined( LIBPHDI_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 | | LIBPHDI_EXTERN \ |
38 | | int libphdi_handle_open_file_io_handle( |
39 | | libphdi_handle_t *handle, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libphdi_error_t **error ); |
43 | | |
44 | | /* Opens the extent data files using a Basic File IO (bfio) pool |
45 | | * This function assumes the extent data files are in same order as defined by the descriptor file |
46 | | * Returns 1 if successful or -1 on error |
47 | | */ |
48 | | LIBPHDI_EXTERN \ |
49 | | int libphdi_handle_open_extent_data_files_file_io_pool( |
50 | | libphdi_handle_t *handle, |
51 | | libbfio_pool_t *file_io_pool, |
52 | | libphdi_error_t **error ); |
53 | | |
54 | | #endif /* !defined( LIBPHDI_HAVE_BFIO ) */ |
55 | | |
56 | | int LLVMFuzzerTestOneInput( |
57 | | const uint8_t *data, |
58 | | size_t size ) |
59 | 813 | { |
60 | 813 | uint8_t buffer[ 512 ]; |
61 | 813 | uint8_t utf8_string[ 64 ]; |
62 | 813 | uint16_t utf16_string[ 64 ]; |
63 | | |
64 | 813 | libbfio_handle_t *file_io_handle = NULL; |
65 | 813 | libbfio_pool_t *file_io_pool = NULL; |
66 | 813 | libphdi_handle_t *handle = NULL; |
67 | 813 | off64_t media_offset = 0; |
68 | 813 | size64_t media_size = 0; |
69 | 813 | size_t string_size = 0; |
70 | 813 | int entry_index = 0; |
71 | 813 | int number_of_extents = 0; |
72 | 813 | int number_of_snapshots = 0; |
73 | 813 | int read_iterator = 0; |
74 | | |
75 | 813 | if( libbfio_memory_range_initialize( |
76 | 813 | &file_io_handle, |
77 | 813 | NULL ) != 1 ) |
78 | 0 | { |
79 | 0 | return( 0 ); |
80 | 0 | } |
81 | 813 | if( libbfio_memory_range_set( |
82 | 813 | file_io_handle, |
83 | 813 | (uint8_t *) data, |
84 | 813 | size, |
85 | 813 | NULL ) != 1 ) |
86 | 0 | { |
87 | 0 | goto on_error_libbfio; |
88 | 0 | } |
89 | 813 | if( libbfio_pool_initialize( |
90 | 813 | &file_io_pool, |
91 | 813 | 0, |
92 | 813 | 0, |
93 | 813 | NULL ) != 1 ) |
94 | 0 | { |
95 | 0 | goto on_error_libbfio; |
96 | 0 | } |
97 | 813 | if( libphdi_handle_initialize( |
98 | 813 | &handle, |
99 | 813 | NULL ) != 1 ) |
100 | 0 | { |
101 | 0 | goto on_error_libbfio; |
102 | 0 | } |
103 | 813 | if( libphdi_handle_open_file_io_handle( |
104 | 813 | handle, |
105 | 813 | file_io_handle, |
106 | 813 | LIBPHDI_OPEN_READ, |
107 | 813 | NULL ) != 1 ) |
108 | 813 | { |
109 | 813 | goto on_error_libphdi; |
110 | 813 | } |
111 | 0 | if( libbfio_pool_append_handle( |
112 | 0 | file_io_pool, |
113 | 0 | &entry_index, |
114 | 0 | file_io_handle, |
115 | 0 | LIBBFIO_OPEN_READ, |
116 | 0 | NULL ) != 1 ) |
117 | 0 | { |
118 | 0 | goto on_error_libphdi; |
119 | 0 | } |
120 | | /* The file IO pool takes over management of the file IO handle |
121 | | */ |
122 | 0 | file_io_handle = NULL; |
123 | | |
124 | | /* Note that the extent data files can only be opened if the number |
125 | | * of file IO handles in the pool matches the number of images |
126 | | * defined in the descriptor file. |
127 | | */ |
128 | 0 | libphdi_handle_open_extent_data_files_file_io_pool( |
129 | 0 | handle, |
130 | 0 | file_io_pool, |
131 | 0 | NULL ); |
132 | |
|
133 | 0 | if( libphdi_handle_get_utf8_name_size( |
134 | 0 | handle, |
135 | 0 | &string_size, |
136 | 0 | NULL ) == -1 ) |
137 | 0 | { |
138 | 0 | goto on_error_libphdi; |
139 | 0 | } |
140 | 0 | if( libphdi_handle_get_utf8_name( |
141 | 0 | handle, |
142 | 0 | utf8_string, |
143 | 0 | 64, |
144 | 0 | NULL ) == -1 ) |
145 | 0 | { |
146 | 0 | goto on_error_libphdi; |
147 | 0 | } |
148 | 0 | if( libphdi_handle_get_utf16_name_size( |
149 | 0 | handle, |
150 | 0 | &string_size, |
151 | 0 | NULL ) == -1 ) |
152 | 0 | { |
153 | 0 | goto on_error_libphdi; |
154 | 0 | } |
155 | 0 | if( libphdi_handle_get_utf16_name( |
156 | 0 | handle, |
157 | 0 | utf16_string, |
158 | 0 | 64, |
159 | 0 | NULL ) == -1 ) |
160 | 0 | { |
161 | 0 | goto on_error_libphdi; |
162 | 0 | } |
163 | 0 | if( libphdi_handle_get_number_of_extents( |
164 | 0 | handle, |
165 | 0 | &number_of_extents, |
166 | 0 | NULL ) != 1 ) |
167 | 0 | { |
168 | 0 | goto on_error_libphdi; |
169 | 0 | } |
170 | 0 | if( libphdi_handle_get_number_of_snapshots( |
171 | 0 | handle, |
172 | 0 | &number_of_snapshots, |
173 | 0 | NULL ) != 1 ) |
174 | 0 | { |
175 | 0 | goto on_error_libphdi; |
176 | 0 | } |
177 | 0 | if( libphdi_handle_get_media_size( |
178 | 0 | handle, |
179 | 0 | &media_size, |
180 | 0 | NULL ) != 1 ) |
181 | 0 | { |
182 | 0 | goto on_error_libphdi; |
183 | 0 | } |
184 | 0 | for( read_iterator = 0; |
185 | 0 | read_iterator < 128; |
186 | 0 | read_iterator++ ) |
187 | 0 | { |
188 | 0 | if( media_offset >= media_size ) |
189 | 0 | { |
190 | 0 | break; |
191 | 0 | } |
192 | 0 | if( libphdi_handle_read_buffer_at_offset( |
193 | 0 | handle, |
194 | 0 | buffer, |
195 | 0 | 497, |
196 | 0 | media_offset, |
197 | 0 | NULL ) == -1 ) |
198 | 0 | { |
199 | 0 | goto on_error_libphdi; |
200 | 0 | } |
201 | 0 | media_offset += 497; |
202 | 0 | } |
203 | 0 | libphdi_handle_close( |
204 | 0 | handle, |
205 | 0 | NULL ); |
206 | |
|
207 | 813 | on_error_libphdi: |
208 | 813 | libphdi_handle_free( |
209 | 813 | &handle, |
210 | 813 | NULL ); |
211 | | |
212 | 813 | on_error_libbfio: |
213 | | /* Note that on error the handle still has a reference to file_io_pool |
214 | | * that will be closed. Therefore the file IO pool and handle need to |
215 | | * be freed after closing or freeing the handle. |
216 | | */ |
217 | 813 | if( file_io_pool != NULL ) |
218 | 813 | { |
219 | 813 | libbfio_pool_free( |
220 | 813 | &file_io_pool, |
221 | 813 | NULL ); |
222 | 813 | } |
223 | 813 | if( file_io_handle != NULL ) |
224 | 813 | { |
225 | 813 | libbfio_handle_free( |
226 | 813 | &file_io_handle, |
227 | | NULL ); |
228 | 813 | } |
229 | 813 | return( 0 ); |
230 | 813 | } |
231 | | |
232 | | } /* extern "C" */ |