Skip to content

fix(security): make the XLA artifact caches private to the user - #3289

Merged
inureyes merged 6 commits into
mainfrom
fix/issue-3248-private-xla-caches
Nov 23, 2026
Merged

inureyes merged 6 commits into
mainfrom
fix/issue-3248-private-xla-caches

Conversation

@inureyes

@inureyes inureyes commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Summary

Every OpenXLA/IREE compiled-artifact cache was a fixed name under the shared temp directory and was loaded with no ownership check, so another local user could pre-create it or plant a VMFB (native code on a CPU target) with a manifest that matches, since manifest fields are all derivable from public inputs. The caches now live under ${MLXCEL_CACHE_DIR:-$HOME/.cache/mlxcel}/xla/<cache>, and nothing is reused unless the cache directories, every directory above them, and the file are private to the current user.

  • mlxcel_core::private_dir (new, public): ensure_private_dir(root, subpath, repair_root, euid), verify_private_dir(path, euid), verify_trusted_ancestors, open_private_file(path, euid) -> Option<File>, seal_private_file, create_fresh_private_dir, current_euid, clear_group_write. Checks run on an O_NOFOLLOW | O_NONBLOCK descriptor (fstat): real directory or regular file, owner equals euid, no group or other write. A relative root, or one containing .., is refused. Group write on the managed chain is cleared with the PTX cache's make_private_dir (fchmod through a descriptor, dev/inode re-check), reused rather than copied; PTX behavior is unchanged.
  • Directories above the cache root: IREE opens a VMFB by path, so whoever can write a parent could rename the checked root away between the check and the load. The root's parent is resolved once and every directory up to / must be owned by the user or root and not other-writable (root-owned sticky directories such as /tmp are allowed), the PTX overlay's rule. The leaf path is returned through the resolved parent. Nothing above the root is modified.
  • Group-writable ancestors (mlxcel_core::private_group, new): under umask 002 with user private groups, ~/.cache is group-writable but only its owner is in the group. Such a directory is accepted only when all of these are proven, and otherwise refused exactly like the strict rule: Linux; its group is the process egid; it is not other-writable; every /etc/nsswitch.conf line for passwd, group, initgroups, passwd_compat and group_compat lists only files, systemd or compat (database name parsed as glibc's process_line does, colon optional, later lines override earlier ones); no other member is listed in /etc/group; no other account in /etc/passwd uses the gid as its primary group; no +/- compat or malformed line in either file. Files are read as plain text with a 4 MiB cap, once per process. A refused location's warning names the path and the fix (chmod g-w <path> or a private MLXCEL_CACHE_DIR).
  • mlxcel-xla cache_dir (new): xla_cache_dir(XlaCache) and artifact_slot(path, companions). Every production site goes through them: the text bundles and diagnostics in iree.rs, the ROCm tuning spec, Phi4MM and Gemma3n audio, the LLaVA, Qwen2/2.5/3-VL, Molmo, Molmo2 and Youtu-VL vision towers, the native GEMV HSACO cache (read from the checked descriptor), the aux smoke and the numeric probes. A refused location is left untouched, named in one stderr warning, and the artifact is compiled into a fresh mkdtemp directory. That fallback root is re-verified before and after each subdirectory is created and replaced if it was removed or swapped (for example by a temp cleaner followed by another user recreating the name).
  • Writes: VMFBs, HSACOs, MLIR inputs, the tuning spec and manifests are published 0600 by atomic rename. Before this, compile_one and the tuning spec wrote straight to the final name (a crash left a partial VMFB that the next load reused), and under umask 002 every file would have been group-writable.
  • Docs: cache location, the ancestor and group rules, and how to clear the cache in docs/installation.md (OpenXLA section) and the MLXCEL_CACHE_DIR row of docs/environment-variables.md; CHANGELOG Fixed entry. No new environment variable.

Known limitation: fallback directories are not removed at exit (documented). Left as is: #[cfg(test)] per-pid scratch paths, and emitter/gemma3n_qmv.rs::run_cuda_stablehlo_probe (behind the diagnostics feature on a CUDA IREE build, never a production load).

Changes during review

  • Review (9ab6dbf): added the ancestor check above; the first version checked only the root and its subdirectories.
  • Policy (85d1f43, 951faf4): the reviewer's first version accepted any group-writable ancestor whose group was the process egid, which is open on hosts with a shared primary group. It now requires the proven sole-member group described above, and the parser gaps found by the security review (a colon-less override line, initgroups, leading-whitespace compat lines) refuse.
  • Security review (42f96db): the mkdtemp fallback root was cached for the process and never re-checked.

Test plan

  • cargo test --profile test-fast -p mlxcel-xla --lib: 447 passed. cache_dir::tests cover another uid (mismatched euid), group/world-writable and explicit-root directories, a symlinked directory, a symlinked artifact, a group-writable artifact, a planted VMFB with a valid manifest (world-writable dir and other uid), owner reuse with one compile, and a fallback root replaced after creation. With the ownership, mode, symlink and parent checks stubbed out, 7 of the 11 fail.
  • cargo test --profile test-fast -p mlxcel-core --lib -- private_dir private_group ptx_cache_dirs: 28 passed, including other-writable, own-group and symlinked parents, a .. root, and the group-policy parser cases (sole member accepted; other member, other primary account, remote sources, override lines, initgroups, compat and malformed lines, missing files all refused).
  • Clippy -D warnings on mlxcel-xla (no features, iree, iree,micro-oracle,diagnostics) and mlxcel-core, lib and tests; cargo fmt --check; scripts/ci/check_used_by.py; scripts/ci/check_env_var_docs.py.
  • ROCm gfx1151, IREE hip (scripts/iree/setup-rocm.sh --env, MLXCEL_XLA_DEVICE=hip, under scripts/rocm_gpu_guard.sh --hold): xla_greedy_timing 3/3 on the final head with no refusal warning (this host: ~/.cache 0775 devops:devops, sole-member group, passwd: files systemd, group: files systemd); xla_decode_scalar_reuse 4/4 and xla_prepared_prefill 4/4 after 9ab6dbf. xla_gemma3n_prefill fails at line 367 ([20, 20, 20] vs [5, 5, 5]) identically on unmodified main (a9860ec); pre-existing.
  • Real-model greedy ids (MLXCEL_BACKEND=xla MLXCEL_PRINT_TOKEN_IDS=1 mlxcel generate --temp 0 -n 24): llama-3.2-1b-instruct-4bit, Qwen3-VL-2B-Instruct-4bit and Molmo-7B-D-0924-4bit (image, MLXCEL_XLA_VISION_BACKEND=iree) identical to main cold and warm at c362e29; llama-3.2-1b re-checked after 85d1f43 with a cleared cache, identical cold (11 s) and warm (4 s), no refusal.
  • GB10 / CUDA run of the same gates and greedy comparison: pending.

Closes #3248

Every OpenXLA/IREE compiled-artifact cache (text VMFB bundles, Phi4MM and Gemma3n audio, the LLaVA, Qwen2/2.5/3-VL, Molmo, Molmo2 and Youtu-VL vision towers, the ROCm tuning spec, the native GEMV HSACO objects, the aux smoke and numeric-probe modules) was a fixed name under the shared temp directory, loaded without any ownership check. Another local user could create the directory first or plant a VMFB plus a manifest that matches, since every manifest field is derivable from public inputs; a CPU-target VMFB is native code.

mlxcel-core gains a public private_dir module: ensure_private_dir, verify_private_dir, open_private_file, seal_private_file and create_fresh_private_dir, all checked on an O_NOFOLLOW descriptor with the effective uid as a parameter, reusing the PTX cache's group-write repair. mlxcel-xla routes every site through a new cache_dir module: caches live under ${MLXCEL_CACHE_DIR:-$HOME/.cache/mlxcel}/xla/<cache>, an artifact is reused only from a verified directory as a private regular file, and anything else is left untouched with one warning while the graph compiles into a fresh mkdtemp directory. VMFBs, HSACOs, the tuning spec, MLIR inputs and manifests are published 0600 by atomic rename.

Tests cover a directory owned by another uid, group- and world-writable directories, symlinked directories and artifacts, a planted VMFB with a valid manifest, and owner reuse. 8 of the 10 new mlxcel-xla tests fail with the checks removed.

Closes #3248
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:high High priority area:inference Generation, sampling, decoding (incl. speculative, DRY) labels Nov 23, 2026
The VMFB is opened by path inside the IREE runtime, so verifying only the cache root and the directories below it left a window: whoever can write a directory above the root (an other-writable shared directory, a foreign-owned parent, or a symlinked parent pointing at either) can rename the verified root away after the check and put their own in its place. The PTX cache overlay already checks every component up to `/` for this reason (#2539).

`ensure_private_dir` now refuses a root with `..`, resolves the root's parent once, checks it and every directory above it with the new `verify_trusted_ancestors` (owned by the user or root, not other-writable unless root-owned and sticky, group write only for the process's own group so a umask-002 `~/.cache` keeps working), and returns the leaf spelled through the canonical parent. The fallback `mkdtemp` root gets the same check on the canonical temp directory. Nothing above the root is modified.

New tests cover an other-writable parent (refused, left unmodified), an own-group-writable parent (accepted), a symlinked parent (canonical leaf), and a `..` root. mlxcel-xla lib tests 446 passed; mlxcel-core private_dir and ptx_cache_dirs 17 passed; clippy -D warnings clean.

Refs #3248
@inureyes

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

No production OpenXLA/IREE path loads a VMFB, manifest, tuning spec, or native GEMV object from a location not verified as private to the current user (#3248).

Findings Addressed

  • The cache root's ancestors were never checked, while the IREE runtime opens the VMFB by path. Whoever could write a directory above the root (an other-writable shared directory, a foreign-owned parent, or a symlinked parent pointing at one) could rename the verified root away after the check and substitute their own. ensure_private_dir now refuses .. in the root, resolves the root's parent once, checks it and every directory up to / with the PTX overlay's rule (owned by the user or root, not other-writable unless root-owned sticky; group write accepted only for the process's own group, so a umask-002 ~/.cache keeps working), and returns the leaf through the canonical parent. The mkdtemp fallback gets the same check on the canonical temp directory. Fixed in 9ab6dbf (HIGH)

Remaining Items

  • emitter/gemma3n_qmv.rs::run_cuda_stablehlo_probe still writes predictable per-pid MLIR/VMFB/input files directly under the temp directory and runs iree-run-module on them. It is gated on the diagnostics feature and a CUDA IREE build, not #[cfg(test)] as the PR body says, so it never reaches a production build. Left as is (LOW)
  • Each refused location leaves its fallback compile directory under the temp directory; documented in docs/installation.md (LOW)

Verification

  • All stated requirements implemented
  • No placeholder/mock code remaining
  • Integrated into project code flow
  • Project conventions followed
  • Existing modules reused where applicable
  • No unintended structural changes
  • Tests pass

Checked: every temp_dir() site left in mlxcel-xla is #[cfg(test)], the fallback, or the diagnostics probe above. The root crate loads no VMFB itself. PTX behavior is unchanged (ptx_cache_dirs.rs untouched). With the ownership, mode, symlink, and ancestor checks stubbed out, 7 of the 11 cache_dir tests fail; the developer's 8 of 10 also reverted the relative-root refusal and the 0600 atomic writes. The production_tests.rs fixture change to 0700 is legitimate: production creates its directories 0700, and under the host's umask 002 a create_dir_all fixture would be group-writable, refused, and redirected.

Tests after the fix (ROCm gfx1151): device gates xla_greedy_timing 3/3, xla_decode_scalar_reuse 4/4, xla_prepared_prefill 4/4 with IREE hip, reusing the existing ~/.cache/mlxcel/xla artifacts (this host's ~/.cache is 0775 under its own group and is accepted; no refusal printed); mlxcel-xla --lib 446 passed; mlxcel-core --lib -- private_dir ptx_cache_dirs 17 passed; clippy -D warnings clean on mlxcel-xla (no features; iree,micro-oracle,diagnostics) and mlxcel-core; cargo fmt --check and check_env_var_docs.py pass.

… private

Accepting a group-writable directory above the cache root whenever its group is the process egid trusts every member of that group, including members added by a remote name service. The relaxation now applies only on Linux and only when the group is the egid, the directory is not other-writable, /etc/nsswitch.conf resolves passwd and group from files, systemd or compat alone (a missing or unreadable file is refused rather than assuming the glibc default), every /etc/group line for that gid lists no member besides the current user, and no other /etc/passwd account has it as its primary gid. Anything else falls back to the strict PTX rule with a GroupWritable error whose message names the path and the fix (chmod g-w, or a private MLXCEL_CACHE_DIR).

The files are parsed as bounded plain text once per process (no getpwent) by the new mlxcel-core private_group module, whose parsers take contents, uid, gid and name so tests run without root. verify_trusted_ancestors_with takes the group decision as a parameter so ancestor tests no longer depend on the host's accounts. Docs and the CHANGELOG entry describe the rule.

Validated: mlxcel-core private_dir, private_group and ptx_cache_dirs 24 passed; mlxcel-xla lib 446 passed; clippy -D warnings clean (xla with no features and iree, core); fmt clean. On gfx1151, xla_greedy_timing 3/3 and llama-3.2-1b greedy ids identical to main from a cold and a warm cache, with ~/.cache at 0775 accepted and no refusal.

Refs #3248
The mkdtemp fallback root under the temp directory was created once per process and cached in a OnceLock, then every later fallback made a subdirectory in it by path without checking it again. A temp cleaner such as systemd-tmpfiles can delete that root under a long-running server; the name is listable in a shared /tmp, so another local user can then create a directory of the same name, and the subdirectories created inside it (where VMFBs are compiled and then loaded by path) can be renamed and replaced with the attacker's own between the compile and the load.

The root is now held in a Mutex and checked with verify_private_dir before and after each subdirectory is created. A root that is missing, foreign, writable by others, or a symlink is dropped and a fresh one is created with mkdtemp. A failed temp-directory check is no longer cached for the life of the process.

Validated with a new test that plants a writable directory and then a symlink at the removed root's name, plus the mlxcel-xla lib tests, clippy with and without the iree feature, and the ROCm xla_greedy_timing test.

Refs #3248
@inureyes

Copy link
Copy Markdown
Member Author

Security and performance review

Reviewed as a local multi-user attacker. The ancestor walk, the canonical-parent respelling of the leaf, O_NOFOLLOW plus fstat on every directory and artifact, the paired VMFB/manifest check, and the GEMV read from the checked descriptor hold up: no component on the returned path can be replaced by anyone but the user or root. Hard links need no nlink check, because only the user can create entries in a verified directory and a linked inode keeps its owner and 0600 mode.

Fixed

  • HIGH, 42f96db: the mkdtemp fallback root was cached in a OnceLock and never re-checked. If a temp cleaner deletes it under a long-running server, another user can recreate the listable name in /tmp and swap the per-load subdirectories between compile and load by path. The root is now re-verified before and after each subdirectory is created and replaced with a fresh mkdtemp when it is missing, foreign, writable, or a symlink.

Not fixed (below the auto-fix threshold)

  • MEDIUM: nsswitch_is_local skips a database line without a colon (glibc treats the colon as optional, and the last line for a database wins), and it does not check an initgroups: line, which can add supplementary members from a remote source. Both fail open only on root-written configuration.
  • MEDIUM: fallback directories are never removed, so every refused run leaves its compiled artifacts in /tmp (tmpfs on Debian 13).
  • LOW: +/- compat lines with leading whitespace are not caught by the prefix check; write_private_atomic and write_owner_only_new delete an existing file on an EEXIST open failure.

Performance

The ancestor walk runs once per cache resolution at model load, never per token; the group decision is computed once per process. The ROCm greedy test reused ~/.cache/mlxcel/xla/vmfb with no refusal warnings.

Validation

mlxcel-xla lib tests (447 passed), mlxcel-core private_dir/private_group/ptx_cache_dirs (24 passed), clippy -D warnings on mlxcel-xla with and without iree and on mlxcel-core, and the hip xla_greedy_timing ignored tests (3 passed).

The relaxed group-write rule for XLA cache ancestors read nsswitch.conf too loosely. glibc's process_line (nss/nss_database.c) takes the database name as the leading token ended by a colon or white space, so the colon is optional, and a later line for the same database overrides an earlier one. nsswitch_is_local required a colon and looked at each line independently, so a `passwd sss` line was ignored and `passwd: files` then `passwd sss` passed.

nsswitch_is_local now parses the name as glibc does and requires every line naming passwd, group or initgroups (and passwd_compat or group_compat, which compat reads) to list only local sources, skipping bracketed actions that may contain spaces. A checked line with an unparsable or empty source list, or an unterminated bracket, refuses. passwd and group must be present, initgroups is optional. The /etc/passwd and /etc/group parsers now trim leading white space before the +/- compat check, as glibc does.

Tests cover override lines, the optional colon (local accepted, remote and empty refused), initgroups with a remote source, bracketed actions, and leading-white-space compat lines in both files. The installation guide, the MLXCEL_CACHE_DIR row and the CHANGELOG entry describe the final rule and note that the fallback directory is not removed at exit.

Validation: cargo test -p mlxcel-core --lib private_dir private_group ptx_cache_dirs (28 passed), cargo test -p mlxcel-xla --lib (447 passed), clippy -D warnings on mlxcel-core and mlxcel-xla with and without iree, xla_greedy_timing on hip (3 passed, no refusal warning).

Refs #3248
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 23, 2026
@inureyes
inureyes merged commit 7a78467 into main Nov 23, 2026
33 checks passed
@inureyes
inureyes deleted the fix/issue-3248-private-xla-caches branch November 23, 2026 02:55
inureyes added a commit that referenced this pull request Nov 23, 2026
#3317)

Three `private_dir` tests from #3289 fail on macOS: `creates_the_chain_owner_only_and_accepts_it`, `the_managed_chain_loses_group_write_but_an_explicit_root_does_not`, and `a_directory_above_the_root_that_others_can_write_is_refused`.

The temp dir on macOS is under `/var`, a symlink to `/private/var`. `ensure_private_dir` respells the chain through the canonical parent of the root, and `verify_trusted_ancestors` documents that its input is already canonical, so the tests compared `/private/var/...` results against `/var/...` expectations and walked a symlinked ancestor. The production code is correct; only the test inputs were non-canonical, which Linux CI never exposes.

The tests now build their paths from a `canonical_tempdir()` helper.

Validation: `cargo test -p mlxcel-core --lib private_dir` passes 11/11 on macOS (M3 Ultra); `cargo fmt --check` clean. Test-only change.
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) priority:high High priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(security): make the XLA artifact caches private to the user

1 participant