Coverage Report

Created: 2026-10-22 07:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfvde/ossfuzz/logical_volume_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libfvde logical volume type
3
 *
4
 * Copyright (C) 2014-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_libfvde.h"
31
32
#if !defined( LIBFVDE_HAVE_BFIO )
33
34
/* Opens a volume using a Basic File IO (bfio) handle
35
 * Returns 1 if successful or -1 on error
36
 */
37
LIBFVDE_EXTERN \
38
int libfvde_volume_open_file_io_handle(
39
     libfvde_volume_t *volume,
40
     libbfio_handle_t *file_io_handle,
41
     int access_flags,
42
     libfvde_error_t **error );
43
44
/* Opens the physical volume files
45
 * This function assumes the physical volume files are in same order as defined by the metadata
46
 * Returns 1 if successful or -1 on error
47
 */
48
LIBFVDE_EXTERN \
49
int libfvde_volume_open_physical_volume_files_file_io_pool(
50
     libfvde_volume_t *volume,
51
     libbfio_pool_t *file_io_pool,
52
     libfvde_error_t **error );
53
54
#endif /* !defined( LIBFVDE_HAVE_BFIO ) */
55
56
int LLVMFuzzerTestOneInput(
57
     const uint8_t *data,
58
     size_t size )
59
3.85k
{
60
3.85k
  uint8_t buffer[ 512 ];
61
3.85k
  uint8_t uuid[ 16 ];
62
3.85k
  uint8_t utf8_string[ 64 ];
63
3.85k
  uint16_t utf16_string[ 64 ];
64
65
3.85k
  libbfio_handle_t *file_io_handle         = NULL;
66
3.85k
  libbfio_pool_t *file_io_pool             = NULL;
67
3.85k
  libfvde_volume_t *volume                 = NULL;
68
3.85k
  libfvde_logical_volume_t *logical_volume = NULL;
69
3.85k
  libfvde_volume_group_t *volume_group     = NULL;
70
3.85k
  off64_t volume_offset                    = 0;
71
3.85k
  size64_t volume_size                     = 0;
72
3.85k
  size_t string_size                       = 0;
73
3.85k
  int entry_index                          = 0;
74
3.85k
  int number_of_logical_volumes            = 0;
75
3.85k
  int read_iterator                        = 0;
76
77
3.85k
  if( libbfio_memory_range_initialize(
78
3.85k
       &file_io_handle,
79
3.85k
       NULL ) != 1 )
80
0
  {
81
0
    return( 0 );
82
0
  }
83
3.85k
  if( libbfio_memory_range_set(
84
3.85k
       file_io_handle,
85
3.85k
       (uint8_t *) data,
86
3.85k
       size,
87
3.85k
       NULL ) != 1 )
88
0
  {
89
0
    goto on_error_libbfio;
90
0
  }
91
3.85k
  if( libbfio_pool_initialize(
92
3.85k
       &file_io_pool,
93
3.85k
       0,
94
3.85k
       0,
95
3.85k
       NULL ) != 1 )
96
0
  {
97
0
    goto on_error_libbfio;
98
0
  }
99
3.85k
  if( libfvde_volume_initialize(
100
3.85k
       &volume,
101
3.85k
       NULL ) != 1 )
102
0
  {
103
0
    goto on_error_libbfio;
104
0
  }
105
3.85k
  if( libfvde_volume_open_file_io_handle(
106
3.85k
       volume,
107
3.85k
       file_io_handle,
108
3.85k
       LIBFVDE_OPEN_READ,
109
3.85k
       NULL ) != 1 )
110
1.85k
  {
111
1.85k
    goto on_error_libfvde_volume;
112
1.85k
  }
113
1.99k
  if( libbfio_pool_append_handle(
114
1.99k
       file_io_pool,
115
1.99k
       &entry_index,
116
1.99k
       file_io_handle,
117
1.99k
       LIBBFIO_OPEN_READ,
118
1.99k
       NULL ) != 1 )
119
0
  {
120
0
    goto on_error_libfvde_volume;
121
0
  }
122
  /* The file IO pool takes over management of the file IO handle
123
   */
124
1.99k
  file_io_handle = NULL;
125
126
1.99k
  if( libfvde_volume_open_physical_volume_files_file_io_pool(
127
1.99k
       volume,
128
1.99k
       file_io_pool,
129
1.99k
       NULL ) != 1 )
130
1.42k
  {
131
1.42k
    goto on_error_libfvde_volume;
132
1.42k
  }
133
567
  if( libfvde_volume_get_volume_group(
134
567
       volume,
135
567
       &volume_group,
136
567
       NULL ) == 1 )
137
567
  {
138
567
    if( libfvde_volume_group_get_number_of_logical_volumes(
139
567
         volume_group,
140
567
         &number_of_logical_volumes,
141
567
         NULL ) != 1 )
142
0
    {
143
0
      goto on_error_libfvde_volume_group;
144
0
    }
145
567
    if( number_of_logical_volumes > 0 )
146
407
    {
147
407
      if( libfvde_volume_group_get_logical_volume_by_index(
148
407
           volume_group,
149
407
           0,
150
407
           &logical_volume,
151
407
           NULL ) != 1 )
152
233
      {
153
233
        goto on_error_libfvde_volume_group;
154
233
      }
155
174
      if( libfvde_logical_volume_get_identifier(
156
174
           logical_volume,
157
174
           uuid,
158
174
           16,
159
174
           NULL ) != 1 )
160
0
      {
161
0
        goto on_error_libfvde_logical_volume;
162
0
      }
163
174
      if( libfvde_logical_volume_get_utf8_name_size(
164
174
           logical_volume,
165
174
           &string_size,
166
174
           NULL ) != 1 )
167
1
      {
168
1
        goto on_error_libfvde_logical_volume;
169
1
      }
170
173
      if( libfvde_logical_volume_get_utf8_name(
171
173
           logical_volume,
172
173
           utf8_string,
173
173
           64,
174
173
           NULL ) != 1 )
175
0
      {
176
0
        goto on_error_libfvde_logical_volume;
177
0
      }
178
173
      if( libfvde_logical_volume_get_utf16_name_size(
179
173
           logical_volume,
180
173
           &string_size,
181
173
           NULL ) != 1 )
182
0
      {
183
0
        goto on_error_libfvde_logical_volume;
184
0
      }
185
173
      if( libfvde_logical_volume_get_utf16_name(
186
173
           logical_volume,
187
173
           utf16_string,
188
173
           64,
189
173
           NULL ) != 1 )
190
0
      {
191
0
        goto on_error_libfvde_logical_volume;
192
0
      }
193
173
      if( libfvde_logical_volume_get_size(
194
173
           logical_volume,
195
173
           &volume_size,
196
173
           NULL ) != 1 )
197
0
      {
198
0
        goto on_error_libfvde_logical_volume;
199
0
      }
200
173
      for( read_iterator = 0;
201
8.09k
           read_iterator < 128;
202
7.92k
           read_iterator++ )
203
8.03k
      {
204
8.03k
        if( volume_offset >= volume_size )
205
28
        {
206
28
          break;
207
28
        }
208
8.00k
        if( libfvde_logical_volume_read_buffer_at_offset(
209
8.00k
             logical_volume,
210
8.00k
             buffer,
211
8.00k
             497,
212
8.00k
             volume_offset,
213
8.00k
             NULL ) == -1 )
214
88
        {
215
88
          goto on_error_libfvde_logical_volume;
216
88
        }
217
7.92k
        volume_offset += 497;
218
7.92k
      }
219
174
on_error_libfvde_logical_volume:
220
174
      libfvde_logical_volume_free(
221
174
       &logical_volume,
222
174
       NULL );
223
174
    }
224
567
on_error_libfvde_volume_group:
225
567
    libfvde_volume_group_free(
226
567
     &volume_group,
227
567
     NULL );
228
567
  }
229
567
  libfvde_volume_close(
230
567
   volume,
231
567
   NULL );
232
233
3.85k
on_error_libfvde_volume:
234
3.85k
  libfvde_volume_free(
235
3.85k
   &volume,
236
3.85k
   NULL );
237
238
3.85k
on_error_libbfio:
239
  /* Note that on error the volume still has a reference to file_io_pool
240
   * that will be closed. Therefore the file IO pool and handle need to
241
   * be freed after closing or freeing the volume.
242
   */
243
3.85k
  if( file_io_pool != NULL )
244
3.85k
  {
245
3.85k
    libbfio_pool_free(
246
3.85k
     &file_io_pool,
247
3.85k
     NULL );
248
3.85k
  }
249
3.85k
  if( file_io_handle != NULL )
250
1.85k
  {
251
1.85k
    libbfio_handle_free(
252
1.85k
     &file_io_handle,
253
     NULL );
254
1.85k
  }
255
3.85k
  return( 0 );
256
3.85k
}
257
258
} /* extern "C" */
259