Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

tty: n_tty: use uint for space returned by tty_write_room()

tty_write_room() returns an "unsigned int". So in case some insane
driver (like my tty test driver) returns (legitimate) UINT_MAX from its
tty_operations::write_room(), n_tty is confused on several places.

For example, in process_output_block(), the result of tty_write_room()
is stored into (signed) "int". So this UINT_MAX suddenly becomes -1. And
that is extended to ssize_t and returned from process_output_block().
This causes a write() to such a node to receive -EPERM (which is -1).

Fix that by using proper "unsigned int" and proper "== 0" test. And
return 0 constant directly in that "if", so that it is immediately clear
what is returned ("space" equals to 0 at that point).

Similarly for process_output() and __process_echoes().

Note this does not fix any in-tree driver as of now.

If you want "Fixes: something", it would be commit 03b3b1a2405c ("tty:
make tty_operations::write_room return uint"). I intentionally do not
mark this patch by a real tag below.

Signed-off-by: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Link: https://lore.kernel.org/r/20250317070046.24386-6-jirislaby@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Jiri Slaby (SUSE) and committed by
Greg Kroah-Hartman
d97aa066 d2e38e71

+7 -6
+7 -6
drivers/tty/n_tty.c
··· 488 488 static int process_output(u8 c, struct tty_struct *tty) 489 489 { 490 490 struct n_tty_data *ldata = tty->disc_data; 491 - int space, retval; 491 + unsigned int space; 492 + int retval; 492 493 493 494 mutex_lock(&ldata->output_lock); 494 495 ··· 525 524 const u8 *buf, unsigned int nr) 526 525 { 527 526 struct n_tty_data *ldata = tty->disc_data; 528 - int space; 529 - int i; 527 + unsigned int space; 528 + int i; 530 529 const u8 *cp; 531 530 532 531 mutex_lock(&ldata->output_lock); 533 532 534 533 space = tty_write_room(tty); 535 - if (space <= 0) { 534 + if (space == 0) { 536 535 mutex_unlock(&ldata->output_lock); 537 - return space; 536 + return 0; 538 537 } 539 538 if (nr > space) 540 539 nr = space; ··· 699 698 static size_t __process_echoes(struct tty_struct *tty) 700 699 { 701 700 struct n_tty_data *ldata = tty->disc_data; 702 - int space, old_space; 701 + unsigned int space, old_space; 703 702 size_t tail; 704 703 u8 c; 705 704