Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libevt/ossfuzz/record_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libevt record 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_libevt.h"
33
34
134k
#define MAXIMUM_STRING_SIZE 8 * 1024 * 1024
35
36
#if !defined( LIBEVT_HAVE_BFIO )
37
38
/* Opens a file using a Basic File IO (bfio) handle
39
 * Returns 1 if successful or -1 on error
40
 */
41
LIBEVT_EXTERN \
42
int libevt_file_open_file_io_handle(
43
     libevt_file_t *file,
44
     libbfio_handle_t *file_io_handle,
45
     int access_flags,
46
     libevt_error_t **error );
47
48
#endif /* !defined( LIBEVT_HAVE_BFIO ) */
49
50
static void fuzzer_fetch_record_properties(
51
     libevt_record_t *record )
52
22.8k
{
53
22.8k
  uint16_t *utf16_string    = NULL;
54
22.8k
  uint8_t *data             = NULL;
55
22.8k
  uint8_t *utf8_string      = NULL;
56
22.8k
  off64_t offset            = 0;
57
22.8k
  uint16_t event_category   = 0;
58
22.8k
  uint16_t event_type       = 0;
59
22.8k
  uint32_t event_identifier = 0;
60
22.8k
  uint32_t identifier       = 0;
61
22.8k
  uint32_t posix_time       = 0;
62
22.8k
  size_t data_size          = 0;
63
22.8k
  size_t utf16_string_size  = 0;
64
22.8k
  size_t utf8_string_size   = 0;
65
22.8k
  int number_of_strings     = 0;
66
22.8k
  int result                = 0;
67
22.8k
  int string_index          = 0;
68
69
22.8k
  if( record == NULL )
70
0
  {
71
0
    return;
72
0
  }
73
22.8k
  libevt_record_get_offset(
74
22.8k
   record,
75
22.8k
   &offset,
76
22.8k
   NULL );
77
78
22.8k
  libevt_record_get_identifier(
79
22.8k
   record,
80
22.8k
   &identifier,
81
22.8k
   NULL );
82
83
22.8k
  libevt_record_get_creation_time(
84
22.8k
   record,
85
22.8k
   &posix_time,
86
22.8k
   NULL );
87
88
22.8k
  libevt_record_get_written_time(
89
22.8k
   record,
90
22.8k
   &posix_time,
91
22.8k
   NULL );
92
93
22.8k
  libevt_record_get_event_identifier(
94
22.8k
   record,
95
22.8k
   &event_identifier,
96
22.8k
   NULL );
97
98
22.8k
  libevt_record_get_event_type(
99
22.8k
   record,
100
22.8k
   &event_type,
101
22.8k
   NULL );
102
103
22.8k
  libevt_record_get_event_category(
104
22.8k
   record,
105
22.8k
   &event_category,
106
22.8k
   NULL );
107
108
22.8k
  result = libevt_record_get_utf8_source_name_size(
109
22.8k
            record,
110
22.8k
            &utf8_string_size,
111
22.8k
            NULL );
112
113
22.8k
  if( ( result == 1 )
114
22.8k
   && ( utf8_string_size > 0 )
115
22.6k
   && ( utf8_string_size < MAXIMUM_STRING_SIZE ) )
116
22.6k
  {
117
22.6k
    utf8_string = (uint8_t *) malloc( utf8_string_size );
118
119
22.6k
    if( utf8_string != NULL )
120
22.6k
    {
121
22.6k
      libevt_record_get_utf8_source_name(
122
22.6k
       record,
123
22.6k
       utf8_string,
124
22.6k
       utf8_string_size,
125
22.6k
       NULL );
126
22.6k
      free( utf8_string );
127
128
22.6k
      utf8_string = NULL;
129
22.6k
    }
130
22.6k
  }
131
22.8k
  result = libevt_record_get_utf16_source_name_size(
132
22.8k
            record,
133
22.8k
            &utf16_string_size,
134
22.8k
            NULL );
135
136
22.8k
  if( ( result == 1 )
137
22.8k
   && ( utf16_string_size > 0 )
138
22.6k
   && ( utf16_string_size < MAXIMUM_STRING_SIZE ) )
139
22.6k
  {
140
22.6k
    utf16_string = (uint16_t *) malloc( utf16_string_size * sizeof( uint16_t ) );
141
142
22.6k
    if( utf16_string != NULL )
143
22.6k
    {
144
22.6k
      libevt_record_get_utf16_source_name(
145
22.6k
       record,
146
22.6k
       utf16_string,
147
22.6k
       utf16_string_size,
148
22.6k
       NULL );
149
22.6k
      free( utf16_string );
150
151
22.6k
      utf16_string = NULL;
152
22.6k
    }
153
22.6k
  }
154
22.8k
  result = libevt_record_get_utf8_computer_name_size(
155
22.8k
            record,
156
22.8k
            &utf8_string_size,
157
22.8k
            NULL );
158
159
22.8k
  if( ( result == 1 )
160
22.7k
   && ( utf8_string_size > 0 )
161
9.87k
   && ( utf8_string_size < MAXIMUM_STRING_SIZE ) )
162
9.87k
  {
163
9.87k
    utf8_string = (uint8_t *) malloc( utf8_string_size );
164
165
9.87k
    if( utf8_string != NULL )
166
9.87k
    {
167
9.87k
      libevt_record_get_utf8_computer_name(
168
9.87k
       record,
169
9.87k
       utf8_string,
170
9.87k
       utf8_string_size,
171
9.87k
       NULL );
172
9.87k
      free( utf8_string );
173
174
9.87k
      utf8_string = NULL;
175
9.87k
    }
176
9.87k
  }
177
22.8k
  result = libevt_record_get_utf16_computer_name_size(
178
22.8k
            record,
179
22.8k
            &utf16_string_size,
180
22.8k
            NULL );
181
182
22.8k
  if( ( result == 1 )
183
22.7k
   && ( utf16_string_size > 0 )
184
9.87k
   && ( utf16_string_size < MAXIMUM_STRING_SIZE ) )
185
9.87k
  {
186
9.87k
    utf16_string = (uint16_t *) malloc( utf16_string_size * sizeof( uint16_t ) );
187
188
9.87k
    if( utf16_string != NULL )
189
9.87k
    {
190
9.87k
      libevt_record_get_utf16_computer_name(
191
9.87k
       record,
192
9.87k
       utf16_string,
193
9.87k
       utf16_string_size,
194
9.87k
       NULL );
195
9.87k
      free( utf16_string );
196
197
9.87k
      utf16_string = NULL;
198
9.87k
    }
199
9.87k
  }
200
22.8k
  result = libevt_record_get_utf8_user_security_identifier_size(
201
22.8k
            record,
202
22.8k
            &utf8_string_size,
203
22.8k
            NULL );
204
205
22.8k
  if( ( result == 1 )
206
1.96k
   && ( utf8_string_size > 0 )
207
1.96k
   && ( utf8_string_size < MAXIMUM_STRING_SIZE ) )
208
1.96k
  {
209
1.96k
    utf8_string = (uint8_t *) malloc( utf8_string_size );
210
211
1.96k
    if( utf8_string != NULL )
212
1.96k
    {
213
1.96k
      libevt_record_get_utf8_user_security_identifier(
214
1.96k
       record,
215
1.96k
       utf8_string,
216
1.96k
       utf8_string_size,
217
1.96k
       NULL );
218
1.96k
      free( utf8_string );
219
220
1.96k
      utf8_string = NULL;
221
1.96k
    }
222
1.96k
  }
223
22.8k
  result = libevt_record_get_utf16_user_security_identifier_size(
224
22.8k
            record,
225
22.8k
            &utf16_string_size,
226
22.8k
            NULL );
227
228
22.8k
  if( ( result == 1 )
229
1.96k
   && ( utf16_string_size > 0 )
230
1.96k
   && ( utf16_string_size < MAXIMUM_STRING_SIZE ) )
231
1.96k
  {
232
1.96k
    utf16_string = (uint16_t *) malloc( utf16_string_size * sizeof( uint16_t ) );
233
234
1.96k
    if( utf16_string != NULL )
235
1.96k
    {
236
1.96k
      libevt_record_get_utf16_user_security_identifier(
237
1.96k
       record,
238
1.96k
       utf16_string,
239
1.96k
       utf16_string_size,
240
1.96k
       NULL );
241
1.96k
      free( utf16_string );
242
243
1.96k
      utf16_string = NULL;
244
1.96k
    }
245
1.96k
  }
246
22.8k
  result = libevt_record_get_number_of_strings(
247
22.8k
            record,
248
22.8k
            &number_of_strings,
249
22.8k
            NULL );
250
251
22.8k
  if( ( result == 1 )
252
22.8k
   && ( number_of_strings > 0 ) )
253
9.52k
  {
254
9.52k
    for( string_index = 0;
255
41.6k
         string_index < number_of_strings;
256
32.1k
         string_index++ )
257
32.1k
    {
258
32.1k
      result = libevt_record_get_utf8_string_size(
259
32.1k
                record,
260
32.1k
                string_index,
261
32.1k
                &utf8_string_size,
262
32.1k
                NULL );
263
264
32.1k
      if( ( result == 1 )
265
31.6k
       && ( utf8_string_size > 0 )
266
31.6k
       && ( utf8_string_size < MAXIMUM_STRING_SIZE ) )
267
31.6k
      {
268
31.6k
        utf8_string = (uint8_t *) malloc( utf8_string_size );
269
270
31.6k
        if( utf8_string != NULL )
271
31.6k
        {
272
31.6k
          libevt_record_get_utf8_string(
273
31.6k
           record,
274
31.6k
           string_index,
275
31.6k
           utf8_string,
276
31.6k
           utf8_string_size,
277
31.6k
           NULL );
278
31.6k
          free( utf8_string );
279
280
31.6k
          utf8_string = NULL;
281
31.6k
        }
282
31.6k
      }
283
32.1k
      result = libevt_record_get_utf16_string_size(
284
32.1k
                record,
285
32.1k
                string_index,
286
32.1k
                &utf16_string_size,
287
32.1k
                NULL );
288
289
32.1k
      if( ( result == 1 )
290
31.6k
       && ( utf16_string_size > 0 )
291
31.6k
       && ( utf16_string_size < MAXIMUM_STRING_SIZE ) )
292
31.6k
      {
293
31.6k
        utf16_string = (uint16_t *) malloc( utf16_string_size * sizeof( uint16_t ) );
294
295
31.6k
        if( utf16_string != NULL )
296
31.6k
        {
297
31.6k
          libevt_record_get_utf16_string(
298
31.6k
           record,
299
31.6k
           string_index,
300
31.6k
           utf16_string,
301
31.6k
           utf16_string_size,
302
31.6k
           NULL );
303
31.6k
          free( utf16_string );
304
305
31.6k
          utf16_string = NULL;
306
31.6k
        }
307
31.6k
      }
308
32.1k
    }
309
9.52k
  }
310
22.8k
  result = libevt_record_get_data_size(
311
22.8k
            record,
312
22.8k
            &data_size,
313
22.8k
            NULL );
314
315
22.8k
  if( ( result == 1 )
316
1.97k
   && ( data_size > 0 )
317
1.97k
   && ( data_size < MAXIMUM_STRING_SIZE ) )
318
1.97k
  {
319
1.97k
    data = (uint8_t *) malloc( data_size );
320
321
1.97k
    if( data != NULL )
322
1.97k
    {
323
1.97k
      libevt_record_get_data(
324
1.97k
       record,
325
1.97k
       data,
326
1.97k
       data_size,
327
1.97k
       NULL );
328
1.97k
      free( data );
329
330
1.97k
      data = NULL;
331
1.97k
    }
332
1.97k
  }
333
22.8k
}
334
335
int LLVMFuzzerTestOneInput(
336
     const uint8_t *data,
337
     size_t size )
338
2.24k
{
339
2.24k
  libbfio_handle_t *file_io_handle = NULL;
340
2.24k
  libevt_file_t *file              = NULL;
341
2.24k
  libevt_record_t *record          = NULL;
342
2.24k
  int number_of_records            = 0;
343
2.24k
  int record_index                 = 0;
344
2.24k
  int result                       = 0;
345
346
2.24k
  if( libbfio_memory_range_initialize(
347
2.24k
       &file_io_handle,
348
2.24k
       NULL ) != 1 )
349
0
  {
350
0
    return( 0 );
351
0
  }
352
2.24k
  if( libbfio_memory_range_set(
353
2.24k
       file_io_handle,
354
2.24k
       (uint8_t *) data,
355
2.24k
       size,
356
2.24k
       NULL ) != 1 )
357
0
  {
358
0
    goto on_error_libbfio;
359
0
  }
360
2.24k
  if( libevt_file_initialize(
361
2.24k
       &file,
362
2.24k
       NULL ) != 1 )
363
0
  {
364
0
    goto on_error_libbfio;
365
0
  }
366
2.24k
  if( libevt_file_open_file_io_handle(
367
2.24k
       file,
368
2.24k
       file_io_handle,
369
2.24k
       LIBEVT_OPEN_READ,
370
2.24k
       NULL ) != 1 )
371
19
  {
372
19
    goto on_error_libevt_file;
373
19
  }
374
2.23k
  result = libevt_file_get_number_of_records(
375
2.23k
            file,
376
2.23k
            &number_of_records,
377
2.23k
            NULL );
378
379
2.23k
  if( ( result == 1 )
380
2.23k
   && ( number_of_records > 0 ) )
381
575
  {
382
575
    for( record_index = 0;
383
23.4k
         record_index < number_of_records;
384
22.8k
         record_index++ )
385
22.8k
    {
386
22.8k
      if( libevt_file_get_record_by_index(
387
22.8k
           file,
388
22.8k
           record_index,
389
22.8k
           &record,
390
22.8k
           NULL ) != 1 )
391
0
      {
392
0
        continue;
393
0
      }
394
22.8k
      fuzzer_fetch_record_properties(
395
22.8k
       record );
396
397
22.8k
      libevt_record_free(
398
22.8k
       &record,
399
22.8k
       NULL );
400
22.8k
    }
401
575
    if( libevt_file_get_record_by_index(
402
575
         file,
403
575
         number_of_records,
404
575
         &record,
405
575
         NULL ) == 1 )
406
0
    {
407
0
      libevt_record_free(
408
0
       &record,
409
0
       NULL );
410
0
    }
411
575
  }
412
2.23k
  libevt_file_close(
413
2.23k
   file,
414
2.23k
   NULL );
415
416
2.24k
on_error_libevt_file:
417
2.24k
  libevt_file_free(
418
2.24k
   &file,
419
2.24k
   NULL );
420
421
2.24k
on_error_libbfio:
422
2.24k
  libbfio_handle_free(
423
2.24k
   &file_io_handle,
424
2.24k
   NULL );
425
426
2.24k
  return( 0 );
427
2.24k
}
428
429
} /* extern "C" */