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(models): restore discovery and activation proof across runtime user IDs (#8236)

Mike Bradley3e0404385039
diff --git a/ods/bin/ods-host-agent.py b/ods/bin/ods-host-agent.py
index bddbee34b..a1faf23dd 100755
--- a/ods/bin/ods-host-agent.py
+++ b/ods/bin/ods-host-agent.py
@@ -15914,6 +15914,9 @@ class AgentHandler(BaseHTTPRequestHandler):
"verifiedAt": str(final_runtime_proof.get("verifiedAt") or _iso_now()),
**({'modelTransactionId':pixel_transaction.id} if pixel_transaction is not None else {}),
},
+ # Non-secret completion proof read by dashboard-api, whose
+ # container UID can differ from the installing host user.
+ mode=0o644,
)
committed = True # system state is committed before the response write
if pixel_transaction is not None:
diff --git a/ods/extensions/services/dashboard-api/tests/test_model_activate.py b/ods/extensions/services/dashboard-api/tests/test_model_activate.py
index 259f0af72..f5285336e 100644
--- a/ods/extensions/services/dashboard-api/tests/test_model_activate.py
+++ b/ods/extensions/services/dashboard-api/tests/test_model_activate.py
@@ -6,7 +6,9 @@ import importlib.util
import http.client
import io
import json
+import os
import shutil
+import stat
import struct
import subprocess
import sys
@@ -6928,6 +6930,80 @@ class TestModelActivateRollback:
assert "OpenCode config is malformed" in handler.parse_response()["error"]
assert env_path.read_text(encoding="utf-8") == env_text
+ @pytest.mark.skipif(os.name == "nt", reason="POSIX receipt permissions")
+ @pytest.mark.parametrize("existing_receipt", [False, True])
+ def test_activation_receipt_is_readable_across_runtime_users(
+ self, tmp_path, monkeypatch, existing_receipt,
+ ):
+ install_dir, env_path, *_ = _write_model_activation_fixture(tmp_path)
+ receipt_path = install_dir / "data" / "model-activation-receipt.json"
+ receipt_path.parent.mkdir(exist_ok=True)
+ if existing_receipt:
+ receipt_path.write_text('{"status":"old"}', encoding="utf-8")
+ receipt_path.chmod(0o600)
+ env_path.chmod(0o600)
+ private_path = install_dir / "data" / "private-test.json"
+ _mod._atomic_write_json(private_path, {"secret": "fixture-only"})
+ private_before = private_path.read_bytes()
+ monkeypatch.setattr(_mod, "INSTALL_DIR", install_dir)
+ monkeypatch.setattr(_mod, "_compose_restart_llama_server", lambda _env: None)
+ monkeypatch.setattr(_mod, "_wait_for_model_readiness", _mock_verified_readiness)
+ handler = _ResponseHandler()
+ previous_umask = os.umask(0o077)
+ try:
+ _mod.AgentHandler._do_model_activate(handler, "target-model")
+ finally:
+ os.umask(previous_umask)
+
+ assert handler.response_code == 200, handler.parse_response()
+ receipt = json.loads(receipt_path.read_text(encoding="utf-8"))
+ assert receipt["status"] == "complete"
+ assert receipt["contextVerified"] is True
+ assert receipt["modelId"] == "target-model"
+ assert stat.S_IMODE(receipt_path.stat().st_mode) == 0o644
+ assert stat.S_IMODE(env_path.stat().st_mode) == 0o600
+ assert stat.S_IMODE(private_path.stat().st_mode) == 0o600
+ assert private_path.read_bytes() == private_before
+ assert set(receipt) <= {
+ "schema", "status", "modelId", "llmModel", "ggufFile",
+ "runtimeModelId", "contextLength", "contextVerified",
+ "gpuAssignment", "consumers", "verifiedAt", "modelTransactionId",
+ }
+
+ @pytest.mark.skipif(os.name == "nt", reason="POSIX receipt permissions")
+ @pytest.mark.parametrize("previous_mode", [0o600, 0o644])
+ def test_activation_receipt_rollback_preserves_prior_custody(
+ self, tmp_path, monkeypatch, previous_mode,
+ ):
+ install_dir, _, *_ = _write_model_activation_fixture(tmp_path)
+ receipt_path = install_dir / "data" / "model-activation-receipt.json"
+ receipt_path.parent.mkdir(exist_ok=True)
+ receipt_path.write_text('{"status":"old"}', encoding="utf-8")
+ receipt_path.chmod(previous_mode)
+ previous = receipt_path.stat()
+ original_write = _mod._atomic_write_json
+
+ def fail_after_receipt_write(path, value, *args, **kwargs):
+ original_write(path, value, *args, **kwargs)
+ if path == receipt_path:
+ assert stat.S_IMODE(path.stat().st_mode) == 0o644
+ raise OSError("injected fault after receipt replacement")
+
+ monkeypatch.setattr(_mod, "INSTALL_DIR", install_dir)
+ monkeypatch.setattr(_mod, "_compose_restart_llama_server", lambda _env: None)
+ monkeypatch.setattr(_mod, "_wait_for_model_readiness", _mock_verified_readiness)
+ monkeypatch.setattr(_mod, "_atomic_write_json", fail_after_receipt_write)
+ handler = _ResponseHandler()
+ _mod.AgentHandler._do_model_activate(handler, "target-model")
+
+ assert handler.response_code == 500
+ assert handler.parse_response()["rolled_back"] is True
+ assert receipt_path.read_text(encoding="utf-8") == '{"status":"old"}'
+ after = receipt_path.stat()
+ assert (after.st_mode, after.st_uid, after.st_gid) == (
+ previous.st_mode, previous.st_uid, previous.st_gid,
+ )
+
def test_cli_activation_metadata_persists_tier_and_bounded_context(
self, tmp_path, monkeypatch,
):
diff --git a/ods/installers/lib/dashboard-data.sh b/ods/installers/lib/dashboard-data.sh
index d05946f57..9e49d3bbf 100644
--- a/ods/installers/lib/dashboard-data.sh
+++ b/ods/installers/lib/dashboard-data.sh
@@ -21,13 +21,22 @@ _ods_dashboard_lock_helper_image() {
}
ods_prepare_dashboard_data() {
- local install_dir="$1" rootless="$2" target private_store metadata group mode lock lock_helper use_docker_repair=false
+ local install_dir="$1" rootless="$2" target private_store models metadata group mode lock lock_helper use_docker_repair=false
lock_helper="${SCRIPT_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)}/installers/lib/dashboard-lock.py"
target="$install_dir/data"
if [[ ! -d "$target" || -L "$target" ]]; then
echo "[error] Dashboard data must be a real directory: $target" >&2
return 1
fi
+ # The API inventories weights through this bind mount. Grant only its
+ # runtime group directory access; model file permissions stay untouched.
+ models="$target/models"
+ if [[ -e "$models" || -L "$models" ]]; then
+ [[ -d "$models" && ! -L "$models" ]] || {
+ echo "[error] Model data must be a real directory: $models" >&2
+ return 1
+ }
+ fi
# Create as the installer, without sudo. An existing unreadable lock can be
# inspected with O_PATH; neither creation nor repair follows a replacement
# symlink. Privileged repair below keeps the same flock inode.
@@ -51,6 +60,11 @@ ods_prepare_dashboard_data() {
[[ "$metadata" == 1000:1000 ]] || use_docker_repair=true
fi
done
+ if [[ -d "$models" ]]; then
+ metadata=$(stat -c '%g:%a' "$models") || return 1
+ IFS=: read -r group mode <<< "$metadata"
+ [[ "$group" == 1000 ]] && (( (8#$mode & 8#050) == 8#050 )) || use_docker_repair=true
+ fi
fi
if "$use_docker_repair"; then
# Host chgrp 1000 is not container GID 1000 in a rootless namespace.
@@ -61,6 +75,9 @@ ods_prepare_dashboard_data() {
docker_run run --rm --network none --user 0:0 \
--mount "type=bind,src=$target,dst=/data" \
"$ODS_ROOTLESS_HELPER_IMAGE" sh -ec '
+ if test -e /data/models || test -L /data/models; then
+ test -d /data/models && test ! -L /data/models || exit 1
+ fi
chgrp 1000 /data
chmod g+rwx /data
test "$(stat -c %g /data)" = 1000
@@ -75,6 +92,13 @@ ods_prepare_dashboard_data() {
test "$(stat -c %u:%g "$private_store")" = 1000:1000
fi
done
+ if test -d /data/models; then
+ chgrp -h 1000 /data/models
+ chmod g+rx /data/models
+ test "$(stat -c %g /data/models)" = 1000
+ mode=$(stat -c %a /data/models)
+ test "$((0$mode & 050))" = 40
+ fi
' || return 1
docker_run run --rm -i --network none --user 0:0 --read-only \
--security-opt no-new-privileges \
@@ -93,6 +117,20 @@ ods_prepare_dashboard_data() {
metadata=$(stat -c '%g:%a' "$target") || return 1
IFS=: read -r group mode <<< "$metadata"
[[ "$group" == 1000 ]] && (( (8#$mode & 8#070) == 8#070 )) || return 1
+ if [[ -e "$models" || -L "$models" ]]; then
+ [[ -d "$models" && ! -L "$models" ]] || return 1
+ metadata=$(stat -c '%g:%a' "$models") || return 1
+ IFS=: read -r group mode <<< "$metadata"
+ if [[ "$group" != 1000 ]]; then
+ _ods_dashboard_mutate chgrp -h 1000 "$models" || return 1
+ fi
+ if (( (8#$mode & 8#050) != 8#050 )); then
+ _ods_dashboard_mutate chmod g+rx "$models" || return 1
+ fi
+ metadata=$(stat -c '%g:%a' "$models") || return 1
+ IFS=: read -r group mode <<< "$metadata"
+ [[ "$group" == 1000 ]] && (( (8#$mode & 8#050) == 8#050 )) || return 1
+ fi
for private_store in "$target/pixel-chat-results" "$target/pixel-images"; do
if [[ -e "$private_store" || -L "$private_store" ]]; then
[[ -d "$private_store" && ! -L "$private_store" ]] || return 1
diff --git a/ods/scripts/prepare-extension-manifests.py b/ods/scripts/prepare-extension-manifests.py
index f16c40c2a..f38d98408 100644
--- a/ods/scripts/prepare-extension-manifests.py
+++ b/ods/scripts/prepare-extension-manifests.py
@@ -10,7 +10,7 @@ This script only fixes the runtime manifest bind-mount surface:
- services root: 0755
- service child dirs: traversal only (add x, remove g/o w)
- public manifest files (manifest.yaml/.yml/.json): 0644
- - config/: traversal only; the two bundled Dashboard catalogs: 0644
+ - config/: traversal only; the named bundled Dashboard/model metadata: 0644
- templates/: 0755 for enumeration; named bundled templates: 0644
It validates the full scoped tree before any mutation, refuses symlinks,
@@ -31,7 +31,10 @@ from pathlib import Path
MANIFEST_NAMES = ("manifest.yaml", "manifest.yml", "manifest.json")
PUBLIC_DASHBOARD_FILES = {
- "config": ("core-service-ids.json", "extensions-catalog.json"),
+ "config": (
+ "core-service-ids.json", "extensions-catalog.json",
+ "model-library.json", "llama-cpp-architectures.json",
+ ),
"templates": (
"ai-coding-workspace.yaml", "chat-playground.yaml", "creative-studio.yaml",
"developer-homelab.yaml", "llm-platform.yaml", "ml-data-workshop.yaml",
diff --git a/ods/tests/test_dashboard_data_permissions.py b/ods/tests/test_dashboard_data_permissions.py
index 17b8cfeae..13f050e78 100644
--- a/ods/tests/test_dashboard_data_permissions.py
+++ b/ods/tests/test_dashboard_data_permissions.py
@@ -52,6 +52,125 @@ def _load_permission_fixture_source(path, namespace, names, *, handler_methods=(
@unittest.skipUnless(hasattr(os, "geteuid") and os.geteuid() == 0, "requires Linux root to drop numeric IDs")
class DashboardDataPermissions(unittest.TestCase):
+ def test_real_helper_grants_runtime_group_without_touching_children(self):
+ with tempfile.TemporaryDirectory() as temporary:
+ install = Path(temporary)
+ install.chmod(0o755)
+ data = install / "data"
+ data.mkdir(mode=0o700)
+ os.chown(data, 1001, 2001)
+ models = data / "models"
+ models.mkdir(mode=0o700)
+ os.chown(models, 1001, 2001)
+ public = models / "public.gguf"
+ public.write_bytes(b"public-weights")
+ public.chmod(0o644)
+ os.chown(public, 1001, 2001)
+ private_weight = models / "private.gguf"
+ private_weight.write_bytes(b"private-weights")
+ private_weight.chmod(0o600)
+ os.chown(private_weight, 1001, 2001)
+ private = models / "private"
+ private.mkdir(mode=0o700)
+ os.chown(private, 1001, 2001)
+ nested = private / "nested"
+ nested.mkdir(mode=0o700)
+ os.chown(nested, 1001, 2001)
+ secret = nested / "turn.json"
+ secret.write_bytes(b"private-chat")
+ secret.chmod(0o600)
+ os.chown(secret, 1001, 2001)
+ outside = install / "outside"
+ outside.write_bytes(b"untouched")
+ os.chown(outside, 2001, 2001)
+ linked = private / "external"
+ linked.symlink_to(outside)
+ snapshot = {}
+ for path in (public, private_weight, private, nested, secret, linked):
+ info = path.lstat()
+ snapshot[path] = (info.st_uid, info.st_gid, info.st_mode & 0o777,
+ path.read_bytes() if path.is_file() and not path.is_symlink() else None)
+ script = ('set -euo pipefail\n'
+ + (ROOT / "installers/lib/dashboard-data.sh").read_text() + r'''
+ods_sudo_available() { return 0; }
+ods_sudo() { "$@"; }
+ods_prepare_dashboard_data "$INSTALL_DIR" false
+''')
+ environment = dict(os.environ, SCRIPT_DIR=str(ROOT), INSTALL_DIR=str(install))
+ probe = '''
+import os, pathlib
+data = pathlib.Path(os.environ["INSTALL_DIR"]) / "data"
+os.setgroups([])
+os.setgid(1000)
+os.setuid(1000)
+assert os.geteuid() == os.getegid() == 1000
+assert sorted(p.name for p in (data / "models").iterdir()) == ["private", "private.gguf", "public.gguf"]
+assert (data / "models/public.gguf").read_bytes() == b"public-weights"
+for denied in (data / "models/private.gguf", data / "models/private", data / "models/private/nested",
+ data / "models/private/nested/turn.json"):
+ try:
+ if denied.is_dir():
+ list(denied.iterdir())
+ else:
+ denied.read_bytes()
+ except PermissionError:
+ pass
+ else:
+ raise AssertionError(f"UID1000 accessed private path {denied}")
+'''
+ denied = subprocess.run(["python3", "-c", probe], env=environment,
+ capture_output=True, text=True, check=False)
+ self.assertNotEqual(denied.returncode, 0)
+ self.assertIn("PermissionError", denied.stderr)
+ for attempt in range(2):
+ subprocess.run(["bash", "-c", script], env=environment, check=True)
+ subprocess.run(["python3", "-c", probe], env=environment, check=True)
+ self.assertEqual((data.stat().st_uid, data.stat().st_gid), (1001, 1000))
+ self.assertEqual(data.stat().st_mode & 0o070, 0o070)
+ self.assertEqual((models.stat().st_uid, models.stat().st_gid), (1001, 1000))
+ self.assertEqual(models.stat().st_mode & 0o050, 0o050)
+ for path, expected in snapshot.items():
+ info = path.lstat()
+ self.assertEqual((info.st_uid, info.st_gid, info.st_mode & 0o777), expected[:3], path)
+ if expected[3] is not None:
+ self.assertEqual(path.read_bytes(), expected[3], path)
+ self.assertEqual(outside.stat().st_uid, 2001)
+ self.assertEqual(outside.read_bytes(), b"untouched")
+
+ def test_rejects_modeldir_symlink_or_file_before_mutation(self):
+ for kind in ("symlink", "file"):
+ with self.subTest(kind=kind), tempfile.TemporaryDirectory() as temporary:
+ install = Path(temporary)
+ install.chmod(0o755)
+ data = install / "data"
+ data.mkdir(mode=0o700)
+ os.chown(data, 1001, 2001)
+ models = data / "models"
+ if kind == "symlink":
+ target = install / "elsewhere"
+ target.mkdir()
+ models.symlink_to(target)
+ else:
+ models.write_bytes(b"not-a-directory")
+ before = (data.stat().st_uid, data.stat().st_gid, data.stat().st_mode & 0o777)
+ script = ('set -euo pipefail\n'
+ + (ROOT / "installers/lib/dashboard-data.sh").read_text() + r'''
+ods_sudo_available() { return 0; }
+ods_sudo() { "$@"; }
+ods_prepare_dashboard_data "$INSTALL_DIR" false
+''')
+ result = subprocess.run(["bash", "-c", script],
+ env=dict(os.environ, SCRIPT_DIR=str(ROOT), INSTALL_DIR=str(install)),
+ capture_output=True, text=True, check=False)
+ self.assertNotEqual(result.returncode, 0)
+ self.assertIn("Model data must be a real directory", result.stderr)
+ self.assertEqual((data.stat().st_uid, data.stat().st_gid, data.stat().st_mode & 0o777), before)
+ self.assertFalse((data / ".extensions-lock").exists())
+ if kind == "symlink":
+ self.assertTrue(models.is_symlink())
+ else:
+ self.assertEqual(models.read_bytes(), b"not-a-directory")
+
def test_private_env_mode_fallback_uses_owner_host_without_chmod(self):
with tempfile.TemporaryDirectory() as temporary:
install = Path(temporary)
@@ -279,6 +398,13 @@ with sqlite3.connect(database) as db:
data = install / "data"
data.mkdir(mode=0o770)
os.chown(data, 1001, 1000)
+ models = data / "models"
+ models.mkdir(mode=0o700)
+ os.chown(models, 1001, 2001)
+ weight = models / "model.gguf"
+ weight.write_bytes(b"retained-weights")
+ weight.chmod(0o644)
+ os.chown(weight, 1001, 2001)
images = data / "pixel-images"
images.mkdir(mode=0o700)
saved = images / "images.sqlite3"
@@ -309,6 +435,11 @@ ods_prepare_dashboard_data "$INSTALL_DIR" "$ROOTLESS"
subprocess.run(["bash", "-c", script], check=True,
env=dict(os.environ, SCRIPT_DIR=str(ROOT), INSTALL_DIR=str(install), ROOTLESS=rootless))
self.assertTrue((install / "helper-used").exists())
+ self.assertEqual((models.stat().st_uid, models.stat().st_gid, models.stat().st_mode & 0o777),
+ (1001, 1000, 0o750))
+ self.assertEqual((weight.stat().st_uid, weight.stat().st_gid, weight.stat().st_mode & 0o777),
+ (1001, 2001, 0o644))
+ self.assertEqual(weight.read_bytes(), b"retained-weights")
self.assertEqual((data.stat().st_uid, data.stat().st_gid), (1001, 1000))
self.assertEqual((images.stat().st_uid, images.stat().st_gid, images.stat().st_mode & 0o777),
(1000, 1000, 0o700))
diff --git a/ods/tests/test_public_extension_manifest_permissions.py b/ods/tests/test_public_extension_manifest_permissions.py
index aa81e4ece..1adb501a8 100644
--- a/ods/tests/test_public_extension_manifest_permissions.py
+++ b/ods/tests/test_public_extension_manifest_permissions.py
@@ -129,6 +129,28 @@ def _make_dashboard_files(root: Path) -> dict[str, Path]:
class PrepareExtensionManifestsTests(unittest.TestCase):
+ def test_factory_model_discovery_metadata_under_private_umask(self) -> None:
+ names = ("model-library.json", "llama-cpp-architectures.json")
+ with tempfile.TemporaryDirectory() as td:
+ root = Path(td)
+ paths = _make_tree(root, 0o077)
+ paths.update(_make_dashboard_files(root))
+ # Independent factory inputs: a missing allowlist entry must fail,
+ # even though the general fixture follows PUBLIC_DASHBOARD_FILES.
+ with _Umask(0o077):
+ for name in names:
+ leaf = root / "config" / name
+ leaf.write_bytes((_HERE.parent.parent / "config" / name).read_bytes())
+ self.assertEqual(_mode(leaf), 0o600)
+ before = {name: (root / "config" / name).read_bytes() for name in names}
+ self._run(root)
+ for name in names:
+ self.assertEqual(_mode(root / "config" / name), 0o644)
+ self.assertEqual((root / "config" / name).read_bytes(), before[name])
+ self.assertEqual(_mode(root / "config/private.json"), 0o600)
+ self.assertEqual(_mode(root / "config/n8n"), 0o700)
+ self.assertEqual(_mode(root / ".env"), 0o600)
+
def test_dashboard_public_scope_and_private_preservation(self) -> None:
with tempfile.TemporaryDirectory() as td:
root = Path(td)