Skip to content

Commit 9cf8547

Browse files
filip-hejsekdscho
authored andcommitted
clone: prevent clashing git dirs when cloning submodule in parallel
While it is expected to have several git dirs within the `.git/modules/` tree, it is important that they do not interfere with each other. For example, if one submodule was called "captain" and another submodule "captain/hooks", their respective git dirs would clash, as they would be located in `.git/modules/captain/` and `.git/modules/captain/hooks/`, respectively, i.e. the latter's files could clash with the actual Git hooks of the former. To prevent these clashes, and in particular to prevent hooks from being written and then executed as part of a recursive clone, we introduced checks as part of the fix for CVE-2019-1387 in a8dee3c (Disallow dubiously-nested submodule git directories, 2019-10-01). It is currently possible to bypass the check for clashing submodule git dirs in two ways: 1. parallel cloning 2. checkout --recurse-submodules Let's check not only before, but also after parallel cloning (and before checking out the submodule), that the git dir is not clashing with another one, otherwise fail. This addresses the parallel cloning issue. As to the parallel checkout issue: It requires quite a few manual steps to create clashing git dirs because Git itself would refuse to initialize the inner one, as demonstrated by the test case. Nevertheless, let's teach the recursive checkout (namely, the `submodule_move_head()` function that is used by the recursive checkout) to be careful to verify that it does not use a clashing git dir, and if it does, disable it (by deleting the `HEAD` file so that subsequent Git calls won't recognize it as a git dir anymore). Note: The parallel cloning test case contains a `cat err` that proved to be highly useful when analyzing the racy nature of the operation (the operation can fail with three different error messages, depending on timing), and was left on purpose to ease future debugging should the need arise. Signed-off-by: Filip Hejsek <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent b20c10f commit 9cf8547

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

builtin/submodule--helper.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,23 @@ static int clone_submodule(const struct module_clone_data *clone_data,
17171717
free(path);
17181718
}
17191719

1720+
/*
1721+
* We already performed this check at the beginning of this function,
1722+
* before cloning the objects. This tries to detect racy behavior e.g.
1723+
* in parallel clones, where another process could easily have made the
1724+
* gitdir nested _after_ it was created.
1725+
*
1726+
* To prevent further harm coming from this unintentionally-nested
1727+
* gitdir, let's disable it by deleting the `HEAD` file.
1728+
*/
1729+
if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0) {
1730+
char *head = xstrfmt("%s/HEAD", sm_gitdir);
1731+
unlink(head);
1732+
free(head);
1733+
die(_("refusing to create/use '%s' in another submodule's "
1734+
"git dir"), sm_gitdir);
1735+
}
1736+
17201737
connect_work_tree_and_git_dir(clone_data_path, sm_gitdir, 0);
17211738

17221739
p = git_pathdup_submodule(clone_data_path, "config");

submodule.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,10 +2146,27 @@ int submodule_move_head(const char *path,
21462146
if (old_head) {
21472147
if (!submodule_uses_gitfile(path))
21482148
absorb_git_dir_into_superproject(path);
2149+
else {
2150+
char *dotgit = xstrfmt("%s/.git", path);
2151+
char *git_dir = xstrdup(read_gitfile(dotgit));
2152+
2153+
free(dotgit);
2154+
if (validate_submodule_git_dir(git_dir,
2155+
sub->name) < 0)
2156+
die(_("refusing to create/use '%s' in "
2157+
"another submodule's git dir"),
2158+
git_dir);
2159+
free(git_dir);
2160+
}
21492161
} else {
21502162
struct strbuf gitdir = STRBUF_INIT;
21512163
submodule_name_to_gitdir(&gitdir, the_repository,
21522164
sub->name);
2165+
if (validate_submodule_git_dir(gitdir.buf,
2166+
sub->name) < 0)
2167+
die(_("refusing to create/use '%s' in another "
2168+
"submodule's git dir"),
2169+
gitdir.buf);
21532170
connect_work_tree_and_git_dir(path, gitdir.buf, 0);
21542171
strbuf_release(&gitdir);
21552172

t/t7450-bad-git-dotfiles.sh

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ test_expect_success WINDOWS 'prevent git~1 squatting on Windows' '
292292
fi
293293
'
294294

295-
test_expect_success 'git dirs of sibling submodules must not be nested' '
295+
test_expect_success 'setup submodules with nested git dirs' '
296296
git init nested &&
297297
test_commit -C nested nested &&
298298
(
@@ -310,9 +310,39 @@ test_expect_success 'git dirs of sibling submodules must not be nested' '
310310
git add .gitmodules thing1 thing2 &&
311311
test_tick &&
312312
git commit -m nested
313-
) &&
313+
)
314+
'
315+
316+
test_expect_success 'git dirs of sibling submodules must not be nested' '
314317
test_must_fail git clone --recurse-submodules nested clone 2>err &&
315318
test_i18ngrep "is inside git dir" err
316319
'
317320

321+
test_expect_success 'submodule git dir nesting detection must work with parallel cloning' '
322+
test_must_fail git clone --recurse-submodules --jobs=2 nested clone_parallel 2>err &&
323+
cat err &&
324+
grep -E "(already exists|is inside git dir|not a git repository)" err &&
325+
{
326+
test_path_is_missing .git/modules/hippo/HEAD ||
327+
test_path_is_missing .git/modules/hippo/hooks/HEAD
328+
}
329+
'
330+
331+
test_expect_success 'checkout -f --recurse-submodules must not use a nested gitdir' '
332+
git clone nested nested_checkout &&
333+
(
334+
cd nested_checkout &&
335+
git submodule init &&
336+
git submodule update thing1 &&
337+
mkdir -p .git/modules/hippo/hooks/refs &&
338+
mkdir -p .git/modules/hippo/hooks/objects/info &&
339+
echo "../../../../objects" >.git/modules/hippo/hooks/objects/info/alternates &&
340+
echo "ref: refs/heads/master" >.git/modules/hippo/hooks/HEAD
341+
) &&
342+
test_must_fail git -C nested_checkout checkout -f --recurse-submodules HEAD 2>err &&
343+
cat err &&
344+
grep "is inside git dir" err &&
345+
test_path_is_missing nested_checkout/thing2/.git
346+
'
347+
318348
test_done

0 commit comments

Comments
 (0)