| 1 |
/* |
| 2 |
* Copyright (c) 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 |
* SPDX-License-Identifier: BSD-2-Clause |
| 6 |
*/ |
| 7 |
|
| 8 |
#undef NDEBUG |
| 9 |
|
| 10 |
#include <sys/types.h> |
| 11 |
|
| 12 |
#include <linux/hidraw.h> |
| 13 |
#include <linux/input.h> |
| 14 |
|
| 15 |
#include <assert.h> |
| 16 |
#include <errno.h> |
| 17 |
#include <libudev.h> |
| 18 |
#include <stdlib.h> |
| 19 |
|
| 20 |
#include "mutator_aux.h" |
| 21 |
|
| 22 |
struct udev { |
| 23 |
int magic; |
| 24 |
}; |
| 25 |
|
| 26 |
struct udev_enumerate { |
| 27 |
int magic; |
| 28 |
struct udev_list_entry *list_entry; |
| 29 |
}; |
| 30 |
|
| 31 |
struct udev_list_entry { |
| 32 |
int magic; |
| 33 |
}; |
| 34 |
|
| 35 |
struct udev_device { |
| 36 |
int magic; |
| 37 |
struct udev_device *parent; |
| 38 |
}; |
| 39 |
|
| 40 |
#define UDEV_MAGIC 0x584492cc |
| 41 |
#define UDEV_DEVICE_MAGIC 0x569180dd |
| 42 |
#define UDEV_LIST_ENTRY_MAGIC 0x497422ee |
| 43 |
#define UDEV_ENUM_MAGIC 0x583570ff |
| 44 |
|
| 45 |
#define ASSERT_TYPE(x, m) assert((x) != NULL && (x)->magic == (m)) |
| 46 |
#define ASSERT_UDEV(x) ASSERT_TYPE((x), UDEV_MAGIC) |
| 47 |
#define ASSERT_UDEV_ENUM(x) ASSERT_TYPE((x), UDEV_ENUM_MAGIC) |
| 48 |
#define ASSERT_UDEV_LIST_ENTRY(x) ASSERT_TYPE((x), UDEV_LIST_ENTRY_MAGIC) |
| 49 |
#define ASSERT_UDEV_DEVICE(x) ASSERT_TYPE((x), UDEV_DEVICE_MAGIC) |
| 50 |
|
| 51 |
static const char *uevent; |
| 52 |
static const struct blob *report_descriptor; |
| 53 |
|
| 54 |
struct udev *__wrap_udev_new(void); |
| 55 |
struct udev_device *__wrap_udev_device_get_parent_with_subsystem_devtype( |
| 56 |
struct udev_device *, const char *, const char *); |
| 57 |
struct udev_device *__wrap_udev_device_new_from_syspath(struct udev *, |
| 58 |
const char *); |
| 59 |
struct udev_enumerate *__wrap_udev_enumerate_new(struct udev *); |
| 60 |
struct udev_list_entry *__wrap_udev_enumerate_get_list_entry( |
| 61 |
struct udev_enumerate *); |
| 62 |
struct udev_list_entry *__wrap_udev_list_entry_get_next( |
| 63 |
struct udev_list_entry *); |
| 64 |
const char *__wrap_udev_device_get_sysattr_value(struct udev_device *, |
| 65 |
const char *); |
| 66 |
const char *__wrap_udev_list_entry_get_name(struct udev_list_entry *); |
| 67 |
const char *__wrap_udev_device_get_devnode(struct udev_device *); |
| 68 |
const char *__wrap_udev_device_get_sysnum(struct udev_device *); |
| 69 |
int __wrap_udev_enumerate_add_match_subsystem(struct udev_enumerate *, |
| 70 |
const char *); |
| 71 |
int __wrap_udev_enumerate_scan_devices(struct udev_enumerate *); |
| 72 |
int __wrap_ioctl(int, unsigned long , ...); |
| 73 |
void __wrap_udev_device_unref(struct udev_device *); |
| 74 |
void __wrap_udev_enumerate_unref(struct udev_enumerate *); |
| 75 |
void __wrap_udev_unref(struct udev *); |
| 76 |
void set_udev_parameters(const char *, const struct blob *); |
| 77 |
|
| 78 |
struct udev_device * |
| 79 |
__wrap_udev_device_get_parent_with_subsystem_devtype(struct udev_device *child, |
| 80 |
const char *subsystem, const char *devtype) |
| 81 |
{ |
| 82 |
ASSERT_UDEV_DEVICE(child); |
| 83 |
fido_log_debug("%s", subsystem); /* XXX consume */ |
| 84 |
fido_log_debug("%s", devtype); /* XXX consume */ |
| 85 |
if (child->parent != NULL) |
| 86 |
return child->parent; |
| 87 |
if ((child->parent = calloc(1, sizeof(*child->parent))) == NULL) |
| 88 |
return NULL; |
| 89 |
child->parent->magic = UDEV_DEVICE_MAGIC; |
| 90 |
|
| 91 |
return child->parent; |
| 92 |
} |
| 93 |
|
| 94 |
const char * |
| 95 |
__wrap_udev_device_get_sysattr_value(struct udev_device *udev_device, |
| 96 |
const char *sysattr) |
| 97 |
{ |
| 98 |
ASSERT_UDEV_DEVICE(udev_device); |
| 99 |
if (uniform_random(400) < 1) |
| 100 |
return NULL; |
| 101 |
if (!strcmp(sysattr, "manufacturer") || !strcmp(sysattr, "product")) |
| 102 |
return "product info"; /* XXX randomise? */ |
| 103 |
else if (!strcmp(sysattr, "uevent")) |
| 104 |
return uevent; |
| 105 |
|
| 106 |
return NULL; |
| 107 |
} |
| 108 |
|
| 109 |
const char * |
| 110 |
__wrap_udev_list_entry_get_name(struct udev_list_entry *entry) |
| 111 |
{ |
| 112 |
ASSERT_UDEV_LIST_ENTRY(entry); |
| 113 |
return uniform_random(400) < 1 ? NULL : "name"; /* XXX randomise? */ |
| 114 |
} |
| 115 |
|
| 116 |
struct udev_device * |
| 117 |
__wrap_udev_device_new_from_syspath(struct udev *udev, const char *syspath) |
| 118 |
{ |
| 119 |
struct udev_device *udev_device; |
| 120 |
|
| 121 |
ASSERT_UDEV(udev); |
| 122 |
fido_log_debug("%s", syspath); |
| 123 |
if ((udev_device = calloc(1, sizeof(*udev_device))) == NULL) |
| 124 |
return NULL; |
| 125 |
udev_device->magic = UDEV_DEVICE_MAGIC; |
| 126 |
|
| 127 |
return udev_device; |
| 128 |
} |
| 129 |
|
| 130 |
const char * |
| 131 |
__wrap_udev_device_get_devnode(struct udev_device *udev_device) |
| 132 |
{ |
| 133 |
ASSERT_UDEV_DEVICE(udev_device); |
| 134 |
return uniform_random(400) < 1 ? NULL : "/dev/zero"; |
| 135 |
} |
| 136 |
|
| 137 |
const char * |
| 138 |
__wrap_udev_device_get_sysnum(struct udev_device *udev_device) |
| 139 |
{ |
| 140 |
ASSERT_UDEV_DEVICE(udev_device); |
| 141 |
return uniform_random(400) < 1 ? NULL : "101010"; /* XXX randomise? */ |
| 142 |
} |
| 143 |
|
| 144 |
void |
| 145 |
__wrap_udev_device_unref(struct udev_device *udev_device) |
| 146 |
{ |
| 147 |
ASSERT_UDEV_DEVICE(udev_device); |
| 148 |
if (udev_device->parent) { |
| 149 |
ASSERT_UDEV_DEVICE(udev_device->parent); |
| 150 |
free(udev_device->parent); |
| 151 |
} |
| 152 |
free(udev_device); |
| 153 |
} |
| 154 |
|
| 155 |
struct udev * |
| 156 |
__wrap_udev_new(void) |
| 157 |
{ |
| 158 |
struct udev *udev; |
| 159 |
|
| 160 |
if ((udev = calloc(1, sizeof(*udev))) == NULL) |
| 161 |
return NULL; |
| 162 |
udev->magic = UDEV_MAGIC; |
| 163 |
|
| 164 |
return udev; |
| 165 |
} |
| 166 |
|
| 167 |
struct udev_enumerate * |
| 168 |
__wrap_udev_enumerate_new(struct udev *udev) |
| 169 |
{ |
| 170 |
struct udev_enumerate *udev_enum; |
| 171 |
|
| 172 |
ASSERT_UDEV(udev); |
| 173 |
if ((udev_enum = calloc(1, sizeof(*udev_enum))) == NULL) |
| 174 |
return NULL; |
| 175 |
udev_enum->magic = UDEV_ENUM_MAGIC; |
| 176 |
|
| 177 |
return udev_enum; |
| 178 |
} |
| 179 |
|
| 180 |
int |
| 181 |
__wrap_udev_enumerate_add_match_subsystem(struct udev_enumerate *udev_enum, |
| 182 |
const char *subsystem) |
| 183 |
{ |
| 184 |
ASSERT_UDEV_ENUM(udev_enum); |
| 185 |
fido_log_debug("%s:", subsystem); |
| 186 |
return uniform_random(400) < 1 ? -EINVAL : 0; |
| 187 |
} |
| 188 |
|
| 189 |
int |
| 190 |
__wrap_udev_enumerate_scan_devices(struct udev_enumerate *udev_enum) |
| 191 |
{ |
| 192 |
ASSERT_UDEV_ENUM(udev_enum); |
| 193 |
return uniform_random(400) < 1 ? -EINVAL : 0; |
| 194 |
} |
| 195 |
|
| 196 |
struct udev_list_entry * |
| 197 |
__wrap_udev_enumerate_get_list_entry(struct udev_enumerate *udev_enum) |
| 198 |
{ |
| 199 |
ASSERT_UDEV_ENUM(udev_enum); |
| 200 |
if ((udev_enum->list_entry = calloc(1, |
| 201 |
sizeof(*udev_enum->list_entry))) == NULL) |
| 202 |
return NULL; |
| 203 |
udev_enum->list_entry->magic = UDEV_LIST_ENTRY_MAGIC; |
| 204 |
|
| 205 |
return udev_enum->list_entry; |
| 206 |
} |
| 207 |
|
| 208 |
struct udev_list_entry * |
| 209 |
__wrap_udev_list_entry_get_next(struct udev_list_entry *udev_list_entry) |
| 210 |
{ |
| 211 |
ASSERT_UDEV_LIST_ENTRY(udev_list_entry); |
| 212 |
return uniform_random(400) < 1 ? NULL : udev_list_entry; |
| 213 |
} |
| 214 |
|
| 215 |
void |
| 216 |
__wrap_udev_enumerate_unref(struct udev_enumerate *udev_enum) |
| 217 |
{ |
| 218 |
ASSERT_UDEV_ENUM(udev_enum); |
| 219 |
if (udev_enum->list_entry) |
| 220 |
ASSERT_UDEV_LIST_ENTRY(udev_enum->list_entry); |
| 221 |
free(udev_enum->list_entry); |
| 222 |
free(udev_enum); |
| 223 |
} |
| 224 |
|
| 225 |
void |
| 226 |
__wrap_udev_unref(struct udev *udev) |
| 227 |
{ |
| 228 |
ASSERT_UDEV(udev); |
| 229 |
free(udev); |
| 230 |
} |
| 231 |
|
| 232 |
int |
| 233 |
__wrap_ioctl(int fd, unsigned long request, ...) |
| 234 |
{ |
| 235 |
va_list ap; |
| 236 |
struct hidraw_report_descriptor *hrd; |
| 237 |
|
| 238 |
(void)fd; |
| 239 |
|
| 240 |
if (uniform_random(400) < 1) { |
| 241 |
errno = EINVAL; |
| 242 |
return -1; |
| 243 |
} |
| 244 |
|
| 245 |
va_start(ap, request); |
| 246 |
|
| 247 |
switch (IOCTL_REQ(request)) { |
| 248 |
case IOCTL_REQ(HIDIOCGRDESCSIZE): |
| 249 |
*va_arg(ap, int *) = (int)report_descriptor->len; |
| 250 |
break; |
| 251 |
case IOCTL_REQ(HIDIOCGRDESC): |
| 252 |
hrd = va_arg(ap, struct hidraw_report_descriptor *); |
| 253 |
assert(hrd->size == report_descriptor->len); |
| 254 |
memcpy(hrd->value, report_descriptor->body, hrd->size); |
| 255 |
break; |
| 256 |
default: |
| 257 |
warnx("%s: unknown request 0x%lx", __func__, request); |
| 258 |
abort(); |
| 259 |
} |
| 260 |
|
| 261 |
va_end(ap); |
| 262 |
|
| 263 |
return 0; |
| 264 |
} |
| 265 |
|
| 266 |
void |
| 267 |
set_udev_parameters(const char *uevent_ptr, |
| 268 |
const struct blob *report_descriptor_ptr) |
| 269 |
{ |
| 270 |
uevent = uevent_ptr; |
| 271 |
report_descriptor = report_descriptor_ptr; |
| 272 |
} |