/src/libfsfat/ossfuzz/volume_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libfsfat volume type |
3 | | * |
4 | | * Copyright (C) 2021-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_libfsfat.h" |
31 | | |
32 | | #if !defined( LIBFSFAT_HAVE_BFIO ) |
33 | | |
34 | | /* Determines if a file contains an ext volume signature using a Basic File IO (bfio) handle |
35 | | * Returns 1 if true, 0 if not or -1 on error |
36 | | */ |
37 | | LIBFSFAT_EXTERN \ |
38 | | int libfsfat_check_volume_signature_file_io_handle( |
39 | | libbfio_handle_t *file_io_handle, |
40 | | libfsfat_error_t **error ); |
41 | | |
42 | | /* Opens a volume using a Basic File IO (bfio) handle |
43 | | * Returns 1 if successful or -1 on error |
44 | | */ |
45 | | LIBFSFAT_EXTERN \ |
46 | | int libfsfat_volume_open_file_io_handle( |
47 | | libfsfat_volume_t *volume, |
48 | | libbfio_handle_t *file_io_handle, |
49 | | int access_flags, |
50 | | libfsfat_error_t **error ); |
51 | | |
52 | | #endif /* !defined( LIBFSFAT_HAVE_BFIO ) */ |
53 | | |
54 | | int LLVMFuzzerTestOneInput( |
55 | | const uint8_t *data, |
56 | | size_t size ) |
57 | 1.35k | { |
58 | 1.35k | uint8_t utf8_label[ 512 ]; |
59 | 1.35k | uint16_t utf16_label[ 512 ]; |
60 | 1.35k | uint16_t utf16_path[ 6 ]; |
61 | | |
62 | 1.35k | libbfio_handle_t *file_io_handle = NULL; |
63 | 1.35k | libfsfat_file_entry_t *file_entry = NULL; |
64 | 1.35k | libfsfat_volume_t *volume = NULL; |
65 | 1.35k | uint8_t file_system_format = 0; |
66 | 1.35k | size_t utf8_label_size = 0; |
67 | 1.35k | size_t utf16_label_size = 0; |
68 | 1.35k | int result = 0; |
69 | | |
70 | | /* Relative path with a leading separator (UTF-8: "\\..\\..") |
71 | | */ |
72 | 1.35k | utf16_path[ 0 ] = (uint16_t) '\\'; |
73 | 1.35k | utf16_path[ 1 ] = (uint16_t) '.'; |
74 | 1.35k | utf16_path[ 2 ] = (uint16_t) '.'; |
75 | 1.35k | utf16_path[ 3 ] = (uint16_t) '\\'; |
76 | 1.35k | utf16_path[ 4 ] = (uint16_t) '.'; |
77 | 1.35k | utf16_path[ 5 ] = (uint16_t) '.'; |
78 | | |
79 | 1.35k | if( libbfio_memory_range_initialize( |
80 | 1.35k | &file_io_handle, |
81 | 1.35k | NULL ) != 1 ) |
82 | 0 | { |
83 | 0 | return( 0 ); |
84 | 0 | } |
85 | 1.35k | if( libbfio_memory_range_set( |
86 | 1.35k | file_io_handle, |
87 | 1.35k | (uint8_t *) data, |
88 | 1.35k | size, |
89 | 1.35k | NULL ) != 1 ) |
90 | 0 | { |
91 | 0 | goto on_error_libbfio; |
92 | 0 | } |
93 | 1.35k | if( libfsfat_check_volume_signature_file_io_handle( |
94 | 1.35k | file_io_handle, |
95 | 1.35k | NULL ) == -1 ) |
96 | 24 | { |
97 | 24 | goto on_error_libbfio; |
98 | 24 | } |
99 | 1.32k | if( libfsfat_volume_initialize( |
100 | 1.32k | &volume, |
101 | 1.32k | NULL ) != 1 ) |
102 | 0 | { |
103 | 0 | goto on_error_libbfio; |
104 | 0 | } |
105 | 1.32k | if( libfsfat_volume_open_file_io_handle( |
106 | 1.32k | volume, |
107 | 1.32k | file_io_handle, |
108 | 1.32k | LIBFSFAT_OPEN_READ, |
109 | 1.32k | NULL ) != 1 ) |
110 | 980 | { |
111 | 980 | goto on_error_libfsfat_volume; |
112 | 980 | } |
113 | 348 | if( libfsfat_volume_get_file_system_format( |
114 | 348 | volume, |
115 | 348 | &file_system_format, |
116 | 348 | NULL ) != 1 ) |
117 | 0 | { |
118 | 0 | goto on_error_libfsfat_volume; |
119 | 0 | } |
120 | 348 | if( libfsfat_volume_get_utf8_label_size( |
121 | 348 | volume, |
122 | 348 | &utf8_label_size, |
123 | 348 | NULL ) != 1 ) |
124 | 17 | { |
125 | 17 | goto on_error_libfsfat_volume; |
126 | 17 | } |
127 | 331 | if( utf8_label_size > 0 ) |
128 | 331 | { |
129 | 331 | if( libfsfat_volume_get_utf8_label( |
130 | 331 | volume, |
131 | 331 | utf8_label, |
132 | 331 | 512, |
133 | 331 | NULL ) == -1 ) |
134 | 0 | { |
135 | 0 | goto on_error_libfsfat_volume; |
136 | 0 | } |
137 | 331 | } |
138 | 331 | if( libfsfat_volume_get_utf16_label_size( |
139 | 331 | volume, |
140 | 331 | &utf16_label_size, |
141 | 331 | NULL ) != 1 ) |
142 | 0 | { |
143 | 0 | goto on_error_libfsfat_volume; |
144 | 0 | } |
145 | 331 | if( utf16_label_size > 0 ) |
146 | 331 | { |
147 | 331 | if( libfsfat_volume_get_utf16_label( |
148 | 331 | volume, |
149 | 331 | utf16_label, |
150 | 331 | 512, |
151 | 331 | NULL ) == -1 ) |
152 | 0 | { |
153 | 0 | goto on_error_libfsfat_volume; |
154 | 0 | } |
155 | 331 | } |
156 | 331 | if( libfsfat_volume_get_root_directory( |
157 | 331 | volume, |
158 | 331 | &file_entry, |
159 | 331 | NULL ) != 1 ) |
160 | 0 | { |
161 | 0 | goto on_error_libfsfat_volume; |
162 | 0 | } |
163 | 331 | if( libfsfat_file_entry_free( |
164 | 331 | &file_entry, |
165 | 331 | NULL ) != 1 ) |
166 | 0 | { |
167 | 0 | goto on_error_libfsfat_volume; |
168 | 0 | } |
169 | 331 | if( libfsfat_volume_get_file_entry_by_identifier( |
170 | 331 | volume, |
171 | 331 | 0, |
172 | 331 | &file_entry, |
173 | 331 | NULL ) != 1 ) |
174 | 0 | { |
175 | 0 | goto on_error_libfsfat_volume; |
176 | 0 | } |
177 | 331 | if( libfsfat_file_entry_free( |
178 | 331 | &file_entry, |
179 | 331 | NULL ) != 1 ) |
180 | 0 | { |
181 | 0 | goto on_error_libfsfat_volume; |
182 | 0 | } |
183 | 331 | result = libfsfat_volume_get_file_entry_by_utf8_path( |
184 | 331 | volume, |
185 | 331 | (const uint8_t *) "\\..\\..", |
186 | 331 | 6, |
187 | 331 | &file_entry, |
188 | 331 | NULL ); |
189 | | |
190 | 331 | if( result == -1 ) |
191 | 71 | { |
192 | 71 | goto on_error_libfsfat_volume; |
193 | 71 | } |
194 | 260 | if( result == 1 ) |
195 | 30 | { |
196 | 30 | if( libfsfat_file_entry_free( |
197 | 30 | &file_entry, |
198 | 30 | NULL ) != 1 ) |
199 | 0 | { |
200 | 0 | goto on_error_libfsfat_volume; |
201 | 0 | } |
202 | 30 | } |
203 | 260 | result = libfsfat_volume_get_file_entry_by_utf16_path( |
204 | 260 | volume, |
205 | 260 | utf16_path, |
206 | 260 | 6, |
207 | 260 | &file_entry, |
208 | 260 | NULL ); |
209 | | |
210 | 260 | if( result == -1 ) |
211 | 0 | { |
212 | 0 | goto on_error_libfsfat_volume; |
213 | 0 | } |
214 | 260 | if( result == 1 ) |
215 | 30 | { |
216 | 30 | if( libfsfat_file_entry_free( |
217 | 30 | &file_entry, |
218 | 30 | NULL ) != 1 ) |
219 | 0 | { |
220 | 0 | goto on_error_libfsfat_volume; |
221 | 0 | } |
222 | 30 | } |
223 | 260 | if( libfsfat_volume_signal_abort( |
224 | 260 | volume, |
225 | 260 | NULL ) != 1 ) |
226 | 0 | { |
227 | 0 | goto on_error_libfsfat_volume; |
228 | 0 | } |
229 | 260 | libfsfat_volume_close( |
230 | 260 | volume, |
231 | 260 | NULL ); |
232 | | |
233 | 1.32k | on_error_libfsfat_volume: |
234 | 1.32k | libfsfat_volume_free( |
235 | 1.32k | &volume, |
236 | 1.32k | NULL ); |
237 | | |
238 | 1.35k | on_error_libbfio: |
239 | 1.35k | libbfio_handle_free( |
240 | 1.35k | &file_io_handle, |
241 | 1.35k | NULL ); |
242 | | |
243 | 1.35k | return( 0 ); |
244 | 1.32k | } |
245 | | |
246 | | } /* extern "C" */ |