Skip to contentSkip to conversation

lib/ts_bm: reset initial match offset for every block of text

PATCH Superseded Jeremy Sowden 2 participants 3 messages Jun 11, 2023 – Jun 19, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/ts_bm: reset initial match offset for every block of text

The shift variable which indicates the offset in the string at which to start matching the pattern is initialized to bm->patlen - 1, but it is not reset when a new block is retrieved. This means the implemen- tation may start looking at later and later positions in each successive block and miss occurrences of the pattern at the beginning. E.g., consider a HTTP packet held in a non-linear skb, where the HTTP request line occurs in the second block:

[... 52 bytes of packet headers ...]
GET /bmtest HTTP/1.1\r\nHost: www.example.com\r\n\r\n

and the pattern is "GET /bmtest".

Once the first block comprising the packet headers has been examined, shift will be pointing to somewhere near the end of the block, and so when the second block is examined the request line at the beginning will be missed.

Reinitialize the variable for each new block.

Adjust some indentation and remove some trailing white-space at the same time.

Fixes 8082e4ed0a61 ("[LIB]: Boyer-Moore extension for textsearch infrastructure strike #2")
---
 lib/ts_bm.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
lib/ts_bm.c +9 -7
index 1f2234221dd1..ef448490a2cc 100644
@@ -60,23 +60,25 @@ static unsigned int bm_find(struct ts_config *conf, struct ts_state *state)
 	struct ts_bm *bm = ts_config_priv(conf);
 	unsigned int i, text_len, consumed = state->offset;
 	const u8 *text;
-	int shift = bm->patlen - 1, bs;
+	int bs;
 	const u8 icase = conf->flags & TS_IGNORECASE;
 
 	for (;;) {
+		int shift = bm->patlen - 1;
+
 		text_len = conf->get_next_block(consumed, &text, conf, state);
 
 		if (unlikely(text_len == 0))
 			break;
 
 		while (shift < text_len) {
-			DEBUGP("Searching in position %d (%c)\n", 
-				shift, text[shift]);
-			for (i = 0; i < bm->patlen; i++) 
+			DEBUGP("Searching in position %d (%c)\n",
+			       shift, text[shift]);
+			for (i = 0; i < bm->patlen; i++)
 				if ((icase ? toupper(text[shift-i])
-				    : text[shift-i])
-					!= bm->pattern[bm->patlen-1-i])
-				     goto next;
+				     : text[shift-i])
+				    != bm->pattern[bm->patlen-1-i])
+					goto next;
 
 			/* London calling... */
 			DEBUGP("found!\n");
Signature · 1 line
2.39.2
#1 lore raw

Hi Jeremy,

Quoted 35 lines · Jeremy Sowden

The shift variable which indicates the offset in the string at which to start matching the pattern is initialized to bm->patlen - 1, but it is not reset when a new block is retrieved. This means the implemen- tation may start looking at later and later positions in each successive block and miss occurrences of the pattern at the beginning. E.g., consider a HTTP packet held in a non-linear skb, where the HTTP request line occurs in the second block:

[... 52 bytes of packet headers ...]
GET /bmtest HTTP/1.1\r\nHost: www.example.com\r\n\r\n

and the pattern is "GET /bmtest".

Once the first block comprising the packet headers has been examined, shift will be pointing to somewhere near the end of the block, and so when the second block is examined the request line at the beginning will be missed.

Reinitialize the variable for each new block.

Adjust some indentation and remove some trailing white-space at the same time.

Fixes 8082e4ed0a61 ("[LIB]: Boyer-Moore extension for textsearch infrastructure strike #2")
---
 lib/ts_bm.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
lib/ts_bm.c +0 -0
index 1f2234221dd1..ef448490a2cc 100644

lib/ts_bm.c static unsigned int bm_find(struct ts_config *conf, struct ts_state *state) · line 67

Quoted 9 lines
lib/ts_bm.c +2 -1
@@ -60,23 +60,25 @@ static unsigned int bm_find(struct ts_config *conf, struct ts_state *state)
 	struct ts_bm *bm = ts_config_priv(conf);
 	unsigned int i, text_len, consumed = state->offset;
 	const u8 *text;
-	int shift = bm->patlen - 1, bs;
+	int bs;
 	const u8 icase = conf->flags & TS_IGNORECASE;
 
 	for (;;) {
+		int shift = bm->patlen - 1;

This line is the fix, right?

lib/ts_bm.c static unsigned int bm_find(struct ts_config *conf, struct ts_state *state) · line 73

Quoted 5 lines
lib/ts_bm.c +0 -0
 		text_len = conf->get_next_block(consumed, &text, conf, state);
 
 		if (unlikely(text_len == 0))
 			break;
 

These updates below are a clean up, right? If so, maybe split this in two patches I'd suggest?

lib/ts_bm.c static unsigned int bm_find(struct ts_config *conf, struct ts_state *state) · line 80

Quoted 13 lines
lib/ts_bm.c +5 -6
 		while (shift < text_len) {
-			DEBUGP("Searching in position %d (%c)\n", 
-				shift, text[shift]);
-			for (i = 0; i < bm->patlen; i++) 
+			DEBUGP("Searching in position %d (%c)\n",
+			       shift, text[shift]);
+			for (i = 0; i < bm->patlen; i++)
 				if ((icase ? toupper(text[shift-i])
-				    : text[shift-i])
-					!= bm->pattern[bm->patlen-1-i])
-				     goto next;
+				     : text[shift-i])
+				    != bm->pattern[bm->patlen-1-i])

Maybe disentagle this with a few helper functions?

static char bm_get_char(const char *text, unsigned int pos, bool icase)

{
        return icase ? toupper(text[pos]) : text[pos];
}

Thanks

Quoted 9 lines
lib/ts_bm.c +1 -1
 				if ((icase ? toupper(text[shift-i])
-				    : text[shift-i])
+					goto next;
 
 			/* London calling... */
 			DEBUGP("found!\n");
Signature · 1 line
2.39.2
#2 lore raw
Quoted 43 lines · Pablo Neira Ayuso
Quoted 42 lines · Jeremy Sowden

The shift variable which indicates the offset in the string at which to start matching the pattern is initialized to bm->patlen - 1, but it is not reset when a new block is retrieved. This means the implemen- tation may start looking at later and later positions in each successive block and miss occurrences of the pattern at the beginning. E.g., consider a HTTP packet held in a non-linear skb, where the HTTP request line occurs in the second block:

[... 52 bytes of packet headers ...]
GET /bmtest HTTP/1.1\r\nHost: www.example.com\r\n\r\n

and the pattern is "GET /bmtest".

Once the first block comprising the packet headers has been examined, shift will be pointing to somewhere near the end of the block, and so when the second block is examined the request line at the beginning will be missed.

Reinitialize the variable for each new block.

Adjust some indentation and remove some trailing white-space at the same time.

Fixes 8082e4ed0a61 ("[LIB]: Boyer-Moore extension for textsearch infrastructure strike #2")
---
 lib/ts_bm.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
lib/ts_bm.c +1 -1
index 1f2234221dd1..ef448490a2cc 100644
@@ -60,23 +60,25 @@ static unsigned int bm_find(struct ts_config *conf, struct ts_state *state)
 	struct ts_bm *bm = ts_config_priv(conf);
 	unsigned int i, text_len, consumed = state->offset;
 	const u8 *text;
-	int shift = bm->patlen - 1, bs;
+	int bs;
 	const u8 icase = conf->flags & TS_IGNORECASE;
 
Quoted 4 lines

lib/ts_bm.c static unsigned int bm_find(struct ts_config *conf, struct ts_state *state) · line 67

Quoted 2 lines
lib/ts_bm.c +1 -0
 	for (;;) {
+		int shift = bm->patlen - 1;

This line is the fix, right?

Yup.

Quoted 8 lines

lib/ts_bm.c static unsigned int bm_find(struct ts_config *conf, struct ts_state *state) · line 73

Quoted 5 lines
lib/ts_bm.c +0 -0
 		text_len = conf->get_next_block(consumed, &text, conf, state);
 
 		if (unlikely(text_len == 0))
 			break;
 

These updates below are a clean up, right? If so, maybe split this in two patches I'd suggest?

Sure.

Quoted 16 lines

lib/ts_bm.c static unsigned int bm_find(struct ts_config *conf, struct ts_state *state) · line 80

Quoted 13 lines
lib/ts_bm.c +5 -6
 		while (shift < text_len) {
-			DEBUGP("Searching in position %d (%c)\n", 
-				shift, text[shift]);
-			for (i = 0; i < bm->patlen; i++) 
+			DEBUGP("Searching in position %d (%c)\n",
+			       shift, text[shift]);
+			for (i = 0; i < bm->patlen; i++)
 				if ((icase ? toupper(text[shift-i])
-				    : text[shift-i])
-					!= bm->pattern[bm->patlen-1-i])
-				     goto next;
+				     : text[shift-i])
+				    != bm->pattern[bm->patlen-1-i])

Maybe disentagle this with a few helper functions?

Quoted 4 lines

static char bm_get_char(const char *text, unsigned int pos, bool icase)

{
        return icase ? toupper(text[pos]) : text[pos];
}

Sure.

Quoted 11 lines

Thanks

Quoted 9 lines
lib/ts_bm.c +1 -1
 				if ((icase ? toupper(text[shift-i])
-				    : text[shift-i])
+					goto next;
 
 			/* London calling... */
 			DEBUGP("found!\n");
Signature · 1 line
2.39.2

J.