blib/lib/Monitoring/Sneck/Checks/pkg_audit.pm

filter: Statementstmt 100% 69 / 69 Branchbran 93.3% 28 / 30 Conditioncond 83.3% 20 / 24 MC/DCmcdc 71.4% 10 / 14 Subroutinesub 100% 10 / 10 Podpod 100% 6 / 6 totaltotal 93.4% 143 / 153 CC 26 SCAR 25.4
CC
26
Cov
95%
check
20.0
db_age
5.2
validate
3.0
CRAP 12.6
« blib/lib/Monitoring/Sneck/Checks/mailq.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/neighbors.pm »
1 package Monitoring::Sneck::Checks::pkg_audit;
2  
3 19 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use parent 'Monitoring::Sneck::Checks::base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Monitoring::Sneck::Checks::pkg_audit - checks FreeBSD packages for known vulnerabilities
11  
12 =head1 VERSION
13  
14 Version 0.0.1
15  
16 =cut
17  
18 19 our $VERSION = '0.0.1';
19  
20 # Where pkg is. Set by tests.
21 19 our $PKG = '/usr/sbin/pkg';
22  
23 =head1 SYNOPSIS
24  
25 !pkg_audit|interval=3600|perl:pkg_audit
26 !pkg_audit_www|interval=3600|perl:pkg_audit -j www
27 !pkg_audit_fetch|interval=86400 timeout=300|perl:pkg_audit -F -w 0 -c 5
28  
29 =head1 DESCRIPTION
30  
31 Runs pkg audit and counts the installed packages it says are
32 vulnerable. By default any is CRITICAL.
33  
34 pkg audit goes by the vulnerability database it has, which the daily
35 periodic security run fetches. A database older than --max-age days is
36 WARNING, as it may be missing what was found since, the same as
37 periodic warns. With --fetch, pkg fetches it first, which needs the
38 network and should not be done every few minutes, so use a compliance
39 check with a interval, as in the synopsis.
40  
41 pkg failing, or not being there, is UNKNOWN.
42  
43 =head1 OPTIONS
44  
45 - -j, --jail :: Audit the packages in this jail, through pkg -j. The
46 age of the database is not checked unless --db is given.
47  
48 - -F, --fetch :: Fetch the vulnerability database first.
49  
50 - -d, --db :: The vulnerability database.
51 Default :: /var/db/pkg/vuln.xml
52  
53 - -a, --max-age :: Days old the database may be. 0 to not check it.
54 Default :: 2
55  
56 - -w, --warning :: Range for the number of vulnerable packages.
57  
58 - -c, --critical :: Range for the number of vulnerable packages.
59 Default :: 0
60  
61 =cut
62  
63 55 ▶sub option_spec { return ( 'jail|j=s', 'fetch|F', 'db|d=s', 'max-age|a=i', 'warning|w=s', 'critical|c=s' ) }
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
64  
65 55 ▶sub defaults { return { 'max-age' => 2, critical => '0' } }
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
66  
67 55 ▶sub ranges { return ( 'warning', 'critical' ) }
Subroutine ranges: called CC=1 SCAR=0.0
Pod documented
68  
69 55 ▶sub validate {
Subroutine validate: called CC=3 SCAR=11.0
Pod documented
70 55 my ( $self, $options ) = @_;
71  
72 return ( '--jail "' . $options->{jail} . '" is not a jail name or ID' )
73 55 ▶ if defined( $options->{jail} ) && $options->{jail} !~ /\A[A-Za-z0-9_.-]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"jail"} and not $options->{"jail"} =~ /\A[A-Za-z0-9_.-]+\z/u 1 54 55
Condition100%3/3
defined $options->{"jail"} and not $options->{"jail"} =~ /\A[A-Za-z0-9_.-]+\z/u
!ll&&!rl&&r
41131
MC/DC100%2/2
defined $options->{"jail"} and not $options->{"jail"} =~ /\A[A-Za-z0-9_.-]+\z/u
defined $options->{"jail"} not $options->{"jail"} =~ /\A[A-Za-z0-9_.-]+\z/u
Truth table100%3/3
A and not(B)
A= defined $options->{"jail"}B= not $options->{"jail"} =~ /\A[A-Za-z0-9_.-]+\z/u
ABresult
0X0
100
111
74  
75 54 return ();
76 }
77  
78 =head1 METHODS
79  
80 =head2 check
81  
82 Does the check.
83  
84 =cut
85  
86 54 ▶sub check {
Subroutine check: called CC=20 SCAR=30.0
Pod documented
87 54 my ($self) = @_;
88  
89 54 my @command = ($PKG);
90 54 ▶ push( @command, '-j', $self->opt('jail') ) if defined( $self->opt('jail') );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->opt("jail") 13 41 54
91 54 push( @command, 'audit' );
92 54 ▶ push( @command, '-F' ) if $self->opt('fetch');
Branch: 1 branch
BranchTrueFalseTotal
if $self->opt("fetch") 8 46 54
93 54 my ( $exit, $output ) = $self->run_command(@command);
94  
95 # 0 is nothing vulnerable and 1 is something is
96 45 ▶ if ( $exit > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit > 1) 1 44 45
97 1 ▶ my $first = ( grep { /\S/ } split( /\n/, $output ) )[0] // '';
Condition50%1/2
(grep {/\S/u;} split(/\n/u, $output, 0))[0] // ""
l!l
10
MC/DC0%0/1
(grep {/\S/u;} split(/\n/u, $output, 0))[0] // ""
(grep {/\S/u;} split(/\n/u, $output, 0))[0]
Truth table50%1/2
A
A= (grep {/\S/u;} split(/\n/u, $output, 0))[0]
Aresult
11
00
98 1 return $self->finish( $self->UNKNOWN, 'pkg audit exited ' . $exit . ', "' . $first . '"' );
99 }
100  
101 # "name-1.2 is vulnerable:", then the problem, CVEs, and a link, each indented
102 44 my @vulnerable;
103 my %problems;
104 44 my $current;
105 44 foreach my $line ( split( /\n/, $output ) ) {
106 434 ▶ if ( $line =~ /\A(\S+) is vulnerable:\s*\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A(\S+) is vulnerable:\s*\z/u) 78 356 434
107 78 $current = $1;
108 78 push( @vulnerable, $current );
109 78 $problems{$current} = [];
110 78 next;
111 }
112 356 ▶ next if !defined($current) || $line !~ /\A\s+(\S.*?)\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current and $line =~ /\A\s+(\S.*?)\s*\z/u 122 234 356
Condition100%3/3
defined $current and $line =~ /\A\s+(\S.*?)\s*\z/u
!ll&&!rl&&r
5117234
MC/DC100%2/2
defined $current and $line =~ /\A\s+(\S.*?)\s*\z/u
defined $current $line =~ /\A\s+(\S.*?)\s*\z/u
Truth table100%3/3
A and B
A= defined $currentB= $line =~ /\A\s+(\S.*?)\s*\z/u
ABresult
0X0
100
111
113 234 my $text = $1;
114 234 ▶ next if $text =~ /\A(?:CVE|WWW):/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\A(?:CVE|WWW):/u 156 78 234
115 78 push( @{ $problems{$current} }, $text );
116 } ## end foreach my $line ( split( /\n/, $output ) )
117 # such as the database not being there
118 44 ▶ if ( $exit == 1 && !defined( $vulnerable[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit == 1 and not defined $vulnerable[0]) 2 42 44
Condition100%3/3
$exit == 1 and not defined $vulnerable[0]
!ll&&!rl&&r
3392
MC/DC100%2/2
$exit == 1 and not defined $vulnerable[0]
$exit == 1 not defined $vulnerable[0]
Truth table100%3/3
A and not(B)
A= $exit == 1B= not defined $vulnerable[0]
ABresult
0X0
100
111
119 2 ▶ my $first = ( grep { /\S/ } split( /\n/, $output ) )[0] // '';
Condition50%1/2
(grep {/\S/u;} split(/\n/u, $output, 0))[0] // ""
l!l
20
MC/DC0%0/1
(grep {/\S/u;} split(/\n/u, $output, 0))[0] // ""
(grep {/\S/u;} split(/\n/u, $output, 0))[0]
Truth table50%1/2
A
A= (grep {/\S/u;} split(/\n/u, $output, 0))[0]
Aresult
11
00
120 2 return $self->finish( $self->UNKNOWN,
121 'pkg audit exited 1 without saying what is vulnerable, "' . $first . '"' );
122 }
123  
124 42 my $count = scalar(@vulnerable);
125 42 my @codes = ( $self->threshold_status( $count, $self->opt('warning'), $self->opt('critical') ) );
126 42 my @notes;
127 42 my $age = $self->db_age;
128 42 ▶ if ( defined($age) && $self->opt('max-age') && $age > $self->opt('max-age') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $age and $self->opt("max-age") and $age > $self->opt("max-age")) 6 36 42
Condition100%6/6
defined $age and $self->opt("max-age") and $age > $self->opt("max-age")
!ll&&!rl&&r
16206
defined $age and $self->opt("max-age")
!ll&&!rl&&r
11526
MC/DC100%3/3
defined $age and $self->opt("max-age") and $age > $self->opt("max-age")
defined $age $self->opt("max-age") $age > $self->opt("max-age")
Truth table100%4/4
A and B and C
A= defined $ageB= $self->opt("max-age")C= $age > $self->opt("max-age")
ABCresult
0XX0
10X0
1100
1111
129 6 push( @codes, $self->WARNING );
130 6 push( @notes, 'the vulnerability database is ' . sprintf( '%.0f', $age ) . ' days old' );
131 }
132 $self->perfdata(
133 42 label => 'vulnerable',
134 value => $count,
135 warning => $self->opt('warning'),
136 critical => $self->opt('critical'),
137 min => 0
138 );
139  
140 42 ▶ my $message = $count . ' vulnerable package' . ( $count == 1 ? '' : 's' );
Branch: 1 branch
BranchTrueFalseTotal
$count == 1 ? : 0 42 42
141 42 ▶ $message = $message . ' in jail ' . $self->opt('jail') if defined( $self->opt('jail') );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->opt("jail") 11 31 42
142 42 ▶ $message = $message . ', ' . join( ', ', @vulnerable ) if defined( $vulnerable[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $vulnerable[0] 39 3 42
143 42 ▶ $message = $message . ', ' . join( ', ', @notes ) if defined( $notes[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined $notes[0] 6 36 42
144 42 my @details = map { $_ . ': ' . join( '; ', @{ $problems{$_} } ) } grep { defined( $problems{$_}[0] ) } @vulnerable;
145  
146 42 return $self->finish( $self->worst(@codes), $message, @details );
147 } ## end sub check
148  
149 =head2 db_age
150  
151 Returns the days since the vulnerability database was changed, or
152 undef if it is not checked, as for a jail without --db, or not there.
153  
154 =cut
155  
156 42 ▶sub db_age {
Subroutine db_age: called CC=5 SCAR=16.6
Pod documented
157 42 my ($self) = @_;
158  
159 42 ▶ return undef if defined( $self->opt('jail') ) && !defined( $self->opt('db') );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->opt("jail") and not defined $self->opt("db") 11 31 42
Condition66%2/3
defined $self->opt("jail") and not defined $self->opt("db")
!ll&&!rl&&r
31011
MC/DC50%1/2
defined $self->opt("jail") and not defined $self->opt("db")
defined $self->opt("jail") not defined $self->opt("db")
Truth table66%2/3
A and not(B)
A= defined $self->opt("jail")B= not defined $self->opt("db")
ABresult
0X0
100
111
160 31 ▶ my @stat = stat( $self->opt('db') // '/var/db/pkg/vuln.xml' );
Condition50%1/2
$self->opt("db") // "/var/db/pkg/vuln.xml"
l!l
310
MC/DC0%0/1
$self->opt("db") // "/var/db/pkg/vuln.xml"
$self->opt("db")
Truth table50%1/2
A
A= $self->opt("db")
Aresult
11
00
161 31 ▶ return undef if !defined( $stat[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stat[0] 0 31 31
162  
163 31 return ( time - $stat[9] ) / 86400;
164 }
165  
166 19 1;
« blib/lib/Monitoring/Sneck/Checks/mailq.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/neighbors.pm »