Skip to content

Commit b9ca5f8

Browse files
Sahara74gregkh
authored andcommitted
tty: pty: Fix race condition between release_one_tty and pty_write
Especially when a linked tty is used such as pty, the linked tty port's buf works have not been cancelled while master tty port's buf work has been cancelled. Since release_one_tty and flush_to_ldisc run in workqueue threads separately, when pty_cleanup happens and link tty port is freed, flush_to_ldisc tries to access freed port and port->itty, eventually it causes a panic. This patch utilizes the magic value with holding the tty_mutex to check if the tty->link is valid. Fixes: 2b022ab ("pty: cancel pty slave port buf's work in tty_release") Signed-off-by: Sahara <keun-o.park@darkmatter.ae> Cc: stable <stable@vger.kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 75ddbc1 commit b9ca5f8

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

‎drivers/tty/pty.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
116116
if (tty->stopped)
117117
return 0;
118118

119+
mutex_lock(&tty_mutex);
120+
if (to->magic != TTY_MAGIC) {
121+
mutex_unlock(&tty_mutex);
122+
return -EIO;
123+
}
124+
119125
if (c > 0) {
120126
spin_lock_irqsave(&to->port->lock, flags);
121127
/* Stuff the data into the input queue of the other end */
@@ -125,6 +131,7 @@ static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
125131
tty_flip_buffer_push(to->port);
126132
spin_unlock_irqrestore(&to->port->lock, flags);
127133
}
134+
mutex_unlock(&tty_mutex);
128135
return c;
129136
}
130137

‎drivers/tty/tty_io.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,10 +1448,13 @@ static void release_one_tty(struct work_struct *work)
14481448
struct tty_driver *driver = tty->driver;
14491449
struct module *owner = driver->owner;
14501450

1451+
mutex_lock(&tty_mutex);
14511452
if (tty->ops->cleanup)
14521453
tty->ops->cleanup(tty);
14531454

14541455
tty->magic = 0;
1456+
mutex_unlock(&tty_mutex);
1457+
14551458
tty_driver_kref_put(driver);
14561459
module_put(owner);
14571460

0 commit comments

Comments
 (0)