Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libcreg/ossfuzz/value_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libcreg value type
3
 *
4
 * Copyright (C) 2013-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_libcreg.h"
31
32
#if !defined( LIBCREG_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
LIBCREG_EXTERN \
38
int libcreg_file_open_file_io_handle(
39
      libcreg_file_t *file,
40
      libbfio_handle_t *file_io_handle,
41
      int access_flags,
42
      libcreg_error_t **error );
43
44
#endif /* !defined( LIBCREG_HAVE_BFIO ) */
45
46
/* Limits the number of sub keys to walk per key
47
 */
48
2.41k
#define FUZZER_KEY_LIMIT  8
49
50
/* Limits the number of values to walk per key
51
 */
52
2.42k
#define FUZZER_VALUE_LIMIT  8
53
54
/* Limits the size of the names used to look up values
55
 */
56
1.58k
#define FUZZER_LOOKUP_STRING_LIMIT  ( (size_t) 0x4000 )
57
58
/* The ASCII codepages supported by libcreg_file_set_ascii_codepage
59
 */
60
static const int ascii_codepages[] = {
61
  LIBCREG_CODEPAGE_ASCII,
62
  LIBCREG_CODEPAGE_WINDOWS_874,
63
  LIBCREG_CODEPAGE_WINDOWS_932,
64
  LIBCREG_CODEPAGE_WINDOWS_936,
65
  LIBCREG_CODEPAGE_WINDOWS_949,
66
  LIBCREG_CODEPAGE_WINDOWS_950,
67
  LIBCREG_CODEPAGE_WINDOWS_1250,
68
  LIBCREG_CODEPAGE_WINDOWS_1251,
69
  LIBCREG_CODEPAGE_WINDOWS_1252,
70
  LIBCREG_CODEPAGE_WINDOWS_1253,
71
  LIBCREG_CODEPAGE_WINDOWS_1254,
72
  LIBCREG_CODEPAGE_WINDOWS_1255,
73
  LIBCREG_CODEPAGE_WINDOWS_1256,
74
  LIBCREG_CODEPAGE_WINDOWS_1257,
75
  LIBCREG_CODEPAGE_WINDOWS_1258,
76
};
77
78
static const size_t number_of_ascii_codepages =
79
    sizeof( ascii_codepages ) / sizeof( ascii_codepages[ 0 ] );
80
81
/* Decodes the value using the accessors of the value type
82
 */
83
static void value_decode_fuzzer(
84
     libcreg_value_t *value )
85
2.53k
{
86
2.53k
  uint8_t name[ 2048 ];
87
2.53k
  uint8_t utf8_string[ 8192 ];
88
2.53k
  uint8_t value_data[ 8192 ];
89
2.53k
  uint16_t utf16_string[ 4096 ];
90
91
2.53k
  off64_t offset            = 0;
92
2.53k
  size_t name_size          = 0;
93
2.53k
  size_t utf8_name_size     = 0;
94
2.53k
  size_t utf16_name_size    = 0;
95
2.53k
  size_t value_data_size    = 0;
96
2.53k
  uint32_t value_type       = 0;
97
2.53k
  uint32_t value_32bit      = 0;
98
2.53k
  uint64_t value_64bit      = 0;
99
100
2.53k
  libcreg_value_is_corrupted(
101
2.53k
   value,
102
2.53k
   NULL );
103
104
2.53k
  libcreg_value_get_offset(
105
2.53k
   value,
106
2.53k
   &offset,
107
2.53k
   NULL );
108
109
2.53k
  libcreg_value_get_value_type(
110
2.53k
   value,
111
2.53k
   &value_type,
112
2.53k
   NULL );
113
114
  /* The typed accessors return an error for an unsupported
115
   * value type, which is part of what this exercises
116
   */
117
2.53k
  libcreg_value_get_value_32bit(
118
2.53k
   value,
119
2.53k
   &value_32bit,
120
2.53k
   NULL );
121
122
2.53k
  libcreg_value_get_value_64bit(
123
2.53k
   value,
124
2.53k
   &value_64bit,
125
2.53k
   NULL );
126
127
  /* The name may be absent, e.g. the default value
128
   */
129
2.53k
  libcreg_value_get_name_size(
130
2.53k
   value,
131
2.53k
   &name_size,
132
2.53k
   NULL );
133
134
2.53k
  libcreg_value_get_utf8_name_size(
135
2.53k
   value,
136
2.53k
   &utf8_name_size,
137
2.53k
   NULL );
138
139
2.53k
  libcreg_value_get_utf16_name_size(
140
2.53k
   value,
141
2.53k
   &utf16_name_size,
142
2.53k
   NULL );
143
144
2.53k
  if( name_size > 0 )
145
1.00k
  {
146
1.00k
    libcreg_value_get_name(
147
1.00k
     value,
148
1.00k
     name,
149
1.00k
     2048,
150
1.00k
     NULL );
151
1.00k
  }
152
2.53k
  if( utf8_name_size > 0 )
153
1.00k
  {
154
1.00k
    libcreg_value_get_utf8_name(
155
1.00k
     value,
156
1.00k
     utf8_string,
157
1.00k
     8192,
158
1.00k
     NULL );
159
1.00k
  }
160
2.53k
  if( utf16_name_size > 0 )
161
1.00k
  {
162
1.00k
    libcreg_value_get_utf16_name(
163
1.00k
     value,
164
1.00k
     utf16_string,
165
1.00k
     4096,
166
1.00k
     NULL );
167
1.00k
  }
168
2.53k
  libcreg_value_get_value_data_size(
169
2.53k
   value,
170
2.53k
   &value_data_size,
171
2.53k
   NULL );
172
173
2.53k
  libcreg_value_get_value_data(
174
2.53k
   value,
175
2.53k
   value_data,
176
2.53k
   8192,
177
2.53k
   NULL );
178
179
2.53k
  libcreg_value_get_value_utf8_string_size(
180
2.53k
   value,
181
2.53k
   &value_data_size,
182
2.53k
   NULL );
183
184
2.53k
  libcreg_value_get_value_utf8_string(
185
2.53k
   value,
186
2.53k
   utf8_string,
187
2.53k
   8192,
188
2.53k
   NULL );
189
190
2.53k
  libcreg_value_get_value_utf16_string_size(
191
2.53k
   value,
192
2.53k
   &value_data_size,
193
2.53k
   NULL );
194
195
2.53k
  libcreg_value_get_value_utf16_string(
196
2.53k
   value,
197
2.53k
   utf16_string,
198
2.53k
   4096,
199
2.53k
   NULL );
200
201
2.53k
  libcreg_value_get_value_binary_data_size(
202
2.53k
   value,
203
2.53k
   &value_data_size,
204
2.53k
   NULL );
205
206
2.53k
  libcreg_value_get_value_binary_data(
207
2.53k
   value,
208
2.53k
   value_data,
209
2.53k
   8192,
210
2.53k
   NULL );
211
2.53k
}
212
213
int LLVMFuzzerTestOneInput(
214
     const uint8_t *data,
215
     size_t size )
216
1.66k
{
217
1.66k
  libbfio_handle_t *file_io_handle = NULL;
218
1.66k
  libcreg_file_t *file             = NULL;
219
1.66k
  libcreg_key_t *key               = NULL;
220
1.66k
  libcreg_key_t *sub_key           = NULL;
221
1.66k
  libcreg_value_t *value           = NULL;
222
1.66k
  size_t query_size                = 0;
223
1.66k
  int number_of_sub_keys           = 0;
224
1.66k
  int number_of_values             = 0;
225
1.66k
  int sub_key_index                = 0;
226
1.66k
  int value_index                  = 0;
227
228
1.66k
  if( libbfio_memory_range_initialize(
229
1.66k
       &file_io_handle,
230
1.66k
       NULL ) != 1 )
231
0
  {
232
0
    return( 0 );
233
0
  }
234
1.66k
  if( libbfio_memory_range_set(
235
1.66k
       file_io_handle,
236
1.66k
       (uint8_t *) data,
237
1.66k
       size,
238
1.66k
       NULL ) != 1 )
239
0
  {
240
0
    goto on_error_libbfio;
241
0
  }
242
1.66k
  if( libcreg_file_initialize(
243
1.66k
       &file,
244
1.66k
       NULL ) != 1 )
245
0
  {
246
0
    goto on_error_libbfio;
247
0
  }
248
1.66k
  if( libcreg_file_open_file_io_handle(
249
1.66k
       file,
250
1.66k
       file_io_handle,
251
1.66k
       LIBCREG_OPEN_READ,
252
1.66k
       NULL ) != 1 )
253
226
  {
254
226
    goto on_error_libcreg_file;
255
226
  }
256
  /* The codepage used to convert value names and value data
257
   * is selected from the fuzz data
258
   */
259
1.43k
  if( size > 0 )
260
1.43k
  {
261
1.43k
    libcreg_file_set_ascii_codepage(
262
1.43k
     file,
263
1.43k
     ascii_codepages[ data[ 0 ] % number_of_ascii_codepages ],
264
1.43k
     NULL );
265
1.43k
  }
266
1.43k
  if( size > 0 )
267
1.43k
  {
268
1.43k
    query_size = size;
269
270
1.43k
    if( query_size > FUZZER_LOOKUP_STRING_LIMIT )
271
149
    {
272
149
      query_size = FUZZER_LOOKUP_STRING_LIMIT;
273
149
    }
274
1.43k
  }
275
1.43k
  if( libcreg_file_get_root_key(
276
1.43k
       file,
277
1.43k
       &key,
278
1.43k
       NULL ) == 1 )
279
678
  {
280
    /* Retrieves the default value
281
     */
282
678
    libcreg_key_get_value_by_utf8_name(
283
678
     key,
284
678
     NULL,
285
678
     0,
286
678
     &value,
287
678
     NULL );
288
289
678
    if( value != NULL )
290
239
    {
291
239
      value_decode_fuzzer(
292
239
       value );
293
294
239
      libcreg_value_free(
295
239
       &value,
296
239
       NULL );
297
239
    }
298
678
    value = NULL;
299
300
678
    if( libcreg_key_get_number_of_values(
301
678
         key,
302
678
         &number_of_values,
303
678
         NULL ) == 1 )
304
678
    {
305
678
      for( value_index = 0;
306
1.38k
           value_index < number_of_values;
307
704
           value_index++ )
308
735
      {
309
735
        if( value_index >= FUZZER_VALUE_LIMIT )
310
31
        {
311
31
          break;
312
31
        }
313
704
        value = NULL;
314
315
704
        if( libcreg_key_get_value_by_index(
316
704
             key,
317
704
             value_index,
318
704
             &value,
319
704
             NULL ) != 1 )
320
0
        {
321
0
          continue;
322
0
        }
323
704
        value_decode_fuzzer(
324
704
         value );
325
326
704
        libcreg_value_free(
327
704
         &value,
328
704
         NULL );
329
704
      }
330
678
    }
331
678
    if( libcreg_key_get_number_of_sub_keys(
332
678
         key,
333
678
         &number_of_sub_keys,
334
678
         NULL ) == 1 )
335
678
    {
336
678
      for( sub_key_index = 0;
337
3.00k
           sub_key_index < number_of_sub_keys;
338
2.32k
           sub_key_index++ )
339
2.41k
      {
340
2.41k
        if( sub_key_index >= FUZZER_KEY_LIMIT )
341
89
        {
342
89
          break;
343
89
        }
344
2.32k
        sub_key = NULL;
345
346
2.32k
        if( libcreg_key_get_sub_key_by_index(
347
2.32k
             key,
348
2.32k
             sub_key_index,
349
2.32k
             &sub_key,
350
2.32k
             NULL ) != 1 )
351
1.51k
        {
352
1.51k
          continue;
353
1.51k
        }
354
812
        if( libcreg_key_get_number_of_values(
355
812
             sub_key,
356
812
             &number_of_values,
357
812
             NULL ) == 1 )
358
812
        {
359
812
          for( value_index = 0;
360
2.40k
               value_index < number_of_values;
361
1.59k
               value_index++ )
362
1.68k
          {
363
1.68k
            if( value_index >= FUZZER_VALUE_LIMIT )
364
97
            {
365
97
              break;
366
97
            }
367
1.59k
            value = NULL;
368
369
1.59k
            if( libcreg_key_get_value_by_index(
370
1.59k
                 sub_key,
371
1.59k
                 value_index,
372
1.59k
                 &value,
373
1.59k
                 NULL ) != 1 )
374
0
            {
375
0
              continue;
376
0
            }
377
1.59k
            value_decode_fuzzer(
378
1.59k
             value );
379
380
1.59k
            libcreg_value_free(
381
1.59k
             &value,
382
1.59k
             NULL );
383
1.59k
          }
384
812
        }
385
        /* Retrieves a value by a name derived from
386
         * the fuzz data. The data is not necessarily
387
         * valid UTF-8 or UTF-16 which exercises the
388
         * validation and conversion error paths
389
         * in the library
390
         */
391
812
        if( size > 0 )
392
812
        {
393
812
          value = NULL;
394
395
812
          libcreg_key_get_value_by_utf8_name(
396
812
           sub_key,
397
812
           data,
398
812
           query_size,
399
812
           &value,
400
812
           NULL );
401
402
812
          if( value != NULL )
403
0
          {
404
0
            value_decode_fuzzer(
405
0
             value );
406
407
0
            libcreg_value_free(
408
0
             &value,
409
0
             NULL );
410
0
          }
411
812
          value = NULL;
412
413
812
          if( query_size >= 2 )
414
812
          {
415
812
            libcreg_key_get_value_by_utf16_name(
416
812
             sub_key,
417
812
             ( const uint16_t * ) data,
418
812
             ( query_size / 2 ),
419
812
             &value,
420
812
             NULL );
421
422
812
            if( value != NULL )
423
0
            {
424
0
              value_decode_fuzzer(
425
0
               value );
426
427
0
              libcreg_value_free(
428
0
               &value,
429
0
               NULL );
430
0
            }
431
812
          }
432
812
        }
433
812
        libcreg_key_free(
434
812
         &sub_key,
435
812
         NULL );
436
812
      }
437
678
    }
438
678
    libcreg_key_free(
439
678
     &key,
440
678
     NULL );
441
678
  }
442
1.43k
  libcreg_file_close(
443
1.43k
   file,
444
1.43k
   NULL );
445
446
1.66k
on_error_libcreg_file:
447
1.66k
  libcreg_file_free(
448
1.66k
   &file,
449
1.66k
   NULL );
450
451
1.66k
on_error_libbfio:
452
1.66k
  libbfio_handle_free(
453
1.66k
   &file_io_handle,
454
1.66k
   NULL );
455
456
1.66k
  return( 0 );
457
1.66k
}
458
459
} /* extern "C" */