Coverage Report

Created: 2026-03-05 07:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfsapfs/ossfuzz/file_entry_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libfsapfs file_entry type
3
 *
4
 * Copyright (C) 2018-2025, 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_libfsapfs.h"
31
32
#if !defined( LIBFSAPFS_HAVE_BFIO )
33
34
/* Opens a container using a Basic File IO (bfio) handle
35
 * Returns 1 if successful or -1 on error
36
 */
37
LIBFSAPFS_EXTERN \
38
int libfsapfs_container_open_file_io_handle(
39
     libfsapfs_container_t *container,
40
     libbfio_handle_t *file_io_handle,
41
     int access_flags,
42
     libfsapfs_error_t **error );
43
44
#endif /* !defined( LIBFSAPFS_HAVE_BFIO ) */
45
46
int LLVMFuzzerTestOneInput(
47
     const uint8_t *data,
48
     size_t size )
49
4.36k
{
50
4.36k
  uint8_t string_value[ 64 ];
51
52
4.36k
  libbfio_handle_t *file_io_handle       = NULL;
53
4.36k
  libfsapfs_container_t *container       = NULL;
54
4.36k
  libfsapfs_file_entry_t *root_directory = NULL;
55
4.36k
  libfsapfs_file_entry_t *sub_file_entry = NULL;
56
4.36k
  libfsapfs_volume_t *volume             = NULL;
57
4.36k
  size64_t file_size                     = 0;
58
4.36k
  size_t string_size                     = 0;
59
4.36k
  off64_t file_offset                    = 0;
60
4.36k
  uint64_t value_64bit                   = 0;
61
4.36k
  int64_t posix_time                     = 0;
62
4.36k
  uint32_t major_device_number           = 0;
63
4.36k
  uint32_t minor_device_number           = 0;
64
4.36k
  uint32_t value_32bit                   = 0;
65
4.36k
  uint16_t value_16bit                   = 0;
66
4.36k
  int number_of_extents                  = 0;
67
4.36k
  int number_of_sub_file_entries         = 0;
68
4.36k
  int number_of_volumes                  = 0;
69
4.36k
  int result                             = 0;
70
71
4.36k
  if( libbfio_memory_range_initialize(
72
4.36k
       &file_io_handle,
73
4.36k
       NULL ) != 1 )
74
0
  {
75
0
    return( 0 );
76
0
  }
77
4.36k
  if( libbfio_memory_range_set(
78
4.36k
       file_io_handle,
79
4.36k
       (uint8_t *) data,
80
4.36k
       size,
81
4.36k
       NULL ) != 1 )
82
0
  {
83
0
    goto on_error_libbfio;
84
0
  }
85
4.36k
  if( libfsapfs_container_initialize(
86
4.36k
       &container,
87
4.36k
       NULL ) != 1 )
88
0
  {
89
0
    goto on_error_libbfio;
90
0
  }
91
4.36k
  if( libfsapfs_container_open_file_io_handle(
92
4.36k
       container,
93
4.36k
       file_io_handle,
94
4.36k
       LIBFSAPFS_OPEN_READ,
95
4.36k
       NULL ) != 1 )
96
1.31k
  {
97
1.31k
    goto on_error_libfsapfs_container;
98
1.31k
  }
99
3.04k
  if( libfsapfs_container_get_number_of_volumes(
100
3.04k
       container,
101
3.04k
       &number_of_volumes,
102
3.04k
       NULL ) != 1 )
103
0
  {
104
0
    goto on_error_libfsapfs_container;
105
0
  }
106
3.04k
  if( number_of_volumes > 0 )
107
3.04k
  {
108
3.04k
    if( libfsapfs_container_get_volume_by_index(
109
3.04k
         container,
110
3.04k
         0,
111
3.04k
         &volume,
112
3.04k
         NULL ) != 1 )
113
1.58k
    {
114
1.58k
      goto on_error_libfsapfs_container;
115
1.58k
    }
116
1.46k
    if( libfsapfs_volume_get_root_directory(
117
1.46k
         volume,
118
1.46k
         &root_directory,
119
1.46k
         NULL ) == 1 )
120
829
    {
121
829
      if( libfsapfs_file_entry_get_number_of_sub_file_entries(
122
829
           root_directory,
123
829
           &number_of_sub_file_entries,
124
829
           NULL ) != 1 )
125
87
      {
126
87
        goto on_error_libfsapfs_root_directory;
127
87
      }
128
742
      if( number_of_sub_file_entries > 0 )
129
659
      {
130
659
        result = libfsapfs_file_entry_get_sub_file_entry_by_index(
131
659
                  root_directory,
132
659
                  0,
133
659
                  &sub_file_entry,
134
659
                  NULL );
135
136
659
        if( result != -1 )
137
616
        {
138
616
          libfsapfs_file_entry_get_identifier(
139
616
           sub_file_entry,
140
616
           &value_64bit,
141
616
           NULL );
142
143
616
          libfsapfs_file_entry_get_parent_identifier(
144
616
           sub_file_entry,
145
616
           &value_64bit,
146
616
           NULL );
147
148
616
          libfsapfs_file_entry_get_creation_time(
149
616
           sub_file_entry,
150
616
           &posix_time,
151
616
           NULL );
152
153
616
          libfsapfs_file_entry_get_modification_time(
154
616
           sub_file_entry,
155
616
           &posix_time,
156
616
           NULL );
157
158
616
          libfsapfs_file_entry_get_access_time(
159
616
           sub_file_entry,
160
616
           &posix_time,
161
616
           NULL );
162
163
616
          libfsapfs_file_entry_get_inode_change_time(
164
616
           sub_file_entry,
165
616
           &posix_time,
166
616
           NULL );
167
168
616
          libfsapfs_file_entry_get_added_time(
169
616
           sub_file_entry,
170
616
           &posix_time,
171
616
           NULL );
172
173
616
          libfsapfs_file_entry_get_file_mode(
174
616
           sub_file_entry,
175
616
           &value_16bit,
176
616
           NULL );
177
178
616
          libfsapfs_file_entry_get_number_of_links(
179
616
           sub_file_entry,
180
616
           &value_32bit,
181
616
           NULL );
182
183
616
          libfsapfs_file_entry_get_owner_identifier(
184
616
           sub_file_entry,
185
616
           &value_32bit,
186
616
           NULL );
187
188
616
          libfsapfs_file_entry_get_group_identifier(
189
616
           sub_file_entry,
190
616
           &value_32bit,
191
616
           NULL );
192
193
616
          libfsapfs_file_entry_get_device_identifier(
194
616
           sub_file_entry,
195
616
           &value_32bit,
196
616
           NULL );
197
198
616
          libfsapfs_file_entry_get_device_number(
199
616
           sub_file_entry,
200
616
           &major_device_number,
201
616
           &minor_device_number,
202
616
           NULL );
203
204
616
          libfsapfs_file_entry_get_utf8_name_size(
205
616
           sub_file_entry,
206
616
           &string_size,
207
616
           NULL );
208
209
616
          libfsapfs_file_entry_get_utf8_name(
210
616
           sub_file_entry,
211
616
           string_value,
212
616
           64,
213
616
           NULL );
214
215
616
          libfsapfs_file_entry_get_utf8_symbolic_link_target_size(
216
616
           sub_file_entry,
217
616
           &string_size,
218
616
           NULL );
219
220
616
          libfsapfs_file_entry_get_utf8_symbolic_link_target(
221
616
           sub_file_entry,
222
616
           string_value,
223
616
           64,
224
616
           NULL );
225
226
616
          libfsapfs_file_entry_get_offset(
227
616
           sub_file_entry,
228
616
           &file_offset,
229
616
           NULL );
230
231
616
          libfsapfs_file_entry_get_size(
232
616
           sub_file_entry,
233
616
           &file_size,
234
616
           NULL );
235
236
616
          libfsapfs_file_entry_get_number_of_extents(
237
616
           sub_file_entry,
238
616
           &number_of_extents,
239
616
           NULL );
240
616
        }
241
659
        libfsapfs_file_entry_free(
242
659
         &sub_file_entry,
243
659
         NULL );
244
659
      }
245
829
on_error_libfsapfs_root_directory:
246
829
      libfsapfs_file_entry_free(
247
829
       &root_directory,
248
829
       NULL );
249
829
    }
250
1.46k
    libfsapfs_volume_free(
251
1.46k
     &volume,
252
1.46k
     NULL );
253
1.46k
  }
254
4.36k
on_error_libfsapfs_container:
255
4.36k
  libfsapfs_container_free(
256
4.36k
   &container,
257
4.36k
   NULL );
258
259
4.36k
on_error_libbfio:
260
4.36k
  libbfio_handle_free(
261
4.36k
   &file_io_handle,
262
4.36k
   NULL );
263
264
4.36k
  return( 0 );
265
4.36k
}
266
267
} /* extern "C" */
268