linux-serial.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jiri Slaby <jslaby@suse.cz>
To: Eric Biggers <ebiggers@kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org,
	syzkaller-bugs@googlegroups.com,
	Eric Dumazet <edumazet@google.com>,
	Nicolas Pitre <nico@fluxnic.net>
Subject: Re: [PATCH v3 2/2] vt: vt_ioctl: fix use-after-free in vt_in_use()
Date: Fri, 27 Mar 2020 11:30:12 +0100	[thread overview]
Message-ID: <38de9a73-9321-09ba-6ef8-023a64c3c023@suse.cz> (raw)
In-Reply-To: <20200322034305.210082-3-ebiggers@kernel.org>

On 22. 03. 20, 4:43, Eric Biggers wrote:
> From: Eric Biggers <ebiggers@google.com>
> 
> 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: 4001d7b7fc27 ("vt: push down the tty lock so we can see what is left to tackle")
> Cc: <stable@vger.kernel.org> # v3.4+
> Signed-off-by: Eric Biggers <ebiggers@google.com>

I cannot think of anything better with the current poor code state, so:

Acked-by: Jiri Slaby <jslaby@suse.cz>

thanks,
-- 
-- 
js
suse labs

  reply	other threads:[~2020-03-27 10:30 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20200318222704.GC2334@sol.localdomain>
2020-03-18 22:38 ` [PATCH v2 0/2] vt: fix some vt_ioctl races Eric Biggers
2020-03-18 22:38   ` [PATCH v2 1/2] vt: vt_ioctl: fix VT_DISALLOCATE freeing in-use virtual console Eric Biggers
2020-03-19  7:36     ` Jiri Slaby
2020-03-20  5:10       ` Eric Biggers
2020-03-20  6:57         ` Greg Kroah-Hartman
2020-03-18 22:38   ` [PATCH v2 2/2] vt: vt_ioctl: fix use-after-free in vt_in_use() Eric Biggers
2020-03-20 13:42     ` Jiri Slaby
2020-03-20 19:34       ` Eric Biggers
2020-03-22  3:43         ` [PATCH v3 0/2] vt: fix some vt_ioctl races Eric Biggers
2020-03-22  3:43           ` [PATCH v3 1/2] vt: vt_ioctl: fix VT_DISALLOCATE freeing in-use virtual console Eric Biggers
2020-03-27 10:28             ` Jiri Slaby
2020-03-22  3:43           ` [PATCH v3 2/2] vt: vt_ioctl: fix use-after-free in vt_in_use() Eric Biggers
2020-03-27 10:30             ` Jiri Slaby [this message]
2020-03-24 11:29           ` [PATCH v3 0/2] vt: fix some vt_ioctl races Greg Kroah-Hartman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=38de9a73-9321-09ba-6ef8-023a64c3c023@suse.cz \
    --to=jslaby@suse.cz \
    --cc=ebiggers@kernel.org \
    --cc=edumazet@google.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=nico@fluxnic.net \
    --cc=syzkaller-bugs@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).