Explorar el Código

fix-deploy-harden-purge-ownership-validation

root hace 3 meses
padre
commit
600edde61a

+ 2 - 35
deploy/amd64-release/install.sh

@@ -203,6 +203,8 @@ for path in "$BINDIR/hthd" "$BINDIR/hthctl" "$BINDIR/hth-capture" \
             "$ETCDIR/hthd.yaml" "$ETCDIR/release-metadata.json" \
             "$ETCDIR/install-manifest.txt" \
             "$ETCDIR/profiles/6400-control/profile.json" \
+            "$RUNDIR" "$STATEDIR/captures" "$STATEDIR/replay" \
+            "$STATEDIR/receipts" \
             "$SHAREDIR/sdk/$(basename "$PACKAGE_WHEEL")"; do
   [[ -e "$path" || -L "$path" ]] && FOOTPRINT=1
 done
@@ -211,26 +213,9 @@ if [[ -d "$SHAREDIR/sdk" ]] &&
   FOOTPRINT=1
 fi
 
-IDENTITY_NAMES=(hth hth-observe hth-control)
-declare -A PREEXISTING_ACCOUNTS=()
-declare -A PREEXISTING_GROUPS=()
-declare -A OWNED_ACCOUNTS=()
-declare -A OWNED_GROUPS=()
 declare -A OLD_MANIFEST_ENTRIES=()
 declare -a OWNED_WHEELS=()
 WANTS_MANIFEST_KEY="symlink $UNITDIR/multi-user.target.wants/hthd.service"
-for identity_name in "${IDENTITY_NAMES[@]}"; do
-  if getent passwd "$identity_name" >/dev/null 2>&1; then
-    PREEXISTING_ACCOUNTS["$identity_name"]=1
-  else
-    PREEXISTING_ACCOUNTS["$identity_name"]=0
-  fi
-  if getent group "$identity_name" >/dev/null 2>&1; then
-    PREEXISTING_GROUPS["$identity_name"]=1
-  else
-    PREEXISTING_GROUPS["$identity_name"]=0
-  fi
-done
 if [[ -f "$OLD_MANIFEST" ]]; then
   safe_product_path config "$INSTALLED_META"
   safe_product_path config "$OLD_MANIFEST"
@@ -254,11 +239,9 @@ if [[ -f "$OLD_MANIFEST" ]]; then
         ;;
       created_account)
         identity_allowed "$old_kind" "$old_value"
-        OWNED_ACCOUNTS["$old_value"]=1
         ;;
       created_group)
         identity_allowed "$old_kind" "$old_value"
-        OWNED_GROUPS["$old_value"]=1
         ;;
       account|group)
         [[ "$old_value" == hth || "$old_value" == hth-observe || "$old_value" == hth-control ]] ||
@@ -397,14 +380,6 @@ fi
 
 getent group dialout >/dev/null 2>&1 || groupadd --system dialout
 systemd-sysusers "$LIBDIR/sysusers.d/hth.conf"
-for identity_name in "${IDENTITY_NAMES[@]}"; do
-  if [[ "${PREEXISTING_ACCOUNTS[$identity_name]}" -eq 0 ]] && getent passwd "$identity_name" >/dev/null 2>&1; then
-    OWNED_ACCOUNTS["$identity_name"]=1
-  fi
-  if [[ "${PREEXISTING_GROUPS[$identity_name]}" -eq 0 ]] && getent group "$identity_name" >/dev/null 2>&1; then
-    OWNED_GROUPS["$identity_name"]=1
-  fi
-done
 systemd-tmpfiles --create "$LIBDIR/tmpfiles.d/hth.conf"
 
 cat >"$ETCDIR/install-manifest.txt" <<EOF
@@ -431,14 +406,6 @@ owned_dir $ETCDIR/profiles
 owned_dir $ETCDIR
 owned_dir $STATEDIR
 EOF
-for identity_name in "${IDENTITY_NAMES[@]}"; do
-  if [[ "${OWNED_ACCOUNTS[$identity_name]:-0}" -eq 1 ]]; then
-    echo "created_account $identity_name" >>"$ETCDIR/install-manifest.txt"
-  fi
-  if [[ "${OWNED_GROUPS[$identity_name]:-0}" -eq 1 ]]; then
-    echo "created_group $identity_name" >>"$ETCDIR/install-manifest.txt"
-  fi
-done
 chmod 0644 "$ETCDIR/install-manifest.txt"
 
 chown root:hth "$ETCDIR" "$ETCDIR/hthd.yaml" "$ETCDIR/profiles" "$ETCDIR/profiles/6400-control" 2>/dev/null || true

+ 135 - 65
deploy/amd64-release/uninstall.sh

@@ -1,6 +1,7 @@
 #!/usr/bin/env bash
 set -euo pipefail
 
+ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
 BINDIR=/usr/local/bin
 SHAREDIR=/usr/local/share/hth
 ETCDIR=/etc/hth
@@ -32,6 +33,22 @@ if [[ "$YES" -eq 1 && "$PURGE" -ne 1 ]]; then
   exit 2
 fi
 
+fail() { echo "uninstall: $*" >&2; exit 1; }
+json_string() {
+  local key="$1" file="$2"
+  sed -n "s/^[[:space:]]*\"${key}\"[[:space:]]*:[[:space:]]*\"\([^\"]*\)\".*/\1/p" "$file" | head -n 1
+}
+verify_current_immutable_file() {
+  local installed="$1" packaged="$2" label="$3" installed_sha package_sha
+  [[ -f "$packaged" && ! -L "$packaged" ]] ||
+    fail "uninstall package is missing immutable file: $label"
+  [[ -f "$installed" && ! -L "$installed" ]] ||
+    fail "required immutable installed file is missing or not regular: $installed"
+  installed_sha="$(sha256sum "$installed" | awk '{print $1}')"
+  package_sha="$(sha256sum "$packaged" | awk '{print $1}')"
+  [[ "$installed_sha" == "$package_sha" ]] ||
+    fail "immutable installed file does not match uninstall package: $installed"
+}
 no_symlink_parents() {
   local path="$1" parent
   parent="${path%/*}"
@@ -97,12 +114,9 @@ remove_owned_path() {
   esac
 }
 
-identity_allowed() {
-  case "$1:$2" in
-    created_account:hth|created_account:hth-observe|created_account:hth-control|\
-    created_group:hth|created_group:hth-observe|created_group:hth-control) return 0 ;;
-    *) echo "uninstall: identity outside product allowlist: $1 $2" >&2; return 1 ;;
-  esac
+manifest_identity_rejected() {
+  echo "uninstall: current manifest cannot claim service identity ownership: $1 $2" >&2
+  return 1
 }
 
 declare -a FILES=()
@@ -110,8 +124,7 @@ declare -a CONFIGS=()
 declare -a TREES=()
 declare -a OWNED_DIRS=()
 declare -a PURGE_OWNED_DIRS=()
-declare -a CREATED_ACCOUNTS=()
-declare -a CREATED_GROUPS=()
+declare -A MANIFEST_ENTRIES=()
 
 if [[ ! -f "$MANIFEST" ]]; then
   footprint=0
@@ -121,7 +134,6 @@ if [[ ! -f "$MANIFEST" ]]; then
               "$LIBDIR/sysusers.d/hth.conf" "$LIBDIR/tmpfiles.d/hth.conf" \
               "$ETCDIR/hthd.yaml" "$ETCDIR/release-metadata.json" \
               "$ETCDIR/profiles/6400-control/profile.json" \
-              "$ETCDIR/profiles/prod2u-door-temp/profile.json" \
               "$RUNDIR" "$STATEDIR/captures" "$STATEDIR/replay" "$STATEDIR/receipts"; do
     [[ -e "$path" || -L "$path" ]] && footprint=1
   done
@@ -134,42 +146,119 @@ if [[ ! -f "$MANIFEST" ]]; then
   exit 0
 fi
 
-if [[ -f "$MANIFEST" ]]; then
-  while read -r kind path extra; do
-    [[ -z "$kind" || "$kind" == \#* ]] && continue
-    [[ -n "$path" && -z "${extra:-}" ]] || { echo "uninstall: malformed manifest line: $kind $path ${extra:-}" >&2; exit 1; }
-    if [[ "$kind:$path" == "config:$ETCDIR/profiles/prod2u-door-temp/profile.json" ||
-          "$kind:$path" == "owned_dir:$ETCDIR/profiles/prod2u-door-temp" ]]; then
-      # The formal v0.1.1 package never had a manifest, and current 6400 does
-      # not create this legacy path. Historical claims cannot prove ownership.
-      continue
-    fi
-    case "$kind" in
-      file|config|symlink|managed_tree|owned_dir) safe_path "$kind" "$path" || exit 1 ;;
-      created_account|created_group) identity_allowed "$kind" "$path" || exit 1 ;;
-      account|group)
-        # Legacy manifests did not record whether an identity predated HTH.
-        # Keep it rather than guessing ownership.
-        [[ "$path" == hth || "$path" == hth-observe || "$path" == hth-control ]] || exit 1
-        continue
-        ;;
-      *) echo "uninstall: unknown manifest kind: $kind" >&2; exit 1 ;;
-    esac
-    case "$kind" in
-      file|symlink) FILES+=("$path") ;;
-      config) CONFIGS+=("$path") ;;
-      managed_tree) TREES+=("$path") ;;
-      owned_dir)
-        case "$path" in
-          "$SHAREDIR"|"$SHAREDIR/sdk") OWNED_DIRS+=("$path") ;;
-          *) PURGE_OWNED_DIRS+=("$path") ;;
-        esac
-        ;;
-      created_account) CREATED_ACCOUNTS+=("$path") ;;
-      created_group) CREATED_GROUPS+=("$path") ;;
-    esac
-  done <"$MANIFEST"
-fi
+[[ -f "$ROOT/RELEASE-METADATA.json" ]] || fail "package is missing RELEASE-METADATA.json"
+[[ -f "$ETCDIR/release-metadata.json" ]] ||
+  fail "trusted install manifest requires installed release metadata"
+PACKAGE_VERSION="$(json_string version "$ROOT/RELEASE-METADATA.json")"
+PACKAGE_COMMIT="$(json_string source_commit "$ROOT/RELEASE-METADATA.json")"
+INSTALLED_VERSION="$(json_string version "$ETCDIR/release-metadata.json")"
+INSTALLED_COMMIT="$(json_string source_commit "$ETCDIR/release-metadata.json")"
+[[ "$PACKAGE_VERSION" =~ ^v[0-9][0-9A-Za-z.+-]*$ ]] ||
+  fail "package release metadata has an invalid version"
+[[ "$PACKAGE_COMMIT" =~ ^[0-9a-f]{40}([0-9a-f]{24})?$ ]] ||
+  fail "package release metadata has an invalid source_commit"
+[[ "$INSTALLED_VERSION" =~ ^v[0-9][0-9A-Za-z.+-]*$ ]] ||
+  fail "installed release metadata has an invalid version"
+[[ "$INSTALLED_COMMIT" =~ ^[0-9a-f]{40}([0-9a-f]{24})?$ ]] ||
+  fail "installed release metadata has an invalid source_commit"
+[[ "$INSTALLED_VERSION" == "$PACKAGE_VERSION" && "$INSTALLED_COMMIT" == "$PACKAGE_COMMIT" ]] ||
+  fail "installed release metadata does not match this uninstall package"
+EXPECTED_HEADER="# HTH owned paths; generated by ${PACKAGE_VERSION} (${PACKAGE_COMMIT})"
+[[ "$(head -n 1 "$MANIFEST")" == "$EXPECTED_HEADER" ]] ||
+  fail "install manifest header does not match installed release metadata"
+
+shopt -s nullglob
+PACKAGE_WHEELS=("$ROOT"/sdk/*.whl)
+shopt -u nullglob
+[[ "${#PACKAGE_WHEELS[@]}" -eq 1 ]] ||
+  fail "uninstall package must contain exactly one Python SDK wheel"
+EXPECTED_WHEEL="$SHAREDIR/sdk/$(basename "${PACKAGE_WHEELS[0]}")"
+verify_current_immutable_file "$ETCDIR/release-metadata.json" \
+  "$ROOT/RELEASE-METADATA.json" release-metadata
+verify_current_immutable_file "$BINDIR/hthd" "$ROOT/bin/hthd" hthd
+verify_current_immutable_file "$BINDIR/hthctl" "$ROOT/bin/hthctl" hthctl
+verify_current_immutable_file "$BINDIR/hth-capture" "$ROOT/bin/hth-capture" hth-capture
+verify_current_immutable_file "$UNITDIR/hthd.service" "$ROOT/service/hthd.service" hthd.service
+verify_current_immutable_file "$LIBDIR/sysusers.d/hth.conf" "$ROOT/service/hth.conf" hth.conf
+verify_current_immutable_file "$LIBDIR/tmpfiles.d/hth.conf" \
+  "$ROOT/service/hthd-tmpfiles.conf" hthd-tmpfiles.conf
+verify_current_immutable_file "$ETCDIR/profiles/6400-control/profile.json" \
+  "$ROOT/profiles/6400-control/profile.json" 6400-control-profile
+verify_current_immutable_file "$EXPECTED_WHEEL" "${PACKAGE_WHEELS[0]}" python-sdk-wheel
+MANIFEST_WHEEL_COUNT=0
+while read -r kind path extra; do
+  [[ -z "$kind" || "$kind" == \#* ]] && continue
+  [[ -n "$path" && -z "${extra:-}" ]] ||
+    fail "malformed manifest line: $kind $path ${extra:-}"
+  case "$kind" in
+    file|config|symlink|managed_tree|owned_dir) safe_path "$kind" "$path" || exit 1 ;;
+    created_account|created_group) manifest_identity_rejected "$kind" "$path" || exit 1 ;;
+    *) fail "unknown current manifest kind: $kind" ;;
+  esac
+  manifest_key="$kind $path"
+  [[ -z "${MANIFEST_ENTRIES[$manifest_key]:-}" ]] ||
+    fail "duplicate current manifest entry: $manifest_key"
+  MANIFEST_ENTRIES["$manifest_key"]=1
+  case "$kind:$path" in
+    file:$BINDIR/hthd|file:$BINDIR/hthctl|file:$BINDIR/hth-capture|\
+    file:$UNITDIR/hthd.service|file:$LIBDIR/sysusers.d/hth.conf|\
+    file:$LIBDIR/tmpfiles.d/hth.conf|file:$EXPECTED_WHEEL|\
+    config:$ETCDIR/hthd.yaml|config:$ETCDIR/release-metadata.json|\
+    config:$ETCDIR/install-manifest.txt|\
+    config:$ETCDIR/profiles/6400-control/profile.json|\
+    symlink:$UNITDIR/multi-user.target.wants/hthd.service|\
+    managed_tree:$RUNDIR|managed_tree:$STATEDIR/captures|\
+    managed_tree:$STATEDIR/replay|managed_tree:$STATEDIR/receipts|\
+    owned_dir:$SHAREDIR/sdk|owned_dir:$SHAREDIR|\
+    owned_dir:$ETCDIR/profiles/6400-control|owned_dir:$ETCDIR/profiles|\
+    owned_dir:$ETCDIR|owned_dir:$STATEDIR) ;;
+    *) fail "entry is not valid for a current HTH manifest: $manifest_key" ;;
+  esac
+  if [[ "$kind" == file && "$path" == "$SHAREDIR"/sdk/hth_sdk-*.whl ]]; then
+    ((MANIFEST_WHEEL_COUNT += 1))
+  fi
+  case "$kind" in
+    file|symlink) FILES+=("$path") ;;
+    config) CONFIGS+=("$path") ;;
+    managed_tree) TREES+=("$path") ;;
+    owned_dir)
+      case "$path" in
+        "$SHAREDIR"|"$SHAREDIR/sdk") OWNED_DIRS+=("$path") ;;
+        *) PURGE_OWNED_DIRS+=("$path") ;;
+      esac
+      ;;
+  esac
+done <"$MANIFEST"
+
+while read -r required_kind required_path; do
+  required_key="$required_kind $required_path"
+  [[ "${MANIFEST_ENTRIES[$required_key]:-0}" -eq 1 ]] ||
+    fail "current manifest is incomplete: $required_key"
+done <<EOF
+file $BINDIR/hthd
+file $BINDIR/hthctl
+file $BINDIR/hth-capture
+file $UNITDIR/hthd.service
+file $LIBDIR/sysusers.d/hth.conf
+file $LIBDIR/tmpfiles.d/hth.conf
+file $EXPECTED_WHEEL
+config $ETCDIR/hthd.yaml
+config $ETCDIR/release-metadata.json
+config $ETCDIR/install-manifest.txt
+config $ETCDIR/profiles/6400-control/profile.json
+managed_tree $RUNDIR
+managed_tree $STATEDIR/captures
+managed_tree $STATEDIR/replay
+managed_tree $STATEDIR/receipts
+owned_dir $SHAREDIR/sdk
+owned_dir $SHAREDIR
+owned_dir $ETCDIR/profiles/6400-control
+owned_dir $ETCDIR/profiles
+owned_dir $ETCDIR
+owned_dir $STATEDIR
+EOF
+[[ "$MANIFEST_WHEEL_COUNT" -eq 1 ]] ||
+  fail "current manifest must contain exactly one owned SDK wheel"
 
 systemctl disable --now "$SERVICE" >/dev/null 2>&1 || true
 systemctl disable --now 'hthd@*.service' >/dev/null 2>&1 || true
@@ -190,12 +279,6 @@ if [[ "$PURGE" -eq 1 ]]; then
   for path in "${CONFIGS[@]}"; do remove_owned_path config "$path"; done
   for path in "${TREES[@]}"; do remove_owned_path managed_tree "$path"; done
   for path in "${PURGE_OWNED_DIRS[@]}"; do remove_owned_path owned_dir "$path"; done
-  for account_name in "${CREATED_ACCOUNTS[@]}"; do
-    if getent passwd "$account_name" >/dev/null 2>&1; then userdel "$account_name"; fi
-  done
-  for group_name in "${CREATED_GROUPS[@]}"; do
-    if getent group "$group_name" >/dev/null 2>&1; then groupdel "$group_name"; fi
-  done
 fi
 for path in "${OWNED_DIRS[@]}"; do remove_owned_path owned_dir "$path"; done
 
@@ -212,23 +295,10 @@ for proc_dir in /proc/[0-9]*; do
 done
 if [[ "$PURGE" -eq 1 ]]; then
   for path in "$ETCDIR/hthd.yaml" "$ETCDIR/release-metadata.json" "$ETCDIR/install-manifest.txt" \
-              "$ETCDIR/profiles/6400-control/profile.json" \
-              "$ETCDIR/profiles/prod2u-door-temp/profile.json" "$RUNDIR" \
+              "$ETCDIR/profiles/6400-control/profile.json" "$RUNDIR" \
               "$STATEDIR/captures" "$STATEDIR/replay" "$STATEDIR/receipts"; do
     if [[ -e "$path" || -L "$path" ]]; then echo "purge residual: $path" >&2; failures=1; fi
   done
-  for account_name in "${CREATED_ACCOUNTS[@]}"; do
-    if getent passwd "$account_name" >/dev/null 2>&1; then
-      echo "purge residual: created account $account_name" >&2
-      failures=1
-    fi
-  done
-  for group_name in "${CREATED_GROUPS[@]}"; do
-    if getent group "$group_name" >/dev/null 2>&1; then
-      echo "purge residual: created group $group_name" >&2
-      failures=1
-    fi
-  done
 fi
 [[ "$failures" -eq 0 ]] || exit 1
 echo "HTH_UNINSTALL_PASS purge=$PURGE residual_conflicts=0"

+ 0 - 0
deploy/examples/podman-socket-only.sh


+ 0 - 0
deploy/install.sh


+ 0 - 0
deploy/uninstall.sh


+ 0 - 0
sdk/python/scripts/gen_stubs.sh


+ 0 - 0
tools/arm64_qemu_smoke.sh


+ 0 - 0
tools/nspawn_hthd_smoke.sh


+ 0 - 0
tools/package_hth_review.sh


+ 0 - 0
tools/qemu_guest_hthd.sh


+ 0 - 0
tools/qemu_systemd_vm.sh


+ 0 - 0
tools/record_cmd.sh


+ 156 - 10
tools/run_amd64_release_matrix.sh

@@ -124,22 +124,163 @@ run_guest SCENARIO_CURRENT_PROFILE_SENTINEL_REJECTION '
   rm -f /etc/hth/profiles/6400-control/profile.json
   rmdir /etc/hth/profiles/6400-control /etc/hth/profiles /etc/hth
 '
+run_guest SCENARIO_MANAGED_TREE_SENTINEL_REJECTION '
+  set -e
+  cd /opt/matrix/new/*
+  assert_managed_tree_rejected() {
+    managed_path="$1"
+    mkdir -p "$managed_path"
+    printf "unknown-managed-state\n" >"$managed_path/unknown-sentinel"
+    before_sentinel="$(sha256sum "$managed_path/unknown-sentinel" | awk "{print \$1}")"
+    if ./install.sh --no-start >/tmp/managed-tree-install.log 2>&1; then
+      echo "fresh install unexpectedly claimed an unknown managed tree: $managed_path" >&2
+      exit 1
+    fi
+    grep -Fq "an existing installation was found" /tmp/managed-tree-install.log
+    [ "$before_sentinel" = "$(sha256sum "$managed_path/unknown-sentinel" | awk "{print \$1}")" ]
+    rm -f "$managed_path/unknown-sentinel"
+    rmdir "$managed_path"
+  }
+  assert_managed_tree_rejected /run/hth
+  assert_managed_tree_rejected /var/lib/hth/captures
+  assert_managed_tree_rejected /var/lib/hth/replay
+  assert_managed_tree_rejected /var/lib/hth/receipts
+  rmdir /var/lib/hth 2>/dev/null || true
+'
 run_guest SCENARIO_UNKNOWN_LEGACY_PROFILE_PRESERVED '
   set -e
   cd /opt/matrix/new/*
   mkdir -p /etc/hth/profiles/prod2u-door-temp
-  printf "unknown-user-profile\n" >/etc/hth/profiles/prod2u-door-temp/sentinel.txt
-  before_sentinel="$(sha256sum /etc/hth/profiles/prod2u-door-temp/sentinel.txt | awk "{print \$1}")"
+  printf "unknown-user-profile\n" >/etc/hth/profiles/prod2u-door-temp/profile.json
+  before_profile="$(sha256sum /etc/hth/profiles/prod2u-door-temp/profile.json | awk "{print \$1}")"
   ./install.sh
   if grep -Fq "/etc/hth/profiles/prod2u-door-temp" /etc/hth/install-manifest.txt; then
     echo "fresh install incorrectly claimed an unknown legacy profile" >&2
     exit 1
   fi
   ./uninstall.sh --purge --yes
-  [ "$before_sentinel" = "$(sha256sum /etc/hth/profiles/prod2u-door-temp/sentinel.txt | awk "{print \$1}")" ]
-  rm -f /etc/hth/profiles/prod2u-door-temp/sentinel.txt
+  [ "$before_profile" = "$(sha256sum /etc/hth/profiles/prod2u-door-temp/profile.json | awk "{print \$1}")" ]
+  rm -f /etc/hth/profiles/prod2u-door-temp/profile.json
   rmdir /etc/hth/profiles/prod2u-door-temp /etc/hth/profiles /etc/hth
 '
+run_guest SCENARIO_FAKE_MANIFEST_UNINSTALL_REJECTION '
+  set -e
+  cd /opt/matrix/new/*
+  mkdir -p /etc/hth /usr/local/bin
+  if ! getent group hth >/dev/null 2>&1; then groupadd --system hth; fi
+  if ! getent passwd hth >/dev/null 2>&1; then
+    useradd --system --gid hth --home-dir /var/lib/hth --shell /usr/sbin/nologin --no-create-home hth
+  fi
+  cp RELEASE-METADATA.json /etc/hth/release-metadata.json
+  package_version="$(sed -n "s/^[[:space:]]*\"version\"[[:space:]]*:[[:space:]]*\"\([^\"]*\)\".*/\1/p" RELEASE-METADATA.json | head -n 1)"
+  package_commit="$(sed -n "s/^[[:space:]]*\"source_commit\"[[:space:]]*:[[:space:]]*\"\([^\"]*\)\".*/\1/p" RELEASE-METADATA.json | head -n 1)"
+  printf "unknown-binary\n" >/usr/local/bin/hthd
+  cat >/etc/hth/install-manifest.txt <<EOF
+# HTH owned paths; generated by ${package_version} (${package_commit})
+file /usr/local/bin/hthd
+created_account hth
+created_group hth
+EOF
+  before_binary="$(sha256sum /usr/local/bin/hthd | awk "{print \$1}")"
+  before_identity="$(id -u hth):$(getent group hth | cut -d: -f3)"
+  if ./uninstall.sh --purge --yes >/tmp/fake-manifest-uninstall.log 2>&1; then
+    echo "uninstall unexpectedly accepted a partial fake manifest" >&2
+    exit 1
+  fi
+  grep -Fq "immutable installed file does not match uninstall package" /tmp/fake-manifest-uninstall.log
+  [ "$before_binary" = "$(sha256sum /usr/local/bin/hthd | awk "{print \$1}")" ]
+  [ "$before_identity" = "$(id -u hth):$(getent group hth | cut -d: -f3)" ]
+  cp bin/hthd /usr/local/bin/hthd
+  mkdir -p /etc/hth/profiles/6400-control /usr/local/share/hth/sdk \
+    /usr/lib/sysusers.d /usr/lib/tmpfiles.d
+  package_wheel="$(find sdk -maxdepth 1 -type f -name "*.whl" -printf "%f\n")"
+  cp bin/hthctl /usr/local/bin/hthctl
+  cp bin/hth-capture /usr/local/bin/hth-capture
+  cp service/hthd.service /etc/systemd/system/hthd.service
+  cp service/hth.conf /usr/lib/sysusers.d/hth.conf
+  cp service/hthd-tmpfiles.conf /usr/lib/tmpfiles.d/hth.conf
+  cp profiles/6400-control/profile.json /etc/hth/profiles/6400-control/profile.json
+  cp "sdk/$package_wheel" "/usr/local/share/hth/sdk/$package_wheel"
+  if ./uninstall.sh --purge --yes >/tmp/identity-manifest-uninstall.log 2>&1; then
+    echo "uninstall unexpectedly accepted a manifest-owned service identity" >&2
+    exit 1
+  fi
+  grep -Fq "current manifest cannot claim service identity ownership" /tmp/identity-manifest-uninstall.log
+  [ "$before_identity" = "$(id -u hth):$(getent group hth | cut -d: -f3)" ]
+  sed -i "/^created_/d" /etc/hth/install-manifest.txt
+  if ./uninstall.sh --purge --yes >/tmp/partial-manifest-uninstall.log 2>&1; then
+    echo "uninstall unexpectedly accepted an incomplete current manifest" >&2
+    exit 1
+  fi
+  grep -Fq "current manifest is incomplete" /tmp/partial-manifest-uninstall.log
+  rm -f /usr/local/bin/hthd /usr/local/bin/hthctl /usr/local/bin/hth-capture \
+    /etc/systemd/system/hthd.service /usr/lib/sysusers.d/hth.conf /usr/lib/tmpfiles.d/hth.conf \
+    /etc/hth/profiles/6400-control/profile.json /etc/hth/install-manifest.txt \
+    /etc/hth/release-metadata.json "/usr/local/share/hth/sdk/$package_wheel"
+  rmdir /usr/local/share/hth/sdk /usr/local/share/hth \
+    /etc/hth/profiles/6400-control /etc/hth/profiles /etc/hth
+  userdel hth
+  groupdel hth
+'
+run_guest SCENARIO_FULL_MANIFEST_PARTIAL_CLOSURE_REJECTION '
+  set -e
+  cd /opt/matrix/new/*
+  package_version="$(sed -n "s/^[[:space:]]*\"version\"[[:space:]]*:[[:space:]]*\"\([^\"]*\)\".*/\1/p" RELEASE-METADATA.json | head -n 1)"
+  package_commit="$(sed -n "s/^[[:space:]]*\"source_commit\"[[:space:]]*:[[:space:]]*\"\([^\"]*\)\".*/\1/p" RELEASE-METADATA.json | head -n 1)"
+  package_wheel="$(find sdk -maxdepth 1 -type f -name "*.whl" -printf "%f\n")"
+  mkdir -p /etc/hth/profiles/6400-control /etc/systemd/system /usr/local/bin \
+    /usr/local/share/hth/sdk /usr/lib/sysusers.d /usr/lib/tmpfiles.d \
+    /var/lib/hth/captures
+  cp RELEASE-METADATA.json /etc/hth/release-metadata.json
+  cp bin/hthd /usr/local/bin/hthd
+  cp bin/hth-capture /usr/local/bin/hth-capture
+  cp service/hthd.service /etc/systemd/system/hthd.service
+  cp service/hth.conf /usr/lib/sysusers.d/hth.conf
+  cp service/hthd-tmpfiles.conf /usr/lib/tmpfiles.d/hth.conf
+  cp profiles/6400-control/profile.json /etc/hth/profiles/6400-control/profile.json
+  cp "sdk/$package_wheel" "/usr/local/share/hth/sdk/$package_wheel"
+  printf "unknown-managed-state\n" >/var/lib/hth/captures/unknown-sentinel
+  before_sentinel="$(sha256sum /var/lib/hth/captures/unknown-sentinel | awk "{print \$1}")"
+  cat >/etc/hth/install-manifest.txt <<EOF
+# HTH owned paths; generated by ${package_version} (${package_commit})
+file /usr/local/bin/hthd
+file /usr/local/bin/hthctl
+file /usr/local/bin/hth-capture
+file /etc/systemd/system/hthd.service
+file /usr/lib/sysusers.d/hth.conf
+file /usr/lib/tmpfiles.d/hth.conf
+file /usr/local/share/hth/sdk/${package_wheel}
+config /etc/hth/hthd.yaml
+config /etc/hth/release-metadata.json
+config /etc/hth/install-manifest.txt
+config /etc/hth/profiles/6400-control/profile.json
+managed_tree /run/hth
+managed_tree /var/lib/hth/captures
+managed_tree /var/lib/hth/replay
+managed_tree /var/lib/hth/receipts
+owned_dir /usr/local/share/hth/sdk
+owned_dir /usr/local/share/hth
+owned_dir /etc/hth/profiles/6400-control
+owned_dir /etc/hth/profiles
+owned_dir /etc/hth
+owned_dir /var/lib/hth
+EOF
+  if ./uninstall.sh --purge --yes >/tmp/partial-installed-closure.log 2>&1; then
+    echo "uninstall unexpectedly accepted a full manifest with a partial installed closure" >&2
+    exit 1
+  fi
+  grep -Fq "required immutable installed file is missing or not regular: /usr/local/bin/hthctl" \
+    /tmp/partial-installed-closure.log
+  [ "$before_sentinel" = "$(sha256sum /var/lib/hth/captures/unknown-sentinel | awk "{print \$1}")" ]
+  rm -f /usr/local/bin/hthd /usr/local/bin/hth-capture \
+    /etc/systemd/system/hthd.service /usr/lib/sysusers.d/hth.conf /usr/lib/tmpfiles.d/hth.conf \
+    /etc/hth/profiles/6400-control/profile.json /etc/hth/release-metadata.json \
+    /etc/hth/install-manifest.txt "/usr/local/share/hth/sdk/$package_wheel" \
+    /var/lib/hth/captures/unknown-sentinel
+  rmdir /usr/local/share/hth/sdk /usr/local/share/hth \
+    /etc/hth/profiles/6400-control /etc/hth/profiles /etc/hth \
+    /var/lib/hth/captures /var/lib/hth
+'
 run_guest SCENARIO_FRESH '
   set -e
   cd /opt/matrix/new/*
@@ -164,10 +305,12 @@ run_guest SCENARIO_PREEXISTING_IDENTITIES '
   cd /opt/matrix/new/*
   ./uninstall.sh --purge --yes
   for identity_name in hth hth-observe hth-control; do
-    groupadd --system "$identity_name"
-    if [ "$identity_name" = hth ]; then identity_home=/var/lib/hth; else identity_home=/nonexistent; fi
-    useradd --system --gid "$identity_name" --home-dir "$identity_home" \
-      --shell /usr/sbin/nologin --no-create-home "$identity_name"
+    if ! getent group "$identity_name" >/dev/null 2>&1; then groupadd --system "$identity_name"; fi
+    if ! getent passwd "$identity_name" >/dev/null 2>&1; then
+      if [ "$identity_name" = hth ]; then identity_home=/var/lib/hth; else identity_home=/nonexistent; fi
+      useradd --system --gid "$identity_name" --home-dir "$identity_home" \
+        --shell /usr/sbin/nologin --no-create-home "$identity_name"
+    fi
   done
   identity_snapshot() {
     for identity_name in hth hth-observe hth-control; do
@@ -177,8 +320,8 @@ run_guest SCENARIO_PREEXISTING_IDENTITIES '
   }
   identities_before="$(identity_snapshot)"
   ./install.sh
-  if grep -Eq "^created_(account|group) (hth|hth-observe|hth-control)$" /etc/hth/install-manifest.txt; then
-    echo "pre-existing identity was incorrectly claimed by HTH" >&2
+  if grep -Eq "^(created_account|created_group|account|group) " /etc/hth/install-manifest.txt; then
+    echo "service identity was incorrectly claimed by HTH" >&2
     exit 1
   fi
   ./uninstall.sh --purge --yes
@@ -237,7 +380,10 @@ run_guest SCENARIO_UPGRADE_FROM_FAILED_OLD '
   echo "previous_archive_sha256=$OLD_SHA"
   echo "symlink_path_rejection=PASS"
   echo "fake_manifest_rejection=PASS"
+  echo "fake_manifest_uninstall_rejection=PASS"
+  echo "full_manifest_partial_closure_rejection=PASS"
   echo "current_profile_sentinel_rejection=PASS"
+  echo "managed_tree_sentinel_rejection=PASS"
   echo "unknown_legacy_profile_preserved=PASS"
   echo "fresh_install=PASS"
   echo "same_version_reinstall=PASS"

+ 0 - 0
tools/run_buf_breaking.sh


+ 0 - 0
tools/run_fuzz_15m.sh


+ 0 - 0
tools/sdk_wheel_matrix.sh


+ 77 - 20
tools/test_amd64_release_script_safety.py

@@ -38,6 +38,13 @@ def test_release_shell_entrypoints_parse() -> None:
         subprocess.run(["bash", "-n", str(script)], check=True)
 
 
+def test_all_tracked_shell_entrypoints_are_executable() -> None:
+    scripts = sorted(ROOT.rglob("*.sh"))
+    assert scripts
+    for script in scripts:
+        assert script.stat().st_mode & 0o111 == 0o111, f"not executable: {script}"
+
+
 def test_uninstall_allowlist_covers_every_owned_target_type() -> None:
     source = _text(UNINSTALL)
     expected = {
@@ -99,7 +106,9 @@ def test_symlink_parent_guard_rejects_adversarial_parent(tmp_path: Path) -> None
 
 
 def test_product_removals_use_the_guarded_dispatchers() -> None:
-    _assert_removals_are_inside_helper(UNINSTALL, "remove_owned_path", "identity_allowed")
+    _assert_removals_are_inside_helper(
+        UNINSTALL, "remove_owned_path", "manifest_identity_rejected"
+    )
     _assert_removals_are_inside_helper(
         INSTALL,
         "remove_product_path",
@@ -109,35 +118,27 @@ def test_product_removals_use_the_guarded_dispatchers() -> None:
     assert 'rm -f -- "$SHAREDIR/sdk"/*.whl' not in _text(INSTALL)
 
 
-def test_manifest_records_only_created_identity_ownership() -> None:
+def test_current_manifest_never_claims_service_identity_ownership() -> None:
     install = _text(INSTALL)
     marker = 'cat >"$ETCDIR/install-manifest.txt" <<EOF'
     manifest = install.split(marker, 1)[1].split("\nEOF", 1)[0]
     assert "\naccount " not in manifest
     assert "\ngroup " not in manifest
-    assert 'echo "created_account $identity_name"' in install
-    assert 'echo "created_group $identity_name"' in install
-    assert 'OWNED_ACCOUNTS["$old_value"]=1' in install
-    assert 'OWNED_GROUPS["$old_value"]=1' in install
+    assert "\ncreated_account " not in manifest
+    assert "\ncreated_group " not in manifest
+    assert 'echo "created_account $identity_name"' not in install
+    assert 'echo "created_group $identity_name"' not in install
     assert "/etc/hth/profiles/prod2u-door-temp" not in manifest
     assert "multi-user.target.wants/hthd.service" not in manifest
     assert 'if [[ -L "$UNITDIR/multi-user.target.wants/hthd.service" ]]' in install
 
 
-def test_purge_deletes_only_manifest_claimed_identities() -> None:
+def test_purge_never_deletes_service_identities() -> None:
     uninstall = _text(UNINSTALL)
-    assert 'created_account) CREATED_ACCOUNTS+=("$path")' in uninstall
-    assert 'created_group) CREATED_GROUPS+=("$path")' in uninstall
-    assert 'for account_name in "${CREATED_ACCOUNTS[@]}"' in uninstall
-    assert 'for group_name in "${CREATED_GROUPS[@]}"' in uninstall
-    assert "for account_name in hth hth-control hth-observe" not in uninstall
-    assert "for group_name in hth-control hth-observe hth" not in uninstall
-    assert "Historical claims cannot prove ownership" in uninstall
-    for line in uninstall.splitlines():
-        if "userdel " in line:
-            assert 'userdel "$account_name"' in line
-        if "groupdel " in line:
-            assert 'groupdel "$group_name"' in line
+    assert "userdel " not in uninstall
+    assert "groupdel " not in uninstall
+    assert "current manifest cannot claim service identity ownership" in uninstall
+    assert "created_account|created_group) manifest_identity_rejected" in uninstall
 
 
 def test_manifestless_paths_fail_closed_and_legacy_upgrade_is_fingerprinted() -> None:
@@ -175,6 +176,55 @@ def test_manifestless_paths_fail_closed_and_legacy_upgrade_is_fingerprinted() ->
     assert "HTH-like files exist without a trusted install manifest" in uninstall
     assert "declare -a FILES=()" in uninstall
     assert "declare -a CONFIGS=()" in uninstall
+    footprint = install.split("FOOTPRINT=0", 1)[1].split(
+        "declare -A OLD_MANIFEST_ENTRIES", 1
+    )[0]
+    for managed_tree in (
+        '"$RUNDIR"',
+        '"$STATEDIR/captures"',
+        '"$STATEDIR/replay"',
+        '"$STATEDIR/receipts"',
+    ):
+        assert managed_tree in footprint
+
+
+def test_uninstall_requires_strict_current_manifest_before_mutation() -> None:
+    uninstall = _text(UNINSTALL)
+    validation_end = uninstall.index('systemctl disable --now "$SERVICE"')
+    validator = uninstall[:validation_end]
+    assert '[[ -f "$ROOT/RELEASE-METADATA.json" ]]' in validator
+    assert 'trusted install manifest requires installed release metadata' in validator
+    assert 'installed release metadata does not match this uninstall package' in validator
+    assert 'install manifest header does not match installed release metadata' in validator
+    assert 'immutable installed file does not match uninstall package' in validator
+    assert 'required immutable installed file is missing or not regular' in validator
+    for artifact in (
+        '"$BINDIR/hthd" "$ROOT/bin/hthd"',
+        '"$BINDIR/hthctl" "$ROOT/bin/hthctl"',
+        '"$BINDIR/hth-capture" "$ROOT/bin/hth-capture"',
+        '"$UNITDIR/hthd.service" "$ROOT/service/hthd.service"',
+        '"$LIBDIR/sysusers.d/hth.conf" "$ROOT/service/hth.conf"',
+        '"$LIBDIR/tmpfiles.d/hth.conf"',
+        '"$ETCDIR/profiles/6400-control/profile.json"',
+        '"$EXPECTED_WHEEL" "${PACKAGE_WHEELS[0]}"',
+        '"$ETCDIR/release-metadata.json"',
+    ):
+        assert artifact in validator
+    assert 'duplicate current manifest entry' in validator
+    assert 'current manifest is incomplete' in validator
+    assert 'current manifest must contain exactly one owned SDK wheel' in validator
+    assert 'entry is not valid for a current HTH manifest' in validator
+    assert "userdel " not in validator
+    assert "groupdel " not in validator
+    assert not any(
+        line.lstrip().startswith("remove_owned_path ") for line in validator.splitlines()
+    )
+    purge_residual = uninstall.split('if [[ "$PURGE" -eq 1 ]]; then', 2)[2]
+    assert '"$ETCDIR/profiles/prod2u-door-temp/profile.json"' not in purge_residual
+    manifestless_footprint = uninstall.split('if [[ ! -f "$MANIFEST" ]]', 1)[1].split(
+        '[[ -f "$ROOT/RELEASE-METADATA.json" ]]', 1
+    )[0]
+    assert '"$ETCDIR/profiles/prod2u-door-temp/profile.json"' not in manifestless_footprint
 
 
 def test_matrix_preserves_preexisting_identities() -> None:
@@ -183,7 +233,7 @@ def test_matrix_preserves_preexisting_identities() -> None:
     assert 'identities_before="$(identity_snapshot)"' in matrix
     assert 'identities_after="$(identity_snapshot)"' in matrix
     assert '[ "$identities_before" = "$identities_after" ]' in matrix
-    assert "pre-existing identity was incorrectly claimed by HTH" in matrix
+    assert "service identity was incorrectly claimed by HTH" in matrix
     assert "preexisting_identity_preservation=PASS" in matrix
 
 
@@ -193,9 +243,16 @@ def test_matrix_contains_destructive_path_adversarial_scenarios() -> None:
     assert "SCENARIO_FAKE_MANIFEST_REJECTION" in matrix
     assert "SCENARIO_UNKNOWN_LEGACY_PROFILE_PRESERVED" in matrix
     assert "SCENARIO_CURRENT_PROFILE_SENTINEL_REJECTION" in matrix
+    assert "SCENARIO_MANAGED_TREE_SENTINEL_REJECTION" in matrix
+    assert "SCENARIO_FAKE_MANIFEST_UNINSTALL_REJECTION" in matrix
+    assert "SCENARIO_FULL_MANIFEST_PARTIAL_CLOSURE_REJECTION" in matrix
     assert "install unexpectedly accepted symlink path" in matrix
     assert "upgrade unexpectedly accepted a fake manifest" in matrix
     assert "symlink_path_rejection=PASS" in matrix
     assert "fake_manifest_rejection=PASS" in matrix
     assert "unknown_legacy_profile_preserved=PASS" in matrix
     assert "current_profile_sentinel_rejection=PASS" in matrix
+    assert "managed_tree_sentinel_rejection=PASS" in matrix
+    assert "fake_manifest_uninstall_rejection=PASS" in matrix
+    assert "full_manifest_partial_closure_rejection=PASS" in matrix
+    assert "/etc/hth/profiles/prod2u-door-temp/profile.json" in matrix