Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp4582854pxu; Wed, 9 Dec 2020 23:31:16 -0800 (PST) X-Google-Smtp-Source: ABdhPJyJHPwMbfOKQPYf1GdLo1zY8vh9GdEzUN4QbW0bDAwXbtUD7WRjOC18cTW0m5S+95N/C0EO X-Received: by 2002:a17:906:85cd:: with SMTP id i13mr5274840ejy.553.1607585476012; Wed, 09 Dec 2020 23:31:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607585476; cv=none; d=google.com; s=arc-20160816; b=mHiGKgOeO26/r5H8AORxPr1GLbS3XUYbTm9PyAH4D7o5XGZVdcEBk5gj+zFDs7x6bi v2f/KQUbVUtuToziFyjApakaMEfyJqd9jbUVJ3moesOp/ULsfn5dOtJaOZvGH8zn6C79 fRLPVpFE5ZheMEOdLz4rKM7r+WdELMEZ2lv+2RSl+J0qkSGIhtbiCbuLkWgTOmyE/3e8 grXSKChb1T/mjz9+Yrka1Ghpd3GG4CXMedcTY/SY/+QugDG+O2d+APp8DrSUBLe+ykRZ D8XF8lTog0yjP0EhzzDm/MJYc0v7c7mr9zyEx5VJQfyKXz2FgGrRyIwMDv/yA89PxJmv WWeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:dkim-signature:date; bh=wcO9BTglSOkbjVtqcg3+jvEheG/oZN6yU/qMAP6XbSs=; b=GJpKOGtcJ06+Yl1ce0BALyHfiZlzYRWaqZX/G3d2gRJ0N5zshtBxgL45qc+pxTgKZU x7RXY7QwC80opbIv7hmYmSSdFaBDa7/WTp9MCdweHL8Ty6KSa2T8EMrtEbkV9bLFZ7Uk i0ZvlLFb2c2IKFpz8ENeZ57eh/VaXSzXDPrzXNHwz1A/o666f/jRwkkHAObdVagsM3wo HeOB8XaTwhde/L4WhPeHRGdML3WGenvch+rFh7Vd621nnMWpFnMu0x2KlpHZVHyeSacb rlVhyLzYVB890aWo1kVrPjOB3jKNzaLVBaPHWnBjje/fg1U7jvh2llJKGaLFGaSmakhc +/5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2JfcCjWZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bt17si2245647edb.469.2020.12.09.23.30.52; Wed, 09 Dec 2020 23:31:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2JfcCjWZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733282AbgLJGWi (ORCPT + 99 others); Thu, 10 Dec 2020 01:22:38 -0500 Received: from mail.kernel.org ([198.145.29.99]:48032 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726061AbgLJGWX (ORCPT ); Thu, 10 Dec 2020 01:22:23 -0500 Date: Thu, 10 Dec 2020 07:21:32 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1607581296; bh=EBkD1Ctf5CppZSrk+VnScGGdvgMF40s0wsgBczCzH0I=; h=From:To:Cc:Subject:References:In-Reply-To:From; b=2JfcCjWZ4ci7MklmOhMfXkkvh+AE2HEzMvjWcS5ALQwodvt0u7UmrKNpzpIsbYi9S JR7T09n0bRxTHqqagY4o1EX79DVOUK3TTtkw2cFol+pomm7YVmqY+NKu2qo9xKyVA3 GqZ4RZKEgx+WWG/sbw9zr8Rff8mAFVZTDB+/yHNs= From: Greg KH To: "Yan.Gao" Cc: jirislaby@kernel.org, linux-kernel@vger.kernel.org, tian.xianting@h3c.com Subject: Re: [PATCH] [v2] tty: Protect disc_data in n_tty_close and n_tty_flush_buffer Message-ID: References: <20201210022507.30729-1-gao.yanB@h3c.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201210022507.30729-1-gao.yanB@h3c.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Dec 10, 2020 at 10:25:07AM +0800, Yan.Gao wrote: > n_tty_flush_buffer can happen in parallel with n_tty_close that the > tty->disc_data will be set to NULL. n_tty_flush_buffer accesses > tty->disc_data, so we must prevent n_tty_close clear tty->disc_data > while n_tty_flush_buffer has a non-NULL view of tty->disc_data. > > So we need to make sure that accesses to disc_data are atomic using > tty->termios_rwsem. > > There is an example I meet: > When n_tty_flush_buffer accesses tty struct, the disc_data is right. > However, then reset_buffer_flags accesses tty->disc_data, disc_data > become NULL, So kernel crash when accesses tty->disc_data->real_tail. > I guess there could be another thread change tty->disc_data to NULL, > and during N_TTY line discipline, n_tty_close will set tty->disc_data > to be NULL. So use tty->termios_rwsem to protect disc_data between close > and flush_buffer. > > IP: reset_buffer_flags+0x9/0xf0 > PGD 0 P4D 0 > Oops: 0002 [#1] SMP > CPU: 23 PID: 2087626 Comm: (agetty) Kdump: loaded Tainted: G > Hardware name: UNISINSIGHT X3036P-G3/ST01M2C7S, BIOS 2.00.13 01/11/2019 > task: ffff9c4e9da71e80 task.stack: ffffb30cfe898000 > RIP: 0010:reset_buffer_flags+0x9/0xf0 > RSP: 0018:ffffb30cfe89bca8 EFLAGS: 00010246 > RAX: ffff9c4e9da71e80 RBX: ffff9c368d1bac00 RCX: 0000000000000000 > RDX: 0000000000000000 RSI: ffff9c4ea17b50f0 RDI: 0000000000000000 > RBP: ffffb30cfe89bcc8 R08: 0000000000000100 R09: 0000000000000001 > R10: 0000000000000001 R11: 0000000000000000 R12: ffff9c368d1bacc0 > R13: ffff9c20cfd18428 R14: ffff9c4ea17b50f0 R15: ffff9c368d1bac00 > FS: 00007f9fbbe97940(0000) GS:ffff9c375c740000(0000) > knlGS:0000000000000000 > CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > CR2: 0000000000002260 CR3: 0000002f72233003 CR4: 00000000007606e0 > DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 > DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 > PKRU: 55555554 > Call Trace: > ? n_tty_flush_buffer+0x2a/0x60 > tty_buffer_flush+0x76/0x90 > tty_ldisc_flush+0x22/0x40 > vt_ioctl+0x5a7/0x10b0 > ? n_tty_ioctl_helper+0x27/0x110 > tty_ioctl+0xef/0x8c0 > do_vfs_ioctl+0xa7/0x5e0 > ? __audit_syscall_entry+0xaf/0x100 > ? syscall_trace_enter+0x1d0/0x2b0 > SyS_ioctl+0x79/0x90 > do_syscall_64+0x6c/0x1b0 > entry_SYSCALL64_slow_path+0x25/0x25 > > n_tty_flush_buffer --->tty->disc_data is OK > ->reset_buffer_flags -->tty->disc_data is NULL > > Signed-off-by: Yan.Gao > Reviewed-by: Xianting Tian > --- > drivers/tty/n_tty.c | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c > index 7e5e36315..e78124ce1 100644 > --- a/drivers/tty/n_tty.c > +++ b/drivers/tty/n_tty.c > @@ -1892,8 +1892,10 @@ static void n_tty_close(struct tty_struct *tty) > if (tty->link) > n_tty_packet_mode_flush(tty); > > + down_write(&tty->termios_rwsem); > vfree(ldata); > tty->disc_data = NULL; > + up_write(&tty->termios_rwsem); > } > > /** So does this solve your problem in testing? Do you have a reproducer for this problem? thanks, greg k-h