From 08003f6440deafc4e193b159c4acece64f7864b1 Mon Sep 17 00:00:00 2001 From: Andrew Donnellan Date: Thu, 12 Dec 2019 17:04:58 +1100 Subject: pkg/vcs: Unset various git environment variables when invoking git If you try to run git-using tests while the GIT_DIR environment variable (and GIT_WORK_TREE, etc) happens to be set, the tests are going to do fun and exciting things on a repository that isn't the test repository it tries to set up. As it turns out, if you try to run "make test" using git rebase -x, you'll end up with GIT_DIR set to the syzkaller tree. Hilarity ensues. Unset GIT_DIR, GIT_WORK_TREE and a few other environment variables when invoking git - that way it'll default to looking at the working directory that we have given it, which is what we expect. Signed-off-by: Andrew Donnellan --- pkg/vcs/git.go | 23 ++++++++++++++++++++++- pkg/vcs/git_test_util.go | 6 +++++- pkg/vcs/vcs.go | 5 +++++ 3 files changed, 32 insertions(+), 2 deletions(-) (limited to 'pkg') diff --git a/pkg/vcs/git.go b/pkg/vcs/git.go index a42f12edc..27064de7b 100644 --- a/pkg/vcs/git.go +++ b/pkg/vcs/git.go @@ -33,6 +33,26 @@ func newGit(dir string, ignoreCC map[string]bool) *git { } } +func filterEnv() []string { + // We have to filter various git environment variables - if + // these variables are set (e.g. if a test is being run as + // part of a rebase) we're going to be acting on some other + // repository (e.g the syzkaller tree itself) rather than the + // intended repo. + env := os.Environ() + for i := 0; i < len(env); i++ { + if strings.HasPrefix(env[i], "GIT_DIR") || + strings.HasPrefix(env[i], "GIT_WORK_TREE") || + strings.HasPrefix(env[i], "GIT_INDEX_FILE") || + strings.HasPrefix(env[i], "GIT_OBJECT_DIRECTORY") { + env = append(env[:i], env[i+1:]...) + i-- + } + } + + return env +} + func (git *git) Poll(repo, branch string) (*Commit, error) { git.reset() origin, err := git.git("remote", "get-url", "origin") @@ -315,6 +335,7 @@ func (git *git) fetchCommits(since, base, user, domain string, greps []string, f args = append(args, base) cmd := exec.Command("git", args...) cmd.Dir = git.dir + cmd.Env = filterEnv() if err := osutil.Sandbox(cmd, true, false); err != nil { return nil, err } @@ -359,7 +380,7 @@ func (git *git) fetchCommits(since, base, user, domain string, greps []string, f } func (git *git) git(args ...string) ([]byte, error) { - return runSandboxed(git.dir, "git", args...) + return runSandboxedEnv(git.dir, "git", filterEnv(), args...) } func splitEmail(email string) (user, domain string, err error) { diff --git a/pkg/vcs/git_test_util.go b/pkg/vcs/git_test_util.go index 86713bac0..2f050a2b2 100644 --- a/pkg/vcs/git_test_util.go +++ b/pkg/vcs/git_test_util.go @@ -25,7 +25,11 @@ type TestRepo struct { } func (repo *TestRepo) Git(args ...string) { - if _, err := osutil.RunCmd(time.Minute, repo.Dir, "git", args...); err != nil { + cmd := osutil.Command("git", args...) + cmd.Dir = repo.Dir + cmd.Env = filterEnv() + + if _, err := osutil.Run(time.Minute, cmd); err != nil { repo.t.Fatal(err) } } diff --git a/pkg/vcs/vcs.go b/pkg/vcs/vcs.go index 23c906087..647a00f42 100644 --- a/pkg/vcs/vcs.go +++ b/pkg/vcs/vcs.go @@ -167,8 +167,13 @@ func CheckCommitHash(hash string) bool { } func runSandboxed(dir, command string, args ...string) ([]byte, error) { + return runSandboxedEnv(dir, command, nil, args...) +} + +func runSandboxedEnv(dir, command string, env []string, args ...string) ([]byte, error) { cmd := osutil.Command(command, args...) cmd.Dir = dir + cmd.Env = env if err := osutil.Sandbox(cmd, true, false); err != nil { return nil, err } -- cgit mrf-deployment