Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1807907imm; Mon, 3 Sep 2018 09:58:59 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYs5tDG0jX/H69YXbs3XiQO3AiLL6hvNZ68ZccutjkY/Bm5TlOD3aOh7bOJsYTCA1kTDK9k X-Received: by 2002:a62:4dc1:: with SMTP id a184-v6mr30310254pfb.5.1535993939599; Mon, 03 Sep 2018 09:58:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535993939; cv=none; d=google.com; s=arc-20160816; b=rLQnmTDKNPhH7+fm+rhisW5BPwCBgUpG04s9aRFDoMS5aK3ih05Fyik8eZpkfero6q YtWVNJtUuIsBbL8D1RPKwqf19Wkf8HsVyQ4NcFX+bt43M8v8AX5NMyaGahyG0WuGDbZ9 4neF2e/8O971SuFOOMRKN9nMTxQ2NHUy/Ik+Bpy13bAokMtsz/zsEJt9SQNSqZxqFIEt /GDHeaGxlujH7swW5od8RrivYY4j1aDb3WnvVOWbmzNdd8Oe/yH71F5ztb4/Kfm7En/c SeOqln0Cnpe1wJSF/nbs3zQIpQTziqCEHulYwoFL9EKseQKcs75rjZ7x8/1KDdj0ESsy IO7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=7/xDAv4nmvZM6/MSUvhwMtYq6DkyvBAstjsxswpf2i0=; b=wZUmJKNnDW+rY/1wWxi+vE/6gqbUwTgdCU7GyLPbCqVdQUKyDs+kdGXCRcgpESqfor myeahV0giYDYtNHzUQ7wkbhuuYsjJG9kD0jO/a49Ddf2xVCoV1vgYVYMpI7KuXpUY8iA ogtHQL1FZJwUnXpXJ87jPntQcVwmGfS1GwCMYGIaLhEclg6hJL4JkT+q42zE8ytl12OP YRpagexQWY5LwMlhsKC7b63wJerXvDY9KT1sOCEPAM2/p9INpYfEFwDBoNkencNFyhHC j2VEl7uFuoOMFA4raEcBj5lrJpEYw3o57v9irZwtHi/SSIX8luRe86lAIVA6+D2osgQi kcgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@arista.com header.s=googlenew header.b=g9nzNJfh; 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=QUARANTINE sp=REJECT dis=NONE) header.from=arista.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a186-v6si17486876pge.408.2018.09.03.09.58.44; Mon, 03 Sep 2018 09:58:59 -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=@arista.com header.s=googlenew header.b=g9nzNJfh; 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=QUARANTINE sp=REJECT dis=NONE) header.from=arista.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728666AbeICVOB (ORCPT + 99 others); Mon, 3 Sep 2018 17:14:01 -0400 Received: from mail-ed1-f67.google.com ([209.85.208.67]:37881 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728640AbeICVOA (ORCPT ); Mon, 3 Sep 2018 17:14:00 -0400 Received: by mail-ed1-f67.google.com with SMTP id a20-v6so1239985edd.4 for ; Mon, 03 Sep 2018 09:53:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=googlenew; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=7/xDAv4nmvZM6/MSUvhwMtYq6DkyvBAstjsxswpf2i0=; b=g9nzNJfh9mHewg/e6MdLX7nSvvdht7dtf6cFFA6JT9J6yMrw5iDNqdbYiTp0/fZeHB tvke5/ey3IGv0CgL/4uRQ33rOozi47VdN2bR3EusgQA/Bp0QRG6J78t750VM2O6nfQkc K06qNAww4Nr8HmgUbYOys8NQAjt0lFRKsZjNMn9nyYe9Gln8stL/p8TkP8HtStWd5bxY Rmf7HwW49qRfV/Re9+xgqjZ8Hvyil/GDA4N2ZxLSpgr/JRqhYuMAiJ/FP8hhKn3MzU9L uJKEwJtzqkqQVeW7HX0qIbP9hc5HbTxKNuH7qsW15fxIZyG8bWSodUJrjl4AAEBURrne aOww== 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; bh=7/xDAv4nmvZM6/MSUvhwMtYq6DkyvBAstjsxswpf2i0=; b=HwUNpHaPzoFJuUV612vm3HWzDEmsUfsKycH+Zid7/+vHSUNF3+4qk1UB8999Sr23q1 OEOzMujU98wFhyvrjBikMFFUgKn3/3srhk/e0vXC0VfAJzf205ZhsIfj06NmYvh70Kjw Nch+pLbzF06zf5r8qLzPPZsVHjHMr+AWzKUWLOwiyuXLVDOXhQ4oxwVH+G0pY84YL3P+ IApvKHf0nacaGsJzS4r3rxUCp81I6kaY9064fSy+9F364Fp4l33BY/qyw058gbcxp2a6 ohZR1s38CwfJxjHl3QuY8qub9iPoU1hYfQgatXu2MHx5ZT6y78wJTYfOeBMqkBqD3pbc lLkw== X-Gm-Message-State: APzg51BFmpiGYi4Z0mI58YOjFne0W2RGOaT6HMRLNVN0bak6U0V4I61Y rLNsfdFvPoZezL4zjqQ4KBFz2WNTt44= X-Received: by 2002:a50:f297:: with SMTP id f23-v6mr33437770edm.40.1535993582809; Mon, 03 Sep 2018 09:53:02 -0700 (PDT) Received: from dhcp.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id m35-v6sm8085745ede.14.2018.09.03.09.53.01 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 03 Sep 2018 09:53:02 -0700 (PDT) From: Dmitry Safonov To: linux-kernel@vger.kernel.org Cc: Dmitry Safonov <0x7f454c46@gmail.com>, Dmitry Safonov , Daniel Axtens , Dmitry Vyukov , Michael Neuling , Mikulas Patocka , Nathan March , =?UTF-8?q?Pasi=20K=C3=A4rkk=C3=A4inen?= , Peter Hurley , Sergey Senozhatsky , Tan Xiaojun , Tetsuo Handa , syzbot+3aa9784721dfb90e984d@syzkaller.appspotmail.com, Tetsuo Handa , Greg Kroah-Hartman , Jiri Slaby , stable@vger.kernel.org Subject: [PATCHv2 2/4] tty: Hold tty_ldisc_lock() during tty_reopen() Date: Mon, 3 Sep 2018 17:52:55 +0100 Message-Id: <20180903165257.29227-3-dima@arista.com> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20180903165257.29227-1-dima@arista.com> References: <20180903165257.29227-1-dima@arista.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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: Greg Kroah-Hartman Cc: Jiri Slaby Cc: stable@vger.kernel.org # depends on commit b027e2298bd5 ("tty: fix data race between tty_init_dev and flush of buf") Reported-by: syzbot+3aa9784721dfb90e984d@syzkaller.appspotmail.com Tested-by: Tetsuo Handa Signed-off-by: Dmitry Safonov --- drivers/tty/tty_io.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c index 5e5da9acaf0a..a947719b4626 100644 --- a/drivers/tty/tty_io.c +++ b/drivers/tty/tty_io.c @@ -1255,7 +1255,7 @@ static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct * static int tty_reopen(struct tty_struct *tty) { struct tty_driver *driver = tty->driver; - int retval; + int retval = 0; if (driver->type == TTY_DRIVER_TYPE_PTY && driver->subtype == PTY_TYPE_MASTER) @@ -1267,15 +1267,18 @@ static int tty_reopen(struct tty_struct *tty) if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN)) return -EBUSY; - tty->count++; + tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT); + 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; } -- 2.13.6