Coverage Report

Created: 2023-06-07 06:53

/src/libfsapfs/ossfuzz/extended_attribute_fuzzer.cc
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * OSS-Fuzz target for libfsapfs extended_attribute type
3
 *
4
 * Copyright (C) 2018-2023, 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.10k
{
50
4.10k
  libbfio_handle_t *file_io_handle                   = NULL;
51
4.10k
  libfsapfs_container_t *container                   = NULL;
52
4.10k
  libfsapfs_extended_attribute_t *extended_attribute = NULL;
53
4.10k
  libfsapfs_file_entry_t *file_entry                 = NULL;
54
4.10k
  libfsapfs_volume_t *volume                         = NULL;
55
4.10k
  int number_of_extended_attributes                  = 0;
56
4.10k
  int number_of_volumes                              = 0;
57
4.10k
  int result                                         = 0;
58
59
4.10k
  if( libbfio_memory_range_initialize(
60
4.10k
       &file_io_handle,
61
4.10k
       NULL ) != 1 )
62
0
  {
63
0
    return( 0 );
64
0
  }
65
4.10k
  if( libbfio_memory_range_set(
66
4.10k
       file_io_handle,
67
4.10k
       (uint8_t *) data,
68
4.10k
       size,
69
4.10k
       NULL ) != 1 )
70
0
  {
71
0
    goto on_error_libbfio;
72
0
  }
73
4.10k
  if( libfsapfs_container_initialize(
74
4.10k
       &container,
75
4.10k
       NULL ) != 1 )
76
0
  {
77
0
    goto on_error_libbfio;
78
0
  }
79
4.10k
  if( libfsapfs_container_open_file_io_handle(
80
4.10k
       container,
81
4.10k
       file_io_handle,
82
4.10k
       LIBFSAPFS_OPEN_READ,
83
4.10k
       NULL ) != 1 )
84
1.41k
  {
85
1.41k
    goto on_error_libfsapfs_container;
86
1.41k
  }
87
2.68k
  if( libfsapfs_container_get_number_of_volumes(
88
2.68k
       container,
89
2.68k
       &number_of_volumes,
90
2.68k
       NULL ) != 1 )
91
0
  {
92
0
    goto on_error_libfsapfs_container;
93
0
  }
94
2.68k
  if( number_of_volumes > 0 )
95
2.68k
  {
96
2.68k
    if( libfsapfs_container_get_volume_by_index(
97
2.68k
         container,
98
2.68k
         0,
99
2.68k
         &volume,
100
2.68k
         NULL ) != 1 )
101
1.50k
    {
102
1.50k
      goto on_error_libfsapfs_container;
103
1.50k
    }
104
1.18k
    result = libfsapfs_volume_get_file_entry_by_utf8_path(
105
1.18k
              volume,
106
1.18k
              (uint8_t *) "/a_directory/a_file",
107
1.18k
              19,
108
1.18k
              &file_entry,
109
1.18k
              NULL );
110
111
1.18k
    if( result == 1 )
112
219
    {
113
219
      if( libfsapfs_file_entry_get_number_of_extended_attributes(
114
219
           file_entry,
115
219
           &number_of_extended_attributes,
116
219
           NULL ) != 1 )
117
77
      {
118
77
        goto on_error_libfsapfs_file_entry;
119
77
      }
120
142
      if( number_of_extended_attributes > 0 )
121
63
      {
122
63
        if( libfsapfs_file_entry_get_extended_attribute_by_index(
123
63
             file_entry,
124
63
             0,
125
63
             &extended_attribute,
126
63
             NULL ) != 1 )
127
0
        {
128
0
          goto on_error_libfsapfs_file_entry;
129
0
        }
130
63
        libfsapfs_extended_attribute_free(
131
63
         &extended_attribute,
132
63
         NULL );
133
63
      }
134
219
on_error_libfsapfs_file_entry:
135
219
      libfsapfs_file_entry_free(
136
219
       &file_entry,
137
219
       NULL );
138
219
    }
139
1.18k
    libfsapfs_volume_free(
140
1.18k
     &volume,
141
1.18k
     NULL );
142
1.18k
  }
143
4.10k
on_error_libfsapfs_container:
144
4.10k
  libfsapfs_container_free(
145
4.10k
   &container,
146
4.10k
   NULL );
147
148
4.10k
on_error_libbfio:
149
4.10k
  libbfio_handle_free(
150
4.10k
   &file_io_handle,
151
4.10k
   NULL );
152
153
4.10k
  return( 0 );
154
4.10k
}
155
156
} /* extern "C" */
157