| 1 |
/* |
| 2 |
* Copyright (c) 2019-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 |
|
| 10 |
#include <assert.h> |
| 11 |
#include <cbor.h> |
| 12 |
#include <errno.h> |
| 13 |
#include <stddef.h> |
| 14 |
#include <stdint.h> |
| 15 |
#include <stdio.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
#include "mutator_aux.h" |
| 20 |
|
| 21 |
int fido_nfc_rx(fido_dev_t *, uint8_t, unsigned char *, size_t, int); |
| 22 |
int fido_nfc_tx(fido_dev_t *, uint8_t, const unsigned char *, size_t); |
| 23 |
size_t LLVMFuzzerMutate(uint8_t *, size_t, size_t); |
| 24 |
|
| 25 |
extern int prng_up; |
| 26 |
static const uint8_t *wire_data_ptr = NULL; |
| 27 |
static size_t wire_data_len = 0; |
| 28 |
|
| 29 |
void |
| 30 |
consume(const void *body, size_t len) |
| 31 |
{ |
| 32 |
const volatile uint8_t *ptr = body; |
| 33 |
volatile uint8_t x = 0; |
| 34 |
|
| 35 |
#ifdef WITH_MSAN |
| 36 |
__msan_check_mem_is_initialized(body, len); |
| 37 |
#endif |
| 38 |
|
| 39 |
while (len--) |
| 40 |
x ^= *ptr++; |
| 41 |
|
| 42 |
(void)x; |
| 43 |
} |
| 44 |
|
| 45 |
void |
| 46 |
consume_str(const char *str) |
| 47 |
{ |
| 48 |
if (str != NULL) |
| 49 |
consume(str, strlen(str) + 1); |
| 50 |
} |
| 51 |
|
| 52 |
int |
| 53 |
unpack_int(cbor_item_t *item, int *v) |
| 54 |
{ |
| 55 |
if (cbor_is_int(item) == false || |
| 56 |
cbor_int_get_width(item) != CBOR_INT_64) |
| 57 |
return -1; |
| 58 |
|
| 59 |
if (cbor_isa_uint(item)) |
| 60 |
*v = (int)cbor_get_uint64(item); |
| 61 |
else |
| 62 |
*v = (int)(-cbor_get_uint64(item) - 1); |
| 63 |
|
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
int |
| 68 |
unpack_string(cbor_item_t *item, char *v) |
| 69 |
{ |
| 70 |
size_t len; |
| 71 |
|
| 72 |
if (cbor_isa_bytestring(item) == false || |
| 73 |
(len = cbor_bytestring_length(item)) >= MAXSTR) |
| 74 |
return -1; |
| 75 |
|
| 76 |
memcpy(v, cbor_bytestring_handle(item), len); |
| 77 |
v[len] = '\0'; |
| 78 |
|
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
int |
| 83 |
unpack_byte(cbor_item_t *item, uint8_t *v) |
| 84 |
{ |
| 85 |
if (cbor_isa_uint(item) == false || |
| 86 |
cbor_int_get_width(item) != CBOR_INT_8) |
| 87 |
return -1; |
| 88 |
|
| 89 |
*v = cbor_get_uint8(item); |
| 90 |
|
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
int |
| 95 |
unpack_blob(cbor_item_t *item, struct blob *v) |
| 96 |
{ |
| 97 |
if (cbor_isa_bytestring(item) == false || |
| 98 |
(v->len = cbor_bytestring_length(item)) > sizeof(v->body)) |
| 99 |
return -1; |
| 100 |
|
| 101 |
memcpy(v->body, cbor_bytestring_handle(item), v->len); |
| 102 |
|
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
cbor_item_t * |
| 107 |
pack_int(int v) NO_MSAN |
| 108 |
{ |
| 109 |
if (v < 0) |
| 110 |
return cbor_build_negint64((uint64_t)(-(int64_t)v - 1)); |
| 111 |
else |
| 112 |
return cbor_build_uint64((uint64_t)v); |
| 113 |
} |
| 114 |
|
| 115 |
cbor_item_t * |
| 116 |
pack_string(const char *v) NO_MSAN |
| 117 |
{ |
| 118 |
if (strlen(v) >= MAXSTR) |
| 119 |
return NULL; |
| 120 |
|
| 121 |
return cbor_build_bytestring((const unsigned char *)v, strlen(v)); |
| 122 |
} |
| 123 |
|
| 124 |
cbor_item_t * |
| 125 |
pack_byte(uint8_t v) NO_MSAN |
| 126 |
{ |
| 127 |
return cbor_build_uint8(v); |
| 128 |
} |
| 129 |
|
| 130 |
cbor_item_t * |
| 131 |
pack_blob(const struct blob *v) NO_MSAN |
| 132 |
{ |
| 133 |
return cbor_build_bytestring(v->body, v->len); |
| 134 |
} |
| 135 |
|
| 136 |
static |
| 137 |
void mutate_mem(void *v, size_t len) |
| 138 |
{ |
| 139 |
LLVMFuzzerMutate(v, len, len); |
| 140 |
#ifdef WITH_MSAN |
| 141 |
__msan_unpoison(v, len); |
| 142 |
#endif |
| 143 |
} |
| 144 |
|
| 145 |
void |
| 146 |
mutate_byte(uint8_t *b) |
| 147 |
{ |
| 148 |
mutate_mem(b, sizeof(*b)); |
| 149 |
} |
| 150 |
|
| 151 |
void |
| 152 |
mutate_int(int *i) |
| 153 |
{ |
| 154 |
mutate_mem(i, sizeof(*i)); |
| 155 |
} |
| 156 |
|
| 157 |
void |
| 158 |
mutate_blob(struct blob *blob) |
| 159 |
{ |
| 160 |
blob->len = LLVMFuzzerMutate((uint8_t *)blob->body, blob->len, |
| 161 |
sizeof(blob->body)); |
| 162 |
} |
| 163 |
|
| 164 |
void |
| 165 |
mutate_string(char *s) |
| 166 |
{ |
| 167 |
size_t n; |
| 168 |
|
| 169 |
n = LLVMFuzzerMutate((uint8_t *)s, strlen(s), MAXSTR - 1); |
| 170 |
s[n] = '\0'; |
| 171 |
} |
| 172 |
|
| 173 |
static int |
| 174 |
buf_read(unsigned char *ptr, size_t len, int ms) |
| 175 |
{ |
| 176 |
size_t n; |
| 177 |
|
| 178 |
(void)ms; |
| 179 |
|
| 180 |
if (prng_up && uniform_random(400) < 1) { |
| 181 |
errno = EIO; |
| 182 |
return -1; |
| 183 |
} |
| 184 |
|
| 185 |
if (wire_data_len < len) |
| 186 |
n = wire_data_len; |
| 187 |
else |
| 188 |
n = len; |
| 189 |
|
| 190 |
memcpy(ptr, wire_data_ptr, n); |
| 191 |
|
| 192 |
wire_data_ptr += n; |
| 193 |
wire_data_len -= n; |
| 194 |
|
| 195 |
return (int)n; |
| 196 |
} |
| 197 |
|
| 198 |
static int |
| 199 |
buf_write(const unsigned char *ptr, size_t len) |
| 200 |
{ |
| 201 |
consume(ptr, len); |
| 202 |
|
| 203 |
if (prng_up && uniform_random(400) < 1) { |
| 204 |
errno = EIO; |
| 205 |
return -1; |
| 206 |
} |
| 207 |
|
| 208 |
return (int)len; |
| 209 |
} |
| 210 |
|
| 211 |
static void * |
| 212 |
hid_open(const char *path) |
| 213 |
{ |
| 214 |
(void)path; |
| 215 |
|
| 216 |
return (void *)HID_DEV_HANDLE; |
| 217 |
} |
| 218 |
|
| 219 |
static void |
| 220 |
hid_close(void *handle) |
| 221 |
{ |
| 222 |
assert(handle == (void *)HID_DEV_HANDLE); |
| 223 |
} |
| 224 |
|
| 225 |
static int |
| 226 |
hid_read(void *handle, unsigned char *ptr, size_t len, int ms) |
| 227 |
{ |
| 228 |
assert(handle == (void *)HID_DEV_HANDLE); |
| 229 |
assert(len >= CTAP_MIN_REPORT_LEN && len <= CTAP_MAX_REPORT_LEN); |
| 230 |
|
| 231 |
return buf_read(ptr, len, ms); |
| 232 |
} |
| 233 |
|
| 234 |
static int |
| 235 |
hid_write(void *handle, const unsigned char *ptr, size_t len) |
| 236 |
{ |
| 237 |
assert(handle == (void *)HID_DEV_HANDLE); |
| 238 |
assert(len >= CTAP_MIN_REPORT_LEN + 1 && |
| 239 |
len <= CTAP_MAX_REPORT_LEN + 1); |
| 240 |
|
| 241 |
return buf_write(ptr, len); |
| 242 |
} |
| 243 |
|
| 244 |
static void * |
| 245 |
nfc_open(const char *path) |
| 246 |
{ |
| 247 |
(void)path; |
| 248 |
|
| 249 |
return (void *)NFC_DEV_HANDLE; |
| 250 |
} |
| 251 |
|
| 252 |
static void |
| 253 |
nfc_close(void *handle) |
| 254 |
{ |
| 255 |
assert(handle == (void *)NFC_DEV_HANDLE); |
| 256 |
} |
| 257 |
|
| 258 |
int |
| 259 |
nfc_read(void *handle, unsigned char *ptr, size_t len, int ms) |
| 260 |
{ |
| 261 |
assert(handle == (void *)NFC_DEV_HANDLE); |
| 262 |
assert(len > 0 && len <= 264); |
| 263 |
|
| 264 |
return buf_read(ptr, len, ms); |
| 265 |
} |
| 266 |
|
| 267 |
int |
| 268 |
nfc_write(void *handle, const unsigned char *ptr, size_t len) |
| 269 |
{ |
| 270 |
assert(handle == (void *)NFC_DEV_HANDLE); |
| 271 |
assert(len > 0 && len <= 256 + 2); |
| 272 |
|
| 273 |
return buf_write(ptr, len); |
| 274 |
} |
| 275 |
|
| 276 |
ssize_t |
| 277 |
fd_read(int fd, void *ptr, size_t len) |
| 278 |
{ |
| 279 |
assert(fd != -1); |
| 280 |
|
| 281 |
return buf_read(ptr, len, -1); |
| 282 |
} |
| 283 |
|
| 284 |
ssize_t |
| 285 |
fd_write(int fd, const void *ptr, size_t len) |
| 286 |
{ |
| 287 |
assert(fd != -1); |
| 288 |
|
| 289 |
return buf_write(ptr, len); |
| 290 |
} |
| 291 |
|
| 292 |
fido_dev_t * |
| 293 |
open_dev(int nfc) |
| 294 |
{ |
| 295 |
fido_dev_t *dev; |
| 296 |
fido_dev_io_t io; |
| 297 |
fido_dev_transport_t t; |
| 298 |
|
| 299 |
memset(&io, 0, sizeof(io)); |
| 300 |
memset(&t, 0, sizeof(t)); |
| 301 |
|
| 302 |
if ((dev = fido_dev_new()) == NULL) |
| 303 |
return NULL; |
| 304 |
|
| 305 |
if (nfc) { |
| 306 |
io.open = nfc_open; |
| 307 |
io.close = nfc_close; |
| 308 |
io.read = nfc_read; |
| 309 |
io.write = nfc_write; |
| 310 |
} else { |
| 311 |
io.open = hid_open; |
| 312 |
io.close = hid_close; |
| 313 |
io.read = hid_read; |
| 314 |
io.write = hid_write; |
| 315 |
} |
| 316 |
|
| 317 |
if (fido_dev_set_io_functions(dev, &io) != FIDO_OK) |
| 318 |
goto fail; |
| 319 |
|
| 320 |
if (nfc) { |
| 321 |
t.rx = fido_nfc_rx; |
| 322 |
t.tx = fido_nfc_tx; |
| 323 |
if (fido_dev_set_transport_functions(dev, &t) != FIDO_OK) |
| 324 |
goto fail; |
| 325 |
} |
| 326 |
|
| 327 |
if (fido_dev_set_timeout(dev, 300) != FIDO_OK || |
| 328 |
fido_dev_open(dev, "nodev") != FIDO_OK) |
| 329 |
goto fail; |
| 330 |
|
| 331 |
return dev; |
| 332 |
fail: |
| 333 |
fido_dev_free(&dev); |
| 334 |
|
| 335 |
return NULL; |
| 336 |
} |
| 337 |
|
| 338 |
void |
| 339 |
set_wire_data(const uint8_t *ptr, size_t len) |
| 340 |
{ |
| 341 |
wire_data_ptr = ptr; |
| 342 |
wire_data_len = len; |
| 343 |
} |