Skip to content

Commit 5827de2

Browse files
gyscosclaude
andcommitted
zstd-safe: zero-fill the gap in OutBuffer::around_pos
around_pos() only checked pos <= capacity(), but the next streaming call marks everything before the position as initialized (Vec::set_len), so a position past the Vec's length exposed whatever the allocator left there. Zero-fill from the end of the initialized data up to pos, as the Cursor implementation already does for the bytes before its position. Also have Cursor::as_slice() return an empty slice rather than panic when the cursor is past the inner buffer's initialized part. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-IRIM9MSX. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MxSMUdwqoQ3qm6UF82MgZD
1 parent adbe03a commit 5827de2

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

‎zstd-safe/src/lib.rs‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1928,7 +1928,12 @@ where
19281928
T: WriteBuf,
19291929
{
19301930
fn as_slice(&self) -> &[u8] {
1931-
&self.get_ref().as_slice()[cursor_position(self)..]
1931+
// A cursor past the inner buffer's initialized part has nothing
1932+
// initialized after it yet (see `filled_until` below).
1933+
self.get_ref()
1934+
.as_slice()
1935+
.get(cursor_position(self)..)
1936+
.unwrap_or(&[])
19321937
}
19331938

19341939
fn capacity(&self) -> usize {
@@ -2132,6 +2137,10 @@ impl<'a, C: WriteBuf + ?Sized> OutBuffer<'a, C> {
21322137

21332138
/// Returns a new `OutBuffer` around the given slice, starting at the given position.
21342139
///
2140+
/// If `pos` is past the initialized part of `dst` (for example a `Vec`
2141+
/// whose `len()` is smaller than `pos`), the gap is zero-filled, as a
2142+
/// `Cursor` would do.
2143+
///
21352144
/// # Panics
21362145
///
21372146
/// If `pos > dst.capacity()`.
@@ -2140,6 +2149,20 @@ impl<'a, C: WriteBuf + ?Sized> OutBuffer<'a, C> {
21402149
panic!("Given position outside of the buffer bounds.");
21412150
}
21422151

2152+
let initialized = dst.as_slice().len();
2153+
if pos > initialized {
2154+
// Writing to this buffer marks everything before `pos` as
2155+
// initialized (see `set_pos`), so it has to be before we start.
2156+
// Safety: `initialized..pos` lies within `capacity()`, which
2157+
// `WriteBuf` promises is valid to write to.
2158+
unsafe {
2159+
dst.as_mut_ptr()
2160+
.add(initialized)
2161+
.write_bytes(0u8, pos - initialized);
2162+
dst.filled_until(pos);
2163+
}
2164+
}
2165+
21432166
OutBuffer { dst, pos }
21442167
}
21452168

‎zstd-safe/src/tests.rs‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,46 @@ fn test_writebuf() {
4646
assert_eq!(data.as_slice(), &[0, 1, 2, 3, 0, 0, 4, 5, 6, 7]);
4747
}
4848

49+
#[cfg(feature = "std")]
50+
#[test]
51+
fn test_around_pos_past_initialized_data() {
52+
use zstd_safe::{InBuffer, OutBuffer};
53+
54+
let end = zstd_safe::zstd_sys::ZSTD_EndDirective::ZSTD_e_end;
55+
let mut cctx = zstd_safe::CCtx::create();
56+
57+
// A position past the `Vec`'s length: the gap must be zero-filled, not
58+
// left as whatever the allocator handed out.
59+
let mut buffer = Vec::with_capacity(256);
60+
let mut input = InBuffer::around(INPUT);
61+
let mut output = OutBuffer::around_pos(&mut buffer, 16);
62+
assert_eq!(output.as_slice(), &[0u8; 16]);
63+
while cctx.compress_stream2(&mut output, &mut input, end).unwrap() != 0 {}
64+
assert_eq!(&buffer[..16], &[0u8; 16]);
65+
assert!(buffer.len() > 16);
66+
67+
// Same through a `Cursor` that itself sits past the `Vec`'s length.
68+
let mut buffer = Vec::with_capacity(256);
69+
let mut cursor = std::io::Cursor::new(&mut buffer);
70+
cursor.set_position(4);
71+
let mut input = InBuffer::around(INPUT);
72+
let mut output = OutBuffer::around_pos(&mut cursor, 8);
73+
assert_eq!(output.as_slice(), &[0u8; 8]);
74+
while cctx.compress_stream2(&mut output, &mut input, end).unwrap() != 0 {}
75+
assert_eq!(&buffer[..12], &[0u8; 12]);
76+
assert!(buffer.len() > 12);
77+
78+
// A position inside the initialized part keeps the existing data.
79+
let mut buffer = std::vec![7u8; 4];
80+
buffer.reserve(256);
81+
let mut input = InBuffer::around(INPUT);
82+
let mut output = OutBuffer::around_pos(&mut buffer, 2);
83+
assert_eq!(output.as_slice(), &[7u8; 2]);
84+
while cctx.compress_stream2(&mut output, &mut input, end).unwrap() != 0 {}
85+
assert_eq!(&buffer[..2], &[7u8; 2]);
86+
assert!(buffer.len() > 2);
87+
}
88+
4989
#[cfg(feature = "std")]
5090
#[test]
5191
fn test_simple_cycle() {

0 commit comments

Comments
 (0)