Cygwin: stat(): Fix "Bad address" error on stat() for /dev/tty.

As reported in
https://cygwin.com/pipermail/cygwin/2023-June/253888.html,
"Bad address" error occurs when stat() is called after the commit
3721a756b0 ("Cygwin: console: Make the console accessible from
other terminals.").

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.

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().

Fixes: 3721a756b0 ("Cygwin: console: Make the console accessible
  from other terminals.").
Fixes: 23771fa1f7 ("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 <bmj001@gmail.com>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
This commit is contained in:
Takashi Yano 2023-07-07 06:11:52 +09:00
parent e38f91d5a9
commit 3edb55af82
2 changed files with 13 additions and 1 deletions

View File

@ -600,7 +600,13 @@ fh_alloc (path_conv& pc)
case FH_TTY:
if (!pc.isopen ())
{
fhraw = cnew_no_ctor (fhandler_console, -1);
if (CTTY_IS_VALID (myself->ctty))
{
if (iscons_dev (myself->ctty))
fhraw = cnew_no_ctor (fhandler_console, -1);
else
fhraw = 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

View File

@ -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 = S_IFCHR | S_IRUSR | S_IWUSR;
pinfo p (get_ttyp ()->getsid ());