/src/libsmraw/ossfuzz/handle_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libsmraw file type |
3 | | * |
4 | | * Copyright (C) 2010-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_libsmraw.h" |
31 | | |
32 | | #if !defined( LIBSMRAW_HAVE_BFIO ) |
33 | | |
34 | | /* Opens a set of storage media RAW files using a Basic File IO (bfio) pool |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | LIBSMRAW_EXTERN \ |
38 | | int libsmraw_handle_open_file_io_pool( |
39 | | libsmraw_handle_t *handle, |
40 | | libbfio_handle_t *file_io_pool, |
41 | | int access_flags, |
42 | | libsmraw_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBSMRAW_HAVE_BFIO ) */ |
45 | | |
46 | | int LLVMFuzzerTestOneInput( |
47 | | const uint8_t *data, |
48 | | size_t size ) |
49 | 102 | { |
50 | 102 | uint8_t buffer[ 512 ]; |
51 | | |
52 | 102 | libbfio_handle_t *file_io_handle = NULL; |
53 | 102 | libbfio_pool_t *file_io_pool = NULL; |
54 | 102 | libsmraw_handle_t *handle = NULL; |
55 | 102 | off64_t media_offset = 0; |
56 | 102 | size64_t media_size = 0; |
57 | 102 | int entry_index = 0; |
58 | 102 | int read_iterator = 0; |
59 | | |
60 | 102 | if( libbfio_memory_range_initialize( |
61 | 102 | &file_io_handle, |
62 | 102 | NULL ) != 1 ) |
63 | 0 | { |
64 | 0 | return( 0 ); |
65 | 0 | } |
66 | 102 | if( libbfio_memory_range_set( |
67 | 102 | file_io_handle, |
68 | 102 | (uint8_t *) data, |
69 | 102 | size, |
70 | 102 | NULL ) != 1 ) |
71 | 0 | { |
72 | 0 | goto on_error_libbfio; |
73 | 0 | } |
74 | 102 | if( libbfio_pool_initialize( |
75 | 102 | &file_io_pool, |
76 | 102 | 0, |
77 | 102 | 0, |
78 | 102 | NULL ) != 1 ) |
79 | 0 | { |
80 | 0 | goto on_error_libbfio; |
81 | 0 | } |
82 | 102 | if( libbfio_pool_append_handle( |
83 | 102 | file_io_pool, |
84 | 102 | &entry_index, |
85 | 102 | file_io_handle, |
86 | 102 | LIBBFIO_OPEN_READ, |
87 | 102 | NULL ) != 1 ) |
88 | 0 | { |
89 | 0 | goto on_error_libbfio; |
90 | 0 | } |
91 | | /* The file IO pool takes over management of the file IO handle |
92 | | */ |
93 | 102 | file_io_handle = NULL; |
94 | | |
95 | 102 | if( libsmraw_handle_initialize( |
96 | 102 | &handle, |
97 | 102 | NULL ) != 1 ) |
98 | 0 | { |
99 | 0 | goto on_error_libbfio; |
100 | 0 | } |
101 | 102 | if( libsmraw_handle_open_file_io_pool( |
102 | 102 | handle, |
103 | 102 | file_io_pool, |
104 | 102 | LIBSMRAW_OPEN_READ, |
105 | 102 | NULL ) != 1 ) |
106 | 0 | { |
107 | 0 | goto on_error_libsmraw; |
108 | 0 | } |
109 | 102 | if( libsmraw_handle_get_media_size( |
110 | 102 | handle, |
111 | 102 | &media_size, |
112 | 102 | NULL ) != 1 ) |
113 | 0 | { |
114 | 0 | goto on_error_libsmraw; |
115 | 0 | } |
116 | 102 | for( read_iterator = 0; |
117 | 4.04k | read_iterator < 128; |
118 | 3.94k | read_iterator++ ) |
119 | 4.02k | { |
120 | 4.02k | if( media_offset >= media_size ) |
121 | 81 | { |
122 | 81 | break; |
123 | 81 | } |
124 | 3.94k | if( libsmraw_handle_read_buffer_at_offset( |
125 | 3.94k | handle, |
126 | 3.94k | buffer, |
127 | 3.94k | 497, |
128 | 3.94k | media_offset, |
129 | 3.94k | NULL ) == -1 ) |
130 | 0 | { |
131 | 0 | goto on_error_libsmraw; |
132 | 0 | } |
133 | 3.94k | media_offset += 497; |
134 | 3.94k | } |
135 | 102 | libsmraw_handle_close( |
136 | 102 | handle, |
137 | 102 | NULL ); |
138 | | |
139 | 102 | on_error_libsmraw: |
140 | 102 | libsmraw_handle_free( |
141 | 102 | &handle, |
142 | 102 | NULL ); |
143 | | |
144 | 102 | on_error_libbfio: |
145 | | /* Note that on error the handle still has a reference to file_io_pool |
146 | | * that will be closed. Therefore the file IO pool and handle need to |
147 | | * be freed after closing or freeing the handle. |
148 | | */ |
149 | 102 | if( file_io_pool != NULL ) |
150 | 102 | { |
151 | 102 | libbfio_pool_free( |
152 | 102 | &file_io_pool, |
153 | 102 | NULL ); |
154 | 102 | } |
155 | 102 | if( file_io_handle != NULL ) |
156 | 0 | { |
157 | 0 | libbfio_handle_free( |
158 | 0 | &file_io_handle, |
159 | | NULL ); |
160 | 0 | } |
161 | 102 | return( 0 ); |
162 | 102 | } |
163 | | |
164 | | } /* extern "C" */ |
165 | | |