Remote code execution on rubygems.org
The RubyGems.org application used unsafe YAML deserialization when parsing a gem's checksum file, allowing an attacker to escalate to remote code execution. An attacker could upload a malicious gem to execute arbitrary commands on the server.
- Triaged in 2 hours
- Fixed in 4 days
- Paid in 5 days
- Disclosed after 34 days
Summary by the program
An unsafe object deserialization vulnerability was found in RubyGems. Unfortunately this vulnerability can be used as a way to escalate to a remote code execution exploit.
The report
When parsing a gem POSTed to the /api/v1/gems endpoint, the rubygems.org application immediately calls Gem::Package.new(body).spec inside app/models/pusher.rb. The authors of the application correctly observed that parsing untrusted YAML is dangerous (since it can serialize more or less arbitrary objects), so they monkey-patched the spec parser to use Psych.safe_load set from config/initializers/forbidden_yaml.rb.
However, YAML.load is called directly when parsing the gem's checksum file in Gem::Package#read_checksums. Using classes accessible within the application, I was able to turn this into a call to Marshal.load on attacker-controlled data. From there, I was able to use known Marshal exploitation techniques to achieve code execution on the server (I'm omitting some details here for brevity so that I can submit this report right away).
A proof of concept, poc.gem, is attached. Run the exploit with the following command:
cat poc.gem | curl -H 'Content-Type: application/gzip' --data-binary @- -H 'Authorization: █████' https://rubygems.org/api/v1/gems
I ran the attached PoC twice. It just does a wget to my server.
Please let me know if I should clarify anything! Thanks for running this program.
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
Root cause
Likely the checksum parsing used YAML.load instead of a safe loader, and the resulting objects allowed a chain to Marshal.load on attacker-controlled data.
Impact
An attacker could execute arbitrary code on the server, as demonstrated by a proof of concept that ran a wget command.
What the attacker needed
An API key for gem upload; ability to send a POST request
The fix
The checksum file should not be parsed with YAML.load on untrusted data.
How it was found
- Send a POST request to the /api/v1/gems endpoint with a crafted gem file.
- The application parses the gem and calls the unsafe YAML loader on the checksum file.
- The crafted data triggers a chain to Marshal.load, leading to code execution.
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.
When a website accepts files from users, it must be careful how it reads them. Here, the site used a method that can turn data into live objects, and a malicious file could make the server run commands. This is called remote code execution, meaning an attacker can run their own code on the server.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.