Skip to content

Commit 781ea45

Browse files
gyscosclaude
andcommitted
Check dictionary sample sizes with overflow detection
ZDICT_trainFromBuffer() is never told the length of the samples buffer: the only thing keeping train_from_buffer() and dict::from_continuous() sound is checking that the sample sizes add up to it. Iterator::sum wraps when overflow checks are off (release builds), so a list totalling 2^64 + len passed the check and the trainer read past the buffer. Sum with checked_add at both sites. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-4COHAHJE. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MxSMUdwqoQ3qm6UF82MgZD
1 parent 08ab8ee commit 781ea45

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎src/dict.rs‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,12 @@ pub fn from_continuous(
187187
use crate::map_error_code;
188188

189189
// Complain if the lengths don't add up to the entire data.
190-
if sample_sizes.iter().sum::<usize>() != sample_data.len() {
190+
// Add them with overflow detection: a wrapping sum would accept sizes
191+
// reaching far outside of `sample_data`.
192+
let total_size = sample_sizes
193+
.iter()
194+
.try_fold(0usize, |total, &size| total.checked_add(size));
195+
if total_size != Some(sample_data.len()) {
191196
return Err(io::Error::new(
192197
io::ErrorKind::Other,
193198
"sample sizes don't add up".to_string(),
@@ -366,4 +371,12 @@ mod tests {
366371
assert_eq!(&content, &result);
367372
}
368373
}
374+
375+
#[test]
376+
fn test_from_continuous_overflowing_sizes() {
377+
// These sizes only add up to the data length by wrapping around.
378+
let data = [0u8; 100];
379+
let sizes = [usize::MAX - 99, 200];
380+
assert!(super::from_continuous(&data, &sizes, 4000).is_err());
381+
}
369382
}

‎zstd-safe/src/lib.rs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2684,7 +2684,16 @@ pub fn train_from_buffer<C: WriteBuf + ?Sized>(
26842684
samples_buffer: &[u8],
26852685
samples_sizes: &[usize],
26862686
) -> SafeResult {
2687-
assert_eq!(samples_buffer.len(), samples_sizes.iter().sum());
2687+
// A plain `sum()` wraps around when overflow checks are disabled, and
2688+
// would accept sizes reaching far outside of `samples_buffer`.
2689+
let total_size = samples_sizes
2690+
.iter()
2691+
.try_fold(0usize, |total, &size| total.checked_add(size));
2692+
assert_eq!(
2693+
Some(samples_buffer.len()),
2694+
total_size,
2695+
"sample sizes don't add up to the samples buffer length"
2696+
);
26882697

26892698
unsafe {
26902699
dict_buffer.write_from(|buffer, capacity| {

‎zstd-safe/src/tests.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,17 @@ fn test_dictionary() {
194194
assert_eq!(bytes, decompressed);
195195
}
196196

197+
#[test]
198+
#[should_panic(expected = "sample sizes don't add up")]
199+
fn test_train_from_buffer_overflowing_sizes() {
200+
// These sizes only add up to the buffer length by wrapping around.
201+
let samples = [0u8; 100];
202+
let sizes = [usize::MAX - 99, 200];
203+
let mut dict_buffer = std::vec![0u8; 1024];
204+
let _ =
205+
zstd_safe::train_from_buffer(&mut dict_buffer[..], &samples, &sizes);
206+
}
207+
197208
#[test]
198209
fn test_checksum() {
199210
let mut buffer = std::vec![0u8; 256];

0 commit comments

Comments
 (0)