Coverage Report

Created: 2026-10-22 07:28

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.85k
{
60
3.85k
  uint8_t buffer[ 512 ];
61
3.85k
  char string[ 64 ];
62
63
3.85k
  libbfio_handle_t *file_io_handle          = NULL;
64
3.85k
  libbfio_pool_t *file_io_pool              = NULL;
65
3.85k
  libvslvm_handle_t *handle                 = NULL;
66
3.85k
  libvslvm_logical_volume_t *logical_volume = NULL;
67
3.85k
  libvslvm_volume_group_t *volume_group     = NULL;
68
3.85k
  off64_t volume_offset                     = 0;
69
3.85k
  size64_t volume_size                      = 0;
70
3.85k
  size_t string_size                        = 0;
71
3.85k
  int entry_index                           = 0;
72
3.85k
  int number_of_logical_volumes             = 0;
73
3.85k
  int number_of_segments                    = 0;
74
3.85k
  int read_iterator                         = 0;
75
76
3.85k
  if( libbfio_memory_range_initialize(
77
3.85k
       &file_io_handle,
78
3.85k
       NULL ) != 1 )
79
0
  {
80
0
    return( 0 );
81
0
  }
82
3.85k
  if( libbfio_memory_range_set(
83
3.85k
       file_io_handle,
84
3.85k
       (uint8_t *) data,
85
3.85k
       size,
86
3.85k
       NULL ) != 1 )
87
0
  {
88
0
    goto on_error_libbfio;
89
0
  }
90
3.85k
  if( libbfio_pool_initialize(
91
3.85k
       &file_io_pool,
92
3.85k
       0,
93
3.85k
       0,
94
3.85k
       NULL ) != 1 )
95
0
  {
96
0
    goto on_error_libbfio;
97
0
  }
98
3.85k
  if( libvslvm_handle_initialize(
99
3.85k
       &handle,
100
3.85k
       NULL ) != 1 )
101
0
  {
102
0
    goto on_error_libbfio;
103
0
  }
104
3.85k
  if( libvslvm_handle_open_file_io_handle(
105
3.85k
       handle,
106
3.85k
       file_io_handle,
107
3.85k
       LIBVSLVM_OPEN_READ,
108
3.85k
       NULL ) != 1 )
109
1.85k
  {
110
1.85k
    goto on_error_libvslvm_handle;
111
1.85k
  }
112
1.99k
  if( libbfio_pool_append_handle(
113
1.99k
       file_io_pool,
114
1.99k
       &entry_index,
115
1.99k
       file_io_handle,
116
1.99k
       LIBBFIO_OPEN_READ,
117
1.99k
       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
1.99k
  file_io_handle = NULL;
124
125
1.99k
  if( libvslvm_handle_open_physical_volume_files_file_io_pool(
126
1.99k
       handle,
127
1.99k
       file_io_pool,
128
1.99k
       NULL ) != 1 )
129
1.42k
  {
130
1.42k
    goto on_error_libvslvm_handle;
131
1.42k
  }
132
567
  if( libvslvm_handle_get_volume_group(
133
567
       handle,
134
567
       &volume_group,
135
567
       NULL ) == 1 )
136
567
  {
137
567
    if( libvslvm_volume_group_get_number_of_logical_volumes(
138
567
         volume_group,
139
567
         &number_of_logical_volumes,
140
567
         NULL ) != 1 )
141
0
    {
142
0
      goto on_error_libvslvm_volume_group;
143
0
    }
144
567
    if( number_of_logical_volumes > 0 )
145
407
    {
146
407
      if( libvslvm_volume_group_get_logical_volume(
147
407
           volume_group,
148
407
           0,
149
407
           &logical_volume,
150
407
           NULL ) != 1 )
151
233
      {
152
233
        goto on_error_libvslvm_volume_group;
153
233
      }
154
174
      if( libvslvm_logical_volume_get_name_size(
155
174
           logical_volume,
156
174
           &string_size,
157
174
           NULL ) != 1 )
158
0
      {
159
0
        goto on_error_libvslvm_logical_volume;
160
0
      }
161
174
      if( libvslvm_logical_volume_get_name(
162
174
           logical_volume,
163
174
           string,
164
174
           64,
165
174
           NULL ) != 1 )
166
1
      {
167
1
        goto on_error_libvslvm_logical_volume;
168
1
      }
169
173
      if( libvslvm_logical_volume_get_identifier_size(
170
173
           logical_volume,
171
173
           &string_size,
172
173
           NULL ) != 1 )
173
0
      {
174
0
        goto on_error_libvslvm_logical_volume;
175
0
      }
176
173
      if( libvslvm_logical_volume_get_identifier(
177
173
           logical_volume,
178
173
           string,
179
173
           64,
180
173
           NULL ) != 1 )
181
0
      {
182
0
        goto on_error_libvslvm_logical_volume;
183
0
      }
184
173
      if( libvslvm_logical_volume_get_number_of_segments(
185
173
           logical_volume,
186
173
           &number_of_segments,
187
173
           NULL ) != 1 )
188
0
      {
189
0
        goto on_error_libvslvm_logical_volume;
190
0
      }
191
173
      if( libvslvm_logical_volume_get_size(
192
173
           logical_volume,
193
173
           &volume_size,
194
173
           NULL ) != 1 )
195
0
      {
196
0
        goto on_error_libvslvm_logical_volume;
197
0
      }
198
173
      for( read_iterator = 0;
199
8.09k
           read_iterator < 128;
200
7.92k
           read_iterator++ )
201
8.03k
      {
202
8.03k
        if( volume_offset >= volume_size )
203
28
        {
204
28
          break;
205
28
        }
206
8.00k
        if( libvslvm_logical_volume_read_buffer_at_offset(
207
8.00k
             logical_volume,
208
8.00k
             buffer,
209
8.00k
             497,
210
8.00k
             volume_offset,
211
8.00k
             NULL ) == -1 )
212
88
        {
213
88
          goto on_error_libvslvm_logical_volume;
214
88
        }
215
7.92k
        volume_offset += 497;
216
7.92k
      }
217
174
on_error_libvslvm_logical_volume:
218
174
      libvslvm_logical_volume_free(
219
174
       &logical_volume,
220
174
       NULL );
221
174
    }
222
567
on_error_libvslvm_volume_group:
223
567
    libvslvm_volume_group_free(
224
567
     &volume_group,
225
567
     NULL );
226
567
  }
227
567
  libvslvm_handle_close(
228
567
   handle,
229
567
   NULL );
230
231
3.85k
on_error_libvslvm_handle:
232
3.85k
  libvslvm_handle_free(
233
3.85k
   &handle,
234
3.85k
   NULL );
235
236
3.85k
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.85k
  if( file_io_pool != NULL )
242
3.85k
  {
243
3.85k
    libbfio_pool_free(
244
3.85k
     &file_io_pool,
245
3.85k
     NULL );
246
3.85k
  }
247
3.85k
  if( file_io_handle != NULL )
248
1.85k
  {
249
1.85k
    libbfio_handle_free(
250
1.85k
     &file_io_handle,
251
     NULL );
252
1.85k
  }
253
3.85k
  return( 0 );
254
3.85k
}
255
256
} /* extern "C" */
257