Skip to content

Commit cd51928

Browse files
committed
Remove Omarchy 3 power udev rules that run a command out of a user home
Omarchy 3 wrote 99-power-profile.rules and 99-wifi-powersave.rules with an unquoted heredoc, baking the installing user's home into a rule udev runs as root. That path resolves through ~/.local/share/omarchy, a symlink the unprivileged user owns, so replacing it and provoking a power_supply event runs their code as root. HEAD points the rules at /usr/bin under new names, but the one-shot cleanup for the old filenames was dropped, leaving the file on every install that came up through the 3.x line. Remove a legacy file only when an active RUN+= really does run that filename's binary out of a home directory, so a rule of the same name a user wrote themselves stays, comments and all.
1 parent a75551d commit cd51928

2 files changed

Lines changed: 327 additions & 0 deletions

File tree

‎migrations/1787946619.sh‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
echo "Remove Omarchy 3 power udev rules that run a command out of a user home"
2+
3+
rules_dir="${OMARCHY_UDEV_RULES_DIR:-/etc/udev/rules.d}"
4+
5+
as_root() {
6+
if (( EUID == 0 )); then
7+
"$@"
8+
else
9+
sudo "$@"
10+
fi
11+
}
12+
13+
# 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
16+
# ~/.local/share/omarchy is a symlink that same unprivileged user owns: replacing
17+
# it with a tree of their own and provoking a power_supply event runs their code
18+
# as root. Quattro ships the rules as 99-omarchy-*.rules under /usr/bin, but the
19+
# one-shot migration that swept the old filenames was itself dropped, so an
20+
# install that came up through the 3.x line keeps the old file until this
21+
# migration removes it.
22+
#
23+
# Pre-4 layout work normally belongs in bin/omarchy-upgrade-to-quattro, but that
24+
# command only runs on a machine still making the crossing, so an install that
25+
# crossed already would never see it. The upgrade command ends by running
26+
# omarchy-migrate, so this covers the installs still to upgrade as well.
27+
#
28+
# Only remove a file that is actually one of those. A comment is inert to udev,
29+
# so the match keys on an active RUN+= whose command really is the legacy path
30+
# under a home directory for that filename's binary. A rule of the same name that
31+
# a user wrote themselves survives, including one that merely mentions the legacy
32+
# path in a comment, and so does a legacy file already repointed at /usr/bin.
33+
rule_runs_from_home() {
34+
local file="$1" binary="$2"
35+
local pattern="^(/home/[^/]+|/root)/\\.local/share/omarchy/bin/$binary\$"
36+
local line logical="" rest command word
37+
local -a words
38+
39+
while IFS= read -r line || [[ -n $line ]]; do
40+
# udev tests for a comment before it joins continuations, and skipping one
41+
# does not end a continuation already under way. Both halves verified with
42+
# `udevadm verify`: "# disabled \" followed by a bogus key reports the error
43+
# on line 2, so a comment's own trailing backslash swallows nothing, while
44+
# 'SUBSYSTEM=="power_supply" \' + "# c" + ', RUN+="..."' reports its style
45+
# warning on line 1, so the rule spans the comment. Testing the comment after
46+
# the join would hide a live rule; clearing the pending line here would hide
47+
# one just as well.
48+
if [[ $line =~ ^[[:space:]]*# ]]; then
49+
continue
50+
fi
51+
52+
# A trailing backslash continues the rule on the next line.
53+
if [[ $line == *\\ ]]; then
54+
logical+=${line%\\}
55+
continue
56+
fi
57+
58+
rest=$logical$line
59+
logical=""
60+
61+
while [[ $rest == *'RUN+="'* ]]; do
62+
rest=${rest#*'RUN+="'}
63+
command=${rest%%'"'*}
64+
rest=${rest#*'"'}
65+
66+
# The legacy rules put the binary first (wifi power save) or last, after a
67+
# systemd-run invocation (power profile), and some variants passed it an
68+
# argument. Compare whole words so no substring stands in for the path.
69+
read -ra words <<<"$command"
70+
for word in "${words[@]}"; do
71+
if [[ $word =~ $pattern || $word == "$HOME/.local/share/omarchy/bin/$binary" ]]; then
72+
return 0
73+
fi
74+
done
75+
done
76+
done <"$file"
77+
78+
return 1
79+
}
80+
81+
removed=0
82+
83+
for legacy_rule in "99-power-profile.rules:omarchy-powerprofiles-set" "99-wifi-powersave.rules:omarchy-wifi-powersave"; do
84+
rule_file="$rules_dir/${legacy_rule%%:*}"
85+
86+
if [[ -f $rule_file ]] && rule_runs_from_home "$rule_file" "${legacy_rule##*:}"; then
87+
as_root rm -f "$rule_file"
88+
removed=1
89+
fi
90+
done
91+
92+
if (( removed )); then
93+
# Drop the rule from the running udevd too; until it reloads, the rule that was
94+
# just deleted still fires on the next power_supply event. Best effort the way
95+
# install/post-install/udev.sh is: a machine with no udevd to talk to has
96+
# already had the file removed, and the next boot reads the directory fresh.
97+
as_root udevadm control --reload 2>/dev/null || true
98+
fi
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
source "$(dirname "$0")/base-test.sh"
6+
7+
migration="$ROOT/migrations/1787946619.sh"
8+
[[ -f $migration ]] || fail "the legacy power udev rule migration exists at $migration"
9+
10+
test_dir=$(mktemp -d)
11+
trap 'rm -rf "$test_dir"' EXIT
12+
13+
mkdir -p "$test_dir/bin"
14+
15+
# sudo runs the real command, so the removals act on the redirected rules
16+
# directory below and the elevated calls land in the log beside it.
17+
cat >"$test_dir/bin/sudo" <<'STUB'
18+
#!/bin/bash
19+
20+
printf 'sudo %s\n' "$*" >>"$CALLS"
21+
exec "$@"
22+
STUB
23+
24+
cat >"$test_dir/bin/udevadm" <<'STUB'
25+
#!/bin/bash
26+
27+
printf 'udevadm %s\n' "$*" >>"$CALLS"
28+
STUB
29+
30+
chmod +x "$test_dir/bin/"*
31+
32+
export CALLS="$test_dir/calls"
33+
34+
rules_dir="$test_dir/rules.d"
35+
home_dir="$test_dir/home"
36+
power_rule="$rules_dir/99-power-profile.rules"
37+
wifi_rule="$rules_dir/99-wifi-powersave.rules"
38+
39+
reset_machine() {
40+
rm -rf "$rules_dir" "$home_dir"
41+
mkdir -p "$rules_dir" "$home_dir"
42+
}
43+
44+
run_migration() {
45+
: >"$CALLS"
46+
47+
HOME="$home_dir" \
48+
OMARCHY_UDEV_RULES_DIR="$rules_dir" \
49+
PATH="$test_dir/bin:$PATH" \
50+
bash -euo pipefail "$migration" >/dev/null
51+
}
52+
53+
reload_count() {
54+
grep -cx 'udevadm control --reload' "$CALLS" || true
55+
}
56+
57+
# What Omarchy 3's unquoted heredoc actually left on disk: the installing user's
58+
# home expanded into a rule root runs on every power_supply event.
59+
write_vulnerable_power_rule() {
60+
cat >"$power_rule" <<'RULE'
61+
SUBSYSTEM=="power_supply", ATTR{type}=="Mains", RUN+="/usr/bin/systemd-run --no-block --collect --unit=omarchy-power-profile --property=After=power-profiles-daemon.service /home/someuser/.local/share/omarchy/bin/omarchy-powerprofiles-set"
62+
SUBSYSTEM=="power_supply", ATTR{type}=="USB", RUN+="/usr/bin/systemd-run --no-block --collect --unit=omarchy-power-profile --property=After=power-profiles-daemon.service /home/someuser/.local/share/omarchy/bin/omarchy-powerprofiles-set"
63+
RULE
64+
}
65+
66+
write_vulnerable_wifi_rule() {
67+
cat >"$wifi_rule" <<'RULE'
68+
SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="0", RUN+="/home/someuser/.local/share/omarchy/bin/omarchy-wifi-powersave on"
69+
SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="1", RUN+="/home/someuser/.local/share/omarchy/bin/omarchy-wifi-powersave off"
70+
RULE
71+
}
72+
73+
reset_machine
74+
write_vulnerable_power_rule
75+
run_migration
76+
77+
[[ ! -e $power_rule ]] ||
78+
fail "migration removes a power profile rule that runs out of a user home" "$(cat "$power_rule")"
79+
pass "migration removes a power profile rule that runs out of a user home"
80+
81+
grep -q '^sudo rm -f .*99-power-profile\.rules$' "$CALLS" ||
82+
fail "migration removes the rule with elevated privileges" "$(cat "$CALLS")"
83+
pass "migration removes the rule with elevated privileges"
84+
85+
reset_machine
86+
write_vulnerable_wifi_rule
87+
run_migration
88+
89+
[[ ! -e $wifi_rule ]] ||
90+
fail "migration removes a Wi-Fi power save rule that runs out of a user home" "$(cat "$wifi_rule")"
91+
pass "migration removes a Wi-Fi power save rule that runs out of a user home"
92+
93+
# udevd keeps running the rule it already parsed, so the file being gone from
94+
# disk is only half the fix until it reloads.
95+
(( $(reload_count) == 1 )) ||
96+
fail "migration reloads udev after removing a rule" "$(cat "$CALLS")"
97+
pass "migration reloads udev after removing a rule"
98+
99+
# Both files gone is still one machine-wide reload, not one per file.
100+
reset_machine
101+
write_vulnerable_power_rule
102+
write_vulnerable_wifi_rule
103+
run_migration
104+
105+
[[ ! -e $power_rule && ! -e $wifi_rule ]] ||
106+
fail "migration removes both legacy rules in one pass"
107+
(( $(reload_count) == 1 )) ||
108+
fail "migration reloads udev once for both removals" "$(cat "$CALLS")"
109+
pass "migration removes both legacy rules and reloads udev once"
110+
111+
# The second run is what every other account on the machine does, and what a
112+
# user gets from running omarchy-migrate again.
113+
run_migration
114+
115+
(( $(reload_count) == 0 )) ||
116+
fail "migration does not reload udev on a second run" "$(cat "$CALLS")"
117+
[[ ! -s $CALLS ]] ||
118+
fail "migration touches nothing on a second run" "$(cat "$CALLS")"
119+
pass "migration is a no-op on a second run"
120+
121+
reset_machine
122+
run_migration
123+
124+
[[ ! -s $CALLS ]] ||
125+
fail "migration touches nothing when the legacy rules are absent" "$(cat "$CALLS")"
126+
pass "migration leaves a machine without the legacy rules alone"
127+
128+
# A user who wrote their own rule under one of these names keeps it, even when
129+
# the file talks about the legacy checkout. udev never runs a comment.
130+
reset_machine
131+
cat >"$power_rule" <<'RULE'
132+
# Replaces the rule Omarchy used to install from
133+
# /home/someuser/.local/share/omarchy/bin/omarchy-powerprofiles-set
134+
#SUBSYSTEM=="power_supply", ATTR{type}=="Mains", RUN+="/home/someuser/.local/share/omarchy/bin/omarchy-powerprofiles-set"
135+
SUBSYSTEM=="power_supply", ATTR{type}=="Mains", RUN+="/usr/local/bin/my-own-power-hook"
136+
RULE
137+
cat >"$wifi_rule" <<'RULE'
138+
# Kept from the old local/share/omarchy setup, rewritten to my own script
139+
SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="1", RUN+="/usr/local/bin/my-own-wifi-hook off"
140+
RULE
141+
before=$(cat "$power_rule" "$wifi_rule")
142+
run_migration
143+
144+
[[ -e $power_rule && -e $wifi_rule ]] ||
145+
fail "migration keeps same-named rules that only mention the legacy path"
146+
[[ $(cat "$power_rule" "$wifi_rule") == "$before" ]] ||
147+
fail "migration leaves the user's own rules byte for byte"
148+
[[ ! -s $CALLS ]] ||
149+
fail "migration escalates nothing when it removes nothing" "$(cat "$CALLS")"
150+
pass "migration keeps same-named rules that only mention the legacy path"
151+
152+
# udev discards a '#' line before it ever looks for a trailing backslash, so the
153+
# rule below the comment is live and root still runs it. `udevadm verify` on this
154+
# exact shape, with a bogus key on the second line, reports the error on line 2.
155+
# The file has to go.
156+
reset_machine
157+
cat >"$power_rule" <<'RULE'
158+
# Disabled while I test the packaged rule: \
159+
SUBSYSTEM=="power_supply", ATTR{type}=="Mains", RUN+="/home/someuser/.local/share/omarchy/bin/omarchy-powerprofiles-set"
160+
RULE
161+
run_migration
162+
163+
[[ ! -e $power_rule ]] ||
164+
fail "migration removes a rule left live under a commented continuation" "$(cat "$power_rule")"
165+
pass "migration removes a rule left live under a commented continuation"
166+
167+
# A comment that does not continue still hides nothing behind it: the file holds
168+
# no active RUN+= at all and stays.
169+
reset_machine
170+
cat >"$power_rule" <<'RULE'
171+
# SUBSYSTEM=="power_supply", ATTR{type}=="Mains", RUN+="/home/someuser/.local/share/omarchy/bin/omarchy-powerprofiles-set"
172+
RULE
173+
run_migration
174+
175+
[[ -e $power_rule ]] ||
176+
fail "migration keeps a rule that is only ever mentioned in a comment"
177+
pass "migration keeps a rule that is only ever mentioned in a comment"
178+
179+
# The May 2026 rename left an intermediate variant under the old filename that
180+
# already ran out of /usr/bin. It duplicates the packaged rule but is not the
181+
# privilege escalation this migration exists to clear, so it is not ours to take.
182+
reset_machine
183+
cat >"$power_rule" <<'RULE'
184+
SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="1", RUN+="/usr/bin/systemd-run --no-block --collect --unit=omarchy-power-profile-ac --property=After=power-profiles-daemon.service /usr/bin/powerprofilesctl set performance"
185+
RULE
186+
run_migration
187+
188+
[[ -e $power_rule ]] ||
189+
fail "migration keeps a legacy filename already repointed at /usr/bin"
190+
pass "migration keeps a legacy filename already repointed at /usr/bin"
191+
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.
194+
reset_machine
195+
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"
197+
RULE
198+
run_migration
199+
200+
[[ ! -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"
203+
204+
# Nothing named the wrong binary is ours: the same path with a different command
205+
# is a rule this migration cannot claim to know anything about.
206+
reset_machine
207+
cat >"$power_rule" <<'RULE'
208+
SUBSYSTEM=="power_supply", ATTR{type}=="Mains", RUN+="/home/someuser/.local/share/omarchy/bin/omarchy-wifi-powersave on"
209+
RULE
210+
run_migration
211+
212+
[[ -e $power_rule ]] ||
213+
fail "migration matches the binary the filename promises, not any home path"
214+
pass "migration matches the binary the filename promises, not any home path"
215+
216+
# udev resumes a continuation across a comment: `udevadm verify` on
217+
# 'SUBSYSTEM=="power_supply" \' + "# c" + ', RUN+="..."' reports its style warning
218+
# on line 1, so those three lines are one rule and the rule is live.
219+
reset_machine
220+
cat >"$power_rule" <<'RULE'
221+
SUBSYSTEM=="power_supply", ATTR{type}=="Mains" \
222+
# split for readability
223+
, RUN+="/home/someuser/.local/share/omarchy/bin/omarchy-powerprofiles-set"
224+
RULE
225+
run_migration
226+
227+
[[ ! -e $power_rule ]] ||
228+
fail "migration removes a rule that continues across a comment" "$(cat "$power_rule")"
229+
pass "migration removes a rule that continues across a comment"

0 commit comments

Comments
 (0)