/src/libsmraw/ossfuzz/information_value_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libsmraw information value 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_libsmraw.h" |
31 | | |
32 | | #if !defined( LIBSMRAW_HAVE_BFIO ) |
33 | | |
34 | | /* Opens a set of storage media RAW files using a Basic File IO (bfio) pool |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | LIBSMRAW_EXTERN \ |
38 | | int libsmraw_handle_open_file_io_pool( |
39 | | libsmraw_handle_t *handle, |
40 | | libbfio_pool_t *file_io_pool, |
41 | | int access_flags, |
42 | | libsmraw_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBSMRAW_HAVE_BFIO ) */ |
45 | | |
46 | | int LLVMFuzzerTestOneInput( |
47 | | const uint8_t *data, |
48 | | size_t size ) |
49 | 1.10k | { |
50 | 1.10k | char string[ 64 ]; |
51 | 1.10k | uint8_t utf8_string[ 512 ]; |
52 | 1.10k | uint16_t utf16_string[ 256 ]; |
53 | | |
54 | 1.10k | libbfio_handle_t *file_io_handle = NULL; |
55 | 1.10k | libbfio_pool_t *file_io_pool = NULL; |
56 | 1.10k | libsmraw_handle_t *handle = NULL; |
57 | 1.10k | size_t remaining_size = 0; |
58 | 1.10k | size_t identifier_length = 0; |
59 | 1.10k | size_t identifier_2_length = 0; |
60 | 1.10k | size_t identifier_size = 0; |
61 | 1.10k | size_t value_length = 0; |
62 | 1.10k | size_t value_2_length = 0; |
63 | 1.10k | size_t utf8_string_size = 0; |
64 | 1.10k | size_t utf16_string_size = 0; |
65 | 1.10k | int entry_index = 0; |
66 | 1.10k | int information_value_index = 0; |
67 | 1.10k | int number_of_information_values = 0; |
68 | | |
69 | 1.10k | if( size < 2 ) |
70 | 2 | { |
71 | 2 | return( 0 ); |
72 | 2 | } |
73 | | |
74 | | /* Note that the set functions interpret the passed string length as the |
75 | | * size excluding the end-of-string character and read one character beyond |
76 | | * the passed length, therefore one byte beyond each string needs to be |
77 | | * readable. One byte of the input data is reserved for that purpose. |
78 | | */ |
79 | 1.10k | remaining_size = size - 1; |
80 | 1.10k | identifier_length = remaining_size; |
81 | | |
82 | 1.10k | if( identifier_length > 16 ) |
83 | 1.02k | { |
84 | 1.02k | identifier_length = 16; |
85 | 1.02k | } |
86 | 1.10k | remaining_size -= identifier_length; |
87 | | |
88 | 1.10k | value_length = remaining_size; |
89 | | |
90 | 1.10k | if( value_length > 480 ) |
91 | 510 | { |
92 | 510 | value_length = 480; |
93 | 510 | } |
94 | 1.10k | remaining_size -= value_length; |
95 | | |
96 | 1.10k | if( remaining_size > 1 ) |
97 | 508 | { |
98 | 508 | identifier_2_length = remaining_size - 1; |
99 | | |
100 | 508 | if( identifier_2_length > 16 ) |
101 | 489 | { |
102 | 489 | identifier_2_length = 16; |
103 | 489 | } |
104 | 508 | value_2_length = remaining_size - identifier_2_length - 1; |
105 | | |
106 | 508 | if( value_2_length > 240 ) |
107 | 148 | { |
108 | 148 | value_2_length = 240; |
109 | 148 | } |
110 | 508 | } |
111 | 1.10k | if( libbfio_memory_range_initialize( |
112 | 1.10k | &file_io_handle, |
113 | 1.10k | NULL ) != 1 ) |
114 | 0 | { |
115 | 0 | return( 0 ); |
116 | 0 | } |
117 | 1.10k | if( libbfio_memory_range_set( |
118 | 1.10k | file_io_handle, |
119 | 1.10k | (uint8_t *) data, |
120 | 1.10k | size, |
121 | 1.10k | NULL ) != 1 ) |
122 | 0 | { |
123 | 0 | goto on_error_libbfio; |
124 | 0 | } |
125 | 1.10k | if( libbfio_pool_initialize( |
126 | 1.10k | &file_io_pool, |
127 | 1.10k | 0, |
128 | 1.10k | 0, |
129 | 1.10k | NULL ) != 1 ) |
130 | 0 | { |
131 | 0 | goto on_error_libbfio; |
132 | 0 | } |
133 | 1.10k | if( libbfio_pool_append_handle( |
134 | 1.10k | file_io_pool, |
135 | 1.10k | &entry_index, |
136 | 1.10k | file_io_handle, |
137 | 1.10k | LIBBFIO_OPEN_WRITE, |
138 | 1.10k | NULL ) != 1 ) |
139 | 0 | { |
140 | 0 | goto on_error_libbfio; |
141 | 0 | } |
142 | | /* The file IO pool takes over management of the file IO handle |
143 | | */ |
144 | 1.10k | file_io_handle = NULL; |
145 | | |
146 | 1.10k | if( libsmraw_handle_initialize( |
147 | 1.10k | &handle, |
148 | 1.10k | NULL ) != 1 ) |
149 | 0 | { |
150 | 0 | goto on_error_libbfio; |
151 | 0 | } |
152 | 1.10k | if( libsmraw_handle_open_file_io_pool( |
153 | 1.10k | handle, |
154 | 1.10k | file_io_pool, |
155 | 1.10k | LIBSMRAW_OPEN_WRITE, |
156 | 1.10k | NULL ) != 1 ) |
157 | 0 | { |
158 | 0 | goto on_error_libsmraw; |
159 | 0 | } |
160 | 1.10k | if( libsmraw_handle_set_utf8_information_value( |
161 | 1.10k | handle, |
162 | 1.10k | (uint8_t *) data, |
163 | 1.10k | identifier_length, |
164 | 1.10k | (uint8_t *) ( data + identifier_length ), |
165 | 1.10k | value_length, |
166 | 1.10k | NULL ) != 1 ) |
167 | 338 | { |
168 | 338 | goto on_error_libsmraw; |
169 | 338 | } |
170 | 768 | if( ( identifier_2_length > 0 ) |
171 | 368 | && ( value_2_length > 0 ) ) |
172 | 357 | { |
173 | 357 | size_t value_index = 0; |
174 | | |
175 | 357 | for( value_index = 0; |
176 | 11.6k | value_index < value_2_length; |
177 | 11.3k | value_index++ ) |
178 | 11.3k | { |
179 | 11.3k | utf16_string[ value_index ] = (uint16_t ) data[ identifier_length + value_length + identifier_2_length + value_index ]; |
180 | 11.3k | } |
181 | 357 | utf16_string[ value_2_length ] = 0; |
182 | | |
183 | 357 | if( libsmraw_handle_set_utf16_information_value( |
184 | 357 | handle, |
185 | 357 | (uint8_t *) ( data + identifier_length + value_length ), |
186 | 357 | identifier_2_length, |
187 | 357 | (uint16_t *) utf16_string, |
188 | 357 | value_2_length, |
189 | 357 | NULL ) != 1 ) |
190 | 0 | { |
191 | 0 | goto on_error_libsmraw; |
192 | 0 | } |
193 | 357 | } |
194 | 768 | if( libsmraw_handle_get_number_of_information_values( |
195 | 768 | handle, |
196 | 768 | &number_of_information_values, |
197 | 768 | NULL ) != 1 ) |
198 | 0 | { |
199 | 0 | goto on_error_libsmraw; |
200 | 0 | } |
201 | 768 | for( information_value_index = 0; |
202 | 1.87k | information_value_index < number_of_information_values; |
203 | 1.10k | information_value_index++ ) |
204 | 1.10k | { |
205 | 1.10k | identifier_size = 0; |
206 | | |
207 | 1.10k | if( libsmraw_handle_get_information_value_identifier_size( |
208 | 1.10k | handle, |
209 | 1.10k | information_value_index, |
210 | 1.10k | &identifier_size, |
211 | 1.10k | NULL ) != 1 ) |
212 | 0 | { |
213 | 0 | goto on_error_libsmraw; |
214 | 0 | } |
215 | 1.10k | if( ( identifier_size > 0 ) |
216 | 1.10k | && ( identifier_size < 64 ) ) |
217 | 1.10k | { |
218 | 1.10k | if( libsmraw_handle_get_information_value_identifier( |
219 | 1.10k | handle, |
220 | 1.10k | information_value_index, |
221 | 1.10k | (uint8_t *) string, |
222 | 1.10k | identifier_size, |
223 | 1.10k | NULL ) != 1 ) |
224 | 0 | { |
225 | 0 | goto on_error_libsmraw; |
226 | 0 | } |
227 | 1.10k | } |
228 | 1.10k | } |
229 | 768 | if( libsmraw_handle_get_utf8_information_value_size( |
230 | 768 | handle, |
231 | 768 | (uint8_t *) data, |
232 | 768 | identifier_length, |
233 | 768 | &utf8_string_size, |
234 | 768 | NULL ) == -1 ) |
235 | 0 | { |
236 | 0 | goto on_error_libsmraw; |
237 | 0 | } |
238 | 768 | if( libsmraw_handle_get_utf8_information_value( |
239 | 768 | handle, |
240 | 768 | (uint8_t *) data, |
241 | 768 | identifier_length, |
242 | 768 | (uint8_t *) utf8_string, |
243 | 768 | 512, |
244 | 768 | NULL ) == -1 ) |
245 | 0 | { |
246 | 0 | goto on_error_libsmraw; |
247 | 0 | } |
248 | 768 | if( libsmraw_handle_get_utf16_information_value_size( |
249 | 768 | handle, |
250 | 768 | (uint8_t *) data, |
251 | 768 | identifier_length, |
252 | 768 | &utf16_string_size, |
253 | 768 | NULL ) == -1 ) |
254 | 0 | { |
255 | 0 | goto on_error_libsmraw; |
256 | 0 | } |
257 | 768 | if( libsmraw_handle_get_utf16_information_value( |
258 | 768 | handle, |
259 | 768 | (uint8_t *) data, |
260 | 768 | identifier_length, |
261 | 768 | (uint16_t *) utf16_string, |
262 | 768 | 256, |
263 | 768 | NULL ) == -1 ) |
264 | 359 | { |
265 | 359 | goto on_error_libsmraw; |
266 | 359 | } |
267 | 409 | libsmraw_handle_close( |
268 | 409 | handle, |
269 | 409 | NULL ); |
270 | | |
271 | 1.10k | on_error_libsmraw: |
272 | 1.10k | libsmraw_handle_free( |
273 | 1.10k | &handle, |
274 | 1.10k | NULL ); |
275 | | |
276 | 1.10k | on_error_libbfio: |
277 | | /* Note that on error the handle still has a reference to file_io_pool |
278 | | * that will be closed. Therefore the file IO pool and handle need to |
279 | | * be freed after closing or freeing the handle. |
280 | | */ |
281 | 1.10k | if( file_io_pool != NULL ) |
282 | 1.10k | { |
283 | 1.10k | libbfio_pool_free( |
284 | 1.10k | &file_io_pool, |
285 | 1.10k | NULL ); |
286 | 1.10k | } |
287 | 1.10k | if( file_io_handle != NULL ) |
288 | 0 | { |
289 | 0 | libbfio_handle_free( |
290 | 0 | &file_io_handle, |
291 | | NULL ); |
292 | 0 | } |
293 | 1.10k | return( 0 ); |
294 | 1.10k | } |
295 | | |
296 | | } /* extern "C" */ |