| 1 | package Monitoring::Sneck::Checks::selinux; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 22 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 22 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 22 | ▶ | use parent 'Monitoring::Sneck::Checks::base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 22 | ▶ | use File::Slurp qw(read_file); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | Monitoring::Sneck::Checks::selinux - checks SELinux is in the mode it should be | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 VERSION | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Version 0.0.1 | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =cut | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 22 | our $VERSION = '0.0.1'; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # Where selinuxfs is mounted and the config is. Set by tests. | ||||||||||||||||||||||||||||
| 22 | 22 | our $SELINUXFS = '/sys/fs/selinux'; | |||||||||||||||||||||||||||
| 23 | 22 | our $CONFIG = '/etc/selinux/config'; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | selinux|perl:selinux | ||||||||||||||||||||||||||||
| 28 | selinux_permissive|perl:selinux -m permissive | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | Checks the mode SELinux is running in, from enforce under | ||||||||||||||||||||||||||||
| 33 | /sys/fs/selinux, it being disabled if that is not there, and the mode | ||||||||||||||||||||||||||||
| 34 | /etc/selinux/config sets for the next boot. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | - CRITICAL :: Running in a mode other than --mode. | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | - WARNING :: /etc/selinux/config setting a mode other than the running | ||||||||||||||||||||||||||||
| 39 | one, which takes effect at the next boot. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | - UNKNOWN :: Not being Linux. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | The policy type, such as targeted, is from SELINUXTYPE in the config. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | - -m, --mode :: The mode it should be running in, enforcing, | ||||||||||||||||||||||||||||
| 48 | permissive, or disabled. | ||||||||||||||||||||||||||||
| 49 | Default :: enforcing | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 7 | ▶ | sub option_spec { return ('mode|m=s') } | ||||||||||||||||||||||||||
|
Subroutine
option_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 7 | ▶ | sub defaults { return { mode => 'enforcing' } } | ||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 7 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 7 | my ( $self, $options ) = @_; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | return ('--mode must be enforcing, permissive, or disabled') | ||||||||||||||||||||||||||||
| 61 | 7 | ▶ | if $options->{mode} !~ /\A(?:enforcing|permissive|disabled)\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 6 | return (); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =head1 METHODS | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head2 check | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | Does the check. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =cut | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 6 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 6 | ▶ | die( 'not supported on ' . $self->os . "\n" ) if $self->os ne 'linux'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 5 | my $enforce = read_file( $SELINUXFS . '/enforce', err_mode => 'quiet' ); | |||||||||||||||||||||||||||
| 80 | 5 | my $running = 'disabled'; | |||||||||||||||||||||||||||
| 81 | 5 | ▶ | $running = ( $enforce =~ /\A\s*1/ ? 'enforcing' : 'permissive' ) if defined($enforce); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 5 | ▶ | my $config = read_file( $CONFIG, err_mode => 'quiet' ) // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
read_file($Monitoring::Sneck::Checks::selinux::CONFIG, "err_mode", "quiet") // ""
MC/DC0%0/1
read_file($Monitoring::Sneck::Checks::selinux::CONFIG, "err_mode", "quiet") // ""
read_file($Monitoring::Sneck::Checks::selinux::CONFIG, "err_mode", "quiet")
Truth table50%1/2
A
A= read_file($Monitoring::Sneck::Checks::selinux::CONFIG, "err_mode", "quiet")
| |||||||||||||||||||||||||||||
| 84 | 5 | my ($config_mode) = $config =~ /^\s*SELINUX\s*=\s*"?(\w+)/m; | |||||||||||||||||||||||||||
| 85 | 5 | my ($type) = $config =~ /^\s*SELINUXTYPE\s*=\s*"?(\w+)/m; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 5 | my @codes = ( $self->OK ); | |||||||||||||||||||||||||||
| 88 | 5 | my @parts = ($running); | |||||||||||||||||||||||||||
| 89 | 5 | ▶ | push( @parts, $type . ' policy' ) if defined($type) && $running ne 'disabled'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $type and $running ne "disabled"
MC/DC50%1/2
defined $type and $running ne "disabled"
defined $type
$running ne "disabled"
Truth table66%2/3
A and B
A= defined $typeB= $running ne "disabled"
| |||||||||||||||||||||||||||||
| 90 | 5 | ▶ | if ( $running ne $self->opt('mode') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 2 | push( @codes, $self->CRITICAL ); | |||||||||||||||||||||||||||
| 92 | 2 | push( @parts, 'wanted ' . $self->opt('mode') ); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 5 | ▶ | if ( defined($config_mode) && lc($config_mode) ne $running ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $config_mode and lc $config_mode ne $running
MC/DC50%1/2
defined $config_mode and lc $config_mode ne $running
defined $config_mode
lc $config_mode ne $running
Truth table66%2/3
A and B
A= defined $config_modeB= lc $config_mode ne $running
| |||||||||||||||||||||||||||||
| 95 | 2 | push( @codes, $self->WARNING ); | |||||||||||||||||||||||||||
| 96 | 2 | push( @parts, $CONFIG . ' says ' . lc($config_mode) . ' from the next boot' ); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 5 | return $self->finish( $self->worst(@codes), join( ', ', @parts ) ); | |||||||||||||||||||||||||||
| 100 | } ## end sub check | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 22 | 1; | |||||||||||||||||||||||||||