Repository navigation
fix(security): make the XLA artifact caches private to the user - #3289
Conversation
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
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
Implementation Review SummaryIntent
Findings Addressed
Remaining Items
Verification
Checked: every Tests after the fix (ROCm gfx1151): device gates |
… 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
Security and performance reviewReviewed 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
Not fixed (below the auto-fix threshold)
PerformanceThe 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 Validationmlxcel-xla lib tests (447 passed), mlxcel-core |
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
#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.
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 anO_NOFOLLOW | O_NONBLOCKdescriptor (fstat): real directory or regular file, owner equalseuid, 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'smake_private_dir(fchmod through a descriptor, dev/inode re-check), reused rather than copied; PTX behavior is unchanged./must be owned by the user or root and not other-writable (root-owned sticky directories such as/tmpare allowed), the PTX overlay's rule. The leaf path is returned through the resolved parent. Nothing above the root is modified.mlxcel_core::private_group, new): under umask 002 with user private groups,~/.cacheis 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.confline forpasswd,group,initgroups,passwd_compatandgroup_compatlists onlyfiles,systemdorcompat(database name parsed as glibc'sprocess_linedoes, colon optional, later lines override earlier ones); no other member is listed in/etc/group; no other account in/etc/passwduses 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 privateMLXCEL_CACHE_DIR).mlxcel-xlacache_dir(new):xla_cache_dir(XlaCache)andartifact_slot(path, companions). Every production site goes through them: the text bundles and diagnostics iniree.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 freshmkdtempdirectory. 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).0600by atomic rename. Before this,compile_oneand 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/installation.md(OpenXLA section) and theMLXCEL_CACHE_DIRrow ofdocs/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, andemitter/gemma3n_qmv.rs::run_cuda_stablehlo_probe(behind thediagnosticsfeature on a CUDA IREE build, never a production load).Changes during review
initgroups, leading-whitespace compat lines) refuse.mkdtempfallback 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::testscover 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).-D warningson 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.hip(scripts/iree/setup-rocm.sh --env,MLXCEL_XLA_DEVICE=hip, underscripts/rocm_gpu_guard.sh --hold):xla_greedy_timing3/3 on the final head with no refusal warning (this host:~/.cache0775devops:devops, sole-member group,passwd: files systemd,group: files systemd);xla_decode_scalar_reuse4/4 andxla_prepared_prefill4/4 after 9ab6dbf.xla_gemma3n_prefillfails at line 367 ([20, 20, 20]vs[5, 5, 5]) identically on unmodified main (a9860ec); pre-existing.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.Closes #3248