{"vuln_id":"GHSA-q339-8rmv-2mhv","aliases":["CVE-2026-41316"],"description":"ERB has an @_init deserialization guard bypass via def_module / def_method / def_class","published_at":"2026-04-24T15:36:05Z","modified_at":"2026-11-17T21:56:05Z","first_seen_at":"2026-11-21T19:15:20.168985Z","severity":"high","severity_source":"ghsa","cvss_score":8.1,"cvss_vector":"CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:H/A:H","cwe":["CWE-502","CWE-693"],"vendor":null,"product":null,"kev":{"is_kev":false,"date_added":null,"due_date":null,"ransomware":false},"epss":null,"epss_percentile":null,"nvd_status":null,"enriched_at":null,"links":{"nvd":"https://github.com/advisories/GHSA-q339-8rmv-2mhv"},"matched_alias":null,"items":[{"id":2450,"source":{"id":"ghsa","name":"GitHub Security Advisories"},"title":"GHSA-q339-8rmv-2mhv / CVE-2026-41316: ERB has an @_init deserialization guard bypass via def_module / def_method / def_class","summary":"ERB has an @_init deserialization guard bypass via def_module / def_method / def_class\n\n## Summary\n\nRuby 2.7.0 (before ERB 2.2.0 was published on rubygems.org) introduced an `@_init` instance variable guard in `ERB#result` and `ERB#run` to prevent code execution when an ERB object is reconstructed via `Marshal.load` (deserialization). However, three other public methods that also evaluate `@src` via `eval()` were not given the same guard:\n\n- `ERB#def_method`\n- `ERB#def_module`\n- `ERB#def_class`\n\nAn attacker who can trigger `Marshal.load` on untrusted data in a Ruby application that has `erb` loaded can use `ERB#def_module` (zero-arg, default parameters) as a code execution sink, bypassing the `@_init` protection entirely.\n\n## The @_init Guard\n\nIn `ERB#initialize`, the guard is set:\n\n```ruby\n# erb.rb line 838\n@_init = self.class.singleton_class\n```\n\nIn `ERB#result` and `ERB#run`, the guard is checked before `eval(@src)`:\n\n```ruby\n# erb.rb line 1008-1012\ndef result(b=new_toplevel)\nunless @_init.equal?(self.class.singleton_class)\nraise ArgumentError, \"not initialized\"\nend\neval(@src, b, (@filename || '(erb)'), @lineno)\nend\n```\n\nWhen an ERB object is reconstructed via `Marshal.load`, `@_init` is either `nil` (not set during marshal reconstruction) or an attacker-controlled value. Since `ERB.singleton_class` cannot be marshaled, the attacker cannot set `@_init` to the correct value, and `result`/`run` correctly refuse to execute.\n\n## The Bypass\n\n`ERB#def_method`, `ERB#def_module`, and `ERB#def_class` all reach `eval(@src)` without checking `@_init`:\n\n```ruby\n# erb.rb line 1088-1093\ndef def_method(mod, methodname, fname='(ERB)')\nsrc = self.src.sub(/^(?!#|$)/) {\"def #{methodname}\\n\"} << \"\\nend\\n\"\nmod.module_eval do\neval(src, binding, fname, -1) # <-- no @_init check\nend\nend\n\n# erb.rb line 1113-1117\ndef def_module(methodname='erb') # <-- zero-arg call possible\nmod = Module.new\ndef_method(mod, methodname, @filename || '(ERB)')\nmod\nend\n\n# erb.rb line 1170-1174\ndef def_class(superklass=Object, methodname='result') # <-- zero-arg call possible\ncls = Class.new(superklass)\ndef_method(cls, methodname, @filename || '(ERB)')\ncls\nend\n```\n\n`def_module` and `def_class` accept zero arguments (all parameters have defaults), making them callable through deserialization gadget chains that can only invoke zero-arg methods.\n\n### Method wrapper breakout\n\n`def_method` wraps `@src` in a method definition: `\"def erb\\n\" + @src + \"\\nend\\n\"`. Code inside a method body only executes when the method is called, not when it's defined. However, by setting `@src` to begin with `end\\n`, the attacker closes the method definition early. Code after the first `end` executes immediately at `module_eval` time:\n\n```ruby\n# Attacker sets @src = \"end\\nsystem('id')\\ndef x\"\n# After def_method transformation, module_eval receives:\n#\n# def erb\n# end\n# system('id') <- executes at eval time\n# def x\n# end\n```\n\n---\n\n## Proof of Concept\n\n### Minimal (ERB only)\n\n```ruby\nrequire 'erb'\n\nerb = ERB.allocate\nerb.instance_variable_\n\nAffected packages: rubygems:erb\nPatched: erb 4.0.3.1, erb 4.0.4.1, erb 6.0.1.1, erb 6.0.4","url":"https://github.com/advisories/GHSA-q339-8rmv-2mhv","published_at":"2026-04-24T15:36:05Z","updated_at":"2026-11-17T21:56:05Z","fetched_at":"2026-11-21T19:15:18.416846Z","severity":"high","cvss_score":8.1,"epss":0.01148,"is_kev":false,"has_exploit":false,"kev_due_date":null,"vendor":null,"product":"erb","vuln_id":"CVE-2026-41316","vuln_ids":["CVE-2026-41316","GHSA-Q339-8RMV-2MHV"],"tags":["ghsa","rubygems","CWE-502","CWE-693"],"cluster_id":2341,"links":{"self":"https://vulnwatch.ve1ve2.com/api/v1/items/2450","html":"https://vulnwatch.ve1ve2.com/item/2450"}}]}