diff --git a/Containerfile b/Containerfile index 89a97d4ee..939e69fcf 100644 --- a/Containerfile +++ b/Containerfile @@ -94,6 +94,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 \ @@ -215,6 +216,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; do \ @@ -284,6 +286,11 @@ RUN --mount=type=bind,from=packages,source=/repository,target=/etc/utah-packages /usr/share/utah/base.toml /usr/share/utah/utah.toml && \ IMAGE_FLAVOR=main /usr/local/libexec/utah-verify-rpm-contract \ /usr/share/utah/base.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 202a9aacd..c93f5cc69 100644 --- a/Justfile +++ b/Justfile @@ -66,6 +66,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 968c7746c..44e347745 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,50 @@ 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 + +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 +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 4e5642d97..a1d3c90e2 100644 --- a/docs/skills/containerfile.md +++ b/docs/skills/containerfile.md @@ -236,6 +236,54 @@ 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`. + +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 +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 137d2018f..85991e6b7 100644 --- a/docs/skills/index.json +++ b/docs/skills/index.json @@ -201,7 +201,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/lifecycle-e2e.md b/docs/skills/lifecycle-e2e.md index e8484c884..26e1ec9bb 100644 --- a/docs/skills/lifecycle-e2e.md +++ b/docs/skills/lifecycle-e2e.md @@ -95,6 +95,18 @@ 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` (#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 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 ea1c25ee8..85d0e18d7 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, 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 + 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" "$@" } @@ -584,6 +607,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 \ @@ -695,6 +735,37 @@ 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)" +# 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" + 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 \ @@ -741,6 +812,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 new file mode 100644 index 000000000..f7f0b5fea --- /dev/null +++ b/iso/scripts/verify-selinux-store.sh @@ -0,0 +1,287 @@ +#!/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 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. +# remove undo the four changes and confirm none is left in effect. +# +# 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 + +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 +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 $?" +} + +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" +} + +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" +} + +# 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: 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 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" + # 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 + [[ -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: types and TE rules of ${pristine_policy} match the running policy; every pristine module is active" +} + +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|apply|image-policy|verify|reject|remove) require_ostree_backend ;; +esac + +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" + ;; + 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 + 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" + 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_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 "$(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" + 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|image-policy|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..94700307e --- /dev/null +++ b/tests/test_selinux_store.py @@ -0,0 +1,439 @@ +"""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" + 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), + UTAH_OSTREE_BOOTED=str(self.ostree_booted)) + 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_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 ( + '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 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.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") + (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("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) + + 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, 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), mode], + 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_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) + 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() + 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_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) + 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()