Skip to content

Commit 7cf64b1

Browse files
ebiggersgregkh
authored andcommitted
vt: vt_ioctl: fix use-after-free in vt_in_use()
vt_in_use() dereferences console_driver->ttys[i] without proper locking. This is broken because the tty can be closed and freed concurrently. We could fix this by using 'READ_ONCE(console_driver->ttys[i]) != NULL' and skipping the check of tty_struct::count. But, looking at console_driver->ttys[i] isn't really appropriate anyway because even if it is NULL the tty can still be in the process of being closed. Instead, fix it by making vt_in_use() require console_lock() and check whether the vt is allocated and has port refcount > 1. This works since following the patch "vt: vt_ioctl: fix VT_DISALLOCATE freeing in-use virtual console" the port refcount is incremented while the vt is open. Reproducer (very unreliable, but it worked for me after a few minutes): #include <fcntl.h> #include <linux/vt.h> int main() { int fd, nproc; struct vt_stat state; char ttyname[16]; fd = open("/dev/tty10", O_RDONLY); for (nproc = 1; nproc < 8; nproc *= 2) fork(); for (;;) { sprintf(ttyname, "/dev/tty%d", rand() % 8); close(open(ttyname, O_RDONLY)); ioctl(fd, VT_GETSTATE, &state); } } KASAN report: BUG: KASAN: use-after-free in vt_in_use drivers/tty/vt/vt_ioctl.c:48 [inline] BUG: KASAN: use-after-free in vt_ioctl+0x1ad3/0x1d70 drivers/tty/vt/vt_ioctl.c:657 Read of size 4 at addr ffff888065722468 by task syz-vt2/132 CPU: 0 PID: 132 Comm: syz-vt2 Not tainted 5.6.0-rc5-00130-g089b6d3654916 #13 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS ?-20191223_100556-anatol 04/01/2014 Call Trace: [...] vt_in_use drivers/tty/vt/vt_ioctl.c:48 [inline] vt_ioctl+0x1ad3/0x1d70 drivers/tty/vt/vt_ioctl.c:657 tty_ioctl+0x9db/0x11b0 drivers/tty/tty_io.c:2660 [...] Allocated by task 136: [...] kzalloc include/linux/slab.h:669 [inline] alloc_tty_struct+0x96/0x8a0 drivers/tty/tty_io.c:2982 tty_init_dev+0x23/0x350 drivers/tty/tty_io.c:1334 tty_open_by_driver drivers/tty/tty_io.c:1987 [inline] tty_open+0x3ca/0xb30 drivers/tty/tty_io.c:2035 [...] Freed by task 41: [...] kfree+0xbf/0x200 mm/slab.c:3757 free_tty_struct+0x8d/0xb0 drivers/tty/tty_io.c:177 release_one_tty+0x22d/0x2f0 drivers/tty/tty_io.c:1468 process_one_work+0x7f1/0x14b0 kernel/workqueue.c:2264 worker_thread+0x8b/0xc80 kernel/workqueue.c:2410 [...] Fixes: 4001d7b ("vt: push down the tty lock so we can see what is left to tackle") Cc: <[email protected]> # v3.4+ Acked-by: Jiri Slaby <[email protected]> Signed-off-by: Eric Biggers <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent ca4463b commit 7cf64b1

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

drivers/tty/vt/vt_ioctl.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,15 @@ bool vt_dont_switch;
4343

4444
static inline bool vt_in_use(unsigned int i)
4545
{
46-
extern struct tty_driver *console_driver;
46+
const struct vc_data *vc = vc_cons[i].d;
4747

48-
return console_driver->ttys[i] && console_driver->ttys[i]->count;
48+
/*
49+
* console_lock must be held to prevent the vc from being deallocated
50+
* while we're checking whether it's in-use.
51+
*/
52+
WARN_CONSOLE_UNLOCKED();
53+
54+
return vc && kref_read(&vc->port.kref) > 1;
4955
}
5056

5157
static inline bool vt_busy(int i)
@@ -643,15 +649,16 @@ int vt_ioctl(struct tty_struct *tty,
643649
struct vt_stat __user *vtstat = up;
644650
unsigned short state, mask;
645651

646-
/* Review: FIXME: Console lock ? */
647652
if (put_user(fg_console + 1, &vtstat->v_active))
648653
ret = -EFAULT;
649654
else {
650655
state = 1; /* /dev/tty0 is always open */
656+
console_lock(); /* required by vt_in_use() */
651657
for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
652658
++i, mask <<= 1)
653659
if (vt_in_use(i))
654660
state |= mask;
661+
console_unlock();
655662
ret = put_user(state, &vtstat->v_state);
656663
}
657664
break;
@@ -661,10 +668,11 @@ int vt_ioctl(struct tty_struct *tty,
661668
* Returns the first available (non-opened) console.
662669
*/
663670
case VT_OPENQRY:
664-
/* FIXME: locking ? - but then this is a stupid API */
671+
console_lock(); /* required by vt_in_use() */
665672
for (i = 0; i < MAX_NR_CONSOLES; ++i)
666673
if (!vt_in_use(i))
667674
break;
675+
console_unlock();
668676
uival = i < MAX_NR_CONSOLES ? (i+1) : -1;
669677
goto setint;
670678

0 commit comments

Comments
 (0)