possible deadlock in n_tty_receive_buf_common

From: syzbot
Date: Fri Feb 14 2020 - 12:37:23 EST


Hello,

syzbot found the following crash on:

HEAD commit: 9f01828e Add linux-next specific files for 20200214
git tree: linux-next
console output: https://syzkaller.appspot.com/x/log.txt?x=119f6c29e00000
kernel config: https://syzkaller.appspot.com/x/.config?x=ffb2752ee30931a0
dashboard link: https://syzkaller.appspot.com/bug?extid=41a045bca47dde704c4c
compiler: gcc (GCC) 9.0.0 20181231 (experimental)
syz repro: https://syzkaller.appspot.com/x/repro.syz?x=1172fc09e00000
C reproducer: https://syzkaller.appspot.com/x/repro.c?x=15a3a431e00000

Bisection is inconclusive: the bug happens on the oldest tested release.

bisection log: https://syzkaller.appspot.com/x/bisect.txt?x=106596b5e00000
final crash: https://syzkaller.appspot.com/x/report.txt?x=126596b5e00000
console output: https://syzkaller.appspot.com/x/log.txt?x=146596b5e00000

IMPORTANT: if you fix the bug, please add the following tag to the commit:
Reported-by: syzbot+41a045bca47dde704c4c@xxxxxxxxxxxxxxxxxxxxxxxxx

======================================================
WARNING: possible circular locking dependency detected
5.6.0-rc1-next-20200214-syzkaller #0 Not tainted
------------------------------------------------------
syz-executor167/9787 is trying to acquire lock:
ffff88809a1872a0 (&tty->termios_rwsem){++++}, at: n_tty_receive_buf_common+0x8a/0x2b70 drivers/tty/n_tty.c:1705

but task is already holding lock:
ffffffff8a1414c0 (sel_lock){+.+.}, at: paste_selection+0x15a/0x4d0 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_common kernel/locking/mutex.c:956 [inline]
__mutex_lock+0x156/0x13c0 kernel/locking/mutex.c:1103
mutex_lock_nested+0x16/0x20 kernel/locking/mutex.c:1118
set_selection_kernel+0x39d/0x13d0 drivers/tty/vt/selection.c:217
set_selection_user+0x95/0xd9 drivers/tty/vt/selection.c:181
tioclinux+0x11c/0x480 drivers/tty/vt/vt.c:3050
vt_ioctl+0x1a41/0x26c0 drivers/tty/vt/vt_ioctl.c:364
tty_ioctl+0xa37/0x14f0 drivers/tty/tty_io.c:2660
vfs_ioctl fs/ioctl.c:47 [inline]
ksys_ioctl+0x123/0x180 fs/ioctl.c:763
__do_sys_ioctl fs/ioctl.c:772 [inline]
__se_sys_ioctl fs/ioctl.c:770 [inline]
__x64_sys_ioctl+0x73/0xb0 fs/ioctl.c:770
do_syscall_64+0xfa/0x790 arch/x86/entry/common.c:294
entry_SYSCALL_64_after_hwframe+0x49/0xbe

-> #1 (console_lock){+.+.}:
console_lock+0x47/0x80 kernel/printk/printk.c:2286
con_flush_chars drivers/tty/vt/vt.c:3223 [inline]
con_flush_chars+0x3d/0xa0 drivers/tty/vt/vt.c:3215
n_tty_write+0xc85/0x1080 drivers/tty/n_tty.c:2350
do_tty_write drivers/tty/tty_io.c:962 [inline]
tty_write+0x496/0x7f0 drivers/tty/tty_io.c:1046
__vfs_write+0x8a/0x110 fs/read_write.c:494
vfs_write+0x268/0x5d0 fs/read_write.c:558
ksys_write+0x14f/0x290 fs/read_write.c:611
__do_sys_write fs/read_write.c:623 [inline]
__se_sys_write fs/read_write.c:620 [inline]
__x64_sys_write+0x73/0xb0 fs/read_write.c:620
do_syscall_64+0xfa/0x790 arch/x86/entry/common.c:294
entry_SYSCALL_64_after_hwframe+0x49/0xbe

-> #0 (&tty->termios_rwsem){++++}:
check_prev_add kernel/locking/lockdep.c:2481 [inline]
check_prevs_add kernel/locking/lockdep.c:2586 [inline]
validate_chain kernel/locking/lockdep.c:3203 [inline]
__lock_acquire+0x29cd/0x6320 kernel/locking/lockdep.c:4190
lock_acquire+0x190/0x410 kernel/locking/lockdep.c:4720
down_read+0x95/0x440 kernel/locking/rwsem.c:1492
n_tty_receive_buf_common+0x8a/0x2b70 drivers/tty/n_tty.c:1705
n_tty_receive_buf2+0x34/0x40 drivers/tty/n_tty.c:1777
tty_ldisc_receive_buf+0xad/0x1c0 drivers/tty/tty_buffer.c:461
paste_selection+0x1ff/0x4d0 drivers/tty/vt/selection.c:389
tioclinux+0x133/0x480 drivers/tty/vt/vt.c:3055
vt_ioctl+0x1a41/0x26c0 drivers/tty/vt/vt_ioctl.c:364
tty_ioctl+0xa37/0x14f0 drivers/tty/tty_io.c:2660
vfs_ioctl fs/ioctl.c:47 [inline]
ksys_ioctl+0x123/0x180 fs/ioctl.c:763
__do_sys_ioctl fs/ioctl.c:772 [inline]
__se_sys_ioctl fs/ioctl.c:770 [inline]
__x64_sys_ioctl+0x73/0xb0 fs/ioctl.c:770
do_syscall_64+0xfa/0x790 arch/x86/entry/common.c:294
entry_SYSCALL_64_after_hwframe+0x49/0xbe

other info that might help us debug this:

Chain exists of:
&tty->termios_rwsem --> console_lock --> sel_lock

Possible unsafe locking scenario:

CPU0 CPU1
---- ----
lock(sel_lock);
lock(console_lock);
lock(sel_lock);
lock(&tty->termios_rwsem);

*** DEADLOCK ***

3 locks held by syz-executor167/9787:
#0: ffff88809a187090 (&tty->ldisc_sem){++++}, at: ldsem_down_read+0x33/0x40 drivers/tty/tty_ldsem.c:340
#1: ffff8880aa5be0a8 (&buf->lock){+.+.}, at: tty_buffer_lock_exclusive+0x30/0x40 drivers/tty/tty_buffer.c:61
#2: ffffffff8a1414c0 (sel_lock){+.+.}, at: paste_selection+0x15a/0x4d0 drivers/tty/vt/selection.c:374

stack backtrace:
CPU: 1 PID: 9787 Comm: syz-executor167 Not tainted 5.6.0-rc1-next-20200214-syzkaller #0
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x197/0x210 lib/dump_stack.c:118
print_circular_bug.isra.0.cold+0x163/0x172 kernel/locking/lockdep.c:1688
check_noncircular+0x32e/0x3e0 kernel/locking/lockdep.c:1812
check_prev_add kernel/locking/lockdep.c:2481 [inline]
check_prevs_add kernel/locking/lockdep.c:2586 [inline]
validate_chain kernel/locking/lockdep.c:3203 [inline]
__lock_acquire+0x29cd/0x6320 kernel/locking/lockdep.c:4190
lock_acquire+0x190/0x410 kernel/locking/lockdep.c:4720
down_read+0x95/0x440 kernel/locking/rwsem.c:1492
n_tty_receive_buf_common+0x8a/0x2b70 drivers/tty/n_tty.c:1705
n_tty_receive_buf2+0x34/0x40 drivers/tty/n_tty.c:1777
tty_ldisc_receive_buf+0xad/0x1c0 drivers/tty/tty_buffer.c:461
paste_selection+0x1ff/0x4d0 drivers/tty/vt/selection.c:389
tioclinux+0x133/0x480 drivers/tty/vt/vt.c:3055
vt_ioctl+0x1a41/0x26c0 drivers/tty/vt/vt_ioctl.c:364
tty_ioctl+0xa37/0x14f0 drivers/tty/tty_io.c:2660
vfs_ioctl fs/ioctl.c:47 [inline]
ksys_ioctl+0x123/0x180 fs/ioctl.c:763
__do_sys_ioctl fs/ioctl.c:772 [inline]
__se_sys_ioctl fs/ioctl.c:770 [inline]
__x64_sys_ioctl+0x73/0xb0 fs/ioctl.c:770
do_syscall_64+0xfa/0x790 arch/x86/entry/common.c:294
entry_SYSCALL_64_after_hwframe+0x49/0xbe
RIP: 0033:0x440219
Code: 18 89 d0 c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 0f 83 5b 14 fc ff c3 66 2e 0f 1f 84 00 00 00 00
RSP: 002b:00007ffe5133a1c8 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
RAX: ffffffffffffffda RBX: 00000000004002c8 RCX: 0000000000440219
RDX: 0000000020000040 RSI: 000000000000541c RDI: 0000000000000004
RBP: 00000000006ca018 R08: 000000000000000d R09: 00000000004002c8
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000401b00
R13: 0000000000401b90 R14: 0000000000000000 R15: 000000000


---
This bug is generated by a bot. It may contain errors.
See https://goo.gl/tpsmEJ for more information about syzbot.
syzbot engineers can be reached at syzkaller@xxxxxxxxxxxxxxxxx

syzbot will keep track of this bug report. See:
https://goo.gl/tpsmEJ#status for how to communicate with syzbot.
For information about bisection process see: https://goo.gl/tpsmEJ#bisection
syzbot can test patches for this bug, for details see:
https://goo.gl/tpsmEJ#testing-patches