Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions bin/omarchy-system-factory-reset
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,58 @@ scrub_factory_accounts() {
rm -f "$root/etc/"{shadow-,gshadow-,passwd-,group-,subuid-,subgid-}
}

disable_factory_usbguard() {
local root="$1"

[[ -f $root/usr/lib/systemd/system/usbguard.service || -f $root/etc/systemd/system/usbguard.service ]] || return 0
systemctl --root="$root" disable usbguard.service >>"$LOG_FILE" 2>&1
}

write_bolt_auth_mode() {
local path="$1" temporary

install -d -m 700 "${path%/*}" || return 1
temporary=$(mktemp "${path%/*}/.config.XXXXXX") || return 1
if awk '
/^[[:space:]]*\[/ {
section = ($0 ~ /^[[:space:]]*\[config\][[:space:]]*$/)
print
if (section) { found = 1; print "AuthMode = enabled" }
next
}
section && /^[[:space:]]*AuthMode[[:space:]]*=/ { next }
{ print }
END { if (!found) print "\n[config]\nAuthMode = enabled" }
' "$(if [[ -e $path ]]; then printf '%s' "$path"; else printf '%s' /dev/null; fi)" >"$temporary" &&
sync "$temporary" && mv -fT -- "$temporary" "$path" && sync "${path%/*}"; then
return 0
fi
rm -f -- "$temporary"
return 1
}

reset_factory_thunderbolt() {
local root="$1" directory

root=$(realpath -e -- "$1") || return 1
[[ $root != "/" && -d $root ]] || return 1
[[ -d $root/var/lib/omarchy/thunderbolt-authorization || -f $root/etc/omarchy/thunderbolt-authorization.enabled || -f $root/etc/omarchy/thunderbolt-authorization.pending ]] || return 0
for directory in etc etc/omarchy var var/lib var/lib/omarchy var/lib/boltd; do
[[ ! -L $root/$directory ]] || return 1
done
if [[ -f $root/usr/lib/systemd/system/omarchy-thunderbolt-authorization.service || -f $root/etc/systemd/system/omarchy-thunderbolt-authorization.service ]]; then
systemctl --root="$root" disable omarchy-thunderbolt-authorization.service >>"$LOG_FILE" 2>&1 || return 1
fi
rm -f -- "$root/etc/systemd/system/multi-user.target.wants/omarchy-thunderbolt-authorization.service" \
"$root/etc/omarchy/thunderbolt-authorization.enabled" \
"$root/etc/omarchy/thunderbolt-authorization.pending" || return 1
rm -rf -- "$root/var/lib/omarchy/thunderbolt-authorization" || return 1
for directory in devices keys domains; do
rm -rf -- "$root/var/lib/boltd/$directory" || return 1
done
write_bolt_auth_mode "$root/var/lib/boltd/boltd.conf"
}

# Remove the seller's account material and machine identity from the retained
# @factory baseline so it can neither be mounted for recovery nor restore the
# seller's account on a future reset. Idempotent (a scrubbed baseline has no
Expand All @@ -463,6 +515,14 @@ sanitize_factory_baseline() {
local factory="$1"
btrfs property set -ts "$factory" ro false

if ! disable_factory_usbguard "$factory"; then
btrfs property set -ts "$factory" ro true
fail "could not disable USBGuard in the factory baseline (see $LOG_FILE)"
fi
if ! reset_factory_thunderbolt "$factory"; then
btrfs property set -ts "$factory" ro true
fail "could not reset Thunderbolt authorization in the factory baseline"
fi
if ! scrub_factory_accounts "$factory"; then
btrfs property set -ts "$factory" ro true
fail "could not remove account credentials from the factory baseline (see $LOG_FILE)"
Expand All @@ -487,6 +547,9 @@ stage_full_reset() {
[[ -f $unit_src/omarchy-provision-owner.service && -x $next/usr/bin/omarchy-provision-owner ]] ||
fail "the factory snapshot predates provisioning support; cannot reset from it"

disable_factory_usbguard "$next" ||
fail "could not disable USBGuard in the factory system (see $LOG_FILE)"

log "Scrubbing machine identity from the factory system"
systemd-id128 new >"$next/etc/machine-id"
rm -f "$next"/etc/ssh/ssh_host_*
Expand All @@ -502,6 +565,9 @@ stage_full_reset() {
scrub_factory_accounts "$next" ||
fail "could not remove account credentials from the factory system (see $LOG_FILE)"

reset_factory_thunderbolt "$next" ||
fail "could not reset Thunderbolt authorization in the staged root"

# @factory itself survives the wipe as the baseline for future resets. If it
# came from a normal install it still holds the seller's account and
# /etc/shadow, which the new wheel user could mount and read — and a second
Expand Down
179 changes: 179 additions & 0 deletions test/shell.d/factory-reset-accessory-trust-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

if (( EUID != 0 )); then
if unshare --user --map-root-user true 2>/dev/null; then
exec unshare --user --map-root-user bash "$0"
fi
skip "no unprivileged user namespace; skipping factory accessory trust cleanup"
exit 0
fi

require_command systemctl

test_tmp=$(mktemp -d)
trap 'rm -rf "$test_tmp"' EXIT

awk '
/^[a-z_]+\(\) \{/ { copying = 1 }
copying { print }
/^}/ { copying = 0 }
' "$ROOT/bin/omarchy-system-factory-reset" >"$test_tmp/functions"

cat >"$test_tmp/reset" <<'SH'
#!/bin/bash
set -euo pipefail
source "$1/functions"
TOP_MNT="$2"
NEXT_NAME=@omarchy-reset-next
PROVISIONING_DIR=/var/lib/omarchy/provisioning
LOG_FILE="$TOP_MNT/reset.log"

log() { printf '%s\n' "$1" >>"$LOG_FILE"; }
fail() { log "$1"; exit 1; }

btrfs() {
if [[ $1 == "subvolume" && $2 == "snapshot" ]]; then
mkdir -p "$4"
cp -a "$3/." "$4/"
elif [[ $1 == "property" ]]; then
printf '%s\n' "$6" >"$4/read-only"
else
return 1
fi
}
systemd-id128() { printf '%032d\n' 1; }
install_provisioning_units() { :; }
encrypted_install() { return 1; }
rebuild_next_boot() { touch "$TOP_MNT/rebuilt"; }

stage_full_reset
SH

make_accounts() {
local factory=$1
mkdir -p "$factory/etc" "$factory/home/seller" \
"$factory/usr/bin" "$factory/usr/share/omarchy/install/provisioning" \
"$factory/var/lib/omarchy/provisioning/packages"
touch "$factory/home/seller/private-file" \
"$factory/usr/share/omarchy/install/provisioning/omarchy-provision-owner.service" \
"$factory/var/lib/omarchy/provisioning/packages/node-v0.tar.gz"
printf '#!/bin/bash\n' >"$factory/usr/bin/omarchy-provision-owner"
chmod +x "$factory/usr/bin/omarchy-provision-owner"
printf 'true\n' >"$factory/read-only"
cat >"$factory/etc/passwd" <<'EOF'
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/:/usr/bin/nologin
seller:x:1000:1000:Seller:/home/seller:/bin/bash
EOF
printf 'root:original-root-hash:20000:0:99999:7:::\ndaemon:*:20000:0:99999:7:::\nseller:original-user-hash:20000:0:99999:7:::\n' >"$factory/etc/shadow"
printf 'root:x:0:\ndaemon:x:1:\nseller:x:1000:\nwheel:x:998:seller\n' >"$factory/etc/group"
printf 'root:!::\ndaemon:!::\nseller:!::\nwheel:!::seller\n' >"$factory/etc/gshadow"
printf 'USERGROUPS_ENAB yes\n' >"$factory/etc/login.defs"
printf 'seller:100000:65536\n' >"$factory/etc/subuid"
printf 'seller:100000:65536\n' >"$factory/etc/subgid"
chmod 600 "$factory/etc/"{shadow,gshadow}
local file
for file in passwd shadow group gshadow subuid subgid; do
cp "$factory/etc/$file" "$factory/etc/$file-"
done
}

plant_trust() {
local factory=$1
mkdir -p "$factory/etc/usbguard/IPCAccessControl.d" \
"$factory/etc/systemd/system/multi-user.target.wants" \
"$factory/usr/lib/systemd/system" \
"$factory/etc/omarchy" \
"$factory/var/lib/omarchy/thunderbolt-authorization" \
"$factory/var/lib/boltd/devices" "$factory/var/lib/boltd/keys" "$factory/var/lib/boltd/domains"
printf '[Unit]\nDescription=USBGuard\n[Service]\nExecStart=/usr/bin/usbguard-daemon\n[Install]\nWantedBy=multi-user.target\n' \
>"$factory/usr/lib/systemd/system/usbguard.service"
ln -s /usr/lib/systemd/system/usbguard.service \
"$factory/etc/systemd/system/multi-user.target.wants/usbguard.service"
printf 'allow id 1d6b:0002\n' >"$factory/etc/usbguard/rules.conf"
printf '[Unit]\nDescription=Thunderbolt authorization\n[Service]\nExecStart=/bin/true\n[Install]\nWantedBy=multi-user.target\n' \
>"$factory/usr/lib/systemd/system/omarchy-thunderbolt-authorization.service"
ln -s /usr/lib/systemd/system/omarchy-thunderbolt-authorization.service \
"$factory/etc/systemd/system/multi-user.target.wants/omarchy-thunderbolt-authorization.service"
printf 'enabled\n' >"$factory/etc/omarchy/thunderbolt-authorization.enabled"
printf 'pending\n' >"$factory/etc/omarchy/thunderbolt-authorization.pending"
printf '{"trusted":{"seller-dock":true}}\n' >"$factory/var/lib/omarchy/thunderbolt-authorization/policy.json"
printf 'enrolled\n' >"$factory/var/lib/boltd/devices/seller-dock"
printf 'key\n' >"$factory/var/lib/boltd/keys/seller-dock"
printf 'domain\n' >"$factory/var/lib/boltd/domains/controller"
printf '[config]\nAuthMode = disabled\nSomeOther = keep\n' >"$factory/var/lib/boltd/boltd.conf"
}

assert_trust_cleared() {
local root=$1 label=$2
[[ ! -L $root/etc/systemd/system/multi-user.target.wants/usbguard.service && ! -e $root/etc/systemd/system/multi-user.target.wants/usbguard.service ]] ||
fail "$label still enables USBGuard"
[[ $(cat "$root/etc/usbguard/rules.conf") == "allow id 1d6b:0002" ]] ||
fail "$label drops the USBGuard rules file"
[[ ! -L $root/etc/systemd/system/multi-user.target.wants/omarchy-thunderbolt-authorization.service && ! -e $root/etc/systemd/system/multi-user.target.wants/omarchy-thunderbolt-authorization.service ]] ||
fail "$label still enables Thunderbolt authorization"
[[ ! -e $root/etc/omarchy/thunderbolt-authorization.enabled && ! -e $root/etc/omarchy/thunderbolt-authorization.pending ]] ||
fail "$label keeps Thunderbolt authorization markers"
[[ ! -e $root/var/lib/omarchy/thunderbolt-authorization ]] ||
fail "$label keeps the Thunderbolt authorization journal"
[[ ! -e $root/var/lib/boltd/devices && ! -e $root/var/lib/boltd/keys && ! -e $root/var/lib/boltd/domains ]] ||
fail "$label keeps Bolt enrollments"
[[ $(awk -F= '/^[[:space:]]*AuthMode[[:space:]]*=/ { gsub(/[[:space:]]/, "", $2); print $2 }' "$root/var/lib/boltd/boltd.conf") == "enabled" ]] ||
fail "$label leaves Bolt authorizing stored devices"
grep -qx 'SomeOther = keep' "$root/var/lib/boltd/boltd.conf" ||
fail "$label rewrites unrelated Bolt settings"
}

top="$test_tmp/trusted"
mkdir -p "$top/@"
touch "$top/@/old-system"
make_accounts "$top/@factory"
plant_trust "$top/@factory"
bash "$test_tmp/reset" "$test_tmp" "$top" || fail "reset stages a snapshot that carries accessory trust"
assert_trust_cleared "$top/@" "the booted factory system"
assert_trust_cleared "$top/@factory" "the retained factory baseline"
[[ $(cat "$top/@factory/read-only") == "true" ]] || fail "baseline returns to read-only"
[[ ! -e $top/@/home/seller && -f $top/rebuilt ]] || fail "reset finishes account cleanup and boot rebuild"
pass "reset clears inherited USBGuard and Bolt trust in both roots"

rm -f "$top/rebuilt"
bash "$test_tmp/reset" "$test_tmp" "$top" || fail "reset can be repeated after accessory trust is gone"
assert_trust_cleared "$top/@" "a second booted factory system"
assert_trust_cleared "$top/@factory" "a second factory baseline"
pass "repeated reset leaves accessory trust cleared"

clean="$test_tmp/clean"
mkdir -p "$clean/@"
touch "$clean/@/old-system"
make_accounts "$clean/@factory"
bash "$test_tmp/reset" "$test_tmp" "$clean" || fail "reset stages a snapshot with no accessory trust"
[[ ! -e $clean/@/var/lib/boltd && ! -e $clean/@factory/var/lib/boltd ]] ||
fail "reset invents a Bolt store the snapshot never had"
[[ ! -e $clean/@/usr/lib/systemd/system/usbguard.service ]] ||
fail "reset invents a USBGuard unit the snapshot never had"
pass "reset leaves a snapshot without accessory trust untouched"

outside="$test_tmp/outside/devices"
mkdir -p "$outside"
printf 'keep\n' >"$outside/seller-dock"
linked="$test_tmp/linked"
mkdir -p "$linked/@"
touch "$linked/@/old-system"
make_accounts "$linked/@factory"
plant_trust "$linked/@factory"
rm -rf "$linked/@factory/var/lib/boltd"
ln -s "$test_tmp/outside" "$linked/@factory/var/lib/boltd"
if bash "$test_tmp/reset" "$test_tmp" "$linked"; then
fail "reset accepted a Bolt store that points outside the snapshot"
fi
[[ -f $outside/seller-dock && $(cat "$outside/seller-dock") == "keep" ]] ||
fail "reset follows a linked Bolt store and deletes the outside enrollment"
[[ -f $linked/@/old-system && ! -e $linked/rebuilt ]] ||
fail "a linked Bolt store still activates the reset"
[[ $(cat "$linked/@factory/read-only") == "true" ]] ||
fail "a linked Bolt store leaves the baseline writable"
pass "reset refuses a linked Bolt store before activation"