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

n_tty: check for negative and zero space return from tty_write_room

The return from tty_write_room could potentially be negative if
a tty write_room driver returns an error number (not that any seem
to do). Rather than just check for a zero return, also check for
a -ve return. This avoids the unsigned nr being set to a large unsigned
value on the assignment from variable space and can lead to overflowing
the buffer buf. Better to be safe than assume all write_room
implementations in tty drivers are going to do the right thing.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Colin Ian King and committed by
Greg Kroah-Hartman
9ef8927f f16aa97d

+2 -2
+2 -2
drivers/tty/n_tty.c
··· 550 550 mutex_lock(&ldata->output_lock); 551 551 552 552 space = tty_write_room(tty); 553 - if (!space) { 553 + if (space <= 0) { 554 554 mutex_unlock(&ldata->output_lock); 555 - return 0; 555 + return space; 556 556 } 557 557 if (nr > space) 558 558 nr = space;