Summary
apko's dirFS abstraction contains a path traversal flaw where directory and symlink creation routines do not verify that computed paths remain within the intended installation root.
An attacker who can introduce a malicious APK package (for example, via a compromised or typosquatted repository) could use crafted paths to create directories or symlinks outside the target root, potentially redirecting writes or influencing the container filesystem layout.
Affected context
This vulnerability affects environments that use apko (versions 0.14.8 to before 1.1.1) to build OCI container images from APK packages.
It specifically involves the dirFS filesystem abstraction in pkg/apk/fs/rwosfs.go, where MkdirAll, Mkdir, and Symlink rely on filepath.Join() alone to construct target paths.
When apko processes APK packages that contain crafted file paths or symlinks, the code does not ensure that the resultant paths stay confined to the configured installation root directory, allowing potential writes outside that root.
Exploit preconditions
- Attack surface: The APK package contents processed during apko image builds (file paths, directory entries, symlinks).
- Required access: Ability to influence which APK packages are installed (e.g., publishing a malicious package to a repository, controlling a typosquatted package name, or compromising a package source).
- Trigger point: apko invoking filesystem operations (
MkdirAll,Mkdir,Symlink) on paths derived from package metadata during image build. - Why exploitation is possible: The implementation uses
filepath.Join(baseDir, userPath)without subsequently validating that the normalized resulting path still resides withinbaseDir. Crafted relative paths (e.g., containing traversal segments) or symlink targets can cause operations to escape the intended root.
Why this happens
The core issue is an incomplete validation of file system paths constructed from partially untrusted input.
The dirFS abstraction assumes that joining a base root with a user-controlled or package-controlled subpath via filepath.Join() is sufficient to maintain confinement.
However, without explicit checks that the final, cleaned path remains a descendant of the base directory, the code is vulnerable to CWE-23: Relative Path Traversal.
This pattern occurs whenever software treats relative paths from external sources as inherently safe, omitting normalization and enforcement of directory boundaries before performing filesystem side effects.
Potential impact
- Creation of directories outside the intended installation root used by apko.
- Creation of symlinks that point outside the root, potentially redirecting later writes or reads.
- Interference with the container filesystem layout, such as redirecting configuration or binary locations.
- Increased risk of follow-on vulnerabilities where other components trust the filesystem layout produced by apko.
- Facilitation of supply-chain manipulation within built images, depending on how the escaped paths are later used.
General mitigation guidance
Effective mitigation focuses on enforcing strict path confinement and treating package-controlled paths as untrusted:
- Normalize and validate all derived paths after joining with the base directory, ensuring they remain within the intended root (e.g., by comparing cleaned prefixes or using chroot-like abstractions).
- Implement helper APIs that encapsulate safe path handling for all filesystem operations, rather than duplicating ad-hoc joins.
- Treat symlink creation and resolution as high-risk operations; restrict or sanitize symlink targets that originate from package metadata.
- Apply a defense-in-depth model where build tools operate with minimal privileges on isolated build roots or temporary filesystems.
- Prefer secure-by-default path utilities and enforce coding guidelines that forbid direct use of unsafe join patterns on untrusted input.
How teams detect issues like this
Detection generally combines code-level and build-time analysis:
- Use static analysis rules to flag instances where
filepath.Join()(or equivalent) is used with untrusted or package-derived components without subsequent boundary checks. - During code review, treat any filesystem abstraction that accepts external or package-controlled paths as security-sensitive, verifying that all operations enforce root confinement.
- Instrument build pipelines with logging for unusual or out-of-tree paths encountered during image assembly, and alert on attempts to write outside expected directories.
- Employ automated secure code review tools or linters that recognize common path traversal antipatterns, particularly in functions that create directories, files, or symlinks.

