From 9d2dad5ee32bd7bac3da905f6dd69e2aba311f82 Mon Sep 17 00:00:00 2001 From: Danathar Date: Sun, 15 Nov 2026 16:08:53 -0500 Subject: [PATCH 1/2] fix(countme): read the booted image ref without running bootc projectbluefin-countme ran `bootc status --json` from an unconfined service. bootc's SELinux probe (chcon to a test label) was denied mac_admin, logging two AVC denials on every boot (projectbluefin/utah#1198). Read the ref from the booted deployment's .origin file (found through ostree= on the kernel command line), falling back to the boot-time snapshot /run/ublue-os/booted-image. Non-bootc hosts keep their image-tag fallback. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01WcWm7tJ5iop2b4GMkKSPAv Signed-off-by: Danathar --- .../system/projectbluefin-countme.service | 3 +- .../shared/usr/libexec/projectbluefin-countme | 35 +++++- tests/test_projectbluefin_countme.bats | 106 +++++++++++++++--- 3 files changed, 125 insertions(+), 19 deletions(-) diff --git a/system_files/shared/usr/lib/systemd/system/projectbluefin-countme.service b/system_files/shared/usr/lib/systemd/system/projectbluefin-countme.service index 0d3059a21..fca95d558 100644 --- a/system_files/shared/usr/lib/systemd/system/projectbluefin-countme.service +++ b/system_files/shared/usr/lib/systemd/system/projectbluefin-countme.service @@ -22,7 +22,8 @@ Type=simple ExecStart=/usr/libexec/projectbluefin-countme StateDirectory=projectbluefin-countme -# Run as root (no DynamicUser) so bootc status can query status. +# Run as root (no DynamicUser) so the script can read the booted deployment's +# .origin file under /sysroot. It does not run bootc (projectbluefin/utah#1198). # Hardened sandbox: PrivateTmp=yes ProtectSystem=strict diff --git a/system_files/shared/usr/libexec/projectbluefin-countme b/system_files/shared/usr/libexec/projectbluefin-countme index ddca356d1..492a4d8fa 100755 --- a/system_files/shared/usr/libexec/projectbluefin-countme +++ b/system_files/shared/usr/libexec/projectbluefin-countme @@ -20,10 +20,41 @@ esac [[ -e $last ]] && (( $(date +%s) - $(stat -c %Y "$last") < 86400 )) && exit 0 -# Stream comes from the booted bootc ref when one is available. Digest suffix +# Stream comes from the booted image ref when one is available. Digest suffix # is stripped; anything outside the known channels (`stable`, `testing`, # `latest`) becomes `unknown`. -ref=$(bootc status --json 2>/dev/null | jq -r '.status.booted.image.image.image // empty' 2>/dev/null) +# +# This never runs `bootc`. A bootc started from this service runs in +# unconfined_service_t, and its SELinux probe (a chcon to a test label) is +# denied mac_admin, logging two AVC denials every boot +# (projectbluefin/utah#1198). The ref is read from files instead: +# 1. the booted deployment's .origin keyfile, found through ostree= on the +# kernel command line; +# 2. the boot-time snapshot some images write (/run/ublue-os/booted-image), +# for hosts with no ostree= (composefs-booted). +booted_ref() { + local cmdline arg dir origin v + read -r cmdline < "${COUNTME_CMDLINE:-/proc/cmdline}" 2>/dev/null + for arg in $cmdline; do + [[ $arg == ostree=* ]] || continue + dir=$(readlink -f -- "${arg#ostree=}") || continue + origin="$dir.origin" + [[ -r $origin ]] || continue + v=$(sed -n 's/^container-image-reference=//p' "$origin" | head -n1) + # Drop the ostree transport, optional remote name and docker://. + v=${v#ostree-remote-registry:*:} + v=${v#ostree-remote-image:*:} + v=${v#ostree-unverified-registry:} + v=${v#ostree-unverified-image:} + v=${v#ostree-image-signed:} + v=${v#docker://} + if [[ -n $v ]]; then printf '%s\n' "$v"; return 0; fi + done + local snap="${UBLUE_BOOTED_IMAGE_FILE:-/run/ublue-os/booted-image}" + [[ -r $snap ]] && read -r v < "$snap" && [[ -n $v ]] && printf '%s\n' "$v" + return 0 +} +ref=$(booted_ref) tag="" if [[ -n $ref ]]; then ref=${ref%%@*} diff --git a/tests/test_projectbluefin_countme.bats b/tests/test_projectbluefin_countme.bats index cd5e5ca5e..3693ac7b0 100755 --- a/tests/test_projectbluefin_countme.bats +++ b/tests/test_projectbluefin_countme.bats @@ -16,17 +16,24 @@ setup() { echo "$@" >> "${TEST_TMPDIR}/curl.log" exit "${CURL_EXIT:-0}" EOF - # bootc prints BOOTED_REF as the booted image, or fails when it is unset. + # The script must never run bootc (#1198): a bootc started from this + # unconfined service is denied mac_admin by its SELinux probe. This stub + # leaves a marker, and teardown checks it is absent. cat << 'EOF' > "${MOCK_BIN}/bootc" #!/usr/bin/env bash -[[ -n "${BOOTED_REF:-}" ]] || exit 1 -printf '{"status":{"booted":{"image":{"image":{"image":"%s"}}}}}\n' "${BOOTED_REF}" +touch "${TEST_TMPDIR}/bootc.called" +exit 1 EOF chmod +x "${MOCK_BIN}/curl" "${MOCK_BIN}/bootc" + # No booted deployment and no snapshot unless a test creates one. + export COUNTME_CMDLINE="${TEST_TMPDIR}/cmdline" + export UBLUE_BOOTED_IMAGE_FILE="${TEST_TMPDIR}/booted-image" + echo "BOOT_IMAGE=/vmlinuz root=/dev/vda2" > "${COUNTME_CMDLINE}" export PATH="${MOCK_BIN}:${PATH}" } teardown() { + [ ! -e "${TEST_TMPDIR}/bootc.called" ] rm -rf "${TEST_TMPDIR}" } @@ -34,13 +41,26 @@ image_info() { printf '{"image-name":"%s","image-flavor":"%s","image-tag":"latest"}\n' "$1" "$2" > "${IMAGE_INFO}" } +# booted REF [ORIGIN_VALUE]: fake an ostree deployment whose .origin keyfile +# names REF, reached from ostree= on the kernel command line through a symlink +# like /ostree/boot.N///. +booted() { + local d="${TEST_TMPDIR}/deploy/abc123.0" + mkdir -p "$d" "${TEST_TMPDIR}/boot/default/abc123" + ln -sfn "$d" "${TEST_TMPDIR}/boot/default/abc123/0" + printf '[origin]\ncontainer-image-reference=%s\n' "${2:-ostree-unverified-registry:$1}" \ + > "${d}.origin" + echo "BOOT_IMAGE=/vmlinuz ostree=${TEST_TMPDIR}/boot/default/abc123/0 rw" > "${COUNTME_CMDLINE}" +} + sent_image() { grep -o '"image":"[^"]*"' "${TEST_TMPDIR}/curl.log" } @test "Dakota reports image, flavor, and the booted stream" { image_info dakota-nvidia nvidia - BOOTED_REF=ghcr.io/projectbluefin/dakota-nvidia:testing run "${SCRIPT}" + booted ghcr.io/projectbluefin/dakota-nvidia:testing + run "${SCRIPT}" [ "$status" -eq 0 ] [ "$(sent_image)" = '"image":"dakota-nvidia/nvidia:testing"' ] grep -q '/v1/ping' "${TEST_TMPDIR}/curl.log" @@ -49,25 +69,28 @@ sent_image() { @test "Utah is counted; a digest-pinned ref keeps its stream" { image_info utah gaming - BOOTED_REF='ghcr.io/projectbluefin/utah-gaming:stable@sha256:abc' run "${SCRIPT}" + booted 'ghcr.io/projectbluefin/utah-gaming:stable@sha256:abc' + run "${SCRIPT}" [ "$(sent_image)" = '"image":"utah/gaming:stable"' ] } @test "a tag that is not a stream is reported as unknown" { image_info dakota main - BOOTED_REF=ghcr.io/projectbluefin/dakota:next run "${SCRIPT}" + booted ghcr.io/projectbluefin/dakota:next + run "${SCRIPT}" [ "$(sent_image)" = '"image":"dakota/main:unknown"' ] } @test "latest from bootc is reported as a stream" { image_info dakota main - BOOTED_REF=ghcr.io/projectbluefin/dakota:latest run "${SCRIPT}" + booted ghcr.io/projectbluefin/dakota:latest + run "${SCRIPT}" [ "$(sent_image)" = '"image":"dakota/main:latest"' ] } @test "Server (DDI) reports its image-tag stream without bootc" { image_info server main - BOOTED_REF="" run "${SCRIPT}" + run "${SCRIPT}" [ "$status" -eq 0 ] [ "$(sent_image)" = '"image":"server/main:latest"' ] [ -e "${STATE_DIRECTORY}/last" ] @@ -75,7 +98,7 @@ sent_image() { @test "Server (DDI) skips when image-tag is empty" { printf '{"image-name":"server","image-flavor":"main"}\n' > "${IMAGE_INFO}" - BOOTED_REF="" run "${SCRIPT}" + run "${SCRIPT}" [ "$status" -eq 0 ] [ ! -e "${TEST_TMPDIR}/curl.log" ] [ ! -e "${STATE_DIRECTORY}/last" ] @@ -83,7 +106,7 @@ sent_image() { @test "Server (DDI) skips when image-name is not exactly server" { image_info server-foo main - BOOTED_REF="" run "${SCRIPT}" + run "${SCRIPT}" [ "$status" -eq 0 ] [ ! -e "${TEST_TMPDIR}/curl.log" ] } @@ -92,14 +115,14 @@ sent_image() { # dakota with no bootc image and a baked image-tag must not count — the # baked tag is the compose tag, not the stream the system actually runs. image_info dakota main - BOOTED_REF="" run "${SCRIPT}" + run "${SCRIPT}" [ "$status" -eq 0 ] [ ! -e "${TEST_TMPDIR}/curl.log" ] } @test "Server (DDI) reports a non-stream image-tag as unknown" { printf '{"image-name":"server","image-flavor":"main","image-tag":"20261007"}\n' > "${IMAGE_INFO}" - BOOTED_REF="" run "${SCRIPT}" + run "${SCRIPT}" [ "$status" -eq 0 ] [ "$(sent_image)" = '"image":"server/main:unknown"' ] } @@ -114,7 +137,8 @@ sent_image() { @test "Bluefin Classic and LTS never contact the service" { for name in bluefin bluefin-lts; do image_info "$name" main - BOOTED_REF="ghcr.io/ublue-os/${name}:stable" run "${SCRIPT}" + booted ghcr.io/ublue-os/${name}:stable + run "${SCRIPT}" [ "$status" -eq 0 ] done [ ! -e "${TEST_TMPDIR}/curl.log" ] @@ -131,17 +155,67 @@ sent_image() { @test "at most one report per 24 hours" { image_info dakota main touch -d '2 hours ago' "${STATE_DIRECTORY}/last" - BOOTED_REF=ghcr.io/projectbluefin/dakota:stable run "${SCRIPT}" + booted ghcr.io/projectbluefin/dakota:stable + run "${SCRIPT}" [ ! -e "${TEST_TMPDIR}/curl.log" ] touch -d '25 hours ago' "${STATE_DIRECTORY}/last" - BOOTED_REF=ghcr.io/projectbluefin/dakota:stable run "${SCRIPT}" + booted ghcr.io/projectbluefin/dakota:stable + run "${SCRIPT}" [ -e "${TEST_TMPDIR}/curl.log" ] } @test "a failed send exits 0 and is retried on the next run" { image_info dakota main - CURL_EXIT=22 BOOTED_REF=ghcr.io/projectbluefin/dakota:stable run "${SCRIPT}" + booted ghcr.io/projectbluefin/dakota:stable + CURL_EXIT=22 run "${SCRIPT}" [ "$status" -eq 0 ] [ ! -e "${STATE_DIRECTORY}/last" ] } + +@test "the ref comes from the .origin file for every transport form" { + image_info dakota main + for form in \ + 'ostree-image-signed:docker://ghcr.io/projectbluefin/dakota:stable' \ + 'ostree-unverified-image:docker://ghcr.io/projectbluefin/dakota:stable' \ + 'ostree-remote-registry:fedora:ghcr.io/projectbluefin/dakota:stable' \ + 'ostree-remote-image:fedora:docker://ghcr.io/projectbluefin/dakota:stable' \ + 'ostree-unverified-registry:ghcr.io/projectbluefin/dakota:stable'; do + rm -f "${TEST_TMPDIR}/curl.log" "${STATE_DIRECTORY}/last" + booted x "$form" + run "${SCRIPT}" + [ "$status" -eq 0 ] + [ "$(sent_image)" = '"image":"dakota/main:stable"' ] + done +} + +@test "a registry port in the ref does not become the stream" { + image_info dakota main + booted localhost:5000/dakota + run "${SCRIPT}" + [ "$(sent_image)" = '"image":"dakota/main:unknown"' ] +} + +@test "without an ostree= deployment, the boot-time snapshot supplies the ref" { + image_info dakota main + echo 'ghcr.io/projectbluefin/dakota:testing' > "${UBLUE_BOOTED_IMAGE_FILE}" + run "${SCRIPT}" + [ "$(sent_image)" = '"image":"dakota/main:testing"' ] +} + +@test "the .origin file wins over the snapshot" { + image_info dakota main + echo 'ghcr.io/projectbluefin/dakota:testing' > "${UBLUE_BOOTED_IMAGE_FILE}" + booted ghcr.io/projectbluefin/dakota:stable + run "${SCRIPT}" + [ "$(sent_image)" = '"image":"dakota/main:stable"' ] +} + +@test "an ostree= path with no .origin file is treated as not booted" { + image_info dakota main + booted ghcr.io/projectbluefin/dakota:stable + rm "${TEST_TMPDIR}"/deploy/abc123.0.origin + run "${SCRIPT}" + [ "$status" -eq 0 ] + [ ! -e "${TEST_TMPDIR}/curl.log" ] +} From 9460399a87e75a570fe8eac9789cade14e1949cf Mon Sep 17 00:00:00 2001 From: Danathar Date: Sun, 15 Nov 2026 16:10:13 -0500 Subject: [PATCH 2/2] fix(countme): handle composefs hosts like utah's booted-image-sync Follow the composefs=/composefs.digest= command-line arguments to /sysroot/state/deploy//.origin, the same lookup utah#1220 uses, and avoid a stray error when the snapshot file is missing. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01WcWm7tJ5iop2b4GMkKSPAv Signed-off-by: Danathar --- .../shared/usr/libexec/projectbluefin-countme | 43 +++++++++++-------- tests/test_projectbluefin_countme.bats | 15 +++++++ 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/system_files/shared/usr/libexec/projectbluefin-countme b/system_files/shared/usr/libexec/projectbluefin-countme index 492a4d8fa..67fb8de4d 100755 --- a/system_files/shared/usr/libexec/projectbluefin-countme +++ b/system_files/shared/usr/libexec/projectbluefin-countme @@ -28,31 +28,40 @@ esac # unconfined_service_t, and its SELinux probe (a chcon to a test label) is # denied mac_admin, logging two AVC denials every boot # (projectbluefin/utah#1198). The ref is read from files instead: -# 1. the booted deployment's .origin keyfile, found through ostree= on the -# kernel command line; -# 2. the boot-time snapshot some images write (/run/ublue-os/booted-image), -# for hosts with no ostree= (composefs-booted). +# 1. the booted deployment's .origin keyfile: found through ostree= on the +# kernel command line, or through composefs= / +# composefs.digest=: for composefs-booted hosts (the same +# lookup as utah's ublue-booted-image-sync); +# 2. the boot-time snapshot some images write (/run/ublue-os/booted-image). booted_ref() { - local cmdline arg dir origin v - read -r cmdline < "${COUNTME_CMDLINE:-/proc/cmdline}" 2>/dev/null + local cmdline arg id snap origin="" v="" + read -r cmdline < "${COUNTME_CMDLINE:-/proc/cmdline}" || cmdline="" for arg in $cmdline; do - [[ $arg == ostree=* ]] || continue - dir=$(readlink -f -- "${arg#ostree=}") || continue - origin="$dir.origin" - [[ -r $origin ]] || continue - v=$(sed -n 's/^container-image-reference=//p' "$origin" | head -n1) - # Drop the ostree transport, optional remote name and docker://. + case $arg in + ostree=*) + origin="$(readlink -f -- "${arg#ostree=}").origin" ;; + composefs=*|composefs.digest=*) + id=${arg#*=}; id=${id##*:} + origin="${COUNTME_SYSROOT:-/sysroot}/state/deploy/$id/$id.origin" ;; + esac + [[ -n $origin ]] && break + done + if [[ -n $origin && -r $origin ]]; then + v=$(sed -n 's/^[[:space:]]*container-image-reference[[:space:]]*=[[:space:]]*//p' "$origin" | head -n1) + # Drop the transport: ostree-image-signed:docker://, ostree- + # unverified-registry:, ostree-remote-image::docker://. v=${v#ostree-remote-registry:*:} v=${v#ostree-remote-image:*:} v=${v#ostree-unverified-registry:} v=${v#ostree-unverified-image:} v=${v#ostree-image-signed:} v=${v#docker://} - if [[ -n $v ]]; then printf '%s\n' "$v"; return 0; fi - done - local snap="${UBLUE_BOOTED_IMAGE_FILE:-/run/ublue-os/booted-image}" - [[ -r $snap ]] && read -r v < "$snap" && [[ -n $v ]] && printf '%s\n' "$v" - return 0 + fi + if [[ -z $v ]]; then + snap="${UBLUE_BOOTED_IMAGE_FILE:-/run/ublue-os/booted-image}" + [[ -r $snap ]] && read -r v < "$snap" + fi + printf '%s\n' "$v" } ref=$(booted_ref) tag="" diff --git a/tests/test_projectbluefin_countme.bats b/tests/test_projectbluefin_countme.bats index 3693ac7b0..3516e918a 100755 --- a/tests/test_projectbluefin_countme.bats +++ b/tests/test_projectbluefin_countme.bats @@ -219,3 +219,18 @@ sent_image() { [ "$status" -eq 0 ] [ ! -e "${TEST_TMPDIR}/curl.log" ] } + +@test "composefs hosts read the .origin file from /sysroot/state/deploy" { + image_info dakota main + export COUNTME_SYSROOT="${TEST_TMPDIR}/sysroot" + mkdir -p "${COUNTME_SYSROOT}/state/deploy/abc123" + printf '[origin]\ncontainer-image-reference=ostree-unverified-registry:ghcr.io/projectbluefin/dakota:testing\n' \ + > "${COUNTME_SYSROOT}/state/deploy/abc123/abc123.origin" + for arg in composefs=abc123 composefs.digest=sha512:abc123; do + rm -f "${TEST_TMPDIR}/curl.log" "${STATE_DIRECTORY}/last" + echo "BOOT_IMAGE=/vmlinuz ${arg} rw" > "${COUNTME_CMDLINE}" + run "${SCRIPT}" + [ "$status" -eq 0 ] + [ "$(sent_image)" = '"image":"dakota/main:testing"' ] + done +}