/src/libvmdk/ossfuzz/handle_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libvmdk file type |
3 | | * |
4 | | * Copyright (C) 2009-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_libvmdk.h" |
31 | | |
32 | | #if !defined( LIBVMDK_HAVE_BFIO ) |
33 | | |
34 | | /* Opens a handle using a Basic File IO (bfio) handle |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | LIBVMDK_EXTERN \ |
38 | | int libvmdk_handle_open_file_io_handle( |
39 | | libvmdk_handle_t *handle, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libvmdk_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBVMDK_HAVE_BFIO ) */ |
45 | | |
46 | | int LLVMFuzzerTestOneInput( |
47 | | const uint8_t *data, |
48 | | size_t size ) |
49 | 8.40k | { |
50 | 8.40k | uint8_t buffer[ 512 ]; |
51 | 8.40k | uint8_t utf8_string[ 64 ]; |
52 | 8.40k | uint16_t utf16_string[ 64 ]; |
53 | | |
54 | 8.40k | libbfio_handle_t *file_io_handle = NULL; |
55 | 8.40k | libvmdk_handle_t *handle = NULL; |
56 | 8.40k | off64_t media_offset = 0; |
57 | 8.40k | size64_t media_size = 0; |
58 | 8.40k | size_t string_size = 0; |
59 | 8.40k | uint32_t value_32bit = 0; |
60 | 8.40k | int disk_type = 0; |
61 | 8.40k | int number_of_extents = 0; |
62 | 8.40k | int read_iterator = 0; |
63 | | |
64 | 8.40k | if( libbfio_memory_range_initialize( |
65 | 8.40k | &file_io_handle, |
66 | 8.40k | NULL ) != 1 ) |
67 | 0 | { |
68 | 0 | return( 0 ); |
69 | 0 | } |
70 | 8.40k | if( libbfio_memory_range_set( |
71 | 8.40k | file_io_handle, |
72 | 8.40k | (uint8_t *) data, |
73 | 8.40k | size, |
74 | 8.40k | NULL ) != 1 ) |
75 | 0 | { |
76 | 0 | goto on_error_libbfio; |
77 | 0 | } |
78 | 8.40k | if( libvmdk_handle_initialize( |
79 | 8.40k | &handle, |
80 | 8.40k | NULL ) != 1 ) |
81 | 0 | { |
82 | 0 | goto on_error_libbfio; |
83 | 0 | } |
84 | 8.40k | if( libvmdk_handle_open_file_io_handle( |
85 | 8.40k | handle, |
86 | 8.40k | file_io_handle, |
87 | 8.40k | LIBVMDK_OPEN_READ, |
88 | 8.40k | NULL ) != 1 ) |
89 | 7.55k | { |
90 | 7.55k | goto on_error_libvmdk; |
91 | 7.55k | } |
92 | 859 | if( libvmdk_handle_get_disk_type( |
93 | 859 | handle, |
94 | 859 | &disk_type, |
95 | 859 | NULL ) != 1 ) |
96 | 0 | { |
97 | 0 | goto on_error_libvmdk; |
98 | 0 | } |
99 | 859 | if( libvmdk_handle_get_content_identifier( |
100 | 859 | handle, |
101 | 859 | &value_32bit, |
102 | 859 | NULL ) != 1 ) |
103 | 0 | { |
104 | 0 | goto on_error_libvmdk; |
105 | 0 | } |
106 | 859 | if( libvmdk_handle_get_parent_content_identifier( |
107 | 859 | handle, |
108 | 859 | &value_32bit, |
109 | 859 | NULL ) == -1 ) |
110 | 0 | { |
111 | 0 | goto on_error_libvmdk; |
112 | 0 | } |
113 | 859 | if( libvmdk_handle_get_utf8_parent_filename_size( |
114 | 859 | handle, |
115 | 859 | &string_size, |
116 | 859 | NULL ) == -1 ) |
117 | 94 | { |
118 | 94 | goto on_error_libvmdk; |
119 | 94 | } |
120 | 765 | if( libvmdk_handle_get_utf8_parent_filename( |
121 | 765 | handle, |
122 | 765 | utf8_string, |
123 | 765 | 64, |
124 | 765 | NULL ) == -1 ) |
125 | 57 | { |
126 | 57 | goto on_error_libvmdk; |
127 | 57 | } |
128 | 708 | if( libvmdk_handle_get_utf16_parent_filename_size( |
129 | 708 | handle, |
130 | 708 | &string_size, |
131 | 708 | NULL ) == -1 ) |
132 | 0 | { |
133 | 0 | goto on_error_libvmdk; |
134 | 0 | } |
135 | 708 | if( libvmdk_handle_get_utf16_parent_filename( |
136 | 708 | handle, |
137 | 708 | utf16_string, |
138 | 708 | 64, |
139 | 708 | NULL ) == -1 ) |
140 | 0 | { |
141 | 0 | goto on_error_libvmdk; |
142 | 0 | } |
143 | 708 | if( libvmdk_handle_get_number_of_extents( |
144 | 708 | handle, |
145 | 708 | &number_of_extents, |
146 | 708 | NULL ) != 1 ) |
147 | 0 | { |
148 | 0 | goto on_error_libvmdk; |
149 | 0 | } |
150 | 708 | if( libvmdk_handle_get_media_size( |
151 | 708 | handle, |
152 | 708 | &media_size, |
153 | 708 | NULL ) != 1 ) |
154 | 0 | { |
155 | 0 | goto on_error_libvmdk; |
156 | 0 | } |
157 | 708 | for( read_iterator = 0; |
158 | 708 | read_iterator < 128; |
159 | 708 | read_iterator++ ) |
160 | 708 | { |
161 | 708 | if( media_offset >= media_size ) |
162 | 677 | { |
163 | 677 | break; |
164 | 677 | } |
165 | 31 | if( libvmdk_handle_read_buffer_at_offset( |
166 | 31 | handle, |
167 | 31 | buffer, |
168 | 31 | 497, |
169 | 31 | media_offset, |
170 | 31 | NULL ) == -1 ) |
171 | 31 | { |
172 | 31 | goto on_error_libvmdk; |
173 | 31 | } |
174 | 0 | media_offset += 497; |
175 | 0 | } |
176 | 677 | libvmdk_handle_close( |
177 | 677 | handle, |
178 | 677 | NULL ); |
179 | | |
180 | 8.40k | on_error_libvmdk: |
181 | 8.40k | libvmdk_handle_free( |
182 | 8.40k | &handle, |
183 | 8.40k | NULL ); |
184 | | |
185 | 8.40k | on_error_libbfio: |
186 | 8.40k | libbfio_handle_free( |
187 | 8.40k | &file_io_handle, |
188 | 8.40k | NULL ); |
189 | | |
190 | 8.40k | return( 0 ); |
191 | 8.40k | } |
192 | | |
193 | | } /* extern "C" */ |
194 | | |