Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Result and Error on interceptor attach and attach_instruction #143

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading