From fabfaedc8953389a4a96130cee5b2a75165e8d62 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Thu, 8 Oct 2026 14:36:53 -0300 Subject: [PATCH] feat(xcat-core): provision compute and service nodes with SELinux enabled Every compute and service node was provisioned with SELinux disabled. mknetboot added selinux=0, the kickstart templates said selinux --disable, and the genimage postinstall and the dracut hooks wrote SELINUX=disabled into the image. noderes.selinux and site.selinux had no consumer on the node side. The mode that xCAT::SELinux->node_modes resolves now reaches the node: - Kernel command line: mknetboot appends kcmdline_selinux(): selinux=0 for disabled, enforcing=0 for permissive, nothing for enforcing. An image built without the relabel hook (EL7 and older), and a squashfs image without labels, boot with selinux=0 and nodeset says why. Test: selinux_netboot_kcmdline.t. - Kickstart: #SELINUXMODE# and #SELINUXRELABEL# in xCAT::Template. The rh el8/9/10, riscv64 and openEuler templates use them; the %post ends with restorecon -RF over the paths the xCAT scripts write. mkinstall warns when a template cannot express the mode of the node. Test: selinux_kickstart_template.t. - genimage: the el8-10 postinstalls no longer write SELINUX=disabled, and the stateless xcatroot no longer writes /etc/sysconfig/selinux. genimage sets SELINUX=enforcing in the image, adds dracut 98selinux, and installs a pre-pivot hook that runs setfiles with the image file_contexts unless selinux=0 is set. Tests: selinux_image_config.t, selinux_relabel_hook.bats. - packimage -m squashfs stores the labels of the image policy through mksquashfs pseudo xattr definitions (squashfs-tools 4.6 or later), and writes rootimg.sfs.selinux beside the image. Test: selinux_squashfs_labels.t. - Service nodes mount /install and /tftpboot from the management node with context=public_content_t and tftpdir_t when SELinux is on. Test: selinux_sn_mount_options.t. - makedns no longer warns about SELinux. xcatprobe xcatmn checks the xcat module and the labels of installdir and tftpdir instead of failing on enforcing. Test: probe_utils_selinux_policy.t. The selinux/cases1 xcattest cases provision a diskful, a stateless and a squashfs node enforcing, and a stateless node with site.selinux=disabled. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- perl-xCAT/xCAT/SELinux.pm | 365 ++++++++++++++++++ xCAT-probe/lib/perl/probe_utils.pm | 41 ++ xCAT-probe/subcmds/xcatmn | 45 ++- xCAT-server/lib/perl/xCAT/Template.pm | 7 + xCAT-server/lib/xcat/plugins/AAsn.pm | 25 +- xCAT-server/lib/xcat/plugins/anaconda.pm | 27 +- xCAT-server/lib/xcat/plugins/ddns.pm | 7 - xCAT-server/lib/xcat/plugins/packimage.pm | 22 ++ .../install/openeuler/compute.openeuler.tmpl | 3 +- .../install/rh/compute.rhels10.riscv64.tmpl | 3 +- .../xcat/install/rh/compute.rhels10.tmpl | 3 +- .../share/xcat/install/rh/compute.rhels8.tmpl | 3 +- .../share/xcat/install/rh/compute.rhels9.tmpl | 3 +- .../rocky/service.rocky10.riscv64.tmpl | 3 +- .../rh/compute.rhels10.ppc64le.postinstall | 7 - .../rh/compute.rhels10.x86_64.postinstall | 7 - .../rh/compute.rhels8.ppc64le.postinstall | 7 - .../rh/compute.rhels8.x86_64.postinstall | 7 - .../rh/compute.rhels9.ppc64le.postinstall | 7 - .../rh/compute.rhels9.x86_64.postinstall | 7 - .../netboot/rh/dracut_047/install.netboot | 1 + .../rh/dracut_047/xcat-selinux-relabel.sh | 35 ++ .../share/xcat/netboot/rh/dracut_047/xcatroot | 4 - .../rh/dracut_105/stateless/module-setup.sh | 3 + .../stateless/xcat-selinux-relabel.sh | 1 + .../netboot/rh/dracut_105/stateless/xcatroot | 4 - xCAT-server/share/xcat/netboot/rh/genimage | 22 ++ .../rocky/compute.rocky10.riscv64.postinstall | 7 - xCAT-test/autotest/testcase/selinux/cases1 | 168 ++++++++ xCAT-test/bats/selinux_relabel_hook.bats | 126 ++++++ xCAT-test/native/netboot_profile_riscv64.t | 6 +- xCAT-test/unit/probe_utils_selinux_policy.t | 58 +++ xCAT-test/unit/selinux_image_config.t | 51 +++ xCAT-test/unit/selinux_kickstart_template.t | 118 ++++++ xCAT-test/unit/selinux_netboot_kcmdline.t | 48 +++ xCAT-test/unit/selinux_sn_mount_options.t | 38 ++ xCAT-test/unit/selinux_squashfs_labels.t | 116 ++++++ 37 files changed, 1298 insertions(+), 107 deletions(-) create mode 100755 xCAT-server/share/xcat/netboot/rh/dracut_047/xcat-selinux-relabel.sh create mode 120000 xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/xcat-selinux-relabel.sh create mode 100644 xCAT-test/autotest/testcase/selinux/cases1 create mode 100644 xCAT-test/bats/selinux_relabel_hook.bats create mode 100644 xCAT-test/unit/probe_utils_selinux_policy.t create mode 100644 xCAT-test/unit/selinux_image_config.t create mode 100644 xCAT-test/unit/selinux_kickstart_template.t create mode 100644 xCAT-test/unit/selinux_netboot_kcmdline.t create mode 100644 xCAT-test/unit/selinux_sn_mount_options.t create mode 100644 xCAT-test/unit/selinux_squashfs_labels.t diff --git a/perl-xCAT/xCAT/SELinux.pm b/perl-xCAT/xCAT/SELinux.pm index 7ad7ca3e34..37f04dc1d5 100644 --- a/perl-xCAT/xCAT/SELinux.pm +++ b/perl-xCAT/xCAT/SELinux.pm @@ -10,6 +10,9 @@ our $CONFIG_FILE = '/etc/selinux/config'; our @MODES = qw(enforcing permissive disabled); +# packimage writes this file beside rootimg.sfs when the squashfs carries the image labels. +our $SQUASHFS_LABELS = 'rootimg.sfs.selinux'; + # The OS families that ship SELinux and get the xCAT policy. SLES 15 and Ubuntu use AppArmor. our $POLICY_OS = qr/^(?:rhel|rhes|rhels|centos|alma|rocky|ol|fedora|openeuler|sl\d)/i; @@ -223,6 +226,368 @@ sub node_modes { return \%modes; } +#----------------------------------------------------------------------------- + +=head3 netboot_supported + + Descriptions: + Reports whether a stateless image of this OS can boot with SELinux on. + genimage builds these images from the dracut_047 or dracut_105 module, + which carries the pre-pivot hook that labels the RAM root. + Arguments: + osver - the OS of the image, for example rhels9.6 or openeuler24.03sp3 + Returns: + 1 when it can, 0 otherwise. + Example: + my $ok = xCAT::SELinux->netboot_supported($osver); + +=cut + +#----------------------------------------------------------------------------- +sub netboot_supported { + my ($class, $osver) = @_; + + return 0 unless defined $osver; + return 1 if $osver =~ /^openeuler/i; + if ($osver =~ /^(?:rhels|rhel|alma|rocky|ol|centos-stream|centos)(\d+)/i) { + return $1 >= 8 ? 1 : 0; + } + + return 0; +} + +#----------------------------------------------------------------------------- + +=head3 kcmdline_selinux + + Descriptions: + Gives the kernel arguments that select the SELinux mode of a + stateless node. One image serves every mode. + Arguments: + mode - the mode node_modes resolved for the node + osver - the OS of the image + Returns: + 'selinux=0' for disabled and for an image that cannot label its root, + 'enforcing=0' for permissive, and the empty string for enforcing. + Example: + $kcmdline .= ' ' . xCAT::SELinux->kcmdline_selinux($mode, $osver); + +=cut + +#----------------------------------------------------------------------------- +sub kcmdline_selinux { + my ($class, $mode, $osver) = @_; + + return 'selinux=0' unless defined $mode && $class->netboot_supported($osver); + return '' if $mode eq 'enforcing'; + return 'enforcing=0' if $mode eq 'permissive'; + return 'selinux=0'; +} + +#----------------------------------------------------------------------------- + +=head3 kickstart_relabel + + Descriptions: + Gives the %post command that relabels the files the xCAT post + scripts wrote. rpm labels what it installs; scripts that write in the + installer chroot can leave the label of the parent directory. + Arguments: + mode - the mode of the node + Returns: + The command, or the empty string when the mode is disabled. + +=cut + +#----------------------------------------------------------------------------- +sub kickstart_relabel { + my ($class, $mode) = @_; + + return '' unless defined $mode && ($mode eq 'enforcing' || $mode eq 'permissive'); + return '/usr/sbin/restorecon -RF /xcatpost /opt/xcat /etc /root /var/log' + . ' || echo "restorecon returned $?"'; +} + +#----------------------------------------------------------------------------- + +=head3 kickstart_mode + + Descriptions: + Reads the SELinux mode a rendered kickstart asks anaconda for. + Arguments: + text - the kickstart + Returns: + enforcing, permissive or disabled. A kickstart with no selinux line + gets the anaconda default, enforcing. + +=cut + +#----------------------------------------------------------------------------- +sub kickstart_mode { + my ($class, $text) = @_; + + my $mode = 'enforcing'; + foreach my $line (split /\n/, (defined $text ? $text : '')) { + next unless $line =~ /^\s*selinux\s+--(\w+)/; + my $word = lc($1); + $mode = $word eq 'disable' ? 'disabled' : $word; + } + + return $mode; +} + +#----------------------------------------------------------------------------- + +=head3 kickstart_mismatch + + Descriptions: + Compares the mode of a node with the mode its kickstart asks for. + A template that hard-codes the selinux line cannot follow the node. + Arguments: + mode - the mode node_modes resolved for the node + text - the rendered kickstart + Returns: + A warning, or undef when both agree. + +=cut + +#----------------------------------------------------------------------------- +sub kickstart_mismatch { + my ($class, $mode, $text) = @_; + + my $asked = $class->kickstart_mode($text); + return undef if !defined $mode || $mode eq $asked; + return "SELinux mode is $mode, but the install template sets $asked. " + . "Use selinux --#SELINUXMODE# in the template."; +} + +#----------------------------------------------------------------------------- + +=head3 write_image_config + + Descriptions: + Sets SELINUX=enforcing in /etc/selinux/config of a rootimg. The + kernel command line that mknetboot writes then selects the mode of + each node. + Arguments: + root - the rootimg directory + Returns: + 1 when the file was written, 0 when the image has no SELinux policy. + +=cut + +#----------------------------------------------------------------------------- +sub write_image_config { + my ($class, %args) = @_; + my $path = "$args{root}$CONFIG_FILE"; + + open(my $in, '<', $path) or return 0; + my @lines = <$in>; + close($in); + + my $found = 0; + foreach my $line (@lines) { + next if $line =~ /^\s*#/; + $found = 1 if $line =~ s/^(\s*SELINUX\s*=\s*)\S*/${1}enforcing/; + } + push @lines, "SELINUX=enforcing\n" unless $found; + + open(my $out, '>', $path) or return 0; + print $out @lines; + close($out); + + return 1; +} + +#----------------------------------------------------------------------------- + +=head3 image_file_contexts + + Descriptions: + Finds the file_contexts of the policy that an image boots with. + Arguments: + root - the image root directory + Returns: + The path of the file under root, or undef when there is none. + +=cut + +#----------------------------------------------------------------------------- +sub image_file_contexts { + my ($class, $root) = @_; + + my $type = 'targeted'; + if (open(my $fh, '<', "$root$CONFIG_FILE")) { + while (my $line = <$fh>) { + $type = $1 if $line =~ /^\s*SELINUXTYPE\s*=\s*(\S+)/; + } + close($fh); + } + my $path = "$root/etc/selinux/$type/contexts/files/file_contexts"; + + return -r $path ? $path : undef; +} + +#----------------------------------------------------------------------------- + +=head3 mksquashfs_supports_pseudo_xattr + + Descriptions: + Reports whether mksquashfs takes xattr pseudo definitions, which + squashfs-tools 4.6 added. + Arguments: + version - the output of mksquashfs -version + Returns: + 1 or 0. + +=cut + +#----------------------------------------------------------------------------- +sub mksquashfs_supports_pseudo_xattr { + my ($class, $version) = @_; + + return 0 unless defined $version && $version =~ /version\s+(\d+)\.(\d+)/; + return ($1 > 4 || ($1 == 4 && $2 >= 6)) ? 1 : 0; +} + +#----------------------------------------------------------------------------- + +=head3 label_paths + + Descriptions: + Looks up the default context of node paths in the file_contexts of + an image. matchpathcon reads the file and does not ask the policy of + this host, so an image type that this host does not know is fine. + Arguments: + file_contexts - the file_contexts of the image + type - file, dir, lnk_file, chr_file, blk_file, sock_file + or fifo_file + paths - a reference to a list of absolute paths on the node + Returns: + A hash reference: path => context. A path with no context is absent. + +=cut + +#----------------------------------------------------------------------------- +sub label_paths { + my ($class, $file_contexts, $type, $paths) = @_; + + my %contexts; + my @todo = @{$paths}; + while (my @batch = splice(@todo, 0, 1000)) { + open(my $fh, '-|', 'matchpathcon', '-m', $type, '-f', $file_contexts, @batch) + or return \%contexts; + while (my $line = <$fh>) { + chomp($line); + $contexts{$1} = $2 if $line =~ /^(.*)\t(\S+)$/ && $2 ne '<>'; + } + close($fh); + } + + return \%contexts; +} + +#----------------------------------------------------------------------------- + +=head3 squashfs_label_args + + Descriptions: + Prepares mksquashfs to store the labels of the image policy. It + writes a pseudo file with one security.selinux definition for each + path, and drops the labels that the temporary copy got on this host. + Arguments: + root - the temporary copy of the rootimg that mksquashfs reads + pseudo - the pseudo file to write + version - the output of mksquashfs -version + Returns: + (\@args) with the mksquashfs arguments, or (undef, $warning) when + the image cannot be labelled. + +=cut + +#----------------------------------------------------------------------------- +sub squashfs_label_args { + my ($class, %args) = @_; + my ($root, $pseudo) = @args{qw(root pseudo)}; + + return (undef, 'squashfs-tools 4.6 or later is needed to store SELinux labels in a squashfs image') + unless $class->mksquashfs_supports_pseudo_xattr($args{version}); + my $fc = $class->image_file_contexts($root); + return (undef, 'the image has no SELinux file_contexts') unless $fc; + + my %by_type; + require File::Find; + File::Find::find({ + no_chdir => 1, + wanted => sub { + my $path = $File::Find::name; + my $rel = substr($path, length($root)); + return if $rel =~ /\n/; + $rel = '/' if $rel eq ''; + push @{ $by_type{ _file_type($path) } }, $rel; + }, + }, $root); + + open(my $out, '>', $pseudo) or return (undef, "cannot write $pseudo: $!"); + foreach my $type (sort keys %by_type) { + my $contexts = $class->label_paths($fc, $type, $by_type{$type}); + foreach my $path (@{ $by_type{$type} }) { + next unless defined $contexts->{$path}; + print $out _pseudo_name($path), " x security.selinux=$contexts->{$path}\n"; + } + } + close($out); + + return ([ '-xattrs-exclude', '^security\.selinux$', '-pf', $pseudo ]); +} + +sub _file_type { + my ($path) = @_; + + return 'lnk_file' if -l $path; + return 'dir' if -d _; + return 'chr_file' if -c _; + return 'blk_file' if -b _; + return 'sock_file' if -S _; + return 'fifo_file' if -p _; + return 'file'; +} + +# mksquashfs reads a quoted pseudo name with backslash escapes, relative to the image root. +sub _pseudo_name { + my ($path) = @_; + + return '"/"' if $path eq '/'; + (my $name = substr($path, 1)) =~ s/(["\\])/\\$1/g; + return "\"$name\""; +} + +#----------------------------------------------------------------------------- + +=head3 nfs_mount_options + + Descriptions: + Gives the mount options for a tree that a service node mounts from + the management node. NFSv3 and NFSv4 without security_label carry no + per-file label, so the mount gets one type for every file. + Arguments: + base - the options without a context + type - the SELinux type of the tree, for example public_content_t + mode - the runtime mode of the service node + Returns: + The options. The kernel refuses context= when SELinux is off, so a + disabled node gets base unchanged. + +=cut + +#----------------------------------------------------------------------------- +sub nfs_mount_options { + my ($class, $base, $type, $mode) = @_; + + return $base if !defined $mode || $mode eq 'disabled'; + return "$base,context=system_u:object_r:$type:s0"; +} + sub _normalize_mode { my ($value) = @_; diff --git a/xCAT-probe/lib/perl/probe_utils.pm b/xCAT-probe/lib/perl/probe_utils.pm index 51f5573eb7..668170e26f 100644 --- a/xCAT-probe/lib/perl/probe_utils.pm +++ b/xCAT-probe/lib/perl/probe_utils.pm @@ -481,6 +481,47 @@ sub is_selinux_enforcing { #------------------------------------------ +=head3 + Description: + Decide the xcatmn verdict on SELinux. xCAT runs with SELinux on when + the xcat policy module is loaded and the xCAT trees carry their types. + Arguments: + mode: enforcing, permissive or disabled + modules: the output of semodule -l + labels: a reference to a list of [path, expected type, context of the path] + Returns: + (level, message) + level is "o" for ok, "w" for warning and "f" for failed +=cut + +#------------------------------------------ +sub selinux_policy_verdict { + my ($mode, $modules, $labels) = @_; + $mode = 'disabled' unless defined $mode; + + return ("o", "SELinux is disabled on current server") if ($mode eq 'disabled'); + + my @problems; + $modules = '' unless defined $modules; + unless (grep { /^xcat(?:\s|$)/ } split(/\n/, $modules)) { + push @problems, "The xcat SELinux module is not loaded. Install xCAT-selinux."; + } + foreach my $label (@{ $labels || [] }) { + my ($path, $expected, $context) = @{$label}; + my $type = (defined $context && $context =~ /^[^:]*:[^:]*:([^:]+)/) ? $1 : undef; + if (!defined $type) { + push @problems, "$path has no SELinux label, expected $expected. Run restorecon -R $path."; + } elsif ($type ne $expected) { + push @problems, "$path has type $type, expected $expected. Run restorecon -R $path."; + } + } + + return ("o", "SELinux is $mode on current server, and the xcat policy is in place") unless (@problems); + return (($mode eq 'enforcing' ? "f" : "w"), join("\n", @problems)); +} + +#------------------------------------------ + =head3 Description: Test if firewall is opened in current operating system diff --git a/xCAT-probe/subcmds/xcatmn b/xCAT-probe/subcmds/xcatmn index 8b5e1b9502..5da5fe83fb 100755 --- a/xCAT-probe/subcmds/xcatmn +++ b/xCAT-probe/subcmds/xcatmn @@ -92,8 +92,8 @@ sub do_main_job { $rst = 0 if ($flag eq "w"); $rc |= $rst; - #check if SElinux is disabled - ($rst, $flag) = check_selinux(\$checkpoint, \@error); + #check the SELinux mode and the xcat policy + ($rst, $flag) = check_selinux(\%sitetable, \$checkpoint, \@error); print_check_result($checkpoint, $flag, $rst, \@error); $rst = 0 if ($flag eq "w"); $rc |= $rst; @@ -683,29 +683,36 @@ sub check_disk { } sub check_selinux { - my $checkpoint_ref = shift; - my $error_ref = shift; - my $rst = 0; - my $flag = "w"; - my $msg = ""; + my $sitetable_ref = shift; + my $checkpoint_ref = shift; + my $error_ref = shift; - $$checkpoint_ref = "Checking SELinux is disabled..."; + $$checkpoint_ref = "Checking SELinux policy for xCAT..."; @$error_ref = (); + my $mode = "disabled"; if (probe_utils->is_selinux_enable()) { - $msg = "SELinux is enabled on current server"; - $rst = 1; - $flag = "w"; + $mode = probe_utils->is_selinux_enforcing() ? "enforcing" : "permissive"; } - if (probe_utils->is_selinux_enforcing()) { - $msg = "SELinux is enforcing on current server"; - $rst = 1; - $flag = "f"; + + my $modules = ""; + my @labels; + if ($mode ne "disabled") { + $modules = `semodule -l 2>/dev/null`; + my $installdir = $sitetable_ref->{installdir} || "/install"; + my $tftpdir = $sitetable_ref->{tftpdir} || "/tftpboot"; + foreach my $dir ([ $installdir, "public_content_t" ], [ $tftpdir, "tftpdir_t" ]) { + my $context = `stat -c %C $dir->[0] 2>/dev/null`; + chomp($context); + push @labels, [ @$dir, ($? == 0 ? $context : undef) ]; + } } - if ($rst) { - push @error_ref, "$msg"; - } - return ($rst, $flag); + + my ($flag, $msg) = probe_utils::selinux_policy_verdict($mode, $modules, \@labels); + return (0, $flag) if ($flag eq "o"); + + push @$error_ref, split(/\n/, $msg); + return (1, $flag); } sub check_firewall { diff --git a/xCAT-server/lib/perl/xCAT/Template.pm b/xCAT-server/lib/perl/xCAT/Template.pm index cf2bfef8ad..747ba01e04 100644 --- a/xCAT-server/lib/perl/xCAT/Template.pm +++ b/xCAT-server/lib/perl/xCAT/Template.pm @@ -18,6 +18,7 @@ use xCAT::TableUtils; use xCAT::NetworkUtils; use xCAT::PasswordUtils; use xCAT::MsgUtils; +use xCAT::SELinux; use XML::Simple; BEGIN @@ -393,6 +394,12 @@ sub subvars { $inc =~ s/#CRYPTORLOCKED:([^:]+):([^:]+):([^#]+)#/crydb_or_locked($1,$2,$3)/eg; $inc =~ s/#COMMAND:([^#]+)#/command($1)/eg; $inc =~ s/#KICKSTARTNET#/kickstartnetwork($platform)/eg; + if ($inc =~ /#SELINUX(?:MODE|RELABEL)#/) { + my $selinux = xCAT::SELinux->node_modes([$node])->{$node}; + $inc =~ s/#SELINUXMODE#/$selinux/g; + my $relabel = xCAT::SELinux->kickstart_relabel($selinux); + $inc =~ s/#SELINUXRELABEL#/$relabel/g; + } $inc =~ s/#MIRRORSPEC#/mirrorspec()/eg; $inc =~ s/#YAST2NET#/yast2network()/eg; $inc =~ s/#KICKSTARTBOOTLOADER#/kickstartbootloader()/eg; diff --git a/xCAT-server/lib/xcat/plugins/AAsn.pm b/xCAT-server/lib/xcat/plugins/AAsn.pm index 543967e3ac..09c97170ae 100644 --- a/xCAT-server/lib/xcat/plugins/AAsn.pm +++ b/xCAT-server/lib/xcat/plugins/AAsn.pm @@ -16,6 +16,7 @@ use xCAT::SvrUtils; use xCAT::TableUtils; use xCAT::NetworkUtils; use xCAT::DHCP::Backend; +use xCAT::SELinux; use xCAT::MsgUtils; use xCAT_plugin::dhcp; @@ -346,15 +347,15 @@ sub setupInstallloc # need to mount the directory my $cmd; + my $options = "rw,nolock"; my @nfsv4 = xCAT::TableUtils->get_site_attribute("useNFSv4onAIX"); if ($nfsv4[0] && ($nfsv4[0] =~ /1|Yes|yes|YES|Y|y/)) { - $cmd = "mount -o vers=4,rw,nolock $master:$installloc $installdir"; - } - else - { - $cmd = "mount -o rw,nolock $master:$installloc $installdir"; + $options = "vers=4,rw,nolock"; } + $options = xCAT::SELinux->nfs_mount_options($options, 'public_content_t', + xCAT::SELinux->runtime_mode()); + $cmd = "mount -o $options $master:$installloc $installdir"; system $cmd; if ($? > 0) { # error @@ -451,7 +452,9 @@ sub setupInstallloc { if ($nomount == 0) # then add the entry { -`echo "$master:$installloc $installdir nfs timeo=14,intr 1 2" >>/etc/fstab`; + my $options = xCAT::SELinux->nfs_mount_options('timeo=14,intr', 'public_content_t', + xCAT::SELinux->runtime_mode()); +`echo "$master:$installloc $installdir nfs $options 1 2" >>/etc/fstab`; } } @@ -1225,15 +1228,15 @@ sub setup_TFTP # need to mount the directory my $cmd; + my $options = "rw,nolock"; my @nfsv4 = xCAT::TableUtils->get_site_attribute("useNFSv4onAIX"); if ($nfsv4[0] && ($nfsv4[0] =~ /1|Yes|yes|YES|Y|y/)) { - $cmd = " mount -o vers=4,rw,nolock $tftphost:$tftpdir $tftpdir"; - } - else - { - $cmd = " mount -o rw,nolock $tftphost:$tftpdir $tftpdir"; + $options = "vers=4,rw,nolock"; } + $options = xCAT::SELinux->nfs_mount_options($options, 'tftpdir_t', + xCAT::SELinux->runtime_mode()); + $cmd = " mount -o $options $tftphost:$tftpdir $tftpdir"; system $cmd; if ($? > 0) { # error diff --git a/xCAT-server/lib/xcat/plugins/anaconda.pm b/xCAT-server/lib/xcat/plugins/anaconda.pm index a06a9fe5f7..4b20bd6a04 100644 --- a/xCAT-server/lib/xcat/plugins/anaconda.pm +++ b/xCAT-server/lib/xcat/plugins/anaconda.pm @@ -18,6 +18,7 @@ use xCAT::NetworkUtils; use xCAT::MsgUtils; use xCAT::SvrUtils; use xCAT::Yum; +use xCAT::SELinux; use Getopt::Long; Getopt::Long::Configure("bundling"); @@ -389,6 +390,9 @@ sub mknetboot $stateHash = $statetab->getNodesAttribs(\@nodes, ['statemnt']); } + my %selinux_why; + my $selinux_modes = xCAT::SELinux->node_modes(\@nodes, \%selinux_why); + foreach my $node (@nodes) { my $osver; @@ -952,10 +956,21 @@ sub mknetboot } } - # turn off the selinux - if ($osver =~ m/(fedora12|fedora13|(?:rhels|ol|alma|rocky)(?:[7-9]|10))/) { - $kcmdline .= " selinux=0 "; + my $selinux = $selinux_modes->{$node} || 'disabled'; + if ($selinux ne 'disabled') { + if (!xCAT::SELinux->netboot_supported($osver)) { + $selinux_why{$node} = "the $osver stateless image cannot label its root"; + $selinux = 'disabled'; + } elsif (defined($compressedrootimg) and $compressedrootimg eq 'rootimg.sfs' + and !-e "$rootimgdir/$xCAT::SELinux::SQUASHFS_LABELS") { + $selinux_why{$node} = "rootimg.sfs has no SELinux labels, run packimage with squashfs-tools 4.6 or later"; + $selinux = 'disabled'; + } } + if ($selinux_why{$node}) { + $callback->({ warning => ["$node: SELinux is disabled: $selinux_why{$node}"] }); + } + $kcmdline .= " " . xCAT::SELinux->kcmdline_selinux($selinux, $osver) . " "; # if kdump service is enbaled, add "crashkernel=" and "kdtarget=" if ($dump) { @@ -1090,6 +1105,7 @@ sub mkinstall my %hents = %{ $hmtab->getNodesAttribs(\@nodes, [ 'serialport', 'serialspeed', 'serialflow' ]) }; my %macents = %{ $mactab->getNodesAttribs(\@nodes, ['mac']) }; + my $selinux_modes = xCAT::SELinux->node_modes(\@nodes); require xCAT::Template; @@ -1388,6 +1404,11 @@ sub mkinstall xCAT::MsgUtils->report_node_error($callback, $node, $tmperr); next; } + if ($os =~ $xCAT::SELinux::POLICY_OS and open(my $ksfh, '<', "/$installroot/autoinst/$node")) { + my $mismatch = xCAT::SELinux->kickstart_mismatch($selinux_modes->{$node}, join('', <$ksfh>)); + close($ksfh); + $callback->({ warning => ["$node: $mismatch"] }) if $mismatch; + } #To support multiple paths for osimage.pkgdir. We require the first value of osimage.pkgdir # should be the os base pkgdir. diff --git a/xCAT-server/lib/xcat/plugins/ddns.pm b/xCAT-server/lib/xcat/plugins/ddns.pm index dc2cb5caa7..4db955585a 100644 --- a/xCAT-server/lib/xcat/plugins/ddns.pm +++ b/xCAT-server/lib/xcat/plugins/ddns.pm @@ -432,13 +432,6 @@ sub process_request { } } - # check for selinux disabled - my $rc = xCAT::Utils->isSELINUX(); - if ($rc == 0) - { - xCAT::SvrUtils::sendmsg([ 0, "Warning:SELINUX is not disabled. The makedns command will not be able to generate a complete DNS setup. Disable SELINUX and run the command again." ], $callback); - - } my @entries = xCAT::TableUtils->get_site_attribute("nameservers"); my $sitens = $entries[0]; unless (defined($site_entry)) { diff --git a/xCAT-server/lib/xcat/plugins/packimage.pm b/xCAT-server/lib/xcat/plugins/packimage.pm index 54293761b5..d89e951713 100644 --- a/xCAT-server/lib/xcat/plugins/packimage.pm +++ b/xCAT-server/lib/xcat/plugins/packimage.pm @@ -37,6 +37,7 @@ use File::Path; use xCAT::Utils; use xCAT::TableUtils; use xCAT::SvrUtils; +use xCAT::SELinux; use xCAT::PasswordUtils; use Digest::MD5 qw(md5_hex); @@ -642,6 +643,7 @@ sub process_request { } $oldmask = umask 0077; } elsif ($method =~ /squashfs/) { + unlink("$destdir/$xCAT::SELinux::SQUASHFS_LABELS"); $temppath = eval { mkdtemp("/tmp/packimage.$$.XXXXXXXX") } or return $native_failure->("Cannot create a staging directory: $@"); chmod 0755, $temppath; @@ -672,9 +674,26 @@ sub process_request { } return $native_failure->("mksquashfs not found, squashfs-tools rpm should be installed on the management node"); } + # The temporary copy has the labels of this host. Store the labels of the image policy. + my $selinux_pseudo; + if ($osver =~ $xCAT::SELinux::POLICY_OS) { + my ($label_args, $label_warning) = xCAT::SELinux->squashfs_label_args( + root => $temppath, + pseudo => "$temppath.selinux", + version => scalar(`mksquashfs -version 2>&1`), + ); + if ($label_args) { + $selinux_pseudo = "$temppath.selinux"; + $flags .= join('', map { " '$_'" } @{$label_args}); + } else { + $callback->({ warning => ["$label_warning. Nodes that boot the squashfs image get SELinux disabled."] }); + } + } + my $mksquashfs_command = "mksquashfs $temppath $archive_output $flags"; xCAT::Utils->runcmd($mksquashfs_command, 0, 1); my $rc = $::RUNCMD_RC; + unlink($selinux_pseudo) if $selinux_pseudo; if ($rc) { return $native_failure->("Command \"$mksquashfs_command\" failed"); } @@ -682,6 +701,9 @@ sub process_request { if ($rc) { return $native_failure->("Failed to clean up temp space"); } + if ($selinux_pseudo and open(my $marker, '>', "$destdir/$xCAT::SELinux::SQUASHFS_LABELS")) { + close($marker); + } } if (my $error = xCAT::SvrUtils->write_rootimg_checksum("$stage/$image_file")) { diff --git a/xCAT-server/share/xcat/install/openeuler/compute.openeuler.tmpl b/xCAT-server/share/xcat/install/openeuler/compute.openeuler.tmpl index 7ff6f5fdc6..77dda6d86e 100644 --- a/xCAT-server/share/xcat/install/openeuler/compute.openeuler.tmpl +++ b/xCAT-server/share/xcat/install/openeuler/compute.openeuler.tmpl @@ -13,7 +13,7 @@ clearpart --all --initlabel %include /tmp/partitionfile #XCAT_PARTITION_END# firewall --disable -selinux --disable +selinux --#SELINUXMODE# reboot %packages @@ -34,5 +34,6 @@ cat /tmp/pre-install.log >>/var/log/xcat/xcat.log echo "Running Kickstart Post-installation script..." #INCLUDE:#ENV:XCATROOT#/share/xcat/install/scripts/post.xcat.ng# #INCLUDE:#ENV:XCATROOT#/share/xcat/install/scripts/post.rhels8# +#SELINUXRELABEL# } &>>/var/log/xcat/xcat.log %end diff --git a/xCAT-server/share/xcat/install/rh/compute.rhels10.riscv64.tmpl b/xCAT-server/share/xcat/install/rh/compute.rhels10.riscv64.tmpl index dbf34f13c7..63e697b6bb 100644 --- a/xCAT-server/share/xcat/install/rh/compute.rhels10.riscv64.tmpl +++ b/xCAT-server/share/xcat/install/rh/compute.rhels10.riscv64.tmpl @@ -28,7 +28,7 @@ clearpart --all --initlabel # Do not configure any iptables rules firewall --disable -selinux --disable +selinux --#SELINUXMODE# reboot # riscv64: EL10 has no default crash kernel reservation for this architecture @@ -63,6 +63,7 @@ echo "Running Kickstart Post-installation script..." #INCLUDE:#ENV:XCATROOT#/share/xcat/install/scripts/post.xcat.rhels10# #INCLUDE:#ENV:XCATROOT#/share/xcat/install/scripts/post.rhels10.riscv64# #INCLUDE:#ENV:XCATROOT#/share/xcat/install/scripts/post.rhels10# +#SELINUXRELABEL# } &>>/var/log/xcat/xcat.log %end diff --git a/xCAT-server/share/xcat/install/rh/compute.rhels10.tmpl b/xCAT-server/share/xcat/install/rh/compute.rhels10.tmpl index 90b60aea85..3c90ba493d 100644 --- a/xCAT-server/share/xcat/install/rh/compute.rhels10.tmpl +++ b/xCAT-server/share/xcat/install/rh/compute.rhels10.tmpl @@ -28,7 +28,7 @@ clearpart --all --initlabel # Do not configure any iptables rules firewall --disable -selinux --disable +selinux --#SELINUXMODE# reboot %packages @@ -49,5 +49,6 @@ cat /tmp/pre-install.log >>/var/log/xcat/xcat.log echo "Running Kickstart Post-installation script..." #INCLUDE:#ENV:XCATROOT#/share/xcat/install/scripts/post.xcat.rhels10# #INCLUDE:#ENV:XCATROOT#/share/xcat/install/scripts/post.rhels10# +#SELINUXRELABEL# } &>>/var/log/xcat/xcat.log %end diff --git a/xCAT-server/share/xcat/install/rh/compute.rhels8.tmpl b/xCAT-server/share/xcat/install/rh/compute.rhels8.tmpl index ddbec300db..64daf4a34f 100644 --- a/xCAT-server/share/xcat/install/rh/compute.rhels8.tmpl +++ b/xCAT-server/share/xcat/install/rh/compute.rhels8.tmpl @@ -28,7 +28,7 @@ clearpart --all --initlabel # Do not configure any iptables rules firewall --disable -selinux --disable +selinux --#SELINUXMODE# reboot %packages @@ -54,5 +54,6 @@ cat /tmp/pre-install.log >>/var/log/xcat/xcat.log echo "Running Kickstart Post-installation script..." #INCLUDE:#ENV:XCATROOT#/share/xcat/install/scripts/post.xcat.ng# #INCLUDE:#ENV:XCATROOT#/share/xcat/install/scripts/post.rhels8# +#SELINUXRELABEL# } &>>/var/log/xcat/xcat.log %end diff --git a/xCAT-server/share/xcat/install/rh/compute.rhels9.tmpl b/xCAT-server/share/xcat/install/rh/compute.rhels9.tmpl index 747703edb9..6cb3a98883 100644 --- a/xCAT-server/share/xcat/install/rh/compute.rhels9.tmpl +++ b/xCAT-server/share/xcat/install/rh/compute.rhels9.tmpl @@ -28,7 +28,7 @@ clearpart --all --initlabel # Do not configure any iptables rules firewall --disable -selinux --disable +selinux --#SELINUXMODE# reboot %packages @@ -49,5 +49,6 @@ cat /tmp/pre-install.log >>/var/log/xcat/xcat.log echo "Running Kickstart Post-installation script..." #INCLUDE:#ENV:XCATROOT#/share/xcat/install/scripts/post.xcat.ng# #INCLUDE:#ENV:XCATROOT#/share/xcat/install/scripts/post.rhels8# +#SELINUXRELABEL# } &>>/var/log/xcat/xcat.log %end diff --git a/xCAT-server/share/xcat/install/rocky/service.rocky10.riscv64.tmpl b/xCAT-server/share/xcat/install/rocky/service.rocky10.riscv64.tmpl index b7c6dc41e5..bc1cc270bd 100644 --- a/xCAT-server/share/xcat/install/rocky/service.rocky10.riscv64.tmpl +++ b/xCAT-server/share/xcat/install/rocky/service.rocky10.riscv64.tmpl @@ -28,7 +28,7 @@ clearpart --all --initlabel # Do not configure any iptables rules firewall --disable -selinux --disable +selinux --#SELINUXMODE# reboot # riscv64: EL10 has no default crash kernel reservation for this architecture @@ -63,6 +63,7 @@ echo "Running Kickstart Post-installation script..." #INCLUDE:#ENV:XCATROOT#/share/xcat/install/scripts/post.xcat.ng# #INCLUDE:#ENV:XCATROOT#/share/xcat/install/scripts/post.rhels10.riscv64# #INCLUDE:#ENV:XCATROOT#/share/xcat/install/scripts/post.rhels8# +#SELINUXRELABEL# } &>>/var/log/xcat/xcat.log %end diff --git a/xCAT-server/share/xcat/netboot/rh/compute.rhels10.ppc64le.postinstall b/xCAT-server/share/xcat/netboot/rh/compute.rhels10.ppc64le.postinstall index dd2576c8f7..e29e51aeec 100755 --- a/xCAT-server/share/xcat/netboot/rh/compute.rhels10.ppc64le.postinstall +++ b/xCAT-server/share/xcat/netboot/rh/compute.rhels10.ppc64le.postinstall @@ -32,13 +32,6 @@ END #cons:12345:respawn:/sbin/smart_agetty -L 38400 console echo "co:2345:respawn:/sbin/agetty -L 38400 console" >> $installroot/etc/inittab -#-- Disable SELinux in the rootimg -#-- Redhat 7.3 will install selinux-policy and selinux is enabled by default -#-- Need to disable selinux, otherwise, the booting will hang on "Loading selinux policy" -if [ -f "$installroot/etc/selinux/config" ] -then - sed -i 's/SELINUX=enforcing\|permissive/SELINUX=disabled/' $installroot/etc/selinux/config -fi #--for redhat 8 and 8.1 #-- Need to disable firewalld, otherwise, the remoteshell script will not able to get all the SSH keys diff --git a/xCAT-server/share/xcat/netboot/rh/compute.rhels10.x86_64.postinstall b/xCAT-server/share/xcat/netboot/rh/compute.rhels10.x86_64.postinstall index 2ad53bb406..8f99e37345 100755 --- a/xCAT-server/share/xcat/netboot/rh/compute.rhels10.x86_64.postinstall +++ b/xCAT-server/share/xcat/netboot/rh/compute.rhels10.x86_64.postinstall @@ -32,13 +32,6 @@ END #echo "co:2345:respawn:/sbin/agetty -L 38400 console" >> $installroot/etc/inittab -#-- Disable SELinux in the rootimg -#-- Redhat 7.3 will install selinux-policy and selinux is enabled by default -#-- Need to disable selinux, otherwise, the booting will hang on "Loading selinux policy" -if [ -f "$installroot/etc/selinux/config" ] -then - sed -i 's/SELINUX=enforcing\|permissive/SELINUX=disabled/' $installroot/etc/selinux/config -fi #--for redhat 8 and 8.1 #-- Need to disable firewalld, otherwise, the remoteshell script will not able to get all the SSH keys FIREWALLD="$installroot/etc/systemd/system/multi-user.target.wants/firewalld.service" diff --git a/xCAT-server/share/xcat/netboot/rh/compute.rhels8.ppc64le.postinstall b/xCAT-server/share/xcat/netboot/rh/compute.rhels8.ppc64le.postinstall index dd2576c8f7..e29e51aeec 100755 --- a/xCAT-server/share/xcat/netboot/rh/compute.rhels8.ppc64le.postinstall +++ b/xCAT-server/share/xcat/netboot/rh/compute.rhels8.ppc64le.postinstall @@ -32,13 +32,6 @@ END #cons:12345:respawn:/sbin/smart_agetty -L 38400 console echo "co:2345:respawn:/sbin/agetty -L 38400 console" >> $installroot/etc/inittab -#-- Disable SELinux in the rootimg -#-- Redhat 7.3 will install selinux-policy and selinux is enabled by default -#-- Need to disable selinux, otherwise, the booting will hang on "Loading selinux policy" -if [ -f "$installroot/etc/selinux/config" ] -then - sed -i 's/SELINUX=enforcing\|permissive/SELINUX=disabled/' $installroot/etc/selinux/config -fi #--for redhat 8 and 8.1 #-- Need to disable firewalld, otherwise, the remoteshell script will not able to get all the SSH keys diff --git a/xCAT-server/share/xcat/netboot/rh/compute.rhels8.x86_64.postinstall b/xCAT-server/share/xcat/netboot/rh/compute.rhels8.x86_64.postinstall index 2ad53bb406..8f99e37345 100755 --- a/xCAT-server/share/xcat/netboot/rh/compute.rhels8.x86_64.postinstall +++ b/xCAT-server/share/xcat/netboot/rh/compute.rhels8.x86_64.postinstall @@ -32,13 +32,6 @@ END #echo "co:2345:respawn:/sbin/agetty -L 38400 console" >> $installroot/etc/inittab -#-- Disable SELinux in the rootimg -#-- Redhat 7.3 will install selinux-policy and selinux is enabled by default -#-- Need to disable selinux, otherwise, the booting will hang on "Loading selinux policy" -if [ -f "$installroot/etc/selinux/config" ] -then - sed -i 's/SELINUX=enforcing\|permissive/SELINUX=disabled/' $installroot/etc/selinux/config -fi #--for redhat 8 and 8.1 #-- Need to disable firewalld, otherwise, the remoteshell script will not able to get all the SSH keys FIREWALLD="$installroot/etc/systemd/system/multi-user.target.wants/firewalld.service" diff --git a/xCAT-server/share/xcat/netboot/rh/compute.rhels9.ppc64le.postinstall b/xCAT-server/share/xcat/netboot/rh/compute.rhels9.ppc64le.postinstall index dd2576c8f7..e29e51aeec 100755 --- a/xCAT-server/share/xcat/netboot/rh/compute.rhels9.ppc64le.postinstall +++ b/xCAT-server/share/xcat/netboot/rh/compute.rhels9.ppc64le.postinstall @@ -32,13 +32,6 @@ END #cons:12345:respawn:/sbin/smart_agetty -L 38400 console echo "co:2345:respawn:/sbin/agetty -L 38400 console" >> $installroot/etc/inittab -#-- Disable SELinux in the rootimg -#-- Redhat 7.3 will install selinux-policy and selinux is enabled by default -#-- Need to disable selinux, otherwise, the booting will hang on "Loading selinux policy" -if [ -f "$installroot/etc/selinux/config" ] -then - sed -i 's/SELINUX=enforcing\|permissive/SELINUX=disabled/' $installroot/etc/selinux/config -fi #--for redhat 8 and 8.1 #-- Need to disable firewalld, otherwise, the remoteshell script will not able to get all the SSH keys diff --git a/xCAT-server/share/xcat/netboot/rh/compute.rhels9.x86_64.postinstall b/xCAT-server/share/xcat/netboot/rh/compute.rhels9.x86_64.postinstall index 2ad53bb406..8f99e37345 100755 --- a/xCAT-server/share/xcat/netboot/rh/compute.rhels9.x86_64.postinstall +++ b/xCAT-server/share/xcat/netboot/rh/compute.rhels9.x86_64.postinstall @@ -32,13 +32,6 @@ END #echo "co:2345:respawn:/sbin/agetty -L 38400 console" >> $installroot/etc/inittab -#-- Disable SELinux in the rootimg -#-- Redhat 7.3 will install selinux-policy and selinux is enabled by default -#-- Need to disable selinux, otherwise, the booting will hang on "Loading selinux policy" -if [ -f "$installroot/etc/selinux/config" ] -then - sed -i 's/SELINUX=enforcing\|permissive/SELINUX=disabled/' $installroot/etc/selinux/config -fi #--for redhat 8 and 8.1 #-- Need to disable firewalld, otherwise, the remoteshell script will not able to get all the SSH keys FIREWALLD="$installroot/etc/systemd/system/multi-user.target.wants/firewalld.service" diff --git a/xCAT-server/share/xcat/netboot/rh/dracut_047/install.netboot b/xCAT-server/share/xcat/netboot/rh/dracut_047/install.netboot index 0be90b3ec5..d9160f1cc0 100755 --- a/xCAT-server/share/xcat/netboot/rh/dracut_047/install.netboot +++ b/xCAT-server/share/xcat/netboot/rh/dracut_047/install.netboot @@ -8,6 +8,7 @@ dracut_install ethtool inst "$moddir/xcat-updateflag" "/tmp/updateflag" inst "$moddir/xcatroot" "/sbin/xcatroot" inst_hook cmdline 10 "$moddir/xcat-cmdline.sh" +inst_hook pre-pivot 60 "$moddir/xcat-selinux-relabel.sh" for file in /etc/udev/rules.d/*;do grep -qi xcat $file && inst $file $file done diff --git a/xCAT-server/share/xcat/netboot/rh/dracut_047/xcat-selinux-relabel.sh b/xCAT-server/share/xcat/netboot/rh/dracut_047/xcat-selinux-relabel.sh new file mode 100755 index 0000000000..d5cbd581f6 --- /dev/null +++ b/xCAT-server/share/xcat/netboot/rh/dracut_047/xcat-selinux-relabel.sh @@ -0,0 +1,35 @@ +#!/bin/sh +# Label the stateless root with the policy of the image. dracut 98selinux loads that +# policy at pre-pivot 50, and systemd cannot start from an unlabelled tmpfs root. + +xcat_selinux_relabel() +{ + getarg selinux=0 >/dev/null && return 0 + + # A squashfs root carries its labels, and a relabel would copy every file to the overlay. + [ -e "${xcat_sfs_image:-/rootimg.sfs}" ] && return 0 + + local config="$NEWROOT/etc/selinux/config" mode type fc setfiles rc + [ -r "$config" ] || return 0 + mode=$(sed -n 's/^[[:space:]]*SELINUX[[:space:]]*=[[:space:]]*//p' "$config" | tail -n 1) + [ "$mode" = disabled ] && return 0 + type=$(sed -n 's/^[[:space:]]*SELINUXTYPE[[:space:]]*=[[:space:]]*//p' "$config" | tail -n 1) + fc="/etc/selinux/${type:-targeted}/contexts/files/file_contexts" + if [ ! -r "$NEWROOT$fc" ]; then + warn "xCAT: $fc is not in the image, the root is not labelled" + return 0 + fi + + setfiles=/usr/sbin/setfiles + [ -x "$NEWROOT$setfiles" ] || setfiles=/sbin/setfiles + + mount -t proc proc "$NEWROOT/proc" + mount --bind /sys "$NEWROOT/sys" + chroot "$NEWROOT" "$setfiles" -F -e /proc -e /sys -e /dev "$fc" / + rc=$? + umount "$NEWROOT/sys" + umount "$NEWROOT/proc" + [ "$rc" -eq 0 ] || warn "xCAT: setfiles returned $rc" +} + +xcat_selinux_relabel diff --git a/xCAT-server/share/xcat/netboot/rh/dracut_047/xcatroot b/xCAT-server/share/xcat/netboot/rh/dracut_047/xcatroot index 99b7b23317..0e6b683039 100755 --- a/xCAT-server/share/xcat/netboot/rh/dracut_047/xcatroot +++ b/xCAT-server/share/xcat/netboot/rh/dracut_047/xcatroot @@ -412,10 +412,6 @@ fi [ "$xcatdebugmode" > "0" ] && logger -t $log_label -p local4.debug "Saving $NEWROOT/etc/resolv.conf" cp /etc/resolv.conf "$NEWROOT/etc/" -if [ -d "$NEWROOT/etc/sysconfig" -a ! -e "$NEWROOT/etc/sysconfig/selinux" ]; then - [ "$xcatdebugmode" > "0" ] && logger -t $log_label -p local4.debug "disable selinux ..." - echo "SELINUX=disabled" >> "$NEWROOT/etc/sysconfig/selinux" -fi logger $SYSLOGHOST -t $log_label -p local4.info "Exiting xcatroot..." # inject new exit_if_exists diff --git a/xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/module-setup.sh b/xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/module-setup.sh index b150bd42c4..28bc073a65 100644 --- a/xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/module-setup.sh +++ b/xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/module-setup.sh @@ -28,6 +28,9 @@ install() { # cmdline hook inst_hook cmdline 10 "$moddir/xcat-cmdline.sh" + # After 98selinux loads the policy of the image at pre-pivot 50. + inst_hook pre-pivot 60 "$moddir/xcat-selinux-relabel.sh" + # udev rules with "xcat" for file in /etc/udev/rules.d/*; do if grep -qi xcat "$file"; then diff --git a/xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/xcat-selinux-relabel.sh b/xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/xcat-selinux-relabel.sh new file mode 120000 index 0000000000..51972524cd --- /dev/null +++ b/xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/xcat-selinux-relabel.sh @@ -0,0 +1 @@ +../../dracut_047/xcat-selinux-relabel.sh \ No newline at end of file diff --git a/xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/xcatroot b/xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/xcatroot index 99b7b23317..0e6b683039 100755 --- a/xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/xcatroot +++ b/xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/xcatroot @@ -412,10 +412,6 @@ fi [ "$xcatdebugmode" > "0" ] && logger -t $log_label -p local4.debug "Saving $NEWROOT/etc/resolv.conf" cp /etc/resolv.conf "$NEWROOT/etc/" -if [ -d "$NEWROOT/etc/sysconfig" -a ! -e "$NEWROOT/etc/sysconfig/selinux" ]; then - [ "$xcatdebugmode" > "0" ] && logger -t $log_label -p local4.debug "disable selinux ..." - echo "SELINUX=disabled" >> "$NEWROOT/etc/sysconfig/selinux" -fi logger $SYSLOGHOST -t $log_label -p local4.info "Exiting xcatroot..." # inject new exit_if_exists diff --git a/xCAT-server/share/xcat/netboot/rh/genimage b/xCAT-server/share/xcat/netboot/rh/genimage index e6c2543ae3..231ffcb8ca 100755 --- a/xCAT-server/share/xcat/netboot/rh/genimage +++ b/xCAT-server/share/xcat/netboot/rh/genimage @@ -20,6 +20,7 @@ use FindBin; use lib "$FindBin::Bin/../imgutils"; use imgutils; use xCAT::Utils; +use xCAT::SELinux; use Digest::MD5 qw(md5_hex); #use strict; @@ -822,6 +823,9 @@ xdie("Error: failed to generate the initial ramdisk: native image has no dracut imgutils::disable_vendor_repositories($rootimg_dir); +# The kernel command line that mknetboot writes selects the SELinux mode of each node. +xCAT::SELinux->write_image_config(root => $rootimg_dir); + #-- run postinstall script unless ($imagename) { $postinstall_filename = imgutils::get_profile_def_filename($osver, $profile, $arch, $customdir, "postinstall"); @@ -1092,6 +1096,7 @@ sub mkinitrd_dracut { 'installkernel', 'module-setup.sh', 'xcat-cmdline.sh', + 'xcat-selinux-relabel.sh', 'xcatroot', 'xcat-updateflag', ], @@ -1167,6 +1172,12 @@ sub mkinitrd_dracut { } } + # 98selinux loads the policy of the image before the xCAT relabel hook runs. + my @selinux_module = glob($dracutmoduledir . "[0-9]*selinux"); + if (-r "$dracutmpath/xcat-selinux-relabel.sh" and @selinux_module) { + $dracutmodulelist .= " selinux"; + } + # update etc/dracut.conf open($DRACUTCONF, '>', "$rootimg_dir/etc/dracut.conf"); print $DRACUTCONF qq{dracutmodules+=" $dracutmodulelist "\n}; @@ -1245,6 +1256,11 @@ sub mkinitrd_dracut { $perm = (stat("$fullpath/$dracutdir/installkernel"))[2]; chmod($perm & 07777, "$dracutmpath/installkernel"); + if (-r "$fullpath/$dracutdir/xcat-selinux-relabel.sh") { + cp("$fullpath/$dracutdir/xcat-selinux-relabel.sh", "$dracutmpath/"); + chmod(0755, "$dracutmpath/xcat-selinux-relabel.sh"); + } + my $dracutmodulelist = "xcat nfs base network kernel-modules syslog"; if (defined(imgutils::openeuler_release_version($osver)) ? scalar(glob($dracutmoduledir . '[0-9]*network-manager')) @@ -1259,6 +1275,12 @@ sub mkinitrd_dracut { } } + # 98selinux loads the policy of the image before the xCAT relabel hook runs. + my @selinux_module = glob($dracutmoduledir . "[0-9]*selinux"); + if (-r "$dracutmpath/xcat-selinux-relabel.sh" and @selinux_module) { + $dracutmodulelist .= " selinux"; + } + # update etc/dracut.conf open($DRACUTCONF, '>', "$rootimg_dir/etc/dracut.conf"); print $DRACUTCONF qq{dracutmodules+=" $dracutmodulelist "\n}; diff --git a/xCAT-server/share/xcat/netboot/rocky/compute.rocky10.riscv64.postinstall b/xCAT-server/share/xcat/netboot/rocky/compute.rocky10.riscv64.postinstall index 015cfec0f9..0cc5674cdc 100755 --- a/xCAT-server/share/xcat/netboot/rocky/compute.rocky10.riscv64.postinstall +++ b/xCAT-server/share/xcat/netboot/rocky/compute.rocky10.riscv64.postinstall @@ -32,13 +32,6 @@ END #echo "co:2345:respawn:/sbin/agetty -L 38400 console" >> $installroot/etc/inittab -#-- Disable SELinux in the rootimg -#-- Redhat 7.3 will install selinux-policy and selinux is enabled by default -#-- Need to disable selinux, otherwise, the booting will hang on "Loading selinux policy" -if [ -f "$installroot/etc/selinux/config" ] -then - sed -i 's/SELINUX=enforcing\|permissive/SELINUX=disabled/' $installroot/etc/selinux/config -fi #--for redhat 8 and 8.1 #-- Need to disable firewalld, otherwise, the remoteshell script will not able to get all the SSH keys FIREWALLD="$installroot/etc/systemd/system/multi-user.target.wants/firewalld.service" diff --git a/xCAT-test/autotest/testcase/selinux/cases1 b/xCAT-test/autotest/testcase/selinux/cases1 new file mode 100644 index 0000000000..e73a1f1e32 --- /dev/null +++ b/xCAT-test/autotest/testcase/selinux/cases1 @@ -0,0 +1,168 @@ +start:selinux_cn_diskful_enforcing +description:on an enforcing management node, a diskful compute node with noderes.selinux=enforcing boots enforcing, with correct labels and no denial +label:selinux,provision +os:Linux +cmd:getenforce +check:rc==0 +check:output=~^Enforcing$ +cmd:sh -c 'date "+%x %T" > /tmp/selinux_cn_diskful.start' +check:rc==0 +cmd:chdef -t node -o $$CN selinux=enforcing +check:rc==0 +cmd:/opt/xcat/share/xcat/tools/autotest/testcase/commoncmd/retry_install.sh $$CN __GETNODEATTR($$CN,os)__-__GETNODEATTR($$CN,arch)__-install-compute +check:rc==0 +check:output=~Provision node\(s\)\: $$CN +cmd:xdsh $$CN getenforce +check:rc==0 +check:output=~Enforcing +cmd:xdsh $$CN cat /proc/cmdline +check:rc==0 +check:output!~selinux=0 +cmd:xdsh $$CN "ls -dZ /root/.ssh /etc/ssh/sshd_config /opt/xcat /xcatpost" +check:rc==0 +check:output=~ssh_home_t +check:output!~default_t +check:output!~unlabeled_t +cmd:xdsh $$CN "restorecon -RnvF /xcatpost /opt/xcat /root /etc/ssh /var/log/xcat" +check:rc==0 +check:output!~Would relabel +cmd:xdsh $$CN "ausearch -m AVC,USER_AVC -ts boot" +check:output=~ +check:output!~type=AVC +cmd:sh -c 'ausearch -m AVC,USER_AVC -ts $(cat /tmp/selinux_cn_diskful.start)' +check:rc==1 +check:output=~ +cmd:chdef -t node -o $$CN selinux= +check:rc==0 +cmd:rm -f /tmp/selinux_cn_diskful.start +end + +start:selinux_cn_diskless_enforcing +description:on an enforcing management node, a stateless (cpio) compute node with noderes.selinux=enforcing labels its RAM root at boot and runs enforcing with no denial +label:selinux,provision +os:Linux +cmd:getenforce +check:rc==0 +check:output=~^Enforcing$ +cmd:sh -c 'date "+%x %T" > /tmp/selinux_cn_diskless.start' +check:rc==0 +cmd:chdef -t node -o $$CN selinux=enforcing +check:rc==0 +cmd:genimage __GETNODEATTR($$CN,os)__-__GETNODEATTR($$CN,arch)__-netboot-compute +check:rc==0 +cmd:packimage -m cpio __GETNODEATTR($$CN,os)__-__GETNODEATTR($$CN,arch)__-netboot-compute +check:rc==0 +cmd:/opt/xcat/share/xcat/tools/autotest/testcase/commoncmd/retry_install.sh $$CN __GETNODEATTR($$CN,os)__-__GETNODEATTR($$CN,arch)__-netboot-compute +check:rc==0 +check:output=~Provision node\(s\)\: $$CN +cmd:xdsh $$CN mount +check:rc==0 +check:output=~on / type tmpfs +cmd:xdsh $$CN getenforce +check:rc==0 +check:output=~Enforcing +cmd:xdsh $$CN cat /proc/cmdline +check:rc==0 +check:output!~selinux=0 +cmd:xdsh $$CN "ls -dZ / /usr/bin/bash /root/.ssh /etc/ssh/sshd_config /opt/xcat /xcatpost" +check:rc==0 +check:output=~bin_t +check:output=~ssh_home_t +check:output!~tmpfs_t +check:output!~default_t +check:output!~unlabeled_t +cmd:xdsh $$CN "restorecon -RnvF /xcatpost /opt/xcat /root /etc/ssh /usr/sbin" +check:rc==0 +check:output!~Would relabel +cmd:xdsh $$CN "ausearch -m AVC,USER_AVC -ts boot" +check:output=~ +check:output!~type=AVC +cmd:sh -c 'ausearch -m AVC,USER_AVC -ts $(cat /tmp/selinux_cn_diskless.start)' +check:rc==1 +check:output=~ +cmd:chdef -t node -o $$CN selinux= +check:rc==0 +cmd:rm -f /tmp/selinux_cn_diskless.start +end + +start:selinux_cn_diskless_squashfs_enforcing +description:on an enforcing management node with squashfs-tools 4.6 or later, a stateless squashfs compute node with noderes.selinux=enforcing boots from labels stored in the image and runs enforcing with no denial +label:selinux,provision +os:Linux +cmd:getenforce +check:rc==0 +check:output=~^Enforcing$ +cmd:sh -c 'date "+%x %T" > /tmp/selinux_cn_squashfs.start' +check:rc==0 +cmd:chdef -t node -o $$CN selinux=enforcing +check:rc==0 +cmd:sh -c 'rpm -q squashfs-tools || dnf install -y squashfs-tools' +check:rc==0 +cmd:genimage __GETNODEATTR($$CN,os)__-__GETNODEATTR($$CN,arch)__-netboot-compute +check:rc==0 +cmd:packimage -m squashfs __GETNODEATTR($$CN,os)__-__GETNODEATTR($$CN,arch)__-netboot-compute +check:rc==0 +check:output=~archive method:squashfs +check:output!~Nodes that boot +cmd:sh -c 'ls "$(lsdef -t osimage -o __GETNODEATTR($$CN,os)__-__GETNODEATTR($$CN,arch)__-netboot-compute -i rootimgdir -c | cut -d= -f2)/rootimg.sfs.selinux"' +check:rc==0 +cmd:/opt/xcat/share/xcat/tools/autotest/testcase/commoncmd/retry_install.sh $$CN __GETNODEATTR($$CN,os)__-__GETNODEATTR($$CN,arch)__-netboot-compute +check:rc==0 +check:output=~Provision node\(s\)\: $$CN +cmd:xdsh $$CN mount +check:rc==0 +check:output=~on / type overlay +cmd:xdsh $$CN getenforce +check:rc==0 +check:output=~Enforcing +cmd:xdsh $$CN "ls -dZ / /usr/bin/bash /etc/ssh/sshd_config /opt/xcat /xcatpost" +check:rc==0 +check:output=~bin_t +check:output!~tmp_t +check:output!~default_t +check:output!~unlabeled_t +cmd:xdsh $$CN "restorecon -RnvF /usr/bin /usr/sbin /etc/ssh /xcatpost /opt/xcat" +check:rc==0 +check:output!~Would relabel +cmd:xdsh $$CN "ausearch -m AVC,USER_AVC -ts boot" +check:output=~ +check:output!~type=AVC +cmd:sh -c 'ausearch -m AVC,USER_AVC -ts $(cat /tmp/selinux_cn_squashfs.start)' +check:rc==1 +check:output=~ +cmd:chdef -t node -o $$CN selinux= +check:rc==0 +cmd:rm -f /tmp/selinux_cn_squashfs.start +end + +start:selinux_cn_diskless_site_disabled +description:with site.selinux=disabled and no noderes.selinux value, a stateless compute node boots with SELinux disabled, as before +label:selinux,provision +os:Linux +cmd:sh -c 'lsdef -t site -i selinux -c | cut -d= -f2 > /tmp/selinux_site.orig' +check:rc==0 +cmd:chtab key=selinux site.value=disabled +check:rc==0 +cmd:chdef -t node -o $$CN selinux= +check:rc==0 +cmd:lsdef -t node -o $$CN -i selinux +check:rc==0 +check:output!~selinux=enforcing +check:output!~selinux=permissive +cmd:genimage __GETNODEATTR($$CN,os)__-__GETNODEATTR($$CN,arch)__-netboot-compute +check:rc==0 +cmd:packimage -m cpio __GETNODEATTR($$CN,os)__-__GETNODEATTR($$CN,arch)__-netboot-compute +check:rc==0 +cmd:/opt/xcat/share/xcat/tools/autotest/testcase/commoncmd/retry_install.sh $$CN __GETNODEATTR($$CN,os)__-__GETNODEATTR($$CN,arch)__-netboot-compute +check:rc==0 +check:output=~Provision node\(s\)\: $$CN +cmd:xdsh $$CN cat /proc/cmdline +check:rc==0 +check:output=~selinux=0 +cmd:xdsh $$CN getenforce +check:rc==0 +check:output=~Disabled +cmd:sh -c 'chtab key=selinux site.value="$(cat /tmp/selinux_site.orig)"' +check:rc==0 +cmd:rm -f /tmp/selinux_site.orig +end diff --git a/xCAT-test/bats/selinux_relabel_hook.bats b/xCAT-test/bats/selinux_relabel_hook.bats new file mode 100644 index 0000000000..3b794b9bac --- /dev/null +++ b/xCAT-test/bats/selinux_relabel_hook.bats @@ -0,0 +1,126 @@ +#!/usr/bin/env bats +# +# Source the pre-pivot hook that labels a stateless root, as dracut does. getarg, warn, +# mount, umount and chroot are dracut and initramfs commands, so shadow them with functions. + +load 'helpers/shell_source' + +HOOKS=( + 'xCAT-server/share/xcat/netboot/rh/dracut_047/xcat-selinux-relabel.sh' + 'xCAT-server/share/xcat/netboot/rh/dracut_105/stateless/xcat-selinux-relabel.sh' +) + +setup() +{ + NEWROOT="${BATS_TEST_TMPDIR}/sysroot" + CALLS="${BATS_TEST_TMPDIR}/calls" + CMDLINE='' + xcat_sfs_image="${BATS_TEST_TMPDIR}/rootimg.sfs" + mkdir -p "$NEWROOT/etc/selinux/targeted/contexts/files" "$NEWROOT/usr/sbin" "$NEWROOT/proc" "$NEWROOT/sys" + : >"$NEWROOT/etc/selinux/targeted/contexts/files/file_contexts" + : >"$NEWROOT/usr/sbin/setfiles" + chmod 0755 "$NEWROOT/usr/sbin/setfiles" + write_config enforcing targeted + : >"$CALLS" +} + +write_config() +{ + printf 'SELINUX=%s\nSELINUXTYPE=%s\n' "$1" "$2" >"$NEWROOT/etc/selinux/config" +} + +getarg() +{ + local arg + for arg in $CMDLINE; do + [ "$arg" = "$1" ] && return 0 + done + return 1 +} + +warn() { echo "warn: $*" >>"$CALLS"; } +mount() { :; } +umount() { :; } +chroot() { echo "chroot $*" >>"$CALLS"; } + +source_hook() +{ + # dracut sources every hook into its own shell. + . "$(repo_path "$1")" +} + +@test "selinux=0 on the kernel command line runs no setfiles" { + for hook in "${HOOKS[@]}"; do + : >"$CALLS" + CMDLINE='root=1 selinux=0 quiet' + source_hook "$hook" + run cat "$CALLS" + [ -z "$output" ] + done +} + +@test "an enforcing node runs setfiles once with the file_contexts of the image" { + for hook in "${HOOKS[@]}"; do + : >"$CALLS" + CMDLINE='root=1 quiet' + source_hook "$hook" + run grep -c '^chroot ' "$CALLS" + [ "$output" = 1 ] + run cat "$CALLS" + [ "$output" = "chroot $NEWROOT /usr/sbin/setfiles -F -e /proc -e /sys -e /dev /etc/selinux/targeted/contexts/files/file_contexts /" ] + done +} + +@test "a permissive node also runs setfiles" { + for hook in "${HOOKS[@]}"; do + : >"$CALLS" + CMDLINE='root=1 enforcing=0' + source_hook "$hook" + run grep -c '^chroot .*/usr/sbin/setfiles' "$CALLS" + [ "$output" = 1 ] + done +} + +@test "the file_contexts follows SELINUXTYPE of the image" { + mkdir -p "$NEWROOT/etc/selinux/mls/contexts/files" + : >"$NEWROOT/etc/selinux/mls/contexts/files/file_contexts" + write_config enforcing mls + for hook in "${HOOKS[@]}"; do + : >"$CALLS" + source_hook "$hook" + run cat "$CALLS" + [[ "$output" == *" /etc/selinux/mls/contexts/files/file_contexts /" ]] + done +} + +@test "a squashfs root keeps the labels of the image" { + : >"$xcat_sfs_image" + for hook in "${HOOKS[@]}"; do + : >"$CALLS" + source_hook "$hook" + run cat "$CALLS" + [ -z "$output" ] + done +} + +@test "an image configured with SELINUX=disabled runs no setfiles" { + write_config disabled targeted + for hook in "${HOOKS[@]}"; do + : >"$CALLS" + source_hook "$hook" + run grep -c '^chroot ' "$CALLS" + [ "$output" = 0 ] + done +} + +@test "an image without file_contexts warns and runs no setfiles" { + rm -f "$NEWROOT/etc/selinux/targeted/contexts/files/file_contexts" + for hook in "${HOOKS[@]}"; do + : >"$CALLS" + source_hook "$hook" + run grep -c '^chroot ' "$CALLS" + [ "$output" = 0 ] + run grep -c '^warn: .*file_contexts' "$CALLS" + [ "$output" = 1 ] + done +} diff --git a/xCAT-test/native/netboot_profile_riscv64.t b/xCAT-test/native/netboot_profile_riscv64.t index be5109d4c9..20f4e9152f 100644 --- a/xCAT-test/native/netboot_profile_riscv64.t +++ b/xCAT-test/native/netboot_profile_riscv64.t @@ -88,10 +88,8 @@ for my $family (@families) { if ($mode eq 'absent') { ok(!-e "$image/etc/selinux/config", "pass $pass leaves absent SELinux configuration absent"); } else { - local $TODO = 'The existing postinstall duplicates SELINUX= for permissive mode' - if $mode eq 'permissive'; - is(read_text("$image/etc/selinux/config"), "SELINUX=disabled\nSELINUXTYPE=targeted\n", - "pass $pass disables SELinux without changing the policy type"); + is(read_text("$image/etc/selinux/config"), "SELINUX=$mode\nSELINUXTYPE=targeted\n", + "pass $pass keeps the SELinux mode the image was built with"); } my @mounts = map { [split /\s+/] } grep { /\S/ } split /\n/, read_text("$image/etc/fstab"); is_deeply(\@mounts, [ diff --git a/xCAT-test/unit/probe_utils_selinux_policy.t b/xCAT-test/unit/probe_utils_selinux_policy.t new file mode 100644 index 0000000000..e134e951f1 --- /dev/null +++ b/xCAT-test/unit/probe_utils_selinux_policy.t @@ -0,0 +1,58 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +# Keep modules out of an installed /opt/xcat, so the checkout is what loads. +BEGIN { $ENV{XCATROOT} = '/nonexistent/xcatroot' } + +use FindBin; +use lib "$FindBin::Bin/../../perl-xCAT"; +use lib "$FindBin::Bin/../../xCAT-probe/lib/perl"; + +use Test::More; + +require probe_utils; + +like($INC{'probe_utils.pm'}, qr/\Q$FindBin::Bin\E/, 'probe_utils comes from this checkout'); + +# semodule -l prints one module per line; EL8 adds a version column. +my $loaded_el9 = "abrt\nxcat\nzosremote\n"; +my $loaded_el8 = "abrt\t1.4.1\nxcat\t1.0.0\n"; +my $not_loaded = "abrt\nxcatlike\nzosremote\n"; +my @good_labels = ( + [ '/install', 'public_content_t', 'system_u:object_r:public_content_t:s0' ], + [ '/tftpboot', 'tftpdir_t', 'system_u:object_r:tftpdir_t:s0' ], +); + +my ($flag, $msg) = probe_utils::selinux_policy_verdict('disabled', '', []); +is($flag, 'o', 'SELinux disabled is ok, whatever the module'); +like($msg, qr/disabled/, 'the message names the disabled mode'); + +foreach my $modules ($loaded_el9, $loaded_el8) { + ($flag, $msg) = probe_utils::selinux_policy_verdict('enforcing', $modules, \@good_labels); + is($flag, 'o', 'enforcing with the xcat module and the expected labels is ok'); +} + +($flag, $msg) = probe_utils::selinux_policy_verdict('enforcing', $not_loaded, \@good_labels); +is($flag, 'f', 'enforcing without the xcat module fails'); +like($msg, qr/xcat SELinux module is not loaded/, 'the failure names the missing module'); +unlike($msg, qr{/install}, 'the failure does not blame a label that is correct'); + +my @bad_labels = ( + [ '/install', 'public_content_t', 'system_u:object_r:default_t:s0' ], + [ '/tftpboot', 'tftpdir_t', undef ], +); +($flag, $msg) = probe_utils::selinux_policy_verdict('enforcing', $loaded_el9, \@bad_labels); +is($flag, 'f', 'enforcing with a wrong label fails'); +like($msg, qr{/install has type default_t, expected public_content_t}, 'the failure names the path, its type and the expected type'); +like($msg, qr{/tftpboot has no SELinux label, expected tftpdir_t}, 'a path with no label is a failure'); +unlike($msg, qr/module/, 'the failure does not blame a module that is loaded'); + +($flag, $msg) = probe_utils::selinux_policy_verdict('permissive', $not_loaded, \@bad_labels); +is($flag, 'w', 'permissive turns the same findings into a warning'); +like($msg, qr/module.*default_t/s, 'the warning lists every finding'); + +($flag, $msg) = probe_utils::selinux_policy_verdict('permissive', $loaded_el9, \@good_labels); +is($flag, 'o', 'permissive with the module and the labels is ok'); + +done_testing(); diff --git a/xCAT-test/unit/selinux_image_config.t b/xCAT-test/unit/selinux_image_config.t new file mode 100644 index 0000000000..ecd83b5c00 --- /dev/null +++ b/xCAT-test/unit/selinux_image_config.t @@ -0,0 +1,51 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +# Keep modules out of an installed /opt/xcat, so the checkout is what loads. +BEGIN { $ENV{XCATROOT} = '/nonexistent/xcatroot' } + +use FindBin; +use lib "$FindBin::Bin/../../perl-xCAT"; + +use File::Path qw(make_path); +use File::Slurper qw(read_text write_text); +use File::Temp qw(tempdir); +use Test::More; + +use xCAT::SELinux; + +like($INC{'xCAT/SELinux.pm'}, qr/\Q$FindBin::Bin\E/, + 'xCAT::SELinux comes from this checkout, not from /opt/xcat'); + +my $root = tempdir(CLEANUP => 1); +make_path("$root/etc/selinux"); +my $config = "$root/etc/selinux/config"; + +# The config an old genimage postinstall left behind. +write_text($config, "# comment SELINUX=keep\nSELINUX=disabled\nSELINUXTYPE=targeted\n"); +ok(xCAT::SELinux->write_image_config(root => $root), 'write_image_config changes a disabled image'); +is(read_text($config), "# comment SELINUX=keep\nSELINUX=enforcing\nSELINUXTYPE=targeted\n", + 'the image boots enforcing unless the kernel command line says otherwise'); +is(xCAT::SELinux->config_mode(root => $root), 'enforcing', 'config_mode of the image reads enforcing'); + +write_text($config, "SELINUX=permissive\nSELINUXTYPE=mls\n"); +xCAT::SELinux->write_image_config(root => $root); +is(read_text($config), "SELINUX=enforcing\nSELINUXTYPE=mls\n", 'a permissive image becomes enforcing; the policy type stays'); + +write_text($config, "SELINUXTYPE=targeted\n"); +xCAT::SELinux->write_image_config(root => $root); +is(read_text($config), "SELINUXTYPE=targeted\nSELINUX=enforcing\n", 'a config with no SELINUX line gets one'); + +my $bare = tempdir(CLEANUP => 1); +ok(!xCAT::SELinux->write_image_config(root => $bare), 'an image without an SELinux policy is left alone'); +ok(!-e "$bare/etc/selinux/config", 'no config is created in an image without an SELinux policy'); + +is(xCAT::SELinux->image_file_contexts($bare), undef, 'an image without a policy has no file_contexts'); +make_path("$root/etc/selinux/mls/contexts/files"); +write_text($config, "SELINUX=enforcing\nSELINUXTYPE=mls\n"); +write_text("$root/etc/selinux/mls/contexts/files/file_contexts", "/.* system_u:object_r:default_t:s0\n"); +is(xCAT::SELinux->image_file_contexts($root), "$root/etc/selinux/mls/contexts/files/file_contexts", + 'image_file_contexts follows SELINUXTYPE of the image'); + +done_testing(); diff --git a/xCAT-test/unit/selinux_kickstart_template.t b/xCAT-test/unit/selinux_kickstart_template.t new file mode 100644 index 0000000000..3ca8a8d8dd --- /dev/null +++ b/xCAT-test/unit/selinux_kickstart_template.t @@ -0,0 +1,118 @@ +#!/usr/bin/env perl +use strict; +use warnings; +no warnings 'once'; +use FindBin; +use lib "$FindBin::Bin/../lib"; +use lib "$FindBin::Bin/../../perl-xCAT"; +use lib "$FindBin::Bin/../../xCAT-server/lib/perl"; +use File::Path qw(make_path); +use File::Slurper qw(read_text); +use File::Temp qw(tempdir); +use Test::More; +use XCAT::Test::File qw(repo_path); + +my $dir = tempdir(CLEANUP => 1); +make_path("$dir/db", "$dir/install"); +symlink(repo_path('xCAT/postscripts'), "$dir/install/postscripts") or die "postscripts link: $!"; +$ENV{XCATROOT} = repo_path('xCAT-server'); +$ENV{XCATCFG} = "SQLite:$dir/db"; +require xCAT::Table; +require xCAT::Template; +require xCAT::SELinux; + +like($INC{'xCAT/SELinux.pm'}, qr/\Q$FindBin::Bin\E/, + 'xCAT::SELinux comes from this checkout, not from /opt/xcat'); + +my %site_values = ( + installdir => "$dir/install", tftpdir => "$dir/tftpboot", master => '192.0.2.1', + timezone => 'UTC', xcatiport => 3002, xcatdport => 3001, httpport => 80, + xcatdebugmode => 0, nodestatus => 1, +); +my $site = xCAT::Table->new('site', -create => 1); +for my $key (keys %site_values) { + $site->setAttribs({ key => $key }, { value => $site_values{$key} }); +} +$site->close(); +%::XCATSITEVALS = (%site_values, secureroot => 1, managedaddressmode => 'dhcp'); + +# The maintained kickstart templates, each with an OS that uses it. +my @templates = ( + [ 'rh/compute.rhels10.tmpl', 'rhels10.0', 'rh' ], + [ 'rh/compute.rhels9.tmpl', 'alma9.6', 'rh' ], + [ 'rh/compute.rhels8.tmpl', 'rocky8.10', 'rh' ], + [ 'rh/compute.rhels10.riscv64.tmpl', 'rhels10.0', 'rh' ], + [ 'openeuler/compute.openeuler.tmpl', 'openeuler24.03sp3', 'openeuler' ], +); + +my $relabel = qr{^/usr/sbin/restorecon -RF /xcatpost /opt/xcat /etc /root /var/log\b}m; + +sub set_node { + my ($os, $mode) = @_; + for my $entry ( + [ 'nodelist', { groups => 'all' } ], + [ 'nodetype', { os => $os, arch => 'x86_64', provmethod => 'install' } ], + [ 'noderes', { xcatmaster => '192.0.2.1', nfsserver => '192.0.2.2', installnic => 'mac', selinux => $mode } ], + [ 'mac', { mac => '52:54:00:12:34:56' } ], + ) { + my ($name, $values) = @$entry; + my $tab = xCAT::Table->new($name, -create => 1); + $tab->setAttribs({ node => 'cn1' }, $values); + $tab->close(); + } +} + +# The text of the first %post section: where the xCAT post scripts run. +sub main_post { + my ($ks) = @_; + my ($post) = $ks =~ /^(%post --interpreter=\/bin\/bash.*?^%end)$/ms; + return $post; +} + +foreach my $case (@templates) { + my ($template, $os, $platform) = @$case; + my $path = "$ENV{XCATROOT}/share/xcat/install/$template"; + (my $pkglist = $path) =~ s/\.tmpl$/.pkglist/; + $pkglist = '' unless -r $pkglist; + + foreach my $mode (qw(enforcing permissive disabled)) { + set_node($os, $mode); + my $out = "$dir/ks.$mode"; + my $error = xCAT::Template->subvars($path, $out, 'cn1', $pkglist, + '/install/media', $platform, undef, { xcatmaster => '192.0.2.1' }); + ok(!$error, "$template renders for $mode") or diag($error); + my $ks = read_text($out); + + my @lines = $ks =~ /^(selinux\s.*)$/mg; + is_deeply(\@lines, ["selinux --$mode"], "$template sets selinux --$mode for a $mode node"); + is(xCAT::SELinux->kickstart_mode($ks), $mode, "the $template kickstart asks anaconda for $mode"); + unlike($ks, qr/#SELINUX[A-Z]*#/, "$template leaves no SELinux directive unresolved"); + + my $post = main_post($ks); + ok(defined $post, "$template has a main %post section"); + if ($mode eq 'disabled') { + unlike($ks, $relabel, "$template does not relabel a $mode node"); + } else { + like($post, $relabel, "$template relabels the xCAT files in %post for a $mode node"); + like($post, qr/$relabel.*\n\}\s*&>>\/var\/log\/xcat\/xcat\.log\s*\n%end\z/s, + "$template relabels after the xCAT post scripts ran"); + } + } +} + +# A custom template that keeps the old line cannot express the mode of the node. +is(xCAT::SELinux->kickstart_mode("text\nselinux --disable\nreboot\n"), 'disabled', + 'selinux --disable asks for disabled'); +is(xCAT::SELinux->kickstart_mode("text\nselinux --enforcing\n"), 'enforcing', + 'selinux --enforcing asks for enforcing'); +is(xCAT::SELinux->kickstart_mode("text\nreboot\n"), 'enforcing', + 'a kickstart with no selinux line gets the anaconda default, enforcing'); + +like(xCAT::SELinux->kickstart_mismatch('enforcing', "selinux --disabled\n"), qr/enforcing.*disabled/, + 'an enforcing node with a template that says --disabled gets a warning'); +like(xCAT::SELinux->kickstart_mismatch('disabled', "reboot\n"), qr/disabled.*enforcing/, + 'a disabled node with a template that has no selinux line gets a warning'); +is(xCAT::SELinux->kickstart_mismatch('permissive', "selinux --permissive\n"), undef, + 'a template that matches the mode of the node gets no warning'); + +done_testing(); diff --git a/xCAT-test/unit/selinux_netboot_kcmdline.t b/xCAT-test/unit/selinux_netboot_kcmdline.t new file mode 100644 index 0000000000..5fb4415e67 --- /dev/null +++ b/xCAT-test/unit/selinux_netboot_kcmdline.t @@ -0,0 +1,48 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +# Keep modules out of an installed /opt/xcat, so the checkout is what loads. +BEGIN { $ENV{XCATROOT} = '/nonexistent/xcatroot' } + +use FindBin; +use lib "$FindBin::Bin/../../perl-xCAT"; + +use Test::More; + +use xCAT::SELinux; + +like($INC{'xCAT/SELinux.pm'}, qr/\Q$FindBin::Bin\E/, + 'xCAT::SELinux comes from this checkout, not from /opt/xcat'); + +# The stateless images whose initramfs carries the xCAT relabel hook. +my @supported = qw(rhels8.10 rhels9.6 rhels10.0 alma9.4 alma10.0 rocky8.10 rocky10.0 + ol9.5 centos-stream9 openeuler22.03sp4 openeuler24.03sp3); + +# Images built from the older dracut module, which has no relabel hook. +my @unsupported = qw(rhels7.9 rhels6.10 centos7 ol7.9 fedora13 sles15.6 ubuntu24.04); + +foreach my $osver (@supported) { + is(xCAT::SELinux->kcmdline_selinux('enforcing', $osver), '', + "$osver enforcing adds nothing to the kernel command line"); + is(xCAT::SELinux->kcmdline_selinux('permissive', $osver), 'enforcing=0', + "$osver permissive boots with enforcing=0"); + is(xCAT::SELinux->kcmdline_selinux('disabled', $osver), 'selinux=0', + "$osver disabled boots with selinux=0"); + ok(xCAT::SELinux->netboot_supported($osver), "$osver supports SELinux on a stateless node"); +} + +foreach my $osver (@unsupported) { + foreach my $mode (qw(enforcing permissive disabled)) { + is(xCAT::SELinux->kcmdline_selinux($mode, $osver), 'selinux=0', + "$osver $mode boots with selinux=0, because its initramfs cannot label the root"); + } + ok(!xCAT::SELinux->netboot_supported($osver), "$osver does not support SELinux on a stateless node"); +} + +is(xCAT::SELinux->kcmdline_selinux(undef, 'rhels9.6'), 'selinux=0', + 'an unresolved mode boots with selinux=0'); +is(xCAT::SELinux->kcmdline_selinux('enforcing', undef), 'selinux=0', + 'an unknown OS boots with selinux=0'); + +done_testing(); diff --git a/xCAT-test/unit/selinux_sn_mount_options.t b/xCAT-test/unit/selinux_sn_mount_options.t new file mode 100644 index 0000000000..e2435a667a --- /dev/null +++ b/xCAT-test/unit/selinux_sn_mount_options.t @@ -0,0 +1,38 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +# Keep modules out of an installed /opt/xcat, so the checkout is what loads. +BEGIN { $ENV{XCATROOT} = '/nonexistent/xcatroot' } + +use FindBin; +use lib "$FindBin::Bin/../../perl-xCAT"; + +use Test::More; + +use xCAT::SELinux; + +like($INC{'xCAT/SELinux.pm'}, qr/\Q$FindBin::Bin\E/, + 'xCAT::SELinux comes from this checkout, not from /opt/xcat'); + +foreach my $mode (qw(enforcing permissive enabled)) { + is(xCAT::SELinux->nfs_mount_options('rw,nolock', 'public_content_t', $mode), + 'rw,nolock,context=system_u:object_r:public_content_t:s0', + "a $mode service node mounts /install as public_content_t"); + is(xCAT::SELinux->nfs_mount_options('vers=4,rw,nolock', 'tftpdir_t', $mode), + 'vers=4,rw,nolock,context=system_u:object_r:tftpdir_t:s0', + "a $mode service node mounts /tftpboot as tftpdir_t"); +} + +# The kernel refuses context= when SELinux is off, so the mount must not carry it. +foreach my $mode ('disabled', undef) { + my $name = defined $mode ? $mode : 'undef'; + is(xCAT::SELinux->nfs_mount_options('rw,nolock', 'public_content_t', $mode), 'rw,nolock', + "a service node in mode $name mounts /install as before"); +} + +is(xCAT::SELinux->nfs_mount_options('timeo=14,intr', 'public_content_t', 'enforcing'), + 'timeo=14,intr,context=system_u:object_r:public_content_t:s0', + 'the fstab options get the same context'); + +done_testing(); diff --git a/xCAT-test/unit/selinux_squashfs_labels.t b/xCAT-test/unit/selinux_squashfs_labels.t new file mode 100644 index 0000000000..cbb4a573ed --- /dev/null +++ b/xCAT-test/unit/selinux_squashfs_labels.t @@ -0,0 +1,116 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +# Keep modules out of an installed /opt/xcat, so the checkout is what loads. +BEGIN { $ENV{XCATROOT} = '/nonexistent/xcatroot' } + +use FindBin; +use lib "$FindBin::Bin/../../perl-xCAT"; + +use File::Path qw(make_path); +use File::Slurper qw(read_lines write_text); +use File::Temp qw(tempdir); +use Test::More; + +use xCAT::SELinux; + +like($INC{'xCAT/SELinux.pm'}, qr/\Q$FindBin::Bin\E/, + 'xCAT::SELinux comes from this checkout, not from /opt/xcat'); + +ok(xCAT::SELinux->mksquashfs_supports_pseudo_xattr('mksquashfs version 4.6.1 (2023/03/25)'), + 'squashfs-tools 4.6.1 (EL10, openEuler 24.03) takes pseudo xattr definitions'); +ok(xCAT::SELinux->mksquashfs_supports_pseudo_xattr("mksquashfs version 4.10 (2030/01/01)\ncopyright"), + 'a later minor version takes them too'); +ok(!xCAT::SELinux->mksquashfs_supports_pseudo_xattr('mksquashfs version 4.5 (2021/07/22)'), + 'squashfs-tools 4.5 (openEuler 22.03) does not'); +ok(!xCAT::SELinux->mksquashfs_supports_pseudo_xattr('mksquashfs version 4.4-git.1 (2020/02/17)'), + 'squashfs-tools 4.4 (EL9) does not'); +ok(!xCAT::SELinux->mksquashfs_supports_pseudo_xattr('mksquashfs version 4.3-git (2014/06/09)'), + 'squashfs-tools 4.3 (EL8) does not'); +ok(!xCAT::SELinux->mksquashfs_supports_pseudo_xattr(undef), 'no version output means no support'); + +# A temporary copy of a rootimg, as packimage makes it before mksquashfs. +my $tmp = tempdir(CLEANUP => 1); +my $root = "$tmp/copy"; +make_path("$root/usr/bin", "$root/etc/selinux/targeted/contexts/files", "$root/a dir"); +my $fc = "$root/etc/selinux/targeted/contexts/files/file_contexts"; +write_text("$root/etc/selinux/config", "SELINUX=enforcing\nSELINUXTYPE=targeted\n"); +write_text($fc, "/.* system_u:object_r:default_t:s0\n"); +write_text("$root/usr/bin/ls", "ELF\n"); +write_text("$root/a dir/say \"hi\"\\now", "x\n"); +write_text("$root/etc/bad\nname", "x\n"); +symlink('ls', "$root/usr/bin/dir") or die "symlink: $!"; + +# label_paths runs matchpathcon against the image policy. Answer by file type instead. +my @lookups; +my %context = (dir => 'root_t', file => 'bin_t', lnk_file => 'lnk_t'); +sub run_args { + my (%args) = @_; + @lookups = (); + no warnings 'redefine'; + local *xCAT::SELinux::label_paths = sub { + my ($class, $file_contexts, $type, $paths) = @_; + push @lookups, [ $file_contexts, $type, [ sort @{$paths} ] ]; + my %answer; + foreach my $path (@{$paths}) { + next if $path eq '/etc/selinux/config'; + $answer{$path} = "system_u:object_r:$context{$type}:s0"; + } + return \%answer; + }; + return xCAT::SELinux->squashfs_label_args(%args); +} + +my $pseudo = "$tmp/pseudo"; +my ($args, $warning) = run_args(root => $root, pseudo => $pseudo, + version => 'mksquashfs version 4.6.1 (2023/03/25)'); +is($warning, undef, 'squashfs-tools 4.6.1 gives no warning'); +is_deeply($args, [ '-xattrs-exclude', '^security\.selinux$', '-pf', $pseudo ], + 'mksquashfs drops the labels of the copy and takes the labels of the image from the pseudo file'); + +is_deeply([ sort map { $_->[1] } @lookups ], [qw(dir file lnk_file)], 'one lookup for each file type'); +is_deeply([ map { $_->[0] } @lookups ], [ ($fc) x 3 ], 'every lookup uses the file_contexts of the image'); +my %paths = map { $_->[1] => $_->[2] } @lookups; +is_deeply($paths{dir}, [ '/', '/a dir', '/etc', '/etc/selinux', '/etc/selinux/targeted', + '/etc/selinux/targeted/contexts', '/etc/selinux/targeted/contexts/files', '/usr', '/usr/bin' ], + 'directories are looked up at their path on the node'); +is_deeply($paths{file}, [ "/a dir/say \"hi\"\\now", '/etc/selinux/config', + '/etc/selinux/targeted/contexts/files/file_contexts', '/usr/bin/ls' ], + 'files are looked up, and a name with a newline is skipped'); +is_deeply($paths{lnk_file}, ['/usr/bin/dir'], 'symbolic links are looked up as lnk_file'); + +my @lines = sort(read_lines($pseudo)); +is_deeply(\@lines, [ sort( + '"/" x security.selinux=system_u:object_r:root_t:s0', + '"a dir" x security.selinux=system_u:object_r:root_t:s0', + '"a dir/say \"hi\"\\\\now" x security.selinux=system_u:object_r:bin_t:s0', + '"etc" x security.selinux=system_u:object_r:root_t:s0', + '"etc/selinux" x security.selinux=system_u:object_r:root_t:s0', + '"etc/selinux/targeted" x security.selinux=system_u:object_r:root_t:s0', + '"etc/selinux/targeted/contexts" x security.selinux=system_u:object_r:root_t:s0', + '"etc/selinux/targeted/contexts/files" x security.selinux=system_u:object_r:root_t:s0', + '"etc/selinux/targeted/contexts/files/file_contexts" x security.selinux=system_u:object_r:bin_t:s0', + '"usr" x security.selinux=system_u:object_r:root_t:s0', + '"usr/bin" x security.selinux=system_u:object_r:root_t:s0', + '"usr/bin/dir" x security.selinux=system_u:object_r:lnk_t:s0', + '"usr/bin/ls" x security.selinux=system_u:object_r:bin_t:s0', + ) ], + 'the pseudo file quotes each name and skips a path the policy gives no context'); + +unlink($pseudo); +($args, $warning) = run_args(root => $root, pseudo => $pseudo, + version => 'mksquashfs version 4.4-git.1 (2020/02/17)'); +is($args, undef, 'squashfs-tools 4.4 gives no label arguments'); +like($warning, qr/4\.6/, 'the warning names the squashfs-tools version that labels the image'); +is(scalar(@lookups), 0, 'squashfs-tools 4.4 looks up no label'); +ok(!-e $pseudo, 'squashfs-tools 4.4 writes no pseudo file'); + +my $bare = "$tmp/bare"; +make_path("$bare/usr"); +($args, $warning) = run_args(root => $bare, pseudo => $pseudo, + version => 'mksquashfs version 4.6.1 (2023/03/25)'); +is($args, undef, 'an image without an SELinux policy gives no label arguments'); +like($warning, qr/file_contexts/, 'the warning says the image has no file_contexts'); + +done_testing();