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

Fix golang + linux + fstat #2259

Merged
merged 4 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 changelog.d/2254.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed issue with Golang calling fstat on Linux causing crash
9 changes: 8 additions & 1 deletion mirrord/layer/src/file/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,14 @@ pub(crate) unsafe fn enable_file_hooks(hook_manager: &mut HookManager) {
FN___LXSTAT64
);
replace!(hook_manager, "lstat", lstat_detour, FnLstat, FN_LSTAT);
replace!(hook_manager, "fstat", fstat_detour, FnFstat, FN_FSTAT);
crate::replace_with_fallback!(
aviramha marked this conversation as resolved.
Show resolved Hide resolved
hook_manager,
"fstat",
fstat_detour,
FnFstat,
FN_FSTAT,
libc::fstat
);
replace!(hook_manager, "stat", stat_detour, FnStat, FN_STAT);
replace!(
hook_manager,
Expand Down
52 changes: 52 additions & 0 deletions mirrord/layer/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,58 @@ macro_rules! replace {
}};
}

/// Replaces the `$func` [`libc`] function, with the equivalent hook `$detour_function`, by calling
/// `HookManager::hook_export_or_any`. This variant accepts a fallback function to put in the
/// original function if the hook fails. This is useful in Go when we actually don't hook any libc
/// but still need to call it.
///
/// ## Parameters
///
/// - `$hook_manager`: a valid [`HookManager`](crate::hooks::HookManager) instance that is used to
/// replace the [`libc`] function;
///
/// - `$func`: the function we want to replace;
///
/// - `$detour_function`: one of our detour functions;
///
/// - `$detour_type`: the type alias that was generated by `hook_fn` for this function type;
///
/// - `$hook_fn`: stores the original function pointer as a [`HookFn`](crate::detour::HookFn) that
/// is created by `hook_fn`.
/// - `$fallback_fn`: stores the libc function to fallback to if the hook fails.
///
/// ## Examples
///
/// - Replacing [`libc::close`] with [`close_detour`](crate::close_detour):
///
/// ```rust, no_run
/// unsafe {
/// replace!(&mut hook_manager, "close", close_detour, FnClose, FN_CLOSE);
aviramha marked this conversation as resolved.
Show resolved Hide resolved
/// }
/// ```
#[macro_export]
macro_rules! replace_with_fallback {
($hook_manager:expr, $func:expr, $detour_function:expr, $detour_type:ty, $hook_fn:expr, $fallback_fn:expr) => {{
let intercept = |hook_manager: &mut $crate::hooks::HookManager,
symbol_name,
detour: $detour_type|
-> $crate::error::Result<$detour_type> {
let replaced =
hook_manager.hook_export_or_any(symbol_name, detour as *mut libc::c_void)?;
let original_fn: $detour_type = std::mem::transmute(replaced);

tracing::trace!("hooked {symbol_name:?}");
Ok(original_fn)
};

let _ = intercept($hook_manager, $func, $detour_function)
.and_then(|hooked| Ok($hook_fn.set(hooked).unwrap()))
.unwrap_or_else(|_| {
$hook_fn.set($fallback_fn).unwrap();
});
}};
}

/// Used by [`go_hooks`](crate::go_hooks) to hook go syscalls with
/// `HookManager::hook_symbol_main_module`.
///
Expand Down
Loading