From fd05f85d440455c9f047a63eb70d61f8b7b0bbdd Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Wed, 4 Nov 2026 16:40:10 +0100 Subject: [PATCH] fix(install): stage the package build workspace with tar, not rsync Run 41743807934 lost Matrix#2, Workspace (debian:13), Gitea#0 and Wazuh#0 to the full job budget right after "Preparing build workspace". On runner image 20261101 the kernel stacks every container onto docker-default//&unconfined, which refuses every in-container kill(); the host journal logs apparmor="DENIED" operation="signal" comm="rsync" signal=usr2. rsync's local copy waits for exactly that SIGUSR2 and sleeps until the job is killed. scripts/install/stage.sh copies the tree through a tar pipe, which sends no signal; --format=posix keeps nanosecond mtimes. package.sh uses it on the arch and debian paths and installs tar instead of rsync. Copying this repository both ways gives identical content, modes, types, link targets and mtimes on all 11306 entries. test_stage.py runs stage.sh under a seccomp filter that fails every signal syscall with EACCES; the old rsync copy hangs past its timeout under the same filter. Co-Authored-By: Claude Opus 5.5 --- scripts/install/README.md | 1 + scripts/install/package.sh | 19 +- scripts/install/stage.sh | 18 ++ .../unit/python/scripts/install/test_stage.py | 178 ++++++++++++++++++ 4 files changed, 201 insertions(+), 15 deletions(-) create mode 100755 scripts/install/stage.sh create mode 100644 tests/unit/python/scripts/install/test_stage.py diff --git a/scripts/install/README.md b/scripts/install/README.md index 4a14ee215a..db3e4501d6 100644 --- a/scripts/install/README.md +++ b/scripts/install/README.md @@ -5,6 +5,7 @@ This directory contains scripts that install and bootstrap host-side tooling use - 📦 Install Python, Ansible, and lint dependencies for supported environments - 🐧 Install ad-hoc APT packages for CI and workflow jobs via `apt.sh` - 📦 Build and install distro package metadata via `package.sh` (Arch/Debian/Ubuntu/Fedora) +- 🗂️ Copy the repository into a fresh package build workspace via `stage.sh`, a tar pipe instead of rsync, because rsync's local copy waits for an in-container signal that hosts stacking AppArmor onto containers refuse - 🪞 Point an Ubuntu image's apt sources at `INFINITO_APT_UBUNTU_MIRRORS` via `apt-mirrors.sh`, run first in the image build: apt tries the mirrors in list order and moves on when one fails or stays silent for 10 s; images of other distros are left untouched - 🏖️ Install OS-level Claude Code sandbox dependencies (`bubblewrap`, `socat`) via `sandbox.sh`, covering Debian, Ubuntu, Fedora, CentOS/RHEL/Rocky/Alma, and Arch; also exposed as `make install-agent` - 🧊 Install the ext4, btrfs and zfs userlands via `filesystem.sh` (distro packages, EPEL for btrfs on CentOS, the AUR PKGBUILD for zfs on Arch, the zfsonlinux repository on Fedora/CentOS); the kernel side comes from the host diff --git a/scripts/install/package.sh b/scripts/install/package.sh index 80a8a47cf9..91f6d7fb6c 100755 --- a/scripts/install/package.sh +++ b/scripts/install/package.sh @@ -29,7 +29,7 @@ build_and_install_arch() { pacman-key --populate manjaro 2>/dev/null || true # nocheck: shell-or-true -- grandfathered: worked in practice; TODO: sharpen to catch only the exact tolerated error echo "[arch] Installing build toolchain..." - pacman -Syu --noconfirm --needed base-devel sudo rsync + pacman -Syu --noconfirm --needed base-devel sudo tar if ! id "${builder}" >/dev/null 2>&1; then echo "[arch] Creating build user '${builder}'..." @@ -41,13 +41,7 @@ build_and_install_arch() { chmod 0440 /etc/sudoers.d/99-"${builder}"-pacman echo "[arch] Preparing build workspace ${build_root}..." - rm -rf "${build_root}" - mkdir -p "${build_root}" - rsync -a --delete \ - --exclude '.git' \ - --exclude '.venv' \ - --exclude '__pycache__' \ - "${REPO_ROOT}/" "${build_root}/" + bash "${SCRIPT_DIR}/stage.sh" "${REPO_ROOT}" "${build_root}" '.git' '.venv' '__pycache__' chown -R "${builder}:${builder}" "${build_root}" @@ -86,15 +80,10 @@ build_and_install_debian_like() { build-essential \ debhelper \ dpkg-dev \ - rsync + tar echo "[debian] Preparing build workspace ${build_root}..." - rm -rf "${build_root}" - mkdir -p "${build_root}" - rsync -a \ - --exclude 'packaging/debian' \ - --exclude '.git' \ - "${REPO_ROOT}/" "${build_root}/" + bash "${SCRIPT_DIR}/stage.sh" "${REPO_ROOT}" "${build_root}" 'packaging/debian' '.git' mkdir -p "${build_root}/debian" cp -a "${REPO_ROOT}/packaging/debian/." "${build_root}/debian/" diff --git a/scripts/install/stage.sh b/scripts/install/stage.sh new file mode 100755 index 0000000000..21124cd13f --- /dev/null +++ b/scripts/install/stage.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +# Param: $1 source tree to copy +# Param: $2 build workspace, removed and recreated empty +# Param: $3... exclusion patterns, unanchored, so `.git` drops every `.git` at any depth +set -euo pipefail + +src="${1:?usage: stage.sh SRC DEST [EXCLUDE...]}" +dest="${2:?usage: stage.sh SRC DEST [EXCLUDE...]}" +shift 2 + +excludes=() +for pattern in "$@"; do + excludes+=("--exclude=${pattern}") +done + +rm -rf "${dest}" +mkdir -p "${dest}" +tar -C "${src}" "${excludes[@]}" --format=posix -cf - . | tar -C "${dest}" -xf - diff --git a/tests/unit/python/scripts/install/test_stage.py b/tests/unit/python/scripts/install/test_stage.py new file mode 100644 index 0000000000..69f79853f4 --- /dev/null +++ b/tests/unit/python/scripts/install/test_stage.py @@ -0,0 +1,178 @@ +from __future__ import annotations + +import ctypes +import errno +import filecmp +import os +import platform +import signal +import stat +import subprocess +import tempfile +import unittest +from pathlib import Path + +from utils.cache.files import PROJECT_ROOT + +STAGE = PROJECT_ROOT / "scripts" / "install" / "stage.sh" +TIMEOUT_SECONDS = 60 + +AUDIT_ARCH = {"x86_64": 0xC000003E, "aarch64": 0xC00000B7} +SIGNAL_SYSCALLS = { + "x86_64": (62, 200, 234, 129, 297, 424), + "aarch64": (129, 130, 131, 138, 240, 424), +} + + +class _SockFilter(ctypes.Structure): + _fields_ = [ + ("code", ctypes.c_ushort), + ("jt", ctypes.c_ubyte), + ("jf", ctypes.c_ubyte), + ("k", ctypes.c_uint), + ] + + +class _SockFprog(ctypes.Structure): + _fields_ = [("len", ctypes.c_ushort), ("filter", ctypes.POINTER(_SockFilter))] + + +def _signal_ban_program(machine: str) -> list[_SockFilter]: + ld_abs, jeq, ret = 0x20, 0x15, 0x06 + allow, deny = 0x7FFF0000, 0x00050000 | errno.EACCES + numbers = SIGNAL_SYSCALLS[machine] + program = [ + _SockFilter(ld_abs, 0, 0, 4), + _SockFilter(jeq, 1, 0, AUDIT_ARCH[machine]), + _SockFilter(ret, 0, 0, allow), + _SockFilter(ld_abs, 0, 0, 0), + ] + for index, number in enumerate(numbers): + program.append(_SockFilter(jeq, len(numbers) - index, 0, number)) + program.append(_SockFilter(ret, 0, 0, allow)) + program.append(_SockFilter(ret, 0, 0, deny)) + return program + + +def _ban_signals() -> None: + program = _signal_ban_program(platform.machine()) + filters = (_SockFilter * len(program))(*program) + fprog = _SockFprog(len(program), filters) + libc = ctypes.CDLL(None, use_errno=True) + libc.prctl.argtypes = [ + ctypes.c_int, + ctypes.c_ulong, + ctypes.c_ulong, + ctypes.c_ulong, + ctypes.c_ulong, + ] + if libc.prctl(38, 1, 0, 0, 0) != 0: + raise OSError(ctypes.get_errno(), "PR_SET_NO_NEW_PRIVS failed") + if libc.prctl(22, 2, ctypes.addressof(fprog), 0, 0) != 0: + raise OSError(ctypes.get_errno(), "PR_SET_SECCOMP failed") + + +def _run_without_signals(argv: list[str]) -> subprocess.CompletedProcess: + proc = subprocess.Popen( + argv, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + preexec_fn=_ban_signals, + start_new_session=True, + ) + try: + stdout, stderr = proc.communicate(timeout=TIMEOUT_SECONDS) + except subprocess.TimeoutExpired: + os.killpg(proc.pid, signal.SIGKILL) + proc.communicate() + raise + return subprocess.CompletedProcess(argv, proc.returncode, stdout, stderr) + + +def _tree(root: Path, files: dict[str, str]) -> None: + for relative, content in files.items(): + path = root / relative + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(content) + + +def _stage(src: Path, dest: Path, *excludes: str) -> subprocess.CompletedProcess: + return _run_without_signals(["bash", str(STAGE), str(src), str(dest), *excludes]) + + +@unittest.skipUnless( + platform.machine() in AUDIT_ARCH, "seccomp table covers x86_64 and aarch64" +) +class StageWithoutSignalsTests(unittest.TestCase): + def setUp(self): + self._tmp = tempfile.TemporaryDirectory() + self.addCleanup(self._tmp.cleanup) + self.src = Path(self._tmp.name) / "src" + self.dest = Path(self._tmp.name) / "dest" + _tree(self.src, {"README.md": "readme\n", "roles/a/tasks/main.yml": "- x\n"}) + + def test_the_harness_refuses_every_signal(self): + result = _run_without_signals(["bash", "-c", "kill -0 $$"]) + self.assertNotEqual(result.returncode, 0) + self.assertIn("Permission denied", result.stderr) + + def test_staging_finishes_while_signals_are_refused(self): + result = _stage(self.src, self.dest) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertTrue( + filecmp.cmp( + self.src / "roles/a/tasks/main.yml", + self.dest / "roles/a/tasks/main.yml", + shallow=False, + ) + ) + + def test_a_name_pattern_drops_matches_at_every_depth(self): + _tree(self.src, {".git/HEAD": "ref\n", "roles/a/.git": "gitdir\n"}) + result = _stage(self.src, self.dest, ".git") + self.assertEqual(result.returncode, 0, result.stderr) + self.assertFalse((self.dest / ".git").exists()) + self.assertFalse((self.dest / "roles/a/.git").exists()) + self.assertTrue((self.dest / "roles/a/tasks/main.yml").is_file()) + + def test_a_path_pattern_drops_only_that_subtree(self): + _tree( + self.src, + {"packaging/debian/control": "c\n", "packaging/arch/PKGBUILD": "p\n"}, + ) + result = _stage(self.src, self.dest, "packaging/debian") + self.assertEqual(result.returncode, 0, result.stderr) + self.assertFalse((self.dest / "packaging/debian").exists()) + self.assertTrue((self.dest / "packaging/arch/PKGBUILD").is_file()) + + def test_a_previous_workspace_is_replaced(self): + _tree(self.dest, {"stale.txt": "old\n"}) + result = _stage(self.src, self.dest) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertFalse((self.dest / "stale.txt").exists()) + + def test_symlinks_stay_links(self): + (self.src / "link").symlink_to("README.md") + result = _stage(self.src, self.dest) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual((self.dest / "link").readlink(), Path("README.md")) + + def test_the_executable_bit_survives(self): + script = self.src / "run.sh" + script.write_text("#!/bin/sh\n") + script.chmod(0o755) + result = _stage(self.src, self.dest) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertTrue( + stat.S_IMODE((self.dest / "run.sh").stat().st_mode) & stat.S_IXUSR + ) + + def test_a_missing_destination_is_rejected(self): + result = _run_without_signals(["bash", str(STAGE), str(self.src)]) + self.assertNotEqual(result.returncode, 0) + self.assertFalse(self.dest.exists()) + + +if __name__ == "__main__": + unittest.main()