Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvhdi/ossfuzz/block_descriptor_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libvhdi block descriptor type
3
 *
4
 * Copyright (C) 2012-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_libvhdi.h"
31
32
#if !defined( LIBVHDI_HAVE_BFIO )
33
34
/* Determines if a file contains a VHD file signature using a Basic File IO (bfio) handle
35
 * Returns 1 if true, 0 if not or -1 on error
36
 */
37
LIBVHDI_EXTERN \
38
int libvhdi_check_file_signature_file_io_handle(
39
      libbfio_handle_t *file_io_handle,
40
      libvhdi_error_t **error );
41
42
/* Opens a file using a Basic File IO (bfio) handle
43
 * Returns 1 if successful or -1 on error
44
 */
45
LIBVHDI_EXTERN \
46
int libvhdi_file_open_file_io_handle(
47
      libvhdi_file_t *file,
48
      libbfio_handle_t *file_io_handle,
49
      int access_flags,
50
      libvhdi_error_t **error );
51
52
#endif /* !defined( LIBVHDI_HAVE_BFIO ) */
53
54
22.6k
#define BLOCK_DESCRIPTOR_FUZZER_MAXIMUM_READ_ITERATIONS 128
55
56
11.1k
#define BLOCK_DESCRIPTOR_FUZZER_MAXIMUM_READ_SIZE       4096
57
58
int LLVMFuzzerTestOneInput(
59
      const uint8_t *data,
60
      size_t size )
61
1.59k
{
62
1.59k
  uint8_t buffer[ BLOCK_DESCRIPTOR_FUZZER_MAXIMUM_READ_SIZE ];
63
1.59k
  uint8_t guid[ 16 ];
64
65
1.59k
  libbfio_handle_t *parent_file_io_handle = NULL;
66
1.59k
  libbfio_handle_t *file_io_handle        = NULL;
67
1.59k
  libvhdi_file_t *parent_file             = NULL;
68
1.59k
  libvhdi_file_t *file                    = NULL;
69
1.59k
  off64_t media_offset                    = 0;
70
1.59k
  off64_t read_offset                     = 0;
71
1.59k
  size64_t media_size                      = 0;
72
1.59k
  size_t read_size                         = 0;
73
1.59k
  int file_type                            = 0;
74
1.59k
  int parent_ready                         = 0;
75
1.59k
  int read_iterator                        = 0;
76
1.59k
  uint32_t value_32bit                     = 0;
77
1.59k
  uint16_t major_version                   = 0;
78
1.59k
  uint16_t minor_version                   = 0;
79
80
1.59k
  if( size == 0 )
81
0
  {
82
0
    return( 0 );
83
0
  }
84
1.59k
  if( libbfio_memory_range_initialize(
85
1.59k
       &file_io_handle,
86
1.59k
       NULL ) != 1 )
87
0
  {
88
0
    return( 0 );
89
0
  }
90
1.59k
  if( libbfio_memory_range_set(
91
1.59k
       file_io_handle,
92
1.59k
       (uint8_t *) data,
93
1.59k
       size,
94
1.59k
       NULL ) != 1 )
95
0
  {
96
0
    goto on_error_libbfio;
97
0
  }
98
1.59k
  if( libvhdi_check_file_signature_file_io_handle(
99
1.59k
       file_io_handle,
100
1.59k
       NULL ) == -1 )
101
0
  {
102
0
    goto on_error_libbfio;
103
0
  }
104
1.59k
  if( libvhdi_file_initialize(
105
1.59k
       &file,
106
1.59k
       NULL ) != 1 )
107
0
  {
108
0
    goto on_error_libbfio;
109
0
  }
110
1.59k
  if( libvhdi_file_open_file_io_handle(
111
1.59k
       file,
112
1.59k
       file_io_handle,
113
1.59k
       LIBVHDI_OPEN_READ,
114
1.59k
       NULL ) != 1 )
115
955
  {
116
955
    goto on_error_libvhdi;
117
955
  }
118
637
  if( libvhdi_file_get_file_type(
119
637
       file,
120
637
       &file_type,
121
637
       NULL ) != 1 )
122
0
  {
123
0
    goto on_error_libvhdi;
124
0
  }
125
637
  if( libvhdi_file_get_format_version(
126
637
       file,
127
637
       &major_version,
128
637
       &minor_version,
129
637
       NULL ) != 1 )
130
0
  {
131
0
    goto on_error_libvhdi;
132
0
  }
133
637
  if( libvhdi_file_get_disk_type(
134
637
       file,
135
637
       &value_32bit,
136
637
       NULL ) != 1 )
137
0
  {
138
0
    goto on_error_libvhdi;
139
0
  }
140
637
  if( libvhdi_file_get_bytes_per_sector(
141
637
       file,
142
637
       &value_32bit,
143
637
       NULL ) != 1 )
144
0
  {
145
0
    goto on_error_libvhdi;
146
0
  }
147
637
  if( libvhdi_file_get_media_size(
148
637
       file,
149
637
       &media_size,
150
637
       NULL ) != 1 )
151
0
  {
152
0
    goto on_error_libvhdi;
153
0
  }
154
637
  if( libvhdi_file_get_identifier(
155
637
       file,
156
637
       guid,
157
637
       16,
158
637
       NULL ) != 1 )
159
0
  {
160
0
    goto on_error_libvhdi;
161
0
  }
162
637
  if( libvhdi_file_get_parent_identifier(
163
637
       file,
164
637
       guid,
165
637
       16,
166
637
       NULL ) == -1 )
167
0
  {
168
0
    goto on_error_libvhdi;
169
0
  }
170
637
  if( ( data[ 0 ] & 0x01 ) != 0 )
171
452
  {
172
452
    size_t parent_data_offset = size / 2;
173
452
    size_t parent_data_length = size - parent_data_offset;
174
175
452
    if( libbfio_memory_range_initialize(
176
452
         &parent_file_io_handle,
177
452
         NULL ) == 1
178
452
     && libbfio_memory_range_set(
179
452
                   parent_file_io_handle,
180
452
                   (uint8_t * ) ( data + parent_data_offset ),
181
452
                   parent_data_length,
182
452
                   NULL ) == 1
183
452
     && libvhdi_file_initialize(
184
452
                   &parent_file,
185
452
                   NULL ) == 1
186
452
     && libvhdi_file_open_file_io_handle(
187
452
                   parent_file,
188
452
                   parent_file_io_handle,
189
452
                   LIBVHDI_OPEN_READ,
190
452
                   NULL ) == 1 )
191
79
    {
192
      /* A 0 result from set_parent_file means the parent does not match
193
       * a -1 result indicates an error
194
       */
195
79
      if( libvhdi_file_set_parent_file(
196
79
           file,
197
79
           parent_file,
198
79
           NULL ) == 1 )
199
5
      {
200
5
        parent_ready = 1;
201
5
      }
202
79
    }
203
452
    if( parent_ready == 0 )
204
447
    {
205
447
      if( parent_file != NULL )
206
447
      {
207
447
        libvhdi_file_free(
208
447
         &parent_file,
209
447
         NULL );
210
211
447
        parent_file = NULL;
212
447
      }
213
447
      if( parent_file_io_handle != NULL )
214
447
      {
215
447
        libbfio_handle_free(
216
447
         &parent_file_io_handle,
217
447
         NULL );
218
219
447
        parent_file_io_handle = NULL;
220
447
      }
221
447
    }
222
452
  }
223
637
  if( media_size > 0 )
224
634
  {
225
    /* Bounded read of the media to exercise the block descriptor
226
     * sector bitmap read path
227
     */
228
634
    for( read_iterator = 0;
229
11.3k
        read_iterator < BLOCK_DESCRIPTOR_FUZZER_MAXIMUM_READ_ITERATIONS
230
11.2k
        && media_offset < ( off64_t ) media_size;
231
10.6k
        read_iterator++ )
232
11.1k
    {
233
11.1k
      if( ( data[ 0 ] & 0x02 ) != 0
234
4.79k
       && ( read_iterator % 4 ) == 3 )
235
1.11k
      {
236
1.11k
        libvhdi_file_signal_abort(
237
1.11k
         file,
238
1.11k
         NULL );
239
1.11k
      }
240
11.1k
      read_size = ( size_t ) ( ( read_iterator % 7 ) + 1 ) * 511;
241
242
11.1k
      if( read_size > BLOCK_DESCRIPTOR_FUZZER_MAXIMUM_READ_SIZE )
243
0
      {
244
0
        read_size = BLOCK_DESCRIPTOR_FUZZER_MAXIMUM_READ_SIZE;
245
0
      }
246
11.1k
      if( ( read_iterator % 2 ) == 0 )
247
5.75k
      {
248
5.75k
        if( media_offset + ( off64_t ) read_size > ( off64_t ) media_size )
249
13
        {
250
13
          break;
251
13
        }
252
5.74k
        if( libvhdi_file_read_buffer_at_offset(
253
5.74k
             file,
254
5.74k
             buffer,
255
5.74k
             read_size,
256
5.74k
             media_offset,
257
5.74k
             NULL ) == -1 )
258
314
        {
259
314
          break;
260
314
        }
261
5.74k
      }
262
5.42k
      else
263
5.42k
      {
264
5.42k
        if( libvhdi_file_read_buffer(
265
5.42k
             file,
266
5.42k
             buffer,
267
5.42k
             read_size,
268
5.42k
             NULL ) == -1 )
269
171
        {
270
171
          break;
271
171
        }
272
5.24k
        if( libvhdi_file_get_offset(
273
5.24k
             file,
274
5.24k
             &read_offset,
275
5.24k
             NULL ) == -1 )
276
0
        {
277
0
          break;
278
0
        }
279
5.24k
      }
280
10.6k
      media_offset += 4097;
281
10.6k
    }
282
634
  }
283
637
  libvhdi_file_close(
284
637
   file,
285
637
   NULL );
286
287
1.59k
on_error_libvhdi:
288
1.59k
  libvhdi_file_free(
289
1.59k
   &file,
290
1.59k
   NULL );
291
292
1.59k
  if( parent_file != NULL )
293
5
  {
294
5
    libvhdi_file_free(
295
5
     &parent_file,
296
5
     NULL );
297
298
5
    parent_file = NULL;
299
5
  }
300
1.59k
on_error_libbfio:
301
1.59k
  if( parent_file_io_handle != NULL )
302
5
  {
303
5
    libbfio_handle_free(
304
5
     &parent_file_io_handle,
305
5
     NULL );
306
307
5
    parent_file_io_handle = NULL;
308
5
  }
309
1.59k
  if( file_io_handle != NULL )
310
1.59k
  {
311
1.59k
    libbfio_handle_free(
312
1.59k
     &file_io_handle,
313
1.59k
     NULL );
314
315
    file_io_handle = NULL;
316
1.59k
  }
317
1.59k
  return( 0 );
318
1.59k
}
319
320
} /* extern "C" */