From 809e209d9e9d7a83cc57fba4f4ca46969b68da09 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Wed, 7 Oct 2026 13:58:50 -0300 Subject: [PATCH] test(xcat-core): measure the SELinux context of the staged Genesis kernel The unit test in this branch drives stage_genesis_payload with a stub runner, so it reads the command mknb builds and never a context on disk. No xcattest case reads one either, so a cell with SELinux enabled cannot show a Genesis kernel that arrives in the TFTP root with the context of the xCAT install tree. genesis_kernel_selinux_context deletes the staged kernel, runs mknb, and reports whether restorecon -F -n -v finds a difference from the policy. cp keeps the context of a destination that exists, so the delete is what makes the case measure the copy. Where SELinux is disabled, or libselinux-utils is absent, the case prints GENESIS_SELINUX_SKIPPED and passes. A permissive node applies labels and is measured like an enforcing one. Measured on an AlmaLinux 9.8 management node with SELinux permissive: without the fix the case reports system_u:object_r:usr_t:s0 against the policy's tftpdir_t and fails, and with the fix it reports tftpdir_t and passes. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- .../genesis/genesis_kernel_selinux_context | 7 +++ .../testcase/genesis/selinux_context.sh | 58 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 xCAT-test/autotest/testcase/genesis/genesis_kernel_selinux_context create mode 100755 xCAT-test/autotest/testcase/genesis/selinux_context.sh diff --git a/xCAT-test/autotest/testcase/genesis/genesis_kernel_selinux_context b/xCAT-test/autotest/testcase/genesis/genesis_kernel_selinux_context new file mode 100644 index 0000000000..ea583b6a18 --- /dev/null +++ b/xCAT-test/autotest/testcase/genesis/genesis_kernel_selinux_context @@ -0,0 +1,7 @@ +start:genesis_kernel_selinux_context +label:others,genesis +description:verify the Genesis kernel in the TFTP root carries the SELinux context of that path +cmd:/opt/xcat/share/xcat/tools/autotest/testcase/genesis/selinux_context.sh __GETNODEATTR($$CN,arch)__ +check:rc==0 +check:output=~GENESIS_SELINUX_(OK|SKIPPED) +end diff --git a/xCAT-test/autotest/testcase/genesis/selinux_context.sh b/xCAT-test/autotest/testcase/genesis/selinux_context.sh new file mode 100755 index 0000000000..1cf59f896d --- /dev/null +++ b/xCAT-test/autotest/testcase/genesis/selinux_context.sh @@ -0,0 +1,58 @@ +#!/bin/bash +# Read back the SELinux context of the Genesis kernel that mknb stages into the TFTP root. +# +# Usage: selinux_context.sh +# +# The caller asserts on one of three tokens, so no error message can carry the token of a pass: +# GENESIS_SELINUX_OK the staged kernel carries the context the policy declares +# GENESIS_SELINUX_SKIPPED SELinux is off, or the tools to read a context are absent +# GENESIS_SELINUX_FAIL the context is wrong, or the kernel was not staged + +arch="$1" +if [ -z "$arch" ]; then + echo "GENESIS_SELINUX_FAIL: no architecture given" + exit 1 +fi + +tftpdir=$(lsdef -t site -i tftpdir 2>/dev/null | sed -n 's/^[[:space:]]*tftpdir=//p') +[ -n "$tftpdir" ] || tftpdir=/tftpboot +kernel="$tftpdir/xcat/genesis.kernel.$arch" + +if ! command -v selinuxenabled >/dev/null 2>&1; then + echo "GENESIS_SELINUX_SKIPPED: selinuxenabled is absent, so no context can be read" + exit 0 +fi +if ! selinuxenabled; then + echo "GENESIS_SELINUX_SKIPPED: SELinux is disabled on $(hostname)" + exit 0 +fi +# A permissive node applies labels, so it is measured like an enforcing one. +echo "SELinux mode on $(hostname): $(getenforce)" +if ! command -v restorecon >/dev/null 2>&1; then + echo "GENESIS_SELINUX_FAIL: SELinux is enabled and restorecon is absent" + exit 1 +fi + +# cp keeps the context of a destination that already exists, so a correct label from an earlier +# run hides the defect. +rm -f "$kernel" +if ! mknb "$arch"; then + echo "GENESIS_SELINUX_FAIL: mknb $arch returned non-zero" + exit 1 +fi +if [ ! -f "$kernel" ]; then + echo "GENESIS_SELINUX_FAIL: mknb $arch staged no $kernel" + exit 1 +fi +echo "Context of $kernel: $(stat -c %C "$kernel")" + +# restorecon names each file whose context differs from the policy. -F compares the whole +# context, which is what mknb applies. +difference=$(restorecon -F -n -v "$kernel" 2>&1) +if [ -n "$difference" ]; then + echo "$difference" + echo "GENESIS_SELINUX_FAIL: the staged kernel does not carry the context of the policy" + exit 1 +fi +echo "GENESIS_SELINUX_OK: the staged kernel carries the context the policy declares" +exit 0