/src/libfplist/ossfuzz/property_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libfplist property type |
3 | | * |
4 | | * Copyright (C) 2016-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 | | #include <stdlib.h> |
26 | | |
27 | | /* Note that some of the OSS-Fuzz engines use C++ |
28 | | */ |
29 | | extern "C" { |
30 | | |
31 | | #include "ossfuzz_libfplist.h" |
32 | | |
33 | | /* Limits on the traversal and buffer sizes to bound the time a single input takes |
34 | | */ |
35 | 0 | #define FUZZER_ARRAY_ENTRY_LIMIT 8 |
36 | 0 | #define FUZZER_DATA_SIZE_LIMIT ( (size_t) 0x00100000 ) |
37 | 48.3k | #define FUZZER_DICTIONARY_ENTRY_LIMIT 8 |
38 | 44.8k | #define FUZZER_KEY_NAME_LENGTH_LIMIT 128 |
39 | 14.4k | #define FUZZER_LOOKUP_STRING_LIMIT 128 |
40 | 12.2k | #define FUZZER_MAX_DEPTH 8 |
41 | 11.9k | #define FUZZER_PROPERTY_LIMIT 64 |
42 | | |
43 | | typedef struct fuzzer_key_name fuzzer_key_name_t; |
44 | | |
45 | | struct fuzzer_key_name |
46 | | { |
47 | | /* The name of a dictionary entry |
48 | | */ |
49 | | const uint8_t *name; |
50 | | |
51 | | /* The size of the name |
52 | | */ |
53 | | size_t name_length; |
54 | | |
55 | | /* The offset from which to start searching for the next name |
56 | | */ |
57 | | size_t next_offset; |
58 | | }; |
59 | | |
60 | | /* Finds the next name within a <key>NAME</key> element of the fuzz data |
61 | | * The names are used as sub property lookup names |
62 | | * Returns 1 if successful or 0 when no more names are available |
63 | | */ |
64 | | int fuzzer_key_name_get( |
65 | | const uint8_t *data, |
66 | | size_t size, |
67 | | size_t offset, |
68 | | fuzzer_key_name_t *key_name ) |
69 | 47.5k | { |
70 | 47.5k | size_t index = 0; |
71 | 47.5k | size_t key_length = 0; |
72 | 47.5k | size_t key_start = 0; |
73 | | |
74 | 47.5k | if( ( data == NULL ) |
75 | 47.5k | || ( key_name == NULL ) ) |
76 | 0 | { |
77 | 0 | return( 0 ); |
78 | 0 | } |
79 | 47.5k | if( offset > size ) |
80 | 0 | { |
81 | 0 | return( 0 ); |
82 | 0 | } |
83 | 47.5k | index = offset; |
84 | | |
85 | 54.6k | while( index + 5 <= size ) |
86 | 54.6k | { |
87 | | /* Search for the <key> element |
88 | | */ |
89 | 70.4M | while( ( index + 5 <= size ) |
90 | 70.4M | && ( ( data[ index ] != '<' ) |
91 | 334k | || ( data[ index + 1 ] != 'k' ) |
92 | 54.3k | || ( data[ index + 2 ] != 'e' ) |
93 | 50.4k | || ( data[ index + 3 ] != 'y' ) |
94 | 49.2k | || ( data[ index + 4 ] != '>' ) ) ) |
95 | 70.3M | { |
96 | 70.3M | index += 1; |
97 | 70.3M | } |
98 | 54.6k | if( index + 5 > size ) |
99 | 8.98k | { |
100 | 8.98k | return( 0 ); |
101 | 8.98k | } |
102 | 45.6k | key_start = index + 5; |
103 | | |
104 | | /* Search for the </key> element |
105 | | */ |
106 | 45.6k | index = key_start; |
107 | | |
108 | 37.3M | while( index + 6 <= size ) |
109 | 37.3M | { |
110 | 37.3M | if( ( data[ index ] == '<' ) |
111 | 50.4k | && ( data[ index + 1 ] == '/' ) |
112 | 48.9k | && ( data[ index + 2 ] == 'k' ) |
113 | 46.1k | && ( data[ index + 3 ] == 'e' ) |
114 | 45.7k | && ( data[ index + 4 ] == 'y' ) |
115 | 45.3k | && ( data[ index + 5 ] == '>' ) ) |
116 | 45.1k | { |
117 | 45.1k | break; |
118 | 45.1k | } |
119 | 37.2M | index += 1; |
120 | 37.2M | } |
121 | 45.6k | if( index + 6 > size ) |
122 | 512 | { |
123 | 512 | return( 0 ); |
124 | 512 | } |
125 | 45.1k | key_length = index - key_start; |
126 | | |
127 | 45.1k | if( ( key_length > 0 ) |
128 | 44.8k | && ( key_length <= FUZZER_KEY_NAME_LENGTH_LIMIT ) ) |
129 | 38.0k | { |
130 | 38.0k | key_name->name = &( data[ key_start ] ); |
131 | 38.0k | key_name->name_length = key_length; |
132 | 38.0k | key_name->next_offset = index + 6; |
133 | | |
134 | 38.0k | return( 1 ); |
135 | 38.0k | } |
136 | 7.06k | index = key_start; |
137 | 7.06k | } |
138 | 0 | return( 0 ); |
139 | 47.5k | } |
140 | | |
141 | | /* Decodes a property and its sub properties, array entries and values |
142 | | * using the typed accessors |
143 | | * Returns 1 if successful, 0 when the traversal is bounded or -1 on error |
144 | | */ |
145 | | int property_decode_fuzzer( |
146 | | const uint8_t *data, |
147 | | size_t size, |
148 | | int depth, |
149 | | libfplist_property_t *property, |
150 | | int *property_count ) |
151 | 12.2k | { |
152 | 12.2k | fuzzer_key_name_t fuzzer_key_name; |
153 | 12.2k | uint8_t uuid_byte_stream[ 16 ]; |
154 | | |
155 | 12.2k | libfplist_property_t *array_entry = NULL; |
156 | 12.2k | libfplist_property_t *sub_property = NULL; |
157 | 12.2k | uint8_t *value_data = NULL; |
158 | 12.2k | uint8_t *value_string = NULL; |
159 | 12.2k | size_t data_size = 0; |
160 | 12.2k | size_t lookup_string_length = 0; |
161 | 12.2k | size_t next_offset = 0; |
162 | 12.2k | size_t string_size = 0; |
163 | 12.2k | uint64_t value_64bit = 0; |
164 | 12.2k | uint8_t value_byte = 0; |
165 | 12.2k | int number_of_entries = 0; |
166 | 12.2k | int entry_index = 0; |
167 | 12.2k | int number_of_key_names = 0; |
168 | 12.2k | int value_type = 0; |
169 | | |
170 | 12.2k | if( ( data == NULL ) |
171 | 12.2k | || ( property == NULL ) |
172 | 12.2k | || ( property_count == NULL ) ) |
173 | 0 | { |
174 | 0 | return( -1 ); |
175 | 0 | } |
176 | 12.2k | if( ( depth >= FUZZER_MAX_DEPTH ) |
177 | 11.9k | || ( *property_count >= FUZZER_PROPERTY_LIMIT ) ) |
178 | 526 | { |
179 | 526 | return( 0 ); |
180 | 526 | } |
181 | 11.7k | *property_count += 1; |
182 | | |
183 | | /* Determines the value type |
184 | | */ |
185 | 11.7k | if( libfplist_property_get_value_type( |
186 | 11.7k | property, |
187 | 11.7k | &value_type, |
188 | 11.7k | NULL ) != 1 ) |
189 | 1.42k | { |
190 | 1.42k | return( -1 ); |
191 | 1.42k | } |
192 | 10.3k | value_byte ^= (uint8_t) value_type; |
193 | | |
194 | | /* Retrieves the value (binary) data |
195 | | */ |
196 | 10.3k | if( libfplist_property_get_value_data_size( |
197 | 10.3k | property, |
198 | 10.3k | &data_size, |
199 | 10.3k | NULL ) == 1 ) |
200 | 96 | { |
201 | 96 | if( ( data_size > 0 ) |
202 | 0 | && ( data_size <= FUZZER_DATA_SIZE_LIMIT ) ) |
203 | 0 | { |
204 | 0 | value_data = (uint8_t *) malloc( data_size ); |
205 | |
|
206 | 0 | if( value_data != NULL ) |
207 | 0 | { |
208 | 0 | if( libfplist_property_get_value_data( |
209 | 0 | property, |
210 | 0 | value_data, |
211 | 0 | data_size, |
212 | 0 | NULL ) == 1 ) |
213 | 0 | { |
214 | 0 | value_byte ^= value_data[ 0 ]; |
215 | 0 | } |
216 | 0 | free( |
217 | 0 | value_data ); |
218 | |
|
219 | 0 | value_data = NULL; |
220 | 0 | } |
221 | 0 | } |
222 | 96 | } |
223 | | /* Retrieves an integer value |
224 | | */ |
225 | 10.3k | if( libfplist_property_get_value_integer( |
226 | 10.3k | property, |
227 | 10.3k | &value_64bit, |
228 | 10.3k | NULL ) == 1 ) |
229 | 517 | { |
230 | 517 | value_byte ^= (uint8_t) value_64bit; |
231 | 517 | } |
232 | | /* Retrieves a string value |
233 | | */ |
234 | 10.3k | if( libfplist_property_get_value_string( |
235 | 10.3k | property, |
236 | 10.3k | &value_string, |
237 | 10.3k | &string_size, |
238 | 10.3k | NULL ) == 1 ) |
239 | 2.65k | { |
240 | | /* Retrieves the string value with a non-NULL string |
241 | | * to exercise the value already set error branch |
242 | | */ |
243 | 2.65k | libfplist_property_get_value_string( |
244 | 2.65k | property, |
245 | 2.65k | &value_string, |
246 | 2.65k | &string_size, |
247 | 2.65k | NULL ); |
248 | 2.65k | if( string_size > 0 ) |
249 | 2.65k | { |
250 | 2.65k | value_byte ^= value_string[ 0 ]; |
251 | 2.65k | } |
252 | 2.65k | free( |
253 | 2.65k | value_string ); |
254 | | |
255 | 2.65k | value_string = NULL; |
256 | 2.65k | } |
257 | | /* Retrieves a UUID (GUID) string value |
258 | | */ |
259 | 10.3k | if( libfplist_property_value_uuid_string_copy_to_byte_stream( |
260 | 10.3k | property, |
261 | 10.3k | uuid_byte_stream, |
262 | 10.3k | sizeof( uuid_byte_stream ), |
263 | 10.3k | NULL ) == 1 ) |
264 | 121 | { |
265 | 121 | value_byte ^= uuid_byte_stream[ 0 ]; |
266 | 121 | } |
267 | | /* Retrieves the array entries |
268 | | */ |
269 | 10.3k | if( libfplist_property_get_array_number_of_entries( |
270 | 10.3k | property, |
271 | 10.3k | &number_of_entries, |
272 | 10.3k | NULL ) == 1 |
273 | 133 | && ( number_of_entries > 0 ) ) |
274 | 0 | { |
275 | 0 | int number_of_entries_to_decode = number_of_entries; |
276 | |
|
277 | 0 | if( number_of_entries_to_decode > FUZZER_ARRAY_ENTRY_LIMIT ) |
278 | 0 | { |
279 | 0 | number_of_entries_to_decode = FUZZER_ARRAY_ENTRY_LIMIT; |
280 | 0 | } |
281 | 0 | for( entry_index = 0; |
282 | 0 | entry_index < number_of_entries_to_decode; |
283 | 0 | entry_index++ ) |
284 | 0 | { |
285 | 0 | if( libfplist_property_get_array_entry_by_index( |
286 | 0 | property, |
287 | 0 | entry_index, |
288 | 0 | &array_entry, |
289 | 0 | NULL ) == 1 ) |
290 | 0 | { |
291 | | /* Retrieves the array entry for the same index |
292 | | * with a non-NULL array entry to exercise the |
293 | | * value already set error branch |
294 | | */ |
295 | 0 | libfplist_property_get_array_entry_by_index( |
296 | 0 | property, |
297 | 0 | entry_index, |
298 | 0 | &array_entry, |
299 | 0 | NULL ); |
300 | 0 | property_decode_fuzzer( |
301 | 0 | data, |
302 | 0 | size, |
303 | 0 | depth + 1, |
304 | 0 | array_entry, |
305 | 0 | property_count ); |
306 | 0 | libfplist_property_free( |
307 | 0 | &array_entry, |
308 | 0 | NULL ); |
309 | 0 | } |
310 | 0 | array_entry = NULL; |
311 | 0 | } |
312 | | /* Retrieves the array entry for an index beyond the range |
313 | | * to exercise the not found branch |
314 | | */ |
315 | 0 | if( libfplist_property_get_array_entry_by_index( |
316 | 0 | property, |
317 | 0 | number_of_entries, |
318 | 0 | &array_entry, |
319 | 0 | NULL ) == 1 ) |
320 | 0 | { |
321 | 0 | libfplist_property_free( |
322 | 0 | &array_entry, |
323 | 0 | NULL ); |
324 | 0 | } |
325 | 0 | array_entry = NULL; |
326 | | |
327 | | /* Retrieves the array entry for a negative index |
328 | | * to exercise the value out of bounds error branch |
329 | | */ |
330 | 0 | if( libfplist_property_get_array_entry_by_index( |
331 | 0 | property, |
332 | 0 | (int) -1, |
333 | 0 | &array_entry, |
334 | 0 | NULL ) == 1 ) |
335 | 0 | { |
336 | 0 | libfplist_property_free( |
337 | 0 | &array_entry, |
338 | 0 | NULL ); |
339 | 0 | } |
340 | 0 | array_entry = NULL; |
341 | 0 | } |
342 | | /* Retrieves the sub properties for the specific UTF-8 encoded names |
343 | | * Uses the <key>NAME</key> names found in the fuzz data such that |
344 | | * both the found and not found branches are exercised |
345 | | */ |
346 | 10.3k | next_offset = 0; |
347 | | |
348 | 48.3k | while( ( number_of_key_names < FUZZER_DICTIONARY_ENTRY_LIMIT ) |
349 | 47.5k | && ( fuzzer_key_name_get( |
350 | 47.5k | data, |
351 | 47.5k | size, |
352 | 47.5k | next_offset, |
353 | 47.5k | &fuzzer_key_name ) == 1 ) ) |
354 | 38.0k | { |
355 | 38.0k | if( libfplist_property_get_sub_property_by_utf8_name( |
356 | 38.0k | property, |
357 | 38.0k | fuzzer_key_name.name, |
358 | 38.0k | fuzzer_key_name.name_length, |
359 | 38.0k | &sub_property, |
360 | 38.0k | NULL ) == 1 ) |
361 | 11.0k | { |
362 | | /* Retrieves the sub property for the same name |
363 | | * with a non-NULL sub property to exercise the |
364 | | * value already set error branch |
365 | | */ |
366 | 11.0k | libfplist_property_get_sub_property_by_utf8_name( |
367 | 11.0k | property, |
368 | 11.0k | fuzzer_key_name.name, |
369 | 11.0k | fuzzer_key_name.name_length, |
370 | 11.0k | &sub_property, |
371 | 11.0k | NULL ); |
372 | 11.0k | property_decode_fuzzer( |
373 | 11.0k | data, |
374 | 11.0k | size, |
375 | 11.0k | depth + 1, |
376 | 11.0k | sub_property, |
377 | 11.0k | property_count ); |
378 | 11.0k | libfplist_property_free( |
379 | 11.0k | &sub_property, |
380 | 11.0k | NULL ); |
381 | 11.0k | } |
382 | 38.0k | sub_property = NULL; |
383 | 38.0k | next_offset = fuzzer_key_name.next_offset; |
384 | | |
385 | 38.0k | number_of_key_names += 1; |
386 | 38.0k | } |
387 | | /* Also uses a lookup name derived from the fuzz data. |
388 | | * The data is not necessarily a valid UTF-8 string |
389 | | * which exercises the name comparison branch in the library |
390 | | */ |
391 | 10.3k | if( size > 0 ) |
392 | 10.3k | { |
393 | 10.3k | lookup_string_length = size; |
394 | | |
395 | 10.3k | if( lookup_string_length > FUZZER_LOOKUP_STRING_LIMIT ) |
396 | 4.17k | { |
397 | 4.17k | lookup_string_length = FUZZER_LOOKUP_STRING_LIMIT; |
398 | 4.17k | } |
399 | 10.3k | libfplist_property_get_sub_property_by_utf8_name( |
400 | 10.3k | property, |
401 | 10.3k | data, |
402 | 10.3k | lookup_string_length, |
403 | 10.3k | &sub_property, |
404 | 10.3k | NULL ); |
405 | 10.3k | } |
406 | 10.3k | sub_property = NULL; |
407 | | |
408 | | /* Use the decoded values as a sink to keep them live |
409 | | */ |
410 | 10.3k | if( value_byte == 0 ) |
411 | 1 | { |
412 | | /* No action required |
413 | | */ |
414 | 1 | } |
415 | 10.3k | return( 1 ); |
416 | 11.7k | } |
417 | | |
418 | | int LLVMFuzzerTestOneInput( |
419 | | const uint8_t *data, |
420 | | size_t size ) |
421 | 2.81k | { |
422 | 2.81k | libfplist_property_t *property = NULL; |
423 | 2.81k | libfplist_property_list_t *property_list = NULL; |
424 | 2.81k | int property_count = 0; |
425 | | |
426 | 2.81k | if( libfplist_property_list_initialize( |
427 | 2.81k | &property_list, |
428 | 2.81k | NULL ) != 1 ) |
429 | 0 | { |
430 | 0 | return( 0 ); |
431 | 0 | } |
432 | 2.81k | if( libfplist_property_list_copy_from_byte_stream( |
433 | 2.81k | property_list, |
434 | 2.81k | data, |
435 | 2.81k | size, |
436 | 2.81k | NULL ) == 1 ) |
437 | 1.42k | { |
438 | 1.42k | if( libfplist_property_list_get_root_property( |
439 | 1.42k | property_list, |
440 | 1.42k | &property, |
441 | 1.42k | NULL ) == 1 ) |
442 | 1.30k | { |
443 | | /* Decodes the root property and its sub properties, |
444 | | * array entries and values using the typed accessors |
445 | | */ |
446 | 1.30k | property_decode_fuzzer( |
447 | 1.30k | data, |
448 | 1.30k | size, |
449 | 1.30k | 0, |
450 | 1.30k | property, |
451 | 1.30k | &property_count ); |
452 | 1.30k | libfplist_property_free( |
453 | 1.30k | &property, |
454 | 1.30k | NULL ); |
455 | 1.30k | } |
456 | 1.42k | property = NULL; |
457 | 1.42k | } |
458 | 2.81k | libfplist_property_list_free( |
459 | 2.81k | &property_list, |
460 | 2.81k | NULL ); |
461 | | |
462 | 2.81k | return( 0 ); |
463 | 2.81k | } |
464 | | |
465 | | } /* extern "C" */ |