/src/libfvde/ossfuzz/sector_data_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libfvde sector data type |
3 | | * |
4 | | * Copyright (C) 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_libfvde.h" |
31 | | |
32 | | #include "../libfvde/libfvde_definitions.h" |
33 | | #include "../libfvde/libfvde_encryption_context.h" |
34 | | #include "../libfvde/libfvde_sector_data.h" |
35 | | |
36 | | int LLVMFuzzerTestOneInput( |
37 | | const uint8_t *data, |
38 | | size_t size ) |
39 | 54 | { |
40 | | /* The AES-128 key |
41 | | */ |
42 | 54 | uint8_t key[ 16 ] = |
43 | 54 | { |
44 | 54 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
45 | 54 | 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10 |
46 | 54 | }; |
47 | | |
48 | | /* The AES-128 tweak key |
49 | | */ |
50 | 54 | uint8_t tweak_key[ 16 ] = |
51 | 54 | { |
52 | 54 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
53 | 54 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
54 | 54 | }; |
55 | | |
56 | 54 | libbfio_handle_t *file_io_handle = NULL; |
57 | 54 | libbfio_pool_t *file_io_pool = NULL; |
58 | 54 | libfvde_encryption_context_t *encryption_context = NULL; |
59 | 54 | libfvde_sector_data_t *sector_data = NULL; |
60 | 54 | uint64_t block_number = 0; |
61 | 54 | int entry_index = 0; |
62 | | |
63 | 54 | if( libbfio_memory_range_initialize( |
64 | 54 | &file_io_handle, |
65 | 54 | NULL ) != 1 ) |
66 | 0 | { |
67 | 0 | return( 0 ); |
68 | 0 | } |
69 | 54 | if( libbfio_memory_range_set( |
70 | 54 | file_io_handle, |
71 | 54 | (uint8_t *) data, |
72 | 54 | size, |
73 | 54 | NULL ) != 1 ) |
74 | 0 | { |
75 | 0 | goto on_error_libbfio; |
76 | 0 | } |
77 | 54 | if( libbfio_pool_initialize( |
78 | 54 | &file_io_pool, |
79 | 54 | 0, |
80 | 54 | 0, |
81 | 54 | NULL ) != 1 ) |
82 | 0 | { |
83 | 0 | goto on_error_libbfio; |
84 | 0 | } |
85 | 54 | if( libfvde_encryption_context_initialize( |
86 | 54 | &encryption_context, |
87 | 54 | LIBFVDE_ENCRYPTION_METHOD_AES_128_XTS, |
88 | 54 | NULL ) != 1 ) |
89 | 0 | { |
90 | 0 | goto on_error_libbfio; |
91 | 0 | } |
92 | 54 | if( libfvde_encryption_context_set_keys( |
93 | 54 | encryption_context, |
94 | 54 | key, |
95 | 54 | 16, |
96 | 54 | tweak_key, |
97 | 54 | 16, |
98 | 54 | NULL ) != 1 ) |
99 | 0 | { |
100 | 0 | goto on_error_libfvde_encryption_context; |
101 | 0 | } |
102 | 54 | if( libfvde_sector_data_initialize( |
103 | 54 | §or_data, |
104 | 54 | 4096, |
105 | 54 | NULL ) != 1 ) |
106 | 0 | { |
107 | 0 | goto on_error_libfvde_encryption_context; |
108 | 0 | } |
109 | 54 | if( libbfio_pool_append_handle( |
110 | 54 | file_io_pool, |
111 | 54 | &entry_index, |
112 | 54 | file_io_handle, |
113 | 54 | LIBBFIO_OPEN_READ, |
114 | 54 | NULL ) != 1 ) |
115 | 0 | { |
116 | 0 | goto on_error_libfvde_sector_data; |
117 | 0 | } |
118 | | /* The file IO pool takes over management of the file IO handle |
119 | | */ |
120 | 54 | file_io_handle = NULL; |
121 | | |
122 | | /* Reads the sector data as-is |
123 | | */ |
124 | 54 | libfvde_sector_data_read( |
125 | 54 | sector_data, |
126 | 54 | NULL, |
127 | 54 | file_io_pool, |
128 | 54 | entry_index, |
129 | 54 | 0, |
130 | 54 | block_number, |
131 | 54 | 0, |
132 | 54 | NULL ); |
133 | | |
134 | 54 | block_number += 1; |
135 | | |
136 | | /* Reads and decrypts the sector data |
137 | | */ |
138 | 54 | libfvde_sector_data_read( |
139 | 54 | sector_data, |
140 | 54 | encryption_context, |
141 | 54 | file_io_pool, |
142 | 54 | entry_index, |
143 | 54 | 0, |
144 | 54 | block_number, |
145 | 54 | 1, |
146 | 54 | NULL ); |
147 | | |
148 | 54 | on_error_libfvde_sector_data: |
149 | 54 | libfvde_sector_data_free( |
150 | 54 | §or_data, |
151 | 54 | NULL ); |
152 | | |
153 | 54 | on_error_libfvde_encryption_context: |
154 | 54 | libfvde_encryption_context_free( |
155 | 54 | &encryption_context, |
156 | 54 | NULL ); |
157 | | |
158 | 54 | on_error_libbfio: |
159 | | /* Note that on error the sector data still has a reference to file_io_pool |
160 | | * that needs to be closed. Therefore the file IO pool and handle need to |
161 | | * be freed after freeing the sector data. |
162 | | */ |
163 | 54 | if( file_io_pool != NULL ) |
164 | 54 | { |
165 | 54 | libbfio_pool_free( |
166 | 54 | &file_io_pool, |
167 | 54 | NULL ); |
168 | 54 | } |
169 | 54 | if( file_io_handle != NULL ) |
170 | 0 | { |
171 | 0 | libbfio_handle_free( |
172 | 0 | &file_io_handle, |
173 | | NULL ); |
174 | 0 | } |
175 | 54 | return( 0 ); |
176 | 54 | } |
177 | | |
178 | | } /* extern "C" */ |