[Devel] [PATCH RHEL8 COMMIT] ms/vt: selection, push sel_lock up #PSBM-120640

Konstantin Khorenko khorenko at virtuozzo.com
Wed Oct 14 15:32:53 MSK 2020


The commit is pushed to "branch-rh8-4.18.0-193.6.3.vz8.4.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh8-4.18.0-193.6.3.vz8.4.12
------>
commit daab9ab9f8ec04454be9c6cdbef8ffba583eaea9
Author: Jiri Slaby <jslaby at suse.cz>
Date:   Wed Oct 14 15:32:53 2020 +0300

    ms/vt: selection, push sel_lock up #PSBM-120640
    
    sel_lock cannot nest in the console lock. Thanks to syzkaller, the
    kernel states firmly:
    
    > WARNING: possible circular locking dependency detected
    > 5.6.0-rc3-syzkaller #0 Not tainted
    > ------------------------------------------------------
    > syz-executor.4/20336 is trying to acquire lock:
    > ffff8880a2e952a0 (&tty->termios_rwsem){++++}, at: tty_unthrottle+0x22/0x100 drivers/tty/tty_ioctl.c:136
    >
    > but task is already holding lock:
    > ffffffff89462e70 (sel_lock){+.+.}, at: paste_selection+0x118/0x470 drivers/tty/vt/selection.c:374
    >
    > which lock already depends on the new lock.
    >
    > the existing dependency chain (in reverse order) is:
    >
    > -> #2 (sel_lock){+.+.}:
    >        mutex_lock_nested+0x1b/0x30 kernel/locking/mutex.c:1118
    >        set_selection_kernel+0x3b8/0x18a0 drivers/tty/vt/selection.c:217
    >        set_selection_user+0x63/0x80 drivers/tty/vt/selection.c:181
    >        tioclinux+0x103/0x530 drivers/tty/vt/vt.c:3050
    >        vt_ioctl+0x3f1/0x3a30 drivers/tty/vt/vt_ioctl.c:364
    
    This is ioctl(TIOCL_SETSEL).
    Locks held on the path: console_lock -> sel_lock
    
    > -> #1 (console_lock){+.+.}:
    >        console_lock+0x46/0x70 kernel/printk/printk.c:2289
    >        con_flush_chars+0x50/0x650 drivers/tty/vt/vt.c:3223
    >        n_tty_write+0xeae/0x1200 drivers/tty/n_tty.c:2350
    >        do_tty_write drivers/tty/tty_io.c:962 [inline]
    >        tty_write+0x5a1/0x950 drivers/tty/tty_io.c:1046
    
    This is write().
    Locks held on the path: termios_rwsem -> console_lock
    
    > -> #0 (&tty->termios_rwsem){++++}:
    >        down_write+0x57/0x140 kernel/locking/rwsem.c:1534
    >        tty_unthrottle+0x22/0x100 drivers/tty/tty_ioctl.c:136
    >        mkiss_receive_buf+0x12aa/0x1340 drivers/net/hamradio/mkiss.c:902
    >        tty_ldisc_receive_buf+0x12f/0x170 drivers/tty/tty_buffer.c:465
    >        paste_selection+0x346/0x470 drivers/tty/vt/selection.c:389
    >        tioclinux+0x121/0x530 drivers/tty/vt/vt.c:3055
    >        vt_ioctl+0x3f1/0x3a30 drivers/tty/vt/vt_ioctl.c:364
    
    This is ioctl(TIOCL_PASTESEL).
    Locks held on the path: sel_lock -> termios_rwsem
    
    > other info that might help us debug this:
    >
    > Chain exists of:
    >   &tty->termios_rwsem --> console_lock --> sel_lock
    
    Clearly. From the above, we have:
     console_lock -> sel_lock
     sel_lock -> termios_rwsem
     termios_rwsem -> console_lock
    
    Fix this by reversing the console_lock -> sel_lock dependency in
    ioctl(TIOCL_SETSEL). First, lock sel_lock, then console_lock.
    
    Signed-off-by: Jiri Slaby <jslaby at suse.cz>
    Reported-by: syzbot+26183d9746e62da329b8 at syzkaller.appspotmail.com
    Fixes: 07e6124a1a46 ("vt: selection, close sel_buffer race")
    Cc: stable <stable at vger.kernel.org>
    Link: https://lore.kernel.org/r/20200228115406.5735-2-jslaby@suse.cz
    Signed-off-by: Greg Kroah-Hartman <gregkh at linuxfoundation.org>
    
    https://jira.sw.ru/browse/PSBM-120640
    
    This is a backport of mainline commit e8c75a30a23c6ba63f4ef6895cbf41fd42f21aa2:
    the affected code is in set_selection() rather than set_selection_kernel().
    
    Signed-off-by: Evgenii Shatokhin <eshatokhin at virtuozzo.com>
---
 drivers/tty/vt/selection.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)

diff --git a/drivers/tty/vt/selection.c b/drivers/tty/vt/selection.c
index 2f378a7cd1fe..c50a05b57470 100644
--- a/drivers/tty/vt/selection.c
+++ b/drivers/tty/vt/selection.c
@@ -163,7 +163,7 @@ static int __set_selection(const struct tiocl_selection __user *sel, struct tty_
 	char *bp, *obp;
 	int i, ps, pe, multiplier;
 	u16 c;
-	int mode, ret = 0;
+	int mode;
 
 	poke_blanked_console();
 	if (copy_from_user(&v, sel, sizeof(*sel)))
@@ -190,7 +190,6 @@ static int __set_selection(const struct tiocl_selection __user *sel, struct tty_
 	if (ps > pe)	/* make sel_start <= sel_end */
 		swap(ps, pe);
 
-	mutex_lock(&sel_lock);
 	if (sel_cons != vc_cons[fg_console].d) {
 		clear_selection();
 		sel_cons = vc_cons[fg_console].d;
@@ -236,10 +235,9 @@ static int __set_selection(const struct tiocl_selection __user *sel, struct tty_
 			break;
 		case TIOCL_SELPOINTER:
 			highlight_pointer(pe);
-			goto unlock;
+			return 0;
 		default:
-			ret = -EINVAL;
-			goto unlock;
+			return -EINVAL;
 	}
 
 	/* remove the pointer */
@@ -261,7 +259,7 @@ static int __set_selection(const struct tiocl_selection __user *sel, struct tty_
 	else if (new_sel_start == sel_start)
 	{
 		if (new_sel_end == sel_end)	/* no action required */
-			goto unlock;
+			return 0;
 		else if (new_sel_end > sel_end)	/* extend to right */
 			highlight(sel_end + 2, new_sel_end);
 		else				/* contract from right */
@@ -289,8 +287,7 @@ static int __set_selection(const struct tiocl_selection __user *sel, struct tty_
 	if (!bp) {
 		printk(KERN_WARNING "selection: kmalloc() failed\n");
 		clear_selection();
-		ret = -ENOMEM;
-		goto unlock;
+		return -ENOMEM;
 	}
 	kfree(sel_buffer);
 	sel_buffer = bp;
@@ -315,18 +312,18 @@ static int __set_selection(const struct tiocl_selection __user *sel, struct tty_
 		}
 	}
 	sel_buffer_lth = bp - sel_buffer;
-unlock:
-	mutex_unlock(&sel_lock);
-	return ret;
+	return 0;
 }
 
 int set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
 {
 	int ret;
 
+	mutex_lock(&sel_lock);
 	console_lock();
 	ret = __set_selection(sel, tty);
 	console_unlock();
+	mutex_unlock(&sel_lock);
 
 	return ret;
 }


More information about the Devel mailing list