Coverage Report

Created: 2026-07-10 07:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvslvm/ossfuzz/logical_volume_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libvslvm 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_libvslvm.h"
31
32
#if !defined( LIBVSLVM_HAVE_BFIO )
33
34
/* Opens a handle using a Basic File IO (bfio) handle
35
 * Returns 1 if successful or -1 on error
36
 */
37
LIBVSLVM_EXTERN \
38
int libvslvm_handle_open_file_io_handle(
39
     libvslvm_handle_t *handle,
40
     libbfio_handle_t *file_io_handle,
41
     int access_flags,
42
     libvslvm_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
LIBVSLVM_EXTERN \
49
int libvslvm_handle_open_physical_volume_files_file_io_pool(
50
     libvslvm_handle_t *handle,
51
     libbfio_pool_t *file_io_pool,
52
     libvslvm_error_t **error );
53
54
#endif /* !defined( LIBVSLVM_HAVE_BFIO ) */
55
56
int LLVMFuzzerTestOneInput(
57
     const uint8_t *data,
58
     size_t size )
59
3.69k
{
60
3.69k
  uint8_t buffer[ 512 ];
61
3.69k
  char string[ 64 ];
62
63
3.69k
  libbfio_handle_t *file_io_handle          = NULL;
64
3.69k
  libbfio_pool_t *file_io_pool              = NULL;
65
3.69k
  libvslvm_handle_t *handle                 = NULL;
66
3.69k
  libvslvm_logical_volume_t *logical_volume = NULL;
67
3.69k
  libvslvm_volume_group_t *volume_group     = NULL;
68
3.69k
  off64_t volume_offset                     = 0;
69
3.69k
  size64_t volume_size                      = 0;
70
3.69k
  size_t string_size                        = 0;
71
3.69k
  int entry_index                           = 0;
72
3.69k
  int number_of_logical_volumes             = 0;
73
3.69k
  int number_of_segments                    = 0;
74
3.69k
  int read_iterator                         = 0;
75
76
3.69k
  if( libbfio_memory_range_initialize(
77
3.69k
       &file_io_handle,
78
3.69k
       NULL ) != 1 )
79
0
  {
80
0
    return( 0 );
81
0
  }
82
3.69k
  if( libbfio_memory_range_set(
83
3.69k
       file_io_handle,
84
3.69k
       (uint8_t *) data,
85
3.69k
       size,
86
3.69k
       NULL ) != 1 )
87
0
  {
88
0
    goto on_error_libbfio;
89
0
  }
90
3.69k
  if( libbfio_pool_initialize(
91
3.69k
       &file_io_pool,
92
3.69k
       0,
93
3.69k
       0,
94
3.69k
       NULL ) != 1 )
95
0
  {
96
0
    goto on_error_libbfio;
97
0
  }
98
3.69k
  if( libvslvm_handle_initialize(
99
3.69k
       &handle,
100
3.69k
       NULL ) != 1 )
101
0
  {
102
0
    goto on_error_libbfio;
103
0
  }
104
3.69k
  if( libvslvm_handle_open_file_io_handle(
105
3.69k
       handle,
106
3.69k
       file_io_handle,
107
3.69k
       LIBVSLVM_OPEN_READ,
108
3.69k
       NULL ) != 1 )
109
1.61k
  {
110
1.61k
    goto on_error_libvslvm_handle;
111
1.61k
  }
112
2.07k
  if( libbfio_pool_append_handle(
113
2.07k
       file_io_pool,
114
2.07k
       &entry_index,
115
2.07k
       file_io_handle,
116
2.07k
       LIBBFIO_OPEN_READ,
117
2.07k
       NULL ) != 1 )
118
0
  {
119
0
    goto on_error_libvslvm_handle;
120
0
  }
121
  /* The file IO pool takes over management of the file IO handle
122
   */
123
2.07k
  file_io_handle = NULL;
124
125
2.07k
  if( libvslvm_handle_open_physical_volume_files_file_io_pool(
126
2.07k
       handle,
127
2.07k
       file_io_pool,
128
2.07k
       NULL ) != 1 )
129
1.48k
  {
130
1.48k
    goto on_error_libvslvm_handle;
131
1.48k
  }
132
588
  if( libvslvm_handle_get_volume_group(
133
588
       handle,
134
588
       &volume_group,
135
588
       NULL ) == 1 )
136
588
  {
137
588
    if( libvslvm_volume_group_get_number_of_logical_volumes(
138
588
         volume_group,
139
588
         &number_of_logical_volumes,
140
588
         NULL ) != 1 )
141
0
    {
142
0
      goto on_error_libvslvm_volume_group;
143
0
    }
144
588
    if( number_of_logical_volumes > 0 )
145
410
    {
146
410
      if( libvslvm_volume_group_get_logical_volume(
147
410
           volume_group,
148
410
           0,
149
410
           &logical_volume,
150
410
           NULL ) != 1 )
151
242
      {
152
242
        goto on_error_libvslvm_volume_group;
153
242
      }
154
168
      if( libvslvm_logical_volume_get_name_size(
155
168
           logical_volume,
156
168
           &string_size,
157
168
           NULL ) != 1 )
158
0
      {
159
0
        goto on_error_libvslvm_logical_volume;
160
0
      }
161
168
      if( libvslvm_logical_volume_get_name(
162
168
           logical_volume,
163
168
           string,
164
168
           64,
165
168
           NULL ) != 1 )
166
1
      {
167
1
        goto on_error_libvslvm_logical_volume;
168
1
      }
169
167
      if( libvslvm_logical_volume_get_identifier_size(
170
167
           logical_volume,
171
167
           &string_size,
172
167
           NULL ) != 1 )
173
0
      {
174
0
        goto on_error_libvslvm_logical_volume;
175
0
      }
176
167
      if( libvslvm_logical_volume_get_identifier(
177
167
           logical_volume,
178
167
           string,
179
167
           64,
180
167
           NULL ) != 1 )
181
0
      {
182
0
        goto on_error_libvslvm_logical_volume;
183
0
      }
184
167
      if( libvslvm_logical_volume_get_number_of_segments(
185
167
           logical_volume,
186
167
           &number_of_segments,
187
167
           NULL ) != 1 )
188
0
      {
189
0
        goto on_error_libvslvm_logical_volume;
190
0
      }
191
167
      if( libvslvm_logical_volume_get_size(
192
167
           logical_volume,
193
167
           &volume_size,
194
167
           NULL ) != 1 )
195
0
      {
196
0
        goto on_error_libvslvm_logical_volume;
197
0
      }
198
167
      for( read_iterator = 0;
199
6.73k
           read_iterator < 128;
200
6.56k
           read_iterator++ )
201
6.68k
      {
202
6.68k
        if( volume_offset >= volume_size )
203
33
        {
204
33
          break;
205
33
        }
206
6.65k
        if( libvslvm_logical_volume_read_buffer_at_offset(
207
6.65k
             logical_volume,
208
6.65k
             buffer,
209
6.65k
             497,
210
6.65k
             volume_offset,
211
6.65k
             NULL ) == -1 )
212
87
        {
213
87
          goto on_error_libvslvm_logical_volume;
214
87
        }
215
6.56k
        volume_offset += 497;
216
6.56k
      }
217
168
on_error_libvslvm_logical_volume:
218
168
      libvslvm_logical_volume_free(
219
168
       &logical_volume,
220
168
       NULL );
221
168
    }
222
588
on_error_libvslvm_volume_group:
223
588
    libvslvm_volume_group_free(
224
588
     &volume_group,
225
588
     NULL );
226
588
  }
227
588
  libvslvm_handle_close(
228
588
   handle,
229
588
   NULL );
230
231
3.69k
on_error_libvslvm_handle:
232
3.69k
  libvslvm_handle_free(
233
3.69k
   &handle,
234
3.69k
   NULL );
235
236
3.69k
on_error_libbfio:
237
  /* Note that on error the handle still has a reference to file_io_pool
238
   * that will be closed. Therefore the file IO pool and handle need to
239
   * be freed after closing or freeing the handle.
240
   */
241
3.69k
  if( file_io_pool != NULL )
242
3.69k
  {
243
3.69k
    libbfio_pool_free(
244
3.69k
     &file_io_pool,
245
3.69k
     NULL );
246
3.69k
  }
247
3.69k
  if( file_io_handle != NULL )
248
1.61k
  {
249
1.61k
    libbfio_handle_free(
250
1.61k
     &file_io_handle,
251
     NULL );
252
1.61k
  }
253
3.69k
  return( 0 );
254
3.69k
}
255
256
} /* extern "C" */
257