snp(4): a wakeup from snp_getc_capture() can be lost
snp_poll() checks ttyoutq_bytesused() while holding the tty lock.
It then calls tty_unlock(), and only after that calls selrecord().
snp_getc_capture() can run in that window, queue data and call
selwakeup() before the poller is on si_tdlist. If that happens, the
poller sleeps even though data is already queued.
Call selrecord() under the tty lock.
Reported by: llm-postcr
Fixes: 932ef5b5cdfd ("Reintroduce the snp(4) driver")
snp(4): drain the users of the tty before detaching it
Since SNPSTTY can detach the tty, snp_tty can change while other
threads use the snoop device. snp_read(), snp_write(), snp_poll(),
and the FIONREAD, SNPGTTY and SNPGTYY_32DEV ioctls read snp_tty
without snp_register_lock and then used the tty. The detach
unregisters the hook, and if the tty is already gone, tty_rel_free()
schedules tty_dealloc(), which frees the tty without waiting for the
snp threads. snp_read() is exposed for as long as it sleeps for data
or copies out with the tty unlocked, and the detach also freed the
output queue under it. The attach path sized the queue after
dropping snp_register_lock, with the same exposure.
Hold snp_register_lock in the paths that use the tty only briefly:
snp_poll(), the ioctls, and the queue sizing on attach. snp_read()
and snp_write(), which sleep or drop the tty lock, take a use
reference counted in snp_busy under the tty lock. The new
snp_detach(), used by SNPSTTY and snp_dtor(), clears snp_tty and
wakes up the readers under the tty lock, waits for the references to
drain, and only then frees the queue and unregisters the hook. A
reader that sleeps for data when the tty is detached now gets EIO.
Holding snp_register_lock in snp_poll() also closes the window where
an attach and the first capture happen between reading snp_tty as
NULL and selrecord().
Reported by: llm-postcr
Fixes: 550e01c55247 ("snp(4): implement detach")
tty: call the close hook when the TTY is gone
tty_rel_gone() wakes up the threads that wait on the TTY itself, but
does not notify the hook, which only hears about the last close from
ttydisc_close(). The TTY often becomes gone after its last close,
e.g. when the master side of a pts is closed after the slave, so a
hook consumer has no way to learn that the TTY went away.
Call th_close from tty_rel_gone() too. A consumer may now see
th_close twice, on the last close and when the TTY is gone, in either
order. There are no in-tree consumers of th_close yet.
snp(4): wake up the readers and pollers when the tty is gone
snp(4) did not register th_close, and snp_poll() only reported queued
data. When the snooped tty went away, nothing woke up a thread
sleeping in select(2) or poll(2) on the snoop device, and a rescan
would not report the gone tty anyway. watch(8) waits in select(2)
and only then asks FIONREAD, which returns SNP_TTYCLOSE, so it hung
once the tty stopped producing output.
Register th_close, which also runs when the tty is gone, to wake up
the readers and the pollers, and return POLLHUP from snp_poll() for a
gone tty. select(2) treats POLLHUP as readable.
Fixes: 932ef5b5cdfd ("Reintroduce the snp(4) driver")
watch(8): detach the gone tty before attaching again
snp(4) keeps a gone tty attached until it is detached or the snoop
device is closed. On SNP_TTYCLOSE, watch(8) asked for a new device,
or reconnected with -c, without detaching the old tty first. SNPSTTY
then failed with EBUSY, and watch exited with "cannot attach to tty".
Detach the gone tty before attaching again.Details
Details
Diff Detail
Diff Detail
- Repository
- rG FreeBSD src repository
- Lint
Lint Skipped - Unit
Tests Skipped