Coverage Report

Created: 2024-06-03 09:43

/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
663k
{
33
663k
        int s = -1;
34
35
663k
        if (ioctl(fd, IOCTL_REQ(HIDIOCGRDESCSIZE), &s) == -1) {
36
1.68k
                fido_log_error(errno, "%s: ioctl HIDIOCGRDESCSIZE", __func__);
37
1.68k
                return (-1);
38
1.68k
        }
39
40
661k
        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
661k
        hrd->size = (unsigned)s;
46
47
661k
        if (ioctl(fd, IOCTL_REQ(HIDIOCGRDESC), hrd) == -1) {
48
1.60k
                fido_log_error(errno, "%s: ioctl HIDIOCGRDESC", __func__);
49
1.60k
                return (-1);
50
1.60k
        }
51
52
659k
        return (0);
53
661k
}
54
55
static bool
56
is_fido(const char *path)
57
664k
{
58
664k
        int                              fd = -1;
59
664k
        uint32_t                         usage_page = 0;
60
664k
        struct hidraw_report_descriptor *hrd = NULL;
61
62
664k
        if ((hrd = calloc(1, sizeof(*hrd))) == NULL ||
63
664k
            (fd = fido_hid_unix_open(path)) == -1)
64
1.58k
                goto out;
65
663k
        if (get_report_descriptor(fd, hrd) < 0 ||
66
663k
            fido_hid_get_usage(hrd->value, hrd->size, &usage_page) < 0)
67
336k
                usage_page = 0;
68
69
664k
out:
70
664k
        free(hrd);
71
72
664k
        if (fd != -1 && close(fd) == -1)
73
0
                fido_log_error(errno, "%s: close", __func__);
74
75
664k
        return (usage_page == 0xf1d0);
76
663k
}
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
107k
{
82
107k
        char                    *cp;
83
107k
        char                    *p;
84
107k
        char                    *s;
85
107k
        bool                     found_id = false;
86
107k
        bool                     found_name = false;
87
107k
        short unsigned int       x;
88
107k
        short unsigned int       y;
89
107k
        short unsigned int       z;
90
91
107k
        if ((s = cp = strdup(uevent)) == NULL)
92
314
                return (-1);
93
94
262k
        while ((p = strsep(&cp, "\n")) != NULL && *p != '\0') {
95
155k
                if (!found_id && strncmp(p, "HID_ID=", 7) == 0) {
96
22.3k
                        if (sscanf(p + 7, "%hx:%hx:%hx", &x, &y, &z) == 3) {
97
16.6k
                                *bus = (int)x;
98
16.6k
                                *vendor_id = (int16_t)y;
99
16.6k
                                *product_id = (int16_t)z;
100
16.6k
                                found_id = true;
101
16.6k
                        }
102
132k
                } else if (!found_name && strncmp(p, "HID_NAME=", 9) == 0) {
103
26.8k
                        if ((*hid_name = strdup(p + 9)) != NULL)
104
26.7k
                                found_name = true;
105
26.8k
                }
106
155k
        }
107
108
107k
        free(s);
109
110
107k
        if (!found_name || !found_id)
111
93.4k
                return (-1);
112
113
13.7k
        return (0);
114
107k
}
115
116
static char *
117
get_parent_attr(struct udev_device *dev, const char *subsystem,
118
    const char *devtype, const char *attr)
119
114k
{
120
114k
        struct udev_device      *parent;
121
114k
        const char              *value;
122
123
114k
        if ((parent = udev_device_get_parent_with_subsystem_devtype(dev,
124
114k
            subsystem, devtype)) == NULL || (value =
125
113k
            udev_device_get_sysattr_value(parent, attr)) == NULL)
126
585
                return (NULL);
127
128
113k
        return (strdup(value));
129
114k
}
130
131
static char *
132
get_usb_attr(struct udev_device *dev, const char *attr)
133
5.64k
{
134
5.64k
        return (get_parent_attr(dev, "usb", "usb_device", attr));
135
5.64k
}
136
137
static int
138
copy_info(fido_dev_info_t *di, struct udev *udev,
139
    struct udev_list_entry *udev_entry)
140
669k
{
141
669k
        const char              *name;
142
669k
        const char              *path;
143
669k
        char                    *uevent = NULL;
144
669k
        struct udev_device      *dev = NULL;
145
669k
        int                      bus = 0;
146
669k
        char                    *hid_name = NULL;
147
669k
        int                      ok = -1;
148
149
669k
        memset(di, 0, sizeof(*di));
150
151
669k
        if ((name = udev_list_entry_get_name(udev_entry)) == NULL ||
152
669k
            (dev = udev_device_new_from_syspath(udev, name)) == NULL ||
153
669k
            (path = udev_device_get_devnode(dev)) == NULL ||
154
669k
            is_fido(path) == 0)
155
561k
                goto fail;
156
157
108k
        if ((uevent = get_parent_attr(dev, "hid", NULL, "uevent")) == NULL ||
158
108k
            parse_uevent(uevent, &bus, &di->vendor_id, &di->product_id,
159
107k
            &hid_name) < 0) {
160
94.5k
                fido_log_debug("%s: uevent", __func__);
161
94.5k
                goto fail;
162
94.5k
        }
163
164
13.7k
#ifndef FIDO_HID_ANY
165
13.7k
        if (bus != BUS_USB) {
166
10.9k
                fido_log_debug("%s: bus", __func__);
167
10.9k
                goto fail;
168
10.9k
        }
169
2.82k
#endif
170
171
2.82k
        di->path = strdup(path);
172
2.82k
        di->manufacturer = get_usb_attr(dev, "manufacturer");
173
2.82k
        di->product = get_usb_attr(dev, "product");
174
175
2.82k
        if (di->manufacturer == NULL && di->product == NULL) {
176
5
                di->product = hid_name;  /* fallback */
177
5
                hid_name = NULL;
178
5
        }
179
2.82k
        if (di->manufacturer == NULL)
180
43
                di->manufacturer = strdup("");
181
2.82k
        if (di->product == NULL)
182
32
                di->product = strdup("");
183
2.82k
        if (di->path == NULL || di->manufacturer == NULL || di->product == NULL)
184
33
                goto fail;
185
186
2.78k
        ok = 0;
187
669k
fail:
188
669k
        if (dev != NULL)
189
666k
                udev_device_unref(dev);
190
191
669k
        free(uevent);
192
669k
        free(hid_name);
193
194
669k
        if (ok < 0) {
195
666k
                free(di->path);
196
666k
                free(di->manufacturer);
197
666k
                free(di->product);
198
666k
                explicit_bzero(di, sizeof(*di));
199
666k
        }
200
201
669k
        return (ok);
202
2.78k
}
203
204
int
205
fido_hid_manifest(fido_dev_info_t *devlist, size_t ilen, size_t *olen)
206
1.66k
{
207
1.66k
        struct udev             *udev = NULL;
208
1.66k
        struct udev_enumerate   *udev_enum = NULL;
209
1.66k
        struct udev_list_entry  *udev_list;
210
1.66k
        struct udev_list_entry  *udev_entry;
211
1.66k
        int                      r = FIDO_ERR_INTERNAL;
212
213
1.66k
        *olen = 0;
214
215
1.66k
        if (ilen == 0)
216
0
                return (FIDO_OK); /* nothing to do */
217
218
1.66k
        if (devlist == NULL)
219
0
                return (FIDO_ERR_INVALID_ARGUMENT);
220
221
1.66k
        if ((udev = udev_new()) == NULL ||
222
1.66k
            (udev_enum = udev_enumerate_new(udev)) == NULL)
223
15
                goto fail;
224
225
1.65k
        if (udev_enumerate_add_match_subsystem(udev_enum, "hidraw") < 0 ||
226
1.65k
            udev_enumerate_scan_devices(udev_enum) < 0)
227
9
                goto fail;
228
229
1.64k
        if ((udev_list = udev_enumerate_get_list_entry(udev_enum)) == NULL) {
230
2
                r = FIDO_OK; /* zero hidraw devices */
231
2
                goto fail;
232
2
        }
233
234
669k
        udev_list_entry_foreach(udev_entry, udev_list) {
235
669k
                if (copy_info(&devlist[*olen], udev, udev_entry) == 0) {
236
2.78k
                        devlist[*olen].io = (fido_dev_io_t) {
237
2.78k
                                fido_hid_open,
238
2.78k
                                fido_hid_close,
239
2.78k
                                fido_hid_read,
240
2.78k
                                fido_hid_write,
241
2.78k
                        };
242
2.78k
                        if (++(*olen) == ilen)
243
62
                                break;
244
2.78k
                }
245
669k
        }
246
247
1.64k
        r = FIDO_OK;
248
1.66k
fail:
249
1.66k
        if (udev_enum != NULL)
250
1.65k
                udev_enumerate_unref(udev_enum);
251
1.66k
        if (udev != NULL)
252
1.66k
                udev_unref(udev);
253
254
1.66k
        return (r);
255
1.64k
}
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
}