/src/libvsbsdl/ossfuzz/partition_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libvsbsdl partition type |
3 | | * |
4 | | * Copyright (C) 2023-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_libvsbsdl.h" |
31 | | |
32 | | #if !defined( LIBVSBSDL_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 | | LIBVSBSDL_EXTERN \ |
38 | | int libvsbsdl_volume_open_file_io_handle( |
39 | | libvsbsdl_volume_t *volume, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libvsbsdl_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBVSBSDL_HAVE_BFIO ) */ |
45 | | |
46 | | int LLVMFuzzerTestOneInput( |
47 | | const uint8_t *data, |
48 | | size_t size ) |
49 | 480 | { |
50 | 480 | uint8_t buffer[ 512 ]; |
51 | 480 | char string[ 64 ]; |
52 | | |
53 | 480 | libbfio_handle_t *file_io_handle = NULL; |
54 | 480 | libvsbsdl_partition_t *partition = NULL; |
55 | 480 | libvsbsdl_volume_t *volume = NULL; |
56 | 480 | off64_t partition_offset = 0; |
57 | 480 | off64_t volume_offset = 0; |
58 | 480 | size64_t partition_size = 0; |
59 | 480 | uint16_t value_16bit = 0; |
60 | 480 | int number_of_partitions = 0; |
61 | 480 | int read_iterator = 0; |
62 | | |
63 | 480 | if( libbfio_memory_range_initialize( |
64 | 480 | &file_io_handle, |
65 | 480 | NULL ) != 1 ) |
66 | 0 | { |
67 | 0 | return( 0 ); |
68 | 0 | } |
69 | 480 | if( libbfio_memory_range_set( |
70 | 480 | file_io_handle, |
71 | 480 | (uint8_t *) data, |
72 | 480 | size, |
73 | 480 | NULL ) != 1 ) |
74 | 0 | { |
75 | 0 | goto on_error_libbfio; |
76 | 0 | } |
77 | 480 | if( libvsbsdl_volume_initialize( |
78 | 480 | &volume, |
79 | 480 | NULL ) != 1 ) |
80 | 0 | { |
81 | 0 | goto on_error_libbfio; |
82 | 0 | } |
83 | 480 | if( libvsbsdl_volume_open_file_io_handle( |
84 | 480 | volume, |
85 | 480 | file_io_handle, |
86 | 480 | LIBVSBSDL_OPEN_READ, |
87 | 480 | NULL ) != 1 ) |
88 | 97 | { |
89 | 97 | goto on_error_libvsbsdl_volume; |
90 | 97 | } |
91 | 383 | if( libvsbsdl_volume_get_number_of_partitions( |
92 | 383 | volume, |
93 | 383 | &number_of_partitions, |
94 | 383 | NULL ) != 1 ) |
95 | 0 | { |
96 | 0 | goto on_error_libvsbsdl_volume; |
97 | 0 | } |
98 | 383 | if( number_of_partitions > 0 ) |
99 | 382 | { |
100 | 382 | if( libvsbsdl_volume_get_partition_by_index( |
101 | 382 | volume, |
102 | 382 | 0, |
103 | 382 | &partition, |
104 | 382 | NULL ) != 1 ) |
105 | 66 | { |
106 | 66 | goto on_error_libvsbsdl_volume; |
107 | 66 | } |
108 | 316 | if( libvsbsdl_partition_get_entry_index( |
109 | 316 | partition, |
110 | 316 | &value_16bit, |
111 | 316 | NULL ) != 1 ) |
112 | 0 | { |
113 | 0 | goto on_error_libvsbsdl_partition; |
114 | 0 | } |
115 | 316 | if( libvsbsdl_partition_get_name_string( |
116 | 316 | partition, |
117 | 316 | string, |
118 | 316 | 64, |
119 | 316 | NULL ) != 1 ) |
120 | 0 | { |
121 | 0 | goto on_error_libvsbsdl_partition; |
122 | 0 | } |
123 | 316 | if( libvsbsdl_partition_get_volume_offset( |
124 | 316 | partition, |
125 | 316 | &volume_offset, |
126 | 316 | NULL ) != 1 ) |
127 | 0 | { |
128 | 0 | goto on_error_libvsbsdl_partition; |
129 | 0 | } |
130 | 316 | if( libvsbsdl_partition_get_size( |
131 | 316 | partition, |
132 | 316 | &partition_size, |
133 | 316 | NULL ) != 1 ) |
134 | 0 | { |
135 | 0 | goto on_error_libvsbsdl_partition; |
136 | 0 | } |
137 | 316 | for( read_iterator = 0; |
138 | 3.71k | read_iterator < 128; |
139 | 3.39k | read_iterator++ ) |
140 | 3.70k | { |
141 | 3.70k | if( partition_offset >= partition_size ) |
142 | 19 | { |
143 | 19 | break; |
144 | 19 | } |
145 | 3.68k | if( libvsbsdl_partition_read_buffer_at_offset( |
146 | 3.68k | partition, |
147 | 3.68k | buffer, |
148 | 3.68k | 497, |
149 | 3.68k | partition_offset, |
150 | 3.68k | NULL ) == -1 ) |
151 | 286 | { |
152 | 286 | goto on_error_libvsbsdl_partition; |
153 | 286 | } |
154 | 3.39k | partition_offset += 497; |
155 | 3.39k | } |
156 | 316 | on_error_libvsbsdl_partition: |
157 | 316 | libvsbsdl_partition_free( |
158 | 316 | &partition, |
159 | 316 | NULL ); |
160 | 316 | } |
161 | 317 | libvsbsdl_volume_close( |
162 | 317 | volume, |
163 | 317 | NULL ); |
164 | | |
165 | 480 | on_error_libvsbsdl_volume: |
166 | 480 | libvsbsdl_volume_free( |
167 | 480 | &volume, |
168 | 480 | NULL ); |
169 | | |
170 | 480 | on_error_libbfio: |
171 | 480 | libbfio_handle_free( |
172 | 480 | &file_io_handle, |
173 | 480 | NULL ); |
174 | | |
175 | 480 | return( 0 ); |
176 | 480 | } |
177 | | |
178 | | } /* extern "C" */ |
179 | | |