Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions src/database/mssql/mode/deadlocks.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

package database::mssql::mode::deadlocks;

use base qw(centreon::plugins::templates::counter);

use strict;
use warnings;
use base qw(centreon::plugins::templates::counter);
use Digest::MD5 qw(md5_hex);

sub set_counters {
my ($self, %options) = @_;
Expand All @@ -32,8 +34,8 @@ sub set_counters {
];

$self->{maps_counters}->{deadlocks} = [
{ label => 'deadlocks', nlabel => 'mssql.deadlocks.count', set => {
key_values => [ { name => 'value' } ],
{ label => 'deadlocks', nlabel => 'mssql.deadlocks.count.persecond', set => {
key_values => [ { name => 'value', per_second => 1 } ],
output_template => '%.2f dead locks/s',
perfdatas => [
{ template => '%.2f', min => 0 },
Expand All @@ -46,11 +48,11 @@ sub set_counters {

sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1);
bless $self, $class;

$options{options}->add_options(arguments => {
"filter-database:s" => { name => 'filter_database' },
"filter-instance:s" => { name => 'filter_instance', default => '_Total' },
});

return $self;
Expand All @@ -67,18 +69,21 @@ sub manage_selection {
sys.dm_os_performance_counters
WHERE
object_name = 'SQLServer:Locks'
AND
counter_name LIKE 'Number of Deadlocks/sec%'
AND counter_name LIKE 'Number of Deadlocks/sec%'
});

my $query_result = $options{sql}->fetchall_arrayref();
$self->{deadlocks}->{value} = 0;

foreach my $row (@{$query_result}) {
next if (defined($self->{option_results}->{filter_database}) && $self->{option_results}->{filter_database} ne ''
&& $$row[0] !~ /$self->{option_results}->{filter_database}/);
next if (defined($self->{option_results}->{filter_instance}) && $self->{option_results}->{filter_instance} ne ''

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User-provided filter_instance is directly interpolated into a regex ($$row[0] !~ /$self->{option_results}->{filter_instance}/), enabling regex injection or ReDoS. Validate or escape (e.g., quotemeta) filter_instance before use.

Details

✨ AI Reasoning
​The code now uses the CLI option filter_instance (defaulting to '_Total' but still user-controlled) directly inside a Perl regex match: $$row[0] !~ /$self->{option_results}->{filter_instance}/. Interpolating unvalidated user input into a regex can lead to regex injection, syntax errors, or ReDoS issues. This was introduced by the PR when replacing filter_database with filter_instance and should be flagged. The problematic expression appears in the newly added filtering logic in both changed files.

🔧 How do I fix it?
Use parameterized queries with placeholders, array-based command execution (no shell interpretation), or properly escaped arguments using vetted libraries. Avoid dynamic queries/commands built with user input concatenation.

Reply @AikidoSec feedback: [FEEDBACK] to get better review comments in the future.
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info

&& $$row[0] !~ /$self->{option_results}->{filter_instance}/);
$self->{deadlocks}->{value} += $$row[1];
}

$self->{cache_name} = 'mssql_' . $self->{mode} . '_' .
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' .
(defined($self->{option_results}->{filter_instance}) ? md5_hex($self->{option_results}->{filter_instance}) : md5_hex('all'));
}

1;
Expand All @@ -99,9 +104,10 @@ Warning threshold number of dead locks per second.

Critical threshold number of dead locks per second.

=item B<--filter-database>
=item B<--filter-instance>

Filter the databases to monitor with a regular expression.
Filter the sub-category inside the performance object. The instance_name represents the lock type. For example: C<_Total>, C<DATABASE>, C<OBJECT>, C<PAGE>, C<KEY>. (default: '_Total')
https://learn.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-locks-object?view=sql-server-ver17

=back

Expand Down
28 changes: 17 additions & 11 deletions src/database/mssql/mode/lockswaits.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

package database::mssql::mode::lockswaits;

use base qw(centreon::plugins::templates::counter);

use strict;
use warnings;
use base qw(centreon::plugins::templates::counter);
use Digest::MD5 qw(md5_hex);

sub set_counters {
my ($self, %options) = @_;
Expand All @@ -32,8 +34,8 @@ sub set_counters {
];

$self->{maps_counters}->{lockswaits} = [
{ label => 'lockswaits', nlabel => 'mssql.lockswaits.count', set => {
key_values => [ { name => 'value' } ],
{ label => 'lockswaits', nlabel => 'mssql.lockswaits.count.persecond', set => {
key_values => [ { name => 'value', per_second => 1 } ],
output_template => '%.2f locks waits/s',
perfdatas => [
{ template => '%.2f', min => 0 }
Expand All @@ -46,11 +48,11 @@ sub set_counters {

sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1);
bless $self, $class;

$options{options}->add_options(arguments => {
'filter-database:s' => { name => 'filter_database' }
'filter-instance:s' => { name => 'filter_instance', default => '_Total' }
});

return $self;
Expand All @@ -67,18 +69,21 @@ sub manage_selection {
sys.dm_os_performance_counters
WHERE
object_name = 'SQLServer:Locks'
AND
counter_name LIKE 'Lock Waits/sec%'
AND counter_name LIKE 'Lock Waits/sec%'
});

my $query_result = $options{sql}->fetchall_arrayref();
$self->{lockswaits}->{value} = 0;

foreach my $row (@{$query_result}) {
next if (defined($self->{option_results}->{filter_database}) && $self->{option_results}->{filter_database} ne ''
&& $$row[0] !~ /$self->{option_results}->{filter_database}/);
next if (defined($self->{option_results}->{filter_instance}) && $self->{option_results}->{filter_instance} ne ''

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User-provided filter_instance is directly interpolated into a regex ($$row[0] !~ /$self->{option_results}->{filter_instance}/), enabling regex injection or ReDoS. Validate or escape (e.g., quotemeta) filter_instance before use.

Details

✨ AI Reasoning
​In lockswaits.pm the PR added the same direct regex interpolation using the CLI-provided filter_instance option: $$row[0] !~ /$self->{option_results}->{filter_instance}/. This introduces the same risk of regex injection or denial-of-service via crafted patterns. The change replaced the previous filter_database usage, so this new usage should be reviewed.

🔧 How do I fix it?
Use parameterized queries with placeholders, array-based command execution (no shell interpretation), or properly escaped arguments using vetted libraries. Avoid dynamic queries/commands built with user input concatenation.

Reply @AikidoSec feedback: [FEEDBACK] to get better review comments in the future.
Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info

&& $$row[0] !~ /$self->{option_results}->{filter_instance}/);
$self->{lockswaits}->{value} += $$row[1];
}

$self->{cache_name} = 'mssql_' . $self->{mode} . '_' .
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' .
(defined($self->{option_results}->{filter_instance}) ? md5_hex($self->{option_results}->{filter_instance}) : md5_hex('all'));
}

1;
Expand All @@ -99,9 +104,10 @@ Warning threshold number of lock-waits per second.

Critical threshold number of lock-waits per second.

=item B<--filter-database>
=item B<--filter-instance>

Filter the databases to monitor with a regular expression.
Filter the sub-category inside the performance object. The instance_name represents the lock type. For example: C<_Total>, C<DATABASE>, C<OBJECT>, C<PAGE>, C<KEY>. (default: '_Total')
https://learn.microsoft.com/en-us/sql/relational-databases/performance-monitor/sql-server-locks-object?view=sql-server-ver17

=back

Expand Down