2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-11-27 12:52:16 +00:00

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>
This commit is contained in:
Daniel Hilst
2026-10-07 13:58:50 -03:00
parent e35e2f8e19
commit 809e209d9e
2 changed files with 65 additions and 0 deletions
@@ -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
+58
View File
@@ -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 <arch>
#
# 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