/src/libluksde/ossfuzz/volume_fuzzer.cc
Line | Count | Source |
1 | | /* |
2 | | * OSS-Fuzz target for libluksde volume type |
3 | | * |
4 | | * Copyright (C) 2013-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_libluksde.h" |
31 | | |
32 | | #if !defined( LIBLUKSDE_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 | | LIBLUKSDE_EXTERN \ |
38 | | int libluksde_volume_open_file_io_handle( |
39 | | libluksde_volume_t *volume, |
40 | | libbfio_handle_t *file_io_handle, |
41 | | int access_flags, |
42 | | libluksde_error_t **error ); |
43 | | |
44 | | #endif /* !defined( LIBLUKSDE_HAVE_BFIO ) */ |
45 | | |
46 | | int LLVMFuzzerTestOneInput( |
47 | | const uint8_t *data, |
48 | | size_t size ) |
49 | 1.08k | { |
50 | 1.08k | uint8_t master_key[ 32 ] = { |
51 | 1.08k | 0xa0, 0x1a, 0x0a, 0x46, 0xc2, 0xa4, 0xe9, 0x97, 0x69, 0xbb, 0xb2, 0x4c, 0x6d, 0x7c, 0x8a, 0x7c, |
52 | 1.08k | 0x3b, 0xf0, 0x30, 0xc1, 0x88, 0x16, 0x29, 0xe6, 0xa6, 0xa0, 0xb7, 0x27, 0xcd, 0x13, 0x70, 0x5b }; |
53 | | |
54 | 1.08k | uint8_t buffer[ 512 ]; |
55 | 1.08k | uint8_t uuid[ 16 ]; |
56 | | |
57 | 1.08k | libbfio_handle_t *file_io_handle = NULL; |
58 | 1.08k | libluksde_volume_t *volume = NULL; |
59 | 1.08k | off64_t volume_offset = 0; |
60 | 1.08k | size64_t volume_size = 0; |
61 | 1.08k | int encryption_chaining_mode = 0; |
62 | 1.08k | int encryption_method = 0; |
63 | 1.08k | int is_locked = 0; |
64 | 1.08k | int read_iterator = 0; |
65 | | |
66 | 1.08k | if( libbfio_memory_range_initialize( |
67 | 1.08k | &file_io_handle, |
68 | 1.08k | NULL ) != 1 ) |
69 | 0 | { |
70 | 0 | return( 0 ); |
71 | 0 | } |
72 | 1.08k | if( libbfio_memory_range_set( |
73 | 1.08k | file_io_handle, |
74 | 1.08k | (uint8_t *) data, |
75 | 1.08k | size, |
76 | 1.08k | NULL ) != 1 ) |
77 | 0 | { |
78 | 0 | goto on_error_libbfio; |
79 | 0 | } |
80 | 1.08k | if( libluksde_volume_initialize( |
81 | 1.08k | &volume, |
82 | 1.08k | NULL ) != 1 ) |
83 | 0 | { |
84 | 0 | goto on_error_libbfio; |
85 | 0 | } |
86 | | /* Using libluksde_volume_set_key to prevent timeouts due to large number |
87 | | * of iterations for PBKDF2 |
88 | | * |
89 | | * Password: luksde-TEST |
90 | | */ |
91 | 1.08k | if( libluksde_volume_set_key( |
92 | 1.08k | volume, |
93 | 1.08k | master_key, |
94 | 1.08k | 32, |
95 | 1.08k | NULL ) != 1 ) |
96 | 0 | { |
97 | 0 | goto on_error_libbfio; |
98 | 0 | } |
99 | 1.08k | if( libluksde_volume_open_file_io_handle( |
100 | 1.08k | volume, |
101 | 1.08k | file_io_handle, |
102 | 1.08k | LIBLUKSDE_OPEN_READ, |
103 | 1.08k | NULL ) != 1 ) |
104 | 793 | { |
105 | 793 | goto on_error_libluksde; |
106 | 793 | } |
107 | 292 | if( libluksde_volume_get_encryption_method( |
108 | 292 | volume, |
109 | 292 | &encryption_method, |
110 | 292 | &encryption_chaining_mode, |
111 | 292 | NULL ) != 1 ) |
112 | 0 | { |
113 | 0 | goto on_error_libluksde; |
114 | 0 | } |
115 | 292 | if( libluksde_volume_get_volume_identifier( |
116 | 292 | volume, |
117 | 292 | uuid, |
118 | 292 | 16, |
119 | 292 | NULL ) != 1 ) |
120 | 0 | { |
121 | 0 | goto on_error_libluksde; |
122 | 0 | } |
123 | 292 | is_locked = libluksde_volume_is_locked( |
124 | 292 | volume, |
125 | 292 | NULL ); |
126 | | |
127 | 292 | if( is_locked == -1 ) |
128 | 0 | { |
129 | 0 | goto on_error_libluksde; |
130 | 0 | } |
131 | 292 | else if( is_locked == 0 ) |
132 | 164 | { |
133 | 164 | if( libluksde_volume_get_size( |
134 | 164 | volume, |
135 | 164 | &volume_size, |
136 | 164 | NULL ) != 1 ) |
137 | 0 | { |
138 | 0 | goto on_error_libluksde; |
139 | 0 | } |
140 | 164 | for( read_iterator = 0; |
141 | 6.32k | read_iterator < 128; |
142 | 6.16k | read_iterator++ ) |
143 | 6.30k | { |
144 | 6.30k | if( volume_offset >= volume_size ) |
145 | 71 | { |
146 | 71 | break; |
147 | 71 | } |
148 | 6.23k | if( libluksde_volume_read_buffer_at_offset( |
149 | 6.23k | volume, |
150 | 6.23k | buffer, |
151 | 6.23k | 497, |
152 | 6.23k | volume_offset, |
153 | 6.23k | NULL ) == -1 ) |
154 | 75 | { |
155 | 75 | goto on_error_libluksde; |
156 | 75 | } |
157 | 6.16k | volume_offset += 497; |
158 | 6.16k | } |
159 | 164 | } |
160 | 217 | libluksde_volume_close( |
161 | 217 | volume, |
162 | 217 | NULL ); |
163 | | |
164 | 1.08k | on_error_libluksde: |
165 | 1.08k | libluksde_volume_free( |
166 | 1.08k | &volume, |
167 | 1.08k | NULL ); |
168 | | |
169 | 1.08k | on_error_libbfio: |
170 | 1.08k | libbfio_handle_free( |
171 | 1.08k | &file_io_handle, |
172 | 1.08k | NULL ); |
173 | | |
174 | 1.08k | return( 0 ); |
175 | 1.08k | } |
176 | | |
177 | | } /* extern "C" */ |
178 | | |