From nobody Fri Sep 22 11:03:45 2023 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4RsTsZ0KQfz4tjFd; Fri, 22 Sep 2023 11:03:46 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RsTsY6v7hz4tGB; Fri, 22 Sep 2023 11:03:45 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1695380626; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=1Hdf+BTU9lXdjBhGt3OSDlTHHqd+ciaK+Dagh7ts6fg=; b=VnUNoM7g3C3f05jU9wjz3Hrrb8BTzoY0REuXOYo+99YK2ecN4MrZiEh9r23RRok2Eb/kir U+paNLfE/ggsPZhjEsCCc56uhsN3lnzJ8sLIKeSEYtWVu++dhPmyZ97fRNt6RnQELmZ8aG PY4yKm0PG5YeF+m7OUOeIBc7Mxrv1eWIA1ElrxB0k4ZzrQK8e3Bg/qJdEFUOds5F80xyVJ /B9Sgjd6ySQLbBr3e+8NDVlz+wHJ8a+PasKsZTxgVBklWHEJu6Qi1fnppeiMlx0vHkfsAz 6oJi5VUtCyIDgbr5IN92utThDB43/krq9ghKm1C1AkzjGRznK3JYvwpLNElinA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1695380626; a=rsa-sha256; cv=none; b=JsUjvQ0kFCWA/nMK1MWmuu+YHM8MBsTXpFjHoDeFvjz4UOmoJ8tXeai8iZxR1y1GGrLSop FsyXBIqjWijjU15sTSQrs/Stz6seSkyfYFf93UlbltzQBMZtT5y2f6X6Pp7Zq6SkbhEvQh 3I/gu/WnaFpRa6JU2q2lZIHMjQyoiCD68542houRnjrd5SmLdP/IiAa5RYTJ24XgrgRASb vEfLz0GhWj/GzbXcf0tsBOIDt89YCcjdA51w1cS8VCuPTVGaSUzmQES7RRXMvVslCt7P8E XXDY0ZeYC0y6AJfnvcGTAc/QG4+Va0fpMUftHkWMxf/mELdKRNkIMGueufbVPg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1695380626; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=1Hdf+BTU9lXdjBhGt3OSDlTHHqd+ciaK+Dagh7ts6fg=; b=HUuQoRaLOd4QHdEdkuLB4s8wTjxITehfYm5pKMl4CMsYLnpo1Px/wxU+fnAHS2Pf1EaxgX dsH1dAV7Mp0L4EGbpa4d7hFMesokJ+K28qNK9XYIbInPLibRZIHrc1qeGR4yMGpLevPHCQ H+yxVN4wI4aXfrMGAwtQIhC27WUUc/cnfdH0EMHecn3s0YbDBTtYcEgEaoTIV1hRVukG5D fhvD348RtgA/JSoGqdfapfWWp2dYRk6o0zUTpSHwWOT1kxpqhzvtB6N3b1V/INYDCvMIkh T1hQ1NDcjKXUD8HzHkP/trbhA7sB+00CvUN66rrqSm6+djwj6R8dOK5En6l6vQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4RsTsY5xWzz42r; Fri, 22 Sep 2023 11:03:45 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 38MB3jpN065308; Fri, 22 Sep 2023 11:03:45 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 38MB3jv9065305; Fri, 22 Sep 2023 11:03:45 GMT (envelope-from git) Date: Fri, 22 Sep 2023 11:03:45 GMT Message-Id: <202309221103.38MB3jv9065305@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Ed Maste Subject: git: 3df74eecfdec - stable/14 - libfido2: backport hidraw(4) support List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: emaste X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 3df74eecfdecbafe55838b45141c275754d526bb Auto-Submitted: auto-generated The branch stable/14 has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=3df74eecfdecbafe55838b45141c275754d526bb commit 3df74eecfdecbafe55838b45141c275754d526bb Author: Ed Maste AuthorDate: 2023-09-19 16:50:13 +0000 Commit: Ed Maste CommitDate: 2023-09-22 11:03:35 +0000 libfido2: backport hidraw(4) support PR: 273596 Submitted by: David Horn Obtained from: libfido2 2c9dfaae2a56, 930160388700 (cherry picked from commit 1843dfb05ed80149f5a412180af882e3cb8f451b) --- contrib/libfido2/src/hid_freebsd.c | 97 ++++++++++++++++++++++++++++++++++---- 1 file changed, 87 insertions(+), 10 deletions(-) diff --git a/contrib/libfido2/src/hid_freebsd.c b/contrib/libfido2/src/hid_freebsd.c index 5aefe69c1bec..5151690afc0a 100644 --- a/contrib/libfido2/src/hid_freebsd.c +++ b/contrib/libfido2/src/hid_freebsd.c @@ -1,13 +1,17 @@ /* - * Copyright (c) 2020 Yubico AB. All rights reserved. + * Copyright (c) 2020-2022 Yubico AB. All rights reserved. * Use of this source code is governed by a BSD-style * license that can be found in the LICENSE file. */ -#include +#include #include #include +#if __FreeBSD_version >= 1300500 +#include +#define USE_HIDRAW /* see usbhid(4) and hidraw(4) on FreeBSD 13+ */ +#endif #include #include @@ -57,8 +61,60 @@ is_fido(int fd) return (usage_page == 0xf1d0); } +#ifdef USE_HIDRAW +static int +copy_info_hidraw(fido_dev_info_t *di, const char *path) +{ + int fd = -1; + int ok = -1; + struct usb_device_info udi; + struct hidraw_devinfo devinfo; + char rawname[129]; + + memset(di, 0, sizeof(*di)); + memset(&udi, 0, sizeof(udi)); + memset(&devinfo, 0, sizeof(devinfo)); + memset(rawname, 0, sizeof(rawname)); + + if ((fd = fido_hid_unix_open(path)) == -1 || is_fido(fd) == 0) + goto fail; + + if (ioctl(fd, IOCTL_REQ(USB_GET_DEVICEINFO), &udi) == -1) { + if (ioctl(fd, IOCTL_REQ(HIDIOCGRAWINFO), &devinfo) == -1 || + ioctl(fd, IOCTL_REQ(HIDIOCGRAWNAME(128)), rawname) == -1 || + (di->path = strdup(path)) == NULL || + (di->manufacturer = strdup(UHID_VENDOR)) == NULL || + (di->product = strdup(rawname)) == NULL) + goto fail; + di->vendor_id = devinfo.vendor; + di->product_id = devinfo.product; + } else { + if ((di->path = strdup(path)) == NULL || + (di->manufacturer = strdup(udi.udi_vendor)) == NULL || + (di->product = strdup(udi.udi_product)) == NULL) + goto fail; + di->vendor_id = (int16_t)udi.udi_vendorNo; + di->product_id = (int16_t)udi.udi_productNo; + } + + ok = 0; +fail: + if (fd != -1 && close(fd) == -1) + fido_log_error(errno, "%s: close %s", __func__, path); + + if (ok < 0) { + free(di->path); + free(di->manufacturer); + free(di->product); + explicit_bzero(di, sizeof(*di)); + } + + return (ok); +} +#endif /* USE_HIDRAW */ + static int -copy_info(fido_dev_info_t *di, const char *path) +copy_info_uhid(fido_dev_info_t *di, const char *path) { int fd = -1; int ok = -1; @@ -81,14 +137,13 @@ copy_info(fido_dev_info_t *di, const char *path) (di->manufacturer = strdup(udi.udi_vendor)) == NULL || (di->product = strdup(udi.udi_product)) == NULL) goto fail; - di->vendor_id = (int16_t)udi.udi_vendorNo; di->product_id = (int16_t)udi.udi_productNo; ok = 0; fail: - if (fd != -1) - close(fd); + if (fd != -1 && close(fd) == -1) + fido_log_error(errno, "%s: close %s", __func__, path); if (ok < 0) { free(di->path); @@ -106,17 +161,35 @@ fido_hid_manifest(fido_dev_info_t *devlist, size_t ilen, size_t *olen) char path[64]; size_t i; - *olen = 0; - if (ilen == 0) return (FIDO_OK); /* nothing to do */ if (devlist == NULL || olen == NULL) return (FIDO_ERR_INVALID_ARGUMENT); - for (i = *olen = 0; i < MAX_UHID && *olen < ilen; i++) { + *olen = 0; + +#ifdef USE_HIDRAW + for (i = 0; i < MAX_UHID && *olen < ilen; i++) { + snprintf(path, sizeof(path), "/dev/hidraw%zu", i); + if (copy_info_hidraw(&devlist[*olen], path) == 0) { + devlist[*olen].io = (fido_dev_io_t) { + fido_hid_open, + fido_hid_close, + fido_hid_read, + fido_hid_write, + }; + ++(*olen); + } + } + /* hidraw(4) is preferred over uhid(4) */ + if (*olen != 0) + return (FIDO_OK); +#endif /* USE_HIDRAW */ + + for (i = 0; i < MAX_UHID && *olen < ilen; i++) { snprintf(path, sizeof(path), "/dev/uhid%zu", i); - if (copy_info(&devlist[*olen], path) == 0) { + if (copy_info_uhid(&devlist[*olen], path) == 0) { devlist[*olen].io = (fido_dev_io_t) { fido_hid_open, fido_hid_close, @@ -153,6 +226,10 @@ fido_hid_open(const char *path) ugd.ugd_data = buf; ugd.ugd_maxlen = sizeof(buf); + /* + * N.B. if ctx->fd is an hidraw(4) device, the ioctl() below puts it in + * uhid(4) compat mode, which we need to keep fido_hid_write() as-is. + */ if ((r = ioctl(ctx->fd, IOCTL_REQ(USB_GET_REPORT_DESC), &ugd) == -1) || ugd.ugd_actlen > sizeof(buf) || fido_hid_get_report_len(ugd.ugd_data, ugd.ugd_actlen,