OCImendby CloudTrace

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

  1. 01Rebuild with a patched Go releaseStandard library fixes ship in Go point releases.
  2. 02Upgrade vulnerable modulesgo get module@fixed-version && go mod tidy.
  3. 03Check what is really calledgovulncheck reports only vulnerabilities the code reaches.
  4. 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

Go images in OCImend's reports

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

Fix a Go image now →