1 /* 2 * Copyright (c) 2020-2021 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 */ 6 7 #include <assert.h> 8 #include <stdint.h> 9 #include <stdlib.h> 10 #include <string.h> 11 #include <stdio.h> 12 13 #include "../openbsd-compat/openbsd-compat.h" 14 #include "mutator_aux.h" 15 #include "dummy.h" 16 17 extern int fido_hid_get_usage(const uint8_t *, size_t, uint32_t *); 18 extern int fido_hid_get_report_len(const uint8_t *, size_t, size_t *, size_t *); 19 extern void set_udev_parameters(const char *, const struct blob *); 20 21 struct param { 22 int seed; 23 char uevent[MAXSTR]; 24 struct blob report_descriptor; 25 struct blob netlink_wiredata; 26 }; 27 28 /* 29 * Sample HID report descriptor from the FIDO HID interface of a YubiKey 5. 30 */ 31 static const uint8_t dummy_report_descriptor[] = { 32 0x06, 0xd0, 0xf1, 0x09, 0x01, 0xa1, 0x01, 0x09, 33 0x20, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 34 0x95, 0x40, 0x81, 0x02, 0x09, 0x21, 0x15, 0x00, 35 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x40, 0x91, 36 0x02, 0xc0 37 }; 38 39 /* 40 * Sample uevent file from a Yubico Security Key. 41 */ 42 static const char dummy_uevent[] = 43 "DRIVER=hid-generic\n" 44 "HID_ID=0003:00001050:00000120\n" 45 "HID_NAME=Yubico Security Key by Yubico\n" 46 "HID_PHYS=usb-0000:00:14.0-3/input0\n" 47 "HID_UNIQ=\n" 48 "MODALIAS=hid:b0003g0001v00001050p00000120\n"; 49 50 struct param * 51 unpack(const uint8_t *ptr, size_t len) 52 { 53 cbor_item_t *item = NULL, **v; 54 struct cbor_load_result cbor; 55 struct param *p; 56 int ok = -1; 57 58 if ((p = calloc(1, sizeof(*p))) == NULL || 59 (item = cbor_load(ptr, len, &cbor)) == NULL || 60 cbor.read != len || 61 cbor_isa_array(item) == false || 62 cbor_array_is_definite(item) == false || 63 cbor_array_size(item) != 4 || 64 (v = cbor_array_handle(item)) == NULL) 65 goto fail; 66 67 if (unpack_int(v[0], &p->seed) < 0 || 68 unpack_string(v[1], p->uevent) < 0 || 69 unpack_blob(v[2], &p->report_descriptor) < 0 || 70 unpack_blob(v[3], &p->netlink_wiredata) < 0) 71 goto fail; 72 73 ok = 0; 74 fail: 75 if (ok < 0) { 76 free(p); 77 p = NULL; 78 } 79 80 if (item) 81 cbor_decref(&item); 82 83 return p; 84 } 85 86 size_t 87 pack(uint8_t *ptr, size_t len, const struct param *p) 88 { 89 cbor_item_t *argv[4], *array = NULL; 90 size_t cbor_alloc_len, cbor_len = 0; 91 unsigned char *cbor = NULL; 92 93 memset(argv, 0, sizeof(argv)); 94 95 if ((array = cbor_new_definite_array(4)) == NULL || 96 (argv[0] = pack_int(p->seed)) == NULL || 97 (argv[1] = pack_string(p->uevent)) == NULL || 98 (argv[2] = pack_blob(&p->report_descriptor)) == NULL || 99 (argv[3] = pack_blob(&p->netlink_wiredata)) == NULL) 100 goto fail; 101 102 for (size_t i = 0; i < 4; i++) 103 if (cbor_array_push(array, argv[i]) == false) 104 goto fail; 105 106 if ((cbor_len = cbor_serialize_alloc(array, &cbor, 107 &cbor_alloc_len)) > len) { 108 cbor_len = 0; 109 goto fail; 110 } 111 112 memcpy(ptr, cbor, cbor_len); 113 fail: 114 for (size_t i = 0; i < 4; i++) 115 if (argv[i]) 116 cbor_decref(&argv[i]); 117 118 if (array) 119 cbor_decref(&array); 120 121 free(cbor); 122 123 return cbor_len; 124 } 125 126 size_t 127 pack_dummy(uint8_t *ptr, size_t len) 128 { 129 struct param dummy; 130 uint8_t blob[4096]; 131 size_t blob_len; 132 133 memset(&dummy, 0, sizeof(dummy)); 134 135 dummy.report_descriptor.len = sizeof(dummy_report_descriptor); 136 strlcpy(dummy.uevent, dummy_uevent, sizeof(dummy.uevent)); 137 memcpy(&dummy.report_descriptor.body, &dummy_report_descriptor, 138 dummy.report_descriptor.len); 139 dummy.netlink_wiredata.len = sizeof(dummy_netlink_wiredata); 140 memcpy(&dummy.netlink_wiredata.body, &dummy_netlink_wiredata, 141 dummy.netlink_wiredata.len); 142 143 assert((blob_len = pack(blob, sizeof(blob), &dummy)) != 0); 144 if (blob_len > len) 145 blob_len = len; 146 147 memcpy(ptr, blob, blob_len); 148 149 return blob_len; 150 } 151 152 static void 153 get_usage(const struct param *p) 154 { 155 uint32_t usage_page = 0; 156 157 fido_hid_get_usage(p->report_descriptor.body, p->report_descriptor.len, 158 &usage_page); 159 consume(&usage_page, sizeof(usage_page)); 160 } 161 162 static void 163 get_report_len(const struct param *p) 164 { 165 size_t report_in_len = 0; 166 size_t report_out_len = 0; 167 168 fido_hid_get_report_len(p->report_descriptor.body, 169 p->report_descriptor.len, &report_in_len, &report_out_len); 170 consume(&report_in_len, sizeof(report_in_len)); 171 consume(&report_out_len, sizeof(report_out_len)); 172 } 173 174 static void 175 manifest(const struct param *p) 176 { 177 size_t ndevs, nfound; 178 fido_dev_info_t *devlist = NULL, *devlist_set = NULL; 179 int16_t vendor_id, product_id; 180 fido_dev_io_t io; 181 fido_dev_transport_t t; 182 183 memset(&io, 0, sizeof(io)); 184 memset(&t, 0, sizeof(t)); 185 set_netlink_io_functions(fd_read, fd_write); 186 set_wire_data(p->netlink_wiredata.body, p->netlink_wiredata.len); 187 set_udev_parameters(p->uevent, &p->report_descriptor); 188 189 ndevs = uniform_random(64); 190 if ((devlist = fido_dev_info_new(ndevs)) == NULL || 191 (devlist_set = fido_dev_info_new(1)) == NULL || 192 fido_dev_info_manifest(devlist, ndevs, &nfound) != FIDO_OK) 193 goto out; 194 for (size_t i = 0; i < nfound; i++) { 195 const fido_dev_info_t *di = fido_dev_info_ptr(devlist, i); 196 consume_str(fido_dev_info_path(di)); 197 consume_str(fido_dev_info_manufacturer_string(di)); 198 consume_str(fido_dev_info_product_string(di)); 199 vendor_id = fido_dev_info_vendor(di); 200 product_id = fido_dev_info_product(di); 201 consume(&vendor_id, sizeof(vendor_id)); 202 consume(&product_id, sizeof(product_id)); 203 fido_dev_info_set(devlist_set, 0, fido_dev_info_path(di), 204 fido_dev_info_manufacturer_string(di), 205 fido_dev_info_product_string(di), &io, &t); 206 } 207 out: 208 fido_dev_info_free(&devlist, ndevs); 209 fido_dev_info_free(&devlist_set, 1); 210 } 211 212 void 213 test(const struct param *p) 214 { 215 prng_init((unsigned int)p->seed); 216 fuzz_clock_reset(); 217 fido_init(FIDO_DEBUG); 218 fido_set_log_handler(consume_str); 219 220 get_usage(p); 221 get_report_len(p); 222 manifest(p); 223 } 224 225 void 226 mutate(struct param *p, unsigned int seed, unsigned int flags) NO_MSAN 227 { 228 if (flags & MUTATE_SEED) 229 p->seed = (int)seed; 230 231 if (flags & MUTATE_PARAM) { 232 mutate_blob(&p->report_descriptor); 233 mutate_string(p->uevent); 234 } 235 236 if (flags & MUTATE_WIREDATA) 237 mutate_blob(&p->netlink_wiredata); 238 } 239