/src/libpff/ossfuzz/multi_value_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libpff multi value type |
3 | | * |
4 | | * Copyright (C) 2008-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_libpff.h" |
31 | | |
32 | | #if !defined( LIBPFF_HAVE_BFIO ) |
33 | | |
34 | | /* Opens a file using a Basic File IO (bfio) handle |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | LIBPFF_EXTERN \ |
38 | | int libpff_file_open_file_io_handle( |
39 | | libpff_file_t *file, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libpff_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBPFF_HAVE_BFIO ) */ |
45 | | |
46 | | /* Limits the number of sub items to walk to find a multi value |
47 | | */ |
48 | 7.68k | #define FUZZER_SUB_ITEM_LIMIT 8 |
49 | | |
50 | | /* Limits the number of record sets to query |
51 | | */ |
52 | 1.01k | #define FUZZER_RECORD_SET_LIMIT 4 |
53 | | |
54 | | /* Limits the number of record entries to query |
55 | | */ |
56 | 9.30k | #define FUZZER_RECORD_ENTRY_LIMIT 16 |
57 | | |
58 | | /* Limits the number of values to query |
59 | | */ |
60 | 2.36k | #define FUZZER_MULTI_VALUE_LIMIT 8 |
61 | | |
62 | | int LLVMFuzzerTestOneInput( |
63 | | const uint8_t *data, |
64 | | size_t size ) |
65 | 3.10k | { |
66 | 3.10k | libbfio_handle_t *file_io_handle = NULL; |
67 | 3.10k | libpff_file_t *file = NULL; |
68 | 3.10k | libpff_item_t *root_folder = NULL; |
69 | 3.10k | libpff_item_t *sub_item = NULL; |
70 | 3.10k | libpff_multi_value_t *multi_value = NULL; |
71 | 3.10k | libpff_record_entry_t *record_entry = NULL; |
72 | 3.10k | libpff_record_set_t *record_set = NULL; |
73 | 3.10k | uint32_t value_type = 0; |
74 | 3.10k | uint32_t value_32bit = 0; |
75 | 3.10k | uint64_t value_64bit = 0; |
76 | 3.10k | size_t value_data_size = 0; |
77 | 3.10k | uint8_t *value_data = NULL; |
78 | 3.10k | int number_of_values = 0; |
79 | 3.10k | int number_of_sub_items = 0; |
80 | 3.10k | int number_of_record_sets = 0; |
81 | 3.10k | int number_of_entries = 0; |
82 | 3.10k | int sub_item_index = 0; |
83 | 3.10k | int record_set_index = 0; |
84 | 3.10k | int entry_index = 0; |
85 | 3.10k | int value_index = 0; |
86 | | |
87 | 3.10k | if( libbfio_memory_range_initialize( |
88 | 3.10k | &file_io_handle, |
89 | 3.10k | NULL ) != 1 ) |
90 | 0 | { |
91 | 0 | return( 0 ); |
92 | 0 | } |
93 | 3.10k | if( libbfio_memory_range_set( |
94 | 3.10k | file_io_handle, |
95 | 3.10k | (uint8_t *) data, |
96 | 3.10k | size, |
97 | 3.10k | NULL ) != 1 ) |
98 | 0 | { |
99 | 0 | goto on_error_libbfio; |
100 | 0 | } |
101 | 3.10k | if( libpff_file_initialize( |
102 | 3.10k | &file, |
103 | 3.10k | NULL ) != 1 ) |
104 | 0 | { |
105 | 0 | goto on_error_libbfio; |
106 | 0 | } |
107 | 3.10k | if( libpff_file_open_file_io_handle( |
108 | 3.10k | file, |
109 | 3.10k | file_io_handle, |
110 | 3.10k | LIBPFF_OPEN_READ, |
111 | 3.10k | NULL ) != 1 ) |
112 | 915 | { |
113 | 915 | goto on_error_libpff_file; |
114 | 915 | } |
115 | 2.19k | if( libpff_file_get_root_folder( |
116 | 2.19k | file, |
117 | 2.19k | &root_folder, |
118 | 2.19k | NULL ) == 1 ) |
119 | 1.98k | { |
120 | | /* Walk the sub items of the root folder to find a multi value |
121 | | */ |
122 | 1.98k | if( libpff_item_get_number_of_sub_items( |
123 | 1.98k | root_folder, |
124 | 1.98k | &number_of_sub_items, |
125 | 1.98k | NULL ) == 1 ) |
126 | 1.98k | { |
127 | 1.98k | for( sub_item_index = 0; |
128 | 9.50k | sub_item_index < number_of_sub_items; |
129 | 7.51k | sub_item_index++ ) |
130 | 7.68k | { |
131 | 7.68k | sub_item = NULL; |
132 | | |
133 | 7.68k | if( sub_item_index >= FUZZER_SUB_ITEM_LIMIT ) |
134 | 170 | { |
135 | 170 | break; |
136 | 170 | } |
137 | 7.51k | if( libpff_item_get_sub_item( |
138 | 7.51k | root_folder, |
139 | 7.51k | sub_item_index, |
140 | 7.51k | &sub_item, |
141 | 7.51k | NULL ) != 1 ) |
142 | 0 | { |
143 | 0 | continue; |
144 | 0 | } |
145 | 7.51k | if( libpff_item_get_number_of_record_sets( |
146 | 7.51k | sub_item, |
147 | 7.51k | &number_of_record_sets, |
148 | 7.51k | NULL ) == 1 ) |
149 | 734 | { |
150 | 734 | for( record_set_index = 0; |
151 | 1.68k | record_set_index < number_of_record_sets; |
152 | 953 | record_set_index++ ) |
153 | 1.01k | { |
154 | 1.01k | record_set = NULL; |
155 | | |
156 | 1.01k | if( record_set_index >= FUZZER_RECORD_SET_LIMIT ) |
157 | 58 | { |
158 | 58 | break; |
159 | 58 | } |
160 | 953 | if( libpff_item_get_record_set_by_index( |
161 | 953 | sub_item, |
162 | 953 | record_set_index, |
163 | 953 | &record_set, |
164 | 953 | NULL ) != 1 ) |
165 | 0 | { |
166 | 0 | continue; |
167 | 0 | } |
168 | 953 | if( libpff_record_set_get_number_of_entries( |
169 | 953 | record_set, |
170 | 953 | &number_of_entries, |
171 | 953 | NULL ) == 1 |
172 | 953 | && number_of_entries > 0 ) |
173 | 953 | { |
174 | 953 | for( entry_index = 0; |
175 | 10.1k | entry_index < number_of_entries; |
176 | 9.14k | entry_index++ ) |
177 | 9.30k | { |
178 | 9.30k | record_entry = NULL; |
179 | | |
180 | 9.30k | if( entry_index >= FUZZER_RECORD_ENTRY_LIMIT ) |
181 | 154 | { |
182 | 154 | break; |
183 | 154 | } |
184 | 9.14k | if( libpff_record_set_get_entry_by_index( |
185 | 9.14k | record_set, |
186 | 9.14k | entry_index, |
187 | 9.14k | &record_entry, |
188 | 9.14k | NULL ) == 1 |
189 | 9.14k | && record_entry != NULL ) |
190 | 9.14k | { |
191 | 9.14k | multi_value = NULL; |
192 | | |
193 | 9.14k | if( libpff_record_entry_get_multi_value( |
194 | 9.14k | record_entry, |
195 | 9.14k | &multi_value, |
196 | 9.14k | NULL ) == 1 |
197 | 357 | && multi_value != NULL ) |
198 | 357 | { |
199 | 357 | number_of_values = 0; |
200 | | |
201 | 357 | if( libpff_multi_value_get_number_of_values( |
202 | 357 | multi_value, |
203 | 357 | &number_of_values, |
204 | 357 | NULL ) == 1 ) |
205 | 357 | { |
206 | 357 | for( value_index = 0; |
207 | 2.49k | value_index < number_of_values; |
208 | 2.13k | value_index++ ) |
209 | 2.36k | { |
210 | 2.36k | if( value_index >= FUZZER_MULTI_VALUE_LIMIT ) |
211 | 229 | { |
212 | 229 | break; |
213 | 229 | } |
214 | 2.13k | value_type = 0; |
215 | 2.13k | value_data = NULL; |
216 | 2.13k | value_data_size = 0; |
217 | | |
218 | 2.13k | if( libpff_multi_value_get_value( |
219 | 2.13k | multi_value, |
220 | 2.13k | value_index, |
221 | 2.13k | &value_type, |
222 | 2.13k | &value_data, |
223 | 2.13k | &( value_data_size ), |
224 | 2.13k | NULL ) == 1 |
225 | 2.13k | ) |
226 | 2.13k | { |
227 | 2.13k | if( value_data_size > 0 |
228 | 2.00k | && value_data != NULL ) |
229 | 2.00k | { |
230 | | /* Use the value as a sink to keep it live |
231 | | */ |
232 | 2.00k | value_32bit = value_data[ 0 ]; |
233 | 2.00k | } |
234 | 2.13k | } |
235 | 0 | else if( libpff_multi_value_get_value_32bit( |
236 | 0 | multi_value, |
237 | 0 | value_index, |
238 | 0 | &value_32bit, |
239 | 0 | NULL ) != 1 ) |
240 | 0 | { |
241 | 0 | libpff_multi_value_get_value_64bit( |
242 | 0 | multi_value, |
243 | 0 | value_index, |
244 | 0 | &value_64bit, |
245 | 0 | NULL ); |
246 | 0 | } |
247 | 2.13k | } |
248 | 357 | } |
249 | 357 | libpff_multi_value_free( |
250 | 357 | &multi_value, |
251 | 357 | NULL ); |
252 | 357 | } |
253 | 9.14k | libpff_record_entry_free( |
254 | 9.14k | &record_entry, |
255 | 9.14k | NULL ); |
256 | 9.14k | } |
257 | 9.14k | } |
258 | 953 | } |
259 | 953 | libpff_record_set_free( |
260 | 953 | &record_set, |
261 | 953 | NULL ); |
262 | 953 | } |
263 | 734 | } |
264 | 7.51k | libpff_item_free( |
265 | 7.51k | &sub_item, |
266 | 7.51k | NULL ); |
267 | 7.51k | } |
268 | 1.98k | } |
269 | 1.98k | libpff_item_free( |
270 | 1.98k | &root_folder, |
271 | 1.98k | NULL ); |
272 | 1.98k | } |
273 | 2.19k | libpff_file_close( |
274 | 2.19k | file, |
275 | 2.19k | NULL ); |
276 | | |
277 | 3.10k | on_error_libpff_file: |
278 | 3.10k | libpff_file_free( |
279 | 3.10k | &file, |
280 | 3.10k | NULL ); |
281 | | |
282 | 3.10k | on_error_libbfio: |
283 | 3.10k | libbfio_handle_free( |
284 | 3.10k | &file_io_handle, |
285 | 3.10k | NULL ); |
286 | | |
287 | 3.10k | return( 0 ); |
288 | 3.10k | } |
289 | | |
290 | | } /* extern "C" */ |