Skip to contentSkip to conversation

n_tty: check for negative and zero space return from tty_write_room

PATCH Superseded Colin King 3 participants 3 messages Mar 28, 2019 – Mar 29, 2019

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH 1/1 n_tty: check for negative and zero space return from tty_write_room

From: Colin Ian King <[email protected]>

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.

---
 drivers/tty/n_tty.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/n_tty.c +1 -1
index 9cdb0fa3c4bf..66630787fbf9 100644
@@ -550,7 +550,7 @@ 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;
 	}
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Colin King

From: Colin Ian King <[email protected]>

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.

Looks reasonable to me.

-Mukesh.

Quoted 17 lines
---
  drivers/tty/n_tty.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/n_tty.c +1 -1
index 9cdb0fa3c4bf..66630787fbf9 100644
@@ -550,7 +550,7 @@ 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;
  	}
#2 lore raw
In reply to Colin King
Quoted 20 lines · Colin King

From: Colin Ian King <[email protected]>

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.

---
 drivers/tty/n_tty.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/n_tty.c +0 -0
index 9cdb0fa3c4bf..66630787fbf9 100644

drivers/tty/n_tty.c static ssize_t process_output_block(struct tty_struct *tty, · line 555

Quoted 7 lines
drivers/tty/n_tty.c +1 -1
@@ -550,7 +550,7 @@ 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;

Instead of zero we should probably return "space" to preserve the error code.

regards,
dan carpenter