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)
URL:
Keywords: vt
Depends on:
Blocks:
 
Reported: 2020-06-23 11:50 UTC by Bill Blake
Modified: 2023-09-24 15:50 UTC (History)
1 user (show)

See Also:


Attachments

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.
Comment 1 commit-hook freebsd_committer freebsd_triage 2022-03-10 21:05:56 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=a2774a9264eb11b26fc6be4127a8c0da1ead297d

commit a2774a9264eb11b26fc6be4127a8c0da1ead297d
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2022-02-24 15:38:16 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2022-03-10 21:04:57 +0000

    vt: clarify comments on kbd add/release

    PR:             247498

 sys/dev/vt/vt_core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Comment 2 commit-hook freebsd_committer freebsd_triage 2023-09-24 15:50:20 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=1b4d7a2ee76121cada89d6f17fab0feed42c3bff

commit 1b4d7a2ee76121cada89d6f17fab0feed42c3bff
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2022-02-24 15:38:16 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2023-09-24 15:48:34 +0000

    vt: clarify comments on kbd add/release

    PR:             247498

    (cherry picked from commit a2774a9264eb11b26fc6be4127a8c0da1ead297d)

 sys/dev/vt/vt_core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)