/src/libsmraw/ossfuzz/media_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libsmraw media 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 | 65 | { |
50 | 65 | char segment_filename[ 32 ]; |
51 | | |
52 | 65 | libbfio_handle_t *file_io_handle = NULL; |
53 | 65 | libbfio_pool_t *file_io_pool = NULL; |
54 | 65 | libsmraw_handle_t *handle = NULL; |
55 | 65 | size64_t media_size = 0; |
56 | 65 | size64_t maximum_segment_size = 0; |
57 | 65 | size_t segment_filename_length = 0; |
58 | 65 | uint8_t media_flags = 0; |
59 | 65 | uint8_t media_type = 0; |
60 | 65 | uint32_t bytes_per_sector = 0; |
61 | 65 | uint8_t value_8bit = 0; |
62 | 65 | uint32_t value_32bit = 0; |
63 | 65 | size_t filename_index = 0; |
64 | 65 | int entry_index = 0; |
65 | | |
66 | 65 | if( size < 2 ) |
67 | 1 | { |
68 | 1 | return( 0 ); |
69 | 1 | } |
70 | 64 | media_size = (size64_t) size; |
71 | 64 | maximum_segment_size = (size64_t) ( size / 2 ); |
72 | | |
73 | 64 | if( size > 2 ) |
74 | 63 | { |
75 | 63 | bytes_per_sector = (uint32_t) data[ 0 ] * 512; |
76 | 63 | media_type = data[ 1 ] % 5; |
77 | 63 | media_flags = data[ 2 ] & 1; |
78 | 63 | } |
79 | 64 | segment_filename_length = size - 1; |
80 | | |
81 | 64 | if( segment_filename_length > 31 ) |
82 | 50 | { |
83 | 50 | segment_filename_length = 31; |
84 | 50 | } |
85 | 64 | for( filename_index = 0; |
86 | 1.80k | filename_index < segment_filename_length; |
87 | 1.74k | filename_index++ ) |
88 | 1.74k | { |
89 | 1.74k | segment_filename[ filename_index ] = (char ) data[ filename_index ]; |
90 | 1.74k | } |
91 | 64 | segment_filename[ segment_filename_length ] = 0; |
92 | | |
93 | 64 | if( libbfio_memory_range_initialize( |
94 | 64 | &file_io_handle, |
95 | 64 | NULL ) != 1 ) |
96 | 0 | { |
97 | 0 | return( 0 ); |
98 | 0 | } |
99 | 64 | if( libbfio_memory_range_set( |
100 | 64 | file_io_handle, |
101 | 64 | (uint8_t *) data, |
102 | 64 | size, |
103 | 64 | NULL ) != 1 ) |
104 | 0 | { |
105 | 0 | goto on_error_libbfio; |
106 | 0 | } |
107 | 64 | if( libbfio_pool_initialize( |
108 | 64 | &file_io_pool, |
109 | 64 | 0, |
110 | 64 | 0, |
111 | 64 | NULL ) != 1 ) |
112 | 0 | { |
113 | 0 | goto on_error_libbfio; |
114 | 0 | } |
115 | 64 | if( libbfio_pool_append_handle( |
116 | 64 | file_io_pool, |
117 | 64 | &entry_index, |
118 | 64 | file_io_handle, |
119 | 64 | LIBBFIO_OPEN_WRITE, |
120 | 64 | NULL ) != 1 ) |
121 | 0 | { |
122 | 0 | goto on_error_libbfio; |
123 | 0 | } |
124 | | /* The file IO pool takes over management of the file IO handle |
125 | | */ |
126 | 64 | file_io_handle = NULL; |
127 | | |
128 | 64 | if( libsmraw_handle_initialize( |
129 | 64 | &handle, |
130 | 64 | NULL ) != 1 ) |
131 | 0 | { |
132 | 0 | goto on_error_libbfio; |
133 | 0 | } |
134 | 64 | if( libsmraw_handle_open_file_io_pool( |
135 | 64 | handle, |
136 | 64 | file_io_pool, |
137 | 64 | LIBSMRAW_OPEN_WRITE, |
138 | 64 | NULL ) != 1 ) |
139 | 0 | { |
140 | 0 | goto on_error_libsmraw; |
141 | 0 | } |
142 | 64 | if( libsmraw_handle_set_media_size( |
143 | 64 | handle, |
144 | 64 | media_size, |
145 | 64 | NULL ) != 1 ) |
146 | 0 | { |
147 | 0 | goto on_error_libsmraw; |
148 | 0 | } |
149 | 64 | if( libsmraw_handle_get_media_size( |
150 | 64 | handle, |
151 | 64 | &media_size, |
152 | 64 | NULL ) != 1 ) |
153 | 0 | { |
154 | 0 | goto on_error_libsmraw; |
155 | 0 | } |
156 | 64 | if( libsmraw_handle_set_bytes_per_sector( |
157 | 64 | handle, |
158 | 64 | bytes_per_sector, |
159 | 64 | NULL ) != 1 ) |
160 | 64 | { |
161 | 64 | goto on_error_libsmraw; |
162 | 64 | } |
163 | 0 | if( libsmraw_handle_get_bytes_per_sector( |
164 | 0 | handle, |
165 | 0 | &value_32bit, |
166 | 0 | NULL ) != 1 ) |
167 | 0 | { |
168 | 0 | goto on_error_libsmraw; |
169 | 0 | } |
170 | 0 | if( libsmraw_handle_set_media_type( |
171 | 0 | handle, |
172 | 0 | media_type, |
173 | 0 | NULL ) != 1 ) |
174 | 0 | { |
175 | 0 | goto on_error_libsmraw; |
176 | 0 | } |
177 | 0 | if( libsmraw_handle_get_media_type( |
178 | 0 | handle, |
179 | 0 | &value_8bit, |
180 | 0 | NULL ) != 1 ) |
181 | 0 | { |
182 | 0 | goto on_error_libsmraw; |
183 | 0 | } |
184 | 0 | if( libsmraw_handle_set_media_flags( |
185 | 0 | handle, |
186 | 0 | media_flags, |
187 | 0 | NULL ) != 1 ) |
188 | 0 | { |
189 | 0 | goto on_error_libsmraw; |
190 | 0 | } |
191 | 0 | if( libsmraw_handle_get_media_flags( |
192 | 0 | handle, |
193 | 0 | &value_8bit, |
194 | 0 | NULL ) != 1 ) |
195 | 0 | { |
196 | 0 | goto on_error_libsmraw; |
197 | 0 | } |
198 | 0 | if( libsmraw_handle_set_maximum_segment_size( |
199 | 0 | handle, |
200 | 0 | maximum_segment_size, |
201 | 0 | NULL ) != 1 ) |
202 | 0 | { |
203 | 0 | goto on_error_libsmraw; |
204 | 0 | } |
205 | 0 | if( libsmraw_handle_get_maximum_segment_size( |
206 | 0 | handle, |
207 | 0 | &maximum_segment_size, |
208 | 0 | NULL ) != 1 ) |
209 | 0 | { |
210 | 0 | goto on_error_libsmraw; |
211 | 0 | } |
212 | 0 | if( libsmraw_handle_set_segment_filename( |
213 | 0 | handle, |
214 | 0 | segment_filename, |
215 | 0 | segment_filename_length, |
216 | 0 | NULL ) != 1 ) |
217 | 0 | { |
218 | 0 | goto on_error_libsmraw; |
219 | 0 | } |
220 | 0 | if( libsmraw_handle_get_segment_filename_size( |
221 | 0 | handle, |
222 | 0 | &segment_filename_length, |
223 | 0 | NULL ) != 1 ) |
224 | 0 | { |
225 | 0 | goto on_error_libsmraw; |
226 | 0 | } |
227 | 0 | if( ( segment_filename_length > 0 ) |
228 | 0 | && ( segment_filename_length <= 32 ) ) |
229 | 0 | { |
230 | 0 | if( libsmraw_handle_get_segment_filename( |
231 | 0 | handle, |
232 | 0 | segment_filename, |
233 | 0 | 32, |
234 | 0 | NULL ) != 1 ) |
235 | 0 | { |
236 | 0 | goto on_error_libsmraw; |
237 | 0 | } |
238 | 0 | } |
239 | 0 | libsmraw_handle_close( |
240 | 0 | handle, |
241 | 0 | NULL ); |
242 | |
|
243 | 64 | on_error_libsmraw: |
244 | 64 | libsmraw_handle_free( |
245 | 64 | &handle, |
246 | 64 | NULL ); |
247 | | |
248 | 64 | on_error_libbfio: |
249 | | /* Note that on error the handle still has a reference to file_io_pool |
250 | | * that will be closed. Therefore the file IO pool and handle need to |
251 | | * be freed after closing or freeing the handle. |
252 | | */ |
253 | 64 | if( file_io_pool != NULL ) |
254 | 64 | { |
255 | 64 | libbfio_pool_free( |
256 | 64 | &file_io_pool, |
257 | 64 | NULL ); |
258 | 64 | } |
259 | 64 | if( file_io_handle != NULL ) |
260 | 0 | { |
261 | 0 | libbfio_handle_free( |
262 | 0 | &file_io_handle, |
263 | | NULL ); |
264 | 0 | } |
265 | 64 | return( 0 ); |
266 | 64 | } |
267 | | |
268 | | } /* extern "C" */ |