Bug 247498 - vd_kbstate not getting reset after keyboard detach/attach?
Summary: vd_kbstate not getting reset after keyboard detach/attach?
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 12.1-RELEASE
Hardware: Any Any
: --- Affects Some People
Assignee: freebsd-bugs (Nobody)
Keywords: vt
Depends on:
Reported: 2020-06-23 11:50 UTC by Bill Blake
Modified: 2020-06-23 16:54 UTC (History)
1 user (show)

See Also:


Note You need to log in before you can comment on or make changes to this bug.
Description Bill Blake 2020-06-23 11:50:54 UTC
I have discovered a condition under which pressing a key results in an input of the ESC character followed by the key WITHOUT my holding down any ALT key.  (See VT_ALT_TO_ESC_HACK.) This is using the vt driver on 12.1-RELEASE with no relevant changes to the GENERIC kernel config.  The bug is NOT reliably reproducible.

I discovered the bug while putting together a script to secure my laptop and power down the screen when I close my laptop's lid.  The script (invoked via devd), switches to the X console, uses DPMS to turn off my display, disables the mouse, and detaches the keyboard with "kbdcontrol -A atkbd0".  (Why, you ask?  Because when using vt, only X can turn off my display, but keyboard locking is worthless when running X. This was the only way I found to save power *and* secure my system when the lid is closed.)  When the lid is opened, a program is called to switch to an unused virtual console, reattach the keyboard (with the KBADDKB ioctl), and read a password (with console switching disabled).

Most of the time, this worked as expected.  Every once in awhile, however, the program would not accept my password.  After some debugging, I discovered that the reason was that the kernel was returning my keyboard input with an ESC character before every character I typed.  Looking around, I found that there is exactly one place where that could be happening, in sys/dev/vt/vt_core.c, line 961, and that it would do this only if ALKED is set in vd->vd_kbstate, a field that *should* only be set while one of my ALT keys is pressed.  I confirmed that this was the likely culprit by pressing my left ALT key while the bug manifested--the kernel immediately stopped adding the ESC characters.

I no longer have the kernel knowledge (or the time) track this bug to its lair, but it seems fairly obvious that vd_kbstate is not getting properly initialized when the keyboard is reattached.  This is relatively harmless; that field controls only the one thing, and the problem is remedied by merely hitting an ALT key.  Of greater concern is the possibility that some *other* field is not getting reinitialized after a keyboard detach/attach sequence, which suggests that a review of the keyboard attach code is in order.