Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

fix(installer): verify Docker Desktop Hermes data proxies by identity (#8263)

Mike Bradley33723262ac5e
diff --git a/ods/scripts/refresh-hermes-mounts.py b/ods/scripts/refresh-hermes-mounts.py
index 46c964172..62e0cb9f3 100644
--- a/ods/scripts/refresh-hermes-mounts.py
+++ b/ods/scripts/refresh-hermes-mounts.py
@@ -4,13 +4,12 @@ import argparse
import hashlib
import json
import os
-from pathlib import Path
import re
import shlex
import stat
import subprocess
import time
-
+from pathlib import Path
FILES = {
"/opt/hermes/cli-config.yaml.example": "extensions/services/hermes/cli-config.yaml.template",
@@ -22,6 +21,7 @@ MAX_BYTES = 1024 * 1024
DESKTOP_FILE_SOURCE = re.compile(
r"/run/desktop/mnt/host/wsl/docker-desktop-bind-mounts/[A-Za-z0-9_.-]{1,128}/[a-f0-9]{64}"
)
+DESKTOP_VISIBLE_ROOT = Path("/mnt/wsl/docker-desktop-bind-mounts")
# Hash inside the container: never return credential-bearing file contents.
CONTAINER_DIGESTS = """import hashlib,json,os,stat,sys
result={}
@@ -57,6 +57,26 @@ def execute(argv, cwd, timeout=30):
raise Refused("Hermes mount verification command failed") from exc
+def desktop_data_source_matches(source, expected):
+ """Prove a Desktop directory proxy aliases this installation's live data."""
+ if not isinstance(source, str) or not DESKTOP_FILE_SOURCE.fullmatch(source):
+ return False
+ distro, digest = source.rsplit("/", 2)[1:]
+ if distro in {".", ".."}:
+ return False
+ visible = DESKTOP_VISIBLE_ROOT / distro / digest
+ try:
+ # Resolution alone follows links; equality rejects links in any ancestor.
+ if visible.resolve(strict=True) != visible or expected.resolve(strict=True) != expected:
+ return False
+ actual = os.stat(visible, follow_symlinks=False)
+ wanted = os.stat(expected, follow_symlinks=False)
+ return (stat.S_ISDIR(actual.st_mode) and stat.S_ISDIR(wanted.st_mode)
+ and (actual.st_dev, actual.st_ino) == (wanted.st_dev, wanted.st_ino))
+ except (OSError, RuntimeError):
+ return False
+
+
def host_digests(root):
result = {}
for destination, relative in FILES.items():
@@ -121,9 +141,11 @@ def refresh(root, docker, compose, run=execute):
relative = "data/hermes" if destination == "/opt/data" else FILES[destination]
source = mounts[0].get("Source")
# Desktop substitutes individual WSL file binds with a proxy inode.
- # Directory binds must still identify this installation's live data.
+ # Desktop can proxy directories too; require filesystem identity
+ # with this installation's live data, never merely a matching prefix.
if source != str(root / relative) and not (
- destination in FILES and isinstance(source, str) and DESKTOP_FILE_SOURCE.fullmatch(source)):
+ destination in FILES and isinstance(source, str) and DESKTOP_FILE_SOURCE.fullmatch(source)) and not (
+ destination == "/opt/data" and desktop_data_source_matches(source, root / relative)):
raise Refused("Hermes running mount has an unexplained source")
return item
diff --git a/ods/tests/test-phase11-hermes-mount-refresh.py b/ods/tests/test-phase11-hermes-mount-refresh.py
index 5b2f58217..e84e45a02 100644
--- a/ods/tests/test-phase11-hermes-mount-refresh.py
+++ b/ods/tests/test-phase11-hermes-mount-refresh.py
@@ -4,9 +4,9 @@ import hashlib
import importlib.util
import json
import os
-from pathlib import Path
import tempfile
import unittest
+from pathlib import Path
from unittest.mock import patch
ROOT = Path(__file__).resolve().parents[1]
@@ -180,6 +180,80 @@ class HermesMountTests(unittest.TestCase):
self.assertEqual(sleep.call_count, 2)
self.assertEqual(len(self.docker.mutations()), 1)
+ def test_desktop_directory_proxy_requires_identity_and_preserves_live_config(self):
+ proxy_root = self.root / "desktop-proxies"
+ visible = proxy_root / "Ubuntu-24.04" / ("e" * 64)
+ visible.mkdir(parents=True)
+ source = "/run/desktop/mnt/host/wsl/docker-desktop-bind-mounts/Ubuntu-24.04/" + "e" * 64
+ self.docker.inspected_mounts[-1]["Source"] = source
+ self.docker.replace(next(iter(mounts.FILES)))
+ live = self.root / "data/hermes"
+ original_config = (live / "config.yaml").read_bytes()
+ original_stat = os.stat
+ live_stat = original_stat(live)
+
+ # A foreign directory is not accepted even with the expected proxy name.
+ with patch.object(mounts, "DESKTOP_VISIBLE_ROOT", proxy_root, create=True):
+ with self.assertRaisesRegex(mounts.Refused, "unexplained source"):
+ self.docker.refresh()
+ self.assertFalse(self.docker.mutations())
+
+ # Unprivileged tests cannot create bind mounts. Model only the
+ # kernel's inode alias; path validation and the refresh stay real.
+ def bind_stat(path, *args, **kwargs):
+ if Path(path) == visible:
+ return live_stat
+ return original_stat(path, *args, **kwargs)
+
+ with patch.object(mounts.os, "stat", side_effect=bind_stat):
+ self.assertEqual(self.docker.refresh(), "refreshed")
+ self.assertEqual(len(self.docker.mutations()), 1)
+ self.assertEqual((live / "config.yaml").read_bytes(), original_config)
+ self.assertEqual(self.docker.live, original_config)
+ self.assertEqual(self.docker.other_identity, "b" * 64)
+
+ def test_desktop_directory_proxy_rejects_missing_and_malformed_paths(self):
+ self.docker.replace(next(iter(mounts.FILES)))
+ prefix = "/run/desktop/mnt/host/wsl/docker-desktop-bind-mounts/"
+ for source in [prefix + "Ubuntu-24.04/" + "f" * 64,
+ prefix + "../" + "f" * 64, prefix + "./" + "f" * 64,
+ prefix + "Ubuntu-24.04/not-a-digest", "/foreign/data", None]:
+ with self.subTest(source=source):
+ self.docker.inspected_mounts[-1]["Source"] = source
+ with patch.object(mounts, "DESKTOP_VISIBLE_ROOT", self.root / "missing", create=True):
+ with self.assertRaisesRegex(mounts.Refused, "unexplained source"):
+ self.docker.refresh()
+ self.assertFalse(self.docker.mutations())
+
+ @unittest.skipIf(os.name == "nt", "Symlink creation needs Windows privileges")
+ def test_desktop_directory_proxy_rejects_symlink_and_symlink_ancestor(self):
+ proxy_root = self.root / "desktop-proxies"
+ visible = proxy_root / "Ubuntu-24.04" / ("e" * 64)
+ visible.parent.mkdir(parents=True)
+ live = self.root / "data/hermes"
+ visible.symlink_to(live)
+ source = "/run/desktop/mnt/host/wsl/docker-desktop-bind-mounts/Ubuntu-24.04/" + "e" * 64
+ self.docker.inspected_mounts[-1]["Source"] = source
+ self.docker.replace(next(iter(mounts.FILES)))
+ with patch.object(mounts, "DESKTOP_VISIBLE_ROOT", proxy_root, create=True):
+ with self.assertRaisesRegex(mounts.Refused, "unexplained source"):
+ self.docker.refresh()
+ visible.unlink()
+ visible.mkdir()
+ real_parent = proxy_root / "real-distro"
+ visible.parent.rename(real_parent)
+ visible.parent.symlink_to(real_parent)
+ original_stat = os.stat
+ live_stat = original_stat(live)
+
+ def bind_stat(path, *args, **kwargs):
+ return live_stat if Path(path) == visible else original_stat(path, *args, **kwargs)
+
+ with patch.object(mounts.os, "stat", side_effect=bind_stat):
+ with self.assertRaisesRegex(mounts.Refused, "unexplained source"):
+ self.docker.refresh()
+ self.assertFalse(self.docker.mutations())
+
def test_unhealthy_or_timed_out_startup_fails_without_retry(self):
self.docker.replace(next(iter(mounts.FILES)))
self.docker.startup_health = ["unhealthy"]