Coverage Report

Created: 2022-07-22 12:05

/libfido2/src/nfc_linux.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2020-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
 */
6
7
#include <sys/types.h>
8
#include <sys/uio.h>
9
#include <sys/socket.h>
10
11
#include <linux/nfc.h>
12
13
#include <errno.h>
14
#include <libudev.h>
15
#include <signal.h>
16
#include <stdio.h>
17
#include <string.h>
18
#include <unistd.h>
19
20
#include "fido.h"
21
#include "fido/param.h"
22
#include "netlink.h"
23
#include "iso7816.h"
24
25
struct nfc_linux {
26
        int             fd;
27
        uint32_t        dev;
28
        uint32_t        target;
29
        sigset_t        sigmask;
30
        const sigset_t *sigmaskp;
31
        struct fido_nl *nl;
32
};
33
34
static char *
35
get_parent_attr(struct udev_device *dev, const char *subsystem,
36
    const char *devtype, const char *attr)
37
0
{
38
0
        struct udev_device *parent;
39
0
        const char *value;
40
41
0
        if ((parent = udev_device_get_parent_with_subsystem_devtype(dev,
42
0
            subsystem, devtype)) == NULL || (value =
43
0
            udev_device_get_sysattr_value(parent, attr)) == NULL)
44
0
                return NULL;
45
46
0
        return strdup(value);
47
0
}
48
49
static char *
50
get_usb_attr(struct udev_device *dev, const char *attr)
51
0
{
52
0
        return get_parent_attr(dev, "usb", "usb_device", attr);
53
0
}
54
55
static int
56
copy_info(fido_dev_info_t *di, struct udev *udev,
57
    struct udev_list_entry *udev_entry)
58
458k
{
59
458k
        const char *name;
60
458k
        char *str;
61
458k
        struct udev_device *dev = NULL;
62
458k
        uint64_t id;
63
458k
        int ok = -1;
64
65
458k
        memset(di, 0, sizeof(*di));
66
67
458k
        if ((name = udev_list_entry_get_name(udev_entry)) == NULL ||
68
458k
            (dev = udev_device_new_from_syspath(udev, name)) == NULL)
69
2.28k
                goto fail;
70
456k
        if (asprintf(&di->path, "%s/%s", FIDO_NFC_PREFIX, name) == -1) {
71
1.22k
                di->path = NULL;
72
1.22k
                goto fail;
73
1.22k
        }
74
455k
        if (nfc_is_fido(di->path) == false) {
75
455k
                fido_log_debug("%s: nfc_is_fido: %s", __func__, di->path);
76
455k
                goto fail;
77
455k
        }
78
0
        if ((di->manufacturer = get_usb_attr(dev, "manufacturer")) == NULL)
79
0
                di->manufacturer = strdup("");
80
0
        if ((di->product = get_usb_attr(dev, "product")) == NULL)
81
0
                di->product = strdup("");
82
0
        if (di->manufacturer == NULL || di->product == NULL)
83
0
                goto fail;
84
        /* XXX assumes USB for vendor/product info */
85
0
        if ((str = get_usb_attr(dev, "idVendor")) != NULL &&
86
0
            fido_to_uint64(str, 16, &id) == 0 && id <= UINT16_MAX)
87
0
                di->vendor_id = (int16_t)id;
88
0
        free(str);
89
0
        if ((str = get_usb_attr(dev, "idProduct")) != NULL &&
90
0
            fido_to_uint64(str, 16, &id) == 0 && id <= UINT16_MAX)
91
0
                di->product_id = (int16_t)id;
92
0
        free(str);
93
94
0
        ok = 0;
95
458k
fail:
96
458k
        if (dev != NULL)
97
456k
                udev_device_unref(dev);
98
99
458k
        if (ok < 0) {
100
458k
                free(di->path);
101
458k
                free(di->manufacturer);
102
458k
                free(di->product);
103
458k
                explicit_bzero(di, sizeof(*di));
104
458k
        }
105
106
458k
        return ok;
107
0
}
108
109
static int
110
sysnum_from_syspath(const char *path)
111
453k
{
112
453k
        struct udev *udev = NULL;
113
453k
        struct udev_device *dev = NULL;
114
453k
        const char *str;
115
453k
        uint64_t idx64;
116
453k
        int idx = -1;
117
118
453k
        if ((udev = udev_new()) != NULL &&
119
453k
            (dev = udev_device_new_from_syspath(udev, path)) != NULL &&
120
453k
            (str = udev_device_get_sysnum(dev)) != NULL &&
121
453k
            fido_to_uint64(str, 10, &idx64) == 0 && idx64 < INT_MAX)
122
449k
                idx = (int)idx64;
123
124
453k
        if (dev != NULL)
125
450k
                udev_device_unref(dev);
126
453k
        if (udev != NULL)
127
451k
                udev_unref(udev);
128
129
453k
        return idx;
130
453k
}
131
132
int
133
fido_nfc_manifest(fido_dev_info_t *devlist, size_t ilen, size_t *olen)
134
1.04k
{
135
1.04k
        struct udev *udev = NULL;
136
1.04k
        struct udev_enumerate *udev_enum = NULL;
137
1.04k
        struct udev_list_entry *udev_list;
138
1.04k
        struct udev_list_entry *udev_entry;
139
1.04k
        int r = FIDO_ERR_INTERNAL;
140
141
1.04k
        *olen = 0;
142
143
1.04k
        if (ilen == 0)
144
0
                return FIDO_OK;
145
146
1.04k
        if (devlist == NULL)
147
0
                return FIDO_ERR_INVALID_ARGUMENT;
148
149
1.04k
        if ((udev = udev_new()) == NULL ||
150
1.04k
            (udev_enum = udev_enumerate_new(udev)) == NULL)
151
8
                goto fail;
152
153
1.03k
        if (udev_enumerate_add_match_subsystem(udev_enum, "nfc") < 0 ||
154
1.03k
            udev_enumerate_scan_devices(udev_enum) < 0)
155
4
                goto fail;
156
157
1.02k
        if ((udev_list = udev_enumerate_get_list_entry(udev_enum)) == NULL) {
158
2
                r = FIDO_OK; /* zero nfc devices */
159
2
                goto fail;
160
2
        }
161
162
458k
        udev_list_entry_foreach(udev_entry, udev_list) {
163
458k
                if (copy_info(&devlist[*olen], udev, udev_entry) == 0) {
164
0
                        devlist[*olen].io = (fido_dev_io_t) {
165
0
                                fido_nfc_open,
166
0
                                fido_nfc_close,
167
0
                                fido_nfc_read,
168
0
                                fido_nfc_write,
169
0
                        };
170
0
                        devlist[*olen].transport = (fido_dev_transport_t) {
171
0
                                fido_nfc_rx,
172
0
                                fido_nfc_tx,
173
0
                        };
174
0
                        if (++(*olen) == ilen)
175
0
                                break;
176
0
                }
177
458k
        }
178
179
1.02k
        r = FIDO_OK;
180
1.04k
fail:
181
1.04k
        if (udev_enum != NULL)
182
1.03k
                udev_enumerate_unref(udev_enum);
183
1.04k
        if (udev != NULL)
184
1.03k
                udev_unref(udev);
185
186
1.04k
        return r;
187
1.02k
}
188
189
static int
190
nfc_target_connect(struct nfc_linux *ctx)
191
0
{
192
0
        struct sockaddr_nfc sa;
193
194
0
        memset(&sa, 0, sizeof(sa));
195
0
        sa.sa_family = AF_NFC;
196
0
        sa.dev_idx = ctx->dev;
197
0
        sa.target_idx = ctx->target;
198
0
        sa.nfc_protocol = NFC_PROTO_ISO14443;
199
200
0
        if ((ctx->fd = socket(AF_NFC, SOCK_SEQPACKET | SOCK_CLOEXEC,
201
0
            NFC_SOCKPROTO_RAW)) == -1) {
202
0
                fido_log_error(errno, "%s: socket", __func__);
203
0
                return -1;
204
0
        }
205
0
        if (connect(ctx->fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
206
0
                fido_log_error(errno, "%s: connect", __func__);
207
0
                if (close(ctx->fd) == -1)
208
0
                        fido_log_error(errno, "%s: close", __func__);
209
0
                ctx->fd = -1;
210
0
                return -1;
211
0
        }
212
213
0
        return 0;
214
0
}
215
216
static void
217
nfc_free(struct nfc_linux **ctx_p)
218
902k
{
219
902k
        struct nfc_linux *ctx;
220
221
902k
        if (ctx_p == NULL || (ctx = *ctx_p) == NULL)
222
453k
                return;
223
448k
        if (ctx->fd != -1 && close(ctx->fd) == -1)
224
448k
                fido_log_error(errno, "%s: close", __func__);
225
448k
        if (ctx->nl != NULL)
226
279
                fido_nl_free(&ctx->nl);
227
228
448k
        free(ctx);
229
448k
        *ctx_p = NULL;
230
448k
}
231
232
static struct nfc_linux *
233
nfc_new(uint32_t dev)
234
449k
{
235
449k
        struct nfc_linux *ctx;
236
237
449k
        if ((ctx = calloc(1, sizeof(*ctx))) == NULL ||
238
449k
            (ctx->nl = fido_nl_new()) == NULL) {
239
449k
                nfc_free(&ctx);
240
449k
                return NULL;
241
449k
        }
242
243
279
        ctx->fd = -1;
244
279
        ctx->dev = dev;
245
246
279
        return ctx;
247
449k
}
248
249
void *
250
fido_nfc_open(const char *path)
251
453k
{
252
453k
        struct nfc_linux *ctx = NULL;
253
453k
        int idx;
254
255
453k
        if (strncmp(path, FIDO_NFC_PREFIX, strlen(FIDO_NFC_PREFIX)) != 0) {
256
0
                fido_log_debug("%s: bad prefix", __func__);
257
0
                goto fail;
258
0
        }
259
453k
        if ((idx = sysnum_from_syspath(path + strlen(FIDO_NFC_PREFIX))) < 0 ||
260
453k
            (ctx = nfc_new((uint32_t)idx)) == NULL) {
261
452k
                fido_log_debug("%s: nfc_new", __func__);
262
452k
                goto fail;
263
452k
        }
264
279
        if (fido_nl_power_nfc(ctx->nl, ctx->dev) < 0 ||
265
279
            fido_nl_get_nfc_target(ctx->nl, ctx->dev, &ctx->target) < 0 ||
266
279
            nfc_target_connect(ctx) < 0) {
267
279
                fido_log_debug("%s: netlink", __func__);
268
279
                goto fail;
269
279
        }
270
271
0
        return ctx;
272
453k
fail:
273
453k
        nfc_free(&ctx);
274
453k
        return NULL;
275
279
}
276
277
void
278
fido_nfc_close(void *handle)
279
0
{
280
0
        struct nfc_linux *ctx = handle;
281
282
0
        nfc_free(&ctx);
283
0
}
284
285
int
286
fido_nfc_set_sigmask(void *handle, const fido_sigset_t *sigmask)
287
0
{
288
0
        struct nfc_linux *ctx = handle;
289
290
0
        ctx->sigmask = *sigmask;
291
0
        ctx->sigmaskp = &ctx->sigmask;
292
293
0
        return FIDO_OK;
294
0
}
295
296
int
297
fido_nfc_read(void *handle, unsigned char *buf, size_t len, int ms)
298
0
{
299
0
        struct nfc_linux *ctx = handle;
300
0
        struct iovec iov[2];
301
0
        uint8_t preamble;
302
0
        ssize_t r;
303
304
0
        memset(&iov, 0, sizeof(iov));
305
0
        iov[0].iov_base = &preamble;
306
0
        iov[0].iov_len = sizeof(preamble);
307
0
        iov[1].iov_base = buf;
308
0
        iov[1].iov_len = len;
309
310
0
        if (fido_hid_unix_wait(ctx->fd, ms, ctx->sigmaskp) < 0) {
311
0
                fido_log_debug("%s: fido_hid_unix_wait", __func__);
312
0
                return -1;
313
0
        }
314
0
        if ((r = readv(ctx->fd, iov, nitems(iov))) == -1) {
315
0
                fido_log_error(errno, "%s: read", __func__);
316
0
                return -1;
317
0
        }
318
0
        if (r < 1) {
319
0
                fido_log_debug("%s: %zd < 1", __func__, r);
320
0
                return -1;
321
0
        }
322
0
        if (preamble != 0x00) {
323
0
                fido_log_debug("%s: preamble", __func__);
324
0
                return -1;
325
0
        }
326
327
0
        r--;
328
0
        fido_log_xxd(buf, (size_t)r, "%s", __func__);
329
330
0
        return (int)r;
331
0
}
332
333
int
334
fido_nfc_write(void *handle, const unsigned char *buf, size_t len)
335
0
{
336
0
        struct nfc_linux *ctx = handle;
337
0
        ssize_t r;
338
339
0
        fido_log_xxd(buf, len, "%s", __func__);
340
341
0
        if (len > INT_MAX) {
342
0
                fido_log_debug("%s: len", __func__);
343
0
                return -1;
344
0
        }
345
0
        if ((r = write(ctx->fd, buf, len)) == -1) {
346
0
                fido_log_error(errno, "%s: write", __func__);
347
0
                return -1;
348
0
        }
349
0
        if (r < 0 || (size_t)r != len) {
350
0
                fido_log_debug("%s: %zd != %zu", __func__, r, len);
351
0
                return -1;
352
0
        }
353
354
0
        return (int)r;
355
0
}