Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp455477ybh; Wed, 18 Mar 2020 03:06:55 -0700 (PDT) X-Google-Smtp-Source: ADFU+vt0CII4/jFaLcMydd6YS5vwTnfztVANdvCbss1eNtk4Ujduw99DLYH48lrdzmZQpP3Vv6Mp X-Received: by 2002:a9d:64ca:: with SMTP id n10mr3164127otl.325.1584526015417; Wed, 18 Mar 2020 03:06:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584526015; cv=none; d=google.com; s=arc-20160816; b=RByfZ7F8dqgYpGhD2Xw0O97sD+IZa7MT+O+aZow7KDJcMENAb7CU0Tvl7lSuNltPP/ i0bwDH1J6Q03lQ2JazIvkzk8r1rRv2DZcwCA2VP/I+qGjK1F4Fdu7qaHB3+Ekjj65Fpm qXrBrzbZzzcqCDA3lZQP6drVyM76Mfx1jTXKWlW7pJc+O8XlCOKHvHl/IH5gmDyGtm1T 9tSK8AjmPKRRH+PZnd0H82SjcsD/N0eE+SfEN+HaHZuCvsDSGaHalBHjyhbww/0SyK90 GELSLtNpQGTCMLSTu156yANZ/kpxUo8Y5MuTgsgBr6zqrnr0wSyZmNnr2nyq0hPtv51T Qlqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=K/xd0jbgsC2Z4hv+ahmM1NVzGZruF7n0H732DcvW2nQ=; b=HqAkbrzOcZ7v/Zqj2CMKJsMJI1BelzxFuWA9+zTnq/oCUr1aL+9dSFRv4KHUGNKu2m vJFF+TSCtU2EOmTJYpWx/e+DRQJXZf/sks2A4zSaxI7vXZGrGFvqk981MkbqUWVgnWqb mHA1/lre45S3BaOnyjNi1lUnspX6AKRzoGcpXroPcuX1JR0Qa4C5R6kDd8vBhxvessWo 5TACm1Ipl2yiYJp57iWluRqg6+mflsSz8oY9Mknqq4gDkpy46IFl7RqZD4XEK+yFFuBH ZbAafCFo9SQbRAHf2D7VNsMWyCyNIu1Ir8lXn7u0NkCkPsziALD4FB7XgT9UhW5AnLvg ti+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=uYBvHY49; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 94si3139420otc.25.2020.03.18.03.06.43; Wed, 18 Mar 2020 03:06:55 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=uYBvHY49; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727547AbgCRKGR (ORCPT + 99 others); Wed, 18 Mar 2020 06:06:17 -0400 Received: from mail.kernel.org ([198.145.29.99]:52078 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726310AbgCRKGR (ORCPT ); Wed, 18 Mar 2020 06:06:17 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id DE29E20771; Wed, 18 Mar 2020 10:06:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1584525975; bh=Z+m3NbivKu0rrSxAnQYJJFG0z90zcyqOPZwMUe7Nzi0=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=uYBvHY49o6QYudJ2QiwHu1AzF/9joVIoCl2PV6ab4Zc0umEO5DECg19XAvV0sxwXa 7xput7FARmVaiJK34IVDIYv/ZCkKbBuGDIngqnQTn1ynU0viazfjq4N2nOlO5v5U3q TJMBYS9KF/L+RJYdh/krZFLlvBgNW4Rl5+yXPH5I= Date: Wed, 18 Mar 2020 11:06:12 +0100 From: Greg Kroah-Hartman To: Eric Biggers Cc: Jiri Slaby , linux-kernel@vger.kernel.org, syzkaller-bugs@googlegroups.com, Eric Dumazet , Nicolas Pitre , Alan Cox Subject: Re: [PATCH] vt: vt_ioctl: fix VT_DISALLOCATE freeing in-use virtual console Message-ID: <20200318100612.GA2044908@kroah.com> References: <0000000000006663de0598d25ab1@google.com> <20200224071247.283098-1-ebiggers@kernel.org> <8fb00b38-abd0-6895-3ad2-85a6f05ee6cf@suse.com> <20200224081913.GA299238@sol.localdomain> <20200302212306.GA78660@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200302212306.GA78660@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Mar 02, 2020 at 01:23:06PM -0800, Eric Biggers wrote: > On Mon, Feb 24, 2020 at 12:19:13AM -0800, Eric Biggers wrote: > > On Mon, Feb 24, 2020 at 09:04:33AM +0100, Jiri Slaby wrote: > > > > KASAN report: > > > > BUG: KASAN: use-after-free in con_shutdown+0x76/0x80 drivers/tty/vt/vt.c:3278 > > > > Write of size 8 at addr ffff88806a4ec108 by task syz_vt/129 > > > > > > > > CPU: 0 PID: 129 Comm: syz_vt Not tainted 5.6.0-rc2 #11 > > > > Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS ?-20191223_100556-anatol 04/01/2014 > > > > Call Trace: > > > > [...] > > > > con_shutdown+0x76/0x80 drivers/tty/vt/vt.c:3278 > > > > release_tty+0xa8/0x410 drivers/tty/tty_io.c:1514 > > > > tty_release_struct+0x34/0x50 drivers/tty/tty_io.c:1629 > > > > tty_release+0x984/0xed0 drivers/tty/tty_io.c:1789 > > > > [...] > > > > > > > > Allocated by task 129: > > > > [...] > > > > kzalloc include/linux/slab.h:669 [inline] > > > > vc_allocate drivers/tty/vt/vt.c:1085 [inline] > > > > vc_allocate+0x1ac/0x680 drivers/tty/vt/vt.c:1066 > > > > con_install+0x4d/0x3f0 drivers/tty/vt/vt.c:3229 > > > > tty_driver_install_tty drivers/tty/tty_io.c:1228 [inline] > > > > tty_init_dev+0x94/0x350 drivers/tty/tty_io.c:1341 > > > > tty_open_by_driver drivers/tty/tty_io.c:1987 [inline] > > > > tty_open+0x3ca/0xb30 drivers/tty/tty_io.c:2035 > > > > [...] > > > > > > > > Freed by task 130: > > > > [...] > > > > kfree+0xbf/0x1e0 mm/slab.c:3757 > > > > vt_disallocate drivers/tty/vt/vt_ioctl.c:300 [inline] > > > > vt_ioctl+0x16dc/0x1e30 drivers/tty/vt/vt_ioctl.c:818 > > > > tty_ioctl+0x9db/0x11b0 drivers/tty/tty_io.c:2660 > > > > > > That means the associated tty_port is destroyed while the tty layer > > > still has a tty on the top of it. That is a BUG anyway. > > > > > > > Fixes: 4001d7b7fc27 ("vt: push down the tty lock so we can see what is left to tackle") > > > > Cc: # v3.4+ > > > > Reported-by: syzbot+522643ab5729b0421998@syzkaller.appspotmail.com > > > > Signed-off-by: Eric Biggers > > > > --- > > > > drivers/tty/vt/vt_ioctl.c | 6 +++++- > > > > 1 file changed, 5 insertions(+), 1 deletion(-) > > > > > > > > diff --git a/drivers/tty/vt/vt_ioctl.c b/drivers/tty/vt/vt_ioctl.c > > > > index ee6c91ef1f6cf..57d681706fa85 100644 > > > > --- a/drivers/tty/vt/vt_ioctl.c > > > > +++ b/drivers/tty/vt/vt_ioctl.c > > > > @@ -42,7 +42,7 @@ > > > > char vt_dont_switch; > > > > extern struct tty_driver *console_driver; > > > > > > > > -#define VT_IS_IN_USE(i) (console_driver->ttys[i] && console_driver->ttys[i]->count) > > > > +#define VT_IS_IN_USE(i) (console_driver->ttys[i] != NULL) > > > > #define VT_BUSY(i) (VT_IS_IN_USE(i) || i == fg_console || vc_cons[i].d == sel_cons) > > > > > > > > /* > > > > @@ -288,12 +288,14 @@ static int vt_disallocate(unsigned int vc_num) > > > > struct vc_data *vc = NULL; > > > > int ret = 0; > > > > > > > > + mutex_lock(&tty_mutex); /* synchronize with release_tty() */ > > > > console_lock(); > > > > > > Is this lock dependency new or pre-existing? > > > > It's the same locking order used during release_tty(). > > > > > > > > Locking tty_mutex here does not sound quite right. What about switching > > > vc_data to proper refcounting based on tty_port? (Instead of doing > > > tty_port_destroy and kfree in vt_disallocate*.) > > > > > > > How would that work? We could make struct vc_data refcounted such that > > VT_DISALLOCATE doesn't free it right away but rather it's freed in the next > > con_shutdown(). But release_tty() still accesses tty->port afterwards, which is > > part of the 'struct vc_data' that would have just been freed. > > > > Jiri, can you explain what you meant here? I don't see how your suggestion > would solve the problem. > > Greg, any opinion? I'll defer to Jiri here :)