Skip to content

Commit

Permalink
go: always emit ldflags version information
Browse files Browse the repository at this point in the history
Even when -trimpath is active, emit full ldflags in the version
information ELF note. Vulnerability scanners typically parse ldflags
field to detect main package version, thus binaries that are built
with -trimpath are currently actively evading vulnerability scanners.

Fixes: wolfi-dev#17647
Fixes: golang/go#63432
  • Loading branch information
xnox committed May 2, 2024
1 parent cfdbc42 commit 06578c4
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions go-1.21
4 changes: 4 additions & 0 deletions go-1.21.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ pipeline:
tag: go${{package.version}}
expected-commit: d8392e69973a64d96534d544d1f8ac2defc1bc64

- uses: patch
with:
patches: cmd-go-always-emit-ldflags-version-information.patch

- runs: |
cd src
./make.bash -v
Expand Down
1 change: 1 addition & 0 deletions go-1.22
4 changes: 4 additions & 0 deletions go-1.22.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ pipeline:
tag: go${{package.version}}
expected-commit: dddf0ae40fa0c1223aba191d73a44425a08e1035

- uses: patch
with:
patches: cmd-go-always-emit-ldflags-version-information.patch

- runs: |
cd src
./make.bash -v
Expand Down
3 changes: 3 additions & 0 deletions go-fips-1.21.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ pipeline:
- uses: patch
with:
patches: /home/build/0004-boring-always-enable-access-to-boring.Enabled-functi.patch
- uses: patch
with:
patches: /home/build/cmd-go-always-emit-ldflags-version-information.patch

- runs: |
cd src
Expand Down
35 changes: 35 additions & 0 deletions go-fips-1.21/cmd-go-always-emit-ldflags-version-information.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
From b9f7deea41be0adeff7eee35f29ee096b9f2ff20 Mon Sep 17 00:00:00 2001
From: Dimitri John Ledkov <dimitri.ledkov@chainguard.dev>
Date: Thu, 2 May 2024 18:16:47 +0100
Subject: [PATCH] cmd/go: always emit ldflags version information

Even when -trimpath is active, emit full ldflags in the version
information ELF note. Vulnerability scanners typically parse ldflags
field to detect main package version, thus binaries that are built
with -trimpath are currently actively evading vulnerability scanners.

Fixes: https://github.com/golang/go/issues/63432

Signed-off-by: Dimitri John Ledkov <dimitri.ledkov@chainguard.dev>
---
src/cmd/go/internal/load/pkg.go | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go
index 1549800afb..010d60a9d1 100644
--- a/src/cmd/go/internal/load/pkg.go
+++ b/src/cmd/go/internal/load/pkg.go
@@ -2393,9 +2393,7 @@ func (p *Package) setBuildInfo(ctx context.Context, autoVCS bool) {
// determine whether they may refer to system paths. If we do that, we can
// redact only those paths from the recorded -ldflags setting and still
// record the system-independent parts of the flags.
- if !cfg.BuildTrimpath {
- appendSetting("-ldflags", ldflags)
- }
+ appendSetting("-ldflags", ldflags)
}
if cfg.BuildMSan {
appendSetting("-msan", "true")
--
2.43.0

35 changes: 35 additions & 0 deletions go/cmd-go-always-emit-ldflags-version-information.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
From b9f7deea41be0adeff7eee35f29ee096b9f2ff20 Mon Sep 17 00:00:00 2001
From: Dimitri John Ledkov <dimitri.ledkov@chainguard.dev>
Date: Thu, 2 May 2024 18:16:47 +0100
Subject: [PATCH] cmd/go: always emit ldflags version information

Even when -trimpath is active, emit full ldflags in the version
information ELF note. Vulnerability scanners typically parse ldflags
field to detect main package version, thus binaries that are built
with -trimpath are currently actively evading vulnerability scanners.

Fixes: https://github.com/golang/go/issues/63432

Signed-off-by: Dimitri John Ledkov <dimitri.ledkov@chainguard.dev>
---
src/cmd/go/internal/load/pkg.go | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go
index 1549800afb..010d60a9d1 100644
--- a/src/cmd/go/internal/load/pkg.go
+++ b/src/cmd/go/internal/load/pkg.go
@@ -2393,9 +2393,7 @@ func (p *Package) setBuildInfo(ctx context.Context, autoVCS bool) {
// determine whether they may refer to system paths. If we do that, we can
// redact only those paths from the recorded -ldflags setting and still
// record the system-independent parts of the flags.
- if !cfg.BuildTrimpath {
- appendSetting("-ldflags", ldflags)
- }
+ appendSetting("-ldflags", ldflags)
}
if cfg.BuildMSan {
appendSetting("-msan", "true")
--
2.43.0

0 comments on commit 06578c4

Please sign in to comment.