Coverage Report

Created: 2026-07-10 07:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libbde/ossfuzz/volume_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libbde volume type
3
 *
4
 * Copyright (C) 2011-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_libbde.h"
31
32
#if !defined( LIBBDE_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
LIBBDE_EXTERN \
38
int libbde_volume_open_file_io_handle(
39
     libbde_volume_t *volume,
40
     libbfio_handle_t *file_io_handle,
41
     int access_flags,
42
     libbde_error_t **error );
43
44
#endif /* !defined( LIBBDE_HAVE_BFIO ) */
45
46
int LLVMFuzzerTestOneInput(
47
     const uint8_t *data,
48
     size_t size )
49
2.77k
{
50
2.77k
  uint8_t buffer[ 512 ];
51
2.77k
  uint8_t utf8_string[ 64 ];
52
2.77k
  uint16_t utf16_string[ 64 ];
53
2.77k
  uint8_t guid[ 16 ];
54
55
2.77k
  libbfio_handle_t *file_io_handle = NULL;
56
2.77k
  libbde_volume_t *volume          = NULL;
57
2.77k
  off64_t volume_offset            = 0;
58
2.77k
  size64_t volume_size             = 0;
59
2.77k
  uint64_t value_64bit             = 0;
60
2.77k
  uint16_t value_16bit             = 0;
61
2.77k
  size_t string_size               = 0;
62
2.77k
  int is_locked                    = 0;
63
2.77k
  int number_of_key_protectors     = 0;
64
2.77k
  int read_iterator                = 0;
65
66
2.77k
  if( libbfio_memory_range_initialize(
67
2.77k
       &file_io_handle,
68
2.77k
       NULL ) != 1 )
69
0
  {
70
0
    return( 0 );
71
0
  }
72
2.77k
  if( libbfio_memory_range_set(
73
2.77k
       file_io_handle,
74
2.77k
       (uint8_t *) data,
75
2.77k
       size,
76
2.77k
       NULL ) != 1 )
77
0
  {
78
0
    goto on_error_libbfio;
79
0
  }
80
2.77k
  if( libbde_volume_initialize(
81
2.77k
       &volume,
82
2.77k
       NULL ) != 1 )
83
0
  {
84
0
    goto on_error_libbfio;
85
0
  }
86
2.77k
  if( libbde_volume_set_utf8_password(
87
2.77k
       volume,
88
2.77k
       (uint8_t *) "bde-TEST",
89
2.77k
       8,
90
2.77k
       NULL ) != 1 )
91
0
  {
92
0
    goto on_error_libbfio;
93
0
  }
94
2.77k
  if( libbde_volume_open_file_io_handle(
95
2.77k
       volume,
96
2.77k
       file_io_handle,
97
2.77k
       LIBBDE_OPEN_READ,
98
2.77k
       NULL ) != 1 )
99
2.23k
  {
100
2.23k
    goto on_error_libbde;
101
2.23k
  }
102
545
  if( libbde_volume_get_encryption_method(
103
545
       volume,
104
545
       &value_16bit,
105
545
       NULL ) != 1 )
106
0
  {
107
0
    goto on_error_libbde;
108
0
  }
109
545
  if( libbde_volume_get_volume_identifier(
110
545
       volume,
111
545
       guid,
112
545
       16,
113
545
       NULL ) == -1 )
114
0
  {
115
0
    goto on_error_libbde;
116
0
  }
117
545
  if( libbde_volume_get_creation_time(
118
545
       volume,
119
545
       &value_64bit,
120
545
       NULL ) != 1 )
121
0
  {
122
0
    goto on_error_libbde;
123
0
  }
124
545
  if( libbde_volume_get_utf8_description_size(
125
545
       volume,
126
545
       &string_size,
127
545
       NULL ) != 1 )
128
278
  {
129
278
    goto on_error_libbde;
130
278
  }
131
267
  if( libbde_volume_get_utf8_description(
132
267
       volume,
133
267
       utf8_string,
134
267
       64,
135
267
       NULL ) != 1 )
136
13
  {
137
13
    goto on_error_libbde;
138
13
  }
139
254
  if( libbde_volume_get_utf8_description_size(
140
254
       volume,
141
254
       &string_size,
142
254
       NULL ) != 1 )
143
0
  {
144
0
    goto on_error_libbde;
145
0
  }
146
254
  if( libbde_volume_get_utf16_description(
147
254
       volume,
148
254
       utf16_string,
149
254
       64,
150
254
       NULL ) != 1 )
151
0
  {
152
0
    goto on_error_libbde;
153
0
  }
154
254
  if( libbde_volume_get_number_of_key_protectors(
155
254
       volume,
156
254
       &number_of_key_protectors,
157
254
       NULL ) != 1 )
158
0
  {
159
0
    goto on_error_libbde;
160
0
  }
161
254
  is_locked = libbde_volume_is_locked(
162
254
               volume,
163
254
         NULL );
164
165
254
  if( is_locked == -1 )
166
0
  {
167
0
    goto on_error_libbde;
168
0
  }
169
254
  else if( is_locked == 0 )
170
252
  {
171
252
    if( libbde_volume_get_size(
172
252
         volume,
173
252
         &volume_size,
174
252
         NULL ) != 1 )
175
0
    {
176
0
      goto on_error_libbde;
177
0
    }
178
252
    for( read_iterator = 0;
179
26.4k
         read_iterator < 128;
180
26.1k
         read_iterator++ )
181
26.2k
    {
182
26.2k
      if( volume_offset >= volume_size )
183
63
      {
184
63
        break;
185
63
      }
186
26.1k
      if( libbde_volume_read_buffer_at_offset(
187
26.1k
           volume,
188
26.1k
           buffer,
189
26.1k
           497,
190
26.1k
           volume_offset,
191
26.1k
           NULL ) == -1 )
192
0
      {
193
0
        goto on_error_libbde;
194
0
      }
195
26.1k
      volume_offset += 497;
196
26.1k
    }
197
252
  }
198
254
  libbde_volume_close(
199
254
   volume,
200
254
   NULL );
201
202
2.77k
on_error_libbde:
203
2.77k
  libbde_volume_free(
204
2.77k
   &volume,
205
2.77k
   NULL );
206
207
2.77k
on_error_libbfio:
208
2.77k
  libbfio_handle_free(
209
2.77k
   &file_io_handle,
210
2.77k
   NULL );
211
212
2.77k
  return( 0 );
213
2.77k
}
214
215
} /* extern "C" */
216