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 err lets execution continue on bad state. Examples are := redeclaration silently discarding an error, and the Kubelet ionice bug where a logged-but-unreturned read error caused an index-out-of-range panic.
  • Panics are recoverable, state isn’t. net/http recovers 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/template vs html/template, encoding/json float64 coercion and zero-value defaults, net/url decoded paths, http.DefaultClient with no timeout, and fmt.Sprintf-built SQL.
  • Deliberate escapes. unsafe.Pointer, go:linkname and cgo opt out of the guarantees.
  • Build output. Binaries were historically non-PIE, ship full symbol and .gopclntab metadata unless stripped, and dynamic linking appears silently via net/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/url semantics, Sprintf SQL, 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, and log.Fatal exits.
  • Go unsafe and cgo: unsafe.Pointer arithmetic, writing unexported fields, zero-copy string and slice aliasing, go:linkname, cgo trust and CGO_ENABLED=0.
  • Go Template SSTI: method-call gadgets in text/template and html/template (ssti topic).

Assessment and tooling

  • Go Security Testing Tools: gosec, go vet, staticcheck and ineffassign, custom go/analysis and 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: .gopclntab symbol recovery, obfuscators (gobfuscate, GoMang), stdlib signature matching, and why offensive tooling is written in Go.

Review quick start

  1. Run gosec ./..., go vet ./..., staticcheck ./... and ineffassign ./... together. Each misses cases the others catch.
  2. Grep for unsafe., //go:linkname, import "C", text/template, fmt.Sprintf( near Query/Exec, exec.Command, http.Get(/http.DefaultClient, recover(), and strconv.Atoi followed by a narrowing cast.
  3. For each mu.Lock() without defer mu.Unlock(), ask what happens if the critical section panics.
  4. Run tests with -race, and build for 32-bit as well as 64-bit targets.
  5. Fuzz parsers and decoders. Inject syscall faults to exercise error paths.
  6. Check that the build output is stripped, PIE, CGO_ENABLED=0 where possible, nonroot and from a digest-pinned scratch image.

Sources