Skip to content
RCE Write-up Advanced

Empowering weak primitives: file truncation to code execution with Git

An argument injection in a service that runs git blame allows an attacker to pass an undocumented --output option, which truncates arbitrary files. By truncating .git/HEAD, the attacker corrupts the local repository so that Git falls back to a malicious repository in the work tree, whose config runs a custom fsmonitor command, achieving code execution.

Thomas Chauchefoin Follow

Published on sonarsource.com

During recent security research, we came up with a fun "trick" that we later shared in a Capture the Flag challenge for the Hack.lu CTF and our Code Security Advent Calendar. We received good feedback and wanted to share the details with a broader audience.

Thomas Chauchefoin (@swapgs), on sonarsource.com

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 service passes user input directly into the git blame command line without sanitising it, allowing argument injection; git blame then accepts an undocumented --output option that truncates files.

Impact

An attacker could execute arbitrary code on the server hosting the service.

What the attacker needed

Service clones attacker-controlled repo; service runs git blame with user input

How it was found

  1. Create a malicious Git repository with a config that sets fsmonitor to a command and bare to false.
  2. Trigger the argument injection to run git blame with --output pointing at .git/HEAD, truncating it.
  3. Invoke git blame again so Git uses the malicious repository and executes the fsmonitor command.

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.

The service lets users supply extra options to the git blame command. One hidden option, --output, creates or empties a file. By emptying a key file in the local repository, the attacker makes Git use a fake repository they control, which runs their command.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

More by Thomas Chauchefoin

View all

5 more disclosed reports, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.