The vulnerability is a path traversal issue within the Buildah container image builder. It can be triggered when processing build contexts from a malicious Git repository or a specially crafted tar archive. The root cause is the improper handling of file paths and symlinks in several key functions responsible for managing build contexts.
The analysis of the security patch applied in version 1.43.2 reveals three vulnerable functions in the define package:
-
define.TempDirForURL: This function was vulnerable because it used the standard filepath.Join to construct paths when dealing with Git repositories. This did not properly sanitize against path traversal attacks using symlinks within the repository, potentially allowing an attacker to force the build process to use a context from an unintended location.
-
define.downloadToDirectory: This function handles downloading build contexts from URLs. It had a fallback mechanism to treat content as a raw Dockerfile if it wasn't a tar archive. The vulnerability lay in writing this Dockerfile. If a previously processed (and malicious) tar archive created a symlink named Dockerfile pointing outside the build directory, this function would follow it, allowing an attacker to write a file to an arbitrary location on the host system.
-
define.stdinToDirectory: This function processes build contexts from standard input and was vulnerable in the same way as downloadToDirectory. A malicious tar archive from stdin could create a symlink that would be followed when the function attempted to write a fallback Dockerfile.
The patch addresses these issues by introducing securejoin.SecureJoin for safe path construction in TempDirForURL and by using a new writeFileInRoot helper function (which utilizes os.OpenRoot) in downloadToDirectory and stdinToDirectory to prevent writing files outside the intended directory, effectively mitigating the symlink-based path traversal.