|
| 1 | +# Verify that soft reboot is blocked when SELinux policies differ |
| 2 | +use std assert |
| 3 | +use tap.nu |
| 4 | + |
| 5 | +let soft_reboot_capable = "/usr/lib/systemd/system/soft-reboot.target" | path exists |
| 6 | +if not $soft_reboot_capable { |
| 7 | + echo "Skipping, system is not soft reboot capable" |
| 8 | + return |
| 9 | +} |
| 10 | + |
| 11 | +# Check if SELinux is enabled |
| 12 | +let selinux_enabled = "/sys/fs/selinux/enforce" | path exists |
| 13 | +if not $selinux_enabled { |
| 14 | + echo "Skipping, SELinux is not enabled" |
| 15 | + return |
| 16 | +} |
| 17 | + |
| 18 | +# This code runs on *each* boot. |
| 19 | +bootc status |
| 20 | + |
| 21 | +# Run on the first boot |
| 22 | +def initial_build [] { |
| 23 | + tap begin "Build base image and test soft reboot with SELinux policy change" |
| 24 | + |
| 25 | + let td = mktemp -d |
| 26 | + cd $td |
| 27 | + |
| 28 | + bootc image copy-to-storage |
| 29 | + |
| 30 | + # Create a derived container that installs a custom SELinux policy module |
| 31 | + # Installing a policy module will change the compiled policy checksum |
| 32 | + # Following Colin's suggestion and the composefs-rs example |
| 33 | + # We create a minimal policy module and install it |
| 34 | + "FROM localhost/bootc |
| 35 | +# Install tools needed to build and install SELinux policy modules |
| 36 | +RUN dnf install -y selinux-policy-devel checkpolicy policycoreutils |
| 37 | +
|
| 38 | +# Create a minimal SELinux policy module that will change the policy checksum |
| 39 | +# We install it to ensure it's part of the deployment filesystem |
| 40 | +RUN mkdir -p /tmp/bootc-test-policy && \\ |
| 41 | + cd /tmp/bootc-test-policy && \\ |
| 42 | + echo 'module bootc_test_policy 1.0;' > bootc_test_policy.te && \\ |
| 43 | + echo 'require {' >> bootc_test_policy.te && \\ |
| 44 | + echo ' type unconfined_t;' >> bootc_test_policy.te && \\ |
| 45 | + echo ' class file { read write };' >> bootc_test_policy.te && \\ |
| 46 | + echo '}' >> bootc_test_policy.te && \\ |
| 47 | + echo 'type bootc_test_t;' >> bootc_test_policy.te && \\ |
| 48 | + checkmodule -M -m -o bootc_test_policy.mod bootc_test_policy.te && \\ |
| 49 | + semodule_package -o bootc_test_policy.pp -m bootc_test_policy.mod && \\ |
| 50 | + semodule -i bootc_test_policy.pp && \\ |
| 51 | + # Ensure the policy module is in /usr/etc/selinux for the deployment |
| 52 | + mkdir -p /usr/etc/selinux/targeted/modules/active/modules && \\ |
| 53 | + if [ -d /etc/selinux/targeted/modules/active/modules ]; then \\ |
| 54 | + cp -a /etc/selinux/targeted/modules/active/modules/* /usr/etc/selinux/targeted/modules/active/modules/ 2>/dev/null || true; \\ |
| 55 | + fi && \\ |
| 56 | + rm -rf /tmp/bootc-test-policy |
| 57 | +" | save Dockerfile |
| 58 | + |
| 59 | + # Build the derived image |
| 60 | + podman build --quiet -t localhost/bootc-derived-policy . |
| 61 | + |
| 62 | + # Verify soft reboot preparation hasn't happened yet |
| 63 | + assert (not ("/run/nextroot" | path exists)) |
| 64 | + |
| 65 | + # Try to soft reboot - this should fail because policies differ |
| 66 | + bootc switch --soft-reboot=auto --transport containers-storage localhost/bootc-derived-policy |
| 67 | + let st = bootc status --json | from json |
| 68 | + |
| 69 | + # Verify staged deployment exists |
| 70 | + assert ($st.status.staged != null) "Expected staged deployment to exist" |
| 71 | + |
| 72 | + # The staged deployment should NOT be soft-reboot capable because policies differ |
| 73 | + assert (not $st.status.staged.softRebootCapable) "Expected soft reboot to be blocked due to SELinux policy difference, but softRebootCapable is true" |
| 74 | + |
| 75 | + # Verify soft reboot preparation didn't happen |
| 76 | + assert (not ("/run/nextroot" | path exists)) "Soft reboot should not be prepared when policies differ" |
| 77 | + |
| 78 | + # Reset and do a full reboot instead |
| 79 | + ostree admin prepare-soft-reboot --reset |
| 80 | + tmt-reboot |
| 81 | +} |
| 82 | + |
| 83 | +# The second boot; verify we're in the derived image |
| 84 | +def second_boot [] { |
| 85 | + tap begin "Verify deployment with different SELinux policy" |
| 86 | + |
| 87 | + # Verify we're in the new deployment |
| 88 | + let st = bootc status --json | from json |
| 89 | + let booted = $st.status.booted.image |
| 90 | + assert ($booted.image.image | str contains "bootc-derived-policy") $"Expected booted image to contain 'bootc-derived-policy', got: ($booted.image.image)" |
| 91 | + |
| 92 | + tap ok |
| 93 | +} |
| 94 | + |
| 95 | +def main [] { |
| 96 | + # See https://tmt.readthedocs.io/en/stable/stories/features.html#reboot-during-test |
| 97 | + match $env.TMT_REBOOT_COUNT? { |
| 98 | + null | "0" => initial_build, |
| 99 | + "1" => second_boot, |
| 100 | + $o => { error make { msg: $"Invalid TMT_REBOOT_COUNT ($o)" } }, |
| 101 | + } |
| 102 | +} |
0 commit comments