From 7b666471370dbadcf30bd7ceff8113c39f71e4dd Mon Sep 17 00:00:00 2001 From: Afonso Oliveira Date: Mon, 31 Aug 2026 21:27:48 +0100 Subject: [PATCH 1/5] Narrow OM-SEC-05 to signed T2 package provenance Retire the unauthenticated source, fail closed until all five signed Omarchy replacements exist, reinstall without trusting legacy bytes, and publish completion only after explicit transaction success. --- install/hardware/apple/fix-t2.sh | 48 +++++- install/hardware/pacman.sh | 16 +- migrations/1788163636.sh | 97 +++++++++++ test/shell.d/t2-package-provenance-test.sh | 184 +++++++++++++++++++++ 4 files changed, 328 insertions(+), 17 deletions(-) create mode 100644 migrations/1788163636.sh create mode 100644 test/shell.d/t2-package-provenance-test.sh diff --git a/install/hardware/apple/fix-t2.sh b/install/hardware/apple/fix-t2.sh index 948828d4d413..9aa2d3661b32 100644 --- a/install/hardware/apple/fix-t2.sh +++ b/install/hardware/apple/fix-t2.sh @@ -3,12 +3,50 @@ if lspci -nn | grep "106b:180[12]" >/dev/null; then echo "Detected MacBook with T2 chip. Installing support items..." - omarchy-pkg-add \ - linux-t2 \ - linux-t2-headers \ - apple-t2-audio-config \ - apple-bcm-firmware \ + t2_packages=( + linux-t2 + linux-t2-headers + apple-t2-audio-config + apple-bcm-firmware t2fanrd + ) + + effective_siglevel=$(/usr/bin/pacman-conf --repo omarchy SigLevel 2>/dev/null) || { + echo "Could not resolve the Omarchy repository signature policy; refusing T2 setup." >&2 + return 1 + } + if [[ -z ${effective_siglevel//[$' \t\n\r']/} ]]; then + effective_siglevel=$(/usr/bin/pacman-conf SigLevel 2>/dev/null) || { + echo "Could not resolve the inherited package signature policy; refusing T2 setup." >&2 + return 1 + } + fi + effective_siglevel=${effective_siglevel//$'\n'/ } + effective_siglevel=${effective_siglevel//$'\t'/ } + if [[ " $effective_siglevel " != *" PackageRequired "* || + " $effective_siglevel " != *" PackageTrustedOnly "* || + " $effective_siglevel " == *" PackageOptional "* || + " $effective_siglevel " == *" PackageNever "* || + " $effective_siglevel " == *" PackageTrustAll "* ]]; then + echo "The Omarchy repository is not enforcing trusted package signatures; refusing T2 setup." >&2 + return 1 + fi + + # These kernel-level artifacts used to come from an unsigned third-party + # repository. They must now exist in Omarchy's pinned-key repository before + # any installation starts; otherwise leave the machine unchanged and report + # the packaging prerequisite explicitly. + for package in "${t2_packages[@]}"; do + repository=$(LC_ALL=C /usr/bin/pacman -Si "$package" 2>/dev/null | + /usr/bin/awk -F: '/^Repository[[:space:]]*:/ { gsub(/[[:space:]]/, "", $2); print $2; exit }') + if [[ $repository != "omarchy" ]]; then + echo "Authenticated T2 package '$package' is unavailable from the Omarchy repository." >&2 + echo "T2 setup cannot continue until all support packages are published there with Omarchy signatures." >&2 + return 1 + fi + done + + omarchy-pkg-add "${t2_packages[@]}" # Enable T2 fan control systemctl enable t2fanrd.service diff --git a/install/hardware/pacman.sh b/install/hardware/pacman.sh index 7533d3f378cf..0eaca8225140 100644 --- a/install/hardware/pacman.sh +++ b/install/hardware/pacman.sh @@ -1,12 +1,4 @@ -# Hardware-specific pacman repository extensions that must survive the final -# pacman.conf restore. -if lspci -nn | grep "106b:180[12]" >/dev/null; then - if ! grep -q '^\[arch-mact2\]' /etc/pacman.conf; then - cat >> /etc/pacman.conf <<'EOF' - -[arch-mact2] -Server = https://github.com/NoaHimesaka1873/arch-mact2-mirror/releases/download/release -SigLevel = Never -EOF - fi -fi +# Hardware-specific repositories belong here only when their packages have an +# authenticated signing chain. T2 support is distributed through Omarchy's +# signature-required repository; never restore the retired unsigned arch-mact2 +# source. diff --git a/migrations/1788163636.sh b/migrations/1788163636.sh new file mode 100644 index 000000000000..b959646cd16b --- /dev/null +++ b/migrations/1788163636.sh @@ -0,0 +1,97 @@ +echo "Replace unauthenticated Apple T2 packages with signed Omarchy packages" + +pacman_conf=/etc/pacman.conf +repair_marker=/var/lib/omarchy/t2-package-provenance-repaired +t2_packages=(linux-t2 linux-t2-headers apple-t2-audio-config apple-bcm-firmware t2fanrd) + +disable_unsafe_t2_repository() ( + [[ -f $pacman_conf ]] || return 0 + local section backup + + section=$(/usr/bin/awk ' + found && /^[[:space:]]*\[/ { exit } + /^[[:space:]]*\[arch-mact2\][[:space:]]*$/ { found = 1 } + found { print } + ' "$pacman_conf") + [[ -n $section ]] || return 0 + /usr/bin/grep -Eq '^[[:space:]]*SigLevel[[:space:]]*=.*((Package)?(Never|Optional)|TrustAll)([[:space:]]|$)' <<<"$section" || return 0 + + backup="$(/usr/bin/dirname "$pacman_conf")/arch-mact2.omarchy-disabled.$(/usr/bin/date +%s).txt" + sudo /usr/bin/install -T -o root -g root -m 0600 /dev/stdin "$backup" <<<"$section" + /usr/bin/awk ' + /^[[:space:]]*\[arch-mact2\][[:space:]]*$/ { drop = 1; next } + drop && /^[[:space:]]*\[/ { drop = 0 } + !drop { print } + ' "$pacman_conf" | + sudo /usr/bin/install -T -o root -g root -m 0644 /dev/stdin "$pacman_conf" + echo "Disabled the unsafe arch-mact2 section; preserved it at $backup" +) + +# This is intentionally first. If package discovery, download, signature +# validation, or reinstall fails later, retrying is safe and the unsigned +# repository remains unavailable. +disable_unsafe_t2_repository + +[[ ! -e $repair_marker ]] || exit 0 + +has_t2_hardware=0 +if /usr/bin/lspci -nn | /usr/bin/grep "106b:180[12]" >/dev/null; then + has_t2_hardware=1 +fi +has_t2_packages=0 +for package in "${t2_packages[@]}"; do + if /usr/bin/pacman -Q "$package" >/dev/null 2>&1; then + has_t2_packages=1 + break + fi +done +((has_t2_hardware || has_t2_packages)) || exit 0 + +# Do not assume an earlier update helper ran this migration. Ask pacman for the +# effective policy and require both mandatory package signatures and trusted +# signers before querying or reinstalling any replacement. +effective_siglevel=$(/usr/bin/pacman-conf --config "$pacman_conf" --repo omarchy SigLevel 2>/dev/null) || { + echo "Could not resolve the Omarchy repository signature policy; refusing T2 replacement." >&2 + exit 1 +} +if [[ -z ${effective_siglevel//[$' \t\n\r']/} ]]; then + effective_siglevel=$(/usr/bin/pacman-conf --config "$pacman_conf" SigLevel 2>/dev/null) || { + echo "Could not resolve the inherited package signature policy; refusing T2 replacement." >&2 + exit 1 + } +fi +effective_siglevel=${effective_siglevel//$'\n'/ } +effective_siglevel=${effective_siglevel//$'\t'/ } +if [[ " $effective_siglevel " != *" PackageRequired "* || + " $effective_siglevel " != *" PackageTrustedOnly "* || + " $effective_siglevel " == *" PackageOptional "* || + " $effective_siglevel " == *" PackageNever "* || + " $effective_siglevel " == *" PackageTrustAll "* ]]; then + echo "The Omarchy repository does not enforce trusted package signatures; refusing T2 replacement." >&2 + exit 1 +fi + +# Resolve every replacement before beginning the reinstall, and require the +# pinned-key Omarchy repository by name. The repository itself inherits the +# effective package-required/trusted-only policy verified above. +for package in "${t2_packages[@]}"; do + repository=$(LC_ALL=C /usr/bin/pacman -Si "$package" 2>/dev/null | + /usr/bin/awk -F: '/^Repository[[:space:]]*:/ { gsub(/[[:space:]]/, "", $2); print $2; exit }') + if [[ $repository != "omarchy" ]]; then + echo "Authenticated replacement '$package' is unavailable from the Omarchy repository." >&2 + echo "The unsafe repository is disabled. Publish all signed T2 artifacts, then retry this migration." >&2 + exit 1 + fi +done + +# No --needed: the bytes already installed under SigLevel=Never have no trusted +# provenance even when their version equals the signed replacement. +if ! sudo /usr/bin/pacman -S --noconfirm "${t2_packages[@]}"; then + echo "Authenticated T2 package reinstall failed; leaving the repair pending." >&2 + exit 1 +fi + +marker_dir=$(/usr/bin/dirname "$repair_marker") +sudo /usr/bin/install -d -o root -g root -m 0755 "$marker_dir" +printf 'Reinstalled from signed Omarchy repository at %s\n' "$(/usr/bin/date --iso-8601=seconds)" | + sudo /usr/bin/install -T -o root -g root -m 0644 /dev/stdin "$repair_marker" diff --git a/test/shell.d/t2-package-provenance-test.sh b/test/shell.d/t2-package-provenance-test.sh new file mode 100644 index 000000000000..f4989a57e810 --- /dev/null +++ b/test/shell.d/t2-package-provenance-test.sh @@ -0,0 +1,184 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +t2_migration="$ROOT/migrations/1788163636.sh" +test_tmp=$(mktemp -d) +trap 'rm -rf "$test_tmp"' EXIT + +# Exercise the provenance migration with trusted absolute command paths mapped +# to deterministic stubs. Every scenario starts with unsafe T2 policy plus an +# unrelated administrator section. +stub_bin="$test_tmp/bin" +mkdir "$stub_bin" +cat >"$stub_bin/sudo" <<'STUB' +#!/bin/bash +if [[ $1 == /usr/bin/install ]]; then + filtered=("$1") + shift + while (($#)); do + case $1 in + -o|-g) shift 2 ;; + *) filtered+=("$1"); shift ;; + esac + done + exec "${filtered[@]}" +fi +exec "$@" +STUB +cat >"$stub_bin/lspci" <<'STUB' +#!/bin/bash +exit 0 +STUB +cat >"$stub_bin/pacman-conf" <<'STUB' +#!/bin/bash +if [[ " $* " == *" --repo omarchy "* ]]; then + printf '%s\n' "${TEST_REPO_SIGLEVEL-${TEST_SIGLEVEL:-PackageRequired PackageTrustedOnly}}" +else + printf '%s\n' "${TEST_GLOBAL_SIGLEVEL-${TEST_SIGLEVEL:-PackageRequired PackageTrustedOnly}}" +fi +STUB +cat >"$stub_bin/pacman" <<'STUB' +#!/bin/bash +case $1 in + -Q) [[ $2 == linux-t2 ]] ;; + -Si) + repository=${TEST_REPOSITORY:-omarchy} + [[ ${TEST_MISSING_PACKAGE:-} != "$2" ]] || repository=core + printf 'Repository : %s\n' "$repository" + ;; + -S) + printf 'TRANSACTION %s\n' "$*" >>"${TEST_TRANSACTION_LOG:?}" + exit "${TEST_TRANSACTION_STATUS:-0}" + ;; + *) exit 2 ;; +esac +STUB +chmod +x "$stub_bin"/* + +mapped_migration_template="$test_tmp/t2-migration.template.sh" +sed -e "s#/usr/bin/pacman-conf#$stub_bin/pacman-conf#g" \ + -e "s#/usr/bin/pacman#$stub_bin/pacman#g" \ + -e "s#/usr/bin/lspci#$stub_bin/lspci#g" "$t2_migration" >"$mapped_migration_template" + +run_t2_scenario() { + local name="$1" policy="$2" repository="$3" + local repo_policy="${4-$policy}" global_policy="${5-$policy}" + local missing_package="${6:-}" transaction_status="${7:-0}" + local dir="$test_tmp/$name" + mkdir "$dir" + cat >"$dir/pacman.conf" <<'CONF' +[options] +SigLevel = Required DatabaseOptional +[arch-mact2] +Server = https://unsafe.invalid/ +SigLevel = Never +[administrator] +Server = file:///srv/admin +SigLevel = Required +[omarchy] +Server = https://pkgs.omarchy.org/ +CONF + : >"$dir/transactions" + sed \ + -e "s|^pacman_conf=/etc/pacman.conf$|pacman_conf=$dir/pacman.conf|" \ + -e "s|^repair_marker=/var/lib/omarchy/t2-package-provenance-repaired$|repair_marker=$dir/marker|" \ + "$mapped_migration_template" >"$dir/t2-migration.sh" + HOME="$dir" PATH="$stub_bin:$PATH" TEST_REPO_SIGLEVEL="$repo_policy" \ + TEST_GLOBAL_SIGLEVEL="$global_policy" TEST_REPOSITORY="$repository" \ + TEST_MISSING_PACKAGE="$missing_package" TEST_TRANSACTION_STATUS="$transaction_status" \ + TEST_TRANSACTION_LOG="$dir/transactions" bash "$dir/t2-migration.sh" >"$dir/output" 2>&1 +} + +if run_t2_scenario insecure 'PackageOptional PackageTrustAll' omarchy; then + fail "T2 migration accepts insecure effective package policy" +fi +! grep -q '^TRANSACTION' "$test_tmp/insecure/transactions" || fail "insecure T2 policy reaches pacman transaction" +! grep -q '^\[arch-mact2\]' "$test_tmp/insecure/pacman.conf" || fail "unsafe T2 repo survives failed migration" +grep -q '^\[administrator\]' "$test_tmp/insecure/pacman.conf" || fail "administrator repo was removed" +backup=$(find "$test_tmp/insecure" -name 'arch-mact2.omarchy-disabled.*.txt' -print -quit) +[[ -n $backup && $(stat -c '%a' "$backup") == 600 ]] || fail "unsafe T2 section was not privately backed up" + +if run_t2_scenario unavailable 'PackageRequired PackageTrustedOnly' core; then + fail "T2 migration accepts unavailable signed replacements" +fi +! grep -q '^TRANSACTION' "$test_tmp/unavailable/transactions" || fail "missing T2 artifacts reach pacman transaction" + +if run_t2_scenario partial 'PackageRequired PackageTrustedOnly' omarchy \ + 'PackageRequired PackageTrustedOnly' 'PackageRequired PackageTrustedOnly' t2fanrd; then + fail "T2 migration accepts a partial signed replacement set" +fi +! grep -q '^TRANSACTION' "$test_tmp/partial/transactions" || fail "partial T2 artifacts reach pacman transaction" + +if run_t2_scenario reinstall-failure 'PackageRequired PackageTrustedOnly' omarchy \ + 'PackageRequired PackageTrustedOnly' 'PackageRequired PackageTrustedOnly' '' 33; then + fail "T2 migration accepts a failed authenticated reinstall" +fi +[[ ! -e $test_tmp/reinstall-failure/marker ]] || fail "failed T2 reinstall publishes a completion marker" + +run_t2_scenario signed 'PackageRequired PackageTrustedOnly' omarchy +transaction=$(<"$test_tmp/signed/transactions") +[[ $transaction == *'linux-t2 linux-t2-headers apple-t2-audio-config apple-bcm-firmware t2fanrd'* ]] || + fail "T2 migration does not reinstall every replacement together" +[[ $transaction != *'--needed'* ]] || fail "T2 migration trusts bytes installed under SigLevel=Never" +[[ -f $test_tmp/signed/marker ]] || fail "successful authenticated T2 replacement is not recorded" +: >"$test_tmp/signed/transactions" +HOME="$test_tmp/signed" PATH="$stub_bin:$PATH" \ + TEST_REPO_SIGLEVEL='PackageRequired PackageTrustedOnly' \ + TEST_GLOBAL_SIGLEVEL='PackageRequired PackageTrustedOnly' TEST_REPOSITORY=omarchy \ + TEST_TRANSACTION_LOG="$test_tmp/signed/transactions" bash "$test_tmp/signed/t2-migration.sh" >/dev/null +[[ ! -s $test_tmp/signed/transactions ]] || fail "completed T2 repair reinstalls packages again" +run_t2_scenario inherited '' omarchy '' 'PackageRequired PackageTrustedOnly' +[[ -f $test_tmp/inherited/marker ]] || fail "T2 migration rejects a secure inherited global package policy" +pass "T2 migration disables unsafe policy first and fails closed until all signed replacements exist" + +# Fresh installation cannot recreate the unsigned repository path. +! rg -n 'SigLevel[[:space:]]*=[[:space:]]*(Never|Optional)|TrustAll|arch-mact2-mirror' \ + "$ROOT/install/hardware/pacman.sh" "$ROOT/install/post-install/pacman.sh" >/dev/null || + fail "fresh installer retains unauthenticated T2 repository configuration" +grep -F '/usr/bin/pacman-conf --repo omarchy SigLevel' "$ROOT/install/hardware/apple/fix-t2.sh" >/dev/null +pass "fresh T2 setup independently requires Omarchy's trusted-only package policy" + +# A minimal unsigned local package under the final policy must be rejected by +# real pacman. DatabaseOptional permits the unsigned database, never a package. +if command -v repo-add >/dev/null && command -v bsdtar >/dev/null && command -v zstd >/dev/null && + unshare --user --map-root-user true 2>/dev/null; then + repo="$test_tmp/repo" + root="$test_tmp/pacman-root" + mkdir -p "$repo/pkg" "$root/var/lib/pacman" "$root/var/cache/pacman/pkg" "$root/etc/pacman.d/gnupg" + cat >"$repo/pkg/.PKGINFO" <<'PKG' +pkgname = omarchy-audit-unsigned +pkgver = 1-1 +pkgdesc = isolated unsigned audit fixture +builddate = 1 +packager = Omarchy test +size = 0 +arch = any +PKG + bsdtar -C "$repo/pkg" -cf - .PKGINFO | zstd -q -o "$repo/omarchy-audit-unsigned-1-1-any.pkg.tar.zst" + repo-add -q "$repo/omarchy.db.tar.gz" "$repo/omarchy-audit-unsigned-1-1-any.pkg.tar.zst" + cat >"$test_tmp/pacman-test.conf" </dev/null + if pacman --config "$1" --root "$2" --dbpath "$2/var/lib/pacman" \ + --cachedir "$2/var/cache/pacman/pkg" -S --noconfirm omarchy-audit-unsigned >"$3" 2>&1; then + exit 90 + fi + ' _ "$test_tmp/pacman-test.conf" "$root" "$test_tmp/pacman.out"; then + fail "isolated pacman did not enforce the final signature policy" "$(cat "$test_tmp/pacman.out" 2>/dev/null || true)" + fi + [[ ! -e $root/usr ]] || fail "unsigned package modified isolated pacman root" + pass "real pacman rejects unsigned packages under final Omarchy policy" +else + pass "package construction tools unavailable; static signature-policy checks completed" +fi From 491bb5b5011fd10f8ff7d9f14af0837560d322a2 Mon Sep 17 00:00:00 2001 From: Afonso Oliveira Date: Sat, 5 Sep 2026 15:50:50 +0100 Subject: [PATCH 2/5] Rewrite T2 pacman policy atomically --- migrations/1788163636.sh | 11 +++++++++-- test/shell.d/t2-package-provenance-test.sh | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/migrations/1788163636.sh b/migrations/1788163636.sh index b959646cd16b..a2c5d55621dd 100644 --- a/migrations/1788163636.sh +++ b/migrations/1788163636.sh @@ -6,7 +6,7 @@ t2_packages=(linux-t2 linux-t2-headers apple-t2-audio-config apple-bcm-firmware disable_unsafe_t2_repository() ( [[ -f $pacman_conf ]] || return 0 - local section backup + local section backup stage="" section=$(/usr/bin/awk ' found && /^[[:space:]]*\[/ { exit } @@ -18,12 +18,19 @@ disable_unsafe_t2_repository() ( backup="$(/usr/bin/dirname "$pacman_conf")/arch-mact2.omarchy-disabled.$(/usr/bin/date +%s).txt" sudo /usr/bin/install -T -o root -g root -m 0600 /dev/stdin "$backup" <<<"$section" + stage=$(sudo /usr/bin/mktemp \ + --tmpdir="$(/usr/bin/dirname "$pacman_conf")" \ + '.pacman.conf.omarchy-t2.XXXXXXXX') + trap '[[ -z ${stage:-} ]] || sudo /usr/bin/rm -f -- "$stage"' EXIT /usr/bin/awk ' /^[[:space:]]*\[arch-mact2\][[:space:]]*$/ { drop = 1; next } drop && /^[[:space:]]*\[/ { drop = 0 } !drop { print } ' "$pacman_conf" | - sudo /usr/bin/install -T -o root -g root -m 0644 /dev/stdin "$pacman_conf" + sudo /usr/bin/install -T -o root -g root -m 0644 /dev/stdin "$stage" + sudo /usr/bin/mv -Tf -- "$stage" "$pacman_conf" + stage="" + trap - EXIT echo "Disabled the unsafe arch-mact2 section; preserved it at $backup" ) diff --git a/test/shell.d/t2-package-provenance-test.sh b/test/shell.d/t2-package-provenance-test.sh index f4989a57e810..a6e697849551 100644 --- a/test/shell.d/t2-package-provenance-test.sh +++ b/test/shell.d/t2-package-provenance-test.sh @@ -67,6 +67,7 @@ run_t2_scenario() { local name="$1" policy="$2" repository="$3" local repo_policy="${4-$policy}" global_policy="${5-$policy}" local missing_package="${6:-}" transaction_status="${7:-0}" + local padding_lines="${8:-0}" local dir="$test_tmp/$name" mkdir "$dir" cat >"$dir/pacman.conf" <<'CONF' @@ -81,6 +82,15 @@ SigLevel = Required [omarchy] Server = https://pkgs.omarchy.org/ CONF + if (( padding_lines > 0 )); then + /usr/bin/awk -v padding_lines="$padding_lines" ' + /^\[arch-mact2\]$/ { + for (line = 0; line < padding_lines; line++) print "# pipeline-truncation-padding" + } + { print } + ' "$dir/pacman.conf" >"$dir/pacman.conf.padded" + /usr/bin/mv "$dir/pacman.conf.padded" "$dir/pacman.conf" + fi : >"$dir/transactions" sed \ -e "s|^pacman_conf=/etc/pacman.conf$|pacman_conf=$dir/pacman.conf|" \ @@ -132,6 +142,12 @@ HOME="$test_tmp/signed" PATH="$stub_bin:$PATH" \ [[ ! -s $test_tmp/signed/transactions ]] || fail "completed T2 repair reinstalls packages again" run_t2_scenario inherited '' omarchy '' 'PackageRequired PackageTrustedOnly' [[ -f $test_tmp/inherited/marker ]] || fail "T2 migration rejects a secure inherited global package policy" +run_t2_scenario large 'PackageRequired PackageTrustedOnly' omarchy \ + 'PackageRequired PackageTrustedOnly' 'PackageRequired PackageTrustedOnly' '' 0 20000 +grep -q '^\[omarchy\]$' "$test_tmp/large/pacman.conf" || + fail "T2 migration truncated a pacman.conf larger than the pipe buffer" +[[ -z $(find "$test_tmp/large" -maxdepth 1 -name '.pacman.conf.omarchy-t2.*' -print -quit) ]] || + fail "T2 migration left its root-owned pacman.conf stage behind" pass "T2 migration disables unsafe policy first and fails closed until all signed replacements exist" # Fresh installation cannot recreate the unsigned repository path. From 9363be2995a62a8ec6f0e87d4522bb054a5567c3 Mon Sep 17 00:00:00 2001 From: Afonso Oliveira Date: Sat, 5 Sep 2026 16:07:29 +0100 Subject: [PATCH 3/5] Handle failed T2 package discovery --- install/hardware/apple/fix-t2.sh | 15 +++++- migrations/1788163636.sh | 15 +++++- test/shell.d/t2-package-provenance-test.sh | 59 ++++++++++++++++++++-- 3 files changed, 82 insertions(+), 7 deletions(-) diff --git a/install/hardware/apple/fix-t2.sh b/install/hardware/apple/fix-t2.sh index 9aa2d3661b32..94fb668891a3 100644 --- a/install/hardware/apple/fix-t2.sh +++ b/install/hardware/apple/fix-t2.sh @@ -37,8 +37,19 @@ if lspci -nn | grep "106b:180[12]" >/dev/null; then # any installation starts; otherwise leave the machine unchanged and report # the packaging prerequisite explicitly. for package in "${t2_packages[@]}"; do - repository=$(LC_ALL=C /usr/bin/pacman -Si "$package" 2>/dev/null | - /usr/bin/awk -F: '/^Repository[[:space:]]*:/ { gsub(/[[:space:]]/, "", $2); print $2; exit }') + if ! repository_metadata=$(LC_ALL=C /usr/bin/pacman -Si "$package" 2>/dev/null); then + repository="" + else + # Consume the complete producer output. Exiting awk after the first field + # turns a large successful pacman query into SIGPIPE under pipefail. + repository=$(/usr/bin/awk -F: ' + !found && /^Repository[[:space:]]*:/ { + gsub(/[[:space:]]/, "", $2) + print $2 + found = 1 + } + ' <<<"$repository_metadata") + fi if [[ $repository != "omarchy" ]]; then echo "Authenticated T2 package '$package' is unavailable from the Omarchy repository." >&2 echo "T2 setup cannot continue until all support packages are published there with Omarchy signatures." >&2 diff --git a/migrations/1788163636.sh b/migrations/1788163636.sh index a2c5d55621dd..c73d5f4bd10f 100644 --- a/migrations/1788163636.sh +++ b/migrations/1788163636.sh @@ -82,8 +82,19 @@ fi # pinned-key Omarchy repository by name. The repository itself inherits the # effective package-required/trusted-only policy verified above. for package in "${t2_packages[@]}"; do - repository=$(LC_ALL=C /usr/bin/pacman -Si "$package" 2>/dev/null | - /usr/bin/awk -F: '/^Repository[[:space:]]*:/ { gsub(/[[:space:]]/, "", $2); print $2; exit }') + if ! repository_metadata=$(LC_ALL=C /usr/bin/pacman -Si "$package" 2>/dev/null); then + repository="" + else + # Consume the complete producer output. Exiting awk after the first field + # turns a large successful pacman query into SIGPIPE under pipefail. + repository=$(/usr/bin/awk -F: ' + !found && /^Repository[[:space:]]*:/ { + gsub(/[[:space:]]/, "", $2) + print $2 + found = 1 + } + ' <<<"$repository_metadata") + fi if [[ $repository != "omarchy" ]]; then echo "Authenticated replacement '$package' is unavailable from the Omarchy repository." >&2 echo "The unsafe repository is disabled. Publish all signed T2 artifacts, then retry this migration." >&2 diff --git a/test/shell.d/t2-package-provenance-test.sh b/test/shell.d/t2-package-provenance-test.sh index a6e697849551..dce47fe94c8e 100644 --- a/test/shell.d/t2-package-provenance-test.sh +++ b/test/shell.d/t2-package-provenance-test.sh @@ -30,7 +30,8 @@ exec "$@" STUB cat >"$stub_bin/lspci" <<'STUB' #!/bin/bash -exit 0 +(( ${TEST_T2_HARDWARE:-0} == 1 )) || exit 0 +printf '%s\n' '01:00.0 Bridge [0680]: Apple Inc. T2 Security Chip [106b:1801]' STUB cat >"$stub_bin/pacman-conf" <<'STUB' #!/bin/bash @@ -48,6 +49,10 @@ case $1 in repository=${TEST_REPOSITORY:-omarchy} [[ ${TEST_MISSING_PACKAGE:-} != "$2" ]] || repository=core printf 'Repository : %s\n' "$repository" + for ((line = 0; line < ${TEST_QUERY_PADDING_LINES:-0}; line++)); do + printf 'Description : metadata padding %s\n' "$line" + done + [[ ${TEST_QUERY_FAIL_PACKAGE:-} != "$2" ]] || exit 44 ;; -S) printf 'TRANSACTION %s\n' "$*" >>"${TEST_TRANSACTION_LOG:?}" @@ -68,6 +73,7 @@ run_t2_scenario() { local repo_policy="${4-$policy}" global_policy="${5-$policy}" local missing_package="${6:-}" transaction_status="${7:-0}" local padding_lines="${8:-0}" + local query_fail_package="${9:-}" query_padding_lines="${10:-0}" local dir="$test_tmp/$name" mkdir "$dir" cat >"$dir/pacman.conf" <<'CONF' @@ -99,7 +105,8 @@ CONF HOME="$dir" PATH="$stub_bin:$PATH" TEST_REPO_SIGLEVEL="$repo_policy" \ TEST_GLOBAL_SIGLEVEL="$global_policy" TEST_REPOSITORY="$repository" \ TEST_MISSING_PACKAGE="$missing_package" TEST_TRANSACTION_STATUS="$transaction_status" \ - TEST_TRANSACTION_LOG="$dir/transactions" bash "$dir/t2-migration.sh" >"$dir/output" 2>&1 + TEST_QUERY_FAIL_PACKAGE="$query_fail_package" TEST_QUERY_PADDING_LINES="$query_padding_lines" \ + TEST_TRANSACTION_LOG="$dir/transactions" bash -euo pipefail "$dir/t2-migration.sh" >"$dir/output" 2>&1 } if run_t2_scenario insecure 'PackageOptional PackageTrustAll' omarchy; then @@ -122,6 +129,36 @@ if run_t2_scenario partial 'PackageRequired PackageTrustedOnly' omarchy \ fi ! grep -q '^TRANSACTION' "$test_tmp/partial/transactions" || fail "partial T2 artifacts reach pacman transaction" +# A real unavailable package makes `pacman -Si` nonzero. Emit more than a pipe +# buffer in a successful query too: the old early-exiting parser converted its +# producer into status 141 under pipefail. +run_t2_scenario large-query 'PackageRequired PackageTrustedOnly' omarchy \ + 'PackageRequired PackageTrustedOnly' 'PackageRequired PackageTrustedOnly' '' 0 0 '' 20000 +[[ -f $test_tmp/large-query/marker ]] || fail "large T2 repository metadata aborts a valid replacement" + +# On a nonzero query, production errexit used to skip the promised recovery +# text. Combining both conditions proves failure is captured before parsing. +if run_t2_scenario query-failure 'PackageRequired PackageTrustedOnly' omarchy \ + 'PackageRequired PackageTrustedOnly' 'PackageRequired PackageTrustedOnly' '' 0 0 linux-t2 20000; then + fail "T2 migration accepts a failed repository query" +fi +grep -q "Authenticated replacement 'linux-t2' is unavailable" "$test_tmp/query-failure/output" || + fail "failed T2 repository query skips its recovery guidance" +grep -q 'Publish all signed T2 artifacts, then retry this migration' "$test_tmp/query-failure/output" || + fail "failed T2 repository query omits retry guidance" +! grep -q '^TRANSACTION' "$test_tmp/query-failure/transactions" || + fail "failed T2 repository query reaches a package transaction" +! grep -q '^\[arch-mact2\]' "$test_tmp/query-failure/pacman.conf" || + fail "failed T2 repository query leaves the unsafe repository enabled" +[[ ! -e $test_tmp/query-failure/marker ]] || fail "failed T2 repository query publishes completion" +HOME="$test_tmp/query-failure" PATH="$stub_bin:$PATH" \ + TEST_REPO_SIGLEVEL='PackageRequired PackageTrustedOnly' \ + TEST_GLOBAL_SIGLEVEL='PackageRequired PackageTrustedOnly' TEST_REPOSITORY=omarchy \ + TEST_TRANSACTION_LOG="$test_tmp/query-failure/transactions" \ + bash -euo pipefail "$test_tmp/query-failure/t2-migration.sh" >/dev/null +[[ -f $test_tmp/query-failure/marker ]] || fail "T2 repository query failure is not retryable" +pass "failed and large T2 repository queries fail with guidance and retry cleanly" + if run_t2_scenario reinstall-failure 'PackageRequired PackageTrustedOnly' omarchy \ 'PackageRequired PackageTrustedOnly' 'PackageRequired PackageTrustedOnly' '' 33; then fail "T2 migration accepts a failed authenticated reinstall" @@ -155,7 +192,23 @@ pass "T2 migration disables unsafe policy first and fails closed until all signe "$ROOT/install/hardware/pacman.sh" "$ROOT/install/post-install/pacman.sh" >/dev/null || fail "fresh installer retains unauthenticated T2 repository configuration" grep -F '/usr/bin/pacman-conf --repo omarchy SigLevel' "$ROOT/install/hardware/apple/fix-t2.sh" >/dev/null -pass "fresh T2 setup independently requires Omarchy's trusted-only package policy" + +mapped_fresh_setup="$test_tmp/fix-t2.mapped.sh" +sed -e "s#/usr/bin/pacman-conf#$stub_bin/pacman-conf#g" \ + -e "s#/usr/bin/pacman#$stub_bin/pacman#g" \ + "$ROOT/install/hardware/apple/fix-t2.sh" >"$mapped_fresh_setup" +if TEST_T2_HARDWARE=1 TEST_REPO_SIGLEVEL='PackageRequired PackageTrustedOnly' \ + TEST_GLOBAL_SIGLEVEL='PackageRequired PackageTrustedOnly' TEST_REPOSITORY=omarchy \ + TEST_QUERY_FAIL_PACKAGE=linux-t2 TEST_QUERY_PADDING_LINES=20000 \ + PATH="$stub_bin:$PATH" bash -euo pipefail -c 'source "$1"' bash "$mapped_fresh_setup" \ + >"$test_tmp/fresh-query-failure.output" 2>&1; then + fail "fresh T2 setup accepts a failed repository query" +fi +grep -q "Authenticated T2 package 'linux-t2' is unavailable" "$test_tmp/fresh-query-failure.output" || + fail "fresh T2 repository query skips its recovery guidance" +grep -q 'cannot continue until all support packages are published' "$test_tmp/fresh-query-failure.output" || + fail "fresh T2 repository query omits release guidance" +pass "fresh T2 setup independently enforces policy and reports failed large queries" # A minimal unsigned local package under the final policy must be rejected by # real pacman. DatabaseOptional permits the unsigned database, never a package. From 4bb298c2ade0aa12b1b65e4d69698a976640d355 Mon Sep 17 00:00:00 2001 From: Afonso Oliveira Date: Mon, 7 Sep 2026 21:06:59 +0100 Subject: [PATCH 4/5] Resolve effective T2 package signature policy --- migrations/1788163636.sh | 25 +++++++++- test/shell.d/t2-package-provenance-test.sh | 55 ++++++++++++++++++++-- 2 files changed, 74 insertions(+), 6 deletions(-) diff --git a/migrations/1788163636.sh b/migrations/1788163636.sh index c73d5f4bd10f..bb3b1c562090 100644 --- a/migrations/1788163636.sh +++ b/migrations/1788163636.sh @@ -6,7 +6,7 @@ t2_packages=(linux-t2 linux-t2-headers apple-t2-audio-config apple-bcm-firmware disable_unsafe_t2_repository() ( [[ -f $pacman_conf ]] || return 0 - local section backup stage="" + local section backup stage="" signature_policy section=$(/usr/bin/awk ' found && /^[[:space:]]*\[/ { exit } @@ -14,7 +14,28 @@ disable_unsafe_t2_repository() ( found { print } ' "$pacman_conf") [[ -n $section ]] || return 0 - /usr/bin/grep -Eq '^[[:space:]]*SigLevel[[:space:]]*=.*((Package)?(Never|Optional)|TrustAll)([[:space:]]|$)' <<<"$section" || return 0 + + # Resolve includes, inherited policy and directive precedence through pacman + # itself. DatabaseOptional/DatabaseNever do not weaken package signatures. + if ! signature_policy=$(/usr/bin/pacman-conf --config "$pacman_conf" --repo arch-mact2 SigLevel); then + echo "Could not resolve the arch-mact2 signature policy; leaving the repair pending." >&2 + return 1 + fi + if [[ -z ${signature_policy//[$' \t\n\r']/} ]]; then + if ! signature_policy=$(/usr/bin/pacman-conf --config "$pacman_conf" SigLevel); then + echo "Could not resolve the inherited signature policy; leaving the repair pending." >&2 + return 1 + fi + fi + signature_policy=${signature_policy//$'\n'/ } + signature_policy=${signature_policy//$'\t'/ } + if [[ " $signature_policy " == *" PackageRequired "* && + " $signature_policy " == *" PackageTrustedOnly "* && + " $signature_policy " != *" PackageOptional "* && + " $signature_policy " != *" PackageNever "* && + " $signature_policy " != *" PackageTrustAll "* ]]; then + return 0 + fi backup="$(/usr/bin/dirname "$pacman_conf")/arch-mact2.omarchy-disabled.$(/usr/bin/date +%s).txt" sudo /usr/bin/install -T -o root -g root -m 0600 /dev/stdin "$backup" <<<"$section" diff --git a/test/shell.d/t2-package-provenance-test.sh b/test/shell.d/t2-package-provenance-test.sh index dce47fe94c8e..93b09f70884a 100644 --- a/test/shell.d/t2-package-provenance-test.sh +++ b/test/shell.d/t2-package-provenance-test.sh @@ -4,6 +4,8 @@ set -euo pipefail source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" +require_command pacman-conf + t2_migration="$ROOT/migrations/1788163636.sh" test_tmp=$(mktemp -d) trap 'rm -rf "$test_tmp"' EXIT @@ -35,7 +37,9 @@ printf '%s\n' '01:00.0 Bridge [0680]: Apple Inc. T2 Security Chip [106b:1801]' STUB cat >"$stub_bin/pacman-conf" <<'STUB' #!/bin/bash -if [[ " $* " == *" --repo omarchy "* ]]; then +if [[ ${TEST_NATIVE_SIGLEVEL:-0} == 1 || " $* " == *" --repo arch-mact2 "* ]]; then + exec /usr/bin/pacman-conf "$@" +elif [[ " $* " == *" --repo omarchy "* ]]; then printf '%s\n' "${TEST_REPO_SIGLEVEL-${TEST_SIGLEVEL:-PackageRequired PackageTrustedOnly}}" else printf '%s\n' "${TEST_GLOBAL_SIGLEVEL-${TEST_SIGLEVEL:-PackageRequired PackageTrustedOnly}}" @@ -76,17 +80,18 @@ run_t2_scenario() { local query_fail_package="${9:-}" query_padding_lines="${10:-0}" local dir="$test_tmp/$name" mkdir "$dir" - cat >"$dir/pacman.conf" <<'CONF' + cat >"$dir/pacman.conf" < 0 )); then /usr/bin/awk -v padding_lines="$padding_lines" ' @@ -109,6 +114,48 @@ CONF TEST_TRANSACTION_LOG="$dir/transactions" bash -euo pipefail "$dir/t2-migration.sh" >"$dir/output" 2>&1 } +check_legacy_policy() { + local name="$1" policy="$2" expected="$3" global_policy="${4:-Required DatabaseOptional}" + TEST_NATIVE_SIGLEVEL=1 TEST_LEGACY_POLICY="$policy" TEST_LEGACY_GLOBAL_POLICY="$global_policy" \ + run_t2_scenario "$name" 'PackageRequired PackageTrustedOnly' omarchy + [[ -f $test_tmp/$name/marker ]] || fail "legacy policy case $name did not finish replacement" + if [[ $expected == "preserve" ]]; then + grep -q '^\[arch-mact2\]$' "$test_tmp/$name/pacman.conf" || + fail "secure legacy package policy $name was removed" + [[ -z $(find "$test_tmp/$name" -name 'arch-mact2.omarchy-disabled.*.txt' -print -quit) ]] || + fail "secure legacy package policy $name was backed up as unsafe" + else + ! grep -q '^\[arch-mact2\]$' "$test_tmp/$name/pacman.conf" || + fail "unsafe legacy package policy $name was retained" + [[ -n $(find "$test_tmp/$name" -name 'arch-mact2.omarchy-disabled.*.txt' -print -quit) ]] || + fail "unsafe legacy package policy $name has no recovery backup" + fi +} + +check_legacy_policy database-optional 'SigLevel = PackageRequired DatabaseOptional PackageTrustedOnly' preserve +check_legacy_policy database-never 'SigLevel = PackageRequired DatabaseNever PackageTrustedOnly' preserve +check_legacy_policy database-trust-all 'SigLevel = PackageRequired DatabaseTrustAll PackageTrustedOnly' preserve +check_legacy_policy package-override 'SigLevel = Optional PackageRequired TrustedOnly' preserve +check_legacy_policy inherited-secure '' preserve +check_legacy_policy package-optional 'SigLevel = PackageOptional DatabaseRequired PackageTrustedOnly' remove +check_legacy_policy package-trust-all 'SigLevel = PackageRequired PackageTrustAll' remove +check_legacy_policy inherited-unsafe '' remove Never +printf '%s\n' 'SigLevel = PackageRequired DatabaseOptional PackageTrustedOnly' >"$test_tmp/included-policy.conf" +check_legacy_policy included-secure "Include = $test_tmp/included-policy.conf" preserve +printf '%s\n' 'SigLevel = PackageOptional DatabaseRequired PackageTrustedOnly' >"$test_tmp/included-policy.conf" +check_legacy_policy included-unsafe "Include = $test_tmp/included-policy.conf" remove +pass "native policy resolution preserves secure database exceptions and disables inherited or included unsafe package policy" + +if TEST_LEGACY_POLICY='SigLevel = InvalidPolicy' \ + run_t2_scenario invalid-policy 'PackageRequired PackageTrustedOnly' omarchy; then + fail "unresolvable legacy signature policy reaches replacement" +fi +[[ ! -s $test_tmp/invalid-policy/transactions && ! -e $test_tmp/invalid-policy/marker ]] || + fail "unresolvable legacy policy publishes a package transaction or completion" +grep -q '^\[arch-mact2\]$' "$test_tmp/invalid-policy/pacman.conf" || + fail "unresolvable legacy policy rewrites administrator configuration" +pass "unresolvable legacy signature policy remains pending without mutation" + if run_t2_scenario insecure 'PackageOptional PackageTrustAll' omarchy; then fail "T2 migration accepts insecure effective package policy" fi From 9fb88358e73e2fce9d6a0a1b67d8ad5707e0f95f Mon Sep 17 00:00:00 2001 From: Afonso Oliveira Date: Mon, 7 Sep 2026 21:25:06 +0100 Subject: [PATCH 5/5] Serialize T2 provenance repair and pin package targets --- install/hardware/apple/fix-t2.sh | 13 +- migrations/1788163636.sh | 236 +++++++++++++-------- test/shell.d/t2-package-provenance-test.sh | 141 ++++++++++-- 3 files changed, 286 insertions(+), 104 deletions(-) diff --git a/install/hardware/apple/fix-t2.sh b/install/hardware/apple/fix-t2.sh index 94fb668891a3..a7a9d0b43015 100644 --- a/install/hardware/apple/fix-t2.sh +++ b/install/hardware/apple/fix-t2.sh @@ -1,6 +1,10 @@ # Detect T2 MacBook models using PCI IDs # Vendor: 106b (Apple), Device IDs: 1801 or 1802 (T2 Security Chip) -if lspci -nn | grep "106b:180[12]" >/dev/null; then +if ! t2_pci_devices=$(/usr/bin/lspci -nn); then + echo "Could not inspect PCI devices; refusing to skip T2 hardware setup." >&2 + return 1 +fi +if [[ $t2_pci_devices == *"106b:1801"* || $t2_pci_devices == *"106b:1802"* ]]; then echo "Detected MacBook with T2 chip. Installing support items..." t2_packages=( @@ -10,6 +14,7 @@ if lspci -nn | grep "106b:180[12]" >/dev/null; then apple-bcm-firmware t2fanrd ) + t2_targets=("${t2_packages[@]/#/omarchy/}") effective_siglevel=$(/usr/bin/pacman-conf --repo omarchy SigLevel 2>/dev/null) || { echo "Could not resolve the Omarchy repository signature policy; refusing T2 setup." >&2 @@ -37,7 +42,7 @@ if lspci -nn | grep "106b:180[12]" >/dev/null; then # any installation starts; otherwise leave the machine unchanged and report # the packaging prerequisite explicitly. for package in "${t2_packages[@]}"; do - if ! repository_metadata=$(LC_ALL=C /usr/bin/pacman -Si "$package" 2>/dev/null); then + if ! repository_metadata=$(LC_ALL=C /usr/bin/pacman -Si "omarchy/$package" 2>/dev/null); then repository="" else # Consume the complete producer output. Exiting awk after the first field @@ -57,7 +62,9 @@ if lspci -nn | grep "106b:180[12]" >/dev/null; then fi done - omarchy-pkg-add "${t2_packages[@]}" + # Pin the transaction as well as the preflight query. Reinstall same-version + # packages too: existing bytes may predate the authenticated repository. + /usr/bin/pacman -S --noconfirm "${t2_targets[@]}" || return 1 # Enable T2 fan control systemctl enable t2fanrd.service diff --git a/migrations/1788163636.sh b/migrations/1788163636.sh index bb3b1c562090..e47c1c5dc790 100644 --- a/migrations/1788163636.sh +++ b/migrations/1788163636.sh @@ -3,28 +3,45 @@ echo "Replace unauthenticated Apple T2 packages with signed Omarchy packages" pacman_conf=/etc/pacman.conf repair_marker=/var/lib/omarchy/t2-package-provenance-repaired t2_packages=(linux-t2 linux-t2-headers apple-t2-audio-config apple-bcm-firmware t2fanrd) +t2_targets=("${t2_packages[@]/#/omarchy/}") -disable_unsafe_t2_repository() ( - [[ -f $pacman_conf ]] || return 0 - local section backup stage="" signature_policy +# Avoid a password prompt on unaffected or already repaired machines. Repeat +# discovery inside the machine lock before acting on shared state. +needs_machine_repair() { + local installed_packages pci_devices package policy_status + if legacy_repository_is_unsafe; then + return 0 + else + policy_status=$? + (( policy_status == 1 )) || return 2 + fi + [[ ! -e $repair_marker ]] || return 1 - section=$(/usr/bin/awk ' - found && /^[[:space:]]*\[/ { exit } - /^[[:space:]]*\[arch-mact2\][[:space:]]*$/ { found = 1 } - found { print } - ' "$pacman_conf") - [[ -n $section ]] || return 0 + installed_packages=$(/usr/bin/pacman -Qq) || return 2 + for package in "${t2_packages[@]}"; do + if [[ $'\n'$installed_packages$'\n' == *$'\n'"$package"$'\n'* ]]; then + return 0 + fi + done + pci_devices=$(/usr/bin/lspci -nn) || return 2 + [[ $pci_devices == *"106b:1801"* || $pci_devices == *"106b:1802"* ]] +} + +legacy_repository_is_unsafe() { + local repositories signature_policy + repositories=$(/usr/bin/pacman-conf --config "$pacman_conf" --repo-list) || return 2 + [[ $'\n'$repositories$'\n' == *$'\narch-mact2\n'* ]] || return 1 # Resolve includes, inherited policy and directive precedence through pacman # itself. DatabaseOptional/DatabaseNever do not weaken package signatures. if ! signature_policy=$(/usr/bin/pacman-conf --config "$pacman_conf" --repo arch-mact2 SigLevel); then echo "Could not resolve the arch-mact2 signature policy; leaving the repair pending." >&2 - return 1 + return 2 fi if [[ -z ${signature_policy//[$' \t\n\r']/} ]]; then if ! signature_policy=$(/usr/bin/pacman-conf --config "$pacman_conf" SigLevel); then echo "Could not resolve the inherited signature policy; leaving the repair pending." >&2 - return 1 + return 2 fi fi signature_policy=${signature_policy//$'\n'/ } @@ -34,103 +51,152 @@ disable_unsafe_t2_repository() ( " $signature_policy " != *" PackageOptional "* && " $signature_policy " != *" PackageNever "* && " $signature_policy " != *" PackageTrustAll "* ]]; then - return 0 + return 1 + fi + return 0 +} + +disable_unsafe_t2_repository() ( + local section backup stage="" policy_status + if legacy_repository_is_unsafe; then + : + else + policy_status=$? + if (( policy_status == 1 )); then + return 0 + else + return 1 + fi + fi + + section=$(/usr/bin/awk ' + found && /^[[:space:]]*\[/ { exit } + /^[[:space:]]*\[arch-mact2\][[:space:]]*$/ { found = 1 } + found { print } + ' "$pacman_conf") + if [[ -z $section ]]; then + echo "Unsafe arch-mact2 policy is defined in an included file; disable that repository and retry this migration." >&2 + return 1 fi backup="$(/usr/bin/dirname "$pacman_conf")/arch-mact2.omarchy-disabled.$(/usr/bin/date +%s).txt" - sudo /usr/bin/install -T -o root -g root -m 0600 /dev/stdin "$backup" <<<"$section" - stage=$(sudo /usr/bin/mktemp \ + /usr/bin/install -T -o root -g root -m 0600 /dev/stdin "$backup" <<<"$section" + stage=$(/usr/bin/mktemp \ --tmpdir="$(/usr/bin/dirname "$pacman_conf")" \ '.pacman.conf.omarchy-t2.XXXXXXXX') - trap '[[ -z ${stage:-} ]] || sudo /usr/bin/rm -f -- "$stage"' EXIT + trap '[[ -z ${stage:-} ]] || /usr/bin/rm -f -- "$stage"' EXIT /usr/bin/awk ' /^[[:space:]]*\[arch-mact2\][[:space:]]*$/ { drop = 1; next } drop && /^[[:space:]]*\[/ { drop = 0 } !drop { print } ' "$pacman_conf" | - sudo /usr/bin/install -T -o root -g root -m 0644 /dev/stdin "$stage" - sudo /usr/bin/mv -Tf -- "$stage" "$pacman_conf" + /usr/bin/install -T -o root -g root -m 0644 /dev/stdin "$stage" + /usr/bin/mv -Tf -- "$stage" "$pacman_conf" stage="" trap - EXIT echo "Disabled the unsafe arch-mact2 section; preserved it at $backup" ) -# This is intentionally first. If package discovery, download, signature -# validation, or reinstall fails later, retrying is safe and the unsigned -# repository remains unavailable. -disable_unsafe_t2_repository +repair_machine() { + # This is intentionally first inside the machine lock. If package discovery, download, signature + # validation, or reinstall fails later, retrying is safe and the unsigned + # repository remains unavailable. + disable_unsafe_t2_repository -[[ ! -e $repair_marker ]] || exit 0 + [[ ! -e $repair_marker ]] || exit 0 -has_t2_hardware=0 -if /usr/bin/lspci -nn | /usr/bin/grep "106b:180[12]" >/dev/null; then - has_t2_hardware=1 -fi -has_t2_packages=0 -for package in "${t2_packages[@]}"; do - if /usr/bin/pacman -Q "$package" >/dev/null 2>&1; then - has_t2_packages=1 - break + if needs_machine_repair; then + : + else + discovery_status=$? + if (( discovery_status == 1 )); then + return 0 + else + echo "Could not inspect T2 hardware or packages; leaving the repair pending." >&2 + return 1 + fi fi -done -((has_t2_hardware || has_t2_packages)) || exit 0 - -# Do not assume an earlier update helper ran this migration. Ask pacman for the -# effective policy and require both mandatory package signatures and trusted -# signers before querying or reinstalling any replacement. -effective_siglevel=$(/usr/bin/pacman-conf --config "$pacman_conf" --repo omarchy SigLevel 2>/dev/null) || { - echo "Could not resolve the Omarchy repository signature policy; refusing T2 replacement." >&2 - exit 1 -} -if [[ -z ${effective_siglevel//[$' \t\n\r']/} ]]; then - effective_siglevel=$(/usr/bin/pacman-conf --config "$pacman_conf" SigLevel 2>/dev/null) || { - echo "Could not resolve the inherited package signature policy; refusing T2 replacement." >&2 + + # Do not assume an earlier update helper ran this migration. Ask pacman for the + # effective policy and require both mandatory package signatures and trusted + # signers before querying or reinstalling any replacement. + effective_siglevel=$(/usr/bin/pacman-conf --config "$pacman_conf" --repo omarchy SigLevel 2>/dev/null) || { + echo "Could not resolve the Omarchy repository signature policy; refusing T2 replacement." >&2 exit 1 } -fi -effective_siglevel=${effective_siglevel//$'\n'/ } -effective_siglevel=${effective_siglevel//$'\t'/ } -if [[ " $effective_siglevel " != *" PackageRequired "* || - " $effective_siglevel " != *" PackageTrustedOnly "* || - " $effective_siglevel " == *" PackageOptional "* || - " $effective_siglevel " == *" PackageNever "* || - " $effective_siglevel " == *" PackageTrustAll "* ]]; then - echo "The Omarchy repository does not enforce trusted package signatures; refusing T2 replacement." >&2 - exit 1 -fi - -# Resolve every replacement before beginning the reinstall, and require the -# pinned-key Omarchy repository by name. The repository itself inherits the -# effective package-required/trusted-only policy verified above. -for package in "${t2_packages[@]}"; do - if ! repository_metadata=$(LC_ALL=C /usr/bin/pacman -Si "$package" 2>/dev/null); then - repository="" - else - # Consume the complete producer output. Exiting awk after the first field - # turns a large successful pacman query into SIGPIPE under pipefail. - repository=$(/usr/bin/awk -F: ' - !found && /^Repository[[:space:]]*:/ { - gsub(/[[:space:]]/, "", $2) - print $2 - found = 1 - } - ' <<<"$repository_metadata") + if [[ -z ${effective_siglevel//[$' \t\n\r']/} ]]; then + effective_siglevel=$(/usr/bin/pacman-conf --config "$pacman_conf" SigLevel 2>/dev/null) || { + echo "Could not resolve the inherited package signature policy; refusing T2 replacement." >&2 + exit 1 + } + fi + effective_siglevel=${effective_siglevel//$'\n'/ } + effective_siglevel=${effective_siglevel//$'\t'/ } + if [[ " $effective_siglevel " != *" PackageRequired "* || + " $effective_siglevel " != *" PackageTrustedOnly "* || + " $effective_siglevel " == *" PackageOptional "* || + " $effective_siglevel " == *" PackageNever "* || + " $effective_siglevel " == *" PackageTrustAll "* ]]; then + echo "The Omarchy repository does not enforce trusted package signatures; refusing T2 replacement." >&2 + exit 1 fi - if [[ $repository != "omarchy" ]]; then - echo "Authenticated replacement '$package' is unavailable from the Omarchy repository." >&2 - echo "The unsafe repository is disabled. Publish all signed T2 artifacts, then retry this migration." >&2 + + # Resolve every replacement before beginning the reinstall, and require the + # pinned-key Omarchy repository by name. The repository itself inherits the + # effective package-required/trusted-only policy verified above. + for package in "${t2_packages[@]}"; do + if ! repository_metadata=$(LC_ALL=C /usr/bin/pacman -Si "omarchy/$package" 2>/dev/null); then + repository="" + else + # Consume the complete producer output. Exiting awk after the first field + # turns a large successful pacman query into SIGPIPE under pipefail. + repository=$(/usr/bin/awk -F: ' + !found && /^Repository[[:space:]]*:/ { + gsub(/[[:space:]]/, "", $2) + print $2 + found = 1 + } + ' <<<"$repository_metadata") + fi + if [[ $repository != "omarchy" ]]; then + echo "Authenticated replacement '$package' is unavailable from the Omarchy repository." >&2 + echo "The unsafe repository is disabled. Publish all signed T2 artifacts, then retry this migration." >&2 + exit 1 + fi + done + + # No --needed: the bytes already installed under SigLevel=Never have no trusted + # provenance even when their version equals the signed replacement. + if ! /usr/bin/pacman -S --noconfirm "${t2_targets[@]}"; then + echo "Authenticated T2 package reinstall failed; leaving the repair pending." >&2 exit 1 fi -done -# No --needed: the bytes already installed under SigLevel=Never have no trusted -# provenance even when their version equals the signed replacement. -if ! sudo /usr/bin/pacman -S --noconfirm "${t2_packages[@]}"; then - echo "Authenticated T2 package reinstall failed; leaving the repair pending." >&2 + marker_dir=$(/usr/bin/dirname "$repair_marker") + /usr/bin/install -d -o root -g root -m 0755 "$marker_dir" + printf 'Reinstalled from signed Omarchy repository at %s\n' "$(/usr/bin/date --iso-8601=seconds)" | + /usr/bin/install -T -o root -g root -m 0644 /dev/stdin "$repair_marker" +} + +if (( $# == 0 )); then + if needs_machine_repair; then + echo "Waiting to apply the Apple T2 system repair..." + umask 077 + # Elevate only the fixed packaged migration. /run is owned by root; keep + # the lock through the package transaction and completion publication. + /usr/bin/sudo -N -- /usr/bin/flock --exclusive --no-fork /run/omarchy-t2-package-provenance.lock \ + /usr/bin/bash -p -euo pipefail /usr/share/omarchy/migrations/1788163636.sh --machine + else + discovery_status=$? + if (( discovery_status != 1 )); then + echo "Could not inspect T2 hardware or packages; leaving the repair pending." >&2 + exit 1 + fi + fi +elif (( $# == 1 && EUID == 0 )) && [[ $1 == "--machine" ]]; then + export PATH=/usr/bin:/bin + repair_machine +else + echo "This migration accepts no arguments; its machine phase requires root." >&2 exit 1 fi - -marker_dir=$(/usr/bin/dirname "$repair_marker") -sudo /usr/bin/install -d -o root -g root -m 0755 "$marker_dir" -printf 'Reinstalled from signed Omarchy repository at %s\n' "$(/usr/bin/date --iso-8601=seconds)" | - sudo /usr/bin/install -T -o root -g root -m 0644 /dev/stdin "$repair_marker" diff --git a/test/shell.d/t2-package-provenance-test.sh b/test/shell.d/t2-package-provenance-test.sh index 93b09f70884a..8939a1e7de7c 100644 --- a/test/shell.d/t2-package-provenance-test.sh +++ b/test/shell.d/t2-package-provenance-test.sh @@ -17,27 +17,31 @@ stub_bin="$test_tmp/bin" mkdir "$stub_bin" cat >"$stub_bin/sudo" <<'STUB' #!/bin/bash -if [[ $1 == /usr/bin/install ]]; then - filtered=("$1") - shift - while (($#)); do - case $1 in - -o|-g) shift 2 ;; - *) filtered+=("$1"); shift ;; - esac - done - exec "${filtered[@]}" -fi +printf 'AUTH %s\n' "$*" >>"${TEST_TRANSACTION_LOG:?}" +[[ $1 != "-N" ]] || shift +[[ $1 != "--" ]] || shift exec "$@" STUB +cat >"$stub_bin/install" <<'STUB' +#!/bin/bash +filtered=() +while (($#)); do + case $1 in + -o|-g) shift 2 ;; + *) filtered+=("$1"); shift ;; + esac +done +exec /usr/bin/install "${filtered[@]}" +STUB cat >"$stub_bin/lspci" <<'STUB' #!/bin/bash +[[ ${TEST_PCI_STATUS:-0} == 0 ]] || exit "$TEST_PCI_STATUS" (( ${TEST_T2_HARDWARE:-0} == 1 )) || exit 0 printf '%s\n' '01:00.0 Bridge [0680]: Apple Inc. T2 Security Chip [106b:1801]' STUB cat >"$stub_bin/pacman-conf" <<'STUB' #!/bin/bash -if [[ ${TEST_NATIVE_SIGLEVEL:-0} == 1 || " $* " == *" --repo arch-mact2 "* ]]; then +if [[ ${TEST_NATIVE_SIGLEVEL:-0} == 1 || " $* " == *" --repo arch-mact2 "* || " $* " == *" --repo-list "* ]]; then exec /usr/bin/pacman-conf "$@" elif [[ " $* " == *" --repo omarchy "* ]]; then printf '%s\n' "${TEST_REPO_SIGLEVEL-${TEST_SIGLEVEL:-PackageRequired PackageTrustedOnly}}" @@ -49,17 +53,26 @@ cat >"$stub_bin/pacman" <<'STUB' #!/bin/bash case $1 in -Q) [[ $2 == linux-t2 ]] ;; + -Qq) + [[ ${TEST_PACKAGE_QUERY_STATUS:-0} == 0 ]] || exit "$TEST_PACKAGE_QUERY_STATUS" + printf '%s\n' "${TEST_INSTALLED_PACKAGES-linux-t2}" + ;; -Si) + package=${2#omarchy/} repository=${TEST_REPOSITORY:-omarchy} - [[ ${TEST_MISSING_PACKAGE:-} != "$2" ]] || repository=core + [[ ${TEST_MISSING_PACKAGE:-} != "$package" ]] || repository=core printf 'Repository : %s\n' "$repository" for ((line = 0; line < ${TEST_QUERY_PADDING_LINES:-0}; line++)); do printf 'Description : metadata padding %s\n' "$line" done - [[ ${TEST_QUERY_FAIL_PACKAGE:-} != "$2" ]] || exit 44 + [[ ${TEST_QUERY_FAIL_PACKAGE:-} != "$package" ]] || exit 44 ;; -S) printf 'TRANSACTION %s\n' "$*" >>"${TEST_TRANSACTION_LOG:?}" + if [[ -n ${TEST_TX_GATE:-} ]]; then + touch "$TEST_TX_GATE.entered" + while [[ ! -e $TEST_TX_GATE.release ]]; do sleep 0.02; done + fi exit "${TEST_TRANSACTION_STATUS:-0}" ;; *) exit 2 ;; @@ -70,6 +83,9 @@ chmod +x "$stub_bin"/* mapped_migration_template="$test_tmp/t2-migration.template.sh" sed -e "s#/usr/bin/pacman-conf#$stub_bin/pacman-conf#g" \ -e "s#/usr/bin/pacman#$stub_bin/pacman#g" \ + -e "s#/usr/bin/install#$stub_bin/install#g" \ + -e "s#/usr/bin/sudo#$stub_bin/sudo#g" \ + -e 's/EUID == 0/${TEST_MACHINE_EUID:-0} == 0/' \ -e "s#/usr/bin/lspci#$stub_bin/lspci#g" "$t2_migration" >"$mapped_migration_template" run_t2_scenario() { @@ -106,6 +122,8 @@ CONF sed \ -e "s|^pacman_conf=/etc/pacman.conf$|pacman_conf=$dir/pacman.conf|" \ -e "s|^repair_marker=/var/lib/omarchy/t2-package-provenance-repaired$|repair_marker=$dir/marker|" \ + -e "s|/run/omarchy-t2-package-provenance.lock|$dir/machine.lock|" \ + -e "s|/usr/share/omarchy/migrations/1788163636.sh|$dir/t2-migration.sh|" \ "$mapped_migration_template" >"$dir/t2-migration.sh" HOME="$dir" PATH="$stub_bin:$PATH" TEST_REPO_SIGLEVEL="$repo_policy" \ TEST_GLOBAL_SIGLEVEL="$global_policy" TEST_REPOSITORY="$repository" \ @@ -214,8 +232,10 @@ fi run_t2_scenario signed 'PackageRequired PackageTrustedOnly' omarchy transaction=$(<"$test_tmp/signed/transactions") -[[ $transaction == *'linux-t2 linux-t2-headers apple-t2-audio-config apple-bcm-firmware t2fanrd'* ]] || +[[ $transaction == *'omarchy/linux-t2 omarchy/linux-t2-headers omarchy/apple-t2-audio-config omarchy/apple-bcm-firmware omarchy/t2fanrd'* ]] || fail "T2 migration does not reinstall every replacement together" +(( $(grep -c '^AUTH ' "$test_tmp/signed/transactions") == 1 )) || + fail "T2 repair does not use a single authorized machine phase" [[ $transaction != *'--needed'* ]] || fail "T2 migration trusts bytes installed under SigLevel=Never" [[ -f $test_tmp/signed/marker ]] || fail "successful authenticated T2 replacement is not recorded" : >"$test_tmp/signed/transactions" @@ -223,7 +243,7 @@ HOME="$test_tmp/signed" PATH="$stub_bin:$PATH" \ TEST_REPO_SIGLEVEL='PackageRequired PackageTrustedOnly' \ TEST_GLOBAL_SIGLEVEL='PackageRequired PackageTrustedOnly' TEST_REPOSITORY=omarchy \ TEST_TRANSACTION_LOG="$test_tmp/signed/transactions" bash "$test_tmp/signed/t2-migration.sh" >/dev/null -[[ ! -s $test_tmp/signed/transactions ]] || fail "completed T2 repair reinstalls packages again" +[[ ! -s $test_tmp/signed/transactions ]] || fail "completed T2 repair prompts or reinstalls packages again" run_t2_scenario inherited '' omarchy '' 'PackageRequired PackageTrustedOnly' [[ -f $test_tmp/inherited/marker ]] || fail "T2 migration rejects a secure inherited global package policy" run_t2_scenario large 'PackageRequired PackageTrustedOnly' omarchy \ @@ -234,6 +254,74 @@ grep -q '^\[omarchy\]$' "$test_tmp/large/pacman.conf" || fail "T2 migration left its root-owned pacman.conf stage behind" pass "T2 migration disables unsafe policy first and fails closed until all signed replacements exist" +# The lock is the real util-linux implementation, but all filesystem effects +# remain in the fixture and every privileged command is mapped above. +gate="$test_tmp/two-users-gate" +TEST_TX_GATE="$gate" run_t2_scenario two-users 'PackageRequired PackageTrustedOnly' omarchy & +first_user=$! +for ((attempt = 0; attempt < 250; attempt++)); do + [[ ! -e $gate.entered ]] || break + sleep 0.02 +done +[[ -e $gate.entered ]] || fail "first user's machine transaction did not start" +TEST_TRANSACTION_LOG="$test_tmp/two-users/transactions" \ + bash -euo pipefail "$test_tmp/two-users/t2-migration.sh" >"$test_tmp/two-users/second-output" 2>&1 & +second_user=$! +for ((attempt = 0; attempt < 250; attempt++)); do + (( $(grep -c '^AUTH ' "$test_tmp/two-users/transactions") < 2 )) || break + sleep 0.02 +done +(( $(grep -c '^AUTH ' "$test_tmp/two-users/transactions") == 2 )) || + fail "second user did not reach the shared repair lock" +(( $(grep -c '^TRANSACTION ' "$test_tmp/two-users/transactions") == 1 )) || + fail "second user entered the transaction before the first finished" +printf '# administrator edit during replacement\n' >>"$test_tmp/two-users/pacman.conf" +touch "$gate.release" +wait "$first_user" +wait "$second_user" +(( $(grep -c '^TRANSACTION ' "$test_tmp/two-users/transactions") == 1 )) || + fail "two users ran overlapping T2 replacement transactions" +grep -q '^# administrator edit during replacement$' "$test_tmp/two-users/pacman.conf" || + fail "waiting migration overwrote the current administrator config" +pass "two users serialize the machine repair and recheck completion under the lock" + +# The native parser discovers repositories whose entire section is included. +# An unsafe custom layout needs administrator repair, never silent completion. +included="$test_tmp/included-repository.conf" +printf '[arch-mact2]\nSigLevel = Never\nServer = https://unsafe.invalid/\n' >"$included" +printf '[options]\nSigLevel = Required DatabaseOptional\nInclude = %s\n[omarchy]\nSigLevel = Required DatabaseOptional\nServer = https://pkgs.omarchy.org/\n' "$included" >"$test_tmp/signed/pacman.conf" +rm "$test_tmp/signed/marker" +if TEST_TRANSACTION_LOG="$test_tmp/signed/transactions" \ + bash -euo pipefail "$test_tmp/signed/t2-migration.sh" >"$test_tmp/included-repository.output" 2>&1; then + fail "unsafe repository in a custom include was silently accepted" +fi +grep -q 'defined in an included file' "$test_tmp/included-repository.output" || + fail "custom included repository lacks recovery guidance" +[[ ! -e $test_tmp/signed/marker ]] || fail "unresolved included repository published completion" +pass "custom included unsafe repository fails with repair guidance" + +printf '[options]\nSigLevel = Required DatabaseOptional\n' >"$test_tmp/signed/pacman.conf" +: >"$test_tmp/signed/transactions" +TEST_INSTALLED_PACKAGES='' TEST_TRANSACTION_LOG="$test_tmp/signed/transactions" \ + bash -euo pipefail "$test_tmp/signed/t2-migration.sh" >/dev/null +[[ ! -s $test_tmp/signed/transactions ]] || fail "unaffected machine requires authorization" +if TEST_PACKAGE_QUERY_STATUS=45 TEST_TRANSACTION_LOG="$test_tmp/signed/transactions" \ + bash -euo pipefail "$test_tmp/signed/t2-migration.sh" >/dev/null 2>&1; then + fail "package discovery failure was treated as an unaffected machine" +fi +if TEST_INSTALLED_PACKAGES='' TEST_PCI_STATUS=46 TEST_TRANSACTION_LOG="$test_tmp/signed/transactions" \ + bash -euo pipefail "$test_tmp/signed/t2-migration.sh" >/dev/null 2>&1; then + fail "PCI discovery failure was treated as an unaffected machine" +fi +[[ ! -s $test_tmp/signed/transactions ]] || fail "failed discovery reaches authorization" +pass "unaffected machines skip authorization and discovery failures remain pending" + +: >"$test_tmp/database-optional/transactions" +TEST_TRANSACTION_LOG="$test_tmp/database-optional/transactions" \ + bash -euo pipefail "$test_tmp/database-optional/t2-migration.sh" >/dev/null +[[ ! -s $test_tmp/database-optional/transactions ]] || fail "completed machine with safe custom repository prompts again" +pass "completed machines preserve safe custom policy without another prompt" + # Fresh installation cannot recreate the unsigned repository path. ! rg -n 'SigLevel[[:space:]]*=[[:space:]]*(Never|Optional)|TrustAll|arch-mact2-mirror' \ "$ROOT/install/hardware/pacman.sh" "$ROOT/install/post-install/pacman.sh" >/dev/null || @@ -243,6 +331,7 @@ grep -F '/usr/bin/pacman-conf --repo omarchy SigLevel' "$ROOT/install/hardware/a mapped_fresh_setup="$test_tmp/fix-t2.mapped.sh" sed -e "s#/usr/bin/pacman-conf#$stub_bin/pacman-conf#g" \ -e "s#/usr/bin/pacman#$stub_bin/pacman#g" \ + -e "s#/usr/bin/lspci#$stub_bin/lspci#g" \ "$ROOT/install/hardware/apple/fix-t2.sh" >"$mapped_fresh_setup" if TEST_T2_HARDWARE=1 TEST_REPO_SIGLEVEL='PackageRequired PackageTrustedOnly' \ TEST_GLOBAL_SIGLEVEL='PackageRequired PackageTrustedOnly' TEST_REPOSITORY=omarchy \ @@ -257,6 +346,26 @@ grep -q 'cannot continue until all support packages are published' "$test_tmp/fr fail "fresh T2 repository query omits release guidance" pass "fresh T2 setup independently enforces policy and reports failed large queries" +mkdir -p "$test_tmp/fresh-system/etc" +cat >"$stub_bin/systemctl" <<'STUB' +#!/bin/bash +exit 0 +STUB +chmod +x "$stub_bin/systemctl" +sed "s|/etc/|$test_tmp/fresh-system/etc/|g" "$mapped_fresh_setup" >"$test_tmp/fresh-setup.sh" +TEST_T2_HARDWARE=1 TEST_TRANSACTION_LOG="$test_tmp/fresh-transactions" PATH="$stub_bin:$PATH" \ + bash -euo pipefail -c 'source "$1"' bash "$test_tmp/fresh-setup.sh" >/dev/null +grep -Fq 'TRANSACTION -S --noconfirm omarchy/linux-t2 omarchy/linux-t2-headers omarchy/apple-t2-audio-config omarchy/apple-bcm-firmware omarchy/t2fanrd' "$test_tmp/fresh-transactions" || + fail "fresh setup transaction is not bound to the signed Omarchy repository" +pass "fresh setup pins every transaction target to the verified repository" +if TEST_PCI_STATUS=46 PATH="$stub_bin:$PATH" \ + bash -euo pipefail -c 'source "$1"' bash "$test_tmp/fresh-setup.sh" >"$test_tmp/fresh-pci-failure.output" 2>&1; then + fail "fresh T2 setup silently ignores PCI discovery failure" +fi +grep -q 'Could not inspect PCI devices' "$test_tmp/fresh-pci-failure.output" || + fail "fresh PCI discovery failure lacks guidance" +pass "fresh setup reports PCI discovery failure instead of skipping T2 support" + # A minimal unsigned local package under the final policy must be rejected by # real pacman. DatabaseOptional permits the unsigned database, never a package. if command -v repo-add >/dev/null && command -v bsdtar >/dev/null && command -v zstd >/dev/null &&