/src/libewf/ossfuzz/write_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for the libewf write path. |
3 | | * |
4 | | * The production handle_fuzzer only opens an EWF image for read. Acquisition |
5 | | * tools (ewfacquire) instead open for write, set format / compression / |
6 | | * sector geometry and forensic header values, then stream media bytes and |
7 | | * finalize. Those bytes are attacker-controlled when a tool converts or |
8 | | * re-wraps an untrusted image, and the write/finalize code is dark in the |
9 | | * production coverage report. |
10 | | * |
11 | | * Copyright (C) 2026 Google LLC |
12 | | * |
13 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
14 | | * you may not use this file except in compliance with the License. |
15 | | */ |
16 | | |
17 | | #include <stddef.h> |
18 | | #include <stdint.h> |
19 | | #include <stdio.h> |
20 | | #include <stdlib.h> |
21 | | #include <string.h> |
22 | | #include <unistd.h> |
23 | | |
24 | | #include <vector> |
25 | | |
26 | | #include <fuzzer/FuzzedDataProvider.h> |
27 | | |
28 | | /* Note that some of the OSS-Fuzz engines use C++ |
29 | | */ |
30 | | extern "C" { |
31 | | |
32 | | #include "ossfuzz_libewf.h" |
33 | | |
34 | | static const uint8_t kFormats[] = { |
35 | | LIBEWF_FORMAT_ENCASE5, |
36 | | LIBEWF_FORMAT_ENCASE6, |
37 | | LIBEWF_FORMAT_ENCASE7, |
38 | | LIBEWF_FORMAT_SMART, |
39 | | LIBEWF_FORMAT_FTK_IMAGER, |
40 | | LIBEWF_FORMAT_LINEN6, |
41 | | LIBEWF_FORMAT_EWF, |
42 | | LIBEWF_FORMAT_EWFX |
43 | | }; |
44 | | |
45 | | static const uint8_t kMediaTypes[] = { |
46 | | LIBEWF_MEDIA_TYPE_REMOVABLE, |
47 | | LIBEWF_MEDIA_TYPE_FIXED, |
48 | | LIBEWF_MEDIA_TYPE_OPTICAL, |
49 | | LIBEWF_MEDIA_TYPE_MEMORY |
50 | | }; |
51 | | |
52 | | static const int8_t kCompressionLevels[] = { |
53 | | LIBEWF_COMPRESSION_NONE, |
54 | | LIBEWF_COMPRESSION_FAST, |
55 | | LIBEWF_COMPRESSION_BEST |
56 | | }; |
57 | | |
58 | | static const uint32_t kSectorsPerChunk[] = { 1, 16, 64, 256 }; |
59 | | static const uint32_t kBytesPerSector[] = { 512, 1024, 2048, 4096 }; |
60 | | |
61 | | /* A failure is not fatal: some identifiers are rejected for some formats. |
62 | | */ |
63 | | static void set_header( |
64 | | libewf_handle_t *handle, |
65 | | const char *identifier, |
66 | | const uint8_t *value, |
67 | | size_t value_size ) |
68 | 6.92k | { |
69 | 6.92k | if( value_size == 0 ) |
70 | 3.76k | { |
71 | 3.76k | return; |
72 | 3.76k | } |
73 | 3.16k | libewf_handle_set_utf8_header_value( |
74 | 3.16k | handle, |
75 | 3.16k | (uint8_t *) identifier, |
76 | 3.16k | strlen( identifier ), |
77 | 3.16k | value, |
78 | 3.16k | value_size, |
79 | 3.16k | NULL ); |
80 | 3.16k | } |
81 | | |
82 | | #if defined( MEMORY_SANITIZER ) |
83 | | /* FuzzedDataProvider::ConsumeRandomLengthString builds its value with |
84 | | * std::string append operations that run in the uninstrumented system |
85 | | * libstdc++, leaving poisoned MSan shadow behind; reading the value is |
86 | | * then reported as uninitialized, even though it is well defined. |
87 | | * Under MSan only build the equivalent string into a plain buffer, |
88 | | * where each byte is stored by instrumented code. The escape handling |
89 | | * matches ConsumeRandomLengthString: "\\" becomes "\"", any other |
90 | | * escape ends the string. |
91 | | */ |
92 | | static std::vector<uint8_t> consume_random_length_string( |
93 | | FuzzedDataProvider &provider, |
94 | | size_t max_length ) |
95 | | { |
96 | | std::vector<uint8_t> value; |
97 | | size_t i = 0; |
98 | | |
99 | | while( ( i < max_length ) && ( provider.remaining_bytes() != 0 ) ) |
100 | | { |
101 | | uint8_t next = provider.ConsumeIntegral<uint8_t>(); |
102 | | |
103 | | if( ( next == '\\' ) && ( provider.remaining_bytes() != 0 ) ) |
104 | | { |
105 | | next = provider.ConsumeIntegral<uint8_t>(); |
106 | | |
107 | | if( next != '\\' ) |
108 | | { |
109 | | break; |
110 | | } |
111 | | } |
112 | | value.push_back( next ); |
113 | | i += 1; |
114 | | } |
115 | | return( value ); |
116 | | } |
117 | | #endif |
118 | | |
119 | | int LLVMFuzzerTestOneInput( |
120 | | const uint8_t *data, |
121 | | size_t size ) |
122 | 1.38k | { |
123 | 1.38k | FuzzedDataProvider provider( data, size ); |
124 | | |
125 | 1.38k | libewf_handle_t *handle = NULL; |
126 | 1.38k | char filename[ 64 ]; |
127 | 1.38k | char *filenames[ 1 ]; |
128 | | |
129 | 1.38k | uint8_t format = kFormats[ provider.ConsumeIntegralInRange<size_t>( |
130 | 1.38k | 0, ( sizeof( kFormats ) / sizeof( kFormats[ 0 ] ) ) - 1 ) ]; |
131 | 1.38k | uint8_t media_type = kMediaTypes[ provider.ConsumeIntegralInRange<size_t>( |
132 | 1.38k | 0, ( sizeof( kMediaTypes ) / sizeof( kMediaTypes[ 0 ] ) ) - 1 ) ]; |
133 | 1.38k | int8_t compression_level = kCompressionLevels[ |
134 | 1.38k | provider.ConsumeIntegralInRange<size_t>( |
135 | 1.38k | 0, ( sizeof( kCompressionLevels ) / sizeof( kCompressionLevels[ 0 ] ) ) - 1 ) ]; |
136 | 1.38k | uint32_t sectors_per_chunk = kSectorsPerChunk[ |
137 | 1.38k | provider.ConsumeIntegralInRange<size_t>( |
138 | 1.38k | 0, ( sizeof( kSectorsPerChunk ) / sizeof( kSectorsPerChunk[ 0 ] ) ) - 1 ) ]; |
139 | 1.38k | uint32_t bytes_per_sector = kBytesPerSector[ |
140 | 1.38k | provider.ConsumeIntegralInRange<size_t>( |
141 | 1.38k | 0, ( sizeof( kBytesPerSector ) / sizeof( kBytesPerSector[ 0 ] ) ) - 1 ) ]; |
142 | 1.38k | uint8_t media_flags = provider.ConsumeIntegralInRange<uint8_t>( 0, 0x0e ); |
143 | 1.38k | uint32_t error_granularity = provider.ConsumeIntegralInRange<uint32_t>( 1, 64 ); |
144 | 1.38k | int write_rounds = provider.ConsumeIntegralInRange<int>( 1, 4 ); |
145 | | |
146 | | #if defined( MEMORY_SANITIZER ) |
147 | | std::vector<uint8_t> case_number = consume_random_length_string( provider, 32 ); |
148 | | std::vector<uint8_t> description = consume_random_length_string( provider, 48 ); |
149 | | std::vector<uint8_t> examiner_name = consume_random_length_string( provider, 32 ); |
150 | | std::vector<uint8_t> notes = consume_random_length_string( provider, 48 ); |
151 | | #else |
152 | 1.38k | std::string case_number = provider.ConsumeRandomLengthString( 32 ); |
153 | 1.38k | std::string description = provider.ConsumeRandomLengthString( 48 ); |
154 | 1.38k | std::string examiner_name = provider.ConsumeRandomLengthString( 32 ); |
155 | 1.38k | std::string notes = provider.ConsumeRandomLengthString( 48 ); |
156 | 1.38k | #endif |
157 | | |
158 | | /* libewf_handle_open(WRITE) treats the name as a basename and appends |
159 | | * the segment extension itself. Passing ".E01" makes it look for a |
160 | | * second extension and the open fails. |
161 | | */ |
162 | 1.38k | snprintf( |
163 | 1.38k | filename, sizeof( filename ), "/tmp/ewf-%d", (int) getpid() ); |
164 | 1.38k | filenames[ 0 ] = filename; |
165 | | |
166 | 1.38k | if( libewf_handle_initialize( &handle, NULL ) != 1 ) |
167 | 0 | { |
168 | 0 | return( 0 ); |
169 | 0 | } |
170 | 1.38k | if( libewf_handle_open( |
171 | 1.38k | handle, filenames, 1, LIBEWF_OPEN_WRITE, NULL ) != 1 ) |
172 | 0 | { |
173 | 0 | goto on_error; |
174 | 0 | } |
175 | 1.38k | libewf_handle_set_format( handle, format, NULL ); |
176 | 1.38k | libewf_handle_set_bytes_per_sector( handle, bytes_per_sector, NULL ); |
177 | 1.38k | libewf_handle_set_sectors_per_chunk( handle, sectors_per_chunk, NULL ); |
178 | | #if defined( MEMORY_SANITIZER ) |
179 | | /* System zlib is not MSan-instrumented in this image, so a deflate |
180 | | * buffer looks uninitialized when it is written back. Keep compression |
181 | | * off under MSan only; ASan/UBSan still sweep the compression levels. |
182 | | */ |
183 | | libewf_handle_set_compression_values( |
184 | | handle, LIBEWF_COMPRESSION_NONE, 0, NULL ); |
185 | | #else |
186 | 1.38k | libewf_handle_set_compression_values( |
187 | 1.38k | handle, compression_level, 0, NULL ); |
188 | 1.38k | #endif |
189 | 1.38k | libewf_handle_set_media_type( handle, media_type, NULL ); |
190 | 1.38k | libewf_handle_set_media_flags( handle, media_flags, NULL ); |
191 | 1.38k | libewf_handle_set_error_granularity( handle, error_granularity, NULL ); |
192 | | /* Bound the image so write + finalize stay inside one input. |
193 | | */ |
194 | 1.38k | libewf_handle_set_media_size( |
195 | 1.38k | handle, |
196 | 1.38k | (size64_t) bytes_per_sector * (size64_t) ( 1 + ( write_rounds * 4 ) ), |
197 | 1.38k | NULL ); |
198 | 1.38k | libewf_handle_set_header_values_date_format( |
199 | 1.38k | handle, LIBEWF_DATE_FORMAT_ISO8601, NULL ); |
200 | | |
201 | 1.38k | set_header( handle, "case_number", ( const uint8_t * ) case_number.data(), case_number.size() ); |
202 | 1.38k | set_header( handle, "description", ( const uint8_t * ) description.data(), description.size() ); |
203 | 1.38k | set_header( handle, "examiner_name", ( const uint8_t * ) examiner_name.data(), examiner_name.size() ); |
204 | 1.38k | set_header( handle, "notes", ( const uint8_t * ) notes.data(), notes.size() ); |
205 | 1.38k | set_header( |
206 | 1.38k | handle, "acquiry_date", |
207 | 1.38k | ( const uint8_t * ) "2026-01-01 00:00:00", |
208 | 1.38k | strlen( "2026-01-01 00:00:00" ) ); |
209 | | |
210 | | /* Only set the hash when the input actually supplies 16 bytes. |
211 | | * ConsumeData zero-fills a short read, which MSan then reports as |
212 | | * an uninitialized value inside libewf_hash_values_parse_md5_hash. |
213 | | */ |
214 | 1.38k | if( provider.remaining_bytes() >= 16 ) |
215 | 619 | { |
216 | 619 | std::vector<uint8_t> md5 = provider.ConsumeBytes<uint8_t>( 16 ); |
217 | | |
218 | 619 | libewf_handle_set_md5_hash( handle, md5.data(), 16, NULL ); |
219 | 619 | } |
220 | 1.38k | if( provider.remaining_bytes() >= 16 ) |
221 | 598 | { |
222 | 598 | std::vector<uint8_t> guid_bytes = provider.ConsumeBytes<uint8_t>( 16 ); |
223 | | |
224 | 598 | libewf_handle_set_segment_file_set_identifier( |
225 | 598 | handle, guid_bytes.data(), 16, NULL ); |
226 | 598 | } |
227 | | |
228 | 4.15k | for( int i = 0; i < 2; i++ ) |
229 | 2.77k | { |
230 | 2.77k | uint64_t start = provider.ConsumeIntegralInRange<uint64_t>( 0, 1024 ); |
231 | 2.77k | uint64_t count = provider.ConsumeIntegralInRange<uint64_t>( 1, 32 ); |
232 | | |
233 | 2.77k | libewf_handle_append_session( handle, start, count, NULL ); |
234 | 2.77k | libewf_handle_append_track( handle, start, count, NULL ); |
235 | 2.77k | libewf_handle_append_acquiry_error( handle, start, count, NULL ); |
236 | 2.77k | } |
237 | 2.79k | for( int round = 0; round < write_rounds; round++ ) |
238 | 2.29k | { |
239 | 2.29k | size_t want = provider.ConsumeIntegralInRange<size_t>( 1, 2048 ); |
240 | 2.29k | std::vector<uint8_t> bytes = provider.ConsumeBytes<uint8_t>( want ); |
241 | | |
242 | 2.29k | if( bytes.empty() ) |
243 | 881 | { |
244 | 881 | break; |
245 | 881 | } |
246 | 1.41k | libewf_handle_write_buffer( |
247 | 1.41k | handle, bytes.data(), bytes.size(), NULL ); |
248 | 1.41k | } |
249 | 1.38k | libewf_handle_write_finalize( handle, NULL ); |
250 | 1.38k | libewf_handle_close( handle, NULL ); |
251 | | |
252 | 1.38k | on_error: |
253 | 1.38k | if( handle != NULL ) |
254 | 1.38k | { |
255 | 1.38k | libewf_handle_free( &handle, NULL ); |
256 | 1.38k | } |
257 | 1.38k | unlink( filename ); |
258 | 1.38k | { |
259 | 1.38k | char segment_name[ 72 ]; |
260 | | |
261 | 1.38k | snprintf( |
262 | 1.38k | segment_name, sizeof( segment_name ), "%s.E01", filename ); |
263 | 1.38k | unlink( segment_name ); |
264 | 1.38k | snprintf( |
265 | 1.38k | segment_name, sizeof( segment_name ), "%s.E02", filename ); |
266 | 1.38k | unlink( segment_name ); |
267 | 1.38k | snprintf( |
268 | 1.38k | segment_name, sizeof( segment_name ), "%s.s01", filename ); |
269 | 1.38k | unlink( segment_name ); |
270 | 1.38k | } |
271 | 1.38k | return( 0 ); |
272 | 1.38k | } |
273 | | |
274 | | } /* extern "C" */ |