use std::{
collections::VecDeque,
ffi::OsString,
fs::{self, File},
io,
os::unix::fs::MetadataExt,
path::{Component, Path, PathBuf},
};
pub(super) mod shebang;
const MAX_LINKS: usize = 40;
pub(super) fn trusted(path: &Path) -> io::Result<(PathBuf, File)> {
if !path.is_absolute() {
return Err(unprotected());
}
let root = Path::new("/");
#[cfg(target_os = "linux")]
let kernel = open_without_magic_links(path)?;
check(&super::open_verified_directory(root)?)?;
let mut prefix = root.to_owned();
let mut pending = components(path);
let mut links = 0;
while let Some(component) = pending.pop_front() {
match component.as_encoded_bytes() {
b"/" => {
prefix = root.to_owned();
}
b".." => {
prefix.pop();
}
_ => {
let next = prefix.join(&component);
let entry = fs::symlink_metadata(&next)?;
if entry.is_symlink() {
if !trusted_owner(entry.uid()) {
return Err(unprotected());
}
links += 1;
if links > MAX_LINKS {
return Err(unprotected());
}
for component in components(&fs::read_link(next)?).into_iter().rev() {
pending.push_front(component);
}
} else if pending.is_empty() {
let file = super::open_executable(&next)?;
check(&file)?;
#[cfg(target_os = "linux")]
if !super::same_file(&kernel, &next)? {
return Err(unprotected());
}
#[cfg(target_os = "macos")]
if !super::same_file(&file, &fs::canonicalize(path)?)? {
return Err(unprotected());
}
const EXECUTE_BITS: u32 = 0o111;
if file.metadata()?.mode() & EXECUTE_BITS == 0 {
return Err(unprotected());
}
return Ok((next, file));
} else {
check(&super::open_verified_directory(&next)?)?;
prefix = next;
}
}
}
}
Err(unprotected())
}
fn components(path: &Path) -> VecDeque<OsString> {
path.components()
.filter(|component| *component != Component::CurDir)
.map(|component| component.as_os_str().to_owned())
.collect()
}
pub(super) fn check(file: &File) -> io::Result<()> {
#[cfg(target_os = "linux")]
{
let filesystem = rustix::fs::fstatfs(file)?.f_type;
if filesystem == libc::FUSE_SUPER_MAGIC as rustix::fs::FsWord
|| filesystem == rustix::fs::PROC_SUPER_MAGIC
{
return Err(unprotected());
}
}
let metadata = file.metadata()?;
const SHARED_WRITE: u32 = 0o022;
if !trusted_owner(metadata.uid()) || metadata.mode() & SHARED_WRITE != 0 {
return Err(unprotected());
}
#[cfg(target_os = "macos")]
if is_descriptor_filesystem(file)? || !crate::platform::private::macos::has_no_grants(file)? {
return Err(unprotected());
}
Ok(())
}
fn trusted_owner(uid: u32) -> bool {
const ROOT_USER: u32 = 0;
uid == ROOT_USER || uid == rustix::process::geteuid().as_raw()
}
#[cfg(target_os = "macos")]
fn is_descriptor_filesystem(file: &File) -> io::Result<bool> {
use std::{ffi::CStr, mem::MaybeUninit, os::fd::AsRawFd};
let mut info = MaybeUninit::<libc::statfs>::uninit();
unsafe {
if libc::fstatfs(file.as_raw_fd(), info.as_mut_ptr()) != 0 {
return Err(io::Error::last_os_error());
}
Ok(matches!(
CStr::from_ptr(info.assume_init().f_fstypename.as_ptr()).to_bytes(),
b"devfs" | b"fdesc"
))
}
}
fn unprotected() -> io::Error {
io::Error::new(
io::ErrorKind::PermissionDenied,
"executable and its path must be protected from other users",
)
}
#[cfg(target_os = "linux")]
fn open_without_magic_links(path: &Path) -> io::Result<File> {
use rustix::fs::{CWD, Mode, OFlags, ResolveFlags, openat2};
let fd = openat2(
CWD,
path,
OFlags::PATH | OFlags::CLOEXEC,
Mode::empty(),
ResolveFlags::NO_MAGICLINKS,
)?;
Ok(fd.into())
}
#[cfg(target_os = "linux")]
pub(super) mod elf;
#[cfg(target_os = "macos")]
pub(super) mod macho;