From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7868) id 764C23858D35; Mon, 10 Jul 2023 08:37:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 764C23858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1688978267; bh=yVkvBaZVhfxBe6nRFifrqy9vjzc2O9Atukd6JKLi7XE=; h=From:To:Subject:Date:From; b=xvCK6cWllDFOIS4WCJBeJrmkPkvnuX/KH2VGwN8XpVAN+F7fzUpf5OIwa+lEg1ISq /32G48wCzCzBNqCed01scUZ5pX/m0Loj2L9GzxuSrZXtAlKWICBZbtX79edN5+7LW3 GYoHHP2jwVxWrcP879aBVe/bsB7aWT2BJRzEaisg= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Takashi Yano To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: stat(): Fix "Bad address" error on stat() for /dev/tty. X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano X-Git-Refname: refs/heads/master X-Git-Oldrev: e38f91d5a96c4554c69c833243e5afec8e3e90eb X-Git-Newrev: 3edb55af820025013b87fdb53249676067a8b8ee Message-Id: <20230710083747.764C23858D35@sourceware.org> Date: Mon, 10 Jul 2023 08:37:47 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D3edb55af820= 025013b87fdb53249676067a8b8ee commit 3edb55af820025013b87fdb53249676067a8b8ee Author: Takashi Yano Date: Fri Jul 7 06:11:52 2023 +0900 Cygwin: stat(): Fix "Bad address" error on stat() for /dev/tty. =20 As reported in https://cygwin.com/pipermail/cygwin/2023-June/253888.html, "Bad address" error occurs when stat() is called after the commit 3721a756b0d8 ("Cygwin: console: Make the console accessible from other terminals."). =20 There are two problems in the current code. One is fhandler_console:: fstat() calls get_ttyp()->getsid(). However, fh_alloc() in dtable.cc omits to initialize the fhandler_console instance when stat() is called. Due to this, get_ttyp() returns NULL and access violation occurs. The other problem is fh_alloc() assigns fhandler_console even if the CTTY is not a console. So the first problem above occurs even if the CTTY is a pty. =20 This patch fixes the issue by: 1) Call set_unit() to initialize _tc if the get_ttyp() returns NULL. 2) Assign fhandler_pty_slave for /dev/tty if CTTY is a pty in fh_alloc(= ). =20 Fixes: 3721a756b0d8 ("Cygwin: console: Make the console accessible from other terminals."). Fixes: 23771fa1f7028 ("dtable.cc (fh_alloc): Make different decisions when generating fhandler for not-opened devices. Add kludge to deal with opening /dev/tty.") Reported-by: Bruce Jerrick Reviewed-by: Corinna Vinschen Signed-off-by: Takashi Yano Diff: --- winsup/cygwin/dtable.cc | 8 +++++++- winsup/cygwin/fhandler/console.cc | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/winsup/cygwin/dtable.cc b/winsup/cygwin/dtable.cc index 18e0f3097..2aae2fd65 100644 --- a/winsup/cygwin/dtable.cc +++ b/winsup/cygwin/dtable.cc @@ -600,7 +600,13 @@ fh_alloc (path_conv& pc) case FH_TTY: if (!pc.isopen ()) { - fhraw =3D cnew_no_ctor (fhandler_console, -1); + if (CTTY_IS_VALID (myself->ctty)) + { + if (iscons_dev (myself->ctty)) + fhraw =3D cnew_no_ctor (fhandler_console, -1); + else + fhraw =3D cnew_no_ctor (fhandler_pty_slave, -1); + } debug_printf ("not called from open for /dev/tty"); } else if (!CTTY_IS_VALID (myself->ctty) && last_tty_dev diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/con= sole.cc index 7768a9941..6aa3b50bf 100644 --- a/winsup/cygwin/fhandler/console.cc +++ b/winsup/cygwin/fhandler/console.cc @@ -4554,6 +4554,12 @@ fhandler_console::set_disable_master_thread (bool x,= fhandler_console *cons) int fhandler_console::fstat (struct stat *st) { + /* When stat() is called, fh_alloc() in dtable.cc omits to initialize + the console instance. Due to this, get_ttyp() returns NULL here. + So, calling set_unit() is necessary to access getsid(). */ + if (!get_ttyp ()) + set_unit (); + fhandler_base::fstat (st); st->st_mode =3D S_IFCHR | S_IRUSR | S_IWUSR; pinfo p (get_ttyp ()->getsid ());