Oj gem CVE-2026-54898
LOWAttack ChainAIDerived
Hypothetical attack flow derived from CVE metadata – not a confirmed exploit chain
-
ReconLoad malicious or buggy SAJ handler in Ruby process
-
DeliveryInvoke Oj::Parser#parse with attacker-influenced JSON
-
ExploitCallback (hash_start) calls String#replace with larger payload
-
InstallRuby frees original string buffer via ruby_sized_xfree
-
C2C parser dereferences dangling const byte* at parser.c:607
-
ExecuteRead freed heap memory, corrupt parser state
-
ImpactInformation disclosure or process crash
Vulnerability Assessment
| Weakness |
CWE
ASVS 5.0
V15 - Secure Coding and Architecture Memory lifecycle management
MITRE ATT&CK
2 techniques across 2 tactics: Execution, Privilege Escalation The technique list with ATT&CK links and the kill-chain view is part of the Basic plan and up. |
| Exploitation Conditions | The application must instantiate Oj::Parser with the :saj or :saj2 mode and assign a handler whose callbacks (hash_start, hash_end, array_start, array_end, or add_value) mutate the exact String object passed to parser.parse(json) in a way that grows it beyond its embedded capacity, forcing Ruby to reallocate and free the original buffer; mutation via String#replace, <<, gsub!, or concat with a larger payload is sufficient. Exploitation is limited by the fact that the attacker must control the handler code (typically application-level Ruby), not just the JSON input - supplying malicious JSON to an application that uses Oj.load or a non-mutating SAJ handler does not trigger the bug. Versions of the oj gem below 3.17.2 containing ext/oj/parser.c are affected; the fixed release is 3.17.3. |
| Impact | No CVSS vector was published in the input, so risk must be reasoned from the technical detail and CWE. Realistically this is a low-to-moderate severity issue: exploitation requires the attacker to control the SAJ/SAJ2 handler object passed to Oj::Parser, which is application code, not raw JSON input - an attacker who can only supply JSON to a parser using a benign application-defined handler cannot trigger the UAF. KEV status is not listed, EPSS is unpublished, and no public exploit identified at time of analysis, though the GHSA advisory contains a complete reproducer that demonstrates use-after-free reliably under ASAN. The tag list ('Memory Corruption, Information Disclosure, Use After Free') overstates the practical attack surface because the realistic trigger is malicious or buggy in-process handler code, not remote untrusted JSON; treat as a correctness/hardening fix rather than an exposed RCE primitive unless a downstream library exposes callback registration to untrusted users. |
| Exploit ScenarioDerived | A developer writes (or imports) a SAJ-style handler whose hash_start callback resizes the original JSON string - for example, a logging wrapper that calls @json.replace(redact(@json)) - and when Oj::Parser#parse is invoked the C parser continues reading from the freed buffer at parser.c:607, producing memory corruption that can leak adjacent heap contents into subsequently parsed values or crash the Ruby worker. The published GHSA reproducer (Mutator class calling @json.replace('x' * 1_000_000) inside hash_start) demonstrates the use-after-free deterministically under AddressSanitizer. |
Get an e-mail when this CVE is exploited, patched or re-scored. Free account.
Alert meAnalysisAIv1 – Jun 19, 2026
Heap use-after-free in the Oj Ruby JSON parser allows malicious SAJ/SAJ2 callback handlers to dereference freed memory by mutating the input JSON string mid-parse, potentially leading to memory disclosure or arbitrary code execution within the Ruby process. The flaw affects the oj gem at versions below 3.17.2 and is triggered when a callback such as hash_start invokes String#replace with a larger payload, causing Ruby to reallocate the backing buffer that the C parser still references. No public exploit identified at time of analysis, but a fully working reproducer is published in the upstream GHSA advisory.
RemediationAI
Vendor-released patch: upgrade the oj gem to version 3.17.3 or later (the GHSA advisory specifies 'fixed in: 3.17.3'); update Gemfile to 'gem "oj", ">= 3.17.3"' and run bundle update oj, then redeploy any long-running Ruby processes so the patched native extension is loaded. Until upgrade is possible, the only meaningful workaround is to audit and constrain SAJ/SAJ2 handler classes so callbacks (hash_start, hash_end, array_start, array_end, add_value) never invoke mutating methods on the JSON string being parsed - specifically forbid String#replace, <<, gsub!, concat or any operation that can resize the source buffer; alternatively, route untrusted JSON through Oj.load or Oj::Doc instead of Oj::Parser with custom SAJ handlers. Refer to https://github.com/ohler55/oj/security/advisories/GHSA-q2gm-54r6-8fwm for upstream details; note that the handler-audit workaround relies on developer discipline and offers no runtime enforcement.
Affected Packages
Source: OSV (Open Source Vulnerabilities)
Affects your stack
Affects 1 package in rubygems
Checking this CVE against your own stacks and servers is part of the Basic plan and up.
Technical ContextAI · show details
Oj is a high-performance JSON parser written as a Ruby C extension (pkg:rubygems/oj), widely used as a drop-in replacement for the standard JSON library and by frameworks such as Rails for serialization. The vulnerability lives in ext/oj/parser.c where parser_parse extracts a raw const byte* via StringValuePtr() from the Ruby String object and passes it directly into the parse() loop at line 607. This pattern violates Ruby's C-API contract because user-controlled SAJ/SAJ2 callbacks executed mid-parse can mutate the same String (via String#replace, <<, gsub!, etc.), causing ruby_sized_xfree to release the embedded buffer and leaving the C parser with a dangling pointer - a textbook CWE-416 (Use-After-Free) caused by failing to re-fetch the buffer pointer after re-entering Ruby code.
Know when the next one hits your stack
- Watch products and get an email when something new is published
- Check this CVE against your own stack and servers
- Exploit and PoC references, threat intelligence from 22 sources
No credit card · 7-day full trial
Severity by source
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
Exploitation requires in-process control of the SAJ handler code (PR:H, AV:L) and a race-like buffer-realloc condition (AC:H); UAF yields limited memory disclosure and possible crash, no scope change.
Primary rating from Vendor (https://github.com/ohler55/oj).
CVSS VectorVendor: https://github.com/ohler55/oj
Lifecycle Timeline
1DescriptionCVE.org
Summary
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.
Version
- Software: oj gem
- Affected: all versions with
ext/oj/parser.c - Latest tested: 3.17.1 (confirmed present)
Details
ext/oj/parser.c, parser_parse → parse:
static VALUE parser_parse(VALUE self, VALUE json) {
const byte *ptr = (const byte *)StringValuePtr(json); // raw pointer into Ruby string
// ...
parse(p, ptr); // ptr used throughout; any realloc frees the backing buffer
}// parser.c:607
static void parse(ojParser p, const byte *json) {
const byte *b = json;
// ...
for (; '\0' != *b; b++) { // ← UAF: reads freed memory after callback resizes jsonRuby'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.
ASAN report:
==372273==ERROR: AddressSanitizer: heap-use-after-free on address 0x51900008ed81
READ of size 1 at 0x51900008ed81 thread T0
#0 parse /ext/oj/parser.c:607
#1 parser_parse /ext/oj/parser.c:1408
0x51900008ed81 is located 1 bytes inside of 1023-byte region [0x51900008ed80, 0x51900008f17f)
freed by thread T0 here:
#0 free
#1 ruby_sized_xfree (libruby-3.3.so.3.3)
Shadow bytes: [fd]fd fd fd fd fd ... (entire region freed)Reproduce
require 'oj'
class Mutator
def initialize(json) = (@json = json; @done = false)
def hash_start(key)
return if @done; @done = true
@json.replace('x' * 1_000_000)
# triggers String realloc, frees original buffer
end
def hash_end(key); end
def array_start(key); end
def array_end(key); end
def add_value(value, key); end
end
json = '{"a":1,"pad":"' + ('A' * 1000) + '","z":2}'
parser = Oj::Parser.new(:saj)
parser.handler = Mutator.new(json)
parser.parse(json)Executive ViewAI
The Oj Ruby JSON parser gem (versions below 3.17.2) contains a vulnerability enabling arbitrary code execution within Ruby applications processing JSON data, with no public exploit identified at time of analysis. This impacts any organization using Oj to parse user input or external JSON feeds; a published proof-of-concept demonstrates the vulnerability is readily exploitable. Immediate inventory and interim security controls are required.
No vendor-released patch identified at time of analysis. Interim mitigations: disable SAJ/SAJ2 callbacks in Oj configuration; implement strict JSON input validation and schema validation; restrict Oj parsing to trusted JSON sources only; monitor application behavior for heap corruption or unexpected process termination; implement network-based detection rules for exploitation attempts; establish incident response procedures for potential system compromise.
Threat Intelligence Overview
How this decision was reached
The engine reads each signal below and applies a fixed list of rules from top to bottom. The first rule that matches sets the recommended action – like a statement, every line is shown.
What every action level means
SSVC AssessmentENISA
Priority Score
Attack Technique
Use-after-free vulnerabilities occur when a program continues to access memory through a pointer after that memory has been deallocated.
Memory corruption occurs when a program writes data beyond the boundaries of allocated memory regions or accesses memory in unintended ways, violating the integrity of the process's address space.
Information disclosure occurs when an application unintentionally exposes sensitive data that aids attackers in reconnaissance or directly compromises security.
GHSA-q2gm-54r6-8fwm
NVD
CVE.org
EPSS
Exploit-DB