Skip to content

Commit 4d697a0

Browse files
committed
Harden privileged cleanup review fixes
1 parent 4996941 commit 4d697a0

13 files changed

Lines changed: 219 additions & 32 deletions

‎agents/skills/migrations.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ New migration format:
126126
- Start with an `echo` describing what the migration does.
127127
- Use `$OMARCHY_PATH` to reference the Omarchy directory.
128128
- Be idempotent. Check existing state before changing it.
129-
- Exit 75 when a temporary condition must leave the migration pending without blocking later migrations. `omarchy-migrate` continues the queue, does not write that migration's completion marker, and retries it on a later run. Other non-zero statuses still abort the migration run.
129+
- When a temporary condition must leave the migration pending without blocking later migrations, write `$OMARCHY_MIGRATION_DEFER_TOKEN` to `$OMARCHY_MIGRATION_DEFER_FILE`, then exit 75. `omarchy-migrate` requires both signals before it continues the queue without writing that migration's completion marker. An unmarked exit 75 is an ordinary failure and aborts the run, so a child command returning `EX_TEMPFAIL` cannot accidentally defer a migration forever.
130130
- Use helper commands such as `omarchy-cmd-present`, `omarchy-cmd-missing`,
131131
`omarchy-pkg-add`, `omarchy-pkg-drop`, `omarchy-pkg-present`, and
132132
`omarchy-pkg-missing` when appropriate.

‎bin/omarchy-migrate‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,28 @@ while IFS=$'\t' read -r name file marker; do
9191

9292
if [[ ! -f $marker ]]; then
9393
echo -e "\e[32m\nRunning migration (${name%.sh})\e[0m"
94-
if OMARCHY_PATH="$OMARCHY_PATH" bash -euo pipefail "$file"; then
94+
defer_file=$(mktemp "$STATE_DIR/.defer.XXXXXX")
95+
defer_token="$BASHPID-$RANDOM-$RANDOM"
96+
migration_status=0
97+
98+
if OMARCHY_PATH="$OMARCHY_PATH" \
99+
OMARCHY_MIGRATION_DEFER_FILE="$defer_file" \
100+
OMARCHY_MIGRATION_DEFER_TOKEN="$defer_token" \
101+
bash -euo pipefail "$file"; then
95102
mkdir -p "$(dirname "$marker")"
96103
touch "$marker"
97104
else
98105
migration_status=$?
99-
if (( migration_status == 75 )); then
106+
if (( migration_status == 75 )) && [[ $(<"$defer_file") == "$defer_token" ]]; then
100107
deferred+=("$name")
101108
echo "Migration ${name%.sh} was deferred and will be retried later."
102109
else
110+
rm -f "$defer_file"
103111
exit "$migration_status"
104112
fi
105113
fi
114+
115+
rm -f "$defer_file"
106116
fi
107117
done < <(migration_entries)
108118

‎bin/omarchy-provision-owner‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ configure_login() {
788788
# After=) is what makes it deterministic — no sleep/race against SDDM's startup.
789789
install_autologin_once_cleanup() {
790790
local unit=omarchy-provision-autologin-once.service
791-
cat >"/etc/systemd/system/$unit" <<'UNIT'
791+
sed "s|@UNIT@|$unit|g" >"/etc/systemd/system/$unit" <<'UNIT'
792792
[Unit]
793793
Description=Drop the first-boot autologin before the next login
794794
Before=display-manager.service
@@ -797,7 +797,7 @@ ConditionPathExists=/etc/sddm.conf.d/autologin.conf
797797
[Service]
798798
Type=oneshot
799799
ExecStart=/usr/bin/rm -f /etc/sddm.conf.d/autologin.conf
800-
ExecStartPost=/usr/bin/rm -f /etc/systemd/system/graphical.target.wants/omarchy-provision-autologin-once.service /etc/systemd/system/omarchy-provision-autologin-once.service
800+
ExecStartPost=/usr/bin/rm -f /etc/systemd/system/graphical.target.wants/@UNIT@ /etc/systemd/system/@UNIT@
801801
802802
[Install]
803803
WantedBy=graphical.target

‎migrations/1787946619.sh‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@ as_root() {
1010
fi
1111
}
1212

13+
defer_privileged_repair() {
14+
echo "Cannot remove the legacy privileged udev rule; omarchy-migrate will retry it later." >&2
15+
if [[ -n ${OMARCHY_MIGRATION_DEFER_FILE:-} && -n ${OMARCHY_MIGRATION_DEFER_TOKEN:-} ]]; then
16+
printf '%s\n' "$OMARCHY_MIGRATION_DEFER_TOKEN" >"$OMARCHY_MIGRATION_DEFER_FILE"
17+
fi
18+
exit 75
19+
}
20+
1321
# Omarchy 3 generated these two rules with an unquoted heredoc, so the installing
14-
# user's $HOME was expanded and the file on disk names
15-
# /home/<user>/.local/share/omarchy/bin/<command>. udev runs RUN+= as root, and
22+
# user's $HOME was expanded and the file on disk names that absolute home path.
23+
# udev runs RUN+= as root, and
1624
# ~/.local/share/omarchy is a symlink that same unprivileged user owns: replacing
1725
# it with a tree of their own and provoking a power_supply event runs their code
1826
# as root. Quattro ships the rules as 99-omarchy-*.rules under /usr/bin, but the
@@ -32,7 +40,7 @@ as_root() {
3240
# path in a comment, and so does a legacy file already repointed at /usr/bin.
3341
rule_runs_from_home() {
3442
local file="$1" binary="$2"
35-
local pattern="^(/home/[^/]+|/root)/\\.local/share/omarchy/bin/$binary\$"
43+
local pattern="^/.+/\\.local/share/omarchy/bin/$binary\$"
3644
local line logical="" rest command word
3745
local -a words
3846

@@ -68,7 +76,7 @@ rule_runs_from_home() {
6876
# argument. Compare whole words so no substring stands in for the path.
6977
read -ra words <<<"$command"
7078
for word in "${words[@]}"; do
71-
if [[ $word =~ $pattern || $word == "$HOME/.local/share/omarchy/bin/$binary" ]]; then
79+
if [[ $word =~ $pattern ]]; then
7280
return 0
7381
fi
7482
done
@@ -84,7 +92,9 @@ for legacy_rule in "99-power-profile.rules:omarchy-powerprofiles-set" "99-wifi-p
8492
rule_file="$rules_dir/${legacy_rule%%:*}"
8593

8694
if [[ -f $rule_file ]] && rule_runs_from_home "$rule_file" "${legacy_rule##*:}"; then
87-
as_root rm -f "$rule_file"
95+
if ! as_root rm -f "$rule_file"; then
96+
defer_privileged_repair
97+
fi
8898
removed=1
8999
fi
90100
done

‎migrations/1788025225.sh‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ sudoers_hash_is_active() {
115115
# one line that is unmistakably this grant: its own self-cleanup. One
116116
# hand-written line anywhere in the file and it is not ours to delete.
117117
first_run_sudoers_is_generated() {
118-
local spec_pattern='^[^[:space:]]+ ALL=\(ALL\) NOPASSWD: (.+)$'
118+
local spec_pattern='^([^[:space:]]+) ALL=\(ALL\) NOPASSWD: (.+)$'
119119
local marker_pattern='^/bin/rm -f /home/[^/]+/\.local/state/omarchy/first-run\.mode$'
120-
local line command
121-
local seen_any=0 seen_marker=0
120+
local line user command generated_user=""
121+
local seen_any=0 seen_marker=0 seen_spec=0
122122

123123
while IFS= read -r line; do
124124
seen_any=1
@@ -140,7 +140,13 @@ first_run_sudoers_is_generated() {
140140
if [[ ! $line =~ $spec_pattern ]]; then
141141
return 1
142142
fi
143-
command=${BASH_REMATCH[1]}
143+
user=${BASH_REMATCH[1]}
144+
command=${BASH_REMATCH[2]}
145+
if [[ -n $generated_user && $user != "$generated_user" ]]; then
146+
return 1
147+
fi
148+
generated_user=$user
149+
seen_spec=1
144150

145151
case "$command" in
146152
"/usr/bin/systemctl" | "/usr/bin/ufw" | "/usr/bin/ufw-docker" | \
@@ -162,7 +168,7 @@ first_run_sudoers_is_generated() {
162168
return 1
163169
done < <(active_lines sudoers)
164170

165-
(( seen_any && seen_marker ))
171+
(( seen_any && seen_marker && seen_spec ))
166172
}
167173

168174
# bin/omarchy-install-tailscale (2025-08-22 to 2026-02-02) ran
@@ -202,7 +208,7 @@ tsui_sudoers_is_generated() {
202208
plymouth_unit_runs_from_home() {
203209
local binary="omarchy-plymouth-shutdown-sync"
204210
local exec_stop_pattern='^ExecStop[[:space:]]*=[[:space:]]*(.*)$'
205-
local home_pattern="^(/home/[^/]+|/root)/\\.local/share/omarchy/bin/$binary\$"
211+
local home_pattern="^/.+/\\.local/share/omarchy/bin/$binary\$"
206212
local line word
207213
local -a words
208214
local matched=1
@@ -229,7 +235,7 @@ plymouth_unit_runs_from_home() {
229235
word=${word:1}
230236
done
231237

232-
if [[ $word =~ $home_pattern || $word == "$HOME/.local/share/omarchy/bin/$binary" ]]; then
238+
if [[ $word =~ $home_pattern ]]; then
233239
# Non-empty ExecStop= assignments append to the command list. Once a
234240
# vulnerable command is present it stays live until an empty assignment
235241
# explicitly resets the list; a later packaged command does not replace it.
@@ -249,6 +255,9 @@ tsui_sudoers="$sudoers_dir/tsui"
249255

250256
defer_privileged_repair() {
251257
echo "Cannot complete the privileged installer-artifact repair; omarchy-migrate will retry it later." >&2
258+
if [[ -n ${OMARCHY_MIGRATION_DEFER_FILE:-} && -n ${OMARCHY_MIGRATION_DEFER_TOKEN:-} ]]; then
259+
printf '%s\n' "$OMARCHY_MIGRATION_DEFER_TOKEN" >"$OMARCHY_MIGRATION_DEFER_FILE"
260+
fi
252261
exit 75
253262
}
254263

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# omarchy:heredoc-expands paths=none -- the positional argument is a scalar
2+
sudo tee /etc/omarchy/example.conf <<EOF
3+
argument=$1
4+
command=$HOME/.local/share/omarchy/bin/example
5+
EOF
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cat <<EOF |
2+
command=$HOME/.local/share/omarchy/bin/example
3+
EOF
4+
sudo tee /etc/omarchy/example.conf
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
tmp=/tmp/omarchy-generated
2+
copy=$tmp
3+
cat >"$tmp" <<EOF
4+
command=$HOME/.local/share/omarchy/bin/example
5+
EOF
6+
sudo install -m644 "$copy" /etc/omarchy/example.conf
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
tmp=/tmp/omarchy-generated
2+
cat >"$tmp" <<EOF
3+
command=$HOME/.local/share/omarchy/bin/example
4+
EOF
5+
sudo install -m644 "${tmp}" /etc/omarchy/example.conf

‎test/shell.d/legacy-power-udev-rules-migration-test.sh‎

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ STUB
2929

3030
chmod +x "$test_dir/bin/"*
3131

32+
mkdir -p "$test_dir/failing-bin"
33+
cat >"$test_dir/failing-bin/sudo" <<'STUB'
34+
#!/bin/bash
35+
36+
echo "sudo: a terminal is required to read the password" >&2
37+
exit 1
38+
STUB
39+
chmod +x "$test_dir/failing-bin/sudo"
40+
3241
export CALLS="$test_dir/calls"
3342

3443
rules_dir="$test_dir/rules.d"
@@ -189,17 +198,41 @@ run_migration
189198
fail "migration keeps a legacy filename already repointed at /usr/bin"
190199
pass "migration keeps a legacy filename already repointed at /usr/bin"
191200

192-
# Homes are not all under /home, so the running user's own home counts too, and
193-
# the argument the later variants passed must not hide the path.
201+
# Homes are not all under /home, and a different account may run this
202+
# machine-wide repair after the installer account has gone away.
194203
reset_machine
195204
cat >"$wifi_rule" <<RULE
196-
SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="0", RUN+="/usr/bin/systemd-run --no-block --collect --unit=omarchy-wifi-powersave-on $home_dir/.local/share/omarchy/bin/omarchy-wifi-powersave on"
205+
SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="0", RUN+="/usr/bin/systemd-run --no-block --collect --unit=omarchy-wifi-powersave-on /srv/retired-installer/.local/share/omarchy/bin/omarchy-wifi-powersave on"
197206
RULE
198207
run_migration
199208

200209
[[ ! -e $wifi_rule ]] ||
201-
fail "migration removes a rule that runs out of a home outside /home" "$(cat "$wifi_rule")"
202-
pass "migration removes a rule that runs out of a home outside /home"
210+
fail "migration removes another user's rule rooted outside /home" "$(cat "$wifi_rule")"
211+
pass "migration removes another user's rule rooted outside /home"
212+
213+
# A user who cannot elevate must leave this repair pending without preventing
214+
# later migrations from running. Once another account removes the machine-wide
215+
# file, the next retry can complete without sudo.
216+
reset_machine
217+
write_vulnerable_wifi_rule
218+
defer_file="$test_dir/defer-signal"
219+
defer_token="legacy-udev-repair"
220+
: >"$defer_file"
221+
222+
set +e
223+
HOME="$home_dir" \
224+
OMARCHY_UDEV_RULES_DIR="$rules_dir" \
225+
OMARCHY_MIGRATION_DEFER_FILE="$defer_file" \
226+
OMARCHY_MIGRATION_DEFER_TOKEN="$defer_token" \
227+
PATH="$test_dir/failing-bin:$PATH" \
228+
bash -euo pipefail "$migration" >"$test_dir/defer.out" 2>&1
229+
defer_status=$?
230+
set -e
231+
232+
(( defer_status == 75 )) || fail "migration defers when sudo cannot remove a vulnerable rule" "status=$defer_status"
233+
[[ -e $wifi_rule ]] || fail "migration keeps the vulnerable rule when its elevated removal fails"
234+
[[ $(<"$defer_file") == "$defer_token" ]] || fail "migration authenticates its deferral to the runner"
235+
pass "migration defers instead of blocking the queue when removal cannot elevate"
203236

204237
# Nothing named the wrong binary is ours: the same path with a different command
205238
# is a rule this migration cannot claim to know anything about.

0 commit comments

Comments
 (0)