Skip to content

Commit

Permalink
readlink attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
aviramha committed Jun 7, 2024
1 parent 1ce31d1 commit dc94168
Showing 1 changed file with 50 additions and 4 deletions.
54 changes: 50 additions & 4 deletions mirrord/layer/src/file/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,17 +948,55 @@ unsafe extern "C" fn realpath_detour(
source_path: *const c_char,
output_path: *mut c_char,
) -> *mut c_char {
realpath_logic(source_path, output_path)
.unwrap_or_bypass_with(|_| FN_REALPATH(source_path, output_path))
realpath_logic(source_path, output_path).unwrap_or_bypass_with(|_bypass| {
#[cfg(target_os = "macos")]
let source_path = update_ptr_from_bypass(source_path, _bypass);
FN_REALPATH(source_path, output_path)
})
}

#[hook_guard_fn]
unsafe extern "C" fn realpath_darwin_extsn_detour(
source_path: *const c_char,
output_path: *mut c_char,
) -> *mut c_char {
realpath_logic(source_path, output_path)
.unwrap_or_bypass_with(|_| FN_REALPATH_DARWIN_EXTSN(source_path, output_path))
realpath_logic(source_path, output_path).unwrap_or_bypass_with(|_bypass| {
#[cfg(target_os = "macos")]
let source_path = update_ptr_from_bypass(source_path, _bypass);
FN_REALPATH_DARWIN_EXTSN(source_path, output_path)
})
}

unsafe fn readlink_logic(
source_path: *const c_char,
output_path: *mut c_char,
buf_size: size_t,
) -> Detour<ssize_t> {
let path = source_path.checked_into();

realpath(path).map(|res| {
let written_bytes = usize::min(buf_size, res.as_os_str().len());
output_path.copy_from_nonoverlapping(
res.as_os_str().as_encoded_bytes().as_ptr() as _,
written_bytes,
);
written_bytes as isize
})
}

/// When path is handled by us, just make it absolute and return, since resolving it remotely
/// doesn't really matter for our case atm (might be in the future)
#[hook_guard_fn]
unsafe extern "C" fn readlink_detour(
source_path: *const c_char,
output_path: *mut c_char,
buf_size: size_t,
) -> ssize_t {
readlink_logic(source_path, output_path, buf_size).unwrap_or_bypass_with(|_bypass| {
#[cfg(target_os = "macos")]
let source_path = update_ptr_from_bypass(source_path, _bypass);
FN_READLINK(source_path, output_path, buf_size)
})
}

fn vec_to_iovec(bytes: &[u8], iovecs: &[iovec]) {
Expand Down Expand Up @@ -1143,6 +1181,14 @@ pub(crate) unsafe fn enable_file_hooks(hook_manager: &mut HookManager) {
FN_REALPATH
);

replace!(
hook_manager,
"readlink",
readlink_detour,
FnReadlink,
FN_READLINK
);

replace!(
hook_manager,
"realpath$DARWIN_EXTSN",
Expand Down

0 comments on commit dc94168

Please sign in to comment.