Skip to content

Commit

Permalink
Add Result and Error on interceptor attach and attach_instruction (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aursen authored Sep 5, 2024
1 parent 2ee2acb commit 117003b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/gum/hook_instruction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ fn init() {
let mut interceptor = Interceptor::obtain(&GUM);
let open = Module::find_export_by_name(None, "open").unwrap();
let mut listener = OpenProbeListener;
interceptor.attach_instruction(open, &mut listener);
interceptor.attach_instruction(open, &mut listener).unwrap();
}
2 changes: 1 addition & 1 deletion examples/gum/open/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ extern "C" fn example_agent_main(_user_data: *const c_void, resident: *mut c_int
}

let open = Module::find_export_by_name(None, "open").unwrap();
interceptor.attach(open, &mut listener);
interceptor.attach(open, &mut listener).unwrap();
}
4 changes: 4 additions & 0 deletions frida-gum/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ pub enum Error {
/// Function is already replaced during Interceptor operation
InterceptorAlreadyReplaced,

/// Function is already attached during Interceptor operation
InterceptorAlreadyAttached,

/// Policy violation
PolicyViolation,

Expand All @@ -30,6 +33,7 @@ impl fmt::Display for Error {
match self {
Error::InterceptorBadSignature => write!(fmt, "Bad signature"),
Error::InterceptorAlreadyReplaced => write!(fmt, "Function already replaced"),
Error::InterceptorAlreadyAttached => write!(fmt, "Function already attached"),
Error::PolicyViolation => write!(fmt, "Policy violation"),
Error::InterceptorError => write!(fmt, "Interceptor error"),
Error::MemoryAccessError => write!(fmt, "Memory access error"),
Expand Down
36 changes: 28 additions & 8 deletions frida-gum/src/interceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,22 @@ impl<'a> Interceptor<'a> {
&mut self,
f: NativePointer,
listener: &mut I,
) -> NativePointer {
) -> Result<NativePointer> {
let listener = invocation_listener_transform(listener);
unsafe {
match unsafe {
gum_sys::gum_interceptor_attach(self.interceptor, f.0, listener, ptr::null_mut())
};
NativePointer(listener as *mut c_void)
} {
gum_sys::GumAttachReturn_GUM_ATTACH_OK => Ok(NativePointer(listener as *mut c_void)),
gum_sys::GumAttachReturn_GUM_ATTACH_WRONG_SIGNATURE => {
Err(Error::InterceptorBadSignature)
}
gum_sys::GumAttachReturn_GUM_ATTACH_ALREADY_ATTACHED => {
Err(Error::InterceptorAlreadyAttached)
}
gum_sys::GumAttachReturn_GUM_ATTACH_POLICY_VIOLATION => Err(Error::PolicyViolation),
gum_sys::GumAttachReturn_GUM_ATTACH_WRONG_TYPE => Err(Error::WrongType),
_ => Err(Error::InterceptorError),
}
}

/// Attach a listener to an instruction address.
Expand All @@ -71,12 +81,22 @@ impl<'a> Interceptor<'a> {
&mut self,
instr: NativePointer,
listener: &mut I,
) -> NativePointer {
) -> Result<NativePointer> {
let listener = probe_listener_transform(listener);
unsafe {
match unsafe {
gum_sys::gum_interceptor_attach(self.interceptor, instr.0, listener, ptr::null_mut())
};
NativePointer(listener as *mut c_void)
} {
gum_sys::GumAttachReturn_GUM_ATTACH_OK => Ok(NativePointer(listener as *mut c_void)),
gum_sys::GumAttachReturn_GUM_ATTACH_WRONG_SIGNATURE => {
Err(Error::InterceptorBadSignature)
}
gum_sys::GumAttachReturn_GUM_ATTACH_ALREADY_ATTACHED => {
Err(Error::InterceptorAlreadyAttached)
}
gum_sys::GumAttachReturn_GUM_ATTACH_POLICY_VIOLATION => Err(Error::PolicyViolation),
gum_sys::GumAttachReturn_GUM_ATTACH_WRONG_TYPE => Err(Error::WrongType),
_ => Err(Error::InterceptorError),
}
}

/// Detach an attached listener.
Expand Down

0 comments on commit 117003b

Please sign in to comment.