Skip to content

Commit 4778452

Browse files
committed
Merge branch 'prevent-name-squatting-on-windows'
This patch series fixes an issue where Git could formerly have been tricked into creating a `.git` file with an unexpected (and therefore unprotected) NTFS short name. Incidentally, it also fixes an issue where a tree entry containing a backslash could be tricked into following a symbolic link, i.e. Git could be tricked into writing files outside the worktree. Signed-off-by: Johannes Schindelin <[email protected]>
2 parents a7b1ad3 + e1d911d commit 4778452

File tree

7 files changed

+60
-2
lines changed

7 files changed

+60
-2
lines changed

builtin/clone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ static int checkout(int submodule_progress)
757757

758758
if (!err && (option_recurse_submodules.nr > 0)) {
759759
struct argv_array args = ARGV_ARRAY_INIT;
760-
argv_array_pushl(&args, "submodule", "update", "--init", "--recursive", NULL);
760+
argv_array_pushl(&args, "submodule", "update", "--require-init", "--recursive", NULL);
761761

762762
if (option_shallow_submodules == 1)
763763
argv_array_push(&args, "--depth=1");

builtin/submodule--helper.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "remote.h"
1414
#include "refs.h"
1515
#include "connect.h"
16+
#include "dir.h"
1617

1718
static char *get_default_remote(void)
1819
{
@@ -623,6 +624,7 @@ static int module_clone(int argc, const char **argv, const char *prefix)
623624
char *p, *path = NULL, *sm_gitdir;
624625
struct strbuf sb = STRBUF_INIT;
625626
struct string_list reference = STRING_LIST_INIT_NODUP;
627+
int require_init = 0;
626628
char *sm_alternate = NULL, *error_strategy = NULL;
627629

628630
struct option module_clone_options[] = {
@@ -647,6 +649,8 @@ static int module_clone(int argc, const char **argv, const char *prefix)
647649
OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
648650
OPT_BOOL(0, "progress", &progress,
649651
N_("force cloning progress")),
652+
OPT_BOOL(0, "require-init", &require_init,
653+
N_("disallow cloning into non-empty directory")),
650654
OPT_END()
651655
};
652656

@@ -685,6 +689,8 @@ static int module_clone(int argc, const char **argv, const char *prefix)
685689
die(_("clone of '%s' into submodule path '%s' failed"),
686690
url, path);
687691
} else {
692+
if (require_init && !access(path, X_OK) && !is_empty_dir(path))
693+
die(_("directory not empty: '%s'"), path);
688694
if (safe_create_leading_directories_const(path) < 0)
689695
die(_("could not create directory '%s'"), path);
690696
strbuf_addf(&sb, "%s/index", sm_gitdir);
@@ -733,6 +739,7 @@ struct submodule_update_clone {
733739
int quiet;
734740
int recommend_shallow;
735741
struct string_list references;
742+
unsigned require_init;
736743
const char *depth;
737744
const char *recursive_prefix;
738745
const char *prefix;
@@ -748,7 +755,7 @@ struct submodule_update_clone {
748755
int failed_clones_nr, failed_clones_alloc;
749756
};
750757
#define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
751-
SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, \
758+
SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, 0, \
752759
NULL, NULL, NULL, \
753760
STRING_LIST_INIT_DUP, 0, NULL, 0, 0}
754761

@@ -850,6 +857,8 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
850857
argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL);
851858
if (suc->recommend_shallow && sub->recommend_shallow == 1)
852859
argv_array_push(&child->args, "--depth=1");
860+
if (suc->require_init)
861+
argv_array_push(&child->args, "--require-init");
853862
argv_array_pushl(&child->args, "--path", sub->path, NULL);
854863
argv_array_pushl(&child->args, "--name", sub->name, NULL);
855864
argv_array_pushl(&child->args, "--url", sub->url, NULL);
@@ -992,6 +1001,8 @@ static int update_clone(int argc, const char **argv, const char *prefix)
9921001
OPT__QUIET(&suc.quiet, N_("don't print cloning progress")),
9931002
OPT_BOOL(0, "progress", &suc.progress,
9941003
N_("force cloning progress")),
1004+
OPT_BOOL(0, "require-init", &suc.require_init,
1005+
N_("disallow cloning into non-empty directory")),
9951006
OPT_END()
9961007
};
9971008

git-submodule.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ reference=
3434
cached=
3535
recursive=
3636
init=
37+
require_init=
3738
files=
3839
remote=
3940
nofetch=
@@ -528,6 +529,10 @@ cmd_update()
528529
-i|--init)
529530
init=1
530531
;;
532+
--require-init)
533+
init=1
534+
require_init=1
535+
;;
531536
--remote)
532537
remote=1
533538
;;
@@ -606,6 +611,7 @@ cmd_update()
606611
${update:+--update "$update"} \
607612
${reference:+"$reference"} \
608613
${depth:+--depth "$depth"} \
614+
${require_init:+--require-init} \
609615
${recommend_shallow:+"$recommend_shallow"} \
610616
${jobs:+$jobs} \
611617
"$@" || echo "#unmatched" $?

t/t1450-fsck.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ while read name path pretty; do
419419
(
420420
git init $name-$type &&
421421
cd $name-$type &&
422+
git config core.protectNTFS false &&
422423
echo content >file &&
423424
git add file &&
424425
git commit -m base &&

t/t7415-submodule-names.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,37 @@ test_expect_success 'clone evil superproject' '
7373
! grep "RUNNING POST CHECKOUT" output
7474
'
7575

76+
test_expect_success MINGW 'prevent git~1 squatting on Windows' '
77+
git init squatting &&
78+
(
79+
cd squatting &&
80+
mkdir a &&
81+
touch a/..git &&
82+
git add a/..git &&
83+
test_tick &&
84+
git commit -m initial &&
85+
86+
modules="$(test_write_lines \
87+
"[submodule \"b.\"]" "url = ." "path = c" \
88+
"[submodule \"b\"]" "url = ." "path = d\\\\a" |
89+
git hash-object -w --stdin)" &&
90+
rev="$(git rev-parse --verify HEAD)" &&
91+
hash="$(echo x | git hash-object -w --stdin)" &&
92+
git -c core.protectNTFS=false update-index --add \
93+
--cacheinfo 100644,$modules,.gitmodules \
94+
--cacheinfo 160000,$rev,c \
95+
--cacheinfo 160000,$rev,d\\a \
96+
--cacheinfo 100644,$hash,d./a/x \
97+
--cacheinfo 100644,$hash,d./a/..git &&
98+
test_tick &&
99+
git -c core.protectNTFS=false commit -m "module" &&
100+
test_must_fail git show HEAD: 2>err &&
101+
test_i18ngrep backslash err
102+
) &&
103+
test_must_fail git -c core.protectNTFS=false \
104+
clone --recurse-submodules squatting squatting-clone 2>err &&
105+
test_i18ngrep "directory not empty" err &&
106+
! grep gitdir squatting-clone/d/a/git~2
107+
'
108+
76109
test_done

t/t9350-fast-export.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ test_expect_success 'directory becomes symlink' '
421421

422422
test_expect_success 'fast-export quotes pathnames' '
423423
git init crazy-paths &&
424+
test_config -C crazy-paths core.protectNTFS false &&
424425
(cd crazy-paths &&
425426
blob=$(echo foo | git hash-object -w --stdin) &&
426427
git update-index --add \

tree-walk.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ static int decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned l
4141
strbuf_addstr(err, _("empty filename in tree entry"));
4242
return -1;
4343
}
44+
#ifdef GIT_WINDOWS_NATIVE
45+
if (protect_ntfs && strchr(path, '\\')) {
46+
strbuf_addf(err, _("filename in tree entry contains backslash: '%s'"), path);
47+
return -1;
48+
}
49+
#endif
4450
len = strlen(path) + 1;
4551

4652
/* Initialize the descriptor entry */

0 commit comments

Comments
 (0)