From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796049435; b=x9fQncDkInD/6FzBHtfDPBFsA7/EWXLti0K1xqmssthSd4wVfeQrIZroFYVQ7clCoPOf tdgcBP3/WRwOPQlE8AYjaFuJSPlTZ1NZP5yjUkS9ooGXLKwhYRcHDaBy+FjovuYdWjJ6N damCDCVZvLO7+eY+MucPTX/9wJ5LfFJs1rilAqrwnz1Jbus7/kUMyWqRGP9llfU9sscoB gwffT1usl5a03BNm4o+zOVpbGqjLz9mSrm0qy9o/ORJUMLnLkdodGl+EKr8DvXzrLCLm+ kOIubUe2frmDqQgIN3wn8zh/1GDRb8M0338ORXIStm+l4M0OdhZv+so9ltBDmA1FEQA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796049435; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=pwR/FLsqek0dgBrwnTNA0SmdHBhIlbfIA5akQZY1z88=; b=GzG0VuybpRSR2qAMFwjpI+sPKDywWYP4kWRw/Y5p3YoieQOQocLnL2n3lsoLDYqckYom bU7+yq+NDBAuZEtML/tlfaeJcnQtRJud9UKSyASnUulQEbS/0B7PgKIS6UnhTafcjSirx GhFb7LNNCb7T7Of86nfeuyDXcxx9qIoaFswthfEegJmb/bf+muqVxf9ziP6pHUlDpnGPv Xmj3cvxu2HbN9veAPHus/A1v/SZjACO3wXk8Anet05KUIQStA26JWvafYtsxvDRjd4Ygy WWlXGAyw5YiO+3zMv4jWLubsO0UNbYgONIq/CyfCgSv+uh48hY5yO8DWbjFJftivvVw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=TMotYBAx; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 27303606A339 for ; Mon, 30 Nov 2026 14:37:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=TMotYBAx Received: by smtp.kernel.org (Postfix) with ESMTPSA id B2C8B1F00896; Mon, 30 Nov 2026 14:37:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796049434; bh=pwR/FLsqek0dgBrwnTNA0SmdHBhIlbfIA5akQZY1z88=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=TMotYBAxneg7d8YB3k5H/sf248YwolLul9SZ/TBhqQHauqKuqBv+O3WQrtBa9WM5p mmy4t/UcTSEVYsljXu2atF/MbSS/A4DOzqTHrwGKXES+riPHhVR4Zq48c4cKJt/qHF gKKB4L0CpoN5iBFcUCTFENZGn2nfpkwi6kG4upjxIG6voXNkDSh0mDO+i3vJqwnCw8 ZRwRfbDIQMlfflc6wdc2TsbJ7oJmeRbTpFyhJCMo7FvuzSqOxSquhmXJIJSE4of6G4 rG8Ed81xipN5rnSAGs4UIant5W71/mliyylkuu4/U8m8cdv4Ub4UQXxH9+BWk8uAHM GCgprGmmo1rEA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH] tty: port: fix hangup of a reopened tty Reply-To: sashiko-reviews@lists.linux.dev To: "YuanZhe Liu" <25031212351@stu.xidian.edu.cn> Cc: linux-serial@vger.kernel.org In-Reply-To: <20261130141413.10581-1-25031212351@stu.xidian.edu.cn> References: <20261130141413.10581-1-25031212351@stu.xidian.edu.cn> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Mon, 30 Nov 2026 14:37:14 +0000 X-Mailing-List: linux-serial@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] tty: port: incomplete hangup and lock-drop race in tty_port_tty_vhangup() - [Medium] tty: port: tty_port_tty_vhangup() breaks cons_filp port->count sync -- commit 2ab4c03c8c59442199d9ab6aed18dfe24377d5b6 Author: YuanZhe Liu <25031212351@stu.xidian.edu.cn> tty: port: fix hangup of a reopened tty This commit calls tty_port_hangup() from tty_port_tty_vhangup() if the port is still initialized after tty_vhangup() returns, covering the case where a concurrent reopen reactivates the port while TTY_HUPPED is still set. > diff --git a/drivers/tty/tty_port.c b/drivers/tty/tty_port.c > index 4ca4796d13097..756a43e0b323d 100644 > --- a/drivers/tty/tty_port.c > +++ b/drivers/tty/tty_port.c > @@ -438,10 +438,23 @@ void tty_port_tty_vhangup(struct tty_port *port) > tty =3D tty_port_tty_get(port); > mutex_unlock(&port->mutex); > =20 > - if (tty) { > - tty_vhangup(tty); > - tty_kref_put(tty); > - } > + if (!tty) > + return; > + > + tty_vhangup(tty); > + > + /* > + * tty_vhangup() does nothing if the tty has already been hung up. > + * A concurrent reopen may however have reactivated the port while > + * TTY_HUPPED was still set (it is only cleared once the open has > + * completed), so make sure the port is shut down here as well. > + */ > + tty_lock(tty); [Severity: High] Does calling tty_port_hangup() after tty_vhangup() returns early on TTY_HUPPED leave the reopening filp and line discipline un-hung-up, and open a race window when tty_lock() is dropped between the two calls? When a hung-up tty is reopened, tty_reopen() reinitializes tty->ldisc, tty_add_file() attaches the new filp with filp->f_op =3D &tty_fops, and tty_port_open() sets TTY_PORT_INITIALIZED before tty_port_block_til_ready() drops tty_lock(tty) to sleep (before tty_open() clears TTY_HUPPED). If tty_port_tty_vhangup() runs at that point, __tty_hangup() sees TTY_HUPPED, drops tty_lock(tty), and returns without setting filp->f_op =3D &hung_up_tty_fops or calling tty_ldisc_hangup(): CPU0 CPU1 tty_open() tty_reopen() tty_ldisc_reinit() tty_add_file() tty_port_open() /* sets TTY_PORT_INITIALIZED */ tty_port_block_til_ready() tty_unlock(tty) tty_port_tty_vhangup() tty_vhangup() __tty_hangup() tty_lock(tty) /* TTY_HUPPED is set */ tty_unlock(tty) /* carrier raised / wakes up */ tty_lock(tty) clear_bit(TTY_HUPPED, &tty->flags) tty_unlock(tty) /* returns live fd to userspace */ tty_lock(tty) tty_port_hangup(port) tty_unlock(tty) /* driver frees port data */ tty_ioctl() / tty_write() /* calls driver on freed port data */ <-- use-after-free Even if the opener does not win the lock-drop race, skipping __tty_hangup() leaves tty_hung_up_p(filp) false while tty_port_hangup() resets port->count to 0. tty_port_block_til_ready() then runs if (!tty_hung_up_p(filp)) port->count++; and corrupts port->count (or tty_port_close_start() underflows --port->count to -1 with a warning). On console ports where port->console is true during tty_port_tty_vhangup(), tty_port_shutdown_locked() returns early without clearing TTY_PORT_INITIALIZED, so tty_port_block_til_ready() sees both tty_hung_up_p(filp) and !tty_port_initialized(port) false and fails to abort the open. In addition, the reinitialized tty->ldisc is not hung up, and other hangup paths (such as tty_hangup() and tty_port_tty_hangup()) still drop hangups during a reopen. Would clearing TTY_HUPPED in tty_reopen() (before dropping tty_lock(tty)) allow __tty_hangup() to perform a complete hangup under a single tty_lock(tty) hold? > + if (tty_port_initialized(port)) > + tty_port_hangup(port); > + tty_unlock(tty); [Severity: Medium] Can this unconditional tty_port_hangup() desynchronize port->count when a redirected console file (such as /dev/console) is open on the tty? In __tty_hangup(), when a file on tty->tty_files has write_iter =3D=3D redirected_tty_write (cons_filp), it leaves cons_filp un-hung-up and calls tty->ops->close() for the non-console files instead of tty->ops->hangup() so that tty->count and port->count stay in sync: drivers/tty/tty_io.c:__tty_hangup() { ... if (cons_filp) { if (tty->ops->close) for (n =3D 0; n < closecount; n++) tty->ops->close(tty, cons_filp); } else if (tty->ops->hangup) tty->ops->hangup(tty); ... } Because TTY_PORT_INITIALIZED remains set after tty_vhangup() in that case, tty_port_tty_vhangup() will now call tty_port_hangup(port) and reset port->count to 0 while cons_filp is still open and not marked hung up, triggering the bad port count warning in tty_port_close_start() when cons_filp is later closed. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261130141413.1058= 1-1-25031212351@stu.xidian.edu.cn?part=3D1