Debian Codegraph @

fuzz/fuzz_pcsc.c

raw
1 /*
2 * Copyright (c) 2022 Yubico AB. All rights reserved.
3 * Use of this source code is governed by a BSD-style
4 * license that can be found in the LICENSE file.
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8 #undef NDEBUG
9 #define _FIDO_INTERNAL
10
11 #include <assert.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdio.h>
16 #include <winscard.h>
17
18 #include "mutator_aux.h"
19 #include "wiredata_fido2.h"
20 #include "dummy.h"
21
22 #include "../src/extern.h"
23
24 struct param {
25 int seed;
26 char path[MAXSTR];
27 struct blob pcsc_list;
28 struct blob tx_apdu;
29 struct blob wiredata_init;
30 struct blob wiredata_msg;
31 };
32
33 static const uint8_t dummy_tx_apdu[] = { WIREDATA_CTAP_EXTENDED_APDU };
34 static const uint8_t dummy_wiredata_init[] = { WIREDATA_CTAP_NFC_INIT };
35 static const uint8_t dummy_wiredata_msg[] = { WIREDATA_CTAP_NFC_MSG };
36
37 struct param *
38 unpack(const uint8_t *ptr, size_t len)
39 {
40 cbor_item_t *item = NULL, **v;
41 struct cbor_load_result cbor;
42 struct param *p;
43 int ok = -1;
44
45 if ((p = calloc(1, sizeof(*p))) == NULL ||
46 (item = cbor_load(ptr, len, &cbor)) == NULL ||
47 cbor.read != len ||
48 cbor_isa_array(item) == false ||
49 cbor_array_is_definite(item) == false ||
50 cbor_array_size(item) != 6 ||
51 (v = cbor_array_handle(item)) == NULL)
52 goto fail;
53
54 if (unpack_int(v[0], &p->seed) < 0 ||
55 unpack_string(v[1], p->path) < 0 ||
56 unpack_blob(v[2], &p->pcsc_list) < 0 ||
57 unpack_blob(v[3], &p->tx_apdu) < 0 ||
58 unpack_blob(v[4], &p->wiredata_init) < 0 ||
59 unpack_blob(v[5], &p->wiredata_msg) < 0)
60 goto fail;
61
62 ok = 0;
63 fail:
64 if (ok < 0) {
65 free(p);
66 p = NULL;
67 }
68
69 if (item)
70 cbor_decref(&item);
71
72 return p;
73 }
74
75 size_t
76 pack(uint8_t *ptr, size_t len, const struct param *p)
77 {
78 cbor_item_t *argv[6], *array = NULL;
79 size_t cbor_alloc_len, cbor_len = 0;
80 unsigned char *cbor = NULL;
81
82 memset(argv, 0, sizeof(argv));
83
84 if ((array = cbor_new_definite_array(6)) == NULL ||
85 (argv[0] = pack_int(p->seed)) == NULL ||
86 (argv[1] = pack_string(p->path)) == NULL ||
87 (argv[2] = pack_blob(&p->pcsc_list)) == NULL ||
88 (argv[3] = pack_blob(&p->tx_apdu)) == NULL ||
89 (argv[4] = pack_blob(&p->wiredata_init)) == NULL ||
90 (argv[5] = pack_blob(&p->wiredata_msg)) == NULL)
91 goto fail;
92
93 for (size_t i = 0; i < 6; i++)
94 if (cbor_array_push(array, argv[i]) == false)
95 goto fail;
96
97 if ((cbor_len = cbor_serialize_alloc(array, &cbor,
98 &cbor_alloc_len)) == 0 || cbor_len > len) {
99 cbor_len = 0;
100 goto fail;
101 }
102
103 memcpy(ptr, cbor, cbor_len);
104 fail:
105 for (size_t i = 0; i < 6; i++)
106 if (argv[i])
107 cbor_decref(&argv[i]);
108
109 if (array)
110 cbor_decref(&array);
111
112 free(cbor);
113
114 return cbor_len;
115 }
116
117 size_t
118 pack_dummy(uint8_t *ptr, size_t len)
119 {
120 struct param dummy;
121 uint8_t blob[MAXCORPUS];
122 size_t blob_len;
123
124 memset(&dummy, 0, sizeof(dummy));
125
126 strlcpy(dummy.path, dummy_pcsc_path, sizeof(dummy.path));
127
128 dummy.pcsc_list.len = sizeof(dummy_pcsc_list);
129 memcpy(&dummy.pcsc_list.body, &dummy_pcsc_list, dummy.pcsc_list.len);
130
131 dummy.tx_apdu.len = sizeof(dummy_tx_apdu);
132 memcpy(&dummy.tx_apdu.body, &dummy_tx_apdu, dummy.tx_apdu.len);
133
134 dummy.wiredata_init.len = sizeof(dummy_wiredata_init);
135 memcpy(&dummy.wiredata_init.body, &dummy_wiredata_init,
136 dummy.wiredata_init.len);
137
138 dummy.wiredata_msg.len = sizeof(dummy_wiredata_msg);
139 memcpy(&dummy.wiredata_msg.body, &dummy_wiredata_msg,
140 dummy.wiredata_msg.len);
141
142 assert((blob_len = pack(blob, sizeof(blob), &dummy)) != 0);
143
144 if (blob_len > len) {
145 memcpy(ptr, blob, len);
146 return len;
147 }
148
149 memcpy(ptr, blob, blob_len);
150
151 return blob_len;
152 }
153
154 static void
155 test_manifest(void)
156 {
157 size_t ndevs, nfound;
158 fido_dev_info_t *devlist = NULL;
159 int16_t vendor_id, product_id;
160 int r;
161
162 r = fido_pcsc_manifest(NULL, 0, &nfound);
163 assert(r == FIDO_OK && nfound == 0);
164 r = fido_pcsc_manifest(NULL, 1, &nfound);
165 assert(r == FIDO_ERR_INVALID_ARGUMENT);
166
167 ndevs = uniform_random(64);
168 if ((devlist = fido_dev_info_new(ndevs)) == NULL ||
169 fido_pcsc_manifest(devlist, ndevs, &nfound) != FIDO_OK)
170 goto out;
171
172 for (size_t i = 0; i < nfound; i++) {
173 const fido_dev_info_t *di = fido_dev_info_ptr(devlist, i);
174 consume_str(fido_dev_info_path(di));
175 consume_str(fido_dev_info_manufacturer_string(di));
176 consume_str(fido_dev_info_product_string(di));
177 vendor_id = fido_dev_info_vendor(di);
178 product_id = fido_dev_info_product(di);
179 consume(&vendor_id, sizeof(vendor_id));
180 consume(&product_id, sizeof(product_id));
181 }
182
183 out:
184 fido_dev_info_free(&devlist, ndevs);
185 }
186
187 static void
188 test_tx(const char *path, const struct blob *apdu, uint8_t cmd, u_char *rx_buf,
189 size_t rx_len)
190 {
191 fido_dev_t dev;
192 const u_char *tx_ptr = NULL;
193 size_t tx_len = 0;
194 int n;
195
196 memset(&dev, 0, sizeof(dev));
197
198 if (fido_dev_set_pcsc(&dev) < 0)
199 return;
200 if ((dev.io_handle = fido_pcsc_open(path)) == NULL)
201 return;
202
203 if (apdu) {
204 tx_ptr = apdu->body;
205 tx_len = apdu->len;
206 }
207
208 fido_pcsc_tx(&dev, cmd, tx_ptr, tx_len);
209
210 if ((n = fido_pcsc_rx(&dev, cmd, rx_buf, rx_len, -1)) >= 0)
211 consume(rx_buf, n);
212
213 fido_pcsc_close(dev.io_handle);
214 }
215
216 static void
217 test_misc(void)
218 {
219 assert(fido_pcsc_open(NULL) == NULL);
220 assert(fido_pcsc_write(NULL, NULL, INT_MAX + 1LL) == -1);
221 }
222
223 void
224 test(const struct param *p)
225 {
226 u_char buf[512];
227
228 prng_init((unsigned int)p->seed);
229 fuzz_clock_reset();
230 fido_init(FIDO_DEBUG);
231 fido_set_log_handler(consume_str);
232
233 set_pcsc_parameters(&p->pcsc_list);
234 set_pcsc_io_functions(nfc_read, nfc_write, consume);
235
236 set_wire_data(p->wiredata_init.body, p->wiredata_init.len);
237 test_manifest();
238
239 test_misc();
240
241 set_wire_data(p->wiredata_init.body, p->wiredata_init.len);
242 test_tx(p->path, NULL, CTAP_CMD_INIT, buf, uniform_random(20));
243
244 set_wire_data(p->wiredata_msg.body, p->wiredata_msg.len);
245 test_tx(p->path, &p->tx_apdu, CTAP_CMD_MSG, buf, sizeof(buf));
246
247 set_wire_data(p->wiredata_msg.body, p->wiredata_msg.len);
248 test_tx(p->path, &p->tx_apdu, CTAP_CMD_CBOR, buf, sizeof(buf));
249
250 set_wire_data(p->wiredata_msg.body, p->wiredata_msg.len);
251 test_tx(p->path, &p->tx_apdu, CTAP_CMD_LOCK, buf, sizeof(buf));
252 }
253
254 void
255 mutate(struct param *p, unsigned int seed, unsigned int flags) NO_MSAN
256 {
257 if (flags & MUTATE_SEED)
258 p->seed = (int)seed;
259
260 if (flags & MUTATE_PARAM) {
261 mutate_string(p->path);
262 mutate_blob(&p->pcsc_list);
263 mutate_blob(&p->tx_apdu);
264 }
265
266 if (flags & MUTATE_WIREDATA) {
267 mutate_blob(&p->wiredata_init);
268 mutate_blob(&p->wiredata_msg);
269 }
270 }