Coverage Report

Created: 2024-06-12 07:07

/src/libfsapfs/ossfuzz/file_entry_fuzzer.cc
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * OSS-Fuzz target for libfsapfs file_entry type
3
 *
4
 * Copyright (C) 2018-2024, 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.27k
{
50
4.27k
  uint8_t string_value[ 64 ];
51
52
4.27k
  libbfio_handle_t *file_io_handle       = NULL;
53
4.27k
  libfsapfs_container_t *container       = NULL;
54
4.27k
  libfsapfs_file_entry_t *root_directory = NULL;
55
4.27k
  libfsapfs_file_entry_t *sub_file_entry = NULL;
56
4.27k
  libfsapfs_volume_t *volume             = NULL;
57
4.27k
  size64_t file_size                     = 0;
58
4.27k
  size_t string_size                     = 0;
59
4.27k
  off64_t file_offset                    = 0;
60
4.27k
  uint64_t value_64bit                   = 0;
61
4.27k
  int64_t posix_time                     = 0;
62
4.27k
  uint32_t major_device_number           = 0;
63
4.27k
  uint32_t minor_device_number           = 0;
64
4.27k
  uint32_t value_32bit                   = 0;
65
4.27k
  uint16_t value_16bit                   = 0;
66
4.27k
  int number_of_extents                  = 0;
67
4.27k
  int number_of_sub_file_entries         = 0;
68
4.27k
  int number_of_volumes                  = 0;
69
4.27k
  int result                             = 0;
70
71
4.27k
  if( libbfio_memory_range_initialize(
72
4.27k
       &file_io_handle,
73
4.27k
       NULL ) != 1 )
74
0
  {
75
0
    return( 0 );
76
0
  }
77
4.27k
  if( libbfio_memory_range_set(
78
4.27k
       file_io_handle,
79
4.27k
       (uint8_t *) data,
80
4.27k
       size,
81
4.27k
       NULL ) != 1 )
82
0
  {
83
0
    goto on_error_libbfio;
84
0
  }
85
4.27k
  if( libfsapfs_container_initialize(
86
4.27k
       &container,
87
4.27k
       NULL ) != 1 )
88
0
  {
89
0
    goto on_error_libbfio;
90
0
  }
91
4.27k
  if( libfsapfs_container_open_file_io_handle(
92
4.27k
       container,
93
4.27k
       file_io_handle,
94
4.27k
       LIBFSAPFS_OPEN_READ,
95
4.27k
       NULL ) != 1 )
96
1.23k
  {
97
1.23k
    goto on_error_libfsapfs_container;
98
1.23k
  }
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.00k
  {
108
3.00k
    if( libfsapfs_container_get_volume_by_index(
109
3.00k
         container,
110
3.00k
         0,
111
3.00k
         &volume,
112
3.00k
         NULL ) != 1 )
113
1.71k
    {
114
1.71k
      goto on_error_libfsapfs_container;
115
1.71k
    }
116
1.29k
    if( libfsapfs_volume_get_root_directory(
117
1.29k
         volume,
118
1.29k
         &root_directory,
119
1.29k
         NULL ) == 1 )
120
785
    {
121
785
      if( libfsapfs_file_entry_get_number_of_sub_file_entries(
122
785
           root_directory,
123
785
           &number_of_sub_file_entries,
124
785
           NULL ) != 1 )
125
84
      {
126
84
        goto on_error_libfsapfs_root_directory;
127
84
      }
128
701
      if( number_of_sub_file_entries > 0 )
129
619
      {
130
619
        result = libfsapfs_file_entry_get_sub_file_entry_by_index(
131
619
                  root_directory,
132
619
                  0,
133
619
                  &sub_file_entry,
134
619
                  NULL );
135
136
619
        if( result != -1 )
137
569
        {
138
569
          libfsapfs_file_entry_get_identifier(
139
569
           sub_file_entry,
140
569
           &value_64bit,
141
569
           NULL );
142
143
569
          libfsapfs_file_entry_get_parent_identifier(
144
569
           sub_file_entry,
145
569
           &value_64bit,
146
569
           NULL );
147
148
569
          libfsapfs_file_entry_get_creation_time(
149
569
           sub_file_entry,
150
569
           &posix_time,
151
569
           NULL );
152
153
569
          libfsapfs_file_entry_get_modification_time(
154
569
           sub_file_entry,
155
569
           &posix_time,
156
569
           NULL );
157
158
569
          libfsapfs_file_entry_get_access_time(
159
569
           sub_file_entry,
160
569
           &posix_time,
161
569
           NULL );
162
163
569
          libfsapfs_file_entry_get_inode_change_time(
164
569
           sub_file_entry,
165
569
           &posix_time,
166
569
           NULL );
167
168
569
          libfsapfs_file_entry_get_added_time(
169
569
           sub_file_entry,
170
569
           &posix_time,
171
569
           NULL );
172
173
569
          libfsapfs_file_entry_get_file_mode(
174
569
           sub_file_entry,
175
569
           &value_16bit,
176
569
           NULL );
177
178
569
          libfsapfs_file_entry_get_number_of_links(
179
569
           sub_file_entry,
180
569
           &value_32bit,
181
569
           NULL );
182
183
569
          libfsapfs_file_entry_get_owner_identifier(
184
569
           sub_file_entry,
185
569
           &value_32bit,
186
569
           NULL );
187
188
569
          libfsapfs_file_entry_get_group_identifier(
189
569
           sub_file_entry,
190
569
           &value_32bit,
191
569
           NULL );
192
193
569
          libfsapfs_file_entry_get_device_identifier(
194
569
           sub_file_entry,
195
569
           &value_32bit,
196
569
           NULL );
197
198
569
          libfsapfs_file_entry_get_device_number(
199
569
           sub_file_entry,
200
569
           &major_device_number,
201
569
           &minor_device_number,
202
569
           NULL );
203
204
569
          libfsapfs_file_entry_get_utf8_name_size(
205
569
           sub_file_entry,
206
569
           &string_size,
207
569
           NULL );
208
209
569
          libfsapfs_file_entry_get_utf8_name(
210
569
           sub_file_entry,
211
569
           string_value,
212
569
           64,
213
569
           NULL );
214
215
569
          libfsapfs_file_entry_get_utf8_symbolic_link_target_size(
216
569
           sub_file_entry,
217
569
           &string_size,
218
569
           NULL );
219
220
569
          libfsapfs_file_entry_get_utf8_symbolic_link_target(
221
569
           sub_file_entry,
222
569
           string_value,
223
569
           64,
224
569
           NULL );
225
226
569
          libfsapfs_file_entry_get_offset(
227
569
           sub_file_entry,
228
569
           &file_offset,
229
569
           NULL );
230
231
569
          libfsapfs_file_entry_get_size(
232
569
           sub_file_entry,
233
569
           &file_size,
234
569
           NULL );
235
236
569
          libfsapfs_file_entry_get_number_of_extents(
237
569
           sub_file_entry,
238
569
           &number_of_extents,
239
569
           NULL );
240
569
        }
241
619
        libfsapfs_file_entry_free(
242
619
         &sub_file_entry,
243
619
         NULL );
244
619
      }
245
785
on_error_libfsapfs_root_directory:
246
785
      libfsapfs_file_entry_free(
247
785
       &root_directory,
248
785
       NULL );
249
785
    }
250
1.29k
    libfsapfs_volume_free(
251
1.29k
     &volume,
252
1.29k
     NULL );
253
1.29k
  }
254
4.27k
on_error_libfsapfs_container:
255
4.27k
  libfsapfs_container_free(
256
4.27k
   &container,
257
4.27k
   NULL );
258
259
4.27k
on_error_libbfio:
260
4.27k
  libbfio_handle_free(
261
4.27k
   &file_io_handle,
262
4.27k
   NULL );
263
264
4.27k
  return( 0 );
265
4.27k
}
266
267
} /* extern "C" */
268