Skip to content

Commit 11092ef

Browse files
committed
t/{t-path,testenv}.sh: refactor and fix path tests
In commits 74d5f23 and 10c4ffc the t/t-path.sh tests were added to validate the remediations of the security issues from CVE-2020-27955 and CVE-2021-21237, respectively. On Windows, both of these tests make use of a "git.bat" script which stands in for Git, and if the script is executed instead of the real Git during a test, then that indicates that a security problem still exists. However, in a previous commit we have now added a new helper program, lfstest-badpathcheck, which can be used for the same purpose, and which has the advantage of having a .exe extension on Windows. We will make use of this helper program in a new test accompanying the remediation of CVE-2022-24826. The existence of this new helper also means we can now use it as a fake "malicious" binary named "git.exe" in our existing t/t-path.sh tests. This will help ensure that these tests are robust against unexpected values of the Windows PATHEXT environment variable or future changes to the set of executable file extensions we support in Git LFS. Using this new helper program instead of a "git.bat" script does mean we need to be careful how and when it might run, because it mirrors the name of the real Git executable. We therefore try to keep it out of any possible execution path until we run our final concluding checks in each test. In other words, rather than try to manipulate PATH (and PATHEXT on Windows), we take steps to keep our "malicious" executable out of any possible search path prior to when we want to explicitly test Git LFS's behaviour in a known state relating to one of the CVEs. (One challenge with manipulating PATH to try to remove paths that resolve to the current directory is that it must be done iteratively to deal with pathological cases like ":.:.:" and ":::". The "sed" regular expressions in our existing tests would also need to use "\." to match only the "." character, as well as additional expressions to match "." at the beginning and end of PATH.) It is easier, therefore, to simply avoid putting our "malicious" executable into the current directory until the last moment. Also, in the second test where we want to add it to a repository which we will then clone, we can remove it as soon as we've run "git add", and we can make sure to run the real Git (i.e., whatever is found first using the extant PATH and PATHEXT variables) rather than ours to perform that "git add". When we reach the end of each test, where we want to explicitly run Git LFS and see if it incorrectly searches in the current directory even when not asked to, we now reset PATH and PATHEXT just for those specific invocations of Git LFS. For PATH we use either BINPATH, which contains only our compiled "git-lfs" binary, or BINPATH plus the path to the real Git as returned by "command -v". For PATHEXT we just use the primary executable file extension (if any) for the current operating system. To determine that primary executable file extension we add an X variable which we set in t/testenv.sh and which parallels the one set in the main Makefile. On Windows, we set X to contain ".exe", and on Unix we set it to the empty string. We can then use this X variable throughout our tests wherever we want to refer to a specific executable's full filename. With these changes, even when PATH includes "." as the first directory to be searched, both of our tests should now always reach their concluding checks and should function as expected at that point. Specifically, they should fail by detecting the output of our "malicious" Git program when run without the Git LFS code changes corresponding to their respective CVEs, and should succeed otherwise. (Technically, the second test will fail for a different reason if the remediation for CVE-2020-27955 is not in effect than if only the remediation for CVE-2021-21237 is not in effect. But the test will fail at the same point in both cases, i.e., in its concluding final check.) In the final checks in both tests we search for the text string "exploit" in the output log file captured after running a Git LFS command, using the shell command "! grep -q 'exploit' output.log". The "!" reverses the exit code from "grep", so if the word "exploit" is found, the test should fail. This works in the first test because the command is the last one in the test, so the inverted exit code from "grep" is returned as the exit code from whole test's subshell. However, in the second test several other commands follow this command, and because "set -e" (which is standard at the top of every test) ignores commands' exit codes when they are inverted with "!", the test proceeds even if the word "exploit" is seen in the output log. To resolve this problem we instead use a command pipeline and ensure that when the "grep" succeeds, the exit code from the final command in the pipeline is generated by "false". This successfully causes the test to fail immediately when the word "exploit" is seen in the output log file. Moreover, in both tests we now follow the "grep" check with checks for the presence of a file named "exploit"; this provides a second level of assurance that our "malicious" Git program has not executed. Finally, we add detailed comments regarding specific steps in both tests where the intention and purpose may not be clear just from the context.
1 parent d71c808 commit 11092ef

File tree

2 files changed

+81
-21
lines changed

2 files changed

+81
-21
lines changed

t/t-path.sh

Lines changed: 79 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ begin_test "does not look in current directory for git"
99
reponame="$(basename "$0" ".sh")"
1010
git init "$reponame"
1111
cd "$reponame"
12-
export PATH="$(echo "$PATH" | sed -e "s/:.:/:/g" -e "s/::/:/g")"
1312

14-
printf "#!/bin/sh\necho exploit >&2\n" > git
15-
chmod +x git || true
16-
printf "echo exploit 1>&2\n" > git.bat
13+
cp "$BINPATH/lfstest-badpathcheck$X" "git$X"
1714

18-
# This needs to succeed. If it fails, that could be because our malicious
19-
# "git" is broken but got invoked anyway.
20-
git lfs env > output.log 2>&1
21-
! grep -q 'exploit' output.log
15+
# This should always succeed, even if git-lfs is incorrectly searching for
16+
# executables in the current directory first, because the "git-lfs env"
17+
# command ignores all errors when it runs "git config". So we should always
18+
# pass this step and then, if our malicious Git was executed, detect
19+
# its output below. If this command does fail, something else is wrong.
20+
PATH="$BINPATH" PATHEXT="$X" "git-lfs$X" env >output.log 2>&1
21+
22+
grep "exploit" output.log && false
23+
[ ! -f exploit ]
2224
)
2325
end_test
2426

@@ -30,32 +32,88 @@ begin_test "does not look in current directory for git with credential helper"
3032
setup_remote_repo "$reponame"
3133

3234
clone_repo "$reponame" credentials-1
33-
export PATH="$(echo "$PATH" | sed -e "s/:.:/:/g" -e "s/::/:/g")"
34-
35-
printf "#!/bin/sh\necho exploit >&2\ntouch exploit\n" > git
36-
chmod +x git || true
37-
printf "echo exploit 1>&2\r\necho >exploit" > git.bat
3835

3936
git lfs track "*.dat"
4037
printf abc > z.dat
4138
git add z.dat
4239
git add .gitattributes
43-
git add git git.bat
44-
git commit -m "Add files"
4540

41+
GITPATH="$(dirname "$(command -v git)")"
42+
43+
# We add our malicious Git to the index and then remove it from the
44+
# work tree so it is not found early, before we perform our key test.
45+
# Specifically, our "git push" below will run git-lfs, which then runs
46+
# "git credential", so if we are looking for Git in the current directory
47+
# first when running a credential helper, we will fail at that point
48+
# because our malicious Git will be found first.
49+
#
50+
# We prefer to check for this behavior during our "git-lfs pull" further
51+
# below when we are populating LFS objects into a clone of this repo
52+
# (which contains the malicious Git), so for now we remove the malicious
53+
# Git as soon as possible.
54+
cp "$BINPATH/lfstest-badpathcheck$X" "git$X"
55+
PATH="$BINPATH:$GITPATH" "$GITPATH/git$X" add "git$X"
56+
rm "git$X"
57+
58+
git commit -m "Add files"
4659
git push origin HEAD
4760
cd ..
4861

4962
unset GIT_ASKPASS SSH_ASKPASS
5063

51-
# This needs to succeed. If it fails, that could be because our malicious
52-
# "git" is broken but got invoked anyway.
53-
GIT_LFS_SKIP_SMUDGE=1 clone_repo "$reponame" credentials-2
64+
# When we call "git clone" below, it will run git-lfs as a smudge filter
65+
# during the post-clone checkout phase, and specifically will run git-lfs
66+
# in the newly cloned repository directory which contains a copy of our
67+
# malicious Git. So, if we are looking for Git in the current directory
68+
# first in most cases (and not just when running a credential helper),
69+
# then when git-lfs runs "git config" we will fail at that point because
70+
# our malicious Git will be found first. This occurs even if we specify
71+
# GIT_LFS_SKIP_SMUDGE=1 because git-lfs will still run "git config".
72+
#
73+
# We could ignore errors from clone_repo() and then search for the output
74+
# of our malicious Git in the t-path-credentials-2 directory; however,
75+
# this may be somewhat fragile as clone_repo() performs other steps such
76+
# as changing the current working directory to the new repo clone and
77+
# attempting to run "git config" there.
78+
#
79+
# Instead, since our key check of "git-lfs pull" below will also detect
80+
# the general failure case where we are looking for Git in the current
81+
# directory first when running most commands, we temporarily uninstall
82+
# Git LFS so no smudge filter will execute when "git clone" checks out the
83+
# repository.
84+
#
85+
# We also remove any "exploit" file potentially created by our malicious
86+
# Git in case it was run anywhere in clone_repo(), which may happen if
87+
# PATH contains the "." directory already. Note that we reset PATH
88+
# to contain only the necessary directories in our key "git-lfs pull"
89+
# check below.
90+
git lfs uninstall
91+
clone_repo "$reponame" t-path-credentials-2
92+
rm -f exploit
93+
pushd ..
94+
git lfs install
95+
popd
5496

55-
git lfs pull | tee output.log
97+
# As noted, if we are looking for Git in the current directory first
98+
# only when running a credential helper, then when this runs
99+
# "git credential", it will find our malicious Git in the current directory
100+
# and execute it.
101+
#
102+
# If we are looking for Git in the current directory first when running
103+
# most commands (and not just when running a credential helper), then this
104+
# will also find our malicious Git. However, in this case it will find it
105+
# earlier when we try to run "git config" rather than later when we try
106+
# to run "git credential".
107+
#
108+
# We use a pipeline with "tee" here so as to avoid an early failure in the
109+
# case that our "git-lfs pull" command executes our malicious Git.
110+
# Unlike "git-lfs env" in the other tests, "git-lfs pull" will halt when
111+
# it does not receive the normal output from Git. This in turn halts
112+
# our test due to our use of the "set -e" option, unless we terminate a
113+
# pipeline with successful command like "tee".
114+
PATH="$BINPATH:$GITPATH" PATHEXT="$X" "git-lfs$X" pull 2>&1 | tee output.log
56115

57-
! grep -q 'exploit' output.log
58-
[ ! -f ../exploit ]
116+
grep "exploit" output.log && false
59117
[ ! -f exploit ]
60118
)
61119
end_test

t/testenv.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ set -e
66
UNAME=$(uname -s)
77
IS_WINDOWS=0
88
IS_MAC=0
9+
X=""
910
SHASUM="shasum -a 256"
1011
PATH_SEPARATOR="/"
1112

1213
if [[ $UNAME == MINGW* || $UNAME == MSYS* || $UNAME == CYGWIN* ]]
1314
then
1415
IS_WINDOWS=1
16+
X=".exe"
1517

1618
# Windows might be MSYS2 which does not have the shasum Perl wrapper
1719
# script by default, so use sha256sum directly. MacOS on the other hand

0 commit comments

Comments
 (0)