mirror of
https://github.com/xcat2/xcat-core.git
synced 2026-11-27 21:02:14 +00:00
refactor(xcat-core): move the xcatconfig SELinux decision into a module
xcatconfig held the SELinux decision inline and read the state with xCAT::Utils->isSELINUX, so no unit test can reach it. The script has no sub API. xCAT::SELinux now reports the runtime mode and the mode that /etc/selinux/config selects, and xcatconfig_action returns what xcatconfig must do about it. xcatconfig applies that answer. The behavior does not change: a node with SELinux enabled still gets the permissive write and the /etc/selinux/config rewrite. runtime_mode reads the selinuxfs enforce file instead of running selinuxenabled. Both report SELinux as enabled when selinuxfs is mounted. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
# IBM(c) 2026 EPL license http://www.eclipse.org/legal/epl-v10.html
|
||||
package xCAT::SELinux;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
# selinuxfs is at /sys/fs/selinux. /selinux is the path a kernel before 2.6.37 uses.
|
||||
our @ENFORCE_FILES = ('/sys/fs/selinux/enforce', '/selinux/enforce');
|
||||
our $CONFIG_FILE = '/etc/selinux/config';
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
=head3 runtime_mode
|
||||
|
||||
Descriptions:
|
||||
Reports the SELinux mode of the running system.
|
||||
Arguments:
|
||||
root - a prefix for the paths this module reads. The default is the
|
||||
empty string, which reads the real system.
|
||||
Returns:
|
||||
'enforcing' - selinuxfs reports enforcing
|
||||
'permissive' - selinuxfs reports permissive
|
||||
'enabled' - selinuxfs is there and the mode is not readable
|
||||
'disabled' - selinuxfs is not there, so SELinux is off
|
||||
Example:
|
||||
my $mode = xCAT::SELinux->runtime_mode();
|
||||
|
||||
=cut
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
sub runtime_mode {
|
||||
my ($class, %args) = @_;
|
||||
my $root = defined $args{root} ? $args{root} : '';
|
||||
|
||||
foreach my $path (@ENFORCE_FILES) {
|
||||
my $value = _first_line("$root$path");
|
||||
next unless defined $value;
|
||||
$value =~ s/\s+//g;
|
||||
return 'enforcing' if $value eq '1';
|
||||
return 'permissive' if $value eq '0';
|
||||
return 'enabled';
|
||||
}
|
||||
|
||||
return 'disabled';
|
||||
}
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
=head3 config_mode
|
||||
|
||||
Descriptions:
|
||||
Reports the mode that /etc/selinux/config selects for the next boot.
|
||||
Arguments:
|
||||
root - a prefix for the paths this module reads.
|
||||
Returns:
|
||||
The lower case value of the last SELINUX= line, or undef when the file
|
||||
has no such line and when the file is not there.
|
||||
Example:
|
||||
my $mode = xCAT::SELinux->config_mode();
|
||||
|
||||
=cut
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
sub config_mode {
|
||||
my ($class, %args) = @_;
|
||||
my $root = defined $args{root} ? $args{root} : '';
|
||||
|
||||
my $mode;
|
||||
open(my $fh, '<', "$root$CONFIG_FILE") or return undef;
|
||||
while (my $line = <$fh>) {
|
||||
next if $line =~ /^\s*#/;
|
||||
$mode = lc($1) if $line =~ /^\s*SELINUX\s*=\s*(\S+)/;
|
||||
}
|
||||
close($fh);
|
||||
|
||||
return $mode;
|
||||
}
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
|
||||
=head3 xcatconfig_action
|
||||
|
||||
Descriptions:
|
||||
Decides what xcatconfig does about SELinux on this node. The caller
|
||||
makes the changes the answer asks for.
|
||||
Arguments:
|
||||
root - a prefix for the paths this module reads.
|
||||
Returns:
|
||||
A hash reference:
|
||||
mode - the mode runtime_mode reports
|
||||
config_mode - the mode config_mode reports
|
||||
warning - a message for the admin, or undef
|
||||
disable - true when xcatconfig must turn SELinux off
|
||||
enforce_file - the selinuxfs file that sets the running mode
|
||||
Example:
|
||||
my $answer = xCAT::SELinux->xcatconfig_action();
|
||||
|
||||
=cut
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
sub xcatconfig_action {
|
||||
my ($class, %args) = @_;
|
||||
my $root = defined $args{root} ? $args{root} : '';
|
||||
|
||||
my $mode = $class->runtime_mode(root => $root);
|
||||
my %answer = (
|
||||
mode => $mode,
|
||||
config_mode => $class->config_mode(root => $root),
|
||||
warning => undef,
|
||||
disable => 0,
|
||||
enforce_file => undef,
|
||||
);
|
||||
|
||||
return \%answer if $mode eq 'disabled';
|
||||
|
||||
$answer{warning} = 'SELINUX is not disabled, disabling it now...';
|
||||
$answer{disable} = 1;
|
||||
foreach my $path (@ENFORCE_FILES) {
|
||||
next unless -f "$root$path";
|
||||
$answer{enforce_file} = $path;
|
||||
last;
|
||||
}
|
||||
|
||||
return \%answer;
|
||||
}
|
||||
|
||||
sub _first_line {
|
||||
my ($path) = @_;
|
||||
|
||||
open(my $fh, '<', $path) or return undef;
|
||||
my $line = <$fh>;
|
||||
close($fh);
|
||||
|
||||
return $line;
|
||||
}
|
||||
|
||||
1;
|
||||
+10
-17
@@ -28,6 +28,7 @@ use xCAT::DHCP::Backend;
|
||||
use xCAT::DHCP::OmapiPolicy;
|
||||
use xCAT::TLSPolicy qw(tls_setting_warnings);
|
||||
use xCAT::NetworkUtils;
|
||||
use xCAT::SELinux;
|
||||
use Getopt::Long;
|
||||
use xCAT::MsgUtils;
|
||||
use xCAT::Table;
|
||||
@@ -509,27 +510,19 @@ if ($::INITIALINSTALL || $::FORCE)
|
||||
|
||||
if (-f "/etc/redhat-release" || -f "/etc/openEuler-release")
|
||||
{
|
||||
my $rc = xCAT::Utils->isSELINUX();
|
||||
|
||||
# rc 0 means SELinux is enabled
|
||||
# rc 1 means SELinux is not enabled
|
||||
if ($rc == 0)
|
||||
my $selinux = xCAT::SELinux->xcatconfig_action();
|
||||
if ($selinux->{warning})
|
||||
{
|
||||
xCAT::MsgUtils->message('W', $selinux->{warning});
|
||||
}
|
||||
if ($selinux->{disable})
|
||||
{
|
||||
xCAT::MsgUtils->message('W', "SELINUX is not disabled, disabling it now...");
|
||||
|
||||
# set selinux be in permissive mode for running system,
|
||||
# if the user forgot to disable selinux,
|
||||
# try to not hurt xcat too much
|
||||
my $enforcefile;
|
||||
if (-f "/selinux/enforce")
|
||||
if ($selinux->{enforce_file})
|
||||
{
|
||||
$enforcefile = "/selinux/enforce";
|
||||
} elsif (-f "/sys/fs/selinux/enforce") {
|
||||
$enforcefile = "/sys/fs/selinux/enforce";
|
||||
}
|
||||
if ($enforcefile)
|
||||
{
|
||||
my $cmd = "echo 0 > $enforcefile";
|
||||
my $cmd = "echo 0 > $selinux->{enforce_file}";
|
||||
my $outref = xCAT::Utils->runcmd("$cmd", 0);
|
||||
if ($::RUNCMD_RC != 0)
|
||||
{
|
||||
@@ -538,7 +531,7 @@ if ($::INITIALINSTALL || $::FORCE)
|
||||
}
|
||||
|
||||
# Disable selinux through reboots
|
||||
my $cmd = "sed -i 's/^SELINUX=.*\$/SELINUX=disabled/' /etc/selinux/config";
|
||||
my $cmd = "sed -i 's/^SELINUX=.*\$/SELINUX=disabled/' $xCAT::SELinux::CONFIG_FILE";
|
||||
my $outref = xCAT::Utils->runcmd("$cmd", 0);
|
||||
if ($::RUNCMD_RC != 0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user