From: Jeff King <peff@peff.net>
To: Elijah Newren <newren@gmail.com>
Cc: Mike Hommey <mh@glandium.org>,
Git Mailing List <git@vger.kernel.org>,
Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH 1/2] fast-import: duplicate parsed encoding string
Date: Mon, 26 Aug 2019 14:44:44 -0400 [thread overview]
Message-ID: <20190826184444.GD23399@sigill.intra.peff.net> (raw)
In-Reply-To: <CABPp-BG3ScDk6_QcjyouqnHrXO2A1mWPEcPh5FnS1popKFzr-Q@mail.gmail.com>
On Mon, Aug 26, 2019 at 11:28:58AM -0700, Elijah Newren wrote:
> On Sun, Aug 25, 2019 at 1:08 AM Jeff King <peff@peff.net> wrote:
> >
> > We read each line of the fast-import stream into the command_buf strbuf.
> > When reading a commit, we parse a line like "encoding foo" by storing a
> > pointer to "foo", but not making a copy. We may then read an unbounded
> > number of other lines (e.g., one for each modified file in the commit),
> > each of which writes into command_buf.
> >
> > This works out in practice for small cases, because we hand off
> > ownership of the heap buffer from command_buf to the cmd_hist array, and
> > read new commands into a fresh heap buffer. And thus the pointer to
> > "foo" remains valid as long as there aren't so many intermediate lines
> > that we end up dropping the original "encoding" line from the history.
> >
> > But as the test modification shows, if we go over our default of 100
> > lines, we end up with our encoding string pointing into freed heap
> > memory. This seems to fail reliably by writing garbage into the output,
> > but running under ASan definitely detects this as a user-after-free.
>
> s/user-after-free/use-after-free/
Wow. I self-corrected "user-after-free" at least three other times while
writing this thread. I clearly have a problem. :)
> > We can fix it by duplicating the encoding value, just as we do for other
> > parsed lines (e.g., an author line ends up in parse_ident, which copies
> > it to a new string).
>
> Eek! Thanks for fixing this up for me; patch looks good.
No problem. On the plus side, finding your bug made me think much harder
about the implications of patch 2 (because it's quite subtle and an easy
mistake to make).
-Peff
next prev parent reply other threads:[~2019-08-26 18:44 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-08-25 4:13 [PATCH] fast-import: Reinitialize command_buf rather than detach it Mike Hommey
2019-08-25 6:57 ` Jeff King
2019-08-25 7:20 ` Mike Hommey
2019-08-25 7:28 ` Jeff King
2019-08-25 8:06 ` [PATCH 0/2] fast-import input string handling bugs Jeff King
2019-08-25 8:08 ` [PATCH 1/2] fast-import: duplicate parsed encoding string Jeff King
2019-08-26 18:28 ` Elijah Newren
2019-08-26 18:44 ` Jeff King [this message]
2019-08-25 8:10 ` [PATCH 2/2] fast-import: duplicate into history rather than passing ownership Jeff King
2019-08-25 10:02 ` Mike Hommey
2019-08-25 14:21 ` René Scharfe
2019-08-26 18:42 ` Jeff King
2019-08-26 15:36 ` [PATCH 0/2] fast-import input string handling bugs Junio C Hamano
2019-08-26 19:18 ` Elijah Newren
2019-08-25 12:35 ` [PATCH] fast-import: Reinitialize command_buf rather than detach it René Scharfe
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190826184444.GD23399@sigill.intra.peff.net \
--to=peff@peff.net \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=mh@glandium.org \
--cc=newren@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).