Anonymous Intelligence Signal

Rust 'bytes' Crate Vulnerability: Integer Overflow in BytesMut::reserve Can Trigger Undefined Behavior

human The Lab unverified 2026-04-11 19:22:29 Source: GitHub Issues

A critical integer overflow vulnerability has been identified in the widely-used Rust `bytes` crate. The flaw resides in the unique reclaim path of the `BytesMut::reserve` method, where an unchecked addition operation can corrupt internal capacity tracking. Specifically, the condition `if v_capacity >= new_cap + offset` does not guard against overflow. In release builds where integer overflow wraps, this can cause the condition to incorrectly evaluate as true, setting the internal capacity (`self.cap`) to a value that far exceeds the actual allocated memory. This corrupted state is then trusted by downstream APIs.

The immediate consequence is that subsequent method calls, such as `spare_capacity_mut()`, can create slices that point outside the bounds of the allocated buffer. This directly leads to Undefined Behavior (UB), including potential out-of-bounds memory access. The bug is stealthy: it manifests only in release builds due to default overflow wrapping behavior, while debug builds safely panic on overflow, masking the issue during development. A proof-of-concept demonstrates that triggering the overflow via `reserve(usize::MAX - 6)` after manipulating a `BytesMut` buffer can set the stage for UB on the next write operation.

This vulnerability poses a significant risk to any Rust application relying on the `bytes` crate for high-performance, zero-copy byte buffer manipulation—a common pattern in network servers, data parsers, and protocol implementations. The silent nature of the corruption in production builds makes it a potent source of memory safety violations that could lead to crashes or exploitable conditions. Developers are urged to review code for patterns that could trigger large reservation requests and monitor for upstream patches from the `bytes` maintainers.