All of lore.kernel.org
 help / color / mirror / Atom feed
* [BUG] git rev-list --missing=allow-promisor
@ 2022-08-10 21:33 Andrew Olsen
  2022-08-11  8:12 ` Jeff King
  0 siblings, 1 reply; 5+ messages in thread
From: Andrew Olsen @ 2022-08-10 21:33 UTC (permalink / raw)
  To: git

Steps to reproduce:
1. Create a git repository with missing+promised blobs.
For example, I did this:
git clone git@github.com:git/git.git git-no-blobs --filter=blob:none
--depth=1 --bare

--filter=blob:none - don't fetch any blobs
--depth=1 - we don't need git's entire history, just one commit is fine
--bare - this avoids git fetching any blobs as part of the checkout operation

2. In this repository, run:
git rev-list HEAD --objects --missing=allow-promisor

Expected outcome:
git rev-list prints the paths and OIDs of all the objects

Actual outcome:
Any of the following:
a) git rev-list prints the paths and OIDs of all the objects
b) git rev-list fails with "fatal: malformed mode in tree entry"
c) git rev-list fails with "fatal: too-short tree object"

Diagnosis:
With missing=allow-promisor, when git encounters a missing object, it
calls is_promisor on it, which in turn calls add_promisor_object on
lots of things.
In this line in add_promisor_object, the buffer of the tree that we
are currently traversing is freed:
https://github.com/git/git/blob/master/packfile.c#L2242
This clearly causes problems but not in a deterministic way.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [BUG] git rev-list --missing=allow-promisor
  2022-08-10 21:33 [BUG] git rev-list --missing=allow-promisor Andrew Olsen
@ 2022-08-11  8:12 ` Jeff King
  2022-08-14  6:29   ` [PATCH] is_promisor_object(): fix use-after-free of tree buffer Jeff King
  0 siblings, 1 reply; 5+ messages in thread
From: Jeff King @ 2022-08-11  8:12 UTC (permalink / raw)
  To: Andrew Olsen; +Cc: git

On Thu, Aug 11, 2022 at 09:33:54AM +1200, Andrew Olsen wrote:

> Steps to reproduce:
> 1. Create a git repository with missing+promised blobs.
> For example, I did this:
> git clone git@github.com:git/git.git git-no-blobs --filter=blob:none
> --depth=1 --bare
> 
> --filter=blob:none - don't fetch any blobs
> --depth=1 - we don't need git's entire history, just one commit is fine
> --bare - this avoids git fetching any blobs as part of the checkout operation
> 
> 2. In this repository, run:
> git rev-list HEAD --objects --missing=allow-promisor

Thanks for a clear report. I can reproduce this easily.

> Expected outcome:
> git rev-list prints the paths and OIDs of all the objects
> 
> Actual outcome:
> Any of the following:
> a) git rev-list prints the paths and OIDs of all the objects
> b) git rev-list fails with "fatal: malformed mode in tree entry"
> c) git rev-list fails with "fatal: too-short tree object"

Even better is to compile with SANITIZE=address, which reliably shows
that this is indeed a use-after-free.

> Diagnosis:
> With missing=allow-promisor, when git encounters a missing object, it
> calls is_promisor on it, which in turn calls add_promisor_object on
> lots of things.
> In this line in add_promisor_object, the buffer of the tree that we
> are currently traversing is freed:
> https://github.com/git/git/blob/master/packfile.c#L2242

Ugh, yes. This is due to my fcc07e980b (is_promisor_object(): free tree
buffer after parsing, 2021-04-13). In most cases we'll have just
allocated memory for the tree buffer in parse_object(), but occasionally
we'll jump into is_promisor_object() while looking at an existing tree,
and parse_object() will just reuse the buffer cached in the struct.

It would be really nice to have some kind of reference-counting for
these cached buffers, but doing it everywhere may be rather complicated.
An easier hack to fix this area is for it to check ahead of time whether
there's a cached buffer we'll reuse. That only fixes this spot, but this
function is rather unlike most others in that it starts looking at new
random trees in the middle of an existing tree traversal (this would be
possible in a regular tree walk if you could have circular references,
but you can't easily because of sha1).

So something like this makes the bug go away, though it does feel a
little dirty:

---
diff --git a/packfile.c b/packfile.c
index 6b0eb9048e..c3186c1a02 100644
--- a/packfile.c
+++ b/packfile.c
@@ -2217,7 +2217,14 @@ static int add_promisor_object(const struct object_id *oid,
 			       void *set_)
 {
 	struct oidset *set = set_;
-	struct object *obj = parse_object(the_repository, oid);
+	struct object *obj;
+	int save_tree_buffer = 0;
+
+	obj = lookup_object(the_repository, oid);
+	if (obj && obj->type == OBJ_TREE && obj->parsed)
+		save_tree_buffer = 1;
+
+	obj = parse_object(the_repository, oid);
 	if (!obj)
 		return 1;
 
@@ -2239,7 +2246,8 @@ static int add_promisor_object(const struct object_id *oid,
 			return 0;
 		while (tree_entry_gently(&desc, &entry))
 			oidset_insert(set, &entry.oid);
-		free_tree_buffer(tree);
+		if (!save_tree_buffer)
+			free_tree_buffer(tree);
 	} else if (obj->type == OBJ_COMMIT) {
 		struct commit *commit = (struct commit *) obj;
 		struct commit_list *parents = commit->parents;

-Peff

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH] is_promisor_object(): fix use-after-free of tree buffer
  2022-08-11  8:12 ` Jeff King
@ 2022-08-14  6:29   ` Jeff King
  2022-08-15  5:32     ` Junio C Hamano
  0 siblings, 1 reply; 5+ messages in thread
From: Jeff King @ 2022-08-14  6:29 UTC (permalink / raw)
  To: Andrew Olsen; +Cc: Junio C Hamano, Jonathan Tan, git

On Thu, Aug 11, 2022 at 04:12:01AM -0400, Jeff King wrote:

> So something like this makes the bug go away, though it does feel a
> little dirty:

Having slept on this, I actually think it's the right approach, for the
reasons given in the commit message below. And we definitely need to do
_something_ to fix the bug, and preferably something small and un-risky,
since this should probably go to 'maint'. The other obvious option is
reverting the original patch, but I think the memory savings it brought
would be hard to give up, and this patch should reliably fix the bug.

Thanks again for a clear bug report.

-- >8 --
Subject: is_promisor_object(): fix use-after-free of tree buffer

Since commit fcc07e980b (is_promisor_object(): free tree buffer after
parsing, 2021-04-13), we'll always free the buffers attached to a
"struct tree" after searching them for promisor links. But there's an
important case where we don't want to do so: if somebody else is already
using the tree!

This can happen during a "rev-list --missing=allow-promisor" traversal
in a partial clone that is missing one or more trees or blobs. The
backtrace for the free looks like this:

      #1 free_tree_buffer tree.c:147
      #2 add_promisor_object packfile.c:2250
      #3 for_each_object_in_pack packfile.c:2190
      #4 for_each_packed_object packfile.c:2215
      #5 is_promisor_object packfile.c:2272
      #6 finish_object__ma builtin/rev-list.c:245
      #7 finish_object builtin/rev-list.c:261
      #8 show_object builtin/rev-list.c:274
      #9 process_blob list-objects.c:63
      #10 process_tree_contents list-objects.c:145
      #11 process_tree list-objects.c:201
      #12 traverse_trees_and_blobs list-objects.c:344
      [...]

We're in the middle of walking through the entries of a tree object via
process_tree_contents(). We see a blob (or it could even be another tree
entry) that we don't have, so we call is_promisor_object() to check it.
That function loops over all of the objects in the promisor packfile,
including the tree we're currently walking. When we're done with it
there, we free the tree buffer. But as we return to the walk in
process_tree_contents(), it's still holding on to a pointer to that
buffer, via its tree_desc iterator, and it accesses the freed memory.

Even a trivial use of "--missing=allow-promisor" triggers this problem,
as the included test demonstrates (it's just a vanilla --blob:none
clone).

We can detect this case by only freeing the tree buffer if it was
allocated on our behalf. This is a little tricky since that happens
inside parse_object(), and it doesn't tell us whether the object was
already parsed, or whether it allocated the buffer itself. But by
checking for an already-parsed tree beforehand, we can distinguish the
two cases.

That feels a little hacky, and does incur an extra lookup in the
object-hash table. But that cost is fairly minimal compared to actually
loading objects (and since we're iterating the whole pack here, we're
likely to be loading most objects, rather than reusing cached results).

It may also be a good direction for this function in general, as there
are other possible optimizations that rely on doing some analysis before
parsing:

  - we could detect blobs and avoid reading their contents; they can't
    link to other objects, but parse_object() doesn't know that we don't
    care about checking their hashes.

  - we could avoid allocating object structs entirely for most objects
    (since we really only need them in the oidset), which would save
    some memory.

  - promisor commits could use the commit-graph rather than loading the
    object from disk

This commit doesn't do any of those optimizations, but I think it argues
that this direction is reasonable, rather than relying on parse_object()
and trying to teach it to give us more information about whether it
parsed.

The included test fails reliably under SANITIZE=address just when
running "rev-list --missing=allow-promisor". Checking the output isn't
strictly necessary to detect the bug, but it seems like a reasonable
addition given the general lack of coverage for "allow-promisor" in the
test suite.

Reported-by: Andrew Olsen <andrew.olsen@koordinates.com>
Signed-off-by: Jeff King <peff@peff.net>
---
The bug is in v2.32.0. I prepared this directly on top of fcc07e980b,
but it should apply cleanly to any recent maint or master tips.

 packfile.c               | 15 +++++++++++++--
 t/t5616-partial-clone.sh |  7 +++++++
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/packfile.c b/packfile.c
index b79cbc8cd4..1556a5c3f3 100644
--- a/packfile.c
+++ b/packfile.c
@@ -2225,7 +2225,17 @@ static int add_promisor_object(const struct object_id *oid,
 			       void *set_)
 {
 	struct oidset *set = set_;
-	struct object *obj = parse_object(the_repository, oid);
+	struct object *obj;
+	int we_parsed_object;
+
+	obj = lookup_object(the_repository, oid);
+	if (obj && obj->parsed) {
+		we_parsed_object = 0;
+	} else {
+		we_parsed_object = 1;
+		obj = parse_object(the_repository, oid);
+	}
+
 	if (!obj)
 		return 1;
 
@@ -2247,7 +2257,8 @@ static int add_promisor_object(const struct object_id *oid,
 			return 0;
 		while (tree_entry_gently(&desc, &entry))
 			oidset_insert(set, &entry.oid);
-		free_tree_buffer(tree);
+		if (we_parsed_object)
+			free_tree_buffer(tree);
 	} else if (obj->type == OBJ_COMMIT) {
 		struct commit *commit = (struct commit *) obj;
 		struct commit_list *parents = commit->parents;
diff --git a/t/t5616-partial-clone.sh b/t/t5616-partial-clone.sh
index 5cb415386e..9c0e422a14 100755
--- a/t/t5616-partial-clone.sh
+++ b/t/t5616-partial-clone.sh
@@ -49,6 +49,13 @@ test_expect_success 'do partial clone 1' '
 	test "$(git -C pc1 config --local remote.origin.partialclonefilter)" = "blob:none"
 '
 
+test_expect_success 'rev-list --missing=allow-promisor on partial clone' '
+	git -C pc1 rev-list --objects --missing=allow-promisor HEAD >actual &&
+	git -C pc1 rev-list --objects --missing=print HEAD >expect.raw &&
+	grep -v "^?" expect.raw >expect &&
+	test_cmp expect actual
+'
+
 test_expect_success 'verify that .promisor file contains refs fetched' '
 	ls pc1/.git/objects/pack/pack-*.promisor >promisorlist &&
 	test_line_count = 1 promisorlist &&
-- 
2.37.1.926.g40145d0bb9


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] is_promisor_object(): fix use-after-free of tree buffer
  2022-08-14  6:29   ` [PATCH] is_promisor_object(): fix use-after-free of tree buffer Jeff King
@ 2022-08-15  5:32     ` Junio C Hamano
  2022-08-15 22:53       ` Jeff King
  0 siblings, 1 reply; 5+ messages in thread
From: Junio C Hamano @ 2022-08-15  5:32 UTC (permalink / raw)
  To: Jeff King; +Cc: Andrew Olsen, Jonathan Tan, git

Jeff King <peff@peff.net> writes:

> This can happen during a "rev-list --missing=allow-promisor" traversal
> in a partial clone that is missing one or more trees or blobs. The
> backtrace for the free looks like this:
>
>       #1 free_tree_buffer tree.c:147
>       #2 add_promisor_object packfile.c:2250
>       #3 for_each_object_in_pack packfile.c:2190
>       #4 for_each_packed_object packfile.c:2215
>       #5 is_promisor_object packfile.c:2272
>       #6 finish_object__ma builtin/rev-list.c:245
>       #7 finish_object builtin/rev-list.c:261
>       #8 show_object builtin/rev-list.c:274
>       #9 process_blob list-objects.c:63
>       #10 process_tree_contents list-objects.c:145
>       #11 process_tree list-objects.c:201
>       #12 traverse_trees_and_blobs list-objects.c:344
>       [...]
>
> We're in the middle of walking through the entries of a tree object via
> process_tree_contents(). We see a blob (or it could even be another tree
> entry) that we don't have, so we call is_promisor_object() to check it.
> That function loops over all of the objects in the promisor packfile,
> including the tree we're currently walking.

I forgot that the above "loops over" happens only once to populate
the oidset hashtable, and briefly wondered if we are being grossly
inefficient by scanning pack .idx file each time we encounter a
missing object.  "Upon first call, that function loops over
... walking, to prepare a hashtable to answer if any object id is
referred to by an object in promisor packs" would have helped ;-). 

> It may also be a good direction for this function in general, as there
> are other possible optimizations that rely on doing some analysis before
> parsing:
>
>   - we could detect blobs and avoid reading their contents; they can't
>     link to other objects, but parse_object() doesn't know that we don't
>     care about checking their hashes.
>
>   - we could avoid allocating object structs entirely for most objects
>     (since we really only need them in the oidset), which would save
>     some memory.
>
>   - promisor commits could use the commit-graph rather than loading the
>     object from disk
>
> This commit doesn't do any of those optimizations, but I think it argues
> that this direction is reasonable, rather than relying on parse_object()
> and trying to teach it to give us more information about whether it
> parsed.

Yeah, all of the future bits sound sensible. 

Will queue.

Thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] is_promisor_object(): fix use-after-free of tree buffer
  2022-08-15  5:32     ` Junio C Hamano
@ 2022-08-15 22:53       ` Jeff King
  0 siblings, 0 replies; 5+ messages in thread
From: Jeff King @ 2022-08-15 22:53 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Andrew Olsen, Jonathan Tan, git

On Sun, Aug 14, 2022 at 10:32:12PM -0700, Junio C Hamano wrote:

> > We're in the middle of walking through the entries of a tree object via
> > process_tree_contents(). We see a blob (or it could even be another tree
> > entry) that we don't have, so we call is_promisor_object() to check it.
> > That function loops over all of the objects in the promisor packfile,
> > including the tree we're currently walking.
> 
> I forgot that the above "loops over" happens only once to populate
> the oidset hashtable, and briefly wondered if we are being grossly
> inefficient by scanning pack .idx file each time we encounter a
> missing object.  "Upon first call, that function loops over
> ... walking, to prepare a hashtable to answer if any object id is
> referred to by an object in promisor packs" would have helped ;-).

Right. When you have worked in an area, sometimes it is easy to forget
which things are common knowledge and which are not. :) I don't mind at
all if you want to amend the commit message as you apply.

> > It may also be a good direction for this function in general, as there
> > are other possible optimizations that rely on doing some analysis before
> > parsing:
> >
> >   - we could detect blobs and avoid reading their contents; they can't
> >     link to other objects, but parse_object() doesn't know that we don't
> >     care about checking their hashes.
> >
> >   - we could avoid allocating object structs entirely for most objects
> >     (since we really only need them in the oidset), which would save
> >     some memory.
> >
> >   - promisor commits could use the commit-graph rather than loading the
> >     object from disk
> >
> > This commit doesn't do any of those optimizations, but I think it argues
> > that this direction is reasonable, rather than relying on parse_object()
> > and trying to teach it to give us more information about whether it
> > parsed.
> 
> Yeah, all of the future bits sound sensible. 

I very intentionally didn't work on those things yet, because I wanted
to make sure we got a simple fix in as quickly as possible. That said, I
don't have immediate plans for them. They are perhaps not quite small
enough for #leftoverbits, but I think they might also be nice bite-sized
chunks for somebody wanting to get their feet wet in that part of the
code.

-Peff

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-08-16  2:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-10 21:33 [BUG] git rev-list --missing=allow-promisor Andrew Olsen
2022-08-11  8:12 ` Jeff King
2022-08-14  6:29   ` [PATCH] is_promisor_object(): fix use-after-free of tree buffer Jeff King
2022-08-15  5:32     ` Junio C Hamano
2022-08-15 22:53       ` Jeff King

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.