/src/libpff/ossfuzz/file_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libpff file type |
3 | | * |
4 | | * Copyright (C) 2008-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_libpff.h" |
31 | | |
32 | | #if !defined( LIBPFF_HAVE_BFIO ) |
33 | | |
34 | | /* Opens a file using a Basic File IO (bfio) handle |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | LIBPFF_EXTERN \ |
38 | | int libpff_file_open_file_io_handle( |
39 | | libpff_file_t *file, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libpff_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBPFF_HAVE_BFIO ) */ |
45 | | |
46 | | /* Limits the number of recovered items to walk |
47 | | * to bound the time a single input takes |
48 | | */ |
49 | 851 | #define FUZZER_RECOVERED_ITEM_LIMIT 4 |
50 | | |
51 | | /* Limits the number of orphan items to walk |
52 | | * to bound the time a single input takes |
53 | | */ |
54 | 1.68k | #define FUZZER_ORPHAN_ITEM_LIMIT 4 |
55 | | |
56 | | int LLVMFuzzerTestOneInput( |
57 | | const uint8_t *data, |
58 | | size_t size ) |
59 | 3.85k | { |
60 | 3.85k | libbfio_handle_t *file_io_handle = NULL; |
61 | 3.85k | libpff_file_t *file = NULL; |
62 | 3.85k | libpff_item_t *orphan_item = NULL; |
63 | 3.85k | libpff_item_t *recovered_item = NULL; |
64 | 3.85k | uint8_t recovery_flags = 0; |
65 | 3.85k | int number_of_orphan_items = 0; |
66 | 3.85k | int number_of_recovered_items = 0; |
67 | 3.85k | int orphan_item_index = 0; |
68 | 3.85k | int recovered_item_index = 0; |
69 | | |
70 | 3.85k | if( libbfio_memory_range_initialize( |
71 | 3.85k | &file_io_handle, |
72 | 3.85k | NULL ) != 1 ) |
73 | 0 | { |
74 | 0 | return( 0 ); |
75 | 0 | } |
76 | 3.85k | if( libbfio_memory_range_set( |
77 | 3.85k | file_io_handle, |
78 | 3.85k | (uint8_t *) data, |
79 | 3.85k | size, |
80 | 3.85k | NULL ) != 1 ) |
81 | 0 | { |
82 | 0 | goto on_error_libbfio; |
83 | 0 | } |
84 | 3.85k | if( libpff_file_initialize( |
85 | 3.85k | &file, |
86 | 3.85k | NULL ) != 1 ) |
87 | 0 | { |
88 | 0 | goto on_error_libbfio; |
89 | 0 | } |
90 | 3.85k | if( libpff_file_open_file_io_handle( |
91 | 3.85k | file, |
92 | 3.85k | file_io_handle, |
93 | 3.85k | LIBPFF_OPEN_READ, |
94 | 3.85k | NULL ) != 1 ) |
95 | 2.80k | { |
96 | 2.80k | goto on_error_libpff_file; |
97 | 2.80k | } |
98 | 1.05k | if( ( size > 0 ) && ( data[ 0 ] == 0x0b ) ) |
99 | 0 | { |
100 | | /* Use an alternative ASCII codepage |
101 | | * to exercise the corresponding conversion code |
102 | | */ |
103 | 0 | libpff_file_set_ascii_codepage( |
104 | 0 | file, |
105 | 0 | 874, |
106 | 0 | NULL ); |
107 | 0 | } |
108 | 1.05k | if( size > 0 ) |
109 | 1.05k | { |
110 | 1.05k | recovery_flags = data[ 0 ] & 0x03; |
111 | 1.05k | } |
112 | | /* Recovers deleted items |
113 | | * Must be called immediately after open, before any item access |
114 | | */ |
115 | 1.05k | if( libpff_file_recover_items( |
116 | 1.05k | file, |
117 | 1.05k | recovery_flags, |
118 | 1.05k | NULL ) == 1 ) |
119 | 732 | { |
120 | 732 | if( libpff_file_get_number_of_recovered_items( |
121 | 732 | file, |
122 | 732 | &number_of_recovered_items, |
123 | 732 | NULL ) == 1 ) |
124 | 732 | { |
125 | 732 | for( recovered_item_index = 0; |
126 | 1.46k | recovered_item_index < number_of_recovered_items; |
127 | 732 | recovered_item_index++ ) |
128 | 851 | { |
129 | 851 | if( recovered_item_index >= FUZZER_RECOVERED_ITEM_LIMIT ) |
130 | 123 | { |
131 | 123 | break; |
132 | 123 | } |
133 | 728 | recovered_item = NULL; |
134 | | |
135 | 728 | if( libpff_file_get_recovered_item_by_index( |
136 | 728 | file, |
137 | 728 | recovered_item_index, |
138 | 728 | &recovered_item, |
139 | 728 | NULL ) == 1 |
140 | 728 | && recovered_item != NULL ) |
141 | 728 | { |
142 | 728 | libpff_item_free( |
143 | 728 | &recovered_item, |
144 | 728 | NULL ); |
145 | 728 | } |
146 | 728 | } |
147 | 732 | } |
148 | 732 | if( libpff_file_get_number_of_orphan_items( |
149 | 732 | file, |
150 | 732 | &number_of_orphan_items, |
151 | 732 | NULL ) == 1 ) |
152 | 732 | { |
153 | 732 | for( orphan_item_index = 0; |
154 | 2.17k | orphan_item_index < number_of_orphan_items; |
155 | 1.44k | orphan_item_index++ ) |
156 | 1.68k | { |
157 | 1.68k | if( orphan_item_index >= FUZZER_ORPHAN_ITEM_LIMIT ) |
158 | 242 | { |
159 | 242 | break; |
160 | 242 | } |
161 | 1.44k | orphan_item = NULL; |
162 | | |
163 | 1.44k | if( libpff_file_get_orphan_item_by_index( |
164 | 1.44k | file, |
165 | 1.44k | orphan_item_index, |
166 | 1.44k | &orphan_item, |
167 | 1.44k | NULL ) == 1 |
168 | 1.44k | && orphan_item != NULL ) |
169 | 1.44k | { |
170 | 1.44k | libpff_item_free( |
171 | 1.44k | &orphan_item, |
172 | 1.44k | NULL ); |
173 | 1.44k | } |
174 | 1.44k | } |
175 | 732 | } |
176 | 732 | } |
177 | 1.05k | libpff_file_close( |
178 | 1.05k | file, |
179 | 1.05k | NULL ); |
180 | | |
181 | 3.85k | on_error_libpff_file: |
182 | 3.85k | libpff_file_free( |
183 | 3.85k | &file, |
184 | 3.85k | NULL ); |
185 | | |
186 | 3.85k | on_error_libbfio: |
187 | 3.85k | libbfio_handle_free( |
188 | 3.85k | &file_io_handle, |
189 | 3.85k | NULL ); |
190 | | |
191 | 3.85k | return( 0 ); |
192 | 3.85k | } |
193 | | |
194 | | } /* extern "C" */ |