From nobody Fri Sep 22 11:07:34 2023 X-Original-To: dev-commits-src-all@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 4RsTxy5GfRz4tjD2; Fri, 22 Sep 2023 11:07:34 +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 4RsTxy4TPHz4v6b; Fri, 22 Sep 2023 11:07:34 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1695380854; 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=6v4+zgn7nHyC94n89iVPNh7qik+V9sAY4mv8Q/SmbP8=; b=rIlXrcFpYO81shZxWqmC/8iHBnJfHea4I/J/Of4jWAFmIHRCC6g59idMOqbYWnN4seR9no aVU4Pm4FUlav2sEbXv0uAI3zP+sWgU8+9z0os+LD//KgzZkOBmGgAr4a+iulR/jCWIh1Ie nUOYQUK79YFDJdmGkzWW8Ud9wgIEr3k19HSNETliToq1T70Ud6yWgvGR7ith/Sg9XvRurX lks2FBPJ903UywK7Dp7saXqyaxzeu15cZo3KrzuWkov4KN36S/tU6zOwtfrQoTYkpZep5t ZL1EzCzZMBhcmWvAe85y+OqoNprXy0qareVcnGzI2s74gc3FpQugm6Po2fGLjg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1695380854; a=rsa-sha256; cv=none; b=N85PaljzKmc0cBC1RCpz+6eES5iWaq/9Ud2hdENzGRcHKK6TNuvkY+tE+etcER/iSEwFt7 bfu5J3dzJxpIsRChN7ZSJkReCl6jydB2z7mp3F64R3kmn88JFlAhToCVT1RZqPmpockwzF tcgAR0IXB0iL/dV4L67zO4PjJkULfaTn35ZtK3w5I+pZVwJZvBpovtpjO9waiIyvorXesx Pnvy7pcyRoU7GeN4w+2hTDj47PjDTOoGgqszwgHrpZFW1SW2ns1XwkF9Y7aXW6rpIqSZcy xB1XKsvANXU1iaxkKY8bRjqts1AJP7IhDEnrZA9h9bvj6atSfIsFfLiT5QOjbQ== 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=1695380854; 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=6v4+zgn7nHyC94n89iVPNh7qik+V9sAY4mv8Q/SmbP8=; b=SGdHYdO0Fk5XvinTENAwjrmfGAQVq7O37ZymOXXxiW/H9nEFjQrlKqB/GlhsvZd/yvSC01 wccKJJUE8S6RxYg3gbkKTgUPJt+xFIjZwBr/cdFZaIHIeyDkWLnUc9e31yFtWpl6MFUkvO 6zDojG3uqfu9RDcrzQmbPSm5Q/YElwATUvNCiJULRiFJMVwj0KY36kraAdzMG5GrQ8GWK0 eyGNIeMBtFxSWEKpFTUjzc8WOhhWXQgcUDrBHxqI6IWdJAOgXQN6gmgqnEEuvXorK+hT1v /dbCQN4sNcmhfoN261aOGux4/KAXEsunFGu4awYvld6azTcSzXM+QGoMmvjjXw== 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 4RsTxy3XbJz4WJ; Fri, 22 Sep 2023 11:07:34 +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 38MB7Y7n065992; Fri, 22 Sep 2023 11:07:34 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 38MB7YEf065989; Fri, 22 Sep 2023 11:07:34 GMT (envelope-from git) Date: Fri, 22 Sep 2023 11:07:34 GMT Message-Id: <202309221107.38MB7YEf065989@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: 3e85eb3c5ef0 - stable/13 - libfido2: backport hidraw(4) support List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@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/13 X-Git-Reftype: branch X-Git-Commit: 3e85eb3c5ef0c08ae3a7db543d72713c365ca3a4 Auto-Submitted: auto-generated The branch stable/13 has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=3e85eb3c5ef0c08ae3a7db543d72713c365ca3a4 commit 3e85eb3c5ef0c08ae3a7db543d72713c365ca3a4 Author: Ed Maste AuthorDate: 2023-09-19 16:50:13 +0000 Commit: Ed Maste CommitDate: 2023-09-22 11:07:25 +0000 libfido2: backport hidraw(4) support PR: 273596 Submitted by: David Horn Obtained from: libfido2 2c9dfaae2a56, 930160388700 (cherry picked from commit 1843dfb05ed80149f5a412180af882e3cb8f451b) (cherry picked from commit 3df74eecfdecbafe55838b45141c275754d526bb) --- 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,