Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp1128764ybh; Wed, 18 Mar 2020 15:41:28 -0700 (PDT) X-Google-Smtp-Source: ADFU+vu/KuCPvXZG0oZKVk8Mqo75ao4apgsTtA9IMvft2k3v2ldgak/u4tDpCB1U/Q0hylzE/TCr X-Received: by 2002:aca:5403:: with SMTP id i3mr212765oib.174.1584571288660; Wed, 18 Mar 2020 15:41:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584571288; cv=none; d=google.com; s=arc-20160816; b=qSxqFakp4D0nbjxStl7WzdNPvP7qVp1jOFxTOF8XFvHEaJFWBnHk2jffqR0pKkoe0c 0YyerdG2eLzRTwd66zxZVaOqpIVgQ/ykwjtCNB1eKOIzPHPwkvgk4/xd5wlY0IXVLI/j dWhBjtnpGwnbgHf+DFdApR8AlcOVTJYZ1svmlEm4jHVb+cTcIPGOj3Gq+f+LoOlaeDA1 Tybop6HJ9JbPb+IGOP+QY2Xds0IA7NpfHltMnuNx3oVau9jD3+IO4vn9C9pWWKXT3Ac1 CDcDsP3Gj+F6UIeLk4SMDfBFYoFPE4cJWKdVZmWDG/xRhThnZxVSY8KAo3XrluGed/EP ggLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=n1t4SO7V++3bTKGUQs/nV36gOxZ2M2GpBPkFyga2D5c=; b=CCmMJgwXK7HWI4rh8Y9knFNfE8vZUiF/wW333a3WRCDcXhrAodgrXwRVZJc1E97UEV V4MjyYb5O2aqPLLTznYxFHBZF3sIfP8Sozw92aHIXUTR5rGe+OLbX7vIVtIItOSIpCUl Xd1SZ77mrZQ2U03Z/EsNi3x5lADAtR6/T6WimYRT+TEH0z3dlrqFQn4U691Lnhlo/7R7 mRB/XoDLCNoH8lIbNkeAFr9ePlIl9S6uxnVlIVKS0SSBBzOu9sJGS9UwijIf1TbMQbro RvHYCv1TL3XcZCr12ONFtKnS8gTaZALbjSS2HP1lIrPWG0j6B4TRh55vXB10jnoMrfix cQiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=YjtLUyHD; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m2si189032otr.270.2020.03.18.15.41.16; Wed, 18 Mar 2020 15:41:28 -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=YjtLUyHD; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727352AbgCRWkR (ORCPT + 99 others); Wed, 18 Mar 2020 18:40:17 -0400 Received: from mail.kernel.org ([198.145.29.99]:54230 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726795AbgCRWkM (ORCPT ); Wed, 18 Mar 2020 18:40:12 -0400 Received: from sol.hsd1.ca.comcast.net (c-107-3-166-239.hsd1.ca.comcast.net [107.3.166.239]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id DA3E42076C; Wed, 18 Mar 2020 22:40:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1584571211; bh=mWITq1H2XrmeVHpjdvnmMMo+okm1i3eV3ifbsG6jwno=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YjtLUyHD9fxTg9BEJbmQGt5zw96zOyq0a6j7bEiL7jS78n5PM6tXfsarLSkPfDOzq x9LSNEvVMW+jwvQF0WBH2qbGr3oHtFRvhm+zMwr4JfAzgbVUuzuTHVDyGKbeVR0crJ QvP4ir4usP+uCoj5DQgLDXTup+Eff7lGJeBNZFvE= From: Eric Biggers To: Greg Kroah-Hartman , Jiri Slaby Cc: linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org, syzkaller-bugs@googlegroups.com, Eric Dumazet , Nicolas Pitre Subject: [PATCH v2 1/2] vt: vt_ioctl: fix VT_DISALLOCATE freeing in-use virtual console Date: Wed, 18 Mar 2020 15:38:09 -0700 Message-Id: <20200318223810.162440-2-ebiggers@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200318223810.162440-1-ebiggers@kernel.org> References: <20200318222704.GC2334@sol.localdomain> <20200318223810.162440-1-ebiggers@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Biggers The VT_DISALLOCATE ioctl can free a virtual console while tty_release() is still running, causing a use-after-free in con_shutdown(). This occurs because VT_DISALLOCATE considers a virtual console's 'struct vc_data' to be unused as soon as the corresponding tty's refcount hits 0. But actually it may be still being closed. Fix this by making vc_data be reference-counted via the embedded 'struct tty_port'. A newly allocated virtual console has refcount 1. Opening it for the first time increments the refcount to 2. Closing it for the last time decrements the refcount (in tty_operations::cleanup() so that it happens late enough), as does VT_DISALLOCATE. Reproducer: #include #include #include #include int main() { if (fork()) { for (;;) close(open("/dev/tty5", O_RDWR)); } else { int fd = open("/dev/tty10", O_RDWR); for (;;) ioctl(fd, VT_DISALLOCATE, 5); } } 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 [...] 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.c | 14 +++++++++++++- drivers/tty/vt/vt_ioctl.c | 12 ++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c index bbc26d73209a4..ec34f1f5f3bb5 100644 --- a/drivers/tty/vt/vt.c +++ b/drivers/tty/vt/vt.c @@ -1102,6 +1102,9 @@ int vc_allocate(unsigned int currcons) /* return 0 on success */ tty_port_init(&vc->port); INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK); + /* if this wasn't the case, we'd have to implement port->ops.destruct */ + BUILD_BUG_ON(offsetof(struct vc_data, port) != 0); + visual_init(vc, currcons, 1); if (!*vc->vc_uni_pagedir_loc) @@ -3250,6 +3253,7 @@ static int con_install(struct tty_driver *driver, struct tty_struct *tty) tty->driver_data = vc; vc->port.tty = tty; + tty_port_get(&vc->port); if (!tty->winsize.ws_row && !tty->winsize.ws_col) { tty->winsize.ws_row = vc_cons[currcons].d->vc_rows; @@ -3285,6 +3289,13 @@ static void con_shutdown(struct tty_struct *tty) console_unlock(); } +static void con_cleanup(struct tty_struct *tty) +{ + struct vc_data *vc = tty->driver_data; + + tty_port_put(&vc->port); +} + static int default_color = 7; /* white */ static int default_italic_color = 2; // green (ASCII) static int default_underline_color = 3; // cyan (ASCII) @@ -3410,7 +3421,8 @@ static const struct tty_operations con_ops = { .throttle = con_throttle, .unthrottle = con_unthrottle, .resize = vt_resize, - .shutdown = con_shutdown + .shutdown = con_shutdown, + .cleanup = con_cleanup, }; static struct cdev vc0_cdev; diff --git a/drivers/tty/vt/vt_ioctl.c b/drivers/tty/vt/vt_ioctl.c index 7297997fcf04c..f62f498f63c05 100644 --- a/drivers/tty/vt/vt_ioctl.c +++ b/drivers/tty/vt/vt_ioctl.c @@ -310,10 +310,8 @@ static int vt_disallocate(unsigned int vc_num) vc = vc_deallocate(vc_num); console_unlock(); - if (vc && vc_num >= MIN_NR_CONSOLES) { - tty_port_destroy(&vc->port); - kfree(vc); - } + if (vc && vc_num >= MIN_NR_CONSOLES) + tty_port_put(&vc->port); return ret; } @@ -333,10 +331,8 @@ static void vt_disallocate_all(void) console_unlock(); for (i = 1; i < MAX_NR_CONSOLES; i++) { - if (vc[i] && i >= MIN_NR_CONSOLES) { - tty_port_destroy(&vc[i]->port); - kfree(vc[i]); - } + if (vc[i] && i >= MIN_NR_CONSOLES) + tty_port_put(&vc[i]->port); } } -- 2.25.1