From patchwork Tue Jan 8 20:18:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guilherme G. Piccoli" X-Patchwork-Id: 1022122 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=canonical.com Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43Z3WK2b0Cz9sMr; Wed, 9 Jan 2019 07:19:09 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1ggxq4-00085s-LM; Tue, 08 Jan 2019 20:19:04 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.86_2) (envelope-from ) id 1ggxq3-00084k-4V for kernel-team@lists.ubuntu.com; Tue, 08 Jan 2019 20:19:03 +0000 Received: from mail-qk1-f199.google.com ([209.85.222.199]) by youngberry.canonical.com with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.76) (envelope-from ) id 1ggxq2-0007Vc-Qu for kernel-team@lists.ubuntu.com; Tue, 08 Jan 2019 20:19:02 +0000 Received: by mail-qk1-f199.google.com with SMTP id y83so4164394qka.7 for ; Tue, 08 Jan 2019 12:19:02 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=St8VJCafTTS2QHvdo/it6cDlGJVQImEnEG+s1e5boR4=; b=JitR4shwLTJx9yqKfgXDC6cp4LNq/7Ki1VGTUF3dD6ruOF7khnr/2zpq/kn9KbQITe s2fK0stWl17i/PuqXmgnk13wqeyJiL8IQaSyBoGSEcS9UiilQkwD/yW1AroSH9B3k6JT NPz7MW4VZoZEXGLYTlEOEDFhPgMA6bm5uIqkIj6dFBbJuoCwBTaUjXnl6n2N4dqxVYUE g0C5THV5BSvt9qaxZOO8EnoEZa+mRQFC8Y7V86lntyXqHYl+Vz5ZOhsAyjmYiAYRmDrh u15fk8guhoXmybHHFq/8kLKNjHNDXs5MmA+MY4a4kal6/4HXA4HXgp0b4efeeGbAoRyo WDBw== X-Gm-Message-State: AJcUukfvCbHfxtwQI1WVbJBVtQ8D0YlT9Km1z/SCQKDBQf0MWIbuWtsl 1gXtZ40yaP4TSg54GOWt5wuxI7FYxexfzaOWzCmb/KCQ40NI9eswml437gjA4InM2ZuTtOltgNy UVca6H5BZYY3jCU58Gn9dslvJEgZHFxHWfEZQoCGncg== X-Received: by 2002:a37:a44e:: with SMTP id n75mr3010017qke.26.1546978741582; Tue, 08 Jan 2019 12:19:01 -0800 (PST) X-Google-Smtp-Source: ALg8bN6gyJ4KhaZdU4rXrF4ER/dJdeI6AtMLLMUND67T9iRtw7CYRyFjj4KWpV+Eg2nusSLacPAAXw== X-Received: by 2002:a37:a44e:: with SMTP id n75mr3010006qke.26.1546978741409; Tue, 08 Jan 2019 12:19:01 -0800 (PST) Received: from localhost ([191.13.50.232]) by smtp.gmail.com with ESMTPSA id x57sm39579583qtx.96.2019.01.08.12.19.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Jan 2019 12:19:00 -0800 (PST) From: "Guilherme G. Piccoli" To: kernel-team@lists.ubuntu.com Subject: [SRU X] [PATCH 3/5] tty: Hold tty_ldisc_lock() during tty_reopen() Date: Tue, 8 Jan 2019 18:18:47 -0200 Message-Id: <20190108201849.11907-4-gpiccoli@canonical.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190108201849.11907-1-gpiccoli@canonical.com> References: <20190108201849.11907-1-gpiccoli@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: gpiccoli@canonical.com Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Dmitry Safonov BugLink: https://bugs.launchpad.net/bugs/1791758 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 (cherry-picked from 83d817f41070c48bc3eb7ec18e43000a548fca5c upstream) Signed-off-by: Guilherme G. Piccoli --- drivers/tty/tty_io.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c index d2f00378fc18..3784f55f3dc9 100644 --- a/drivers/tty/tty_io.c +++ b/drivers/tty/tty_io.c @@ -1486,15 +1486,20 @@ static int tty_reopen(struct tty_struct *tty) 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; }