Skip to content

Commit 0383bbb

Browse files
committed
submodule-config: verify submodule names as paths
Submodule "names" come from the untrusted .gitmodules file, but we blindly append them to $GIT_DIR/modules to create our on-disk repo paths. This means you can do bad things by putting "../" into the name (among other things). Let's sanity-check these names to avoid building a path that can be exploited. There are two main decisions: 1. What should the allowed syntax be? It's tempting to reuse verify_path(), since submodule names typically come from in-repo paths. But there are two reasons not to: a. It's technically more strict than what we need, as we really care only about breaking out of the $GIT_DIR/modules/ hierarchy. E.g., having a submodule named "foo/.git" isn't actually dangerous, and it's possible that somebody has manually given such a funny name. b. Since we'll eventually use this checking logic in fsck to prevent downstream repositories, it should be consistent across platforms. Because verify_path() relies on is_dir_sep(), it wouldn't block "foo\..\bar" on a non-Windows machine. 2. Where should we enforce it? These days most of the .gitmodules reads go through submodule-config.c, so I've put it there in the reading step. That should cover all of the C code. We also construct the name for "git submodule add" inside the git-submodule.sh script. This is probably not a big deal for security since the name is coming from the user anyway, but it would be polite to remind them if the name they pick is invalid (and we need to expose the name-checker to the shell anyway for our test scripts). This patch issues a warning when reading .gitmodules and just ignores the related config entry completely. This will generally end up producing a sensible error, as it works the same as a .gitmodules file which is missing a submodule entry (so "submodule update" will barf, but "git clone --recurse-submodules" will print an error but not abort the clone. There is one minor oddity, which is that we print the warning once per malformed config key (since that's how the config subsystem gives us the entries). So in the new test, for example, the user would see three warnings. That's OK, since the intent is that this case should never come up outside of malicious repositories (and then it might even benefit the user to see the message multiple times). Credit for finding this vulnerability and the proof of concept from which the test script was adapted goes to Etienne Stalmans. Signed-off-by: Jeff King <[email protected]>
1 parent 42e6fde commit 0383bbb

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

builtin/submodule--helper.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,29 @@ static int is_active(int argc, const char **argv, const char *prefix)
11951195
return !is_submodule_initialized(argv[1]);
11961196
}
11971197

1198+
/*
1199+
* Exit non-zero if any of the submodule names given on the command line is
1200+
* invalid. If no names are given, filter stdin to print only valid names
1201+
* (which is primarily intended for testing).
1202+
*/
1203+
static int check_name(int argc, const char **argv, const char *prefix)
1204+
{
1205+
if (argc > 1) {
1206+
while (*++argv) {
1207+
if (check_submodule_name(*argv) < 0)
1208+
return 1;
1209+
}
1210+
} else {
1211+
struct strbuf buf = STRBUF_INIT;
1212+
while (strbuf_getline(&buf, stdin) != EOF) {
1213+
if (!check_submodule_name(buf.buf))
1214+
printf("%s\n", buf.buf);
1215+
}
1216+
strbuf_release(&buf);
1217+
}
1218+
return 0;
1219+
}
1220+
11981221
#define SUPPORT_SUPER_PREFIX (1<<0)
11991222

12001223
struct cmd_struct {
@@ -1216,6 +1239,7 @@ static struct cmd_struct commands[] = {
12161239
{"push-check", push_check, 0},
12171240
{"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
12181241
{"is-active", is_active, 0},
1242+
{"check-name", check_name, 0},
12191243
};
12201244

12211245
int cmd_submodule__helper(int argc, const char **argv, const char *prefix)

git-submodule.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ Use -f if you really want to add it." >&2
228228
sm_name="$sm_path"
229229
fi
230230

231+
if ! git submodule--helper check-name "$sm_name"
232+
then
233+
die "$(eval_gettext "'$sm_name' is not a valid submodule name")"
234+
fi
235+
231236
# perhaps the path exists and is already a git repo, else clone it
232237
if test -e "$sm_path"
233238
then

submodule-config.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,31 @@ static struct submodule *cache_lookup_name(struct submodule_cache *cache,
163163
return NULL;
164164
}
165165

166+
int check_submodule_name(const char *name)
167+
{
168+
/* Disallow empty names */
169+
if (!*name)
170+
return -1;
171+
172+
/*
173+
* Look for '..' as a path component. Check both '/' and '\\' as
174+
* separators rather than is_dir_sep(), because we want the name rules
175+
* to be consistent across platforms.
176+
*/
177+
goto in_component; /* always start inside component */
178+
while (*name) {
179+
char c = *name++;
180+
if (c == '/' || c == '\\') {
181+
in_component:
182+
if (name[0] == '.' && name[1] == '.' &&
183+
(!name[2] || name[2] == '/' || name[2] == '\\'))
184+
return -1;
185+
}
186+
}
187+
188+
return 0;
189+
}
190+
166191
static int name_and_item_from_var(const char *var, struct strbuf *name,
167192
struct strbuf *item)
168193
{
@@ -174,6 +199,12 @@ static int name_and_item_from_var(const char *var, struct strbuf *name,
174199
return 0;
175200

176201
strbuf_add(name, subsection, subsection_len);
202+
if (check_submodule_name(name->buf) < 0) {
203+
warning(_("ignoring suspicious submodule name: %s"), name->buf);
204+
strbuf_release(name);
205+
return 0;
206+
}
207+
177208
strbuf_addstr(item, key);
178209

179210
return 1;

submodule-config.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,11 @@ extern int gitmodule_sha1_from_commit(const unsigned char *commit_sha1,
3535
struct strbuf *rev);
3636
extern void submodule_free(void);
3737

38+
/*
39+
* Returns 0 if the name is syntactically acceptable as a submodule "name"
40+
* (e.g., that may be found in the subsection of a .gitmodules file) and -1
41+
* otherwise.
42+
*/
43+
int check_submodule_name(const char *name);
44+
3845
#endif /* SUBMODULE_CONFIG_H */

t/t7415-submodule-names.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/bin/sh
2+
3+
test_description='check handling of .. in submodule names
4+
5+
Exercise the name-checking function on a variety of names, and then give a
6+
real-world setup that confirms we catch this in practice.
7+
'
8+
. ./test-lib.sh
9+
10+
test_expect_success 'check names' '
11+
cat >expect <<-\EOF &&
12+
valid
13+
valid/with/paths
14+
EOF
15+
16+
git submodule--helper check-name >actual <<-\EOF &&
17+
valid
18+
valid/with/paths
19+
20+
../foo
21+
/../foo
22+
..\foo
23+
\..\foo
24+
foo/..
25+
foo/../
26+
foo\..
27+
foo\..\
28+
foo/../bar
29+
EOF
30+
31+
test_cmp expect actual
32+
'
33+
34+
test_expect_success 'create innocent subrepo' '
35+
git init innocent &&
36+
git -C innocent commit --allow-empty -m foo
37+
'
38+
39+
test_expect_success 'submodule add refuses invalid names' '
40+
test_must_fail \
41+
git submodule add --name ../../modules/evil "$PWD/innocent" evil
42+
'
43+
44+
test_expect_success 'add evil submodule' '
45+
git submodule add "$PWD/innocent" evil &&
46+
47+
mkdir modules &&
48+
cp -r .git/modules/evil modules &&
49+
write_script modules/evil/hooks/post-checkout <<-\EOF &&
50+
echo >&2 "RUNNING POST CHECKOUT"
51+
EOF
52+
53+
git config -f .gitmodules submodule.evil.update checkout &&
54+
git config -f .gitmodules --rename-section \
55+
submodule.evil submodule.../../modules/evil &&
56+
git add modules &&
57+
git commit -am evil
58+
'
59+
60+
# This step seems like it shouldn't be necessary, since the payload is
61+
# contained entirely in the evil submodule. But due to the vagaries of the
62+
# submodule code, checking out the evil module will fail unless ".git/modules"
63+
# exists. Adding another submodule (with a name that sorts before "evil") is an
64+
# easy way to make sure this is the case in the victim clone.
65+
test_expect_success 'add other submodule' '
66+
git submodule add "$PWD/innocent" another-module &&
67+
git add another-module &&
68+
git commit -am another
69+
'
70+
71+
test_expect_success 'clone evil superproject' '
72+
git clone --recurse-submodules . victim >output 2>&1 &&
73+
! grep "RUNNING POST CHECKOUT" output
74+
'
75+
76+
test_done

0 commit comments

Comments
 (0)