Skip to content

Commit cd63341

Browse files
committed
Thread marshal recursion depth through nested code objects
Code objects embedded in const-tuples reset the depth budget on each recursion, so a hostile or pathological marshal stream of code-in-tuple- in-code can blow the stack despite MAX_MARSHAL_STACK_DEPTH. Pass the current depth through deserialize_code_inner and read_marshal_const_tuple and decrement at each code-object/tuple boundary. Also route dict keys through deserialize_value_after_header so TYPE_CODE keys decode instead of failing with BadType.
1 parent 8c3b61f commit cd63341

1 file changed

Lines changed: 35 additions & 31 deletions

File tree

‎crates/compiler-core/src/marshal.rs‎

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ pub fn deserialize_code<R: Read, Bag: ConstantBag>(
195195
bag: Bag,
196196
) -> Result<CodeObject<Bag::Constant>> {
197197
let mut refs: Vec<Option<Bag::Constant>> = Vec::new();
198-
deserialize_code_inner(rdr, bag, &mut refs)
198+
deserialize_code_inner(rdr, bag, MAX_MARSHAL_STACK_DEPTH, &mut refs)
199199
}
200200

201201
/// Inner code-object deserializer that shares a ref table with caller.
@@ -204,8 +204,12 @@ pub fn deserialize_code<R: Read, Bag: ConstantBag>(
204204
fn deserialize_code_inner<R: Read, Bag: ConstantBag>(
205205
rdr: &mut R,
206206
bag: Bag,
207+
depth: usize,
207208
refs: &mut Vec<Option<Bag::Constant>>,
208209
) -> Result<CodeObject<Bag::Constant>> {
210+
if depth == 0 {
211+
return Err(MarshalError::InvalidBytecode);
212+
}
209213
// 1–5: scalar fields
210214
let arg_count = rdr.read_u32()?;
211215
let posonlyarg_count = rdr.read_u32()?;
@@ -217,7 +221,7 @@ fn deserialize_code_inner<R: Read, Bag: ConstantBag>(
217221
let code_bytes = read_marshal_bytes(rdr, &bag, refs)?;
218222

219223
// 7: co_consts
220-
let constants = read_marshal_const_tuple(rdr, bag, refs)?;
224+
let constants = read_marshal_const_tuple(rdr, bag, depth, refs)?;
221225

222226
// 8: co_names
223227
let names = read_marshal_name_tuple(rdr, &bag, refs)?;
@@ -448,8 +452,12 @@ fn read_marshal_name_tuple<R: Read, Bag: ConstantBag>(
448452
fn read_marshal_const_tuple<R: Read, Bag: ConstantBag>(
449453
rdr: &mut R,
450454
bag: Bag,
455+
depth: usize,
451456
refs: &mut Vec<Option<Bag::Constant>>,
452457
) -> Result<Constants<Bag::Constant>> {
458+
if depth == 0 {
459+
return Err(MarshalError::InvalidBytecode);
460+
}
453461
let raw = rdr.read_u8()?;
454462
let type_byte = raw & !FLAG_REF;
455463
let has_flag = raw & FLAG_REF != 0;
@@ -469,8 +477,9 @@ fn read_marshal_const_tuple<R: Read, Bag: ConstantBag>(
469477
_ => return Err(MarshalError::BadType),
470478
};
471479
let slot = reserve_ref_slot(has_flag, refs);
480+
let child_depth = depth - 1;
472481
let items: Vec<Bag::Constant> = (0..n)
473-
.map(|_| read_const_value(rdr, bag, MAX_MARSHAL_STACK_DEPTH, refs))
482+
.map(|_| read_const_value(rdr, bag, child_depth, refs))
474483
.collect::<Result<_>>()?;
475484
if let Some(idx) = slot {
476485
refs[idx] =
@@ -505,7 +514,7 @@ fn read_const_value<R: Read, Bag: ConstantBag>(
505514
let slot = reserve_ref_slot(flag, refs);
506515
let typ = Type::try_from(type_code)?;
507516
let value = if matches!(typ, Type::Code) {
508-
let code = deserialize_code_inner(rdr, bag, refs)?;
517+
let code = deserialize_code_inner(rdr, bag, depth - 1, refs)?;
509518
bag.make_code(code)
510519
} else {
511520
deserialize_value_typed(rdr, bag, depth, refs, typ)?
@@ -679,6 +688,23 @@ fn deserialize_value_depth<R: Read, Bag: MarshalBag>(
679688
return Err(MarshalError::InvalidBytecode);
680689
}
681690
let raw = rdr.read_u8()?;
691+
deserialize_value_after_header(rdr, bag, depth, refs, raw)
692+
}
693+
694+
/// Continue deserializing a value after the header byte has already been
695+
/// consumed. Shared by `deserialize_value_depth` and the dict-key branch,
696+
/// where the header byte is read up front to detect the TYPE_NULL
697+
/// terminator.
698+
fn deserialize_value_after_header<R: Read, Bag: MarshalBag>(
699+
rdr: &mut R,
700+
bag: Bag,
701+
depth: usize,
702+
refs: &mut Vec<Option<Bag::Value>>,
703+
raw: u8,
704+
) -> Result<Bag::Value> {
705+
if depth == 0 {
706+
return Err(MarshalError::InvalidBytecode);
707+
}
682708
let flag = raw & FLAG_REF != 0;
683709
let type_code = raw & !FLAG_REF;
684710

@@ -704,14 +730,14 @@ fn deserialize_value_depth<R: Read, Bag: MarshalBag>(
704730
// Code-objects keep their own inner ref table because Bag::Value (the
705731
// outer marshal value) and the constant-bag's Constant type are not
706732
// in general the same. When the outer header carried FLAG_REF, the
707-
// code object occupies slot 0 of CPython's single global ref space,
708-
// so we mirror that by reserving slot 0 of the inner table.
733+
// code object occupies slot 0 of the single global ref space, so we
734+
// mirror that by reserving slot 0 of the inner table.
709735
let value = if matches!(typ, Type::Code) {
710736
let mut inner_refs: Vec<Option<<Bag::ConstantBag as ConstantBag>::Constant>> = Vec::new();
711737
if flag {
712738
inner_refs.push(None);
713739
}
714-
let code = deserialize_code_inner(rdr, bag.constant_bag(), &mut inner_refs)?;
740+
let code = deserialize_code_inner(rdr, bag.constant_bag(), depth - 1, &mut inner_refs)?;
715741
bag.make_code(code)
716742
} else {
717743
deserialize_value_typed(rdr, bag, depth, refs, typ)?
@@ -817,32 +843,10 @@ fn deserialize_value_typed<R: Read, Bag: MarshalBag>(
817843
let mut pairs = Vec::new();
818844
loop {
819845
let raw = rdr.read_u8()?;
820-
let type_code = raw & !FLAG_REF;
821-
if type_code == b'0' {
846+
if raw & !FLAG_REF == b'0' {
822847
break;
823848
}
824-
// TYPE_REF for key
825-
let k = if type_code == Type::Ref as u8 {
826-
let idx = rdr.read_u32()? as usize;
827-
refs.get(idx)
828-
.and_then(|v| v.clone())
829-
.ok_or(MarshalError::InvalidBytecode)?
830-
} else {
831-
let flag = raw & FLAG_REF != 0;
832-
let key_slot = if flag {
833-
let idx = refs.len();
834-
refs.push(None);
835-
Some(idx)
836-
} else {
837-
None
838-
};
839-
let key_type = Type::try_from(type_code)?;
840-
let k = deserialize_value_typed(rdr, bag, d, refs, key_type)?;
841-
if let Some(idx) = key_slot {
842-
refs[idx] = Some(k.clone());
843-
}
844-
k
845-
};
849+
let k = deserialize_value_after_header(rdr, bag, d, refs, raw)?;
846850
let v = deserialize_value_depth(rdr, bag, d, refs)?;
847851
pairs.push((k, v));
848852
}

0 commit comments

Comments
 (0)