/src/libregf/ossfuzz/file_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libregf file type |
3 | | * |
4 | | * Copyright (C) 2009-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 | | #include <stdlib.h> |
26 | | |
27 | | /* Note that some of the OSS-Fuzz engines use C++ |
28 | | */ |
29 | | extern "C" { |
30 | | |
31 | | #include "ossfuzz_libbfio.h" |
32 | | #include "ossfuzz_libregf.h" |
33 | | |
34 | | #if !defined( LIBREGF_HAVE_BFIO ) |
35 | | |
36 | | /* Opens a file using a Basic File IO (bfio) handle |
37 | | * Returns 1 if successful or -1 on error |
38 | | */ |
39 | | LIBREGF_EXTERN \ |
40 | | int libregf_file_open_file_io_handle( |
41 | | libregf_file_t *file, |
42 | | libbfio_handle_t *file_io_handle, |
43 | | int access_flags, |
44 | | libregf_error_t **error ); |
45 | | |
46 | | #endif /* !defined( LIBREGF_HAVE_BFIO ) */ |
47 | | |
48 | | /* Limits on the traversal and buffer sizes to bound the time a single input takes |
49 | | */ |
50 | 2.82k | #define FUZZER_LOOKUP_STRING_LIMIT ( (size_t) 0x4000 ) |
51 | | |
52 | | /* The ASCII codepages supported by libregf_file_set_ascii_codepage |
53 | | */ |
54 | | static const int value_codepages[] = { |
55 | | LIBREGF_CODEPAGE_ASCII, |
56 | | LIBREGF_CODEPAGE_WINDOWS_874, |
57 | | LIBREGF_CODEPAGE_WINDOWS_932, |
58 | | LIBREGF_CODEPAGE_WINDOWS_936, |
59 | | LIBREGF_CODEPAGE_WINDOWS_949, |
60 | | LIBREGF_CODEPAGE_WINDOWS_950, |
61 | | LIBREGF_CODEPAGE_WINDOWS_1250, |
62 | | LIBREGF_CODEPAGE_WINDOWS_1251, |
63 | | LIBREGF_CODEPAGE_WINDOWS_1252, |
64 | | LIBREGF_CODEPAGE_WINDOWS_1253, |
65 | | LIBREGF_CODEPAGE_WINDOWS_1254, |
66 | | LIBREGF_CODEPAGE_WINDOWS_1255, |
67 | | LIBREGF_CODEPAGE_WINDOWS_1256, |
68 | | LIBREGF_CODEPAGE_WINDOWS_1257, |
69 | | LIBREGF_CODEPAGE_WINDOWS_1258, |
70 | | }; |
71 | | |
72 | | static const size_t value_number_of_codepages = |
73 | | sizeof( value_codepages ) / sizeof( value_codepages[ 0 ] ); |
74 | | |
75 | | int LLVMFuzzerTestOneInput( |
76 | | const uint8_t *data, |
77 | | size_t size ) |
78 | 1.66k | { |
79 | 1.66k | libbfio_handle_t *file_io_handle = NULL; |
80 | 1.66k | libregf_file_t *file = NULL; |
81 | 1.66k | libregf_key_t *key = NULL; |
82 | 1.66k | uint16_t *utf16_lookup_string = NULL; |
83 | 1.66k | size_t codepage_index = 0; |
84 | 1.66k | size_t index = 0; |
85 | 1.66k | size_t utf8_lookup_string_length = 0; |
86 | 1.66k | size_t utf16_lookup_string_length = 0; |
87 | 1.66k | uint32_t file_type = 0; |
88 | 1.66k | uint32_t major_version = 0; |
89 | 1.66k | uint32_t minor_version = 0; |
90 | 1.66k | int ascii_codepage = 0; |
91 | | |
92 | 1.66k | if( libbfio_memory_range_initialize( |
93 | 1.66k | &file_io_handle, |
94 | 1.66k | NULL ) != 1 ) |
95 | 0 | { |
96 | 0 | return( 0 ); |
97 | 0 | } |
98 | 1.66k | if( libbfio_memory_range_set( |
99 | 1.66k | file_io_handle, |
100 | 1.66k | (uint8_t *) data, |
101 | 1.66k | size, |
102 | 1.66k | NULL ) != 1 ) |
103 | 0 | { |
104 | 0 | goto on_error_libbfio; |
105 | 0 | } |
106 | 1.66k | if( libregf_file_initialize( |
107 | 1.66k | &file, |
108 | 1.66k | NULL ) != 1 ) |
109 | 0 | { |
110 | 0 | goto on_error_libbfio; |
111 | 0 | } |
112 | 1.66k | if( libregf_file_open_file_io_handle( |
113 | 1.66k | file, |
114 | 1.66k | file_io_handle, |
115 | 1.66k | LIBREGF_OPEN_READ, |
116 | 1.66k | NULL ) != 1 ) |
117 | 417 | { |
118 | 417 | goto on_error_libregf_file; |
119 | 417 | } |
120 | 1.24k | libregf_file_get_type( |
121 | 1.24k | file, |
122 | 1.24k | &file_type, |
123 | 1.24k | NULL ); |
124 | | |
125 | 1.24k | libregf_file_get_ascii_codepage( |
126 | 1.24k | file, |
127 | 1.24k | &ascii_codepage, |
128 | 1.24k | NULL ); |
129 | | |
130 | 1.24k | libregf_file_get_format_version( |
131 | 1.24k | file, |
132 | 1.24k | &major_version, |
133 | 1.24k | &minor_version, |
134 | 1.24k | NULL ); |
135 | | |
136 | 1.24k | libregf_file_is_corrupted( |
137 | 1.24k | file, |
138 | 1.24k | NULL ); |
139 | | |
140 | | /* Cycles through the supported ASCII codepages to exercise the |
141 | | * codepage handling in the library |
142 | | */ |
143 | 1.24k | for( codepage_index = 0; |
144 | 19.9k | codepage_index < value_number_of_codepages; |
145 | 18.6k | codepage_index++ ) |
146 | 18.6k | { |
147 | 18.6k | libregf_file_set_ascii_codepage( |
148 | 18.6k | file, |
149 | 18.6k | value_codepages[ codepage_index ], |
150 | 18.6k | NULL ); |
151 | | |
152 | 18.6k | libregf_file_get_ascii_codepage( |
153 | 18.6k | file, |
154 | 18.6k | &ascii_codepage, |
155 | 18.6k | NULL ); |
156 | 18.6k | } |
157 | | /* Sets an unsupported codepage to exercise the error path |
158 | | */ |
159 | 1.24k | libregf_file_set_ascii_codepage( |
160 | 1.24k | file, |
161 | 1.24k | 65001, |
162 | 1.24k | NULL ); |
163 | 1.24k | utf8_lookup_string_length = size; |
164 | | |
165 | 1.24k | if( utf8_lookup_string_length > FUZZER_LOOKUP_STRING_LIMIT ) |
166 | 179 | { |
167 | 179 | utf8_lookup_string_length = FUZZER_LOOKUP_STRING_LIMIT; |
168 | 179 | } |
169 | 1.24k | utf16_lookup_string_length = ( size / 2 ); |
170 | | |
171 | 1.24k | if( utf16_lookup_string_length > FUZZER_LOOKUP_STRING_LIMIT ) |
172 | 155 | { |
173 | 155 | utf16_lookup_string_length = FUZZER_LOOKUP_STRING_LIMIT; |
174 | 155 | } |
175 | 1.24k | if( utf16_lookup_string_length > 0 ) |
176 | 1.24k | { |
177 | 1.24k | utf16_lookup_string = (uint16_t *) malloc( utf16_lookup_string_length * sizeof( uint16_t ) ); |
178 | | |
179 | 1.24k | if( utf16_lookup_string != NULL ) |
180 | 1.24k | { |
181 | 1.24k | for( index = 0; |
182 | 5.16M | index < utf16_lookup_string_length; |
183 | 5.16M | index++ ) |
184 | 5.16M | { |
185 | 5.16M | utf16_lookup_string[ index ] = (uint16_t) ( data[ index * 2 ] | ( (uint16_t) data[ index * 2 + 1 ] << 8 ) ); |
186 | 5.16M | } |
187 | 1.24k | } |
188 | 1.24k | } |
189 | | /* Retrieves a key using a path derived from the fuzz data. |
190 | | * The data is not necessarily valid UTF-8 or UTF-16 |
191 | | * which exercises the validation and conversion error paths |
192 | | * in the library |
193 | | */ |
194 | 1.24k | if( utf8_lookup_string_length > 0 ) |
195 | 1.24k | { |
196 | 1.24k | libregf_file_get_key_by_utf8_path( |
197 | 1.24k | file, |
198 | 1.24k | data, |
199 | 1.24k | utf8_lookup_string_length, |
200 | 1.24k | &key, |
201 | 1.24k | NULL ); |
202 | | |
203 | 1.24k | if( key != NULL ) |
204 | 0 | { |
205 | 0 | libregf_key_free( |
206 | 0 | &key, |
207 | 0 | NULL ); |
208 | 0 | } |
209 | 1.24k | key = NULL; |
210 | 1.24k | } |
211 | 1.24k | if( ( utf16_lookup_string != NULL ) |
212 | 1.24k | && ( utf16_lookup_string_length > 0 ) ) |
213 | 1.24k | { |
214 | 1.24k | libregf_file_get_key_by_utf16_path( |
215 | 1.24k | file, |
216 | 1.24k | utf16_lookup_string, |
217 | 1.24k | utf16_lookup_string_length, |
218 | 1.24k | &key, |
219 | 1.24k | NULL ); |
220 | | |
221 | 1.24k | if( key != NULL ) |
222 | 0 | { |
223 | 0 | libregf_key_free( |
224 | 0 | &key, |
225 | 0 | NULL ); |
226 | 0 | } |
227 | 1.24k | key = NULL; |
228 | 1.24k | } |
229 | | #if defined( LIBREGF_HAVE_BFIO ) |
230 | | libregf_check_file_signature_file_io_handle( |
231 | | file_io_handle, |
232 | | NULL ); |
233 | | #endif |
234 | 1.24k | libregf_file_close( |
235 | 1.24k | file, |
236 | 1.24k | NULL ); |
237 | | |
238 | 1.66k | on_error_libregf_file: |
239 | 1.66k | free( utf16_lookup_string ); |
240 | 1.66k | libregf_file_free( |
241 | 1.66k | &file, |
242 | 1.66k | NULL ); |
243 | | |
244 | 1.66k | on_error_libbfio: |
245 | 1.66k | libbfio_handle_free( |
246 | 1.66k | &file_io_handle, |
247 | 1.66k | NULL ); |
248 | | |
249 | 1.66k | return( 0 ); |
250 | 1.66k | } |
251 | | |
252 | | } /* extern "C" */ |