Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp169691imm; Tue, 28 Aug 2018 19:25:24 -0700 (PDT) X-Google-Smtp-Source: ANB0Vda9a3ZiGjz8GUjlp+w1h4p3kwdmK9z/cckSU8+5B+WrLbO9gpjVnHxdD2e+85u5IaAYJCuI X-Received: by 2002:a63:e841:: with SMTP id a1-v6mr3835877pgk.126.1535509524571; Tue, 28 Aug 2018 19:25:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535509524; cv=none; d=google.com; s=arc-20160816; b=Dv41n+U92/9R0v5sCGz6cEmcfRSFQ+XndBBYhpIbDaR6wa0P+omVbrYd30+TkqK9BT PJonFMa6t0NssCjjfR9Wp/h1oD4c/NTLFOEZ+ENi5sKIe5gkvKft5QawD0ckb083T0J4 1EzwzKeHpb0XwO1BakNEkCv2xFrXE1r6uM7GliENTP9/rKYOEJDjmkIyepF+H/eSWKYR WM0i3SKOIm2GwLmWsb9tppgmo2cn/OIxXybd6ZosXnzFuukU0f5z1ftdi4P5S1aVPw7U ZnLrd9rGbMJhSQKIimBOFBjacheu3Y4qeHWOkRdO1eEmpJqDCecqP3d9U2SF5LWYSbR+ p3Iw== 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=1qrQiIIGCJFPVtNRO1+XE4NkipPOjwyNIEa9DuQIVZs=; b=0q7HYU9rYOVq876S85jikyjUoDecZvjJ5BLLY8pU8LXwexfOKGq2FSHJHa96N3FEDR p/+DiYnFyR8vHXbdD4rAGjAH31V/plOp/HCOTOt/6xVyFjGDcm6EZgMsiIFW9H7U3oFM sVU55JBLOBf6SpL4OXg921DsLnNZqywUGGPxLd02n9XVxCOggjQfS9pzGOnnKX88pjyv uUNLFSih8YfYigsAJS5/Kr19vWGH18eMHLMfF5/ZnT5U4J8gjbjocFOflISs6p/X75IG 3HYmk24gnqRAYeHUY/YS9tBy68Z1KOh9i7QdiXYTHnjkQMP3yDrGZTaEVFwS1oPiEz00 Bz7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@arista.com header.s=googlenew header.b=TD16t7tW; 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 v11-v6si2580524pgg.303.2018.08.28.19.25.08; Tue, 28 Aug 2018 19:25:24 -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=TD16t7tW; 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 S1727452AbeH2GSb (ORCPT + 99 others); Wed, 29 Aug 2018 02:18:31 -0400 Received: from mail-ed1-f67.google.com ([209.85.208.67]:46075 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727293AbeH2GSa (ORCPT ); Wed, 29 Aug 2018 02:18:30 -0400 Received: by mail-ed1-f67.google.com with SMTP id p52-v6so2765315eda.12 for ; Tue, 28 Aug 2018 19:23:58 -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=1qrQiIIGCJFPVtNRO1+XE4NkipPOjwyNIEa9DuQIVZs=; b=TD16t7tWaVy7KYtWzQhm4PA8NrPHdwmjavi1QFXdiCRzPMJ++p5UgY6MD+cKX5tTMy R2TB5++NU9eSgkB2kisRMasxYvMhep5JGUrhcT2fFBVBp+FTQTT9F1BzXxFaV/+vZGwM /oq41MPAO3srJLxHfsost8WkGi208aBwURrke8K4UgR8ULS0008m1IyoLXlvfGdfXgS6 ZD/lvhEEEKWVzCKN5nCZbS7u0M1u2RscGpR7tNTQ99lmc2Y+vczXx48FZ7eXbUzbmJ7Z ZvrkymjpWIdi2ClxcqMgqOCzVzlxMx/w9pKWBUOzIrSbke5yl8QuORJ9rf1NUx7lOIVO 1txg== 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=1qrQiIIGCJFPVtNRO1+XE4NkipPOjwyNIEa9DuQIVZs=; b=Zi5MzRnzp8BSf4JBSx9CVQyNalfT/ualAuQlN7WpTRQ4a7BkXTj+yrGZhDXN0fYJhz H5/ZX+Bqu5zasuddw2Wx1djSM2+1Tpvj5M5TDZxg0Kj+/RWwVBbUEfpBzT9ynIggab4i +T7BXQnEIWVH/yWmN9omF+xMjfgY9SnrzYSaaFhnG9mx0xJ4DZMtsrvi+HxPY5eRhYoD fn9gOd4IpyV/x5x7r1U3pnOOo05Io94eqa0KY2wsl1QxUCULyVAFtw94SJtFUFt1LBNp 9zrmfoNacElo9SMBz9UnGEFPTo4oN9vOlx9VdLBJi1Sscg/vT7ce4bhAA1VKlEU9oCvV LQLA== X-Gm-Message-State: APzg51B3EtRki26oqwVW3j3tGAPiBoRc0vIfScpbM7WPnOrjIVN30elS jlT8RH2kUui8ZJ27r1BUL546vWfxV08= X-Received: by 2002:a50:b8a6:: with SMTP id l35-v6mr5238018ede.273.1535509437525; Tue, 28 Aug 2018 19:23:57 -0700 (PDT) Received: from dhcp.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id f26-v6sm1236367edb.28.2018.08.28.19.23.56 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 28 Aug 2018 19:23:56 -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 , =?UTF-8?q?Pasi=20K=C3=A4rkk=C3=A4inen?= , Peter Hurley , Sergey Senozhatsky , Tan Xiaojun , Greg Kroah-Hartman , Jiri Slaby , stable@vger.kernel.org Subject: [PATCH 2/4] tty: Hold tty_ldisc_lock() during tty_reopen() Date: Wed, 29 Aug 2018 03:23:51 +0100 Message-Id: <20180829022353.23568-3-dima@arista.com> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20180829022353.23568-1-dima@arista.com> References: <20180829022353.23568-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 I think, tty_ldisc_reinit() should be called with ldisc_sem hold for writing, which will protect any reader against line discipline changes. Note: I failed to reproduce the described crash, so obiviously can't guarantee that this is the place where line discipline was switched. Cc: Greg Kroah-Hartman Cc: Jiri Slaby Cc: stable@vger.kernel.org Signed-off-by: Dmitry Safonov --- 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 5e5da9acaf0a..3ef8b977b167 100644 --- a/drivers/tty/tty_io.c +++ b/drivers/tty/tty_io.c @@ -1267,15 +1267,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; } -- 2.13.6