Coverage Report

Created: 2024-02-05 19:20

/libfido2/src/hid_linux.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2019-2024 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
#include <sys/types.h>
9
#include <sys/file.h>
10
#include <sys/ioctl.h>
11
12
#include <linux/hidraw.h>
13
#include <linux/input.h>
14
15
#include <errno.h>
16
#include <libudev.h>
17
#include <time.h>
18
#include <unistd.h>
19
20
#include "fido.h"
21
22
struct hid_linux {
23
        int             fd;
24
        size_t          report_in_len;
25
        size_t          report_out_len;
26
        sigset_t        sigmask;
27
        const sigset_t *sigmaskp;
28
};
29
30
static int
31
get_report_descriptor(int fd, struct hidraw_report_descriptor *hrd)
32
3.84M
{
33
3.84M
        int s = -1;
34
35
3.84M
        if (ioctl(fd, IOCTL_REQ(HIDIOCGRDESCSIZE), &s) == -1) {
36
9.17k
                fido_log_error(errno, "%s: ioctl HIDIOCGRDESCSIZE", __func__);
37
9.17k
                return (-1);
38
9.17k
        }
39
40
3.83M
        if (s < 0 || (unsigned)s > HID_MAX_DESCRIPTOR_SIZE) {
41
0
                fido_log_debug("%s: HIDIOCGRDESCSIZE %d", __func__, s);
42
0
                return (-1);
43
0
        }
44
45
3.83M
        hrd->size = (unsigned)s;
46
47
3.83M
        if (ioctl(fd, IOCTL_REQ(HIDIOCGRDESC), hrd) == -1) {
48
8.90k
                fido_log_error(errno, "%s: ioctl HIDIOCGRDESC", __func__);
49
8.90k
                return (-1);
50
8.90k
        }
51
52
3.82M
        return (0);
53
3.83M
}
54
55
static bool
56
is_fido(const char *path)
57
3.85M
{
58
3.85M
        int                              fd = -1;
59
3.85M
        uint32_t                         usage_page = 0;
60
3.85M
        struct hidraw_report_descriptor *hrd = NULL;
61
62
3.85M
        if ((hrd = calloc(1, sizeof(*hrd))) == NULL ||
63
3.85M
            (fd = fido_hid_unix_open(path)) == -1)
64
9.77k
                goto out;
65
3.84M
        if (get_report_descriptor(fd, hrd) < 0 ||
66
3.84M
            fido_hid_get_usage(hrd->value, hrd->size, &usage_page) < 0)
67
2.18M
                usage_page = 0;
68
69
3.85M
out:
70
3.85M
        free(hrd);
71
72
3.85M
        if (fd != -1 && close(fd) == -1)
73
0
                fido_log_error(errno, "%s: close", __func__);
74
75
3.85M
        return (usage_page == 0xf1d0);
76
3.84M
}
77
78
static int
79
parse_uevent(const char *uevent, int *bus, int16_t *vendor_id,
80
    int16_t *product_id, char **hid_name)
81
726k
{
82
726k
        char                    *cp;
83
726k
        char                    *p;
84
726k
        char                    *s;
85
726k
        bool                     found_id = false;
86
726k
        bool                     found_name = false;
87
726k
        short unsigned int       x;
88
726k
        short unsigned int       y;
89
726k
        short unsigned int       z;
90
91
726k
        if ((s = cp = strdup(uevent)) == NULL)
92
2.21k
                return (-1);
93
94
1.46M
        while ((p = strsep(&cp, "\n")) != NULL && *p != '\0') {
95
743k
                if (!found_id && strncmp(p, "HID_ID=", 7) == 0) {
96
427k
                        if (sscanf(p + 7, "%hx:%hx:%hx", &x, &y, &z) == 3) {
97
367k
                                *bus = (int)x;
98
367k
                                *vendor_id = (int16_t)y;
99
367k
                                *product_id = (int16_t)z;
100
367k
                                found_id = true;
101
367k
                        }
102
427k
                } else if (!found_name && strncmp(p, "HID_NAME=", 9) == 0) {
103
26.7k
                        if ((*hid_name = strdup(p + 9)) != NULL)
104
26.6k
                                found_name = true;
105
26.7k
                }
106
743k
        }
107
108
724k
        free(s);
109
110
724k
        if (!found_name || !found_id)
111
707k
                return (-1);
112
113
17.4k
        return (0);
114
724k
}
115
116
static char *
117
get_parent_attr(struct udev_device *dev, const char *subsystem,
118
    const char *devtype, const char *attr)
119
757k
{
120
757k
        struct udev_device      *parent;
121
757k
        const char              *value;
122
123
757k
        if ((parent = udev_device_get_parent_with_subsystem_devtype(dev,
124
757k
            subsystem, devtype)) == NULL || (value =
125
755k
            udev_device_get_sysattr_value(parent, attr)) == NULL)
126
4.10k
                return (NULL);
127
128
753k
        return (strdup(value));
129
757k
}
130
131
static char *
132
get_usb_attr(struct udev_device *dev, const char *attr)
133
24.7k
{
134
24.7k
        return (get_parent_attr(dev, "usb", "usb_device", attr));
135
24.7k
}
136
137
static int
138
copy_info(fido_dev_info_t *di, struct udev *udev,
139
    struct udev_list_entry *udev_entry)
140
3.88M
{
141
3.88M
        const char              *name;
142
3.88M
        const char              *path;
143
3.88M
        char                    *uevent = NULL;
144
3.88M
        struct udev_device      *dev = NULL;
145
3.88M
        int                      bus = 0;
146
3.88M
        char                    *hid_name = NULL;
147
3.88M
        int                      ok = -1;
148
149
3.88M
        memset(di, 0, sizeof(*di));
150
151
3.88M
        if ((name = udev_list_entry_get_name(udev_entry)) == NULL ||
152
3.88M
            (dev = udev_device_new_from_syspath(udev, name)) == NULL ||
153
3.88M
            (path = udev_device_get_devnode(dev)) == NULL ||
154
3.88M
            is_fido(path) == 0)
155
3.15M
                goto fail;
156
157
732k
        if ((uevent = get_parent_attr(dev, "hid", NULL, "uevent")) == NULL ||
158
732k
            parse_uevent(uevent, &bus, &di->vendor_id, &di->product_id,
159
726k
            &hid_name) < 0) {
160
715k
                fido_log_debug("%s: uevent", __func__);
161
715k
                goto fail;
162
715k
        }
163
164
17.4k
#ifndef FIDO_HID_ANY
165
17.4k
        if (bus != BUS_USB) {
166
5.01k
                fido_log_debug("%s: bus", __func__);
167
5.01k
                goto fail;
168
5.01k
        }
169
12.3k
#endif
170
171
12.3k
        di->path = strdup(path);
172
12.3k
        di->manufacturer = get_usb_attr(dev, "manufacturer");
173
12.3k
        di->product = get_usb_attr(dev, "product");
174
175
12.3k
        if (di->manufacturer == NULL && di->product == NULL) {
176
2
                di->product = hid_name;  /* fallback */
177
2
                hid_name = NULL;
178
2
        }
179
12.3k
        if (di->manufacturer == NULL)
180
270
                di->manufacturer = strdup("");
181
12.3k
        if (di->product == NULL)
182
28
                di->product = strdup("");
183
12.3k
        if (di->path == NULL || di->manufacturer == NULL || di->product == NULL)
184
30
                goto fail;
185
186
12.3k
        ok = 0;
187
3.88M
fail:
188
3.88M
        if (dev != NULL)
189
3.86M
                udev_device_unref(dev);
190
191
3.88M
        free(uevent);
192
3.88M
        free(hid_name);
193
194
3.88M
        if (ok < 0) {
195
3.87M
                free(di->path);
196
3.87M
                free(di->manufacturer);
197
3.87M
                free(di->product);
198
3.87M
                explicit_bzero(di, sizeof(*di));
199
3.87M
        }
200
201
3.88M
        return (ok);
202
12.3k
}
203
204
int
205
fido_hid_manifest(fido_dev_info_t *devlist, size_t ilen, size_t *olen)
206
9.19k
{
207
9.19k
        struct udev             *udev = NULL;
208
9.19k
        struct udev_enumerate   *udev_enum = NULL;
209
9.19k
        struct udev_list_entry  *udev_list;
210
9.19k
        struct udev_list_entry  *udev_entry;
211
9.19k
        int                      r = FIDO_ERR_INTERNAL;
212
213
9.19k
        *olen = 0;
214
215
9.19k
        if (ilen == 0)
216
0
                return (FIDO_OK); /* nothing to do */
217
218
9.19k
        if (devlist == NULL)
219
0
                return (FIDO_ERR_INVALID_ARGUMENT);
220
221
9.19k
        if ((udev = udev_new()) == NULL ||
222
9.19k
            (udev_enum = udev_enumerate_new(udev)) == NULL)
223
27
                goto fail;
224
225
9.16k
        if (udev_enumerate_add_match_subsystem(udev_enum, "hidraw") < 0 ||
226
9.16k
            udev_enumerate_scan_devices(udev_enum) < 0)
227
22
                goto fail;
228
229
9.14k
        if ((udev_list = udev_enumerate_get_list_entry(udev_enum)) == NULL) {
230
3
                r = FIDO_OK; /* zero hidraw devices */
231
3
                goto fail;
232
3
        }
233
234
3.88M
        udev_list_entry_foreach(udev_entry, udev_list) {
235
3.88M
                if (copy_info(&devlist[*olen], udev, udev_entry) == 0) {
236
12.3k
                        devlist[*olen].io = (fido_dev_io_t) {
237
12.3k
                                fido_hid_open,
238
12.3k
                                fido_hid_close,
239
12.3k
                                fido_hid_read,
240
12.3k
                                fido_hid_write,
241
12.3k
                        };
242
12.3k
                        if (++(*olen) == ilen)
243
284
                                break;
244
12.3k
                }
245
3.88M
        }
246
247
9.14k
        r = FIDO_OK;
248
9.19k
fail:
249
9.19k
        if (udev_enum != NULL)
250
9.16k
                udev_enumerate_unref(udev_enum);
251
9.19k
        if (udev != NULL)
252
9.18k
                udev_unref(udev);
253
254
9.19k
        return (r);
255
9.14k
}
256
257
void *
258
fido_hid_open(const char *path)
259
0
{
260
0
        struct hid_linux *ctx;
261
0
        struct hidraw_report_descriptor *hrd;
262
0
        struct timespec tv_pause;
263
0
        long interval_ms, retries = 0;
264
0
        bool looped;
265
266
0
retry:
267
0
        looped = false;
268
269
0
        if ((ctx = calloc(1, sizeof(*ctx))) == NULL ||
270
0
            (ctx->fd = fido_hid_unix_open(path)) == -1) {
271
0
                free(ctx);
272
0
                return (NULL);
273
0
        }
274
275
0
        while (flock(ctx->fd, LOCK_EX|LOCK_NB) == -1) {
276
0
                if (errno != EWOULDBLOCK) {
277
0
                        fido_log_error(errno, "%s: flock", __func__);
278
0
                        fido_hid_close(ctx);
279
0
                        return (NULL);
280
0
                }
281
0
                looped = true;
282
0
                if (retries++ >= 20) {
283
0
                        fido_log_debug("%s: flock timeout", __func__);
284
0
                        fido_hid_close(ctx);
285
0
                        return (NULL);
286
0
                }
287
0
                interval_ms = retries * 100000000L;
288
0
                tv_pause.tv_sec = interval_ms / 1000000000L;
289
0
                tv_pause.tv_nsec = interval_ms % 1000000000L;
290
0
                if (nanosleep(&tv_pause, NULL) == -1) {
291
0
                        fido_log_error(errno, "%s: nanosleep", __func__);
292
0
                        fido_hid_close(ctx);
293
0
                        return (NULL);
294
0
                }
295
0
        }
296
297
0
        if (looped) {
298
0
                fido_log_debug("%s: retrying", __func__);
299
0
                fido_hid_close(ctx);
300
0
                goto retry;
301
0
        }
302
303
0
        if ((hrd = calloc(1, sizeof(*hrd))) == NULL ||
304
0
            get_report_descriptor(ctx->fd, hrd) < 0 ||
305
0
            fido_hid_get_report_len(hrd->value, hrd->size, &ctx->report_in_len,
306
0
            &ctx->report_out_len) < 0 || ctx->report_in_len == 0 ||
307
0
            ctx->report_out_len == 0) {
308
0
                fido_log_debug("%s: using default report sizes", __func__);
309
0
                ctx->report_in_len = CTAP_MAX_REPORT_LEN;
310
0
                ctx->report_out_len = CTAP_MAX_REPORT_LEN;
311
0
        }
312
313
0
        free(hrd);
314
315
0
        return (ctx);
316
0
}
317
318
void
319
fido_hid_close(void *handle)
320
0
{
321
0
        struct hid_linux *ctx = handle;
322
323
0
        if (close(ctx->fd) == -1)
324
0
                fido_log_error(errno, "%s: close", __func__);
325
326
0
        free(ctx);
327
0
}
328
329
int
330
fido_hid_set_sigmask(void *handle, const fido_sigset_t *sigmask)
331
0
{
332
0
        struct hid_linux *ctx = handle;
333
334
0
        ctx->sigmask = *sigmask;
335
0
        ctx->sigmaskp = &ctx->sigmask;
336
337
0
        return (FIDO_OK);
338
0
}
339
340
int
341
fido_hid_read(void *handle, unsigned char *buf, size_t len, int ms)
342
0
{
343
0
        struct hid_linux        *ctx = handle;
344
0
        ssize_t                  r;
345
346
0
        if (len != ctx->report_in_len) {
347
0
                fido_log_debug("%s: len %zu", __func__, len);
348
0
                return (-1);
349
0
        }
350
351
0
        if (fido_hid_unix_wait(ctx->fd, ms, ctx->sigmaskp) < 0) {
352
0
                fido_log_debug("%s: fd not ready", __func__);
353
0
                return (-1);
354
0
        }
355
356
0
        if ((r = read(ctx->fd, buf, len)) == -1) {
357
0
                fido_log_error(errno, "%s: read", __func__);
358
0
                return (-1);
359
0
        }
360
361
0
        if (r < 0 || (size_t)r != len) {
362
0
                fido_log_debug("%s: %zd != %zu", __func__, r, len);
363
0
                return (-1);
364
0
        }
365
366
0
        return ((int)r);
367
0
}
368
369
int
370
fido_hid_write(void *handle, const unsigned char *buf, size_t len)
371
0
{
372
0
        struct hid_linux        *ctx = handle;
373
0
        ssize_t                  r;
374
375
0
        if (len != ctx->report_out_len + 1) {
376
0
                fido_log_debug("%s: len %zu", __func__, len);
377
0
                return (-1);
378
0
        }
379
380
0
        if ((r = write(ctx->fd, buf, len)) == -1) {
381
0
                fido_log_error(errno, "%s: write", __func__);
382
0
                return (-1);
383
0
        }
384
385
0
        if (r < 0 || (size_t)r != len) {
386
0
                fido_log_debug("%s: %zd != %zu", __func__, r, len);
387
0
                return (-1);
388
0
        }
389
390
0
        return ((int)r);
391
0
}
392
393
size_t
394
fido_hid_report_in_len(void *handle)
395
0
{
396
0
        struct hid_linux *ctx = handle;
397
398
0
        return (ctx->report_in_len);
399
0
}
400
401
size_t
402
fido_hid_report_out_len(void *handle)
403
0
{
404
0
        struct hid_linux *ctx = handle;
405
406
0
        return (ctx->report_out_len);
407
0
}