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
Fix MXC inherited runtime permissions and pending grant recovery (#12758)
Lee Jackson8a469fed6957
diff --git a/studio/backend/core/inference/mxc_read_grants.py b/studio/backend/core/inference/mxc_read_grants.pyindex 392fbca4d3d..729759df03d 100644--- a/studio/backend/core/inference/mxc_read_grants.py+++ b/studio/backend/core/inference/mxc_read_grants.py@@ -202,6 +202,36 @@ def _identity(root: str) -> dict[str, int] | None:return None+def _read_execute_covered(aces: list[tuple[int, int]]) -> bool:+ """Check this folder and inheritable file/directory access separately.++ Windows' standard Program Files ACL splits (RX) on the folder from+ inherit-only (OI)(CI)(GR,GE) on children. Generic rights in the latter+ must be mapped to file rights before comparing them with (RX).+ """+ folder = files = directories = 0+ for mask, flags in aces:+ if mask & 0x10000000: # GENERIC_ALL+ mask |= 0x1F01FF+ if mask & 0x80000000: # GENERIC_READ+ mask |= 0x120089+ if mask & 0x40000000: # GENERIC_WRITE+ mask |= 0x120116+ if mask & 0x20000000: # GENERIC_EXECUTE+ mask |= 0x1200A0+ if not flags & _INHERIT_ONLY:+ folder |= mask+ if flags & 0x4: # NO_PROPAGATE_INHERIT cannot cover the whole tree.+ continue+ if flags & _OBJECT_INHERIT:+ files |= mask+ if flags & _CONTAINER_INHERIT:+ directories |= mask+ return all(+ (mask & READ_EXECUTE_MASK) == READ_EXECUTE_MASK for mask in (folder, files, directories)+ )++def _package_aces(path: str) -> tuple[bool, bool]:"""(covers, explicit) for ALL APPLICATION PACKAGES on the folder's own DACL.@@ -265,8 +295,8 @@ def _package_aces(path: str) -> tuple[bool, bool]: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+ allowed: list[tuple[int, int]] = []+ explicit = Falsefor index in range(dacl.contents.AceCount):raw = ctypes.c_void_p()if not get_ace(dacl, index, ctypes.byref(raw)):@@ -290,10 +320,8 @@ def _package_aces(path: str) -> tuple[bool, bool]:explicit = Trueif 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+ allowed.append((ace.Mask, flags))+ return _read_execute_covered(allowed), explicitfinally:local_free(descriptor)@@ -333,6 +361,28 @@ def _save_quietly(record: dict) -> None:logger.warning("Could not update the MXC read-grant record: %s", exc)+def _pending_grant_has_no_explicit_aces(root: str, identity: dict) -> bool:+ """Prove a failed attempt left no explicit package ACE anywhere in its tree.++ Checking only the root would miss an interrupted propagation or rollback.+ Unknown ACLs and reparse points keep the recovery record intact.+ """+ pending = [root]+ try:+ while pending:+ directory = pending.pop()+ with os.scandir(directory) as entries:+ for entry in entries:+ if _is_reparse(entry) or _package_aces(entry.path)[1]:+ return False+ if entry.is_dir(follow_symlinks = False):+ pending.append(entry.path)+ covers, explicit = _package_aces(root)+ return covers and not explicit and _identity(root) == identity+ except OSError:+ return False++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):@@ -351,6 +401,10 @@ def _revoke_recorded_root(record: dict, key: str) -> str:)record.pop(key)return "dropped"+ if record[key].get("state") == "pending" and _pending_grant_has_no_explicit_aces(key, current):+ # Only inherited Windows permissions remain; none belong to Studio.+ record.pop(key)+ return "dropped"ok, output = _revoke(key)if ok:record.pop(key)@@ -404,6 +458,10 @@ def _ensure_root(record: dict, root: str) -> bool:) from exclogger.info("Keeping the per-launch MXC grant for %s: %s", root, exc)return False+ if pending and covers and not explicit and _pending_grant_has_no_explicit_aces(root, current):+ record.pop(key)+ _save_quietly(record)+ return Trueif covers and not pending:return Trueif explicit and entry is None:diff --git a/studio/backend/tests/test_mxc_read_grants.py b/studio/backend/tests/test_mxc_read_grants.pyindex 4c553d05d2d..66dbceacb2b 100644--- a/studio/backend/tests/test_mxc_read_grants.py+++ b/studio/backend/tests/test_mxc_read_grants.py@@ -649,3 +649,104 @@ def test_real_dacl_check_sees_a_granted_folder(tmp_path):["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)+++ "aces, expected",+ [+ ([(0x1200A9, 0), (0xA0000000, 0x0B)], True),+ ([(0x1200A9, 0x03)], True),+ ([(0x10000000, 0x03)], True),+ ([(0x1200A9, 0)], False),+ ([(0xA0000000, 0x0B)], False),+ ([(0x1200A9, 0), (0x80000000, 0x0B)], False),+ ([(0x1200A9, 0), (0xA0000000, 0x09)], False),+ ([(0x1200A9, 0), (0xA0000000, 0x0A)], False),+ ([(0x1200A9, 0), (0xA0000000, 0x0F)], False),+ ],+)+def test_split_inherited_package_read_access(aces, expected):+ assert mxc_read_grants._read_execute_covered(aces) is expected++[email protected](sys.platform != "win32", reason = "reads a real Windows DACL")+def test_real_dacl_split_folder_and_child_grants(tmp_path):+ import subprocess++ folder = tmp_path / "split-runtime"+ folder.mkdir()+ subprocess.run(+ [+ "icacls",+ str(folder),+ "/grant",+ "*S-1-15-2-1:(RX)",+ "*S-1-15-2-1:(OI)(CI)(IO)(GR,GE)",+ "/Q",+ ],+ check = True,+ capture_output = True,+ )+ assert mxc_read_grants._package_aces(str(folder)) == (True, True)+ child = folder / "child"+ child.mkdir()+ assert mxc_read_grants._package_aces(str(child)) == (True, False)+++def test_failed_grant_with_only_inherited_access_recovers_without_acl_edits(host):+ root = _runtime(host)+ key = os.path.normcase(root)+ host.granted.add(key)+ mxc_read_grants._save_record(+ {+ key: {+ "state": "pending",+ "identity": mxc_read_grants._identity(root),+ }+ }+ )+ assert mxc_read_grants.ensure([root]) == (root,)+ assert _record() == {}+ assert host.calls == []+++def test_opt_out_drops_proven_empty_pending_grant_without_revoking_windows_access(host):+ root = _runtime(host)+ key = os.path.normcase(root)+ host.granted.add(key)+ mxc_read_grants._save_record(+ {+ key: {+ "state": "pending",+ "identity": mxc_read_grants._identity(root),+ }+ }+ )+ mxc_read_grants.revoke_recorded()+ assert _record() == {}+ assert key in host.granted+ assert host.calls == []++[email protected]("obstacle", ["explicit_child", "unreadable_child", "changed_identity"])+def test_pending_recovery_retains_uncertain_or_partial_grants(host, monkeypatch, obstacle):+ root = _runtime(host)+ key = os.path.normcase(root)+ host.granted.add(key)+ identity = mxc_read_grants._identity(root)+ mxc_read_grants._save_record({key: {"state": "pending", "identity": identity}})+ child = os.path.normcase(str(Path(root) / "Lib"))+ if obstacle == "explicit_child":+ host.explicit.add(child)+ elif obstacle == "unreadable_child":++ def aces(path):+ if os.path.normcase(path) == child:+ raise OSError("cannot inspect child ACL")+ return host.aces(path)++ monkeypatch.setattr(mxc_read_grants, "_package_aces", aces)+ else:+ monkeypatch.setattr(mxc_read_grants, "_identity", lambda _path: {"fileId": -1})+ assert not mxc_read_grants._pending_grant_has_no_explicit_aces(root, identity)+ assert _states() == {key: "pending"}