Skip to content

Commit

Permalink
Prevent pattern matching abuse (CVE-2024-31228)
Browse files Browse the repository at this point in the history
  • Loading branch information
oranagra committed Oct 2, 2024
1 parent 6b0a3fe commit 736be20
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@

/* Glob-style pattern matching. */
static int stringmatchlen_impl(const char *pattern, int patternLen,
const char *string, int stringLen, int nocase, int *skipLongerMatches)
const char *string, int stringLen, int nocase, int *skipLongerMatches, int nesting)
{
/* Protection against abusive patterns. */
if (nesting > 1000) return 0;

while(patternLen && stringLen) {
switch(pattern[0]) {
case '*':
Expand All @@ -68,7 +71,7 @@ static int stringmatchlen_impl(const char *pattern, int patternLen,
return 1; /* match */
while(stringLen) {
if (stringmatchlen_impl(pattern+1, patternLen-1,
string, stringLen, nocase, skipLongerMatches))
string, stringLen, nocase, skipLongerMatches, nesting+1))
return 1; /* match */
if (*skipLongerMatches)
return 0; /* no match */
Expand Down Expand Up @@ -190,7 +193,7 @@ static int stringmatchlen_impl(const char *pattern, int patternLen,
int stringmatchlen(const char *pattern, int patternLen,
const char *string, int stringLen, int nocase) {
int skipLongerMatches = 0;
return stringmatchlen_impl(pattern,patternLen,string,stringLen,nocase,&skipLongerMatches);
return stringmatchlen_impl(pattern,patternLen,string,stringLen,nocase,&skipLongerMatches,0);
}

int stringmatch(const char *pattern, const char *string, int nocase) {
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/keyspace.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,12 @@ foreach {type large} [array get largevalue] {
r KEYS "a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*b"
} {}

test {Regression for pattern matching very long nested loops} {
r flushdb
r SET [string repeat "a" 50000] 1
r KEYS [string repeat "*?" 50000]
} {}

test {Coverage: basic SWAPDB test and unhappy path} {
r flushall
r select 0
Expand Down

0 comments on commit 736be20

Please sign in to comment.