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

feat: add bunch of ldflags for reproducibility #143

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

developer-guy
Copy link
Contributor

Signed-off-by: Batuhan Apaydın [email protected]
cc: @tomhjp 🙋🏻‍♂️

@tomhjp
Copy link
Contributor

tomhjp commented Feb 11, 2022

Hi @developer-guy, please can you include some documentation and rationale in the Makefile for those options?

@developer-guy
Copy link
Contributor Author

developer-guy commented Feb 11, 2022

Hello @tomhjp, I found this:

  • -trimpath: increases build reproducibility. Per default full file paths are added to the go binary so two users will very likely have different binaries build even though the code is the same. Important for Reproducible Builds so full build paths and module paths are not embedded.
  • -w: No DWARF (reduces binary size)
  • -s: No symbol table (even less binary size)
  • -znow and -zrelro: Hardening, adds runtime costs. See https://www.redhat.com/en/blog/hardening-elf-binaries-using-relocation-read-only-relro
  • -buildmode=pie: enables PIE compilation for binary harderning.
  • -mod=readonly: ensure the module files are not updated in any go actions.
  • -modcacherw: is not important, but it ensures that go modules creates a write-able path. Default is read-only.
> https://giters.com/ansible/receptor/issues/398

@developer-guy
Copy link
Contributor Author

And also a bunch of useful articles about reproducible builds for Go binaries:

@developer-guy
Copy link
Contributor Author

developer-guy commented Feb 11, 2022

We reduced the binary size up to 4MB 📉

Screen Shot 2022-02-11 at 21 42 01

Screen Shot 2022-02-11 at 21 41 14

@developer-guy
Copy link
Contributor Author

Generally speaking; go and build flags is a rabbit hole since Go is unable to parse cli flags and env flags correctly. There is also a difference between hardening flags (PIE and RELRO) and reproducible builds flags. Setting trimpath and unsetting buildid is enough.

thanks to @Foxboron

@developer-guy
Copy link
Contributor Author

kindly ping @tomhjp

@developer-guy
Copy link
Contributor Author

kindly ping @tomhjp 🙋🏻‍♂️

@tomhjp
Copy link
Contributor

tomhjp commented Feb 25, 2022

Hi @developer-guy, please can you put the reasoning in the code alongside/just before the usage?

  • -buildid= - I'm not convinced the need for this one is proven. It's set to a hash of the build inputs - that could be stable. Unfortunately I don't have the bandwidth to run experiments around this one
  • -s -w - I'm fine with this
  • -trimpath LGTM
  • -mod=readonly LGTM
  • -modcacherw - needs more justification. What paths? And why is it important that they're writeable?

I'm happy to land this either with just the ones that are definitely ok, or you're welcome to argue your case :)

@hashicorp-cla
Copy link

hashicorp-cla commented Mar 12, 2022

CLA assistant check
All committers have signed the CLA.

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.

None yet

3 participants