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

Re: [PATCH 0/2] Fix possible buffer overflow in remove_subtree()

From
Jeff King <peff@peff.net>
Date
Mar 13, 2014, 13:44 UTC
Message-ID
<20140313134409.GA22046@sigill.intra.peff.net>
In-Reply-To
<1394702348-6433-1-git-send-email-mhagger@alum.mit.edu>
On Thu, Mar 13, 2014 at 10:19:06AM +0100, Michael Haggerty wrote:
Show 5 quoted lines
> These patches are proposed for maint (but also apply cleanly to
> master).
> 
> I presume that this is exploitable via Git commands, though I haven't
> verified it explicitly [1].
It's possible to overflow this buffer, like:
    git init repo && cd repo &&
    blob=$(git hash-object -w /dev/null) &&
    big=$(perl -e 'print "a" x 250')
    (for i in $(seq 1 17); do mkdir $big && cd $big; done)
    printf "100644 blob $blob\t$big\n" >tree &&
    tree=$(git mktree <tree) &&
    git read-tree $tree &&
    git checkout-index -f --all
but I'm not sure if it is easily exploitable for two reasons:
  1. We never actually return from the function with the smashed stack.
     Immediately after overflowing the buffer, we call lstat(), which
     will fail with ENAMETOOLONG (at least on Linux), causing us to call
     into die_errno and exit.
  2. The overflow relies on us trying to move a very deep hierarchy out
     of the way, but I could not convince git to _create_ such a
     hierarchy in the first place. Here I do it using relative paths and
     recursion, but git generally tries to create paths from the top of
     the repo using full pathnames. So it gets ENAMETOOLONG trying to
     create the paths in the first place.

Of course somebody may be more clever than I am, or perhaps some systems have a PATH_MAX that is not enforced by the kernel. It's still a suspicious bit of code, and I think your patches are a strict improvement. Besides fixing this potential problem, I notice that we currently put 4096 bytes on the stack for a recursive function. Removing "a/a/a..." can put up to 8M on the stack, which might be too much on some systems (besides just being silly and wasteful).

-Peff
Previous: Duy Nguyen
Message 6 of 6 in “Fix possible buffer overflow in remove_subtree()”
  1. 0/2 Fix possible buffer overflow in remove_subtree()Michael Haggerty, Mar 13, 2014
  2. 1/2 checkout_entry(): use the strbuf throughout the functionMichael Haggerty, Mar 13, 2014
  3. Duy NguyenMar 13, 2014
  4. 2/2 entry.c: fix possible buffer overflow in remove_subtree()Michael Haggerty, Mar 13, 2014
  5. Duy NguyenMar 13, 2014
  6. Jeff KingMar 13, 2014

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.