Skip to content

Commit cc04a14

Browse files
peterhurleygregkh
authored andcommitted
tty: Drop krefs for interrupted tty lock
commit e9036d0662360cd4c79578565ce422ed5872f301 upstream. When the tty lock is interrupted on attempted re-open, 2 tty krefs are still held. Drop extra kref before returning failure from tty_lock_interruptible(), and drop lookup kref before returning failure from tty_open(). Fixes: 0bfd464d3fdd ("tty: Wait interruptibly for tty lock on reopen") Reported-by: Dmitry Vyukov <dvyukov@google.com> Signed-off-by: Peter Hurley <peter@hurleysoftware.com> Cc: Jiri Slaby <jslaby@suse.cz> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 983c09e commit cc04a14

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

drivers/tty/tty_io.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,13 +2070,12 @@ static int tty_open(struct inode *inode, struct file *filp)
20702070
if (tty) {
20712071
mutex_unlock(&tty_mutex);
20722072
retval = tty_lock_interruptible(tty);
2073+
tty_kref_put(tty); /* drop kref from tty_driver_lookup_tty() */
20732074
if (retval) {
20742075
if (retval == -EINTR)
20752076
retval = -ERESTARTSYS;
20762077
goto err_unref;
20772078
}
2078-
/* safe to drop the kref from tty_driver_lookup_tty() */
2079-
tty_kref_put(tty);
20802079
retval = tty_reopen(tty);
20812080
if (retval < 0) {
20822081
tty_unlock(tty);

drivers/tty/tty_mutex.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ EXPORT_SYMBOL(tty_lock);
2424

2525
int tty_lock_interruptible(struct tty_struct *tty)
2626
{
27+
int ret;
28+
2729
if (WARN(tty->magic != TTY_MAGIC, "L Bad %p\n", tty))
2830
return -EIO;
2931
tty_kref_get(tty);
30-
return mutex_lock_interruptible(&tty->legacy_mutex);
32+
ret = mutex_lock_interruptible(&tty->legacy_mutex);
33+
if (ret)
34+
tty_kref_put(tty);
35+
return ret;
3136
}
3237

3338
void __lockfunc tty_unlock(struct tty_struct *tty)

0 commit comments

Comments
 (0)