Skip to content

Commit d252fdd

Browse files
Bryan C. Millscherrymui
Bryan C. Mills
authored andcommitted
[release-branch.go1.18] cmd/go: omit build metadata that may contain system paths when -trimpath is set
CGO flag variables often include system paths for header files and compiled libraries. The point of -trimpath is to avoid dependending on system paths, so stamping these variables is counterproductive. Moreover, the point of stamping build information is to improve reproducibility. Since we don't also stamp the versions of C compilers, headers, and libraries used in a cgo build, only the most trivial cgo programs can be faithfully reproduced from the stamped information. Likewise, the -ldflags flag may include system-specific paths, particularly if external linking is in use. For now, we omit -ldflags entirely; however, in the future we may instead want to parse and redact the individual flags. Updates #52372. Fixes #53119. Change-Id: I73318a01cce4371d66955b3261fc7ee58d4b33dd Reviewed-on: https://go-review.googlesource.com/c/go/+/409174 TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Bryan Mills <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Bryan Mills <[email protected]> (cherry picked from commit a6e5be0) Reviewed-on: https://go-review.googlesource.com/c/go/+/414794 Reviewed-by: Nooras Saba‎ <[email protected]>
1 parent 4782f42 commit d252fdd

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/cmd/go/internal/load/pkg.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2329,7 +2329,17 @@ func (p *Package) setBuildInfo(includeVCS bool) {
23292329
appendSetting("-gcflags", BuildGcflags.String())
23302330
}
23312331
if BuildLdflags.present {
2332-
appendSetting("-ldflags", BuildLdflags.String())
2332+
// https://go.dev/issue/52372: only include ldflags if -trimpath is not set,
2333+
// since it can include system paths through various linker flags (notably
2334+
// -extar, -extld, and -extldflags).
2335+
//
2336+
// TODO: since we control cmd/link, in theory we can parse ldflags to
2337+
// determine whether they may refer to system paths. If we do that, we can
2338+
// redact only those paths from the recorded -ldflags setting and still
2339+
// record the system-independent parts of the flags.
2340+
if !cfg.BuildTrimpath {
2341+
appendSetting("-ldflags", BuildLdflags.String())
2342+
}
23332343
}
23342344
if cfg.BuildMSan {
23352345
appendSetting("-msan", "true")
@@ -2345,7 +2355,14 @@ func (p *Package) setBuildInfo(includeVCS bool) {
23452355
cgo = "1"
23462356
}
23472357
appendSetting("CGO_ENABLED", cgo)
2348-
if cfg.BuildContext.CgoEnabled {
2358+
// https://go.dev/issue/52372: only include CGO flags if -trimpath is not set.
2359+
// (If -trimpath is set, it is possible that these flags include system paths.)
2360+
// If cgo is involved, reproducibility is already pretty well ruined anyway,
2361+
// given that we aren't stamping header or library versions.
2362+
//
2363+
// TODO(bcmills): perhaps we could at least parse the flags and stamp the
2364+
// subset of flags that are known not to be paths?
2365+
if cfg.BuildContext.CgoEnabled && !cfg.BuildTrimpath {
23492366
for _, name := range []string{"CGO_CFLAGS", "CGO_CPPFLAGS", "CGO_CXXFLAGS", "CGO_LDFLAGS"} {
23502367
appendSetting(name, cfg.Getenv(name))
23512368
}

src/cmd/go/testdata/script/version_build_settings.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,34 @@ go build
4747
go version -m m$GOEXE
4848
stdout '^\tbuild\tCGO_ENABLED=0$'
4949
! stdout CGO_CPPFLAGS|CGO_CFLAGS|CGO_CXXFLAGS|CGO_LDFLAGS
50+
5051
[cgo] env CGO_ENABLED=1
5152
[cgo] env CGO_CPPFLAGS=-DFROM_CPPFLAGS=1
5253
[cgo] env CGO_CFLAGS=-DFROM_CFLAGS=1
5354
[cgo] env CGO_CXXFLAGS=-DFROM_CXXFLAGS=1
5455
[cgo] env CGO_LDFLAGS=-L/extra/dir/does/not/exist
55-
[cgo] go build
56+
[cgo] go build '-ldflags=all=-linkmode=external -extldflags=-L/bonus/dir/does/not/exist'
5657
[cgo] go version -m m$GOEXE
58+
[cgo] stdout '^\tbuild\t-ldflags="all=-linkmode=external -extldflags=-L/bonus/dir/does/not/exist"$'
5759
[cgo] stdout '^\tbuild\tCGO_ENABLED=1$'
5860
[cgo] stdout '^\tbuild\tCGO_CPPFLAGS=-DFROM_CPPFLAGS=1$'
5961
[cgo] stdout '^\tbuild\tCGO_CFLAGS=-DFROM_CFLAGS=1$'
6062
[cgo] stdout '^\tbuild\tCGO_CXXFLAGS=-DFROM_CXXFLAGS=1$'
6163
[cgo] stdout '^\tbuild\tCGO_LDFLAGS=-L/extra/dir/does/not/exist$'
6264

65+
# https://go.dev/issue/52372: a cgo-enabled binary should not be stamped with
66+
# CGO_ flags that contain paths.
67+
[cgo] env CGO_ENABLED=1
68+
[cgo] env CGO_CPPFLAGS=-DFROM_CPPFLAGS=1
69+
[cgo] env CGO_CFLAGS=-DFROM_CFLAGS=1
70+
[cgo] env CGO_CXXFLAGS=-DFROM_CXXFLAGS=1
71+
[cgo] env CGO_LDFLAGS=-L/extra/dir/does/not/exist
72+
[cgo] go build -trimpath '-ldflags=all=-linkmode=external -extldflags=-L/bonus/dir/does/not/exist'
73+
[cgo] go version -m m$GOEXE
74+
[cgo] ! stdout '/extra/dir/does/not/exist'
75+
[cgo] ! stdout '/bonus/dir/does/not/exist'
76+
[cgo] stdout '^\tbuild\tCGO_ENABLED=1$'
77+
6378
-- go.mod --
6479
module example.com/m
6580

0 commit comments

Comments
 (0)