/src/libfsntfs/ossfuzz/data_stream_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libfsntfs data_stream 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 | 4.97k | { |
50 | 4.97k | uint8_t buffer[ 512 ]; |
51 | 4.97k | uint8_t utf8_string[ 64 ]; |
52 | 4.97k | uint16_t utf16_string[ 32 ]; |
53 | | |
54 | 4.97k | libbfio_handle_t *file_io_handle = NULL; |
55 | 4.97k | libfsntfs_data_stream_t *data_stream = NULL; |
56 | 4.97k | libfsntfs_file_entry_t *file_entry = NULL; |
57 | 4.97k | libfsntfs_file_entry_t *root_directory = NULL; |
58 | 4.97k | libfsntfs_file_entry_t *sub_file_entry = NULL; |
59 | 4.97k | libfsntfs_volume_t *volume = NULL; |
60 | 4.97k | size64_t extent_size = 0; |
61 | 4.97k | size64_t stream_size = 0; |
62 | 4.97k | size_t utf8_string_size = 0; |
63 | 4.97k | size_t utf16_string_size = 0; |
64 | 4.97k | off64_t extent_offset = 0; |
65 | 4.97k | off64_t stream_offset = 0; |
66 | 4.97k | uint32_t extent_flags = 0; |
67 | 4.97k | int number_of_alternate_data_streams = 0; |
68 | 4.97k | int number_of_extents = 0; |
69 | 4.97k | int number_of_sub_file_entries = 0; |
70 | 4.97k | int read_iterator = 0; |
71 | | |
72 | 4.97k | if( libbfio_memory_range_initialize( |
73 | 4.97k | &file_io_handle, |
74 | 4.97k | NULL ) != 1 ) |
75 | 0 | { |
76 | 0 | return( 0 ); |
77 | 0 | } |
78 | 4.97k | if( libbfio_memory_range_set( |
79 | 4.97k | file_io_handle, |
80 | 4.97k | (uint8_t *) data, |
81 | 4.97k | size, |
82 | 4.97k | NULL ) != 1 ) |
83 | 0 | { |
84 | 0 | goto on_error_libbfio; |
85 | 0 | } |
86 | 4.97k | if( libfsntfs_volume_initialize( |
87 | 4.97k | &volume, |
88 | 4.97k | NULL ) != 1 ) |
89 | 0 | { |
90 | 0 | goto on_error_libbfio; |
91 | 0 | } |
92 | 4.97k | if( libfsntfs_volume_open_file_io_handle( |
93 | 4.97k | volume, |
94 | 4.97k | file_io_handle, |
95 | 4.97k | LIBFSNTFS_OPEN_READ, |
96 | 4.97k | NULL ) != 1 ) |
97 | 3.61k | { |
98 | 3.61k | goto on_error_libfsntfs_volume; |
99 | 3.61k | } |
100 | 1.35k | if( libfsntfs_volume_get_root_directory( |
101 | 1.35k | volume, |
102 | 1.35k | &root_directory, |
103 | 1.35k | NULL ) != 1 ) |
104 | 929 | { |
105 | 929 | goto on_error_libfsntfs_volume; |
106 | 929 | } |
107 | 429 | if( libfsntfs_file_entry_get_number_of_sub_file_entries( |
108 | 429 | root_directory, |
109 | 429 | &number_of_sub_file_entries, |
110 | 429 | NULL ) != 1 ) |
111 | 0 | { |
112 | 0 | goto on_error_libfsntfs_file_entry; |
113 | 0 | } |
114 | 429 | if( number_of_sub_file_entries > 0 ) |
115 | 144 | { |
116 | 144 | if( libfsntfs_file_entry_get_sub_file_entry_by_index( |
117 | 144 | root_directory, |
118 | 144 | 0, |
119 | 144 | &sub_file_entry, |
120 | 144 | NULL ) != 1 ) |
121 | 64 | { |
122 | 64 | goto on_error_libfsntfs_file_entry; |
123 | 64 | } |
124 | 80 | file_entry = sub_file_entry; |
125 | 80 | } |
126 | 285 | else |
127 | 285 | { |
128 | 285 | file_entry = root_directory; |
129 | 285 | } |
130 | 365 | if( libfsntfs_file_entry_get_number_of_alternate_data_streams( |
131 | 365 | file_entry, |
132 | 365 | &number_of_alternate_data_streams, |
133 | 365 | NULL ) != 1 ) |
134 | 0 | { |
135 | 0 | goto on_error_libfsntfs_file_entry; |
136 | 0 | } |
137 | 365 | if( number_of_alternate_data_streams > 0 ) |
138 | 259 | { |
139 | 259 | if( libfsntfs_file_entry_get_alternate_data_stream_by_index( |
140 | 259 | file_entry, |
141 | 259 | 0, |
142 | 259 | &data_stream, |
143 | 259 | NULL ) != 1 ) |
144 | 152 | { |
145 | 152 | goto on_error_libfsntfs_data_stream; |
146 | 152 | } |
147 | 107 | if( libfsntfs_data_stream_get_utf8_name_size( |
148 | 107 | data_stream, |
149 | 107 | &utf8_string_size, |
150 | 107 | NULL ) == -1 ) |
151 | 0 | { |
152 | 0 | goto on_error_libfsntfs_data_stream; |
153 | 0 | } |
154 | 107 | if( utf8_string_size > 0 ) |
155 | 107 | { |
156 | 107 | if( libfsntfs_data_stream_get_utf8_name( |
157 | 107 | data_stream, |
158 | 107 | utf8_string, |
159 | 107 | 64, |
160 | 107 | NULL ) == -1 ) |
161 | 3 | { |
162 | 3 | goto on_error_libfsntfs_data_stream; |
163 | 3 | } |
164 | 107 | } |
165 | 104 | if( libfsntfs_data_stream_get_utf16_name_size( |
166 | 104 | data_stream, |
167 | 104 | &utf16_string_size, |
168 | 104 | NULL ) == -1 ) |
169 | 0 | { |
170 | 0 | goto on_error_libfsntfs_data_stream; |
171 | 0 | } |
172 | 104 | if( utf16_string_size > 0 ) |
173 | 104 | { |
174 | 104 | if( libfsntfs_data_stream_get_utf16_name( |
175 | 104 | data_stream, |
176 | 104 | utf16_string, |
177 | 104 | 32, |
178 | 104 | NULL ) == -1 ) |
179 | 0 | { |
180 | 0 | goto on_error_libfsntfs_data_stream; |
181 | 0 | } |
182 | 104 | } |
183 | 104 | if( libfsntfs_data_stream_get_offset( |
184 | 104 | data_stream, |
185 | 104 | &stream_offset, |
186 | 104 | NULL ) != 1 ) |
187 | 0 | { |
188 | 0 | goto on_error_libfsntfs_data_stream; |
189 | 0 | } |
190 | 104 | if( libfsntfs_data_stream_get_size( |
191 | 104 | data_stream, |
192 | 104 | &stream_size, |
193 | 104 | NULL ) != 1 ) |
194 | 0 | { |
195 | 0 | goto on_error_libfsntfs_data_stream; |
196 | 0 | } |
197 | 104 | for( read_iterator = 0; |
198 | 4.55k | read_iterator < 128; |
199 | 4.45k | read_iterator++ ) |
200 | 4.52k | { |
201 | 4.52k | if( stream_offset >= stream_size ) |
202 | 56 | { |
203 | 56 | break; |
204 | 56 | } |
205 | 4.47k | if( libfsntfs_data_stream_read_buffer_at_offset( |
206 | 4.47k | data_stream, |
207 | 4.47k | buffer, |
208 | 4.47k | 497, |
209 | 4.47k | stream_offset, |
210 | 4.47k | NULL ) == -1 ) |
211 | 21 | { |
212 | 21 | goto on_error_libfsntfs_data_stream; |
213 | 21 | } |
214 | 4.45k | stream_offset += 497; |
215 | 4.45k | } |
216 | 83 | if( libfsntfs_data_stream_get_number_of_extents( |
217 | 83 | data_stream, |
218 | 83 | &number_of_extents, |
219 | 83 | NULL ) != 1 ) |
220 | 0 | { |
221 | 0 | goto on_error_libfsntfs_data_stream; |
222 | 0 | } |
223 | 83 | if( number_of_extents > 0 ) |
224 | 24 | { |
225 | 24 | if( libfsntfs_data_stream_get_extent_by_index( |
226 | 24 | data_stream, |
227 | 24 | 0, |
228 | 24 | &extent_offset, |
229 | 24 | &extent_size, |
230 | 24 | &extent_flags, |
231 | 24 | NULL ) != 1 ) |
232 | 0 | { |
233 | 0 | goto on_error_libfsntfs_data_stream; |
234 | 0 | } |
235 | 24 | } |
236 | 259 | on_error_libfsntfs_data_stream: |
237 | 259 | libfsntfs_data_stream_free( |
238 | 259 | &data_stream, |
239 | 259 | NULL ); |
240 | 259 | } |
241 | 429 | on_error_libfsntfs_file_entry: |
242 | 429 | if( sub_file_entry != NULL ) |
243 | 80 | { |
244 | 80 | libfsntfs_file_entry_free( |
245 | 80 | &sub_file_entry, |
246 | 80 | NULL ); |
247 | 80 | } |
248 | 429 | libfsntfs_file_entry_free( |
249 | 429 | &root_directory, |
250 | 429 | NULL ); |
251 | 429 | libfsntfs_volume_close( |
252 | 429 | volume, |
253 | 429 | NULL ); |
254 | | |
255 | 4.97k | on_error_libfsntfs_volume: |
256 | 4.97k | libfsntfs_volume_free( |
257 | 4.97k | &volume, |
258 | 4.97k | NULL ); |
259 | | |
260 | 4.97k | on_error_libbfio: |
261 | 4.97k | libbfio_handle_free( |
262 | 4.97k | &file_io_handle, |
263 | 4.97k | NULL ); |
264 | | |
265 | 4.97k | return( 0 ); |
266 | 4.97k | } |
267 | | |
268 | | } /* extern "C" */ |