"""Independent Windows updater. Transaction data never includes the ARM user profile.""" import argparse from contextlib import contextmanager import json import logging import msvcrt import os from pathlib import Path, PurePosixPath import re import shutil import stat import subprocess import sys from time import monotonic, sleep from uuid import uuid4 import winreg from zipfile import ZipFile import pefile import psutil from .branding import INSTALL_ID, PUBLISHER from .config import data_path from .update_client import write_atomic from .update_release import MANIFEST, UpdateError, eligible, source_config, verify_file, verify_manifest from .update_state import read_json, transaction_from_ack from .version import VERSION, read_version, version_key EXE = "AdaptiveResourceMonitor.exe" WORKER = "AdaptiveResourceMonitorMaintenance.exe" MANAGED = {EXE, WORKER, "_internal", "Third-party notices", "VERSION.md", "VERSION.txt", "BUILD.json", "README.md", "SECURITY.md", "REMOTE_MONITORING.md", "CHANGELOG.md", "LICENSE.md"} UNINSTALL_KEY = rf"Software\Microsoft\Windows\CurrentVersion\Uninstall\{INSTALL_ID}_is1" RUN_ONCE = r"Software\Microsoft\Windows\CurrentVersion\RunOnce" UNINSTALLER = re.compile(r"unins[0-9]{3}\.(?:exe|dat|msg)", re.I) ACK = "ARM_UPDATE_READY" LOG = logging.getLogger(__name__) def safe_entry(path): if path.is_symlink() or getattr(path.lstat(), "st_file_attributes", 0) & stat.FILE_ATTRIBUTE_REPARSE_POINT: raise UpdateError(f"Update cannot replace the link or junction {path.name}") def registered_view(target): for view in (winreg.KEY_WOW64_64KEY, winreg.KEY_WOW64_32KEY): try: with winreg.OpenKey(winreg.HKEY_CURRENT_USER, UNINSTALL_KEY, 0, winreg.KEY_READ | view) as key: location = winreg.QueryValueEx(key, "InstallLocation")[0] if location and Path(location).resolve() == target: return view except FileNotFoundError: continue return None def verify_executable(path, version): pe = pefile.PE(str(path), fast_load=True) try: pe.parse_data_directories([pefile.DIRECTORY_ENTRY["IMAGE_DIRECTORY_ENTRY_RESOURCE"]]) table = next(node.StringTable[0].entries for group in pe.FileInfo for node in group if hasattr(node, "StringTable")) numeric = version_key(version)[:4] fixed = pe.VS_FIXEDFILEINFO[0] product = (fixed.ProductVersionMS >> 16, fixed.ProductVersionMS & 65535, fixed.ProductVersionLS >> 16, fixed.ProductVersionLS & 65535) file = (fixed.FileVersionMS >> 16, fixed.FileVersionMS & 65535, fixed.FileVersionLS >> 16, fixed.FileVersionLS & 65535) if (pe.FILE_HEADER.Machine != 0x8664 or table[b"ProductVersion"].decode().strip() != version or table[b"FileVersion"].decode().strip() != version or table[b"CompanyName"].decode().strip() != PUBLISHER or product != numeric or file != numeric): raise UpdateError(f"Executable identity or version is inconsistent: {path.name}") finally: pe.close() def managed_entries(target, installed): return sorted(entry.name for entry in target.iterdir() if entry.name in MANAGED or (installed and UNINSTALLER.fullmatch(entry.name))) def unpack_portable(artifact, output, version): seen, total = set(), 0 with ZipFile(artifact) as archive: if len(archive.infolist()) > 10_000: raise UpdateError("Portable package contains too many entries") for member in archive.infolist(): path = PurePosixPath(member.filename) parts = path.parts if (len(parts) < 2 or path.is_absolute() or parts[0] != "AdaptiveResourceMonitor" or parts[1] not in MANAGED or any(not part or part in (".", "..") or part.endswith((".", " ")) or re.search(r'[<>:"\\|?*\x00-\x1f]', part) or Path(part).is_reserved() for part in parts) or stat.S_ISLNK(member.external_attr >> 16)): raise UpdateError("Portable package contains an unexpected or unsafe path") key = str(path).casefold() total += member.file_size if key in seen or total > 2 * 1024**3: raise UpdateError("Portable package exceeds extraction limits or duplicates a path") seen.add(key) destination = output.joinpath(*parts[1:]) if member.is_dir(): destination.mkdir(parents=True, exist_ok=True) else: destination.parent.mkdir(parents=True, exist_ok=True) with archive.open(member) as source, destination.open("xb") as target: shutil.copyfileobj(source, target, 256 * 1024) verify_payload(output, version) def verify_payload(directory, version): verify_executable(directory / EXE, version) verify_executable(directory / WORKER, version) if any(read_version(path) != version for path in ( directory / "VERSION.md", directory / "VERSION.txt", directory / "_internal" / "VERSION.md" )): raise UpdateError("Packaged version documents are inconsistent") if json.loads((directory / "BUILD.json").read_bytes()).get("version") != version: raise UpdateError("Packaged build metadata is inconsistent") def set_status(plan_dir, state, message): write_atomic(plan_dir / "status.json", json.dumps({"state": state, "message": message}).encode()) LOG.info("%s. %s", state, message) @contextmanager def update_lock(root): with (root / "updates" / "maintenance.lock").open("a+b") as lock: try: msvcrt.locking(lock.fileno(), msvcrt.LK_NBLCK, 1) except OSError as exc: raise UpdateError("Another update is already running.") from exc yield def updates_locked(root): if not (root / "updates").is_dir(): return False try: with update_lock(root): return False except UpdateError: return True def save_presentation(plan_dir, state): write_atomic(plan_dir / "presentation.json", json.dumps(state).encode()) def presentation(plan_dir): state = read_json(plan_dir / "presentation.json", {}) if not isinstance(state, dict) or type(state.get("expanded")) is not bool: return {} rect = state.get("geometry") if (not isinstance(rect, list) or len(rect) != 4 or any(type(value) is not int for value in rect) or any(abs(value) > 32768 for value in rect) or min(rect[2:]) <= 0): state.pop("geometry", None) return state def acknowledge_shutdown(plan_dir): write_atomic(plan_dir / "closed.json", json.dumps({"pid": os.getpid(), "version": VERSION}).encode()) def prepare(release, artifact, target, kind, root): config = source_config() signed = verify_manifest(release.envelope, config["public_key"], release.version) if not eligible(signed.version, VERSION) or signed.version == VERSION: raise UpdateError("Maintenance supports eligible upgrades only") verify_file(artifact, signed.packages[kind]) plan_dir = root / "transactions" / uuid4().hex plan_dir.mkdir(parents=True) plan = plan_dir / "plan.json" parent = psutil.Process() data = {"target": str(target), "artifact": str(Path(artifact).resolve()), "kind": kind, "old": VERSION, "new": signed.version, "parent": parent.pid, "created": parent.create_time()} write_atomic(plan, json.dumps(data).encode()) write_atomic(plan_dir / MANIFEST, signed.envelope) worker = plan_dir / WORKER shutil.copy2(target / WORKER, worker) verify_executable(worker, VERSION) subprocess.Popen([str(worker), str(plan)], cwd=plan_dir, creationflags=subprocess.CREATE_NO_WINDOW) return plan_dir def snapshot_registry(view): if view is None: return [] with winreg.OpenKey(winreg.HKEY_CURRENT_USER, UNINSTALL_KEY, 0, winreg.KEY_READ | view) as key: return [winreg.EnumValue(key, index) for index in range(winreg.QueryInfoKey(key)[1])] def restore_registry(view, previous): if view is None: return with winreg.CreateKeyEx(winreg.HKEY_CURRENT_USER, UNINSTALL_KEY, 0, winreg.KEY_WRITE | winreg.KEY_READ | view) as key: names = [winreg.EnumValue(key, index)[0] for index in range(winreg.QueryInfoKey(key)[1])] keep = {name.casefold() for name, _, _ in previous} for name in names: if name.casefold() not in keep: winreg.DeleteValue(key, name) for name, value, kind in previous: winreg.SetValueEx(key, name, 0, kind, value) def recovery_entry(plan_dir, add): name = "*ARMUpdate-" + plan_dir.name with winreg.CreateKey(winreg.HKEY_CURRENT_USER, RUN_ONCE) as key: if add: command = subprocess.list2cmdline([str(plan_dir / WORKER), str(plan_dir / "plan.json"), "--recover"]) winreg.SetValueEx(key, name, 0, winreg.REG_SZ, command) else: try: winreg.DeleteValue(key, name) except FileNotFoundError: pass def copy_managed(source, target): safe_entry(source) if source.is_dir(): for item in source.rglob("*"): safe_entry(item) shutil.copytree(source, target) else: shutil.copy2(source, target) def remove_managed(path): if path.exists(): safe_entry(path) if path.is_dir(): shutil.rmtree(path) else: path.unlink() def requesting_process(data, target): try: parent = psutil.Process(data["parent"]) if abs(parent.create_time() - data["created"]) <= 0.01 and Path(parent.exe()).resolve() == target / EXE: return parent except psutil.NoSuchProcess: pass raise UpdateError("The requesting process is not this Adaptive Resource Monitor installation.") def wait_parent(data, target, plan_dir, timeout=75): expected = {"pid": data["parent"], "version": data["old"]} limit = monotonic() + timeout closing = False while monotonic() < limit: if (plan_dir / "abort").exists(): raise UpdateError("Installation canceled before touching the application.") acknowledged = read_json(plan_dir / "closed.json") == expected if acknowledged and not closing: set_status(plan_dir, "closing", "Closing Adaptive Resource Monitor…") closing = True try: parent = psutil.Process(data["parent"]) alive = abs(parent.create_time() - data["created"]) <= 0.01 except psutil.NoSuchProcess: alive = False if not alive: if not acknowledged: raise UpdateError("ARM exited without confirming that settings and history were closed. Installation stopped.") return if Path(parent.exe()).resolve() != target / EXE: raise UpdateError("The requesting process is not this Adaptive Resource Monitor installation.") sleep(.25) raise UpdateError("ARM did not shut down in time. To retry, close any open ARM dialogs and start the update again.") def restart(target, version, plan_dir): ready = plan_dir / "ready.json" ready.unlink(missing_ok=True) child = subprocess.Popen([str(target / EXE)], cwd=target, env=dict(os.environ, **{ACK: str(ready)})) try: deadline = monotonic() + 35 while monotonic() < deadline and child.poll() is None: if ready.is_file(): if json.loads(ready.read_bytes()) == {"version": version, "pid": child.pid}: return raise UpdateError("The restarted application reported a different version") sleep(.2) raise UpdateError("The restarted application did not confirm initialization") except BaseException: if child.poll() is None: child.terminate() child.wait(timeout=15) raise def rollback(target, work, journal): originals = set(journal["originals"]) backup = work / "previous" for name in managed_entries(target, journal["view"] is not None): if name not in originals or (backup / name).exists(): remove_managed(target / name) for original in backup.iterdir(): original.replace(target / original.name) restore_registry(journal["view"], journal["registration"]) verify_executable(target / EXE, journal["old"]) def transact(plan_dir, recover=False): data = json.loads((plan_dir / "plan.json").read_bytes()) target = Path(data["target"]).resolve(strict=True) work = target / (".arm-update-" + plan_dir.name) checkpoint = plan_dir / "journal.json" if not eligible(data["new"], VERSION) or data["old"] != VERSION or data["new"] == VERSION: raise UpdateError("Update plan does not describe an upgrade from this maintenance version") safe_entry(target) if recover: journal = json.loads(checkpoint.read_bytes()) if journal.get("committed"): remove_managed(work) recovery_entry(plan_dir, False) set_status(plan_dir, "complete", f"Updated and restarted Adaptive Resource Monitor {data['new']}.") return set_status(plan_dir, "recovering", "Restoring the previous application after an interrupted installation…") rollback(target, work, journal) restart(target, data["old"], plan_dir) set_status(plan_dir, "rolled_back", "Restored the previous application after an interrupted installation.") recovery_entry(plan_dir, False) remove_managed(work) return requesting_process(data, target) set_status(plan_dir, "verifying", "Verifying the signed update package…") release = verify_manifest((plan_dir / MANIFEST).read_bytes(), source_config()["public_key"], data["new"]) view = registered_view(target) kind = "installed" if view is not None else "portable" if data["kind"] != kind: raise UpdateError("The Windows installation type has changed") artifact = Path(data["artifact"]) package = release.packages[kind] verify_file(artifact, package) verify_executable(target / EXE, data["old"]) work.mkdir() previous = work / "previous" previous.mkdir() try: payload = work / "next" payload.mkdir() if kind == "portable": unpack_portable(artifact, payload, release.version) else: shutil.copy2(artifact, payload / package.name) verify_file(payload / package.name, package) verify_executable(payload / package.name, release.version) set_status(plan_dir, "waiting", "Saving application settings and pending telemetry…") wait_parent(data, target, plan_dir) set_status(plan_dir, "verifying", "Rechecking the update package before installation…") signed = verify_manifest((plan_dir / MANIFEST).read_bytes(), source_config()["public_key"], data["new"]) if signed.envelope != release.envelope: raise UpdateError("The signed update manifest changed during shutdown.") verify_file(artifact, signed.packages[kind]) for name in managed_entries(target, view is not None): safe_entry(target / name) originals = managed_entries(target, view is not None) journal = {"originals": originals, "view": view, "registration": snapshot_registry(view), "old": data["old"]} if kind == "installed": for name in originals: copy_managed(target / name, previous / name) write_atomic(checkpoint, json.dumps(journal).encode()) recovery_entry(plan_dir, True) set_status(plan_dir, "installing", "Installing the verified update.") try: if kind == "portable": for name in originals: (target / name).replace(previous / name) for entry in payload.iterdir(): entry.replace(target / entry.name) else: setup = payload / package.name verify_file(setup, package) subprocess.run([str(setup), "/ARMUPDATE=1", "/VERYSILENT", "/SUPPRESSMSGBOXES", "/NORESTART", "/NOCLOSEAPPLICATIONS", "/NOICONS", "/TASKS=", f"/DIR={target}", f"/LOG={plan_dir / 'installer.log'}"], cwd=payload, check=True, creationflags=subprocess.CREATE_NO_WINDOW) with winreg.OpenKey(winreg.HKEY_CURRENT_USER, UNINSTALL_KEY, 0, winreg.KEY_READ | view) as key: if winreg.QueryValueEx(key, "DisplayVersion")[0] != release.version: raise UpdateError("Windows installer registration did not report the expected version") set_status(plan_dir, "verifying_installation", "Verifying installed files and application version…") verify_payload(target, release.version) build = json.loads((target / "BUILD.json").read_bytes()) if (build.get("source_revision"), build.get("jenkins_build")) != (release.commit, release.build): raise UpdateError("Installed build identity does not match the signed manifest") set_status(plan_dir, "restarting", "Restarting Adaptive Resource Monitor…") restart(target, release.version, plan_dir) except Exception as exc: LOG.exception("Installation or startup failed") set_status(plan_dir, "recovering", "Update failed. Restoring the previous release…") rollback(target, work, journal) restart(target, data["old"], plan_dir) set_status(plan_dir, "rolled_back", f"Update failed. The previous version was restored and restarted. {exc}") recovery_entry(plan_dir, False) return journal["committed"] = True write_atomic(checkpoint, json.dumps(journal).encode()) recovery_entry(plan_dir, False) set_status(plan_dir, "complete", f"Updated and restarted Adaptive Resource Monitor {release.version}.") finally: if not checkpoint.exists() or json.loads(checkpoint.read_bytes()).get("committed") or ( (plan_dir / "status.json").exists() and json.loads((plan_dir / "status.json").read_bytes())["state"] == "rolled_back" ): remove_managed(work) def signal_ready(root=None): if directory := transaction_from_ack(root or data_path(), os.environ.pop(ACK, None)): write_atomic(directory / "ready.json", json.dumps({"version": VERSION, "pid": os.getpid()}).encode()) def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("plan", type=Path) parser.add_argument("--recover", action="store_true") args = parser.parse_args() plan_dir = args.plan.resolve().parent expected = (data_path() / "updates" / "transactions").resolve() if args.plan.name != "plan.json" or plan_dir.parent != expected or not re.fullmatch(r"[0-9a-f]{32}", plan_dir.name): return 2 logging.basicConfig(filename=plan_dir / "maintenance.log", level=logging.INFO, encoding="utf-8", format="%(asctime)s %(levelname)s %(message)s") def run(): try: with update_lock(data_path()): set_status(plan_dir, "preparing", "Preparing the Adaptive Resource Monitor update…") transact(plan_dir, args.recover) except Exception as exc: LOG.exception("Maintenance failed") message = f"{exc}\nDiagnostics: {plan_dir / 'maintenance.log'}" journal = read_json(plan_dir / "journal.json", {}) if journal and not journal.get("committed"): message += ("\nClose ARM before attempting recovery again. If it still fails, use the previous " "version's installer or restore the portable files from the previous verified package. " "Leave the ARM profile directory in place.") try: recovery_entry(plan_dir, False) except OSError: LOG.exception("Could not remove the scheduled recovery entry") set_status(plan_dir, "failed", message) return 1 return 0 from .update_progress import show_progress return show_progress(plan_dir, run)