From 027aa6c10baf523f9f57139b3c6896c69fff1f99 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 16 Oct 2026 05:36:58 +0000 Subject: [PATCH 01/11] fix: store the CID of the verified release head The installer hashes the signed release-head.json it just checked and writes that UnixFS CIDv0 into sources.json. A non-empty stamp that names different bytes exits 1. The launcher contract records that failure. Co-authored-by: Sash --- docs/LAUNCHER_CONTRACT.md | 2 +- scripts/install-bootstrap-test.py | 67 ++++++++++++++++++++++++++++--- scripts/install.sh | 42 +++++++++++++++++++ 3 files changed, 105 insertions(+), 6 deletions(-) diff --git a/docs/LAUNCHER_CONTRACT.md b/docs/LAUNCHER_CONTRACT.md index b079a7c1c..16044bb44 100644 --- a/docs/LAUNCHER_CONTRACT.md +++ b/docs/LAUNCHER_CONTRACT.md @@ -66,7 +66,7 @@ the installed path. On Linux that running path then ends in ` (deleted)`. | Code | Meaning | | --- | --- | | 0 | The command finished. | -| 1 | The command failed. A second host for the same data directory is one case. | +| 1 | The command failed. A second host for the same data directory is one case. An installer stamp that differs from the verified release head is another. | | 2 | The `elastos` command line is invalid. | | 75 | The running gateway's binary watch exited. | diff --git a/scripts/install-bootstrap-test.py b/scripts/install-bootstrap-test.py index 08c0dff36..a0cb2f9f5 100644 --- a/scripts/install-bootstrap-test.py +++ b/scripts/install-bootstrap-test.py @@ -32,6 +32,8 @@ INSTALLER = Path(__file__).with_name("install.sh") SANDBOX_BASE = Path(__file__).resolve().parent.parent / "target/installer-sandboxes" SOURCE = INSTALLER.read_text() +if "# ── Main" not in SOURCE: + raise SystemExit("install.sh is missing the '# ── Main' helper marker") HELPERS = SOURCE.split("# ── Main", 1)[0] PYTHON = SOURCE.split("<<'PY_ED25519'\n", 1)[1].split("\nPY_ED25519", 1)[0] CRYPTO = {"__name__": "installer_test"} @@ -46,6 +48,22 @@ FIXTURE_DID = "did:key:z6MkvDqGT54cXesYGvABpF1UapVNwjCqRcafi4Px6Thv5T3Z" +def unixfs_v0_cid(data): + """CIDv0 from scripts/release-signer.py, the publisher's implementation.""" + global _SIGNER_CID + if _SIGNER_CID is None: + import importlib.util + path = Path(__file__).with_name("release-signer.py") + spec = importlib.util.spec_from_file_location("release_signer_cid", path) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + _SIGNER_CID = module + return _SIGNER_CID.unixfs_metadata_cid(data) + + +_SIGNER_CID = None + + def binding_fixture(): # Fixed OpenSSL Ed25519 vectors from the disposable seed [7; 32]. This is # a test identity. Running these stdlib tests needs neither a key nor OpenSSL. @@ -482,6 +500,28 @@ def test_release_identity_requires_matching_nonempty_strings(self): with self.subTest(case=index): self.assertEqual(result.returncode == 0, accepted, result.stdout + result.stderr) + def test_verified_head_cid_is_the_hashed_bytes(self): + vector = b'{"payload":{"version":"0.7.1-rc.2"}}\n' + expected = "QmZFMnZjkiTqy9VY5FDdKudvpsKtKxifk1poBrV7k4sqp8" + self.assertEqual(unixfs_v0_cid(vector), expected) + with tempfile.TemporaryDirectory() as directory: + path = Path(directory, "release-head.json") + path.write_bytes(vector) + stored = shell('HEAD_CID=""\nbind_verified_head_cid "$1"\nprintf "%s\\n" "$HEAD_CID"\n', path) + self.assertEqual((stored.returncode, stored.stdout.strip()), (0, expected), stored.stderr) + same = shell('HEAD_CID="$2"\nbind_verified_head_cid "$1"\nprintf "%s\\n" "$HEAD_CID"\n', + path, expected) + self.assertEqual((same.returncode, same.stdout.strip()), (0, expected), same.stderr) + mismatch = shell('HEAD_CID="QmStampNamesOtherBytes"\nbind_verified_head_cid "$1"\n', path) + self.assertEqual(mismatch.returncode, 1, mismatch.stdout) + self.assertIn("Stamped head CID differs from the verified release head", mismatch.stderr) + self.assertIn("Stamp: QmStampNamesOtherBytes", mismatch.stderr) + self.assertIn("Verified: " + expected, mismatch.stderr) + path.write_bytes(b"") + empty = shell('HEAD_CID=""\nbind_verified_head_cid "$1"\n', path) + self.assertEqual(empty.returncode, 1, empty.stdout) + self.assertIn("no single-block CID", empty.stderr) + class InstallerSandbox: """Disposable HOME, data root, install dir, temp dir and logs for the complete installer. @@ -557,18 +597,18 @@ def respond(self, name, data): def requests(self): return (self.root / "requests").read_text().splitlines() - def run(self, *options, umask="0022"): + def run(self, *options, umask="0022", head_cid=""): seen = len(self.requests()) result = shell('umask "$6"\n' + ''' export HOME="$1/home" XDG_DATA_HOME="$1/home/xdg-data" TMPDIR="$1/tmp" FIXTURES="$1" PATH="$1/mocks:$PATH" # Apple Python otherwise adds bytecode caches to HOME during file-preservation checks. export PYTHONDONTWRITEBYTECODE=1 -export ELASTOS_PUBLISHER_GATEWAY="" ELASTOS_HEAD_CID="" +export ELASTOS_PUBLISHER_GATEWAY="" ELASTOS_HEAD_CID="$7" export ELASTOS_SOURCE_CONNECT_TICKET="" ELASTOS_PUBLISHER_NODE_ID="" ELASTOS_INSTALL_ONLY="" export ELASTOS_TEST_CALLS="$1/calls" ELASTOS_TEST_SETUP_MARKER="$1/setup-marker" MOCK_SYSTEM="$4" MOCK_MACHINE="$5" # Feed the complete stamped script through stdin, as curl | bash does. -cat "$3" | "$2" --noprofile --norc -s -- "${@:7}" -''', self.root, OPTIONS.bash, self.installer, self.system, self.machine, umask, +cat "$3" | "$2" --noprofile --norc -s -- "${@:8}" +''', self.root, OPTIONS.bash, self.installer, self.system, self.machine, umask, head_cid, "--maintainer-did", self.did, "--publisher-gateway", "https://test.invalid", *options) return result, self.requests()[seen:] @@ -790,6 +830,23 @@ def test_installer_refuses_a_maintainer_did_with_control_text_before_printing_it self.assertNotIn("Maintainer DID:", result.stdout) self.assertNotIn("\x1b]", result.stdout + result.stderr) + def test_stamped_head_cid_must_match_the_verified_head(self): + did, head, release = installable_fixture() + verified = unixfs_v0_cid(head) + with InstallerSandbox(head, release, did) as sandbox: + sandbox.respond("binary", RUNTIME_STUB) + refused, requests = sandbox.run("--install-only", head_cid="QmStampNamesOtherBytes") + self.assertEqual(refused.returncode, 1, refused.stdout + refused.stderr) + self.assertIn("Stamped head CID differs from the verified release head", refused.stderr) + self.assertIn(verified, refused.stderr) + self.assertEqual(requests, ["https://test.invalid/release-head.json"]) + self.assertFalse(sandbox.binary.exists()) + accepted, accepted_requests = sandbox.run("--install-only", head_cid=verified) + self.assertEqual(accepted.returncode, 0, accepted.stdout + accepted.stderr) + self.assertEqual(accepted_requests, self.REQUESTS) + sources = json.loads((sandbox.root / "candidate" / "sources.json").read_text()) + self.assertEqual(sources["sources"][0]["head_cid"], verified) + def test_installer_prints_the_selected_maintainer_did_before_verification(self): did, head, release = installable_fixture() with InstallerSandbox(head, release, did) as sandbox: @@ -1048,7 +1105,7 @@ def assert_clean_install(self, sandbox, did, head, release, before, runtime=RUNT self.assertEqual(source["discovery_uri"], "elastos://source/stable/" + hashlib.sha256(did.encode()).hexdigest()[:32]) registration = (source["gateways"], source["head_cid"], source["connect_ticket"], source["publisher_node_id"]) - self.assertEqual(registration, (["https://test.invalid"], "", "fixture-ticket", "fixture-node")) + self.assertEqual(registration, (["https://test.invalid"], unixfs_v0_cid(head), "fixture-ticket", "fixture-node")) # Apart from the stand-in's Runtime, the shell writes no installation file # itself and removes its staged copy. self.assertEqual(changed_paths(before, sandbox.home_state()), {".local/bin/elastos"}) diff --git a/scripts/install.sh b/scripts/install.sh index 46339c3a6..a73378cfb 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -47,6 +47,8 @@ MAINTAINER_DID_PLACEHOLDER="__MAINTAINER""_DID__" if [[ "$MAINTAINER_DID" == "$MAINTAINER_DID_PLACEHOLDER" ]]; then MAINTAINER_DID="" fi +# Publisher stamp. After signature verification this becomes the UnixFS CIDv0 +# of the verified release-head.json bytes. A stamp that names other bytes is refused. HEAD_CID="${ELASTOS_HEAD_CID:-__HEAD_CID__}" HEAD_CID_PLACEHOLDER="__HEAD""_CID__" if [[ "$HEAD_CID" == "$HEAD_CID_PLACEHOLDER" ]]; then @@ -824,6 +826,44 @@ PY_ED25519 ok "Signature verified" } +# The stored head CID is the UnixFS CIDv0 of the verified release-head.json +# bytes. A non-empty stamp that names different bytes is refused. +bind_verified_head_cid() { + local head_path="$1" + local verified + verified="$(python3 - "$head_path" <<'PY_HEAD_CID' +import hashlib +import sys + +def varint(value): + encoded = bytearray() + while value >= 128: + encoded.append((value & 127) | 128) + value >>= 7 + encoded.append(value) + return bytes(encoded) + +data = open(sys.argv[1], "rb").read() +if not data or len(data) > 256 * 1024: + print("release head is outside the single-block UnixFS bound", file=sys.stderr) + sys.exit(1) +unixfs = b"\x08\x02\x12" + varint(len(data)) + data + b"\x18" + varint(len(data)) +node = b"\x0a" + varint(len(unixfs)) + unixfs +number = int.from_bytes(b"\x12\x20" + hashlib.sha256(node).digest(), "big") +alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" +text = "" +while number: + number, digit = divmod(number, 58) + text = alphabet[digit] + text +print(text) +PY_HEAD_CID +)" || die "Verified release head has no single-block CID" + if [[ -n "$HEAD_CID" && "$HEAD_CID" != "$verified" ]]; then + die "Stamped head CID differs from the verified release head. Stamp: ${HEAD_CID}. Verified: ${verified}" + fi + HEAD_CID="$verified" +} + validate_release_identity() { # Both envelopes are verified before this check, on every transport. # The signed digest binds exact envelope bytes; the CID stays content identity. @@ -1284,6 +1324,7 @@ HEAD_SCHEMA=$(json_get "${INSTALL_SCRATCH}/release-head.json" 'd["payload"]["sch info "Verifying release head signature..." verify_signature "${INSTALL_SCRATCH}/release-head.json" "elastos.release.head.v1" "$MAINTAINER_DID" +bind_verified_head_cid "${INSTALL_SCRATCH}/release-head.json" RELEASE_CID=$(json_get "${INSTALL_SCRATCH}/release-head.json" 'd["payload"]["latest_release_cid"]') RELEASE_VERSION=$(json_get "${INSTALL_SCRATCH}/release-head.json" 'd["payload"]["version"]') @@ -1386,6 +1427,7 @@ ok "Checksum and version match the signed release" SIGNER_DID=$(json_get "${INSTALL_SCRATCH}/release-head.json" 'd["signer_did"]') SOURCES_PATH="${INSTALL_SCRATCH}/sources.json" +# HEAD_CID is the UnixFS CIDv0 of the verified release-head.json bytes. PUBLISHER_DID="${SIGNER_DID}" \ PUBLISHER_GATEWAY="${PUBLISHER_GATEWAY}" \ INSTALLED_VERSION="${RELEASE_VERSION}" \ From 58a76ac930724dd40d5ccca10ed885734d203227 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 16 Oct 2026 11:57:29 +0000 Subject: [PATCH 02/11] fix: stop an Intel Mac before release lookup detect_platform exits before any download when the Mac is not Apple silicon. The message names Intel Mac and the source-home path in docs/MAC.md. Help text states that there is no signed Intel download. Co-authored-by: Sash --- docs/MAC.md | 5 +++-- scripts/install-bootstrap-test.py | 13 +++++++++++++ scripts/install.sh | 3 ++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/docs/MAC.md b/docs/MAC.md index af327b887..6122a4dc5 100644 --- a/docs/MAC.md +++ b/docs/MAC.md @@ -8,8 +8,9 @@ artifacts, and `.dmg` packaging work. The public release id for an Intel Mac is `x86_64-darwin`. The signed installer stops on that host before a download lookup. The message is -`macOS release lookup requires Apple silicon`. This runbook's source-home path -is the local build. A signed download for that id is a separate release decision. +`Intel Mac has no signed release yet. See docs/MAC.md for the source-home path.` +This runbook is that source-home path. A signed download for that id is a +separate release decision. CI job `gateway-smoke-macos-intel` builds the native `elastos` binary on `macos-15-intel` and requires `GET /healthz` to return the two bytes `OK` from diff --git a/scripts/install-bootstrap-test.py b/scripts/install-bootstrap-test.py index a0cb2f9f5..826af0285 100644 --- a/scripts/install-bootstrap-test.py +++ b/scripts/install-bootstrap-test.py @@ -1582,6 +1582,19 @@ def test_three_platform_release_keys(self): self.assertEqual(result.returncode, 0, result.stderr) self.assertEqual(result.stdout.strip(), expected) + def test_intel_mac_stops_before_release_lookup(self): + result = shell(''' +SYSTEM=Darwin MACHINE=x86_64 +uname() { case "$1" in -s) echo "$SYSTEM";; -m) echo "$MACHINE";; esac; } +detect_platform +printf '%s\\n' "$PLATFORM" +''') + self.assertEqual(result.returncode, 1, result.stdout) + self.assertIn("Intel Mac has no signed release yet", result.stderr) + self.assertIn("docs/MAC.md", result.stderr) + main = SOURCE.split("# ── Main", 1)[1] + self.assertLess(main.index("detect_platform"), main.index("release-head.json")) + def test_platform_paths_match_rust_dirs5_contract(self): # setup.rs and sources.rs use dirs5.0.1::data_dir. Mac ignores XDG; # Linux uses an absolute XDG path, otherwise HOME/.local/share. diff --git a/scripts/install.sh b/scripts/install.sh index a73378cfb..4b08224ce 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -108,6 +108,7 @@ show_help() { echo "" echo -e "${BOLD}ElastOS Installer${NC}" echo " Release lookup: Linux x86_64/aarch64 and macOS Apple silicon." + echo " Intel Mac has no signed download. Use the source-home path in docs/MAC.md." echo " The signed release determines which platforms have downloads." echo "" echo -e "${BOLD}Usage:${NC}" @@ -330,7 +331,7 @@ detect_platform() { case "${OS}" in linux) PLATFORM="${ARCH}-linux" ;; darwin) - [[ "$ARCH" == aarch64 ]] || die "macOS release lookup requires Apple silicon" + [[ "$ARCH" == aarch64 ]] || die "Intel Mac has no signed release yet. See docs/MAC.md for the source-home path." PLATFORM="aarch64-darwin" ;; *) die "Unsupported OS: ${OS}" ;; From 8e3c774fed91bc9f1a39a0dea1a19fd72714c35e Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 16 Oct 2026 14:04:45 +0000 Subject: [PATCH 03/11] fix: leave an empty release-head stamp empty A frozen installer has no head CID. Filling that stamp from the verified bytes made a fresh install cache a head CID, which the update hop refuses. A matching stamp still stores the verified CID. A different stamp still stops. Co-authored-by: Sash --- scripts/install-bootstrap-test.py | 4 ++-- scripts/install.sh | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/scripts/install-bootstrap-test.py b/scripts/install-bootstrap-test.py index 826af0285..5cbaf361a 100644 --- a/scripts/install-bootstrap-test.py +++ b/scripts/install-bootstrap-test.py @@ -508,7 +508,7 @@ def test_verified_head_cid_is_the_hashed_bytes(self): path = Path(directory, "release-head.json") path.write_bytes(vector) stored = shell('HEAD_CID=""\nbind_verified_head_cid "$1"\nprintf "%s\\n" "$HEAD_CID"\n', path) - self.assertEqual((stored.returncode, stored.stdout.strip()), (0, expected), stored.stderr) + self.assertEqual((stored.returncode, stored.stdout.strip()), (0, ""), stored.stderr) same = shell('HEAD_CID="$2"\nbind_verified_head_cid "$1"\nprintf "%s\\n" "$HEAD_CID"\n', path, expected) self.assertEqual((same.returncode, same.stdout.strip()), (0, expected), same.stderr) @@ -1105,7 +1105,7 @@ def assert_clean_install(self, sandbox, did, head, release, before, runtime=RUNT self.assertEqual(source["discovery_uri"], "elastos://source/stable/" + hashlib.sha256(did.encode()).hexdigest()[:32]) registration = (source["gateways"], source["head_cid"], source["connect_ticket"], source["publisher_node_id"]) - self.assertEqual(registration, (["https://test.invalid"], unixfs_v0_cid(head), "fixture-ticket", "fixture-node")) + self.assertEqual(registration, (["https://test.invalid"], "", "fixture-ticket", "fixture-node")) # Apart from the stand-in's Runtime, the shell writes no installation file # itself and removes its staged copy. self.assertEqual(changed_paths(before, sandbox.home_state()), {".local/bin/elastos"}) diff --git a/scripts/install.sh b/scripts/install.sh index 4b08224ce..cbac1efe1 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -47,8 +47,10 @@ MAINTAINER_DID_PLACEHOLDER="__MAINTAINER""_DID__" if [[ "$MAINTAINER_DID" == "$MAINTAINER_DID_PLACEHOLDER" ]]; then MAINTAINER_DID="" fi -# Publisher stamp. After signature verification this becomes the UnixFS CIDv0 -# of the verified release-head.json bytes. A stamp that names other bytes is refused. +# Publisher stamp. A frozen installer leaves this empty, and an empty stamp +# stays empty so a fresh install stores no head CID. A non-empty stamp must +# equal the UnixFS CIDv0 of the verified release-head.json bytes. A stamp that +# names other bytes is refused. HEAD_CID="${ELASTOS_HEAD_CID:-__HEAD_CID__}" HEAD_CID_PLACEHOLDER="__HEAD""_CID__" if [[ "$HEAD_CID" == "$HEAD_CID_PLACEHOLDER" ]]; then @@ -827,8 +829,9 @@ PY_ED25519 ok "Signature verified" } -# The stored head CID is the UnixFS CIDv0 of the verified release-head.json -# bytes. A non-empty stamp that names different bytes is refused. +# Compute the UnixFS CIDv0 of the verified release-head.json bytes. An empty +# stamp stays empty. A non-empty stamp that names those bytes is kept. A stamp +# that names different bytes is refused. bind_verified_head_cid() { local head_path="$1" local verified @@ -859,10 +862,12 @@ while number: print(text) PY_HEAD_CID )" || die "Verified release head has no single-block CID" - if [[ -n "$HEAD_CID" && "$HEAD_CID" != "$verified" ]]; then + if [[ -z "$HEAD_CID" ]]; then + return 0 + fi + if [[ "$HEAD_CID" != "$verified" ]]; then die "Stamped head CID differs from the verified release head. Stamp: ${HEAD_CID}. Verified: ${verified}" fi - HEAD_CID="$verified" } validate_release_identity() { @@ -1428,7 +1433,7 @@ ok "Checksum and version match the signed release" SIGNER_DID=$(json_get "${INSTALL_SCRATCH}/release-head.json" 'd["signer_did"]') SOURCES_PATH="${INSTALL_SCRATCH}/sources.json" -# HEAD_CID is the UnixFS CIDv0 of the verified release-head.json bytes. +# A matching stamp is the UnixFS CIDv0 of the verified bytes. An empty stamp stays empty. PUBLISHER_DID="${SIGNER_DID}" \ PUBLISHER_GATEWAY="${PUBLISHER_GATEWAY}" \ INSTALLED_VERSION="${RELEASE_VERSION}" \ From 343da76a640915591bd0b52be36795bf110fd9da Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 17 Oct 2026 06:34:17 +0000 Subject: [PATCH 04/11] test: check the Intel Mac installer stop text in the smoke contract test install.sh now stops an Intel Mac with "Intel Mac has no signed release yet. See docs/MAC.md for the source-home path." The test requires that stop in detect_platform, requires detect_platform before release-head.json in the main flow, requires MAC.md to quote the same text, and refuses the old text. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01W6Qn9RX7LDqrwiAfNC2F2K --- scripts/gateway-healthz-smoke-test.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/scripts/gateway-healthz-smoke-test.py b/scripts/gateway-healthz-smoke-test.py index 07fbd3bfb..663b6232d 100644 --- a/scripts/gateway-healthz-smoke-test.py +++ b/scripts/gateway-healthz-smoke-test.py @@ -409,8 +409,14 @@ def test_contract_names_the_command_and_url_this_smoke_uses(self): self.assertIn("ELASTOS_GATEWAY_ADDR-localhost:8090", smoke) mac = (ROOT / "docs/MAC.md").read_text() install = (ROOT / "scripts/install.sh").read_text() - self.assertIn('die "macOS release lookup requires Apple silicon"', install) - self.assertIn("macOS release lookup requires Apple silicon", mac) + # This smoke is no Intel download. install.sh stops an Intel Mac in + # detect_platform before release lookup, and MAC.md quotes that stop. + message = "Intel Mac has no signed release yet. See docs/MAC.md for the source-home path." + self.assertIn(f'[[ "$ARCH" == aarch64 ]] || die "{message}"', install) + main = install.split("# ── Main", 1)[1] + self.assertLess(main.index("detect_platform"), main.index("release-head.json")) + self.assertIn(f"`{message}`", mac) + self.assertNotIn("macOS release lookup requires Apple silicon", install + mac) if __name__ == "__main__": From 20944a055c5f690eac86fcc07036eec58d5bdc54 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 17 Oct 2026 08:55:40 +0000 Subject: [PATCH 05/11] fix: tell an Intel Mac user plainly that it is not supported yet The installer stop on an Intel Mac pointed to the source-home path in docs/MAC.md. That path is a developer build, not an install path, so a first-time user had no step they could take. The team decided on a plain stop with no pointer to that build. - install.sh detect_platform now stops with "This Mac has an Intel processor. ElastOS supports Macs with Apple silicon (M1 or later). Intel Mac is not supported yet." It still exits 1 before any download. - --help says "Intel Mac is not supported yet." instead of the source-home pointer. - docs/MAC.md#intel-mac quotes the new message and says there is no other install path for an Intel Mac. - gateway-healthz-smoke-test.py requires the new text in install.sh and MAC.md, requires detect_platform before the first fetch_url, and refuses the retired texts. - install-bootstrap-test.py: test_intel_mac_stops_before_release_lookup requires exit 1, no PLATFORM output and no "source-home" in the message. New test_intel_mac_full_run_downloads_nothing runs the whole install.sh with a configured publisher, uname reporting Darwin x86_64 and curl/wget mocks that log. It requires no network call, no file in HOME, exit 1 and the message. With the die line removed, it records the curl fetch of release-head.json and fails. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01W6Qn9RX7LDqrwiAfNC2F2K --- docs/MAC.md | 15 +++++----- scripts/gateway-healthz-smoke-test.py | 11 +++++-- scripts/install-bootstrap-test.py | 42 +++++++++++++++++++++++++-- scripts/install.sh | 4 +-- 4 files changed, 59 insertions(+), 13 deletions(-) diff --git a/docs/MAC.md b/docs/MAC.md index 99d4f98d3..bcc459a5c 100644 --- a/docs/MAC.md +++ b/docs/MAC.md @@ -10,13 +10,14 @@ This section is the one record of Intel Mac support. Other documents link here instead of repeating it. The public release id for an Intel Mac is `x86_64-darwin`. `scripts/install.sh` -stops on that host before a download lookup. The message is -`Intel Mac has no signed release yet. See docs/MAC.md for the source-home path.` - -A person with an Intel Mac has no signed download. The next step is the signed -installer on an Apple silicon Mac. The source-home commands later in this -runbook are a developer build. They are not the installer, and this note does -not claim that build has passed on Intel. +stops on that host before it downloads anything, and exits with code 1. The +message is +`This Mac has an Intel processor. ElastOS supports Macs with Apple silicon (M1 or later). Intel Mac is not supported yet.` + +Intel Mac is not supported yet. There is no signed download and no other +install path for it. The source-home commands later in this runbook are a +developer build for Apple silicon. They are not an install path for an Intel +Mac, and this note does not claim that build works on Intel. These files hold the rules: diff --git a/scripts/gateway-healthz-smoke-test.py b/scripts/gateway-healthz-smoke-test.py index 663b6232d..46a468b5d 100644 --- a/scripts/gateway-healthz-smoke-test.py +++ b/scripts/gateway-healthz-smoke-test.py @@ -411,12 +411,19 @@ def test_contract_names_the_command_and_url_this_smoke_uses(self): install = (ROOT / "scripts/install.sh").read_text() # This smoke is no Intel download. install.sh stops an Intel Mac in # detect_platform before release lookup, and MAC.md quotes that stop. - message = "Intel Mac has no signed release yet. See docs/MAC.md for the source-home path." + message = ("This Mac has an Intel processor. ElastOS supports Macs with " + "Apple silicon (M1 or later). Intel Mac is not supported yet.") self.assertIn(f'[[ "$ARCH" == aarch64 ]] || die "{message}"', install) main = install.split("# ── Main", 1)[1] + self.assertLess(main.index("detect_platform"), main.index("fetch_url")) self.assertLess(main.index("detect_platform"), main.index("release-head.json")) self.assertIn(f"`{message}`", mac) - self.assertNotIn("macOS release lookup requires Apple silicon", install + mac) + # The stop names no developer build as a way to install. + self.assertNotIn("source-home", message) + for retired in ["macOS release lookup requires Apple silicon", + "Intel Mac has no signed release yet", + "Use the source-home path in docs/MAC.md"]: + self.assertNotIn(retired, install + mac) if __name__ == "__main__": diff --git a/scripts/install-bootstrap-test.py b/scripts/install-bootstrap-test.py index 5cbaf361a..930205c35 100644 --- a/scripts/install-bootstrap-test.py +++ b/scripts/install-bootstrap-test.py @@ -32,6 +32,8 @@ INSTALLER = Path(__file__).with_name("install.sh") SANDBOX_BASE = Path(__file__).resolve().parent.parent / "target/installer-sandboxes" SOURCE = INSTALLER.read_text() +INTEL_MAC_STOP = ("This Mac has an Intel processor. ElastOS supports Macs with " + "Apple silicon (M1 or later). Intel Mac is not supported yet.") if "# ── Main" not in SOURCE: raise SystemExit("install.sh is missing the '# ── Main' helper marker") HELPERS = SOURCE.split("# ── Main", 1)[0] @@ -1590,11 +1592,47 @@ def test_intel_mac_stops_before_release_lookup(self): printf '%s\\n' "$PLATFORM" ''') self.assertEqual(result.returncode, 1, result.stdout) - self.assertIn("Intel Mac has no signed release yet", result.stderr) - self.assertIn("docs/MAC.md", result.stderr) + self.assertEqual(result.stdout, "") + self.assertIn(INTEL_MAC_STOP, result.stderr) + self.assertNotIn("source-home", result.stderr) main = SOURCE.split("# ── Main", 1)[1] + self.assertLess(main.index("detect_platform"), main.index("fetch_url")) self.assertLess(main.index("detect_platform"), main.index("release-head.json")) + def test_intel_mac_full_run_downloads_nothing(self): + # The whole installer, not only detect_platform: an Intel Mac exits 1 + # with the stop message before any network tool runs or any file is written. + # The installer refuses a home under a writable parent such as /tmp. + SANDBOX_BASE.mkdir(parents=True, exist_ok=True) + for path in (SANDBOX_BASE.parent, SANDBOX_BASE): + path.chmod(0o755) + with tempfile.TemporaryDirectory(prefix="installer-intel-", dir=SANDBOX_BASE) as directory: + root = Path(directory) + mocks = root / "mocks" + home = root / "home" + mocks.mkdir() + home.mkdir() + calls = root / "calls" + (mocks / "uname").write_text('#!/bin/sh\ncase "$1" in -s) echo Darwin;; -m) echo x86_64;; *) echo Darwin;; esac\n') + for tool in ["curl", "wget"]: + (mocks / tool).write_text(f'#!/bin/sh\necho {tool} "$@" >> "$CALLS"\nexit 97\n') + for mock in mocks.iterdir(): + mock.chmod(0o755) + # A configured publisher, so only the platform stop can end the run + # before the release-head fetch. + env = dict(os.environ, HOME=str(home), CALLS=str(calls), + ELASTOS_PUBLISHER_GATEWAY="https://publisher.invalid", + ELASTOS_MAINTAINER_DID="did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK", + PATH=str(mocks) + os.pathsep + os.environ["PATH"]) + for name in ["ELASTOS_INSTALL_ONLY", "XDG_DATA_HOME"]: + env.pop(name, None) + result = subprocess.run([OPTIONS.bash, "--noprofile", "--norc", str(INSTALLER.resolve())], + env=env, text=True, capture_output=True, timeout=15) + self.assertFalse(calls.exists(), calls.read_text() if calls.exists() else "") + self.assertEqual(list(home.iterdir()), []) + self.assertEqual(result.returncode, 1, result.stdout + result.stderr) + self.assertIn(INTEL_MAC_STOP, result.stderr) + def test_platform_paths_match_rust_dirs5_contract(self): # setup.rs and sources.rs use dirs5.0.1::data_dir. Mac ignores XDG; # Linux uses an absolute XDG path, otherwise HOME/.local/share. diff --git a/scripts/install.sh b/scripts/install.sh index cbac1efe1..83917230d 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -110,7 +110,7 @@ show_help() { echo "" echo -e "${BOLD}ElastOS Installer${NC}" echo " Release lookup: Linux x86_64/aarch64 and macOS Apple silicon." - echo " Intel Mac has no signed download. Use the source-home path in docs/MAC.md." + echo " Intel Mac is not supported yet." echo " The signed release determines which platforms have downloads." echo "" echo -e "${BOLD}Usage:${NC}" @@ -333,7 +333,7 @@ detect_platform() { case "${OS}" in linux) PLATFORM="${ARCH}-linux" ;; darwin) - [[ "$ARCH" == aarch64 ]] || die "Intel Mac has no signed release yet. See docs/MAC.md for the source-home path." + [[ "$ARCH" == aarch64 ]] || die "This Mac has an Intel processor. ElastOS supports Macs with Apple silicon (M1 or later). Intel Mac is not supported yet." PLATFORM="aarch64-darwin" ;; *) die "Unsupported OS: ${OS}" ;; From b1ff58cd387e824ea16bde99d5b8508ca0ec7299 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 17 Oct 2026 12:12:14 +0000 Subject: [PATCH 06/11] fix: stop an Intel Mac with only "Intel Mac is not supported yet." Decision 4 is the plain stop. The installer put two sentences about the processor and Apple silicon before it. - install.sh keeps the text once, in INTEL_MAC_UNSUPPORTED. detect_platform and --help both print that variable. - docs/MAC.md#intel-mac names the variable and quotes its text. - gateway-healthz-smoke-test.py requires the variable, the die line, a single copy of the text, and none of the retired sentences. - install-bootstrap-test.py requires stderr to be exactly "Error: Intel Mac is not supported yet." from detect_platform, and from a full run that downloads nothing and writes nothing in HOME. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01W6Qn9RX7LDqrwiAfNC2F2K --- docs/MAC.md | 12 ++-- scripts/gateway-healthz-smoke-test.py | 9 ++- scripts/install-bootstrap-test.py | 97 ++++++++++++++++++--------- scripts/install.sh | 7 +- 4 files changed, 81 insertions(+), 44 deletions(-) diff --git a/docs/MAC.md b/docs/MAC.md index ac5aab977..afc6b84e6 100644 --- a/docs/MAC.md +++ b/docs/MAC.md @@ -11,13 +11,13 @@ here instead of repeating it. The public release id for an Intel Mac is `x86_64-darwin`. `scripts/install.sh` stops on that host before it downloads anything, and exits with code 1. The -message is -`This Mac has an Intel processor. ElastOS supports Macs with Apple silicon (M1 or later). Intel Mac is not supported yet.` +message is `INTEL_MAC_UNSUPPORTED` in that script: +`Intel Mac is not supported yet.` -Intel Mac is not supported yet. There is no signed download and no other -install path for it. The source-home commands later in this runbook are a -developer build for Apple silicon. They are not an install path for an Intel -Mac, and this note does not claim that build works on Intel. +There is no signed download and no other install path for an Intel Mac. The +source-home commands later in this runbook are a developer build for Apple +silicon. They are not an install path for an Intel Mac, and this note does not +claim that build works on Intel. These files hold the rules: diff --git a/scripts/gateway-healthz-smoke-test.py b/scripts/gateway-healthz-smoke-test.py index 46a468b5d..0b860a016 100644 --- a/scripts/gateway-healthz-smoke-test.py +++ b/scripts/gateway-healthz-smoke-test.py @@ -411,9 +411,10 @@ def test_contract_names_the_command_and_url_this_smoke_uses(self): install = (ROOT / "scripts/install.sh").read_text() # This smoke is no Intel download. install.sh stops an Intel Mac in # detect_platform before release lookup, and MAC.md quotes that stop. - message = ("This Mac has an Intel processor. ElastOS supports Macs with " - "Apple silicon (M1 or later). Intel Mac is not supported yet.") - self.assertIn(f'[[ "$ARCH" == aarch64 ]] || die "{message}"', install) + message = "Intel Mac is not supported yet." + self.assertIn(f'INTEL_MAC_UNSUPPORTED="{message}"', install) + self.assertIn('[[ "$ARCH" == aarch64 ]] || die "$INTEL_MAC_UNSUPPORTED"', install) + self.assertEqual(install.count(message), 1) main = install.split("# ── Main", 1)[1] self.assertLess(main.index("detect_platform"), main.index("fetch_url")) self.assertLess(main.index("detect_platform"), main.index("release-head.json")) @@ -421,6 +422,8 @@ def test_contract_names_the_command_and_url_this_smoke_uses(self): # The stop names no developer build as a way to install. self.assertNotIn("source-home", message) for retired in ["macOS release lookup requires Apple silicon", + "This Mac has an Intel processor", + "ElastOS supports Macs with Apple silicon", "Intel Mac has no signed release yet", "Use the source-home path in docs/MAC.md"]: self.assertNotIn(retired, install + mac) diff --git a/scripts/install-bootstrap-test.py b/scripts/install-bootstrap-test.py index 930205c35..0d7156274 100644 --- a/scripts/install-bootstrap-test.py +++ b/scripts/install-bootstrap-test.py @@ -32,8 +32,29 @@ INSTALLER = Path(__file__).with_name("install.sh") SANDBOX_BASE = Path(__file__).resolve().parent.parent / "target/installer-sandboxes" SOURCE = INSTALLER.read_text() -INTEL_MAC_STOP = ("This Mac has an Intel processor. ElastOS supports Macs with " - "Apple silicon (M1 or later). Intel Mac is not supported yet.") +# Decision 4: the plain stop, and nothing before it. +INTEL_MAC_STOP = "Intel Mac is not supported yet." +ANSI = re.compile(r"\x1b\[[0-9;]*m") +# sysctl -in answers for (sysctl.proc_translated, hw.optional.arm64). None +# means sysctl fails; a missing command is its own case. +INTEL_OR_UNPROVEN = [("0", "0"), ("", ""), ("0", ""), ("2", "0"), None, "missing"] + + +def sysctl_function(answers): + """A bash sysctl() for detect_platform: answers, a failure (None) or a missing command.""" + if answers == "missing": + return 'sysctl() { echo "bash: sysctl: command not found" >&2; return 127; }' + if answers is None: + return 'sysctl() { echo "sysctl: cannot read" >&2; return 1; }' + translated, arm64 = answers + return ('sysctl() { [ "$1" = -in ] || return 2; case "$2" in ' + f'sysctl.proc_translated) echo "{translated}";; hw.optional.arm64) echo "{arm64}";; ' + '*) return 2;; esac; }') + + +def sysctl_mock(answers): + """The same answers as an executable for a full installer run.""" + return "#!/bin/bash\n" + sysctl_function(answers) + '\nsysctl "$@"\n' if "# ── Main" not in SOURCE: raise SystemExit("install.sh is missing the '# ── Main' helper marker") HELPERS = SOURCE.split("# ── Main", 1)[0] @@ -1585,16 +1606,19 @@ def test_three_platform_release_keys(self): self.assertEqual(result.stdout.strip(), expected) def test_intel_mac_stops_before_release_lookup(self): - result = shell(''' + # uname says x86_64 on an Intel Mac and in a Rosetta shell. Without the + # kernel's word for Apple silicon, the plain stop is the answer. + for answers in INTEL_OR_UNPROVEN: + result = shell(sysctl_function(answers) + ''' SYSTEM=Darwin MACHINE=x86_64 uname() { case "$1" in -s) echo "$SYSTEM";; -m) echo "$MACHINE";; esac; } detect_platform printf '%s\\n' "$PLATFORM" ''') - self.assertEqual(result.returncode, 1, result.stdout) - self.assertEqual(result.stdout, "") - self.assertIn(INTEL_MAC_STOP, result.stderr) - self.assertNotIn("source-home", result.stderr) + with self.subTest(sysctl=answers): + self.assertEqual(result.returncode, 1, result.stdout) + self.assertEqual(result.stdout, "") + self.assertEqual(ANSI.sub("", result.stderr), "Error: " + INTEL_MAC_STOP + "\n") main = SOURCE.split("# ── Main", 1)[1] self.assertLess(main.index("detect_platform"), main.index("fetch_url")) self.assertLess(main.index("detect_platform"), main.index("release-head.json")) @@ -1606,32 +1630,39 @@ def test_intel_mac_full_run_downloads_nothing(self): SANDBOX_BASE.mkdir(parents=True, exist_ok=True) for path in (SANDBOX_BASE.parent, SANDBOX_BASE): path.chmod(0o755) - with tempfile.TemporaryDirectory(prefix="installer-intel-", dir=SANDBOX_BASE) as directory: - root = Path(directory) - mocks = root / "mocks" - home = root / "home" - mocks.mkdir() - home.mkdir() - calls = root / "calls" - (mocks / "uname").write_text('#!/bin/sh\ncase "$1" in -s) echo Darwin;; -m) echo x86_64;; *) echo Darwin;; esac\n') - for tool in ["curl", "wget"]: - (mocks / tool).write_text(f'#!/bin/sh\necho {tool} "$@" >> "$CALLS"\nexit 97\n') - for mock in mocks.iterdir(): - mock.chmod(0o755) - # A configured publisher, so only the platform stop can end the run - # before the release-head fetch. - env = dict(os.environ, HOME=str(home), CALLS=str(calls), - ELASTOS_PUBLISHER_GATEWAY="https://publisher.invalid", - ELASTOS_MAINTAINER_DID="did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK", - PATH=str(mocks) + os.pathsep + os.environ["PATH"]) - for name in ["ELASTOS_INSTALL_ONLY", "XDG_DATA_HOME"]: - env.pop(name, None) - result = subprocess.run([OPTIONS.bash, "--noprofile", "--norc", str(INSTALLER.resolve())], - env=env, text=True, capture_output=True, timeout=15) - self.assertFalse(calls.exists(), calls.read_text() if calls.exists() else "") - self.assertEqual(list(home.iterdir()), []) - self.assertEqual(result.returncode, 1, result.stdout + result.stderr) - self.assertIn(INTEL_MAC_STOP, result.stderr) + # "missing" leaves sysctl off PATH entirely. + system_path = [entry for entry in os.environ["PATH"].split(os.pathsep) + if entry and not Path(entry, "sysctl").exists()] + for answers in [("0", "0"), None, "missing"]: + with self.subTest(sysctl=answers), \ + tempfile.TemporaryDirectory(prefix="installer-intel-", dir=SANDBOX_BASE) as directory: + root = Path(directory) + mocks = root / "mocks" + home = root / "home" + mocks.mkdir() + home.mkdir() + calls = root / "calls" + (mocks / "uname").write_text('#!/bin/sh\ncase "$1" in -s) echo Darwin;; -m) echo x86_64;; *) echo Darwin;; esac\n') + if answers != "missing": + (mocks / "sysctl").write_text(sysctl_mock(answers)) + for tool in ["curl", "wget"]: + (mocks / tool).write_text(f'#!/bin/sh\necho {tool} "$@" >> "$CALLS"\nexit 97\n') + for mock in mocks.iterdir(): + mock.chmod(0o755) + # A configured publisher, so only the platform stop can end the run + # before the release-head fetch. + env = dict(os.environ, HOME=str(home), CALLS=str(calls), + ELASTOS_PUBLISHER_GATEWAY="https://publisher.invalid", + ELASTOS_MAINTAINER_DID="did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK", + PATH=os.pathsep.join([str(mocks)] + system_path)) + for name in ["ELASTOS_INSTALL_ONLY", "XDG_DATA_HOME"]: + env.pop(name, None) + result = subprocess.run([OPTIONS.bash, "--noprofile", "--norc", str(INSTALLER.resolve())], + env=env, text=True, capture_output=True, timeout=15) + self.assertFalse(calls.exists(), calls.read_text() if calls.exists() else "") + self.assertEqual(list(home.iterdir()), []) + self.assertEqual(result.returncode, 1, result.stdout + result.stderr) + self.assertEqual(result.stderr, "Error: " + INTEL_MAC_STOP + "\n") def test_platform_paths_match_rust_dirs5_contract(self): # setup.rs and sources.rs use dirs5.0.1::data_dir. Mac ignores XDG; diff --git a/scripts/install.sh b/scripts/install.sh index 83917230d..c086e789b 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -110,7 +110,7 @@ show_help() { echo "" echo -e "${BOLD}ElastOS Installer${NC}" echo " Release lookup: Linux x86_64/aarch64 and macOS Apple silicon." - echo " Intel Mac is not supported yet." + echo " ${INTEL_MAC_UNSUPPORTED}" echo " The signed release determines which platforms have downloads." echo "" echo -e "${BOLD}Usage:${NC}" @@ -319,6 +319,9 @@ installer_data_dir() { esac } +# The one Intel Mac stop (docs/MAC.md#intel-mac quotes it). +INTEL_MAC_UNSUPPORTED="Intel Mac is not supported yet." + detect_platform() { OS=$(uname -s | tr '[:upper:]' '[:lower:]') ARCH=$(uname -m) @@ -333,7 +336,7 @@ detect_platform() { case "${OS}" in linux) PLATFORM="${ARCH}-linux" ;; darwin) - [[ "$ARCH" == aarch64 ]] || die "This Mac has an Intel processor. ElastOS supports Macs with Apple silicon (M1 or later). Intel Mac is not supported yet." + [[ "$ARCH" == aarch64 ]] || die "$INTEL_MAC_UNSUPPORTED" PLATFORM="aarch64-darwin" ;; *) die "Unsupported OS: ${OS}" ;; From 88ec250f5838be8ca2ca4dcec4a3a408f2938354 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 17 Oct 2026 12:12:36 +0000 Subject: [PATCH 07/11] fix: install the Apple silicon build from a Rosetta Terminal A Terminal under Rosetta on an M1 or later Mac reports uname -m x86_64, so the installer told an Apple silicon Mac that it was an Intel Mac. On Darwin x86_64, detect_platform now asks the kernel. When sysctl -in sysctl.proc_translated or sysctl -in hw.optional.arm64 prints 1, the platform is aarch64-darwin. That build is arm64 only, so macOS runs it natively when the Rosetta shell starts it. Any other answer, a failed sysctl or a missing sysctl proves nothing, so the installer stops with the Intel Mac message before any download. Linux never asks sysctl. Tests (install-bootstrap-test.py, sysctl and uname faked): - test_rosetta_shell_on_apple_silicon_is_apple_silicon: four Rosetta or arm64 answers give aarch64-darwin; Linux x86_64 stays x86_64-linux. - test_intel_mac_stops_before_release_lookup: 0/0, empty, 0/empty, 2/0, a failing sysctl and a missing sysctl all stop with exit 1. - test_intel_mac_full_run_downloads_nothing: the whole installer with an Intel answer, a failing sysctl, and no sysctl on PATH runs no curl and writes nothing in HOME. - test_rosetta_shell_installs_the_apple_silicon_build_and_intel_downloads_nothing: one sandbox; the Intel answer makes no request, then the Rosetta answer fetches artifacts/elastos-aarch64-darwin and installs it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01W6Qn9RX7LDqrwiAfNC2F2K --- docs/MAC.md | 8 ++++++ scripts/install-bootstrap-test.py | 48 +++++++++++++++++++++++++++++++ scripts/install.sh | 14 +++++++++ 3 files changed, 70 insertions(+) diff --git a/docs/MAC.md b/docs/MAC.md index afc6b84e6..6d03bae36 100644 --- a/docs/MAC.md +++ b/docs/MAC.md @@ -19,6 +19,14 @@ source-home commands later in this runbook are a developer build for Apple silicon. They are not an install path for an Intel Mac, and this note does not claim that build works on Intel. +A Terminal that runs under Rosetta on an Apple silicon Mac also reports +`uname -m` as `x86_64`. The installer then asks the kernel: +`sysctl -in sysctl.proc_translated` or `sysctl -in hw.optional.arm64` must +print `1`. If one does, it installs the signed `aarch64-darwin` build. That +build has only arm64 code, so macOS runs it natively, also when a Rosetta shell +starts it. If neither prints `1`, or `sysctl` is missing or fails, the +installer cannot prove Apple silicon and stops with the Intel Mac message. + These files hold the rules: - Signing: the release platform allowlist in `scripts/release-signer.py`. diff --git a/scripts/install-bootstrap-test.py b/scripts/install-bootstrap-test.py index 0d7156274..4d8cece16 100644 --- a/scripts/install-bootstrap-test.py +++ b/scripts/install-bootstrap-test.py @@ -37,6 +37,7 @@ ANSI = re.compile(r"\x1b\[[0-9;]*m") # sysctl -in answers for (sysctl.proc_translated, hw.optional.arm64). None # means sysctl fails; a missing command is its own case. +ROSETTA_ON_APPLE_SILICON = [("1", "1"), ("1", ""), ("", "1"), ("0", "1")] INTEL_OR_UNPROVEN = [("0", "0"), ("", ""), ("0", ""), ("2", "0"), None, "missing"] @@ -617,6 +618,12 @@ def __exit__(self, *exc): def respond(self, name, data): (self.responses / name).write_bytes(data) + def mock(self, name, text): + """Put one more executable ahead of the system on the installer's PATH.""" + path = self.root / "mocks" / name + path.write_text(text) + path.chmod(0o755) + def requests(self): return (self.root / "requests").read_text().splitlines() @@ -870,6 +877,29 @@ def test_stamped_head_cid_must_match_the_verified_head(self): sources = json.loads((sandbox.root / "candidate" / "sources.json").read_text()) self.assertEqual(sources["sources"][0]["head_cid"], verified) + def test_rosetta_shell_installs_the_apple_silicon_build_and_intel_downloads_nothing(self): + # Terminal under Rosetta on an M1 or later Mac: uname -m says x86_64, + # sysctl.proc_translated says 1. The signed aarch64-darwin build is + # fetched, checked and installed. A native Intel answer stops first. + binary = {"cid": "binary-a", "sha256": hashlib.sha256(RUNTIME_STUB).hexdigest(), + "size": len(RUNTIME_STUB)} + did, head, release = installable_fixture(release_extra={"platforms": {"aarch64-darwin": { + "binary": binary, "components": {"cid": "components-a", "sha256": "c" * 64, "size": 1}}}}) + with InstallerSandbox(head, release, did, system="Darwin", machine="x86_64") as sandbox: + sandbox.respond("binary", RUNTIME_STUB) + sandbox.mock("sysctl", sysctl_mock(("0", "0"))) + intel, intel_requests = sandbox.run("--install-only") + self.assertEqual(intel.returncode, 1, intel.stdout + intel.stderr) + self.assertEqual(intel.stderr, "Error: " + INTEL_MAC_STOP + "\n") + self.assertEqual(intel_requests, []) + self.assertFalse(sandbox.binary.exists()) + sandbox.mock("sysctl", sysctl_mock(("1", "1"))) + rosetta, requests = sandbox.run("--install-only") + self.assertEqual(rosetta.returncode, 0, rosetta.stdout + rosetta.stderr) + self.assertIn("(aarch64-darwin)", rosetta.stdout) + self.assertEqual(requests, self.REQUESTS[:2] + ["https://test.invalid/artifacts/elastos-aarch64-darwin"]) + self.assertEqual(sandbox.binary.read_bytes(), RUNTIME_STUB) + def test_installer_prints_the_selected_maintainer_did_before_verification(self): did, head, release = installable_fixture() with InstallerSandbox(head, release, did) as sandbox: @@ -1623,6 +1653,24 @@ def test_intel_mac_stops_before_release_lookup(self): self.assertLess(main.index("detect_platform"), main.index("fetch_url")) self.assertLess(main.index("detect_platform"), main.index("release-head.json")) + def test_rosetta_shell_on_apple_silicon_is_apple_silicon(self): + # Terminal under Rosetta reports uname -m x86_64 on an M1 or later Mac. + for answers in ROSETTA_ON_APPLE_SILICON: + result = shell(sysctl_function(answers) + ''' +uname() { case "$1" in -s) echo Darwin;; -m) echo x86_64;; esac; } +detect_platform +printf '%s\\n' "$PLATFORM" +''') + with self.subTest(sysctl=answers): + self.assertEqual((result.returncode, result.stdout), (0, "aarch64-darwin\n"), result.stderr) + # Linux never asks sysctl, so a Linux x86_64 host stays x86_64. + linux = shell(sysctl_function(("1", "1")) + ''' +uname() { case "$1" in -s) echo Linux;; -m) echo x86_64;; esac; } +detect_platform +printf '%s\\n' "$PLATFORM" +''') + self.assertEqual(linux.stdout, "x86_64-linux\n", linux.stderr) + def test_intel_mac_full_run_downloads_nothing(self): # The whole installer, not only detect_platform: an Intel Mac exits 1 # with the stop message before any network tool runs or any file is written. diff --git a/scripts/install.sh b/scripts/install.sh index c086e789b..6c9dc4c6f 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -322,6 +322,14 @@ installer_data_dir() { # The one Intel Mac stop (docs/MAC.md#intel-mac quotes it). INTEL_MAC_UNSUPPORTED="Intel Mac is not supported yet." +# A Terminal under Rosetta on an M1 or later Mac reports uname -m x86_64. +# The kernel still says the processor is Apple silicon. sysctl -i prints +# nothing for a key this macOS lacks, which an Intel Mac does for both. +mac_has_apple_silicon() { + [[ "$(sysctl -in sysctl.proc_translated 2>/dev/null)" == 1 || + "$(sysctl -in hw.optional.arm64 2>/dev/null)" == 1 ]] +} + detect_platform() { OS=$(uname -s | tr '[:upper:]' '[:lower:]') ARCH=$(uname -m) @@ -336,6 +344,12 @@ detect_platform() { case "${OS}" in linux) PLATFORM="${ARCH}-linux" ;; darwin) + # Only the kernel's answer turns x86_64 into Apple silicon. A + # missing, failed or unclear sysctl proves nothing, so it stops + # here like an Intel Mac, before any download. + if [[ "$ARCH" == x86_64 ]] && mac_has_apple_silicon; then + ARCH="aarch64" + fi [[ "$ARCH" == aarch64 ]] || die "$INTEL_MAC_UNSUPPORTED" PLATFORM="aarch64-darwin" ;; From 51404a76e7df4be04213db8a966afdc3d1ee1605 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 17 Oct 2026 12:27:45 +0000 Subject: [PATCH 08/11] fix: use only the release bytes the installer checked The installer checked release-head.json, release.json and the Runtime, then read or copied them again from the temporary folder. Nothing tied those later reads to the bytes that passed. A process that could change that folder between the check and the use could: - put a head signed by its own key in place, and sources.json pinned that key as the publisher, read again from the file (d["signer_did"]); - put another Runtime in place after its SHA-256 passed. The installer ran that copy (--version, install-release) and installed it. Now: - verify_signature reads the envelope once and returns the SHA-256 of the bytes whose signature passed (VERIFIED_SHA256). - Every later read (bind_verified_head_cid, json_get, validate_release_identity) takes that SHA-256 and stops with "The release files changed on this computer after they were checked. Nothing was installed. Run the installer again." for other bytes. - sources.json pins MAINTAINER_DID, the trust root that verify_signature compared with the signer. It is not read again. - The staged Runtime copy in the install directory is checked again after the copy. That copy is the file the installer runs and hands to the writer. The writer reads each file once, checks those bytes against sources.json and installs those same bytes (update.rs install_release). Tests (install-bootstrap-test.py): - test_runtime_swapped_after_its_checksum_check_is_not_run_or_installed: a sha256sum hook swaps the download in TMPDIR after it hashes it. The swapped Runtime never runs and nothing is installed. Without the staged check, the swapped Runtime runs and is installed. - test_release_head_swapped_after_its_signature_check_is_refused: a python3 hook swaps in an attacker-signed head after its signature passes. The run stops after one request. Without the SHA-256 checks it installs. - test_publisher_pin_is_the_trust_root_not_a_second_read: the head is swapped after the last read; sources.json still pins the maintainer DID. With the old d["signer_did"] read, it pins the attacker key. - test_reads_after_the_signature_check_refuse_other_bytes: each read refuses one changed byte. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01W6Qn9RX7LDqrwiAfNC2F2K --- scripts/install-bootstrap-test.py | 140 ++++++++++++++++++++++++++++-- scripts/install.sh | 109 ++++++++++++++++------- 2 files changed, 207 insertions(+), 42 deletions(-) diff --git a/scripts/install-bootstrap-test.py b/scripts/install-bootstrap-test.py index 4d8cece16..cc13c10a5 100644 --- a/scripts/install-bootstrap-test.py +++ b/scripts/install-bootstrap-test.py @@ -22,6 +22,7 @@ import signal import ssl import subprocess +import sys import tempfile import threading import time @@ -88,6 +89,10 @@ def unixfs_v0_cid(data): _SIGNER_CID = None +def sha256_files(paths): + return [hashlib.sha256(Path(path).read_bytes()).hexdigest() for path in paths] + + def binding_fixture(): # Fixed OpenSSL Ed25519 vectors from the disposable seed [7; 32]. This is # a test identity. Running these stdlib tests needs neither a key nor OpenSSL. @@ -491,7 +496,7 @@ def test_signed_version_syntax_matches_release_policy(self): "release_sha256": hashlib.sha256(paths[1].read_bytes()).hexdigest()}} paths[0].write_text(json.dumps(head)) expected = subprocess.run([OPTIONS.bash, str(policy), version], capture_output=True) - actual = shell('validate_release_identity "$1" "$2"\n', *paths) + actual = shell('validate_release_identity "$1" "$2" "$3" "$4"\n', *paths, *sha256_files(paths)) with self.subTest(version=version): self.assertEqual(expected.returncode == 0, version in accepted_versions, expected.stdout + expected.stderr) @@ -520,10 +525,42 @@ def test_release_identity_requires_matching_nonempty_strings(self): first = copy.deepcopy(first) first["payload"]["release_sha256"] = hashlib.sha256(paths[1].read_bytes()).hexdigest() paths[0].write_text(json.dumps(first)) - result = shell('validate_release_identity "$1" "$2"\n', *paths) + result = shell('validate_release_identity "$1" "$2" "$3" "$4"\n', *paths, *sha256_files(paths)) with self.subTest(case=index): self.assertEqual(result.returncode == 0, accepted, result.stdout + result.stderr) + def test_reads_after_the_signature_check_refuse_other_bytes(self): + # Every later read of a verified envelope names the SHA-256 that the + # signature check saw. One changed byte must stop each of them. + did, head, release = installable_fixture() + with tempfile.TemporaryDirectory() as directory: + paths = [Path(directory, "release-head.json"), Path(directory, "release.json")] + paths[0].write_bytes(head) + paths[1].write_bytes(release) + verified = shell('verify_signature "$1" elastos.release.head.v1 "$2"\nprintf "%s\\n" "$VERIFIED_SHA256"\n', + paths[0], did) + self.assertEqual(verified.stdout.splitlines()[-1], hashlib.sha256(head).hexdigest(), verified.stderr) + head_sha256, release_sha256 = sha256_files(paths) + reads = { + "json_get": ('json_get "$1" \'d["payload"]["version"]\' "$3"\n', "0.7.1"), + "bind_verified_head_cid": ('HEAD_CID=""\nbind_verified_head_cid "$1" "$3"\n', ""), + "validate_release_identity": ('validate_release_identity "$1" "$2" "$3" "$4"\n', ""), + } + for name, (script, output) in reads.items(): + with self.subTest(read=name, bytes="verified"): + paths[0].write_bytes(head) + result = shell(script, *paths, head_sha256, release_sha256) + self.assertEqual((result.returncode, result.stdout.strip()), (0, output), result.stderr) + with self.subTest(read=name, bytes="changed"): + paths[0].write_bytes(head.replace(b'"updated_at":1', b'"updated_at":2')) + self.assertNotEqual(paths[0].read_bytes(), head) + result = shell(script, *paths, head_sha256, release_sha256) + self.assertNotEqual(result.returncode, 0) + self.assertNotIn("0.7.1", result.stdout) + if name != "json_get": + self.assertIn("The release files changed on this computer after they were checked", + result.stderr) + def test_verified_head_cid_is_the_hashed_bytes(self): vector = b'{"payload":{"version":"0.7.1-rc.2"}}\n' expected = "QmZFMnZjkiTqy9VY5FDdKudvpsKtKxifk1poBrV7k4sqp8" @@ -531,18 +568,19 @@ def test_verified_head_cid_is_the_hashed_bytes(self): with tempfile.TemporaryDirectory() as directory: path = Path(directory, "release-head.json") path.write_bytes(vector) - stored = shell('HEAD_CID=""\nbind_verified_head_cid "$1"\nprintf "%s\\n" "$HEAD_CID"\n', path) + digest = hashlib.sha256(vector).hexdigest() + stored = shell('HEAD_CID=""\nbind_verified_head_cid "$1" "$2"\nprintf "%s\\n" "$HEAD_CID"\n', path, digest) self.assertEqual((stored.returncode, stored.stdout.strip()), (0, ""), stored.stderr) - same = shell('HEAD_CID="$2"\nbind_verified_head_cid "$1"\nprintf "%s\\n" "$HEAD_CID"\n', - path, expected) + same = shell('HEAD_CID="$3"\nbind_verified_head_cid "$1" "$2"\nprintf "%s\\n" "$HEAD_CID"\n', + path, digest, expected) self.assertEqual((same.returncode, same.stdout.strip()), (0, expected), same.stderr) - mismatch = shell('HEAD_CID="QmStampNamesOtherBytes"\nbind_verified_head_cid "$1"\n', path) + mismatch = shell('HEAD_CID="QmStampNamesOtherBytes"\nbind_verified_head_cid "$1" "$2"\n', path, digest) self.assertEqual(mismatch.returncode, 1, mismatch.stdout) self.assertIn("Stamped head CID differs from the verified release head", mismatch.stderr) self.assertIn("Stamp: QmStampNamesOtherBytes", mismatch.stderr) self.assertIn("Verified: " + expected, mismatch.stderr) path.write_bytes(b"") - empty = shell('HEAD_CID=""\nbind_verified_head_cid "$1"\n', path) + empty = shell('HEAD_CID=""\nbind_verified_head_cid "$1" "$2"\n', path, hashlib.sha256(b"").hexdigest()) self.assertEqual(empty.returncode, 1, empty.stdout) self.assertIn("no single-block CID", empty.stderr) @@ -777,8 +815,9 @@ def test_exact_signed_release_required_even_at_same_version(self): paths[1].write_bytes(candidate) result = shell(''' verify_signature "$1" elastos.release.head.v1 "$3" +head_sha256="$VERIFIED_SHA256" verify_signature "$2" elastos.release.v1 "$3" -validate_release_identity "$1" "$2" +validate_release_identity "$1" "$2" "$head_sha256" "$VERIFIED_SHA256" ''', *paths, did) with self.subTest(case=name): self.assertEqual(result.stdout.count("Signature verified"), 2, result.stderr) @@ -793,7 +832,7 @@ def test_missing_or_invalid_digest_fails_closed(self): first = json.loads(head) first["payload"]["release_sha256"] = value paths[0].write_text(json.dumps(first)) - result = shell('validate_release_identity "$1" "$2"\n', *paths) + result = shell('validate_release_identity "$1" "$2" "$3" "$4"\n', *paths, *sha256_files(paths)) with self.subTest(value=value): self.assertNotEqual(result.returncode, 0) self.assertIn("Release identity check failed", result.stderr) @@ -900,6 +939,89 @@ def test_rosetta_shell_installs_the_apple_silicon_build_and_intel_downloads_noth self.assertEqual(requests, self.REQUESTS[:2] + ["https://test.invalid/artifacts/elastos-aarch64-darwin"]) self.assertEqual(sandbox.binary.read_bytes(), RUNTIME_STUB) + def test_runtime_swapped_after_its_checksum_check_is_not_run_or_installed(self): + # Another process changes the download in the temporary folder right + # after its SHA-256 passed. The installer runs and installs only bytes + # it checked, so the swapped Runtime never runs. + did, head, release = installable_fixture() + swapped = RUNTIME_STUB.replace(b"#!/bin/bash\n", b'#!/bin/bash\necho ran >> "$FIXTURES/swapped-ran"\n', 1) + self.assertNotEqual(swapped, RUNTIME_STUB) + with InstallerSandbox(head, release, did) as sandbox: + sandbox.respond("binary", RUNTIME_STUB) + (sandbox.root / "swap-binary").write_bytes(swapped) + sandbox.mock("sha256sum", f'''#!/bin/sh +"{sys.executable}" -I -c 'import hashlib, sys; print(hashlib.sha256(open(sys.argv[1], "rb").read()).hexdigest(), sys.argv[1])' "$1" || exit 1 +case "$1" in "$TMPDIR"/*) cp "$FIXTURES/swap-binary" "$1";; esac +''') + result, requests = sandbox.run("--install-only") + self.assertFalse((sandbox.root / "swapped-ran").exists(), result.stdout + result.stderr) + self.assertEqual(result.returncode, 1, result.stdout + result.stderr) + self.assertIn("The downloaded Runtime changed on this computer after its check", result.stderr) + self.assertEqual(requests, self.REQUESTS) + self.assertFalse(sandbox.binary.exists()) + self.assertEqual(list(sandbox.binary.parent.iterdir()), []) + self.assertEqual(sandbox.runtime_calls(), []) + + def test_release_head_swapped_after_its_signature_check_is_refused(self): + # Another process replaces release-head.json right after its signature + # passed, with a head signed by its own key. Nothing after the check may + # read those new bytes, and no other key may become the publisher pin. + did, head, release = installable_fixture() + payload = json.loads(head)["payload"] + seed = bytes([9]) * 32 + attacker_did = sign_envelope({}, "elastos.release.head.v1", seed=seed)["signer_did"] + attacker = sign_envelope(dict(payload, signer_did=attacker_did), "elastos.release.head.v1", seed=seed) + self.assertNotEqual(attacker["signer_did"], did) + with InstallerSandbox(head, release, did) as sandbox: + sandbox.respond("binary", RUNTIME_STUB) + (sandbox.root / "swap-head").write_bytes(encode_envelope(attacker)) + sandbox.mock("python3", f'''#!/bin/sh +"{sys.executable}" "$@" +status=$? +if [ "$status" = 0 ] && [ "$1" = - ] && [ "$3" = elastos.release.head.v1 ]; then + cp "$FIXTURES/swap-head" "$2" +fi +exit "$status" +''') + result, requests = sandbox.run("--install-only") + candidate = sandbox.root / "candidate" / "sources.json" + pinned = json.loads(candidate.read_text())["sources"][0]["publisher_dids"] if candidate.exists() else None + self.assertEqual(result.returncode, 1, f"pinned publisher_dids={pinned}\n" + result.stdout + result.stderr) + self.assertIn("The release files changed on this computer after they were checked", result.stderr) + self.assertEqual(requests, self.REQUESTS[:1]) + self.assertFalse(sandbox.binary.exists()) + self.assertIsNone(pinned) + + def test_publisher_pin_is_the_trust_root_not_a_second_read(self): + # The head is swapped after the installer's last read of it, so only the + # writer reads the new bytes. sources.json must still pin the maintainer + # DID; the writer then refuses a head signed by any other key + # (update.rs installer_moves_a_canary_home_to_stable_but_never_stable_to_canary). + did, head, release = installable_fixture() + payload = json.loads(head)["payload"] + seed = bytes([9]) * 32 + attacker_did = sign_envelope({}, "elastos.release.head.v1", seed=seed)["signer_did"] + attacker = encode_envelope(sign_envelope(dict(payload, signer_did=attacker_did), + "elastos.release.head.v1", seed=seed)) + with InstallerSandbox(head, release, did) as sandbox: + sandbox.respond("binary", RUNTIME_STUB) + (sandbox.root / "swap-head").write_bytes(attacker) + # After validate_release_identity, the last check that reads both files. + sandbox.mock("python3", f'''#!/bin/sh +"{sys.executable}" "$@" +status=$? +if [ "$status" = 0 ] && [ "$1" = - ] && [ "${{2##*/}}" = release-head.json ] && [ "${{3##*/}}" = release.json ]; then + cp "$FIXTURES/swap-head" "$2" +fi +exit "$status" +''') + result, requests = sandbox.run("--install-only") + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + candidate = sandbox.root / "candidate" + self.assertEqual((candidate / "release-head.json").read_bytes(), attacker) + sources = json.loads((candidate / "sources.json").read_text()) + self.assertEqual(sources["sources"][0]["publisher_dids"], [did]) + def test_installer_prints_the_selected_maintainer_did_before_verification(self): did, head, release = installable_fixture() with InstallerSandbox(head, release, did) as sandbox: diff --git a/scripts/install.sh b/scripts/install.sh index 6c9dc4c6f..05732ebb8 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -155,6 +155,8 @@ fi die() { echo -e "${RED:-}Error:${NC:-} $*" >&2; exit 1; } # For messages holding %q-quoted paths: print them byte for byte. die_exact() { printf '%bError:%b %s\n' "${RED:-}" "${NC:-}" "$*" >&2; exit 1; } +# Every read of a verified envelope must see the SHA-256 its signature check saw. +RELEASE_CHANGED="The release files changed on this computer after they were checked. Nothing was installed. Run the installer again." # Colour, the banner and the step layout are for a person at a terminal. # Pipes, CI, NO_COLOR and dumb terminals get plain [OK] and [WARN] lines. @@ -678,13 +680,18 @@ fetch_url() { # Usage: json_get # json_get release.json 'd["payload"]["schema"]' # json_get release.json 'd["payload"]["platforms"]["aarch64-linux"]["binary"]["cid"]' +# A third argument is the SHA-256 of the verified bytes: other bytes exit 3. json_get() { local file="$1" local expr="$2" - python3 - "$file" "$expr" <<'PY' -import json, sys -with open(sys.argv[1], 'r', encoding='utf-8') as f: - d = json.load(f) + local expected_sha256="${3:-}" + python3 - "$file" "$expr" "$expected_sha256" <<'PY' +import hashlib, json, sys +with open(sys.argv[1], 'rb') as f: + data = f.read() +if sys.argv[3] and hashlib.sha256(data).hexdigest() != sys.argv[3]: + sys.exit(3) +d = json.loads(data.decode('utf-8')) try: v = eval(sys.argv[2], {"d": d}) if v is None: @@ -697,12 +704,13 @@ PY # ── Ed25519 verification ───────────────────────────────────────────── +# Sets VERIFIED_SHA256 to the SHA-256 of the exact bytes whose signature passed. verify_signature() { local json_file="$1" local domain="$2" local expected_did="$3" - if ! python3 - "$json_file" "$domain" "$expected_did" <<'PY_ED25519' + if ! VERIFIED_SHA256="$(python3 - "$json_file" "$domain" "$expected_did" <<'PY_ED25519' # RFC 8032 sections 5.1.3, 5.1.4 and 5.1.7: # https://www.rfc-editor.org/rfc/rfc8032.html#section-5.1 # Verification only: all curve operations below use public data. @@ -812,8 +820,10 @@ def unique_object(pairs): def verify_envelope(json_file, domain, expected_did): - with open(json_file, "r", encoding="utf-8") as source: - envelope = json.load(source, object_pairs_hook=unique_object) + # One read: the signature, and the SHA-256 returned, cover the same bytes. + with open(json_file, "rb") as source: + data = source.read() + envelope = json.loads(data.decode("utf-8"), object_pairs_hook=unique_object) signer = envelope["signer_did"] if not expected_did or signer != expected_did: raise ValueError("Envelope signer differs from the pinned maintainer DID") @@ -831,28 +841,31 @@ def verify_envelope(json_file, domain, expected_did): ensure_ascii=False, allow_nan=False).encode("utf-8") digest = hashlib.sha256(domain.encode("utf-8") + b"\0" + canonical).digest() verify_ed25519(decode_did_key(signer), digest, bytes.fromhex(signature)) + return hashlib.sha256(data).hexdigest() if __name__ == "__main__": try: - verify_envelope(*sys.argv[1:]) + print(verify_envelope(*sys.argv[1:])) except (ValueError, TypeError, KeyError, OSError, AttributeError) as error: print("Signature verification failed: " + str(error), file=sys.stderr) sys.exit(1) PY_ED25519 - then +)"; then die "Signature verification FAILED" fi + [[ "$VERIFIED_SHA256" =~ ^[0-9a-f]{64}$ ]] || die "Signature verification FAILED" ok "Signature verified" } -# Compute the UnixFS CIDv0 of the verified release-head.json bytes. An empty -# stamp stays empty. A non-empty stamp that names those bytes is kept. A stamp -# that names different bytes is refused. +# Compute the UnixFS CIDv0 of the verified release-head.json bytes, whose +# SHA-256 is the second argument. An empty stamp stays empty. A non-empty stamp +# that names those bytes is kept. A stamp that names different bytes is refused. bind_verified_head_cid() { local head_path="$1" - local verified - verified="$(python3 - "$head_path" <<'PY_HEAD_CID' + local expected_sha256="$2" + local verified status=0 + verified="$(python3 - "$head_path" "$expected_sha256" <<'PY_HEAD_CID' import hashlib import sys @@ -865,6 +878,8 @@ def varint(value): return bytes(encoded) data = open(sys.argv[1], "rb").read() +if hashlib.sha256(data).hexdigest() != sys.argv[2]: + sys.exit(3) if not data or len(data) > 256 * 1024: print("release head is outside the single-block UnixFS bound", file=sys.stderr) sys.exit(1) @@ -878,7 +893,9 @@ while number: text = alphabet[digit] + text print(text) PY_HEAD_CID -)" || die "Verified release head has no single-block CID" +)" || status=$? + (( status != 3 )) || die "$RELEASE_CHANGED" + (( status == 0 )) || die "Verified release head has no single-block CID" if [[ -z "$HEAD_CID" ]]; then return 0 fi @@ -888,19 +905,25 @@ PY_HEAD_CID } validate_release_identity() { - # Both envelopes are verified before this check, on every transport. + # Both envelopes are verified before this check, on every transport; $3 and + # $4 are the SHA-256 values of the verified head and release bytes. # The signed digest binds exact envelope bytes; the CID stays content identity. - if ! python3 - "$1" "$2" <<'PY_RELEASE_IDENTITY' + local status=0 + python3 - "$1" "$2" "$3" "$4" <<'PY_RELEASE_IDENTITY' || status=$? import hashlib import json import re import sys try: - with open(sys.argv[1], encoding="utf-8") as source: - head = json.load(source)["payload"] + with open(sys.argv[1], "rb") as source: + head_bytes = source.read() with open(sys.argv[2], "rb") as source: release_bytes = source.read() + if (hashlib.sha256(head_bytes).hexdigest() != sys.argv[3] + or hashlib.sha256(release_bytes).hexdigest() != sys.argv[4]): + sys.exit(3) + head = json.loads(head_bytes.decode("utf-8"))["payload"] release = json.loads(release_bytes)["payload"] if head.get("schema") != "elastos.release.head/v1" or release.get("schema") != "elastos.release/v1": raise ValueError("Unexpected release schema") @@ -920,7 +943,8 @@ except (ValueError, TypeError, KeyError, OSError, AttributeError) as error: print("Release identity check failed: " + str(error), file=sys.stderr) sys.exit(1) PY_RELEASE_IDENTITY - then + (( status != 3 )) || die "$RELEASE_CHANGED" + if (( status != 0 )); then die "Release binding or metadata mismatch; retry after the publisher finishes updating" fi } @@ -1347,11 +1371,16 @@ HEAD_SCHEMA=$(json_get "${INSTALL_SCRATCH}/release-head.json" 'd["payload"]["sch info "Verifying release head signature..." verify_signature "${INSTALL_SCRATCH}/release-head.json" "elastos.release.head.v1" "$MAINTAINER_DID" -bind_verified_head_cid "${INSTALL_SCRATCH}/release-head.json" - -RELEASE_CID=$(json_get "${INSTALL_SCRATCH}/release-head.json" 'd["payload"]["latest_release_cid"]') -RELEASE_VERSION=$(json_get "${INSTALL_SCRATCH}/release-head.json" 'd["payload"]["version"]') -RELEASE_CHANNEL=$(json_get "${INSTALL_SCRATCH}/release-head.json" 'd["payload"].get("channel","stable")') +# From here on, each read of this file must find these verified bytes. +HEAD_SHA256="$VERIFIED_SHA256" +bind_verified_head_cid "${INSTALL_SCRATCH}/release-head.json" "$HEAD_SHA256" + +RELEASE_CID=$(json_get "${INSTALL_SCRATCH}/release-head.json" 'd["payload"]["latest_release_cid"]' "$HEAD_SHA256") \ + || die "$RELEASE_CHANGED" +RELEASE_VERSION=$(json_get "${INSTALL_SCRATCH}/release-head.json" 'd["payload"]["version"]' "$HEAD_SHA256") \ + || die "$RELEASE_CHANGED" +RELEASE_CHANNEL=$(json_get "${INSTALL_SCRATCH}/release-head.json" 'd["payload"].get("channel","stable")' "$HEAD_SHA256") \ + || die "$RELEASE_CHANGED" if ! is_allowed_channel "$RELEASE_CHANNEL"; then die "Unsupported release channel '${RELEASE_CHANNEL}' in release-head.json. Allowed channels: ${ALLOWED_CHANNELS[*]}" fi @@ -1375,16 +1404,22 @@ RELEASE_SCHEMA=$(json_get "${INSTALL_SCRATCH}/release.json" 'd["payload"]["schem info "Verifying release signature..." verify_signature "${INSTALL_SCRATCH}/release.json" "elastos.release.v1" "$MAINTAINER_DID" -validate_release_identity "${INSTALL_SCRATCH}/release-head.json" "${INSTALL_SCRATCH}/release.json" +RELEASE_SHA256="$VERIFIED_SHA256" +validate_release_identity "${INSTALL_SCRATCH}/release-head.json" "${INSTALL_SCRATCH}/release.json" \ + "$HEAD_SHA256" "$RELEASE_SHA256" # ── Extract platform info ──────────────────────────────────────────── -BINARY_CID=$(json_get "${INSTALL_SCRATCH}/release.json" "d['payload']['platforms']['${PLATFORM}']['binary']['cid']") -BINARY_SHA256=$(json_get "${INSTALL_SCRATCH}/release.json" "d['payload']['platforms']['${PLATFORM}']['binary']['sha256']") -BINARY_SIZE=$(json_get "${INSTALL_SCRATCH}/release.json" "d['payload']['platforms']['${PLATFORM}']['binary']['size']") +BINARY_CID=$(json_get "${INSTALL_SCRATCH}/release.json" "d['payload']['platforms']['${PLATFORM}']['binary']['cid']" "$RELEASE_SHA256") \ + || die "$RELEASE_CHANGED" +BINARY_SHA256=$(json_get "${INSTALL_SCRATCH}/release.json" "d['payload']['platforms']['${PLATFORM}']['binary']['sha256']" "$RELEASE_SHA256") \ + || die "$RELEASE_CHANGED" +BINARY_SIZE=$(json_get "${INSTALL_SCRATCH}/release.json" "d['payload']['platforms']['${PLATFORM}']['binary']['size']" "$RELEASE_SHA256") \ + || die "$RELEASE_CHANGED" if [[ -z "$BINARY_CID" ]]; then - AVAILABLE=$(json_get "${INSTALL_SCRATCH}/release.json" "', '.join(d['payload'].get('platforms',{}).keys())") + AVAILABLE=$(json_get "${INSTALL_SCRATCH}/release.json" "', '.join(d['payload'].get('platforms',{}).keys())" "$RELEASE_SHA256") \ + || die "$RELEASE_CHANGED" die "No release available for platform: ${PLATFORM}\n Available: ${AVAILABLE:-none}" fi @@ -1429,6 +1464,11 @@ require_install_space "$INSTALL_DIR" "$BINARY_SIZE" TMP_INSTALL_BIN="$(mktemp "${INSTALL_DIR}/.elastos.install.XXXXXX")" trap 'rm -rf "$INSTALL_SCRATCH" "$TMP_INSTALL_BIN"' EXIT cp "${INSTALL_SCRATCH}/elastos" "${TMP_INSTALL_BIN}" +# Check the staged copy: it is the file this installer runs and hands to the +# writer. The check above refused a bad download before anything was written. +if [[ "$(sha256_file "${TMP_INSTALL_BIN}")" != "$BINARY_SHA256" ]]; then + die "The downloaded Runtime changed on this computer after its check. Nothing was installed. Run the installer again." +fi chmod +x "${TMP_INSTALL_BIN}" # The staged executable must report the exact release version on stdout with @@ -1447,11 +1487,11 @@ ok "Checksum and version match the signed release" # ── Carrier contact + release metadata for `elastos update` ───────── -SIGNER_DID=$(json_get "${INSTALL_SCRATCH}/release-head.json" 'd["signer_did"]') - SOURCES_PATH="${INSTALL_SCRATCH}/sources.json" +# The publisher pin is the trust root. verify_signature proved that it signed +# the verified head, so the pin is not read again from release-head.json. # A matching stamp is the UnixFS CIDv0 of the verified bytes. An empty stamp stays empty. -PUBLISHER_DID="${SIGNER_DID}" \ +PUBLISHER_DID="${MAINTAINER_DID}" \ PUBLISHER_GATEWAY="${PUBLISHER_GATEWAY}" \ INSTALLED_VERSION="${RELEASE_VERSION}" \ INSTALLED_HEAD_CID="${HEAD_CID}" \ @@ -1507,6 +1547,9 @@ PY # The writer refuses an older release, another channel, a pending Home # update and a concurrent writer before this installer stops Runtime. +# It reads each file below once, checks those bytes against the pin, the +# version, the channel and any head CID stamp in sources.json, and installs +# those same bytes (update.rs install_release). INSTALL_RELEASE=("${TMP_INSTALL_BIN}" install-release --data-dir "$DATA_DIR" --binary "${INSTALL_DIR}/elastos" --candidate "${TMP_INSTALL_BIN}" "$SOURCES_PATH" "${INSTALL_SCRATCH}/release-head.json" "${INSTALL_SCRATCH}/release.json") From f8defe860ee794d3343eae9faa5e2c1a1016b4d9 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 17 Oct 2026 15:19:19 +0000 Subject: [PATCH 09/11] Make the swapped-Runtime test swap the real download on macOS too The round-3 hook swapped a file only when its path was under TMPDIR. Apple's mktemp -d ignores TMPDIR and uses the per-user folder from confstr, so on a Mac nothing was swapped, the install succeeded and the test failed. The hook now swaps the exact path curl wrote, logs the swap, and the test asserts the swap happened before it asserts the refusal. sha256sum and shasum are both mocked. Extra cases run as Darwin with Apple's mktemp layout, a symlinked TMPDIR, and a PATH without sha256sum. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01W6Qn9RX7LDqrwiAfNC2F2K --- scripts/install-bootstrap-test.py | 134 +++++++++++++++++++++++++----- 1 file changed, 113 insertions(+), 21 deletions(-) diff --git a/scripts/install-bootstrap-test.py b/scripts/install-bootstrap-test.py index cc13c10a5..abb518640 100644 --- a/scripts/install-bootstrap-test.py +++ b/scripts/install-bootstrap-test.py @@ -19,6 +19,7 @@ import platform import re import shlex +import shutil import signal import ssl import subprocess @@ -613,6 +614,10 @@ def __init__(self, head, release, did, system="Linux", machine="x86_64"): self.responses = self.root / "responses" for path in (self.home, self.root / "tmp", self.responses, self.root / "mocks"): path.mkdir(parents=True) + # The installer's TMPDIR is this private folder, never the caller's. + (self.root / "tmp").chmod(0o700) + self.tmpdir = self.root / "tmp" + self.path_tail = None (self.root / "requests").write_text("") self.respond("release-head.json", head) self.respond("release.json", release) @@ -642,6 +647,8 @@ def __init__(self, head, release, did, system="Linux", machine="x86_64"): http://*) echo 'curl: (1) Protocol "http" not supported' >&2; exit 1;; esac [ -f "$FIXTURES/responses/$response" ] || { echo artifact-request-blocked >&2; exit 93; } +# Each file curl writes, so a test can name the exact download path. +[ -z "$destination" ] || printf '%s %s\\n' "$response" "$destination" >> "$FIXTURES/downloads" if [ -n "$destination" ]; then cp "$FIXTURES/responses/$response" "$destination"; else cat "$FIXTURES/responses/$response"; fi ''') for path in mocks.iterdir(): @@ -665,18 +672,39 @@ def mock(self, name, text): def requests(self): return (self.root / "requests").read_text().splitlines() + def downloads(self): + path = self.root / "downloads" + return dict(line.split(" ", 1) for line in path.read_text().splitlines()) if path.exists() else {} + + def hide_commands(self, *names): + """Run with a PATH that has no command by these names, mock or system.""" + for name in names: + (self.root / "mocks" / name).unlink(missing_ok=True) + system = self.root / "system-bin" + system.mkdir() + for directory in os.environ["PATH"].split(os.pathsep): + if not os.path.isdir(directory): + continue + for entry in sorted(os.listdir(directory)): + target = os.path.join(directory, entry) + if (entry not in names and not (system / entry).exists() + and os.path.isfile(target) and os.access(target, os.X_OK)): + (system / entry).symlink_to(target) + self.path_tail = system + def run(self, *options, umask="0022", head_cid=""): seen = len(self.requests()) result = shell('umask "$6"\n' + ''' -export HOME="$1/home" XDG_DATA_HOME="$1/home/xdg-data" TMPDIR="$1/tmp" FIXTURES="$1" PATH="$1/mocks:$PATH" +export HOME="$1/home" XDG_DATA_HOME="$1/home/xdg-data" TMPDIR="$8" FIXTURES="$1" PATH="$1/mocks:${9:-$PATH}" # Apple Python otherwise adds bytecode caches to HOME during file-preservation checks. export PYTHONDONTWRITEBYTECODE=1 export ELASTOS_PUBLISHER_GATEWAY="" ELASTOS_HEAD_CID="$7" export ELASTOS_SOURCE_CONNECT_TICKET="" ELASTOS_PUBLISHER_NODE_ID="" ELASTOS_INSTALL_ONLY="" export ELASTOS_TEST_CALLS="$1/calls" ELASTOS_TEST_SETUP_MARKER="$1/setup-marker" MOCK_SYSTEM="$4" MOCK_MACHINE="$5" # Feed the complete stamped script through stdin, as curl | bash does. -cat "$3" | "$2" --noprofile --norc -s -- "${@:8}" +cat "$3" | "$2" --noprofile --norc -s -- "${@:10}" ''', self.root, OPTIONS.bash, self.installer, self.system, self.machine, umask, head_cid, + self.tmpdir, self.path_tail or "", "--maintainer-did", self.did, "--publisher-gateway", "https://test.invalid", *options) return result, self.requests()[seen:] @@ -707,6 +735,38 @@ def run_offline_installer(head, release, did, system="Linux", machine="x86_64"): return result, requests +# sha256sum FILE and shasum -a 256 FILE: print the real hash, then replace the +# file curl downloaded (only that one, only once) and log the swap. +SWAP_HASHER = """import hashlib, pathlib, sys +fixtures, tool, path = pathlib.Path(sys.argv[1]), sys.argv[2], sys.argv[-1] +data = pathlib.Path(path).read_bytes() +print(hashlib.sha256(data).hexdigest() + " " + path, flush=True) +downloads = dict(line.split(" ", 1) for line in (fixtures / "downloads").read_text().splitlines()) +log = fixtures / "swaps" +if path == downloads.get("binary") and not log.exists(): + pathlib.Path(path).write_bytes((fixtures / "swap-binary").read_bytes()) + after = hashlib.sha256(pathlib.Path(path).read_bytes()).hexdigest() + log.write_text(" ".join([tool, path, hashlib.sha256(data).hexdigest(), after]) + "\\n") +""" + + +def mock_swapping_hashers(sandbox): + (sandbox.root / "swap-hasher.py").write_text(SWAP_HASHER) + run = f'exec "{sys.executable}" -I "$FIXTURES/swap-hasher.py" "$FIXTURES"' + sandbox.mock("sha256sum", f'#!/bin/sh\n[ "$#" = 1 ] || exit 2\n{run} sha256sum "$@"\n') + sandbox.mock("shasum", f'#!/bin/sh\n[ "$#" = 3 ] && [ "$1 $2" = "-a 256" ] || exit 2\n{run} shasum "$@"\n') + + +def mock_apple_mktemp(sandbox): + """mktemp as macOS ships it: -d with no template ignores TMPDIR.""" + (sandbox.root / "darwin-user-temp").mkdir(mode=0o700) + real = shutil.which("mktemp") + sandbox.mock("mktemp", f'''#!/bin/sh +if [ "$#" = 1 ] && [ "$1" = -d ]; then exec "{real}" -d "$FIXTURES/darwin-user-temp/tmp.XXXXXXXXXX"; fi +exec "{real}" "$@" +''') + + def changed_paths(before, after): return {key for key in set(before) | set(after) if before.get(key) != after.get(key)} @@ -940,27 +1000,59 @@ def test_rosetta_shell_installs_the_apple_silicon_build_and_intel_downloads_noth self.assertEqual(sandbox.binary.read_bytes(), RUNTIME_STUB) def test_runtime_swapped_after_its_checksum_check_is_not_run_or_installed(self): - # Another process changes the download in the temporary folder right - # after its SHA-256 passed. The installer runs and installs only bytes - # it checked, so the swapped Runtime never runs. - did, head, release = installable_fixture() + # Another process changes the download right after its SHA-256 passed. + # The installer runs and installs only bytes it checked, so the swapped + # Runtime never runs. The swap targets the exact path curl wrote, so it + # does not depend on where mktemp puts the scratch folder: Apple's + # mktemp -d ignores TMPDIR and uses the per-user folder from confstr + # (shell_cmds mktemp.c, implied -t mode), GNU mktemp -d uses TMPDIR. swapped = RUNTIME_STUB.replace(b"#!/bin/bash\n", b'#!/bin/bash\necho ran >> "$FIXTURES/swapped-ran"\n', 1) self.assertNotEqual(swapped, RUNTIME_STUB) - with InstallerSandbox(head, release, did) as sandbox: - sandbox.respond("binary", RUNTIME_STUB) - (sandbox.root / "swap-binary").write_bytes(swapped) - sandbox.mock("sha256sum", f'''#!/bin/sh -"{sys.executable}" -I -c 'import hashlib, sys; print(hashlib.sha256(open(sys.argv[1], "rb").read()).hexdigest(), sys.argv[1])' "$1" || exit 1 -case "$1" in "$TMPDIR"/*) cp "$FIXTURES/swap-binary" "$1";; esac -''') - result, requests = sandbox.run("--install-only") - self.assertFalse((sandbox.root / "swapped-ran").exists(), result.stdout + result.stderr) - self.assertEqual(result.returncode, 1, result.stdout + result.stderr) - self.assertIn("The downloaded Runtime changed on this computer after its check", result.stderr) - self.assertEqual(requests, self.REQUESTS) - self.assertFalse(sandbox.binary.exists()) - self.assertEqual(list(sandbox.binary.parent.iterdir()), []) - self.assertEqual(sandbox.runtime_calls(), []) + binary = {"cid": "binary-a", "sha256": hashlib.sha256(RUNTIME_STUB).hexdigest(), "size": len(RUNTIME_STUB)} + components = {"cid": "components-a", "sha256": hashlib.sha256(COMPONENTS).hexdigest(), + "size": len(COMPONENTS)} + did, head, release = installable_fixture(release_extra={"platforms": { + platform: {"binary": binary, "components": components} + for platform in ("x86_64-linux", "aarch64-darwin")}}) + cases = [ + # (uname -s, uname -m, commands hidden from PATH, Apple mktemp, TMPDIR is a symlink, hasher) + ("Linux", "x86_64", (), False, False, "sha256sum"), + ("Darwin", "arm64", (), True, True, "sha256sum"), + # A Mac without sha256sum: the installer falls back to shasum -a 256. + ("Darwin", "arm64", ("sha256sum",), True, True, "shasum"), + ] + for system, machine, hidden, apple_mktemp, tmp_link, hasher in cases: + with self.subTest(system=system, hasher=hasher), \ + InstallerSandbox(head, release, did, system, machine) as sandbox: + sandbox.respond("binary", RUNTIME_STUB) + (sandbox.root / "swap-binary").write_bytes(swapped) + mock_swapping_hashers(sandbox) + if hidden: + sandbox.hide_commands(*hidden) + if apple_mktemp: + mock_apple_mktemp(sandbox) + if tmp_link: + # macOS /tmp and /var are symlinks into /private. + (sandbox.root / "tmp-link").symlink_to(sandbox.tmpdir) + sandbox.tmpdir = sandbox.root / "tmp-link" + result, requests = sandbox.run("--install-only") + output = result.stdout + result.stderr + # The swap happened, once, to the download, right after it was hashed. + download = sandbox.downloads()["binary"] + swaps = (sandbox.root / "swaps").read_text().split() if (sandbox.root / "swaps").exists() else [] + self.assertEqual(swaps, [hasher, download, hashlib.sha256(RUNTIME_STUB).hexdigest(), + hashlib.sha256(swapped).hexdigest()], output) + scratch = sandbox.root / ("darwin-user-temp" if apple_mktemp else "tmp") + self.assertEqual(Path(download).resolve().parent.parent, scratch.resolve()) + # Then the installer refused it: it never ran, nothing was installed. + self.assertFalse((sandbox.root / "swapped-ran").exists(), output) + self.assertEqual(result.returncode, 1, output) + self.assertIn("The downloaded Runtime changed on this computer after its check", result.stderr) + artifact = "elastos-x86_64-linux" if system == "Linux" else "elastos-aarch64-darwin" + self.assertEqual(requests, self.REQUESTS[:2] + ["https://test.invalid/artifacts/" + artifact]) + self.assertFalse(sandbox.binary.exists()) + self.assertEqual(list(sandbox.binary.parent.iterdir()), []) + self.assertEqual(sandbox.runtime_calls(), []) def test_release_head_swapped_after_its_signature_check_is_refused(self): # Another process replaces release-head.json right after its signature From 112e168f1729c8a76a4f6fdc70daecfe0dc99b91 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 17 Oct 2026 15:29:01 +0000 Subject: [PATCH 10/11] fix: run the staged Runtime from one held file, not by path The installer hashed the staged Runtime by path, then ran it by path three times. A process that replaced or rewrote that path between the hash and a run got its own program run as the installer's writer. The staged copy now sits read-only (0500) in a new 0700 folder made by mktemp -d in the checked install directory, and is opened once on fd 9. Its SHA-256 is taken from that open file. Before each run, a check refuses unless the held file is unchanged (device, inode, size, mtime, ctime, mode 0500, one link, this user) and the path still names it. Where the kernel can start an open file (Linux /proc/self/fd), the held file itself runs, so a path swapped after the last check cannot run. macOS has no way to start an open file, so there the path runs right after the check. Tests: staged swap (rename and in place) between the hash and the run is refused on the Linux and Darwin layouts; a swap after the last check runs only the checked bytes (Linux); held_runtime refuses each kind of change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01W6Qn9RX7LDqrwiAfNC2F2K --- scripts/install-bootstrap-test.py | 174 +++++++++++++++++++++++++++--- scripts/install.sh | 91 ++++++++++++++-- 2 files changed, 240 insertions(+), 25 deletions(-) diff --git a/scripts/install-bootstrap-test.py b/scripts/install-bootstrap-test.py index abb518640..c7263a55d 100644 --- a/scripts/install-bootstrap-test.py +++ b/scripts/install-bootstrap-test.py @@ -562,6 +562,44 @@ def test_reads_after_the_signature_check_refuse_other_bytes(self): self.assertIn("The release files changed on this computer after they were checked", result.stderr) + def test_held_runtime_refuses_any_change_to_the_held_file_or_its_path(self): + # The check behind every staged run, alone. fd 9 holds the staged file. + with tempfile.TemporaryDirectory() as directory: + staged = Path(directory) / "elastos" + expected = hashlib.sha256(RUNTIME_STUB).hexdigest() + setups = { + "unchanged": "", + "path renamed over": 'printf x > "$1.new"; chmod 0500 "$1.new"; mv -f "$1.new" "$1"', + "path removed": 'rm -f "$1"', + "path now a symlink": 'cp "$1" "$1.real"; rm -f "$1"; ln -s "$1.real" "$1"', + "written in place": 'chmod u+w "$1"; printf x >> "$1"; chmod 0500 "$1"', + "made writable": 'chmod u+w "$1"', + "second link": 'ln "$1" "$1.link"', + } + for name, change in setups.items(): + for path in Path(directory).iterdir(): + path.unlink() + staged.write_bytes(RUNTIME_STUB) + staged.chmod(0o500) + result = shell('''exec 9<"$1" +id="$(held_runtime hash "$1" "$2")" || exit 10 +held_runtime check "$1" "$id" || exit 11 +''' + change + ''' +held_runtime check "$1" "$id" || exit 12 +''', staged, expected) + with self.subTest(change=name): + self.assertEqual(result.returncode, 0 if name == "unchanged" else 12, result.stderr) + self.assertEqual(result.stderr, "") + # The hash is of the held file's bytes, and a mode other than 0500 is refused. + staged.chmod(0o700) + staged.write_bytes(RUNTIME_STUB) + staged.chmod(0o500) + wrong = shell('exec 9<"$1"; held_runtime hash "$1" "$2"', staged, "0" * 64) + self.assertEqual(wrong.returncode, 3) + staged.chmod(0o700) + loose = shell('exec 9<"$1"; held_runtime hash "$1" "$2"', staged, expected) + self.assertEqual(loose.returncode, 3) + def test_verified_head_cid_is_the_hashed_bytes(self): vector = b'{"payload":{"version":"0.7.1-rc.2"}}\n' expected = "QmZFMnZjkiTqy9VY5FDdKudvpsKtKxifk1poBrV7k4sqp8" @@ -735,26 +773,54 @@ def run_offline_installer(head, release, did, system="Linux", machine="x86_64"): return result, requests -# sha256sum FILE and shasum -a 256 FILE: print the real hash, then replace the -# file curl downloaded (only that one, only once) and log the swap. -SWAP_HASHER = """import hashlib, pathlib, sys +# sha256sum FILE and shasum -a 256 FILE print the real hash; "held" only swaps +# (the real python3 already hashed the held file). Then the first file that +# matches swap-rule is replaced, once, and the swap is logged: +# download inplace the file curl downloaded, rewritten in place +# staged rename the staged Runtime, replaced by another file at its path +# staged inplace the staged Runtime, rewritten in place, mode put back +SWAP_HASHER = """import hashlib, os, pathlib, sys fixtures, tool, path = pathlib.Path(sys.argv[1]), sys.argv[2], sys.argv[-1] -data = pathlib.Path(path).read_bytes() -print(hashlib.sha256(data).hexdigest() + " " + path, flush=True) +digest = lambda: hashlib.sha256(pathlib.Path(path).read_bytes()).hexdigest() +before = digest() +if tool != "held": + print(before + " " + path, flush=True) +rule = fixtures / "swap-rule" +target, how = rule.read_text().split() if rule.exists() else ("download", "inplace") downloads = dict(line.split(" ", 1) for line in (fixtures / "downloads").read_text().splitlines()) +hit = path == downloads.get("binary") if target == "download" else "/.elastos.install." in path log = fixtures / "swaps" -if path == downloads.get("binary") and not log.exists(): - pathlib.Path(path).write_bytes((fixtures / "swap-binary").read_bytes()) - after = hashlib.sha256(pathlib.Path(path).read_bytes()).hexdigest() - log.write_text(" ".join([tool, path, hashlib.sha256(data).hexdigest(), after]) + "\\n") +if hit and not log.exists(): + swap = (fixtures / "swap-binary").read_bytes() + mode = os.stat(path).st_mode & 0o777 + if how == "rename": + pathlib.Path(path + ".swap").write_bytes(swap) + os.chmod(path + ".swap", mode) + os.replace(path + ".swap", path) + else: + os.chmod(path, mode | 0o200) + with open(path, "r+b") as handle: + handle.write(swap) + handle.truncate() + os.chmod(path, mode) + log.write_text(" ".join([tool, path, before, digest()]) + "\\n") """ -def mock_swapping_hashers(sandbox): +def mock_swapping_hashers(sandbox, rule=None): (sandbox.root / "swap-hasher.py").write_text(SWAP_HASHER) - run = f'exec "{sys.executable}" -I "$FIXTURES/swap-hasher.py" "$FIXTURES"' - sandbox.mock("sha256sum", f'#!/bin/sh\n[ "$#" = 1 ] || exit 2\n{run} sha256sum "$@"\n') - sandbox.mock("shasum", f'#!/bin/sh\n[ "$#" = 3 ] && [ "$1 $2" = "-a 256" ] || exit 2\n{run} shasum "$@"\n') + if rule: + (sandbox.root / "swap-rule").write_text(rule) + run = f'"{sys.executable}" -I "$FIXTURES/swap-hasher.py" "$FIXTURES"' + sandbox.mock("sha256sum", f'#!/bin/sh\n[ "$#" = 1 ] || exit 2\nexec {run} sha256sum "$@"\n') + sandbox.mock("shasum", f'#!/bin/sh\n[ "$#" = 3 ] && [ "$1 $2" = "-a 256" ] || exit 2\nexec {run} shasum "$@"\n') + # install.sh hashes the held staged Runtime with python3 (held_runtime hash). + sandbox.mock("python3", f'''#!/bin/sh +"{sys.executable}" "$@" +status=$? +if [ "$status" = 0 ] && [ "$1" = - ] && [ "$2" = hash ]; then {run} held "$3"; fi +exit "$status" +''') def mock_apple_mktemp(sandbox): @@ -1054,6 +1120,86 @@ def test_runtime_swapped_after_its_checksum_check_is_not_run_or_installed(self): self.assertEqual(list(sandbox.binary.parent.iterdir()), []) self.assertEqual(sandbox.runtime_calls(), []) + def test_staged_runtime_swapped_between_its_hash_and_its_run_is_refused(self): + # Another process of this user replaces the staged Runtime after its + # hash passed and before it runs, at its path or in place. The installer + # holds the staged file open; each run first checks that the held file + # is unchanged and that its path still names it, so nothing runs. + swapped = RUNTIME_STUB.replace(b"#!/bin/bash\n", b'#!/bin/bash\necho ran >> "$FIXTURES/swapped-ran"\n', 1) + binary = {"cid": "binary-a", "sha256": hashlib.sha256(RUNTIME_STUB).hexdigest(), "size": len(RUNTIME_STUB)} + components = {"cid": "components-a", "sha256": hashlib.sha256(COMPONENTS).hexdigest(), + "size": len(COMPONENTS)} + did, head, release = installable_fixture(release_extra={"platforms": { + platform: {"binary": binary, "components": components} + for platform in ("x86_64-linux", "aarch64-darwin")}}) + for system, machine in (("Linux", "x86_64"), ("Darwin", "arm64")): + for how in ("rename", "inplace"): + with self.subTest(system=system, swap=how), \ + InstallerSandbox(head, release, did, system, machine) as sandbox: + sandbox.respond("binary", RUNTIME_STUB) + (sandbox.root / "swap-binary").write_bytes(swapped) + mock_swapping_hashers(sandbox, "staged " + how) + if system == "Darwin": + sandbox.hide_commands("sha256sum") + mock_apple_mktemp(sandbox) + result, requests = sandbox.run("--install-only") + output = result.stdout + result.stderr + swaps = (sandbox.root / "swaps").read_text().split() if (sandbox.root / "swaps").exists() else [] + self.assertEqual(swaps[2:], [hashlib.sha256(RUNTIME_STUB).hexdigest(), + hashlib.sha256(swapped).hexdigest()], output) + self.assertFalse((sandbox.root / "swapped-ran").exists(), output) + self.assertRegex(swaps[1], r"/\.elastos\.install\.\w+/elastos$") + self.assertEqual(sandbox.runtime_calls(), []) + self.assertEqual(result.returncode, 1, output) + self.assertTrue(result.stderr.endswith( + "Error: The downloaded Runtime changed on this computer after its check. " + "Nothing was installed. Run the installer again.\n"), result.stderr) + self.assertEqual(len(requests), 3) + self.assertFalse(sandbox.binary.exists()) + self.assertEqual(list(sandbox.binary.parent.iterdir()), []) + + @unittest.skipUnless(os.path.isdir("/proc/self/fd"), "the kernel cannot start an open file here (macOS)") + def test_staged_runtime_swapped_after_its_last_check_still_runs_the_checked_bytes(self): + # The window between the last check and exec: where /proc/self/fd + # exists the installer starts the held file, not the path, so the + # replaced path never runs. The writer then reads the candidate path, + # finds bytes without the signed SHA-256 and refuses, as Runtime's + # writer does (installed_release.rs admit_candidate). + runtime = RUNTIME_STUB.replace(b" install-release)\n", b''' install-release) + if [[ "${*: -1}" != --check ]] && [[ "$(python3 -I -c 'import hashlib, sys; print(hashlib.sha256(open(sys.argv[1], "rb").read()).hexdigest())' "${@: -4:1}")" != "$(cat "$FIXTURES/signed-sha256")" ]]; then + echo "Error: fixture writer refused a candidate without the signed SHA-256" >&2; exit 1 + fi +''', 1) + self.assertNotEqual(runtime, RUNTIME_STUB) + swapped = runtime.replace(b"#!/bin/bash\n", b'#!/bin/bash\necho ran >> "$FIXTURES/swapped-ran"\n', 1) + did, head, release = installable_fixture(runtime=runtime) + with InstallerSandbox(head, release, did) as sandbox: + sandbox.respond("binary", runtime) + (sandbox.root / "signed-sha256").write_text(hashlib.sha256(runtime).hexdigest()) + (sandbox.root / "swap-binary").write_bytes(swapped) + (sandbox.root / "swap-hasher.py").write_text(SWAP_HASHER) + (sandbox.root / "swap-rule").write_text("staged rename") + # The third check is the one right before the writer installs. + sandbox.mock("python3", f'''#!/bin/sh +"{sys.executable}" "$@" +status=$? +if [ "$status" = 0 ] && [ "$1" = - ] && [ "$2" = check ]; then + echo check >> "$FIXTURES/checks" + if [ "$(wc -l < "$FIXTURES/checks")" -eq 3 ]; then "{sys.executable}" -I "$FIXTURES/swap-hasher.py" "$FIXTURES" held "$3"; fi +fi +exit "$status" +''') + result, _ = sandbox.run("--install-only") + output = result.stdout + result.stderr + self.assertTrue((sandbox.root / "swaps").exists(), output) + self.assertFalse((sandbox.root / "swapped-ran").exists(), output) + calls = sandbox.runtime_calls() + self.assertEqual([call.split()[0] for call in calls], ["--version", "install-release", "install-release"]) + self.assertEqual(result.returncode, 1, output) + self.assertIn("fixture writer refused a candidate without the signed SHA-256", result.stderr) + self.assertIn("The previous installation was preserved", result.stderr) + self.assertFalse(sandbox.binary.exists()) + def test_release_head_swapped_after_its_signature_check_is_refused(self): # Another process replaces release-head.json right after its signature # passed, with a head signed by its own key. Nothing after the check may @@ -1342,7 +1488,7 @@ def assert_clean_install(self, sandbox, did, head, release, before, runtime=RUNT self.assertIn("Runtime installed:", result.stdout) self.assertNotIn("Setting up Home", result.stdout) calls = sandbox.runtime_calls() - writer = "^install-release --data-dir %s --binary %s --candidate %s/\\.elastos\\.install\\.\\w+ " \ + writer = "^install-release --data-dir %s --binary %s --candidate %s/\\.elastos\\.install\\.\\w+/elastos " \ "(\\S+)/sources\\.json \\1/release-head\\.json \\1/release\\.json" % ( re.escape(str(sandbox.data)), re.escape(str(sandbox.binary)), re.escape(str(sandbox.binary.parent))) diff --git a/scripts/install.sh b/scripts/install.sh index 05732ebb8..6891483ec 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -155,6 +155,8 @@ fi die() { echo -e "${RED:-}Error:${NC:-} $*" >&2; exit 1; } # For messages holding %q-quoted paths: print them byte for byte. die_exact() { printf '%bError:%b %s\n' "${RED:-}" "${NC:-}" "$*" >&2; exit 1; } +# The staged Runtime must stay the file whose SHA-256 was checked. +RUNTIME_CHANGED="The downloaded Runtime changed on this computer after its check. Nothing was installed. Run the installer again." # Every read of a verified envelope must see the SHA-256 its signature check saw. RELEASE_CHANGED="The release files changed on this computer after they were checked. Nothing was installed. Run the installer again." @@ -398,6 +400,56 @@ sha256_file() { fi } +# The staged Runtime is held open on fd 9 (see the install step). +# held_runtime hash PATH SHA256 hashes the bytes of the held file; prints its identity +# held_runtime check PATH ID the held file is unchanged and PATH still names it +# Both refuse (exit 3) unless the held file is a regular file of this user, +# mode 0500, with one link, and PATH names that same file. +held_runtime() { + python3 - "$@" <<'PY_HELD_RUNTIME' +import hashlib +import os +import stat +import sys + +mode, path, expected = sys.argv[1:4] +try: + held, named = os.fstat(9), os.lstat(path) +except OSError: + sys.exit(3) + + +def identity(info): + return ":".join(str(value) for value in ( + info.st_dev, info.st_ino, info.st_size, info.st_mtime_ns, info.st_ctime_ns)) + + +if (not stat.S_ISREG(held.st_mode) or held.st_uid != os.geteuid() + or stat.S_IMODE(held.st_mode) != 0o500 or held.st_nlink != 1 + or identity(named) != identity(held)): + sys.exit(3) +if mode == "hash": + digest, offset = hashlib.sha256(), 0 + while True: + chunk = os.pread(9, 1 << 20, offset) + if not chunk: + break + digest.update(chunk) + offset += len(chunk) + if digest.hexdigest() != expected or offset != held.st_size or identity(os.fstat(9)) != identity(held): + sys.exit(3) + print(identity(held)) +elif mode != "check" or identity(held) != expected: + sys.exit(3) +PY_HELD_RUNTIME +} + +# Run the held staged Runtime: the open file itself where HELD_RUNTIME is +# /proc/self/fd/9, else its path. argv[0] stays the staged path. +run_held_runtime() { + (exec -a "${TMP_INSTALL_BIN}" "${HELD_RUNTIME}" "$@") +} + installer_runtime_control() { # All callers stop verified processes before changing the selected install. # Smoke cleanup can disable the binary scan when using a shared branch binary. @@ -1461,21 +1513,32 @@ sha256_check "${INSTALL_SCRATCH}/elastos" "$BINARY_SHA256" (umask 077; mkdir -p "$DATA_DIR") require_install_space "$INSTALL_DIR" "$BINARY_SIZE" -TMP_INSTALL_BIN="$(mktemp "${INSTALL_DIR}/.elastos.install.XXXXXX")" -trap 'rm -rf "$INSTALL_SCRATCH" "$TMP_INSTALL_BIN"' EXIT +# Stage the Runtime in a new folder only this user can open (mktemp -d makes +# it 0700) inside the checked install directory, read-only and executable. +STAGE_DIR="$(mktemp -d "${INSTALL_DIR}/.elastos.install.XXXXXX")" +trap 'rm -rf "$INSTALL_SCRATCH" "$STAGE_DIR"' EXIT +TMP_INSTALL_BIN="${STAGE_DIR}/elastos" cp "${INSTALL_SCRATCH}/elastos" "${TMP_INSTALL_BIN}" -# Check the staged copy: it is the file this installer runs and hands to the -# writer. The check above refused a bad download before anything was written. -if [[ "$(sha256_file "${TMP_INSTALL_BIN}")" != "$BINARY_SHA256" ]]; then - die "The downloaded Runtime changed on this computer after its check. Nothing was installed. Run the installer again." +chmod 0500 "${TMP_INSTALL_BIN}" +# Open the staged copy once and hold it on fd 9. Its hash is taken from that +# open file, and every later run of it is checked against that open file. The +# check above refused a bad download before anything was written. +exec 9<"${TMP_INSTALL_BIN}" +HELD_RUNTIME_ID="$(held_runtime hash "${TMP_INSTALL_BIN}" "$BINARY_SHA256")" || die "$RUNTIME_CHANGED" +# Where the kernel can start an open file (Linux /proc/self/fd), the held file +# runs, so a path swapped after the last check cannot run. Elsewhere (macOS) +# the path runs, right after a check that it still names the held file. +HELD_RUNTIME="${TMP_INSTALL_BIN}" +if [[ -r /proc/self/fd/9 ]]; then + HELD_RUNTIME=/proc/self/fd/9 fi -chmod +x "${TMP_INSTALL_BIN}" # The staged executable must report the exact release version on stdout with # empty stderr before it can write this installation. STAGED_VERSION_STATUS=0 STAGED_VERSION_STDERR_PATH="${INSTALL_SCRATCH}/elastos-version.stderr" -STAGED_VERSION_OUTPUT="$("${TMP_INSTALL_BIN}" --version 2>"${STAGED_VERSION_STDERR_PATH}")" || STAGED_VERSION_STATUS=$? +held_runtime check "${TMP_INSTALL_BIN}" "$HELD_RUNTIME_ID" || die "$RUNTIME_CHANGED" +STAGED_VERSION_OUTPUT="$(run_held_runtime --version 2>"${STAGED_VERSION_STDERR_PATH}")" || STAGED_VERSION_STATUS=$? STAGED_VERSION_ERROR="$(cat "${STAGED_VERSION_STDERR_PATH}")" if [[ "${STAGED_VERSION_STATUS}" -ne 0 ]]; then die "Downloaded binary failed its version check (exit ${STAGED_VERSION_STATUS}); the current installation was preserved\n Output: ${STAGED_VERSION_OUTPUT:-}\n Stderr: ${STAGED_VERSION_ERROR:-}" @@ -1550,11 +1613,14 @@ PY # It reads each file below once, checks those bytes against the pin, the # version, the channel and any head CID stamp in sources.json, and installs # those same bytes (update.rs install_release). -INSTALL_RELEASE=("${TMP_INSTALL_BIN}" install-release --data-dir "$DATA_DIR" +# The writer reads the candidate once and admits it only if those bytes have +# the signed SHA-256 (installed_release.rs admit_candidate). +INSTALL_RELEASE=(install-release --data-dir "$DATA_DIR" --binary "${INSTALL_DIR}/elastos" --candidate "${TMP_INSTALL_BIN}" "$SOURCES_PATH" "${INSTALL_SCRATCH}/release-head.json" "${INSTALL_SCRATCH}/release.json") step 4 "Install" -"${INSTALL_RELEASE[@]}" --check || die "This installation was not changed" +held_runtime check "${TMP_INSTALL_BIN}" "$HELD_RUNTIME_ID" || die "$RUNTIME_CHANGED" +run_held_runtime "${INSTALL_RELEASE[@]}" --check || die "This installation was not changed" info "Stopping verified Runtime processes for this installation so the new version can be installed..." installer_runtime_control "$DATA_DIR" "${INSTALL_DIR}/elastos" true \ @@ -1566,7 +1632,10 @@ fi # One journaled transaction replaces the Runtime, trusted sources and the # verified release pair; an interrupted run restores the previous set. info "Installing binary to $(display_path "${INSTALL_DIR}/elastos")..." -"${INSTALL_RELEASE[@]}" || die "The previous installation was preserved" +held_runtime check "${TMP_INSTALL_BIN}" "$HELD_RUNTIME_ID" || die "$RUNTIME_CHANGED" +run_held_runtime "${INSTALL_RELEASE[@]}" || die "The previous installation was preserved" +# Later commands run the installed Runtime; they do not need the staged copy. +exec 9<&- ok "Installed ElastOS ${RELEASE_VERSION} to $(display_path "${INSTALL_DIR}/elastos")" PRINCIPAL_ROOT_BACKUP_DIR="${DATA_DIR}/backups/principal-root-upgrade-$(date -u +%s)-$$" From da63c23561c80d15f8880e4b2a34e4016be628ef Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 18 Oct 2026 02:51:06 +0000 Subject: [PATCH 11/11] test: mock GNU mktemp in the Linux case of the swapped-Runtime test The Linux case of test_runtime_swapped_after_its_checksum_check_is_not_run_or_installed used the host's own mktemp and then required the installer's scratch folder to be under the fixture TMPDIR. GNU mktemp -d uses TMPDIR, but Apple's mktemp -d ignores it and uses the per-user temp folder, so the case failed on every Mac host (install-bootstrap-test.py:1112). The Linux case now uses mock_gnu_mktemp, as the macOS cases use mock_apple_mktemp, with a template form that GNU and BSD mktemp both accept. The test no longer depends on the host's mktemp. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01W6Qn9RX7LDqrwiAfNC2F2K --- scripts/install-bootstrap-test.py | 32 ++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/scripts/install-bootstrap-test.py b/scripts/install-bootstrap-test.py index c7263a55d..eb41a6795 100644 --- a/scripts/install-bootstrap-test.py +++ b/scripts/install-bootstrap-test.py @@ -823,6 +823,20 @@ def mock_swapping_hashers(sandbox, rule=None): ''') +def mock_gnu_mktemp(sandbox): + """mktemp as GNU coreutils ships it: -d with no template uses TMPDIR. + + The fixture Linux run must not use the host's own mktemp. On a Mac host + that is Apple's mktemp, which ignores TMPDIR (see mock_apple_mktemp). The + template form is one that GNU and BSD mktemp both accept. + """ + real = shutil.which("mktemp") + sandbox.mock("mktemp", f'''#!/bin/sh +if [ "$#" = 1 ] && [ "$1" = -d ]; then exec "{real}" -d "${{TMPDIR:-/tmp}}/tmp.XXXXXXXXXX"; fi +exec "{real}" "$@" +''') + + def mock_apple_mktemp(sandbox): """mktemp as macOS ships it: -d with no template ignores TMPDIR.""" (sandbox.root / "darwin-user-temp").mkdir(mode=0o700) @@ -1081,13 +1095,15 @@ def test_runtime_swapped_after_its_checksum_check_is_not_run_or_installed(self): platform: {"binary": binary, "components": components} for platform in ("x86_64-linux", "aarch64-darwin")}}) cases = [ - # (uname -s, uname -m, commands hidden from PATH, Apple mktemp, TMPDIR is a symlink, hasher) - ("Linux", "x86_64", (), False, False, "sha256sum"), - ("Darwin", "arm64", (), True, True, "sha256sum"), + # (uname -s, uname -m, commands hidden from PATH, mktemp, TMPDIR is a symlink, hasher) + # Each case mocks mktemp, so the result does not depend on the + # host: on a Mac host the real mktemp is Apple's. + ("Linux", "x86_64", (), "gnu", False, "sha256sum"), + ("Darwin", "arm64", (), "apple", True, "sha256sum"), # A Mac without sha256sum: the installer falls back to shasum -a 256. - ("Darwin", "arm64", ("sha256sum",), True, True, "shasum"), + ("Darwin", "arm64", ("sha256sum",), "apple", True, "shasum"), ] - for system, machine, hidden, apple_mktemp, tmp_link, hasher in cases: + for system, machine, hidden, mktemp, tmp_link, hasher in cases: with self.subTest(system=system, hasher=hasher), \ InstallerSandbox(head, release, did, system, machine) as sandbox: sandbox.respond("binary", RUNTIME_STUB) @@ -1095,8 +1111,10 @@ def test_runtime_swapped_after_its_checksum_check_is_not_run_or_installed(self): mock_swapping_hashers(sandbox) if hidden: sandbox.hide_commands(*hidden) - if apple_mktemp: + if mktemp == "apple": mock_apple_mktemp(sandbox) + else: + mock_gnu_mktemp(sandbox) if tmp_link: # macOS /tmp and /var are symlinks into /private. (sandbox.root / "tmp-link").symlink_to(sandbox.tmpdir) @@ -1108,7 +1126,7 @@ def test_runtime_swapped_after_its_checksum_check_is_not_run_or_installed(self): swaps = (sandbox.root / "swaps").read_text().split() if (sandbox.root / "swaps").exists() else [] self.assertEqual(swaps, [hasher, download, hashlib.sha256(RUNTIME_STUB).hexdigest(), hashlib.sha256(swapped).hexdigest()], output) - scratch = sandbox.root / ("darwin-user-temp" if apple_mktemp else "tmp") + scratch = sandbox.root / ("darwin-user-temp" if mktemp == "apple" else "tmp") self.assertEqual(Path(download).resolve().parent.parent, scratch.resolve()) # Then the installer refused it: it never ran, nothing was installed. self.assertFalse((sandbox.root / "swapped-ran").exists(), output)