Coverage Report

Created: 2026-12-01 07:31

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