#11322·trivy

fix(alpine): false positives for apk packages without an origin (o:) field

Author: DmitriyLewenCreated Sep 28, 2026Updated Sep 29, 2026
Labelskind/bughelp wantedscan/vulnerability

Description

The Alpine detector reports already fixed vulnerabilities for apk packages that have no o: (origin) field in /lib/apk/db/installed. Such packages show up in images whose packages are not built with abuild, e.g. Docker Hardened Images.

Original report: #11311.

Reproduction

bash
trivy image --pkg-types os dhi.io/git:2-alpine@sha256:c22bf201b598dabf2e84defb4fdddfb485cc91c215db92614b879b624795b8f2
┌─────────┬────────────────┬──────────┬────────┬───────────────────┬───────────────┐
│ Library │ Vulnerability  │ Severity │ Status │ Installed Version │ Fixed Version │
├─────────┼────────────────┼──────────┼────────┼───────────────────┼───────────────┤
│ git-lfs │ CVE-2021-38561 │ HIGH     │ fixed  │ 3.8.0-r3          │ 3.1.2-r3      │
│         │ CVE-2022-27191 │ HIGH     │ fixed  │ 3.8.0-r3          │ 3.1.2-r4      │
│         │ CVE-2025-26625 │ HIGH     │ fixed  │ 3.8.0-r3          │ 3.7.1-r0      │
└─────────┴────────────────┴──────────┴────────┴───────────────────┴───────────────┘

The installed version 3.8.0-r3 is higher than every fixed version, yet all three CVEs are reported.

The git-lfs entry in /lib/apk/db/installed (R:, Z: and similar fields omitted):

P:git-lfs
V:3.8.0-r3
A:x86_64
L:MIT
m:Docker Hardened Images <[email protected]>
D:git

The other 19 packages in the image have an o: field; git-lfs does not.

Root cause

  1. The apk analyzer sets SrcName and SrcVersion only when it parses an o: line (apk.go#L88-L91). Without o: both fields stay empty.
  2. The Alpine detector falls back to Name when SrcName is empty (alpine.go#L89-L92, #3079), so advisories for the package are found. There is no such fallback for the version: it compares utils.FormatSrcVersion(pkg), which is an empty string (alpine.go#L101).
  3. go-apk-version parses "" without an error and treats it as lower than any other version, so every advisory with a fixed version matches.
  4. The report shows the binary version (FormatVersion), which is why the installed version in the table looks higher than the fixed one.

Similar problems

The same situation — empty source fields when there is no information about the source package — exists elsewhere:

  • rpm analyzer: when SOURCERPM is (none), empty or unparsable, SrcName/SrcVersion/SrcRelease stay empty, but SrcEpoch is still taken from the binary package (rpm.go#L131-L139, rpm.go#L179). With a non-zero epoch, FormatSrcVersion returns a string like 1: instead of an empty one.
  • Azure Linux: advisories are looked up by pkg.SrcName with no fallback to Name (azure.go#L51), so no vulnerabilities are found for such an rpm package (false negative).
  • Root.io and Seal: the name falls back to Name, but the version comes from FormatSrcVersion (rootio.go#L85, seal.go#L110). checkConstraints returns false for an empty version, so fixed vulnerabilities are missed (false negative); for rpm with a non-zero epoch a string like 1: is compared.
  • RapidFort: already uses cmp.Or(utils.FormatSrcVersion(pkg), utils.FormatVersion(pkg)) (rapidfort.go#L145), but that does not kick in for rpm packages with a non-zero epoch and no SOURCERPM, because of the same 1:.

Not affected: the dpkg analyzer fills the source fields from the binary package, the same way dpkg itself does (dpkg.go#L310-L320), so Debian, Ubuntu and Echo always have a source version. Detectors that compare the binary version (Red Hat, Amazon, Oracle, SUSE, Rocky, Alma, Photon, Wolfi, Chainguard, MinimOS) do not depend on the source version.

Challenges

  • The source fields are not only detector input. They end up in CycloneDX properties and SPDX sourceInfo (encode.go#L409, encode.go#L476, marshal.go#L357) and in RPC. For dpkg, using the binary package as the source is a rule of the format itself; for apk and rpm it is less clear-cut. apk has no separate source version at all (subpackages share the main package's version, and the analyzer already takes SrcVersion from V:), and apk list and apk search --origin print the package name when the origin is empty (app_list.c, app_search.c); the rest of apk-tools (replaces, index --prune-origin, apk query) treats an empty origin as absent rather than equal to the name. rpmdb has no source package information at all for packages without SOURCERPM.
  • utils.FormatSrcVersion is used both by detectors and by the SBOM encoder.
  • Name and version must be taken consistently: a source name with a binary version (or vice versa) produces a wrong match.
  • Checking whether the result of FormatSrcVersion is empty does not work for rpm because of SrcEpoch (see above).
  • Any change to an analyzer's output requires bumping its Version(), otherwise cached results keep serving the old data.

Before implementation

Please discuss and agree on the approach in this issue before opening a PR.