From 67ef66552e79a1225d1b4bbb9291842f8104642e Mon Sep 17 00:00:00 2001 From: "hivecommons-hive[bot]" <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Sun, 25 Oct 2026 01:41:30 +0000 Subject: [PATCH 1/5] fix(selinux): move the policy store to /etc/selinux so persistent changes work (#874) * fix(selinux): move the policy store to /etc/selinux so persistent changes work On Utah the SELinux module store inherited from the Hummingbird base lives under /usr/lib/selinux, which is read-only on a booted system. As a result setsebool -P, semanage and semodule all fail, so users cannot make any lasting SELinux change the way they can on Bluefin. Move the store to /etc/selinux at build time, where it is writable and preserved across updates by the normal /etc three-way merge, and point semanage.conf at the new location. ostree already refreshes the policy from /etc/selinux when it finalizes a staged deployment, so local changes survive updates and rollbacks without any extra tooling. - scripts/relocate-selinux-store.sh moves the store during the image build, fails loudly on anything unexpected and is a no-op if the store is already in place. - iso/scripts/verify-selinux-store.sh is a guest-side checker that applies sample changes (boolean, fcontext, port, CIL module), verifies them after an update and removes them again. - iso/scripts/lifecycle-e2e.sh runs the checker before and after the staged update so `just lifecycle-test` produces the booted evidence the maintainers asked for. - README and skill docs describe the new behaviour and recovery steps. Fixes #551 Hive-Run: projectbluefin/utah#551 Hive-Plan: issue-551-option-2 Hive-Spec: utah-persistent-selinux#option-2 Assisted-by: Claude Fable 5.1 via GitHub Copilot Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: mrbobbytables * fix(selinux): carry libsemanage's store locks along with the relocated store The full image matrix failed at utah-relocate-selinux-store because /usr/lib/selinux/targeted still held semanage.read.LOCK and semanage.trans.LOCK after the move. libsemanage keeps its store locks at // and the package transaction that runs before the relocation takes them, so they are present at build time even though the pinned Hummingbird base ships only targeted/active and an empty final/. Move the two lock files into /etc/selinux/targeted with the rest of the store (dropping an empty old one if /etc already has the lock), and keep failing on anything else left behind. The scratch-tree fixture now includes the locks; two tests cover a base without them and a pre-existing lock under /etc. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * test(selinux): lifecycle rejects an invalid local module without disturbing policy state Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * docs(local-testing): condense SELinux store paragraph to fit the 500-line limit * docs(local-testing): keep skill page under 500-line hard max Reflow the BLS loader-entries paragraph and move the SELinux store paragraph after the boot-files paragraph it had split, bringing the page back to 500 lines so check-skill-frontmatter passes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --------- Signed-off-by: mrbobbytables Signed-off-by: Danathar <6772335+Danathar@users.noreply.github.com> Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Co-authored-by: mrbobbytables Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Danathar <6772335+Danathar@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 Co-authored-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- Containerfile | 7 + Justfile | 5 + README.md | 40 ++++ docs/skills/containerfile.md | 41 +++++ docs/skills/index.json | 4 +- docs/skills/local-testing.md | 66 +++---- docs/skills/setup-hooks.md | 9 +- iso/scripts/lifecycle-e2e.sh | 69 ++++++- iso/scripts/verify-selinux-store.sh | 227 +++++++++++++++++++++++ scripts/relocate-selinux-store.sh | 110 +++++++++++ tests/test_selinux_store.py | 271 ++++++++++++++++++++++++++++ 11 files changed, 811 insertions(+), 38 deletions(-) create mode 100644 iso/scripts/verify-selinux-store.sh create mode 100644 scripts/relocate-selinux-store.sh create mode 100644 tests/test_selinux_store.py diff --git a/Containerfile b/Containerfile index 559862be9..7ba4ffc0b 100644 --- a/Containerfile +++ b/Containerfile @@ -93,6 +93,7 @@ COPY scripts/install-packages.py \ scripts/mirror-shim.sh \ scripts/verify-efi-chain.sh \ scripts/fix-home-labels.sh \ + scripts/relocate-selinux-store.sh \ scripts/regenerate-initramfs.sh \ iso/scripts/live-kernel.py \ scripts/install-v4l2loopback.sh \ @@ -209,6 +210,7 @@ RUN --mount=type=bind,from=v4l2loopback,source=/out,target=/tmp/utah-v4l2loopbac mirror-shim.sh:utah-mirror-shim \ verify-efi-chain.sh:utah-verify-efi-chain \ fix-home-labels.sh:utah-fix-home-labels \ + relocate-selinux-store.sh:utah-relocate-selinux-store \ regenerate-initramfs.sh:utah-regenerate-initramfs \ live-kernel.py:utah-live-kernel \ install-v4l2loopback.sh:utah-install-v4l2loopback \ @@ -278,6 +280,11 @@ RUN --mount=type=bind,from=packages,source=/repository,target=/etc/utah-packages /usr/share/utah/bluefin.toml /usr/share/utah/utah.toml && \ IMAGE_FLAVOR=main /usr/local/libexec/utah-verify-rpm-contract \ /usr/share/utah/bluefin.toml /usr/share/utah/utah.toml && \ + # The SELinux module store moves from Hummingbird's read-only + # /usr/lib/selinux to /etc/selinux so setsebool -P, semanage and semodule + # work on a booted system (#551). It runs before the home-label rebuild + # below so the rebuilt policy lands in the store's final location. + /usr/local/libexec/utah-relocate-selinux-store && \ /usr/local/libexec/utah-fix-home-labels && \ DNF="$(command -v dnf5 || command -v dnf)" && \ "$DNF" clean all && rm -rf /var/cache/libdnf5 /var/cache/dnf && \ diff --git a/Justfile b/Justfile index 35d0b7e9b..4d42e304e 100644 --- a/Justfile +++ b/Justfile @@ -67,6 +67,11 @@ check: grep -q 'serial-getty@ttyS0.service' contracts/bluefin-desktop.toml test -f scripts/configure-services.sh test -f scripts/configure-branding.sh + test -f scripts/relocate-selinux-store.sh + test -f iso/scripts/verify-selinux-store.sh + # The SELinux module store must leave Hummingbird's read-only + # /usr/lib/selinux before the home-label policy rebuild (#551). + grep -q '/usr/local/libexec/utah-relocate-selinux-store' Containerfile test -f scripts/verify-desktop-contract.py test -f scripts/verify-gnome-extensions.py test -f scripts/mirror-shim.sh diff --git a/README.md b/README.md index af5201e88..6b43ad60c 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,46 @@ Why this is still pre-alpha: See the [open issues](https://github.com/projectbluefin/utah/issues) to track progress or report bugs. +## Persistent SELinux changes + +SELinux stays enforcing on Utah, and lasting policy changes are made with the +same commands as on Bluefin: `sudo setsebool -P` for a boolean, +`sudo semanage fcontext` for a file-labelling rule, `sudo semanage port` for a +port label, and `sudo semodule -i` for a local module built with `audit2allow`. +Each of those survives a reboot. + +Hummingbird's base image keeps the SELinux policy store under `/usr`, which is +read-only on a running system, so all of those commands used to fail — the +first of them silently. Utah moves the store to `/etc/selinux`, where Fedora +atomic and Bluefin keep it (links #551). + +What happens on an update: the system rebuilds the policy in the new +deployment from the new image's policy plus your local changes, so both take +effect after the reboot. Nothing you changed is lost, and new policy shipped +in the image still reaches your machine. + +One case needs attention: a local module the new image's policy can no longer +satisfy (for example, one that refers to a type the new policy removed) stops +that update from being applied. The machine simply keeps running the image it +had; nothing breaks. To recognise it, `bootc status` still shows the old image +after a reboot, and the log of the step that finalises updates names the +failed policy rebuild: + +```bash +journalctl -b -1 -u ostree-finalize-staged.service +``` + +To recover, remove the module (`sudo semodule -r `) and update again. +If you would rather discard every local SELinux change, restore the image's +own copy of the store and reboot: + +```bash +sudo mv /etc/selinux /etc/selinux.local && sudo cp -a /usr/etc/selinux /etc/selinux +``` + +Changes carried in from another image (for example, a machine switched to +Utah from Bluefin) are kept, but are not promised to take effect on Utah. + ## Contributing See [docs/building.md](docs/building.md) to build locally, [docs/skills/](docs/skills/) for architecture docs, and [AGENTS.md](AGENTS.md) for agent rules. diff --git a/docs/skills/containerfile.md b/docs/skills/containerfile.md index 82c80b4f3..8fc67c24f 100644 --- a/docs/skills/containerfile.md +++ b/docs/skills/containerfile.md @@ -233,6 +233,47 @@ promotes the OGC kernel for gaming flavors, rebuilds that release's initramfs, and verifies early microcode plus the installed TPM/passkey/smart-card unlock modules before the image can commit. +## The SELinux module store lives under `/etc/selinux` + +Hummingbird's base relocates the libsemanage store to `/usr/lib/selinux` and +appends `store-root=/usr/lib/selinux` to `/etc/selinux/semanage.conf`, so +the store is image-managed and immutable. On a booted bootc system `/usr` is +read-only, so every tool that writes -- or even locks -- the store fails: +`setsebool -P` exits 255 with no message, `semanage` and `semodule -l` cannot +take the read lock. Users could not persist a boolean, add a file context or +port label, or load a local module, all of which work on Bluefin (#551). + +`utah-relocate-selinux-store` (`scripts/relocate-selinux-store.sh`) moves +`targeted/active` and `final` to `/etc/selinux` and rewrites `store-root`, +the layout Fedora atomic and Bluefin use. It runs inside the package +transaction RUN, immediately before `utah-fix-home-labels`, so that script's +`semodule -n -B` rebuilds into the store's final location. The compiled +policy the kernel loads was always at `/etc/selinux/targeted/policy`; only +the module store moves. The script fails the build rather than guess when +`semanage.conf` names a different root or anything other than those two +entries remains under `/usr/lib/selinux`, and is a no-op when the store is +already under `/etc` (`tests/test_selinux_store.py`). + +Why updates still deliver policy: ostree rebuilds the policy in every new +deployment after the 3-way `/etc` merge (`semodule -N --refresh` in +`ostree-finalize-staged`, ostree >= 2022.3, ostreedev/ostree#2569). The merge +carries the machine's local modules, booleans, file contexts and port labels +forward and the refresh combines them with the new image's modules. The +trade-off accepted in #551: about 18 MB and some 1,300 files join the `/etc` +merge, and a local module the new policy cannot resolve fails the refresh, +so ostree leaves the machine on its current deployment -- `bootc status` +keeps showing the old image and `journalctl -b -1 -u +ostree-finalize-staged.service` names the failure. Recovery is +`semodule -r ` or restoring the pristine store from +`/usr/etc/selinux`; both are documented for users in `README.md`. + +The lifecycle harness proves this on a booted guest +(`iso/scripts/verify-selinux-store.sh`, see `local-testing.md`). A +`switch` from a pre-#551 image, whose `/etc/selinux` has no `active/`, +takes the new image's store whole; a machine switched in from Bluefin +carries whatever store files it changed there, which become live again at +its first update -- they are kept, not promised to work. + ## Clean and lint share a layer Everything above writes build-time residue that bootc lint rejects: dnf logs diff --git a/docs/skills/index.json b/docs/skills/index.json index 50c70de02..9dbd682da 100644 --- a/docs/skills/index.json +++ b/docs/skills/index.json @@ -143,7 +143,7 @@ ], "description": "Local validation loop: build-ghcr, bootc install to-disk, QEMU/noVNC boot, live ISO boot paths, and Secure Boot strategy. Use when validating changes end-to-end or debugging boot, GDM, or live-session failures.", "version": "1.0", - "last_updated": "2026-10-11", + "last_updated": "2026-10-13", "doc_type": "runbook" }, { @@ -180,7 +180,7 @@ ], "description": "First-boot setup hooks stamp completion through libsetup.sh. Use when editing a hook, migrating one off the legacy `version-script` gate, or debugging a hook that never runs or runs every boot.", "version": "1.0", - "last_updated": "2026-10-12", + "last_updated": "2026-10-13", "doc_type": "procedure" }, { diff --git a/docs/skills/local-testing.md b/docs/skills/local-testing.md index 1fb79b1f0..e81051d62 100644 --- a/docs/skills/local-testing.md +++ b/docs/skills/local-testing.md @@ -1,7 +1,7 @@ --- name: local-testing version: "1.0" -last_updated: "2026-10-11" +last_updated: "2026-10-13" id: local-testing one_line_purpose: Build, install, and boot Utah locally in a VM or live ISO. entry_point: docs/skills/local-testing.md @@ -387,38 +387,33 @@ baseline digest. Phase-keyed diagnostics (`evidence/lifecycle-*.json`, `lifecycle-summary.json`) and screendumps identify the active deployment and digest at every phase. -After each lifecycle phase the harness also captures the BLS Type #1 -entries under the Fedora/bootc layout's BLS root `/boot/loader/entries/` -(ostree writes them here per the sysroot deploy) and any -`/boot/efi/loader/entries/` if present, then runs `validate-bootmgr` -against them. -This is the surface that an `ostree-finalize-staged` regression could -silently leave behind: `bootc status` would still report the new -deployment as queued, but the boot manager would have no entry to chain -to it and the next reboot would boot the old kernel set. The validator -parses the `ostree=/ostree/boot.N///` path -from each BLS entry's `options` line, groups both the expected -deployments and the captured entries by their `(stateroot, -deploy_serial)` tuple, and requires the count of entries in each group -to be at least the count of deployments. A purely-by-serial match would -let one BLS entry satisfy two deployments (ostree allocates -`deployserial` per `(osname, commit)`, so two commits with no prior -deployment at that commit both receive serial 0) and silently miss a -missing-entry regression; the count check forces a failure in that -case. The `` segment in the path is the kernel+initramfs -layout hash (ostree's `ostree_deployment_get_bootcsum`), NOT the -commit checksum the deployment exposes as `ostree.checksum`, and is -intentionally not used as a match key -- bootc's `BootEntryOstree` -JSON does not expose it. Phase 3 (post-upgrade) and phase 4 -(post-rollback) both check the `booted` and `rollback` slots so a -missing entry for the non-booted slot is caught even though the guest -necessarily booted from the `booted` entry that already exists. The -validator then confirms each matched entry carries a `linux` line and -at least one of `initrd` or `options`. Evidence is written to +After each lifecycle phase the harness also captures the BLS Type #1 entries +under the Fedora/bootc layout's BLS root `/boot/loader/entries/` (ostree writes +them here per the sysroot deploy) and any `/boot/efi/loader/entries/` if +present, then runs `validate-bootmgr` against them. This is the surface that an +`ostree-finalize-staged` regression could silently leave behind: `bootc status` +would still report the new deployment as queued, but the boot manager would have +no entry to chain to it and the next reboot would boot the old kernel set. The +validator parses the `ostree=/ostree/boot.N///` +path from each BLS entry's `options` line, groups both the expected deployments +and the captured entries by their `(stateroot, deploy_serial)` tuple, and +requires the count of entries in each group to be at least the count of +deployments. A purely-by-serial match would let one BLS entry satisfy two +deployments (ostree allocates `deployserial` per `(osname, commit)`, so two +commits with no prior deployment at that commit both receive serial 0) and +silently miss a missing-entry regression; the count check forces a failure in +that case. The `` segment in the path is the kernel+initramfs layout +hash (ostree's `ostree_deployment_get_bootcsum`), NOT the commit checksum the +deployment exposes as `ostree.checksum`, and is intentionally not used as a +match key -- bootc's `BootEntryOstree` JSON does not expose it. Phase 3 +(post-upgrade) and phase 4 (post-rollback) both check the `booted` and +`rollback` slots so a missing entry for the non-booted slot is caught even +though the guest necessarily booted from the `booted` entry that already exists. +The validator then confirms each matched entry carries a `linux` line and at +least one of `initrd` or `options`. Evidence is written to `evidence/loader-entries-.txt` (the raw BLS listing) and -`evidence/bootmgr-.json` (which slot matched which entry, -which slots had no entry, and any entries whose `ostree=` karg did not -parse). +`evidence/bootmgr-.json` (which slot matched which entry, which slots had +no entry, and any entries whose `ostree=` karg did not parse). Each phase also runs `iso/scripts/verify-boot-files.sh` as root in the guest and saves `evidence/boot-files-.txt`. For every published OSTree BLS @@ -433,6 +428,13 @@ This file gate complements deployment-to-entry matching (issue #361 / PR #571); it does not itself prove which deployment an entry represents. Fixture tests run the same guest checker via `python3 -m unittest discover -s tests -p test_boot_files.py`. +Phases 1 and 3 also run `iso/scripts/verify-selinux-store.sh` in the guest (#551; +evidence `selinux--.txt`). `probe` reports whether the store is under +`/etc/selinux` (`persistent`) or `/usr` (`immutable`). Phase 1 makes sample local +changes; Phase 3 checks they survive the reboot along with the new image's policy, +`/var/home` labels and `Enforcing`, then `reject` offers an invalid CIL module and +`remove` cleans up. Mechanism: `containerfile.md`. + The baseline image (default `ghcr.io/projectbluefin/utah:testing`, the target ref `just iso testing` builds with) is the ref a live ISO installs its offline payload under, and phase 1 fails unless the booted deployment tracks it. The diff --git a/docs/skills/setup-hooks.md b/docs/skills/setup-hooks.md index 32a20e4b8..d6692cd83 100644 --- a/docs/skills/setup-hooks.md +++ b/docs/skills/setup-hooks.md @@ -1,7 +1,7 @@ --- name: setup-hooks version: "1.0" -last_updated: "2026-10-12" +last_updated: "2026-10-13" id: setup-hooks one_line_purpose: Change a first-boot setup hook without breaking the once-only contract. entry_point: docs/skills/setup-hooks.md @@ -132,7 +132,12 @@ Bluefin do (`scripts/fix-home-labels.sh`). ostree rebuilds the SELinux policy the `/etc` merge, so `20-home-labels.sh` rewrites a lingering exact `HOME=/var/home` to `HOME=/home` and warns about any other non-`/home` value. `utah-fix-home-labels --check` at the image's last step repeats that -rebuild and fails the build if `file_contexts.homedirs` changes. Build-time +rebuild and fails the build if `file_contexts.homedirs` changes. Since #551 +the module store that rebuild reads lives under `/etc/selinux` rather than +Hummingbird's `/usr/lib/selinux` (`utah-relocate-selinux-store`, see +`containerfile.md`), so a user's own `semodule`/`semanage` changes take part +in the same rebuild; the home-rule invariant above is what keeps those +rebuilds from re-keying `/var/home`. Build-time `useradd -m` must name `/var/home/` explicitly: `/var/home` does not exist in a container, so the `/home` symlink dangles (#281). diff --git a/iso/scripts/lifecycle-e2e.sh b/iso/scripts/lifecycle-e2e.sh index 2dd3b64f2..c36c2d9af 100755 --- a/iso/scripts/lifecycle-e2e.sh +++ b/iso/scripts/lifecycle-e2e.sh @@ -2,9 +2,11 @@ # Validate bootc upgrade and rollback lifecycle in QEMU. # # Lifecycle Phases: -# 1. Boot a known Utah deployment, verify desktop session and baseline digest +# 1. Boot a known Utah deployment, verify desktop session and baseline digest; +# when its SELinux store is writable, make four persistent SELinux changes # 2. Stage upgrade to a candidate digest using bootc / uupd policy -# 3. Reboot into the upgraded deployment, verify graphical desktop and identity +# 3. Reboot into the upgraded deployment, verify graphical desktop and identity, +# and that persistent SELinux changes work and survived the update (#551) # 4. Roll back to the previous deployment and reboot # 5. Verify the restored deployment boots successfully to graphical desktop # @@ -164,6 +166,27 @@ verify_boot_files() { || diagnose_failure "Kernel/initrd file validation failed (${phase}); see boot-files-${phase}.txt" } +# Persistent SELinux changes (#551): the candidate must keep its policy store +# under writable /etc/selinux, and ostree's policy rebuild in the new +# deployment must carry local changes forward while the image's own policy +# still takes effect. Modes: probe, apply, verify, reject, remove; stdout and stderr +# land in evidence/selinux--.txt. Returns the checker's status. +selinux_check() { + local phase="$1" mode="$2" checker + checker="$(< "${ROOT}/iso/scripts/verify-selinux-store.sh")" + ssh_target_sudo bash -c "${checker}" utah-selinux "${mode}" \ + > "${EVIDENCE}/selinux-${mode}-${phase}.txt" 2>&1 +} + +# Prints `persistent` or `immutable`. Never calls diagnose_failure itself: +# callers run it in a command substitution, where the report would be +# swallowed, so they test its status instead. +selinux_probe() { + local phase="$1" + selinux_check "${phase}" probe || return 1 + tail -n 1 "${EVIDENCE}/selinux-probe-${phase}.txt" +} + lifecycle_helper() { python3 "${ROOT}/scripts/bootc_lifecycle.py" "$@" } @@ -572,6 +595,23 @@ verify_boot_files baseline verify_bootmgr_entries "${WORK}/baseline-status.json" baseline booted +# A baseline that already keeps its store under /etc gets the four sample +# changes now, so Phase 3 proves they survive the update. A baseline from +# before the store moved cannot persist anything; Phase 3 then proves the +# update alone makes it possible (a machine installed earlier gains it by +# updating, no reinstall or migration step). +SELINUX_LOCAL_CHANGES=0 +BASELINE_SELINUX="$(selinux_probe baseline)" \ + || diagnose_failure "Could not probe the SELinux store layout on the baseline deployment; see selinux-probe-baseline.txt" +if [[ "${BASELINE_SELINUX}" == "persistent" ]]; then + selinux_check baseline apply \ + || diagnose_failure "Persistent SELinux changes failed on the baseline deployment; see selinux-apply-baseline.txt" + SELINUX_LOCAL_CHANGES=1 + echo " [Phase 1] SELinux: four local policy changes applied; they must survive the upgrade" +else + echo " [Phase 1] SELinux: baseline keeps its policy store under read-only /usr; the upgrade must make persistent changes possible" +fi + python3 "${ROOT}/scripts/bootc_lifecycle.py" record-diagnostics \ --output-dir "${EVIDENCE}" \ --phase baseline \ @@ -683,6 +723,31 @@ verify_boot_files upgraded # necessarily exists since the guest booted from it. verify_bootmgr_entries "${WORK}/upgraded-status.json" upgraded "booted,rollback" +# The candidate must make persistent SELinux changes work. With changes made +# in Phase 1 they must all be in effect after the reboot, together with the +# new image's policy; otherwise the tools must work for the first time here. +# Removal exercises the fourth tool path and leaves the deployment clean. +UPGRADED_SELINUX="$(selinux_probe upgraded)" \ + || diagnose_failure "Could not probe the SELinux store layout on the upgraded deployment; see selinux-probe-upgraded.txt" +[[ "${UPGRADED_SELINUX}" == "persistent" ]] \ + || diagnose_failure "Upgraded deployment keeps its SELinux policy store under read-only /usr; setsebool -P, semanage and semodule cannot work (#551)" +if [[ "${SELINUX_LOCAL_CHANGES}" == 1 ]]; then + selinux_check upgraded verify \ + || diagnose_failure "Local SELinux changes made before the upgrade are not in effect after it, or the image's policy is not; see selinux-verify-upgraded.txt" + echo " [Phase 3] SELinux: local changes survived the upgrade alongside the image's policy" +else + selinux_check upgraded apply \ + || diagnose_failure "Persistent SELinux changes failed on the upgraded deployment; see selinux-apply-upgraded.txt" + echo " [Phase 3] SELinux: persistent changes work on the upgraded deployment" +fi +# A module the policy compiler refuses must leave the store, the running +# policy, the local changes and the labels untouched. +selinux_check upgraded reject \ + || diagnose_failure "A rejected local SELinux module changed the policy state on the upgraded deployment; see selinux-reject-upgraded.txt" +echo " [Phase 3] SELinux: invalid local module rejected without disturbing the policy" +selinux_check upgraded remove \ + || diagnose_failure "Removing the local SELinux changes failed on the upgraded deployment; see selinux-remove-upgraded.txt" + python3 "${ROOT}/scripts/bootc_lifecycle.py" record-diagnostics \ --output-dir "${EVIDENCE}" \ --phase upgraded \ diff --git a/iso/scripts/verify-selinux-store.sh b/iso/scripts/verify-selinux-store.sh new file mode 100644 index 000000000..1d9a33d5c --- /dev/null +++ b/iso/scripts/verify-selinux-store.sh @@ -0,0 +1,227 @@ +#!/usr/bin/env bash +# Run as root in a booted Utah guest by iso/scripts/lifecycle-e2e.sh: prove +# that persistent SELinux changes work and survive an update (#551). +# +# Four kinds of local change stand in for everything the standard tools can +# persist: a boolean (`setsebool -P`), a file-context rule (`semanage +# fcontext`), a port label (`semanage port`) and a local module (`semodule +# -i`). The harness applies them before an update and verifies them after +# the reboot into the new deployment, then removes them. +# +# probe print `persistent` when the module store lives under writable +# /etc/selinux, `immutable` when it is still under /usr. +# apply make the four sample changes; fail if any tool fails or the +# change is not in effect afterwards. +# verify the four changes are in effect, the store is listable, SELinux +# is enforcing, home directories keep their labels, and every +# type and module of the image's own policy is part of the +# running policy (a local change must not hold the policy back). +# reject offer semodule a module it must refuse (invalid CIL); the install +# must fail and leave the store, the running policy and the local +# changes exactly as they were, so the machine stays bootable. +# remove undo the four changes and confirm none is left in effect. +# +# The three paths can be overridden for host-side fixture tests; nothing on +# the host is touched when they point into a scratch tree. +set -euo pipefail + +mode="${1:-}" +SEMANAGE_CONF="${UTAH_SEMANAGE_CONF:-/etc/selinux/semanage.conf}" +PRISTINE_STORE="${UTAH_PRISTINE_STORE:-/usr/etc/selinux/targeted}" +HOME_ROOT="${UTAH_HOME_ROOT:-/var/home}" + +BOOLEAN=virt_use_samba +FCONTEXT_DIR=/srv/utah-e2e +FCONTEXT_RULE="${FCONTEXT_DIR}(/.*)?" +FCONTEXT_TYPE=httpd_sys_content_t +PORT=18081 +PORT_TYPE=http_port_t +MODULE=utah_e2e +MODULE_TYPE=utah_e2e_t +BAD_MODULE=utah_e2e_bad + +fail() { + echo "FAIL: $*" >&2 + exit 1 +} + +run() { + echo "+ $*" + "$@" || fail "'$*' exited $?" +} + +store_is_persistent() { + grep -qx 'store-root=/etc/selinux' "$SEMANAGE_CONF" +} + +boolean_is() { + [[ "$(getsebool "$BOOLEAN")" == *"--> $1" ]] +} + +fcontext_listed() { + local rules + rules=$(semanage fcontext -l -C | grep -F -- "$FCONTEXT_RULE" || true) + [[ "$rules" == *"$FCONTEXT_TYPE"* ]] +} + +port_listed() { + local ports + ports=$(semanage port -l -C | grep -E "^${PORT_TYPE} +tcp +" || true) + [[ " ${ports//,/ } " == *" ${PORT} "* ]] +} + +module_listed() { + grep -qx "$MODULE" <<<"$(semodule -l)" +} + +type_in_running_policy() { + [[ "$(seinfo -t "$MODULE_TYPE" 2>/dev/null || true)" == *"$MODULE_TYPE"* ]] +} + +labelled_as() { + [[ "$(stat -c '%C' "$1")" == *":$2:"* ]] +} + +require_enforcing() { + local mode_now + mode_now=$(getenforce) + [[ "$mode_now" == Enforcing ]] || fail "getenforce reports ${mode_now}, want Enforcing" + echo "getenforce: ${mode_now}" +} + +require_home_labels() { + local got + got=$(matchpathcon -m dir "$HOME_ROOT" | awk '{print $2}') + [[ "$got" == *:home_root_t:* ]] || fail "${HOME_ROOT} resolves to ${got}, want home_root_t" + got=$(matchpathcon -m dir "${HOME_ROOT}/someone" | awk '{print $2}') + [[ "$got" == *:user_home_dir_t:* ]] || fail "${HOME_ROOT}/someone resolves to ${got}, want user_home_dir_t" + got=$(stat -c '%C' "$HOME_ROOT") + [[ "$got" == *:home_root_t:* ]] || fail "${HOME_ROOT} is labelled ${got} on disk, want home_root_t" + echo "home labels: ${HOME_ROOT} home_root_t, ${HOME_ROOT}/ user_home_dir_t" +} + +# The image's own policy must be the running policy, with the local changes +# on top: every type of the pristine compiled policy under /usr/etc and +# every module shipped in the pristine store must be present. A machine +# whose local store held back the new image's policy fails here. +require_image_policy() { + local pristine_policy missing name active_modules + pristine_policy=$(find "${PRISTINE_STORE}/policy" -maxdepth 1 -name 'policy.*' 2>/dev/null | sort -V | tail -n 1) + [[ -n "$pristine_policy" ]] || fail "no compiled policy under ${PRISTINE_STORE}/policy" + missing=$(comm -23 \ + <(seinfo "$pristine_policy" -t | tail -n +2 | sed 's/^ *//' | sort) \ + <(seinfo -t | tail -n +2 | sed 's/^ *//' | sort)) + [[ -z "$missing" ]] || fail "types of the image's policy are missing from the running policy: $(echo "$missing" | head -n 5 | tr '\n' ' ')" + missing="" + active_modules=$(semodule -l) + for dir in "${PRISTINE_STORE}"/active/modules/*/*/; do + [[ -d "$dir" ]] || continue + name=$(basename "$dir") + if [[ ! -e "${dir}/disabled" ]] && ! grep -qx "$name" <<<"$active_modules"; then + missing+=" ${name}" + fi + done + [[ -z "$missing" ]] || fail "modules of the image's store are missing from the active store:${missing}" + echo "image policy: every pristine type and module is in the running policy" +} + +report_finalize_log() { + # Informational: the previous boot's deployment finalization is where + # ostree rebuilds the policy. Journal persistence is not guaranteed. + journalctl -b -1 -u ostree-finalize-staged.service --no-pager 2>/dev/null \ + | grep -i -E 'selinux|semodule|policy' || echo "(no SELinux lines from the previous boot's ostree-finalize-staged.service)" +} + +case "$mode" in + probe) + if store_is_persistent; then + echo persistent + else + echo immutable + fi + ;; + apply) + store_is_persistent || fail "store-root in ${SEMANAGE_CONF} is not /etc/selinux; persistent changes cannot work" + require_enforcing + run setsebool -P "$BOOLEAN" 1 + boolean_is on || fail "${BOOLEAN} is not on after setsebool -P" + run semanage fcontext -a -t "$FCONTEXT_TYPE" "$FCONTEXT_RULE" + fcontext_listed || fail "file context rule for ${FCONTEXT_RULE} is not listed" + mkdir -p "$FCONTEXT_DIR" + : > "${FCONTEXT_DIR}/index.html" + run restorecon -R "$FCONTEXT_DIR" + labelled_as "${FCONTEXT_DIR}/index.html" "$FCONTEXT_TYPE" \ + || fail "${FCONTEXT_DIR}/index.html did not take ${FCONTEXT_TYPE}" + run semanage port -a -t "$PORT_TYPE" -p tcp "$PORT" + port_listed || fail "port ${PORT}/tcp is not labelled ${PORT_TYPE}" + module_dir=$(mktemp -d) + printf '(type %s)\n' "$MODULE_TYPE" > "${module_dir}/${MODULE}.cil" + run semodule -i "${module_dir}/${MODULE}.cil" + rm -rf "$module_dir" + module_listed || fail "module ${MODULE} is not listed by semodule -l" + type_in_running_policy || fail "${MODULE_TYPE} is not in the running policy" + require_home_labels + require_enforcing + echo "PASS: four local SELinux changes applied and in effect" + ;; + verify) + store_is_persistent || fail "store-root in ${SEMANAGE_CONF} is not /etc/selinux" + require_enforcing + report_finalize_log + run semodule -l >/dev/null + run semanage boolean -l >/dev/null + boolean_is on || fail "${BOOLEAN} is not on" + fcontext_listed || fail "file context rule for ${FCONTEXT_RULE} is not listed" + labelled_as "${FCONTEXT_DIR}/index.html" "$FCONTEXT_TYPE" \ + || fail "${FCONTEXT_DIR}/index.html lost ${FCONTEXT_TYPE}" + port_listed || fail "port ${PORT}/tcp is not labelled ${PORT_TYPE}" + module_listed || fail "module ${MODULE} is not listed by semodule -l" + type_in_running_policy || fail "${MODULE_TYPE} is not in the running policy" + require_image_policy + require_home_labels + echo "PASS: local SELinux changes in effect alongside the image's policy" + ;; + remove) + run setsebool -P "$BOOLEAN" 0 + boolean_is off || fail "${BOOLEAN} is still on" + run semanage fcontext -d "$FCONTEXT_RULE" + ! fcontext_listed || fail "file context rule for ${FCONTEXT_RULE} is still listed" + run semanage port -d -p tcp "$PORT" + ! port_listed || fail "port ${PORT}/tcp is still labelled" + run semodule -r "$MODULE" + ! module_listed || fail "module ${MODULE} is still listed" + ! type_in_running_policy || fail "${MODULE_TYPE} is still in the running policy" + rm -rf "$FCONTEXT_DIR" + require_home_labels + require_enforcing + echo "PASS: local SELinux changes removed" + ;; + reject) + store_is_persistent || fail "store-root in ${SEMANAGE_CONF} is not /etc/selinux" + require_enforcing + module_dir=$(mktemp -d) + printf '(type %s\n(this is not valid CIL\n' "$BAD_MODULE" > "${module_dir}/${BAD_MODULE}.cil" + echo "+ semodule -i ${module_dir}/${BAD_MODULE}.cil (must be rejected)" + if semodule -i "${module_dir}/${BAD_MODULE}.cil"; then + rm -rf "$module_dir" + fail "semodule accepted the invalid module ${BAD_MODULE}" + fi + rm -rf "$module_dir" + run semodule -l >/dev/null + ! grep -qx "$BAD_MODULE" <<<"$(semodule -l)" || fail "rejected module ${BAD_MODULE} is listed" + if [[ -n "$(find /etc/selinux/targeted/active -name "${BAD_MODULE}*" 2>/dev/null)" ]]; then + fail "rejected module ${BAD_MODULE} left files in the store" + fi + boolean_is on || fail "${BOOLEAN} changed after the rejected module" + module_listed || fail "module ${MODULE} vanished after the rejected module" + type_in_running_policy || fail "${MODULE_TYPE} left the running policy after the rejected module" + require_image_policy + require_home_labels + require_enforcing + echo "PASS: invalid local module rejected; store, policy and labels unchanged" + ;; + *) + echo "Usage: $0 probe|apply|verify|reject|remove" >&2 + exit 2 + ;; +esac diff --git a/scripts/relocate-selinux-store.sh b/scripts/relocate-selinux-store.sh new file mode 100644 index 000000000..15bfbf8fc --- /dev/null +++ b/scripts/relocate-selinux-store.sh @@ -0,0 +1,110 @@ +#!/usr/bin/bash +# Move the SELinux module store from /usr/lib/selinux to /etc/selinux, where +# Fedora atomic and Bluefin keep it, so persistent SELinux changes work on a +# booted system (#551). +# +# Hummingbird's base image relocates the libsemanage store to +# /usr/lib/selinux and appends `store-root=/usr/lib/selinux` to +# /etc/selinux/semanage.conf, so that the store is image-managed and +# immutable. On a booted bootc system /usr is read-only, which means every +# tool that writes the store fails: `setsebool -P` exits 255 without a +# message, and `semanage` and `semodule` -- even `semodule -l` -- cannot take +# the store lock. Users cannot persist a boolean, add a file context or port +# label, or load a local module, all of which work on Bluefin. +# +# Under /etc the store is writable at runtime and ostree carries local +# changes across updates through its 3-way /etc merge. Image policy updates +# still reach every machine: ostree rebuilds the policy in each new +# deployment after the merge (`semodule -N --refresh`, ostree >= 2022.3), +# combining the new image's modules with the machine's local ones. A local +# module the new policy cannot satisfy fails that rebuild and ostree then +# does not apply the update; see docs/skills/containerfile.md. +# +# This runs during the image build, where /usr is writable, before any step +# that rebuilds the policy (scripts/fix-home-labels.sh), so the rebuild +# writes into the store's final location. The compiled policy that the +# kernel loads is unaffected: it already lives at /etc/selinux/targeted/. +# +# The first argument, if given, is the root to operate on; it defaults to /. +# Tests pass a scratch tree. +set -euo pipefail + +root="${1:-}" +root="${root%/}" + +old_store="${root}/usr/lib/selinux" +new_store="${root}/etc/selinux" +conf="${new_store}/semanage.conf" +old_line='store-root=/usr/lib/selinux' +new_line='store-root=/etc/selinux' + +if [[ ! -f "$conf" ]]; then + echo "ERROR: ${conf} is missing; cannot relocate the SELinux module store" >&2 + exit 1 +fi + +if grep -qx "$new_line" "$conf" && [[ ! -e "$old_store" ]]; then + echo "SELinux module store already under /etc/selinux" + exit 0 +fi + +if ! grep -qx "$old_line" "$conf"; then + echo "ERROR: ${conf} does not carry '${old_line}'; the base image's store layout changed, review this script" >&2 + exit 1 +fi + +if [[ ! -d "${old_store}/targeted/active" ]]; then + echo "ERROR: ${old_store}/targeted/active is missing; nothing to relocate" >&2 + exit 1 +fi + +if [[ -e "${new_store}/targeted/active" ]]; then + echo "ERROR: ${new_store}/targeted/active already exists alongside ${old_store}; two stores cannot both be live" >&2 + exit 1 +fi + +mkdir -p "${new_store}/targeted" +mv "${old_store}/targeted/active" "${new_store}/targeted/active" +# libsemanage stages the compiled policy under /final before +# installing it into /etc/selinux/; the directory follows the store. +if [[ -d "${old_store}/final" ]]; then + rm -rf "${new_store}/final" + mv "${old_store}/final" "${new_store}/final" +fi +# libsemanage keeps its store locks at //semanage.*.LOCK +# and (re)creates them empty on first use. The package transaction that +# precedes this script takes them, so they are present in the build and +# belong with the store. +for lock in semanage.read.LOCK semanage.trans.LOCK; do + old_lock="${old_store}/targeted/${lock}" + new_lock="${new_store}/targeted/${lock}" + [[ -f "$old_lock" ]] || continue + if [[ ! -e "$new_lock" ]]; then + mv "$old_lock" "$new_lock" + elif [[ ! -s "$old_lock" ]]; then + rm -f "$old_lock" + else + echo "ERROR: ${old_lock} is not an empty lock file and ${new_lock} already exists" >&2 + exit 1 + fi +done + +# Only the entries Hummingbird moved and libsemanage's locks may be left +# behind; anything else means the base changed under us and would be lost +# silently by an rm -rf. +rmdir "${old_store}/targeted" 2>/dev/null || true +leftovers=$(find "$old_store" -mindepth 1 -print 2>/dev/null | head -n 5 || true) +if [[ -n "$leftovers" ]]; then + echo "ERROR: ${old_store} still holds unexpected entries after the move:" >&2 + echo "$leftovers" >&2 + exit 1 +fi +rmdir "$old_store" + +sed -i "s|^${old_line}\$|${new_line}|" "$conf" +if ! grep -qx "$new_line" "$conf" || grep -qx "$old_line" "$conf"; then + echo "ERROR: failed to point ${conf} at ${new_line}" >&2 + exit 1 +fi + +echo "SELinux module store relocated to /etc/selinux ($(grep -x "$new_line" "$conf"))" diff --git a/tests/test_selinux_store.py b/tests/test_selinux_store.py new file mode 100644 index 000000000..5184672ab --- /dev/null +++ b/tests/test_selinux_store.py @@ -0,0 +1,271 @@ +"""The SELinux module store must live under writable /etc/selinux (#551). + +Hummingbird keeps the libsemanage store under /usr/lib/selinux, which is +read-only on a booted bootc system, so `setsebool -P`, `semanage` and +`semodule` all fail there. `scripts/relocate-selinux-store.sh` moves the +store to /etc/selinux at build time, as Fedora atomic and Bluefin do, and +`iso/scripts/verify-selinux-store.sh` proves on a booted guest that the +four kinds of persistent change work and survive an update. + +Host-side tests cannot run semanage, so they drive the relocation script +against scratch trees and check the two scripts' contracts statically. +""" + +from __future__ import annotations + +import os +import subprocess +import tempfile +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +RELOCATE = ROOT / "scripts/relocate-selinux-store.sh" +CHECKER = ROOT / "iso/scripts/verify-selinux-store.sh" +LIFECYCLE = ROOT / "iso/scripts/lifecycle-e2e.sh" +CONTAINERFILE = ROOT / "Containerfile" +JUSTFILE = ROOT / "Justfile" + +CONF_HEAD = "# Authors: Jason Tang\nmodule-store = direct\n\n[verify kernel]\n" +OLD_LINE = "store-root=/usr/lib/selinux" +NEW_LINE = "store-root=/etc/selinux" + + +class RelocateStoreTests(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.root = Path(self.temp.name) + self.old = self.root / "usr/lib/selinux" + self.new = self.root / "etc/selinux" + self.conf = self.new / "semanage.conf" + + def hummingbird_layout(self): + """The layout Hummingbird's base Containerfile produces.""" + (self.old / "targeted/active/modules/100/abrt").mkdir(parents=True) + (self.old / "targeted/active/modules/100/abrt/cil").write_bytes(b"cil") + (self.old / "targeted/active/commit_num").write_text("1\n") + # libsemanage's store locks, taken by the package transaction that + # runs before the relocation (CI run 38405812025). + (self.old / "targeted/semanage.read.LOCK").touch() + (self.old / "targeted/semanage.trans.LOCK").touch() + (self.old / "final/targeted/contexts/files").mkdir(parents=True) + (self.old / "final/targeted/contexts/files/file_contexts").write_text("x\n") + (self.new / "targeted/policy").mkdir(parents=True) + (self.new / "targeted/policy/policy.35").write_bytes(b"compiled") + self.conf.write_text(CONF_HEAD + "\n" + OLD_LINE + "\n") + + def run_script(self): + return subprocess.run( + ["bash", str(RELOCATE), str(self.root)], + capture_output=True, text=True, check=False, + ) + + def test_moves_store_and_points_semanage_conf_at_etc(self): + self.hummingbird_layout() + result = self.run_script() + self.assertEqual(result.returncode, 0, result.stderr) + self.assertTrue((self.new / "targeted/active/modules/100/abrt/cil").is_file()) + self.assertTrue((self.new / "targeted/active/commit_num").is_file()) + self.assertTrue((self.new / "final/targeted/contexts/files/file_contexts").is_file()) + self.assertTrue((self.new / "targeted/semanage.read.LOCK").is_file()) + self.assertTrue((self.new / "targeted/semanage.trans.LOCK").is_file()) + self.assertFalse(self.old.exists(), "/usr/lib/selinux must not linger") + lines = self.conf.read_text().splitlines() + self.assertIn(NEW_LINE, lines) + self.assertNotIn(OLD_LINE, lines) + self.assertTrue( + self.conf.read_text().startswith(CONF_HEAD), + "only the store-root line may change", + ) + # The compiled policy the kernel loads was already under /etc. + self.assertEqual((self.new / "targeted/policy/policy.35").read_bytes(), b"compiled") + + def test_store_without_lock_files_still_relocates(self): + """A base that never took the store lock leaves no LOCK files behind.""" + self.hummingbird_layout() + (self.old / "targeted/semanage.read.LOCK").unlink() + (self.old / "targeted/semanage.trans.LOCK").unlink() + result = self.run_script() + self.assertEqual(result.returncode, 0, result.stderr) + self.assertFalse(self.old.exists()) + self.assertFalse((self.new / "targeted/semanage.read.LOCK").exists()) + + def test_existing_lock_in_etc_wins_over_an_empty_old_one(self): + self.hummingbird_layout() + (self.new / "targeted/semanage.read.LOCK").touch() + result = self.run_script() + self.assertEqual(result.returncode, 0, result.stderr) + self.assertFalse(self.old.exists()) + self.assertTrue((self.new / "targeted/semanage.read.LOCK").is_file()) + + def test_second_run_is_a_no_op(self): + self.hummingbird_layout() + self.assertEqual(self.run_script().returncode, 0) + before = sorted(str(p.relative_to(self.root)) for p in self.root.rglob("*")) + result = self.run_script() + self.assertEqual(result.returncode, 0, result.stderr) + self.assertIn("already", result.stdout) + after = sorted(str(p.relative_to(self.root)) for p in self.root.rglob("*")) + self.assertEqual(before, after) + + def test_unknown_store_root_fails_instead_of_guessing(self): + self.hummingbird_layout() + self.conf.write_text(CONF_HEAD + "\nstore-root=/var/lib/selinux\n") + result = self.run_script() + self.assertNotEqual(result.returncode, 0) + self.assertIn("does not carry", result.stderr) + self.assertTrue((self.old / "targeted/active").is_dir(), "nothing may move on failure") + + def test_missing_semanage_conf_fails(self): + result = self.run_script() + self.assertNotEqual(result.returncode, 0) + self.assertIn("semanage.conf", result.stderr) + + def test_unexpected_files_under_old_store_fail_the_build(self): + self.hummingbird_layout() + (self.old / "targeted/previous").mkdir() + (self.old / "targeted/previous/keep").write_text("?\n") + result = self.run_script() + self.assertNotEqual(result.returncode, 0) + self.assertIn("unexpected entries", result.stderr) + self.assertIn("targeted/previous", result.stderr) + self.assertTrue( + (self.old / "targeted/previous/keep").is_file(), + "an rm -rf must not swallow what the base changed", + ) + self.assertIn(OLD_LINE, self.conf.read_text().splitlines()) + + def test_two_live_stores_fail(self): + self.hummingbird_layout() + (self.new / "targeted/active").mkdir() + result = self.run_script() + self.assertNotEqual(result.returncode, 0) + self.assertIn("two stores", result.stderr) + + +class ContainerfileContractTests(unittest.TestCase): + def test_script_is_staged_and_installed(self): + text = CONTAINERFILE.read_text() + self.assertIn("scripts/relocate-selinux-store.sh \\", text) + self.assertIn( + "relocate-selinux-store.sh:utah-relocate-selinux-store \\", text + ) + + def test_store_moves_before_the_home_label_policy_rebuild(self): + """fix-home-labels rebuilds the policy; the store must already be in /etc.""" + text = CONTAINERFILE.read_text() + relocate = text.index("/usr/local/libexec/utah-relocate-selinux-store") + rebuild = text.index("/usr/local/libexec/utah-fix-home-labels && \\") + self.assertLess(relocate, rebuild) + run_start = text.rindex("\nRUN ", 0, relocate) + run_end = text.index("\n\n", rebuild) + self.assertIn("utah-install-packages", text[run_start:run_end], + "the relocation shares the package transaction's layer") + + def test_just_check_guards_the_scripts(self): + text = JUSTFILE.read_text() + self.assertIn("test -f scripts/relocate-selinux-store.sh", text) + self.assertIn("test -f iso/scripts/verify-selinux-store.sh", text) + self.assertIn("utah-relocate-selinux-store' Containerfile", text) + + +class GuestCheckerTests(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.conf = Path(self.temp.name) / "semanage.conf" + + def run_checker(self, *args): + env = dict(os.environ, UTAH_SEMANAGE_CONF=str(self.conf)) + return subprocess.run( + ["bash", str(CHECKER), *args], + capture_output=True, text=True, check=False, env=env, + ) + + def test_probe_reports_store_layout(self): + self.conf.write_text(CONF_HEAD + NEW_LINE + "\n") + result = self.run_checker("probe") + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(result.stdout.strip(), "persistent") + self.conf.write_text(CONF_HEAD + OLD_LINE + "\n") + self.assertEqual(self.run_checker("probe").stdout.strip(), "immutable") + + def test_apply_refuses_an_immutable_store(self): + """Without a writable store the sample changes cannot be made.""" + self.conf.write_text(CONF_HEAD + OLD_LINE + "\n") + result = self.run_checker("apply") + self.assertNotEqual(result.returncode, 0) + self.assertIn("store-root", result.stderr) + + def test_unknown_mode_is_a_usage_error(self): + result = self.run_checker("bogus") + self.assertEqual(result.returncode, 2) + + def test_checker_covers_the_four_kinds_of_change(self): + text = CHECKER.read_text() + for command in ( + 'setsebool -P "$BOOLEAN" 1', + 'semanage fcontext -a -t "$FCONTEXT_TYPE" "$FCONTEXT_RULE"', + 'semanage port -a -t "$PORT_TYPE" -p tcp "$PORT"', + 'semodule -i "${module_dir}/${MODULE}.cil"', + 'setsebool -P "$BOOLEAN" 0', + 'semanage fcontext -d "$FCONTEXT_RULE"', + 'semanage port -d -p tcp "$PORT"', + 'semodule -r "$MODULE"', + ): + self.assertIn(command, text, command) + # The new image's policy must be running, not held back by a local store. + self.assertIn("require_image_policy", text) + self.assertIn("home_root_t", text) + self.assertIn("Enforcing", text) + + def test_reject_mode_requires_a_refused_module_and_unchanged_state(self): + text = CHECKER.read_text() + self.assertIn("semodule accepted the invalid module", text) + self.assertIn("require_image_policy", text[text.index(" reject)"):text.index(" *)")]) + self.conf.write_text(CONF_HEAD + OLD_LINE + "\n") + result = self.run_checker("reject") + self.assertNotEqual(result.returncode, 0) + self.assertIn("store-root", result.stderr) + + def test_no_quiet_grep_at_the_end_of_a_pipeline(self): + """`grep -q` closing a pipe early trips pipefail on the producer.""" + for line in CHECKER.read_text().splitlines(): + if "| grep -q" in line: + self.fail(f"grep -q must not consume a pipe: {line.strip()}") + + +class LifecycleHarnessTests(unittest.TestCase): + def test_harness_applies_before_and_verifies_after_the_upgrade(self): + text = LIFECYCLE.read_text() + self.assertIn("iso/scripts/verify-selinux-store.sh", text) + baseline_apply = text.index("selinux_check baseline apply") + stage = text.index('ACTIVE_PHASE="staged"') + upgraded_verify = text.index("selinux_check upgraded verify") + upgraded_apply = text.index("selinux_check upgraded apply") + upgraded_reject = text.index("selinux_check upgraded reject") + upgraded_remove = text.index("selinux_check upgraded remove") + rollback = text.index('ACTIVE_PHASE="rollback"') + self.assertLess(baseline_apply, stage) + self.assertLess(stage, upgraded_verify) + self.assertLess(upgraded_verify, upgraded_apply) + self.assertLess(upgraded_apply, upgraded_reject) + self.assertLess(upgraded_reject, upgraded_remove) + self.assertLess(upgraded_remove, rollback) + + def test_upgraded_deployment_must_have_a_writable_store(self): + text = LIFECYCLE.read_text() + self.assertIn('[[ "${UPGRADED_SELINUX}" == "persistent" ]]', text) + self.assertIn("diagnose_failure \"Upgraded deployment keeps its SELinux policy store", text) + + def test_probe_never_reports_from_a_subshell(self): + """diagnose_failure's report would vanish inside `$(selinux_probe)`.""" + text = LIFECYCLE.read_text() + start = text.index("selinux_probe() {") + end = text.index("\n}\n", start) + self.assertNotIn("diagnose_failure", text[start:end]) + + +if __name__ == "__main__": + unittest.main() From 694732a92193ce3e5be6e2405d369fea87f5acbc Mon Sep 17 00:00:00 2001 From: Danathar <6772335+Danathar@users.noreply.github.com> Date: Sat, 24 Oct 2026 22:38:03 -0400 Subject: [PATCH 2/5] docs(skills): keep local-testing.md within the 500-line hard limit Re-applying #874 on current main leaves local-testing.md at 502 lines. Rewrap two paragraphs (the SELinux-store lifecycle note and the baseline image note); no wording change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SnzFs53Qcer1xDySyxv1Vj Signed-off-by: Danathar <6772335+Danathar@users.noreply.github.com> --- docs/skills/local-testing.md | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/docs/skills/local-testing.md b/docs/skills/local-testing.md index e81051d62..64f5273ef 100644 --- a/docs/skills/local-testing.md +++ b/docs/skills/local-testing.md @@ -428,21 +428,19 @@ This file gate complements deployment-to-entry matching (issue #361 / PR #571); it does not itself prove which deployment an entry represents. Fixture tests run the same guest checker via `python3 -m unittest discover -s tests -p test_boot_files.py`. -Phases 1 and 3 also run `iso/scripts/verify-selinux-store.sh` in the guest (#551; -evidence `selinux--.txt`). `probe` reports whether the store is under -`/etc/selinux` (`persistent`) or `/usr` (`immutable`). Phase 1 makes sample local -changes; Phase 3 checks they survive the reboot along with the new image's policy, -`/var/home` labels and `Enforcing`, then `reject` offers an invalid CIL module and -`remove` cleans up. Mechanism: `containerfile.md`. - -The baseline image (default `ghcr.io/projectbluefin/utah:testing`, the target -ref `just iso testing` builds with) is the ref a live ISO installs its offline -payload under, and phase 1 fails unless the booted deployment tracks it. The -candidate is what gets staged, so it has no default: with the `bootc` policy it -must be a different ref from the baseline, since `bootc switch` to the ref -already booted stages nothing. The staged slot is checked against that -candidate, not against itself: it must come from the candidate's repository, -and a digest-pinned candidate must stage exactly that digest. +Phases 1 and 3 also run `iso/scripts/verify-selinux-store.sh` in the guest (#551; evidence +`selinux--.txt`). `probe` reports whether the store is under `/etc/selinux` +(`persistent`) or `/usr` (`immutable`). Phase 1 makes sample local changes; Phase 3 checks they +survive the reboot along with the new image's policy, `/var/home` labels and `Enforcing`, then +`reject` offers an invalid CIL module and `remove` cleans up. Mechanism: `containerfile.md`. + +The baseline image (default `ghcr.io/projectbluefin/utah:testing`, the target ref `just iso +testing` builds with) is the ref a live ISO installs its offline payload under, and phase 1 fails +unless the booted deployment tracks it. The candidate is what gets staged, so it has no default: +with the `bootc` policy it must be a different ref from the baseline, since `bootc switch` to the +ref already booted stages nothing. The staged slot is checked against that candidate, not against +itself: it must come from the candidate's repository, and a digest-pinned candidate must stage +exactly that digest. The default `bootc` policy stages the candidate with `bootc switch`, so the candidate may come from any repository. `UTAH_LIFECYCLE_POLICY=uupd` instead From b088f4b00d375e3e11370559731dc03a6484ca16 Mon Sep 17 00:00:00 2001 From: Danathar Date: Wed, 28 Oct 2026 16:14:52 -0400 Subject: [PATCH 3/5] fix(selinux): compare the candidate's policy rule by rule, check it on rollback Review on #894: require_image_policy compared only type names and module names, so a candidate that changed an allow rule in an existing module passed while the old compiled policy kept running. - verify-selinux-store.sh compares types and TE rules (sesearch -A -T) of the pristine compiled policy with the running policy, both ways, minus the sample module's type. Empty tool output fails instead of comparing equal. A new image-policy mode runs only this check. - lifecycle-e2e.sh runs image-policy in Phase 3 before apply, verify, reject or remove rebuild the policy, and in Phase 4 checks that the rolled-back baseline has its own store layout and runs its own policy. - Fixture tests drive image-policy against fake seinfo/sesearch/semodule. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Danathar --- docs/skills/local-testing.md | 10 +-- iso/scripts/lifecycle-e2e.sh | 18 +++- iso/scripts/verify-selinux-store.sh | 74 +++++++++++++--- tests/test_selinux_store.py | 133 ++++++++++++++++++++++++++++ 4 files changed, 215 insertions(+), 20 deletions(-) diff --git a/docs/skills/local-testing.md b/docs/skills/local-testing.md index 64f5273ef..a75e4bda9 100644 --- a/docs/skills/local-testing.md +++ b/docs/skills/local-testing.md @@ -428,11 +428,11 @@ This file gate complements deployment-to-entry matching (issue #361 / PR #571); it does not itself prove which deployment an entry represents. Fixture tests run the same guest checker via `python3 -m unittest discover -s tests -p test_boot_files.py`. -Phases 1 and 3 also run `iso/scripts/verify-selinux-store.sh` in the guest (#551; evidence -`selinux--.txt`). `probe` reports whether the store is under `/etc/selinux` -(`persistent`) or `/usr` (`immutable`). Phase 1 makes sample local changes; Phase 3 checks they -survive the reboot along with the new image's policy, `/var/home` labels and `Enforcing`, then -`reject` offers an invalid CIL module and `remove` cleans up. Mechanism: `containerfile.md`. +Phases 1, 3 and 4 run `iso/scripts/verify-selinux-store.sh` in the guest (#551; evidence +`selinux--.txt`). `probe`: store under `/etc/selinux` (`persistent`) or `/usr`. Phase 1 +makes sample local changes. Phase 3 first runs `image-policy` (types and TE rules equal the candidate's +compiled policy), then checks the changes, `/var/home` labels and `Enforcing`, `reject`s an invalid CIL +module and `remove`s; Phase 4 re-runs `probe`/`image-policy` on the rollback. See `containerfile.md`. The baseline image (default `ghcr.io/projectbluefin/utah:testing`, the target ref `just iso testing` builds with) is the ref a live ISO installs its offline payload under, and phase 1 fails diff --git a/iso/scripts/lifecycle-e2e.sh b/iso/scripts/lifecycle-e2e.sh index c36c2d9af..a66a1b94b 100755 --- a/iso/scripts/lifecycle-e2e.sh +++ b/iso/scripts/lifecycle-e2e.sh @@ -169,7 +169,7 @@ verify_boot_files() { # Persistent SELinux changes (#551): the candidate must keep its policy store # under writable /etc/selinux, and ostree's policy rebuild in the new # deployment must carry local changes forward while the image's own policy -# still takes effect. Modes: probe, apply, verify, reject, remove; stdout and stderr +# still takes effect. Modes: probe, apply, image-policy, verify, reject, remove; stdout and stderr # land in evidence/selinux--.txt. Returns the checker's status. selinux_check() { local phase="$1" mode="$2" checker @@ -731,6 +731,12 @@ UPGRADED_SELINUX="$(selinux_probe upgraded)" \ || diagnose_failure "Could not probe the SELinux store layout on the upgraded deployment; see selinux-probe-upgraded.txt" [[ "${UPGRADED_SELINUX}" == "persistent" ]] \ || diagnose_failure "Upgraded deployment keeps its SELinux policy store under read-only /usr; setsebool -P, semanage and semodule cannot work (#551)" +# Before apply, reject or remove rebuild the policy, the policy the upgraded +# deployment booted with must be the candidate's own, rule for rule; a later +# rebuild would otherwise hide an update that never took effect. +selinux_check upgraded image-policy \ + || diagnose_failure "The upgraded deployment is not running the candidate image's SELinux policy; see selinux-image-policy-upgraded.txt" +echo " [Phase 3] SELinux: running policy matches the candidate image's policy" if [[ "${SELINUX_LOCAL_CHANGES}" == 1 ]]; then selinux_check upgraded verify \ || diagnose_failure "Local SELinux changes made before the upgrade are not in effect after it, or the image's policy is not; see selinux-verify-upgraded.txt" @@ -794,6 +800,16 @@ verify_boot_files rollback # BLS entry went missing after `bootc rollback`. verify_bootmgr_entries "${WORK}/rollback-status.json" rollback "booted,rollback" +# The restored baseline keeps its own store layout and runs its own image's +# policy again (with its own /etc's local changes), not the candidate's. +ROLLBACK_SELINUX="$(selinux_probe rollback)" \ + || diagnose_failure "Could not probe the SELinux store layout after rollback; see selinux-probe-rollback.txt" +[[ "${ROLLBACK_SELINUX}" == "${BASELINE_SELINUX}" ]] \ + || diagnose_failure "Rollback deployment reports a ${ROLLBACK_SELINUX} SELinux store; the baseline had ${BASELINE_SELINUX}" +selinux_check rollback image-policy \ + || diagnose_failure "The restored baseline is not running its own image's SELinux policy after rollback; see selinux-image-policy-rollback.txt" +echo " [Phase 4] SELinux: restored baseline runs its own image's policy" + python3 "${ROOT}/scripts/bootc_lifecycle.py" record-diagnostics \ --output-dir "${EVIDENCE}" \ --phase rollback \ diff --git a/iso/scripts/verify-selinux-store.sh b/iso/scripts/verify-selinux-store.sh index 1d9a33d5c..bbe66a084 100644 --- a/iso/scripts/verify-selinux-store.sh +++ b/iso/scripts/verify-selinux-store.sh @@ -13,9 +13,14 @@ # apply make the four sample changes; fail if any tool fails or the # change is not in effect afterwards. # verify the four changes are in effect, the store is listable, SELinux -# is enforcing, home directories keep their labels, and every -# type and module of the image's own policy is part of the -# running policy (a local change must not hold the policy back). +# is enforcing, home directories keep their labels, and the +# running policy is the image's own (see image-policy) with every +# module of the image's store active (a local change must not +# hold the policy back). +# image-policy +# SELinux is enforcing and the running policy is the image's own +# compiled policy plus the local changes, compared rule by rule. +# Run it first after a reboot, before anything rebuilds the policy. # reject offer semodule a module it must refuse (invalid CIL); the install # must fail and leave the store, the running policy and the local # changes exactly as they were, so the machine stays bootable. @@ -100,18 +105,54 @@ require_home_labels() { echo "home labels: ${HOME_ROOT} home_root_t, ${HOME_ROOT}/ user_home_dir_t" } +# Types and TE rules (allow, type_transition, ...) of a policy, sorted, one +# per line; without an argument, of the running policy. The sample module +# only declares its type, so dropping that type leaves the image's own part. +policy_types() { + seinfo "$@" -t | tail -n +2 | sed 's/^ *//' | { grep -vx -- "$MODULE_TYPE" || true; } | LC_ALL=C sort +} + +policy_rules() { + sesearch "$@" -A -T | { grep -vw -- "$MODULE_TYPE" || true; } | LC_ALL=C sort +} + +# Fail with the first differing lines unless the image's and the running +# sorted lists are equal. `|| true`: head closing the pipe trips pipefail. +require_same() { + local what="$1" image="$2" running="$3" differences + differences=$(LC_ALL=C comm -3 <(printf '%s\n' "$image") <(printf '%s\n' "$running") \ + | sed $'s/^\t/ running only: /; t; s/^/ image only: /' | head -n 5 || true) + [[ -z "$differences" ]] || fail "${what} differ between the image's policy and the running policy:"$'\n'"${differences}" +} + # The image's own policy must be the running policy, with the local changes -# on top: every type of the pristine compiled policy under /usr/etc and -# every module shipped in the pristine store must be present. A machine -# whose local store held back the new image's policy fails here. +# on top: the types and TE rules of the pristine compiled policy under +# /usr/etc must equal the running ones, and every module shipped in the +# pristine store must be active. Comparing rules, not only names, catches a +# candidate that changes an allow rule in an existing module while the old +# compiled policy keeps running. A machine whose local store held back the +# new image's policy fails here. require_image_policy() { - local pristine_policy missing name active_modules + local pristine_policy image running missing name active_modules pristine_policy=$(find "${PRISTINE_STORE}/policy" -maxdepth 1 -name 'policy.*' 2>/dev/null | sort -V | tail -n 1) [[ -n "$pristine_policy" ]] || fail "no compiled policy under ${PRISTINE_STORE}/policy" - missing=$(comm -23 \ - <(seinfo "$pristine_policy" -t | tail -n +2 | sed 's/^ *//' | sort) \ - <(seinfo -t | tail -n +2 | sed 's/^ *//' | sort)) - [[ -z "$missing" ]] || fail "types of the image's policy are missing from the running policy: $(echo "$missing" | head -n 5 | tr '\n' ' ')" + # An empty list means the tool failed; it must not compare equal. + image=$(policy_types "$pristine_policy") || image="" + running=$(policy_types) || running="" + [[ -n "$image" ]] || fail "seinfo cannot list the types of ${pristine_policy}" + [[ -n "$running" ]] || fail "seinfo cannot list the types of the running policy" + require_same types "$image" "$running" + image=$(policy_rules "$pristine_policy") || image="" + running=$(policy_rules) || running="" + [[ -n "$image" ]] || fail "sesearch cannot list the TE rules of ${pristine_policy}" + [[ -n "$running" ]] || fail "sesearch cannot list the TE rules of the running policy" + require_same "TE rules" "$image" "$running" + # An immutable store cannot even be listed (semodule cannot take its + # lock), and nothing local can hold its modules back. + if ! store_is_persistent; then + echo "image policy: types and TE rules of ${pristine_policy} match the running policy" + return + fi missing="" active_modules=$(semodule -l) for dir in "${PRISTINE_STORE}"/active/modules/*/*/; do @@ -122,7 +163,7 @@ require_image_policy() { fi done [[ -z "$missing" ]] || fail "modules of the image's store are missing from the active store:${missing}" - echo "image policy: every pristine type and module is in the running policy" + echo "image policy: types and TE rules of ${pristine_policy} match the running policy; every pristine module is active" } report_finalize_log() { @@ -164,9 +205,15 @@ case "$mode" in require_enforcing echo "PASS: four local SELinux changes applied and in effect" ;; + image-policy) + require_enforcing + require_image_policy + echo "PASS: the running SELinux policy is the image's own, plus local changes" + ;; verify) store_is_persistent || fail "store-root in ${SEMANAGE_CONF} is not /etc/selinux" require_enforcing + require_image_policy report_finalize_log run semodule -l >/dev/null run semanage boolean -l >/dev/null @@ -177,7 +224,6 @@ case "$mode" in port_listed || fail "port ${PORT}/tcp is not labelled ${PORT_TYPE}" module_listed || fail "module ${MODULE} is not listed by semodule -l" type_in_running_policy || fail "${MODULE_TYPE} is not in the running policy" - require_image_policy require_home_labels echo "PASS: local SELinux changes in effect alongside the image's policy" ;; @@ -221,7 +267,7 @@ case "$mode" in echo "PASS: invalid local module rejected; store, policy and labels unchanged" ;; *) - echo "Usage: $0 probe|apply|verify|reject|remove" >&2 + echo "Usage: $0 probe|apply|image-policy|verify|reject|remove" >&2 exit 2 ;; esac diff --git a/tests/test_selinux_store.py b/tests/test_selinux_store.py index 5184672ab..e97a06f11 100644 --- a/tests/test_selinux_store.py +++ b/tests/test_selinux_store.py @@ -236,6 +236,124 @@ def test_no_quiet_grep_at_the_end_of_a_pipeline(self): self.fail(f"grep -q must not consume a pipe: {line.strip()}") +class ImagePolicyCheckTests(unittest.TestCase): + """`image-policy` against fake SELinux tools. + + The fakes answer from `-image` when given the pristine compiled + policy and from `-running` otherwise, as seinfo and sesearch read + the running policy when no policy file is named. + """ + + TYPES = "Types: 3\n init_t\n httpd_t\n user_home_dir_t\n" + RULES = ( + "allow httpd_t httpd_sys_content_t:file { getattr open read };\n" + "type_transition init_t httpd_exec_t:process httpd_t;\n" + ) + + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.dir = Path(self.temp.name) + self.conf = self.dir / "semanage.conf" + self.conf.write_text(CONF_HEAD + NEW_LINE + "\n") + self.store = self.dir / "pristine" + (self.store / "policy").mkdir(parents=True) + (self.store / "policy/policy.35").write_bytes(b"compiled") + (self.store / "active/modules/100/apache").mkdir(parents=True) + self.bin = self.dir / "bin" + self.bin.mkdir() + for tool in ("seinfo", "sesearch"): + self.fake(tool, f"""for arg; do + if [[ "$arg" == "{self.store}/policy/policy.35" ]]; then + exec cat "{self.dir}/{tool}-image" + fi +done +exec cat "{self.dir}/{tool}-running" +""") + self.fake("getenforce", "echo Enforcing\n") + self.fake("semodule", "printf 'apache\\nutah_e2e\\n'\n") + self.write("seinfo-image", self.TYPES) + self.write("sesearch-image", self.RULES) + + def fake(self, name, body): + path = self.bin / name + path.write_text("#!/usr/bin/bash\n" + body) + path.chmod(0o755) + + def write(self, name, text): + (self.dir / name).write_text(text) + + def run_check(self): + env = dict( + os.environ, + PATH=f"{self.bin}:{os.environ['PATH']}", + UTAH_SEMANAGE_CONF=str(self.conf), + UTAH_PRISTINE_STORE=str(self.store), + ) + return subprocess.run( + ["bash", str(CHECKER), "image-policy"], + capture_output=True, text=True, check=False, env=env, + ) + + def test_image_policy_plus_the_sample_module_passes(self): + self.write("seinfo-running", self.TYPES + " utah_e2e_t\n") + self.write("sesearch-running", self.RULES + "allow utah_e2e_t utah_e2e_t:file read;\n") + result = self.run_check() + self.assertEqual(result.returncode, 0, result.stderr) + self.assertIn("PASS", result.stdout) + + def test_changed_allow_rule_with_identical_names_fails(self): + """hanthor's #894 finding: name sets alone miss a changed rule.""" + self.write("seinfo-running", self.TYPES) + self.write("sesearch-running", self.RULES.replace(" open read", " read")) + result = self.run_check() + self.assertNotEqual(result.returncode, 0) + self.assertIn("TE rules differ", result.stderr) + self.assertIn("image only: allow httpd_t", result.stderr) + self.assertIn("running only: allow httpd_t", result.stderr) + + def test_type_the_image_dropped_but_still_running_fails(self): + self.write("seinfo-running", self.TYPES + " old_t\n") + self.write("sesearch-running", self.RULES) + result = self.run_check() + self.assertNotEqual(result.returncode, 0) + self.assertIn("types differ", result.stderr) + self.assertIn("running only: old_t", result.stderr) + + def test_empty_tool_output_fails_instead_of_comparing_equal(self): + self.write("seinfo-image", "") + self.write("seinfo-running", "") + self.write("sesearch-running", self.RULES) + result = self.run_check() + self.assertNotEqual(result.returncode, 0) + self.assertIn("seinfo cannot list the types", result.stderr) + + def test_many_differences_report_instead_of_dying_on_sigpipe(self): + self.write("seinfo-running", self.TYPES) + self.write("sesearch-running", "".join( + f"allow t{i}_t t{i}_t:file read;\n" for i in range(20000) + )) + result = self.run_check() + self.assertNotEqual(result.returncode, 0) + self.assertIn("TE rules differ", result.stderr) + + def test_missing_pristine_module_fails(self): + (self.store / "active/modules/100/extra").mkdir() + self.write("seinfo-running", self.TYPES) + self.write("sesearch-running", self.RULES) + result = self.run_check() + self.assertNotEqual(result.returncode, 0) + self.assertIn("missing from the active store: extra", result.stderr) + + def test_immutable_store_compares_rules_without_listing_modules(self): + self.conf.write_text(CONF_HEAD + OLD_LINE + "\n") + self.fake("semodule", "exit 1\n") + self.write("seinfo-running", self.TYPES) + self.write("sesearch-running", self.RULES) + result = self.run_check() + self.assertEqual(result.returncode, 0, result.stderr) + + class LifecycleHarnessTests(unittest.TestCase): def test_harness_applies_before_and_verifies_after_the_upgrade(self): text = LIFECYCLE.read_text() @@ -254,6 +372,21 @@ def test_harness_applies_before_and_verifies_after_the_upgrade(self): self.assertLess(upgraded_reject, upgraded_remove) self.assertLess(upgraded_remove, rollback) + def test_candidate_policy_is_checked_before_anything_rebuilds_it(self): + text = LIFECYCLE.read_text() + image_policy = text.index("selinux_check upgraded image-policy") + self.assertLess(text.index('ACTIVE_PHASE="upgraded"'), image_policy) + for rebuild in ("verify", "apply", "reject", "remove"): + self.assertLess(image_policy, text.index(f"selinux_check upgraded {rebuild}"), rebuild) + + def test_rollback_checks_the_restored_selinux_state(self): + text = LIFECYCLE.read_text() + rollback = text.index('ACTIVE_PHASE="rollback"') + complete = text.index('ACTIVE_PHASE="complete"') + section = text[rollback:complete] + self.assertIn('[[ "${ROLLBACK_SELINUX}" == "${BASELINE_SELINUX}" ]]', section) + self.assertIn("selinux_check rollback image-policy", section) + def test_upgraded_deployment_must_have_a_writable_store(self): text = LIFECYCLE.read_text() self.assertIn('[[ "${UPGRADED_SELINUX}" == "persistent" ]]', text) From 180e1f1e5be5bdb7e9b438c6551018fb475b8c7c Mon Sep 17 00:00:00 2001 From: Danathar Date: Wed, 28 Oct 2026 16:14:52 -0400 Subject: [PATCH 4/5] docs(skills): wrap local-testing.md at 80 columns, trim the SELinux note Review on #894: the SELinux paragraph and the rewrapped baseline paragraph ran 91-103 columns to stay under the 500-line hard max. The SELinux note now points at the verifier's header and containerfile.md for detail, and the baseline paragraph is back to main's wrapping. The page stays at 500 lines, also when merged with current main. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Danathar --- docs/skills/local-testing.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/docs/skills/local-testing.md b/docs/skills/local-testing.md index a75e4bda9..e147eace6 100644 --- a/docs/skills/local-testing.md +++ b/docs/skills/local-testing.md @@ -428,19 +428,19 @@ This file gate complements deployment-to-entry matching (issue #361 / PR #571); it does not itself prove which deployment an entry represents. Fixture tests run the same guest checker via `python3 -m unittest discover -s tests -p test_boot_files.py`. -Phases 1, 3 and 4 run `iso/scripts/verify-selinux-store.sh` in the guest (#551; evidence -`selinux--.txt`). `probe`: store under `/etc/selinux` (`persistent`) or `/usr`. Phase 1 -makes sample local changes. Phase 3 first runs `image-policy` (types and TE rules equal the candidate's -compiled policy), then checks the changes, `/var/home` labels and `Enforcing`, `reject`s an invalid CIL -module and `remove`s; Phase 4 re-runs `probe`/`image-policy` on the rollback. See `containerfile.md`. - -The baseline image (default `ghcr.io/projectbluefin/utah:testing`, the target ref `just iso -testing` builds with) is the ref a live ISO installs its offline payload under, and phase 1 fails -unless the booted deployment tracks it. The candidate is what gets staged, so it has no default: -with the `bootc` policy it must be a different ref from the baseline, since `bootc switch` to the -ref already booted stages nothing. The staged slot is checked against that candidate, not against -itself: it must come from the candidate's repository, and a digest-pinned candidate must stage -exactly that digest. +Phases 1, 3 and 4 run `iso/scripts/verify-selinux-store.sh` (#551; evidence +`selinux--.txt`): store location, sample local changes, the running +policy against the candidate's compiled policy rule by rule, then the rollback. +Modes are in the script header; the store design is in `containerfile.md`. + +The baseline image (default `ghcr.io/projectbluefin/utah:testing`, the target +ref `just iso testing` builds with) is the ref a live ISO installs its offline +payload under, and phase 1 fails unless the booted deployment tracks it. The +candidate is what gets staged, so it has no default: with the `bootc` policy it +must be a different ref from the baseline, since `bootc switch` to the ref +already booted stages nothing. The staged slot is checked against that +candidate, not against itself: it must come from the candidate's repository, +and a digest-pinned candidate must stage exactly that digest. The default `bootc` policy stages the candidate with `bootc switch`, so the candidate may come from any repository. `UTAH_LIFECYCLE_POLICY=uupd` instead From 3c345ac9f57c83b8b3a4b4fec2e76d729580d5d6 Mon Sep 17 00:00:00 2001 From: Danathar Date: Mon, 9 Nov 2026 14:54:42 +0000 Subject: [PATCH 5/5] fix(selinux): guard backend assumptions and retain failure logs Require OSTree before guest policy operations, preserve finalization diagnostics on policy mismatches, and respect the fixture store in rejection checks. Document the unsupported composefs verification boundary and keep the booted-evidence hold. Assisted-by: GPT-6 via GitHub Copilot Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Danathar --- README.md | 4 +++ docs/skills/containerfile.md | 11 ++++++-- docs/skills/lifecycle-e2e.md | 7 +++++ iso/scripts/verify-selinux-store.sh | 20 +++++++++++--- tests/test_selinux_store.py | 41 ++++++++++++++++++++++++++--- 5 files changed, 75 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1008e2d6a..3d0891bb5 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,10 @@ See the [open issues](https://github.com/projectbluefin/utah/issues) to track pr ## Persistent SELinux changes +The update and recovery procedure below applies to OSTree installs. +Composefs installs remain experimental (#1129); SELinux policy persistence +and update verification on that backend are not established by this change. + SELinux stays enforcing on Utah, and lasting policy changes are made with the same commands as on Bluefin: `sudo setsebool -P` for a boolean, `sudo semanage fcontext` for a file-labelling rule, `sudo semanage port` for a diff --git a/docs/skills/containerfile.md b/docs/skills/containerfile.md index 8fc67c24f..ab3a3f4e3 100644 --- a/docs/skills/containerfile.md +++ b/docs/skills/containerfile.md @@ -267,8 +267,15 @@ ostree-finalize-staged.service` names the failure. Recovery is `semodule -r ` or restoring the pristine store from `/usr/etc/selinux`; both are documented for users in `README.md`. -The lifecycle harness proves this on a booted guest -(`iso/scripts/verify-selinux-store.sh`, see `local-testing.md`). A +These update and recovery semantics apply to the OSTree backend. Composefs +remains opt-in (#1129); neither its policy refresh nor a pristine-store path +is established here. The lifecycle verifier refuses a guest without +`/run/ostree-booted` before performing its OSTree-specific checks. Keep the +hold until booted update/rollback evidence on the new head establishes the +supported backend; a passing image build or host fixture is insufficient. + +The lifecycle harness checks this on a booted OSTree guest +(`iso/scripts/verify-selinux-store.sh`, see `lifecycle-e2e.md`). A `switch` from a pre-#551 image, whose `/etc/selinux` has no `active/`, takes the new image's store whole; a machine switched in from Bluefin carries whatever store files it changed there, which become live again at diff --git a/docs/skills/lifecycle-e2e.md b/docs/skills/lifecycle-e2e.md index 4f67407e9..78c87a458 100644 --- a/docs/skills/lifecycle-e2e.md +++ b/docs/skills/lifecycle-e2e.md @@ -99,6 +99,13 @@ Phases 1, 3 and 4 run `iso/scripts/verify-selinux-store.sh` (#551; evidence `selinux--.txt`): store location, sample local changes, the running policy against the candidate's compiled policy rule by rule, then the rollback. Modes are in the script header; the store design is in `containerfile.md`. +The SELinux verifier requires `/run/ostree-booted` before probing, mutating +or comparing policy. Composefs runs fail explicitly at that gate; they do not +establish SELinux acceptance. The `/usr/etc` pristine policy and previous-boot +`ostree-finalize-staged` log are OSTree-specific. Capture that log before +policy comparison so a mismatch retains its finalization diagnostics. +Keep #551 held until booted update/rollback evidence exists on the new head; +host fixtures do not prove either backend's update behavior. The baseline image (default `ghcr.io/projectbluefin/utah:testing`, the target ref `just iso testing` builds with) is the ref a live ISO installs its offline diff --git a/iso/scripts/verify-selinux-store.sh b/iso/scripts/verify-selinux-store.sh index bbe66a084..f7f0b5fea 100644 --- a/iso/scripts/verify-selinux-store.sh +++ b/iso/scripts/verify-selinux-store.sh @@ -26,7 +26,10 @@ # changes exactly as they were, so the machine stays bootable. # remove undo the four changes and confirm none is left in effect. # -# The three paths can be overridden for host-side fixture tests; nothing on +# This verifier relies on OSTree's /etc merge, finalization refresh and +# /usr/etc pristine policy. Refuse other backends before touching policy; +# composefs acceptance requires its own verification and booted evidence. +# The paths can be overridden for host-side fixture tests; nothing on # the host is touched when they point into a scratch tree. set -euo pipefail @@ -34,6 +37,7 @@ mode="${1:-}" SEMANAGE_CONF="${UTAH_SEMANAGE_CONF:-/etc/selinux/semanage.conf}" PRISTINE_STORE="${UTAH_PRISTINE_STORE:-/usr/etc/selinux/targeted}" HOME_ROOT="${UTAH_HOME_ROOT:-/var/home}" +OSTREE_BOOTED="${UTAH_OSTREE_BOOTED:-/run/ostree-booted}" BOOLEAN=virt_use_samba FCONTEXT_DIR=/srv/utah-e2e @@ -55,6 +59,11 @@ run() { "$@" || fail "'$*' exited $?" } +require_ostree_backend() { + [[ -e "$OSTREE_BOOTED" ]] \ + || fail "OSTree backend required (${OSTREE_BOOTED} missing); composefs SELinux update verification is not implemented (#551, #1129)" +} + store_is_persistent() { grep -qx 'store-root=/etc/selinux' "$SEMANAGE_CONF" } @@ -173,6 +182,10 @@ report_finalize_log() { | grep -i -E 'selinux|semodule|policy' || echo "(no SELinux lines from the previous boot's ostree-finalize-staged.service)" } +case "$mode" in + probe|apply|image-policy|verify|reject|remove) require_ostree_backend ;; +esac + case "$mode" in probe) if store_is_persistent; then @@ -207,14 +220,15 @@ case "$mode" in ;; image-policy) require_enforcing + report_finalize_log require_image_policy echo "PASS: the running SELinux policy is the image's own, plus local changes" ;; verify) store_is_persistent || fail "store-root in ${SEMANAGE_CONF} is not /etc/selinux" require_enforcing - require_image_policy report_finalize_log + require_image_policy run semodule -l >/dev/null run semanage boolean -l >/dev/null boolean_is on || fail "${BOOLEAN} is not on" @@ -255,7 +269,7 @@ case "$mode" in rm -rf "$module_dir" run semodule -l >/dev/null ! grep -qx "$BAD_MODULE" <<<"$(semodule -l)" || fail "rejected module ${BAD_MODULE} is listed" - if [[ -n "$(find /etc/selinux/targeted/active -name "${BAD_MODULE}*" 2>/dev/null)" ]]; then + if [[ -n "$(find "$(dirname "$SEMANAGE_CONF")/targeted/active" -name "${BAD_MODULE}*" 2>/dev/null)" ]]; then fail "rejected module ${BAD_MODULE} left files in the store" fi boolean_is on || fail "${BOOLEAN} changed after the rejected module" diff --git a/tests/test_selinux_store.py b/tests/test_selinux_store.py index e97a06f11..94700307e 100644 --- a/tests/test_selinux_store.py +++ b/tests/test_selinux_store.py @@ -175,9 +175,12 @@ def setUp(self): self.temp = tempfile.TemporaryDirectory() self.addCleanup(self.temp.cleanup) self.conf = Path(self.temp.name) / "semanage.conf" + self.ostree_booted = Path(self.temp.name) / "ostree-booted" + self.ostree_booted.touch() def run_checker(self, *args): - env = dict(os.environ, UTAH_SEMANAGE_CONF=str(self.conf)) + env = dict(os.environ, UTAH_SEMANAGE_CONF=str(self.conf), + UTAH_OSTREE_BOOTED=str(self.ostree_booted)) return subprocess.run( ["bash", str(CHECKER), *args], capture_output=True, text=True, check=False, env=env, @@ -202,6 +205,15 @@ def test_unknown_mode_is_a_usage_error(self): result = self.run_checker("bogus") self.assertEqual(result.returncode, 2) + def test_non_ostree_backend_is_rejected_before_policy_operations(self): + self.ostree_booted.unlink() + for mode in ("probe", "apply", "image-policy", "verify", "reject", "remove"): + with self.subTest(mode=mode): + result = self.run_checker(mode) + self.assertNotEqual(result.returncode, 0) + self.assertIn("OSTree backend required", result.stderr) + self.assertNotIn("PASS", result.stdout) + def test_checker_covers_the_four_kinds_of_change(self): text = CHECKER.read_text() for command in ( @@ -256,6 +268,8 @@ def setUp(self): self.dir = Path(self.temp.name) self.conf = self.dir / "semanage.conf" self.conf.write_text(CONF_HEAD + NEW_LINE + "\n") + self.ostree_booted = self.dir / "ostree-booted" + self.ostree_booted.touch() self.store = self.dir / "pristine" (self.store / "policy").mkdir(parents=True) (self.store / "policy/policy.35").write_bytes(b"compiled") @@ -271,6 +285,7 @@ def setUp(self): exec cat "{self.dir}/{tool}-running" """) self.fake("getenforce", "echo Enforcing\n") + self.fake("journalctl", "echo 'finalize: SELinux policy refresh diagnostic'\n") self.fake("semodule", "printf 'apache\\nutah_e2e\\n'\n") self.write("seinfo-image", self.TYPES) self.write("sesearch-image", self.RULES) @@ -283,15 +298,16 @@ def fake(self, name, body): def write(self, name, text): (self.dir / name).write_text(text) - def run_check(self): + def run_check(self, mode="image-policy"): env = dict( os.environ, PATH=f"{self.bin}:{os.environ['PATH']}", UTAH_SEMANAGE_CONF=str(self.conf), UTAH_PRISTINE_STORE=str(self.store), + UTAH_OSTREE_BOOTED=str(self.ostree_booted), ) return subprocess.run( - ["bash", str(CHECKER), "image-policy"], + ["bash", str(CHECKER), mode], capture_output=True, text=True, check=False, env=env, ) @@ -312,6 +328,25 @@ def test_changed_allow_rule_with_identical_names_fails(self): self.assertIn("image only: allow httpd_t", result.stderr) self.assertIn("running only: allow httpd_t", result.stderr) + def test_finalize_log_survives_policy_mismatch_in_both_modes(self): + self.write("seinfo-running", self.TYPES) + self.write("sesearch-running", self.RULES.replace(" open read", " read")) + for mode in ("image-policy", "verify"): + with self.subTest(mode=mode): + result = self.run_check(mode) + self.assertNotEqual(result.returncode, 0) + self.assertIn("TE rules differ", result.stderr) + self.assertIn("finalize: SELinux policy refresh diagnostic", result.stdout) + + def test_rejected_module_residue_is_checked_in_the_fixture_store(self): + active = self.dir / "targeted/active" + active.mkdir(parents=True) + (active / "utah_e2e_bad-residue").touch() + self.fake("semodule", 'if [[ "$1" == -i ]]; then exit 1; fi\nprintf "apache\\nutah_e2e\\n"\n') + result = self.run_check("reject") + self.assertNotEqual(result.returncode, 0) + self.assertIn("left files in the store", result.stderr) + def test_type_the_image_dropped_but_still_running_fails(self): self.write("seinfo-running", self.TYPES + " old_t\n") self.write("sesearch-running", self.RULES)