New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: respect environment LDFLAGS and strip the build path #1994

Merged
merged 3 commits into from Jan 2, 2019

Conversation

Projects
None yet
2 participants
@eli-schwartz
Copy link
Contributor

eli-schwartz commented Dec 31, 2018

golang does not natively respect LDFLAGS, but you can pass them on the command line using -ldflags=-extldflags=...
This is important for distributions, in order to provide common functionality such as hardening flags.

Also strip the prefixed root source directory from the embedded source file paths. This is not important information for the debugger, which should only care about paths relative to $GOPATH, and results in less build environment metadata leaking into the final binary. (This also aids in reproducible builds when using different build directories, see e.g. golang/go#16860)

@eli-schwartz

This comment has been minimized.

Copy link
Contributor

eli-schwartz commented Dec 31, 2018

Before:

$ checksec -f bin/hub
RELRO           STACK CANARY      NX            PIE             RPATH      RUNPATH	Symbols		FORTIFY	Fortified	Fortifiable  FILE
Partial RELRO   Canary found      NX enabled    PIE enabled     No RPATH   No RUNPATH   No Symbols       Yes	0		2	bin/hub
$ strings bin/hub| grep $USER
/home/eschwartz/git/hub/main.go
/home/eschwartz/git/hub/.gopath/src/github.com/github/hub/commands/version.go
/home/eschwartz/git/hub/.gopath/src/github.com/github/hub/commands/utils.go
/home/eschwartz/git/hub/.gopath/src/github.com/github/hub/commands/sync.go
[...]

After, with LDFLAGS="-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now" (note the presence of relro)

$ checksec -f bin/hub
RELRO           STACK CANARY      NX            PIE             RPATH      RUNPATH	Symbols		FORTIFY	Fortified	Fortifiable  FILE
Full RELRO      Canary found      NX enabled    PIE enabled     No RPATH   No RUNPATH   No Symbols       Yes	0		2	bin/hub
$ strings bin/hub| grep $USER
$ 
@mislav

This comment has been minimized.

Copy link
Member

mislav commented Jan 2, 2019

Thank you! I've pushed some tweaks. @eli-schwartz care to review please?

@eli-schwartz

This comment has been minimized.

Copy link
Contributor

eli-schwartz commented Jan 2, 2019

I've been trying to think what to do for go 1.9, the problem is that -gcflags=all= on go>=1.10 is equivalent to -gcflags= on go 1.9 and using the latter everywhere causes newer versions of go to not trim all paths (only some).

From the release notes:

The go build -asmflags, -gcflags, -gccgoflags, and -ldflags options now apply by default only to the packages listed directly on the command line. For example, go build -gcflags=-m mypkg passes the compiler the -m flag when building mypkg but not its dependencies. The new, more general form -asmflags=pattern=flags (and similarly for the others) applies the flags only to the packages matching the pattern. For example: go install -ldflags=cmd/gofmt=-X=main.version=1.2.3 cmd/... installs all the commands matching cmd/... but only applies the -X option to the linker flags for cmd/gofmt. For more details, see go help build.

I think the solution is to do string comparison on the output of go version to choose between the two.

eli-schwartz and others added some commits Dec 31, 2018

build: respect environment LDFLAGS and strip the build path
golang does not natively respect LDFLAGS, but you can pass them on the
command line using -ldflags=-extldflags=...
This is important for distributions, in order to provide common
functionality such as hardening flags.

Also strip the prefixed root source directory from the embedded source
file paths. This is not important information for the debugger, which
should only care about paths relative to $GOPATH, and results in less
build environment metadata leaking into the final binary. (This also
aids in reproducible builds when using different build directories, see
e.g. golang/go#16860)
Support `trimpath` for Go v1.8/1.9
Go 1.10 release notes:

> The go build -asmflags, -gcflags, -gccgoflags, and -ldflags options
> now apply by default only to the packages listed directly on the
> command line. For example, go build -gcflags=-m mypkg passes the
> compiler the -m flag when building mypkg but not its dependencies. The
> new, more general form -asmflags=pattern=flags (and similarly for the
> others) applies the flags only to the packages matching the pattern.
> For example: go install -ldflags=cmd/gofmt=-X=main.version=1.2.3
> cmd/... installs all the commands matching cmd/... but only applies
> the -X option to the linker flags for cmd/gofmt. For more details, see
> go help build.

@mislav mislav force-pushed the eli-schwartz:goflags branch from fa9a68c to dd5388f Jan 2, 2019

@mislav mislav merged commit f56701c into github:master Jan 2, 2019

1 check was pending

continuous-integration/travis-ci/pr The Travis CI build is in progress
Details
@eli-schwartz

This comment has been minimized.

Copy link
Contributor

eli-schwartz commented Jan 2, 2019

Thanks for polishing that up.

@eli-schwartz eli-schwartz deleted the eli-schwartz:goflags branch Jan 2, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment