Skip to contentSkip to conversation

tty: Fix unreasonable write toward closed pty.

PATCH In review Ilya Zykov 3 participants 4 messages Dec 19, 2012 – Dec 21, 2012 TTY layer and serial drivers

lore
Share & tools
lore

Ilya Zykov

author
#0 lore raw

PATCH 1/1 tty: Fix unreasonable write toward closed pty.

We should not write toward the closed pty.
Now it happens, if one side close last file descriptor, and other side in this moment write to it.
It also prevents scheduling unnecessary work.

---
 drivers/tty/pty.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
drivers/tty/pty.c +2 -0
index a82b399..1ce1362 100644
@@ -116,6 +116,8 @@ static int pty_space(struct tty_struct *to)
 
 static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
 {
+	if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
+		return -EIO;
 	struct tty_struct *to = tty->link;
 
 	if (tty->stopped)
#1 lore raw

drivers/tty/pty.c static int pty_space(struct tty_struct *to) · line 120

Quoted 10 lines
drivers/tty/pty.c +2 -0
index a82b399..1ce1362 100644
@@ -116,6 +116,8 @@ static int pty_space(struct tty_struct *to)
 
 static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
 {
+	if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
+		return -EIO;

The flag can change between the test and ny further code being executed ?

Alan

author
#2 lore raw
Quoted 6 lines · Alan Cox
Quoted 6 lines
drivers/tty/pty.c +0 -0
index a82b399..1ce1362 100644
@@ -116,6 +116,8 @@ static int pty_space(struct tty_struct *to)
 
Quoted 9 lines

drivers/tty/pty.c static int pty_space(struct tty_struct *to) · line 120

Quoted 4 lines
drivers/tty/pty.c +2 -0
 static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
 {
+	if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
+		return -EIO;

The flag can change between the test and ny further code being executed ?

Alan

Yes, if I understand you correctly, but this is no matter here, because ldisc's layer will wait, flush this data later. Here, only beginning stage of tty_close.
This is safe later stage from unnecessary data.

Ilya

#3 lore raw
In reply to Ilya Zykov
Quoted 15 lines · On Wed, 19 Dec 2012 22:59:19 +0400 Ilya Zykov <[email protected]> wrote:

We should not write toward the closed pty.
Now it happens, if one side close last file descriptor, and other side in this moment write to it.
It also prevents scheduling unnecessary work.

---
 drivers/tty/pty.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
drivers/tty/pty.c +0 -0
index a82b399..1ce1362 100644

drivers/tty/pty.c static int pty_space(struct tty_struct *to) · line 121

Quoted 6 lines
drivers/tty/pty.c +2 -0
@@ -116,6 +116,8 @@ static int pty_space(struct tty_struct *to)
 
 static int pty_write(struct tty_struct *tty, const unsigned char
 *buf, int c) {
+	if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
+		return -EIO;
 	struct tty_struct *to = tty->link;

This

a) doesn't do anything in many cases because there is no lock to make the test_bit meaningful

b) produces an obvious compiler warning