FS#60928 - [go] [go-pie] the default go installation should be pie-enabled
Attached to Project:
Community Packages
Opened by Eli Schwartz (eschwartz) - Wednesday, 28 November 2018, 21:21 GMT
Last edited by Morten Linderud (Foxboron) - Monday, 18 May 2020, 17:45 GMT
Opened by Eli Schwartz (eschwartz) - Wednesday, 28 November 2018, 21:21 GMT
Last edited by Morten Linderud (Foxboron) - Monday, 18 May 2020, 17:45 GMT
|
Details
We try to recommend the use of the go-pie package, but the
default go package does not use pie. The only reason we have
both, IIRC, is because some packages would not compile with
the PIE-enabled one.
This being the case, a preferable setup would be to have: - a go package which provides/replaces go-pie and builds PIE-enabled executables - a go-nopie package which provides/conflicts go and builds boring non-PIE executables Other considerations: The latest versions of go support environment $GOFLAGS, where users (and PKGBUILDs) can define e.g. -buildmode=default to disable PIE. |
This task depends upon
Closed by Morten Linderud (Foxboron)
Monday, 18 May 2020, 17:45 GMT
Reason for closing: Implemented
Additional comments about closing: Fixed with go-2:1.14.3-1
Monday, 18 May 2020, 17:45 GMT
Reason for closing: Implemented
Additional comments about closing: Fixed with go-2:1.14.3-1
"ssp/pie enforced by gcc at worst exposes badly written makefiles"
At the same time, we want to enforce this (where it works) on a distribution level, as a default. This is primarily targeted at packaging, which is an environment we can control...
As I understand it, GOFLAGS are read by the go compiler itself, whereas CFLAGS/LDFLAGS are a convention read by Makefiles and passed as arguments *to* gcc... so there's more benefit to sneaking around the environment variable for gcc and badly written Makefiles than there is for golang.
...
This being the case, maybe it does make more sense to add first-class support for GOFLAGS in makepkg itself, then set this as a distribution packaging default. Benefits: Enabling PIE, enabling debug support via OPTIONS=(), possibly configuring optimization levels, etc.
I can put in the work to push this into makepkg.
How this works:
- makepkg.conf will contain $FOO and $FOO_DEBUG settings for a given type of flag
-- $FOO (or GOFLAGS) contains the default flags passed for all packages
-- $FOO_DEBUG (or GOFLAGS_DEBUG) contains additional flags appended as GOFLAGS="$GOFLAGS $GOFLAGS_DEBUG", iff the user has debug packages enabled. The flags are cumulative and can override the default flags, but both will be passed.
- makepkg will handle the process of exporting the $FOO (or GOFLAGS) variable.
-- If options=(!buildflags) is set in the PKGBUILD, all build flags will be unset instead. More fine-grained control can be achieved by the PKGBUILD itself setting: `export GOFLAGS="${GOFLAGS/--bad-flag/}"` to filter out bad flags.
-- If the package is debug-enabled, gcc's CFLAGS will also receive the (not configurable) flag -fdebug-prefix-map=$srcdir=${DBGSRCDIR:-/usr/src/debug} which ensures that debug data references to source code files will point to source code packaged in /usr/src/debug/. This assumes that makepkg's "strip" routine can find the source files referenced via the keywords "DW_AT_name"/"DW_AT_comp_dir" (from readelf --debug-dump). I can find DW_AT_name in a sample golang binary, but it points to e.g. "github.com/keybase/client/go/vendor/github.com/keybase/go-crypto/ssh/terminal.keyCtrlC" which appears to be a ${GOPATH}-relative import path for a symbol contained inside .../ssh/terminal/terminal.go, which is not at all similar. So, how does golang work instead?
...
So the question becomes, what should go inside those variables in order to ensure the best out-of-the-box experience from a distribution packaging perspective?
As I said on IRC, in my opinion we should ditch go-pie completely, as it's just too simplistic attempt to fix everything, as shown by snapd, containerd or gopass that segfault; instead, just as Eli said, we should make GOFLAGS first class citizen in makepkg.conf and then think about fixing packages that ignore it somehow.
Foxboron said he has figured out what should be in GOFLAGS to make PIE default without breaking projects above.
(Other story is that go-pie is just unsupported upstream as is, which is not what we aim for.)
I know the GOFLAGS for PIE and relro, along with having a default value for trimpath. I can't make any promises regarding breakage.
For documentation:
export GOFLAGS="-buildmode=pie -gcflags=all=-trimpath=${PWD} -asmflags=all=-trimpath=${PWD} -ldflags=-extldflags=-zrelro -ldflags=-extldflags=-znow"
GOFLAGS="-buildmode=pie -gcflags=all=-trimpath=${PWD} -asmflags=all=-trimpath=${PWD} -ldflags=-extldflags=-zrelro -ldflags=-extldflags=-znow -ldflags=-extldflags=-Wl -ldflags=-extldflags=-O1 -ldflags=-extldflags=--sort-common -ldflags=-extldflags=--as-needed"
(-Wl,comma,separated,values where "comma" and "separated" and "values" are three flags that /usr/bin/ld will receive.)
@Foxboron, does trimpath do something w.r.t. debugging symbols? If I understand correctly, golang does not recommend using gdb to debug code, but recommends something called "delve" instead. Is there anything we need to do in order to get that to work nicely OOTB, like packaging detached debugging source files for the debugger alongside the detached debugging symbols?
I assume other Linux distros will face the same challenge as well.
containerd W: ELF file ('usr/bin/containerd-shim') lacks FULL RELRO, check LDFLAGS.
containerd W: ELF file ('usr/bin/containerd-shim-runc-v1') lacks FULL RELRO, check LDFLAGS.
containerd W: ELF file ('usr/bin/containerd-shim') lacks PIE.
containerd W: ELF file ('usr/bin/containerd-shim-runc-v1') lacks PIE.
containerd W: Dependency included and not needed ('runc')
Also test suite fails but I assume that Makefile is doing something stupid:
==> Starting check()...
🇩 test
go test: ldflags flag may be set only once
run "go help test" or "go help testflag" for more information
make: *** [Makefile:150: test] Error 2
hub W: ELF file ('usr/bin/hub') lacks FULL RELRO, check LDFLAGS.
But at least no warnings about PIE.
GO for it!