CVE-2026-54502: Oj: Stack Buffer Overflow in Oj.dump via Large Indent
Summary
Oj.dump is vulnerable to a stack-based buffer overflow when a large :indent value is provided by the developer. fillindent in dump.h calls memset(indentstr, ' ', (sizet)opts->indent) without validating the size. When opts->indent is set to INTMAX (2,147,483,647), the (sizet) cast preserves the large value and memset writes 2 GB into the stack-allocated out buffer (4,184 bytes), corrupting the stack and crashing the process.
Version
- Software: oj gem - Affected: all versions with ext/oj/dump.h - Latest tested: 3.17.1 (confirmed present)
Details
ext/oj/dump.h, line 77:
c static void fillindent(Out out, int depth) { if (0 < out->opts->indent) { sizet len = (sizet)(out->opts->indent depth); // ... memset(out->buf + ..., ' ', len); // len = 2147483647 depth
The indent option is accepted as a plain Ruby integer and stored as int without range validation. Multiplying by depth can produce a value larger than any stack or heap buffer.
ASAN report: ==69820==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fd1fc201278 WRITE of size 2147483647 at 0x7fd1fc201278 thread T0 #0 memset #1 fillindent /ext/oj/dump.h:77 #2 dumparray /ext/oj/dumpcompat.c:165 #3 ojdumpobjtojsonusingparams /ext/oj/dump.c:818 #4 dumpbody /ext/oj/oj.c:1429 #5 dump /ext/oj/oj.c:1480 Address is in stack of thread T0 at offset 4728 in frame: #0 dump /ext/oj/oj.c:1453 [544, 4728) 'out' <== Memory access at offset 4728 overflows this variable
Reproduce
ruby require "oj" obj = [0] Oj.dump(obj, mode: :compat, indent: 2147483647)
Workaround
The develop should not use extreme indents and should not offer the option for users to dump Ruby data with unlimited indentation size.
Other sources
Oj (Optimized JSON) is a JSON parser and Object marshaller packaged as a Ruby gem. In versions prior to 3.17.2, Oj.dump is vulnerable to a stack-based buffer overflow when a large :indent value is provided by the developer. fillindent in dump.h calls memset(indentstr, ' ', (sizet)opts->indent) without validating the size. When opts->indent is set to INTMAX (2,147,483,647), the (sizet) cast preserves the large value and memset writes 2 GB into the stack-allocated out buffer (4,184 bytes), corrupting the stack and crashing the process. This issue has been fixed in version 3.17.2.
— NVD
Oj (Optimized JSON) is a JSON parser and Object marshaller packaged as a Ruby gem. In versions prior to 3.17.2, Oj.dump is vulnerable to a stack-based buffer overflow when a large :indent value is provided by the developer. fillindent in dump.h calls memset(indentstr, ' ', (sizet)opts-indent) without validating the size. When opts-indent is set to INTMAX (2,147,483,647), the (sizet) cast preserves the large value and memset writes 2 GB into the stack-allocated out buffer (4,184 bytes), corrupting the stack and crashing the process. This issue has been fixed in version 3.17.2.
— IBM
Affected Software
Remediation
Recommended actions to resolve this vulnerability, in priority order.
- Upgrade
Upgrade
rubygems/ojto a version that resolves this vulnerability.Fixed in 3.17.3 - Upgrade
Upgrade
oj gemto a version that resolves this vulnerability.Fixed in 3.17.2 - Configuration
In developer code, do not call Oj.dump with an extreme :indent value (e.g., INT_MAX / 2_147_483_647). Use a small, practical indent value to avoid triggering the stack-buffer-overflow when indentation is computed and used for memset.
Oj.dump (Oj gem) indent = a small, validated integer (do not use INT_MAX / extreme values)
Event History
Frequently Asked Questions
What is the severity of CVE-2026-54502?
CVE-2026-54502 has a risk score of 37, indicating a moderate severity level.
How do I fix CVE-2026-54502?
To mitigate CVE-2026-54502, developers should validate the size of the `:indent` value before using it in the `Oj.dump` method.
What is the nature of the vulnerability in CVE-2026-54502?
CVE-2026-54502 is a stack-based buffer overflow vulnerability caused by insufficient validation of large `:indent` values.
Which software is affected by CVE-2026-54502?
CVE-2026-54502 affects the Oj gem, part of the RubyGems ecosystem.
When was CVE-2026-54502 disclosed?
CVE-2026-54502 was published on June 19, 2026.