From eb8acecb2153227421163f590f45d977c28c4b0f Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Tue, 3 Nov 2026 18:09:01 -0600 Subject: [PATCH] Verify updated registry tags before adoption --- bootstrap/apps/README.md | 7 +++-- scripts/registry-storage-reconcile.py | 45 +++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/bootstrap/apps/README.md b/bootstrap/apps/README.md index 5be2457..ac12e56 100644 --- a/bootstrap/apps/README.md +++ b/bootstrap/apps/README.md @@ -119,9 +119,10 @@ boot a source VM while host K3s is active. The transition below prepares the rem registry offline. Apps accepts it only when the receipt's host, node and path match the live cluster; before the PV exists it also verifies the complete file manifest. When the remote registry has grown, the offline check accepts it only if the -Debian copy matches the original receipt, every original file is unchanged, -and registry blob contents match their SHA-256 paths. The remote manifest is -checked again before adoption. +Debian copy matches the original receipt, original files remain intact, and +registry blob contents match their SHA-256 paths. Updated tag pointers are +accepted only when their image manifests and referenced blobs verify. The +remote manifest is checked again before adoption. The native profile declares `storage.required_mount: /data`. Runtime/start guards, offline publication and remote registry reconciliation require that exact diff --git a/scripts/registry-storage-reconcile.py b/scripts/registry-storage-reconcile.py index ac44d8a..1e8221f 100644 --- a/scripts/registry-storage-reconcile.py +++ b/scripts/registry-storage-reconcile.py @@ -130,8 +130,8 @@ def main(): if hashlib.sha256(baseline.encode()).hexdigest() != receipt["manifest_sha256"]: raise RuntimeError("Retained registry baseline does not match the migration receipt") original_files, current_files = manifest_files(baseline), manifest_files(current) - if any(current_files.get(name) != digest for name, digest in original_files.items()): - raise RuntimeError("Remote registry is missing or changed a file from its verified baseline") + conflicts = [name for name, digest in original_files.items() if current_files.get(name) != digest] + verify_current_registry_tags(remote, path, current_files, conflicts) blob_pattern = re.compile(r"^\./docker/registry/v2/blobs/sha256/([0-9a-f]{2})/([0-9a-f]{64})/data$") for name, digest in current_files.items(): match = blob_pattern.fullmatch(name) @@ -181,6 +181,47 @@ def remote_manifest(remote, path): return remote("sudo -n bash -o pipefail -c " + shlex.quote(command)) +def verify_current_registry_tags(remote, path, files, conflicts): + """Keep newer tag pointers only when their complete image graph is intact.""" + visited = set() + + def read(relative): + return remote("sudo -n cat -- " + shlex.quote(str(Path(path) / relative))) + + def verify(digest, manifest=False, depth=0): + if not re.fullmatch(r"sha256:[0-9a-f]{64}", digest) or depth > 12 or len(visited) > 4096: + raise RuntimeError("Remote registry tag has an invalid or oversized manifest graph") + hexdigest = digest[7:] + relative = f"docker/registry/v2/blobs/sha256/{hexdigest[:2]}/{hexdigest}/data" + if files.get("./" + relative) != hexdigest: + raise RuntimeError("Remote registry tag references a missing or corrupt blob") + if not manifest or digest in visited: + return + visited.add(digest) + document = json.loads(read(relative)) + if not isinstance(document, dict) or document.get("schemaVersion") != 2: + raise RuntimeError("Remote registry tag has an unsupported image manifest") + if "manifests" in document: + for descriptor in document["manifests"]: + verify(descriptor["digest"], manifest=True, depth=depth + 1) + else: + if not isinstance(document.get("config"), dict) or not isinstance(document.get("layers"), list): + raise RuntimeError("Remote registry image has incomplete config or layers") + verify(document["config"]["digest"], depth=depth + 1) + for descriptor in document["layers"]: + verify(descriptor["digest"], depth=depth + 1) + + for name in conflicts: + match = re.fullmatch(r"\./docker/registry/v2/repositories/(.+)/_manifests/tags/([^/]+)/current/link", name) + if not match or name not in files: + raise RuntimeError(f"Remote registry is missing or changed a baseline file: {name}") + value = read(name.removeprefix("./")) + if files[name] not in (hashlib.sha256(value.encode()).hexdigest(), hashlib.sha256((value + "\n").encode()).hexdigest()): + raise RuntimeError("Remote registry tag changed during verification") + verify(value, manifest=True) + print(f"Keeping verified SSD registry tag {match[1]}:{match[2]} ({value[:23]}...)", flush=True) + + def require_remote_data_mount(remote, mount): if not mount: return