✨ From vibe coding to vibe deployment. UBOS MCP turns ideas into infra with one message.

Learn more
Andrii Bidochko
  • Updated: January 7, 2026
  • 5 min read

Ruby Array#pack Vulnerability Allows Out‑of‑Bounds Memory Reads

The Ruby Array#pack vulnerability affecting Ruby 4.0.0 and earlier permits out‑of‑bounds memory reads by exploiting negative repeat counts in the pack format string, which can lead to unintended data disclosure.

Ruby Array#pack Vulnerability: Negative Repeat Counts and Out‑of‑Bounds Memory Reads

Ruby Array#pack vulnerability illustration

What’s the issue?

Ruby’s Array#pack method converts an array into a binary string based on a format template. A flaw in the handling of repeat counts—numeric prefixes that tell the method how many times a directive should be applied—allows an attacker to supply a negative count. When combined with the X directive (which backs up a byte), the negative count can cause the internal buffer to shrink by a negative amount, effectively expanding it and reading memory beyond the allocated region.

This vulnerability, tracked in Ruby 4.0.0 and all prior releases (potentially back to Ruby 1.6.7), is classified as an out‑of‑bounds memory read. While exploitation requires control over the format string—a scenario that is uncommon in typical Ruby applications—the impact can be severe in environments where user‑controlled data reaches Array#pack, such as custom serialization services or insecure API endpoints.

Technical Deep‑Dive: Negative Repeat Counts

The core of the bug lives in ruby/pack.c. The parser extracts a repeat count using STRTOUL, which returns an unsigned long. The result is then stored in a signed long variable len. When the unsigned value exceeds LONG_MAX, it wraps around to a negative number:

len = STRTOUL(p, (char**)&p, 10); // unsigned → signed conversion

The X directive’s implementation expects a positive len to back up bytes. If len is negative, the code mistakenly expands the string:

case 'X':
  plen = RSTRING_LEN(res);
  if (plen < len) rb_raise(rb_eArgError, "X outside of string");
  rb_str_set_len(res, plen - len); // negative len → growth

By crafting a format like "H6X#{2**64-1}", the repeat count becomes a massive negative number, causing the internal buffer to grow and read adjacent memory pages. The guard in rb_str_set_len only blocks lengths that exceed the allocated capacity or become negative after the operation, but the overflow can still occur when the capacity is a power‑of‑two and the negative offset aligns just right.

Affected Ruby Versions and Real‑World Impact

  • Ruby 4.0.0 (released December 2025) – fully vulnerable.
  • All prior stable releases back to Ruby 1.6.7 – the same parsing logic exists.
  • Enterprise Ruby platforms that embed MRI without patching – also at risk.

In practice, the vulnerability is most dangerous for:

  • Web services that expose Array#pack via user‑controlled APIs.
  • Background jobs that deserialize data from untrusted sources.
  • Embedded Ruby interpreters in SaaS products that allow custom format strings.

Although the likelihood of an attacker gaining direct control over the format string is low, the presence of this flaw underscores the importance of strict input validation and timely patching—especially for security‑focused teams managing Ruby‑based infrastructure.

Proof‑of‑Concept Exploit

The following IRB session illustrates how a crafted format string can trigger the out‑of‑bounds read. Do not run this on production systems.

# Prepare a buffer of known content
arr = ["A" * 512]

# Use a negative repeat count with the X directive
# 2**64 - 511 yields a large negative number after signed conversion
payload = "a512X#{2**64 - 511}"
leak = arr.pack(payload)

puts "Leaked bytes (hex): #{leak.unpack('H*').first[0,64]}"

The output shows a series of bytes that belong to adjacent memory regions, confirming that the pack operation read beyond the intended buffer. The exact data varies per runtime, but the pattern demonstrates the core issue.

Mitigation Steps and Official Patches

The Ruby core team addressed the bug in PR #15763. The fix includes:

  1. Validating repeat counts as signed values before conversion.
  2. Rejecting negative repeat counts outright for directives that cannot safely handle them.
  3. Adding explicit bounds checks in the X directive implementation.

All supported Ruby releases (4.0.0‑p0 and later) now contain the patch. Users should:

  • Upgrade to the latest Ruby version (≥ 4.0.1) or apply the back‑ported patch for older branches.
  • Audit codebases for any direct calls to Array#pack that accept external input.
  • Wrap Array#pack usage in a safe‑guard method that validates the format string against a whitelist of allowed directives.

For organizations using Ruby within a larger platform, consider employing UBOS platform overview to enforce runtime security policies and automate patch deployment across your fleet.

Reference and Additional Resources

The full technical write‑up and original disclosure can be found in the original security advisory. Additional guidance on secure coding for Ruby can be accessed via the following UBOS resources:

Take Action: Strengthen Your Ruby Security Posture

Secure development is a continuous journey. By staying informed about vulnerabilities like the Ruby pack vulnerability and integrating automated security tooling, you can protect your applications from subtle memory‑safety bugs.

UBOS offers a suite of solutions designed for developers and security teams:

Whether you are a startup, an SMB, or an enterprise, UBOS provides tailored options:

Ready to protect your Ruby applications? Explore the UBOS pricing plans and start a free trial today.


Andrii Bidochko

CTO UBOS

Andrii Bidochko is an AI entrepreneur and researcher focused on AI agents, reinforcement learning, and autonomous systems. He writes about the technologies shaping the future of machine intelligence, from frontier models and agent architectures to real-world AI applications.

Sign up for our newsletter

Stay up to date with the roadmap progress, announcements and exclusive discounts feel free to sign up with your email.

Sign In

Register

Reset Password

Please enter your username or email address, you will receive a link to create a new password via email.