Broken parsing of Git diff allows an attacker to inject arbitrary Ruby scripts to Casks on official taps
A flaw in parsing Git diff files allowed a crafted diff to be misinterpreted, letting an attacker inject arbitrary Ruby code into a Homebrew cask. When a user installed the tampered cask, the injected code would execute, potentially leading to remote code execution.
- Fixed in 3 hours
- Disclosed after 3 days
Summary by the program
https://brew.sh/2021/04/21/security-incident-disclosure/
Summary by the researcher
https://blog.ryotak.me/post/homebrew-security-incident-en/
The report
Description
Due to improper parsing of Git diff in Homebrew/actions/review-cask-pr, it's possible to confuse parser to ignore additional lines. Which leads injection of malicious Ruby scripts.
Root cause
review-cask-pr uses the git diff file to check if the pull request is "simple" enough to automatically merge it.
To parse the git diff file, it uses git_diff gem with some modifications.
https://github.com/Homebrew/actions/blob/master/review-cask-pr/git_diff_extension.rb
Since git_diff has a small bug that allows a crafted diff file to confuse additions as a a_path, it's possible to confuse review-cask-pr.
https://github.com/anolson/git_diff/blob/21913c2a51661449a7250cc3a5ba5f5f4f128959/lib/git_diff/file.rb#L61-L62
Steps to reproduce
- Fork Homebrew/homebrew-cask.
- Modify a cask file to add following lines:
++ "b/#{puts 'Going to report it - RyotaK (https://hackeorne.com/ryotak)';b = 1;Casks = 1;iterm2 = {};iterm2.define_singleton_method(:rb) do 1 end}"
++ b/Casks/iterm2.rb
- Open a pull request on Homebrew/homebrew-cask.
- BrewTestBot will approve these changes.
https://github.com/Homebrew/homebrew-cask/pull/104191
Impact
Injected script will be evaluated once someone installed the cask, which may allows remote code execution.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Impact
The injected script would be evaluated when someone installed the cask, which may allow remote code execution.
What the attacker needed
Ability to fork the repository; ability to open a pull request
How it was found
- Fork the Homebrew cask repository.
- Modify a cask file to add crafted lines that exploit the diff parsing bug.
- Open a pull request with the modified cask.
- The automated review bot approves the changes.
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 report has not been written yet. The Original tab links to the source.
Homebrew is a package manager for macOS that installs software using recipes called casks. The review tool that checks pull requests used a library to read the changes, but that library could be tricked by a specially crafted change so that extra lines were ignored. This let an attacker slip harmful code into a cask, which would run when someone installed it.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.