Coverage Report

Created: 2026-12-01 07:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfsntfs/ossfuzz/file_entry_fuzzer.cc
Line
Count
Source
1
/*
2
 * OSS-Fuzz target for libfsntfs file_entry type
3
 *
4
 * Copyright (C) 2010-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_libfsntfs.h"
31
32
#if !defined( LIBFSNTFS_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
LIBFSNTFS_EXTERN \
38
int libfsntfs_volume_open_file_io_handle(
39
      libfsntfs_volume_t *volume,
40
      libbfio_handle_t *file_io_handle,
41
      int access_flags,
42
      libfsntfs_error_t **error );
43
44
#endif /* !defined( LIBFSNTFS_HAVE_BFIO ) */
45
46
int LLVMFuzzerTestOneInput(
47
      const uint8_t *data,
48
      size_t size )
49
5.36k
{
50
5.36k
  uint8_t buffer[ 512 ];
51
5.36k
  uint8_t utf8_string[ 64 ];
52
5.36k
  uint16_t utf16_string[ 32 ];
53
54
5.36k
  libbfio_handle_t *file_io_handle       = NULL;
55
5.36k
  libfsntfs_file_entry_t *root_directory = NULL;
56
5.36k
  libfsntfs_file_entry_t *sub_file_entry = NULL;
57
5.36k
  libfsntfs_volume_t *volume             = NULL;
58
5.36k
  uint64_t file_reference                = 0;
59
5.36k
  uint64_t filetime                      = 0;
60
5.36k
  uint64_t i30_size                      = 0;
61
5.36k
  uint64_t journal_sequence_number       = 0;
62
5.36k
  uint64_t parent_file_reference         = 0;
63
5.36k
  size64_t extent_size                   = 0;
64
5.36k
  size64_t file_size                     = 0;
65
5.36k
  size_t security_descriptor_size        = 0;
66
5.36k
  size_t utf8_string_size                = 0;
67
5.36k
  size_t utf16_string_size               = 0;
68
5.36k
  off64_t extent_offset                  = 0;
69
5.36k
  off64_t file_offset                    = 0;
70
5.36k
  uint32_t extent_flags                  = 0;
71
5.36k
  uint32_t file_attribute_flags          = 0;
72
5.36k
  uint32_t i30_file_attribute_flags      = 0;
73
5.36k
  int name_attribute_index               = 0;
74
5.36k
  int number_of_extents                  = 0;
75
5.36k
  int number_of_sub_file_entries         = 0;
76
5.36k
  int read_iterator                      = 0;
77
5.36k
  int result                             = 0;
78
79
5.36k
  if( libbfio_memory_range_initialize(
80
5.36k
       &file_io_handle,
81
5.36k
       NULL ) != 1 )
82
0
  {
83
0
    return( 0 );
84
0
  }
85
5.36k
  if( libbfio_memory_range_set(
86
5.36k
       file_io_handle,
87
5.36k
       (uint8_t *) data,
88
5.36k
       size,
89
5.36k
       NULL ) != 1 )
90
0
  {
91
0
    goto on_error_libbfio;
92
0
  }
93
5.36k
  if( libfsntfs_volume_initialize(
94
5.36k
       &volume,
95
5.36k
       NULL ) != 1 )
96
0
  {
97
0
    goto on_error_libbfio;
98
0
  }
99
5.36k
  if( libfsntfs_volume_open_file_io_handle(
100
5.36k
       volume,
101
5.36k
       file_io_handle,
102
5.36k
       LIBFSNTFS_OPEN_READ,
103
5.36k
       NULL ) != 1 )
104
3.35k
  {
105
3.35k
    goto on_error_libfsntfs_volume;
106
3.35k
  }
107
2.01k
  if( libfsntfs_volume_get_root_directory(
108
2.01k
       volume,
109
2.01k
       &root_directory,
110
2.01k
       NULL ) != 1 )
111
1.72k
  {
112
1.72k
    goto on_error_libfsntfs_volume;
113
1.72k
  }
114
295
  if( libfsntfs_file_entry_is_empty(
115
295
       root_directory,
116
295
       NULL ) == -1 )
117
0
  {
118
0
    goto on_error_libfsntfs_root_directory;
119
0
  }
120
295
  if( libfsntfs_file_entry_is_allocated(
121
295
       root_directory,
122
295
       NULL ) == -1 )
123
2
  {
124
2
    goto on_error_libfsntfs_root_directory;
125
2
  }
126
293
  if( libfsntfs_file_entry_is_corrupted(
127
293
       root_directory,
128
293
       NULL ) == -1 )
129
0
  {
130
0
    goto on_error_libfsntfs_root_directory;
131
0
  }
132
293
  if( libfsntfs_file_entry_get_file_reference(
133
293
       root_directory,
134
293
       &file_reference,
135
293
       NULL ) != 1 )
136
0
  {
137
0
    goto on_error_libfsntfs_root_directory;
138
0
  }
139
293
  if( libfsntfs_file_entry_get_number_of_sub_file_entries(
140
293
       root_directory,
141
293
       &number_of_sub_file_entries,
142
293
       NULL ) != 1 )
143
0
  {
144
0
    goto on_error_libfsntfs_root_directory;
145
0
  }
146
293
  if( number_of_sub_file_entries > 0 )
147
126
  {
148
126
    if( libfsntfs_file_entry_get_sub_file_entry_by_index(
149
126
         root_directory,
150
126
         0,
151
126
         &sub_file_entry,
152
126
         NULL ) != 1 )
153
25
    {
154
25
      goto on_error_libfsntfs_root_directory;
155
25
    }
156
101
    if( libfsntfs_file_entry_is_empty(
157
101
         sub_file_entry,
158
101
         NULL ) == -1 )
159
0
    {
160
0
      goto on_error_libfsntfs_sub_file_entry;
161
0
    }
162
101
    if( libfsntfs_file_entry_is_allocated(
163
101
         sub_file_entry,
164
101
         NULL ) == -1 )
165
1
    {
166
1
      goto on_error_libfsntfs_sub_file_entry;
167
1
    }
168
100
    if( libfsntfs_file_entry_is_corrupted(
169
100
         sub_file_entry,
170
100
         NULL ) == -1 )
171
0
    {
172
0
      goto on_error_libfsntfs_sub_file_entry;
173
0
    }
174
100
    if( libfsntfs_file_entry_get_file_reference(
175
100
         sub_file_entry,
176
100
         &file_reference,
177
100
         NULL ) != 1 )
178
0
    {
179
0
      goto on_error_libfsntfs_sub_file_entry;
180
0
    }
181
100
    if( libfsntfs_file_entry_get_base_record_file_reference(
182
100
         sub_file_entry,
183
100
         &file_reference,
184
100
         NULL ) == -1 )
185
0
    {
186
0
      goto on_error_libfsntfs_sub_file_entry;
187
0
    }
188
100
    if( libfsntfs_file_entry_get_parent_file_reference(
189
100
         sub_file_entry,
190
100
         &parent_file_reference,
191
100
         NULL ) == -1 )
192
0
    {
193
0
      goto on_error_libfsntfs_sub_file_entry;
194
0
    }
195
100
    if( libfsntfs_file_entry_get_journal_sequence_number(
196
100
         sub_file_entry,
197
100
         &journal_sequence_number,
198
100
         NULL ) != 1 )
199
0
    {
200
0
      goto on_error_libfsntfs_sub_file_entry;
201
0
    }
202
100
    if( libfsntfs_file_entry_get_creation_time(
203
100
         sub_file_entry,
204
100
         &filetime,
205
100
         NULL ) == -1 )
206
1
    {
207
1
      goto on_error_libfsntfs_sub_file_entry;
208
1
    }
209
99
    if( libfsntfs_file_entry_get_modification_time(
210
99
         sub_file_entry,
211
99
         &filetime,
212
99
         NULL ) == -1 )
213
0
    {
214
0
      goto on_error_libfsntfs_sub_file_entry;
215
0
    }
216
99
    if( libfsntfs_file_entry_get_access_time(
217
99
         sub_file_entry,
218
99
         &filetime,
219
99
         NULL ) == -1 )
220
0
    {
221
0
      goto on_error_libfsntfs_sub_file_entry;
222
0
    }
223
99
    if( libfsntfs_file_entry_get_entry_modification_time(
224
99
         sub_file_entry,
225
99
         &filetime,
226
99
         NULL ) == -1 )
227
0
    {
228
0
      goto on_error_libfsntfs_sub_file_entry;
229
0
    }
230
99
    if( libfsntfs_file_entry_get_file_attribute_flags(
231
99
         sub_file_entry,
232
99
         &file_attribute_flags,
233
99
         NULL ) == -1 )
234
0
    {
235
0
      goto on_error_libfsntfs_sub_file_entry;
236
0
    }
237
99
    if( libfsntfs_file_entry_get_i30_file_reference(
238
99
         sub_file_entry,
239
99
         &file_reference,
240
99
         NULL ) == -1 )
241
0
    {
242
0
      goto on_error_libfsntfs_sub_file_entry;
243
0
    }
244
99
    if( libfsntfs_file_entry_get_i30_creation_time(
245
99
         sub_file_entry,
246
99
         &filetime,
247
99
         NULL ) == -1 )
248
0
    {
249
0
      goto on_error_libfsntfs_sub_file_entry;
250
0
    }
251
99
    if( libfsntfs_file_entry_get_i30_modification_time(
252
99
         sub_file_entry,
253
99
         &filetime,
254
99
         NULL ) == -1 )
255
0
    {
256
0
      goto on_error_libfsntfs_sub_file_entry;
257
0
    }
258
99
    if( libfsntfs_file_entry_get_i30_access_time(
259
99
         sub_file_entry,
260
99
         &filetime,
261
99
         NULL ) == -1 )
262
0
    {
263
0
      goto on_error_libfsntfs_sub_file_entry;
264
0
    }
265
99
    if( libfsntfs_file_entry_get_i30_entry_modification_time(
266
99
         sub_file_entry,
267
99
         &filetime,
268
99
         NULL ) == -1 )
269
0
    {
270
0
      goto on_error_libfsntfs_sub_file_entry;
271
0
    }
272
99
    if( libfsntfs_file_entry_get_i30_size(
273
99
         sub_file_entry,
274
99
         &i30_size,
275
99
         NULL ) == -1 )
276
0
    {
277
0
      goto on_error_libfsntfs_sub_file_entry;
278
0
    }
279
99
    if( libfsntfs_file_entry_get_i30_file_attribute_flags(
280
99
         sub_file_entry,
281
99
         &i30_file_attribute_flags,
282
99
         NULL ) == -1 )
283
0
    {
284
0
      goto on_error_libfsntfs_sub_file_entry;
285
0
    }
286
99
    if( libfsntfs_file_entry_get_utf8_name_size(
287
99
         sub_file_entry,
288
99
         &utf8_string_size,
289
99
         NULL ) == -1 )
290
0
    {
291
0
      goto on_error_libfsntfs_sub_file_entry;
292
0
    }
293
99
    if( libfsntfs_file_entry_get_utf8_name(
294
99
         sub_file_entry,
295
99
         utf8_string,
296
99
         64,
297
99
         NULL ) == -1 )
298
0
    {
299
0
      goto on_error_libfsntfs_sub_file_entry;
300
0
    }
301
99
    if( libfsntfs_file_entry_get_utf16_name_size(
302
99
         sub_file_entry,
303
99
         &utf16_string_size,
304
99
         NULL ) == -1 )
305
0
    {
306
0
      goto on_error_libfsntfs_sub_file_entry;
307
0
    }
308
99
    if( libfsntfs_file_entry_get_utf16_name(
309
99
         sub_file_entry,
310
99
         utf16_string,
311
99
         32,
312
99
         NULL ) == -1 )
313
0
    {
314
0
      goto on_error_libfsntfs_sub_file_entry;
315
0
    }
316
99
    result = libfsntfs_file_entry_get_name_attribute_index(
317
99
               sub_file_entry,
318
99
               &name_attribute_index,
319
99
               NULL );
320
321
99
    if( result == -1 )
322
31
    {
323
31
      goto on_error_libfsntfs_sub_file_entry;
324
31
    }
325
68
    if( result == 1 )
326
1
    {
327
1
      if( libfsntfs_file_entry_get_parent_file_reference_by_attribute_index(
328
1
           sub_file_entry,
329
1
           name_attribute_index,
330
1
           &parent_file_reference,
331
1
           NULL ) != 1 )
332
0
      {
333
0
        goto on_error_libfsntfs_sub_file_entry;
334
0
      }
335
1
      if( libfsntfs_file_entry_get_utf8_name_size_by_attribute_index(
336
1
           sub_file_entry,
337
1
           name_attribute_index,
338
1
           &utf8_string_size,
339
1
           NULL ) != 1 )
340
0
      {
341
0
        goto on_error_libfsntfs_sub_file_entry;
342
0
      }
343
1
      if( libfsntfs_file_entry_get_utf8_name_by_attribute_index(
344
1
           sub_file_entry,
345
1
           name_attribute_index,
346
1
           utf8_string,
347
1
           64,
348
1
           NULL ) != 1 )
349
0
      {
350
0
        goto on_error_libfsntfs_sub_file_entry;
351
0
      }
352
1
      if( libfsntfs_file_entry_get_utf16_name_size_by_attribute_index(
353
1
           sub_file_entry,
354
1
           name_attribute_index,
355
1
           &utf16_string_size,
356
1
           NULL ) != 1 )
357
0
      {
358
0
        goto on_error_libfsntfs_sub_file_entry;
359
0
      }
360
1
      if( libfsntfs_file_entry_get_utf16_name_by_attribute_index(
361
1
           sub_file_entry,
362
1
           name_attribute_index,
363
1
           utf16_string,
364
1
           32,
365
1
           NULL ) != 1 )
366
0
      {
367
0
        goto on_error_libfsntfs_sub_file_entry;
368
0
      }
369
1
      if( libfsntfs_file_entry_get_utf8_path_hint_size(
370
1
           sub_file_entry,
371
1
           name_attribute_index,
372
1
           &utf8_string_size,
373
1
           NULL ) == -1 )
374
0
      {
375
0
        goto on_error_libfsntfs_sub_file_entry;
376
0
      }
377
1
      if( libfsntfs_file_entry_get_utf8_path_hint(
378
1
           sub_file_entry,
379
1
           name_attribute_index,
380
1
           utf8_string,
381
1
           64,
382
1
           NULL ) == -1 )
383
0
      {
384
0
        goto on_error_libfsntfs_sub_file_entry;
385
0
      }
386
1
      if( libfsntfs_file_entry_get_utf16_path_hint_size(
387
1
           sub_file_entry,
388
1
           name_attribute_index,
389
1
           &utf16_string_size,
390
1
           NULL ) == -1 )
391
0
      {
392
0
        goto on_error_libfsntfs_sub_file_entry;
393
0
      }
394
1
      if( libfsntfs_file_entry_get_utf16_path_hint(
395
1
           sub_file_entry,
396
1
           name_attribute_index,
397
1
           utf16_string,
398
1
           32,
399
1
           NULL ) == -1 )
400
0
      {
401
0
        goto on_error_libfsntfs_sub_file_entry;
402
0
      }
403
1
    }
404
68
    if( libfsntfs_file_entry_is_symbolic_link(
405
68
         sub_file_entry,
406
68
         NULL ) == -1 )
407
0
    {
408
0
      goto on_error_libfsntfs_sub_file_entry;
409
0
    }
410
68
    if( libfsntfs_file_entry_get_utf8_symbolic_link_target_size(
411
68
         sub_file_entry,
412
68
         &utf8_string_size,
413
68
         NULL ) == -1 )
414
1
    {
415
1
      goto on_error_libfsntfs_sub_file_entry;
416
1
    }
417
67
    if( libfsntfs_file_entry_get_utf8_symbolic_link_target(
418
67
         sub_file_entry,
419
67
         utf8_string,
420
67
         64,
421
67
         NULL ) == -1 )
422
0
    {
423
0
      goto on_error_libfsntfs_sub_file_entry;
424
0
    }
425
67
    if( libfsntfs_file_entry_get_utf16_symbolic_link_target_size(
426
67
         sub_file_entry,
427
67
         &utf16_string_size,
428
67
         NULL ) == -1 )
429
0
    {
430
0
      goto on_error_libfsntfs_sub_file_entry;
431
0
    }
432
67
    if( libfsntfs_file_entry_get_utf16_symbolic_link_target(
433
67
         sub_file_entry,
434
67
         utf16_string,
435
67
         32,
436
67
         NULL ) == -1 )
437
0
    {
438
0
      goto on_error_libfsntfs_sub_file_entry;
439
0
    }
440
67
    if( libfsntfs_file_entry_get_security_descriptor_size(
441
67
         sub_file_entry,
442
67
         &security_descriptor_size,
443
67
         NULL ) == -1 )
444
16
    {
445
16
      goto on_error_libfsntfs_sub_file_entry;
446
16
    }
447
51
    if( libfsntfs_file_entry_get_security_descriptor(
448
51
         sub_file_entry,
449
51
         utf8_string,
450
51
         64,
451
51
         NULL ) == -1 )
452
28
    {
453
28
      goto on_error_libfsntfs_sub_file_entry;
454
28
    }
455
23
    if( libfsntfs_file_entry_has_directory_entries_index(
456
23
         sub_file_entry,
457
23
         NULL ) == -1 )
458
0
    {
459
0
      goto on_error_libfsntfs_sub_file_entry;
460
0
    }
461
23
    result = libfsntfs_file_entry_has_default_data_stream(
462
23
               sub_file_entry,
463
23
               NULL );
464
465
23
    if( result == -1 )
466
0
    {
467
0
      goto on_error_libfsntfs_sub_file_entry;
468
0
    }
469
23
    if( libfsntfs_file_entry_has_i30_entry(
470
23
         sub_file_entry,
471
23
         NULL ) == -1 )
472
0
    {
473
0
      goto on_error_libfsntfs_sub_file_entry;
474
0
    }
475
23
    if( libfsntfs_file_entry_get_number_of_sub_file_entries(
476
23
         sub_file_entry,
477
23
         &number_of_sub_file_entries,
478
23
         NULL ) != 1 )
479
0
    {
480
0
      goto on_error_libfsntfs_sub_file_entry;
481
0
    }
482
23
    if( result == 1 )
483
15
    {
484
15
      if( libfsntfs_file_entry_get_offset(
485
15
           sub_file_entry,
486
15
           &file_offset,
487
15
           NULL ) != 1 )
488
1
      {
489
1
        goto on_error_libfsntfs_sub_file_entry;
490
1
      }
491
14
      if( libfsntfs_file_entry_get_size(
492
14
           sub_file_entry,
493
14
           &file_size,
494
14
           NULL ) != 1 )
495
0
      {
496
0
        goto on_error_libfsntfs_sub_file_entry;
497
0
      }
498
14
      for( read_iterator = 0;
499
782
           read_iterator < 128;
500
768
           read_iterator++ )
501
778
      {
502
778
        if( file_offset >= file_size )
503
4
        {
504
4
          break;
505
4
        }
506
774
        if( libfsntfs_file_entry_read_buffer_at_offset(
507
774
             sub_file_entry,
508
774
             buffer,
509
774
             497,
510
774
             file_offset,
511
774
             NULL ) == -1 )
512
6
        {
513
6
          goto on_error_libfsntfs_sub_file_entry;
514
6
        }
515
768
        file_offset += 497;
516
768
      }
517
8
      if( libfsntfs_file_entry_get_number_of_extents(
518
8
           sub_file_entry,
519
8
           &number_of_extents,
520
8
           NULL ) != 1 )
521
0
      {
522
0
        goto on_error_libfsntfs_sub_file_entry;
523
0
      }
524
8
      if( number_of_extents > 0 )
525
6
      {
526
6
        if( libfsntfs_file_entry_get_extent_by_index(
527
6
             sub_file_entry,
528
6
             0,
529
6
             &extent_offset,
530
6
             &extent_size,
531
6
             &extent_flags,
532
6
             NULL ) != 1 )
533
0
        {
534
0
          goto on_error_libfsntfs_sub_file_entry;
535
0
        }
536
6
      }
537
8
    }
538
101
on_error_libfsntfs_sub_file_entry:
539
101
    libfsntfs_file_entry_free(
540
101
     &sub_file_entry,
541
101
     NULL );
542
101
  }
543
295
on_error_libfsntfs_root_directory:
544
295
  libfsntfs_file_entry_free(
545
295
   &root_directory,
546
295
   NULL );
547
295
  libfsntfs_volume_close(
548
295
   volume,
549
295
   NULL );
550
551
5.36k
on_error_libfsntfs_volume:
552
5.36k
  libfsntfs_volume_free(
553
5.36k
   &volume,
554
5.36k
   NULL );
555
556
5.36k
on_error_libbfio:
557
5.36k
  libbfio_handle_free(
558
5.36k
   &file_io_handle,
559
5.36k
   NULL );
560
561
5.36k
  return( 0 );
562
5.36k
}
563
564
} /* extern "C" */