Coverage Report

Created: 2024-02-25 07:19

/src/libfvde/ossfuzz/volume_fuzzer.cc
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * OSS-Fuzz target for libfvde volume type
3
 *
4
 * Copyright (C) 2011-2024, 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
1.37k
{
60
1.37k
  libbfio_handle_t *file_io_handle = NULL;
61
1.37k
  libbfio_pool_t *file_io_pool     = NULL;
62
1.37k
  libfvde_volume_t *volume        = NULL;
63
1.37k
  int entry_index                  = 0;
64
65
1.37k
  if( libbfio_memory_range_initialize(
66
1.37k
       &file_io_handle,
67
1.37k
       NULL ) != 1 )
68
0
  {
69
0
    return( 0 );
70
0
  }
71
1.37k
  if( libbfio_memory_range_set(
72
1.37k
       file_io_handle,
73
1.37k
       (uint8_t *) data,
74
1.37k
       size,
75
1.37k
       NULL ) != 1 )
76
0
  {
77
0
    goto on_error_libbfio;
78
0
  }
79
1.37k
  if( libbfio_pool_initialize(
80
1.37k
       &file_io_pool,
81
1.37k
       0,
82
1.37k
       0,
83
1.37k
       NULL ) != 1 )
84
0
  {
85
0
    goto on_error_libbfio;
86
0
  }
87
1.37k
  if( libfvde_volume_initialize(
88
1.37k
       &volume,
89
1.37k
       NULL ) != 1 )
90
0
  {
91
0
    goto on_error_libbfio;
92
0
  }
93
1.37k
  if( libfvde_volume_open_file_io_handle(
94
1.37k
       volume,
95
1.37k
       file_io_handle,
96
1.37k
       LIBFVDE_OPEN_READ,
97
1.37k
       NULL ) != 1 )
98
1.37k
  {
99
1.37k
    goto on_error_libfvde;
100
1.37k
  }
101
0
  if( libbfio_pool_append_handle(
102
0
       file_io_pool,
103
0
       &entry_index,
104
0
       file_io_handle,
105
0
       LIBBFIO_OPEN_READ,
106
0
       NULL ) != 1 )
107
0
  {
108
0
    goto on_error_libfvde;
109
0
  }
110
  /* The file IO pool takes over management of the file IO handle
111
   */
112
0
  file_io_handle = NULL;
113
114
0
  if( libfvde_volume_open_physical_volume_files_file_io_pool(
115
0
       volume,
116
0
       file_io_pool,
117
0
       NULL ) != 1 )
118
0
  {
119
0
    goto on_error_libfvde;
120
0
  }
121
0
  libfvde_volume_close(
122
0
   volume,
123
0
   NULL );
124
125
1.37k
on_error_libfvde:
126
1.37k
  libfvde_volume_free(
127
1.37k
   &volume,
128
1.37k
   NULL );
129
130
1.37k
on_error_libbfio:
131
  /* Note that on error the volume still has a reference to file_io_pool
132
   * that will be closed. Therefore the file IO pool and handle need to
133
   * be freed after closing or freeing the volume.
134
   */
135
1.37k
  if( file_io_pool != NULL )
136
1.37k
  {
137
1.37k
    libbfio_pool_free(
138
1.37k
     &file_io_pool,
139
1.37k
     NULL );
140
1.37k
  }
141
1.37k
  if( file_io_handle != NULL )
142
1.37k
  {
143
1.37k
    libbfio_handle_free(
144
1.37k
     &file_io_handle,
145
1.37k
     NULL );
146
1.37k
  }
147
1.37k
  return( 0 );
148
1.37k
}
149
150
} /* extern "C" */
151