Arbitrary File Write in Dulwich via Symlink Directory Traversal
The Dulwich library stash pop implementation fails to validate symlinks, allowing attackers to write arbitrary files outside the repository worktree and achieve remote code execution.
The Dulwich library contains a path traversal vulnerability in its stash.pop() function, specifically within dulwich/stash.py. The vulnerability arises because the library uses os.path.exists() to verify directory paths before writing stashed files. Because os.path.exists() follows symlinks, it fails to detect when a target path resolves to a location outside the repository worktree. The validate_path() function provides insufficient validation as it only checks against forbidden filenames and does not account for symlink resolution. This flaw affects Dulwich versions 0.22.5 through 1.2.7. An attacker can weaponize this by creating a repository with carefully crafted symlinks that, when popped by a victim, overwrite sensitive system files or local configuration files.
Attack Chain
- Attacker creates a malicious git repository containing a symlink (e.g.,
linkpointing to../../.git/hooks). - Attacker creates a stash containing an executable payload file nested within that symlinked path (e.g.,
link/post-checkout). - Victim clones the malicious repository and checks out the branch containing the symlink.
- Victim executes
stash.pop()on the malicious stash content. - Dulwich evaluates the target path, where
os.path.exists()confirms the presence of the directory via the symlink, bypassing safety checks. - The
build_file_from_blob()function writes the payload content directly to the resolved location. - The payload is written to the victim's local
.git/hooks/post-checkoutfile. - The victim triggers the payload upon the next git checkout, resulting in Remote Code Execution.
Impact
Successful exploitation allows for arbitrary file writes on the victim's system. By targeting critical hooks like .git/hooks/post-checkout within the local environment, an attacker can gain arbitrary command execution under the user context of the victim, leading to full compromise of the local development workstation.
Recommendation
- Update the Dulwich library to a patched version once available.
- Audit all automated tooling that utilizes Dulwich for git repository management to ensure they are running with the least privilege necessary.
- Monitor local development directories for unexpected creation or modification of git hook files using File Integrity Monitoring (FIM).
- For developers using the Dulwich library, implement path canonicalization using
os.path.realpath()before file write operations to verify the target directory resides within the expected worktree.
Immediate actions
Inventory all internal services and development workstations using the Dulwich library
Mitigations
Upgrade or patch Dulwich in all affected environments
dulwich library vulnerability