Skip to content

Commit e8e7fac

Browse files
rscrolandshoemaker
authored andcommitted
[release-branch.go1.15-security] cmd/go: pass resolved CC, GCCGO to cgo
This makes sure the go command and cgo agree about exactly which compiler is being used. This issue was reported by RyotaK. Fixes CVE-2021-3115. Change-Id: If171c5c8b2523efb5ea2d957e5ad1380a038149c Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/949416 Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Jay Conrod <[email protected]> (cherry picked from commit 4cf399ca38587a6e4a3e85b494cd9a9b4cc53378) Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/955293 Reviewed-by: Katie Hockman <[email protected]>
1 parent 5c8fd72 commit e8e7fac

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/cmd/go/internal/work/action.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ type Builder struct {
5656
id sync.Mutex
5757
toolIDCache map[string]string // tool name -> tool ID
5858
buildIDCache map[string]string // file name -> build ID
59+
60+
cgoEnvOnce sync.Once
61+
cgoEnvCache []string
5962
}
6063

6164
// NOTE: Much of Action would not need to be exported if not for test.

src/cmd/go/internal/work/exec.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,10 +1080,8 @@ func (b *Builder) vet(a *Action) error {
10801080
return err
10811081
}
10821082

1083-
env := b.cCompilerEnv()
1084-
if cfg.BuildToolchainName == "gccgo" {
1085-
env = append(env, "GCCGO="+BuildToolchain.compiler())
1086-
}
1083+
// TODO(rsc): Why do we pass $GCCGO to go vet?
1084+
env := b.cgoEnv()
10871085

10881086
p := a.Package
10891087
tool := VetTool
@@ -2014,6 +2012,24 @@ func (b *Builder) cCompilerEnv() []string {
20142012
return []string{"TERM=dumb"}
20152013
}
20162014

2015+
// cgoEnv returns environment variables to set when running cgo.
2016+
// Some of these pass through to cgo running the C compiler,
2017+
// so it includes cCompilerEnv.
2018+
func (b *Builder) cgoEnv() []string {
2019+
b.cgoEnvOnce.Do(func() {
2020+
cc, err := exec.LookPath(b.ccExe()[0])
2021+
if err != nil || filepath.Base(cc) == cc { // reject relative path
2022+
cc = "/missing-cc"
2023+
}
2024+
gccgo := GccgoBin
2025+
if filepath.Base(gccgo) == gccgo { // reject relative path
2026+
gccgo = "/missing-gccgo"
2027+
}
2028+
b.cgoEnvCache = append(b.cCompilerEnv(), "CC="+cc, "GCCGO="+gccgo)
2029+
})
2030+
return b.cgoEnvCache
2031+
}
2032+
20172033
// mkdir makes the named directory.
20182034
func (b *Builder) Mkdir(dir string) error {
20192035
// Make Mkdir(a.Objdir) a no-op instead of an error when a.Objdir == "".
@@ -2603,13 +2619,13 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
26032619
// along to the host linker. At this point in the code, cgoLDFLAGS
26042620
// consists of the original $CGO_LDFLAGS (unchecked) and all the
26052621
// flags put together from source code (checked).
2606-
cgoenv := b.cCompilerEnv()
2622+
cgoenv := b.cgoEnv()
26072623
if len(cgoLDFLAGS) > 0 {
26082624
flags := make([]string, len(cgoLDFLAGS))
26092625
for i, f := range cgoLDFLAGS {
26102626
flags[i] = strconv.Quote(f)
26112627
}
2612-
cgoenv = []string{"CGO_LDFLAGS=" + strings.Join(flags, " ")}
2628+
cgoenv = append(cgoenv, "CGO_LDFLAGS="+strings.Join(flags, " "))
26132629
}
26142630

26152631
if cfg.BuildToolchainName == "gccgo" {
@@ -2824,7 +2840,7 @@ func (b *Builder) dynimport(a *Action, p *load.Package, objdir, importGo, cgoExe
28242840
if p.Standard && p.ImportPath == "runtime/cgo" {
28252841
cgoflags = []string{"-dynlinker"} // record path to dynamic linker
28262842
}
2827-
return b.run(a, p.Dir, p.ImportPath, b.cCompilerEnv(), cfg.BuildToolexec, cgoExe, "-dynpackage", p.Name, "-dynimport", dynobj, "-dynout", importGo, cgoflags)
2843+
return b.run(a, p.Dir, p.ImportPath, b.cgoEnv(), cfg.BuildToolexec, cgoExe, "-dynpackage", p.Name, "-dynimport", dynobj, "-dynout", importGo, cgoflags)
28282844
}
28292845

28302846
// Run SWIG on all SWIG input files.

0 commit comments

Comments
 (0)