Breaking Git with a carriage return and cloning RCE
A flaw in Git's configuration parsing allows a malicious repository to write files outside the intended submodule directory when cloned recursively. An attacker could achieve remote code execution by placing a hook script in the .git directory.
dgl.cx
Published on dgl.cx
If you've ever used an old mechanical typewriter, you know that when you get to the end of the line there's a physical action to get back to the start of the line. Sometimes this was done through an actual lever on the typewriter, later models had a button.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the configuration writer does not quote values containing carriage returns, so when the value is read back, the trailing carriage return is stripped, causing a mismatch between the validated path and the actual path used.
Impact
An attacker could achieve remote code execution when a victim clones a malicious repository with --recursive, but only on Unix-like systems that allow control characters in filenames; Windows is not directly vulnerable.
What the attacker needed
Victim uses git clone --recursive; Unix-like filesystem allowing control characters; attacker controls a repository
The fix
The bug was fixed by ensuring that values containing a carriage return are quoted when written to the configuration file.
How it was found
- Create a .gitmodules file with a submodule path that ends with a carriage return.
- Clone the repository recursively using git clone --recursive.
- Git writes the submodule configuration, stripping the carriage return and changing the path.
- The submodule contents are written to a different location, such as inside the .git directory.
- A hook script placed there executes when Git runs the hook.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
Git is a version control system that tracks changes in files. When you clone a repository with submodules (nested repositories), Git reads a configuration file called .gitmodules. A bug in how Git handles carriage return characters in that file lets an attacker trick Git into writing files to unintended locations, potentially leading to code execution.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.