Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libcreg/ossfuzz/key_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libcreg key 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
1.69k
#define FUZZER_KEY_LIMIT  8
49
50
/* Limits the size of the names and paths used to look up keys
51
 */
52
795
#define FUZZER_LOOKUP_STRING_LIMIT  ( (size_t) 0x4000 )
53
54
/* The ASCII codepages supported by libcreg_file_set_ascii_codepage
55
 */
56
static const int ascii_codepages[] = {
57
  LIBCREG_CODEPAGE_ASCII,
58
  LIBCREG_CODEPAGE_WINDOWS_874,
59
  LIBCREG_CODEPAGE_WINDOWS_932,
60
  LIBCREG_CODEPAGE_WINDOWS_936,
61
  LIBCREG_CODEPAGE_WINDOWS_949,
62
  LIBCREG_CODEPAGE_WINDOWS_950,
63
  LIBCREG_CODEPAGE_WINDOWS_1250,
64
  LIBCREG_CODEPAGE_WINDOWS_1251,
65
  LIBCREG_CODEPAGE_WINDOWS_1252,
66
  LIBCREG_CODEPAGE_WINDOWS_1253,
67
  LIBCREG_CODEPAGE_WINDOWS_1254,
68
  LIBCREG_CODEPAGE_WINDOWS_1255,
69
  LIBCREG_CODEPAGE_WINDOWS_1256,
70
  LIBCREG_CODEPAGE_WINDOWS_1257,
71
  LIBCREG_CODEPAGE_WINDOWS_1258,
72
};
73
74
static const size_t number_of_ascii_codepages =
75
    sizeof( ascii_codepages ) / sizeof( ascii_codepages[ 0 ] );
76
77
int LLVMFuzzerTestOneInput(
78
     const uint8_t *data,
79
     size_t size )
80
1.65k
{
81
1.65k
  uint8_t name[ 2048 ];
82
1.65k
  uint8_t utf8_string[ 8192 ];
83
1.65k
  uint16_t utf16_string[ 4096 ];
84
85
1.65k
  libbfio_handle_t *file_io_handle = NULL;
86
1.65k
  libcreg_file_t *file             = NULL;
87
1.65k
  libcreg_key_t *key               = NULL;
88
1.65k
  libcreg_key_t *sub_key           = NULL;
89
1.65k
  libcreg_value_t *value           = NULL;
90
1.65k
  off64_t offset                   = 0;
91
1.65k
  size_t name_size                 = 0;
92
1.65k
  size_t query_size                = 0;
93
1.65k
  int number_of_sub_keys           = 0;
94
1.65k
  int sub_key_index                = 0;
95
96
1.65k
  if( libbfio_memory_range_initialize(
97
1.65k
       &file_io_handle,
98
1.65k
       NULL ) != 1 )
99
0
  {
100
0
    return( 0 );
101
0
  }
102
1.65k
  if( libbfio_memory_range_set(
103
1.65k
       file_io_handle,
104
1.65k
       (uint8_t *) data,
105
1.65k
       size,
106
1.65k
       NULL ) != 1 )
107
0
  {
108
0
    goto on_error_libbfio;
109
0
  }
110
1.65k
  if( libcreg_file_initialize(
111
1.65k
       &file,
112
1.65k
       NULL ) != 1 )
113
0
  {
114
0
    goto on_error_libbfio;
115
0
  }
116
1.65k
  if( libcreg_file_open_file_io_handle(
117
1.65k
       file,
118
1.65k
       file_io_handle,
119
1.65k
       LIBCREG_OPEN_READ,
120
1.65k
       NULL ) != 1 )
121
202
  {
122
202
    goto on_error_libcreg_file;
123
202
  }
124
  /* The codepage used to convert key names and value data
125
   * is selected from the fuzz data
126
   */
127
1.45k
  if( size > 0 )
128
1.45k
  {
129
1.45k
    libcreg_file_set_ascii_codepage(
130
1.45k
     file,
131
1.45k
     ascii_codepages[ data[ 0 ] % number_of_ascii_codepages ],
132
1.45k
     NULL );
133
1.45k
  }
134
1.45k
  if( libcreg_file_get_root_key(
135
1.45k
       file,
136
1.45k
       &key,
137
1.45k
       NULL ) == 1 )
138
720
  {
139
720
    libcreg_key_is_corrupted(
140
720
     key,
141
720
     NULL );
142
143
720
    libcreg_key_get_offset(
144
720
     key,
145
720
     &offset,
146
720
     NULL );
147
148
720
    libcreg_key_get_name_size(
149
720
     key,
150
720
     &name_size,
151
720
     NULL );
152
153
720
    libcreg_key_get_name(
154
720
     key,
155
720
     name,
156
720
     2048,
157
720
     NULL );
158
159
720
    libcreg_key_get_utf8_name_size(
160
720
     key,
161
720
     &name_size,
162
720
     NULL );
163
164
720
    libcreg_key_get_utf8_name(
165
720
     key,
166
720
     utf8_string,
167
720
     8192,
168
720
     NULL );
169
170
720
    libcreg_key_get_utf16_name_size(
171
720
     key,
172
720
     &name_size,
173
720
     NULL );
174
175
720
    libcreg_key_get_utf16_name(
176
720
     key,
177
720
     utf16_string,
178
720
     4096,
179
720
     NULL );
180
181
    /* Looks up sub keys and values using names and paths derived
182
     * from the fuzz data. The data is not necessarily valid UTF-8
183
     * or UTF-16 which exercises the validation and conversion
184
     * error paths in the library
185
     */
186
720
    if( size > 0 )
187
720
    {
188
720
      query_size = size;
189
190
720
      if( query_size > FUZZER_LOOKUP_STRING_LIMIT )
191
75
      {
192
75
        query_size = FUZZER_LOOKUP_STRING_LIMIT;
193
75
      }
194
720
      sub_key = NULL;
195
196
720
      libcreg_key_get_sub_key_by_utf8_name(
197
720
       key,
198
720
       data,
199
720
       query_size,
200
720
       &sub_key,
201
720
       NULL );
202
203
720
      if( sub_key != NULL )
204
0
      {
205
0
        libcreg_key_free(
206
0
         &sub_key,
207
0
         NULL );
208
0
      }
209
720
      sub_key = NULL;
210
211
720
      libcreg_key_get_sub_key_by_utf8_path(
212
720
       key,
213
720
       data,
214
720
       query_size,
215
720
       &sub_key,
216
720
       NULL );
217
218
720
      if( sub_key != NULL )
219
0
      {
220
0
        libcreg_key_free(
221
0
         &sub_key,
222
0
         NULL );
223
0
      }
224
720
      value = NULL;
225
226
      /* Retrieves the default value
227
       */
228
720
      libcreg_key_get_value_by_utf8_name(
229
720
       key,
230
720
       NULL,
231
720
       0,
232
720
       &value,
233
720
       NULL );
234
235
720
      if( value != NULL )
236
96
      {
237
96
        libcreg_value_free(
238
96
         &value,
239
96
         NULL );
240
96
      }
241
720
      value = NULL;
242
243
720
      libcreg_key_get_value_by_utf8_name(
244
720
       key,
245
720
       data,
246
720
       query_size,
247
720
       &value,
248
720
       NULL );
249
250
720
      if( value != NULL )
251
0
      {
252
0
        libcreg_value_free(
253
0
         &value,
254
0
         NULL );
255
0
      }
256
720
      if( query_size >= 2 )
257
720
      {
258
720
        sub_key = NULL;
259
260
720
        libcreg_key_get_sub_key_by_utf16_name(
261
720
         key,
262
720
         ( const uint16_t * ) data,
263
720
         ( query_size / 2 ),
264
720
         &sub_key,
265
720
         NULL );
266
267
720
        if( sub_key != NULL )
268
0
        {
269
0
          libcreg_key_free(
270
0
           &sub_key,
271
0
           NULL );
272
0
        }
273
720
        sub_key = NULL;
274
275
720
        libcreg_key_get_sub_key_by_utf16_path(
276
720
         key,
277
720
         ( const uint16_t * ) data,
278
720
         ( query_size / 2 ),
279
720
         &sub_key,
280
720
         NULL );
281
282
720
        if( sub_key != NULL )
283
0
        {
284
0
          libcreg_key_free(
285
0
           &sub_key,
286
0
           NULL );
287
0
        }
288
720
        value = NULL;
289
290
720
        libcreg_key_get_value_by_utf16_name(
291
720
         key,
292
720
         ( const uint16_t * ) data,
293
720
         ( query_size / 2 ),
294
720
         &value,
295
720
         NULL );
296
297
720
        if( value != NULL )
298
0
        {
299
0
          libcreg_value_free(
300
0
           &value,
301
0
           NULL );
302
0
        }
303
720
      }
304
720
    }
305
720
    if( libcreg_key_get_number_of_sub_keys(
306
720
         key,
307
720
         &number_of_sub_keys,
308
720
         NULL ) == 1 )
309
720
    {
310
720
      for( sub_key_index = 0;
311
2.35k
           sub_key_index < number_of_sub_keys;
312
1.63k
           sub_key_index++ )
313
1.69k
      {
314
1.69k
        if( sub_key_index >= FUZZER_KEY_LIMIT )
315
67
        {
316
67
          break;
317
67
        }
318
1.63k
        sub_key = NULL;
319
320
1.63k
        if( libcreg_key_get_sub_key_by_index(
321
1.63k
             key,
322
1.63k
             sub_key_index,
323
1.63k
             &sub_key,
324
1.63k
             NULL ) != 1 )
325
1.26k
        {
326
1.26k
          continue;
327
1.26k
        }
328
368
        libcreg_key_is_corrupted(
329
368
         sub_key,
330
368
         NULL );
331
332
368
        libcreg_key_get_offset(
333
368
         sub_key,
334
368
         &offset,
335
368
         NULL );
336
337
368
        libcreg_key_get_name_size(
338
368
         sub_key,
339
368
         &name_size,
340
368
         NULL );
341
342
368
        libcreg_key_get_name(
343
368
         sub_key,
344
368
         name,
345
368
         2048,
346
368
         NULL );
347
348
368
        libcreg_key_get_utf8_name_size(
349
368
         sub_key,
350
368
         &name_size,
351
368
         NULL );
352
353
368
        libcreg_key_get_utf8_name(
354
368
         sub_key,
355
368
         utf8_string,
356
368
         8192,
357
368
         NULL );
358
359
368
        libcreg_key_get_utf16_name_size(
360
368
         sub_key,
361
368
         &name_size,
362
368
         NULL );
363
364
368
        libcreg_key_get_utf16_name(
365
368
         sub_key,
366
368
         utf16_string,
367
368
         4096,
368
368
         NULL );
369
370
368
        if( size > 0 )
371
368
        {
372
368
          value = NULL;
373
374
368
          libcreg_key_get_value_by_utf8_name(
375
368
           sub_key,
376
368
           data,
377
368
           query_size,
378
368
           &value,
379
368
           NULL );
380
381
368
          if( value != NULL )
382
0
          {
383
0
            libcreg_value_free(
384
0
             &value,
385
0
             NULL );
386
0
          }
387
368
          if( query_size >= 2 )
388
368
          {
389
368
            value = NULL;
390
391
368
            libcreg_key_get_value_by_utf16_name(
392
368
             sub_key,
393
368
             ( const uint16_t * ) data,
394
368
             ( query_size / 2 ),
395
368
             &value,
396
368
             NULL );
397
398
368
            if( value != NULL )
399
0
            {
400
0
              libcreg_value_free(
401
0
               &value,
402
0
               NULL );
403
0
            }
404
368
          }
405
368
        }
406
368
        libcreg_key_free(
407
368
         &sub_key,
408
368
         NULL );
409
368
      }
410
720
    }
411
720
    libcreg_key_free(
412
720
     &key,
413
720
     NULL );
414
720
  }
415
1.45k
  libcreg_file_close(
416
1.45k
   file,
417
1.45k
   NULL );
418
419
1.65k
on_error_libcreg_file:
420
1.65k
  libcreg_file_free(
421
1.65k
   &file,
422
1.65k
   NULL );
423
424
1.65k
on_error_libbfio:
425
1.65k
  libbfio_handle_free(
426
1.65k
   &file_io_handle,
427
1.65k
   NULL );
428
429
1.65k
  return( 0 );
430
1.65k
}
431
432
} /* extern "C" */