/src/libvsgpt/ossfuzz/partition_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libvsgpt volume type |
3 | | * |
4 | | * Copyright (C) 2019-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_libvsgpt.h" |
31 | | |
32 | | #if !defined( LIBVSGPT_HAVE_BFIO ) |
33 | | |
34 | | /* Opens a volume using a Basic File IO (bfio) handle |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | LIBVSGPT_EXTERN \ |
38 | | int libvsgpt_volume_open_file_io_handle( |
39 | | libvsgpt_volume_t *volume, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libvsgpt_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBVSGPT_HAVE_BFIO ) */ |
45 | | |
46 | | int LLVMFuzzerTestOneInput( |
47 | | const uint8_t *data, |
48 | | size_t size ) |
49 | 1.42k | { |
50 | 1.42k | uint8_t buffer[ 512 ]; |
51 | 1.42k | uint8_t guid[ 16 ]; |
52 | | |
53 | 1.42k | libbfio_handle_t *file_io_handle = NULL; |
54 | 1.42k | libvsgpt_partition_t *partition = NULL; |
55 | 1.42k | libvsgpt_volume_t *volume = NULL; |
56 | 1.42k | off64_t partition_offset = 0; |
57 | 1.42k | off64_t volume_offset = 0; |
58 | 1.42k | size64_t partition_size = 0; |
59 | 1.42k | uint32_t value_32bit = 0; |
60 | 1.42k | uint8_t value_8bit = 0; |
61 | 1.42k | int number_of_partitions = 0; |
62 | 1.42k | int read_iterator = 0; |
63 | | |
64 | 1.42k | if( libbfio_memory_range_initialize( |
65 | 1.42k | &file_io_handle, |
66 | 1.42k | NULL ) != 1 ) |
67 | 0 | { |
68 | 0 | return( 0 ); |
69 | 0 | } |
70 | 1.42k | if( libbfio_memory_range_set( |
71 | 1.42k | file_io_handle, |
72 | 1.42k | (uint8_t *) data, |
73 | 1.42k | size, |
74 | 1.42k | NULL ) != 1 ) |
75 | 0 | { |
76 | 0 | goto on_error_libbfio; |
77 | 0 | } |
78 | 1.42k | if( libvsgpt_volume_initialize( |
79 | 1.42k | &volume, |
80 | 1.42k | NULL ) != 1 ) |
81 | 0 | { |
82 | 0 | goto on_error_libbfio; |
83 | 0 | } |
84 | 1.42k | if( libvsgpt_volume_open_file_io_handle( |
85 | 1.42k | volume, |
86 | 1.42k | file_io_handle, |
87 | 1.42k | LIBVSGPT_OPEN_READ, |
88 | 1.42k | NULL ) != 1 ) |
89 | 1.25k | { |
90 | 1.25k | goto on_error_libvsgpt_volume; |
91 | 1.25k | } |
92 | 163 | if( libvsgpt_volume_get_number_of_partitions( |
93 | 163 | volume, |
94 | 163 | &number_of_partitions, |
95 | 163 | NULL ) != 1 ) |
96 | 0 | { |
97 | 0 | goto on_error_libvsgpt_volume; |
98 | 0 | } |
99 | 163 | if( number_of_partitions > 0 ) |
100 | 139 | { |
101 | 139 | if( libvsgpt_volume_get_partition_by_index( |
102 | 139 | volume, |
103 | 139 | 0, |
104 | 139 | &partition, |
105 | 139 | NULL ) != 1 ) |
106 | 0 | { |
107 | 0 | goto on_error_libvsgpt_volume; |
108 | 0 | } |
109 | 139 | if( libvsgpt_partition_get_entry_index( |
110 | 139 | partition, |
111 | 139 | &value_32bit, |
112 | 139 | NULL ) != 1 ) |
113 | 0 | { |
114 | 0 | goto on_error_libvsgpt_partition; |
115 | 0 | } |
116 | 139 | if( libvsgpt_partition_get_identifier( |
117 | 139 | partition, |
118 | 139 | guid, |
119 | 139 | 16, |
120 | 139 | NULL ) != 1 ) |
121 | 0 | { |
122 | 0 | goto on_error_libvsgpt_partition; |
123 | 0 | } |
124 | 139 | if( libvsgpt_partition_get_type_identifier( |
125 | 139 | partition, |
126 | 139 | guid, |
127 | 139 | 16, |
128 | 139 | NULL ) != 1 ) |
129 | 0 | { |
130 | 0 | goto on_error_libvsgpt_partition; |
131 | 0 | } |
132 | 139 | if( libvsgpt_partition_get_type( |
133 | 139 | partition, |
134 | 139 | &value_8bit, |
135 | 139 | NULL ) != 1 ) |
136 | 0 | { |
137 | 0 | goto on_error_libvsgpt_partition; |
138 | 0 | } |
139 | 139 | if( libvsgpt_partition_get_volume_offset( |
140 | 139 | partition, |
141 | 139 | &volume_offset, |
142 | 139 | NULL ) != 1 ) |
143 | 0 | { |
144 | 0 | goto on_error_libvsgpt_partition; |
145 | 0 | } |
146 | 139 | if( libvsgpt_partition_get_size( |
147 | 139 | partition, |
148 | 139 | &partition_size, |
149 | 139 | NULL ) != 1 ) |
150 | 0 | { |
151 | 0 | goto on_error_libvsgpt_partition; |
152 | 0 | } |
153 | 139 | for( read_iterator = 0; |
154 | 4.94k | read_iterator < 128; |
155 | 4.80k | read_iterator++ ) |
156 | 4.93k | { |
157 | 4.93k | if( partition_offset >= partition_size ) |
158 | 64 | { |
159 | 64 | break; |
160 | 64 | } |
161 | 4.86k | if( libvsgpt_partition_read_buffer_at_offset( |
162 | 4.86k | partition, |
163 | 4.86k | buffer, |
164 | 4.86k | 497, |
165 | 4.86k | partition_offset, |
166 | 4.86k | NULL ) == -1 ) |
167 | 60 | { |
168 | 60 | goto on_error_libvsgpt_partition; |
169 | 60 | } |
170 | 4.80k | partition_offset += 497; |
171 | 4.80k | } |
172 | 139 | on_error_libvsgpt_partition: |
173 | 139 | libvsgpt_partition_free( |
174 | 139 | &partition, |
175 | 139 | NULL ); |
176 | 139 | } |
177 | 163 | libvsgpt_volume_close( |
178 | 163 | volume, |
179 | 163 | NULL ); |
180 | | |
181 | 1.42k | on_error_libvsgpt_volume: |
182 | 1.42k | libvsgpt_volume_free( |
183 | 1.42k | &volume, |
184 | 1.42k | NULL ); |
185 | | |
186 | 1.42k | on_error_libbfio: |
187 | 1.42k | libbfio_handle_free( |
188 | 1.42k | &file_io_handle, |
189 | 1.42k | NULL ); |
190 | | |
191 | 1.42k | return( 0 ); |
192 | 1.42k | } |
193 | | |
194 | | } /* extern "C" */ |
195 | | |