Arbitrary heap exposure in JSON.generate
The JSON generator in Ruby mishandles a custom space string containing a NUL byte, so the length recorded for the string does not match the actual copied data. An attacker who can control the space option could cause arbitrary heap memory to be written into the generated JSON output.
Originally disclosed on HackerOne
- Fixed in 45 days
- Paid in 7 months
- Disclosed after 7 months
Running this snippet can expose arbitrary memory:
require 'json'
state = JSON.state.new
state.space = "\0" * 1024
puts JSON.generate({a: :b}, state)
{"a":
psych/handlers/recorder.rb
tensi0
reeze)
Gem::Specification.new do |s|
# to objects of the same type as the original delegate.
mydata/scm/git/ruby/dist/lib/ruby/2.5.0/json/ext.rb
pass the namP
See http://guides.rubygems.org/specification-reference/ for help
# # constant and class member data initialization...
"b"}
The issues lies in using strdup in generator.c, which will stop after encountering a NULL byte returning a pointer to zero length string, which is not the length stored in space_len. Eventually fbuffer_append will copy the length of the string (e.g. the 1024 above) into the generated buffer.
Simpler snippets like JSON.generate({foo: "bar"}, space: "\0" * 1024 suffer the same issue but for slightly different reason; as fstrndup is using memccpy which will, again, stop copying after encountering a NULL byte returning a pointer to zero length string.
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 generator uses strdup and memccpy to copy the space string, which stop at a NUL byte, while the stored length still reflects the full original size, so a later append copies more bytes than were actually duplicated.
Impact
Arbitrary heap memory could be exposed in the generated JSON output, potentially leaking sensitive data from the process.
What the attacker needed
Attacker controls the JSON space option
How it was found
- Create a JSON state object and set its space option to a string of NUL bytes.
- Call JSON.generate with that state and any object.
- Observe that the output contains memory beyond the intended spacer.
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.
A program can be told to add a custom spacer string when it turns data into JSON text. If that spacer contains a special zero byte, the code that copies it stops early but still records the original length, so it reads past the end of the copied string. That means random memory from the program can end up printed in the JSON output.
What is Info disclosure?
Data the application should keep private (personal data, internal details, other users' records) reaches someone it should not.