|
| 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 injects a local SELinux policy module |
| 31 | + # This modifies the policy in a way that changes the policy checksum |
| 32 | + # Following Colin's suggestion: inject a local selinux policy module |
| 33 | + "FROM localhost/bootc |
| 34 | +# Inject a local SELinux policy change by modifying file_contexts |
| 35 | +# This will change the policy checksum between deployments |
| 36 | +RUN mkdir -p /opt/bootc-test-selinux-policy && \ |
| 37 | + echo '/opt/bootc-test-selinux-policy /opt/bootc-test-selinux-policy' >> /etc/selinux/targeted/contexts/files/file_contexts.subs_dist || true |
| 38 | +" | save Dockerfile |
| 39 | + |
| 40 | + # Build the derived image |
| 41 | + podman build -t localhost/bootc-derived-policy . |
| 42 | + |
| 43 | + # Try to soft reboot - this should fail because policies differ |
| 44 | + bootc switch --soft-reboot=auto --transport containers-storage localhost/bootc-derived-policy |
| 45 | + let st = bootc status --json | from json |
| 46 | + |
| 47 | + # The staged deployment should NOT be soft-reboot capable because policies differ |
| 48 | + assert (not $st.status.staged.softRebootCapable) "Expected soft reboot to be blocked due to SELinux policy difference" |
| 49 | + |
| 50 | + print "Soft reboot correctly blocked when SELinux policies differ" |
| 51 | + |
| 52 | + # Reset and do a full reboot instead |
| 53 | + ostree admin prepare-soft-reboot --reset |
| 54 | + tmt-reboot |
| 55 | +} |
| 56 | + |
| 57 | +# The second boot; verify we're in the derived image |
| 58 | +def second_boot [] { |
| 59 | + tap begin "Verify deployment and test soft reboot with same policy" |
| 60 | + |
| 61 | + # Verify we're in the new deployment |
| 62 | + let st = bootc status --json | from json |
| 63 | + assert ($st.status.booted.image.name | str contains "bootc-derived-policy") |
| 64 | + |
| 65 | + # Now create another derived image with the SAME policy (no changes) |
| 66 | + let td = mktemp -d |
| 67 | + cd $td |
| 68 | + |
| 69 | + bootc image copy-to-storage |
| 70 | + |
| 71 | + # Create a derived container that doesn't change the policy |
| 72 | + "FROM localhost/bootc-derived-policy |
| 73 | +RUN echo 'same policy test' > /usr/share/testfile-same-policy.txt |
| 74 | +" | save Dockerfile |
| 75 | + |
| 76 | + podman build -t localhost/bootc-same-policy . |
| 77 | + |
| 78 | + # Try to soft reboot - this should succeed because policies match |
| 79 | + bootc switch --soft-reboot=auto --transport containers-storage localhost/bootc-same-policy |
| 80 | + let st = bootc status --json | from json |
| 81 | + |
| 82 | + # The staged deployment SHOULD be soft-reboot capable because policies match |
| 83 | + assert $st.status.staged.softRebootCapable "Expected soft reboot to be allowed when SELinux policies match" |
| 84 | + |
| 85 | + print "Soft reboot correctly allowed when SELinux policies match" |
| 86 | + |
| 87 | + # See ../bug-soft-reboot.md - TMT cannot handle systemd soft-reboots |
| 88 | + ostree admin prepare-soft-reboot --reset |
| 89 | + tmt-reboot |
| 90 | +} |
| 91 | + |
| 92 | +# The third boot; verify we're in the same-policy deployment |
| 93 | +def third_boot [] { |
| 94 | + tap begin "Verify same-policy deployment" |
| 95 | + |
| 96 | + assert ("/usr/share/testfile-same-policy.txt" | path exists) |
| 97 | + |
| 98 | + let st = bootc status --json | from json |
| 99 | + assert ($st.status.booted.image.name | str contains "bootc-same-policy") |
| 100 | + |
| 101 | + print "Successfully verified soft reboot with SELinux policy checks" |
| 102 | + |
| 103 | + tap ok |
| 104 | +} |
| 105 | + |
| 106 | +def main [] { |
| 107 | + # See https://tmt.readthedocs.io/en/stable/stories/features.html#reboot-during-test |
| 108 | + match $env.TMT_REBOOT_COUNT? { |
| 109 | + null | "0" => initial_build, |
| 110 | + "1" => second_boot, |
| 111 | + "2" => third_boot, |
| 112 | + $o => { error make { msg: $"Invalid TMT_REBOOT_COUNT ($o)" } }, |
| 113 | + } |
| 114 | +} |
| 115 | + |
0 commit comments