Hpmason / retour-rs

A cross-platform detour library written in Rust
Other
123 stars 21 forks source link

Bug with RIP relative addressing #42

Open VivekPanyam opened 1 year ago

VivekPanyam commented 1 year ago

The code that handles RIP relative addressing assumes that the displacement is always the last 4 bytes of the instruction:

https://github.com/Hpmason/retour-rs/blob/3bf7863bc0ad4ba1d0657e6ee98d43145c16b658/src/arch/x86/trampoline/mod.rs#L166-L167

This is not always true. CMP reg/mem32, imm32 is an example.

Locally, I fixed this by scanning the bytes of the original instruction for the original displacement value, making sure there's only one match, and then using that location as the index.

There's probably a cleaner way to do that (ideally you'd get the offset of the operand within the instruction from the disassembler). I can put up a PR if you'd like, but my code simply asserts that there's one matching location, which is probably not flexible enough to include in the library.

Hpmason commented 1 year ago

Good catch. There's currently a PR (#41) already replacing a chunk of that code, so we can make sure include a fix for this there.

kerrytazi commented 2 weeks ago

Any progress? It's been a year. Still no fix? Is this repo even alive?

Hpmason commented 2 weeks ago

Sorry, haven't had a ton of free time, but I really appreciate the PR! I'm working on reviewing it now.