/src/libewf/ossfuzz/data_chunk_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libewf data chunk type |
3 | | * |
4 | | * Copyright (C) 2006-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 | | #include <stdlib.h> |
25 | | |
26 | | /* Note that some of the OSS-Fuzz engines use C++ |
27 | | */ |
28 | | extern "C" { |
29 | | |
30 | | #include "ossfuzz_libbfio.h" |
31 | | #include "ossfuzz_libewf.h" |
32 | | |
33 | | #if !defined( LIBEWF_HAVE_BFIO ) |
34 | | |
35 | | /* Opens a set of EWF file(s) using a Basic File IO (bfio) pool |
36 | | * Returns 1 if successful or -1 on error |
37 | | */ |
38 | | LIBEWF_EXTERN \ |
39 | | int libewf_handle_open_file_io_pool( |
40 | | libewf_handle_t *handle, |
41 | | libbfio_pool_t *file_io_pool, |
42 | | int access_flags, |
43 | | libewf_error_t **error ); |
44 | | |
45 | | #endif /* !defined( LIBEWF_HAVE_BFIO ) */ |
46 | | |
47 | | int LLVMFuzzerTestOneInput( |
48 | | const uint8_t *data, |
49 | | size_t size ) |
50 | 1.87k | { |
51 | 1.87k | uint8_t *chunk_buffer = NULL; |
52 | 1.87k | libbfio_handle_t *file_io_handle = NULL; |
53 | 1.87k | libbfio_pool_t *file_io_pool = NULL; |
54 | 1.87k | libewf_handle_t *handle = NULL; |
55 | 1.87k | libewf_data_chunk_t *data_chunk = NULL; |
56 | 1.87k | ssize_t read_count = 0; |
57 | 1.87k | size_t chunk_buffer_size = 0; |
58 | 1.87k | uint32_t bytes_per_sector = 0; |
59 | 1.87k | uint32_t sectors_per_chunk = 0; |
60 | 1.87k | int entry_index = 0; |
61 | 1.87k | int read_iterator = 0; |
62 | | |
63 | 1.87k | if( libbfio_memory_range_initialize( |
64 | 1.87k | &file_io_handle, |
65 | 1.87k | NULL ) != 1 ) |
66 | 0 | { |
67 | 0 | return( 0 ); |
68 | 0 | } |
69 | 1.87k | if( libbfio_memory_range_set( |
70 | 1.87k | file_io_handle, |
71 | 1.87k | (uint8_t *) data, |
72 | 1.87k | size, |
73 | 1.87k | NULL ) != 1 ) |
74 | 0 | { |
75 | 0 | goto on_error_libbfio; |
76 | 0 | } |
77 | 1.87k | if( libbfio_pool_initialize( |
78 | 1.87k | &file_io_pool, |
79 | 1.87k | 0, |
80 | 1.87k | 0, |
81 | 1.87k | NULL ) != 1 ) |
82 | 0 | { |
83 | 0 | goto on_error_libbfio; |
84 | 0 | } |
85 | 1.87k | if( libbfio_pool_append_handle( |
86 | 1.87k | file_io_pool, |
87 | 1.87k | &entry_index, |
88 | 1.87k | file_io_handle, |
89 | 1.87k | LIBBFIO_OPEN_READ, |
90 | 1.87k | NULL ) != 1 ) |
91 | 0 | { |
92 | 0 | goto on_error_libbfio; |
93 | 0 | } |
94 | | /* The file IO pool takes over management of the file IO handle |
95 | | */ |
96 | 1.87k | file_io_handle = NULL; |
97 | | |
98 | 1.87k | if( libewf_handle_initialize( |
99 | 1.87k | &handle, |
100 | 1.87k | NULL ) != 1 ) |
101 | 0 | { |
102 | 0 | goto on_error_libbfio; |
103 | 0 | } |
104 | 1.87k | if( libewf_handle_open_file_io_pool( |
105 | 1.87k | handle, |
106 | 1.87k | file_io_pool, |
107 | 1.87k | LIBEWF_OPEN_READ, |
108 | 1.87k | NULL ) != 1 ) |
109 | 1.24k | { |
110 | 1.24k | goto on_error_libewf; |
111 | 1.24k | } |
112 | 635 | if( libewf_handle_get_data_chunk( |
113 | 635 | handle, |
114 | 635 | &data_chunk, |
115 | 635 | NULL ) != 1 ) |
116 | 0 | { |
117 | 0 | goto on_error_libewf; |
118 | 0 | } |
119 | | /* libewf_data_chunk_read_buffer requires a buffer that can hold the |
120 | | * entire uncompressed chunk. Size it from the chunk geometry the handle |
121 | | * reports and bound it so a malformed image cannot force a huge |
122 | | * allocation. |
123 | | */ |
124 | 635 | if( libewf_handle_get_bytes_per_sector( |
125 | 635 | handle, |
126 | 635 | &bytes_per_sector, |
127 | 635 | NULL ) != 1 || bytes_per_sector == 0 ) |
128 | 0 | { |
129 | 0 | bytes_per_sector = 512; |
130 | 0 | } |
131 | 635 | if( libewf_handle_get_sectors_per_chunk( |
132 | 635 | handle, |
133 | 635 | §ors_per_chunk, |
134 | 635 | NULL ) != 1 || sectors_per_chunk == 0 ) |
135 | 0 | { |
136 | 0 | sectors_per_chunk = 64; |
137 | 0 | } |
138 | 635 | chunk_buffer_size = (size_t) bytes_per_sector * (size_t) sectors_per_chunk; |
139 | | |
140 | 635 | if( chunk_buffer_size < 512 ) |
141 | 1 | { |
142 | 1 | chunk_buffer_size = 512; |
143 | 1 | } |
144 | | /* Upper bound at 64 MiB: covers any realistic chunk while keeping the |
145 | | * allocation bounded for attacker-supplied geometry. |
146 | | */ |
147 | 635 | if( chunk_buffer_size > ( 64 * 1024 * 1024 ) ) |
148 | 3 | { |
149 | 3 | chunk_buffer_size = (size_t) ( 64 * 1024 * 1024 ); |
150 | 3 | } |
151 | 635 | chunk_buffer = (uint8_t *) malloc( chunk_buffer_size ); |
152 | | |
153 | 635 | if( chunk_buffer == NULL ) |
154 | 0 | { |
155 | 0 | goto on_error_libewf_data_chunk; |
156 | 0 | } |
157 | 635 | for( read_iterator = 0; |
158 | 21.1k | read_iterator < 128; |
159 | 20.5k | read_iterator++ ) |
160 | 21.1k | { |
161 | 21.1k | read_count = libewf_handle_read_data_chunk( |
162 | 21.1k | handle, |
163 | 21.1k | data_chunk, |
164 | 21.1k | NULL ); |
165 | | |
166 | 21.1k | if( read_count == -1 ) |
167 | 446 | { |
168 | 446 | break; |
169 | 446 | } |
170 | 20.6k | if( read_count == 0 ) |
171 | 137 | { |
172 | 137 | break; |
173 | 137 | } |
174 | 20.5k | libewf_data_chunk_is_corrupted( |
175 | 20.5k | data_chunk, |
176 | 20.5k | NULL ); |
177 | 20.5k | if( libewf_data_chunk_read_buffer( |
178 | 20.5k | data_chunk, |
179 | 20.5k | chunk_buffer, |
180 | 20.5k | chunk_buffer_size, |
181 | 20.5k | NULL ) == -1 ) |
182 | 2 | { |
183 | 2 | goto on_error_libewf_data_chunk; |
184 | 2 | } |
185 | 20.5k | } |
186 | 635 | on_error_libewf_data_chunk: |
187 | 635 | if( chunk_buffer != NULL ) |
188 | 635 | { |
189 | 635 | free( chunk_buffer ); |
190 | 635 | } |
191 | 635 | if( libewf_data_chunk_free( |
192 | 635 | &data_chunk, |
193 | 635 | NULL ) != 1 ) |
194 | 0 | { |
195 | 0 | goto on_error_libewf; |
196 | 0 | } |
197 | 635 | libewf_handle_close( |
198 | 635 | handle, |
199 | 635 | NULL ); |
200 | | |
201 | 1.87k | on_error_libewf: |
202 | 1.87k | libewf_handle_free( |
203 | 1.87k | &handle, |
204 | 1.87k | NULL ); |
205 | | |
206 | 1.87k | on_error_libbfio: |
207 | | /* Note that on error the handle still has a reference to file_io_pool |
208 | | * that will be closed. Therefore the file IO pool and handle need to |
209 | | * be freed after closing or freeing the handle. |
210 | | */ |
211 | 1.87k | if( file_io_pool != NULL ) |
212 | 1.87k | { |
213 | 1.87k | libbfio_pool_free( |
214 | 1.87k | &file_io_pool, |
215 | 1.87k | NULL ); |
216 | 1.87k | } |
217 | 1.87k | if( file_io_handle != NULL ) |
218 | 0 | { |
219 | 0 | libbfio_handle_free( |
220 | 0 | &file_io_handle, |
221 | | NULL ); |
222 | 0 | } |
223 | 1.87k | return( 0 ); |
224 | 1.87k | } |
225 | | |
226 | | } /* extern "C" */ |