Skip to content

feat: add bunch of ldflags for reproducibility - #143

Open
developer-guy wants to merge 1 commit into
hashicorp:mainfrom
developer-guy:main
Open

feat: add bunch of ldflags for reproducibility#143
developer-guy wants to merge 1 commit into
hashicorp:mainfrom
developer-guy:main

Conversation

@developer-guy

Copy link
Copy Markdown
Contributor

Signed-off-by: Batuhan Apaydın batuhan.apaydin@trendyol.com
cc: @tomhjp 🙋🏻‍♂️

@tomhjp

tomhjp commented Feb 11, 2022

Copy link
Copy Markdown
Contributor

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

@developer-guy

developer-guy commented Feb 11, 2022

Copy link
Copy Markdown
Contributor Author

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

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

@developer-guy
developer-guy force-pushed the main branch 2 times, most recently from ba78328 to 465a605 Compare February 11, 2022 18:39
@developer-guy

developer-guy commented Feb 11, 2022

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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

Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com>
@developer-guy

Copy link
Copy Markdown
Contributor Author

kindly ping @tomhjp

@developer-guy

Copy link
Copy Markdown
Contributor Author

kindly ping @tomhjp 🙋🏻‍♂️

@tomhjp

tomhjp commented Feb 25, 2022

Copy link
Copy Markdown
Contributor

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

hashicorp-cla commented Mar 12, 2022

Copy link
Copy Markdown

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.

3 participants