From 3277066c5a7f7d15530145ceabfde648d52db6c6 Mon Sep 17 00:00:00 2001 From: Jeremy Karst Date: Fri, 27 Nov 2026 21:30:15 -0500 Subject: [PATCH] Release tooling: commit lock per release, escaping symlinks, doc fixes - K2: config/lock- gives tools, src, ports, plugins and core their exact commits; build stops when a checkout differs (a tag moved upstream, or src moved on), and fetches tags without --force. build.sh lock resolves and writes them. lock-26.7.9 is what 26.7.9 was built from, checked against the upstream tags. - K3: verify --root reports symlinks that lead outside the tree or nowhere, which publish (rsync -L) would follow; such a path is now reported instead of crashing verify. - K6: RELEASE.md no longer says core trusts only our key (it also trusts upstream's, for changelog and bogons), and names no host or user. Co-Authored-By: Claude Opus 5.5 --- RELEASE.md | 40 ++++++++++++------ config/lock-26.7.9 | 5 +++ mirror/gwmirror.py | 38 +++++++++++++++-- mirror/tests/test_gwmirror.py | 49 ++++++++++++++++++++++ release/build.sh | 53 +++++++++++++++++++++++- release/tests/test_scripts.py | 78 +++++++++++++++++++++++++++++++++++ 6 files changed, 245 insertions(+), 18 deletions(-) create mode 100644 config/lock-26.7.9 diff --git a/RELEASE.md b/RELEASE.md index be1780a..5b41f8e 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -129,8 +129,8 @@ two names in a directory that differ only in case. ### Build host Builds run natively in a FreeBSD arm64 VM that QEMU emulates (TCG) inside a -rootless podman container on a build host: ours is the HTPC (Bazzite, Ryzen -AI Max+ 395, 16 cores), `GW_VMHOST=htpc`. The build host needs podman, curl, +rootless podman container on a build host, named by `GW_VMHOST` (ours: a +16-core Ryzen AI Max+ 395 running Bazzite). The build host needs podman, curl, sha256sum and xz, and no root. `release/vm.sh` runs `release/vmhost.sh` there over ssh; that script builds the container image (QEMU and the arm64 UEFI firmware, from Debian) on first use and keeps the VM's files in @@ -151,7 +151,7 @@ machine works with the same `build.sh` and no changes. ```sh # ~/.config/gwmirror/vm.conf, sourced by vm.sh -GW_VMHOST=htpc # ~/.ssh/config alias of the build host +GW_VMHOST=buildhost # ~/.ssh/config alias of the build host GW_VM_CPUS=24 GW_VM_MEM=12G # the HTPC's other work needs most of its memory; see Memory below @@ -208,7 +208,14 @@ release/vm.sh pull-out 15.1 26.7 # -> /scratch/mono-opnsense/out/26.7 Src is fetched shallow (its single commit, no tags): the build never needs its history, which is most of its 2 GB. Git never prompts for credentials, and a transfer that moves nothing for 10 minutes fails the build instead - of hanging. + of hanging. Each checkout must then be at the commit + `config/lock-` gives it (`tools`, `src`, `ports`, `plugins`, + `core`, before our patches): a tag that moved upstream, or a src branch + that moved on, stops the build. Tags are fetched without `--force`, so a + moved tag already fails the fetch. `build.sh lock SERIES VERSION` + resolves the refs and writes the commits to `/root/gwlock-` in + the VM; check them against the upstream tags (`git ls-remote`), then + commit them as `config/lock-`. 2. Applies `patches//*.patch` to core with `git am`. Core's version becomes `_`. 3. Builds base and kernel, then this repository's kernel modules, as an @@ -383,7 +390,9 @@ python3 mirror/gwmirror.py verify --url https://opnsense.karsttech.com --series directories are excepted, and so is the Go toolchain's source tree (`/usr/local/go/src/`, in the aux repository), which ships test data and race-detector objects for every platform. -- that every name can be stored on the SMB share (see "Names on disk"). +- that every name can be stored on the SMB share (see "Names on disk"); +- that no symlink leads outside the tree or nowhere: `publish` copies with + `rsync -L`, which would publish a link's target. Nothing is extracted to disk while verifying. @@ -407,7 +416,7 @@ needs no privileges: ``` # /etc/fstab -//// /mnt/opnsense-mirror cifs credentials=/home/code/.config/gwmirror/smb-credentials,uid=code,gid=code,vers=3.0,nofail,_netdev 0 0 +//// /mnt/opnsense-mirror cifs credentials=/home//.config/gwmirror/smb-credentials,uid=,gid=,vers=3.0,nofail,_netdev 0 0 ``` `~/.config/gwmirror/smb-credentials` (mode 0600) holds `username=` and @@ -542,8 +551,11 @@ History), `configctl firmware reload`. After step 7: `bectl activate pre-gwmirror` and reboot. **7. Update** in System > Firmware > Updates: Check for updates, then -update. This installs our core `26.1.11_2`, which trusts only our key and -names only our mirror, the packages, base and kernel, then reboots. +update. This installs our core `26.1.11_2`, then the packages, base and +kernel, and reboots. That core names only our mirror and revokes the +vendor's keys. Besides ours it still trusts upstream's key +(`pkg.opnsense.org.20260120`; `.20260608` in 26.7), which signs the +changelog and bogons sets the mirror passes through from upstream. **8. Reinstall everything once**, so no vendor-built binary remains, then reboot: @@ -645,13 +657,17 @@ pgrep -l cmm || echo cmm-stopped - **Point release** (e.g. 26.7.10): 1. Rebase the src fork onto the new tag. - 2. Run `build` / `sign` / `pull-out`; tools skips unchanged ports. - 3. Run `stage`, `verify`, `publish`. + 2. Run `build.sh lock` in the VM, check the commits and commit + `config/lock-` (build step 1). + 3. Run `build` / `sign` / `pull-out`; tools skips unchanged ports. + 4. Run `stage`, `verify`, `publish`. - **Major release** (27.1 is due January 2027): the procedure above. - **Board fix between releases:** commit here, then rebuild. The package version rises with the commit count. -- **Kernel-only fix between releases:** rebuild and republish the same-version - kernel set; gateways pick it up with `opnsense-update -fk`. +- **Kernel-only fix between releases:** push it to the src fork, update `src` + in `config/lock-`, delete the kernel set in the VM and rebuild, + then republish the same-version kernel set; gateways pick it up with + `opnsense-update -fk`. ## Monitoring diff --git a/config/lock-26.7.9 b/config/lock-26.7.9 new file mode 100644 index 0000000..d13682f --- /dev/null +++ b/config/lock-26.7.9 @@ -0,0 +1,5 @@ +tools 877ba55e86d20c4e66fb56770cb2bfd8538a4c9e +src 623bab267337ac7a1ed6087e2c35324679751e53 +ports 725e9093601820fc4e7e11d6c2e0edf1375f3ea8 +plugins a01a2e8cef738b3762830a7bbfc6e857384328f1 +core 228ab68d54513046f79bc36d7f583936922d1951 diff --git a/mirror/gwmirror.py b/mirror/gwmirror.py index 2eb8ea4..8fbb157 100644 --- a/mirror/gwmirror.py +++ b/mirror/gwmirror.py @@ -122,6 +122,27 @@ def share_name_problems(root): return problems +def link_problems(root): + """(path, problem) for every symlink under root that resolves outside it or + to nothing: publish copies with rsync -L, which would publish the target. + Dotfiles are skipped, as in share_name_problems.""" + root = os.path.realpath(root) + problems = [] + for d, dirs, files in os.walk(root): + dirs[:] = sorted(n for n in dirs if not n.startswith(".")) + for name in sorted(dirs + [n for n in files if not n.startswith(".")]): + path = os.path.join(d, name) + if not os.path.islink(path): + continue + rel = os.path.relpath(path, root) + target = os.path.realpath(path) + if target != root and not target.startswith(root + os.sep): + problems.append((rel, "symlink leads outside the mirror tree")) + elif not os.path.exists(target): + problems.append((rel, "symlink leads nowhere")) + return problems + + class VerifyError(Exception): """A single verification failure, reported with its location.""" @@ -301,7 +322,10 @@ class DirSource: return path def exists(self, rel): - return os.path.isfile(self._path(rel)) + try: + return os.path.isfile(self._path(rel)) + except VerifyError: # leaves the tree: link_problems names it + return False def size(self, rel): try: @@ -792,7 +816,7 @@ def verify_with(args, trust, upstream): if args.root: source, url_mode, deep = DirSource(args.root), False, not args.shallow series = args.series or discover_series(source) - for rel, problem in share_name_problems(source.root): + for rel, problem in share_name_problems(source.root) + link_problems(source.root): report.error(rel, problem) else: source = HttpSource(args.url, args.ca_file, args.allow_http) @@ -819,9 +843,15 @@ def verify_with(args, trust, upstream): if not ABI_RE.match(abi) or not SERIES_RE.match(ser): report.error(s, "series must look like FreeBSD:14:aarch64/26.1") continue - verify_series(source, s, trust, upstream, report, deep, args.device, url_mode) + try: + verify_series(source, s, trust, upstream, report, deep, args.device, url_mode) + except VerifyError as e: + report.error(s, str(e)) if args.root: - verify_releases(source, report, trust, deep) + try: + verify_releases(source, report, trust, deep) + except VerifyError as e: + report.error("releases", str(e)) status = "FAILED" if report.errors else "PASSED" print(f"{status}: {len(report.errors)} errors, {len(report.warnings)} warnings ({source.label})") return 1 if report.errors else 0 diff --git a/mirror/tests/test_gwmirror.py b/mirror/tests/test_gwmirror.py index 5769406..2d0da2e 100644 --- a/mirror/tests/test_gwmirror.py +++ b/mirror/tests/test_gwmirror.py @@ -764,6 +764,55 @@ class VerifyUrlTest(Base): g.main(["verify", "--url", "https://localhost"]) +class LinkTest(Base): + """verify --root refuses symlinks that publish (rsync -L) would follow out of the tree.""" + + def test_links_inside_the_tree_pass(self): + self.staged() # Latest/*.pkg are links into All/ + code, text = self.verify() + self.assertEqual(code, 0, text) + + def test_file_link_outside(self): + d = self.staged() + secret = os.path.join(self.tmp, "secret") + open(secret, "w").close() + os.symlink(secret, os.path.join(d, "sets", "leak.txz")) + self.assertFails("sets/leak.txz: symlink leads outside the mirror tree") + + def test_relative_link_outside(self): + d = self.staged() + os.symlink("../../../../../secret", os.path.join(d, "latest", "All", "x.pkg")) + open(os.path.join(self.tmp, "secret"), "w").close() + self.assertFails("latest/All/x.pkg: symlink leads outside the mirror tree") + + def test_directory_link_outside(self): + self.staged() + os.symlink(self.tmp, os.path.join(self.mirror, "releases", "etc")) + self.assertFails("releases/etc: symlink leads outside the mirror tree") + + def test_dangling_link(self): + d = self.staged() + os.symlink("nothing-here", os.path.join(d, "sets", "gone.txz")) + self.assertFails("sets/gone.txz: symlink leads nowhere") + + def test_dotfiles_are_not_published(self): + d = self.staged() + os.symlink(self.tmp, os.path.join(d, ".cache")) + code, text = self.verify() + self.assertEqual(code, 0, text) + + def test_publish_refuses(self): + d = self.staged() + os.symlink(self.tmp, os.path.join(d, "sets", "leak")) + dest = os.path.join(self.tmp, "dest") + os.makedirs(dest) + with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()): + code = g.main(["publish", "--mirror", self.mirror, "--dest", dest, + "--fingerprints", self.keys.ours, "--upstream-fingerprints", self.keys.upstream]) + self.assertNotEqual(code, 0) + self.assertEqual(os.listdir(dest), []) + + class DiskNameTest(Base): def test_disk_rel(self): for rel, disk in ((f"{ABI}/{SERIES}/latest/meta.conf", f"{DISK_ABI}/{SERIES}/latest/meta.conf"), diff --git a/release/build.sh b/release/build.sh index 3c3402e..cb21c8e 100644 --- a/release/build.sh +++ b/release/build.sh @@ -5,6 +5,9 @@ # release/vm.sh, or a real aarch64 machine. This repository must be at /usr/deps. # # build.sh setup install the host packages the build needs +# build.sh lock SERIES VERSION resolve every repository's ref for VERSION and +# write the commits to /root/gwlock-VERSION, for +# config/lock-VERSION (review it, then commit it) # build.sh build SERIES VERSION check out every repository at VERSION and build # base, kernel, ports, plugins, core and the # mono-gateway package; nothing is signed yet @@ -17,6 +20,8 @@ # VERSION tag unless overridden (upstream sometimes skips a tag in one of them): # TOOLS_REF, CORE_REF, PORTS_REF, PLUGINS_REF (default VERSION) and SRC_REF # (default stable/SERIES of our src fork). FORGE and UPSTREAM name the git hosts. +# Each checkout must be at the commit config/lock-VERSION gives it, so a moved +# upstream tag or a src branch that moved on stops the build. # # Copyright 2026 Karst Tech # SPDX-License-Identifier: BSD-2-Clause @@ -54,7 +59,7 @@ step() usage() { - sed -n '3,19p' "${0}" >&2 + sed -n '3,24p' "${0}" >&2 DIED=yes exit 2 } @@ -65,6 +70,7 @@ args() VERSION=${2:-} echo "${SERIES}" | grep -Eqx '[0-9]{2}\.[0-9]' || die "bad series '${SERIES}'" echo "${VERSION}" | grep -Eqx "${SERIES}(\.[0-9]+)?" || die "version '${VERSION}' is not in series ${SERIES}" + LOCK=${DEPS}/config/lock-${VERSION} SETS=/usr/local/opnsense/build/${SERIES}/aarch64/sets IMAGES=/usr/local/opnsense/build/${SERIES}/aarch64/images # the signing key is never read from the tools tree; finish passes it explicitly @@ -89,7 +95,9 @@ checkout() git -C "${1}" fetch -q --depth 1 --no-tags --force origin "${3}" _ref=FETCH_HEAD else - git -C "${1}" fetch -q --tags --force origin + # no --force: a tag that moved upstream fails the fetch instead of + # silently replacing ours (and the lock would catch it as well) + git -C "${1}" fetch -q --tags origin if git -C "${1}" rev-parse -q --verify "refs/remotes/origin/${3}" > /dev/null; then _ref=refs/remotes/origin/${3} else @@ -102,15 +110,44 @@ checkout() echo " ${1}: ${4} = $(git -C "${1}" describe --tags --always) ($(git -C "${1}" rev-parse --short HEAD))" } +# lock_check NAME DIR: DIR must be at the commit LOCK gives NAME ("NAME <40 hex>"). +lock_check() +{ + _want=$(sed -n "s/^${1}[[:space:]][[:space:]]*\([0-9a-f]\{40\}\)[[:space:]]*\$/\1/p" "${LOCK}") + [ "$(echo "${_want}" | grep -c .)" -eq 1 ] || die "$(basename "${LOCK}") must give exactly one commit for ${1}" + _have=$(git -C "${2}" rev-parse HEAD) + [ "${_have}" = "${_want}" ] || + die "${2} is at ${_have}, but $(basename "${LOCK}") says ${_want}: a ref moved (build.sh lock shows the new commits; review them before updating the lock)" + echo " ${2}: locked commit" +} + +# lock_step NAME DIR: check DIR against the lock, or record it (build.sh lock) +lock_step() +{ + if [ -n "${WRITE_LOCK:-}" ]; then + printf '%s %s\n' "${1}" "$(git -C "${2}" rev-parse HEAD)" >> "${WRITE_LOCK}" + else + lock_check "${1}" "${2}" + fi +} + checkout_all() { step "checking out ${SERIES} ${VERSION}" [ -f "${DEPS}/release/build.sh" ] || die "this repository must be at ${DEPS} (vm.sh sync)" + [ -n "${WRITE_LOCK:-}" ] || [ -f "${LOCK}" ] || + die "no config/lock-${VERSION}: run build.sh lock ${SERIES} ${VERSION}, review it and commit it" checkout /usr/tools "${UPSTREAM}/tools.git" "${TOOLS_REF:-${VERSION}}" master + lock_step tools /usr/tools checkout /usr/src "${FORGE}/opnsense-src.git" "${SRC_REF:-stable/${SERIES}}" "stable/${SERIES}" shallow + lock_step src /usr/src checkout /usr/ports "${UPSTREAM}/ports.git" "${PORTS_REF:-${VERSION}}" master + lock_step ports /usr/ports checkout /usr/plugins "${UPSTREAM}/plugins.git" "${PLUGINS_REF:-${VERSION}}" "stable/${SERIES}" + lock_step plugins /usr/plugins checkout /usr/core "${UPSTREAM}/core.git" "${CORE_REF:-${VERSION}}" "stable/${SERIES}" + lock_step core /usr/core # before our patches + [ -z "${WRITE_LOCK:-}" ] || return 0 PATCHES=$(ls "${DEPS}/patches/${SERIES}"/*.patch 2>/dev/null) || die "no patches/${SERIES}" # shellcheck disable=SC2086 git ${COMMITTER} -C /usr/core am -q --committer-date-is-author-date ${PATCHES} @@ -575,11 +612,23 @@ finish() ls -l "${SETS}" "${IMAGES}" } +lock() +{ + args "$@" + [ "$(id -u)" = 0 ] || die "run as root" + WRITE_LOCK=/root/gwlock-${VERSION} + : > "${WRITE_LOCK}" + checkout_all + step "commits for ${VERSION}; review them, then: vm.sh ssh cat ${WRITE_LOCK} > config/lock-${VERSION}" + cat "${WRITE_LOCK}" +} + [ $# -ge 1 ] || usage CMD=${1} shift case "${CMD}" in setup) [ $# -eq 0 ] || usage; setup ;; +lock) [ $# -eq 2 ] || usage; lock "$@" ;; build) [ $# -eq 2 ] || usage; build "$@" ;; finish) [ $# -eq 3 ] || usage; finish "$@" ;; *) usage ;; diff --git a/release/tests/test_scripts.py b/release/tests/test_scripts.py index 34863e3..d4a6f41 100644 --- a/release/tests/test_scripts.py +++ b/release/tests/test_scripts.py @@ -1242,6 +1242,84 @@ class CmmRcTest(unittest.TestCase): self.assertIsNone(other.poll(), "stop killed a process that is not cmm") +class LockTest(unittest.TestCase): + """build.sh's lock_check and lock_step: every checkout at its locked commit.""" + + def setUp(self): + self.tmp = tempfile.mkdtemp(prefix="gw-lock-") + self.addCleanup(shutil.rmtree, self.tmp) + self.repo = os.path.join(self.tmp, "repo") + env = {"GIT_AUTHOR_NAME": "t", "GIT_AUTHOR_EMAIL": "t@t", "GIT_COMMITTER_NAME": "t", + "GIT_COMMITTER_EMAIL": "t@t", "PATH": "/usr/bin:/bin:/usr/local/bin", "HOME": self.tmp} + for cmd in (["git", "init", "-q", self.repo], + ["git", "-C", self.repo, "commit", "-q", "--allow-empty", "-m", "x"]): + subprocess.run(cmd, check=True, env=env) + self.head = subprocess.run(["git", "-C", self.repo, "rev-parse", "HEAD"], check=True, + capture_output=True, text=True).stdout.strip() + self.lock = os.path.join(self.tmp, "lock-26.7.9") + self.func = build_function("die") + build_function("lock_check") + build_function("lock_step") + + def run_lock(self, content, name="src", **env): + with open(self.lock, "w") as f: + f.write(content) + e = {"PATH": "/usr/bin:/bin:/usr/local/bin", "LOCK": self.lock, **env} + return subprocess.run(["sh", "-c", "set -eu\n" + self.func + '\nlock_step "$1" "$2"', "sh", name, self.repo], + env=e, capture_output=True, text=True) + + def test_locked_commit(self): + r = self.run_lock(f"tools {'1' * 40}\nsrc {self.head}\n") + self.assertEqual(r.returncode, 0, r.stderr) + self.assertIn("locked commit", r.stdout) + + def test_moved_ref(self): + r = self.run_lock(f"src {'a' * 40}\n") + self.assertNotEqual(r.returncode, 0) + self.assertIn(f"is at {self.head}, but lock-26.7.9 says {'a' * 40}", r.stderr) + + def test_missing_entry(self): + r = self.run_lock(f"tools {self.head}\n") + self.assertNotEqual(r.returncode, 0) + self.assertIn("must give exactly one commit for src", r.stderr) + + def test_duplicate_entry(self): + r = self.run_lock(f"src {self.head}\nsrc {'b' * 40}\n") + self.assertNotEqual(r.returncode, 0) + self.assertIn("exactly one commit", r.stderr) + + def test_malformed_entries(self): + for content in (f"src {self.head[:12]}\n", f"src {self.head.upper()}\n", f"src {self.head}x\n", + f"srcx {self.head}\n", f"# src {self.head}\n", f"src\t{self.head} extra\n"): + with self.subTest(content=content): + self.assertNotEqual(self.run_lock(content).returncode, 0) + + def test_name_is_not_a_prefix_match(self): + r = self.run_lock(f"src {self.head}\n", name="sr") + self.assertNotEqual(r.returncode, 0) + + def test_tab_separated_and_trailing_space(self): + self.assertEqual(self.run_lock(f"src\t{self.head} \n").returncode, 0) + + def test_write_mode_records(self): + out = os.path.join(self.tmp, "gwlock") + open(out, "w").close() + r = self.run_lock("", WRITE_LOCK=out) + self.assertEqual(r.returncode, 0, r.stderr) + with open(out) as f: + self.assertEqual(f.read(), f"src {self.head}\n") + + def test_build_refuses_without_lock(self): + func = build_function("die") + build_function("step") + build_function("checkout_all") + deps = os.path.join(self.tmp, "deps") + os.makedirs(os.path.join(deps, "release")) + open(os.path.join(deps, "release", "build.sh"), "w").close() + r = subprocess.run(["sh", "-c", "set -eu\n" + func + "\ncheckout_all"], + env={"PATH": "/usr/bin:/bin", "DEPS": deps, "SERIES": "26.7", "VERSION": "26.7.9", + "LOCK": os.path.join(deps, "config", "lock-26.7.9")}, + capture_output=True, text=True) + self.assertNotEqual(r.returncode, 0) + self.assertIn("no config/lock-26.7.9: run build.sh lock 26.7 26.7.9", r.stderr) + + class SignAuxRepoTest(unittest.TestCase): """build.sh's sign_aux_repo: the aux set's catalogue signed with KEYDIR's key."""