Skip to content

Commit e8c75a3

Browse files
Jiri Slabygregkh
authored andcommitted
vt: selection, push sel_lock up
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 <[email protected]> Reported-by: [email protected] Fixes: 07e6124 ("vt: selection, close sel_buffer race") Cc: stable <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 4b70dd5 commit e8c75a3

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

drivers/tty/vt/selection.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ static int __set_selection_kernel(struct tiocl_selection *v, struct tty_struct *
214214
if (ps > pe) /* make sel_start <= sel_end */
215215
swap(ps, pe);
216216

217-
mutex_lock(&sel_lock);
218217
if (sel_cons != vc_cons[fg_console].d) {
219218
clear_selection();
220219
sel_cons = vc_cons[fg_console].d;
@@ -260,10 +259,9 @@ static int __set_selection_kernel(struct tiocl_selection *v, struct tty_struct *
260259
break;
261260
case TIOCL_SELPOINTER:
262261
highlight_pointer(pe);
263-
goto unlock;
262+
return 0;
264263
default:
265-
ret = -EINVAL;
266-
goto unlock;
264+
return -EINVAL;
267265
}
268266

269267
/* remove the pointer */
@@ -285,7 +283,7 @@ static int __set_selection_kernel(struct tiocl_selection *v, struct tty_struct *
285283
else if (new_sel_start == sel_start)
286284
{
287285
if (new_sel_end == sel_end) /* no action required */
288-
goto unlock;
286+
return 0;
289287
else if (new_sel_end > sel_end) /* extend to right */
290288
highlight(sel_end + 2, new_sel_end);
291289
else /* contract from right */
@@ -313,8 +311,7 @@ static int __set_selection_kernel(struct tiocl_selection *v, struct tty_struct *
313311
if (!bp) {
314312
printk(KERN_WARNING "selection: kmalloc() failed\n");
315313
clear_selection();
316-
ret = -ENOMEM;
317-
goto unlock;
314+
return -ENOMEM;
318315
}
319316
kfree(sel_buffer);
320317
sel_buffer = bp;
@@ -339,18 +336,19 @@ static int __set_selection_kernel(struct tiocl_selection *v, struct tty_struct *
339336
}
340337
}
341338
sel_buffer_lth = bp - sel_buffer;
342-
unlock:
343-
mutex_unlock(&sel_lock);
339+
344340
return ret;
345341
}
346342

347343
int set_selection_kernel(struct tiocl_selection *v, struct tty_struct *tty)
348344
{
349345
int ret;
350346

347+
mutex_lock(&sel_lock);
351348
console_lock();
352349
ret = __set_selection_kernel(v, tty);
353350
console_unlock();
351+
mutex_unlock(&sel_lock);
354352

355353
return ret;
356354
}

0 commit comments

Comments
 (0)