From 905c63e47c8d9e1bc4bb6449ed2402ad28652395 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Fri, 16 Oct 2026 11:16:55 -0300 Subject: [PATCH] test(xcat-core): capture the image roots that restorecon relabels xcat.fc excludes a rootimg from the /install relabel only at the default depth, /install/netboot////rootimg. An osimage with rootimgdir=/install/netboot/custom-image resolves rootimg/etc/shadow to public_content_t, so the restorecon -R /install in %posttrans overwrites the labels of the guest operating system. The new native test builds xcat.pp, installs it into a private copy of the targeted store, and asks matchpathcon for the label of each path. Then it labels a copy of /install, relabels it with setfiles after the install and after a reinstall of the module, and reads the labels back. The test fails for the custom roots. The unit test no longer compares the fc lines as text. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- .github/workflows/xcat_test.yml | 19 ++- xCAT-test/native/README.md | 15 +++ xCAT-test/native/selinux_file_contexts.t | 150 +++++++++++++++++++++++ xCAT-test/unit/xcat_selinux_package.t | 8 +- 4 files changed, 185 insertions(+), 7 deletions(-) create mode 100644 xCAT-test/native/selinux_file_contexts.t diff --git a/.github/workflows/xcat_test.yml b/.github/workflows/xcat_test.yml index fc30f70a84..38d67809ab 100644 --- a/.github/workflows/xcat_test.yml +++ b/.github/workflows/xcat_test.yml @@ -71,8 +71,25 @@ jobs: - name: Install lifecycle test dependencies run: | dnf install -y epel-release - dnf --enablerepo=${{ matrix.repository }} install -y perl perl-Capture-Tiny perl-File-Slurper perl-Test-Simple rpm-build bubblewrap systemd chkconfig initscripts tar gzip which diffutils httpd curl procps-ng + dnf --enablerepo=${{ matrix.repository }} install -y perl perl-Capture-Tiny perl-File-Slurper perl-Test-Simple perl-Test-Harness rpm-build bubblewrap systemd chkconfig initscripts tar gzip which diffutils httpd curl procps-ng - name: Enable lifecycle test namespaces run: sysctl -w kernel.apparmor_restrict_unprivileged_userns=0 - name: Test RPM package lifecycle run: su -s /bin/sh nobody -c 'prove -v xCAT-test/native/xcatd_rpm_lifecycle.t xCAT-test/native/apache_rpm_configuration.t xCAT-test/native/apache_package_lifecycle.t xCAT-test/native/package_upgrade_logging.t' + selinux_policy_test: + runs-on: ubuntu-24.04 + timeout-minutes: 20 + container: + image: almalinux:10 + options: --privileged + steps: + - uses: actions/checkout@v6 + - name: Install policy test dependencies + run: | + dnf install -y epel-release + dnf --enablerepo=crb install -y make selinux-policy-devel selinux-policy-targeted libselinux-utils policycoreutils attr perl perl-Capture-Tiny perl-Test-Simple perl-Test-Harness + - name: Look up and relabel the xCAT file contexts + run: | + mkdir -p /mnt/labels + mount -t tmpfs -o size=1g tmpfs /mnt/labels + TMPDIR=/mnt/labels prove -v xCAT-test/native/selinux_file_contexts.t diff --git a/xCAT-test/native/README.md b/xCAT-test/native/README.md index 58d3240c4f..ece2a64e3c 100644 --- a/xCAT-test/native/README.md +++ b/xCAT-test/native/README.md @@ -111,6 +111,21 @@ an ordinary user with `XCAT_TEST_PG_BINDIR` set to the directory containing XCAT_TEST_PG_BINDIR=/usr/bin prove xCAT-test/native/pgsqlsetup_native_schema.t ``` +The SELinux policy case runs as root in a disposable EL10 container. It needs +`make`, `selinux-policy-devel`, `selinux-policy-targeted`, `libselinux-utils`, +`policycoreutils`, `attr` and Capture::Tiny. It installs the xCAT module into a +private copy of the targeted store, so the host policy does not change. Set +`TMPDIR` to a file system that stores `security.selinux` attributes, such as a +tmpfs: + +``` +TMPDIR=/mnt/labels prove xCAT-test/native/selinux_file_contexts.t +``` + +The case looks up labels in the compiled `file_contexts` and relabels a copy of +`/install` with `setfiles`. It does not need an enforcing kernel, and it does not +run the rpm scriptlets. + Check the TAP output for skipped prerequisites. A successful `prove` exit with skipped cases does not qualify those cases. diff --git a/xCAT-test/native/selinux_file_contexts.t b/xCAT-test/native/selinux_file_contexts.t new file mode 100644 index 0000000000..c0e15681b0 --- /dev/null +++ b/xCAT-test/native/selinux_file_contexts.t @@ -0,0 +1,150 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +use Capture::Tiny qw(capture); +use File::Path qw(make_path); +use File::Temp qw(tempdir); +use FindBin; +use Test::More; + +# semodule writes a private copy of the targeted policy store, and setfiles +# writes security.selinux attributes. Both need root, not an enforcing kernel. +die "run as root\n" unless $> == 0; +die "requires the targeted policy store\n" unless -d '/var/lib/selinux/targeted'; + +my $work = tempdir(CLEANUP => 1); + +#--- + +=head3 run + + Descriptions: Run a command and die with its output when it fails. + Arguments: The command and its arguments. + Returns: The standard output of the command. + +=cut + +#--- +sub run { + my (@command) = @_; + my ($out, $err, $rc) = capture { system(@command) }; + die "@command: rc=$rc\n$out$err" if $rc != 0; + return $out; +} + +my $build = "$work/build"; +make_path($build); +run('cp', glob("$FindBin::Bin/../../xCAT-selinux/xcat.*"), $build); +run('make', '-C', $build, '-f', '/usr/share/selinux/devel/Makefile', 'xcat.pp'); + +# The policy root is a copy of the host store, so the host policy is unchanged. +my $root = "$work/root"; +make_path("$root/etc/selinux", "$root/var/lib/selinux"); +run('cp', '-a', '/etc/selinux/config', '/etc/selinux/targeted', "$root/etc/selinux/"); +run('cp', '-a', '/var/lib/selinux/targeted', "$root/var/lib/selinux/"); +my $fc = "$root/etc/selinux/targeted/contexts/files/file_contexts"; + +#--- + +=head3 install_module + + Descriptions: Install xcat.pp into the private store, as %post does. + Arguments: None. + Returns: Nothing. + +=cut + +#--- +sub install_module { + run('semodule', '-p', $root, '-i', "$build/xcat.pp"); +} + +#--- + +=head3 lookup + + Descriptions: Ask libselinux for the default label of a path. + Arguments: The path on the management node. + Returns: The type, or <> when restorecon leaves the path alone. + +=cut + +#--- +sub lookup { + my ($path) = @_; + my $out = run('matchpathcon', '-f', $fc, $path); + return $1 if $out =~ /^\S+\s+[^:\s]+:[^:\s]+:(\w+):/; + return '<>' if $out =~ /^\S+\s+<>$/; + die "matchpathcon $path: unexpected output: $out"; +} + +install_module(); + +my @images = ( + '/install/netboot/rhels9/x86_64/compute', + '/install/netboot/custom-image', + '/install/custom/images/rhels9-compute', +); +my @guest = map { ("$_/rootimg/etc/shadow", "$_/rootimg-statelite/etc/shadow") } @images; +my @published = map { + my $dir = $_; + map { "$dir/$_" } qw(kernel initrd-stateless.gz initrd-statelite.gz rootimg.cpio.gz + rootimg.cpio.gz.sha256 rootimg.cpio.gz.metainfo rootimg.tar.xz rootimg.sfs + rootimg-statelite.gz) +} @images; + +foreach my $path (@guest) { + is(lookup($path), '<>', "restorecon leaves $path alone"); +} +foreach my $path (@published, '/install', '/install/postscripts/xcatdsklspost') { + is(lookup($path), 'public_content_t', "$path is public_content_t"); +} + +# A management node: each guest file keeps the label of its own operating +# system, and each published file starts with the label it had before the +# package was installed. +my $mn = "$work/mn"; +my %before; +$before{$_} = 'shadow_t' foreach @guest; +$before{$_} = 'default_t' foreach @published; +foreach my $path (sort keys %before) { + my $file = "$mn$path"; + (my $dir = $file) =~ s{/[^/]+$}{}; + make_path($dir); + open(my $fh, '>', $file) or die "$file: $!"; + close($fh); + run('setfattr', '-n', 'security.selinux', '-v', "system_u:object_r:$before{$path}:s0", $file); +} + +#--- + +=head3 label_of + + Descriptions: Read the security.selinux attribute of a file. + Arguments: The path on the management node. + Returns: The type in the attribute. + +=cut + +#--- +sub label_of { + my ($path) = @_; + my $value = run('getfattr', '--only-values', '-n', 'security.selinux', "$mn$path"); + return $value =~ /^[^:]+:[^:]+:(\w+):/ ? $1 : $value; +} + +# %posttrans runs restorecon -R /install. setfiles -F applies the same +# file_contexts to the copy and also resets the user and role fields. +foreach my $step ('install', 'upgrade') { + install_module() if $step eq 'upgrade'; + run('setfiles', '-F', '-r', $mn, $fc, "$mn/install"); + foreach my $path (@guest) { + is(label_of($path), $before{$path}, "after the $step relabel $path keeps $before{$path}"); + } + foreach my $path (@published) { + is(label_of($path), 'public_content_t', "after the $step relabel $path is public_content_t"); + } +} + +done_testing(); diff --git a/xCAT-test/unit/xcat_selinux_package.t b/xCAT-test/unit/xcat_selinux_package.t index 8758b2e045..e501ef7d55 100644 --- a/xCAT-test/unit/xcat_selinux_package.t +++ b/xCAT-test/unit/xcat_selinux_package.t @@ -119,12 +119,8 @@ foreach my $section (qw(pre preun post postun posttrans)) { my @fc = grep { !/^\s*(?:#|$)/ } lines_of("$root/xCAT-selinux/xcat.fc"); my %fc = map { (split /\s+/, $_, 2) } @fc; -is($fc{'/install(/.*)?'}, 'gen_context(system_u:object_r:public_content_t,s0)', - '/install is public_content_t'); -is($fc{'/install/netboot/[^/]+/[^/]+/[^/]+/rootimg(/.*)?'}, '<>', - '... and restorecon leaves a rootimg alone'); -is($fc{'/install/netboot/[^/]+/[^/]+/[^/]+/rootimg-statelite(/.*)?'}, '<>', - '... and a statelite rootimg too'); +# The labels of /install and of the image trees are looked up in the compiled +# policy by xCAT-test/native/selinux_file_contexts.t. ok(!(grep { m{^/tftpboot} } keys %fc), '/tftpboot keeps the stock tftpdir_t'); # The type of an fc entry, dropping the file-type flag that executables carry.