/src/libolecf/ossfuzz/file_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libolecf file type |
3 | | * |
4 | | * Copyright (C) 2008-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_libolecf.h" |
31 | | |
32 | | #if !defined( LIBOLECF_HAVE_BFIO ) |
33 | | |
34 | | /* Opens a file using a Basic File IO (bfio) handle |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | LIBOLECF_EXTERN \ |
38 | | int libolecf_file_open_file_io_handle( |
39 | | libolecf_file_t *file, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libolecf_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBOLECF_HAVE_BFIO ) */ |
45 | | |
46 | | /* Limits on the number of unallocated blocks to query |
47 | | */ |
48 | 0 | #define FUZZER_UNALLOCATED_BLOCKS_LIMIT 8 |
49 | | |
50 | | /* Limits the size of the query strings used to look up items |
51 | | */ |
52 | 538 | #define FUZZER_PATH_SIZE_LIMIT ( (size_t) 0x100 ) |
53 | | |
54 | | int LLVMFuzzerTestOneInput( |
55 | | const uint8_t *data, |
56 | | size_t size ) |
57 | 1.19k | { |
58 | 1.19k | libbfio_handle_t *file_io_handle = NULL; |
59 | 1.19k | libolecf_file_t *file = NULL; |
60 | 1.19k | libolecf_item_t *item = NULL; |
61 | 1.19k | size32_t sector_size = 0; |
62 | 1.19k | size32_t short_sector_size = 0; |
63 | 1.19k | uint16_t major_version = 0; |
64 | 1.19k | uint16_t minor_version = 0; |
65 | 1.19k | int ascii_codepage = 0; |
66 | 1.19k | int number_of_unallocated_blocks = 0; |
67 | 1.19k | int unallocated_block_index = 0; |
68 | 1.19k | off64_t offset = 0; |
69 | 1.19k | size64_t unallocated_block_size = 0; |
70 | 1.19k | size_t query_size = 0; |
71 | | |
72 | 1.19k | if( libbfio_memory_range_initialize( |
73 | 1.19k | &file_io_handle, |
74 | 1.19k | NULL ) != 1 ) |
75 | 0 | { |
76 | 0 | return( 0 ); |
77 | 0 | } |
78 | 1.19k | if( libbfio_memory_range_set( |
79 | 1.19k | file_io_handle, |
80 | 1.19k | (uint8_t *) data, |
81 | 1.19k | size, |
82 | 1.19k | NULL ) != 1 ) |
83 | 0 | { |
84 | 0 | goto on_error_libbfio; |
85 | 0 | } |
86 | | #if defined( LIBOLECF_HAVE_BFIO ) |
87 | | if( libolecf_check_file_signature_file_io_handle( |
88 | | file_io_handle, |
89 | | NULL ) != 1 ) |
90 | | { |
91 | | goto on_error_libbfio; |
92 | | } |
93 | | #endif /* defined( LIBOLECF_HAVE_BFIO ) */ |
94 | 1.19k | if( libolecf_file_initialize( |
95 | 1.19k | &file, |
96 | 1.19k | NULL ) != 1 ) |
97 | 0 | { |
98 | 0 | goto on_error_libbfio; |
99 | 0 | } |
100 | 1.19k | if( libolecf_file_open_file_io_handle( |
101 | 1.19k | file, |
102 | 1.19k | file_io_handle, |
103 | 1.19k | LIBOLECF_OPEN_READ, |
104 | 1.19k | NULL ) != 1 ) |
105 | 921 | { |
106 | 921 | goto on_error_libolecf_file; |
107 | 921 | } |
108 | | /* Use the value as a sink to keep it live |
109 | | */ |
110 | 269 | ascii_codepage = 0; |
111 | | |
112 | 269 | libolecf_file_get_sector_size( |
113 | 269 | file, |
114 | 269 | §or_size, |
115 | 269 | NULL ); |
116 | 269 | libolecf_file_get_short_sector_size( |
117 | 269 | file, |
118 | 269 | &short_sector_size, |
119 | 269 | NULL ); |
120 | 269 | libolecf_file_get_ascii_codepage( |
121 | 269 | file, |
122 | 269 | &ascii_codepage, |
123 | 269 | NULL ); |
124 | | /* Attempt to set an unsupported and a supported ASCII codepage |
125 | | * to trigger the validation |
126 | | */ |
127 | 269 | libolecf_file_set_ascii_codepage( |
128 | 269 | file, |
129 | 269 | 0x004e, |
130 | 269 | NULL ); |
131 | 269 | libolecf_file_set_ascii_codepage( |
132 | 269 | file, |
133 | 269 | LIBOLECF_CODEPAGE_WINDOWS_1252, |
134 | 269 | NULL ); |
135 | 269 | libolecf_file_get_ascii_codepage( |
136 | 269 | file, |
137 | 269 | &ascii_codepage, |
138 | 269 | NULL ); |
139 | 269 | libolecf_file_get_format_version( |
140 | 269 | file, |
141 | 269 | &major_version, |
142 | 269 | &minor_version, |
143 | 269 | NULL ); |
144 | 269 | libolecf_file_signal_abort( |
145 | 269 | file, |
146 | 269 | NULL ); |
147 | 269 | if( libolecf_file_get_number_of_unallocated_blocks( |
148 | 269 | file, |
149 | 269 | &number_of_unallocated_blocks, |
150 | 269 | NULL ) == 1 ) |
151 | 269 | { |
152 | 269 | for( unallocated_block_index = 0; |
153 | 269 | unallocated_block_index < number_of_unallocated_blocks; |
154 | 269 | unallocated_block_index++ ) |
155 | 0 | { |
156 | 0 | offset = 0; |
157 | 0 | unallocated_block_size = 0; |
158 | |
|
159 | 0 | if( unallocated_block_index >= FUZZER_UNALLOCATED_BLOCKS_LIMIT ) |
160 | 0 | { |
161 | 0 | break; |
162 | 0 | } |
163 | 0 | libolecf_file_get_unallocated_block( |
164 | 0 | file, |
165 | 0 | unallocated_block_index, |
166 | 0 | &offset, |
167 | 0 | &unallocated_block_size, |
168 | 0 | NULL ); |
169 | 0 | } |
170 | 269 | } |
171 | 269 | if( ( data != NULL ) |
172 | 269 | && ( size > 0 ) ) |
173 | 269 | { |
174 | 269 | query_size = size; |
175 | | |
176 | 269 | if( query_size > FUZZER_PATH_SIZE_LIMIT ) |
177 | 269 | { |
178 | 269 | query_size = FUZZER_PATH_SIZE_LIMIT; |
179 | 269 | } |
180 | | /* Use the value as a sink to keep it live |
181 | | */ |
182 | 269 | ascii_codepage = data[ 0 ]; |
183 | 269 | libolecf_file_get_item_by_utf8_path( |
184 | 269 | file, |
185 | 269 | data, |
186 | 269 | query_size, |
187 | 269 | &item, |
188 | 269 | NULL ); |
189 | 269 | if( item != NULL ) |
190 | 0 | { |
191 | 0 | libolecf_item_free( |
192 | 0 | &item, |
193 | 0 | NULL ); |
194 | 0 | } |
195 | 269 | item = NULL; |
196 | 269 | if( query_size >= 2 ) |
197 | 269 | { |
198 | 269 | libolecf_file_get_item_by_utf16_path( |
199 | 269 | file, |
200 | 269 | (const uint16_t *) data, |
201 | 269 | query_size / 2, |
202 | 269 | &item, |
203 | 269 | NULL ); |
204 | 269 | if( item != NULL ) |
205 | 0 | { |
206 | 0 | libolecf_item_free( |
207 | 0 | &item, |
208 | 0 | NULL ); |
209 | 0 | } |
210 | 269 | } |
211 | 269 | } |
212 | 269 | libolecf_file_get_root_item( |
213 | 269 | file, |
214 | 269 | &item, |
215 | 269 | NULL ); |
216 | 269 | if( item != NULL ) |
217 | 155 | { |
218 | 155 | libolecf_item_free( |
219 | 155 | &item, |
220 | 155 | NULL ); |
221 | 155 | } |
222 | 269 | libolecf_file_close( |
223 | 269 | file, |
224 | 269 | NULL ); |
225 | | /* Attempt to open with unsupported access flags |
226 | | * to trigger the validation |
227 | | */ |
228 | 269 | libolecf_file_open_file_io_handle( |
229 | 269 | file, |
230 | 269 | file_io_handle, |
231 | 269 | LIBOLECF_OPEN_WRITE, |
232 | 269 | NULL ); |
233 | 269 | libolecf_file_open_file_io_handle( |
234 | 269 | file, |
235 | 269 | file_io_handle, |
236 | 269 | 0, |
237 | 269 | NULL ); |
238 | 269 | libolecf_file_close( |
239 | 269 | file, |
240 | 269 | NULL ); |
241 | | |
242 | 1.19k | on_error_libolecf_file: |
243 | 1.19k | libolecf_file_free( |
244 | 1.19k | &file, |
245 | 1.19k | NULL ); |
246 | | |
247 | 1.19k | on_error_libbfio: |
248 | 1.19k | libbfio_handle_free( |
249 | 1.19k | &file_io_handle, |
250 | 1.19k | NULL ); |
251 | | |
252 | 1.19k | return( 0 ); |
253 | 1.19k | } |
254 | | |
255 | | } /* extern "C" */ |