| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | . "$(dirname "$0")/testlib.sh" |
| 4 | |
| 5 | begin_test "does not look in current directory for git" |
| 6 | ( |
| 7 | set -e |
| 8 | |
| 9 | reponame="$(basename "$0" ".sh")" |
| 10 | git init "$reponame" |
| 11 | cd "$reponame" |
| 12 | |
| 13 | cp "$BINPATH/lfstest-badpathcheck$X" "git$X" |
| 14 | |
| 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 ] |
| 24 | ) |
| 25 | end_test |
| 26 | |
| 27 | begin_test "does not look in current directory for git with credential helper" |
| 28 | ( |
| 29 | set -e |
| 30 | |
| 31 | reponame="$(basename "$0" ".sh")-credentials" |
| 32 | setup_remote_repo "$reponame" |
| 33 | |
| 34 | clone_repo "$reponame" credentials-1 |
| 35 | |
| 36 | git lfs track "*.dat" |
| 37 | printf abc > z.dat |
| 38 | git add z.dat |
| 39 | git add .gitattributes |
| 40 | |
| 41 | GITPATH="$(dirname "$(command -v git)")" |
| 42 | SHELLPATH="$(dirname "$(command -v sh)")" |
| 43 | |
| 44 | # We add our malicious Git to the index and then remove it from the |
| 45 | # work tree so it is not found early, before we perform our key test. |
| 46 | # Specifically, our "git push" below will run git-lfs, which then runs |
| 47 | # "git credential", so if we are looking for Git in the current directory |
| 48 | # first when running a credential helper, we will fail at that point |
| 49 | # because our malicious Git will be found first. |
| 50 | # |
| 51 | # We prefer to check for this behavior during our "git-lfs pull" further |
| 52 | # below when we are populating LFS objects into a clone of this repo |
| 53 | # (which contains the malicious Git), so for now we remove the malicious |
| 54 | # Git as soon as possible. |
| 55 | # |
| 56 | # As of Go 1.19 we also need to specify the GODEBUG environment variable |
| 57 | # with a value of "execerrdot=0" in order to avoid occasional failures |
| 58 | # our "git add" command below. These failures occur due to a specific |
| 59 | # set of conditions. First, if the last-modified time of the .git/index |
| 60 | # file is within a second of that of z.dat, the "git add" command will |
| 61 | # refresh the Git index (assuming Git was compiled with USE_NSEC=0, as |
| 62 | # appears to be the case for Git for Windows), and Git LFS will be invoked |
| 63 | # to "clean" the z.dat file again. |
| 64 | # |
| 65 | # If that occurs, then when Git LFS runs it looks for Git, and until we |
| 66 | # revise Git LFS to rely on Go's os/exec package to not execute programs |
| 67 | # found in the current working directory (as described in |
| 68 | # https://go.dev/blog/path-security), the os/exec package will detect our |
| 69 | # malicious Git program in the current working directory and report an |
| 70 | # error. This occurs when Git LFS first initializes a new exec.Cmd |
| 71 | # structure, even though Git LFS would then locate the true Git executable |
| 72 | # from our custom PATH and reset the Path member of the Cmd structure |
| 73 | # before trying to execute the program. |
| 74 | # |
| 75 | # Since we explicitly test Git LFS's avoidance of programs in the current |
| 76 | # working directory using the "git-lfs pull" command further below, here |
| 77 | # we just want "git add" to succeed, and so for the time being we disable |
| 78 | # Go's new security checks for this command only. We will revisit this |
| 79 | # when we address the larger issue of re-adopting Go's own logic for |
| 80 | # locating executable programs. |
| 81 | cp "$BINPATH/lfstest-badpathcheck$X" "git$X" |
| 82 | GODEBUG=execerrdot=0 \ |
| 83 | PATH="$BINPATH:$GITPATH:$SHELLPATH" "$GITPATH/git$X" add "git$X" |
| 84 | rm "git$X" |
| 85 | |
| 86 | git commit -m "Add files" |
| 87 | git push origin HEAD |
| 88 | cd .. |
| 89 | |
| 90 | unset GIT_ASKPASS SSH_ASKPASS |
| 91 | |
| 92 | # When we call "git clone" below, it will run git-lfs as a smudge filter |
| 93 | # during the post-clone checkout phase, and specifically will run git-lfs |
| 94 | # in the newly cloned repository directory which contains a copy of our |
| 95 | # malicious Git. So, if we are looking for Git in the current directory |
| 96 | # first in most cases (and not just when running a credential helper), |
| 97 | # then when git-lfs runs "git config" we will fail at that point because |
| 98 | # our malicious Git will be found first. This occurs even if we specify |
| 99 | # GIT_LFS_SKIP_SMUDGE=1 because git-lfs will still run "git config". |
| 100 | # |
| 101 | # We could ignore errors from clone_repo() and then search for the output |
| 102 | # of our malicious Git in the t-path-credentials-2 directory; however, |
| 103 | # this may be somewhat fragile as clone_repo() performs other steps such |
| 104 | # as changing the current working directory to the new repo clone and |
| 105 | # attempting to run "git config" there. |
| 106 | # |
| 107 | # Instead, since our key check of "git-lfs pull" below will also detect |
| 108 | # the general failure case where we are looking for Git in the current |
| 109 | # directory first when running most commands, we temporarily uninstall |
| 110 | # Git LFS so no smudge filter will execute when "git clone" checks out the |
| 111 | # repository. |
| 112 | # |
| 113 | # We also remove any "exploit" file potentially created by our malicious |
| 114 | # Git in case it was run anywhere in clone_repo(), which may happen if |
| 115 | # PATH contains the "." directory already. Note that we reset PATH |
| 116 | # to contain only the necessary directories in our key "git-lfs pull" |
| 117 | # check below. |
| 118 | git lfs uninstall |
| 119 | clone_repo "$reponame" t-path-credentials-2 |
| 120 | rm -f exploit |
| 121 | pushd .. |
| 122 | git lfs install |
| 123 | popd |
| 124 | |
| 125 | # As noted, if we are looking for Git in the current directory first |
| 126 | # only when running a credential helper, then when this runs |
| 127 | # "git credential", it will find our malicious Git in the current directory |
| 128 | # and execute it. |
| 129 | # |
| 130 | # If we are looking for Git in the current directory first when running |
| 131 | # most commands (and not just when running a credential helper), then this |
| 132 | # will also find our malicious Git. However, in this case it will find it |
| 133 | # earlier when we try to run "git config" rather than later when we try |
| 134 | # to run "git credential". |
| 135 | # |
| 136 | # We use a pipeline with "tee" here so as to avoid an early failure in the |
| 137 | # case that our "git-lfs pull" command executes our malicious Git. |
| 138 | # Unlike "git-lfs env" in the other tests, "git-lfs pull" will halt when |
| 139 | # it does not receive the normal output from Git. This in turn halts |
| 140 | # our test due to our use of the "set -e" option, unless we terminate a |
| 141 | # pipeline with successful command like "tee". |
| 142 | PATH="$BINPATH:$GITPATH" PATHEXT="$X" "git-lfs$X" pull 2>&1 | tee output.log |
| 143 | |
| 144 | grep "exploit" output.log && false |
| 145 | [ ! -f exploit ] |
| 146 | ) |
| 147 | end_test |
| 148 | |
| 149 | begin_test "does not look in current directory for wrong binary using PATHEXT" |
| 150 | ( |
| 151 | set -e |
| 152 | |
| 153 | # Windows is the only platform where Go searches for executable files |
| 154 | # by appending file extensions from PATHEXT. |
| 155 | [ "$IS_WINDOWS" -eq 0 ] && exit 0 |
| 156 | |
| 157 | reponame="$(basename "$0" ".sh")-notfound" |
| 158 | git init "$reponame" |
| 159 | cd "$reponame" |
| 160 | |
| 161 | # Go on Windows always looks in the current directory first when creating |
| 162 | # a command handler, so we need a dummy git.exe for it to find there since |
| 163 | # we will restrict PATH to exclude the real Git when we run "git-lfs env" |
| 164 | # below. If our git-lfs incorrectly proceeds to run the command handler |
| 165 | # despite not finding Git in PATH either, Go may then search for a file |
| 166 | # named "." with any path extension from PATHEXT and execute that file |
| 167 | # instead, so we create a malicious file named "..exe" to check this case. |
| 168 | touch "git$X" |
| 169 | cp "$BINPATH/lfstest-badpathcheck$X" ".$X" |
| 170 | |
| 171 | # This should always succeed, even if git-lfs is incorrectly searching for |
| 172 | # executables in the current directory first, because the "git-lfs env" |
| 173 | # command ignores all errors when it runs "git config". So we should always |
| 174 | # pass this step and then, if our malicious program was executed, detect |
| 175 | # its output below. If this command does fail, something else is wrong. |
| 176 | PATH="$BINPATH" PATHEXT="$X" "git-lfs$X" env >output.log 2>&1 |
| 177 | |
| 178 | grep "exploit" output.log && false |
| 179 | [ ! -f exploit ] |
| 180 | ) |
| 181 | end_test |