Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp4352026pxu; Wed, 9 Dec 2020 15:07:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJyakA1n3NaIaJkJ8P3zpgI1WXoAyCWarSB99+gaZd6PsIsfVvDwU6lrrhmsXuWFssQJtllt X-Received: by 2002:aa7:ca03:: with SMTP id y3mr4210665eds.87.1607555229519; Wed, 09 Dec 2020 15:07:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607555229; cv=none; d=google.com; s=arc-20160816; b=UoC+rEbK1Tf6cSCMe6fnw7gUaTXnTskMNANXTwth0erdzs8mhxHdpCZAUI0VP1kPsO jq3zbLftUzNlM/h3WQpqEv3EDgQ9QAnLATe9fqe+dzwJKua+eWzKxN9BqLpiRPmXioWa ZTSV5glg6N9uj/KC1uAtpFt0noje2ByBOPmagLApyZtpRJibGV2+xgxMgGxvMsxX1971 Phi4FqYutSCqn6CzxGfip8iFSF8j7hudOHr0U6p8fC5nx00wjYZBnPgq8U2ZSe6Se8oC 9NrWU9HGwGImKmYq4NcmUkRmOlYbdICKZbffD+01+slcDWPzstBlYuCMs+Ojr9WkBL1/ EYjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from; bh=3WzW06yW9veJTrp2Gui/C/xI5YcCf/ADJF2lW2T2Upc=; b=ZdtihaNTf+ntKBWU2eQ3YmPSwmGBcEGRMHiIL4CkShuKDPZTbP2uaN1yAGGfYYgtLG IrEdnRUG/tFX7h36ViwOMz9tzZyjcP1pyWEbRIbohx377FTueMVcZ+n6rGBsVISQCL7R G194vWOX1gV4wPtMDOpUivo6pNT0BkxGQ1UM+zRBJ78SaemyCVts5/bA/cvKCj5uVNvN gx2VDQ/oswLMyofD6niN8QZF1X84WX4msAQCjMeS10wx+py/2XjUPC89dBKuIzgsXkmG 5l7Nlq9zNlm48n+vLG1kXOO4NKLdMg13ORIT+mPqRfskjQW9TKmW6MT3qOTWUB5QSzBX KS8A== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e26si1918211edr.162.2020.12.09.15.06.45; Wed, 09 Dec 2020 15:07:09 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729745AbgLIKLh (ORCPT + 99 others); Wed, 9 Dec 2020 05:11:37 -0500 Received: from smtp.h3c.com ([60.191.123.50]:15420 "EHLO h3cspam02-ex.h3c.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729196AbgLIKLh (ORCPT ); Wed, 9 Dec 2020 05:11:37 -0500 Received: from DAG2EX08-IDC.srv.huawei-3com.com ([10.8.0.71]) by h3cspam02-ex.h3c.com with ESMTP id 0B9A9ndH016364; Wed, 9 Dec 2020 18:09:49 +0800 (GMT-8) (envelope-from gao.yanB@h3c.com) Received: from localhost.localdomain (10.99.212.201) by DAG2EX08-IDC.srv.huawei-3com.com (10.8.0.71) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2106.2; Wed, 9 Dec 2020 18:09:52 +0800 From: "Yan.Gao" To: , CC: , , "Yan.Gao" Subject: [PATCH] tty: Protect disc_data in n_tty_close and n_tty_flush_buffer Date: Wed, 9 Dec 2020 17:59:21 +0800 Message-ID: <20201209095921.40248-1-gao.yanB@h3c.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.99.212.201] X-ClientProxiedBy: BJSMTP01-EX.srv.huawei-3com.com (10.63.20.132) To DAG2EX08-IDC.srv.huawei-3com.com (10.8.0.71) X-DNSRBL: X-MAIL: h3cspam02-ex.h3c.com 0B9A9ndH016364 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 spinlock. 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 add spinlock 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 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c index 7e5e36315..f4b152f20 100644 --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -87,6 +87,8 @@ # define n_tty_trace(f, args...) no_printk(f, ##args) #endif +static DEFINE_SPINLOCK(disc_data_lock); + struct n_tty_data { /* producer-published */ size_t read_head; @@ -371,8 +373,10 @@ static void n_tty_packet_mode_flush(struct tty_struct *tty) static void n_tty_flush_buffer(struct tty_struct *tty) { down_write(&tty->termios_rwsem); + spin_lock(&disc_data_lock); reset_buffer_flags(tty->disc_data); n_tty_kick_worker(tty); + spin_unlock(&disc_data_lock); if (tty->link) n_tty_packet_mode_flush(tty); @@ -1892,8 +1896,10 @@ static void n_tty_close(struct tty_struct *tty) if (tty->link) n_tty_packet_mode_flush(tty); + spin_lock_irq(&disc_data_lock); vfree(ldata); tty->disc_data = NULL; + spin_unlock_irq(&disc_data_lock); } /** -- 2.17.1