Coverage Report

Created: 2026-07-25 07:30

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.71k
{
50
2.71k
  uint8_t buffer[ 512 ];
51
2.71k
  uint8_t utf8_string[ 64 ];
52
2.71k
  uint16_t utf16_string[ 64 ];
53
2.71k
  uint8_t guid[ 16 ];
54
55
2.71k
  libbfio_handle_t *file_io_handle = NULL;
56
2.71k
  libbde_volume_t *volume          = NULL;
57
2.71k
  off64_t volume_offset            = 0;
58
2.71k
  size64_t volume_size             = 0;
59
2.71k
  uint64_t value_64bit             = 0;
60
2.71k
  uint16_t value_16bit             = 0;
61
2.71k
  size_t string_size               = 0;
62
2.71k
  int is_locked                    = 0;
63
2.71k
  int number_of_key_protectors     = 0;
64
2.71k
  int read_iterator                = 0;
65
66
2.71k
  if( libbfio_memory_range_initialize(
67
2.71k
       &file_io_handle,
68
2.71k
       NULL ) != 1 )
69
0
  {
70
0
    return( 0 );
71
0
  }
72
2.71k
  if( libbfio_memory_range_set(
73
2.71k
       file_io_handle,
74
2.71k
       (uint8_t *) data,
75
2.71k
       size,
76
2.71k
       NULL ) != 1 )
77
0
  {
78
0
    goto on_error_libbfio;
79
0
  }
80
2.71k
  if( libbde_volume_initialize(
81
2.71k
       &volume,
82
2.71k
       NULL ) != 1 )
83
0
  {
84
0
    goto on_error_libbfio;
85
0
  }
86
2.71k
  if( libbde_volume_set_utf8_password(
87
2.71k
       volume,
88
2.71k
       (uint8_t *) "bde-TEST",
89
2.71k
       8,
90
2.71k
       NULL ) != 1 )
91
0
  {
92
0
    goto on_error_libbfio;
93
0
  }
94
2.71k
  if( libbde_volume_open_file_io_handle(
95
2.71k
       volume,
96
2.71k
       file_io_handle,
97
2.71k
       LIBBDE_OPEN_READ,
98
2.71k
       NULL ) != 1 )
99
2.18k
  {
100
2.18k
    goto on_error_libbde;
101
2.18k
  }
102
529
  if( libbde_volume_get_encryption_method(
103
529
       volume,
104
529
       &value_16bit,
105
529
       NULL ) != 1 )
106
0
  {
107
0
    goto on_error_libbde;
108
0
  }
109
529
  if( libbde_volume_get_volume_identifier(
110
529
       volume,
111
529
       guid,
112
529
       16,
113
529
       NULL ) == -1 )
114
0
  {
115
0
    goto on_error_libbde;
116
0
  }
117
529
  if( libbde_volume_get_creation_time(
118
529
       volume,
119
529
       &value_64bit,
120
529
       NULL ) != 1 )
121
0
  {
122
0
    goto on_error_libbde;
123
0
  }
124
529
  if( libbde_volume_get_utf8_description_size(
125
529
       volume,
126
529
       &string_size,
127
529
       NULL ) != 1 )
128
261
  {
129
261
    goto on_error_libbde;
130
261
  }
131
268
  if( libbde_volume_get_utf8_description(
132
268
       volume,
133
268
       utf8_string,
134
268
       64,
135
268
       NULL ) != 1 )
136
15
  {
137
15
    goto on_error_libbde;
138
15
  }
139
253
  if( libbde_volume_get_utf8_description_size(
140
253
       volume,
141
253
       &string_size,
142
253
       NULL ) != 1 )
143
0
  {
144
0
    goto on_error_libbde;
145
0
  }
146
253
  if( libbde_volume_get_utf16_description(
147
253
       volume,
148
253
       utf16_string,
149
253
       64,
150
253
       NULL ) != 1 )
151
0
  {
152
0
    goto on_error_libbde;
153
0
  }
154
253
  if( libbde_volume_get_number_of_key_protectors(
155
253
       volume,
156
253
       &number_of_key_protectors,
157
253
       NULL ) != 1 )
158
0
  {
159
0
    goto on_error_libbde;
160
0
  }
161
253
  is_locked = libbde_volume_is_locked(
162
253
               volume,
163
253
         NULL );
164
165
253
  if( is_locked == -1 )
166
0
  {
167
0
    goto on_error_libbde;
168
0
  }
169
253
  else if( is_locked == 0 )
170
251
  {
171
251
    if( libbde_volume_get_size(
172
251
         volume,
173
251
         &volume_size,
174
251
         NULL ) != 1 )
175
0
    {
176
0
      goto on_error_libbde;
177
0
    }
178
251
    for( read_iterator = 0;
179
25.7k
         read_iterator < 128;
180
25.4k
         read_iterator++ )
181
25.5k
    {
182
25.5k
      if( volume_offset >= volume_size )
183
64
      {
184
64
        break;
185
64
      }
186
25.4k
      if( libbde_volume_read_buffer_at_offset(
187
25.4k
           volume,
188
25.4k
           buffer,
189
25.4k
           497,
190
25.4k
           volume_offset,
191
25.4k
           NULL ) == -1 )
192
2
      {
193
2
        goto on_error_libbde;
194
2
      }
195
25.4k
      volume_offset += 497;
196
25.4k
    }
197
251
  }
198
251
  libbde_volume_close(
199
251
   volume,
200
251
   NULL );
201
202
2.71k
on_error_libbde:
203
2.71k
  libbde_volume_free(
204
2.71k
   &volume,
205
2.71k
   NULL );
206
207
2.71k
on_error_libbfio:
208
2.71k
  libbfio_handle_free(
209
2.71k
   &file_io_handle,
210
2.71k
   NULL );
211
212
2.71k
  return( 0 );
213
2.71k
}
214
215
} /* extern "C" */
216