tty: n_tty: Restore EOF push handling behavior
cover tty: n_tty: Restore EOF push handling behavior
Since Kernel 5.12 there was a bug in Pseudo-Terminals (it has been fixed recently) in ICANON mode where the terminating newline was missing in a read() if the line had exactly 64 chars PLUS newline - and that newline was then returned at the beginning of the next line that was read().
https://bugzilla.kernel.org/show_bug.cgi?id=215611 has more information on this bug, but I'll summarize things relevant to this patch here:
This bug was introduced by 3b830a9c34d5 ("tty: convert tty_ldisc_ops 'read()' function to take a kernel pointer") but it turned out that a slight variation of it existed even before (if the newline was just after the buffer size passed to read()).
That bug had been introduced by ac8f3bf8832a ("n_tty: Fix poll() after buffer-limited eof push read") which fixed an edge-case very similar to the one it broke - if there was a "EOF" pseudo-EOL character (that's a __DISABLED_CHAR and thus not supposed to be returned to the reader) directly after a read-buffer-sized line, this EOF character was supposed to be silently discarded - and while doing this the case of a regular EOL character (that's supposed to be returned to the reader, like '\n') directly following the read-buffer was broken.
As this original bug was less likely to be triggered than the newer 64 char case (I guess usually the reader passes a buffer that's big enough to hold the whole line) it has never been found.
This EOF/pseudo-EOL character handling is also called "EOF pushing", and the character that's used for it is EOT (ASCII 0x4). Like a regular EOL character it terminates a line that's written to a pseudo-terminal, but unlike a regular EOL character it's not returned to the reader.
Anyway, the fix for the bug with 64 chars + newline (359303076163 "tty: n_tty: do not look ahead for EOL character past the end of the buffer") broke the edge case of discarding EOT after the read buffer size, which causes the next read() to return 0, because it's detected as an empty line with a newline-character that's not returned to the reader.
read() returning 0 in this case actually *is* valid behavior (FreeBSD and OSX do it), and has the advantage that the reader can interpret it as "the line I just received is over" - otherwise the reader can't know, could as well have been an even longer line and the previous read() just returned the first part of it.
On the other hand, read() returning 0 means EOF and the reader could misinterpret it as "there's no more data, probably the other side disconnected" and stop reading or even close the pseudo-terminal. So it might indeed be a good idea to avoid this by silently discarding EOF (EOT) characters and returning the next line, like Linux did before. Solaris (or at least OpenIndiana 2021.10) does the same.
While both options are defensible (and are used by other Unix-likes), IMHO it's best to stick to the "old" Linux behavior since 40d5e0905a03 ("n_tty: Fix EOF push handling") of 2013, i.e. silently discard the EOF.
The following patch, which is based on a patch Linus gave me for testing, restores that behavior.
I wrote a pretty extensive test for several edge-cases of PTYs in ICANON mode (including the bugs mentioned before) which you can download at:
https://bugzilla.kernel.org/show_bug.cgi?id=215611#c10
I plan to port that standalone-test to kselftest eventually, but so far haven't gotten around to doing that, so I'd prefer to get this patch merged now and the test later once it's ported.
Daniel Gibson (1):
tty: n_tty: Restore EOF push handling behavior
drivers/tty/n_tty.c | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-)
Signature · 1 line
2.25.1
PATCH 1/1 tty: n_tty: Restore EOF push handling behavior
TTYs in ICANON mode have a special case that allows "pushing" a line without a regular EOL character (like newline), by using EOF (the EOT character - ASCII 0x4) as a pseudo-EOL. It is silently discarded, so the reader of the PTS will receive the line *without* EOF or any other terminating character.
This special case has an edge case: What happens if the readers buffer is the same size as the line (without EOF)? Will they be able to tell if the whole line is received, i.e. if the next read() will return more of the same line or the next line?
There are two possibilities, that both have (dis)advantages:
1. The next read() returns 0. FreeBSD (13.0) and OSX (10.11) do this.
Advantage: The reader can interpret this as "the line is over".
Disadvantage: read() returning 0 means EOF, the reader could also interpret it as "there's no more data" and stop reading or even close the PT.
2. The next read() returns the next line, the EOF is silently discarded. Solaris (or at least OpenIndiana 2021.10) does this, Linux has done do this since commit 40d5e0905a03 ("n_tty: Fix EOF push handling"); this behavior was recently broken by commit 359303076163 ("tty:
n_tty: do not look ahead for EOL character past the end of the buffer").
Advantage: read() won't return 0 (EOF), reader less likely to be confused (and things like while(read(..)>0) don't break)
Disadvantage: The reader can't really know if the read() continues the last line (that filled the whole read buffer) or starts a new line.
As both options are defensible (and are used by other Unix-likes), it's best to stick to the "old" behavior since "n_tty: Fix EOF push handling" of 2013, i.e. silently discard that EOF.
This patch - that I actually got from Linus for testing and only modified slightly - restores that behavior by skipping an EOF character if it's the next character after reading is done.
Based on a patch from Linus Torvalds.
--- drivers/tty/n_tty.c | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-)
drivers/tty/n_tty.c +37 -1
@@ -1975,6 +1975,35 @@ static bool canon_copy_from_read_buf(struct tty_struct *tty,return ldata->read_tail != canon_head; } +/* +* If we finished a read at the exact location of an +* EOF (special EOL character that's a __DISABLED_CHAR) +* in the stream, silently eat the EOF. +*/ +static void canon_skip_eof (struct tty_struct *tty) +{ +struct n_tty_data *ldata = tty->disc_data; +size_t tail, canon_head; + + canon_head =smp_load_acquire (&ldata->canon_head); + tail = ldata->read_tail; + +// No data? +if (tail == canon_head) +return ; + +// See if the tail position is EOF in the circular buffer + tail &= (N_TTY_BUF_SIZE -1 ); +if (!test_bit (tail, ldata->read_flags)) +return ; +if (read_buf (ldata, tail) != __DISABLED_CHAR) +return ; + +// Clear the EOL bit, skip the EOF char. +clear_bit (tail, ldata->read_flags); +smp_store_release (&ldata->read_tail, ldata->read_tail +1 ); +} +/** * job_control - check job control * @tty: tty
@@ -2045,7 +2074,14 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,*/ if (*cookie) {if (ldata->icanon && !L_EXTPROC (tty)) { -if (canon_copy_from_read_buf (tty, &kb, &nr)) +/* +* If we have filled the user buffer, see +* if we should skip an EOF character before +* releasing the lock and returning done. +*/ +if (!nr) +canon_skip_eof (tty); +else if (canon_copy_from_read_buf (tty, &kb, &nr))return kb - kbuf; }else {if (copy_from_read_buf (tty, &kb, &nr))
Signature · 1 line
2.25.1