/src/libfsext/ossfuzz/file_entry_fuzzer.cc
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * OSS-Fuzz target for libfsext file_entry type  | 
3  |  |  *  | 
4  |  |  * Copyright (C) 2010-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_libfsext.h"  | 
31  |  |  | 
32  |  | #if !defined( LIBFSEXT_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  |  | LIBFSEXT_EXTERN \  | 
38  |  | int libfsext_volume_open_file_io_handle(  | 
39  |  |      libfsext_volume_t *volume,  | 
40  |  |      libbfio_handle_t *file_io_handle,  | 
41  |  |      int access_flags,  | 
42  |  |      libfsext_error_t **error );  | 
43  |  |  | 
44  |  | #endif /* !defined( LIBFSEXT_HAVE_BFIO ) */  | 
45  |  |  | 
46  |  | int LLVMFuzzerTestOneInput(  | 
47  |  |      const uint8_t *data,  | 
48  |  |      size_t size )  | 
49  | 6.13k  | { | 
50  | 6.13k  |   uint8_t string_value[ 64 ];  | 
51  |  |  | 
52  | 6.13k  |   libbfio_handle_t *file_io_handle      = NULL;  | 
53  | 6.13k  |   libfsext_file_entry_t *root_directory = NULL;  | 
54  | 6.13k  |   libfsext_file_entry_t *sub_file_entry = NULL;  | 
55  | 6.13k  |   libfsext_volume_t *volume             = NULL;  | 
56  | 6.13k  |   size64_t file_size                    = 0;  | 
57  | 6.13k  |   size_t string_size                    = 0;  | 
58  | 6.13k  |   off64_t file_offset                   = 0;  | 
59  | 6.13k  |   int64_t posix_time_64bit              = 0;  | 
60  | 6.13k  |   uint32_t value_32bit                  = 0;  | 
61  | 6.13k  |   int32_t posix_time_32bit              = 0;  | 
62  | 6.13k  |   uint16_t value_16bit                  = 0;  | 
63  | 6.13k  |   uint8_t major_device_number           = 0;  | 
64  | 6.13k  |   uint8_t minor_device_number           = 0;  | 
65  | 6.13k  |   int number_of_extents                 = 0;  | 
66  | 6.13k  |   int number_of_sub_file_entries        = 0;  | 
67  | 6.13k  |   int result                            = 0;  | 
68  |  |  | 
69  | 6.13k  |   if( libbfio_memory_range_initialize(  | 
70  | 6.13k  |        &file_io_handle,  | 
71  | 6.13k  |        NULL ) != 1 )  | 
72  | 0  |   { | 
73  | 0  |     return( 0 );  | 
74  | 0  |   }  | 
75  | 6.13k  |   if( libbfio_memory_range_set(  | 
76  | 6.13k  |        file_io_handle,  | 
77  | 6.13k  |        (uint8_t *) data,  | 
78  | 6.13k  |        size,  | 
79  | 6.13k  |        NULL ) != 1 )  | 
80  | 0  |   { | 
81  | 0  |     goto on_error_libbfio;  | 
82  | 0  |   }  | 
83  | 6.13k  |   if( libfsext_volume_initialize(  | 
84  | 6.13k  |        &volume,  | 
85  | 6.13k  |        NULL ) != 1 )  | 
86  | 0  |   { | 
87  | 0  |     goto on_error_libbfio;  | 
88  | 0  |   }  | 
89  | 6.13k  |   if( libfsext_volume_open_file_io_handle(  | 
90  | 6.13k  |        volume,  | 
91  | 6.13k  |        file_io_handle,  | 
92  | 6.13k  |        LIBFSEXT_OPEN_READ,  | 
93  | 6.13k  |        NULL ) != 1 )  | 
94  | 1.54k  |   { | 
95  | 1.54k  |     goto on_error_libfsext_volume;  | 
96  | 1.54k  |   }  | 
97  | 4.58k  |   if( libfsext_volume_get_root_directory(  | 
98  | 4.58k  |        volume,  | 
99  | 4.58k  |        &root_directory,  | 
100  | 4.58k  |        NULL ) == 1 )  | 
101  | 3.00k  |   { | 
102  | 3.00k  |     if( libfsext_file_entry_get_number_of_sub_file_entries(  | 
103  | 3.00k  |          root_directory,  | 
104  | 3.00k  |          &number_of_sub_file_entries,  | 
105  | 3.00k  |          NULL ) != 1 )  | 
106  | 604  |     { | 
107  | 604  |       goto on_error_libfsext_root_directory;  | 
108  | 604  |     }  | 
109  | 2.39k  |     if( number_of_sub_file_entries > 0 )  | 
110  | 2.17k  |     { | 
111  | 2.17k  |       result = libfsext_file_entry_get_sub_file_entry_by_index(  | 
112  | 2.17k  |                 root_directory,  | 
113  | 2.17k  |                 0,  | 
114  | 2.17k  |                 &sub_file_entry,  | 
115  | 2.17k  |                 NULL );  | 
116  |  |  | 
117  | 2.17k  |       if( result != -1 )  | 
118  | 1.74k  |       { | 
119  | 1.74k  |         libfsext_file_entry_get_inode_number(  | 
120  | 1.74k  |          sub_file_entry,  | 
121  | 1.74k  |          &value_32bit,  | 
122  | 1.74k  |          NULL );  | 
123  |  |  | 
124  | 1.74k  |         libfsext_file_entry_get_creation_time(  | 
125  | 1.74k  |          sub_file_entry,  | 
126  | 1.74k  |          &posix_time_64bit,  | 
127  | 1.74k  |          NULL );  | 
128  |  |  | 
129  | 1.74k  |         libfsext_file_entry_get_modification_time(  | 
130  | 1.74k  |          sub_file_entry,  | 
131  | 1.74k  |          &posix_time_64bit,  | 
132  | 1.74k  |          NULL );  | 
133  |  |  | 
134  | 1.74k  |         libfsext_file_entry_get_access_time(  | 
135  | 1.74k  |          sub_file_entry,  | 
136  | 1.74k  |          &posix_time_64bit,  | 
137  | 1.74k  |          NULL );  | 
138  |  |  | 
139  | 1.74k  |         libfsext_file_entry_get_inode_change_time(  | 
140  | 1.74k  |          sub_file_entry,  | 
141  | 1.74k  |          &posix_time_64bit,  | 
142  | 1.74k  |          NULL );  | 
143  |  |  | 
144  | 1.74k  |         libfsext_file_entry_get_deletion_time(  | 
145  | 1.74k  |          sub_file_entry,  | 
146  | 1.74k  |          &posix_time_32bit,  | 
147  | 1.74k  |          NULL );  | 
148  |  |  | 
149  | 1.74k  |         libfsext_file_entry_get_file_mode(  | 
150  | 1.74k  |          sub_file_entry,  | 
151  | 1.74k  |          &value_16bit,  | 
152  | 1.74k  |          NULL );  | 
153  |  |  | 
154  | 1.74k  |         libfsext_file_entry_get_number_of_links(  | 
155  | 1.74k  |          sub_file_entry,  | 
156  | 1.74k  |          &value_16bit,  | 
157  | 1.74k  |          NULL );  | 
158  |  |  | 
159  | 1.74k  |         libfsext_file_entry_get_owner_identifier(  | 
160  | 1.74k  |          sub_file_entry,  | 
161  | 1.74k  |          &value_32bit,  | 
162  | 1.74k  |          NULL );  | 
163  |  |  | 
164  | 1.74k  |         libfsext_file_entry_get_group_identifier(  | 
165  | 1.74k  |          sub_file_entry,  | 
166  | 1.74k  |          &value_32bit,  | 
167  | 1.74k  |          NULL );  | 
168  |  |  | 
169  | 1.74k  |         libfsext_file_entry_get_device_identifier(  | 
170  | 1.74k  |          sub_file_entry,  | 
171  | 1.74k  |          &value_32bit,  | 
172  | 1.74k  |          NULL );  | 
173  |  |  | 
174  | 1.74k  |         libfsext_file_entry_get_device_number(  | 
175  | 1.74k  |          sub_file_entry,  | 
176  | 1.74k  |          &major_device_number,  | 
177  | 1.74k  |          &minor_device_number,  | 
178  | 1.74k  |          NULL );  | 
179  |  |  | 
180  | 1.74k  |         libfsext_file_entry_get_utf8_name_size(  | 
181  | 1.74k  |          sub_file_entry,  | 
182  | 1.74k  |          &string_size,  | 
183  | 1.74k  |          NULL );  | 
184  |  |  | 
185  | 1.74k  |         libfsext_file_entry_get_utf8_name(  | 
186  | 1.74k  |          sub_file_entry,  | 
187  | 1.74k  |          string_value,  | 
188  | 1.74k  |          64,  | 
189  | 1.74k  |          NULL );  | 
190  |  |  | 
191  | 1.74k  |         libfsext_file_entry_get_utf8_symbolic_link_target_size(  | 
192  | 1.74k  |          sub_file_entry,  | 
193  | 1.74k  |          &string_size,  | 
194  | 1.74k  |          NULL );  | 
195  |  |  | 
196  | 1.74k  |         libfsext_file_entry_get_utf8_symbolic_link_target(  | 
197  | 1.74k  |          sub_file_entry,  | 
198  | 1.74k  |          string_value,  | 
199  | 1.74k  |          64,  | 
200  | 1.74k  |          NULL );  | 
201  |  |  | 
202  | 1.74k  |         libfsext_file_entry_get_offset(  | 
203  | 1.74k  |          sub_file_entry,  | 
204  | 1.74k  |          &file_offset,  | 
205  | 1.74k  |          NULL );  | 
206  |  |  | 
207  | 1.74k  |         libfsext_file_entry_get_size(  | 
208  | 1.74k  |          sub_file_entry,  | 
209  | 1.74k  |          &file_size,  | 
210  | 1.74k  |          NULL );  | 
211  |  |  | 
212  | 1.74k  |         libfsext_file_entry_get_number_of_extents(  | 
213  | 1.74k  |          sub_file_entry,  | 
214  | 1.74k  |          &number_of_extents,  | 
215  | 1.74k  |          NULL );  | 
216  | 1.74k  |       }  | 
217  | 2.17k  |       libfsext_file_entry_free(  | 
218  | 2.17k  |        &sub_file_entry,  | 
219  | 2.17k  |        NULL );  | 
220  | 2.17k  |     }  | 
221  | 3.00k  | on_error_libfsext_root_directory:  | 
222  | 3.00k  |     libfsext_file_entry_free(  | 
223  | 3.00k  |      &root_directory,  | 
224  | 3.00k  |      NULL );  | 
225  | 3.00k  |   }  | 
226  | 4.58k  |   libfsext_volume_close(  | 
227  | 4.58k  |    volume,  | 
228  | 4.58k  |    NULL );  | 
229  |  |  | 
230  | 6.13k  | on_error_libfsext_volume:  | 
231  | 6.13k  |   libfsext_volume_free(  | 
232  | 6.13k  |    &volume,  | 
233  | 6.13k  |    NULL );  | 
234  |  |  | 
235  | 6.13k  | on_error_libbfio:  | 
236  | 6.13k  |   libbfio_handle_free(  | 
237  | 6.13k  |    &file_io_handle,  | 
238  | 6.13k  |    NULL );  | 
239  |  |  | 
240  | 6.13k  |   return( 0 );  | 
241  | 6.13k  | }  | 
242  |  |  | 
243  |  | } /* extern "C" */  | 
244  |  |  |