Menu: the activation-context manifest is written to a fixed name in %TEMP%, follows links, and is never removed #2272

Open
opened 2026-11-26 05:25:09 +00:00 by github-bot · 2 comments
Owner

@⁠itsonewi on GitHub · 2026-11-25 11:57 UTC · original

Summary

crates/xcl/src/main.rs (1a0bbe3), Ui::enter():

let path = std::env::temp_dir().join("xcl-menu.manifest");
let actctx = std::fs::write(&path, MANIFEST).ok().and_then(|()| unsafe {
    let source = wide(&path.to_string_lossy());
    ...
    let h = CreateActCtxW(&c);

The manifest is the Common-Controls 6 dependency that makes the task dialog look right; the file is a means to an end. Four things about how it is made:

  1. The name is fixed and the directory is not guaranteed private. %TEMP% is %LOCALAPPDATA%\Temp for a normal user, but it is C:\Windows\Temp for a service, it can be redirected or roamed by policy, and a shared or lab machine can have it writable by more than one account.
  2. std::fs::write is create(true).truncate(true) and follows reparse points. If xcl-menu.manifest already exists as a symlink, junction or hard link, the write lands on its target and truncates it first. Two xcl.exe windows opening the menu at once also write the same file.
  3. Whatever is in it is then activated by CreateActCtxW, in the process that is about to run the game. An activation context can list <file name="..."> entries and dependency assemblies, which is a way to name binaries to load.
  4. It is never deleted. The file stays in %TEMP% after the menu closes, and if the write fails (std::fs::write(...).ok() swallows the error) an older file left by an earlier run, or by someone else, is what gets activated - with no message.

Suggested fix

There is already a precedent in the tree: crates/xcl/src/rpf.rs creates its temporary file with create_new(true), in a loop, and owns its removal through TempPath:

match std::fs::OpenOptions::new().write(true).create_new(true).open(&candidate) { ... }

Doing the same here fixes 2 and 4 together: create_new refuses at once when the name exists or is a link, so the XML is never written into someone else's file, and a guard struct that deletes the path on Drop (or FILE_FLAG_DELETE_ON_CLOSE through OpenOptionsExt::custom_flags) leaves nothing behind. A unique name (pid + counter, as rpf.rs does with TEMP_ID) also removes the two-windows race.

If the file is dropped for good instead, CreateActCtxW also takes lpResourceName from GetModuleHandle(NULL) - but the comment in Ui::enter() says a manifest inside xcl.exe would apply to the game as well, so that is not an option here; the fix is the file, not the location.

Not tested

  • Nothing run: read from 1a0bbe3 without a checkout. The menu is only reached by double-clicking xcl.exe with no arguments (started_by_double_click()), so this is a hardening report, not a reproduced failure.

AI use

  • Claude (Opus 5.5) read crates/xcl/src/main.rs and crates/xcl/src/rpf.rs on 1a0bbe3 and wrote this report; the human account checked both files before opening it.
**@⁠itsonewi** on GitHub · 2026-11-25 11:57 UTC · [original](https://github.com/xcl-dev/xcl/issues/2272) ### Summary `crates/xcl/src/main.rs` (`1a0bbe3`), `Ui::enter()`: ```rust let path = std::env::temp_dir().join("xcl-menu.manifest"); let actctx = std::fs::write(&path, MANIFEST).ok().and_then(|()| unsafe { let source = wide(&path.to_string_lossy()); ... let h = CreateActCtxW(&c); ``` The manifest is the Common-Controls 6 dependency that makes the task dialog look right; the file is a means to an end. Four things about how it is made: 1. **The name is fixed and the directory is not guaranteed private.** `%TEMP%` is `%LOCALAPPDATA%\Temp` for a normal user, but it is `C:\Windows\Temp` for a service, it can be redirected or roamed by policy, and a shared or lab machine can have it writable by more than one account. 2. **`std::fs::write` is `create(true).truncate(true)` and follows reparse points.** If `xcl-menu.manifest` already exists as a symlink, junction or hard link, the write lands on its target and truncates it first. Two `xcl.exe` windows opening the menu at once also write the same file. 3. **Whatever is in it is then activated** by `CreateActCtxW`, in the process that is about to run the game. An activation context can list `<file name="...">` entries and dependency assemblies, which is a way to name binaries to load. 4. **It is never deleted.** The file stays in `%TEMP%` after the menu closes, and if the write fails (`std::fs::write(...).ok()` swallows the error) an older file left by an earlier run, or by someone else, is what gets activated - with no message. ### Suggested fix There is already a precedent in the tree: `crates/xcl/src/rpf.rs` creates its temporary file with `create_new(true)`, in a loop, and owns its removal through `TempPath`: ```rust match std::fs::OpenOptions::new().write(true).create_new(true).open(&candidate) { ... } ``` Doing the same here fixes 2 and 4 together: `create_new` refuses at once when the name exists or is a link, so the XML is never written into someone else's file, and a guard struct that deletes the path on `Drop` (or `FILE_FLAG_DELETE_ON_CLOSE` through `OpenOptionsExt::custom_flags`) leaves nothing behind. A unique name (pid + counter, as `rpf.rs` does with `TEMP_ID`) also removes the two-windows race. If the file is dropped for good instead, `CreateActCtxW` also takes `lpResourceName` from `GetModuleHandle(NULL)` - but the comment in `Ui::enter()` says a manifest inside `xcl.exe` would apply to the game as well, so that is not an option here; the fix is the file, not the location. **Not tested** - Nothing run: read from `1a0bbe3` without a checkout. The menu is only reached by double-clicking `xcl.exe` with no arguments (`started_by_double_click()`), so this is a hardening report, not a reproduced failure. **AI use** - Claude (Opus 5.5) read `crates/xcl/src/main.rs` and `crates/xcl/src/rpf.rs` on `1a0bbe3` and wrote this report; the human account checked both files before opening it.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:25:09 +00:00
Author
Owner

@⁠Decodere on GitHub · 2026-11-25 12:09 UTC · original

Taking #2272 in crates/xcl/src/main.rs (Ui::enter / Ui::drop).

Plan:

  • Replace the fixed %TEMP%\xcl-menu.manifest std::fs::write with an RAII TempManifest created via OpenOptions::new().write(true).create_new(true) using a unique xcl-menu-<pid>-<seq>.manifest name (retrying on AlreadyExists, following rpf.rs's TempPath pattern), cleaning up on write error and removing the file on Drop as soon as CreateActCtxW completes.
  • Release the ACTCTX handle (ReleaseActCtx(h)) if ActivateActCtx fails, and guard CoUninitialize() on CoInitializeEx(...) >= 0.
  • Verify with failing-first / post-fix unit tests (collision/non-truncation/cleanup + live Windows CreateActCtxW / ActivateActCtx activation and teardown) plus workspace build/tests and pr_check.py.

AI use: drafted with Google Antigravity (Gemini 4 Neon); implementation and Windows tests will run locally.

**@⁠Decodere** on GitHub · 2026-11-25 12:09 UTC · [original](https://github.com/xcl-dev/xcl/issues/2272#issuecomment-7161985815) Taking **#2272** in `crates/xcl/src/main.rs` (`Ui::enter` / `Ui::drop`). **Plan:** - Replace the fixed `%TEMP%\xcl-menu.manifest` `std::fs::write` with an RAII `TempManifest` created via `OpenOptions::new().write(true).create_new(true)` using a unique `xcl-menu-<pid>-<seq>.manifest` name (retrying on `AlreadyExists`, following `rpf.rs`'s `TempPath` pattern), cleaning up on write error and removing the file on `Drop` as soon as `CreateActCtxW` completes. - Release the `ACTCTX` handle (`ReleaseActCtx(h)`) if `ActivateActCtx` fails, and guard `CoUninitialize()` on `CoInitializeEx(...) >= 0`. - Verify with failing-first / post-fix unit tests (collision/non-truncation/cleanup + live Windows `CreateActCtxW` / `ActivateActCtx` activation and teardown) plus workspace build/tests and `pr_check.py`. _AI use: drafted with Google Antigravity (Gemini 4 Neon); implementation and Windows tests will run locally._
Author
Owner

@⁠itsonewi on GitHub · 2026-11-25 15:55 UTC · original

The plan matches the issue, and the extras are welcome: ReleaseActCtx on a failed ActivateActCtx and the CoUninitialize guard are two leaks the report did not name, so please keep them in.

Two things from the source that may save a round trip:

  • Deleting the manifest as soon as CreateActCtxW returns is safe: the activation context is parsed from the file inside that call, so the file is not needed afterwards. That is what makes the RAII-with-Drop shape correct here rather than a keep-it-until-exit workaround.
  • The name has to be unique across processes, not just within one. pid + sequence is enough, but keep the AlreadyExists retry bounded and return the error after a few tries instead of looping, since %TEMP% can be a redirected folder with odd ACLs.
  • The "collision" case is worth keeping separate from "non-truncation": a fixed-name manifest can also be left behind by an earlier run of XCL, which is a different starting state than two live processes.

Cheapest check that the manifest is really parsed: an empty or truncated manifest makes CreateActCtxW fail with ERROR_SXS_CANT_GEN_ACTCTX, so a passing CreateActCtxW on the written bytes is already the assertion.

Measured on the source only (no build here).


AI use: read main.rs and rpf.rs on main through the GitHub API to check the plan against the existing TempPath precedent; no code was written by an agent here and no build was run.

**@⁠itsonewi** on GitHub · 2026-11-25 15:55 UTC · [original](https://github.com/xcl-dev/xcl/issues/2272#issuecomment-7167435614) The plan matches the issue, and the extras are welcome: `ReleaseActCtx` on a failed `ActivateActCtx` and the `CoUninitialize` guard are two leaks the report did not name, so please keep them in. Two things from the source that may save a round trip: - Deleting the manifest as soon as `CreateActCtxW` returns is safe: the activation context is parsed from the file inside that call, so the file is not needed afterwards. That is what makes the RAII-with-Drop shape correct here rather than a keep-it-until-exit workaround. - The name has to be unique across processes, not just within one. `pid` + sequence is enough, but keep the `AlreadyExists` retry bounded and return the error after a few tries instead of looping, since `%TEMP%` can be a redirected folder with odd ACLs. - The "collision" case is worth keeping separate from "non-truncation": a fixed-name manifest can also be left behind by an earlier run of XCL, which is a different starting state than two live processes. Cheapest check that the manifest is really parsed: an empty or truncated manifest makes `CreateActCtxW` fail with `ERROR_SXS_CANT_GEN_ACTCTX`, so a passing `CreateActCtxW` on the written bytes is already the assertion. Measured on the source only (no build here). --- **AI use:** read `main.rs` and `rpf.rs` on `main` through the GitHub API to check the plan against the existing `TempPath` precedent; no code was written by an agent here and no build was run.
This discussion has been locked. Commenting is limited to contributors.
No description provided.