## Coverage Report

#### Created: 2025-03-01 02:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)

    /libfido2/src/nfc_linux.c

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