2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-11-20 01:12:18 +00:00

fix(xcat-core): a failed Genesis kernel relabel is reported as success

stage_genesis_payload() discarded the exit status of restorecon, so mknb
reported success with the staged kernel still carrying the context of the xCAT
install tree. The routine now returns that status, the kernel path and a message
that names the relabel, and process_request reports it. A failed copy keeps its
own message.

selinux_context.sh reads the exit status of restorecon -F -n -v before its
output, so a non-zero exit with no output is a failure.

The helper also resolves the name mknb stages instead of the architecture it was
given: the exact architecture when an OpenEmbedded export is installed, ppc64
for the legacy POWER image. When that kernel is absent it lists the kernels that
are present, so a mapping that no longer matches mknb is named rather than read
as a missing file.

Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com>
This commit is contained in:
Daniel Hilst
2026-10-07 15:12:33 -03:00
parent 809e209d9e
commit aed2df95c9
2 changed files with 42 additions and 11 deletions
+17 -9
View File
@@ -372,8 +372,10 @@ Arguments:
genesis_type, genesis_dir, tftpdir, arch, tempdir, and an optional run
coderef used in place of system() by the tests.
Returns:
(rc, source) -- rc is the exit status of the copy that failed, and source
names it, so the caller reports the file it could not read.
(rc, source, error) -- rc is the exit status of the step that failed,
source names the file it acted on, and error is the message for the caller,
which says WHICH step failed. A copy and a relabel fail for different
reasons and need different messages.
=cut
@@ -386,20 +388,26 @@ sub stage_genesis_payload {
# Two copies, each able to fail on its own. Return on the first, so neither the exit
# status nor the name of the unreadable file is lost to the one that follows it.
$rc = $run->("shopt -s dotglob; GLOBIGNORE=\".:..\" cp -a $a{genesis_dir}/fs/* $a{tempdir}");
return ($rc, "$a{genesis_dir}/fs") if $rc;
return ($rc, "$a{genesis_dir}/fs", "Failed to copy $a{genesis_dir}/fs contents") if $rc;
my $kernel = "$a{tftpdir}/xcat/genesis.kernel.$a{arch}";
# cp -a implies --preserve=all, which carries the SELinux context of the xCAT install
# tree into the TFTP root, where the policy declares tftpdir_t. The context also
# travels in the security.selinux xattr, so xattr has to go with context here.
$rc = $run->("cp -a --no-preserve=context,xattr $a{genesis_dir}/kernel $kernel");
return ($rc, "$a{genesis_dir}/kernel") if $rc;
return ($rc, "$a{genesis_dir}/kernel", "Failed to copy $a{genesis_dir}/kernel contents") if $rc;
# A kernel staged by an earlier xCAT keeps its own context through the copy, so set
# the context from the policy. isSELINUX returns 0 when SELinux is enabled.
$run->("restorecon -F $kernel") if xCAT::Utils->isSELINUX() == 0;
return (0, undef);
if (xCAT::Utils->isSELINUX() == 0) {
# A relabel that fails leaves the kernel with the context of the install tree,
# which is the state this routine exists to prevent. Report it.
$rc = $run->("restorecon -F $kernel");
return ($rc, $kernel, "Failed to set the SELinux context of $kernel") if $rc;
}
return (0, undef, undef);
}
$rc = $run->("cp -a $a{genesis_dir}/nbroot/* $a{tempdir}");
return ($rc, "$a{genesis_dir}/nbroot");
return ($rc, "$a{genesis_dir}/nbroot",
$rc ? "Failed to copy $a{genesis_dir}/nbroot contents" : undef);
}
sub process_request {
@@ -631,12 +639,12 @@ sub process_request {
mkpath("$tftpdir/xcat");
}
$invisibletouch = 1 if $genesis_type eq 'legacy';
my ($rc, $failed_src) = stage_genesis_payload(
my ($rc, $failed_src, $failed_error) = stage_genesis_payload(
genesis_type => $genesis_type, genesis_dir => $genesis_dir,
tftpdir => $tftpdir, arch => $arch, tempdir => $tempdir);
if ($rc) {
system("rm -rf $tempdir");
$callback->({ error => ["Failed to copy $failed_src contents"], errorcode => [1] });
$callback->({ error => [$failed_error], errorcode => [1] });
return;
}
my $sshdir;
@@ -13,10 +13,23 @@ if [ -z "$arch" ]; then
echo "GENESIS_SELINUX_FAIL: no architecture given"
exit 1
fi
[ "$arch" = ppc64el ] && arch=ppc64le
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"
# mknb names the kernel for the architecture of the image it resolved, not for the one it was
# asked about. An OpenEmbedded export keeps the exact architecture. The legacy tree holds one
# combined POWER image under ppc64, so mknb ppc64le writes genesis.kernel.ppc64 there.
netboot="${XCATROOT:-/opt/xcat}/share/xcat/netboot"
if [ -d "$netboot/genesis-openembedded/$arch" ]; then
staged_arch="$arch"
elif [ "$arch" = ppc64le ]; then
staged_arch=ppc64
else
staged_arch="$arch"
fi
kernel="$tftpdir/xcat/genesis.kernel.$staged_arch"
if ! command -v selinuxenabled >/dev/null 2>&1; then
echo "GENESIS_SELINUX_SKIPPED: selinuxenabled is absent, so no context can be read"
@@ -41,14 +54,24 @@ if ! mknb "$arch"; then
exit 1
fi
if [ ! -f "$kernel" ]; then
# Name what mknb did stage. A kernel under another name means the architecture mapping
# above no longer matches mknb, which is a different defect from an absent kernel.
echo "Kernels in $tftpdir/xcat: $(ls "$tftpdir"/xcat/genesis.kernel.* 2>/dev/null | tr '\n' ' ')"
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.
# context, which is what mknb applies. It can also fail without naming a file -- no policy
# loaded, or a path it cannot read -- so its exit status decides first.
difference=$(restorecon -F -n -v "$kernel" 2>&1)
status=$?
if [ "$status" -ne 0 ]; then
[ -n "$difference" ] && echo "$difference"
echo "GENESIS_SELINUX_FAIL: restorecon -F -n -v exited $status and read no context for $kernel"
exit 1
fi
if [ -n "$difference" ]; then
echo "$difference"
echo "GENESIS_SELINUX_FAIL: the staged kernel does not carry the context of the policy"