git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [BUG] rebase: can write reflog with uninit. `action` string

From
Jeff King <peff@peff.net>
Date
May 1, 2025, 15:36 UTC
Message-ID
<20250501153638.GA1793056@coredump.intra.peff.net>
In-Reply-To
<e4ea1e44-af7e-4c24-b0ee-f87091200b90@gmail.com>
On Thu, May 01, 2025 at 03:36:13PM +0100, phillip.wood123@gmail.com wrote:
Show 13 quoted lines
> > But I think the way reflog_message() returns the "buf" member of a
> > static strbuf is kind of an anti-pattern, exactly because you can get
> > this kind of subtle re-use. It probably should just return a non-const
> > pointer, handing over memory ownership to the caller. That would require
> > adjusting its other callers, too.
> 
> Getting rid of the static buffer would certainly protect us from the
> use-after-free. The bug here is that we're not calling reflog_message() and
> storing the result in ctx->reflog_message() to create the correct message in
> do_merge(). Looking at your patch, having to remember to copy the string
> returned from reflog_message() is a bit of a pain. I wonder if we could
> change ctx->reflog_message to be an strbuf and update reflog_message() like
> so

Yeah, I agree that would solve the use-after-free. I just wasn't sure if there would still be a logic bug, though. If we have a sequence like:

  /* somebody sets the variable */
  ctx->reflog_message = reflog_message(...);
  ...
  /* some caller, possibly far away, uses the function again */
  foo(reflog_message(...));
  ...
  /* now the original caller wants to use what it stored */
  printf("%s", ctx->reflog_message);

Right now that is a potential use-after-free because of reallocating the static storage inside reflog_message(). If we teach reflog_message to store things in ctx->reflog_message always, that use-after-free goes away, but will the printf() above print the wrong thing?

I wasn't clear on who is setting the value or why. It could be that doing so is actually _fixing_ a bug (because it should be printing what comes from the invocation in the foo() call).

> I'll try and put a proper patch together next week that removes the static
> buffer and starts calling reflog_message() when we're merging.
Great, thanks.
-Peff
Previous: phillip.wood123@gmail.comNext: Junio C Hamano
Message 8 of 12 in “[BUG] rebase: can write reflog with uninit. `action` string”
  1. Kristoffer HaugsbakkApr 28, 2025
  2. Phillip WoodApr 29, 2025
  3. Kristoffer HaugsbakkApr 29, 2025
  4. Jeff KingApr 29, 2025
  5. Kristoffer HaugsbakkApr 30, 2025
  6. Jeff KingMay 1, 2025
  7. phillip.wood123@gmail.comMay 1, 2025
  8. Jeff KingMay 1, 2025
  9. Junio C HamanoMay 1, 2025
  10. Jeff KingMay 1, 2025
  11. Junio C HamanoMay 1, 2025
  12. phillip.wood123@gmail.comMay 1, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.