/src/libevt/ossfuzz/file_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libevt file type |
3 | | * |
4 | | * Copyright (C) 2011-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_libevt.h" |
31 | | |
32 | | #if !defined( LIBEVT_HAVE_BFIO ) |
33 | | |
34 | | /* Determines if a file contains an EVT file signature using a Basic File IO (bfio) handle |
35 | | * Returns 1 if true, 0 if not or -1 on error |
36 | | */ |
37 | | LIBEVT_EXTERN \ |
38 | | int libevt_check_file_signature_file_io_handle( |
39 | | libbfio_handle_t *file_io_handle, |
40 | | libevt_error_t **error ); |
41 | | |
42 | | /* Opens a file using a Basic File IO (bfio) handle |
43 | | * Returns 1 if successful or -1 on error |
44 | | */ |
45 | | LIBEVT_EXTERN \ |
46 | | int libevt_file_open_file_io_handle( |
47 | | libevt_file_t *file, |
48 | | libbfio_handle_t *file_io_handle, |
49 | | int access_flags, |
50 | | libevt_error_t **error ); |
51 | | |
52 | | #endif /* !defined( LIBEVT_HAVE_BFIO ) */ |
53 | | |
54 | | int LLVMFuzzerTestOneInput( |
55 | | const uint8_t *data, |
56 | | size_t size ) |
57 | 1.98k | { |
58 | 1.98k | libbfio_handle_t *file_io_handle = NULL; |
59 | 1.98k | libevt_file_t *file = NULL; |
60 | 1.98k | libevt_record_t *record = NULL; |
61 | 1.98k | uint32_t flags = 0; |
62 | 1.98k | uint32_t major_format_version = 0; |
63 | 1.98k | uint32_t minor_format_version = 0; |
64 | 1.98k | int ascii_codepage = 0; |
65 | 1.98k | int number_of_records = 0; |
66 | 1.98k | int number_of_recovered_records = 0; |
67 | 1.98k | int result = 0; |
68 | | |
69 | 1.98k | if( libbfio_memory_range_initialize( |
70 | 1.98k | &file_io_handle, |
71 | 1.98k | NULL ) != 1 ) |
72 | 0 | { |
73 | 0 | return( 0 ); |
74 | 0 | } |
75 | 1.98k | if( libbfio_memory_range_set( |
76 | 1.98k | file_io_handle, |
77 | 1.98k | (uint8_t *) data, |
78 | 1.98k | size, |
79 | 1.98k | NULL ) != 1 ) |
80 | 0 | { |
81 | 0 | goto on_error_libbfio; |
82 | 0 | } |
83 | 1.98k | libevt_set_codepage( |
84 | 1.98k | LIBEVT_CODEPAGE_WINDOWS_1252, |
85 | 1.98k | NULL ); |
86 | | |
87 | 1.98k | if( libevt_file_initialize( |
88 | 1.98k | &file, |
89 | 1.98k | NULL ) != 1 ) |
90 | 0 | { |
91 | 0 | goto on_error_libbfio; |
92 | 0 | } |
93 | 1.98k | libevt_get_codepage( |
94 | 1.98k | &ascii_codepage, |
95 | 1.98k | NULL ); |
96 | | |
97 | 1.98k | libevt_check_file_signature_file_io_handle( |
98 | 1.98k | file_io_handle, |
99 | 1.98k | NULL ); |
100 | | |
101 | 1.98k | if( libevt_file_set_ascii_codepage( |
102 | 1.98k | file, |
103 | 1.98k | LIBEVT_CODEPAGE_WINDOWS_1252, |
104 | 1.98k | NULL ) != 1 ) |
105 | 0 | { |
106 | 0 | goto on_error_libevt; |
107 | 0 | } |
108 | 1.98k | if( libevt_file_get_ascii_codepage( |
109 | 1.98k | file, |
110 | 1.98k | &ascii_codepage, |
111 | 1.98k | NULL ) != 1 ) |
112 | 0 | { |
113 | 0 | goto on_error_libevt; |
114 | 0 | } |
115 | 1.98k | if( libevt_file_open_file_io_handle( |
116 | 1.98k | file, |
117 | 1.98k | file_io_handle, |
118 | 1.98k | LIBEVT_OPEN_READ, |
119 | 1.98k | NULL ) != 1 ) |
120 | 60 | { |
121 | 60 | goto on_error_libevt; |
122 | 60 | } |
123 | 1.92k | libevt_file_is_corrupted( |
124 | 1.92k | file, |
125 | 1.92k | NULL ); |
126 | | |
127 | 1.92k | libevt_file_get_format_version( |
128 | 1.92k | file, |
129 | 1.92k | &major_format_version, |
130 | 1.92k | &minor_format_version, |
131 | 1.92k | NULL ); |
132 | | |
133 | 1.92k | libevt_file_get_flags( |
134 | 1.92k | file, |
135 | 1.92k | &flags, |
136 | 1.92k | NULL ); |
137 | | |
138 | 1.92k | result = libevt_file_get_number_of_records( |
139 | 1.92k | file, |
140 | 1.92k | &number_of_records, |
141 | 1.92k | NULL ); |
142 | | |
143 | 1.92k | if( ( result == 1 ) |
144 | 1.92k | && ( number_of_records > 0 ) ) |
145 | 225 | { |
146 | 225 | if( libevt_file_get_record_by_index( |
147 | 225 | file, |
148 | 225 | 0, |
149 | 225 | &record, |
150 | 225 | NULL ) == 1 ) |
151 | 225 | { |
152 | 225 | libevt_record_free( |
153 | 225 | &record, |
154 | 225 | NULL ); |
155 | 225 | } |
156 | 225 | } |
157 | 1.92k | result = libevt_file_get_number_of_recovered_records( |
158 | 1.92k | file, |
159 | 1.92k | &number_of_recovered_records, |
160 | 1.92k | NULL ); |
161 | | |
162 | 1.92k | if( ( result == 1 ) |
163 | 1.92k | && ( number_of_recovered_records > 0 ) ) |
164 | 520 | { |
165 | 520 | if( libevt_file_get_recovered_record_by_index( |
166 | 520 | file, |
167 | 520 | 0, |
168 | 520 | &record, |
169 | 520 | NULL ) == 1 ) |
170 | 520 | { |
171 | 520 | libevt_record_free( |
172 | 520 | &record, |
173 | 520 | NULL ); |
174 | 520 | } |
175 | 520 | } |
176 | 1.92k | libevt_file_close( |
177 | 1.92k | file, |
178 | 1.92k | NULL ); |
179 | | |
180 | 1.98k | on_error_libevt: |
181 | 1.98k | libevt_file_free( |
182 | 1.98k | &file, |
183 | 1.98k | NULL ); |
184 | | |
185 | 1.98k | on_error_libbfio: |
186 | 1.98k | libbfio_handle_free( |
187 | 1.98k | &file_io_handle, |
188 | 1.98k | NULL ); |
189 | | |
190 | 1.98k | return( 0 ); |
191 | 1.98k | } |
192 | | |
193 | | } /* extern "C" */ |