How to fix CVEs in Go container images
Short answer
A Go binary carries the standard library of the Go release that built it, plus its modules, so CVEs in a Go image are fixed by rebuilding: with a patched Go release for standard library CVEs, and with fixed module versions (go get module@version, go mod tidy) for dependencies. A distroless or scratch runtime image then has almost nothing else to patch.
Fix a Go image now →
free, no sign-up; pre-filled with golang:1.23, change it to yours
The steps
- 01Rebuild with a patched Go releaseStandard library fixes ship in Go point releases.
- 02Upgrade vulnerable modulesgo get module@fixed-version && go mod tidy.
- 03Check what is really calledgovulncheck reports only vulnerabilities the code reaches.
- 04Minimal runtime imageCopy the static binary into distroless or scratch.
By hand
FROM golang:1.23 AS build WORKDIR /src COPY . . RUN go get golang.org/x/net@latest && go mod tidy \ && CGO_ENABLED=0 go build -trimpath -o /app ./cmd/app FROM gcr.io/distroless/static-debian12 COPY --from=build /app /app ENTRYPOINT ["/app"]
Then rescan: the CVEs that remain are either without a released fix or in code the application never loads (the report marks both).
Traps that bring CVEs back
- An old binary keeps its old standard library forever: upgrading the base image does not change it.
- Helper binaries bundled in other images (gosu in database images) are Go programs with their own CVEs.
- Scanners list every module CVE; many are in code the binary never calls.
Go images in OCImend's reports
| Image | Grade | Known CVEs | |
|---|---|---|---|
| golang:1.19.9-bullseye | F | 749 exploited | Fix |
| mcr.microsoft.com/oss/go/microsoft/golang:latest | F | 199 | Fix |
| golang:latest | D | 121 | Fix |
Questions
How do I fix CVEs in a Go container image?
A Go binary carries the standard library of the Go release that built it, plus its modules, so CVEs in a Go image are fixed by rebuilding: with a patched Go release for standard library CVEs, and with fixed module versions (go get module@version, go mod tidy) for dependencies. A distroless or scratch runtime image then has almost nothing else to patch.
Why does my Go image show CVEs in stdlib?
The binary was built with a Go release that had them; rebuild with a patched release. OCImend rebuilds Go programs from their source when it can.
Does a scratch image need patching?
Only the binaries in it: rebuild them.
Other image types
Python · Node.js · Java · nginx · PostgreSQL · Alpine · Debian · Ubuntu · Red Hat UBI