Coverage Report

Created: 2022-04-27 14:33

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