Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvhdi/ossfuzz/file_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libvhdi file 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
int LLVMFuzzerTestOneInput(
55
      const uint8_t *data,
56
      size_t size )
57
1.62k
{
58
1.62k
  uint8_t guid[ 16 ];
59
1.62k
  uint8_t utf8_string[ 64 ];
60
1.62k
  uint16_t utf16_string[ 64 ];
61
62
1.62k
  libbfio_handle_t *file_io_handle = NULL;
63
1.62k
  libvhdi_file_t *file             = NULL;
64
1.62k
  size64_t media_size              = 0;
65
1.62k
  size_t string_size               = 0;
66
1.62k
  uint32_t value_32bit             = 0;
67
1.62k
  uint16_t major_version           = 0;
68
1.62k
  uint16_t minor_version           = 0;
69
1.62k
  int file_type                    = 0;
70
71
1.62k
  if( libbfio_memory_range_initialize(
72
1.62k
       &file_io_handle,
73
1.62k
       NULL ) != 1 )
74
0
  {
75
0
    return( 0 );
76
0
  }
77
1.62k
  if( libbfio_memory_range_set(
78
1.62k
       file_io_handle,
79
1.62k
       (uint8_t *) data,
80
1.62k
       size,
81
1.62k
       NULL ) != 1 )
82
0
  {
83
0
    goto on_error_libbfio;
84
0
  }
85
1.62k
  if( libvhdi_check_file_signature_file_io_handle(
86
1.62k
       file_io_handle,
87
1.62k
       NULL ) == -1 )
88
0
  {
89
0
    goto on_error_libbfio;
90
0
  }
91
1.62k
  if( libvhdi_file_initialize(
92
1.62k
       &file,
93
1.62k
       NULL ) != 1 )
94
0
  {
95
0
    goto on_error_libbfio;
96
0
  }
97
1.62k
  if( libvhdi_file_open_file_io_handle(
98
1.62k
       file,
99
1.62k
       file_io_handle,
100
1.62k
       LIBVHDI_OPEN_READ,
101
1.62k
       NULL ) != 1 )
102
1.43k
  {
103
1.43k
    goto on_error_libvhdi;
104
1.43k
  }
105
183
  if( libvhdi_file_get_file_type(
106
183
       file,
107
183
       &file_type,
108
183
       NULL ) != 1 )
109
0
  {
110
0
    goto on_error_libvhdi;
111
0
  }
112
183
  if( libvhdi_file_get_format_version(
113
183
       file,
114
183
       &major_version,
115
183
       &minor_version,
116
183
       NULL ) != 1 )
117
0
  {
118
0
    goto on_error_libvhdi;
119
0
  }
120
183
  if( libvhdi_file_get_disk_type(
121
183
       file,
122
183
       &value_32bit,
123
183
       NULL ) != 1 )
124
0
  {
125
0
    goto on_error_libvhdi;
126
0
  }
127
183
  if( libvhdi_file_get_identifier(
128
183
       file,
129
183
       guid,
130
183
       16,
131
183
       NULL ) != 1 )
132
0
  {
133
0
    goto on_error_libvhdi;
134
0
  }
135
183
  if( libvhdi_file_get_parent_identifier(
136
183
       file,
137
183
       guid,
138
183
       16,
139
183
       NULL ) == -1 )
140
0
  {
141
0
    goto on_error_libvhdi;
142
0
  }
143
183
  if( libvhdi_file_get_bytes_per_sector(
144
183
       file,
145
183
       &value_32bit,
146
183
       NULL ) != 1 )
147
0
  {
148
0
    goto on_error_libvhdi;
149
0
  }
150
183
  if( libvhdi_file_get_utf8_parent_filename_size(
151
183
       file,
152
183
       &string_size,
153
183
       NULL ) == -1 )
154
43
  {
155
43
    goto on_error_libvhdi;
156
43
  }
157
140
  if( libvhdi_file_get_utf8_parent_filename(
158
140
       file,
159
140
       utf8_string,
160
140
       64,
161
140
       NULL ) == -1 )
162
36
  {
163
36
    goto on_error_libvhdi;
164
36
  }
165
104
  if( libvhdi_file_get_utf16_parent_filename_size(
166
104
       file,
167
104
       &string_size,
168
104
       NULL ) == -1 )
169
0
  {
170
0
    goto on_error_libvhdi;
171
0
  }
172
104
  if( libvhdi_file_get_utf16_parent_filename(
173
104
       file,
174
104
       utf16_string,
175
104
       64,
176
104
       NULL ) == -1 )
177
0
  {
178
0
    goto on_error_libvhdi;
179
0
  }
180
104
  if( libvhdi_file_get_media_size(
181
104
       file,
182
104
       &media_size,
183
104
       NULL ) != 1 )
184
0
  {
185
0
    goto on_error_libvhdi;
186
0
  }
187
104
  libvhdi_file_close(
188
104
   file,
189
104
   NULL );
190
191
1.62k
on_error_libvhdi:
192
1.62k
  libvhdi_file_free(
193
1.62k
   &file,
194
1.62k
   NULL );
195
196
1.62k
on_error_libbfio:
197
1.62k
  libbfio_handle_free(
198
1.62k
   &file_io_handle,
199
1.62k
   NULL );
200
201
1.62k
  return( 0 );
202
1.62k
}
203
204
} /* extern "C" */