2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-11-19 17:02:15 +00:00

test(xcat-core): the Genesis SELinux checks pass when the relabel fails

stage_genesis_payload() runs restorecon and discards its exit status, so a
relabel that fails returns 0 and the kernel keeps the context of the install
tree. mknb_genesis_selinux_context.t asserted that rc, so the test held the
defect in place. It now requires the failure to reach the caller, with the
kernel path and a message that names the relabel.

selinux_context.sh reads only the output of restorecon -F -n -v, so a non-zero
exit with no output reports GENESIS_SELINUX_OK.

The helper also builds genesis.kernel.<arch> from the architecture it was
given. With legacy Genesis, mknb ppc64le resolves share/xcat/netboot/genesis/
ppc64 and writes genesis.kernel.ppc64, so the helper fails on a kernel mknb
never staged. Three ppc64le cells run this case.

genesis_kernel_selinux_context.bats drives the helper with lsdef, mknb,
selinuxenabled and restorecon shadowed and XCATROOT in a scratch tree. Three of
its four checks fail without the fix that follows, and the fourth fails when the
OpenEmbedded branch is removed.

Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com>
This commit is contained in:
Daniel Hilst
2026-10-07 15:12:26 -03:00
parent 16e20fbb52
commit e35e2f8e19
2 changed files with 109 additions and 5 deletions
@@ -0,0 +1,96 @@
#!/usr/bin/env bats
#
# Drive selinux_context.sh, the helper the genesis_kernel_selinux_context case runs on a
# management node. It needs SELinux, mknb and the xCAT site table, so shadow all three and
# point XCATROOT at a scratch image root.
load 'helpers/shell_source'
setup()
{
HELPER="$(repo_path 'xCAT-test/autotest/testcase/genesis/selinux_context.sh')"
[ -r "$HELPER" ] || skip "$HELPER is required"
BIN="${BATS_TEST_TMPDIR}/bin"
TFTPDIR="${BATS_TEST_TMPDIR}/tftpboot"
IMAGE_ROOT="${BATS_TEST_TMPDIR}/xcatroot"
mkdir -p "$BIN" "$TFTPDIR/xcat" "$IMAGE_ROOT"
export HELPER BIN TFTPDIR IMAGE_ROOT
}
stub()
{
printf '#!/bin/sh\n%s\n' "$2" >"$BIN/$1"
chmod 0755 "$BIN/$1"
}
# The commands the helper calls on an SELinux management node. mknb stages a kernel named for
# the architecture it resolved, which is not always the architecture it was asked for.
stub_node()
{
local staged_arch="$1" restorecon_body="${2:-exit 0}"
stub lsdef "echo ' tftpdir=$TFTPDIR'"
stub selinuxenabled 'exit 0'
stub getenforce 'echo Enforcing'
stub mknb "echo kernel > '$TFTPDIR/xcat/genesis.kernel.$staged_arch'"
stub restorecon "$restorecon_body"
}
# The image root mknb reads to choose between an OpenEmbedded export and the legacy tree.
install_genesis()
{
local kind="$1" arch="$2"
mkdir -p "$IMAGE_ROOT/share/xcat/netboot/$kind/$arch"
}
run_helper()
{
PATH="$BIN:$PATH" XCATROOT="$IMAGE_ROOT" bash "$HELPER" "$@" 2>&1
}
@test "a verification command that fails without output is not a pass" {
install_genesis genesis x86_64
# restorecon can fail without naming a file: no policy loaded, or a path it cannot read.
stub_node x86_64 'exit 3'
run run_helper x86_64
[ "$status" -ne 0 ]
[[ "$output" == *GENESIS_SELINUX_FAIL* ]]
[[ "$output" != *GENESIS_SELINUX_OK* ]]
[[ "$output" == *3* ]]
}
@test "a legacy POWER node measures the kernel mknb staged" {
# Only xCAT-genesis-openembedded-ppc64le creates the OpenEmbedded directory, and the
# ppc64le genesis-legacy cells do not enable the repository that carries it. mknb then
# uses share/xcat/netboot/genesis/ppc64 and writes genesis.kernel.ppc64.
install_genesis genesis ppc64
stub_node ppc64
run run_helper ppc64le
[ "$status" -eq 0 ]
[[ "$output" == *GENESIS_SELINUX_OK* ]]
[[ "$output" == *genesis.kernel.ppc64* ]]
[[ "$output" != *genesis.kernel.ppc64le* ]]
}
@test "an OpenEmbedded POWER node keeps the exact architecture" {
install_genesis genesis-openembedded ppc64le
install_genesis genesis ppc64
stub_node ppc64le
run run_helper ppc64le
[ "$status" -eq 0 ]
[[ "$output" == *GENESIS_SELINUX_OK* ]]
[[ "$output" == *genesis.kernel.ppc64le* ]]
}
@test "a kernel staged under another name fails and names both" {
install_genesis genesis ppc64
stub_node ppc64le
run run_helper ppc64le
[ "$status" -ne 0 ]
[[ "$output" == *GENESIS_SELINUX_FAIL* ]]
[[ "$output" == *genesis.kernel.ppc64* ]]
[[ "$output" == *genesis.kernel.ppc64le* ]]
}
+13 -5
View File
@@ -45,7 +45,7 @@ sub stage {
my @ran;
no warnings qw(redefine once);
local *xCAT::Utils::isSELINUX = sub { return $opt{selinux} ? 0 : 1 };
my ($rc, $src) = xCAT_plugin::mknb::stage_genesis_payload(
my ($rc, $src, $error) = xCAT_plugin::mknb::stage_genesis_payload(
genesis_type => 'legacy',
genesis_dir => '/opt/xcat/share/xcat/netboot/genesis/x86_64',
tftpdir => $TFTPDIR,
@@ -57,7 +57,7 @@ sub stage {
return ($opt{fail} && $cmd =~ /$opt{fail}/) ? 256 : 0;
},
);
return { rc => $rc, src => $src, ran => \@ran };
return { rc => $rc, src => $src, error => $error, ran => \@ran };
}
# --- the copy must not carry the install tree's context into the TFTP root ---------
@@ -91,14 +91,22 @@ my $off = stage(selinux => 0);
is(scalar(grep { m{restorecon} } @{ $off->{ran} }), 0,
'a node without SELinux runs no relabel');
# --- a relabel that fails must not fail the step ----------------------------------
# --- a relabel that fails must fail the step --------------------------------------
# A relabel that is attempted and fails leaves the kernel with the context the copy gave it,
# which is the defect this routine exists to prevent. Reporting success hides it.
my $norelabel = stage(selinux => 1, fail => qr{restorecon});
is($norelabel->{rc}, 0, 'a failed relabel leaves the staged payload in place');
is($norelabel->{src}, undef, 'and names no unreadable source');
isnt($norelabel->{rc}, 0, 'a failed relabel fails the step');
is($norelabel->{src}, $KERNEL, 'and names the staged Genesis kernel');
like($norelabel->{error} // '', qr{SELinux context},
'and the message names the relabel, not a copy');
like($norelabel->{error} // '', qr{\Q$KERNEL\E},
'and the message carries the kernel path');
# --- the existing failures still name the copy that failed ------------------------
my $nokernel = stage(selinux => 1, fail => qr{/kernel\s});
isnt($nokernel->{rc}, 0, 'an unreadable kernel still fails the step');
like($nokernel->{error} // '', qr{\QFailed to copy\E},
'and still reports the copy it could not make');
is(scalar(grep { m{restorecon} } @{ $nokernel->{ran} }), 0,
'and a kernel that was not copied is not relabelled');