Coverage Report

Created: 2026-10-22 07:28

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