GHSA-389P-892W-QWGF
Control characters in package data allow forging apk installed-database records
CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:N/VC:N/VI:L/VA:N/SC:N/SI:H/SA:N
Fuente: GitHub
Ciclo de vida de la vulnerabilidad
| Fecha | Fase | Evento | Detalle |
|---|---|---|---|
| Divulgación | Publicada | GHSA-389P-892W-QWGF divulgada · CVSS 6.9 (GitHub) | |
| Divulgación | Modificada | Última actualización del registro | |
| Remediación | Parche v1.4.2 | chainguard.dev/apko · 0 d desde divulgación | |
| Monitoreo | Detectada | Ingresó al índice | |
| Monitoreo | Verificada | Última verificación contra las fuentes |
Impact
apko writes the apk installed database (`/lib/apk/db/installed`) as a newline-delimited `<token>:<value>` record format, rendering attacker-influenced data into those lines verbatim with no escaping or validation. A crafted package can embed a newline in that data, terminating the current record and forging an entire additional package record.
The installed database is the source for SBOM generation (`pkg/build/sbom.go`, `pkg/build/build.go`), so forged records propagate into apko's SPDX output.
The significant consequence is concealment rather than addition: an attacker who can place a package in a repository the build trusts can install a vulnerable component while forging a record that reports a different name or version, causing downstream vulnerability scanners that consume the SBOM to miss the real component.
Scope
This advisory covers one mechanism: control characters — a newline in particular — carried in package data into installed-database records. It does not claim to close every route by which a package can misrepresent itself in the SBOM. The fix is the byte-level validation described under Patches, not a general guarantee that recorded package identity matches the package installed.
Affected data paths
Three distinct families of attacker-influenced data reach these records. All were unvalidated.
1. Package metadata from `.PKGINFO` (no filesystem interaction required). `PackageToInstalled` (`pkg/apk/apk/package.go`) renders `P:`, `V:`, `T:`, `L:`, `o:`, `m:`, `U:`, `D:`, `p:`, `r:` and `c:` lines directly from parsed `.PKGINFO` values. apk-tools reads `.PKGINFO` as a plain `key = value` line splitter with no quoting, and so can neither produce nor observe a multi-line value. apko parses it with `gopkg.in/ini.v1` (`pkg/apk/types/package.go`), which honours backquoted and triple-quoted multi-line values by default. A triple-quoted `pkgdesc` therefore carries an embedded newline straight into the database.
This is the most accessible variant — it needs nothing more than a crafted `pkgdesc` field, and apko explicitly prefers `.PKGINFO` over APKINDEX as the more complete source (`pkg/apk/apk/implementation.go`), so the permissive parser is the one that reaches the database.
2. Archive entry names. `AddInstalledPackage` (`pkg/apk/apk/installed.go`) writes `F:` and `R:` lines from `tar.Header.Name`. No control-character validation existed anywhere in `pkg/apk`; `sanitizeArchivePath` guards only path traversal. Both install paths were affected — `installAPKFiles` and the `lazilyInstallAPKFiles` fastpath that `installPackage` selects whenever the backing filesystem implements `WriteHeaderer`, which is the normal apko configuration. Control-section entry names reached `usr/lib/apk/db/scripts.tar` in the produced image by the same route, via `updateScriptsTar`.
3. PAX checksum records. The `Z:` line forwards an `APK-TOOLS.checksum.SHA1` PAX record unchanged when it already carries a `Q1` prefix. `installAPKFiles` recomputes that record for regular files, but the symlink and hardlink branches pass the supplied value through untouched.
Additionally, an empty archive entry name reached an unguarded `header.Name[0]` index and panicked both install paths (`runtime error: index out of range [0] with length 0`). `archive/tar` encodes an empty `Name` without complaint, so a malicious package crashed apko outright — a denial of service in a library consumed by melange.
Affected versions
All released versions through v1.2.39. Confirmed reproducible end to end: a single `AddInstalledPackage` call yielding two packages from `GetInstalled`, both via a crafted `.PKGINFO` and via a crafted entry name.
Attack prerequisites
The attacker must be able to introduce a package into a repository that the build trusts, or otherwise control the contents of an installed package. This is a meaningful precondition and is the primary reason for the moderate severity rating rather than a higher one. No filesystem-level access or unusual configuration is required.
Patches
Fixed by validating at each source and again at the sink:
- `.PKGINFO` values containing a newline or carriage return are rejected at parse time in `types.ParsePackageInfo`, restoring agreement with apk-tools about what the format permits with respect to line breaks. The check is reflective over the struct, and a field whose type it cannot walk is a hard error rather than a silent skip, so a field added later cannot escape validation unnoticed.
- Archive entry names containing a byte below `0x20` or equal to `0x7f`, or that are empty, are rejected on both install paths and in `updateScriptsTar`, via a typed `InvalidEntryNameError`. This mirrors apk-tools byte for byte.
- `AddInstalledPackage` also receives names read back out of an existing image's database, which apko re-writes when building on that image as a base. Applying the strict class there would fail builds over bytes already present in a third-party base image, and silently dropping the entry would conceal a file that is really present, so that sink rejects only the bytes that break the record format — newline and carriage return. Neither can reach it from a database read. An empty name is accepted there because it is the representation of the top-level directory.
- Every rendered database line is checked for an embedded newline or carriage return immediately before the record is written, which covers the `Z:` passthrough and any field that acquires such a value in future. The record is now built entirely before the database file is opened, so a rejection cannot leave a created-but-empty database behind.
- `ParseInstalled` no longer returns a short package list with a nil error when a record line exceeds the scanner's buffer. An over-long line previously stopped the scan silently, so a single such line erased every record after it from the parsed result and therefore from the SBOM — the same concealment impact, reached without a control character. The bound is raised to accommodate legitimately long dependency lines, and exceeding it is now an error.
- `ParseInstalled` no longer discards `sanitizeArchivePath`'s error, which had converted a path traversal in an `R:` value into a file header with an empty name.
- An install that fails part-way leaves entries written and claimed in the in-memory ownership map, which later packages' records are filtered against. Those writes cannot be safely unwound, so the affected `APK` value now refuses further installs and record writes with `ErrInstallAborted` rather than producing records built from a stale view.
Upstream context
apk-tools addressed the entry-name half of this class in commit `c1594f60` https://gitlab.alpinelinux.org/alpine/apk-tools/-/commit/c1594f60770483625891541375a074fe07338401 (2021-02-04), "db: consider control characters in filename as malicious", reported by Luca Weiss, with the rationale that "especially a newline can produce havoc in the database file as the filename is written there as-is". It added a `contains_control_character()` check rejecting any byte below `0x20` or equal to `0x7f`. It shipped in apk-tools v2.12.2 and, as far as we can determine, was never assigned a CVE. apko reimplements apk in Go and did not inherit this hardening.
The initial upstream implementation used a signed `char` comparison, which incorrectly rejected all non-ASCII (multi-byte UTF-8) names; corrected three days later in `ab7b8e3` https://gitlab.alpinelinux.org/alpine/apk-tools/-/commit/ab7b8e3c995eb5a05ddbaa4546cd16a10da2d836 ("db: fix control character check to use uint8_t", alpine issue #10737). The Go port compares unsigned bytes and carries regression tests pinning that legitimate UTF-8 names remain acceptable.
Upstream applies `contains_control_character()` to the archive entry name only — `src/database.c`, one call site, on `ae->name`. apko matches that scope deliberately: link targets are not validated, because they never become an `F:` or `R:` value and so cannot forge a record, and rejecting them would refuse packages that `apk add` installs without complaint.
The metadata variant has no upstream analogue, because apk-tools' line-based `.PKGINFO` reader cannot represent it.
Related
Before #2328 https://github.com/chainguard-dev/apko/pull/2328, an entry name embedding a single-character line also reached an out-of-range slice panic in `ParseInstalled`, making that denial of service reachable from a package rather than only from a corrupt base image. That specific path is closed by #2328.
Two adjacent defects found during this work were fixed publicly ahead of this advisory, as neither was reachable from a package: an infinite loop walking to the filesystem root (#2389 https://github.com/chainguard-dev/apko/pull/2389) and a silently discarded record for a package with an empty name (#2390 https://github.com/chainguard-dev/apko/pull/2390).
A reproducer has been shared with maintainers separately.
Desglose de métricas
Rangos afectados
Estado del parche por paquete
| Paquete / producto | Estado | Corregida en | Desde divulgación | Última versión |
|---|---|---|---|---|
gochainguard.dev/apko |
Corregida | v1.4.2 | 0 d | v1.5.0 no afectada |
1.4.2v1.4.2Arsenal público
Sin exploits, módulos ni PoC públicos conocidos a la fecha de verificación.
https://github.com/chainguard-dev/apko/security/advisories/GHSA-389p-892w-qwgfLas referencias externas se muestran para copiar: cve.llama.ec no lo redirige a otros sitios.
