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 a8dee3ca61 (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 <filip.hejsek@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>

Filip Hejsek committed Jan 28, 2024 at 05:09 UTC 9cf85473209ea8ae2b56c13145c4704d12ee1374
3 files changed +66 -2
builtin/submodule--helper.c
+17
@@ -1717,6 +1717,23 @@ static int clone_submodule(const struct module_clone_data *clone_data,
1717 free(path);
1718 }
1719
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 +
1737 connect_work_tree_and_git_dir(clone_data_path, sm_gitdir, 0);
1738
1739 p = git_pathdup_submodule(clone_data_path, "config");
submodule.c
+17
@@ -2146,10 +2146,27 @@ int submodule_move_head(const char *path,
2146 if (old_head) {
2147 if (!submodule_uses_gitfile(path))
2148 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 + }
2161 } else {
2162 struct strbuf gitdir = STRBUF_INIT;
2163 submodule_name_to_gitdir(&gitdir, the_repository,
2164 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);
2170 connect_work_tree_and_git_dir(path, gitdir.buf, 0);
2171 strbuf_release(&gitdir);
2172
t/t7450-bad-git-dotfiles.sh
+32 -2
@@ -292,7 +292,7 @@ test_expect_success WINDOWS 'prevent git~1 squatting on Windows' '
292 fi
293 '
294
295 -test_expect_success 'git dirs of sibling submodules must not be nested' '
295 +test_expect_success 'setup submodules with nested git dirs' '
296 git init nested &&
297 test_commit -C nested nested &&
298 (
@@ -310,9 +310,39 @@ test_expect_success 'git dirs of sibling submodules must not be nested' '
310 git add .gitmodules thing1 thing2 &&
311 test_tick &&
312 git commit -m nested
313 - ) &&
313 + )
314 +'
315 +
316 +test_expect_success 'git dirs of sibling submodules must not be nested' '
317 test_must_fail git clone --recurse-submodules nested clone 2>err &&
318 test_i18ngrep "is inside git dir" err
319 '
320
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 +
348 test_done