Segmentation fault due to double free for archetype.
Takashi Yano
takashi.yano@nifty.ne.jp
Mon Jan 17 12:48:01 GMT 2022
On Mon, 17 Jan 2022 13:11:46 +0100
Corinna Vinschen wrote:
> On Jan 17 20:41, Takashi Yano wrote:
> > On Mon, 17 Jan 2022 12:01:51 +0100
> > Corinna Vinschen wrote:
> > > On Jan 15 19:20, Takashi Yano wrote:
> > > > I also found the following patch fixes the issue. Is this the
> > > > right thing?
> > > >
> > > > diff --git a/winsup/cygwin/fhandler.cc b/winsup/cygwin/fhandler.cc
> > > > index fc7c0422e..e51208117 100644
> > > > --- a/winsup/cygwin/fhandler.cc
> > > > +++ b/winsup/cygwin/fhandler.cc
> > > > @@ -441,7 +441,7 @@ fhandler_base::open_with_arch (int flags, mode_t mode)
> > > > || open (flags, mode & 07777)))
> > > > {
> > > > if (archetype)
> > > > - delete archetype;
> > > > + cygheap->fdtab.delete_archetype (archetype);
> > > > }
> > > > else if (archetype)
> > > > {
> > >
> > > Good catch! I think this is basically ok, but you have to check the
> > > usecount, i. e.
> > >
> > > if (archetype && archetype_usecount (-1) == 0)
> > > cygheap->fdtab.delete_archetype (archetype);
> > >
> > > Does that sound right?
> >
> > Where is archetype->usecount is incremented?
>
> In fhandler_base::open_with_arch.
>
> > It seems that
> > archetype->usecount is zero here. archetype->usecount is not
> > incremented around line 672 in dtable.cc when archetype is
> > created by fh->clone().
> >
> > else
> > {
> > if (!fh->get_name ())
> > fh->set_name (fh->dev ().native ());
> > fh->archetype = fh->clone ();
> > debug_printf ("created an archetype (%p) for %s(%d/%d)", fh->archetype, fh->get_name (), fh->dev ().get_major (), fh->dev ().get_minor ());
> > fh->archetype->archetype = NULL;
> > *cygheap->fdtab.add_archetype () = fh->archetype;
> > }
>
> Right, but if open isn't called, because you already have an archetype
> at this point:
>
> if (!(res = (archetype && archetype->io_handle)
> || open (flags, mode & 07777)))
>
> Then the archetype is one already created by a former open_with_arch
> call and then you delete an archetype which is still in use, no?
archetype->usecount is not incremented yet here.
archetype->usecount is incremented only when this 'if' clause
is not true. So, isn't the following code right?
if (!(res = (archetype && archetype->io_handle)
|| open (flags, mode & 07777)))
{
if (archetype && archetype->usecount == 0)
cygheap->fdtab.delete_archetype (archetype);
}
--
Takashi Yano <takashi.yano@nifty.ne.jp>
More information about the Cygwin
mailing list