Browse files

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? I've reused
     verify_path() here, which is the same function that
     restricts index entries, and includes things like
     ".git" matching. Technically this is overly strict, as
     it does currently work to have a .gitmodules like:

       [submodule "foo/.git/bar"]
       path = more-sane-path

     But in practice you'd have to intentionally make such a
     funny config. By default, the subsection name is
     derived from the path at which the submodule was added,
     so it would generaly be subject to the verify_path()
     rules already.

  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. There is one place in which still constructs a
     $GIT_DIR/modules/$name path itself, but the name there
     comes from either:

       a. An in-tree path, which we already know satisfies

       b. The --name parameter on the command line. This is
          presumably under the user's control.

     So this patch should close the vulnerability

     This patch issues a warning and just ignores the 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.

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 <>
Signed-off-by: Junio C Hamano <>
  • Loading branch information...
peff authored and gitster committed May 5, 2018
1 parent 42e6fde commit 8fea94e3aa10ad23bed344c1201eca81b48aacc5
Showing with 63 additions and 0 deletions.
  1. +13 −0 submodule-config.c
  2. +7 −0 submodule-config.h
  3. +43 −0 t/
@@ -163,6 +163,13 @@ static struct submodule *cache_lookup_name(struct submodule_cache *cache,
return NULL;
int check_submodule_name(const char *name)
if (!verify_path(name))
return -1;
return 0;
static int name_and_item_from_var(const char *var, struct strbuf *name,
struct strbuf *item)
@@ -174,6 +181,12 @@ static int name_and_item_from_var(const char *var, struct strbuf *name,
return 0;
strbuf_add(name, subsection, subsection_len);
if (check_submodule_name(name->buf) < 0) {
warning("ignoring suspicious submodule name: %s", name->buf);
return 0;
strbuf_addstr(item, key);
return 1;
@@ -35,4 +35,11 @@ extern int gitmodule_sha1_from_commit(const unsigned char *commit_sha1,
struct strbuf *rev);
extern void submodule_free(void);
* Returns 0 if the name is syntactically acceptable as a submodule "name"
* (e.g., that may be found in the subsection of a .gitmodules file) and -1
* otherwise.
int check_submodule_name(const char *name);
@@ -0,0 +1,43 @@
test_description='check handling of .. in submodule names'
. ./
test_expect_success 'create innocent subrepo' '
git init innocent &&
git -C innocent commit --allow-empty -m foo
test_expect_success 'add evil submodule' '
git submodule add "$PWD/innocent" evil &&
mkdir modules &&
cp -r .git/modules/evil modules &&
write_script modules/evil/hooks/post-checkout <<-\EOF &&
git config -f .gitmodules submodule.evil.update checkout &&
git config -f .gitmodules --rename-section \
submodule.evil submodule.../../modules/evil &&
git add modules &&
git commit -am evil
# This step seems like it shouldn't be necessary, since the payload is
# contained entirely in the evil submodule. But due to the vagaries of the
# submodule code, checking out the evil module will fail unless ".git/modules"
# exists. Adding another submodule (with a name that sorts before "evil") is an
# easy way to make sure this is the case in the victim clone.
test_expect_success 'add other submodule' '
git submodule add "$PWD/innocent" another-module &&
git add another-module &&
git commit -am another
test_expect_success 'clone evil superproject' '
git clone --recurse-submodules . victim >output 2>&1 &&

0 comments on commit 8fea94e

Please sign in to comment.