Go Security
Security of Go code, binaries and builds. The runtime is memory-safe by default, so real Go bugs concentrate in error-handling semantics, panics and concurrency, stdlib API footguns, deliberate escapes from the type system (
unsafe, cgo), and how binaries are built and shipped.
How it works
Go removes most C-class memory bugs with bounds-checked slices, garbage collection and typed pointers. Trail of Bits’ assessment of Go projects (after their Kubernetes audit) found that the exploitable problems sit elsewhere: scoping, goroutines, error handling and dependency management. Most of these aren’t runtime bugs. The recurring root causes:
- Errors are values, not control flow. An ignored or overwritten
errlets execution continue on bad state. Examples are:=redeclaration silently discarding an error, and the Kubeletionicebug where a logged-but-unreturned read error caused an index-out-of-range panic. - Panics are recoverable, state isn’t.
net/httprecovers per-request panics, but a panic inside a critical section leaves mutexes locked or invariants half-applied. The result is a persistent DoS from one request. - Convenience APIs with surprising semantics. These include
text/templatevshtml/template,encoding/jsonfloat64 coercion and zero-value defaults,net/urldecoded paths,http.DefaultClientwith no timeout, andfmt.Sprintf-built SQL. - Deliberate escapes.
unsafe.Pointer,go:linknameand cgo opt out of the guarantees. - Build output. Binaries were historically non-PIE, ship full symbol and
.gopclntabmetadata unless stripped, and dynamic linking appears silently vianet/os/user.
Contrast notes a tooling gap: Go’s static typing plus interface-heavy code breaks many data-flow SAST tools, and Go APIs often front NoSQL stores. So the classic SQLi-centric rule packs under-fit.
Map of the topic
Code-level bugs
- Go Secure Coding Pitfalls: shadowed
err, integer width (TOB-K8S-015), template package choice, JSON decoding quirks,net/urlsemantics,SprintfSQL, HTTP client timeouts, timing-safe compares and bcrypt cost. - Go Panics and Concurrency Bugs: recover-in-handler deadlocks, inconsistent state after recovery,
recover()scoping, loop-variable capture, unsynchronized maps, closed-channel and nil-map panics, andlog.Fatalexits. - Go unsafe and cgo:
unsafe.Pointerarithmetic, writing unexported fields, zero-copy string and slice aliasing,go:linkname, cgo trust andCGO_ENABLED=0. - Go Template SSTI: method-call gadgets in
text/templateandhtml/template(ssti topic).
Assessment and tooling
- Go Security Testing Tools: gosec, go vet, staticcheck and ineffassign, custom
go/analysisand SSA checkers, go-fuzz and gopter, KRF fault injection, the race detector and on-edge, and IAST.
Build, supply chain and binaries
- Go Build and Deployment Hardening:
go.mod/go.sum, dependency scanning, static linking (netgo,osusergo),-trimpath -ldflags="-s -w", PIE and ASLR, scratch images pinned by digest, and gops agent exposure. - Go Binary Reverse Engineering:
.gopclntabsymbol recovery, obfuscators (gobfuscate, GoMang), stdlib signature matching, and why offensive tooling is written in Go.
Review quick start
- Run
gosec ./...,go vet ./...,staticcheck ./...andineffassign ./...together. Each misses cases the others catch. - Grep for
unsafe.,//go:linkname,import "C",text/template,fmt.Sprintf(nearQuery/Exec,exec.Command,http.Get(/http.DefaultClient,recover(), andstrconv.Atoifollowed by a narrowing cast. - For each
mu.Lock()withoutdefer mu.Unlock(), ask what happens if the critical section panics. - Run tests with
-race, and build for 32-bit as well as 64-bit targets. - Fuzz parsers and decoders. Inject syscall faults to exercise error paths.
- Check that the build output is stripped, PIE,
CGO_ENABLED=0where possible, nonroot and from a digest-pinned scratch image.
Related
- Software Supply Chain Security · Malicious Packages · SBOM and Provenance
- Python Security · Python SAST Tools (comparable tooling map for Python)
- Server-Side Template Injection · Cross-Site Scripting · SQL Injection · Command Injection
- URL Parser Differentials · SSRF in Kubernetes and Containers
Sources
- Security assessment techniques for Go projects · original
- Go Security cheatsheet · original
- Secure Coding with Go · original
- Go, HTTP handlers, panic, and deadlocks · original
- 50 Shades of Go: Traps, Gotchas, and Common Mistakes · original
- The Top 10 Most Common Mistakes I've Seen in Go Projects · original
- Understand unsafe in GoLang · original
- Golang ASLR · original
- Statically compiling Go programs · original
- Create the smallest and secured golang docker image based on scratch · original
- De-Obfuscating GoLang Functions · original
- Write your own Go static analysis tool · original
- url package - net/url · original
- Building and testing a REST API in golang using gorilla mux and MySQL · original