Coverage Report

Created: 2026-08-31 07:43

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.90k
{
60
3.90k
  uint8_t buffer[ 512 ];
61
3.90k
  uint8_t uuid[ 16 ];
62
3.90k
  uint8_t utf8_string[ 64 ];
63
3.90k
  uint16_t utf16_string[ 64 ];
64
65
3.90k
  libbfio_handle_t *file_io_handle         = NULL;
66
3.90k
  libbfio_pool_t *file_io_pool             = NULL;
67
3.90k
  libfvde_volume_t *volume                 = NULL;
68
3.90k
  libfvde_logical_volume_t *logical_volume = NULL;
69
3.90k
  libfvde_volume_group_t *volume_group     = NULL;
70
3.90k
  off64_t volume_offset                    = 0;
71
3.90k
  size64_t volume_size                     = 0;
72
3.90k
  size_t string_size                       = 0;
73
3.90k
  int entry_index                          = 0;
74
3.90k
  int number_of_logical_volumes            = 0;
75
3.90k
  int read_iterator                        = 0;
76
77
3.90k
  if( libbfio_memory_range_initialize(
78
3.90k
       &file_io_handle,
79
3.90k
       NULL ) != 1 )
80
0
  {
81
0
    return( 0 );
82
0
  }
83
3.90k
  if( libbfio_memory_range_set(
84
3.90k
       file_io_handle,
85
3.90k
       (uint8_t *) data,
86
3.90k
       size,
87
3.90k
       NULL ) != 1 )
88
0
  {
89
0
    goto on_error_libbfio;
90
0
  }
91
3.90k
  if( libbfio_pool_initialize(
92
3.90k
       &file_io_pool,
93
3.90k
       0,
94
3.90k
       0,
95
3.90k
       NULL ) != 1 )
96
0
  {
97
0
    goto on_error_libbfio;
98
0
  }
99
3.90k
  if( libfvde_volume_initialize(
100
3.90k
       &volume,
101
3.90k
       NULL ) != 1 )
102
0
  {
103
0
    goto on_error_libbfio;
104
0
  }
105
3.90k
  if( libfvde_volume_open_file_io_handle(
106
3.90k
       volume,
107
3.90k
       file_io_handle,
108
3.90k
       LIBFVDE_OPEN_READ,
109
3.90k
       NULL ) != 1 )
110
1.86k
  {
111
1.86k
    goto on_error_libfvde_volume;
112
1.86k
  }
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
580
  if( libfvde_volume_get_volume_group(
134
580
       volume,
135
580
       &volume_group,
136
580
       NULL ) == 1 )
137
580
  {
138
580
    if( libfvde_volume_group_get_number_of_logical_volumes(
139
580
         volume_group,
140
580
         &number_of_logical_volumes,
141
580
         NULL ) != 1 )
142
0
    {
143
0
      goto on_error_libfvde_volume_group;
144
0
    }
145
580
    if( number_of_logical_volumes > 0 )
146
411
    {
147
411
      if( libfvde_volume_group_get_logical_volume_by_index(
148
411
           volume_group,
149
411
           0,
150
411
           &logical_volume,
151
411
           NULL ) != 1 )
152
237
      {
153
237
        goto on_error_libfvde_volume_group;
154
237
      }
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
7.73k
           read_iterator < 128;
202
7.55k
           read_iterator++ )
203
7.67k
      {
204
7.67k
        if( volume_offset >= volume_size )
205
32
        {
206
32
          break;
207
32
        }
208
7.64k
        if( libfvde_logical_volume_read_buffer_at_offset(
209
7.64k
             logical_volume,
210
7.64k
             buffer,
211
7.64k
             497,
212
7.64k
             volume_offset,
213
7.64k
             NULL ) == -1 )
214
87
        {
215
87
          goto on_error_libfvde_logical_volume;
216
87
        }
217
7.55k
        volume_offset += 497;
218
7.55k
      }
219
174
on_error_libfvde_logical_volume:
220
174
      libfvde_logical_volume_free(
221
174
       &logical_volume,
222
174
       NULL );
223
174
    }
224
580
on_error_libfvde_volume_group:
225
580
    libfvde_volume_group_free(
226
580
     &volume_group,
227
580
     NULL );
228
580
  }
229
580
  libfvde_volume_close(
230
580
   volume,
231
580
   NULL );
232
233
3.90k
on_error_libfvde_volume:
234
3.90k
  libfvde_volume_free(
235
3.90k
   &volume,
236
3.90k
   NULL );
237
238
3.90k
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.90k
  if( file_io_pool != NULL )
244
3.90k
  {
245
3.90k
    libbfio_pool_free(
246
3.90k
     &file_io_pool,
247
3.90k
     NULL );
248
3.90k
  }
249
3.90k
  if( file_io_handle != NULL )
250
1.86k
  {
251
1.86k
    libbfio_handle_free(
252
1.86k
     &file_io_handle,
253
     NULL );
254
1.86k
  }
255
3.90k
  return( 0 );
256
3.90k
}
257
258
} /* extern "C" */
259