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 <peff@peff.net>
2018-04-30 09:25:25 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
test_description='check handling of .. in submodule names
|
|
|
|
|
|
|
|
Exercise the name-checking function on a variety of names, and then give a
|
|
|
|
real-world setup that confirms we catch this in practice.
|
|
|
|
'
|
|
|
|
. ./test-lib.sh
|
index-pack: check .gitmodules files with --strict
Now that the internal fsck code has all of the plumbing we
need, we can start checking incoming .gitmodules files.
Naively, it seems like we would just need to add a call to
fsck_finish() after we've processed all of the objects. And
that would be enough to cover the initial test included
here. But there are two extra bits:
1. We currently don't bother calling fsck_object() at all
for blobs, since it has traditionally been a noop. We'd
actually catch these blobs in fsck_finish() at the end,
but it's more efficient to check them when we already
have the object loaded in memory.
2. The second pass done by fsck_finish() needs to access
the objects, but we're actually indexing the pack in
this process. In theory we could give the fsck code a
special callback for accessing the in-pack data, but
it's actually quite tricky:
a. We don't have an internal efficient index mapping
oids to packfile offsets. We only generate it on
the fly as part of writing out the .idx file.
b. We'd still have to reconstruct deltas, which means
we'd basically have to replicate all of the
reading logic in packfile.c.
Instead, let's avoid running fsck_finish() until after
we've written out the .idx file, and then just add it
to our internal packed_git list.
This does mean that the objects are "in the repository"
before we finish our fsck checks. But unpack-objects
already exhibits this same behavior, and it's an
acceptable tradeoff here for the same reason: the
quarantine mechanism means that pushes will be
fully protected.
In addition to a basic push test in t7415, we add a sneaky
pack that reverses the usual object order in the pack,
requiring that index-pack access the tree and blob during
the "finish" step.
This already works for unpack-objects (since it will have
written out loose objects), but we'll check it with this
sneaky pack for good measure.
Signed-off-by: Jeff King <peff@peff.net>
2018-05-05 01:45:01 +02:00
|
|
|
. "$TEST_DIRECTORY"/lib-pack.sh
|
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 <peff@peff.net>
2018-04-30 09:25:25 +02:00
|
|
|
|
|
|
|
test_expect_success 'check names' '
|
|
|
|
cat >expect <<-\EOF &&
|
|
|
|
valid
|
|
|
|
valid/with/paths
|
|
|
|
EOF
|
|
|
|
|
|
|
|
git submodule--helper check-name >actual <<-\EOF &&
|
|
|
|
valid
|
|
|
|
valid/with/paths
|
|
|
|
|
|
|
|
../foo
|
|
|
|
/../foo
|
|
|
|
..\foo
|
|
|
|
\..\foo
|
|
|
|
foo/..
|
|
|
|
foo/../
|
|
|
|
foo\..
|
|
|
|
foo\..\
|
|
|
|
foo/../bar
|
|
|
|
EOF
|
|
|
|
|
|
|
|
test_cmp expect actual
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'create innocent subrepo' '
|
|
|
|
git init innocent &&
|
|
|
|
git -C innocent commit --allow-empty -m foo
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'submodule add refuses invalid names' '
|
|
|
|
test_must_fail \
|
|
|
|
git submodule add --name ../../modules/evil "$PWD/innocent" evil
|
|
|
|
'
|
|
|
|
|
|
|
|
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 &&
|
|
|
|
echo >&2 "RUNNING 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 &&
|
|
|
|
! grep "RUNNING POST CHECKOUT" output
|
|
|
|
'
|
|
|
|
|
2018-05-02 23:20:35 +02:00
|
|
|
test_expect_success 'fsck detects evil superproject' '
|
|
|
|
test_must_fail git fsck
|
|
|
|
'
|
|
|
|
|
2018-05-05 01:40:08 +02:00
|
|
|
test_expect_success 'transfer.fsckObjects detects evil superproject (unpack)' '
|
|
|
|
rm -rf dst.git &&
|
|
|
|
git init --bare dst.git &&
|
|
|
|
git -C dst.git config transfer.fsckObjects true &&
|
|
|
|
test_must_fail git push dst.git HEAD
|
|
|
|
'
|
|
|
|
|
index-pack: check .gitmodules files with --strict
Now that the internal fsck code has all of the plumbing we
need, we can start checking incoming .gitmodules files.
Naively, it seems like we would just need to add a call to
fsck_finish() after we've processed all of the objects. And
that would be enough to cover the initial test included
here. But there are two extra bits:
1. We currently don't bother calling fsck_object() at all
for blobs, since it has traditionally been a noop. We'd
actually catch these blobs in fsck_finish() at the end,
but it's more efficient to check them when we already
have the object loaded in memory.
2. The second pass done by fsck_finish() needs to access
the objects, but we're actually indexing the pack in
this process. In theory we could give the fsck code a
special callback for accessing the in-pack data, but
it's actually quite tricky:
a. We don't have an internal efficient index mapping
oids to packfile offsets. We only generate it on
the fly as part of writing out the .idx file.
b. We'd still have to reconstruct deltas, which means
we'd basically have to replicate all of the
reading logic in packfile.c.
Instead, let's avoid running fsck_finish() until after
we've written out the .idx file, and then just add it
to our internal packed_git list.
This does mean that the objects are "in the repository"
before we finish our fsck checks. But unpack-objects
already exhibits this same behavior, and it's an
acceptable tradeoff here for the same reason: the
quarantine mechanism means that pushes will be
fully protected.
In addition to a basic push test in t7415, we add a sneaky
pack that reverses the usual object order in the pack,
requiring that index-pack access the tree and blob during
the "finish" step.
This already works for unpack-objects (since it will have
written out loose objects), but we'll check it with this
sneaky pack for good measure.
Signed-off-by: Jeff King <peff@peff.net>
2018-05-05 01:45:01 +02:00
|
|
|
test_expect_success 'transfer.fsckObjects detects evil superproject (index)' '
|
|
|
|
rm -rf dst.git &&
|
|
|
|
git init --bare dst.git &&
|
|
|
|
git -C dst.git config transfer.fsckObjects true &&
|
|
|
|
git -C dst.git config transfer.unpackLimit 1 &&
|
|
|
|
test_must_fail git push dst.git HEAD
|
|
|
|
'
|
|
|
|
|
|
|
|
# Normally our packs contain commits followed by trees followed by blobs. This
|
|
|
|
# reverses the order, which requires backtracking to find the context of a
|
|
|
|
# blob. We'll start with a fresh gitmodules-only tree to make it simpler.
|
|
|
|
test_expect_success 'create oddly ordered pack' '
|
|
|
|
git checkout --orphan odd &&
|
|
|
|
git rm -rf --cached . &&
|
|
|
|
git add .gitmodules &&
|
|
|
|
git commit -m odd &&
|
|
|
|
{
|
|
|
|
pack_header 3 &&
|
|
|
|
pack_obj $(git rev-parse HEAD:.gitmodules) &&
|
|
|
|
pack_obj $(git rev-parse HEAD^{tree}) &&
|
|
|
|
pack_obj $(git rev-parse HEAD)
|
|
|
|
} >odd.pack &&
|
|
|
|
pack_trailer odd.pack
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'transfer.fsckObjects handles odd pack (unpack)' '
|
|
|
|
rm -rf dst.git &&
|
|
|
|
git init --bare dst.git &&
|
|
|
|
test_must_fail git -C dst.git unpack-objects --strict <odd.pack
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'transfer.fsckObjects handles odd pack (index)' '
|
|
|
|
rm -rf dst.git &&
|
|
|
|
git init --bare dst.git &&
|
|
|
|
test_must_fail git -C dst.git index-pack --strict --stdin <odd.pack
|
|
|
|
'
|
|
|
|
|
2018-05-05 02:03:35 +02:00
|
|
|
test_expect_success 'fsck detects symlinked .gitmodules file' '
|
|
|
|
git init symlink &&
|
|
|
|
(
|
|
|
|
cd symlink &&
|
|
|
|
|
|
|
|
# Make the tree directly to avoid index restrictions.
|
|
|
|
#
|
|
|
|
# Because symlinks store the target as a blob, choose
|
|
|
|
# a pathname that could be parsed as a .gitmodules file
|
|
|
|
# to trick naive non-symlink-aware checking.
|
|
|
|
tricky="[foo]bar=true" &&
|
|
|
|
content=$(git hash-object -w ../.gitmodules) &&
|
|
|
|
target=$(printf "$tricky" | git hash-object -w --stdin) &&
|
|
|
|
tree=$(
|
|
|
|
{
|
|
|
|
printf "100644 blob $content\t$tricky\n" &&
|
|
|
|
printf "120000 blob $target\t.gitmodules\n"
|
|
|
|
} | git mktree
|
|
|
|
) &&
|
|
|
|
commit=$(git commit-tree $tree) &&
|
|
|
|
|
|
|
|
# Check not only that we fail, but that it is due to the
|
|
|
|
# symlink detector; this grep string comes from the config
|
|
|
|
# variable name and will not be translated.
|
|
|
|
test_must_fail git fsck 2>output &&
|
|
|
|
grep gitmodulesSymlink output
|
|
|
|
)
|
|
|
|
'
|
|
|
|
|
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 <peff@peff.net>
2018-04-30 09:25:25 +02:00
|
|
|
test_done
|