Skip to content

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.

Studio: registered model folders and HF cache on /run/media drives stay silent for the owner (#14646)

Daniel Han99d9e8e90cb0
diff --git a/studio/backend/core/inference/tool_path_approval.py b/studio/backend/core/inference/tool_path_approval.py
index 299b2ab23ad..e3027bacbef 100644
--- a/studio/backend/core/inference/tool_path_approval.py
+++ b/studio/backend/core/inference/tool_path_approval.py
@@ -159,6 +159,12 @@ _WRITABLE_RUNTIME_SUBTREES = tuple(
)
+# User-storage mounts: a configured root on one (model folder, HF cache) still covers paths under it.
+_USER_STORAGE_MOUNTS = tuple(
+ os.path.normcase(part) for part in ("/run/media", "/run/mount", "/run/host")
+)
+
+
# Pseudo-devices a command legitimately writes to (`2> /dev/null`); the rest of /dev is read-silent but not
# write-silent.
_WRITE_SILENT_DEVICE_NODES = (
@@ -491,6 +497,30 @@ def _contained_in(candidate: str, roots) -> bool:
return any(candidate == root or candidate.startswith(root + os.sep) for root in roots)
+def _uncovered_runtime_subtree(path: str, roots) -> bool:
+ """*path* is in a writable runtime subtree that no silent root on user storage covers."""
+ return _contained_in(path, _WRITABLE_RUNTIME_SUBTREES) and not _contained_in(
+ path, tuple(root for root in roots if _contained_in(root, _USER_STORAGE_MOUNTS))
+ )
+
+
+def _follows_its_own_root_link(candidate: str, resolved: str, roots) -> bool:
+ """*candidate* sits under a root that is itself a link INTO a runtime subtree (`/dev/stdout` ->
+ `/dev/fd/1`), and *resolved* stays inside that link's target. A root linking to a broad directory
+ (`/var/run` -> `/run`) exempts nothing, or it would cover every runtime subtree under it."""
+ for root in roots:
+ if not _contained_in(candidate, (root,)):
+ continue
+ target = _resolved_fs_text(root)
+ if (
+ target != root
+ and _contained_in(target, _WRITABLE_RUNTIME_SUBTREES)
+ and _contained_in(resolved, (target,))
+ ):
+ return True
+ return False
+
+
def _resolved_fs_text(text: str) -> str:
"""*text* with its symlinked components resolved, folded the way the roots are."""
try:
@@ -574,13 +604,11 @@ def _path_needs_approval(text, *, writing: bool = False) -> bool:
# A writable subtree of an otherwise system root. `/dev/shm` and `/run/user/<uid>` are tmpfs the
# user's own processes write into, so a read there is a read of somebody's data, not of the
# machine's configuration. Checked before containment, so the enclosing root cannot cover them.
- if any(
- candidate == root or candidate.startswith(root + os.sep)
- for root in _WRITABLE_RUNTIME_SUBTREES
- ):
- return True
+ # Exception: a silent root INSIDE /run/media, /run/mount or /run/host covers it; `/run` itself never does.
read_roots, write_roots = _silent_roots()
roots = write_roots if writing else read_roots
+ if _uncovered_runtime_subtree(candidate, roots):
+ return True
if not _contained_in(candidate, roots):
return True
# On a managed install the shared projects and tmp bases hold every account's own subtree, so a
@@ -594,10 +622,24 @@ def _path_needs_approval(text, *, writing: bool = False) -> bool:
# allowed rather than on every token. A link planted between this and the open is not covered;
# that TOCTOU gap is the OS sandbox's to close.
resolved = _resolved_fs_text(candidate)
- # Against the RESOLVED roots as well: a root that is itself a symlink (a sandbox under a
- # macOS `/tmp`, `/dev/stdout`) would otherwise reject everything under it.
- if resolved != candidate and not _contained_in(resolved, roots):
- if not _contained_in(resolved, tuple(_resolved_fs_text(r) for r in roots)):
+ if resolved != candidate:
+ lexical_roots = roots
+ # Against the RESOLVED roots as well: a root that is itself a symlink (a sandbox under a
+ # macOS `/tmp`, `/dev/stdout`, `/models -> /run/media/...`) would otherwise reject everything under it.
+ if not _contained_in(resolved, roots) or _contained_in(
+ resolved, _WRITABLE_RUNTIME_SUBTREES
+ ):
+ roots = roots + tuple(_resolved_fs_text(r) for r in roots)
+ if not _contained_in(resolved, roots):
+ return True
+ # Same carve-out for the target (a link may point into /run/user, held by the broad `/run` root).
+ # Exempt only when it follows its own root link: macOS `/dev/stdout` -> `/dev/fd/1` (`2> /dev/stderr`).
+ if _uncovered_runtime_subtree(resolved, roots) and not _follows_its_own_root_link(
+ candidate, resolved, lexical_roots
+ ):
+ return True
+ # And the account segment: a link can hide `Accounts/<other>/...` behind an innocent name.
+ if _names_a_managed_account_subtree(resolved):
return True
# Inside a silent root, so only a credential underneath one still asks. Running the (superlinear) credential
# scan only here, rather than on every operand, keeps the ordering guarantee at a fraction of the cost:
diff --git a/studio/backend/tests/test_managed_tool_confinement_hardening.py b/studio/backend/tests/test_managed_tool_confinement_hardening.py
index eb62da52e72..1ee9f596532 100644
--- a/studio/backend/tests/test_managed_tool_confinement_hardening.py
+++ b/studio/backend/tests/test_managed_tool_confinement_hardening.py
@@ -228,3 +228,80 @@ def test_macos_managed_profile_filters_network_mach_sysctl_and_shm(tmp_path):
# The account's own sockets (multiprocessing) still work.
assert f'(allow network-outbound (remote unix-socket (subpath "{writable}")))' in lines
assert f'(allow network-bind (local unix-socket (subpath "{writable}")))' in lines
+
+
[email protected](os.sep != "/", reason = "POSIX paths")
+def test_registered_folders_on_removable_mounts_stay_silent(monkeypatch):
+ """A model folder registered on a /run/media drive, or an HF cache there, is not a new prompt."""
+ models = "/run/media/alice/SSD/models"
+ hf_home = "/run/media/alice/SSD/hf"
+ monkeypatch.setattr(
+ path_gate,
+ "_silent_roots",
+ lambda: (("/run", models, hf_home), (hf_home,)),
+ )
+ monkeypatch.setattr(path_gate, "_resolved_fs_text", lambda text: text)
+ assert path_gate._path_needs_approval(f"{models}/x.gguf") is False
+ assert path_gate._path_needs_approval(f"{hf_home}/hub/models--a--b/blob") is False
+ assert path_gate._path_needs_approval(f"{hf_home}/hub/new.bin", writing = True) is False
+ # The rest of the drive, a write into a read-only model folder, a credential in the HF home,
+ # and the other user-storage mounts still ask; `/run` itself never covers them.
+ assert path_gate._path_needs_approval("/run/media/alice/SSD/notes.txt") is True
+ assert path_gate._path_needs_approval("/run/media/alice/SSD/models-old/x") is True
+ assert path_gate._path_needs_approval(f"{models}/x.gguf", writing = True) is True
+ assert path_gate._path_needs_approval(f"{hf_home}/token") is True
+ assert path_gate._path_needs_approval("/run/host/etc/hostname") is True
+ assert path_gate._path_needs_approval("/run/user/1000/bus") is True
+ assert path_gate._path_needs_approval("/dev/sda") is True
+ # A link inside the registered folder pointing at the rest of the drive or at /run/user still asks.
+ links = {
+ f"{models}/link": "/run/media/alice/SSD/private/notes.txt",
+ f"{models}/bus": "/run/user/1000/bus",
+ f"{models}/same": f"{models}/x.gguf",
+ }
+ monkeypatch.setattr(path_gate, "_resolved_fs_text", lambda text: links.get(text, text))
+ assert path_gate._path_needs_approval(f"{models}/link") is True
+ assert path_gate._path_needs_approval(f"{models}/bus") is True
+ assert path_gate._path_needs_approval(f"{models}/same") is False
+ # A link that hides another account's subtree behind an innocent name still asks.
+ links[f"{models}/public"] = f"{models}/Accounts/bob/Projects/a.txt"
+ assert path_gate._path_needs_approval(f"{models}/public") is True
+ # A model folder registered through a symlink onto the drive stays silent, as it was before.
+ monkeypatch.setattr(path_gate, "_silent_roots", lambda: (("/run", "/models"), ()))
+ links.clear()
+ links["/models"] = models
+ links["/models/x.gguf"] = f"{models}/x.gguf"
+ links["/models/link"] = "/run/media/alice/SSD/private/notes.txt"
+ assert path_gate._path_needs_approval("/models/x.gguf") is False
+ assert path_gate._path_needs_approval("/models/link") is True
+
+
[email protected](os.sep != "/", reason = "POSIX paths")
+def test_std_stream_writes_stay_silent_when_they_resolve_into_dev_fd(monkeypatch):
+ """macOS resolves /dev/stdout to /dev/fd/1; a `2> /dev/stderr` redirect must not become a prompt."""
+ monkeypatch.setattr(
+ path_gate,
+ "_silent_roots",
+ lambda: (
+ ("/dev/stdout", "/dev/stderr", "/run", "/var/run", "/models"),
+ ("/dev/stdout", "/dev/stderr"),
+ ),
+ )
+ links = {
+ "/dev/stdout": "/dev/fd/1",
+ "/dev/stderr": "/dev/fd/2",
+ "/models": "/run/media/alice/SSD/models",
+ "/models/bus": "/run/user/1000/bus",
+ "/var/run": "/run",
+ "/var/run/user/1000/bus": "/run/user/1000/bus",
+ "/var/run/media/alice/SSD/notes.txt": "/run/media/alice/SSD/notes.txt",
+ }
+ monkeypatch.setattr(path_gate, "_resolved_fs_text", lambda text: links.get(text, text))
+ assert path_gate._path_needs_approval("/dev/stdout", writing = True) is False
+ assert path_gate._path_needs_approval("/dev/stderr", writing = True) is False
+ assert path_gate._path_needs_approval("/dev/stdout") is False
+ # A link under a symlinked root that leaves that root's own target still asks.
+ assert path_gate._path_needs_approval("/models/bus") is True
+ # Nor does a root that links to a broad directory hand out the runtime subtrees under it.
+ assert path_gate._path_needs_approval("/var/run/user/1000/bus") is True
+ assert path_gate._path_needs_approval("/var/run/media/alice/SSD/notes.txt") is True