diff --git a/src/ld_so/linker.rs b/src/ld_so/linker.rs index b70a46faed539f7be4f27b02933bc44960e6a547..149f26543f816fe887439b2b5c30060ee320c5fa 100644 --- a/src/ld_so/linker.rs +++ b/src/ld_so/linker.rs @@ -26,6 +26,7 @@ use crate::{ }; use super::{ + access, debug::{RTLDDebug, RTLDState, _dl_debug_state, _r_debug}, tcb::{Master, Tcb}, PAGE_SIZE, @@ -163,7 +164,20 @@ impl Linker { if self.verbose { println!("check {}", path); } - return Ok(Some(self.load_recursive(name, &path)?)); + let access = unsafe { + let path_c = CString::new(path.as_bytes()).map_err(|err| { + Error::Malformed(format!("invalid path '{}': {}", path, err)) + })?; + + // TODO: Use R_OK | X_OK + // We cannot use unix stdlib because errno is thead local variable + // and fs:[0] is not set yet. + access(path_c.as_ptr(), unistd::F_OK) == 0 + }; + + if access { + return Ok(Some(self.load_recursive(name, &path)?)); + } } Err(Error::Malformed(format!("failed to locate '{}'", name))) diff --git a/src/ld_so/mod.rs b/src/ld_so/mod.rs index fbb62f290aa68270815e2552b58770c4dfe442cf..b39a5e7e269e2c8ffb937ffb113046b591d881e6 100644 --- a/src/ld_so/mod.rs +++ b/src/ld_so/mod.rs @@ -79,6 +79,12 @@ pub fn static_init(sp: &'static Stack) { } } +// Wrapper over the systemcall, Do not use outside of ld_so +pub unsafe fn access(path: *const c_char, mode: c_int) -> c_int { + let path = CStr::from_ptr(path); + syscall!(ACCESS, (path).as_ptr(), mode) as c_int +} + #[cfg(target_os = "linux")] pub unsafe fn init(sp: &'static Stack) { let mut tp = 0usize;