/src/libvhdi/ossfuzz/file_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libvhdi file type |
3 | | * |
4 | | * Copyright (C) 2012-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_libvhdi.h" |
31 | | |
32 | | #if !defined( LIBVHDI_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 | | LIBVHDI_EXTERN \ |
38 | | int libvhdi_file_open_file_io_handle( |
39 | | libvhdi_file_t *file, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libvhdi_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBVHDI_HAVE_BFIO ) */ |
45 | | |
46 | | int LLVMFuzzerTestOneInput( |
47 | | const uint8_t *data, |
48 | | size_t size ) |
49 | 1.76k | { |
50 | 1.76k | uint8_t buffer[ 512 ]; |
51 | 1.76k | uint8_t guid[ 16 ]; |
52 | 1.76k | uint8_t utf8_string[ 64 ]; |
53 | 1.76k | uint16_t utf16_string[ 64 ]; |
54 | | |
55 | 1.76k | libbfio_handle_t *file_io_handle = NULL; |
56 | 1.76k | libvhdi_file_t *file = NULL; |
57 | 1.76k | off64_t media_offset = 0; |
58 | 1.76k | size64_t media_size = 0; |
59 | 1.76k | size_t string_size = 0; |
60 | 1.76k | uint32_t value_32bit = 0; |
61 | 1.76k | uint16_t major_version = 0; |
62 | 1.76k | uint16_t minor_version = 0; |
63 | 1.76k | int file_type = 0; |
64 | 1.76k | int read_iterator = 0; |
65 | | |
66 | 1.76k | if( libbfio_memory_range_initialize( |
67 | 1.76k | &file_io_handle, |
68 | 1.76k | NULL ) != 1 ) |
69 | 0 | { |
70 | 0 | return( 0 ); |
71 | 0 | } |
72 | 1.76k | if( libbfio_memory_range_set( |
73 | 1.76k | file_io_handle, |
74 | 1.76k | (uint8_t *) data, |
75 | 1.76k | size, |
76 | 1.76k | NULL ) != 1 ) |
77 | 0 | { |
78 | 0 | goto on_error_libbfio; |
79 | 0 | } |
80 | 1.76k | if( libvhdi_file_initialize( |
81 | 1.76k | &file, |
82 | 1.76k | NULL ) != 1 ) |
83 | 0 | { |
84 | 0 | goto on_error_libbfio; |
85 | 0 | } |
86 | 1.76k | if( libvhdi_file_open_file_io_handle( |
87 | 1.76k | file, |
88 | 1.76k | file_io_handle, |
89 | 1.76k | LIBVHDI_OPEN_READ, |
90 | 1.76k | NULL ) != 1 ) |
91 | 1.73k | { |
92 | 1.73k | goto on_error_libvhdi; |
93 | 1.73k | } |
94 | 31 | if( libvhdi_file_get_file_type( |
95 | 31 | file, |
96 | 31 | &file_type, |
97 | 31 | NULL ) != 1 ) |
98 | 0 | { |
99 | 0 | goto on_error_libvhdi; |
100 | 0 | } |
101 | 31 | if( libvhdi_file_get_format_version( |
102 | 31 | file, |
103 | 31 | &major_version, |
104 | 31 | &minor_version, |
105 | 31 | NULL ) != 1 ) |
106 | 0 | { |
107 | 0 | goto on_error_libvhdi; |
108 | 0 | } |
109 | 31 | if( libvhdi_file_get_disk_type( |
110 | 31 | file, |
111 | 31 | &value_32bit, |
112 | 31 | NULL ) != 1 ) |
113 | 0 | { |
114 | 0 | goto on_error_libvhdi; |
115 | 0 | } |
116 | 31 | if( libvhdi_file_get_bytes_per_sector( |
117 | 31 | file, |
118 | 31 | &value_32bit, |
119 | 31 | NULL ) != 1 ) |
120 | 0 | { |
121 | 0 | goto on_error_libvhdi; |
122 | 0 | } |
123 | 31 | if( libvhdi_file_get_identifier( |
124 | 31 | file, |
125 | 31 | guid, |
126 | 31 | 16, |
127 | 31 | NULL ) != 1 ) |
128 | 0 | { |
129 | 0 | goto on_error_libvhdi; |
130 | 0 | } |
131 | 31 | if( libvhdi_file_get_parent_identifier( |
132 | 31 | file, |
133 | 31 | guid, |
134 | 31 | 16, |
135 | 31 | NULL ) == -1 ) |
136 | 0 | { |
137 | 0 | goto on_error_libvhdi; |
138 | 0 | } |
139 | 31 | if( libvhdi_file_get_utf8_parent_filename_size( |
140 | 31 | file, |
141 | 31 | &string_size, |
142 | 31 | NULL ) == -1 ) |
143 | 3 | { |
144 | 3 | goto on_error_libvhdi; |
145 | 3 | } |
146 | 28 | if( libvhdi_file_get_utf8_parent_filename( |
147 | 28 | file, |
148 | 28 | utf8_string, |
149 | 28 | 64, |
150 | 28 | NULL ) == -1 ) |
151 | 7 | { |
152 | 7 | goto on_error_libvhdi; |
153 | 7 | } |
154 | 21 | if( libvhdi_file_get_utf16_parent_filename_size( |
155 | 21 | file, |
156 | 21 | &string_size, |
157 | 21 | NULL ) == -1 ) |
158 | 0 | { |
159 | 0 | goto on_error_libvhdi; |
160 | 0 | } |
161 | 21 | if( libvhdi_file_get_utf16_parent_filename( |
162 | 21 | file, |
163 | 21 | utf16_string, |
164 | 21 | 64, |
165 | 21 | NULL ) == -1 ) |
166 | 0 | { |
167 | 0 | goto on_error_libvhdi; |
168 | 0 | } |
169 | 21 | if( libvhdi_file_get_media_size( |
170 | 21 | file, |
171 | 21 | &media_size, |
172 | 21 | NULL ) != 1 ) |
173 | 0 | { |
174 | 0 | goto on_error_libvhdi; |
175 | 0 | } |
176 | 21 | for( read_iterator = 0; |
177 | 23 | read_iterator < 128; |
178 | 21 | read_iterator++ ) |
179 | 23 | { |
180 | 23 | if( media_offset >= media_size ) |
181 | 1 | { |
182 | 1 | break; |
183 | 1 | } |
184 | 22 | if( libvhdi_file_read_buffer_at_offset( |
185 | 22 | file, |
186 | 22 | buffer, |
187 | 22 | 497, |
188 | 22 | media_offset, |
189 | 22 | NULL ) == -1 ) |
190 | 20 | { |
191 | 20 | goto on_error_libvhdi; |
192 | 20 | } |
193 | 2 | media_offset += 497; |
194 | 2 | } |
195 | 1 | libvhdi_file_close( |
196 | 1 | file, |
197 | 1 | NULL ); |
198 | | |
199 | 1.76k | on_error_libvhdi: |
200 | 1.76k | libvhdi_file_free( |
201 | 1.76k | &file, |
202 | 1.76k | NULL ); |
203 | | |
204 | 1.76k | on_error_libbfio: |
205 | 1.76k | libbfio_handle_free( |
206 | 1.76k | &file_io_handle, |
207 | 1.76k | NULL ); |
208 | | |
209 | 1.76k | return( 0 ); |
210 | 1.76k | } |
211 | | |
212 | | } /* extern "C" */ |
213 | | |