Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/external/bsd/libfido2/dist/src replace our merged version wi...



details:   https://anonhg.NetBSD.org/src/rev/96a5a48894b5
branches:  trunk
changeset: 1021785:96a5a48894b5
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Jun 17 12:53:43 2021 +0000

description:
replace our merged version with upstream.

diffstat:

 external/bsd/libfido2/dist/src/hid_netbsd.c |  203 ++++++++++-----------------
 1 files changed, 76 insertions(+), 127 deletions(-)

diffs (truncated from 304 to 300 lines):

diff -r f7ae942362d9 -r 96a5a48894b5 external/bsd/libfido2/dist/src/hid_netbsd.c
--- a/external/bsd/libfido2/dist/src/hid_netbsd.c       Thu Jun 17 12:45:58 2021 +0000
+++ b/external/bsd/libfido2/dist/src/hid_netbsd.c       Thu Jun 17 12:53:43 2021 +0000
@@ -11,23 +11,22 @@
 #include <dev/usb/usbhid.h>
 
 #include <errno.h>
-#include <fcntl.h>
 #include <poll.h>
+#include <signal.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include <usbhid.h>
 
 #include "fido.h"
 
 #define MAX_UHID       64
 
 struct hid_netbsd {
-       int     fd;
-       size_t  report_in_len;
-       size_t  report_out_len;
+       int             fd;
+       size_t          report_in_len;
+       size_t          report_out_len;
        sigset_t        sigmask;
        const sigset_t *sigmaskp;
 };
@@ -41,47 +40,39 @@
 static bool
 is_fido(int fd)
 {
-       report_desc_t                   rdesc;
-       hid_data_t                      hdata;
-       hid_item_t                      hitem;
-       bool                            isfido;
+       struct usb_ctl_report_desc      ucrd;
+       uint32_t                        usage_page = 0;
        int                             raw = 1;
 
-       if ((rdesc = hid_get_report_desc(fd)) == NULL) {
-               fido_log_debug("%s: failed to get report descriptor",
-                   __func__);
-               return (false);
-       }
-       if ((hdata = hid_start_parse(rdesc, 1 << hid_collection, -1))
-           == NULL) {
-               fido_log_debug("%s: failed to parse report descriptor",
-                   __func__);
-               hid_dispose_report_desc(rdesc);
+       memset(&ucrd, 0, sizeof(ucrd));
+
+       if (ioctl(fd, IOCTL_REQ(USB_GET_REPORT_DESC), &ucrd) == -1) {
+               fido_log_error(errno, "%s: ioctl", __func__);
                return (false);
        }
-       isfido = false;
-       while ((hid_get_item(hdata, &hitem)) > 0) {
-               if (HID_PAGE(hitem.usage) == 0xf1d0) {
-                       isfido = true;
-                       break;
-               }
+
+       if (ucrd.ucrd_size < 0 ||
+           (size_t)ucrd.ucrd_size > sizeof(ucrd.ucrd_data) ||
+           fido_hid_get_usage(ucrd.ucrd_data, (size_t)ucrd.ucrd_size,
+               &usage_page) < 0) {
+               fido_log_debug("%s: fido_hid_get_usage", __func__);
+               return (false);
        }
-       hid_end_parse(hdata);
-       hid_dispose_report_desc(rdesc);
-       if (!isfido)
+
+       if (usage_page != 0xf1d0)
                return (false);
 
-        /*
+       /*
         * This step is not strictly necessary -- NetBSD puts fido
-         * devices into raw mode automatically by default, but in
-         * principle that might change, and this serves as a test to
-         * verify that we're running on a kernel with support for raw
-         * mode at all so we don't get confused issuing writes that try
-         * to set the report descriptor rather than transfer data on
-         * the output interrupt pipe as we need.
+        * devices into raw mode automatically by default, but in
+        * principle that might change, and this serves as a test to
+        * verify that we're running on a kernel with support for raw
+        * mode at all so we don't get confused issuing writes that try
+        * to set the report descriptor rather than transfer data on
+        * the output interrupt pipe as we need.
         */
-       if (ioctl(fd, USB_HID_SET_RAW, &raw) == -1) {
-               fido_log_debug("%s: unable to set raw", __func__);
+       if (ioctl(fd, IOCTL_REQ(USB_HID_SET_RAW), &raw) == -1) {
+               fido_log_error(errno, "%s: unable to set raw", __func__);
                return (false);
        }
 
@@ -98,17 +89,13 @@
        memset(di, 0, sizeof(*di));
        memset(&udi, 0, sizeof(udi));
 
-       if ((fd = open(path, O_RDWR)) == -1) {
-               if (errno != EBUSY && errno != ENOENT)
-                       fido_log_debug("%s: open %s: %s", __func__, path,
-                           strerror(errno));
+       if ((fd = fido_hid_unix_open(path)) == -1 || is_fido(fd) == 0)
+               goto fail;
+
+       if (ioctl(fd, IOCTL_REQ(USB_GET_DEVICEINFO), &udi) == -1) {
+               fido_log_error(errno, "%s: ioctl", __func__);
                goto fail;
        }
-       if (!is_fido(fd))
-               goto fail;
-
-       if (ioctl(fd, USB_GET_DEVICEINFO, &udi) == -1)
-               goto fail;
 
        if ((di->path = strdup(path)) == NULL ||
            (di->manufacturer = strdup(udi.udi_vendor)) == NULL ||
@@ -120,8 +107,8 @@
 
        ok = 0;
 fail:
-       if (fd != -1)
-               close(fd);
+       if (fd != -1 && close(fd) == -1)
+               fido_log_error(errno, "%s: close", __func__);
 
        if (ok < 0) {
                free(di->path);
@@ -198,7 +185,7 @@
                pfd.fd = ctx->fd;
                pfd.events = POLLIN;
                if ((n = poll(&pfd, 1, 100)) == -1) {
-                       fido_log_debug("%s: poll: %d", __func__, errno);
+                       fido_log_error(errno, "%s: poll", __func__);
                        return -1;
                } else if (n == 0) {
                        fido_log_debug("%s: timed out", __func__);
@@ -223,58 +210,40 @@
 fido_hid_open(const char *path)
 {
        struct hid_netbsd               *ctx;
-       report_desc_t                   rdesc = NULL;
-       hid_data_t                      hdata;
-       int                             len, report_id = 0;
+       struct usb_ctl_report_desc       ucrd;
+       int                              r;
+
+       memset(&ucrd, 0, sizeof(ucrd));
 
-       if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
-               goto fail0;
-       if ((ctx->fd = open(path, O_RDWR)) == -1)
-               goto fail1;
-       if (ioctl(ctx->fd, USB_GET_REPORT_ID, &report_id) == -1) {
-               fido_log_debug("%s: failed to get report ID: %s", __func__,
-                   strerror(errno));
-               goto fail2;
-       }
-       if ((rdesc = hid_get_report_desc(ctx->fd)) == NULL) {
-               fido_log_debug("%s: failed to get report descriptor",
-                   __func__);
-               goto fail2;
+       if ((ctx = calloc(1, sizeof(*ctx))) == NULL ||
+           (ctx->fd = fido_hid_unix_open(path)) == -1) {
+               free(ctx);
+               return (NULL);
        }
-       if ((hdata = hid_start_parse(rdesc, 1 << hid_collection, -1))
-           == NULL) {
-               fido_log_debug("%s: failed to parse report descriptor",
-                   __func__);
-               goto fail3;
+
+       if ((r = ioctl(ctx->fd, IOCTL_REQ(USB_GET_REPORT_DESC), &ucrd)) == -1 ||
+           ucrd.ucrd_size < 0 ||
+           (size_t)ucrd.ucrd_size > sizeof(ucrd.ucrd_data) ||
+           fido_hid_get_report_len(ucrd.ucrd_data, (size_t)ucrd.ucrd_size,
+               &ctx->report_in_len, &ctx->report_out_len) < 0) {
+               if (r == -1)
+                       fido_log_error(errno, "%s: ioctl", __func__);
+               fido_log_debug("%s: using default report sizes", __func__);
+               ctx->report_in_len = CTAP_MAX_REPORT_LEN;
+               ctx->report_out_len = CTAP_MAX_REPORT_LEN;
        }
-       if ((len = hid_report_size(rdesc, hid_input, report_id)) <= 0 ||
-           (size_t)len > CTAP_MAX_REPORT_LEN) {
-               fido_log_debug("%s: bad input report size %d", __func__, len);
-               goto fail3;
-       }
-       ctx->report_in_len = (size_t)len;
-       if ((len = hid_report_size(rdesc, hid_output, report_id)) <= 0 ||
-           (size_t)len > CTAP_MAX_REPORT_LEN) {
-               fido_log_debug("%s: bad output report size %d", __func__, len);
-               goto fail3;
-       }
-       ctx->report_out_len = (size_t)len;
-       hid_dispose_report_desc(rdesc);
 
        /*
         * NetBSD has a bug that causes it to lose
         * track of the DATA0/DATA1 sequence toggle across uhid device
         * open and close. This is a terrible hack to work around it.
         */
-       if (!is_fido(ctx->fd) || terrible_ping_kludge(ctx) != 0)
-               goto fail2;
+       if (!is_fido(ctx->fd) || terrible_ping_kludge(ctx) != 0) {
+               fido_hid_close(ctx);
+               return NULL;
+       }
 
        return (ctx);
-
-fail3: hid_dispose_report_desc(rdesc);
-fail2: close(ctx->fd);
-fail1: free(ctx);
-fail0: return (NULL);
 }
 
 void
@@ -282,39 +251,10 @@
 {
        struct hid_netbsd *ctx = handle;
 
-       close(ctx->fd);
-       free(ctx);
-}
-
-static void
-xstrerror(int errnum, char *buf, size_t len)
-{
-       if (len < 1)
-               return;
-
-       memset(buf, 0, len);
-
-       if (strerror_r(errnum, buf, len - 1) != 0)
-               snprintf(buf, len - 1, "error %d", errnum);
-}
+       if (close(ctx->fd) == -1)
+               fido_log_error(errno, "%s: close", __func__);
 
-static int
-timespec_to_ms(const struct timespec *ts, int upper_bound)
-{
-       int64_t x;
-       int64_t y;
-
-       if (ts->tv_sec < 0 || (uint64_t)ts->tv_sec > INT64_MAX / 1000LL ||
-           ts->tv_nsec < 0 || (uint64_t)ts->tv_nsec / 1000000LL > INT64_MAX)
-               return (upper_bound);
-
-       x = ts->tv_sec * 1000LL;
-       y = ts->tv_nsec / 1000000LL;
-
-       if (INT64_MAX - x < y || x + y > upper_bound)
-               return (upper_bound);
-
-       return (int)(x + y);
+       free(ctx);
 }
 
 int
@@ -344,8 +284,13 @@
                return (-1);
        }
 
-       if ((r = read(ctx->fd, buf, len)) == -1 || (size_t)r != len) {
-               fido_log_debug("%s: read", __func__);
+       if ((r = read(ctx->fd, buf, len)) == -1) {
+               fido_log_error(errno, "%s: read", __func__);
+               return (-1);
+       }
+
+       if (r < 0 || (size_t)r != len) {
+               fido_log_error(errno, "%s: %zd != %zu", __func__, r, len);
                return (-1);
        }
 
@@ -363,9 +308,13 @@
                return (-1);
        }
 
-       if ((r = write(ctx->fd, buf + 1, len - 1)) == -1 ||
-           (size_t)r != len - 1) {
-               fido_log_debug("%s: write", __func__);
+       if ((r = write(ctx->fd, buf + 1, len - 1)) == -1) {
+               fido_log_error(errno, "%s: write", __func__);
+               return (-1);
+       }
+
+       if (r < 0 || (size_t)r != len - 1) {



Home | Main Index | Thread Index | Old Index