Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvmdk/ossfuzz/handle_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libvmdk handle type
3
 *
4
 * Copyright (C) 2009-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_libvmdk.h"
31
32
#if !defined( LIBVMDK_HAVE_BFIO )
33
34
/* Opens a handle using a Basic File IO (bfio) handle
35
 * Returns 1 if successful or -1 on error
36
 */
37
LIBVMDK_EXTERN \
38
int libvmdk_handle_open_file_io_handle(
39
      libvmdk_handle_t *handle,
40
      libbfio_handle_t *file_io_handle,
41
      int access_flags,
42
      libvmdk_error_t **error );
43
44
/* Opens the extent data files using a Basic File IO (bfio) pool
45
 * This function assumes the extent data files are in the same order as defined by the descriptor file
46
 * Returns 1 if successful or -1 on error
47
 */
48
LIBVMDK_EXTERN \
49
int libvmdk_handle_open_extent_data_files_file_io_pool(
50
      libvmdk_handle_t *handle,
51
      libbfio_pool_t *file_io_pool,
52
      libvmdk_error_t **error );
53
54
#endif /* !defined( LIBVMDK_HAVE_BFIO ) */
55
56
int LLVMFuzzerTestOneInput(
57
      const uint8_t *data,
58
      size_t size )
59
8.68k
{
60
8.68k
  uint8_t buffer[ 512 ];
61
8.68k
  uint8_t utf8_string[ 64 ];
62
8.68k
  uint16_t utf16_string[ 64 ];
63
64
8.68k
  libbfio_handle_t *file_io_handle = NULL;
65
8.68k
  libbfio_pool_t *file_io_pool     = NULL;
66
8.68k
  libvmdk_handle_t *handle         = NULL;
67
8.68k
  off64_t media_offset             = 0;
68
8.68k
  size64_t media_size              = 0;
69
8.68k
  size_t string_size               = 0;
70
8.68k
  ssize_t read_count               = 0;
71
8.68k
  uint32_t value_32bit             = 0;
72
8.68k
  int disk_type                    = 0;
73
8.68k
  int entry_index                  = 0;
74
8.68k
  int number_of_extents            = 0;
75
8.68k
  int read_iterator                = 0;
76
77
8.68k
  if( libbfio_memory_range_initialize(
78
8.68k
       &file_io_handle,
79
8.68k
       NULL ) != 1 )
80
0
  {
81
0
    return( 0 );
82
0
  }
83
8.68k
  if( libbfio_memory_range_set(
84
8.68k
       file_io_handle,
85
8.68k
       (uint8_t *) data,
86
8.68k
       size,
87
8.68k
       NULL ) != 1 )
88
0
  {
89
0
    goto on_error_libbfio;
90
0
  }
91
8.68k
  if( libbfio_pool_initialize(
92
8.68k
       &file_io_pool,
93
8.68k
       0,
94
8.68k
       0,
95
8.68k
       NULL ) != 1 )
96
0
  {
97
0
    goto on_error_libbfio;
98
0
  }
99
8.68k
  if( libvmdk_handle_initialize(
100
8.68k
       &handle,
101
8.68k
       NULL ) != 1 )
102
0
  {
103
0
    goto on_error_libbfio;
104
0
  }
105
8.68k
  if( libvmdk_handle_open_file_io_handle(
106
8.68k
       handle,
107
8.68k
       file_io_handle,
108
8.68k
       LIBVMDK_OPEN_READ,
109
8.68k
       NULL ) != 1 )
110
7.22k
  {
111
7.22k
    goto on_error_libvmdk;
112
7.22k
  }
113
1.45k
  if( libbfio_pool_append_handle(
114
1.45k
       file_io_pool,
115
1.45k
       &entry_index,
116
1.45k
       file_io_handle,
117
1.45k
       LIBBFIO_OPEN_READ,
118
1.45k
       NULL ) != 1 )
119
0
  {
120
0
    goto on_error_libvmdk;
121
0
  }
122
  /* The file IO pool takes over management of the file IO handle
123
   */
124
1.45k
  file_io_handle = NULL;
125
126
  /* Note that the extent data files can only be opened if the number
127
   * of file IO handles in the pool matches the number of extents
128
   * defined in the descriptor file.
129
   */
130
1.45k
  libvmdk_handle_open_extent_data_files_file_io_pool(
131
1.45k
   handle,
132
1.45k
   file_io_pool,
133
1.45k
   NULL );
134
135
1.45k
  if( libvmdk_handle_set_maximum_number_of_open_handles(
136
1.45k
       handle,
137
1.45k
       8,
138
1.45k
       NULL ) != 1 )
139
25
  {
140
25
    goto on_error_libvmdk;
141
25
  }
142
1.43k
  if( libvmdk_handle_get_disk_type(
143
1.43k
       handle,
144
1.43k
       &disk_type,
145
1.43k
       NULL ) != 1 )
146
0
  {
147
0
    goto on_error_libvmdk;
148
0
  }
149
1.43k
  if( libvmdk_handle_get_media_size(
150
1.43k
       handle,
151
1.43k
       &media_size,
152
1.43k
       NULL ) != 1 )
153
0
  {
154
0
    goto on_error_libvmdk;
155
0
  }
156
1.43k
  if( libvmdk_handle_get_content_identifier(
157
1.43k
       handle,
158
1.43k
       &value_32bit,
159
1.43k
       NULL ) != 1 )
160
0
  {
161
0
    goto on_error_libvmdk;
162
0
  }
163
1.43k
  if( libvmdk_handle_get_parent_content_identifier(
164
1.43k
       handle,
165
1.43k
       &value_32bit,
166
1.43k
       NULL ) == -1 )
167
0
  {
168
0
    goto on_error_libvmdk;
169
0
  }
170
1.43k
  if( libvmdk_handle_get_utf8_parent_filename_size(
171
1.43k
       handle,
172
1.43k
       &string_size,
173
1.43k
       NULL ) == -1 )
174
82
  {
175
82
    goto on_error_libvmdk;
176
82
  }
177
1.35k
  if( libvmdk_handle_get_utf8_parent_filename(
178
1.35k
       handle,
179
1.35k
       utf8_string,
180
1.35k
       64,
181
1.35k
       NULL ) == -1 )
182
56
  {
183
56
    goto on_error_libvmdk;
184
56
  }
185
1.29k
  if( libvmdk_handle_get_utf16_parent_filename_size(
186
1.29k
       handle,
187
1.29k
       &string_size,
188
1.29k
       NULL ) == -1 )
189
0
  {
190
0
    goto on_error_libvmdk;
191
0
  }
192
1.29k
  if( libvmdk_handle_get_utf16_parent_filename(
193
1.29k
       handle,
194
1.29k
       utf16_string,
195
1.29k
       64,
196
1.29k
       NULL ) == -1 )
197
0
  {
198
0
    goto on_error_libvmdk;
199
0
  }
200
1.29k
  if( libvmdk_handle_get_number_of_extents(
201
1.29k
       handle,
202
1.29k
       &number_of_extents,
203
1.29k
       NULL ) != 1 )
204
0
  {
205
0
    goto on_error_libvmdk;
206
0
  }
207
1.29k
  if( libvmdk_handle_seek_offset(
208
1.29k
       handle,
209
1.29k
       0,
210
1.29k
       SEEK_SET,
211
1.29k
       NULL ) == -1 )
212
1.29k
  {
213
1.29k
    goto on_error_libvmdk;
214
1.29k
  }
215
0
  for( read_iterator = 0;
216
0
       read_iterator < 128;
217
0
       read_iterator++ )
218
0
  {
219
0
    if( media_offset >= media_size )
220
0
    {
221
0
      break;
222
0
    }
223
0
    read_count = libvmdk_handle_read_buffer(
224
0
                  handle,
225
0
                  buffer,
226
0
                  497,
227
0
                  NULL );
228
229
0
    if( read_count == -1 )
230
0
    {
231
0
      goto on_error_libvmdk;
232
0
    }
233
0
    if( read_count == 0 )
234
0
    {
235
0
      break;
236
0
    }
237
0
    media_offset += 497;
238
0
  }
239
0
  if( media_size > 0 )
240
0
  {
241
0
    if( libvmdk_handle_seek_offset(
242
0
         handle,
243
0
         (off64_t) ( media_size / 2 ),
244
0
         SEEK_SET,
245
0
         NULL ) == -1 )
246
0
    {
247
0
      goto on_error_libvmdk;
248
0
    }
249
0
  }
250
0
  if( libvmdk_handle_seek_offset(
251
0
       handle,
252
0
       512,
253
0
       SEEK_CUR,
254
0
       NULL ) == -1 )
255
0
  {
256
0
    goto on_error_libvmdk;
257
0
  }
258
0
  if( libvmdk_handle_seek_offset(
259
0
       handle,
260
0
       0,
261
0
       SEEK_END,
262
0
       NULL ) == -1 )
263
0
  {
264
0
    goto on_error_libvmdk;
265
0
  }
266
0
  if( libvmdk_handle_get_offset(
267
0
       handle,
268
0
       &media_offset,
269
0
       NULL ) != 1 )
270
0
  {
271
0
    goto on_error_libvmdk;
272
0
  }
273
0
  if( libvmdk_handle_seek_offset(
274
0
       handle,
275
0
       0,
276
0
       SEEK_SET,
277
0
       NULL ) == -1 )
278
0
  {
279
0
    goto on_error_libvmdk;
280
0
  }
281
0
  media_offset = 0;
282
283
0
  for( read_iterator = 0;
284
0
       read_iterator < 128;
285
0
       read_iterator++ )
286
0
  {
287
0
    if( media_offset >= media_size )
288
0
    {
289
0
      break;
290
0
    }
291
0
    if( libvmdk_handle_read_buffer_at_offset(
292
0
         handle,
293
0
         buffer,
294
0
         497,
295
0
         media_offset,
296
0
         NULL ) == -1 )
297
0
    {
298
0
      goto on_error_libvmdk;
299
0
    }
300
0
    media_offset += 497;
301
0
  }
302
0
  if( libvmdk_handle_signal_abort(
303
0
       handle,
304
0
       NULL ) != 1 )
305
0
  {
306
0
    goto on_error_libvmdk;
307
0
  }
308
0
  libvmdk_handle_close(
309
0
   handle,
310
0
   NULL );
311
312
8.68k
on_error_libvmdk:
313
8.68k
  libvmdk_handle_free(
314
8.68k
   &handle,
315
8.68k
   NULL );
316
317
8.68k
on_error_libbfio:
318
  /* Note that on error the handle still has a reference to file_io_pool
319
   * that will be closed. Therefore the file IO pool and handle need to
320
   * be freed after closing or freeing the handle.
321
   */
322
8.68k
  if( file_io_pool != NULL )
323
8.68k
  {
324
8.68k
    libbfio_pool_free(
325
8.68k
     &file_io_pool,
326
8.68k
     NULL );
327
8.68k
  }
328
8.68k
  if( file_io_handle != NULL )
329
7.22k
  {
330
7.22k
    libbfio_handle_free(
331
7.22k
     &file_io_handle,
332
     NULL );
333
7.22k
  }
334
8.68k
  return( 0 );
335
8.68k
}
336
337
} /* extern "C" */