Skip to content

Commit 53a8a29

Browse files
gyscosclaude
andcommitted
zstd-safe: refuse to enable CParameter::StableInBuffer
In stable-input mode zstd keeps the ZSTD_inBuffer pointer of a compress_stream* call and reads deferred bytes from it in later calls, including flush_stream()/end_stream(), which take no input at all. An InBuffer only borrows its slice for one call, so safe code could free it in between, and zstd would compress freed memory into the frame. The safe API cannot uphold this requirement: refuse StableInBuffer(true) with ZSTD_error_parameter_unsupported. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-BF808SHX. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MxSMUdwqoQ3qm6UF82MgZD
1 parent 781ea45 commit 53a8a29

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

‎zstd-safe/src/lib.rs‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ const MEMORY_ALLOCATION_ERROR: ErrorCode =
8181
(zstd_sys::ZSTD_ErrorCode::ZSTD_error_memory_allocation as usize)
8282
.wrapping_neg();
8383

84+
/// The error code zstd itself returns for a parameter it does not support
85+
/// (`-ZSTD_error_parameter_unsupported` as a `size_t`), for the parameters
86+
/// that are refused on the Rust side of a call.
87+
#[cfg(feature = "experimental")]
88+
const PARAMETER_UNSUPPORTED_ERROR: ErrorCode =
89+
(zstd_sys::ZSTD_ErrorCode::ZSTD_error_parameter_unsupported as usize)
90+
.wrapping_neg();
91+
8492
/// Indicates an error happened when parsing the frame content size.
8593
///
8694
/// The stream may be corrupted, or the given frame prefix was too small.
@@ -749,8 +757,13 @@ impl<'a> CCtx<'a> {
749757
EnableDedicatedDictSearch(enable) => {
750758
(ZSTD_c_enableDedicatedDictSearch, enable as c_int)
751759
}
760+
// In stable mode zstd keeps reading from the `InBuffer` of one
761+
// call during `flush_stream`/`end_stream`, but the slice is only
762+
// borrowed for that one call: the safe API cannot uphold this.
752763
#[cfg(feature = "experimental")]
753-
StableInBuffer(stable) => (ZSTD_c_stableInBuffer, stable as c_int),
764+
StableInBuffer(true) => return Err(PARAMETER_UNSUPPORTED_ERROR),
765+
#[cfg(feature = "experimental")]
766+
StableInBuffer(false) => (ZSTD_c_stableInBuffer, 0),
754767
#[cfg(feature = "experimental")]
755768
StableOutBuffer(stable) => {
756769
(ZSTD_c_stableOutBuffer, stable as c_int)
@@ -2529,6 +2542,12 @@ pub enum CParameter {
25292542
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
25302543
EnableDedicatedDictSearch(bool),
25312544

2545+
/// Whether zstd may keep referencing the input buffer instead of copying
2546+
/// it.
2547+
///
2548+
/// This requires the input to stay allocated and unmodified until the
2549+
/// frame is complete, which a borrowed `InBuffer` cannot guarantee:
2550+
/// `CCtx::set_parameter` refuses to enable it and returns an error.
25322551
#[cfg(feature = "experimental")]
25332552
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
25342553
StableInBuffer(bool),

‎zstd-safe/src/tests.rs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,24 @@ fn test_upper_bound() {
255255
);
256256
}
257257

258+
#[cfg(feature = "experimental")]
259+
#[test]
260+
fn test_stable_in_buffer_cannot_be_enabled() {
261+
use zstd_safe::CParameter::StableInBuffer;
262+
263+
// Enabling it would let `end_stream` read from an `InBuffer` whose
264+
// borrow ended with the `compress_stream` call that received it.
265+
let mut cctx = zstd_safe::CCtx::create();
266+
let err = cctx
267+
.set_parameter(StableInBuffer(true))
268+
.map_err(zstd_safe::get_error_name)
269+
.unwrap_err();
270+
assert!(err.contains("Unsupported parameter"));
271+
272+
// Asking for the default is still fine.
273+
cctx.set_parameter(StableInBuffer(false)).unwrap();
274+
}
275+
258276
#[cfg(feature = "seekable")]
259277
#[test]
260278
fn test_seekable_cycle() {

0 commit comments

Comments
 (0)