CVE-2026-54902: Oj Use-After-Free Vulnerability

HIGH
Advisory Identifier: GHSA-m578-w5vf-rfcm
CWE-416
Published
Advisory Summary

CVE-2026-54902 is a HIGH use-after-free vulnerability (CWE-416: Use After Free) affecting Oj libraries in the RubyGems ecosystem. The issue involves use-after-free in oj::parser saj long key callback. Remediation is available by upgrading affected packages to version 3.17.3.

CVE-2026-54902 at a Glance

CVE Identifier CVE-2026-54902
GHSA Identifier GHSA-m578-w5vf-rfcm
Severity HIGH
CVSS v3.1 Base Score N/A
CWE Classification CWE-416: Use After Free
Affected Product Oj
Package Ecosystem RubyGems
Published Date
Last Updated

How to Fix CVE-2026-54902

To remediate this vulnerability, upgrade all affected Oj dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.

oj fixed: 3.17.3
Upgrade oj to 3.17.3
Verify Direct and Transitive Dependencies

Run the following command in your project directory to verify that all resolved dependencies use the patched release:

inspect dependencies for oj

How to Check if You Are Affected

To determine whether your application is affected by CVE-2026-54902, inspect your project dependencies for Oj:

inspect dependencies for oj

Analyze the terminal output to locate any version of Oj in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 3.17.3), your application is affected.

Note: Verify declared build manifests and deployed runtime container environments.

Technical Details

Vulnerability Mechanism & Technical Analysis

This vulnerability is classified under CWE-416: Use After Free, where the product reuses or references memory after it has been freed. at some point afterward, the memory may be allocated again and saved in another pointer, while the original pointer references a location somewhere within the new allocation. any operations using the original pointer are no longer valid because the memory "belongs" to the code that operates on the new pointer. In Oj, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.

Source Advisory Statement

### Summary `Oj::Parser` in SAJ mode does not protect cached object keys (≥ 35 bytes) from garbage collection. A Ruby callback that triggers GC inside `hash_end` can cause the key string to be reclaimed while the C parser still holds a pointer to it. The subsequent access to the freed string VALUE results in a segfault, confirmed by an RIP pointing to address `0x4242` (a canary-style pattern suggesting control over the freed memory's content). ### Version - **Software**: oj gem - **Affected**: all versions with `ext/oj/saj2.c` / `ext/oj/parser.c` - **Latest tested**: 3.17.1 (confirmed present) ### Details Short keys (≤ 34 bytes) are stored inline on the C stack and are safe. Long keys (≥ 35 bytes) are stored as heap-allocated Ruby String objects passed to `rb_funcall` as the `key` argument. Between the key being resolved and the callback completing, a GC triggered inside the callback (e.g. `GC.start`) can collect the key String, leaving a dangling VALUE. Crash output: ``` long_key_trigger [BUG] Segmentation fault at 0x0000000000004242 close_object+0x260 /ext/oj/usual.c:405 (calls rb_funcall with freed key) parse+0x11ff /ext/oj/parser.c:693 parser_parse+0x145 /ext/oj/parser.c:1408 RIP: 0x7fd1b46d68b7 RDI: 0x0000000000004242 (freed key VALUE) R12: 0x0000000000004242 ``` The freed VALUE `0x4242` shows the attacker-controlled content of the key string was loaded as a pointer — a classic use-after-free indicator. ### Reproduce ```ruby require 'oj' class H < Oj::Saj def add_value(value, key) GC.start(full_mark: true, immediate_sweep: true) if key == 'x' end def hash_start(key); end def hash_end(key); end end p = Oj::Parser.new(:saj) p.handler = H.new p.parse('{"' + 'A' * 35 + '":{"x":1}}') # long outer key, GC fires on inner key ```

CVE-2026-54902 Affected Oj Versions

1 component(s)
Fixed Versions:
3.17.3
Product / Package Ecosystem Affected Versions Fixed Version
oj
RubyGems
< 3.17.3
3.17.3

References

Frequently Asked Questions

What is CVE-2026-54902?
CVE-2026-54902 is a security vulnerability in Oj involving Use-After-Free in Oj::Parser SAJ Long Key Callback under CWE-416: Use After Free.
Which Oj versions are affected?
Affected versions include releases prior to 3.17.3 across affected packages: oj.
What version fixes CVE-2026-54902?
Upgrading to 3.17.3 (or the latest patched release for your respective branch) resolves the vulnerability.
Is oj affected?
Yes, oj is affected in releases prior to 3.17.3. Upgrade your dependency to 3.17.3 or later.
What CWE is associated with CVE-2026-54902?
CVE-2026-54902 is classified under CWE-416: Use After Free (The product reuses or references memory after it has been freed. At some point afterward, the memory may be allocated again and saved in another pointer, while the original pointer references a location somewhere within the new allocation. Any operations using the original pointer are no longer valid because the memory "belongs" to the code that operates on the new pointer.).
How can I check RubyGems dependencies for CVE-2026-54902?
Run the dependency tree command: 'inspect dependencies for oj'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "schema_version" : "1.4.0",
  "id" : "GHSA-m578-w5vf-rfcm",
  "modified" : "2026-06-19T20:47:28Z",
  "published" : "2026-06-19T20:47:28Z",
  "aliases" : [ "CVE-2026-54902" ],
  "summary" : "Oj: Use-After-Free in Oj::Parser SAJ Long Key Callback",
  "details" : "### Summary\n\n`Oj::Parser` in SAJ mode does not protect cached object keys (≥ 35 bytes) from garbage collection. A Ruby callback that triggers GC inside `hash_end` can cause the key string to be reclaimed while the C parser still holds a pointer to it. The subsequent access to the freed string VALUE results in a segfault, confirmed by an RIP pointing to address `0x4242` (a canary-style pattern suggesting control over the freed memory's content).\n\n### Version\n\n- **Software**: oj gem\n- **Affected**: all versions with `ext/oj/saj2.c` / `ext/oj/parser.c`\n- **Latest tested**: 3.17.1 (confirmed present)\n\n### Details\n\nShort keys (≤ 34 bytes) are stored inline on the C stack and are safe. Long keys (≥ 35 bytes) are stored as heap-allocated Ruby String objects passed to `rb_funcall` as the `key` argument. Between the key being resolved and the callback completing, a GC triggered inside the callback (e.g. `GC.start`) can collect the key String, leaving a dangling VALUE.\n\nCrash output:\n```\nlong_key_trigger\n[BUG] Segmentation fault at 0x0000000000004242\n    close_object+0x260    /ext/oj/usual.c:405  (calls rb_funcall with freed key)\n    parse+0x11ff          /ext/oj/parser.c:693\n    parser_parse+0x145    /ext/oj/parser.c:1408\n\nRIP: 0x7fd1b46d68b7  RDI: 0x0000000000004242  (freed key VALUE)\nR12: 0x0000000000004242\n```\n\nThe freed VALUE `0x4242` shows the attacker-controlled content of the key string was loaded as a pointer — a classic use-after-free indicator.\n\n### Reproduce\n\n```ruby\nrequire 'oj'\n\nclass H < Oj::Saj\n  def add_value(value, key)\n    GC.start(full_mark: true, immediate_sweep: true) if key == 'x'\n  end\n  def hash_start(key); end\n  def hash_end(key); end\nend\n\np = Oj::Parser.new(:saj)\np.handler = H.new\np.parse('{\"' + 'A' * 35 + '\":{\"x\":1}}')  # long outer key, GC fires on inner key\n```",
  "severity" : [ {
    "type" : "CVSS_V4",
    "score" : "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"
  } ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "RubyGems",
      "name" : "oj"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "0"
      }, {
        "fixed" : "3.17.3"
      } ]
    } ],
    "database_specific" : {
      "last_known_affected_version_range" : "< 3.17.2"
    }
  } ],
  "references" : [ {
    "type" : "WEB",
    "url" : "https://github.com/ohler55/oj/security/advisories/GHSA-m578-w5vf-rfcm"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/ohler55/oj"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-416" ],
    "severity" : "HIGH",
    "github_reviewed" : true,
    "github_reviewed_at" : "2026-06-19T20:47:28Z",
    "nvd_published_at" : null
  }
}