208 points · by nnx · 5y ago · 91 comments
const compile_version = gorge "git describe --tags --always --dirty"
const compile_time = gorge "date --rfc-3339=seconds"Often this arbitrary code is to do things like run "pkg-config --libs" or such to find dependencies to link against, or generate some files that shouldn't be checked into source code, but rarely does it have sandboxing or other restrictions.
Languages, like Go, which don't let a package execute arbitrary code on installation are the exception.
-buildinfo=false
I especially found out this is really useful for extracting information form non-stripped firmware binaries. They lay around on some S3 buckets, where cheap IoT manufacturers think it's safe to expose them online for updating purposes...
> the currently checked-out revision and a flag indicating whether edited or untracked files are present
Basically, it's including a git commit ID, which is a hash, and flags that indicate if there are local changes.
It's pretty common to include build hashes in shipping code, logs, ect.
BUILD_TIME=$(date +"%Y%m%d.%H%M%S")
CommitHash=$(git describe | cut -d- -f3)
GoVersion=$(go version | cut -c 14- | cut -d' ' -f1)
GitTag=$(git describe | cut -d- -f1)
FLAG="-X $TRG_PKG.BuildTime=$BUILD_TIME"
FLAG="$FLAG -X $TRG_PKG.CommitHash=$CommitHash"
FLAG="$FLAG -X $TRG_PKG.GoVersion=$GoVersion"
FLAG="$FLAG -X $TRG_PKG.GitTag=$GitTag"
go build -v -ldflags "$FLAG"
Part of the reason why "reproducibility" has taken so long to appear is precisely that its use cases take a long time and a lot of scale to appear. I approve of distros pursuing this goal, but haven't got a lot of use of it for the stuff I build, which is why I have a very similar build script.
Please don't reply with a simple recitation of the putative benefits of reproducibility (I'm well aware) or some wild "what if" scenario relating to my own binaries that will occur once every few centuries on average. I have had multiple real problems in my life where not knowing the exact source checkout and time something was built has been a problem. I've only really once had a problem where I would have felt better if I could have reproduced something exactly, to be sure that the source and random unattributed binary were in fact the same, and even that ended up only as a "I would have felt better." (And now that I think about it, if said unattributed binary had just said what commit it was built out of that would have been fine too....) I am much more interested in solving the problems that actually manifest than problems that someone somewhere hypothesizes I may someday have.
Once you know the exact source, what does it matter when the binary was built?
Reproducible releases should probably _not_ have these commit metadata built in (e.g. imagine reproducible building this 5 years in the future where you've moved off git). Though this is quite useful in debug builds where you might be have installed a number of different builds in the wild and want to be able to quickly figure out the exact version of the code a given binary was generated from.
When converting VCSs most converters will preserve the old hash in some form, e.g. by appending it to the commit message.
Github downloads however remain a problem, as they just include the branch name, not the commit hash.
buildtime="${BUILD_TIME:-$( date -u +%FT%TZ%z )}"
# Use $buildtime in your go build command.
This will only set the variable buildtime if BUILD_TIME
isn't set already. Making reproducing a build easy: just
define BUILD_TIME to be equal to the one of the binary
you want to reproduce. TZ=UTC git log -n1 --format='%cd' --date='format-local:%Y-%m-%dT%H:%M:%S'
There's probably a better way to do that, but the basic gist is to derive the most recent commit date.The solarwinds and other recent supply chain attack involved injecting additional code into a build environment. Reproducible builds could have been done in multiple places as an auditing mechanism to catch this.
Another place where reproducible builds is useful in industry is being able to reproduce debug symbols for something after it has shipped. Sure you can rely on always saving debug symbols in an artifact repo, but if those age out and you get a customer case... what ya gonna do?
Because that where reproducible builds started.
Projects not interested in reproducibility can/should content themselves to live in the non-reproducible set instead of expecting someone else to monkey patch for them!
The reason for this is the change in syntax for build conditionals. Go 1.17 being compatible with the old and new, but Go 1.18 will only be compatible with the new. By which point Go 1.18 will only use the new and Go 1.4 would no longer be able to build later versions of Go.
https://github.com/golang/go/issues/44505#issuecomment-94259...
This reminds me of the mcs(1) command (Manipulate Comment Section) used for some Solaris 10 binaries to display the patch level information:
# /usr/ccs/bin/mcs -p /kernel/crypto/sparcv9/aes
/kernel/crypto/sparcv9/aes:
@(#)SunOS 5.10 Generic 150400-64 Dec 2018
> People who distribute binaries widely and build them doing funny things may now have more of those funny things be somewhat visible, though. Well, until they turn these features off with those documented flags.
Yeah only if they know to check! This is pretty much the unsafe-by-default anti-pattern. You know, mysql_real_escape(), yaml.load(disable_gaping_security_holes=True) and so on.
That said, the actual information looks fairly innocuous and file paths get accidentally embedded in C++ binaries all the time with no issues.
Some people used to strip or blank out all sort of identifiers for various reasons (IIRC, primarily to complicate analysis with automated tools).
1. Go binaries are bloated already. Do they plan to compress this (and other textual info, like full type names) in the binary?
2. I saw enough of build pipelines that do not clone repositories, but do a shallow copy for a tag, and have no .git folder. How much info will be added in this case?
3. Moreover, pretty much every serious build has source generation steps (protobuf, jooq, etc - you name it). If resulting executable will have a very particular info about these, the sheer amount of information added will probably make executables considerable larger.2): It will not be added (based on information provided in the article [0]).
3): Is it valid question? By my basic understanding of article the case of adding more bytes to binary will require having the main package [0]. Also, generation (e.g. gogo-proto, go:generate) will create only business logic code in new package, no additional metadata will be added.
[0] Quote from article:
> Version control information is embedded if the go command is invoked in a directory within a Git or Mercurial repository, and the main package and its containing main module are in the same repository.
only way for this to be 'incorrect' would be if someone intentionally fucked it up or disabled it.