diff --git a/.agents/mind/gotchas.md b/.agents/mind/gotchas.md index f0ce64b..53f687c 100644 --- a/.agents/mind/gotchas.md +++ b/.agents/mind/gotchas.md @@ -16,5 +16,6 @@ - [2026-11-15] npm's local `--prefix` install writes `node_modules/.bin`; the Linux worker's `.local/bin/codex` requires a user-owned global prefix. The regression installs an offline synthetic package with real npm. - [2026-11-15] Compare copied/archived license bytes with source bytes. `read_text()` normalizes Windows CRLF while ZIP reads retain it; explicit LF/CRLF fixtures cover both checkouts. - [2026-11-15] Do not assume a bundled proxy or host-driven updates for LXC deployments. Use the operator's existing proxy and an in-container updater; preserve the public Host/Origin when the private backend uses HTTP. +- [2026-11-15] A build-owned source/output cannot supply code later executed as root. Capture updater bytes from the verified archive before build handoff and atomically install them in the root-owned release, excluding build-supplied updater files. ## Archive diff --git a/.agents/mind/index.md b/.agents/mind/index.md index 90000ae..6e0b7a1 100644 --- a/.agents/mind/index.md +++ b/.agents/mind/index.md @@ -11,5 +11,5 @@ - Desktop/Stream Deck retain `snapshot.json` (D9); the optional authenticated server/worker transport is separate (D14). ## Table of Contents -- `gotchas.md` — 11 entries — CLI login, existing-proxy/in-container updates, publication checks and Avalonia tooltips. +- `gotchas.md` — 12 entries — CLI login, in-container updater trust boundaries, publication checks and Avalonia tooltips. - `architecture.md` — 7 entries — Fleet analysis, attribution, migration, authentication, durability, Proxmox and publication. diff --git a/docs/proxmox.md b/docs/proxmox.md index 0626846..649add1 100644 --- a/docs/proxmox.md +++ b/docs/proxmox.md @@ -54,6 +54,8 @@ and activates the update **inside this container**. It does not call `pct`, cont host or require the original host checkout. It preserves the CT role, service keys, settings, CLI profiles and history. Official provider CLIs keep their own update/login lifecycle. The updater is included in each binary release, so switching/rolling back releases updates it too. +Its root-executed code comes directly from the verified source archive; build-user modifications +to extracted source or build output cannot replace the installed updater. ```bash finaltoken-update --dry-run diff --git a/tools/proxmox/AGENTS.md b/tools/proxmox/AGENTS.md index 849cfbf..a65bdbf 100644 --- a/tools/proxmox/AGENTS.md +++ b/tools/proxmox/AGENTS.md @@ -1,2 +1,2 @@ # Proxmox deployment tools -Host creates only a new explicitly selected CT. Updates run inside the CT with finaltoken-update and require this installer's marker. Use the operator's existing reverse proxy; new installs expose private HTTP :5187 and never install a proxy. Legacy migration may change only the known listener and disable finaltoken-proxy.service; preserve keys/data and unrelated services. Never destroy a CT, alter host package repositories or read provider credentials. Builds run inside the CT as an isolated user. Keep commands as argv lists. Test with `python -B tools/proxmox/test_installer.py` and `python -B tools/proxmox/test_update.py`. +Host creates only a new explicitly selected CT. Updates run inside the CT with finaltoken-update and require this installer's marker. Use the operator's existing reverse proxy; new installs expose private HTTP :5187 and never install a proxy. Legacy migration may change only the known listener and disable finaltoken-proxy.service; preserve keys/data and unrelated services. Never destroy a CT, alter host package repositories or read provider credentials. Builds run inside the CT as an isolated user; root updater code must come directly from the verified archive, never build-writable source/output. Keep commands as argv lists. Test with `python -B tools/proxmox/test_installer.py` and `python -B tools/proxmox/test_update.py`. diff --git a/tools/proxmox/guest.py b/tools/proxmox/guest.py index c40a295..53ff48a 100644 --- a/tools/proxmox/guest.py +++ b/tools/proxmox/guest.py @@ -200,6 +200,17 @@ def configure(meta, enrollment_key=None): def build(archive, meta): + # The updater runs as root: capture it from the verified, root-owned archive, + # never from source or output writable by the unprivileged compiler. + with tarfile.open(archive, "r:gz") as bundle: + try: + member = bundle.getmember("tools/lxc_update.py") + except KeyError: + raise ValueError("Source archive has no updater script") from None + if not member.isfile() or member.size > 16 * 1024 * 1024: + raise ValueError("Source archive has an invalid updater script") + with bundle.extractfile(member) as stream: + trusted_updater = stream.read().decode("utf-8") entry = user("finaltoken-build", BUILD) temporary = Path(tempfile.mkdtemp(prefix="build-", dir=BUILD)) try: @@ -217,12 +228,11 @@ def build(archive, meta): if meta["role"] != "worker" and not (release / "web/wwwroot/index.html").is_file(): shutil.copytree(source / "src/FinalToken.Web/wwwroot", release / "web/wwwroot", dirs_exist_ok=True) shutil.copy2(source / "LICENSE", release / "LICENSE") - shutil.copy2(source / "tools/lxc_update.py", release / "finaltoken-update") destination = APP / "releases" / (meta["sourceSha256"][:16] + "-" + uuid.uuid4().hex[:8]) destination.parent.mkdir(parents=True, exist_ok=True) APP.chmod(0o755) destination.parent.chmod(0o755) - shutil.copytree(release, destination) + shutil.copytree(release, destination, ignore=shutil.ignore_patterns("finaltoken-update")) destination.chmod(0o755) for path in destination.rglob("*"): path.chmod(0o755 if path.is_dir() else 0o644) @@ -236,7 +246,7 @@ def build(archive, meta): if header[:4] != b"\x7fELF" or header[18:20] != b"\x3e\x00": raise ValueError("Published executable is not a Linux x64 ELF binary") executable.chmod(0o755) - (destination / "finaltoken-update").chmod(0o755) + atomic(destination / "finaltoken-update", trusted_updater, 0o755) return destination finally: # Only this invocation's fresh build directory, never profiles, state or previous releases. diff --git a/tools/proxmox/test_installer.py b/tools/proxmox/test_installer.py index 2631f65..24fb227 100644 --- a/tools/proxmox/test_installer.py +++ b/tools/proxmox/test_installer.py @@ -413,6 +413,32 @@ class GuestTests(unittest.TestCase): release = guest.build(archive, {"role": "worker", "sourceSha256": "0" * 64}) self.assertEqual(license_text, (release / "LICENSE").read_bytes()) + def test_build_user_cannot_replace_the_root_updater(self): + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + trusted = b"#!/usr/bin/env python3\n# trusted archive updater\n" + malicious = b"#!/usr/bin/env python3\n# build-user replacement\n" + archive = root / "source.tgz" + with tarfile.open(archive, "w:gz") as stream: + for name, content in (("LICENSE", b"fixture"), ("tools/lxc_update.py", trusted)): + item = tarfile.TarInfo(name) + item.size = len(content) + stream.addfile(item, io.BytesIO(content)) + home = root / "build" + home.mkdir() + def compromised_publish(name, home, command, **kwargs): + source = Path(kwargs["cwd"]) + (source / "tools/lxc_update.py").write_bytes(malicious) + output = Path(command[command.index("-o") + 1]) + output.mkdir(parents=True) + (output / "finaltoken-worker").write_bytes(b"\x7fELF" + b"\0" * 14 + b"\x3e\x00") + (output.parent / "finaltoken-update").write_bytes(malicious) + with patch.object(guest, "APP", root / "app"), patch.object(guest, "BUILD", home), \ + patch.object(guest, "user", return_value=types.SimpleNamespace(pw_uid=1000, pw_gid=1000)), \ + patch.object(guest.os, "chown", create=True), patch.object(guest, "as_user", side_effect=compromised_publish): + release = guest.build(archive, {"role": "worker", "sourceSha256": "0" * 64}) + self.assertEqual(trusted, (release / "finaltoken-update").read_bytes()) + def test_atomic_configuration_is_never_replaced_on_update(self): with tempfile.TemporaryDirectory() as tmp: target = Path(tmp) / "config.json"