Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: grant MXC Tier 3 read access to runtime folders once, not per launch (#12121)
Daniel Han1508a0ebd541
diff --git a/studio/backend/core/inference/mxc_policy.py b/studio/backend/core/inference/mxc_policy.pyindex 498aed2cab7..b1171199a10 100644--- a/studio/backend/core/inference/mxc_policy.py+++ b/studio/backend/core/inference/mxc_policy.py@@ -17,6 +17,7 @@ import unicodedataimport uuidfrom . import os_sandbox+from . import mxc_read_grantsfrom . import mxc_runtimefrom .mxc_runtime import MXC_SCHEMA_VERSION@@ -347,9 +348,14 @@ def build_launch_request(plan, *, run_id: str | None = None) -> dict:workdir = _safe_canonical_path(plan.workdir, directory = True)limitations = _scan_workdir(workdir, required = required)selected_runtime = _selected_runtime(plan)- readonly = _runtime_read_roots(selected_runtime, _trusted_terminal_path_dirs(plan))- readonly = _without_nested(readonly + _model_read_roots(workdir, readonly))+ runtime_roots = _runtime_read_roots(selected_runtime, _trusted_terminal_path_dirs(plan))+ readonly = _without_nested(runtime_roots + _model_read_roots(workdir, runtime_roots))_reject_grants_over_dacl_journal([workdir, *readonly])+ # Tier 3 walks every readonly tree per launch; a one-time grant on the runtime folders skips it.+ if dacl_fallback_enabled():+ mxc_read_grants.ensure(runtime_roots)+ else:+ mxc_read_grants.revoke_recorded()execution_argv = list(plan.argv)execution_argv[0] = selected_runtimerun_id = run_id or uuid.uuid4().hexdiff --git a/studio/backend/core/inference/mxc_read_grants.py b/studio/backend/core/inference/mxc_read_grants.pynew file mode 100644index 00000000000..255d3a3e212--- /dev/null+++ b/studio/backend/core/inference/mxc_read_grants.py@@ -0,0 +1,496 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0+"""One-time AppContainer read grants on Studio's runtime folders for MXC's AppContainer + DACL tier.++On that tier wxc-exec grants every readonly path per launch with an inheritable ACE, which Windows+propagates to every file below it and reverts on exit, so a Python environment costs 10+ s per+command (microsoft/mxc#572). wxc-exec skips a path whose DACL already grants ALL APPLICATION PACKAGES+read and execute, as Windows ships C:\\Windows and Program Files. Granting that once on the runtime+folders Studio owns lets every later launch skip the walk. Read and execute only, never write.+"""++from __future__ import annotations++import contextlib+from contextlib import contextmanager+import json+import logging+import os+from pathlib import Path+import subprocess+import threading+import uuid++logger = logging.getLogger(__name__)++PERSISTENT_GRANTS_ENV = "UNSLOTH_MXC_PERSISTENT_READ_GRANTS"+ALL_APPLICATION_PACKAGES = "S-1-15-2-1"+# FILE_GENERIC_READ | FILE_GENERIC_EXECUTE: wxc-exec's readonly mask and icacls (RX).+READ_EXECUTE_MASK = 0x1200A9+# A tree holding one of these anywhere is never opened to every AppContainer on the machine.+CREDENTIAL_FILES = frozenset(+ {"pip.ini", "pip.conf", "uv.toml", ".pypirc", ".netrc", "_netrc", ".env"}+)+GRANT_TIMEOUT_SECONDS = 900+LOCK_TIMEOUT_SECONDS = GRANT_TIMEOUT_SECONDS + 60++_OBJECT_INHERIT = 0x1+_CONTAINER_INHERIT = 0x2+_INHERIT_ONLY = 0x8+_INHERITED = 0x10+_lock = threading.Lock()+# Root -> folder identity whose whole tree passed the credential scan in this process; a folder+# replaced at the same path, or a new process, scans again.+_scanned: dict[str, dict[str, int] | None] = {}+++class ReadGrantError(RuntimeError):+ """A persistent grant is in an unknown state, so this launch must not rely on it."""+++def _on_windows() -> bool:+ return os.name == "nt"+++def enabled() -> bool:+ return os.environ.get(PERSISTENT_GRANTS_ENV, "").strip() != "0"+++def record_path() -> Path:+ from .mxc_runtime import _studio_root+ return Path(os.path.abspath(_studio_root() / "mxc-runtime" / "persistent-read-grants.json"))+++@contextmanager+def _transaction():+ """Serialize the record across threads and Studio processes; yields None when it cannot be locked."""+ from filelock import FileLock, Timeout++ path = record_path()+ with _lock:+ try:+ path.parent.mkdir(parents = True, exist_ok = True)+ lock = FileLock(str(path) + ".lock", timeout = LOCK_TIMEOUT_SECONDS)+ lock.acquire()+ except Timeout as exc:+ # Another Studio process is still granting: its tree may be half propagated.+ raise ReadGrantError(+ f"another Studio process holds the MXC read-grant record: {exc}"+ ) from exc+ except OSError as exc:+ # No lock file means no process can be granting through this record either.+ logger.warning("Could not lock the MXC read-grant record: %s", exc)+ yield None+ return+ try:+ yield _load_record()+ finally:+ lock.release()+++def _load_record() -> dict[str, dict]:+ try:+ data = json.loads(record_path().read_text(encoding = "utf-8"))+ except (OSError, ValueError):+ return {}+ grants = data.get("grants") if isinstance(data, dict) else None+ if not isinstance(grants, dict):+ return {}+ return {+ str(key): value+ for key, value in grants.items()+ if isinstance(value, dict) and value.get("state") in {"pending", "complete"}+ }+++def _save_record(grants: dict[str, dict]) -> None:+ path = record_path()+ path.parent.mkdir(parents = True, exist_ok = True)+ temporary = path.with_name(f"{path.name}.{uuid.uuid4().hex}.tmp")+ try:+ temporary.write_text(+ json.dumps({"grants": grants}, indent = 2, sort_keys = True), encoding = "utf-8"+ )+ os.replace(temporary, path)+ finally:+ if temporary.exists():+ temporary.unlink()+++def _within(path: str, root: str) -> bool:+ folded, base = os.path.normcase(path), os.path.normcase(root).rstrip("\\/")+ return folded == base or folded.startswith(base + os.sep)+++def _protected_paths() -> list[str]:+ from . import os_sandbox+ from .mxc_runtime import dacl_state_path++ paths = [str(dacl_state_path()), str(record_path()), os.path.expanduser("~")]+ paths.extend(os_sandbox.studio_state_roots())+ return [os.path.realpath(p) for p in paths if p]+++def _is_reparse(entry: os.DirEntry) -> bool:+ try:+ attributes = getattr(entry.stat(follow_symlinks = False), "st_file_attributes", 0)+ except OSError:+ return True+ return entry.is_symlink() or bool(attributes & 0x400)+++def _links_within(path: str, real_root: str) -> bool:+ """A symlink or junction whose final target stays inside the granted tree."""+ try:+ os.readlink(path) # only symlinks and junctions; other reparse tags raise+ return _within(os.path.realpath(path), real_root)+ except (OSError, ValueError):+ return False+++def _tree_problem(root: str, *, deep: bool) -> str | None:+ """A credential file or reparse point in the tree the inheritable grant would reach."""+ real_root = os.path.realpath(root)+ pending = [root]+ while pending:+ directory = pending.pop()+ try:+ with os.scandir(directory) as entries:+ for entry in entries:+ if entry.name.casefold() in CREDENTIAL_FILES:+ return f"it holds a credential file ({entry.path})"+ if _is_reparse(entry):+ if _links_within(entry.path, real_root):+ continue # e.g. setup-python's python3.exe -> python.exe; the target is scanned anyway+ return f"it contains a reparse point ({entry.path})"+ if deep and entry.is_dir(follow_symlinks = False):+ pending.append(entry.path)+ except OSError as exc:+ return f"it cannot be inspected ({exc})"+ return None+++def ineligible_reason(root: str, *, deep: bool = True) -> str | None:+ """Why ``root`` must keep MXC's per-launch grant instead of a persistent one, or None."""+ _drive, tail = os.path.splitdrive(root)+ if not tail.strip("\\/"):+ return "a volume root"+ system_root = os.environ.get("SystemRoot") or os.environ.get("WINDIR")+ if system_root and _within(root, system_root):+ return "inside the Windows directory"+ for protected in _protected_paths():+ # Only a grant ABOVE protected state exposes it; a runtime folder below the Studio home is fine.+ if _within(protected, root):+ return f"it contains {protected}"+ return _tree_problem(root, deep = deep)+++def _identity(root: str) -> dict[str, int] | None:+ from .mxc_policy import MxcPolicyError, _object_identity, _safe_canonical_path+ try:+ if os.path.normcase(_safe_canonical_path(root, directory = True)) != os.path.normcase(root):+ return None+ return _object_identity(root, directory = True)+ except (MxcPolicyError, OSError):+ return None+++def _package_aces(path: str) -> tuple[bool, bool]:+ """(covers, explicit) for ALL APPLICATION PACKAGES on the folder's own DACL.++ covers: inheritable read and execute is granted. explicit: the folder carries its own ACE for+ that SID. Raises OSError when the DACL cannot be read, so an unknown ACL is never modified.+ """+ import ctypes+ from ctypes import wintypes++ advapi32 = ctypes.WinDLL("advapi32", use_last_error = True)+ kernel32 = ctypes.WinDLL("kernel32", use_last_error = True)++ class AceHeader(ctypes.Structure):+ _fields_ = [+ ("AceType", ctypes.c_ubyte),+ ("AceFlags", ctypes.c_ubyte),+ ("AceSize", ctypes.c_ushort),+ ]++ class AccessAllowedAce(ctypes.Structure):+ _fields_ = [("Header", AceHeader), ("Mask", wintypes.DWORD), ("SidStart", wintypes.DWORD)]++ class Acl(ctypes.Structure):+ _fields_ = [+ ("AclRevision", ctypes.c_ubyte),+ ("Sbz1", ctypes.c_ubyte),+ ("AclSize", ctypes.c_ushort),+ ("AceCount", ctypes.c_ushort),+ ("Sbz2", ctypes.c_ushort),+ ]++ get_info = advapi32.GetNamedSecurityInfoW+ get_info.argtypes = [+ wintypes.LPCWSTR,+ ctypes.c_int,+ wintypes.DWORD,+ ctypes.c_void_p,+ ctypes.c_void_p,+ ctypes.POINTER(ctypes.POINTER(Acl)),+ ctypes.c_void_p,+ ctypes.POINTER(ctypes.c_void_p),+ ]+ get_info.restype = wintypes.DWORD+ get_ace = advapi32.GetAce+ get_ace.argtypes = [ctypes.POINTER(Acl), wintypes.DWORD, ctypes.POINTER(ctypes.c_void_p)]+ get_ace.restype = wintypes.BOOL+ sid_to_string = advapi32.ConvertSidToStringSidW+ sid_to_string.argtypes = [ctypes.c_void_p, ctypes.POINTER(wintypes.LPWSTR)]+ sid_to_string.restype = wintypes.BOOL+ local_free = kernel32.LocalFree+ local_free.argtypes = [ctypes.c_void_p]+ local_free.restype = ctypes.c_void_p++ dacl = ctypes.POINTER(Acl)()+ descriptor = ctypes.c_void_p()+ # SE_FILE_OBJECT = 1, DACL_SECURITY_INFORMATION = 4+ status = get_info(path, 1, 4, None, None, ctypes.byref(dacl), None, ctypes.byref(descriptor))+ if status != 0:+ raise OSError(status, f"GetNamedSecurityInfoW failed for {path}")+ try:+ if not dacl:+ # A NULL DACL already grants everyone, AppContainers included: nothing to add.+ return True, False+ allowed, explicit = 0, False+ inheritable = _OBJECT_INHERIT | _CONTAINER_INHERIT+ for index in range(dacl.contents.AceCount):+ raw = ctypes.c_void_p()+ if not get_ace(dacl, index, ctypes.byref(raw)):+ raise OSError(ctypes.get_last_error(), f"GetAce failed for {path}")+ ace = ctypes.cast(raw, ctypes.POINTER(AccessAllowedAce)).contents+ # ACCESS_ALLOWED_ACE_TYPE = 0 and ACCESS_DENIED_ACE_TYPE = 1 share this layout.+ if ace.Header.AceType not in (0, 1):+ continue+ sid_text = wintypes.LPWSTR()+ sid = ctypes.c_void_p(raw.value + AccessAllowedAce.SidStart.offset)+ if not sid_to_string(sid, ctypes.byref(sid_text)):+ raise OSError(ctypes.get_last_error(), f"ConvertSidToStringSidW failed for {path}")+ try:+ matches = sid_text.value == ALL_APPLICATION_PACKAGES+ finally:+ local_free(ctypes.cast(sid_text, ctypes.c_void_p))+ if not matches:+ continue+ flags = ace.Header.AceFlags+ if not flags & _INHERITED:+ explicit = True+ if ace.Header.AceType == 1:+ return False, explicit+ if flags & _INHERIT_ONLY or (flags & inheritable) != inheritable:+ continue+ allowed |= ace.Mask+ return (allowed & READ_EXECUTE_MASK) == READ_EXECUTE_MASK, explicit+ finally:+ local_free(descriptor)+++def _icacls(root: str, *args: str) -> tuple[bool, str]:+ system_root = os.environ.get("SystemRoot") or os.environ.get("WINDIR") or "C:\\Windows"+ icacls = os.path.join(system_root, "System32", "icacls.exe")+ try:+ done = subprocess.run(+ [icacls, root, *args, "/Q"],+ capture_output = True,+ text = True,+ # icacls writes the console (OEM) codepage to a pipe; the codec exists only on Windows.+ encoding = "oem" if os.name == "nt" else "utf-8",+ errors = "replace",+ timeout = GRANT_TIMEOUT_SECONDS,+ creationflags = getattr(subprocess, "CREATE_NO_WINDOW", 0),+ )+ except (OSError, subprocess.TimeoutExpired) as exc:+ return False, str(exc)+ return done.returncode == 0, (done.stdout + done.stderr).strip()[-300:]+++def _grant(root: str) -> tuple[bool, str]:+ return _icacls(root, "/grant", f"*{ALL_APPLICATION_PACKAGES}:(OI)(CI)(RX)")+++def _revoke(root: str) -> tuple[bool, str]:+ return _icacls(root, "/remove:g", f"*{ALL_APPLICATION_PACKAGES}")+++def _save_quietly(record: dict) -> None:+ """Persist a record change after the ACL already matches it; a failed write only costs a retry."""+ try:+ _save_record(record)+ except OSError as exc:+ logger.warning("Could not update the MXC read-grant record: %s", exc)+++def _revoke_recorded_root(record: dict, key: str) -> str:+ """Take back one recorded grant: "revoked", "dropped" (nothing of Studio's left there), or "failed"."""+ if not os.path.isdir(key):+ record.pop(key)+ return "dropped"+ current = _identity(key)+ if current is None:+ logger.warning(+ "Not revoking the MXC read grant on %s yet: its identity could not be read", key+ )+ return "failed"+ if current != record[key].get("identity"):+ # Moved, replaced, or reached through a new junction: revoking would edit a folder Studio never touched.+ logger.warning(+ "Not revoking the MXC read grant on %s: it is no longer the folder Studio granted", key+ )+ record.pop(key)+ return "dropped"+ ok, output = _revoke(key)+ if ok:+ record.pop(key)+ _scanned.pop(key, None)+ return "revoked"+ logger.warning("Could not remove the persistent MXC read grant from %s: %s", key, output)+ return "failed"+++def _ensure_root(record: dict, root: str) -> bool:+ """Grant one root if it is eligible; True when wxc-exec will skip it. Saves the record as it goes.++ Raises ReadGrantError when a grant Studio started may be half propagated and cannot be settled.+ """+ key = os.path.normcase(root)+ entry = record.get(key)+ pending = entry is not None and entry.get("state") == "pending"+ current = _identity(root)+ if entry is not None:+ if current is None:+ if pending:+ raise ReadGrantError(f"the unfinished MXC read grant on {root} cannot be checked")+ logger.info(+ "Keeping the per-launch MXC grant for %s: its identity could not be read", root+ )+ return False+ if current != entry.get("identity"):+ # Another folder now sits at this path; Studio's claim is void and its ACL is not Studio's.+ record.pop(key)+ _save_quietly(record)+ entry, pending = None, False+ deep = key not in _scanned or _scanned[key] != current+ reason = ineligible_reason(root, deep = deep)+ if reason is not None:+ _scanned.pop(key, None)+ if entry is not None:+ # A folder Studio granted gained a credential file or a link: take the grant back.+ outcome = _revoke_recorded_root(record, key)+ _save_quietly(record)+ if outcome == "failed":+ raise ReadGrantError(f"could not revoke the MXC read grant on {root} ({reason})")+ logger.info("Keeping the per-launch MXC grant for %s: %s", root, reason)+ return False+ _scanned[key] = current+ try:+ covers, explicit = _package_aces(root)+ except OSError as exc:+ if pending:+ raise ReadGrantError(+ f"the unfinished MXC read grant on {root} cannot be checked: {exc}"+ ) from exc+ logger.info("Keeping the per-launch MXC grant for %s: %s", root, exc)+ return False+ if covers and not pending:+ return True+ if explicit and entry is None:+ # Someone else set an entry for that group here; /remove:g on opt-out would take theirs too.+ logger.info(+ "Keeping the per-launch MXC grant for %s: it already has its own entry for that group",+ root,+ )+ return False+ identity = _identity(root)+ if identity is None:+ if pending:+ raise ReadGrantError(f"the unfinished MXC read grant on {root} cannot be checked")+ logger.info("Keeping the per-launch MXC grant for %s: its identity could not be read", root)+ return False+ record[key] = {"state": "pending", "identity": identity}+ try:+ _save_record(record)+ except OSError as exc:+ if pending:+ raise ReadGrantError(+ f"could not record the retried MXC read grant on {root}: {exc}"+ ) from exc+ record.pop(key, None)+ logger.warning(+ "Keeping the per-launch MXC grant for %s: the grant record is not writable (%s)",+ root,+ exc,+ )+ return False+ ok, output = _grant(root)+ if ok:+ record[key] = {"state": "complete", "identity": identity}+ # On a failed write the pending entry stays on disk, so the next launch redoes the grant.+ _save_quietly(record)+ logger.info("Granted ALL APPLICATION PACKAGES read access to %s once", root)+ return True+ # A partly propagated grant would let wxc-exec skip files the container cannot read. The pending+ # entry stays on disk until the rollback is known to have hit the folder Studio granted.+ if _identity(root) != identity:+ raise ReadGrantError(+ f"the MXC read grant on {root} failed ({output}) and the folder changed"+ )+ restored, restore_output = _revoke(root)+ if not restored:+ raise ReadGrantError(+ f"the MXC read grant on {root} failed ({output}) and could not be rolled back ({restore_output})"+ )+ record.pop(key, None)+ _save_quietly(record)+ logger.warning("Could not add the persistent MXC read grant to %s: %s", root, output)+ return False+++def ensure(roots: list[str]) -> tuple[str, ...]:+ """Give ``roots`` a persistent read grant where eligible; returns the roots wxc-exec will skip.++ A root that cannot be granted keeps MXC's per-launch grant, which is only slower. Raises+ ReadGrantError only when a grant is left in an unknown state.+ """+ if not _on_windows():+ return ()+ if not enabled():+ revoke_recorded()+ return ()+ covered: list[str] = []+ with _transaction() as record:+ if record is None:+ return ()+ for root in roots:+ if _ensure_root(record, root):+ covered.append(root)+ return tuple(covered)+++def revoke_recorded() -> tuple[str, ...]:+ """Remove every grant Studio recorded; returns the roots that were restored."""+ if not _on_windows() or not record_path().exists():+ return ()+ restored: list[str] = []+ try:+ transaction = _transaction()+ record = transaction.__enter__()+ except ReadGrantError as exc:+ # Cleanup never blocks a launch; the entries stay and the next check retries.+ logger.warning("Deferring the MXC read-grant cleanup: %s", exc)+ return ()+ with contextlib.ExitStack() as stack:+ stack.push(transaction)+ if not record:+ return ()+ for key in list(record):+ if _revoke_recorded_root(record, key) == "revoked":+ restored.append(key)+ # A failed revoke keeps its entry, so the next launch or capability check retries it.+ _save_quietly(record)+ return tuple(restored)diff --git a/studio/backend/core/inference/sandbox_windows_mxc.py b/studio/backend/core/inference/sandbox_windows_mxc.pyindex e5c8936690e..039f4b16ea2 100644--- a/studio/backend/core/inference/sandbox_windows_mxc.py+++ b/studio/backend/core/inference/sandbox_windows_mxc.py@@ -11,7 +11,7 @@ import osimport subprocessimport sys-from . import mxc_adapter, mxc_policy, mxc_probe, mxc_runtime+from . import mxc_adapter, mxc_policy, mxc_probe, mxc_read_grants, mxc_runtimefrom .os_sandbox import (PreparedSandboxLaunch,SandboxBuildError,@@ -61,6 +61,9 @@ def capability_snapshot(remediation = "Select a supported native Windows runtime.",limitations = ("unsupported_execution_kind",),)+ if not (mxc_policy.dacl_fallback_enabled() and mxc_read_grants.enabled()):+ # Here too: a host with only the DACL tier stops at this capability and never builds a launch.+ mxc_read_grants.revoke_recorded()available, reason = mxc_probe.probe(selected_executable,execution_kind = execution_kind,@@ -82,6 +85,8 @@ def capability_snapshot()if dacl:limitations += ("mxc_tier3_dacl_host_permission_changes",)+ if mxc_read_grants.enabled():+ limitations += ("mxc_tier3_persistent_runtime_read_grants",)shell_incompatible = reason == mxc_probe.MSYS_NAMESPACE_REASONif shell_incompatible:remediation = (@@ -98,7 +103,10 @@ def capability_snapshot("Install the pinned Microsoft WXC runtime and enable BaseContainer/PSEC. On Windows "f"builds without it, set {mxc_policy.DACL_FALLBACK_ENV}=1 to use the AppContainer ""tier: it adds temporary permission entries to the granted host folders, removed "- "on exit, and needs a one-time administrator host preparation plus one per reboot."+ "on exit, and needs a one-time administrator host preparation plus one per reboot. "+ "Studio's own Python runtime folders get a permanent read-only entry instead, so "+ f"launches stay fast; set {mxc_read_grants.PERSISTENT_GRANTS_ENV}=0 to keep every "+ "entry temporary.")# Only in DACL mode: a bare --probe allows the fallback, so it warns on hosts Studio never uses it on.host_prep = (diff --git a/studio/backend/tests/test_mxc_dacl_fallback.py b/studio/backend/tests/test_mxc_dacl_fallback.pyindex 3438d5067f9..3a32c23da01 100644--- a/studio/backend/tests/test_mxc_dacl_fallback.py+++ b/studio/backend/tests/test_mxc_dacl_fallback.py@@ -12,7 +12,14 @@ from types import SimpleNamespaceimport pytest-from core.inference import mxc_adapter, mxc_policy, mxc_probe, mxc_runtime, os_sandbox+from core.inference import (+ mxc_adapter,+ mxc_policy,+ mxc_probe,+ mxc_read_grants,+ mxc_runtime,+ os_sandbox,+)from core.inference import sandbox_windows_mxcOPT_IN = mxc_policy.DACL_FALLBACK_ENV@@ -23,6 +30,9 @@ def _studio_home(monkeypatch, tmp_path):home = tmp_path / "studio-home"monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(home))monkeypatch.delenv(OPT_IN, raising = False)+ # Unit tests never touch real ACLs; test_mxc_read_grants.py covers the grant itself.+ monkeypatch.setattr(mxc_read_grants, "ensure", lambda _roots: ())+ monkeypatch.setattr(mxc_read_grants, "revoke_recorded", lambda: ())mxc_probe.invalidate_cache()yield homemxc_probe.invalidate_cache()diff --git a/studio/backend/tests/test_mxc_git_bash_namespace.py b/studio/backend/tests/test_mxc_git_bash_namespace.pyindex 6f11420cba6..a7845de547f 100644--- a/studio/backend/tests/test_mxc_git_bash_namespace.py+++ b/studio/backend/tests/test_mxc_git_bash_namespace.py@@ -6,7 +6,7 @@ from types import SimpleNamespaceimport pytest-from core.inference import mxc_probe, os_sandbox, sandbox_windows_mxc+from core.inference import mxc_probe, mxc_read_grants, os_sandbox, sandbox_windows_mxc# What Git for Windows' bash prints under an AppContainer before it reads its command (microsoft/mxc#1061).MSYS_STARTUP_FAILURE = (@@ -17,7 +17,10 @@ DLL_INIT_FAILED = 3221225794 # 0xC0000142@pytest.fixture(autouse = True)-def _fresh_probe_cache():+def _fresh_probe_cache(monkeypatch):+ # Unit tests never touch real ACLs; test_mxc_read_grants.py covers the grant itself.+ monkeypatch.setattr(mxc_read_grants, "ensure", lambda _roots: ())+ monkeypatch.setattr(mxc_read_grants, "revoke_recorded", lambda: ())mxc_probe.invalidate_cache()yieldmxc_probe.invalidate_cache()diff --git a/studio/backend/tests/test_mxc_read_grants.py b/studio/backend/tests/test_mxc_read_grants.pynew file mode 100644index 00000000000..a29a8445e3f--- /dev/null+++ b/studio/backend/tests/test_mxc_read_grants.py@@ -0,0 +1,520 @@+# SPDX-License-Identifier: AGPL-3.0-only++from __future__ import annotations++import json+import multiprocessing+import os+from pathlib import Path+import sys++import pytest++from core.inference import mxc_read_grants, mxc_runtime, os_sandbox+++class _Host:+ """Stands in for the Windows DACL: which roots already grant, and what icacls was asked to do."""++ def __init__(self):+ self.granted: set[str] = set()+ self.explicit: set[str] = set()+ self.calls: list[tuple[str, str]] = []+ self.fail_grant = False+ self.fail_revoke = False++ def aces(self, root):+ key = os.path.normcase(root)+ return key in self.granted, key in self.explicit++ def grant(self, root):+ self.calls.append(("grant", root))+ if self.fail_grant:+ return False, "Access is denied."+ self.granted.add(os.path.normcase(root))+ self.explicit.add(os.path.normcase(root))+ return True, "Successfully processed 1 files"++ def revoke(self, root):+ self.calls.append(("revoke", root))+ if self.fail_revoke:+ return False, "Access is denied."+ self.granted.discard(os.path.normcase(root))+ self.explicit.discard(os.path.normcase(root))+ return True, "Successfully processed 1 files"+++def _isolate(monkeypatch, tmp_path):+ studio_home = tmp_path / "home" / ".unsloth" / "studio"+ studio_home.mkdir(parents = True, exist_ok = True)+ monkeypatch.setattr(mxc_read_grants, "_on_windows", lambda: True)+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(studio_home))+ monkeypatch.setenv("SystemRoot", str(tmp_path / "Windows"))+ monkeypatch.setenv("HOME", str(tmp_path / "home"))+ monkeypatch.delenv(mxc_read_grants.PERSISTENT_GRANTS_ENV, raising = False)+ monkeypatch.setattr(os_sandbox, "studio_state_roots", lambda: (str(studio_home),))+ monkeypatch.setattr(+ mxc_runtime, "dacl_state_path", lambda: studio_home / "mxc-runtime" / "dacl-restore"+ )+ mxc_read_grants._scanned.clear()+ return studio_home+++def host(monkeypatch, tmp_path):+ fake = _Host()+ fake.studio_home = _isolate(monkeypatch, tmp_path)+ monkeypatch.setattr(mxc_read_grants, "_package_aces", fake.aces)+ monkeypatch.setattr(mxc_read_grants, "_grant", fake.grant)+ monkeypatch.setattr(mxc_read_grants, "_revoke", fake.revoke)+ return fake+++def _runtime(host, name = "unsloth_studio"):+ root = host.studio_home / name+ (root / "Lib" / "site-packages" / "six").mkdir(parents = True)+ (root / "Lib" / "site-packages" / "six" / "__init__.py").write_text("")+ return str(root)+++def _record():+ path = mxc_read_grants.record_path()+ return json.loads(path.read_text(encoding = "utf-8"))["grants"] if path.exists() else {}+++def _states():+ return {key: value["state"] for key, value in _record().items()}+++def test_eligible_runtime_root_is_granted_once_and_recorded(host):+ venv = _runtime(host)+ assert mxc_read_grants.ensure([venv]) == (venv,)+ assert host.calls == [("grant", venv)]+ assert _states() == {os.path.normcase(venv): "complete"}+ assert _record()[os.path.normcase(venv)]["identity"]+ # The second launch finds the ACE and never walks the tree again.+ assert mxc_read_grants.ensure([venv]) == (venv,)+ assert host.calls == [("grant", venv)]+++def test_a_folder_windows_already_grants_is_left_alone(host, tmp_path):+ program_files = tmp_path / "Program Files" / "Git" / "usr" / "bin"+ program_files.mkdir(parents = True)+ host.granted.add(os.path.normcase(str(program_files)))+ assert mxc_read_grants.ensure([str(program_files)]) == (str(program_files),)+ assert host.calls == []+ assert _record() == {}+++def test_an_existing_entry_for_the_group_is_never_taken_over(host):+ # Someone else's partial grant: adding ours and later /remove:g would delete theirs too.+ venv = _runtime(host)+ host.explicit.add(os.path.normcase(venv))+ assert mxc_read_grants.ensure([venv]) == ()+ assert host.calls == []+++ "make_root, why",+ [+ (lambda host, tmp: str(host.studio_home.parent), "contains"),+ (lambda host, tmp: str(tmp / "home"), "contains"),+ (lambda host, tmp: str(tmp / "Windows" / "System32"), "Windows directory"),+ ],+)+def test_roots_above_protected_state_or_under_windows_are_never_granted(+ host, tmp_path, make_root, why+):+ root = make_root(host, tmp_path)+ os.makedirs(root, exist_ok = True)+ assert why in mxc_read_grants.ineligible_reason(root)+ assert mxc_read_grants.ensure([root]) == ()+ assert host.calls == []++[email protected]("where", [".", "Lib/site-packages/six"])[email protected]("name", ["pip.ini", "PIP.CONF", ".pypirc", ".netrc", ".env"])+def test_a_credential_file_anywhere_in_the_tree_keeps_the_per_launch_grant(host, where, name):+ venv = _runtime(host)+ Path(venv, where, name).write_text(+ "[global]\nindex-url = https://user:[email protected]/simple\n"+ )+ assert "credential file" in mxc_read_grants.ineligible_reason(venv)+ assert mxc_read_grants.ensure([venv]) == ()+ assert host.calls == []+++def test_a_link_out_of_the_tree_keeps_the_per_launch_grant(host, tmp_path):+ venv = _runtime(host)+ elsewhere = tmp_path / "elsewhere"+ elsewhere.mkdir()+ try:+ os.symlink(elsewhere, Path(venv, "Lib", "linked"), target_is_directory = True)+ except OSError:+ pytest.skip("symlinks are not available")+ assert "reparse point" in mxc_read_grants.ineligible_reason(venv)+ assert mxc_read_grants.ensure([venv]) == ()+ assert host.calls == []+++def test_a_link_to_a_file_in_the_same_tree_is_granted(host):+ venv = _runtime(host)+ Path(venv, "python.dat").write_bytes(b"")+ try:+ os.symlink(Path(venv, "python.dat"), Path(venv, "python3.dat"))+ except OSError:+ pytest.skip("symlinks are not available")+ assert mxc_read_grants.ineligible_reason(venv) is None+ assert mxc_read_grants.ensure([venv]) == (venv,)+++def test_a_credential_named_link_inside_the_tree_is_refused(host):+ venv = _runtime(host)+ try:+ os.symlink(Path(venv, "Lib", "site-packages", "six", "__init__.py"), Path(venv, ".env"))+ except OSError:+ pytest.skip("symlinks are not available")+ assert "credential file" in mxc_read_grants.ineligible_reason(venv)+ assert mxc_read_grants.ensure([venv]) == ()+++def test_a_link_that_leaves_the_tree_through_an_inner_link_is_refused(host, tmp_path):+ venv = _runtime(host)+ elsewhere = tmp_path / "elsewhere"+ elsewhere.mkdir()+ try:+ os.symlink(elsewhere, Path(venv, "Lib", "outer"), target_is_directory = True)+ os.symlink(Path(venv, "Lib", "outer"), Path(venv, "inner"), target_is_directory = True)+ except OSError:+ pytest.skip("symlinks are not available")+ assert "reparse point" in mxc_read_grants.ineligible_reason(venv)+++def test_a_credential_file_added_after_the_grant_takes_it_back(host):+ venv = _runtime(host)+ mxc_read_grants.ensure([venv])+ Path(venv, "pip.ini").write_text("[global]\n")+ assert mxc_read_grants.ensure([venv]) == ()+ assert host.calls == [("grant", venv), ("revoke", os.path.normcase(venv))]+ assert _record() == {}+++def test_a_failed_grant_is_revoked_so_wxc_exec_never_skips_an_unreadable_tree(host):+ venv = _runtime(host)+ host.fail_grant = True+ assert mxc_read_grants.ensure([venv]) == ()+ assert host.calls == [("grant", venv), ("revoke", venv)]+ assert _record() == {}+++def test_a_failed_rollback_refuses_the_launch_and_keeps_the_retry(host):+ venv = _runtime(host)+ host.fail_grant = host.fail_revoke = True+ with pytest.raises(mxc_read_grants.ReadGrantError, match = "could not be rolled back"):+ mxc_read_grants.ensure([venv])+ assert _states() == {os.path.normcase(venv): "pending"}+ host.fail_grant = host.fail_revoke = False+ assert mxc_read_grants.ensure([venv]) == (venv,)+ assert _states() == {os.path.normcase(venv): "complete"}+++def test_a_grant_interrupted_by_a_crash_is_redone(host):+ venv = _runtime(host)+ host.granted.add(os.path.normcase(venv)) # the root ACE landed, the propagation may not have+ host.explicit.add(os.path.normcase(venv))+ identity = mxc_read_grants._identity(venv)+ mxc_read_grants._save_record(+ {os.path.normcase(venv): {"state": "pending", "identity": identity}}+ )+ assert mxc_read_grants.ensure([venv]) == (venv,)+ assert host.calls == [("grant", venv)]+ assert _states() == {os.path.normcase(venv): "complete"}+++def test_an_unwritable_record_keeps_the_per_launch_grant(host, monkeypatch):+ venv = _runtime(host)++ def refuse(_grants):+ raise PermissionError("read-only")++ monkeypatch.setattr(mxc_read_grants, "_save_record", refuse)+ assert mxc_read_grants.ensure([venv]) == ()+ assert host.calls == []+++def test_opting_out_revokes_every_recorded_grant(host, monkeypatch):+ venv = _runtime(host)+ mxc_read_grants.ensure([venv])+ monkeypatch.setenv(mxc_read_grants.PERSISTENT_GRANTS_ENV, "0")+ assert not mxc_read_grants.enabled()+ assert mxc_read_grants.ensure([venv]) == ()+ assert host.calls == [("grant", venv), ("revoke", os.path.normcase(venv))]+ assert _record() == {}+++def test_revocation_never_follows_a_folder_that_was_replaced(host, tmp_path):+ venv = _runtime(host)+ mxc_read_grants.ensure([venv])+ os.replace(venv, tmp_path / "moved-away")+ other = tmp_path / "another-app"+ other.mkdir()+ try:+ os.symlink(other, venv, target_is_directory = True)+ linked = True+ except OSError:+ os.mkdir(venv) # no symlinks here: a fresh folder at the same path has another identity+ linked = False+ assert mxc_read_grants.revoke_recorded() == ()+ assert ("revoke", os.path.normcase(venv)) not in host.calls+ # A link cannot be identified, so the entry waits; a different folder voids it.+ assert (_record() != {}) is linked+++def test_an_unreadable_identity_keeps_the_record_for_a_later_revoke(host, monkeypatch):+ venv = _runtime(host)+ mxc_read_grants.ensure([venv])+ monkeypatch.setattr(mxc_read_grants, "_identity", lambda _root: None)+ assert mxc_read_grants.revoke_recorded() == ()+ assert _states() == {os.path.normcase(venv): "complete"}+ assert ("revoke", os.path.normcase(venv)) not in host.calls+++def test_a_replaced_folder_is_not_adopted_through_a_stale_record(host, tmp_path):+ venv = _runtime(host)+ mxc_read_grants.ensure([venv])+ os.replace(venv, tmp_path / "moved-away")+ _runtime(host) # a new folder at the same path, carrying its owner's own entry for the group+ host.explicit.add(os.path.normcase(venv))+ host.granted.discard(os.path.normcase(venv))+ assert mxc_read_grants.ensure([venv]) == ()+ assert host.calls == [("grant", venv)]+ assert _record() == {}+++def test_a_folder_replaced_at_a_scanned_path_is_scanned_again(host, tmp_path):+ venv = _runtime(host)+ assert mxc_read_grants.ensure([venv]) == (venv,)+ os.replace(venv, tmp_path / "moved-away")+ _runtime(host)+ Path(venv, "Lib", "site-packages", "six", ".env").write_text("TOKEN=secret\n")+ host.granted.discard(os.path.normcase(venv))+ host.explicit.discard(os.path.normcase(venv))+ assert mxc_read_grants.ensure([venv]) == ()+ assert host.calls == [("grant", venv)]++[email protected]("unknown", ["aces", "identity"])+def test_an_unfinished_grant_that_cannot_be_checked_refuses_the_launch(host, monkeypatch, unknown):+ venv = _runtime(host)+ identity = mxc_read_grants._identity(venv)+ mxc_read_grants._save_record(+ {os.path.normcase(venv): {"state": "pending", "identity": identity}}+ )+ if unknown == "aces":++ def unreadable(_root):+ raise OSError(5, "Access is denied")++ monkeypatch.setattr(mxc_read_grants, "_package_aces", unreadable)+ else:+ monkeypatch.setattr(mxc_read_grants, "_identity", lambda _root: None)+ with pytest.raises(mxc_read_grants.ReadGrantError, match = "cannot be checked"):+ mxc_read_grants.ensure([venv])+ assert _states() == {os.path.normcase(venv): "pending"}+++def test_a_record_write_failure_after_a_clean_rollback_does_not_block_the_launch(host, monkeypatch):+ venv = _runtime(host)+ host.fail_grant = True+ real_save = mxc_read_grants._save_record+ writes = []++ def save_then_fail(grants):+ writes.append(dict(grants))+ if len(writes) > 1:+ raise PermissionError("disk went read-only")+ real_save(grants)++ monkeypatch.setattr(mxc_read_grants, "_save_record", save_then_fail)+ assert mxc_read_grants.ensure([venv]) == ()+ assert host.calls == [("grant", venv), ("revoke", venv)]+++def test_another_process_mid_grant_refuses_the_launch_but_not_the_cleanup(host, monkeypatch):+ from filelock import FileLock++ venv = _runtime(host)+ mxc_read_grants.ensure([venv])+ monkeypatch.setattr(mxc_read_grants, "LOCK_TIMEOUT_SECONDS", 0.2)+ holder = FileLock(str(mxc_read_grants.record_path()) + ".lock")+ holder.acquire()+ try:+ # Held by "another process": this launch cannot know whether that grant finished.+ mxc_read_grants._scanned.clear()+ host.granted.discard(os.path.normcase(venv))+ monkeypatch.setattr(mxc_read_grants, "_lock", __import__("threading").Lock())+ with pytest.raises(mxc_read_grants.ReadGrantError, match = "another Studio process"):+ _in_thread(lambda: mxc_read_grants.ensure([venv]))+ assert _in_thread(mxc_read_grants.revoke_recorded) == ()+ finally:+ holder.release()+++def _in_thread(function):+ """filelock is reentrant within a thread, so a second holder must run on another thread."""+ import concurrent.futures+ with concurrent.futures.ThreadPoolExecutor(1) as pool:+ return pool.submit(function).result()+++def test_off_windows_nothing_is_touched(host, monkeypatch):+ monkeypatch.setattr(mxc_read_grants, "_on_windows", lambda: False)+ assert mxc_read_grants.ensure([_runtime(host)]) == ()+ assert mxc_read_grants.revoke_recorded() == ()+ assert host.calls == []+++def _grant_in_another_process(tmp_path, name, queue):+ monkeypatch = pytest.MonkeyPatch()+ studio_home = _isolate(monkeypatch, Path(tmp_path))+ monkeypatch.setattr(mxc_read_grants, "_package_aces", lambda _root: (False, False))+ monkeypatch.setattr(mxc_read_grants, "_grant", lambda _root: (True, "ok"))+ root = studio_home / name+ root.mkdir()+ queue.put(mxc_read_grants.ensure([str(root)]))+++def test_concurrent_studio_processes_keep_every_record(tmp_path):+ context = multiprocessing.get_context("spawn")+ queue = context.Queue()+ names = [f"runtime-{index}" for index in range(4)]+ workers = [+ context.Process(target = _grant_in_another_process, args = (str(tmp_path), name, queue))+ for name in names+ ]+ for worker in workers:+ worker.start()+ for worker in workers:+ worker.join(120)+ assert [worker.exitcode for worker in workers] == [0] * len(names)+ assert all(queue.get(timeout = 5) for _ in names)+ record = (+ tmp_path / "home" / ".unsloth" / "studio" / "mxc-runtime" / "persistent-read-grants.json"+ )+ assert sorted(Path(key).name for key in json.loads(record.read_text())["grants"]) == names+++def _python_plan(tmp_path):+ selected = tmp_path / "venv" / "Scripts" / "python.exe"+ selected.parent.mkdir(parents = True)+ selected.touch()+ workdir = tmp_path / "work"+ workdir.mkdir()+ return os_sandbox.ToolLaunchPlan(+ argv = (str(selected), "-c", "pass"),+ workdir = str(workdir),+ env = {"PATH": str(selected.parent)},+ execution_kind = "python",+ )+++def _policy_on_linux(monkeypatch):+ from core.inference import mxc_policy++ outside = Path(os.path.abspath(os.sep)) / "unsloth-test-dacl-journal-never-created"+ monkeypatch.setattr(mxc_runtime, "dacl_state_path", lambda: outside)+ monkeypatch.setattr(mxc_policy.sys, "platform", "win32")+ return mxc_policy++[email protected]("dacl", [True, False])+def test_launch_policy_grants_runtime_roots_only_on_the_dacl_tier(monkeypatch, tmp_path, dacl):+ mxc_policy = _policy_on_linux(monkeypatch)+ model = os.path.abspath(os.path.join(os.sep, "unsloth-test-model-library"))+ monkeypatch.setattr(mxc_policy, "_model_read_roots", lambda _workdir, _granted: [model])+ if dacl:+ monkeypatch.setenv(mxc_policy.DACL_FALLBACK_ENV, "1")+ else:+ monkeypatch.delenv(mxc_policy.DACL_FALLBACK_ENV, raising = False)+ calls = []+ monkeypatch.setattr(+ mxc_read_grants, "ensure", lambda roots: calls.append(("ensure", list(roots))) or ()+ )+ monkeypatch.setattr(+ mxc_read_grants, "revoke_recorded", lambda: calls.append(("revoke", None)) or ()+ )++ request = mxc_policy.build_launch_request(_python_plan(tmp_path))+ readonly = request["config"]["filesystem"]["readonlyPaths"]+ assert model in readonly+ if dacl:+ [(kind, roots)] = calls+ assert kind == "ensure"+ # The model library stays a per-launch grant: it is user data, not Studio's runtime.+ assert model not in roots+ assert set(roots) <= set(readonly)+ else:+ assert calls == [("revoke", None)]+++def test_a_grant_left_in_an_unknown_state_refuses_the_launch(monkeypatch, tmp_path):+ mxc_policy = _policy_on_linux(monkeypatch)+ monkeypatch.setenv(mxc_policy.DACL_FALLBACK_ENV, "1")++ def unknown(_roots):+ raise mxc_read_grants.ReadGrantError("rollback failed")++ monkeypatch.setattr(mxc_read_grants, "ensure", unknown)+ with pytest.raises(mxc_read_grants.ReadGrantError):+ mxc_policy.build_launch_request(_python_plan(tmp_path))+++def test_capability_names_the_persistent_grant_and_cleans_up_when_off(monkeypatch):+ from core.inference import mxc_policy, sandbox_windows_mxc++ monkeypatch.setattr(sandbox_windows_mxc.sys, "platform", "win32")+ monkeypatch.setattr(+ sandbox_windows_mxc.mxc_probe, "probe", lambda *_a, **_k: (False, "unavailable")+ )+ monkeypatch.setattr(sandbox_windows_mxc.mxc_probe, "host_prep_remediation", lambda: None)+ monkeypatch.setattr(sandbox_windows_mxc.mxc_runtime, "installation_identity", lambda: "id")+ revoked = []+ monkeypatch.setattr(mxc_read_grants, "revoke_recorded", lambda: revoked.append(True) or ())+ monkeypatch.setenv(mxc_policy.DACL_FALLBACK_ENV, "1")+ monkeypatch.delenv(mxc_read_grants.PERSISTENT_GRANTS_ENV, raising = False)+ snapshot = sandbox_windows_mxc.capability_snapshot+ on = snapshot(execution_kind = "python", selected_executable = sys.executable)+ assert "mxc_tier3_persistent_runtime_read_grants" in on.limitations+ assert revoked == []+ monkeypatch.setenv(mxc_read_grants.PERSISTENT_GRANTS_ENV, "0")+ off = snapshot(execution_kind = "python", selected_executable = sys.executable)+ assert "mxc_tier3_persistent_runtime_read_grants" not in off.limitations+ assert revoked == [True]+ monkeypatch.delenv(mxc_read_grants.PERSISTENT_GRANTS_ENV)+ monkeypatch.delenv(mxc_policy.DACL_FALLBACK_ENV)+ # Leaving the DACL tier on a host with nothing else: the capability is unavailable, no launch is+ # ever built, and the grants still come back.+ default = snapshot(execution_kind = "python", selected_executable = sys.executable)+ assert "mxc_tier3_persistent_runtime_read_grants" not in default.limitations+ assert mxc_read_grants.PERSISTENT_GRANTS_ENV in default.remediation+ assert revoked == [True, True]++[email protected](sys.platform != "win32", reason = "reads a real Windows DACL")+def test_real_dacl_check_sees_a_granted_folder(tmp_path):+ import subprocess++ folder = tmp_path / "runtime"+ folder.mkdir()+ grant = ["icacls", str(folder), "/grant", "*S-1-15-2-1:(OI)(CI)(RX)", "/Q"]+ assert mxc_read_grants._package_aces(str(folder)) == (False, False)+ subprocess.run(grant, check = True, capture_output = True)+ assert mxc_read_grants._package_aces(str(folder)) == (True, True)+ child = folder / "child"+ child.mkdir()+ # Inherited, not explicit: a child of a granted folder is covered but was never set by Studio.+ assert mxc_read_grants._package_aces(str(child)) == (True, False)+ subprocess.run(+ ["icacls", str(folder), "/remove:g", "*S-1-15-2-1", "/Q"], check = True, capture_output = True+ )+ assert mxc_read_grants._package_aces(str(folder)) == (False, False)diff --git a/studio/backend/tests/test_mxc_windows_backend.py b/studio/backend/tests/test_mxc_windows_backend.pyindex 68b5145c1d5..68e6979eab4 100644--- a/studio/backend/tests/test_mxc_windows_backend.py+++ b/studio/backend/tests/test_mxc_windows_backend.py@@ -14,7 +14,7 @@ from dataclasses import replaceimport pytest-from core.inference import mxc_runtime, os_sandbox, tools+from core.inference import mxc_read_grants, mxc_runtime, os_sandbox, tools@pytest.fixture(autouse = True)@@ -23,6 +23,9 @@ def _dacl_journal_outside_grants(monkeypatch, tmp_path):outside = Path(os.path.abspath(os.sep)) / "unsloth-test-dacl-journal-never-created"monkeypatch.setattr(mxc_runtime, "dacl_state_path", lambda: outside)monkeypatch.setattr(mxc_runtime, "dacl_state_dir", lambda: tmp_path)+ # Unit tests never touch real ACLs; test_mxc_read_grants.py covers the grant itself.+ monkeypatch.setattr(mxc_read_grants, "ensure", lambda _roots: ())+ monkeypatch.setattr(mxc_read_grants, "revoke_recorded", lambda: ())def _plan(tmp_path, mode = "auto"):