Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libqcow/ossfuzz/file_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libqcow file type
3
 *
4
 * Copyright (C) 2010-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 <byte_stream.h>
23
#include <stddef.h>
24
#include <stdint.h>
25
#include <stdio.h>
26
#include <unistd.h>
27
28
/* Note that some of the OSS-Fuzz engines use C++
29
 */
30
extern "C" {
31
32
#include "ossfuzz_libbfio.h"
33
#include "ossfuzz_libqcow.h"
34
35
#if !defined( LIBQCOW_HAVE_BFIO )
36
37
/* Opens a file using a Basic File IO (bfio) handle
38
 * Returns 1 if successful or -1 on error
39
 */
40
LIBQCOW_EXTERN \
41
int libqcow_file_open_file_io_handle(
42
     libqcow_file_t *file,
43
     libbfio_handle_t *file_io_handle,
44
     int access_flags,
45
     libqcow_error_t **error );
46
47
/* Determines if a file contains a QCOW file signature using a Basic File IO (bfio) handle
48
 * Returns 1 if true, 0 if not or -1 on error
49
 */
50
LIBQCOW_EXTERN \
51
int libqcow_check_file_signature_file_io_handle(
52
     libbfio_handle_t *bfio_handle,
53
     libqcow_error_t **error );
54
55
#endif /* !defined( LIBQCOW_HAVE_BFIO ) */
56
57
/* POSIX function that is not exposed in C++ mode
58
 */
59
int mkstemp( char *name );
60
61
static uint64_t libqcow_fuzzer_state = 0;
62
63
/* Advances the fuzzer state and returns the next 32-bit value
64
 */
65
static uint32_t libqcow_fuzzer_next_u32( void )
66
23.4k
{
67
23.4k
  uint64_t value = libqcow_fuzzer_state + 0x9e3779b97f4a7c15ULL;
68
69
23.4k
  value  = ( value ^ byte_stream_bit_rotate_right_64bit( value, 30 ) ) * 0xbf58476d1ce4e5b9ULL;
70
23.4k
  value  = ( value ^ byte_stream_bit_rotate_right_64bit( value, 27 ) ) * 0x94d049bb133111ebULL;
71
23.4k
  value  = value ^ byte_stream_bit_rotate_right_64bit( value, 31 );
72
23.4k
  libqcow_fuzzer_state = value;
73
74
23.4k
  return( (uint32_t) value );
75
23.4k
}
76
77
2.71k
#define LIBQCOW_FUZZER_MAXIMUM_READ_ITERATIONS    128
78
79
int LLVMFuzzerTestOneInput(
80
     const uint8_t *data,
81
     size_t size )
82
2.09k
{
83
2.09k
  uint8_t buffer[ 4096 ];
84
2.09k
  uint8_t utf8_string[ 64 ];
85
2.09k
  uint16_t utf16_password[ 16 ];
86
2.09k
  uint16_t utf16_string[ 64 ];
87
88
2.09k
  libbfio_handle_t *parent_file_io_handle = NULL;
89
2.09k
  libbfio_handle_t *file_io_handle        = NULL;
90
2.09k
  libqcow_file_t *parent_file             = NULL;
91
2.09k
  libqcow_file_t *path_file               = NULL;
92
2.09k
  libqcow_file_t *file                    = NULL;
93
2.09k
  off64_t media_offset                    = 0;
94
2.09k
  off64_t read_offset                     = 0;
95
2.09k
  size64_t media_size                     = 0;
96
2.09k
  size_t password_data_offset             = 0;
97
2.09k
  size_t password_size                    = 0;
98
2.09k
  size_t parent_data_length               = 0;
99
2.09k
  size_t parent_data_offset               = 0;
100
2.09k
  size_t string_size                      = 0;
101
2.09k
  int number_of_snapshots                 = 0;
102
2.09k
  int read_iterator                       = 0;
103
2.09k
  int parent_ready                        = 0;
104
2.09k
  int reverse_read_pattern                = 0;
105
2.09k
  int abort_read                          = 0;
106
2.09k
  int codepage                            = 0;
107
2.09k
  uint32_t iteration_limit                = 0;
108
2.09k
  uint32_t read_whence                    = 0;
109
2.09k
  uint32_t value_32bit                    = 0;
110
2.09k
  FILE *notify_stream                     = NULL;
111
112
2.09k
  if( size == 0 )
113
0
  {
114
0
    return( 0 );
115
0
  }
116
  /* Derives a deterministic set of fuzzing parameters from the entire input
117
   * This keeps the full input available as QCOW image data while making
118
   * the exercised code paths a function of the input
119
   */
120
2.09k
  libqcow_fuzzer_state = 0xcbf29ce484222325ULL;
121
122
34.2M
  for( size_t data_iterator = 0; data_iterator < size; data_iterator++ )
123
34.2M
  {
124
34.2M
    libqcow_fuzzer_state ^= ( uint64_t ) data[ data_iterator ];
125
34.2M
    libqcow_fuzzer_state *= 0x100000001b3ULL;
126
34.2M
  }
127
2.09k
  iteration_limit      = 1 + ( libqcow_fuzzer_next_u32() % ( LIBQCOW_FUZZER_MAXIMUM_READ_ITERATIONS - 1 ) );
128
2.09k
  read_whence          = libqcow_fuzzer_next_u32() % 3;
129
2.09k
  abort_read           = libqcow_fuzzer_next_u32() & 1;
130
2.09k
  reverse_read_pattern = libqcow_fuzzer_next_u32() & 1;
131
2.09k
  notify_stream        = fopen(
132
2.09k
                          "/dev/null",
133
2.09k
                          "a" );
134
2.09k
  if( notify_stream != NULL
135
2.09k
   && libqcow_notify_set_stream(
136
2.09k
        notify_stream,
137
2.09k
        NULL ) != 1 )
138
0
  {
139
0
    libqcow_notify_set_stream(
140
0
     NULL,
141
0
     NULL );
142
0
  }
143
  /* Exercises the library accessors and codepage functions
144
   */
145
2.09k
  value_32bit = ( uint32_t ) libqcow_get_access_flags_read();
146
2.09k
  ( void ) value_32bit;
147
148
2.09k
  value_32bit = ( uint32_t ) libqcow_get_access_flags_read_write();
149
2.09k
  ( void ) value_32bit;
150
151
2.09k
  value_32bit = ( uint32_t ) libqcow_get_access_flags_write();
152
2.09k
  ( void ) value_32bit;
153
154
2.09k
  if( libqcow_get_codepage(
155
2.09k
       &codepage,
156
2.09k
       NULL ) != 1 )
157
0
  {
158
0
    goto on_error;
159
0
  }
160
2.09k
  if( libqcow_set_codepage(
161
2.09k
       codepage,
162
2.09k
       NULL ) != 1 )
163
0
  {
164
0
    goto on_error;
165
0
  }
166
2.09k
  if( libbfio_memory_range_initialize(
167
2.09k
       &file_io_handle,
168
2.09k
       NULL ) != 1 )
169
0
  {
170
0
    goto on_error;
171
0
  }
172
2.09k
  if( libbfio_memory_range_set(
173
2.09k
       file_io_handle,
174
2.09k
       (uint8_t *) data,
175
2.09k
       size,
176
2.09k
       NULL ) != 1 )
177
0
  {
178
0
    goto on_error;
179
0
  }
180
2.09k
  if( libqcow_check_file_signature_file_io_handle(
181
2.09k
       file_io_handle,
182
2.09k
       NULL ) == -1 )
183
3
  {
184
3
    goto on_error;
185
3
  }
186
2.09k
  if( libqcow_file_initialize(
187
2.09k
       &file,
188
2.09k
       NULL ) != 1 )
189
0
  {
190
0
    goto on_error;
191
0
  }
192
2.09k
  if( libqcow_file_open_file_io_handle(
193
2.09k
       file,
194
2.09k
       file_io_handle,
195
2.09k
       LIBQCOW_OPEN_READ,
196
2.09k
       NULL ) != 1 )
197
828
  {
198
828
    goto on_error;
199
828
  }
200
1.26k
  if( libqcow_file_get_format_version(
201
1.26k
       file,
202
1.26k
       &value_32bit,
203
1.26k
       NULL ) != 1 )
204
0
  {
205
0
    goto on_error;
206
0
  }
207
1.26k
  if( libqcow_file_get_encryption_method(
208
1.26k
       file,
209
1.26k
       &value_32bit,
210
1.26k
       NULL ) != 1 )
211
0
  {
212
0
    goto on_error;
213
0
  }
214
1.26k
  if( libqcow_file_get_utf8_backing_filename_size(
215
1.26k
       file,
216
1.26k
       &string_size,
217
1.26k
       NULL ) == -1 )
218
60
  {
219
60
    goto on_error;
220
60
  }
221
1.20k
  if( libqcow_file_get_utf8_backing_filename(
222
1.20k
       file,
223
1.20k
       utf8_string,
224
1.20k
       64,
225
1.20k
       NULL ) == -1 )
226
15
  {
227
15
    goto on_error;
228
15
  }
229
1.19k
  if( libqcow_file_get_utf16_backing_filename_size(
230
1.19k
       file,
231
1.19k
       &string_size,
232
1.19k
       NULL ) == -1 )
233
0
  {
234
0
    goto on_error;
235
0
  }
236
1.19k
  if( libqcow_file_get_utf16_backing_filename(
237
1.19k
       file,
238
1.19k
       utf16_string,
239
1.19k
       64,
240
1.19k
       NULL ) == -1 )
241
0
  {
242
0
    goto on_error;
243
0
  }
244
1.19k
  if( libqcow_file_get_number_of_snapshots(
245
1.19k
       file,
246
1.19k
       &number_of_snapshots,
247
1.19k
       NULL ) != 1 )
248
0
  {
249
0
    goto on_error;
250
0
  }
251
1.19k
  if( libqcow_file_get_media_size(
252
1.19k
       file,
253
1.19k
       &media_size,
254
1.19k
       NULL ) != 1 )
255
0
  {
256
0
    goto on_error;
257
0
  }
258
  /* Retrieves whether the file is locked
259
   * A result of 1 if locked or 0 if not locked is both successful
260
   */
261
1.19k
  value_32bit = ( uint32_t ) libqcow_file_is_locked(
262
1.19k
                       file,
263
1.19k
                       NULL );
264
1.19k
  ( void ) value_32bit;
265
266
1.19k
  if( ( libqcow_fuzzer_next_u32() & 1 ) != 0 )
267
605
  {
268
605
    password_size       = 1 + ( libqcow_fuzzer_next_u32() % 16 );
269
605
    password_data_offset = ( size_t ) ( libqcow_fuzzer_next_u32() % size );
270
271
605
    if( password_data_offset + password_size > size )
272
8
    {
273
8
      password_size = size - password_data_offset;
274
8
    }
275
605
    libqcow_file_set_utf8_password(
276
605
     file,
277
605
     ( data + password_data_offset ),
278
605
     password_size,
279
605
     NULL );
280
605
  }
281
1.19k
  if( ( libqcow_fuzzer_next_u32() & 1 ) != 0 )
282
564
  {
283
564
    password_size = 1 + ( libqcow_fuzzer_next_u32() % 16 );
284
285
5.43k
    for( read_iterator = 0; ( size_t ) read_iterator < password_size; read_iterator++ )
286
4.87k
    {
287
4.87k
      utf16_password[ read_iterator ] = ( uint16_t ) ( libqcow_fuzzer_next_u32() & 0xff );
288
4.87k
    }
289
564
    libqcow_file_set_utf16_password(
290
564
     file,
291
564
     utf16_password,
292
564
     password_size,
293
564
     NULL );
294
564
  }
295
1.19k
  if( ( libqcow_fuzzer_next_u32() & 1 ) != 0 )
296
602
  {
297
602
    password_size       = 1 + ( libqcow_fuzzer_next_u32() % 16 );
298
602
    password_data_offset = ( size_t ) ( libqcow_fuzzer_next_u32() % size );
299
300
602
    if( password_data_offset + password_size > size )
301
10
    {
302
10
      password_size = size - password_data_offset;
303
10
    }
304
602
    libqcow_file_set_keys(
305
602
     file,
306
602
     ( data + password_data_offset ),
307
602
     password_size,
308
602
     NULL );
309
602
  }
310
1.19k
  if( ( libqcow_fuzzer_next_u32() & 1 ) != 0 )
311
617
  {
312
617
    parent_data_offset = ( size_t ) ( libqcow_fuzzer_next_u32() % size );
313
617
    parent_data_length = 1 + ( size_t ) ( libqcow_fuzzer_next_u32() % ( LIBQCOW_FUZZER_MAXIMUM_READ_ITERATIONS * 16 ) );
314
315
617
    if( parent_data_offset + parent_data_length > size )
316
478
    {
317
478
      parent_data_length = size - parent_data_offset;
318
478
    }
319
617
    if( libbfio_memory_range_initialize(
320
617
         &parent_file_io_handle,
321
617
         NULL ) == 1
322
617
     && libbfio_memory_range_set(
323
617
                   parent_file_io_handle,
324
617
                   ( uint8_t * ) ( data + parent_data_offset ),
325
617
                   ( size_t ) parent_data_length,
326
617
                   NULL ) == 1
327
617
     && libqcow_file_initialize(
328
617
                   &parent_file,
329
617
                   NULL ) == 1
330
617
     && libqcow_file_open_file_io_handle(
331
617
                   parent_file,
332
617
                   parent_file_io_handle,
333
617
                   LIBQCOW_OPEN_READ,
334
617
                   NULL ) == 1 )
335
29
    {
336
29
      parent_ready = 1;
337
29
    }
338
617
    if( parent_ready == 0 )
339
588
    {
340
588
      if( parent_file != NULL )
341
588
      {
342
588
        libqcow_file_free(
343
588
         &parent_file,
344
588
         NULL );
345
588
      }
346
588
      if( parent_file_io_handle != NULL )
347
588
      {
348
588
        libbfio_handle_free(
349
588
         &parent_file_io_handle,
350
588
         NULL );
351
588
      }
352
588
    }
353
29
    else
354
29
    {
355
29
      libqcow_file_set_parent_file(
356
29
       file,
357
29
       parent_file,
358
29
       NULL );
359
29
    }
360
617
  }
361
1.19k
  if( media_size > 0 )
362
1.18k
  {
363
1.18k
    read_offset = ( off64_t ) ( libqcow_fuzzer_next_u32() % ( size_t ) media_size );
364
1.18k
  }
365
1.19k
  if( reverse_read_pattern != 0 )
366
474
  {
367
    /* Reverse read pattern
368
     * Uses seek_offset and read_buffer to read from the end towards the start
369
     */
370
474
    media_offset = ( off64_t ) media_size - ( ( off64_t ) media_size % 64 );
371
372
474
    for( read_iterator = 0;
373
4.73k
         read_iterator < ( int ) iteration_limit
374
4.69k
          && media_offset > 0;
375
4.26k
         read_iterator++ )
376
4.59k
    {
377
4.59k
      if( abort_read != 0
378
1.98k
       && read_iterator == 3 )
379
33
      {
380
33
        libqcow_file_signal_abort(
381
33
         file,
382
33
         NULL );
383
33
      }
384
4.59k
      if( libqcow_file_seek_offset(
385
4.59k
           file,
386
4.59k
           ( media_offset - 64 ) < 0 ? 0 : ( media_offset - 64 ),
387
4.59k
           SEEK_SET,
388
4.59k
           NULL ) == -1 )
389
52
      {
390
52
        break;
391
52
      }
392
4.54k
      if( libqcow_file_read_buffer(
393
4.54k
           file,
394
4.54k
           buffer,
395
4.54k
           64,
396
4.54k
           NULL ) == -1 )
397
283
      {
398
283
        break;
399
283
      }
400
4.26k
      media_offset -= 64;
401
4.26k
    }
402
474
  }
403
716
  else
404
716
  {
405
    /* Forward read pattern
406
     * Alternates read_buffer_at_offset and read_buffer with varying read sizes
407
     */
408
716
    media_offset = read_offset;
409
410
716
    for( read_iterator = 0;
411
7.32k
         read_iterator < ( int ) iteration_limit
412
7.26k
          && ( off64_t ) media_size > media_offset;
413
6.60k
         read_iterator++ )
414
7.19k
    {
415
7.19k
      if( abort_read != 0
416
3.79k
       && read_iterator == 3 )
417
104
      {
418
104
        libqcow_file_signal_abort(
419
104
         file,
420
104
         NULL );
421
104
      }
422
7.19k
      if( ( read_iterator % 2 ) == 0 )
423
3.89k
      {
424
3.89k
        if( libqcow_file_read_buffer_at_offset(
425
3.89k
             file,
426
3.89k
             buffer,
427
3.89k
             ( size_t ) ( ( read_iterator % 7 ) + 1 ) * 511,
428
3.89k
             media_offset,
429
3.89k
             NULL ) == -1 )
430
531
        {
431
531
          break;
432
531
        }
433
3.36k
        media_offset += 637;
434
3.36k
      }
435
3.29k
      else
436
3.29k
      {
437
3.29k
        if( libqcow_file_seek_offset(
438
3.29k
             file,
439
3.29k
             ( off64_t ) ( read_iterator % 5 ),
440
3.29k
             ( int ) read_whence,
441
3.29k
             NULL ) == -1 )
442
1
        {
443
1
          break;
444
1
        }
445
3.29k
        if( libqcow_file_read_buffer(
446
3.29k
             file,
447
3.29k
             buffer,
448
3.29k
             ( size_t ) ( ( read_iterator % 7 ) + 1 ) * 511,
449
3.29k
             NULL ) == -1 )
450
54
        {
451
54
          break;
452
54
        }
453
3.29k
      }
454
7.19k
    }
455
716
  }
456
  /* Exercises the filename based functions by writing the input data
457
   * to a temporary file
458
   */
459
1.19k
  if( size > 4
460
1.19k
   && data[0] == ( uint8_t ) 'Q'
461
1.19k
   && data[1] == ( uint8_t ) 'F'
462
1.19k
   && data[2] == ( uint8_t ) 'I'
463
1.19k
   && ( data[3] == ( uint8_t ) 0xfb || data[3] == ( uint8_t ) 0xff ) )
464
1.19k
  {
465
1.19k
    char path_name[ 48 ] = "/tmp/libqcow_fuzzer_XXXXXX";
466
467
1.19k
    int fd = mkstemp(
468
1.19k
                path_name );
469
470
1.19k
    if( fd != -1 )
471
1.19k
    {
472
1.19k
      FILE *data_file = fopen(
473
1.19k
                          path_name,
474
1.19k
                          "wb" );
475
476
1.19k
      if( data_file != NULL )
477
1.19k
      {
478
1.19k
        fwrite(
479
1.19k
         data,
480
1.19k
         1,
481
1.19k
         size,
482
1.19k
         data_file );
483
1.19k
        fclose(
484
1.19k
         data_file );
485
1.19k
      }
486
1.19k
      close(
487
1.19k
       fd );
488
1.19k
      if( libqcow_file_initialize(
489
1.19k
           &path_file,
490
1.19k
           NULL ) == 1 )
491
1.19k
      {
492
1.19k
        libqcow_check_file_signature(
493
1.19k
         path_name,
494
1.19k
         NULL );
495
496
#if defined( LIBQCOW_HAVE_WIDE_CHARACTER_TYPE )
497
        wchar_t path_wide_name[ 48 ];
498
        size_t wide_name_length       = 0;
499
500
        while( wide_name_length < 47
501
         && path_name[ wide_name_length ] != 0 )
502
        {
503
          path_wide_name[ wide_name_length ] = ( wchar_t ) path_name[ wide_name_length ];
504
          wide_name_length++;
505
        }
506
        path_wide_name[ wide_name_length ] = 0;
507
508
        libqcow_check_file_signature_wide(
509
         path_wide_name,
510
         NULL );
511
#endif
512
1.19k
        if( libqcow_file_open(
513
1.19k
             path_file,
514
1.19k
             path_name,
515
1.19k
             LIBQCOW_OPEN_READ,
516
1.19k
             NULL ) == 1 )
517
1.19k
        {
518
1.19k
          libqcow_file_read_buffer(
519
1.19k
           path_file,
520
1.19k
           buffer,
521
1.19k
           64,
522
1.19k
           NULL );
523
524
1.19k
          libqcow_file_get_media_size(
525
1.19k
           path_file,
526
1.19k
           &media_size,
527
1.19k
           NULL );
528
529
1.19k
          libqcow_file_close(
530
1.19k
           path_file,
531
1.19k
           NULL );
532
1.19k
        }
533
1.19k
        libqcow_file_free(
534
1.19k
         &path_file,
535
1.19k
         NULL );
536
1.19k
      }
537
1.19k
      unlink(
538
1.19k
       path_name );
539
1.19k
    }
540
1.19k
  }
541
1.19k
  libqcow_file_get_offset(
542
1.19k
   file,
543
1.19k
   &read_offset,
544
1.19k
   NULL );
545
1.19k
  libqcow_file_close(
546
1.19k
   file,
547
1.19k
   NULL );
548
549
2.09k
on_error:
550
2.09k
  if( notify_stream != NULL )
551
2.09k
  {
552
2.09k
    libqcow_notify_set_stream(
553
2.09k
     NULL,
554
2.09k
     NULL );
555
556
2.09k
    fclose(
557
2.09k
     notify_stream );
558
2.09k
  }
559
2.09k
  if( path_file != NULL )
560
0
  {
561
0
    libqcow_file_free(
562
0
     &path_file,
563
0
     NULL );
564
0
  }
565
2.09k
  libqcow_file_free(
566
2.09k
   &file,
567
2.09k
   NULL );
568
569
2.09k
  if( parent_file != NULL )
570
29
  {
571
29
    libqcow_file_free(
572
29
     &parent_file,
573
29
     NULL );
574
29
  }
575
2.09k
  libbfio_handle_free(
576
2.09k
   &file_io_handle,
577
2.09k
   NULL );
578
579
2.09k
  if( parent_file_io_handle != NULL )
580
29
  {
581
29
    libbfio_handle_free(
582
29
     &parent_file_io_handle,
583
     NULL );
584
29
  }
585
2.09k
  return( 0 );
586
1.19k
}
587
588
} /* extern "C" */