Technical post
Ruby2Shell (CVE-2026-132825): One Byte Past the Buffer in String#upcase
Ruby2Shell is a heap buffer overflow of a single byte in CRuby's case conversion for legacy encodings that use one byte per character, fixed in Ruby 3.3.14, 3.4.13 and 4.0.10. This post walks through the C code, the arithmetic that makes the 21st ß overflow, how a string like that reaches upcase in a Rails app, and what separates a corrupted byte from remote code execution.

On November 18, 2026 the Ruby core team shipped Ruby 4.0.10, 3.4.13 and 3.3.14 along with a security advisory covering seven vulnerabilities in the regular expression engine and in string case conversion. The one most people talked about is GHSA-34m4-m9fh-gqp5, later assigned CVE-2026-132825 and published by Veria Labs under the name Ruby2Shell.
In plain terms, the bug is this. When Ruby changes the case of certain strings, with upcase for example, it sets aside a block of memory for the result and can write one byte past the end of it. That extra byte lands on memory that belongs to something else in the process. Usually nothing visible happens. But an attacker who controls the string, and who can influence what sits next to that block, may be able to turn the damage into control over what the Ruby process does next. That is what remote code execution, or RCE, means: someone on the other end of an HTTP request getting your server to run their code.
None of this needs a malicious file hosted somewhere. In attacks of this kind the input arrives as ordinary request data, and the instructions that end up running either came in with that data or were already loaded in the process. I go through both options later, with one caveat that matters: the actual Ruby2Shell exploit is not public, so that part describes the general technique, not this specific attack.
The advisory itself is short: "Ruby has a one-byte heap-based buffer overflow in the case conversion of strings. Changing the case of a crafted string may allow remote code execution." It also says the Ruby team is "aware of PoCs that prove this flaw can be used to trigger RCE in some scenarios."
What makes this uncomfortable is where it lives. Nobody treats upcase as dangerous. We audit eval, send, Marshal.load and SQL fragments, not the line that normalizes an HTTP method name or compares two strings without regard to case. That is where this bug lives, in C code that has shipped since Ruby 2.4.
I read the advisories and the public research, diffed the Ruby source around the fix, and ran the trigger on a vulnerable and a patched interpreter. This post is what I found: how the overflow happens in the C code, how a crafted string reaches upcase through a stock Rails stack, and where the line sits between the byte I can demonstrate and the code execution I cannot. I mark throughout what comes from primary sources, what I verified myself, and what is still unknown.
What is confirmed and what is not
The headline mixes up a few things that are known with different levels of certainty.
The bug itself is not in question. Ruby writes one byte past the end of a buffer on the heap. The advisory and the fix commit both describe it, and you can read it in the source. The byte written is S (0x53) when upcasing or s (0x73) when case folding, and it lands right after the output buffer. What it damages depends on what the allocator put there.
Code execution is claimed by several people who have seen it work. The Ruby advisory says proofs of concept have shown RCE "in some scenarios". Veria Labs says it got RCE on stock Rails, and Moritz Sanft, who found the bug independently, reports the same against a plain rails new app. Red Hat's bulletin calls it possible "with careful heap layout manipulation".
How they did it is not public. As of November 23, 2026, Veria Labs plans to release its proof of concept on November 25, 2026 "or later if major vendors have not shipped a fix by then", and Sanft's post leaves the exploit out. None of the sources explain how one byte becomes code execution.
Characters, bytes and encodings
A Ruby String is a sequence of bytes plus an encoding that says how to read them, and the same character can take a different number of bytes in different encodings.
The German sharp s, ß, is a good example. In UTF-8 it takes two bytes. In ISO-8859-1, a legacy encoding where every character is one byte, it is the single byte 0xDF.
utf8 = "ß"
legacy = "\xDF".force_encoding("ISO-8859-1")
puts utf8.encoding
puts legacy.encoding
p utf8.bytes # the bytes that make up the string
p legacy.bytes
p utf8.length # length counts characters
p legacy.length
p utf8.bytesize # bytesize counts bytes
p legacy.bytesize
UTF-8
ISO-8859-1
[195, 159]
[223]
1
1
2
1
Both strings are one character long. They differ in how many bytes that character occupies. force_encoding does not convert anything. It only changes the label Ruby attaches to the bytes.
The bug lives in one quirk of that mapping. Traditional German orthography has no capital form of ß, so the Unicode uppercase mapping of ß is the two letters SS. ISO-8859-1 has no capital sharp s either, so Ruby applies the same rule there:
p utf8.upcase # => "SS"
p legacy.upcase # => "SS"
p legacy.upcase.bytesize # => 2
p legacy.downcase(:fold) # => "ss"
In UTF-8 two bytes in, two bytes out. In ISO-8859-1 one byte in, two out. This is one of the few cases where case conversion in an encoding with one byte per character makes a string longer, and it is the whole reason the bug exists.
The vulnerable implementation
Case conversion in CRuby is split in two layers. string.c owns the methods you call from Ruby and the output buffer. Each encoding provides its own case_map function, in enc/*.c, that knows how to map its own characters.
Who allocates the buffer
When you call upcase on a string that is not pure ASCII, rb_str_upcase ends up in rb_str_casemap. The output may be longer than the input, so it cannot convert in place. It allocates a temporary buffer and asks the encoding to fill it. Here is the code right before the fix, from string.c lines 9856 to 9923, trimmed to the relevant lines:
/* 16 should be long enough to absorb any kind of single character length increase */
#define CASE_MAPPING_ADDITIONAL_LENGTH 20
typedef struct mapping_buffer {
size_t capa;
size_t used;
struct mapping_buffer *next;
OnigUChar space[FLEX_ARY_LEN];
} mapping_buffer;
/* inside rb_str_casemap */
while (source_current < source_end) {
/* increase multiplier using buffer count to converge quickly */
size_t capa = (size_t)(source_end-source_current)*++buffer_count + CASE_MAPPING_ADDITIONAL_LENGTH;
current_buffer = xmalloc(offsetof(mapping_buffer, space) + capa);
/* ... */
buffer_length_or_invalid = enc->case_map(flags,
&source_current, source_end,
current_buffer->space,
current_buffer->space+current_buffer->capa,
enc);
/* ... */
}
For the first buffer, buffer_count is 1, so the capacity is the input length in bytes plus 20. The design is reasonable. Most conversions keep the size, and 20 spare bytes absorb a few characters that grow. If the buffer fills before the input is consumed, case_map returns and the outer while allocates another buffer for the rest with a larger multiplier. The buffers are then joined into the result string.
So the contract is: case_map receives a start pointer to and an end pointer to_end, and it must stop before writing at or past to_end.
The case_map loop
Here is the ISO-8859-1 case_map before the fix, enc/iso_8859_1.c lines 268 to 297:
while (*pp < end && to < to_end) {
code = *(*pp)++;
if (code == SHARP_s) {
if (flags & ONIGENC_CASE_UPCASE) {
flags |= ONIGENC_CASE_MODIFIED;
*to++ = 'S';
code = (flags & ONIGENC_CASE_TITLECASE) ? 's' : 'S';
}
else if (flags & ONIGENC_CASE_FOLD) {
flags |= ONIGENC_CASE_MODIFIED;
*to++ = 's';
code = 's';
}
}
/* ... other characters: at most one byte in, one byte out ... */
*to++ = code;
if (flags & ONIGENC_CASE_TITLECASE) /* switch from titlecase to lowercase for capitalize */
flags ^= (ONIGENC_CASE_UPCASE | ONIGENC_CASE_DOWNCASE | ONIGENC_CASE_TITLECASE);
}
The loop reads one input byte per iteration. For every character except ß it writes one output byte, at the *to++ = code line near the bottom. For ß (SHARP_s, 0xDF) it writes two: one inside the if, one at that same bottom line.
The boundary check is the loop condition, to < to_end. It only guarantees one free byte, which is enough for every character except the one that needs two.
If the loop starts an iteration with one free byte and the next input byte is ß, the first S takes the last valid slot and the second S lands at to_end, one byte past the buffer.
The 20 spare bytes mean a single ß cannot trigger this. Each ß reads one input byte and writes two, so each one eats a byte of the slack. You need 20 to use up the slack and one more to cross the boundary.
Take a string of 21 ß in ISO-8859-1:
- Input length is 21, so the capacity is 21 + 20 = 41 bytes, valid indexes 0 to 40.
- After 20 iterations, 40 bytes are written.
topoints at index 40. - The loop checks
to < to_end: 40 < 41 is true. One byte of room. - The 21st
ßwritesSat index 40, thenSat index 41. Index 41 is outside the buffer.
Position matters as well as count. With 22 ß the capacity is 42, the 21st iteration ends with to exactly at to_end, the loop exits cleanly, and the last ß goes into a second buffer. So by my reading the overflow needs at least 21 ß, lined up so the output position sits at capacity minus one just as a ß comes up. Any other characters in the string shift that position by a byte each, which makes it easy for an attacker to line up. Only the first buffer can overflow. Later buffers are sized at twice the remaining input or more, which always fits.
The same loop was copied into every encoding that stores one character per byte and has a ß at 0xDF. The fix commit touches 14 files: ISO-8859-1, -2, -3, -4, -9, -10, -13, -14, -15 and -16, and Windows-1250, -1252, -1254 and -1257. UTF-8 goes through a different case_map that understands Unicode, and it is not affected.
Which methods reach it
The branches that write two bytes run when the flags contain ONIGENC_CASE_UPCASE or ONIGENC_CASE_FOLD. Following the callers in string.c, and consistent with the lists published by Red Hat and Sanft, that means:
upcaseandupcase!swapcaseandswapcase!downcase(:fold)anddowncase!(:fold)casecmp?, which folds both strings withdowncase(:fold)internally- the corresponding
Symbolmethods
Plain downcase is not affected, because lowercasing never expands ß. capitalize expands a leading ß to Ss, but only for the first character, written at the very start of the buffer where there is always room. Pure ASCII strings, strings tagged as binary (ASCII-8BIT), and calls with the :ascii option take a separate path in string.c that works byte by byte and never reach case_map.
Understanding the heap buffer overflow
The memory that matters here is the heap, a region managed by an allocator (malloc and free in C) that hands out blocks of whatever size the program asks for at run time. Ruby objects, string contents, array storage and this temporary mapping buffer all come from the heap.
Bug reports use a few terms loosely, and this bug makes the difference between them easy to see. The buffer capacity is how many bytes were allocated, here the input length plus 20. The data length is how many of those bytes hold real data. case_map returns it and rb_str_casemap stores it in used. A write out of bounds is a write to an address past the capacity, here one byte at space[capa]. Heap memory corruption is what follows: some other piece of the heap now holds a value its owner did not put there.
The allocator packs blocks next to each other with no guard between them. Nothing in C stops a write at space[capa]. The address is valid memory. It just belongs to something else.
In the overflowing case the returned data length is 42 and the capacity is 41. rb_str_casemap then builds the result by copying used bytes out of the buffer, which reads that one byte past the end too. It is the S just written there, so the result still comes out correct.
Where the byte lands
What the stray byte damages depends on the allocator. This part is general allocator behavior I checked on my own machine, not something the advisories state.
Allocators round requests up to fixed size classes. With glibc on a 64 bit machine the usable sizes are 24, 40, 56, 72, 88 bytes and so on. The mapping buffer request is the 24 byte header (capa, used and next) plus the capacity of input length plus 20, so input length plus 44 in total. I measured it with malloc_usable_size: for input lengths 21 to 27 the request is 65 to 71 bytes and the allocator returns 72, so the overflow byte lands in padding nobody uses. At input length 28 the request is exactly 72 bytes with no padding, and space[capa] is the first byte after the usable area. On glibc that is the lowest byte of the next chunk's size field, the allocator's own bookkeeping for the neighboring block.
Measured with malloc_usable_size on my machine. The advisories do not describe this part.
So on glibc only some input lengths corrupt anything at all, and the attacker picks the input length. Veria Labs puts it the other way around: "The attacker controls the size of the buffer through the input length." Other allocators like jemalloc, which many Ruby deployments use, have different size classes and keep metadata elsewhere, so the neighbor is another allocation's data. Sanft reports reaching code execution under both jemalloc and glibc.
Example
The smallest example of the vulnerable operation is this:
input = "\xDF".force_encoding("ISO-8859-1")
result = input.upcase
p result # => "SS"
p result.bytesize # => 2
This goes through the vulnerable function and shows the expansion from one byte to two. It does not overflow anything. For an input of one byte the buffer has 21 bytes, and the output needs two.
The minimal input that writes out of bounds is the one from the arithmetic above. It is public in several places, including the regression test shipped with the fix:
s = ("\xDF".b * 21).force_encoding("ISO-8859-1")
r = s.upcase
p r.bytesize # => 42
p r == "SS" * 21 # => true
I ran this on Ruby 3.4.12, which is vulnerable, and on Ruby 4.0.10, which is patched. The output is identical on both. The vulnerable interpreter did not crash, raise, or print a warning, and it returned the correct string.
That silence is the point. On a normal build an overflow of one byte leaves no trace. To see it you need an interpreter built with AddressSanitizer, or a tool like Valgrind. I did not reach for either one here. Correct output tells you nothing about memory safety. This snippet writes one byte out of bounds and hands you back the right answer, and that is all it does. Running it executes no one's code.
From memory corruption to remote code execution
A heap overflow does not execute code. It changes data. To see why that can still end in code execution, picture what lives in the memory of a running Ruby process: the bytes of your strings, Ruby objects, pointers from one piece of memory to another, and the bookkeeping the interpreter and the allocator keep for themselves. All of it sits side by side on the heap, which is why one byte written past the end of a buffer lands on somebody else's data.
Some of that data decides what the program does next. A program keeps addresses that say where to go: which function to call, where to return when the current one finishes. If an attacker can corrupt one of those, directly or by corrupting something that later causes the program to overwrite it, they can send the process to instructions of their choosing. This is called control flow hijacking, and it is the step that turns a memory safety bug into remote code execution. It does not come for free with every overflow. The attacker needs precise control over what gets corrupted, and protections like ASLR and NX exist to make that harder.
The general shape of that work is well understood from decades of exploitation. The specific chain for Ruby2Shell is the part nobody has published. In general terms, it usually goes like this. The attacker sends a string in an affected encoding with the right number of ß in the right places, and the conversion writes one byte out of bounds. The value is always 0x53 or 0x73. The attacker does not get to choose it, only where the buffer ends.
On its own, that byte hits whatever happened to sit next to the buffer, which is most likely useless to the attacker, or it crashes the process. So the next step is to influence what sits there. By sending requests that allocate and free memory of chosen sizes in a chosen order, an attacker can steer which object ends up next to the buffer. This is usually called heap grooming, and it is what Red Hat means by "careful heap layout manipulation".
One byte is a weak primitive, so the goal is to turn it into a stronger one. If it lands in a length field, a pointer or allocator metadata, some later and entirely legitimate operation may read or write far more memory than it should. Overflows of a single byte have a long history of being escalated this way. With a strong enough read and write primitive, the attacker may be able to overwrite something the process will later use as a code address, such as a function pointer. That is the control flow hijacking step from above. Whether Ruby2Shell works this way is not something the public sources say.
That raises the question of what it jumps to. The attacker does not need to upload an executable file, and there are two classic answers.
The first is shellcode. The attacker places raw machine instructions somewhere in process memory, for example inside a request body, and sends execution there. On modern systems that rarely works directly, because data memory is marked as not executable (NX, also called DEP), and the CPU refuses to run instructions from the heap or the stack.
The second is return oriented programming, or ROP. Instead of bringing new instructions, the attacker reuses ones that are already loaded and executable: short sequences inside the Ruby binary, libc or other shared libraries. Chaining their addresses builds the behavior the attacker wants, often just enough to call an existing function such as system. This gets around NX, but the attacker has to know where that code sits in memory, which address space layout randomization (ASLR) exists to hide. Getting past ASLR usually takes a separate information leak.
Shellcode and ROP are textbook techniques, not the Ruby2Shell exploit. No public source says which path the proof of concept takes, and there are others, such as corrupting data the interpreter already trusts without ever hijacking a native code pointer. The diagram is a map of the terrain, not a route anyone has walked in public.
So a memory corruption bug is not an RCE by itself. It becomes one only when the attacker can use the corrupted memory to steer what the process executes. What the sources do establish is that for this bug the chain can be completed in at least some configurations, and that success depends on things outside the bug itself: the allocator, the memory layout of the target process, how predictable its allocations are, and which runtime protections are on. The scorers disagree sharply. Red Hat gives it 9.8 with a network attack vector. The CVE record carries 7.4, local vector, high attack complexity. GitHub's advisory calls it critical. They are all looking at the same byte.
Exposure in Rails and Sinatra applications
A vulnerable interpreter is not the same thing as an exploitable application. For the bug to be reachable, three things have to be true at once:
- A string whose bytes come from the attacker.
- That string is tagged with one of the affected encodings.
- The application, or a library it uses, calls one of the affected case conversion methods on it.
The second condition is the unusual one. Nearly everything in a modern Ruby web application is UTF-8: request parameters, database strings, source files. An attacker cannot normally hand you an ISO-8859-1 string.
Except through multipart forms. In a multipart/form-data request every part can carry its own Content-Type, and Rack's multipart parser honors a charset on text/plain parts. From Rack::Multipart::Parser#tag_multipart_encoding in Rack 3.2.7:
if k == "charset"
encoding = find_encoding(v)
end
# ...
name.force_encoding(encoding)
body.force_encoding(encoding)
That hands the client conditions 1 and 2 for any form field. Condition 3 is met by middleware that runs before any application code. Rack::MethodOverride is what lets an HTML form send PATCH or DELETE through a hidden _method field on a POST:
def method_override(env)
req = Request.new(env)
method = method_override_param(req) ||
env[HTTP_METHOD_OVERRIDE_HEADER]
begin
method.to_s.upcase
rescue ArgumentError
env[RACK_ERRORS].puts "Invalid string for method"
end
end
It reads the _method parameter from the parsed body and calls upcase on it. This is the path Sanft's post identifies, and I confirmed both pieces in the Rack source.
This lines up with Veria Labs' exposure table, which lists Rails and Sinatra apps in the classic style as affected "in their default configuration" and other frameworks and plain Rack as "not exposed by default". Veria does not name the code path, but the defaults match. Rails adds Rack::MethodOverride to its default middleware stack unless the app is configured as api_only. Sinatra turns method_override off in Sinatra::Base and turns it on in Sinatra::Application, the classic style where routes live at the top level of a file. A plain Rack app only has it if someone added the middleware.
Reachable is not the same as exploitable. An unauthenticated request can trigger the write past the buffer on a vulnerable interpreter, but turning that write into code execution still depends on everything in the previous section.
Dropping this middleware does not make an application safe either. Any code that changes the case of a string in a legacy encoding is another entry point: a CSV import that reads Windows-1252 files and upcases a column, a mail processor that handles ISO-8859-1 bodies, a header or filename comparison with casecmp?. Those depend on each application, and nobody can list them for you.
An application that only ever handles UTF-8 never reaches the vulnerable function, whatever Ruby it runs on. Being sure of that is the hard part, because the multipart parser will happily tag an incoming string with whatever encoding the client declared.
On the Ruby side, the fix is commit d582f8f by Yusuke Endoh, dated October 28, 2026. The commit message explains it completely:
The case_map functions of ISO-8859-x and Windows-125x map the sharp s (0xDF) to two bytes but checked for only one byte of room in the output buffer, so they wrote one byte past the buffer. Check for two bytes.
The change is a single line, applied to each of the 14 encoding files:
- while (*pp < end && to < to_end) {
+ while (*pp < end && to_end - to > 1) {
code = *(*pp)++;
if (code == SHARP_s) {
if (flags & ONIGENC_CASE_UPCASE) {
Before, the loop continued while at least one byte was free. After, it continues only while at least two bytes are free, which is the most any single iteration can write. Nothing else in the function changes. The two writes for ß are still there, and they are now always in bounds.
The fix is this small because the rest of the machinery already handled a full buffer. When the patched loop stops with one byte still free and input remaining, case_map returns normally. Back in rb_str_casemap, the outer while (source_current < source_end) sees unconsumed input, allocates a second buffer sized at twice the remaining input plus 20, and continues. The buffers are concatenated into the result. At most one byte goes unused at the end of the first buffer, and that is the entire cost.
The commit also adds a regression test, test_single_byte_sharp_s_expansion, that runs upcase, downcase(:fold), swapcase, capitalize and casecmp? on strings of 1 to 64 ß in each of the 14 encodings. Note that a test like this only catches the bug when the test suite runs under a memory checker. As shown above, the vulnerable code returns the right answer.
The advisory credits Stephan Lipp (Ada Logics) and Anthropic with the report. Veria Labs, and Moritz Sanft working with OpenAI, found it independently.
Sources
- Ruby security advisory: Multiple vulnerabilities in Regexp and String case conversion, ruby-lang.org, November 18, 2026
- GitHub advisory: GHSA-34m4-m9fh-gqp5, Heap buffer overflow in case conversion of strings
- Fix commit: ruby/ruby@d582f8f, Fix a heap buffer overflow in case mapping of the sharp s
- Source before the fix:
enc/iso_8859_1.candstring.c,rb_str_casemap - Veria Labs: Ruby2Shell (CVE-2026-132825)
- Red Hat: RHSB-2026-021
- Moritz Sanft: post on the case mapping bugs
- CVE record: CVE-2026-132825
- Release announcements: Ruby 4.0.10, Ruby 3.4.13, Ruby 3.3.14
- Rack 3.2.7:
Rack::MethodOverrideandRack::Multipart::Parser