FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
security(ogg): bound recovery scanning (#2950)
Earl Co85db84cf93f3
diff --git a/crates/ogg/src/lib.rs b/crates/ogg/src/lib.rsindex a251cca3e..2f734b104 100644--- a/crates/ogg/src/lib.rs+++ b/crates/ogg/src/lib.rs@@ -211,11 +211,12 @@ pub fn open(src: &(impl ByteSource + ?Sized)) -> Result<OggFile> {let mut warnings = Vec::new();let mut pos = 0u64;let mut found_any = false;+ let mut recovery_budget = 64 << 20;let mut hdr = [0u8; 27 + 255];while pos + 27 <= len {src.read_at(pos, &mut hdr[..27])?;if &hdr[..4] != b"OggS" || hdr[4] != 0 {- match find_capture(src, pos + 1, len)? {+ match find_capture(src, pos + 1, len, &mut recovery_budget)? {Some(p) => {if found_any {warnings.push(format!("lost sync at byte {pos}; resumed at {p}"));@@ -328,12 +329,13 @@ pub fn open(src: &(impl ByteSource + ?Sized)) -> Result<OggFile> {Ok(OggFile { streams, warnings })}-fn find_capture(src: &(impl ByteSource + ?Sized), from: u64, len: u64) -> Result<Option<u64>> {+fn find_capture(src: &(impl ByteSource + ?Sized), from: u64, len: u64, budget: &mut usize) -> Result<Option<u64>> {let mut at = from;- while at + 4 <= len {- let n = ((len - at) as usize).min(1 << 20);+ while at + 4 <= len && *budget > 0 {+ let n = ((len - at) as usize).min(1 << 20).min(*budget);let mut buf = vec![0u8; n];src.read_at(at, &mut buf)?;+ *budget = budget.saturating_sub(n);if let Some(i) = buf.windows(4).position(|w| w == b"OggS") {return Ok(Some(at + i as u64));}diff --git a/crates/ogg/src/tests.rs b/crates/ogg/src/tests.rsindex f1879b36b..b2d7c4c77 100644--- a/crates/ogg/src/tests.rs+++ b/crates/ogg/src/tests.rs@@ -2,6 +2,15 @@use super::*;+#[test]+fn recovery_scan_stops_when_its_shared_budget_is_spent() {+ let source = b"junkOggS".to_vec();+ let mut budget = 3;+ assert_eq!(find_capture(&source, 0, source.len() as u64, &mut budget).unwrap(), None);+ let mut budget = source.len();+ assert_eq!(find_capture(&source, 0, source.len() as u64, &mut budget).unwrap(), Some(4));+}+/// Write one page holding `segments` (lacing already computed by `lace`).fn page(out: &mut Vec<u8>, flags: u8, granule: i64, serial: u32, seq: u32, lacing: &[u8], body: &[u8]) {let start = out.len();