Remote code execution in Homebrew by compromising the official Cask repository
A parsing flaw in the git_diff library allowed a malicious pull request to be disguised as a harmless version bump, bypassing automated review and merging into the official Homebrew Cask repository. An attacker could then execute arbitrary Ruby code on users' machines when they ran brew commands.
2021-04-21 2092 字 Homebrew Vulnerability Ruby Supply Chain
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
The git_diff library incorrectly parsed diff files, allowing file path information to be changed multiple times and treating added lines as file paths, which bypassed the review script's checks.
Impact
An attacker could execute arbitrary Ruby code on users' machines when they ran brew commands, potentially compromising their systems.
What the attacker needed
Ability to submit a pull request to Homebrew Cask; victim runs brew command
The fix
The vulnerability was fixed by updating the review script and possibly the git_diff library to correctly parse diffs.
How it was found
- Fork the Homebrew Cask repository and modify a cask file to include Ruby code that prints a message.
- Craft the diff so that added lines match the pattern for file path information, causing git_diff to treat them as paths rather than code changes.
- Submit a pull request with the crafted diff.
- The automated review script approves the pull request because it appears to have zero line changes.
- The pull request is automatically merged.
- Run brew install or brew cleanup to execute the malicious code.
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.
Homebrew is a package manager for macOS that lets you install software. It uses automated scripts to check pull requests, but a bug in a library that reads code changes allowed a bad pull request to sneak through and run harmful code on users' computers when they used Homebrew.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.