RC RANDOM CHAOS

Twenty-one ß characters walk one byte past Ruby's heap

A one-byte heap overflow in Ruby's single-byte case conversion gives pre-auth RCE; fixed in Ruby 3.3.14, 3.4.13 and 4.0.10.

· 2 min read
Twenty-one ß characters walk one byte past Ruby's heap

Twenty-one ß characters are enough to walk one byte past the end of a heap buffer in Ruby. Veria Labs’ writeup, which they call ruby2shell, traces a one-byte heap overflow in Ruby’s single-byte case conversion to pre-auth remote code execution, reachable in the default configuration of common Ruby web frameworks and self-hosted software. It is fixed in Ruby 3.3.14, 3.4.13 and 4.0.10, under GHSA-34m4-m9fh-gqp5, with a CVE still pending.

The mechanism is specific to the German sharp s. In most of Ruby’s single-byte encodings there is no capital ß, so upcasing it produces two bytes, SS. The conversion loop writes both but only bounds-checks one against the output end (to < to_end). Ruby allocates the output buffer as the input length plus 20 bytes of slack. Every ß in the input turns one input byte into two output bytes, eating one byte of that slack. An input carrying 21 of them exhausts the 20-byte margin and writes one byte past the buffer.

The byte that lands out of bounds is S (0x53) on upcase or s (0x73) on case-fold, placed directly after the output buffer. The attacker controls the buffer’s size through the length of the input string, which is the useful part: a single-byte overflow sitting next to an allocation whose size you choose is a workable heap primitive. Copying the result back out of the buffer then reads one byte out of bounds as well.

The same loop sits in several ISO-8859 and Windows-125x encodings and has been there since Ruby 2.4.0. It is reached through upcase, swapcase, downcase(:fold) and casecmp?. UTF-8 strings take a different code path and are not affected. The vulnerable path only opens when a string in one of the legacy single-byte encodings reaches one of those methods.

That is the condition to check in your own code. The listed frameworks reach the bug in their default configuration, so for those the answer is to upgrade. Beyond that, application code that upcases or case-folds strings received in a legacy encoding is exposed too, even on a framework that is safe by default. Anywhere you call upcase, swapcase, downcase(:fold) or casecmp? on attacker-supplied input, confirm the string’s encoding rather than relying on a framework default to save you.

Remediation is a Ruby upgrade to 3.3.14, 3.4.13 or 4.0.10. For self-hosted products that bundle their own Ruby, the latest release of several popular packages still ships an affected interpreter, so install the vendor’s fixed release. Veria Labs says it will publish a proof of concept in a week, once people have had time to upgrade, so the window to patch ahead of a public exploit is short.

Share

Keep Reading

Latest on the Wire

Full wire →

New signal daily · RSS

Stay in the loop

New writing delivered when it's ready. No schedule, no spam.