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

Frida windows thread local #2433

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions libafl_frida/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ frida-gum = { version = "0.13.6", features = [
"invocation-listener",
"module-names",
] }
os-thread-local = "0.1.3"
dynasmrt = "2"

color-backtrace = { version = "0.6", features = ["resolve-modules"] }
Expand Down
36 changes: 15 additions & 21 deletions libafl_frida/src/asan/asan_rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use frida_gum_sys::Insn;
use hashbrown::HashMap;
use libafl_bolts::{cli::FuzzerOptions, AsSlice};
use libc::wchar_t;
use os_thread_local::ThreadLocal;
use rangemap::RangeMap;
#[cfg(target_arch = "aarch64")]
use yaxpeax_arch::Arch;
Expand Down Expand Up @@ -94,10 +95,6 @@ pub const ASAN_SAVE_REGISTER_NAMES: [&str; ASAN_SAVE_REGISTER_COUNT] = [
"actual rip",
];

thread_local! {
static ASAN_IN_HOOK: Cell<bool> = const { Cell::new(false) };
}

/// The count of registers that need to be saved by the asan runtime
#[cfg(target_arch = "aarch64")]
pub const ASAN_SAVE_REGISTER_COUNT: usize = 32;
Expand Down Expand Up @@ -140,6 +137,7 @@ pub struct AsanRuntime {
pc: Option<usize>,
hooks: Vec<NativePointer>,
pub(crate) hooks_enabled: bool,
thread_in_hook: ThreadLocal<Cell<bool>>,
#[cfg(target_arch = "aarch64")]
eh_frame: [u32; ASAN_EH_FRAME_DWORD_COUNT],
}
Expand Down Expand Up @@ -512,18 +510,13 @@ impl AsanRuntime {
//is this necessary? The stalked return address will always be the real return address
// let real_address = this.real_address_for_stalked(invocation.return_addr());
let original = [<$name:snake:upper _PTR>].get().unwrap();
if this.hooks_enabled {
let previous_hook_state = this.hooks_enabled;
this.hooks_enabled = false;
if !this.thread_in_hook.with(|f| f.get()) && this.hooks_enabled {
this.thread_in_hook.with(|f|f.set(true));
let ret = this.[<hook_ $name>](*original, $($param),*);
this.hooks_enabled = previous_hook_state;
this.thread_in_hook.with(|f|f.set(false));
ret
} else {

let previous_hook_state = this.hooks_enabled;
this.hooks_enabled = false;
let ret = (original)($($param),*);
this.hooks_enabled = previous_hook_state;
ret
}
}
Expand Down Expand Up @@ -556,10 +549,10 @@ impl AsanRuntime {
//is this necessary? The stalked return address will always be the real return address
// let real_address = this.real_address_for_stalked(invocation.return_addr());
let original = [<$lib_ident:snake:upper _ $name:snake:upper _PTR>].get().unwrap();
if !ASAN_IN_HOOK.get() && this.hooks_enabled {
ASAN_IN_HOOK.set(true);
if !this.thread_in_hook.with(|f| f.get()) && this.hooks_enabled {
this.thread_in_hook.with(|f|f.set(true));
let ret = this.[<hook_ $name>](*original, $($param),*);
ASAN_IN_HOOK.set(false);
this.thread_in_hook.with(|f|f.set(false));
ret
} else {
let ret = (original)($($param),*);
Expand Down Expand Up @@ -599,10 +592,10 @@ impl AsanRuntime {
let this = &mut *(invocation.replacement_data().unwrap().0 as *mut AsanRuntime);
let original = [<$name:snake:upper _PTR>].get().unwrap();

if !ASAN_IN_HOOK.get() && this.hooks_enabled && this.[<hook_check_ $name>]($($param),*){
ASAN_IN_HOOK.set(true);
if !this.thread_in_hook.with(|f| f.get()) && this.hooks_enabled && this.[<hook_check_ $name>]($($param),*){
this.thread_in_hook.with(|f|f.set(true));
let ret = this.[<hook_ $name>](*original, $($param),*);
ASAN_IN_HOOK.set(false);
this.thread_in_hook.with(|f|f.set(false));
ret
} else {
let ret = (original)($($param),*);
Expand Down Expand Up @@ -638,10 +631,10 @@ impl AsanRuntime {
let this = &mut *(invocation.replacement_data().unwrap().0 as *mut AsanRuntime);
let original = [<$lib_ident:snake:upper _ $name:snake:upper _PTR>].get().unwrap();

if !ASAN_IN_HOOK.get() && this.hooks_enabled && this.[<hook_check_ $name>]($($param),*){
ASAN_IN_HOOK.set(true);
if !this.thread_in_hook.with(|f|f.get()) && this.hooks_enabled && this.[<hook_check_ $name>]($($param),*){
this.thread_in_hook.with(|f|f.set(true));
let ret = this.[<hook_ $name>](*original, $($param),*);
ASAN_IN_HOOK.set(false);
this.thread_in_hook.with(|f|f.set(false));
ret
} else {
let ret = (original)($($param),*);
Expand Down Expand Up @@ -2749,6 +2742,7 @@ impl Default for AsanRuntime {
pc: None,
hooks: Vec::new(),
hooks_enabled: false,
thread_in_hook: ThreadLocal::new(|| Cell::new(false)),
}
}
}