Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6273011imu; Mon, 21 Jan 2019 06:11:43 -0800 (PST) X-Google-Smtp-Source: ALg8bN6FWoIvqgnenmrTCeOe6deBbwMOXlCKgVECB+SbpnQJK1jfai+DposqjyZK/uVOWK7Q3EYO X-Received: by 2002:a63:4d0e:: with SMTP id a14mr28526939pgb.408.1548079903905; Mon, 21 Jan 2019 06:11:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548079903; cv=none; d=google.com; s=arc-20160816; b=gNnQ2vlfSblqF9/qxirP19F7XuickrorhqaI++4bUVRS3LzmnLPs7/huoa75MVaWr5 P7LyCaqbZ5aCo9oc/mREKFJsJGOEgB8ZnLWqBuawCO7QnHxaNxEM0V8W6SaCn8wE4ypD S1By6uncJ5EFgnxA/6S5T6E420uJRy3spsacz3geB6enBPcDBf9OFeqYZsSwKgADlqI2 mBpnVuJsggnga71MR0RUQdxr7j6ireqsUDwW8uBMAL3hU6LrBhMycQS8eWAEaEmZnZKA T8FBhvUyjDOdnm1ZCXpEStu5fTyrp3FFbMwO59eS530sYHUyak7QDL1qW1+cS3KEXJFn ks9A== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=TKp1kEnmlbbfGP3QqLf/17ELuuh+kKmuZzwRCOSH/+s=; b=mRbqU/u7K1EdnDLFmi33Cvn6dO1SLFSpiF/RGcCbSfA5aAQ+soNRpUcryw9l9r7nMp DKlf50MVAWVfCkAOnkWDKcIWo+ppg7f+KKrGJwECkFSC/vlzXi4XJMFM3bj36aOGX8v3 FkZl/r5S8fsu4Xa1eUCCkqE2Q5kcxOhG9yGmP8unoMu/DVcafKyUoS2scLv5gf4rV7PP jFkyPc2MRofcFRl4vbt6bGtcwOVEKTeRUjBZXzAUZ0xpnQJusOO1WD68jS5H3+B+95xH i+5IVrZ5pLwSyEL8oG1NajmiI52EVCb6lTeiBq6Si/RSASPd94prm64UpdVZBalRM7S3 w+eA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Lmn4rSUb; 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 a8si9622891ple.216.2019.01.21.06.11.28; Mon, 21 Jan 2019 06:11:43 -0800 (PST) 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=Lmn4rSUb; 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 S1731605AbfAUNyx (ORCPT + 99 others); Mon, 21 Jan 2019 08:54:53 -0500 Received: from mail.kernel.org ([198.145.29.99]:39192 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731593AbfAUNyv (ORCPT ); Mon, 21 Jan 2019 08:54:51 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.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 58F1420879; Mon, 21 Jan 2019 13:54:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1548078890; bh=Z7bmrNysSk5GYYhMHmnJYusxWmezQrliKcrv5hoQkZs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Lmn4rSUb+YVMJZJ+1cKCOaIwbaeTWuexmoHkDtPb9BvyK3+RxJtQn0EV/5jzcIrgl 1r3IjJqD5QaM3KxmWWKZrzmLXD4pbYzVJgahE0UEevHle/jW5+eg4B2XyWXNRF7T+n 2S00A03GrYIYpkZD0RDekCutVRP0BMEvckvny7iw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jiri Slaby , Jiri Slaby , syzbot+3aa9784721dfb90e984d@syzkaller.appspotmail.com, Mark Rutland , Tetsuo Handa , Dmitry Safonov , Tycho Andersen Subject: [PATCH 4.9 02/51] tty: Hold tty_ldisc_lock() during tty_reopen() Date: Mon, 21 Jan 2019 14:43:58 +0100 Message-Id: <20190121122453.838900027@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190121122453.700446926@linuxfoundation.org> References: <20190121122453.700446926@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.9-stable review patch. If anyone has any objections, please let me know. ------------------ From: Dmitry Safonov commit 83d817f41070c48bc3eb7ec18e43000a548fca5c upstream. tty_ldisc_reinit() doesn't race with neither tty_ldisc_hangup() nor set_ldisc() nor tty_ldisc_release() as they use tty lock. But it races with anyone who expects line discipline to be the same after hoding read semaphore in tty_ldisc_ref(). We've seen the following crash on v4.9.108 stable: BUG: unable to handle kernel paging request at 0000000000002260 IP: [..] n_tty_receive_buf_common+0x5f/0x86d Workqueue: events_unbound flush_to_ldisc Call Trace: [..] n_tty_receive_buf2 [..] tty_ldisc_receive_buf [..] flush_to_ldisc [..] process_one_work [..] worker_thread [..] kthread [..] ret_from_fork tty_ldisc_reinit() should be called with ldisc_sem hold for writing, which will protect any reader against line discipline changes. Cc: Jiri Slaby Cc: stable@vger.kernel.org # b027e2298bd5 ("tty: fix data race between tty_init_dev and flush of buf") Reviewed-by: Jiri Slaby Reported-by: syzbot+3aa9784721dfb90e984d@syzkaller.appspotmail.com Tested-by: Mark Rutland Tested-by: Tetsuo Handa Signed-off-by: Dmitry Safonov Tested-by: Tycho Andersen Signed-off-by: Greg Kroah-Hartman Signed-off-by: Greg Kroah-Hartman --- drivers/tty/tty_io.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) --- a/drivers/tty/tty_io.c +++ b/drivers/tty/tty_io.c @@ -1487,15 +1487,20 @@ static int tty_reopen(struct tty_struct if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN)) return -EBUSY; - tty->count++; + retval = tty_ldisc_lock(tty, 5 * HZ); + if (retval) + return retval; + tty->count++; if (tty->ldisc) - return 0; + goto out_unlock; retval = tty_ldisc_reinit(tty, tty->termios.c_line); if (retval) tty->count--; +out_unlock: + tty_ldisc_unlock(tty); return retval; }