Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libcreg/ossfuzz/file_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libcreg file 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 size of the paths used to look up keys
47
 */
48
1.37k
#define FUZZER_PATH_SIZE_LIMIT  ( (size_t) 0x4000 )
49
50
/* The ASCII codepages supported by libcreg_file_set_ascii_codepage
51
 */
52
static const int ascii_codepages[] = {
53
  LIBCREG_CODEPAGE_ASCII,
54
  LIBCREG_CODEPAGE_WINDOWS_874,
55
  LIBCREG_CODEPAGE_WINDOWS_932,
56
  LIBCREG_CODEPAGE_WINDOWS_936,
57
  LIBCREG_CODEPAGE_WINDOWS_949,
58
  LIBCREG_CODEPAGE_WINDOWS_950,
59
  LIBCREG_CODEPAGE_WINDOWS_1250,
60
  LIBCREG_CODEPAGE_WINDOWS_1251,
61
  LIBCREG_CODEPAGE_WINDOWS_1252,
62
  LIBCREG_CODEPAGE_WINDOWS_1253,
63
  LIBCREG_CODEPAGE_WINDOWS_1254,
64
  LIBCREG_CODEPAGE_WINDOWS_1255,
65
  LIBCREG_CODEPAGE_WINDOWS_1256,
66
  LIBCREG_CODEPAGE_WINDOWS_1257,
67
  LIBCREG_CODEPAGE_WINDOWS_1258,
68
};
69
70
static const size_t number_of_ascii_codepages =
71
    sizeof( ascii_codepages ) / sizeof( ascii_codepages[ 0 ] );
72
73
int LLVMFuzzerTestOneInput(
74
     const uint8_t *data,
75
     size_t size )
76
1.29k
{
77
1.29k
  libbfio_handle_t *file_io_handle = NULL;
78
1.29k
  libcreg_file_t *file             = NULL;
79
1.29k
  libcreg_key_t *key               = NULL;
80
1.29k
  size_t query_size                = 0;
81
1.29k
  uint32_t file_type               = 0;
82
1.29k
  uint16_t major_version           = 0;
83
1.29k
  uint16_t minor_version           = 0;
84
1.29k
  int ascii_codepage               = 0;
85
1.29k
  int codepage_index               = 0;
86
87
1.29k
  if( libbfio_memory_range_initialize(
88
1.29k
       &file_io_handle,
89
1.29k
       NULL ) != 1 )
90
0
  {
91
0
    return( 0 );
92
0
  }
93
1.29k
  if( libbfio_memory_range_set(
94
1.29k
       file_io_handle,
95
1.29k
       (uint8_t *) data,
96
1.29k
       size,
97
1.29k
       NULL ) != 1 )
98
0
  {
99
0
    goto on_error_libbfio;
100
0
  }
101
1.29k
  if( libcreg_file_initialize(
102
1.29k
       &file,
103
1.29k
       NULL ) != 1 )
104
0
  {
105
0
    goto on_error_libbfio;
106
0
  }
107
1.29k
  if( libcreg_file_open_file_io_handle(
108
1.29k
       file,
109
1.29k
       file_io_handle,
110
1.29k
       LIBCREG_OPEN_READ,
111
1.29k
       NULL ) != 1 )
112
228
  {
113
228
    goto on_error_libcreg_file;
114
228
  }
115
1.06k
  libcreg_file_get_type(
116
1.06k
   file,
117
1.06k
   &file_type,
118
1.06k
   NULL );
119
120
1.06k
  libcreg_file_get_ascii_codepage(
121
1.06k
   file,
122
1.06k
   &ascii_codepage,
123
1.06k
   NULL );
124
125
1.06k
  libcreg_file_get_format_version(
126
1.06k
   file,
127
1.06k
   &major_version,
128
1.06k
   &minor_version,
129
1.06k
   NULL );
130
131
1.06k
  libcreg_file_is_corrupted(
132
1.06k
   file,
133
1.06k
   NULL );
134
135
  /* Cycles through the supported ASCII codepages to exercise the
136
   * codepage handling in the library
137
   */
138
1.06k
  for( codepage_index = 0;
139
17.0k
       codepage_index < number_of_ascii_codepages;
140
15.9k
       codepage_index++ )
141
15.9k
  {
142
15.9k
    libcreg_file_set_ascii_codepage(
143
15.9k
     file,
144
15.9k
     ascii_codepages[ codepage_index ],
145
15.9k
     NULL );
146
147
15.9k
    libcreg_file_get_ascii_codepage(
148
15.9k
     file,
149
15.9k
     &ascii_codepage,
150
15.9k
     NULL );
151
15.9k
  }
152
  /* Sets an unsupported codepage to exercise the error path
153
   */
154
1.06k
  libcreg_file_set_ascii_codepage(
155
1.06k
   file,
156
1.06k
   65001,
157
1.06k
   NULL );
158
#if defined( LIBCREG_HAVE_BFIO )
159
  libcreg_check_file_signature_file_io_handle(
160
   file_io_handle,
161
   NULL );
162
#endif /* defined( LIBCREG_HAVE_BFIO ) */
163
  /* Looks up keys using paths derived from the fuzz data.
164
   * The data is not necessarily valid UTF-8 or UTF-16
165
   * which exercises the validation and conversion error
166
   * paths in the library
167
   */
168
1.06k
  if( size > 0 )
169
1.06k
  {
170
1.06k
    query_size = size;
171
172
1.06k
    if( query_size > FUZZER_PATH_SIZE_LIMIT )
173
304
    {
174
304
      query_size = FUZZER_PATH_SIZE_LIMIT;
175
304
    }
176
1.06k
    key = NULL;
177
178
1.06k
    libcreg_file_get_key_by_utf8_path(
179
1.06k
     file,
180
1.06k
     data,
181
1.06k
     query_size,
182
1.06k
     &key,
183
1.06k
     NULL );
184
185
1.06k
    if( key != NULL )
186
0
    {
187
0
      libcreg_key_free(
188
0
       &key,
189
0
       NULL );
190
0
    }
191
1.06k
    if( query_size >= 2 )
192
1.06k
    {
193
1.06k
      key = NULL;
194
195
1.06k
      libcreg_file_get_key_by_utf16_path(
196
1.06k
       file,
197
1.06k
       ( const uint16_t * ) data,
198
1.06k
       ( query_size / 2 ),
199
1.06k
       &key,
200
1.06k
       NULL );
201
202
1.06k
      if( key != NULL )
203
0
      {
204
0
        libcreg_key_free(
205
0
         &key,
206
0
         NULL );
207
0
      }
208
1.06k
    }
209
1.06k
  }
210
1.06k
  libcreg_file_close(
211
1.06k
   file,
212
1.06k
   NULL );
213
214
1.29k
on_error_libcreg_file:
215
1.29k
  libcreg_file_free(
216
1.29k
   &file,
217
1.29k
   NULL );
218
219
1.29k
on_error_libbfio:
220
1.29k
  libbfio_handle_free(
221
1.29k
   &file_io_handle,
222
1.29k
   NULL );
223
224
1.29k
  return( 0 );
225
1.29k
}
226
227
} /* extern "C" */