Skip to content

Commit cee6529

Browse files
gyscosclaude
andcommitted
zstd-safe: make DCtx::try_clone refuse owned state and keep its limits
ZSTD_copyDCtx() is a plain memcpy of every field before inBuff. That range includes the owning ddictLocal (load_dictionary, ref_prefix, init_using_dict) and ddictSet (ref_ddict with RefMultipleDDicts) pointers, so both contexts freed them on drop. It also includes streamStage but not the stream buffers that go with it, so a clone taken mid-frame resumed on uninitialized state. Conversely, maxWindowSize (WindowLogMax) and outBufferMode (StableOutBuffer) live after inBuff, so a clone silently fell back to the 128 MiB default window limit. Refuse to clone a context that owns a dictionary, a prefix, a DDict table or stream buffers (with ZSTD_error_stage_wrong, as the documentation promises), reset the clone's session after the copy, and carry over the parameters ZSTD_copyDCtx() leaves behind. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-WVVZMOB8. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-QPS9S9CM. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MxSMUdwqoQ3qm6UF82MgZD
1 parent 53a8a29 commit cee6529

2 files changed

Lines changed: 194 additions & 3 deletions

File tree

‎zstd-safe/src/lib.rs‎

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ const PARAMETER_UNSUPPORTED_ERROR: ErrorCode =
8989
(zstd_sys::ZSTD_ErrorCode::ZSTD_error_parameter_unsupported as usize)
9090
.wrapping_neg();
9191

92+
/// The error code zstd itself returns for an operation the context is in no
93+
/// state to perform (`-ZSTD_error_stage_wrong` as a `size_t`), for the places
94+
/// where that has to be decided on the Rust side of a call.
95+
#[cfg(feature = "experimental")]
96+
const STAGE_WRONG_ERROR: ErrorCode =
97+
(zstd_sys::ZSTD_ErrorCode::ZSTD_error_stage_wrong as usize).wrapping_neg();
98+
9299
/// Indicates an error happened when parsing the frame content size.
93100
///
94101
/// The stream may be corrupted, or the given frame prefix was too small.
@@ -1035,7 +1042,13 @@ pub fn get_error_name(code: usize) -> &'static str {
10351042
/// If no dictionary was used, it will most likely be `'static`.
10361043
///
10371044
/// Same as `DStream`.
1038-
pub struct DCtx<'a>(NonNull<zstd_sys::ZSTD_DCtx>, PhantomData<&'a ()>, Poison);
1045+
pub struct DCtx<'a>(
1046+
NonNull<zstd_sys::ZSTD_DCtx>,
1047+
PhantomData<&'a ()>,
1048+
Poison,
1049+
// Whether `RefMultipleDDicts` was ever enabled, see `try_clone`.
1050+
#[cfg(feature = "experimental")] bool,
1051+
);
10391052

10401053
impl Default for DCtx<'_> {
10411054
fn default() -> Self {
@@ -1064,7 +1077,13 @@ impl<'a> DCtx<'a> {
10641077
// Safety: zstd returns a newly owned context or null.
10651078
let context = unsafe { zstd_sys::ZSTD_createDCtx() };
10661079

1067-
Some(Self(NonNull::new(context)?, PhantomData, Poison::default()))
1080+
Some(Self(
1081+
NonNull::new(context)?,
1082+
PhantomData,
1083+
Poison::default(),
1084+
#[cfg(feature = "experimental")]
1085+
false,
1086+
))
10681087
}
10691088

10701089
/// Creates a new decoding context.
@@ -1318,6 +1337,9 @@ impl<'a> DCtx<'a> {
13181337
}
13191338
#[cfg(feature = "experimental")]
13201339
RefMultipleDDicts(value) => {
1340+
// From now on `ref_ddict` may allocate the table of
1341+
// dictionaries, which zstd keeps until the context is freed.
1342+
self.3 |= value;
13211343
(ZSTD_d_refMultipleDDicts, value as c_int)
13221344
}
13231345

@@ -1435,24 +1457,85 @@ impl<'a> DCtx<'a> {
14351457
/// This only works before any data has been decompressed. An error will be
14361458
/// returned otherwise.
14371459
/// A poisoned context cannot be cloned until its session is reset.
1460+
/// The copy always starts at the beginning of a frame.
1461+
///
1462+
/// A dictionary this context merely references (see [`DCtx::ref_ddict`])
1463+
/// is referenced by the copy too. One it owns cannot be shared, so an
1464+
/// error is also returned while a dictionary from
1465+
/// [`DCtx::load_dictionary`] or a prefix from [`DCtx::ref_prefix`] is set,
1466+
/// and once [`DParameter::RefMultipleDDicts`] has been enabled.
14381467
#[cfg(feature = "experimental")]
14391468
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))]
14401469
#[deprecated(
14411470
note = "Context copying is deprecated by zstd; create a new context and configure it instead."
14421471
)]
14431472
pub fn try_clone(&self) -> Result<Self, ErrorCode> {
1473+
use zstd_sys::ZSTD_dParameter::{
1474+
ZSTD_d_experimentalParam2 as ZSTD_d_stableOutBuffer,
1475+
ZSTD_d_windowLogMax,
1476+
};
1477+
14441478
// An error may leave the C context undefined; do not copy it.
14451479
self.2.guard()?;
14461480
// zstd copies the allocator callbacks from `self` into the new
14471481
// context, so both must be created the same way; `try_create` always
14481482
// picks the same allocator within a build.
1449-
let context = Self::try_create().ok_or(MEMORY_ALLOCATION_ERROR)?;
1483+
let mut context = Self::try_create().ok_or(MEMORY_ALLOCATION_ERROR)?;
1484+
1485+
// `ZSTD_copyDCtx()` is a plain `memcpy`, so a dictionary `self` owns
1486+
// would end up owned, and freed, by both contexts. A loaded dictionary
1487+
// or prefix shows up as memory a new context does not use - as do the
1488+
// streaming buffers, once data has been decompressed. The table behind
1489+
// `RefMultipleDDicts` is not counted there, and outlives the
1490+
// parameter, hence the flag.
1491+
if self.3 || self.sizeof() != context.sizeof() {
1492+
return Err(STAGE_WRONG_ERROR);
1493+
}
1494+
1495+
// `ZSTD_copyDCtx` stops where the streaming buffers start and leaves
1496+
// the parameters stored past that point (the window limit and the
1497+
// output buffer mode) at their defaults. Carry them over explicitly,
1498+
// on the fresh context so that it is still in its initial stage; the
1499+
// copy below does not touch these fields.
1500+
// Only for values that differ: the window limit reads back as a log,
1501+
// which rounds zstd's default limit down.
1502+
let get = |dctx: &Self, param| {
1503+
let mut value: c_int = 0;
1504+
// Safety: Just FFI
1505+
parse_code(unsafe {
1506+
zstd_sys::ZSTD_DCtx_getParameter(
1507+
dctx.0.as_ptr(),
1508+
param,
1509+
&mut value,
1510+
)
1511+
})
1512+
.map(|_| value)
1513+
};
1514+
for param in [ZSTD_d_windowLogMax, ZSTD_d_stableOutBuffer] {
1515+
let value = get(self, param)?;
1516+
if value == get(&context, param)? {
1517+
continue;
1518+
}
1519+
// Safety: Just FFI
1520+
parse_code(unsafe {
1521+
zstd_sys::ZSTD_DCtx_setParameter(
1522+
context.0.as_ptr(),
1523+
param,
1524+
value,
1525+
)
1526+
})?;
1527+
}
14501528

14511529
// Safety: Just FFI
14521530
unsafe {
14531531
zstd_sys::ZSTD_copyDCtx(context.0.as_ptr(), self.0.as_ptr())
14541532
};
14551533

1534+
// The position within a frame is copied, but not the state that goes
1535+
// with it (a partial header, a legacy context), so start from a frame
1536+
// boundary rather than run on what the new context never initialized.
1537+
context.reset(ResetDirective::SessionOnly)?;
1538+
14561539
Ok(context)
14571540
}
14581541
}

‎zstd-safe/src/tests.rs‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,73 @@ fn test_cloning_poisoned_contexts_returns_the_original_error() {
748748
assert_eq!(cctx.try_clone(None).err(), Some(error));
749749
}
750750

751+
#[cfg(all(feature = "std", feature = "experimental"))]
752+
#[test]
753+
#[allow(deprecated)] // Exercises the deprecated context-copying API.
754+
fn test_cloning_decompression_context_never_shares_what_it_owns() {
755+
use crate::{
756+
CCtx, DCtx, DDict, DParameter, InBuffer, OutBuffer, ResetDirective,
757+
};
758+
759+
let dict = &LONG_CONTENT.as_bytes()[..1000];
760+
let ddict = DDict::create(dict);
761+
let mut compressed =
762+
Vec::with_capacity(crate::compress_bound(INPUT.len()));
763+
CCtx::create()
764+
.compress_using_dict(&mut compressed, INPUT, dict, 3)
765+
.unwrap();
766+
767+
// The copy would free these along with the original.
768+
let mut dctx = DCtx::create();
769+
dctx.load_dictionary(dict).unwrap();
770+
assert!(dctx.try_clone().is_err());
771+
dctx.ref_prefix(dict).unwrap();
772+
assert!(dctx.try_clone().is_err());
773+
dctx.init_using_dict(dict).unwrap();
774+
assert!(dctx.try_clone().is_err());
775+
dctx.disable_dictionary().unwrap();
776+
assert!(dctx.try_clone().is_ok());
777+
778+
// zstd keeps the table of dictionaries even after the parameter is reset.
779+
let mut dctx = DCtx::create();
780+
dctx.set_parameter(DParameter::RefMultipleDDicts(true))
781+
.unwrap();
782+
dctx.ref_ddict(&ddict).unwrap();
783+
assert!(dctx.try_clone().is_err());
784+
dctx.reset(ResetDirective::SessionAndParameters).unwrap();
785+
assert!(dctx.try_clone().is_err());
786+
787+
// A referenced dictionary belongs to neither context.
788+
let mut dctx = DCtx::create();
789+
dctx.ref_ddict(&ddict).unwrap();
790+
// Part of a header leaves the original in the middle of a frame.
791+
dctx.decompress_stream(
792+
&mut OutBuffer::around(&mut [0; 8][..]),
793+
&mut InBuffer::around(&compressed[..3]),
794+
)
795+
.unwrap();
796+
let mut cloned = dctx.try_clone().unwrap();
797+
drop(dctx);
798+
799+
let mut decoded = Vec::with_capacity(INPUT.len());
800+
let mut output = OutBuffer::around(&mut decoded);
801+
let hint = cloned
802+
.decompress_stream(&mut output, &mut InBuffer::around(&compressed))
803+
.unwrap();
804+
assert_eq!(hint, 0);
805+
assert_eq!(decoded, INPUT);
806+
807+
// Once the streaming buffers exist, data has been decompressed.
808+
let mut dctx = DCtx::create();
809+
dctx.ref_ddict(&ddict).unwrap();
810+
dctx.decompress_stream(
811+
&mut OutBuffer::around(&mut [0; 8][..]),
812+
&mut InBuffer::around(&compressed),
813+
)
814+
.unwrap();
815+
assert!(dctx.try_clone().is_err());
816+
}
817+
751818
#[cfg(all(feature = "std", feature = "experimental"))]
752819
#[test]
753820
#[allow(deprecated)] // Exercises the deprecated context-copying API.
@@ -772,6 +839,47 @@ fn test_cloning_initialized_compression_context() {
772839
assert_eq!(decoded, INPUT);
773840
}
774841

842+
#[cfg(all(feature = "std", feature = "experimental"))]
843+
#[test]
844+
#[allow(deprecated)] // Exercises the deprecated context-copying API.
845+
fn test_cloning_decompression_context_keeps_streaming_parameters() {
846+
use crate::{DCtx, DParameter, InBuffer, OutBuffer};
847+
848+
// A frame announcing a 128 MiB window, followed by a raw block of 1 byte.
849+
let frame = b"\x28\xb5\x2f\xfd\x00\x88\x08\x00\x00\x78";
850+
let mut output = [0; 16];
851+
852+
// The window limit must survive the copy.
853+
let mut dctx = DCtx::create();
854+
dctx.set_parameter(DParameter::WindowLogMax(20)).unwrap();
855+
let mut cloned = dctx.try_clone().unwrap();
856+
assert_eq!(
857+
cloned
858+
.decompress_stream(
859+
&mut OutBuffer::around(&mut output[..]),
860+
&mut InBuffer::around(frame),
861+
)
862+
.map_err(zstd_safe::get_error_name),
863+
Err("Frame requires too much memory for decoding")
864+
);
865+
assert!(cloned.sizeof() < 1 << 20);
866+
867+
// So must the output buffer mode: no window buffer is allocated when
868+
// the caller's output buffer is used as the window.
869+
let mut dctx = DCtx::create();
870+
dctx.set_parameter(DParameter::StableOutBuffer(true))
871+
.unwrap();
872+
let mut cloned = dctx.try_clone().unwrap();
873+
cloned
874+
.decompress_stream(
875+
&mut OutBuffer::around(&mut output[..]),
876+
&mut InBuffer::around(frame),
877+
)
878+
.unwrap();
879+
assert_eq!(output[0], b'x');
880+
assert!(cloned.sizeof() < 1 << 20);
881+
}
882+
775883
#[cfg(all(feature = "std", feature = "with-rust-allocator"))]
776884
#[test]
777885
fn test_custom_allocator_dictionaries_interoperate_with_c_dictionaries() {

0 commit comments

Comments
 (0)