Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libevtx/ossfuzz/template_definition_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libevtx template definition type
3
 *
4
 * Copyright (C) 2011-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_libbfio.h"
32
#include "ossfuzz_libevtx.h"
33
34
#if !defined( LIBEVTX_HAVE_BFIO )
35
36
/* Opens a file using a Basic File IO (bfio) handle
37
 * Returns 1 if successful or -1 on error
38
 */
39
LIBEVTX_EXTERN \
40
int libevtx_file_open_file_io_handle(
41
      libevtx_file_t *file,
42
      libbfio_handle_t *file_io_handle,
43
      int access_flags,
44
      libevtx_error_t **error );
45
46
#endif /* !defined( LIBEVTX_HAVE_BFIO ) */
47
48
/* Limits on the traversal and buffer sizes to bound the time a single input takes
49
 */
50
0
#define FUZZER_STRING_LIMIT        8
51
0
#define FUZZER_STRING_SIZE_LIMIT   ( (size_t) 0x00100000 )
52
53
/* The ASCII codepages supported by libevtx_file_set_ascii_codepage
54
 */
55
static const int file_codepages[] = {
56
  LIBEVTX_CODEPAGE_ASCII,
57
  LIBEVTX_CODEPAGE_WINDOWS_874,
58
  LIBEVTX_CODEPAGE_WINDOWS_932,
59
  LIBEVTX_CODEPAGE_WINDOWS_936,
60
  LIBEVTX_CODEPAGE_WINDOWS_949,
61
  LIBEVTX_CODEPAGE_WINDOWS_950,
62
  LIBEVTX_CODEPAGE_WINDOWS_1250,
63
  LIBEVTX_CODEPAGE_WINDOWS_1251,
64
  LIBEVTX_CODEPAGE_WINDOWS_1252,
65
  LIBEVTX_CODEPAGE_WINDOWS_1253,
66
  LIBEVTX_CODEPAGE_WINDOWS_1254,
67
  LIBEVTX_CODEPAGE_WINDOWS_1255,
68
  LIBEVTX_CODEPAGE_WINDOWS_1256,
69
  LIBEVTX_CODEPAGE_WINDOWS_1257,
70
  LIBEVTX_CODEPAGE_WINDOWS_1258
71
};
72
73
static const size_t file_number_of_codepages =
74
    sizeof( file_codepages ) / sizeof( file_codepages[ 0 ] );
75
76
/* Creates a template definition from the fuzz data and if the file
77
 * contains records uses it to parse and decode the first record
78
 * Returns 1 if successful or -1 on error
79
 */
80
int template_definition_decode_fuzzer(
81
      const uint8_t *data,
82
      size_t size,
83
      libevtx_file_t *file )
84
1.67k
{
85
1.67k
  libevtx_record_t *record                            = NULL;
86
1.67k
  libevtx_template_definition_t *template_definition  = NULL;
87
1.67k
  uint8_t *utf8_string                               = NULL;
88
1.67k
  uint16_t *utf16_string                             = NULL;
89
1.67k
  size_t utf8_string_size                            = 0;
90
1.67k
  size_t utf16_string_size                           = 0;
91
1.67k
  uint8_t value_byte                                 = 0;
92
1.67k
  int number_of_records                              = 0;
93
1.67k
  int number_of_strings                              = 0;
94
1.67k
  int string_index                                   = 0;
95
1.67k
  int result                                         = 0;
96
97
1.67k
  if( libevtx_template_definition_initialize(
98
1.67k
       &template_definition,
99
1.67k
       NULL ) != 1 )
100
0
  {
101
0
    return( -1 );
102
0
  }
103
1.67k
  if( libevtx_template_definition_set_data(
104
1.67k
       template_definition,
105
1.67k
       data,
106
1.67k
       size,
107
1.67k
       0,
108
1.67k
       NULL ) != 1 )
109
1.59k
  {
110
1.59k
    goto on_error;
111
1.59k
  }
112
82
  if( file != NULL )
113
82
  {
114
82
    result = libevtx_file_get_number_of_records(
115
82
              file,
116
82
              &number_of_records,
117
82
              NULL );
118
119
82
    if( ( result == 1 )
120
0
     && ( number_of_records > 0 ) )
121
0
    {
122
0
      record = NULL;
123
124
0
      result = libevtx_file_get_record_by_index(
125
0
                file,
126
0
                0,
127
0
                &record,
128
0
                NULL );
129
130
0
      if( ( result == 1 )
131
0
       && ( record != NULL ) )
132
0
      {
133
        /* Note that a record can only be parsed once and that parsing
134
         * the data with a template definition may report the data
135
         * could not be parsed, the record values are accessed anyway
136
         * to exercise the getter functions with that state
137
         */
138
0
        libevtx_record_parse_data_with_template_definition(
139
0
         record,
140
0
         template_definition,
141
0
         NULL );
142
143
0
        result = libevtx_record_get_number_of_strings(
144
0
                  record,
145
0
                  &number_of_strings,
146
0
                  NULL );
147
148
0
        if( ( result == 1 )
149
0
         && ( number_of_strings > 0 ) )
150
0
        {
151
0
          for( string_index = 0;
152
0
               string_index < number_of_strings;
153
0
               string_index++ )
154
0
          {
155
0
            if( string_index >= FUZZER_STRING_LIMIT )
156
0
            {
157
0
              break;
158
0
            }
159
0
            result = libevtx_record_get_utf8_string_size(
160
0
                      record,
161
0
                      string_index,
162
0
                      &utf8_string_size,
163
0
                      NULL );
164
165
0
            if( ( result == 1 )
166
0
             && ( utf8_string_size > 0 )
167
0
             && ( utf8_string_size <= FUZZER_STRING_SIZE_LIMIT ) )
168
0
            {
169
0
              utf8_string = (uint8_t *) malloc( utf8_string_size );
170
171
0
              if( utf8_string != NULL )
172
0
              {
173
0
                if( libevtx_record_get_utf8_string(
174
0
                     record,
175
0
                     string_index,
176
0
                     utf8_string,
177
0
                     utf8_string_size,
178
0
                     NULL ) == 1 )
179
0
                {
180
0
                  value_byte ^= utf8_string[ 0 ];
181
0
                }
182
0
                free( utf8_string );
183
0
              }
184
0
            }
185
            /* The size is the number of UTF-16 characters,
186
             * including the end of string character
187
             */
188
0
            result = libevtx_record_get_utf16_string_size(
189
0
                      record,
190
0
                      string_index,
191
0
                      &utf16_string_size,
192
0
                      NULL );
193
194
0
            if( ( result == 1 )
195
0
             && ( utf16_string_size > 0 )
196
0
             && ( utf16_string_size <= FUZZER_STRING_SIZE_LIMIT ) )
197
0
            {
198
0
              utf16_string = (uint16_t *) malloc( utf16_string_size * sizeof( uint16_t ) );
199
200
0
              if( utf16_string != NULL )
201
0
              {
202
0
                if( libevtx_record_get_utf16_string(
203
0
                     record,
204
0
                     string_index,
205
0
                     utf16_string,
206
0
                     utf16_string_size,
207
0
                     NULL ) == 1 )
208
0
                {
209
0
                  value_byte ^= (uint8_t) utf16_string[ 0 ];
210
0
                }
211
0
                free( utf16_string );
212
0
              }
213
0
            }
214
0
          }
215
          /* Use the decoded values as a sink to keep them live
216
           */
217
0
          if( value_byte != 0 )
218
0
          {
219
            /* No action required
220
             */
221
0
          }
222
0
        }
223
0
      }
224
0
    }
225
82
  }
226
227
1.67k
on_error:
228
1.67k
  libevtx_record_free(
229
1.67k
   &record,
230
1.67k
   NULL );
231
232
1.67k
  libevtx_template_definition_free(
233
1.67k
   &template_definition,
234
1.67k
   NULL );
235
236
1.67k
  return( 0 );
237
82
}
238
239
int LLVMFuzzerTestOneInput(
240
      const uint8_t *data,
241
      size_t size )
242
1.67k
{
243
1.67k
  libbfio_handle_t *file_io_handle = NULL;
244
1.67k
  libevtx_file_t *file             = NULL;
245
1.67k
  int ascii_codepage               = 0;
246
1.67k
  int file_opened                  = 0;
247
248
1.67k
  if( libbfio_memory_range_initialize(
249
1.67k
       &file_io_handle,
250
1.67k
       NULL ) != 1 )
251
0
  {
252
0
    return( 0 );
253
0
  }
254
1.67k
  if( libbfio_memory_range_set(
255
1.67k
       file_io_handle,
256
1.67k
       (uint8_t *) data,
257
1.67k
       size,
258
1.67k
       NULL ) != 1 )
259
0
  {
260
0
    goto on_error_libbfio;
261
0
  }
262
1.67k
  if( libevtx_file_initialize(
263
1.67k
       &file,
264
1.67k
       NULL ) != 1 )
265
0
  {
266
0
    goto on_error_libbfio;
267
0
  }
268
1.67k
  if( libevtx_file_open_file_io_handle(
269
1.67k
       file,
270
1.67k
       file_io_handle,
271
1.67k
       LIBEVTX_OPEN_READ,
272
1.67k
       NULL ) == 1 )
273
877
  {
274
877
    file_opened = 1;
275
877
  }
276
  /* The codepage is selected from the fuzz data to steer the
277
   * codepage conversion of the record values
278
   */
279
1.67k
  if( ( file_opened != 0 )
280
877
   && ( size > 0 ) )
281
877
  {
282
877
    libevtx_file_set_ascii_codepage(
283
877
     file,
284
877
     file_codepages[ data[ 0 ] % file_number_of_codepages ],
285
877
     NULL );
286
287
877
    libevtx_file_get_ascii_codepage(
288
877
     file,
289
877
     &ascii_codepage,
290
877
     NULL );
291
877
  }
292
1.67k
  template_definition_decode_fuzzer(
293
1.67k
   data,
294
1.67k
   size,
295
1.67k
   file );
296
297
1.67k
  if( file_opened != 0 )
298
877
  {
299
877
    libevtx_file_close(
300
877
     file,
301
877
     NULL );
302
877
  }
303
304
1.67k
on_error_libevtx_file:
305
1.67k
  libevtx_file_free(
306
1.67k
   &file,
307
1.67k
   NULL );
308
309
1.67k
on_error_libbfio:
310
1.67k
  libbfio_handle_free(
311
1.67k
   &file_io_handle,
312
1.67k
   NULL );
313
314
1.67k
  return( 0 );
315
1.67k
}
316
317
} /* extern "C" */