Skip to content

Commit e9036d0

Browse files
peterhurleygregkh
authored andcommitted
tty: Drop krefs for interrupted tty lock
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: 0bfd464 ("tty: Wait interruptibly for tty lock on reopen") Reported-by: Dmitry Vyukov <dvyukov@google.com> Signed-off-by: Peter Hurley <peter@hurleysoftware.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 36f90b0 commit e9036d0

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

drivers/tty/tty_io.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,13 +2066,12 @@ static int tty_open(struct inode *inode, struct file *filp)
20662066
if (tty) {
20672067
mutex_unlock(&tty_mutex);
20682068
retval = tty_lock_interruptible(tty);
2069+
tty_kref_put(tty); /* drop kref from tty_driver_lookup_tty() */
20692070
if (retval) {
20702071
if (retval == -EINTR)
20712072
retval = -ERESTARTSYS;
20722073
goto err_unref;
20732074
}
2074-
/* safe to drop the kref from tty_driver_lookup_tty() */
2075-
tty_kref_put(tty);
20762075
retval = tty_reopen(tty);
20772076
if (retval < 0) {
20782077
tty_unlock(tty);

drivers/tty/tty_mutex.c

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

2222
int tty_lock_interruptible(struct tty_struct *tty)
2323
{
24+
int ret;
25+
2426
if (WARN(tty->magic != TTY_MAGIC, "L Bad %p\n", tty))
2527
return -EIO;
2628
tty_kref_get(tty);
27-
return mutex_lock_interruptible(&tty->legacy_mutex);
29+
ret = mutex_lock_interruptible(&tty->legacy_mutex);
30+
if (ret)
31+
tty_kref_put(tty);
32+
return ret;
2833
}
2934

3035
void __lockfunc tty_unlock(struct tty_struct *tty)

0 commit comments

Comments
 (0)