From 8874a291633382633b317c9a90e59fd2cccb5617 Mon Sep 17 00:00:00 2001 From: Colin Ian King Date: Mon, 13 May 2019 20:08:05 +0800 Subject: [PATCH] n_tty: check for negative and zero space return from tty_write_room mainline inclusion from mainline-5.1 commit 9ef8927f45f2 category: bugfix bugzilla: 15274 CVE: NA ------------------------------------------------- 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 Signed-off-by: Greg Kroah-Hartman Signed-off-by: Yang Yingliang Reviewed-by: Yao Hongbo Signed-off-by: Yang Yingliang --- drivers/tty/n_tty.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c index 3ad460219fd6..5c37d04c6bca 100644 --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -548,9 +548,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; -- GitLab