Skip to contentSkip to conversation

fsck: avoid looking at NULL blob->object

PATCH In review Jeff King 5 participants 17 messages Jun 9, 2018 – Jun 12, 2018

lore
Share & tools
lore

Jeff King

author
#0 lore raw

PATCH 1/1 fsck: avoid looking at NULL blob->object

Commit 159e7b080b (fsck: detect gitmodules files, 2018-05-02) taught fsck to look at the content of .gitmodules files. If the object turns out not to be a blob at all, we just complain and punt on checking the content. And since this was such an obvious and trivial code path, I didn't even bother to add a test.

Except it _does_ do one non-trivial thing, which is call the report() function, which wants us to pass a pointer to a "struct object". Which we don't have (we have only a "struct object_id"). So we erroneously passed the NULL object, which ends up segfaulting.

It seems like we could refactor report() to just take the object_id itself. But we pass the object pointer along to a callback function, and indeed this ends up in builtin/fsck.c's objreport() which does want to look at other parts of the object (like the type).

So instead, let's just use lookup_unknown_object() to get the real "struct object", and pass that.

---
The problem is in v2.17.1, and of course the v2.18 release candidates.

 fsck.c                     |  3 ++-
 t/t7415-submodule-names.sh | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)
fsck.c +2 -1
index bcae2c30e6..48e7e36869 100644
@@ -1036,7 +1036,8 @@ int fsck_finish(struct fsck_options *options)
 
 		blob = lookup_blob(oid);
 		if (!blob) {
-			ret |= report(options, &blob->object,
+			struct object *obj = lookup_unknown_object(oid->hash);
+			ret |= report(options, obj,
 				      FSCK_MSG_GITMODULES_BLOB,
 				      "non-blob found at .gitmodules");
 			continue;
t/t7415-submodule-names.sh +18 -0
index a770d92a55..63c767bf99 100755
@@ -151,4 +151,22 @@ test_expect_success 'fsck detects symlinked .gitmodules file' '
 	)
 '
 
+test_expect_success 'fsck detects non-blob .gitmodules' '
+	git init non-blob &&
+	(
+		cd non-blob &&
+
+		# As above, make the funny directly to avoid index restrictions.
+		mkdir subdir &&
+		cp ../.gitmodules subdir/file &&
+		git add subdir/file &&
+		git commit -m ok &&
+		tree=$(git ls-tree HEAD | sed s/subdir/.gitmodules/ | git mktree) &&
+		commit=$(git commit-tree $tree) &&
+
+		test_must_fail git fsck 2>output &&
+		grep gitmodulesBlob output
+	)
+'
+
 test_done
Signature · 1 line
2.18.0.rc1.446.g4486251e51
#1 lore raw
Quoted 11 lines · Jeff King

Commit 159e7b080b (fsck: detect gitmodules files, 2018-05-02) taught fsck to look at the content of .gitmodules files. If the object turns out not to be a blob at all, we just complain and punt on checking the content. And since this was such an obvious and trivial code path, I didn't even bother to add a test.

---

t/t7415-submodule-names.sh test_expect_success 'fsck detects symlinked .gitmodules file' ' · line 156

Quoted 6 lines
t/t7415-submodule-names.sh +6 -0
@@ -151,4 +151,22 @@ test_expect_success 'fsck detects symlinked .gitmodules file' '
+test_expect_success 'fsck detects non-blob .gitmodules' '
+       git init non-blob &&
+       (
+               cd non-blob &&
+
+               # As above, make the funny directly to avoid index restrictions.

Is there a word missing after "funny"?

t/t7415-submodule-names.sh test_expect_success 'fsck detects symlinked .gitmodules file' ' · line 165

Quoted 6 lines
t/t7415-submodule-names.sh +6 -0
+               mkdir subdir &&
+               cp ../.gitmodules subdir/file &&
+               git add subdir/file &&
+               git commit -m ok &&
+               tree=$(git ls-tree HEAD | sed s/subdir/.gitmodules/ | git mktree) &&
+               commit=$(git commit-tree $tree) &&

I see that this is just mirroring the preceding test, but do you need to assign to variable 'commit' which is never consulted by anything later in the test?

Quoted 4 lines
t/t7415-submodule-names.sh +4 -0
+               test_must_fail git fsck 2>output &&
+               grep gitmodulesBlob output
+       )
+'
author
#2 lore raw
Quoted 12 lines · Eric Sunshine
Quoted 11 lines · Jeff King

Commit 159e7b080b (fsck: detect gitmodules files, 2018-05-02) taught fsck to look at the content of .gitmodules files. If the object turns out not to be a blob at all, we just complain and punt on checking the content. And since this was such an obvious and trivial code path, I didn't even bother to add a test.

---

t/t7415-submodule-names.sh +0 -0
@@ -151,4 +151,22 @@ test_expect_success 'fsck detects symlinked .gitmodules file' '
Quoted 8 lines

t/t7415-submodule-names.sh test_expect_success 'fsck detects symlinked .gitmodules file' ' · line 159

Quoted 6 lines
t/t7415-submodule-names.sh +6 -0
+test_expect_success 'fsck detects non-blob .gitmodules' '
+       git init non-blob &&
+       (
+               cd non-blob &&
+
+               # As above, make the funny directly to avoid index restrictions.

Is there a word missing after "funny"?

Oops, should be "funny tree" (that's what I get for trying to wordsmith it at the last minute).

Quoted 10 lines

t/t7415-submodule-names.sh test_expect_success 'fsck detects symlinked .gitmodules file' ' · line 165

Quoted 6 lines
t/t7415-submodule-names.sh +6 -0
+               mkdir subdir &&
+               cp ../.gitmodules subdir/file &&
+               git add subdir/file &&
+               git commit -m ok &&
+               tree=$(git ls-tree HEAD | sed s/subdir/.gitmodules/ | git mktree) &&
+               commit=$(git commit-tree $tree) &&

I see that this is just mirroring the preceding test, but do you need to assign to variable 'commit' which is never consulted by anything later in the test?

No (nor above). I think originally I had planned to points refs to these commits, but it isn't necessary for fsck. In fact, in the final form of the patches, we do not even need the commit at all, since we will complain about .gitmodules in _any_ tree (early versions actually did an extra pass to find which trees were root trees).

-Peff

#3 lore raw
In reply to Jeff King
Quoted 14 lines · Jeff King

Commit 159e7b080b (fsck: detect gitmodules files, 2018-05-02) taught fsck to look at the content of .gitmodules files. If the object turns out not to be a blob at all, we just complain and punt on checking the content. And since this was such an obvious and trivial code path, I didn't even bother to add a test.

Except it _does_ do one non-trivial thing, which is call the report() function, which wants us to pass a pointer to a "struct object". Which we don't have (we have only a "struct object_id"). So we erroneously passed the NULL object, which ends up segfaulting.

Quoted 5 lines

It seems like we could refactor report() to just take the object_id itself. But we pass the object pointer along to a callback function, and indeed this ends up in builtin/fsck.c's objreport() which does want to look at other parts of the object (like the type).

And objreport() can handle OBJ_NONE well, which is the type given by lookup_unknown_object(). So yeah this looks good.

Quoted 2 lines

So instead, let's just use lookup_unknown_object() to get the real "struct object", and pass that.

Signature · 1 line
Duy
author
#4 lore raw
Quoted 8 lines · Duy Nguyen
Quoted 5 lines

It seems like we could refactor report() to just take the object_id itself. But we pass the object pointer along to a callback function, and indeed this ends up in builtin/fsck.c's objreport() which does want to look at other parts of the object (like the type).

And objreport() can handle OBJ_NONE well, which is the type given by lookup_unknown_object(). So yeah this looks good.

Actually, we know that we have some "real" type, because otherwise lookup_blob() would not have returned NULL. In fact, we could actually use lookup_object() here to find that object, knowing that it exists in the hash. But IMHO that would be depending too much on how lookup_blob() works. :)

-Peff

#5 lore raw
In reply to Jeff King
Quoted 4 lines · Jeff King

Except it _does_ do one non-trivial thing, which is call the report() function, which wants us to pass a pointer to a "struct object". Which we don't have (we have only a "struct object_id"). So we erroneously passed the NULL object, which

s/passed/dereferenced/? Probably doesn't affect the fix though.

Quoted 9 lines

ends up segfaulting.

                blob = lookup_blob(oid);
                if (!blob) {
fsck.c +2 -1
-                       ret |= report(options, &blob->object,
+                       struct object *obj = lookup_unknown_object(oid->hash);
+                       ret |= report(options, obj,
                                      FSCK_MSG_GITMODULES_BLOB,
                                      "non-blob found at .gitmodules");
author
#6 lore raw
Quoted 7 lines · Martin Ågren
Quoted 4 lines · Jeff King

Except it _does_ do one non-trivial thing, which is call the report() function, which wants us to pass a pointer to a "struct object". Which we don't have (we have only a "struct object_id"). So we erroneously passed the NULL object, which

s/passed/dereferenced/? Probably doesn't affect the fix though.

Well, we passed it, and then that function dereferenced it. :)

I'm going to re-roll for the minor bits that Eric pointed out, so I'll try to word this better.

-Peff

#7 lore raw
Quoted 8 lines · Jeff King
Quoted 6 lines · Martin Ågren
Quoted 4 lines · Jeff King

Except it _does_ do one non-trivial thing, which is call the report() function, which wants us to pass a pointer to a "struct object". Which we don't have (we have only a "struct object_id"). So we erroneously passed the NULL object, which

Quoted 6 lines
Quoted 1 line

s/passed/dereferenced/? Probably doesn't affect the fix though.

Well, we passed it, and then that function dereferenced it. :)

I'm going to re-roll for the minor bits that Eric pointed out, so I'll try to word this better.

My bad. I somehow thought we get into trouble already before we call report(). Well, we do, since we have undefined behavior. But for all practical purposes &blob->object and blob are the same (NULL-)pointer so we only crash after we call report().

Anyway, obviously no need to do anything about this in a v3.

Martin

author
#8 lore raw
Quoted 16 lines · Martin Ågren
Quoted 8 lines · Jeff King
Quoted 6 lines · Martin Ågren
Quoted 4 lines · Jeff King

Except it _does_ do one non-trivial thing, which is call the report() function, which wants us to pass a pointer to a "struct object". Which we don't have (we have only a "struct object_id"). So we erroneously passed the NULL object, which

Quoted 6 lines
Quoted 1 line

s/passed/dereferenced/? Probably doesn't affect the fix though.

Well, we passed it, and then that function dereferenced it. :)

I'm going to re-roll for the minor bits that Eric pointed out, so I'll try to word this better.

Quoted 6 lines

My bad. I somehow thought we get into trouble already before we call report(). Well, we do, since we have undefined behavior. But for all practical purposes &blob->object and blob are the same (NULL-)pointer so we only crash after we call report().

Anyway, obviously no need to do anything about this in a v3.

Ah, yeah, I didn't really think of it that way. But certainly you are right that the moment we look at &blob->object, we are invoking undefined behavior according to the standard. Hopefully the wording tweak I made covers both ways of thinking about it. :)

-Peff

author
#9 lore raw

v2 cover .gitmodules fsck cleanups

In reply to Jeff King

Here's a v2 which fixes the comment typo and drops the unnecessary commit creation in the tests. I've added a preparatory patch to do the same cleanup in the existing test for consistency.

As before, these should apply on 'maint'.

[1/2]: t7415: don't bother creating commit for symlink test [2/2]: fsck: avoid looking at NULL blob->object

 fsck.c                     |  3 ++-
 t/t7415-submodule-names.sh | 19 ++++++++++++++++++-
 2 files changed, 20 insertions(+), 2 deletions(-)

-Peff

author
#10 lore raw

PATCH v2 1/2 t7415: don't bother creating commit for symlink test

Early versions of the fsck .gitmodules detection code actually required a tree to be at the root of a commit for it to be checked for .gitmodules. What we ended up with in 159e7b080b (fsck: detect gitmodules files, 2018-05-02), though, finds a .gitmodules file in _any_ tree (see that commit for more discussion).

As a result, there's no need to create a commit in our tests. Let's drop it in the name of simplicity.

---
 t/t7415-submodule-names.sh | 1 -
 1 file changed, 1 deletion(-)
t/t7415-submodule-names.sh +0 -1
index a770d92a55..e2aae587ae 100755
@@ -141,7 +141,6 @@ test_expect_success 'fsck detects symlinked .gitmodules file' '
 				printf "120000 blob $target\t.gitmodules\n"
 			} | git mktree
 		) &&
-		commit=$(git commit-tree $tree) &&
 
 		# Check not only that we fail, but that it is due to the
 		# symlink detector; this grep string comes from the config
Signature · 1 line
2.18.0.rc1.446.g4486251e51
#11 lore raw

Re: [PATCH v2 1/2] t7415: don't bother creating commit for symlink test

t/t7415-submodule-names.sh test_expect_success 'fsck detects symlinked .gitmodules file' '

Quoted 13 lines

As a result, there's no need to create a commit in our tests. Let's drop it in the name of simplicity.

---
 t/t7415-submodule-names.sh | 1 -
 1 file changed, 1 deletion(-)
t/t7415-submodule-names.sh +0 -0
index a770d92a55..e2aae587ae 100755
@@ -141,7 +141,6 @@ test_expect_success 'fsck detects symlinked .gitmodules file' '

I add few more lines of context here:

                tree=$(
                        {
                                printf "100644 blob $content\t$tricky\n" &&

t/t7415-submodule-names.sh test_expect_success 'fsck detects symlinked .gitmodules file' ' · line 144

Quoted 4 lines
t/t7415-submodule-names.sh +0 -1
 				printf "120000 blob $target\t.gitmodules\n"
 			} | git mktree
 		) &&
-		commit=$(git commit-tree $tree) &&

This was the only case where that $tree variable was used, so perhaps that can go away as well, in the name of even more simplicity?

Quoted 7 lines
t/t7415-submodule-names.sh +0 -0
 		# Check not only that we fail, but that it is due to the
 		# symlink detector; this grep string comes from the config
Signature · 1 line
2.18.0.rc1.446.g4486251e51
author
#12 lore raw

v3 cover .gitmodules fsck cleanups

Quoted 6 lines · SZEDER Gábor

I add few more lines of context here:

                tree=$(
                        {
                                printf "100644 blob $content\t$tricky\n" &&
Quoted 7 lines

t/t7415-submodule-names.sh test_expect_success 'fsck detects symlinked .gitmodules file' ' · line 144

Quoted 4 lines
t/t7415-submodule-names.sh +0 -1
 				printf "120000 blob $target\t.gitmodules\n"
 			} | git mktree
 		) &&
-		commit=$(git commit-tree $tree) &&

This was the only case where that $tree variable was used, so perhaps that can go away as well, in the name of even more simplicity?

Yep, that is simpler. Here's a v3 just dropping those $tree variables, and adjusting the commit message as appropriate.

[1/2]: t7415: don't bother creating commit for symlink test [2/2]: fsck: avoid looking at NULL blob->object

 fsck.c                     |  3 ++-
 t/t7415-submodule-names.sh | 29 ++++++++++++++++++++++-------
 2 files changed, 24 insertions(+), 8 deletions(-)

-Peff

author
#13 lore raw

PATCH v3 1/2 t7415: don't bother creating commit for symlink test

Early versions of the fsck .gitmodules detection code actually required a tree to be at the root of a commit for it to be checked for .gitmodules. What we ended up with in 159e7b080b (fsck: detect gitmodules files, 2018-05-02), though, finds a .gitmodules file in _any_ tree (see that commit for more discussion).

As a result, there's no need to create a commit in our tests. Let's drop it in the name of simplicity. And since that was the only thing referencing $tree, we can pull our tree creation out of a command substitution.

---
 t/t7415-submodule-names.sh | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
t/t7415-submodule-names.sh +4 -7
index a770d92a55..541bd81684 100755
@@ -135,13 +135,10 @@ test_expect_success 'fsck detects symlinked .gitmodules file' '
 		tricky="[foo]bar=true" &&
 		content=$(git hash-object -w ../.gitmodules) &&
 		target=$(printf "$tricky" | git hash-object -w --stdin) &&
-		tree=$(
-			{
-				printf "100644 blob $content\t$tricky\n" &&
-				printf "120000 blob $target\t.gitmodules\n"
-			} | git mktree
-		) &&
-		commit=$(git commit-tree $tree) &&
+		{
+			printf "100644 blob $content\t$tricky\n" &&
+			printf "120000 blob $target\t.gitmodules\n"
+		} | git mktree &&
 
 		# Check not only that we fail, but that it is due to the
 		# symlink detector; this grep string comes from the config
Signature · 1 line
2.18.0.rc1.446.g4486251e51
author
#14 lore raw

PATCH v3 2/2 fsck: avoid looking at NULL blob->object

In reply to Jeff King

Commit 159e7b080b (fsck: detect gitmodules files, 2018-05-02) taught fsck to look at the content of .gitmodules files. If the object turns out not to be a blob at all, we just complain and punt on checking the content. And since this was such an obvious and trivial code path, I didn't even bother to add a test.

Except it _does_ do one non-trivial thing, which is call the report() function, which wants us to pass a pointer to a "struct object". Which we don't have (we have only a "struct object_id"). So we erroneously pass a NULL object to report(), which gets dereferenced and causes a segfault.

It seems like we could refactor report() to just take the object_id itself. But we pass the object pointer along to a callback function, and indeed this ends up in builtin/fsck.c's objreport() which does want to look at other parts of the object (like the type).

So instead, let's just use lookup_unknown_object() to get the real "struct object", and pass that.

---
 fsck.c                     |  3 ++-
 t/t7415-submodule-names.sh | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)
fsck.c +2 -1
index bcae2c30e6..48e7e36869 100644
@@ -1036,7 +1036,8 @@ int fsck_finish(struct fsck_options *options)
 
 		blob = lookup_blob(oid);
 		if (!blob) {
-			ret |= report(options, &blob->object,
+			struct object *obj = lookup_unknown_object(oid->hash);
+			ret |= report(options, obj,
 				      FSCK_MSG_GITMODULES_BLOB,
 				      "non-blob found at .gitmodules");
 			continue;
t/t7415-submodule-names.sh +18 -0
index 541bd81684..3c0f1a102a 100755
@@ -148,4 +148,22 @@ test_expect_success 'fsck detects symlinked .gitmodules file' '
 	)
 '
 
+test_expect_success 'fsck detects non-blob .gitmodules' '
+	git init non-blob &&
+	(
+		cd non-blob &&
+
+		# As above, make the funny tree directly to avoid index
+		# restrictions.
+		mkdir subdir &&
+		cp ../.gitmodules subdir/file &&
+		git add subdir/file &&
+		git commit -m ok &&
+		git ls-tree HEAD | sed s/subdir/.gitmodules/ | git mktree &&
+
+		test_must_fail git fsck 2>output &&
+		grep gitmodulesBlob output
+	)
+'
+
 test_done
Signature · 1 line
2.18.0.rc1.446.g4486251e51
author
#15 lore raw

PATCH v2 2/2 fsck: avoid looking at NULL blob->object

In reply to Jeff King

Commit 159e7b080b (fsck: detect gitmodules files, 2018-05-02) taught fsck to look at the content of .gitmodules files. If the object turns out not to be a blob at all, we just complain and punt on checking the content. And since this was such an obvious and trivial code path, I didn't even bother to add a test.

Except it _does_ do one non-trivial thing, which is call the report() function, which wants us to pass a pointer to a "struct object". Which we don't have (we have only a "struct object_id"). So we erroneously pass a NULL object to report(), which gets dereferenced and causes a segfault.

It seems like we could refactor report() to just take the object_id itself. But we pass the object pointer along to a callback function, and indeed this ends up in builtin/fsck.c's objreport() which does want to look at other parts of the object (like the type).

So instead, let's just use lookup_unknown_object() to get the real "struct object", and pass that.

---
 fsck.c                     |  3 ++-
 t/t7415-submodule-names.sh | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)
fsck.c +2 -1
index bcae2c30e6..48e7e36869 100644
@@ -1036,7 +1036,8 @@ int fsck_finish(struct fsck_options *options)
 
 		blob = lookup_blob(oid);
 		if (!blob) {
-			ret |= report(options, &blob->object,
+			struct object *obj = lookup_unknown_object(oid->hash);
+			ret |= report(options, obj,
 				      FSCK_MSG_GITMODULES_BLOB,
 				      "non-blob found at .gitmodules");
 			continue;
t/t7415-submodule-names.sh +18 -0
index e2aae587ae..d7868c11fa 100755
@@ -150,4 +150,22 @@ test_expect_success 'fsck detects symlinked .gitmodules file' '
 	)
 '
 
+test_expect_success 'fsck detects non-blob .gitmodules' '
+	git init non-blob &&
+	(
+		cd non-blob &&
+
+		# As above, make the funny tree directly to avoid index
+		# restrictions.
+		mkdir subdir &&
+		cp ../.gitmodules subdir/file &&
+		git add subdir/file &&
+		git commit -m ok &&
+		tree=$(git ls-tree HEAD | sed s/subdir/.gitmodules/ | git mktree) &&
+
+		test_must_fail git fsck 2>output &&
+		grep gitmodulesBlob output
+	)
+'
+
 test_done
Signature · 1 line
2.18.0.rc1.446.g4486251e51
#16 lore raw

t/t7415-submodule-names.sh test_expect_success 'fsck detects symlinked .gitmodules file' ' · line 164

Quoted 12 lines
t/t7415-submodule-names.sh +12 -0
+test_expect_success 'fsck detects non-blob .gitmodules' '
+	git init non-blob &&
+	(
+		cd non-blob &&
+
+		# As above, make the funny tree directly to avoid index
+		# restrictions.
+		mkdir subdir &&
+		cp ../.gitmodules subdir/file &&
+		git add subdir/file &&
+		git commit -m ok &&
+		tree=$(git ls-tree HEAD | sed s/subdir/.gitmodules/ | git mktree) &&

And $tree won't be used here, either.

Quoted 10 lines
t/t7415-submodule-names.sh +6 -0
+
+		test_must_fail git fsck 2>output &&
+		grep gitmodulesBlob output
+	)
+'
+
 test_done
Signature · 1 line
2.18.0.rc1.446.g4486251e51