tools: isolate the root updater from build output
agent-review agent review: pass

Capture the updater from the verified source archive before unprivileged build handoff. Exclude build-supplied updater files from copied artifacts and atomically install the trusted command in the root-owned release. Cover source and output replacement with an adversarial publishing regression.

Refs #21
This commit is contained in:
Florian Schmidt
2026-11-15 13:00:23 +01:00
parent 7b70c7a0fa
commit b404d75fe8
6 changed files with 44 additions and 5 deletions
+26
View File
@@ -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"