From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33A15C77B7A for ; Sun, 11 Jun 2023 08:18:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232405AbjFKISp (ORCPT ); Sun, 11 Jun 2023 04:18:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229902AbjFKISp (ORCPT ); Sun, 11 Jun 2023 04:18:45 -0400 Received: from taras.nevrast.org (unknown [IPv6:2a05:d01c:431:aa03:b7e1:333d:ea2a:b14e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87FC4E1 for ; Sun, 11 Jun 2023 01:18:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=azazel.net; s=20220717; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject: To:From:Sender:Reply-To:Cc:Content-Type:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=JxBKzhD65IMq7HWEbMIAcrw+ranwp/xfMACZzbSGrW0=; b=IJnI3c01f+/uFMVJcir/+wkxUD T+lIZ+8t7FXzz20/OKsPOSnlY4S7tUyq+0QK1dGLZeNaP5E0UQJsQOCUNdeMDu+58LrFBMEfxKQU3 joYpYLIMNOrMUhMarO4eoshqKRi+owb+65BR+VYZG2eM2FlNmn/TzZLuVySYCunHVQPH3Nz+DiV1y ag3LOJfV8GGoqcJKZ/uUC9ShrvZVVPQjG8s1ZsDMKG/GPPaIR7ZIy9EgDkkxzzgOLN/6zGZANthcL i/547YO7Qb8jwPJO5Y7PMV3zgG/KquaX9obRdCO7BaVnsPjrxf7LTg/mgFINJEe1IM9J2jd3KexpE dlPQ8OiQ==; Received: from [2001:8b0:135f:bcd1:e0cb:4eff:fedf:e608] (helo=ulthar.dreamlands) by taras.nevrast.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1q8GHY-005Pye-PH for netfilter-devel@vger.kernel.org; Sun, 11 Jun 2023 09:18:40 +0100 From: Jeremy Sowden To: Netfilter Devel Subject: [PATCH nf] lib/ts_bm: reset initial match offset for every block of text Date: Sun, 11 Jun 2023 09:17:19 +0100 Message-Id: <20230611081719.612675-1-jeremy@azazel.net> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 2001:8b0:135f:bcd1:e0cb:4eff:fedf:e608 X-SA-Exim-Mail-From: jeremy@azazel.net X-SA-Exim-Scanned: No (on taras.nevrast.org); SAEximRunCond expanded to false Precedence: bulk List-ID: X-Mailing-List: netfilter-devel@vger.kernel.org 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") Link: https://bugzilla.netfilter.org/show_bug.cgi?id=1390 Signed-off-by: Jeremy Sowden --- lib/ts_bm.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/ts_bm.c b/lib/ts_bm.c index 1f2234221dd1..ef448490a2cc 100644 --- a/lib/ts_bm.c +++ b/lib/ts_bm.c @@ -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"); -- 2.39.2