Skip to content

Commit ce8e67c

Browse files
committed
Fuzzing: add back FuzzedDataProvider
1 parent cd591f2 commit ce8e67c

File tree

1 file changed

+34
-49
lines changed

1 file changed

+34
-49
lines changed

pkcs11/fuzz/fuzz_get_attribute_value.cc

Lines changed: 34 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#include <stddef.h>
33
#include <string.h>
44

5+
#include "fuzzer/FuzzedDataProvider.h"
6+
#include <algorithm>
7+
58
#include <openssl/ec.h>
69
#include <openssl/x509.h>
710

@@ -85,34 +88,16 @@ static EVP_PKEY *generate_keypair_openssl() {
8588
return pkey;
8689
}
8790

88-
typedef struct {
89-
CK_ULONG attribute_count;
90-
CK_OBJECT_HANDLE obj_handle;
91-
uint8_t derived_ecdh_key_count;
92-
} test_case_t;
93-
9491
void populate_attribute_template(CK_ATTRIBUTE_PTR *attribute_array,
9592
CK_ULONG attribute_count,
96-
uint8_t **fuzzer_data,
97-
size_t *fuzzer_data_size) {
93+
FuzzedDataProvider *fdp) {
9894
CK_ATTRIBUTE_PTR new_array = new CK_ATTRIBUTE[attribute_count];
9995
memset(new_array, 0, sizeof(CK_ATTRIBUTE) * attribute_count);
10096

10197
for (int i = 0; i < attribute_count; i++) {
102-
unsigned long ulValueLen = 0;
103-
104-
if (*fuzzer_data_size > 0) {
105-
ulValueLen = (*fuzzer_data)[0];
106-
*fuzzer_data += 1;
107-
*fuzzer_data_size -= 1;
108-
}
109-
110-
if (*fuzzer_data_size >= sizeof(unsigned long)) {
111-
memcpy(&new_array[i].type, *fuzzer_data, sizeof(unsigned long));
112-
*fuzzer_data += sizeof(unsigned long);
113-
*fuzzer_data_size -= sizeof(unsigned long);
114-
}
98+
uint8_t ulValueLen = fdp->ConsumeIntegral<uint8_t>();
11599

100+
new_array[i].type = fdp->ConsumeIntegral<CK_ATTRIBUTE_TYPE>();
116101
new_array[i].pValue = new uint8_t[ulValueLen]; // TODO populate pValue from
117102
// fuzzer generated data?
118103
new_array[i].ulValueLen = ulValueLen;
@@ -122,33 +107,22 @@ void populate_attribute_template(CK_ATTRIBUTE_PTR *attribute_array,
122107
}
123108

124109
void populate_derived_ecdh_key_template(CK_ATTRIBUTE_PTR *attribute_array,
125-
uint8_t **fuzzer_data,
126-
size_t *fuzzer_data_size) {
127-
110+
FuzzedDataProvider *fdp) {
128111
CK_ATTRIBUTE_PTR new_array = new CK_ATTRIBUTE[ECDH_ATTRIBUTE_COUNT];
129112
memset(new_array, 0, sizeof(CK_ATTRIBUTE) * ECDH_ATTRIBUTE_COUNT);
130113

131-
uint8_t value_len = 0;
132-
uint8_t label_len = 0;
133-
if (*fuzzer_data_size > 0) {
134-
value_len = (*fuzzer_data)[0];
135-
*fuzzer_data += 1;
136-
*fuzzer_data_size -= 1;
137-
}
138-
if (*fuzzer_data_size > 0) {
139-
label_len = (*fuzzer_data)[0];
140-
*fuzzer_data += 1;
141-
*fuzzer_data_size -= 1;
142-
}
114+
uint8_t value_len = fdp->ConsumeIntegral<uint8_t>();
115+
std::vector<uint8_t> value = fdp->ConsumeBytes<uint8_t>(value_len);
143116

144117
new_array[0].type = CKA_VALUE_LEN;
145118
new_array[0].ulValueLen = value_len;
146119
new_array[0].pValue = new uint8_t[value_len];
147-
if (*fuzzer_data_size < value_len) {
148-
value_len = *fuzzer_data_size;
149-
}
150-
memcpy(new_array[0].pValue, *fuzzer_data, value_len);
151-
*fuzzer_data_size -= value_len;
120+
121+
memset(new_array[0].pValue, 0, value_len);
122+
memcpy(new_array[0].pValue, &value[0],
123+
std::min(value.size(), (size_t) value_len));
124+
125+
uint8_t label_len = fdp->ConsumeIntegral<uint8_t>();
152126

153127
new_array[1].type = CKA_LABEL;
154128
new_array[1].ulValueLen = label_len;
@@ -201,16 +175,25 @@ void free_attribute_template(CK_ATTRIBUTE_PTR attribute_array,
201175
}
202176

203177
extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
178+
typedef struct {
179+
CK_ULONG attribute_count;
180+
CK_OBJECT_HANDLE obj_handle;
181+
uint8_t derived_ecdh_key_count;
182+
} test_case_t;
183+
204184
static bool p11_initialized = init_p11();
205185

206186
if (size < sizeof(test_case_t)) {
207187
return 0;
208188
}
209189

190+
FuzzedDataProvider *fdp = new FuzzedDataProvider(data, size);
191+
210192
test_case_t test_case;
211-
memcpy(&test_case, data, sizeof(test_case_t));
212-
data += sizeof(test_case_t);
213-
size -= sizeof(test_case_t);
193+
memset(&test_case, 0, sizeof(test_case_t));
194+
test_case.attribute_count = fdp->ConsumeIntegral<CK_ULONG>();
195+
test_case.obj_handle = fdp->ConsumeIntegral<CK_OBJECT_HANDLE>();
196+
test_case.derived_ecdh_key_count = fdp->ConsumeIntegral<uint8_t>();
214197

215198
/* limit the number of request attributes to 10
216199
* this is an artificial limitation to make fuzzer iterations faster
@@ -220,14 +203,14 @@ extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
220203
}
221204

222205
CK_ATTRIBUTE_PTR attribute_array;
223-
populate_attribute_template(&attribute_array, test_case.attribute_count,
224-
&data, &size);
225206
CK_ATTRIBUTE_PTR ecdh_attribute_array;
226-
populate_derived_ecdh_key_template(&ecdh_attribute_array, &data, &size);
207+
populate_attribute_template(&attribute_array, test_case.attribute_count, fdp);
208+
populate_derived_ecdh_key_template(&ecdh_attribute_array, fdp);
227209

228210
// the rest of the data is used for responses sent back by the backend
229-
backend_data = data;
230-
backend_data_len = size;
211+
std::vector<uint8_t> backend_vector = fdp->ConsumeRemainingBytes<uint8_t>();
212+
backend_data = &backend_vector[0];
213+
backend_data_len = backend_vector.size();
231214

232215
init_session();
233216

@@ -245,6 +228,8 @@ extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
245228
free_attribute_template(attribute_array, test_case.attribute_count);
246229
free_attribute_template(ecdh_attribute_array, ECDH_ATTRIBUTE_COUNT);
247230

231+
delete fdp;
232+
248233
fflush(stdout);
249234
return 0;
250235
}

0 commit comments

Comments
 (0)