Bound binary FBX record and property allocations

This commit is contained in:
Dylan M. Taylor
2026-11-15 18:47:54 +00:00
parent 3f4de9ca11
commit ad0205e903
2 changed files with 36 additions and 4 deletions
+17 -4
View File
@@ -254,6 +254,7 @@ struct Bin<'a> {
/// 64-bit record offsets (version 7500 and later).
wide: bool,
budget: &'a Budget,
records_budget: &'a Budget,
}
impl Bin<'_> {
@@ -313,6 +314,8 @@ impl Bin<'_> {
if end <= start || end > limit.min(self.d.len()) {
return Err(malformed(KIND, format!("bad record end {end} at byte {start}")));
}
// Bound record and property-vector allocations before copying names or reserving storage.
self.records_budget.take(std::mem::size_of::<Record>().checked_add(nlen.saturating_mul(3)), "its records")?;
let name = String::from_utf8_lossy(self.take(pos, nlen)?).into_owned();
let pstart = *pos;
let pend =
@@ -321,7 +324,9 @@ impl Bin<'_> {
if nprops > plen as u64 {
return Err(malformed(KIND, format!("`{name}`: {nprops} properties in {plen} bytes")));
}
let mut props = Vec::with_capacity(nprops as usize);
let count = usize::try_from(nprops).map_err(|_| too_large(KIND, "too many properties"))?;
self.records_budget.take(count.checked_mul(std::mem::size_of::<Prop>()), "its properties")?;
let mut props = Vec::with_capacity(count);
for _ in 0..nprops {
props.push(self.prop(pos, pend)?);
}
@@ -332,7 +337,12 @@ impl Bin<'_> {
}
fn prop(&self, pos: &mut usize, pend: usize) -> Result<Prop> {
let sub = Bin { d: self.d.get(..pend).ok_or_else(|| malformed(KIND, "truncated property"))?, wide: self.wide, budget: self.budget };
let sub = Bin {
d: self.d.get(..pend).ok_or_else(|| malformed(KIND, "truncated property"))?,
wide: self.wide,
budget: self.budget,
records_budget: self.records_budget,
};
let t = sub.arr::<1>(pos)?[0];
Ok(match t {
b'Y' => Prop::Int(i64::from(i16::from_le_bytes(sub.arr(pos)?))),
@@ -343,10 +353,12 @@ impl Bin<'_> {
b'D' => Prop::Float(f64::from_le_bytes(sub.arr(pos)?)),
b'S' => {
let n = sub.u32(pos)? as usize;
sub.budget.take(n.checked_mul(3), "a string")?;
Prop::Str(object_name(sub.take(pos, n)?))
}
b'R' => {
let n = sub.u32(pos)? as usize;
sub.budget.take(Some(n), "binary data")?;
Prop::Bytes(sub.take(pos, n)?.to_vec())
}
b'f' | b'd' | b'l' | b'i' | b'b' => {
@@ -400,8 +412,9 @@ fn object_name(b: &[u8]) -> String {
fn read_binary(bytes: &[u8], budget: &Budget) -> Result<(u32, Vec<Record>)> {
let mut pos = BINARY_MAGIC.len();
let version = Bin { d: bytes, wide: false, budget }.u32(&mut pos)?;
let bin = Bin { d: bytes, wide: version >= 7500, budget };
let records_budget = Budget::new(budget.limit);
let version = Bin { d: bytes, wide: false, budget, records_budget: &records_budget }.u32(&mut pos)?;
let bin = Bin { d: bytes, wide: version >= 7500, budget, records_budget: &records_budget };
let mut records = vec![];
while pos < bytes.len() {
match bin.record(&mut pos, bytes.len(), 0) {
+19
View File
@@ -627,3 +627,22 @@ fn fbx6_model_holds_its_mesh() {
assert_eq!(d.unit_scale(), Some(100.0));
}
}
#[test]
fn binary_scalar_properties_are_bounded_before_reserving() {
let mut bytes = BINARY_MAGIC.to_vec();
bytes.extend(7400u32.to_le_bytes());
let count = 1000u32;
let end = bytes.len() + 13 + 1 + count as usize * 2;
bytes.extend((end as u32).to_le_bytes());
bytes.extend(count.to_le_bytes());
bytes.extend((count * 2).to_le_bytes());
bytes.push(1);
bytes.push(b'A');
for _ in 0..count {
bytes.extend([b'C', 1]);
}
let limits = Limits { max_data_bytes: 1024, ..Limits::default() };
assert!(matches!(read_document(&bytes, &limits), Err(Error::TooLarge(_))));
assert!(read_document(&bytes, &Limits::default()).is_ok());
}