Skip to content

Commit 80506be

Browse files
gyscosclaude
andcommitted
zstd-safe: clamp Seekable::decompress to the end of the data
ZSTD_seekable_decompress() clamps the requested length with eos - offset without checking that offset <= eos. For an offset past the end it writes nothing and returns a wrapped length (or never returns, for offsets near u64::MAX), which Seekable::decompress passed to Vec::set_len, leaving a Vec whose length exceeds its capacity. Compute the end of the data from the seek table, return Ok(0) at or past it, and never ask zstd for more than what is left. Also make WriteBuf::write_from assert that the reported count fits in the capacity, so a bogus count from C can never reach filled_until(). Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-V86MQO4K. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MxSMUdwqoQ3qm6UF82MgZD
1 parent 006e1d1 commit 80506be

3 files changed

Lines changed: 69 additions & 3 deletions

File tree

‎zstd-safe/src/lib.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1888,8 +1888,12 @@ pub unsafe trait WriteBuf {
18881888
where
18891889
F: FnOnce(*mut c_void, usize) -> SafeResult,
18901890
{
1891-
let res = f(ptr_mut_void(self), self.capacity());
1891+
let capacity = self.capacity();
1892+
let res = f(ptr_mut_void(self), capacity);
18921893
if let Ok(n) = res {
1894+
// Nothing can have been written past the end of the buffer; a
1895+
// larger count would mark memory we do not own as initialized.
1896+
assert!(n <= capacity, "zstd reported writing past the buffer");
18931897
self.filled_until(n);
18941898
}
18951899
res

‎zstd-safe/src/seekable.rs‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,24 +322,65 @@ impl<'a> Seekable<'a> {
322322
/// range of data, it will efficiently retain the decompressor object and avoid
323323
/// redecompressing frame prefixes.
324324
///
325-
/// Returns the number of bytes decompressed, or an error code.
325+
/// Returns the number of bytes decompressed, or an error code. This is less than the length
326+
/// of `dst` if the data ends first, and `0` if `offset` is at or past the end of the data.
326327
pub fn decompress<C: WriteBuf + ?Sized>(
327328
&mut self,
328329
dst: &mut C,
329330
offset: u64,
330331
) -> SafeResult {
332+
// `ZSTD_seekable_decompress()` does cut the length down to what is left
333+
// of the data, but with unchecked arithmetic: for an `offset` past the
334+
// end it wraps around and either claims to have written bytes it did
335+
// not write (possibly far more than `dst` can hold), or never returns.
336+
// So only ever ask it for a range that lies within the data.
337+
let remaining = self.decompressed_size().saturating_sub(offset);
331338
unsafe {
332339
dst.write_from(|buffer, capacity| {
340+
if remaining == 0 {
341+
return Ok(0);
342+
}
333343
parse_code(zstd_sys::ZSTD_seekable_decompress(
334344
self.0.as_ptr(),
335345
buffer,
336-
capacity,
346+
// No more than `capacity`, so this fits a `usize`.
347+
remaining.min(capacity as u64) as usize,
337348
offset,
338349
))
339350
})
340351
}
341352
}
342353

354+
/// Get the decompressed size of all the frames together, i.e. the end of
355+
/// the data as the seek table describes it.
356+
///
357+
/// Returns `0` if the seekable is not initialized.
358+
fn decompressed_size(&self) -> u64 {
359+
let last_frame = match self.num_frames().checked_sub(1) {
360+
Some(last_frame) => last_frame,
361+
None => return 0,
362+
};
363+
364+
// Safety: Just FFI. `last_frame` is in range, so neither is an error.
365+
let offset = unsafe {
366+
zstd_sys::ZSTD_seekable_getFrameDecompressedOffset(
367+
self.0.as_ptr(),
368+
last_frame,
369+
)
370+
};
371+
let size = unsafe {
372+
zstd_sys::ZSTD_seekable_getFrameDecompressedSize(
373+
self.0.as_ptr(),
374+
last_frame,
375+
)
376+
};
377+
378+
// The seek table stores cumulative offsets in wrapping `u64`
379+
// arithmetic, so this gives exactly the end-of-stream value zstd
380+
// compares against, even for a table whose sizes do not add up.
381+
offset.wrapping_add(size as u64)
382+
}
383+
343384
/// Decompresses the frame with index `frame_index` into `dst`.
344385
///
345386
/// Returns an error if `frame_index` is larger than the value returned by `num_frames()`.

‎zstd-safe/src/tests.rs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ fn test_seekable_cycle() {
192192
seekable.decompress(dst, offset)
193193
});
194194

195+
// A `Vec` takes its length from the result
196+
#[cfg(feature = "std")]
197+
{
198+
let mut buffer = Vec::with_capacity(64);
199+
assert_eq!(seekable.decompress(&mut buffer, 4096), Ok(0));
200+
assert!(buffer.is_empty());
201+
}
202+
195203
// Check that the archive can also be decompressed by a regular function
196204
let mut buffer = std::vec![0u8; 256];
197205
let written = zstd_safe::decompress(&mut buffer[..], &seekable_archive)
@@ -337,6 +345,19 @@ where
337345
.unwrap();
338346
let decompressed = &buffer[..written];
339347
assert_eq!(INPUT, decompressed);
348+
349+
// Stop at the end of the data
350+
let end = INPUT.len() as u64;
351+
let written = decompress(&mut buffer[..], end - 10)
352+
.map_err(zstd_safe::get_error_name)
353+
.unwrap();
354+
assert_eq!(&INPUT[INPUT.len() - 10..], &buffer[..written]);
355+
356+
// There is nothing to decompress from there on, no matter how far
357+
for offset in [end, end + 1, end + 4096, u64::MAX - 9, u64::MAX] {
358+
assert_eq!(decompress(&mut buffer[..], offset), Ok(0));
359+
assert_eq!(decompress(&mut buffer[..30], offset), Ok(0));
360+
}
340361
}
341362

342363
/// An error can leave a context in a state zstd calls undefined, and says it is

0 commit comments

Comments
 (0)