Created attachment 178508 [details]
Logs from pcscd
pcscd installed on FreeBSD 11.0-CURRENT has serious problems when YubiKey was detached and reattached to machine. It looks like one of pcscd's threads hangs after device removal on function libusb_handle_events_completed() from libusb.
Steps to reproduce:
Install pcsc-lite either from ports or packages. Start pcscd demon in foreground mode with logging enabled:
pcscd -f -a -I
Try to attach YubiKey, list YubiKey status in GPG (gpg2 --card-status), detach it, reattach, list card status in GPG.
In my case the second attempt shows something like:
$ gpg2 --card-status
gpg: selecting openpgp failed: Operation not supported by device
while pcscd logs the following error:
02006441 ccid_usb.c:1323:InterruptStop() libusb_cancel_transfer failed: -5
14759547 winscard.c:261:SCardConnect() Reader Yubico Yubikey 4 OTP+U2F+CCID 00 00 Not Found
Expected result is that pcscd correctly reinitializes YubiKey and gpg shows correctly status of a YubiKey.
Also, there are questions from maintainer of pcscd and ccid related to libusb implementation in FreeBSD:
- Why libusb_handle_events_completed() does not return on device removal?
- How to cancel a transfer after device removal if libusb_cancel_transfer() fails with LIBUSB_ERROR_NOT_FOUND since the device is removed?
Please notice that on MacOS libusb_handle_events_completed() returns when the USB device is removed.
Here are the details about platform and installed software:
FreeBSD AMDC2690 11.0-RELEASE-p2 FreeBSD 11.0-RELEASE-p2 #0: Mon Oct 24 06:55:27 UTC 2016 firstname.lastname@example.org:/usr/obj/usr/src/sys/GENERIC amd64
pcsc-lite version 1.8.18.
Copyright (C) 1999-2002 by David Corcoran <email@example.com>.
Copyright (C) 2001-2015 by Ludovic Rousseau <firstname.lastname@example.org>.
Copyright (C) 2003-2004 by Damien Sauveron <email@example.com>.
Report bugs to <firstname.lastname@example.org>.
Enabled features: FreeBSD amd64-portbld-freebsd11.0 serial usb libusb usbdropdir=/usr/local/lib/pcsc/drivers/ ipcdir=/var/run/pcscd configdir=/usr/local/etc/reader.conf.d
ccid version is 1.4.25 patched to add more verbose logging.
Logs from verbose version of ccid are also attached to this mail.
Respects for Ludovico Rousseau for his help on debugging ccid and pcscd.
Can you set the LIBUSB_DEBUG environment before running the USB application?
env LIBUSB_DEBUG=0 program > log0.txt
env LIBUSB_DEBUG=1 program > log1.txt
env LIBUSB_DEBUG=2 program > log2.txt
And reproduce the issue?
libusb_handle_events_completed() only returns when the *completed variable is set to non-zero, typically when the USB transfer callbacks receive a CANCELLED error code. Maybe the pcscd APP doesn't check for this error code?
Also try to attach to the hung process using "gdb -p proc_number" and enter "bt" to get a backtrace. It will be helpful.
Q: How to cancel a transfer after device removal if libusb_cancel_transfer() fails with LIBUSB_ERROR_NOT_FOUND since the device is removed?
A: The FreeBSD USB stack will automatically cancel all pending transfers belonging to a USB device when it is detached and upon receiving a transfer status of LIBUSB_TRANSFER_CANCELLED, pcscd should not try re-submit the USB transfer and set its termination flag.