Skip to contentSkip to conversation

Segmentation fault on non-commit objects.

PATCH In review Davide Berardi 4 participants 8 messages Oct 29, 2019 – Nov 1, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Segmentation fault on non-commit objects.

Fixed segmentation fault that can be triggered using

$ git clone --branch $object $repository
with object pointing to a non-commit (e.g. a blob).

---
builtin/clone.c | 3 +++

1 file changed, 3 insertions(+)
builtin/clone.c +0 -0
index f665b28ccc..6ad2d8fe77 100644
@@ -720,6 +720,9 @@ static void update_head(const struct ref *our, const struct ref *remote,
	} else if (our) {
		struct commit *c = lookup_commit_reference(the_repository,
							   &our->old_oid);
+		/* Check if --branch specifies a non-commit. */
+		if (c == NULL)
+			die(_("unable to update HEAD (cannot find commit)"));
		/* --branch specifies a non-branch (i.e. tags), detach HEAD */
		update_ref(msg, "HEAD", &c->object.oid, NULL, REF_NO_DEREF,
			   UPDATE_REFS_DIE_ON_ERR);
Signature · 1 line
2.23.0
#1 lore raw

Hi Davide,

[please remove the trailing period character in the commit subject]

Quoted 14 lines · Davide Berardi

Fixed segmentation fault that can be triggered using

$ git clone --branch $object $repository
with object pointing to a non-commit (e.g. a blob).

---
builtin/clone.c | 3 +++

1 file changed, 3 insertions(+)
builtin/clone.c +0 -0
index f665b28ccc..6ad2d8fe77 100644

builtin/clone.c static void update_head(const struct ref *our, const · line 726

Quoted 7 lines
builtin/clone.c +3 -0
@@ -720,6 +720,9 @@ static void update_head(const struct ref *our, const
 struct ref *remote,
 	} else if (our) {
 		struct commit *c = lookup_commit_reference(the_repository,
 							   &our->old_oid);
+		/* Check if --branch specifies a non-commit. */
+		if (c == NULL)
+			die(_("unable to update HEAD (cannot find commit)"));

Could the error message maybe repeat whatever the user specified for $object? That would probably be more helpful. Maybe even say "not a commit"?

Ciao,
Johannes

Quoted 8 lines
builtin/clone.c +0 -0
 		/* --branch specifies a non-branch (i.e. tags), detach HEAD */
 		update_ref(msg, "HEAD", &c->object.oid, NULL, REF_NO_DEREF,
			   UPDATE_REFS_DIE_ON_ERR);

--
2.23.0

#3 lore raw
In reply to Davide Berardi
Quoted 3 lines · Davide Berardi

Fixed segmentation fault that can be triggered using

$ git clone --branch $object $repository
with object pointing to a non-commit (e.g. a blob).

One subtle thing here is that $object still needs to be at the tip of a ref (an easy real-world case is "-b junio-gpg-pub" against git.git).

It might be nice to cover this with a test.

builtin/clone.c static void update_head(const struct ref *our, const struct ref *remote, · line 725

Quoted 11 lines
builtin/clone.c +3 -0
index f665b28ccc..6ad2d8fe77 100644
@@ -720,6 +720,9 @@ static void update_head(const struct ref *our, const struct ref *remote,
 	} else if (our) {
 		struct commit *c = lookup_commit_reference(the_repository,
 							   &our->old_oid);
+		/* Check if --branch specifies a non-commit. */
+		if (c == NULL)
+			die(_("unable to update HEAD (cannot find commit)"));

This is definitely a strict improvement over the current behavior (though I agree with Dscho's comments on the error message). A few further thoughts:

- we'll have successfully completed the rest of the clone at this point. Should we leave the objects and refs in place to allow the user to fix it up, as we do when "git checkout" fails?

We'd have to leave _something_ in HEAD for it to be a valid repo. I guess just "refs/heads/master" would be fine, or perhaps we could fall back to whatever the other side had in their HEAD (i.e., pretending that "-b" wasn't specified).

- there's a related case just above the lines you touched: what happens if the other side feeds us a non-commit in their refs/heads/? That shouldn't happen (i.e., their repo is broken), but should we be protecting ourselves on the receiving side more?

Likewise in "else" below just above your lines.

I think in either case we wouldn't segfault (because we don't try to dereference to a commit ourselves), but we'd produce a bogus on-disk state.

-Peff

#4 lore raw
Quoted 4 lines · Jeff King

This is definitely a strict improvement over the current behavior (though I agree with Dscho's comments on the error message). A few further thoughts:

Quoted 8 lines

- we'll have successfully completed the rest of the clone at this point. Should we leave the objects and refs in place to allow the user to fix it up, as we do when "git checkout" fails?

We'd have to leave _something_ in HEAD for it to be a valid repo. I guess just "refs/heads/master" would be fine, or perhaps we could fall back to whatever the other side had in their HEAD (i.e., pretending that "-b" wasn't specified).

Do we know for sure that the object at HEAD on the other side is a commit, or do we need to prepare for a case where it is not? I suspect it is the latter. HEAD needs to exist and point at a ref that is in refs/heads/ hierarchy, and the ref can even be unborn, so falling back on 'master' sounds like a good position.

Quoted 6 lines

- there's a related case just above the lines you touched: what happens if the other side feeds us a non-commit in their refs/heads/? That shouldn't happen (i.e., their repo is broken), but should we be protecting ourselves on the receiving side more?

Likewise in "else" below just above your lines.

Both are good points.

Thanks, all.

#5 lore raw
Quoted 15 lines · Junio C Hamano
Quoted 4 lines · Jeff King

This is definitely a strict improvement over the current behavior (though I agree with Dscho's comments on the error message). A few further thoughts:

Quoted 8 lines

- we'll have successfully completed the rest of the clone at this point. Should we leave the objects and refs in place to allow the user to fix it up, as we do when "git checkout" fails?

We'd have to leave _something_ in HEAD for it to be a valid repo. I guess just "refs/heads/master" would be fine, or perhaps we could fall back to whatever the other side had in their HEAD (i.e., pretending that "-b" wasn't specified).

Quoted 5 lines

Do we know for sure that the object at HEAD on the other side is a commit, or do we need to prepare for a case where it is not? I suspect it is the latter. HEAD needs to exist and point at a ref that is in refs/heads/ hierarchy, and the ref can even be unborn, so falling back on 'master' sounds like a good position.

Yeah, I don't think that we do. This is the same as the case I mentioned later, and it should be handled in all three arms of the conditional.

Davide, do you have an interest in trying to make these code paths a bit more robust?

-Peff

#6 lore raw
Quoted 3 lines

Davide, do you have an interest in trying to make these code paths a bit more robust?

Absolutely, I am writing a patch taking in consideration your comments. I only need to write a test routine then I will commit my new patch.

Quoted 1 line

-Peff

Thanks and sorry for the silence but I am learning from your comments. :)
D.