Coverage Report

Created: 2025-06-24 07:14

/src/libfshfs/ossfuzz/file_entry_fuzzer.cc
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * OSS-Fuzz target for libfshfs file_entry type
3
 *
4
 * Copyright (C) 2009-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_libfshfs.h"
31
32
#if !defined( LIBFSHFS_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
LIBFSHFS_EXTERN \
38
int libfshfs_volume_open_file_io_handle(
39
     libfshfs_volume_t *volume,
40
     libbfio_handle_t *file_io_handle,
41
     int access_flags,
42
     libfshfs_error_t **error );
43
44
#endif /* !defined( LIBFSHFS_HAVE_BFIO ) */
45
46
int LLVMFuzzerTestOneInput(
47
     const uint8_t *data,
48
     size_t size )
49
6.25k
{
50
6.25k
  uint8_t string_value[ 64 ];
51
52
6.25k
  libbfio_handle_t *file_io_handle      = NULL;
53
6.25k
  libfshfs_file_entry_t *root_directory = NULL;
54
6.25k
  libfshfs_file_entry_t *sub_file_entry = NULL;
55
6.25k
  libfshfs_volume_t *volume             = NULL;
56
6.25k
  size64_t file_size                    = 0;
57
6.25k
  size_t string_size                    = 0;
58
6.25k
  off64_t file_offset                   = 0;
59
6.25k
  uint32_t hfs_time                     = 0;
60
6.25k
  uint32_t major_device_number          = 0;
61
6.25k
  uint32_t minor_device_number          = 0;
62
6.25k
  uint32_t value_32bit                  = 0;
63
6.25k
  int32_t posix_time                    = 0;
64
6.25k
  uint16_t value_16bit                  = 0;
65
6.25k
  int number_of_extents                 = 0;
66
6.25k
  int number_of_sub_file_entries        = 0;
67
6.25k
  int result                            = 0;
68
69
6.25k
  if( libbfio_memory_range_initialize(
70
6.25k
       &file_io_handle,
71
6.25k
       NULL ) != 1 )
72
0
  {
73
0
    return( 0 );
74
0
  }
75
6.25k
  if( libbfio_memory_range_set(
76
6.25k
       file_io_handle,
77
6.25k
       (uint8_t *) data,
78
6.25k
       size,
79
6.25k
       NULL ) != 1 )
80
0
  {
81
0
    goto on_error_libbfio;
82
0
  }
83
6.25k
  if( libfshfs_volume_initialize(
84
6.25k
       &volume,
85
6.25k
       NULL ) != 1 )
86
0
  {
87
0
    goto on_error_libbfio;
88
0
  }
89
6.25k
  if( libfshfs_volume_open_file_io_handle(
90
6.25k
       volume,
91
6.25k
       file_io_handle,
92
6.25k
       LIBFSHFS_OPEN_READ,
93
6.25k
       NULL ) != 1 )
94
1.56k
  {
95
1.56k
    goto on_error_libfshfs_volume;
96
1.56k
  }
97
4.69k
  if( libfshfs_volume_get_root_directory(
98
4.69k
       volume,
99
4.69k
       &root_directory,
100
4.69k
       NULL ) == 1 )
101
3.08k
  {
102
3.08k
    if( libfshfs_file_entry_get_number_of_sub_file_entries(
103
3.08k
         root_directory,
104
3.08k
         &number_of_sub_file_entries,
105
3.08k
         NULL ) != 1 )
106
625
    {
107
625
      goto on_error_libfshfs_root_directory;
108
625
    }
109
2.45k
    if( number_of_sub_file_entries > 0 )
110
2.23k
    {
111
2.23k
      result = libfshfs_file_entry_get_sub_file_entry_by_index(
112
2.23k
                root_directory,
113
2.23k
                0,
114
2.23k
                &sub_file_entry,
115
2.23k
                NULL );
116
117
2.23k
      if( result != -1 )
118
1.79k
      {
119
1.79k
        libfshfs_file_entry_get_identifier(
120
1.79k
         sub_file_entry,
121
1.79k
         &value_32bit,
122
1.79k
         NULL );
123
124
1.79k
        libfshfs_file_entry_get_parent_identifier(
125
1.79k
         sub_file_entry,
126
1.79k
         &value_32bit,
127
1.79k
         NULL );
128
129
1.79k
        libfshfs_file_entry_get_link_identifier(
130
1.79k
         sub_file_entry,
131
1.79k
         &value_32bit,
132
1.79k
         NULL );
133
134
1.79k
        libfshfs_file_entry_get_creation_time(
135
1.79k
         sub_file_entry,
136
1.79k
         &hfs_time,
137
1.79k
         NULL );
138
139
1.79k
        libfshfs_file_entry_get_access_time(
140
1.79k
         sub_file_entry,
141
1.79k
         &hfs_time,
142
1.79k
         NULL );
143
144
1.79k
        libfshfs_file_entry_get_backup_time(
145
1.79k
         sub_file_entry,
146
1.79k
         &hfs_time,
147
1.79k
         NULL );
148
149
1.79k
        libfshfs_file_entry_get_added_time(
150
1.79k
         sub_file_entry,
151
1.79k
         &posix_time,
152
1.79k
         NULL );
153
154
1.79k
        libfshfs_file_entry_get_file_mode(
155
1.79k
         sub_file_entry,
156
1.79k
         &value_16bit,
157
1.79k
         NULL );
158
159
1.79k
        libfshfs_file_entry_get_number_of_links(
160
1.79k
         sub_file_entry,
161
1.79k
         &value_32bit,
162
1.79k
         NULL );
163
164
1.79k
        libfshfs_file_entry_get_owner_identifier(
165
1.79k
         sub_file_entry,
166
1.79k
         &value_32bit,
167
1.79k
         NULL );
168
169
1.79k
        libfshfs_file_entry_get_group_identifier(
170
1.79k
         sub_file_entry,
171
1.79k
         &value_32bit,
172
1.79k
         NULL );
173
174
1.79k
        libfshfs_file_entry_get_device_identifier(
175
1.79k
         sub_file_entry,
176
1.79k
         &value_32bit,
177
1.79k
         NULL );
178
179
1.79k
        libfshfs_file_entry_get_device_number(
180
1.79k
         sub_file_entry,
181
1.79k
         &major_device_number,
182
1.79k
         &minor_device_number,
183
1.79k
         NULL );
184
185
1.79k
        libfshfs_file_entry_get_utf8_name_size(
186
1.79k
         sub_file_entry,
187
1.79k
         &string_size,
188
1.79k
         NULL );
189
190
1.79k
        libfshfs_file_entry_get_utf8_name(
191
1.79k
         sub_file_entry,
192
1.79k
         string_value,
193
1.79k
         64,
194
1.79k
         NULL );
195
196
1.79k
        libfshfs_file_entry_get_utf8_symbolic_link_target_size(
197
1.79k
         sub_file_entry,
198
1.79k
         &string_size,
199
1.79k
         NULL );
200
201
1.79k
        libfshfs_file_entry_get_utf8_symbolic_link_target(
202
1.79k
         sub_file_entry,
203
1.79k
         string_value,
204
1.79k
         64,
205
1.79k
         NULL );
206
207
1.79k
        libfshfs_file_entry_has_resource_fork(
208
1.79k
         sub_file_entry,
209
1.79k
         NULL );
210
211
1.79k
        libfshfs_file_entry_get_offset(
212
1.79k
         sub_file_entry,
213
1.79k
         &file_offset,
214
1.79k
         NULL );
215
216
1.79k
        libfshfs_file_entry_get_size(
217
1.79k
         sub_file_entry,
218
1.79k
         &file_size,
219
1.79k
         NULL );
220
221
1.79k
        libfshfs_file_entry_get_number_of_extents(
222
1.79k
         sub_file_entry,
223
1.79k
         &number_of_extents,
224
1.79k
         NULL );
225
1.79k
      }
226
2.23k
      libfshfs_file_entry_free(
227
2.23k
       &sub_file_entry,
228
2.23k
       NULL );
229
2.23k
    }
230
3.08k
on_error_libfshfs_root_directory:
231
3.08k
    libfshfs_file_entry_free(
232
3.08k
     &root_directory,
233
3.08k
     NULL );
234
3.08k
  }
235
4.69k
  libfshfs_volume_close(
236
4.69k
   volume,
237
4.69k
   NULL );
238
239
6.25k
on_error_libfshfs_volume:
240
6.25k
  libfshfs_volume_free(
241
6.25k
   &volume,
242
6.25k
   NULL );
243
244
6.25k
on_error_libbfio:
245
6.25k
  libbfio_handle_free(
246
6.25k
   &file_io_handle,
247
6.25k
   NULL );
248
249
6.25k
  return( 0 );
250
6.25k
}
251
252
} /* extern "C" */
253