n_tty: check for negative and zero space return from tty_write_room
authorColin Ian King <colin.king@canonical.com>
Sat, 30 Mar 2019 00:46:28 +0000 (00:46 +0000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 16 Apr 2019 13:21:33 +0000 (15:21 +0200)
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>
drivers/tty/n_tty.c

index 9cdb0fa3c4bf97c6a658a35e5ae3c1fd9784d3f7..f9c584244f72c1b856ce71bebd673b81ee00cf76 100644 (file)
@@ -550,9 +550,9 @@ static ssize_t process_output_block(struct tty_struct *tty,
        mutex_lock(&ldata->output_lock);
 
        space = tty_write_room(tty);
-       if (!space) {
+       if (space <= 0) {
                mutex_unlock(&ldata->output_lock);
-               return 0;
+               return space;
        }
        if (nr > space)
                nr = space;