Skip to content
Memory corruption Disclosed report Advanced

Heap corruption in string.c tr_trans() due to undersized buffer

A heap buffer overflow occurs in Ruby's string translation when translating between UTF-32 encoded strings, because the destination buffer is too small for the multi-byte characters written.

guido Follow

Originally disclosed on HackerOne

  • Disclosed after 7 days

(originally send by e-mail on 6 Jun 2016)

Configure with ASAN AddressSanitizer:

mkdir install; CFLAGS="-fsanitize=address" ./configure
--disable-install-doc --disable-install-rdoc --disable-install-capi
-prefix=`realpath ./install` && make -j4 && make install

Then execute:

$ ./ruby -e '"a".encode("utf-32").tr("b".encode("utf-32"),
"c".encode("utf-32"))'
=================================================================
==17122==ERROR: AddressSanitizer: heap-buffer-overflow on address
0x602000014a98 at pc 0x7ff04065cf01 bp 0x7ffdfe7629b0 sp 0x7ffdfe7629a8
WRITE of size 4 at 0x602000014a98 thread T0
...
...

The actual corruption occurs here:

C
6196     TERM_FILL(t, rb_enc_mbminlen(enc));

Guido

The original report as published on HackerOne, by guido. 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 buffer allocated for the translated string was sized using the minimum character length rather than the actual multi-byte width of the UTF-32 characters, so the fill operation wrote beyond the allocation.

Impact

The overflow writes four bytes past the allocated buffer, which could crash the interpreter and may allow memory corruption, though the report does not demonstrate code execution.

What the attacker needed

Ability to pass UTF-32 strings to tr

How it was found

  1. Build Ruby with the AddressSanitizer flag enabled.
  2. Run Ruby with an expression that encodes a string to UTF-32 and calls tr with UTF-32 arguments.
  3. Observe the AddressSanitizer heap-buffer-overflow report.

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 reserves a fixed amount of space for a copy. Here the space was too small for the characters written, so it wrote past the end of its buffer, corrupting nearby memory. This kind of mistake can crash the program.

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 by guido

View all

49 more disclosed reports, best first.

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.