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

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/<os>/<arch>/<profile>/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>
This commit is contained in:
Daniel Hilst
2026-10-16 11:16:55 -03:00
parent 11c91fd512
commit 905c63e47c
4 changed files with 185 additions and 7 deletions
+18 -1
View File
@@ -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
+15
View File
@@ -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.
+150
View File
@@ -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 <<none>> 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 '<<none>>' if $out =~ /^\S+\s+<<none>>$/;
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), '<<none>>', "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();
+2 -6
View File
@@ -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(/.*)?'}, '<<none>>',
'... and restorecon leaves a rootimg alone');
is($fc{'/install/netboot/[^/]+/[^/]+/[^/]+/rootimg-statelite(/.*)?'}, '<<none>>',
'... 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.