Skip to content

Commit

Permalink
read: validate DW_OP_deref_size when evaluating (#739)
Browse files Browse the repository at this point in the history
We document that the size field of EvaluationResult::RequiresMemory
will be at most the word size of the target, so we should check that.
  • Loading branch information
philipc authored Aug 2, 2024
1 parent 705d2c0 commit f5dd7df
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ pub enum Error {
UnsupportedTypeOperation,
/// The shift value in an expression must be a non-negative integer.
InvalidShiftExpression,
/// The size of a deref expression must not be larger than the size of an address.
InvalidDerefSize(u8),
/// An unknown DW_CFA_* instruction.
UnknownCallFrameInstruction(constants::DwCfa),
/// The end of an address range was before the beginning.
Expand Down Expand Up @@ -545,6 +547,9 @@ impl Error {
Error::InvalidShiftExpression => {
"The shift value in an expression must be a non-negative integer."
}
Error::InvalidDerefSize(_) => {
"The size of a deref expression must not be larger than the size of an address."
}
Error::UnknownCallFrameInstruction(_) => "An unknown DW_CFA_* instructiion",
Error::InvalidAddressRange => {
"The end of an address range must not be before the beginning."
Expand Down
43 changes: 43 additions & 0 deletions src/read/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,9 @@ impl<R: Reader, S: EvaluationStorage<R>> Evaluation<R, S> {
size,
space,
} => {
if size > self.encoding.address_size {
return Err(Error::InvalidDerefSize(size));
}
let entry = self.pop()?;
let addr = entry.to_u64(self.addr_mask)?;
let addr_space = if space {
Expand Down Expand Up @@ -3507,6 +3510,46 @@ mod tests {
Ok(result)
},
);

#[rustfmt::skip]
let program = [
Op(DW_OP_addr), U32(0x7fff_ffff),
Op(DW_OP_deref_size), U8(8),
];
check_eval_with_args(
&program,
Err(Error::InvalidDerefSize(8)),
encoding4(),
None,
None,
None,
|eval, mut result| {
while result != EvaluationResult::Complete {
result = match result {
EvaluationResult::RequiresMemory {
address,
size,
space,
base_type,
} => {
assert_eq!(base_type, UnitOffset(0));
let mut v = address << 2;
if let Some(value) = space {
v += value;
}
v &= (1u64 << (8 * size)) - 1;
eval.resume_with_memory(Value::Generic(v))?
}
EvaluationResult::RequiresRelocatedAddress(address) => {
eval.resume_with_relocated_address(address)?
}
_ => panic!("Unexpected result: {:?}", result),
};
}

Ok(result)
},
);
}

#[test]
Expand Down

0 comments on commit f5dd7df

Please sign in to comment.