Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
131 changes: 131 additions & 0 deletions bin/omarchy-apply-binfmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#!/bin/bash

# omarchy:summary=Keep QEMU binfmt registrations usable for setuid binaries in cross-arch containers
# omarchy:group=apply
# omarchy:requires-sudo=true
# omarchy:hidden=true

# qemu-user-static-binfmt registers its interpreters with flags FP. Setuid
# binaries such as sudo in an emulated-architecture Docker container only keep
# their credentials under binfmt_misc emulation when the registration carries
# C (which implies O, handing the interpreter the binary as an open file). F,
# already set by the package, is what lets the interpreter run inside a
# container whose rootfs differs from the host's.
#
# This writes an /etc/binfmt.d override per package registration with O and C
# added to the package's own flags, so P (argv[0] preserved) and anything a
# later package adds survive. The 30-omarchy-binfmt pacman hook reruns it
# whenever qemu changes its registrations, so overrides follow interpreter,
# magic and mask changes, cover newly added architectures, and go away with
# removed ones. Files without the managed marker belong to the administrator
# and are left alone.
#
# C is machine-wide: QEMU runs a root-owned setuid foreign-architecture binary
# with root's credentials anywhere on the host, not only inside Docker.

set -euo pipefail

usage() {
cat <<USAGE
Usage: omarchy-apply-binfmt [--check]

Regenerates /etc/binfmt.d/qemu-*-static.conf from the qemu-user-static-binfmt
registrations with the O and C flags added, removes managed overrides whose
registration is gone, and restarts systemd-binfmt when the running
registrations differ.

--check exit 1 if anything would change, without changing it
USAGE
}

check=0
case "${1:-}" in
"") ;;
--check) check=1 ;;
-h|--help) usage; exit 0 ;;
*) usage >&2; exit 2 ;;
esac

source_dir="${OMARCHY_BINFMT_SOURCE_DIR:-/usr/lib/binfmt.d}"
dest_dir="${OMARCHY_BINFMT_DIR:-/etc/binfmt.d}"
proc_dir="${OMARCHY_BINFMT_PROC_DIR:-/proc/sys/fs/binfmt_misc}"
marker="# Managed by omarchy-apply-binfmt from $source_dir; local edits are overwritten"

managed() {
local first_line
[[ -f $1 ]] && IFS= read -r first_line <"$1" && [[ $first_line == "# Managed by omarchy-apply-binfmt "* ]]
}

sorted_letters() {
grep -o . <<<"$1" | sort | tr -d '\n'
}

declare -A writes=()
declare -A live_flags=()
removes=()

for conf in "$source_dir"/qemu-*-static.conf; do
[[ -e $conf ]] || continue

line=$(grep -m1 -v -e '^[#;]' -e '^$' "$conf" || true)
[[ -n $line ]] || continue

flags=${line##*:}
for flag in O C; do
[[ $flags == *$flag* ]] || flags+=$flag
done

target="$dest_dir/$(basename "$conf")"
if [[ -e $target ]] && ! managed "$target"; then
echo "Leaving unmanaged $target alone" >&2
continue
fi

content=$(printf '%s\n%s:%s' "$marker" "${line%:*}" "$flags")
if [[ ! -e $target || $(<"$target") != "$content" ]]; then
writes[$target]=$content
fi

name=${line#:}
live_flags[${name%%:*}]=$flags
done

for target in "$dest_dir"/qemu-*-static.conf; do
if [[ -e $target && ! -e $source_dir/$(basename "$target") ]] && managed "$target"; then
removes+=("$target")
fi
done

restart=0
if (( ${#writes[@]} || ${#removes[@]} )); then
restart=1
elif [[ -e $proc_dir/status ]]; then
for name in "${!live_flags[@]}"; do
live=$(sed -n 's/^flags: //p' "$proc_dir/$name" 2>/dev/null || true)
if [[ $(sorted_letters "$live") != $(sorted_letters "${live_flags[$name]}") ]]; then
restart=1
break
fi
done
fi

if (( check )); then
exit "$restart"
fi

if (( ${#writes[@]} )); then
mkdir -p "$dest_dir"
for target in "${!writes[@]}"; do
printf '%s\n' "${writes[$target]}" >"$target"
done
fi

for target in "${removes[@]}"; do
rm -f "$target"
done

# binfmt_misc is not mounted in the ISO's target chroot; the registrations load
# at first boot there.
if (( restart )) && [[ -e $proc_dir/status ]]; then
systemctl restart systemd-binfmt.service
fi
14 changes: 14 additions & 0 deletions default/libalpm/hooks/30-omarchy-binfmt.hook
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Sorts after 25-systemd-binfmt.hook, which reloads the package registrations
# that the overrides written here shadow.
[Trigger]
Type = Path
Operation = Install
Operation = Upgrade
Operation = Remove
Target = usr/lib/binfmt.d/qemu-*-static.conf

[Action]
Description = Adding O and C to QEMU binfmt registrations...
When = PostTransaction
Depends = omarchy
Comment thread
axelfontaine marked this conversation as resolved.
Resolved
Exec = /usr/bin/omarchy-apply-binfmt
2 changes: 2 additions & 0 deletions docs/update-process.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ hook runs `omarchy-hyprland-reload-guard resume`, forces one `hyprctl reload`,
and restores the session's previous `misc.disable_autoreload` and
`debug.suppress_errors` values.

A post-transaction hook on `usr/lib/binfmt.d/qemu-*-static.conf` runs `omarchy-apply-binfmt`, which regenerates the `/etc/binfmt.d` overrides that add `O` and `C` to the QEMU registrations, so they follow every qemu update, and restarts `systemd-binfmt` when the running registrations differ. It sorts after systemd's own `25-systemd-binfmt.hook`.

## Path 1: `omarchy update`

High-level flow:
Expand Down
1 change: 1 addition & 0 deletions install/config/all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ run_logged "$OMARCHY_INSTALL/config/fix-powerprofilesctl-shebang.sh"
run_logged "$OMARCHY_INSTALL/config/ssh-command-path.sh"
run_logged "$OMARCHY_INSTALL/config/ssh-keepalive.sh"
run_logged "$OMARCHY_INSTALL/config/docker.sh"
run_logged "$OMARCHY_INSTALL/config/binfmt.sh"
run_logged "$OMARCHY_INSTALL/config/snapper.sh"
run_logged "$OMARCHY_INSTALL/config/enable-services.sh"
run_logged "$OMARCHY_INSTALL/config/firewall.sh"
3 changes: 3 additions & 0 deletions install/config/binfmt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Add O and C to the QEMU binfmt registrations so setuid binaries keep their
# credentials in emulated cross-arch containers; see omarchy-apply-binfmt.
omarchy-apply-binfmt
3 changes: 3 additions & 0 deletions migrations/1791290450.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
echo "Add O and C to QEMU binfmt registrations so sudo works in emulated Docker cross-arch builds"

omarchy-apply-binfmt --check || sudo omarchy-apply-binfmt
173 changes: 173 additions & 0 deletions test/shell.d/binfmt-multiarch-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#!/bin/bash

set -euo pipefail

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

command="$ROOT/bin/omarchy-apply-binfmt"

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

source_dir="$test_tmp/lib-binfmt.d"
dest_dir="$test_tmp/etc-binfmt.d"
proc_dir="$test_tmp/binfmt_misc"
fake_bin="$test_tmp/bin"
calls="$test_tmp/calls.log"
mkdir -p "$source_dir" "$fake_bin"

cat >"$fake_bin/systemctl" <<'STUB'
#!/bin/bash
printf 'systemctl %s\n' "$*" >>"$TEST_LOG"
STUB
cat >"$fake_bin/sudo" <<'STUB'
#!/bin/bash
printf 'sudo %s\n' "$*" >>"$TEST_LOG"
exec "$@"
STUB
chmod +x "$fake_bin/systemctl" "$fake_bin/sudo"

write_conf() {
# Shaped like the qemu-user-static-binfmt package's own registrations:
# a magic/mask binfmt_misc line ending in the flags field.
printf ':qemu-%s:M::magic:mask:/usr/bin/qemu-%s-static:%s\n' "$1" "$1" "${3:-FP}" >"$2"
}

registration() {
printf ':qemu-%s:M::magic:mask:/usr/bin/qemu-%s-static:%s' "$1" "$1" "$2"
}

set_live() {
mkdir -p "$proc_dir"
: >"$proc_dir/status"
printf 'enabled\ninterpreter /usr/bin/qemu-%s-static\nflags: %s\n' "$1" "$2" >"$proc_dir/qemu-$1"
}

apply() {
TEST_LOG="$calls" \
PATH="$ROOT/bin:$fake_bin:$PATH" \
OMARCHY_BINFMT_SOURCE_DIR="$source_dir" \
OMARCHY_BINFMT_DIR="$dest_dir" \
OMARCHY_BINFMT_PROC_DIR="$proc_dir" \
"$@"
}

reset() {
rm -rf "$source_dir" "$dest_dir" "$proc_dir"
mkdir -p "$source_dir"
: >"$calls"
}

# Every qemu static registration gets O and C added to the package's own flags,
# with the rest of the line untouched, and anything else is ignored.
reset
write_conf aarch64 "$source_dir/qemu-aarch64-static.conf"
write_conf arm "$source_dir/qemu-arm-static.conf"
printf 'unrelated\n' >"$source_dir/other.conf"

apply "$command" --check && fail "--check reports pending registrations"
[[ ! -e $dest_dir ]] || fail "--check changes nothing"
apply "$command"

grep -qFx "$(registration aarch64 FPOC)" "$dest_dir/qemu-aarch64-static.conf" ||
fail "adds O and C to aarch64 flags without dropping FP"
grep -qFx "$(registration arm FPOC)" "$dest_dir/qemu-arm-static.conf" ||
fail "adds O and C to arm flags without dropping FP"
grep -q '^# Managed by omarchy-apply-binfmt ' "$dest_dir/qemu-aarch64-static.conf" ||
fail "marks overrides as managed"
[[ ! -e $dest_dir/other.conf ]] || fail "copies files that aren't qemu static registrations"
pass "adds O and C to every qemu static registration without dropping existing flags"

# Re-running neither rewrites nor piles up duplicate letters.
before=$(cat "$dest_dir"/*.conf)
apply "$command"
[[ $(cat "$dest_dir"/*.conf) == "$before" ]] || fail "is idempotent"
apply "$command" --check || fail "--check passes once applied"
pass "is idempotent and --check passes once applied"

# A registration that already carries O and C keeps its flags as they are.
reset
write_conf riscv64 "$source_dir/qemu-riscv64-static.conf" FPOCX
apply "$command"
grep -qFx "$(registration riscv64 FPOCX)" "$dest_dir/qemu-riscv64-static.conf" ||
fail "leaves flags that already include O and C alone"
pass "leaves flags that already include O and C alone"

# A package update that changes a registration, adds an architecture or drops
# one is followed, which a one-time copy would hide behind a stale override.
reset
write_conf aarch64 "$source_dir/qemu-aarch64-static.conf"
write_conf sparc "$source_dir/qemu-sparc-static.conf"
apply "$command"
sed -i 's/magic/magic2/' "$source_dir/qemu-aarch64-static.conf"
write_conf loongarch64 "$source_dir/qemu-loongarch64-static.conf"
rm "$source_dir/qemu-sparc-static.conf"
apply "$command"
grep -qFx ':qemu-aarch64:M::magic2:mask:/usr/bin/qemu-aarch64-static:FPOC' "$dest_dir/qemu-aarch64-static.conf" ||
fail "follows a changed registration"
grep -qFx "$(registration loongarch64 FPOC)" "$dest_dir/qemu-loongarch64-static.conf" ||
fail "covers an added architecture"
[[ ! -e $dest_dir/qemu-sparc-static.conf ]] || fail "removes the override of a dropped architecture"
pass "follows changed, added and dropped registrations"

# Administrator-authored overrides are never rewritten or removed.
reset
mkdir -p "$dest_dir"
write_conf aarch64 "$source_dir/qemu-aarch64-static.conf"
printf ':qemu-aarch64:M::magic:mask:/opt/qemu/qemu-aarch64:F\n' >"$dest_dir/qemu-aarch64-static.conf"
printf ':qemu-m68k:M::magic:mask:/opt/qemu/qemu-m68k:F\n' >"$dest_dir/qemu-m68k-static.conf"
apply "$command" 2>/dev/null
grep -qFx ':qemu-aarch64:M::magic:mask:/opt/qemu/qemu-aarch64:F' "$dest_dir/qemu-aarch64-static.conf" ||
fail "leaves an unmanaged override alone"
[[ -e $dest_dir/qemu-m68k-static.conf ]] || fail "leaves an unmanaged override without a registration alone"
pass "leaves administrator-authored overrides alone"

# An empty source directory, as before qemu-user-static-binfmt is installed.
reset
apply "$command" || fail "tolerates a source directory with no qemu registrations"
pass "tolerates a source directory with no qemu registrations"

# systemd-binfmt is restarted when overrides change, and when they are already
# on disk but the running registrations still carry the old flags.
reset
write_conf aarch64 "$source_dir/qemu-aarch64-static.conf"
set_live aarch64 PF
apply "$command"
grep -qFx 'systemctl restart systemd-binfmt.service' "$calls" || fail "restarts systemd-binfmt after writing overrides"

: >"$calls"
apply "$command"
grep -qFx 'systemctl restart systemd-binfmt.service' "$calls" ||
fail "restarts systemd-binfmt while running registrations lack O and C"

: >"$calls"
set_live aarch64 POCF
apply "$command"
[[ ! -s $calls ]] || fail "leaves systemd-binfmt alone once running registrations match"
pass "restarts systemd-binfmt only while running registrations differ"

# Without binfmt_misc mounted, as in the ISO's target chroot, overrides are
# still written and load at boot.
reset
write_conf aarch64 "$source_dir/qemu-aarch64-static.conf"
apply "$command"
[[ -e $dest_dir/qemu-aarch64-static.conf ]] || fail "writes overrides without binfmt_misc mounted"
[[ ! -s $calls ]] || fail "skips the restart without binfmt_misc mounted"
pass "writes overrides without restarting when binfmt_misc is not mounted"

# The migration escalates only when something is pending, so a second user on
# an already fixed machine is not prompted.
migration=$(grep -rl 'omarchy-apply-binfmt' "$ROOT/migrations" | head -n 1 || true)
[[ -n $migration ]] || fail "binfmt migration exists"

reset
write_conf aarch64 "$source_dir/qemu-aarch64-static.conf"
apply bash -euo pipefail "$migration" >/dev/null
grep -qFx "$(registration aarch64 FPOC)" "$dest_dir/qemu-aarch64-static.conf" ||
fail "binfmt migration applies pending overrides"
grep -q '^sudo omarchy-apply-binfmt$' "$calls" || fail "binfmt migration escalates to apply"

: >"$calls"
apply bash -euo pipefail "$migration" >/dev/null
[[ ! -s $calls ]] || fail "binfmt migration does not escalate once applied"
pass "binfmt migration escalates only when overrides are pending"
1 change: 1 addition & 0 deletions test/shell.d/config-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ if notify_alias not in pkgbuild:
alpm_hooks = [
"00-omarchy-update-guard.hook",
"10-omarchy-hyprland-reload-pause.hook",
"30-omarchy-binfmt.hook",
"90-omarchy-hyprland-reload-resume.hook",
]
for hook in alpm_hooks:
Expand Down