Closed clubby789 closed 2 years ago
The issue seems to be related to the clone
ing of instructions. I was iterating over the text sections (using the object
crate) like this:
fn disassemble_section<'a>(cs: &'a Capstone, section: Section<'a, 'a, &'a [u8]>) -> Vec<Insn<'a>> {
cs.disasm_all(section.data().unwrap(), section.address())
.expect("Disassembly failed")
.iter()
.cloned()
.collect()
}
Refactoring my code to remove the clone so that the original instruction references are used all the way down prevents the crash.
Not sure of the root cause, but lifetimes are improperly handled. The clone
implementation for Insn
just memcpy
s the data (including the detail
pointer) then Drop
is called on each instruction, which calls cs_free
, which frees the detail pointer
Thanks for finding the issue! It's not simple creating a safe API over library like capstone.
While iterating over
operands
(from theX86InsnDetail
binding) I'm experiencing a crash due to an invalidop_type
.The crash occurs specifically in https://github.com/capstone-rust/capstone-rs/blob/fa37a3779b440fe9efe4f30265a714b09ea77076/capstone-rs/src/arch/x86.rs#L30-L42 -
op_type
has a value of1633878019
which causes an invalid read from thematch
's jump table.If I force my program to skip the offending instruction, it later panics on an out-of-bounds array access: https://github.com/capstone-rust/capstone-rs/blob/fa37a3779b440fe9efe4f30265a714b09ea77076/capstone-rs/src/arch/mod.rs#L671, where the
operands
array is of size 8 andop_count
has a value of 160.Neither crash occurs when running under Valgrind, so I haven't been able to trace the root cause of the memory corruption.
I'm not sure if this is a bug in capstone-rs, capstone-sys, or Capstone itself, but I'm reporting it here as that's where the crash is occuring for me.
Unfortunately, I haven't been able to create a reliable reproducer, as the crash is only triggered in a large program. However, this snippet demonstrates the configuation I am using and code I am disassembling. The crash occurs at the instruction at offset 0x30:
0x2030: jmp qword ptr [rip + 0x6e4a]