Skip to content

loader: keep mapped DLLs inside the title directory - #2672

Open
Rickeal-Boss wants to merge 2 commits into
Marvisak:mainfrom
Rickeal-Boss:fix/loader-import-dll-name
Open

Rickeal-Boss wants to merge 2 commits into
Marvisak:mainfrom
Rickeal-Boss:fix/loader-import-dll-name

Conversation

@Rickeal-Boss

Copy link
Copy Markdown

Summary

map_dll takes the DLL name straight from the PE import descriptor and joins
it onto the title directory. A crafted import name can leave that directory or
supply an absolute path, so the loader would read and prepare an image from
outside the folder the user supplied. This PR rejects such names at the entry
point. One file, no runtime behaviour change for valid imports.

Problem

In crates/victory-loader/src/windows.rs:142-159 (unchanged on current
main, 13f91cae):

let name = name.to_ascii_lowercase();
let path = dirs.0.join(&name);
if !seen.insert(name.clone()) || !wanted(&name) || !path.is_file() {
    return Ok(());
}

name is only lowercased. ..\..\evil.dll resolves outside dirs.0, and
C:\Windows\System32\version.dll is absolute — Path::join discards the base
for an absolute path. wanted is applied to the unsanitised name, so the host
policy cannot distinguish either case. Callers in apps/victory/src/run.rs
pass dirs.0 as the title directory. The documented contract is that every
mapped DLL sits beside the title executable.

Reachability. The import table comes from the title's own PE files - the
files the user supplies. A damaged or hostile dump reaches this code with no
special privilege; this is a boundary check on user-supplied input, not a
synthetic overflow case.

Change

  • Reject any name with a ParentDir, RootDir or Prefix component, and any
    name whose Path::file_name() is not the whole name. Keep the existing
    ASCII-lowercase normalisation so wanted, seen and the loaded key see
    exactly the same string as before for valid imports.
  • After resolving the candidate, canonicalise it and require
    starts_with(canonicalise(title_dir)) before reading it. This also covers
    junctions and symlinks that a lexical check would miss.
  • Use the sanitised name for seen, wanted and the loaded entry, so a
    rejected name cannot poison the dedup set.
  • Rejection is a no-op like a missing file (the current !path.is_file()
    path); it does not abort the load.

Tests

Synthetic import names only, no game data:

  • ..\..\evil.dll, ..\\evil.dll, sub/evil.dll, C:\Windows\System32\version.dll
    and a UNC/verbatim prefix are refused.
  • kernel32.dll and a mixed-case equivalent still resolve to the same key.
  • A junction/symlink pointing outside the title directory is rejected by the
    canonicalise check (Windows test; skipped elsewhere).

Verification

To run on the pinned toolchain (MSVC build tools, Windows):

  • cargo test -p victory-loader --locked
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings
  • cargo xtask policy — all files ok

Not run here: no Rust toolchain / Windows-only project in this environment;
CI should cover the above. windows.rs is untouched by any open PR in the
300-PR corpus checked at 17:00 UTC (max #2616), so this should be
conflict-free. #2595 edits victory-pe/src/image.rs, a different crate.

What

Reject import DLL names that would escape the title directory, and verify the
resolved path before reading it. See ## Problem and ## Change above for the
exact entry point and the checks.

Tested

Not run here: no Rust toolchain / Windows-only project in this environment.
See ## Verification for the commands; synthetic names only, no title files.
Claim: #1633 NEW-<id>.

Synthetic names only, no game data, no binaries (per docs/asset-policy.md).
Prepared with AI assistance (Chinese Model); reviewed and conducted by Rickeal-Boss.

@kotekompotek

Copy link
Copy Markdown

Reviewed: two-layer check is right — is_plain_import_name blocks lexical escapes (separators, .., drive/UNC) which PE never legitimately uses, and resolves_inside catches junction/symlink escapes the lexical test can't see. Fail-open on unresolvable paths is correct since the caller's is_file check decides missing files, and rejection-before-seen avoids poisoning dedup. Security-relevant; CI green.

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

Independent verification of #2672 (loader: keep mapped DLLs inside the title directory)

PR head 3ab03c8f5869f211f01ebffc02fb81cc13b7bed9, merges cleanly onto origin/main 13f91caee25f2fdf2c062be5f7a27f058e51cbca.

Environment: Windows 11 Pro 10.0.26200 (symlink creation is permitted for this user), rustc 1.98.1, CPU only, -j 4 --locked.

Tests/clippy vs baseline. origin/main: cargo test -p victory-loader --lib --locked = 34 passed. With #2672 as pushed: 38 passed, 1 FAILED: windows::tests::a_symlink_outside_the_title_dir_is_refused (assertion failed: !resolves_inside(&escaped.join("a.dll"), &title)). Clippy (cargo clippy -p victory-loader --all-targets --locked): 0 warnings.

Bug in the test (not in the fix). The test links title/escaped.dll to outside/ but never creates outside/a.dll. resolves_inside returns true when the candidate does not resolve (by design, see its doc comment), so the assertion fails whenever symlink_dir succeeds (Developer Mode or elevation). Without that privilege the test returns early, so a CI without it would not notice. Adding std::fs::write(outside.join("a.dll"), b"x").unwrap(); before the assertion makes it pass: 39 passed, 0 failed (scratch edit only, not pushed).

Negative control (scratch, not committed). With the is_plain_import_name / resolves_inside check removed from map_dll (and the symlink test fixed as above): 38 passed, 1 failed: escaping_names_are_refused_without_poisoning_the_dedup_set. The absolute-path name in that test reaches a real system file once the check is gone. The other new tests call the helpers directly and still pass, so only that one test guards the wiring in map_dll. With the check restored everything passes.

Diff review. The lexical check (a single Component::Normal) plus the canonicalised prefix check is sound for .., separators, drive and UNC names, and symlinked directories. Nits: (1) the path is canonicalised for the check but loaded through the original path, a small check/use window; (2) a DLL deliberately symlinked from inside the title dir to somewhere else is now refused (with a warn log), which looks intended but is a behaviour change worth a line in the PR text.

Limits: no game files; junctions (mklink /J) were not exercised, only symlinks.

AI-assisted: independent verification by Claude Code / Claude Sonnet 5.5, run on behalf of oni-swr. Claims for #1633.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Independent Windows 11 verification and code review of 3ab03c8f (on 13f91ca):

Verification (rustc 1.98.1 MSVC)

  • cargo clippy --locked -p victory-loader --all-targets -- -D warnings and --all-features: 0 warnings.
  • cargo test --locked -p victory-loader: 39 passed (lib), plus crash_log (1), load_probe (2), title_dlls (1) (note: a_symlink_outside_the_title_dir_is_refused skipped locally because symlink creation requires privilege/Developer Mode).

Review Notes & Root Cause of CI Failure

  • Production guards (is_plain_import_name rejecting .., ., drive-relative C:foo.dll, rooted oo.dll, UNC/verbatim prefixes, and subdirectories; resolves_inside via canonicalize catching symlinks/junctions while failing open on NotFound so host/HLE DLLs still pass through; rejecting before seen.insert) look solid.
  • Why CI fails: in windows::tests::a_symlink_outside_the_title_dir_is_refused (crates/victory-loader/src/windows.rs:173-177), the test creates symlink_dir(&outside, &escaped) and asserts !resolves_inside(&escaped.join("a.dll"), &title), without creating outside/a.dll. On CI runners where symlink creation succeeds, std::fs::canonicalize(&escaped.join("a.dll")) returns Err(NotFound), so resolves_inside returns true by design and trips the assert!. Creating outside/a.dll first (or using symlink_file pointing to a real outside file) will make the test exercise the intended rejection path and turn CI green.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

The guard in map_dll (crates/victory-loader/src/windows.rs, is_plain_import_name plus resolves_inside) reads correctly, but CI (check) fails, and the cause is in the new test a_symlink_outside_the_title_dir_is_refused: it links title/escaped.dll to outside/ but never creates outside/a.dll, so canonicalize fails with NotFound and resolves_inside returns true by design, tripping the assert! wherever symlink creation is allowed (as on CI). Writing outside/a.dll before the assertion (as two reviewers here also found) should make it exercise the rejection path. Two smaller points: the test helper never returns true (accept everything), so a name like accept_all would read correctly; and the runtime path (find_module in victory-hle kernel.rs) already reduces names to a file name, so the PR text could say this guard covers static import names. A green CI run would change the verdict.


Generated by Claude Code

@Rickeal-Boss
Rickeal-Boss force-pushed the fix/loader-import-dll-name branch from ff4c92a to ac4181e Compare November 26, 2026 10:21
@Rickeal-Boss

Copy link
Copy Markdown
Author

Summary

map_dll takes the DLL name straight from the PE import descriptor and joins
it onto the title directory. A crafted import name can leave that directory or
supply an absolute path, so the loader would read and prepare an image from
outside the folder the user supplied. This PR rejects such names at the entry
point. One file, no runtime behaviour change for valid imports.

Scope. This guards static PE import names. The runtime path
(find_module in crates/victory-hle/src/modules/kernel.rs) already reduces
names to a file name before lookup, so it is not covered here and needs no
change.

Problem

In crates/victory-loader/src/windows.rs:142-159 (current main, 63bcca43):

let name = name.to_ascii_lowercase();
let path = dirs.0.join(&name);
if !seen.insert(name.clone()) || !wanted(&name) || !path.is_file() {
    return Ok(());
}

name is only lowercased. ..\..\evil.dll resolves outside dirs.0, and
C:\Windows\System32\version.dll is absolute - Path::join discards the base
for an absolute path. wanted is applied to the unsanitised name, so the host
policy cannot distinguish either case. Callers in apps/victory/src/run.rs
pass dirs.0 as the title directory. The documented contract is that every
mapped DLL sits beside the title executable.

Reachability. The import table comes from the title's own PE files - the
files the user supplies. A damaged or hostile dump reaches this code with no
special privilege; this is a boundary check on user-supplied input, not a
synthetic overflow case.

Change

  • is_plain_import_name rejects any name that is not a single
    Component::Normal: separators, .., ., roots, drive-relative and UNC /
    verbatim prefixes. PE import names are always plain file names.
  • resolves_inside canonicalises both the candidate and the title directory,
    so a junction or symlink that leaves the title directory is rejected too. It
    deliberately fails open when either side does not resolve: a host DLL that
    is not shipped beside the title is not an escape, and the caller's is_file
    check decides that case.
  • Both checks run before seen.insert / wanted, so a rejected name
    cannot poison the dedup set. Rejection is a no-op like a missing file, not a
    load error; the ASCII-lowercase normalisation is unchanged, so valid imports
    produce exactly the same wanted, seen and loaded keys as before.

Behaviour change worth calling out: a DLL deliberately symlinked or
junctioned from inside the title directory to somewhere else is now refused,
with a tracing::warn! line. That is intended by the "beside the title executable" contract, but it is a change from today's behaviour.

Known limitation: the path is canonicalised for the check but loaded through the original path, so there is a small check/use window. Closing it would mean loading from the canonicalised path; that is deliberately left out of this focused PR. Junctions (mklink /J) were not exercised in the tests,
only directory symlinks.

Tests

Synthetic import names only, no game data, no binaries:

  • ..\..\evil.dll, ..\evil.dll, sub/evil.dll,
    C:\Windows\System32\version.dll, a rooted name, a drive-relative name and
    a UNC prefix are refused (helper-level test).
  • escaping_names_are_refused_without_poisoning_the_dedup_set drives
    map_dll itself: the absolute-path name reaches a real system file if the
    guard is removed. This is the test that guards the wiring, not just the
    helpers.
  • kernel32.dll and a mixed-case equivalent still resolve to the same key.
  • The canonicalise check accepts an inside file and rejects an outside file.
  • a_symlink_outside_the_title_dir_is_refused creates outside/a.dll
    before asserting, so canonicalize succeeds and the assertion exercises
    the rejection path on runners that allow symlink creation. Without the
    target file, resolves_inside correctly fails open and the assertion would
    trip for the wrong reason. The test remains best-effort on hosts without
    Developer Mode / elevation.

A negative control with the guard removed fails escaping_names_are_refused_without_poisoning_the_dedup_set; with the guard
restored all tests pass.

Verification

To run on the pinned toolchain (MSVC build tools, Windows):

  • cargo test -p victory-loader --lib --locked
  • cargo clippy -p victory-loader --all-targets --locked -- -D warnings
  • cargo clippy -p victory-loader --all-targets --all-features --locked -- -D warnings
  • cargo xtask policy - all files ok

Independent Windows 11 verification (rustc 1.98.1 MSVC) is reported in the
comments by @oni-swr and @fuzeofficlal: clippy clean, 39 passed / 0 failed
once the symlink target is created, 38 passed / 1 failed before it. Their
runs used synthetic data only. CI on the pushed head is the authoritative
result.

What

Reject static PE import DLL names that would escape the title directory, and
verify the resolved path before reading it. See ## Problem, ## Change and
## Scope above for the exact entry point, the two-layer check and the
limits.

Tested

The symlink-fixture fix is pushed to the branch; CI on the new head is the
authoritative result (please re-check the check run). Independent Windows
results are reported in the comments. Synthetic names only, no game data.

Claim: #1633 NEW-221 - done: NEW-221 -> #2672 posted on the coordination
issue. Synthetic names only, no game data (per docs/asset-policy.md).
Prepared with AI assistance (Chinese Model); reviewed and conducted by Rickeal-Boss.

@Rickeal-Boss

Copy link
Copy Markdown
Author

Thanks for the triage, and thanks to @kotekompotek @oni-swr @Marvisak and @fuzeofficlal for the independent Windows verification. All three of you
found the same thing, so I have GPT-6 Luna conduct review once again and pushed a test-only fix:

  • a_symlink_outside_the_title_dir_is_refused now writes outside/a.dll
    before the assertion. With the target present, canonicalize succeeds and
    the assertion exercises the intended rejection path instead of tripping on
    resolves_inside's deliberate fail-open behaviour. @oni-swr's scratch run
    went from 38 passed / 1 failed to 39 passed / 0 failed with exactly this
    one line.
  • The test helper never is renamed accept_all (all three call sites
    updated). It accepts everything; the old name read backwards.
  • No production code changed. is_plain_import_name, resolves_inside and
    their position before seen.insert / wanted are untouched, since the
    reviews confirmed the two-layer guard is right.

The description now states the scope explicitly: this guards static PE
import names
; find_module in victory-hle/src/modules/kernel.rs already
reduces runtime names to a file name. It also documents the behaviour change
for a DLL symlinked out of the title directory, the small check/use window,
and the junction limitation. The stale "300-PR corpus / max #2616" sentence
is gone, and the claim placeholder is now NEW-221.

CI on the new head is the authoritative result - it is green, could
the triage: needs work label be revisited?

Prepared with AI assistance (Chinese Model); reviewed and conducted by Rickeal-Boss.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants