Restrict title DLL loading to simple file names

Reject parent, subdirectory, absolute, and prefixed import paths before touching the filesystem. Add focused unit coverage.

(cherry picked from commit 15c4d9a6955b63e14abd59a1e69fd19e5ecce512)
This commit is contained in:
Gauss999
2026-11-28 18:28:54 -03:00
committed by DoubleGhosty
parent 8abcf20bc9
commit 9178779136

View File

@ -3,7 +3,7 @@
use std::collections::HashSet;
use std::ffi::{CString, c_void};
use std::os::windows::ffi::OsStrExt;
use std::path::{Path, PathBuf};
use std::path::{Component, Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::AtomicU64;
@ -147,8 +147,15 @@ fn map_dll(
loaded: &mut Vec<(String, LoadedImage)>,
) -> Result<()> {
let name = name.to_ascii_lowercase();
// Preserve host/HLE filtering: only names selected as title DLLs are paths.
if !seen.insert(name.clone()) || !wanted(&name) {
return Ok(());
}
if !is_simple_title_dll_name(&name) {
return Ok(());
}
let path = dirs.0.join(&name);
if !seen.insert(name.clone()) || !wanted(&name) || !path.is_file() {
if !path.is_file() {
return Ok(());
}
let original = std::fs::read(&path).with_context(|| format!("reading {}", path.display()))?;
@ -159,6 +166,15 @@ fn map_dll(
Ok(())
}
/// Returns whether a title DLL name is exactly one normal path component.
///
/// This is deliberately lexical. Containment through symlinks/reparse points
/// would require separate canonicalization as future defense in depth.
fn is_simple_title_dll_name(name: &str) -> bool {
let mut components = Path::new(name).components();
matches!(components.next(), Some(Component::Normal(_))) && components.next().is_none()
}
/// Resolves through `inner`, then through the exports of the title's DLLs.
pub struct WithTitleDlls<'a> {
pub inner: &'a dyn ImportResolver,
@ -425,3 +441,40 @@ impl LoadedImage {
unsafe { FreeLibrary(self.module) };
}
}
#[cfg(test)]
mod tests {
use super::is_simple_title_dll_name;
#[test]
fn accepts_a_normal_title_dll_name() {
assert!(is_simple_title_dll_name("title.dll"));
}
#[test]
fn rejects_parent_components_in_title_dll_names() {
for name in ["../outside.dll", r"..\outside.dll"] {
assert!(!is_simple_title_dll_name(name), "{name:?}");
}
}
#[test]
fn rejects_subdirectories_in_title_dll_names() {
for name in ["subdir/title.dll", r"subdir\title.dll"] {
assert!(!is_simple_title_dll_name(name), "{name:?}");
}
}
#[test]
fn rejects_absolute_and_prefixed_title_dll_names() {
for name in [
r"\outside.dll",
r"C:\outside.dll",
"C:/outside.dll",
r"\\server\share\outside.dll",
r"\\?\C:\outside.dll",
] {
assert!(!is_simple_title_dll_name(name), "{name:?}");
}
}
}