/src/libvsmbr/ossfuzz/partition_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libvsmbr partition 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_libvsmbr.h" |
31 | | |
32 | | #if !defined( LIBVSMBR_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 | | LIBVSMBR_EXTERN \ |
38 | | int libvsmbr_volume_open_file_io_handle( |
39 | | libvsmbr_volume_t *volume, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libvsmbr_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBVSMBR_HAVE_BFIO ) */ |
45 | | |
46 | | int LLVMFuzzerTestOneInput( |
47 | | const uint8_t *data, |
48 | | size_t size ) |
49 | 369 | { |
50 | 369 | uint8_t buffer[ 512 ]; |
51 | | |
52 | 369 | libbfio_handle_t *file_io_handle = NULL; |
53 | 369 | libvsmbr_partition_t *partition = NULL; |
54 | 369 | libvsmbr_volume_t *volume = NULL; |
55 | 369 | off64_t partition_offset = 0; |
56 | 369 | off64_t volume_offset = 0; |
57 | 369 | size64_t partition_size = 0; |
58 | 369 | uint8_t value_8bit = 0; |
59 | 369 | int number_of_partitions = 0; |
60 | 369 | int read_iterator = 0; |
61 | | |
62 | 369 | if( libbfio_memory_range_initialize( |
63 | 369 | &file_io_handle, |
64 | 369 | NULL ) != 1 ) |
65 | 0 | { |
66 | 0 | return( 0 ); |
67 | 0 | } |
68 | 369 | if( libbfio_memory_range_set( |
69 | 369 | file_io_handle, |
70 | 369 | (uint8_t *) data, |
71 | 369 | size, |
72 | 369 | NULL ) != 1 ) |
73 | 0 | { |
74 | 0 | goto on_error_libbfio; |
75 | 0 | } |
76 | 369 | if( libvsmbr_volume_initialize( |
77 | 369 | &volume, |
78 | 369 | NULL ) != 1 ) |
79 | 0 | { |
80 | 0 | goto on_error_libbfio; |
81 | 0 | } |
82 | 369 | if( libvsmbr_volume_open_file_io_handle( |
83 | 369 | volume, |
84 | 369 | file_io_handle, |
85 | 369 | LIBVSMBR_OPEN_READ, |
86 | 369 | NULL ) != 1 ) |
87 | 158 | { |
88 | 158 | goto on_error_libvsmbr_volume; |
89 | 158 | } |
90 | 211 | if( libvsmbr_volume_get_number_of_partitions( |
91 | 211 | volume, |
92 | 211 | &number_of_partitions, |
93 | 211 | NULL ) != 1 ) |
94 | 0 | { |
95 | 0 | goto on_error_libvsmbr_volume; |
96 | 0 | } |
97 | 211 | if( number_of_partitions > 0 ) |
98 | 208 | { |
99 | 208 | if( libvsmbr_volume_get_partition_by_index( |
100 | 208 | volume, |
101 | 208 | 0, |
102 | 208 | &partition, |
103 | 208 | NULL ) != 1 ) |
104 | 0 | { |
105 | 0 | goto on_error_libvsmbr_volume; |
106 | 0 | } |
107 | 208 | if( libvsmbr_partition_get_type( |
108 | 208 | partition, |
109 | 208 | &value_8bit, |
110 | 208 | NULL ) != 1 ) |
111 | 0 | { |
112 | 0 | goto on_error_libvsmbr_partition; |
113 | 0 | } |
114 | 208 | if( libvsmbr_partition_get_volume_offset( |
115 | 208 | partition, |
116 | 208 | &volume_offset, |
117 | 208 | NULL ) != 1 ) |
118 | 0 | { |
119 | 0 | goto on_error_libvsmbr_partition; |
120 | 0 | } |
121 | 208 | if( libvsmbr_partition_get_size( |
122 | 208 | partition, |
123 | 208 | &partition_size, |
124 | 208 | NULL ) != 1 ) |
125 | 0 | { |
126 | 0 | goto on_error_libvsmbr_partition; |
127 | 0 | } |
128 | 208 | for( read_iterator = 0; |
129 | 4.95k | read_iterator < 128; |
130 | 4.74k | read_iterator++ ) |
131 | 4.94k | { |
132 | 4.94k | if( partition_offset >= partition_size ) |
133 | 23 | { |
134 | 23 | break; |
135 | 23 | } |
136 | 4.92k | if( libvsmbr_partition_read_buffer_at_offset( |
137 | 4.92k | partition, |
138 | 4.92k | buffer, |
139 | 4.92k | 497, |
140 | 4.92k | partition_offset, |
141 | 4.92k | NULL ) == -1 ) |
142 | 178 | { |
143 | 178 | goto on_error_libvsmbr_partition; |
144 | 178 | } |
145 | 4.74k | partition_offset += 497; |
146 | 4.74k | } |
147 | 208 | on_error_libvsmbr_partition: |
148 | 208 | libvsmbr_partition_free( |
149 | 208 | &partition, |
150 | 208 | NULL ); |
151 | 208 | } |
152 | 211 | libvsmbr_volume_close( |
153 | 211 | volume, |
154 | 211 | NULL ); |
155 | | |
156 | 369 | on_error_libvsmbr_volume: |
157 | 369 | libvsmbr_volume_free( |
158 | 369 | &volume, |
159 | 369 | NULL ); |
160 | | |
161 | 369 | on_error_libbfio: |
162 | 369 | libbfio_handle_free( |
163 | 369 | &file_io_handle, |
164 | 369 | NULL ); |
165 | | |
166 | 369 | return( 0 ); |
167 | 369 | } |
168 | | |
169 | | } /* extern "C" */ |
170 | | |