From feea4e316df5c0a2ce6466f409a96bc58bde0bae Mon Sep 17 00:00:00 2001 From: BFlat Date: Sun, 15 Nov 2026 07:55:30 -0500 Subject: [PATCH] Import binary plist parser changes from Craft Anything 15de3661 --- crates/plist/src/binary.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/crates/plist/src/binary.rs b/crates/plist/src/binary.rs index 21ac84a86..9b792410a 100644 --- a/crates/plist/src/binary.rs +++ b/crates/plist/src/binary.rs @@ -102,13 +102,15 @@ pub fn parse_binary(data: &[u8]) -> Result { for i in 0..num { let o = read_be(&r, table + i * off_size, off_size).ok_or_else(|| FormatError::Truncated("offset table".into()))?; let o = usize::try_from(o).map_err(|_| FormatError::Malformed("object offset".into()))?; - if o >= table { + if o < 8 || o >= table { return Err(FormatError::Malformed(format!("object {i} lies outside the object area"))); } offsets.push(o); } let top = usize::try_from(top).map_err(|_| FormatError::Malformed("top object".into()))?; - let mut b = Bin { r, offsets, ref_size, nodes: 0 }; + // Object payloads and references must not borrow bytes from the offset table or trailer. + let objects = data.get(..table).ok_or_else(|| FormatError::Truncated("object area".into()))?; + let mut b = Bin { r: Reader::new(objects), offsets, ref_size, nodes: 0 }; let mut stack = Vec::new(); b.object(top, &mut stack) } @@ -127,7 +129,10 @@ impl Bin<'_> { if m & 0xf0 != 0x10 { return Err(FormatError::Malformed("bad extended length marker".into())); } - let n = 1usize << (m & 0x0f).min(3); + if m & 0x0f > 3 { + return Err(FormatError::Malformed("extended length width exceeds 64 bits".into())); + } + let n = 1usize << (m & 0x0f); let v = read_be(&self.r, at + 2, n).ok_or_else(|| FormatError::Truncated("length".into()))?; let v = usize::try_from(v).map_err(|_| FormatError::Malformed("length".into()))?; Ok((v, at + 2 + n)) @@ -161,7 +166,10 @@ impl Bin<'_> { _ => Value::Null, }, 0x1 => { - let n = 1usize << lo.min(4); + if lo > 4 { + return Err(FormatError::Malformed("integer width exceeds 128 bits".into())); + } + let n = 1usize << lo; let bytes = self.r.slice(at + 1, n).ok_or_else(|| FormatError::Truncated("integer".into()))?; let v = match n { 8 => i128::from(i64::from_be_bytes(bytes.try_into().map_err(|_| FormatError::Truncated("integer".into()))?)), @@ -197,6 +205,9 @@ impl Bin<'_> { Value::String(String::from_utf16_lossy(&units)) } 0x8 => { + if lo > 7 { + return Err(FormatError::Malformed("uid width exceeds 64 bits".into())); + } let n = usize::from(lo) + 1; Value::Uid(read_be(&self.r, at + 1, n).ok_or_else(|| FormatError::Truncated("uid".into()))?) }