Coverage Report

Created: 2026-12-01 07:31

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.84k
{
60
3.84k
  uint8_t buffer[ 512 ];
61
3.84k
  uint8_t uuid[ 16 ];
62
3.84k
  uint8_t utf8_string[ 64 ];
63
3.84k
  uint16_t utf16_string[ 64 ];
64
3.84k
  uint8_t volume_master_key[ 16 ] = {
65
3.84k
    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10
66
3.84k
  };
67
3.84k
  uint16_t utf16_password[ 9 ] = {
68
3.84k
    0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x00
69
3.84k
  };
70
3.84k
  uint16_t utf16_recovery_password[ 9 ] = {
71
3.84k
    0x72, 0x65, 0x63, 0x6f, 0x76, 0x65, 0x72, 0x79, 0x00
72
3.84k
  };
73
74
3.84k
  libbfio_handle_t *file_io_handle         = NULL;
75
3.84k
  libbfio_pool_t *file_io_pool             = NULL;
76
3.84k
  libfvde_volume_t *volume                 = NULL;
77
3.84k
  libfvde_logical_volume_t *logical_volume = NULL;
78
3.84k
  libfvde_volume_group_t *volume_group     = NULL;
79
3.84k
  off64_t volume_offset                    = 0;
80
3.84k
  off64_t stream_offset                    = 0;
81
3.84k
  size64_t volume_size                     = 0;
82
3.84k
  size_t string_size                       = 0;
83
3.84k
  int entry_index                          = 0;
84
3.84k
  int number_of_logical_volumes            = 0;
85
3.84k
  int read_iterator                        = 0;
86
87
3.84k
  if( libbfio_memory_range_initialize(
88
3.84k
       &file_io_handle,
89
3.84k
       NULL ) != 1 )
90
0
  {
91
0
    return( 0 );
92
0
  }
93
3.84k
  if( libbfio_memory_range_set(
94
3.84k
       file_io_handle,
95
3.84k
       (uint8_t *) data,
96
3.84k
       size,
97
3.84k
       NULL ) != 1 )
98
0
  {
99
0
    goto on_error_libbfio;
100
0
  }
101
3.84k
  if( libbfio_pool_initialize(
102
3.84k
       &file_io_pool,
103
3.84k
       0,
104
3.84k
       0,
105
3.84k
       NULL ) != 1 )
106
0
  {
107
0
    goto on_error_libbfio;
108
0
  }
109
3.84k
  if( libfvde_volume_initialize(
110
3.84k
       &volume,
111
3.84k
       NULL ) != 1 )
112
0
  {
113
0
    goto on_error_libbfio;
114
0
  }
115
3.84k
  if( libfvde_volume_open_file_io_handle(
116
3.84k
       volume,
117
3.84k
       file_io_handle,
118
3.84k
       LIBFVDE_OPEN_READ,
119
3.84k
       NULL ) != 1 )
120
1.88k
  {
121
1.88k
    goto on_error_libfvde_volume;
122
1.88k
  }
123
1.95k
  if( libbfio_pool_append_handle(
124
1.95k
       file_io_pool,
125
1.95k
       &entry_index,
126
1.95k
       file_io_handle,
127
1.95k
       LIBBFIO_OPEN_READ,
128
1.95k
       NULL ) != 1 )
129
0
  {
130
0
    goto on_error_libfvde_volume;
131
0
  }
132
  /* The file IO pool takes over management of the file IO handle
133
   */
134
1.95k
  file_io_handle = NULL;
135
136
1.95k
  if( libfvde_volume_open_physical_volume_files_file_io_pool(
137
1.95k
       volume,
138
1.95k
       file_io_pool,
139
1.95k
       NULL ) != 1 )
140
1.43k
  {
141
1.43k
    goto on_error_libfvde_volume;
142
1.43k
  }
143
519
  if( libfvde_volume_get_volume_group(
144
519
       volume,
145
519
       &volume_group,
146
519
       NULL ) == 1 )
147
519
  {
148
519
    if( libfvde_volume_group_get_number_of_logical_volumes(
149
519
         volume_group,
150
519
         &number_of_logical_volumes,
151
519
         NULL ) != 1 )
152
0
    {
153
0
      goto on_error_libfvde_volume_group;
154
0
    }
155
519
    if( number_of_logical_volumes > 0 )
156
365
    {
157
365
      if( libfvde_volume_group_get_logical_volume_by_index(
158
365
           volume_group,
159
365
           0,
160
365
           &logical_volume,
161
365
           NULL ) != 1 )
162
202
      {
163
202
        goto on_error_libfvde_volume_group;
164
202
      }
165
163
      if( libfvde_logical_volume_get_identifier(
166
163
           logical_volume,
167
163
           uuid,
168
163
           16,
169
163
           NULL ) != 1 )
170
0
      {
171
0
        goto on_error_libfvde_logical_volume;
172
0
      }
173
163
      if( libfvde_logical_volume_get_utf8_name_size(
174
163
           logical_volume,
175
163
           &string_size,
176
163
           NULL ) != 1 )
177
1
      {
178
1
        goto on_error_libfvde_logical_volume;
179
1
      }
180
162
      if( libfvde_logical_volume_get_utf8_name(
181
162
           logical_volume,
182
162
           utf8_string,
183
162
           64,
184
162
           NULL ) != 1 )
185
0
      {
186
0
        goto on_error_libfvde_logical_volume;
187
0
      }
188
162
      if( libfvde_logical_volume_get_utf16_name_size(
189
162
           logical_volume,
190
162
           &string_size,
191
162
           NULL ) != 1 )
192
0
      {
193
0
        goto on_error_libfvde_logical_volume;
194
0
      }
195
162
      if( libfvde_logical_volume_get_utf16_name(
196
162
           logical_volume,
197
162
           utf16_string,
198
162
           64,
199
162
           NULL ) != 1 )
200
0
      {
201
0
        goto on_error_libfvde_logical_volume;
202
0
      }
203
162
      if( libfvde_logical_volume_get_size(
204
162
           logical_volume,
205
162
           &volume_size,
206
162
           NULL ) != 1 )
207
0
      {
208
0
        goto on_error_libfvde_logical_volume;
209
0
      }
210
162
      libfvde_logical_volume_is_locked(
211
162
       logical_volume,
212
162
       NULL );
213
214
162
      libfvde_logical_volume_set_utf8_password(
215
162
       logical_volume,
216
162
       (uint8_t *) "password",
217
162
       8,
218
162
       NULL );
219
220
162
      libfvde_logical_volume_set_utf16_password(
221
162
       logical_volume,
222
162
       utf16_password,
223
162
       sizeof( utf16_password ),
224
162
       NULL );
225
226
162
      libfvde_logical_volume_set_utf8_recovery_password(
227
162
       logical_volume,
228
162
       (uint8_t *) "recovery_password",
229
162
       17,
230
162
       NULL );
231
232
162
      libfvde_logical_volume_set_utf16_recovery_password(
233
162
       logical_volume,
234
162
       utf16_recovery_password,
235
162
       sizeof( utf16_recovery_password ),
236
162
       NULL );
237
238
162
      libfvde_logical_volume_set_key(
239
162
       logical_volume,
240
162
       volume_master_key,
241
162
       16,
242
162
       NULL );
243
244
162
      libfvde_logical_volume_unlock(
245
162
       logical_volume,
246
162
       NULL );
247
248
162
      libfvde_logical_volume_is_locked(
249
162
       logical_volume,
250
162
       NULL );
251
252
162
      libfvde_logical_volume_get_offset(
253
162
       logical_volume,
254
162
       &stream_offset,
255
162
       NULL );
256
257
162
      libfvde_logical_volume_read_buffer(
258
162
       logical_volume,
259
162
       buffer,
260
162
       512,
261
162
       NULL );
262
263
162
      libfvde_logical_volume_get_offset(
264
162
       logical_volume,
265
162
       &stream_offset,
266
162
       NULL );
267
268
162
      libfvde_logical_volume_seek_offset(
269
162
       logical_volume,
270
162
       0,
271
162
       0,
272
162
       NULL );
273
274
162
      for( read_iterator = 0;
275
7.82k
           read_iterator < 128;
276
7.66k
           read_iterator++ )
277
7.77k
      {
278
7.77k
        if( volume_offset >= volume_size )
279
24
        {
280
24
          break;
281
24
        }
282
7.74k
        if( libfvde_logical_volume_read_buffer_at_offset(
283
7.74k
             logical_volume,
284
7.74k
             buffer,
285
7.74k
             497,
286
7.74k
             volume_offset,
287
7.74k
             NULL ) == -1 )
288
83
        {
289
83
          goto on_error_libfvde_logical_volume;
290
83
        }
291
7.66k
        volume_offset += 497;
292
7.66k
      }
293
163
on_error_libfvde_logical_volume:
294
163
      libfvde_logical_volume_free(
295
163
       &logical_volume,
296
163
       NULL );
297
163
    }
298
519
on_error_libfvde_volume_group:
299
519
    libfvde_volume_group_free(
300
519
     &volume_group,
301
519
     NULL );
302
519
  }
303
519
  libfvde_volume_close(
304
519
   volume,
305
519
   NULL );
306
307
3.84k
on_error_libfvde_volume:
308
3.84k
  libfvde_volume_free(
309
3.84k
   &volume,
310
3.84k
   NULL );
311
312
3.84k
on_error_libbfio:
313
  /* Note that on error the volume still has a reference to file_io_pool
314
   * that will be closed. Therefore the file IO pool and handle need to
315
   * be freed after closing or freeing the volume.
316
   */
317
3.84k
  if( file_io_pool != NULL )
318
3.84k
  {
319
3.84k
    libbfio_pool_free(
320
3.84k
     &file_io_pool,
321
3.84k
     NULL );
322
3.84k
  }
323
3.84k
  if( file_io_handle != NULL )
324
1.88k
  {
325
1.88k
    libbfio_handle_free(
326
1.88k
     &file_io_handle,
327
     NULL );
328
1.88k
  }
329
3.84k
  return( 0 );
330
3.84k
}
331
332
} /* extern "C" */
333