# Go Build and Deployment Hardening > How Go modules are pinned and verified, how to produce a static, stripped, position-independent binary, and how to ship it in a minimal nonroot container without exposing runtime diagnostics. ## Modules and dependencies - **Go Modules** have been the official system since 1.11; `dep` and vendor-only workflows are deprecated. `go mod init <module>` creates `go.mod`, and `go build`/`go test` populate it. **`go.sum`** records per-module hashes, checked against the checksum database, to detect unexpected module mutation. Commit both files. - `go mod download && go mod verify` in the build stage re-checks the cache against `go.sum`. The scratch-image guide shows this as the alternative to `go get -d -v`. - **`go get` works on packages, not repositories.** It fetches the import path and its dependencies and builds or installs them (classic GOPATH behavior). Run against an attacker-chosen path, it fetches and compiles arbitrary code. Treat it as code execution in CI. - **Private modules** need the toolchain to authenticate to private Git hosts. That is a credential on the build host. Keep it scoped and out of the final image. See [[Developer Workstation Secrets]]. - **Scanning:** `snyk test` resolves the module graph and reports CVEs with fixed versions. OWASP DependencyCheck has an experimental `go mod` analyzer. Contrast's Go agent does composition analysis at build time so known-CVE upgrades can be decided before QA. Trail of Bits warns that vendored and locally modified dependencies defeat version-based scanning. Dependencies usually outweigh first-party code. - Ecosystem attacks such as typosquats and malicious modules are covered in [[Software Supply Chain Security]] and [[Malicious Packages]]. ## Static vs dynamic linking Go produces static binaries **unless cgo is used**, and `net` (C resolver) and `os/user` (libc NSS lookups) pull cgo in transitively. Check with: ``` file ./app # "statically linked" vs "dynamically linked, interpreter /lib/ld-linux..." ldd ./app # "not a dynamic executable" ``` Options: - `CGO_ENABLED=0 go build`: pure Go everywhere. This is the simplest and is recommended by Snyk unless cgo is required. - `go build -tags netgo,osusergo`: pure-Go `net` and `os/user` while other cgo stays available. - `go build -ldflags="-extldflags=-static"`: statically link the cgo parts. Watch for `dlopen`/`getaddrinfo` warnings (go-sqlite3 needs `-tags sqlite_omit_load_extension`). ## Build flags ``` CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \ go build -trimpath -ldflags="-s -w" -o /out/app ./cmd/app ``` - `-trimpath` removes build-host filesystem paths from the binary. - `-ldflags -s` drops the symbol table, and `-w` drops DWARF. This makes reverse engineering harder but doesn't remove `.gopclntab` (see [[Go Binary Reverse Engineering]]). - **ASLR:** for years Google's toolchain produced fixed-address binaries, and maintainers argued that bounds checking made ASLR less important. After an arbitrary write in a Go binary, that predictability made code execution trivial. **`-buildmode=pie`** (since Go 1.6) gives randomized layout on Linux, though not on OpenBSD at the time of writing. The older advice to use gccgo with `-fPIC -pie` is obsolete. PIE matters most for binaries with `unsafe` or cgo (see [[Go unsafe and cgo]]). ## Container image Multi-stage build: compile in `golang:<ver>`, ship `FROM scratch` containing only the binary plus `/etc/passwd` and `/etc/group` for a nonroot user. ```dockerfile FROM golang@sha256:<digest> AS builder RUN adduser -D -H -s /sbin/nologin -u 10001 appuser ... FROM scratch COPY --from=builder /etc/passwd /etc/passwd COPY --from=builder /etc/group /etc/group COPY --from=builder /out/app /app USER appuser:appuser EXPOSE 9292 ENTRYPOINT ["/app"] ``` - There is no `sh` or `ps` in the image, which removes post-exploitation tooling. - **Pin the base image by digest** and pull with `DOCKER_CONTENT_TRUST=1`, because tags can be swapped or intercepted. - Listen on a port above 1024 so no capability is needed, run as a fixed UID, and keep credentials and data out of the image (use volumes or secrets). - Copy CA certificates into the scratch image if the app makes TLS calls. ## Runtime diagnostics exposure `github.com/google/gops/agent` (`agent.Listen`) opens a listener that serves stack traces, Go version, memory stats and CPU/heap pprof profiles to the `gops` CLI. Remote mode targets `host:port`. Confirm it isn't enabled in production builds, or that it is bound to loopback. ## Checklist - [ ] `go.mod` + `go.sum` committed; `go mod verify` in CI; dependency scan gating merges - [ ] `CGO_ENABLED=0`, or deliberate `netgo,osusergo`; `file` shows statically linked - [ ] `-trimpath -ldflags="-s -w"`; `-buildmode=pie` where supported - [ ] Scratch or minimal image, base pinned by digest, nonroot `USER`, unprivileged port - [ ] No gops/pprof agent reachable off-host ## Related - [[Go Security]] (hub) · [[Go unsafe and cgo]] · [[Go Binary Reverse Engineering]] · [[Go Security Testing Tools]] - [[Software Supply Chain Security]] · [[Malicious Packages]] · [[Typosquatting]] · [[SBOM and Provenance]] · [[SSRF in Kubernetes and Containers]] ## Sources - [[raw/snyk.io/go-security-cheatsheet-snyk-blog-1225|Go Security cheatsheet]] - [[raw/blog.trailofbits.com/security-assessment-techniques-for-go-projects-trail-of-bits-blog-1213|Security assessment techniques for Go projects]] - [[raw/nanxiao.gitbooks.io/go-get-command-golang-101-hacks-1274|"go get" command · golang-101-hacks]] - [[raw/link.medium.com/go-modules-with-private-git-repositories-1363|Go Modules with Private Git Repositories]] - [[raw/arp242.net/statically-compiling-go-programs-1195|Statically compiling Go programs]] - [[raw/link.medium.com/create-the-smallest-and-secured-golang-docker-image-based-on-scratch-1184|Create the smallest and secured golang docker image based on scratch]] - [[raw/rain-1.github.io/golang-aslr-rain-1-github-io-1098|Golang ASLR]] - [[raw/github.com/github-google-gops-a-tool-to-list-and-diagnose-go-processes-currently-1238|gops: list and diagnose Go processes]] - [[raw/contrastsecurity.com/secure-coding-with-go-1224|Secure Coding with Go]] - [[raw/medium.com/de-ofuscating-golang-functions-1368|De-Obfuscating GoLang Functions]]