I’ve got my computer setup to use yubikey challenge-response authentication for most things (unlocking my KeepassXC databasee, user logins and disk encryption). I’ve had problems multiple times with permissions for this setup: The Yubikey challenge-response implementation needs to maintain some state. This state can either be stored in the user’s home directory, or some root-accessible directory.
Tools like swaylock generally do not run as root, and so the per-user implementation is generally
more useful (it took me a while to figure this out). For system authentication via PAM, the
authentication is done via the polkit-agent-helper that runs as root. Because this helper is very
security-sensitive, it is heavily sandboxed as of the time of writing.
While this makes sense, it breaks Yubico authentication: Any two-factor authentication obviously requires USB access, and the Yubico implementation requires read-write access to the relevant home directory.
Additionally, the yubico PAM module uses AF_NETLINK for something (I figured this out by using
strace ykchalresp 12345).
Updated!
To enable Yubico challenge-response authentication with the “new” sandboxed agent helper, it is thus
sufficient to add the four following lines to the service configuration via systemctl edit polkit-agent-helper@.service:
[Service]
# Allow access to $HOME/.yubico for challenge-response state
ProtectHome=false
# Allow USB access
PrivateDevices=false
DevicePolicy=closed
DeviceAllow=char-usb_device rw
# Allow netlink access for pam_yubico.
RestrictAddressFamilies=AF_UNIX AF_NETLINK
The Debian manpage for polkit-agent-helper alludes to the authentication helper perhaps requiring
additional privileges, but finding the actual permissions needed was left as an exercise for the reader.
Extra info: Debugging
For debugging, I allowed the helper to write /tmp/ and configured the Yubico PAM module for
debugging in /etc/pam.d/common-auth. To do this, edit the PAM file, and append debug debug_file=/tmp/yubico_debug to the line configuring the yubico module. It should look something like this:
auth required pam_yubico.so mode=challenge-response debug debug_file=/tmp/yubico_debug
Then, allow access to the /tmp/ directory by disabling PrivateTmp and allowing Read/write access
to the debug file.
PrivateTmp=false
ReadWritePaths=/tmp/yubico_debug
Extra info: More security
Two configuration settings for better security immediately spring to mind: Restricting the paths to
/home/<something>/.yubico/ and only exposing the Yubico USB device, not all of them.
I currently don’t know how I would implement this, feel free to contact me if you have ideas.
UPDATE: Using DeviceAllow seems to allow access to only a specific set of devices (but only if
PrivateDevices is false). This seems like a more secure configuration compared to only setting
PrivateDevices to false. My personal thinking seems to be that PrivateDevices can be combined
with DeviceAllow, but this does not seem to be the case. Only one of the two configuration option
makes sense to use at a time, with PrivateDevices being the “big hammer” (which is suitable for
most use cases where hardware access is not needed).