Skip to content

Commit d34502a

Browse files
gyscosclaude
andcommitted
zstd-safe: reset the session after one-shot context calls
The one-shot wrappers only cleared the poison flag, on the assumption from zstd.h that the C call resets the context. The bundled zstd does not end an open stream there, so a stream left unfinished before the call was resumed by the next streaming call - against history pointers that could still point at the dictionary borrowed for the one-shot call, or, on the compression side, at the one-shot call's input. Reset the session explicitly after DCtx::decompress*, CCtx::compress, compress_using_dict and compress_using_cdict. CCtx::compress2 is different: it already resets the session first, and ends it when it succeeds. When it fails it is left mid-frame, possibly with zstdmt workers still running, so it is not reset (zstd keeps refusing dictionary changes in that state); the context is poisoned instead, so streaming calls wait for an explicit reset. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-JFI4RZ1I. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MxSMUdwqoQ3qm6UF82MgZD
1 parent 682b8b2 commit d34502a

2 files changed

Lines changed: 177 additions & 21 deletions

File tree

‎zstd-safe/src/lib.rs‎

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -359,16 +359,27 @@ impl<'a> CCtx<'a> {
359359
.expect("zstd returned null pointer when creating new context")
360360
}
361361

362+
/// Ends the session of a one-shot compression, and passes `res` through.
363+
///
364+
/// zstd.h has these calls imply a reset, but most of them leave the stream
365+
/// stage alone (and their source size behind as the pledged one), while
366+
/// `ZSTD_compress2()` stays mid-frame when it fails. A later stream call
367+
/// would then carry on from a state that still points at buffers and
368+
/// dictionaries borrowed for that one call. So reset explicitly: the next
369+
/// stream starts from scratch.
370+
fn finish_one_shot(&mut self, res: SafeResult) -> SafeResult {
371+
self.reset(ResetDirective::SessionOnly).and(res)
372+
}
373+
362374
/// Wraps the `ZSTD_compressCCtx()` function
363375
pub fn compress<C: WriteBuf + ?Sized>(
364376
&mut self,
365377
dst: &mut C,
366378
src: &[u8],
367379
compression_level: CompressionLevel,
368380
) -> SafeResult {
369-
self.2.clear();
370381
// Safety: ZSTD_compressCCtx returns how many bytes were written.
371-
unsafe {
382+
let res = unsafe {
372383
dst.write_from(|buffer, capacity| {
373384
parse_code(zstd_sys::ZSTD_compressCCtx(
374385
self.0.as_ptr(),
@@ -379,7 +390,8 @@ impl<'a> CCtx<'a> {
379390
compression_level,
380391
))
381392
})
382-
}
393+
};
394+
self.finish_one_shot(res)
383395
}
384396

385397
/// Wraps the `ZSTD_compress2()` function.
@@ -388,9 +400,8 @@ impl<'a> CCtx<'a> {
388400
dst: &mut C,
389401
src: &[u8],
390402
) -> SafeResult {
391-
self.2.clear();
392403
// Safety: ZSTD_compress2 returns how many bytes were written.
393-
unsafe {
404+
let res = unsafe {
394405
dst.write_from(|buffer, capacity| {
395406
parse_code(zstd_sys::ZSTD_compress2(
396407
self.0.as_ptr(),
@@ -400,7 +411,14 @@ impl<'a> CCtx<'a> {
400411
src.len(),
401412
))
402413
})
403-
}
414+
};
415+
// Unlike the others, this starts by resetting the session, and
416+
// ends it when it succeeds. When it fails, it stays mid-frame,
417+
// possibly with worker threads still running: leave it so (zstd then
418+
// refuses to change the dictionary they read from), and make
419+
// streaming calls wait for an explicit reset.
420+
self.2.clear();
421+
self.2.record(res)
404422
}
405423

406424
/// Wraps the `ZSTD_compress_usingDict()` function.
@@ -411,9 +429,8 @@ impl<'a> CCtx<'a> {
411429
dict: &[u8],
412430
compression_level: CompressionLevel,
413431
) -> SafeResult {
414-
self.2.clear();
415432
// Safety: ZSTD_compress_usingDict returns how many bytes were written.
416-
unsafe {
433+
let res = unsafe {
417434
dst.write_from(|buffer, capacity| {
418435
parse_code(zstd_sys::ZSTD_compress_usingDict(
419436
self.0.as_ptr(),
@@ -426,7 +443,8 @@ impl<'a> CCtx<'a> {
426443
compression_level,
427444
))
428445
})
429-
}
446+
};
447+
self.finish_one_shot(res)
430448
}
431449

432450
/// Wraps the `ZSTD_compress_usingCDict()` function.
@@ -436,9 +454,8 @@ impl<'a> CCtx<'a> {
436454
src: &[u8],
437455
cdict: &CDict<'_>,
438456
) -> SafeResult {
439-
self.2.clear();
440457
// Safety: ZSTD_compress_usingCDict returns how many bytes were written.
441-
unsafe {
458+
let res = unsafe {
442459
dst.write_from(|buffer, capacity| {
443460
parse_code(zstd_sys::ZSTD_compress_usingCDict(
444461
self.0.as_ptr(),
@@ -449,7 +466,8 @@ impl<'a> CCtx<'a> {
449466
cdict.0.as_ptr(),
450467
))
451468
})
452-
}
469+
};
470+
self.finish_one_shot(res)
453471
}
454472

455473
/// Initializes the context with the given compression level.
@@ -1138,6 +1156,16 @@ impl<'a> DCtx<'a> {
11381156
.expect("zstd returned null pointer when creating new context")
11391157
}
11401158

1159+
/// Ends the session of a one-shot decompression, and passes `res` through.
1160+
///
1161+
/// zstd.h has these calls imply a reset, but they leave the stream stage
1162+
/// alone. A stream they interrupted would then carry on from a state that
1163+
/// still points at buffers and dictionaries borrowed for that one call. So
1164+
/// reset explicitly: the next stream starts from scratch.
1165+
fn finish_one_shot(&mut self, res: SafeResult) -> SafeResult {
1166+
self.reset(ResetDirective::SessionOnly).and(res)
1167+
}
1168+
11411169
/// Fully decompress the given frame.
11421170
///
11431171
/// This decompress an entire frame in-memory. If you can have enough memory to store both the
@@ -1149,8 +1177,7 @@ impl<'a> DCtx<'a> {
11491177
dst: &mut C,
11501178
src: &[u8],
11511179
) -> SafeResult {
1152-
self.2.clear();
1153-
unsafe {
1180+
let res = unsafe {
11541181
dst.write_from(|buffer, capacity| {
11551182
parse_code(zstd_sys::ZSTD_decompressDCtx(
11561183
self.0.as_ptr(),
@@ -1160,7 +1187,8 @@ impl<'a> DCtx<'a> {
11601187
src.len(),
11611188
))
11621189
})
1163-
}
1190+
};
1191+
self.finish_one_shot(res)
11641192
}
11651193

11661194
/// Fully decompress the given frame using a dictionary.
@@ -1177,8 +1205,7 @@ impl<'a> DCtx<'a> {
11771205
src: &[u8],
11781206
dict: &[u8],
11791207
) -> SafeResult {
1180-
self.2.clear();
1181-
unsafe {
1208+
let res = unsafe {
11821209
dst.write_from(|buffer, capacity| {
11831210
parse_code(zstd_sys::ZSTD_decompress_usingDict(
11841211
self.0.as_ptr(),
@@ -1190,7 +1217,8 @@ impl<'a> DCtx<'a> {
11901217
dict.len(),
11911218
))
11921219
})
1193-
}
1220+
};
1221+
self.finish_one_shot(res)
11941222
}
11951223

11961224
/// Fully decompress the given frame using a dictionary.
@@ -1204,8 +1232,7 @@ impl<'a> DCtx<'a> {
12041232
src: &[u8],
12051233
ddict: &DDict<'_>,
12061234
) -> SafeResult {
1207-
self.2.clear();
1208-
unsafe {
1235+
let res = unsafe {
12091236
dst.write_from(|buffer, capacity| {
12101237
parse_code(zstd_sys::ZSTD_decompress_usingDDict(
12111238
self.0.as_ptr(),
@@ -1216,7 +1243,8 @@ impl<'a> DCtx<'a> {
12161243
ddict.0.as_ptr(),
12171244
))
12181245
})
1219-
}
1246+
};
1247+
self.finish_one_shot(res)
12201248
}
12211249

12221250
/// Initializes an existing `DStream` for decompression.

‎zstd-safe/src/tests.rs‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,134 @@ fn test_failed_legacy_stream_reports_no_output() {
705705
assert_eq!(buffer.len(), 0);
706706
}
707707

708+
/// The one-shot calls start a new job without resetting the stream stage in C,
709+
/// so a stream they interrupted used to carry on afterwards - decoding against
710+
/// the dictionary of the one-shot call, which may be long gone.
711+
#[cfg(feature = "std")]
712+
#[test]
713+
fn test_one_shot_decompression_resets_the_stream() {
714+
use zstd_safe::{DCtx, DDict, InBuffer, OutBuffer, SafeResult};
715+
716+
type OneShot = fn(&mut DCtx<'_>, &mut Vec<u8>, &[u8]) -> SafeResult;
717+
let one_shots: [OneShot; 3] = [
718+
|dctx, dst, src| dctx.decompress(dst, src),
719+
|dctx, dst, src| {
720+
dctx.decompress_using_dict(dst, src, &std::vec![b'd'; 1000])
721+
},
722+
|dctx, dst, src| {
723+
let ddict = DDict::create(&[b'd'; 1000]);
724+
dctx.decompress_using_ddict(dst, src, &ddict)
725+
},
726+
];
727+
728+
let long = LONG_CONTENT.as_bytes();
729+
let mut abandoned =
730+
Vec::with_capacity(zstd_safe::compress_bound(long.len()));
731+
zstd_safe::compress(&mut abandoned, long, 1).unwrap();
732+
let mut compressed =
733+
Vec::with_capacity(zstd_safe::compress_bound(INPUT.len()));
734+
zstd_safe::compress(&mut compressed, INPUT, 1).unwrap();
735+
736+
for one_shot in one_shots {
737+
let mut dctx = DCtx::create();
738+
739+
// A stream that is abandoned with output still buffered...
740+
let mut head = [0u8; 16];
741+
dctx.decompress_stream(
742+
&mut OutBuffer::around(&mut head[..]),
743+
&mut InBuffer::around(&abandoned),
744+
)
745+
.unwrap();
746+
747+
// ... a one-shot job on the same context, with no room for output...
748+
one_shot(&mut dctx, &mut Vec::new(), &compressed).unwrap_err();
749+
750+
// ... and a new stream, which must see nothing of either.
751+
let mut decoded = Vec::with_capacity(INPUT.len() + 1);
752+
let hint = dctx
753+
.decompress_stream(
754+
&mut OutBuffer::around(&mut decoded),
755+
&mut InBuffer::around(&compressed),
756+
)
757+
.map_err(zstd_safe::get_error_name)
758+
.unwrap();
759+
assert_eq!(hint, 0);
760+
assert_eq!(decoded, INPUT);
761+
}
762+
}
763+
764+
/// Same on the compression side, whether or not the one-shot call succeeds.
765+
#[cfg(feature = "std")]
766+
#[test]
767+
fn test_one_shot_compression_resets_the_stream() {
768+
use zstd_safe::{CCtx, CDict, InBuffer, OutBuffer, SafeResult};
769+
770+
type OneShot = fn(&mut CCtx<'_>, &mut Vec<u8>, &[u8]) -> SafeResult;
771+
let one_shots: [OneShot; 4] = [
772+
|cctx, dst, src| cctx.compress(dst, src, 1),
773+
|cctx, dst, src| cctx.compress2(dst, src),
774+
|cctx, dst, src| {
775+
cctx.compress_using_dict(dst, src, &std::vec![b'd'; 1000], 1)
776+
},
777+
|cctx, dst, src| {
778+
let cdict = CDict::create(&[b'd'; 1000], 1);
779+
cctx.compress_using_cdict(dst, src, &cdict)
780+
},
781+
];
782+
783+
let long = LONG_CONTENT.as_bytes();
784+
let bound = zstd_safe::compress_bound(long.len());
785+
786+
for (i, one_shot) in one_shots.iter().enumerate() {
787+
for capacity in [0, bound] {
788+
let mut cctx = CCtx::create();
789+
let mut compressed = Vec::with_capacity(bound);
790+
791+
// A stream that is abandoned with input still buffered...
792+
cctx.compress_stream(
793+
&mut OutBuffer::around(&mut compressed),
794+
&mut InBuffer::around(INPUT),
795+
)
796+
.unwrap();
797+
compressed.clear();
798+
799+
// ... a one-shot job on the same context...
800+
let res =
801+
one_shot(&mut cctx, &mut Vec::with_capacity(capacity), INPUT);
802+
assert_eq!(res.is_ok(), capacity != 0);
803+
804+
// A failed `compress2` is left mid-frame (it may have worker
805+
// threads still running), so streaming waits for a reset.
806+
if i == 1 && res.is_err() {
807+
cctx.compress_stream(
808+
&mut OutBuffer::around(&mut compressed),
809+
&mut InBuffer::around(long),
810+
)
811+
.unwrap_err();
812+
assert!(compressed.is_empty());
813+
cctx.reset(zstd_safe::ResetDirective::SessionOnly).unwrap();
814+
}
815+
816+
// ... and a new stream, which must see nothing of either.
817+
let mut output = OutBuffer::around(&mut compressed);
818+
cctx.compress_stream(&mut output, &mut InBuffer::around(long))
819+
.map_err(zstd_safe::get_error_name)
820+
.unwrap();
821+
let remaining = cctx
822+
.end_stream(&mut output)
823+
.map_err(zstd_safe::get_error_name)
824+
.unwrap();
825+
assert_eq!(remaining, 0);
826+
827+
let mut decoded = Vec::with_capacity(long.len());
828+
zstd_safe::decompress(&mut decoded, &compressed)
829+
.map_err(zstd_safe::get_error_name)
830+
.unwrap();
831+
assert_eq!(decoded, long);
832+
}
833+
}
834+
}
835+
708836
#[test]
709837
fn test_poison_tracking() {
710838
use crate::Poison;

0 commit comments

Comments
 (0)