Skip to content
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

eli-schwartz
Copy link
Contributor

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
Copy link
Contributor Author

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
Copy link
Owner

mislav commented Jan 2, 2019

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

@eli-schwartz
Copy link
Contributor Author

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 3 commits January 2, 2019 17:00
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)
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 merged commit f56701c into mislav:master Jan 2, 2019
@eli-schwartz
Copy link
Contributor Author

eli-schwartz commented Jan 2, 2019

Thanks for polishing that up.

@eli-schwartz eli-schwartz deleted the goflags branch January 2, 2019 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants