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

Ownership handling of the "remote name" buffer given to `remote_get` is inconsistent

From
Mike Hommey <mh@glandium.org>
Date
Jun 19, 2024, 19:53 UTC
Message-ID
<20240619195311.yhmbv5ruixa433rn@glandium.org>
Hi,

I actually found this a few years ago, but didn't worry too much about it, forgot about it, then rediscovered it yesterday. I tried to untangle the situation, but it looks like it would take me more time than I can allocate, so I'm sending this report.

When you call `remote_get(name)`, you may end up giving ownership of `name` to the remote, such that if you `free(name)`, using the remote after that may lead to use-after-free. Of course, this is a use-libgit-as-a-library situation; I don't think it happens in actual git code.

Here's how it goes: if your `name` is actually a URL, you end up in this case in remotes_remote_get_1:

        if (name_given && !valid_remote(ret))
                add_url_alias(remote_state, ret, name);

`add_url_alias` will eventually set `ret->url` to contain `name` as-is, taking ownership. Later `release_clear` will free it, which will lead to double-free if the caller frees it.

It's further complicated by `add_url_alias` also sometimes *not* taking ownership, in the face of URL rewrites (in `alias_url`). Meaning, the fix is not as straightforward as changing the above to

                add_url_alias(remote_state, ret, xstrdup(name));
This is as far as I went before my time was up on this issue.
Mike
Next: Jeff King
Message 1 of 3 in “Ownership handling of the "remote name" buffer given to `remote_get` is inconsistent”
  1. Mike HommeyJun 19, 2024
  2. Jeff KingJun 19, 2024
  3. Mike HommeyJun 19, 2024

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.