Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit 535c7a4

Browse files
mergify[bot]mvines
andauthored
Strengthen EpochSlots sanitization (#13873)
(cherry picked from commit 90d557d) Co-authored-by: Michael Vines <mvines@gmail.com>
1 parent fbbc640 commit 535c7a4

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

core/src/epoch_slots.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ impl Sanitize for Uncompressed {
2424
if self.num >= MAX_SLOTS_PER_ENTRY {
2525
return Err(SanitizeError::ValueOutOfBounds);
2626
}
27+
if self.slots.len() % 8 != 0 {
28+
// Uncompressed::new() ensures the length is always a multiple of 8
29+
return Err(SanitizeError::ValueOutOfBounds);
30+
}
31+
if self.slots.len() != self.slots.capacity() {
32+
// A BitVec<u8> with a length that's a multiple of 8 will always have len() equal to
33+
// capacity(), assuming no bit manipulation
34+
return Err(SanitizeError::ValueOutOfBounds);
35+
}
2736
Ok(())
2837
}
2938
}
@@ -132,7 +141,7 @@ impl Uncompressed {
132141
if *s < self.first_slot {
133142
return i;
134143
}
135-
if *s - self.first_slot >= self.slots.capacity() {
144+
if *s - self.first_slot >= self.slots.len() {
136145
return i;
137146
}
138147
self.slots.set(*s - self.first_slot, true);
@@ -393,6 +402,14 @@ mod tests {
393402
o.num = MAX_SLOTS_PER_ENTRY;
394403
assert_eq!(o.sanitize(), Err(SanitizeError::ValueOutOfBounds));
395404

405+
let mut o = slots.clone();
406+
o.slots = BitVec::new_fill(false, 7); // Length not a multiple of 8
407+
assert_eq!(o.sanitize(), Err(SanitizeError::ValueOutOfBounds));
408+
409+
let mut o = slots.clone();
410+
o.slots = BitVec::with_capacity(8); // capacity() not equal to len()
411+
assert_eq!(o.sanitize(), Err(SanitizeError::ValueOutOfBounds));
412+
396413
let compressed = Flate2::deflate(slots).unwrap();
397414
assert!(compressed.sanitize().is_ok());
398415

0 commit comments

Comments
 (0)