Skip to main content
Menu
Advisories

apk-embedded SPDX documents can hide, impersonate, and displace packages in apko image SBOMs

GHSA-45CW-MP53-RRV6Published Updated GitHub

Description

Impact

apko builds the package portion of an image's SPDX SBOM entirely from SPDX documents that the installed apks ship under /var/lib/db/sbom. It never checks that a document describes the package that shipped it, and it omits a package without error when that package's document is missing or fails to parse. A crafted apk can therefore:

  • remove itself from the image SBOM,
  • appear under another name and version, with a well-formed PURL, and
  • replace the record of a genuine package, for example reporting a patched version of a vulnerable library.

Tools that trust the SBOM act on the false data: SBOM-driven vulnerability scanners, policy engines that evaluate SBOM attestations, and license reports. apko generates the SBOM before the image is signed, so attestations built from it verify cleanly. The image's /lib/apk/db/installed still lists the true package set, so scanners that inspect the image filesystem are unaffected.

Images built from repositories whose packages carry no embedded SBOM, such as Alpine, receive an SBOM with no apk packages at all, with no attacker involved.

Affected data paths

All in pkg/sbom/generator/spdx/spdx.go, the only SBOM generator in apko:

  1. locateApkSBOM chooses a document by file name alone, trying <name>-<version>.spdx.json, then the version without its -rN suffix, then <name>.spdx.json. It never checks that the installed package owns the file.
  2. ProcessInternalApkSBOM returns nil when no document matches or the document fails to parse.
  3. ProcessInternalApkSBOM and copySBOMElements copy every package the document describes. Nothing compares name, version, SPDX ID, or PURL against the installed package.
  4. Generate deduplicates packages by SPDX ID and keeps the first. melange assigns predictable IDs, SPDXRef-Package-<name>-<version> (melange pkg/sbom/package.go:147), so a document processed earlier can displace a genuine record.
  5. Lines 156-160 are the only place apk packages enter the SBOM. apko never falls back to the installed database.

Scope

  • Packages built by melange cannot replace their own document. melange runs the build pipelines (pkg/build/build.go:671) and then writes <name>-<version>.spdx.json, truncating any existing file (build.go:847, pkg/build/sbom/spdx/spdx.go:257).
  • **A melange pipeline can add further *.spdx.json files** to var/lib/db/sbom. melange's sbom linter flags only files without that suffix (pkg/linter/linters/sbom.go:30) and warns by default (pkg/linter/linter.go:87-90). apko uses such a file for any co-installed package that lacks a correctly named document of its own.
  • No package can overwrite another origin's document. apko refuses to install two packages from different origins that ship the same path with different content (pkg/apk/apk/install.go:141-149).

Attack prerequisites

The attacker must place a crafted apk in the resolved package set of a build, for example by publishing it to a repository the build trusts. No access to the build host is required.

Affected versions

All releases through 1.4.5. Unchecked ingestion of embedded documents ships in v0.7.0 and later. Keep-first deduplication by SPDX ID, which enables displacement of genuine records, ships in v0.19.3 and later (#1366).

Proof of concept

A table-driven test against SPDX.Generate on an in-memory filesystem, confirmed at e3915900 and 739e7ce. Generate returns nil in every case.

| Scenario | Installed | Embedded SBOM | apk entries in image SBOM | | -- | -- | -- | -- | | Substitution | evil-helper 1.0.0-r0 | evil-helper-1.0.0-r0.spdx.json describes openssl 9.9.9-r0, pkg:apk/wolfi/openssl@9.9.9-r0 | openssl 9.9.9-r0 only | | Missing SBOM | no-sbom 1.0.0-r0 | none | none | | Malformed SBOM | broken 1.0.0-r0 | {not json | none | | Displacement | aaa 1.0.0-r0, openssl 3.0.1-r0 | aaa's document reuses SPDXRef-Package-openssl-3.0.1-r0 with version 3.5.0-r0 | openssl 3.5.0-r0; the genuine 3.0.1-r0 record is dropped | | Name-only fallback | busybox 1.36.1-r0, no document of its own | added busybox.spdx.json says 9.9.9-r0 | busybox 9.9.9-r0 |

Save as pkg/sbom/generator/spdx/embedded_sbom_identity_test.go and run go test ./pkg/sbom/generator/spdx/ -run TestEmbeddedSBOMIdentity -v. Each subtest passes while the defect exists.

package spdx

import (
	"context"
	"encoding/json"
	"os"
	"path/filepath"
	"testing"

	"chainguard.dev/apko/pkg/apk/apk"
	apkfs "chainguard.dev/apko/pkg/apk/fs"
)

func installedPkg(name, version, origin string) *apk.InstalledPackage {
	return &apk.InstalledPackage{Package: apk.Package{Name: name, Version: version, Origin: origin, Arch: "x86_64"}}
}

func embeddedSBOM(id, name, version, purl string) []byte {
	doc := map[string]any{
		"SPDXID":            "SPDXRef-DOCUMENT",
		"spdxVersion":       "SPDX-2.3",
		"documentDescribes": []string{id},
		"packages": []map[string]any{{
			"SPDXID":           id,
			"name":             name,
			"versionInfo":      version,
			"downloadLocation": "NOASSERTION",
			"externalRefs": []map[string]string{{
				"referenceCategory": "PACKAGE_MANAGER",
				"referenceType":     "purl",
				"referenceLocator":  purl,
			}},
		}},
	}
	b, _ := json.Marshal(doc)
	return b
}

func generateDoc(t *testing.T, fsys apkfs.FullFS, pkgs ...*apk.InstalledPackage) *Document {
	t.Helper()
	opts := testOpts(fsys)
	opts.Packages = pkgs
	out := filepath.Join(t.TempDir(), "sbom.spdx.json")
	if err := New().Generate(context.Background(), opts, out); err != nil {
		t.Fatalf("Generate: %v", err)
	}
	b, err := os.ReadFile(out)
	if err != nil {
		t.Fatal(err)
	}
	var doc Document
	if err := json.Unmarshal(b, &doc); err != nil {
		t.Fatal(err)
	}
	return &doc
}

func packageVersions(doc *Document) map[string]string {
	m := map[string]string{}
	for _, p := range doc.Packages {
		m[p.Name] = p.Version
	}
	return m
}

func TestEmbeddedSBOMIdentity(t *testing.T) {
	t.Run("mismatched SBOM substitutes a fake package", func(t *testing.T) {
		fsys := apkfs.NewMemFS()
		_ = fsys.MkdirAll("var/lib/db/sbom", 0o755)
		_ = fsys.WriteFile("var/lib/db/sbom/evil-helper-1.0.0-r0.spdx.json",
			embeddedSBOM("SPDXRef-Package-openssl-9.9.9-r0", "openssl", "9.9.9-r0", "pkg:apk/wolfi/openssl@9.9.9-r0?arch=x86_64"), 0o644)
		got := packageVersions(generateDoc(t, fsys, installedPkg("evil-helper", "1.0.0-r0", "evil-helper")))
		t.Logf("packages: %v", got)
		if _, ok := got["evil-helper"]; ok {
			t.Errorf("evil-helper present")
		}
		if got["openssl"] != "9.9.9-r0" {
			t.Errorf("fake openssl absent")
		}
	})

	t.Run("package without SBOM is silently dropped", func(t *testing.T) {
		fsys := apkfs.NewMemFS()
		_ = fsys.MkdirAll("var/lib/db/sbom", 0o755)
		got := packageVersions(generateDoc(t, fsys, installedPkg("no-sbom", "1.0.0-r0", "no-sbom")))
		t.Logf("packages: %v", got)
		if _, ok := got["no-sbom"]; ok {
			t.Errorf("no-sbom present")
		}
	})

	t.Run("malformed SBOM is silently dropped", func(t *testing.T) {
		fsys := apkfs.NewMemFS()
		_ = fsys.MkdirAll("var/lib/db/sbom", 0o755)
		_ = fsys.WriteFile("var/lib/db/sbom/broken-1.0.0-r0.spdx.json", []byte("{not json"), 0o644)
		got := packageVersions(generateDoc(t, fsys, installedPkg("broken", "1.0.0-r0", "broken")))
		t.Logf("packages: %v", got)
		if _, ok := got["broken"]; ok {
			t.Errorf("broken present")
		}
	})

	t.Run("earlier SBOM shadows a real package by SPDX ID", func(t *testing.T) {
		fsys := apkfs.NewMemFS()
		_ = fsys.MkdirAll("var/lib/db/sbom", 0o755)
		_ = fsys.WriteFile("var/lib/db/sbom/aaa-1.0.0-r0.spdx.json",
			embeddedSBOM("SPDXRef-Package-openssl-3.0.1-r0", "openssl", "3.5.0-r0", "pkg:apk/wolfi/openssl@3.5.0-r0?arch=x86_64"), 0o644)
		_ = fsys.WriteFile("var/lib/db/sbom/openssl-3.0.1-r0.spdx.json",
			embeddedSBOM("SPDXRef-Package-openssl-3.0.1-r0", "openssl", "3.0.1-r0", "pkg:apk/wolfi/openssl@3.0.1-r0?arch=x86_64"), 0o644)
		got := packageVersions(generateDoc(t, fsys,
			installedPkg("aaa", "1.0.0-r0", "aaa"), installedPkg("openssl", "3.0.1-r0", "openssl")))
		t.Logf("packages: %v", got)
		if got["openssl"] != "3.5.0-r0" {
			t.Errorf("shadowing did not occur: openssl=%q", got["openssl"])
		}
	})

	t.Run("name-only fallback lets another package author the entry", func(t *testing.T) {
		fsys := apkfs.NewMemFS()
		_ = fsys.MkdirAll("var/lib/db/sbom", 0o755)
		// busybox ships no SBOM of its own (e.g. a non-melange apk); another
		// package ships var/lib/db/sbom/busybox.spdx.json, which the linter accepts.
		_ = fsys.WriteFile("var/lib/db/sbom/busybox.spdx.json",
			embeddedSBOM("SPDXRef-Package-busybox-9.9.9-r0", "busybox", "9.9.9-r0", "pkg:apk/wolfi/busybox@9.9.9-r0?arch=x86_64"), 0o644)
		got := packageVersions(generateDoc(t, fsys, installedPkg("busybox", "1.36.1-r0", "busybox")))
		t.Logf("packages: %v", got)
		if got["busybox"] != "9.9.9-r0" {
			t.Errorf("fallback not taken: busybox=%q", got["busybox"])
		}
	})
}

Patches

None released yet. The fix, in pkg/sbom/generator/spdx:

  1. reads an embedded document only from a path the installed database lists among the package's own files, so a document another package installs is ignored,
  2. requires every described record, and every record carrying an apk PURL, to name the installed package at its version, with or without the -rN epoch, so a package's own apk entry cataloged as a reachable record stays valid,
  3. rejects a reachable record that uses an installed package's canonical SPDX ID,
  4. fails the build when a package's own document does not parse, describes nothing, or describes an element that is not a package,
  5. describes a package that ships no document from its installed-database entry and logs a warning, and
  6. fails the build when two records share an SPDX ID but differ in name, version, external references, or checksums, rather than keeping the first. Records that differ only in metadata, such as the license, are kept once, and apko logs a warning.

It also returns an error, rather than dereferencing a nil FileInfo, when inspecting an SBOM path fails for a reason other than the path not existing.

Limits of the fix

The fix binds each embedded document to the package that ships it: a document cannot describe another package, claim another installed package's SPDX ID, or carry another package's apk PURL. It does not make the document's other records trustworthy. A package's document still attests to its own dependencies and upstream sources, so a crafted apk can add records naming components it does not contain, provided they carry no apk PURL and no installed package's SPDX ID. An SBOM consumer can trust the apk records, but should treat the non-apk records as claims made by the package that ships them.

A crafted apk can also reuse another document's SPDX ID with a different name, version, reference, or checksum to make the build fail. apko refuses to build the image rather than record false content.

Workarounds

  • Build only from repositories whose publishers you trust and whose packages melange builds.
  • Compare the SBOM's package list with /lib/apk/db/installed in the image, or scan the image filesystem rather than the SBOM.

Affected versions

References

GitHub · chainguard-dev/apko