Coverage Report

Created: 2026-07-25 07:30

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.89k
{
60
3.89k
  uint8_t buffer[ 512 ];
61
3.89k
  uint8_t uuid[ 16 ];
62
3.89k
  uint8_t utf8_string[ 64 ];
63
3.89k
  uint16_t utf16_string[ 64 ];
64
65
3.89k
  libbfio_handle_t *file_io_handle         = NULL;
66
3.89k
  libbfio_pool_t *file_io_pool             = NULL;
67
3.89k
  libfvde_volume_t *volume                 = NULL;
68
3.89k
  libfvde_logical_volume_t *logical_volume = NULL;
69
3.89k
  libfvde_volume_group_t *volume_group     = NULL;
70
3.89k
  off64_t volume_offset                    = 0;
71
3.89k
  size64_t volume_size                     = 0;
72
3.89k
  size_t string_size                       = 0;
73
3.89k
  int entry_index                          = 0;
74
3.89k
  int number_of_logical_volumes            = 0;
75
3.89k
  int read_iterator                        = 0;
76
77
3.89k
  if( libbfio_memory_range_initialize(
78
3.89k
       &file_io_handle,
79
3.89k
       NULL ) != 1 )
80
0
  {
81
0
    return( 0 );
82
0
  }
83
3.89k
  if( libbfio_memory_range_set(
84
3.89k
       file_io_handle,
85
3.89k
       (uint8_t *) data,
86
3.89k
       size,
87
3.89k
       NULL ) != 1 )
88
0
  {
89
0
    goto on_error_libbfio;
90
0
  }
91
3.89k
  if( libbfio_pool_initialize(
92
3.89k
       &file_io_pool,
93
3.89k
       0,
94
3.89k
       0,
95
3.89k
       NULL ) != 1 )
96
0
  {
97
0
    goto on_error_libbfio;
98
0
  }
99
3.89k
  if( libfvde_volume_initialize(
100
3.89k
       &volume,
101
3.89k
       NULL ) != 1 )
102
0
  {
103
0
    goto on_error_libbfio;
104
0
  }
105
3.89k
  if( libfvde_volume_open_file_io_handle(
106
3.89k
       volume,
107
3.89k
       file_io_handle,
108
3.89k
       LIBFVDE_OPEN_READ,
109
3.89k
       NULL ) != 1 )
110
1.84k
  {
111
1.84k
    goto on_error_libfvde_volume;
112
1.84k
  }
113
2.04k
  if( libbfio_pool_append_handle(
114
2.04k
       file_io_pool,
115
2.04k
       &entry_index,
116
2.04k
       file_io_handle,
117
2.04k
       LIBBFIO_OPEN_READ,
118
2.04k
       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
2.04k
  file_io_handle = NULL;
125
126
2.04k
  if( libfvde_volume_open_physical_volume_files_file_io_pool(
127
2.04k
       volume,
128
2.04k
       file_io_pool,
129
2.04k
       NULL ) != 1 )
130
1.46k
  {
131
1.46k
    goto on_error_libfvde_volume;
132
1.46k
  }
133
587
  if( libfvde_volume_get_volume_group(
134
587
       volume,
135
587
       &volume_group,
136
587
       NULL ) == 1 )
137
587
  {
138
587
    if( libfvde_volume_group_get_number_of_logical_volumes(
139
587
         volume_group,
140
587
         &number_of_logical_volumes,
141
587
         NULL ) != 1 )
142
0
    {
143
0
      goto on_error_libfvde_volume_group;
144
0
    }
145
587
    if( number_of_logical_volumes > 0 )
146
415
    {
147
415
      if( libfvde_volume_group_get_logical_volume_by_index(
148
415
           volume_group,
149
415
           0,
150
415
           &logical_volume,
151
415
           NULL ) != 1 )
152
243
      {
153
243
        goto on_error_libfvde_volume_group;
154
243
      }
155
172
      if( libfvde_logical_volume_get_identifier(
156
172
           logical_volume,
157
172
           uuid,
158
172
           16,
159
172
           NULL ) != 1 )
160
0
      {
161
0
        goto on_error_libfvde_logical_volume;
162
0
      }
163
172
      if( libfvde_logical_volume_get_utf8_name_size(
164
172
           logical_volume,
165
172
           &string_size,
166
172
           NULL ) != 1 )
167
1
      {
168
1
        goto on_error_libfvde_logical_volume;
169
1
      }
170
171
      if( libfvde_logical_volume_get_utf8_name(
171
171
           logical_volume,
172
171
           utf8_string,
173
171
           64,
174
171
           NULL ) != 1 )
175
0
      {
176
0
        goto on_error_libfvde_logical_volume;
177
0
      }
178
171
      if( libfvde_logical_volume_get_utf16_name_size(
179
171
           logical_volume,
180
171
           &string_size,
181
171
           NULL ) != 1 )
182
0
      {
183
0
        goto on_error_libfvde_logical_volume;
184
0
      }
185
171
      if( libfvde_logical_volume_get_utf16_name(
186
171
           logical_volume,
187
171
           utf16_string,
188
171
           64,
189
171
           NULL ) != 1 )
190
0
      {
191
0
        goto on_error_libfvde_logical_volume;
192
0
      }
193
171
      if( libfvde_logical_volume_get_size(
194
171
           logical_volume,
195
171
           &volume_size,
196
171
           NULL ) != 1 )
197
0
      {
198
0
        goto on_error_libfvde_logical_volume;
199
0
      }
200
171
      for( read_iterator = 0;
201
7.53k
           read_iterator < 128;
202
7.36k
           read_iterator++ )
203
7.48k
      {
204
7.48k
        if( volume_offset >= volume_size )
205
29
        {
206
29
          break;
207
29
        }
208
7.45k
        if( libfvde_logical_volume_read_buffer_at_offset(
209
7.45k
             logical_volume,
210
7.45k
             buffer,
211
7.45k
             497,
212
7.45k
             volume_offset,
213
7.45k
             NULL ) == -1 )
214
90
        {
215
90
          goto on_error_libfvde_logical_volume;
216
90
        }
217
7.36k
        volume_offset += 497;
218
7.36k
      }
219
172
on_error_libfvde_logical_volume:
220
172
      libfvde_logical_volume_free(
221
172
       &logical_volume,
222
172
       NULL );
223
172
    }
224
587
on_error_libfvde_volume_group:
225
587
    libfvde_volume_group_free(
226
587
     &volume_group,
227
587
     NULL );
228
587
  }
229
587
  libfvde_volume_close(
230
587
   volume,
231
587
   NULL );
232
233
3.89k
on_error_libfvde_volume:
234
3.89k
  libfvde_volume_free(
235
3.89k
   &volume,
236
3.89k
   NULL );
237
238
3.89k
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.89k
  if( file_io_pool != NULL )
244
3.89k
  {
245
3.89k
    libbfio_pool_free(
246
3.89k
     &file_io_pool,
247
3.89k
     NULL );
248
3.89k
  }
249
3.89k
  if( file_io_handle != NULL )
250
1.84k
  {
251
1.84k
    libbfio_handle_free(
252
1.84k
     &file_io_handle,
253
     NULL );
254
1.84k
  }
255
3.89k
  return( 0 );
256
3.89k
}
257
258
} /* extern "C" */
259