Skip to content

cmd/go: Can't override linkmode when setting buildmode=pie #44480

Description

@Foxboron

What version of Go are you using (go version)?

$ go version
go version go1.16 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/fox/.cache/go-build"
GOENV="/home/fox/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/fox/.go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/fox/.go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1989593130=/tmp/go-build -gno-record-gcc-switches"

What did you do?

The commit 6c0135d breaks RELRO as there is no way to override which linker to use when -buildmode=pie is set and the target is linux/amd64. This makes it impossible to build hardened binaries on most Linux platforms.

I'm not sure if it's intentional to force the internal linker? Shoulnd't -linkmode=external ensure we don't use the internal linker?

https://www.redhat.com/en/blog/hardening-elf-binaries-using-relocation-read-only-relro

What did you expect to see?

λ fzf » export GOFLAGS="-buildmode=pie -trimpath -ldflags=-linkmode=external"
λ fzf » export CGO_LDFLAGS="-Wl,-z,relro,-z,now"

λ fzf » CGO_ENABLED=0 go build -o fzf                                         
# github.com/junegunn/fzf
loadinternal: cannot find runtime/cgo
λ fzf » checksec --file=./fzf         
RELRO           STACK CANARY      NX            PIE             RPATH      RUNPATH	Symbols		FORTIFY	Fortified	Fortifiable	FILE
Partial RELRO   No canary found   NX enabled    PIE enabled     No RPATH   No RUNPATH   5316) Symbols	  No	0		0	./fzf

λ fzf » CGO_ENABLED=1 go build -o fzf           
λ fzf » checksec --file=./fzf                   
RELRO           STACK CANARY      NX            PIE             RPATH      RUNPATH	Symbols		FORTIFY	Fortified	Fortifiable	FILE
Full RELRO      Canary found      NX enabled    PIE enabled     No RPATH   No RUNPATH   5396) Symbols	  No	0		2	./fzf

What did you see instead?

λ fzf » export GOFLAGS="-buildmode=pie -trimpath -ldflags=-linkmode=external"
λ fzf » export CGO_LDFLAGS="-Wl,-z,relro,-z,now"


λ fzf » CGO_ENABLED=0 go build -o fzf 
# github.com/junegunn/fzf
loadinternal: cannot find runtime/cgo
λ fzf » checksec --file=./fzf         
RELRO           STACK CANARY      NX            PIE             RPATH      RUNPATH	Symbols		FORTIFY	Fortified	Fortifiable	FILE
Partial RELRO   No canary found   NX enabled    PIE enabled     No RPATH   No RUNPATH   4594) Symbols	  No	0		0	./fzf

λ fzf » CGO_ENABLED=1 go build -o fzf 
# github.com/junegunn/fzf
loadinternal: cannot find runtime/cgo
λ fzf » checksec --file=./fzf         
RELRO           STACK CANARY      NX            PIE             RPATH      RUNPATH	Symbols		FORTIFY	Fortified	Fortifiable	FILE
Partial RELRO   No canary found   NX enabled    PIE enabled     No RPATH   No RUNPATH   4594) Symbols	  No	0		0	./fzf

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions