| 1 | package Monitoring::Sneck::Checks::apparmor; | ||||||||||||||||||||||||||||
| 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::apparmor - checks AppArmor is enabled with its profiles enforcing | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 VERSION | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Version 0.0.1 | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =cut | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 22 | our $VERSION = '0.0.1'; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # Where AppArmor says if it is enabled and lists its profiles. Set by tests. | ||||||||||||||||||||||||||||
| 22 | 22 | our $ENABLED = '/sys/module/apparmor/parameters/enabled'; | |||||||||||||||||||||||||||
| 23 | 22 | our $PROFILES = '/sys/kernel/security/apparmor/profiles'; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # How many profiles in complain mode to name in the status line. | ||||||||||||||||||||||||||||
| 26 | 22 | our $COMPLAIN_NAMED = 5; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | apparmor|perl:apparmor | ||||||||||||||||||||||||||||
| 31 | apparmor_web|perl:apparmor -p /usr/sbin/nginx -p php-fpm -m 20 | ||||||||||||||||||||||||||||
| 32 | apparmor_lab|perl:apparmor -x '^/usr/bin/firefox' | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | Checks AppArmor is enabled and the profiles it has loaded, from | ||||||||||||||||||||||||||||
| 37 | /sys/kernel/security/apparmor/profiles, which only root can read. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | - CRITICAL :: AppArmor not enabled. A profile given with --profile not | ||||||||||||||||||||||||||||
| 40 | loaded or not in enforce mode. Fewer profiles than --min-profiles. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | - WARNING :: Profiles in complain mode, other than those --exclude | ||||||||||||||||||||||||||||
| 43 | matches. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | - UNKNOWN :: Not being Linux, or the profiles not being readable. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | Profiles flagged unconfined, as some distros ship, are counted but not | ||||||||||||||||||||||||||||
| 48 | warned about. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | - -p, --profile :: A profile that must be loaded and enforcing. May be | ||||||||||||||||||||||||||||
| 53 | given more than once. | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | - -m, --min-profiles :: How many profiles there must be at least. | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | - -x, --exclude :: A regex for profiles allowed to be in complain mode. | ||||||||||||||||||||||||||||
| 58 | May be given more than once. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 7 | ▶ | sub option_spec { return ( 'profile|p=s@', 'min-profiles|m=i', 'exclude|x=s@' ) } | ||||||||||||||||||||||||||
|
Subroutine
option_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 7 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 7 | my ( $self, $options ) = @_; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 7 | my @problems; | |||||||||||||||||||||||||||
| 68 | 7 | ▶ | foreach my $regex ( @{ $options->{exclude} // [] } ) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$options->{"exclude"} // []
MC/DC100%1/1
$options->{"exclude"} // []
$options->{"exclude"}
Truth table100%2/2
A
A= $options->{"exclude"}
| |||||||||||||||||||||||||||||
| 69 | 5 | ▶ | push( @problems, '--exclude "' . $regex . '" is not a valid regex' ) if !eval { qr/$regex/; 1 }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 7 | return @problems; | |||||||||||||||||||||||||||
| 73 | } ## end sub validate | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =head1 METHODS | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =head2 check | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | Does the check. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 7 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=25 SCAR=32.3
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 7 | my ($self) = @_; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 7 | ▶ | die( 'not supported on ' . $self->os . "\n" ) if $self->os ne 'linux'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 6 | ▶ | my $enabled = read_file( $ENABLED, err_mode => 'quiet' ) // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
read_file($Monitoring::Sneck::Checks::apparmor::ENABLED, "err_mode", "quiet") // ""
MC/DC0%0/1
read_file($Monitoring::Sneck::Checks::apparmor::ENABLED, "err_mode", "quiet") // ""
read_file($Monitoring::Sneck::Checks::apparmor::ENABLED, "err_mode", "quiet")
Truth table50%1/2
A
A= read_file($Monitoring::Sneck::Checks::apparmor::ENABLED, "err_mode", "quiet")
| |||||||||||||||||||||||||||||
| 89 | 6 | ▶ | return $self->finish( $self->CRITICAL, 'AppArmor is not enabled' ) if $enabled !~ /\A\s*Y/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 5 | my $listed = read_file( $PROFILES, err_mode => 'quiet' ); | |||||||||||||||||||||||||||
| 91 | 5 | ▶ | return $self->finish( $self->UNKNOWN, 'can not read ' . $PROFILES . ', ' . $! ) if !defined($listed); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # lines such as "/usr/sbin/cupsd (enforce)", child profiles being "parent//child" | ||||||||||||||||||||||||||||
| 94 | 4 | my %profiles; | |||||||||||||||||||||||||||
| 95 | 4 | foreach my $line ( split( /\n/, $listed ) ) { | |||||||||||||||||||||||||||
| 96 | 48 | ▶ | $profiles{$1} = $2 if $line =~ /\A(.+) \((\w+)\)\s*\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 4 | my %modes; | |||||||||||||||||||||||||||
| 99 | 4 | $modes{$_}++ foreach values(%profiles); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 4 | my $count = scalar( keys(%profiles) ); | |||||||||||||||||||||||||||
| 102 | 4 | my @codes = ( $self->OK ); | |||||||||||||||||||||||||||
| 103 | 4 | ▶ | my @parts = ( $count . ' profile' . ( $count == 1 ? '' : 's' ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 4 | push( @parts, map { $modes{$_} . ' ' . $_ } sort( keys(%modes) ) ); | |||||||||||||||||||||||||||
| 105 | 4 | ▶ | if ( defined( $self->opt('min-profiles') ) && $count < $self->opt('min-profiles') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->opt("min-profiles") and $count < $self->opt("min-profiles")
MC/DC50%1/2
defined $self->opt("min-profiles") and $count < $self->opt("min-profiles")
defined $self->opt("min-profiles")
$count < $self->opt("min-profiles")
Truth table66%2/3
A and B
A= defined $self->opt("min-profiles")B= $count < $self->opt("min-profiles")
| |||||||||||||||||||||||||||||
| 106 | 1 | push( @codes, $self->CRITICAL ); | |||||||||||||||||||||||||||
| 107 | 1 | push( @parts, 'under ' . $self->opt('min-profiles') ); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 4 | ▶ | my %required = map { $_ => 1 } @{ $self->opt('profile') // [] }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->opt("profile") // []
MC/DC100%1/1
$self->opt("profile") // []
$self->opt("profile")
Truth table100%2/2
A
A= $self->opt("profile")
| |||||||||||||||||||||||||||||
| 111 | 4 | foreach my $profile ( sort( keys(%required) ) ) { | |||||||||||||||||||||||||||
| 112 | 3 | ▶ | next if ( $profiles{$profile} // '' ) eq 'enforce'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$profiles{$profile} // ""
MC/DC100%1/1
$profiles{$profile} // ""
$profiles{$profile}
Truth table100%2/2
A
A= $profiles{$profile}
| |||||||||||||||||||||||||||||
| 113 | 2 | push( @codes, $self->CRITICAL ); | |||||||||||||||||||||||||||
| 114 | push( @parts, | ||||||||||||||||||||||||||||
| 115 | defined( $profiles{$profile} ) | ||||||||||||||||||||||||||||
| 116 | 2 | ▶ | ? $profile . ' in ' . $profiles{$profile} . ' mode' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | : $profile . ' not loaded' ); | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # required ones are said above, so not again here | ||||||||||||||||||||||||||||
| 121 | 4 | ▶ | my @excludes = map { qr/$_/ } @{ $self->opt('exclude') // [] }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->opt("exclude") // []
MC/DC100%1/1
$self->opt("exclude") // []
$self->opt("exclude")
Truth table100%2/2
A
A= $self->opt("exclude")
| |||||||||||||||||||||||||||||
| 122 | 4 | my @complaining; | |||||||||||||||||||||||||||
| 123 | 4 | foreach my $profile ( sort( keys(%profiles) ) ) { | |||||||||||||||||||||||||||
| 124 | 48 | ▶ | next if $profiles{$profile} ne 'complain' || $required{$profile}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$profiles{$profile} ne "complain" or $required{$profile}
MC/DC100%2/2
$profiles{$profile} ne "complain" or $required{$profile}
$profiles{$profile} ne "complain"
$required{$profile}
Truth table100%3/3
A or B
A= $profiles{$profile} ne "complain"B= $required{$profile}
| |||||||||||||||||||||||||||||
| 125 | 23 | ▶ | next if grep { $profile =~ $_ } @excludes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 6 | push( @complaining, $profile ); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 4 | ▶ | if (@complaining) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 1 | push( @codes, $self->WARNING ); | |||||||||||||||||||||||||||
| 130 | 1 | my @named = @complaining; | |||||||||||||||||||||||||||
| 131 | 1 | ▶ | splice( @named, $COMPLAIN_NAMED ) if @named > $COMPLAIN_NAMED; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 1 | my $says = 'complaining: ' . join( ', ', @named ); | |||||||||||||||||||||||||||
| 133 | 1 | ▶ | $says = $says . ' and ' . ( @complaining - @named ) . ' more' if @complaining > @named; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 1 | push( @parts, $says ); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 4 | $self->perfdata( label => 'profiles', value => $count, min => 0 ); | |||||||||||||||||||||||||||
| 138 | 4 | ▶ | $self->perfdata( label => 'complain', value => $modes{complain} // 0, min => 0 ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$modes{"complain"} // 0
MC/DC0%0/1
$modes{"complain"} // 0
$modes{"complain"}
Truth table50%1/2
A
A= $modes{"complain"}
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 4 | return $self->finish( $self->worst(@codes), join( ', ', @parts ), map { 'complaining: ' . $_ } @complaining ); | |||||||||||||||||||||||||||
| 141 | } ## end sub check | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 22 | 1; | |||||||||||||||||||||||||||