The vulnerability is a classic path traversal issue within the tar archive extraction functionality of kaniko. The root cause is the usage of filepath.Join to construct file paths from tar headers without properly sanitizing the filenames. An attacker could craft a tar archive with entries containing ../ sequences (e.g., ../pwned.txt) to write files outside of the intended destination directory.
The analysis of the patch a370e4b1f66e6e842b685c8f70ed507964c4b221 clearly shows the vulnerable code and the corresponding fix. The primary vulnerable function is ExtractFile in pkg/util/fs_util.go, where the insecure path concatenation occurs. The UnTar function is the entry point that reads the malicious tar archive and passes the data to ExtractFile. Additionally, the GetFSFromLayers function had a similar flaw when processing whiteout files (.wh.), which could lead to arbitrary file deletion outside the target directory. The fix involves replacing the insecure filepath.Join with securejoin.SecureJoin and adding explicit checks to deny paths containing ../, thus confining all file operations to the intended directory.