Summary
Kaniko's build context extraction logic allows tar entries with directory traversal sequences (such as ../outside.txt) to write files outside the intended destination directory.
In environments where Kaniko has access to registry credentials and helper binaries, an attacker who controls or tampers with the build context may be able to place files that lead to code execution within the executor process.
Affected context
The vulnerability occurs when Kaniko unpacks a build context archive inside the executor, typically within a container or Kubernetes pod.
It is relevant in setups where:
- Build contexts are supplied as tar archives (e.g., from remote sources, CI pipelines, or user uploads).
- The archive contents are not fully trusted or may be influenced by external actors.
- Kaniko has access to docker credential helpers or other executables that can be abused if arbitrary files are written adjacent to them.
The issue manifests during tar extraction, before the build process completes.
Exploit preconditions
- Attack surface: Kaniko build context ingestion (tar-based build contexts).
- Required access: Ability to provide or modify the build context archive used by Kaniko (e.g., pushing a malicious context to a repository, altering CI artifacts, or man-in-the-middle on context delivery).
- Trigger point: Submission of a crafted tar archive containing file names with
..components (such as../prefixes) that is then extracted by Kaniko. - Why exploitation is possible: The code uses
filepath.Join(dest, cleanedName)to resolve extraction paths without subsequently enforcing that the resulting path remains within thedestdirectory, enabling directory traversal and writes outside the expected extraction root.
Why this happens
The underlying bug is a classic directory traversal issue in archive extraction.
Kaniko constructs extraction paths by joining the target directory with the file name from the tar entry using filepath.Join(dest, cleanedName), but does not verify that the resulting path is still beneath dest after normalization.
As described by CWE-22: Improper Limitation of a Pathname to a Restricted Directory, this pattern allows inputs containing .. components to escape the intended directory.
When combined with sensitive filesystem locations (such as directories containing credential helpers or configuration files), the arbitrary file write capability may be escalated into code execution within the executor process.
Potential impact
- Arbitrary file creation or overwrite outside the intended build context directory.
- Overwriting configuration files used by the build environment.
- Planting or modifying helper binaries or scripts in search paths used by the executor.
- Achieving code execution within the Kaniko executor process when combined with registry credential helpers or similar mechanisms.
- Lateral movement or escalation within the CI/CD or Kubernetes environment, depending on surrounding controls.
General mitigation guidance
Effective mitigation focuses on enforcing strict path confinement during archive extraction and reducing the trust placed in build contexts:
- Use canonical path resolution that verifies the final extraction path remains under the intended root directory before writing (e.g., secure join patterns).
- Reject tar entries containing
.., absolute paths, or other path components that could escape the extraction root. - Treat build contexts as untrusted inputs by default; avoid giving the executor unnecessary privileges, filesystem access, or sensitive helper binaries in the same directory tree.
- Run the Kaniko executor with least privilege, using isolated work directories and avoiding shared volumes that contain secrets or executables.
- Separate secrets and credential helpers from locations where user-controlled archives are unpacked to limit the impact of any traversal.
How teams detect issues like this
Organizations can detect and monitor for similar issues through multiple approaches:
- Static analysis: Scan codebases for archive extraction routines that use
filepath.Join(or equivalents) without subsequent checks that the resolved path is confined to a designated root. Flag patterns that do not normalize and validate paths after joining. - Code review: Pay special attention to tar/zip extraction logic, looking for use of entry names directly in filesystem operations and for missing checks against
..components, absolute paths, or symlink resolution. - Security testing: Include tests that feed crafted archives with traversal payloads into build and packaging tools, verifying they are rejected or safely contained.
- Runtime monitoring: In sensitive environments, monitor for unexpected file writes outside designated build directories during Kaniko executions, using file integrity monitoring or container runtime auditing.
- Automated review tools or custom linters can be configured to flag insecure archive extraction constructs early in development.

