Skip to content
This repository was archived by the owner on Jan 26, 2026. It is now read-only.

Commit 21adb54

Browse files
Jakujecryptomilk
authored andcommitted
match: Limit possible recursion when parsing wildcards to a sensible number
Fixes T186 Signed-off-by: Jakub Jelen <jjelen@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org> (cherry picked from commit 31f9c39)
1 parent 13fa009 commit 21adb54

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

src/match.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@
4343

4444
#include "libssh/priv.h"
4545

46+
#define MAX_MATCH_RECURSION 32
47+
4648
/*
4749
* Returns true if the given string matches the pattern (which may contain ?
4850
* and * as wildcards), and zero if it does not match.
4951
*/
50-
static int match_pattern(const char *s, const char *pattern)
52+
static int match_pattern(const char *s, const char *pattern, size_t limit)
5153
{
5254
bool had_asterisk = false;
53-
if (s == NULL || pattern == NULL) {
55+
if (s == NULL || pattern == NULL || limit <= 0) {
5456
return 0;
5557
}
5658

@@ -79,7 +81,7 @@ static int match_pattern(const char *s, const char *pattern)
7981
* those.
8082
*/
8183
for (; *s; s++)
82-
if (*s == *pattern && match_pattern(s + 1, pattern + 1)) {
84+
if (*s == *pattern && match_pattern(s + 1, pattern + 1, limit - 1)) {
8385
return 1;
8486
}
8587
/* Failed. */
@@ -90,7 +92,7 @@ static int match_pattern(const char *s, const char *pattern)
9092
* match at each position.
9193
*/
9294
for (; *s; s++) {
93-
if (match_pattern(s, pattern)) {
95+
if (match_pattern(s, pattern, limit - 1)) {
9496
return 1;
9597
}
9698
}
@@ -167,7 +169,7 @@ int match_pattern_list(const char *string, const char *pattern,
167169
sub[subi] = '\0';
168170

169171
/* Try to match the subpattern against the string. */
170-
if (match_pattern(string, sub)) {
172+
if (match_pattern(string, sub, MAX_MATCH_RECURSION)) {
171173
if (negated) {
172174
return -1; /* Negative */
173175
} else {

0 commit comments

Comments
 (0)