🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #9246

[PATCH v2 1/2] lib/ts_fsm: preserve token types when exporting patterns

#9246
From: Zhiqi Zhang
Date: Sun, 01 Nov 2026 08:17
71 lines
13996 bytes
Dumping and restoring a tc filter with an FSM text ematch can silently
change which packets it matches, or fail to restore it with EINVAL. This
happens when userspace reuses the pattern returned by RTM_GETTFILTER to
configure another filter. Dumping alone does not change the live filter.

fsm_init() replaces each public token type with an internal character-class
mask, and fsm_get_pattern() returns that converted array. For example,
TS_FSM_DIGIT (2) becomes the _D mask (4). Restoring the exported pattern
interprets 4 as TS_FSM_PRINT. With a DIGIT/MULTI pattern, a rule that
matched the UDP payload "7777" but rejected "AAAA" starts matching both
after restoration. Other exported masks exceed TS_FSM_TYPE_MAX and are
rejected with EINVAL.

Keep the public token types in the configuration and look up their masks
in match_token(). This preserves the exported representation without
adding another pattern allocation. Initialization still validates every
type before it can be used to index token_map. The extra lookup during
character-class matching avoids storing a second copy of the pattern.

I identified this issue. OpenAI Codex helped confirm it and drafted the
fix and changelog. I reviewed the fix, built it and verified that the
tests passed.

Fixes: 6408f79cce40 ("[LIB]: Naive finite state machine based textsearch")
Cc: stable@vger.kernel.org
Signed-off-by: Zhiqi Zhang <zhiqizhangg@gmail.com>
---
Changes in v2: describe the tc impact, split out the KUnit tests and add
stable. The production code change is unchanged from v1.

Validation: updated KUnit tests change from 2/15 to 15/15 passing on ARM64
GCC; ARM32 GCC and ARM64 Clang also pass. An isolated QEMU rtnetlink/UDP
test demonstrates altered packet matching and EINVAL restoration before
the fix, with both corrected afterwards. See the cover letter for details.

An ARM64 userspace microbenchmark of the extracted matcher measured median
paired slowdowns of about 1-5% for tested long character-class scans and
13% for a one-byte rejection case with appreciable variability. This is
not a measurement of in-kernel or packet throughput.

 lib/ts_fsm.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/lib/ts_fsm.c b/lib/ts_fsm.c
index 053615f4f..8725ea6f0 100644
--- a/lib/ts_fsm.c
+++ b/lib/ts_fsm.c
@@ -126,7 +126,7 @@ _W|_L,         _W|_L,        _W|_L,        _W|_L};		/* 252-255 */
 static inline int match_token(struct ts_fsm_token *t, u8 d)
 {
 	if (t->type)
-		return (token_lookup_tbl[d] & t->type) != 0;
+		return (token_lookup_tbl[d] & token_map[t->type]) != 0;
 	else
 		return t->value == d;
 }
@@ -288,11 +288,6 @@ static struct ts_config *fsm_init(const void *pattern, unsigned int len,
 	fsm->ntokens = ntokens;
 	memcpy(fsm->tokens, pattern, len);
 
-	for (i = 0; i < fsm->ntokens; i++) {
-		struct ts_fsm_token *t = &fsm->tokens[i];
-		t->type = token_map[t->type];
-	}
-
 	return conf;
 
 errout:
-- 
2.54.0 (Apple Git-157)
Technical Details
Message-ID: <20261101081736.51803-2-zhiqizhangg@gmail.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-354202-linux-kernel-netdev=dewy.ru
References: <20261101081736.51803-1-zhiqizhangg@gmail.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-354202-linux-kernel-netdev=dewy.ru
From: zhiqizhangg@gmail.com (Zhiqi Zhang)
Newsgroups: linux.kernel.netdev
Subject: [PATCH v2 1/2] lib/ts_fsm: preserve token types when exporting patterns
Date: Sun,  1 Nov 2026 16:17:35 +0800
Organization: Usenet Network
Message-ID: <20261101081736.51803-2-zhiqizhangg@gmail.com>
References: <20261101081736.51803-1-zhiqizhangg@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2651552"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:Aa0Yd4LsVDcYE7eSi+oBlY5c2GQ= sha256:Z1zzIGpfg7GH99eOOOujlCZNsxFHrjOPMxqmyxjE2PU=
	sha1:/0lETv/m1RIpGt5Oz5j0F00vSK0= sha256:Nad9N4GTnw4e+OgB1tAlcGd/GHcdLcYfREAJXFqKJbQ=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 92B421122F7; Sun,  1 Nov 2026 11:19:21 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.048
X-Spam-Status: No, score=-3.048 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.001,
	FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=gmail.com
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id xP4ZAMjWFEBZ;
	Sun,  1 Nov 2026 11:19:19 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-354202-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 1139B1122E9; Sun,  1 Nov 2026 11:19:11 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id B31892B935; Sun,  1 Nov 2026 08:17:49 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1793521069;
	b=h6IVpU6FzoqSbsMQuccM0mePcYlSPpHGmrpbv7twKBJC8P2TetTIfGje9bF//U/22DMc
	 SnCCpqJNE5gQ9zJkNIonrU0e2eUwU0eoYgesUn3Ey+d9nQ6nyW+G1Y2Tbq1+F2ohOwRvS
	 njd+hCnaaML9od3esIWeYggQj39Kw6Epc5QE7uSPB035sREsGg5MBRIciahYMSZ7AiDEr
	 EkhwMoXafr9W2haUWROeFvKE9N5JTRIre35+y+aDJQSofPNhZx67jyUiEo7hEGPgUDByn
	 uOGFiG8ZaBWOG9ZXngmdL1pwgRyDFGGKdjjz/xX7v7SsvaYnXHo2BbXZxQN0f3WL/fg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793521069;
	h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
	 X-Google-DKIM-Signature:X-Forwarded-Encrypted:X-Gm-Message-State:
	 X-Gm-Gg:X-Received:Received:From:To:Cc:Subject:Date:Message-ID:
	 X-Mailer:In-Reply-To:References:X-Mailing-List:List-Id:List-Post:
	 List-Help:List-Subscribe:List-Unsubscribe:List-Owner:Precedence:
	 MIME-Version:Content-Transfer-Encoding;
	bh=PquNKABWi0C6tGTol3B3b6YxtKfoZjVRBFpfFj8hv80=;
	b=jIT4hFZsn4GoulNPOczHpBMZA9TzdvO8X6cNekFIf2ILLGnzdDLoSEUb3oUo0KvHaP6/
	 AgwwUMceYP3pM+5zpxDMfQY35KQcERvrhboaCFegExM8ni4DIhdbFoLURDKTzGduhWwOf
	 KGD1EAhjGNV/WLEsMgrptwmEF1Y2VBy3FKkiDJWZZCS42COy5A/1CpgwFRWclc11Z0tQC
	 ZhlMHjFOO8RCCFvNgm3eJGGCfbDdD3v7mZkwxzDb636KGBoiZvJ43Iex4HGk6oNS3KK1W
	 x+dY2P4DNqbdlpEA5v34LXH+k2IsdRjfCL423HWOXHL3U84wO/1ea7BbHuzbpoQ4DPA==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=guLijxjT;
	dmarc=pass header.from=gmail.com;
	spf=pass smtp.mailfrom=gmail.com;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 8A0BA606A330;
	Sun,  1 Nov 2026 08:17:49 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=guLijxjT
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.44
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1793521068;
	b=JE+VS3cpT9R1T88UiV+9pBpAgPsCz8D5mi03fHVd+jTEUkRdvfb9Eo7J8CRa/V4MgbPW
	 DksQ9W8D4/6H9E/V0D26BIO0Eb8/3OHvWbdFYJcow+nwOuoH7XuLrnLeCtZjCUhy1lpNP
	 WCLz7oMRQz0tyuA6fos3sww32OZvn0IbWxFMvCtj+6VAtPephy/FbIkoLM/1sR4HkRBDM
	 uOLX1nSZawFj/Y1uHGGHBWNh/yLCXy8uqUwpsdPf9MkRaqJcb3fXnvETAH64JRmJwsrbv
	 RMey91XTQTkeV6u/vm1JLaGcEq5Bwu0ceDUFLjd4zgolQdWM7xb2Q2NX7jTfYKynzcg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793521068;
	h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
	 X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received:
	 From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References:
	 MIME-Version:Content-Transfer-Encoding;
	bh=PquNKABWi0C6tGTol3B3b6YxtKfoZjVRBFpfFj8hv80=;
	b=hZdz7FI9dAwLgILIHdmdCu9bmSYOMEqGjTqTqb2EqBNs+8sPnb9GQGhxVx81O6rFcx3w
	 g9oAzSAPo79Cq7qJsjVdRqlh1u2lwpPgSfOEefBWG7lPh4yZ4uanVcP6sYvabCJYPTco6
	 Nmb4Mh7pox+dHHJxiLBtjgHKPTY3DLJoeTtgqGhdfWoxNQFgds0qvH1qImNJnJxdjAGiY
	 oOtTq4N5WQSo5eBIOF4Vzk+eN33TC+hB++H/9gF0wBFVe6EtnV6LN0EG62jecjRY1D3os
	 K7GrYZb5OUIhxz61GTSx+DhlAaG+MZ1nD5vTV/98cGGLrlbctWCFNyoOEfbFuLO4TGw==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=pass header.from=gmail.com;
	spf=pass smtp.mailfrom=gmail.com;
	dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=guLijxjT;
	arc=none smtp.remote-ip=209.85.216.44
X-Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44])
	(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id B1EC7606A329
	for <netdev@vger.kernel.org>; Sun,  1 Nov 2026 08:17:47 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B1EC7606A329
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-3ac9f49b984so1136435a91.1
        for <netdev@vger.kernel.org>; Sun, 01 Nov 2026 01:17:47 -0700 (PDT)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20260707; t=1793521067; x=1794125867; darn=vger.kernel.org;
        h=content-transfer-encoding:mime-version:references:in-reply-to
         :message-id:date:subject:cc:to:from:from:to:cc:subject:date
         :message-id:reply-to:content-type;
        bh=PquNKABWi0C6tGTol3B3b6YxtKfoZjVRBFpfFj8hv80=;
        b=guLijxjTMJ66lfe8n6JMhPGpbFDCjjgWc1gp+wR2hHlNR2BFETzJHyZvtojeB4V0ap
         Qgwr4Nvp8zeubBiwsErHaPxQ1dx7Kc7iHSxjfoPrKEWUZsMzEYzwxmPsEtixyD/TD4du
         bbF6dCwa5HSE96wTUvihfcqYutieQhyyGuWmo3VZLWpubLCrtbKsz0pk19k16Avhp6fN
         GIhlUnytDFwfQZ9K/HjeNscYCWHrrUx9rXrg/28tR/2dZ14Vc0ohkzEocbO5MD5IQvQ2
         9w2oIAkyHdT4HMU1jnxuj+bLqxrxjRCVVkkNINuzWr877xU+8FfrXRogVQuqZYziny95
         /+Sw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20260707; t=1793521067; x=1794125867;
        h=content-transfer-encoding:mime-version:references:in-reply-to
         :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from
         :to:cc:subject:date:message-id:reply-to:content-type;
        bh=PquNKABWi0C6tGTol3B3b6YxtKfoZjVRBFpfFj8hv80=;
        b=BYScKuxZlv7qfrWmRx5E7or0BY2twrWdWbM64jcs2AWCpuTVcNV02O/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N6w21+ZY9BAz5
	0eTOfbkh7fH4UKWlmobTzjCnZUk8KXXchF7tThbOgUSnNZPIC8Qf7L70RxxvbIqLXohpBRRScRW
	AuXxTfWnWV7t8q+yNGGjYhVc1pmpvYEi9F9q62Rr4qm/6NPXZCuAtPTBNTEh/CitotqBd720DgO
	X3lAqsBcWOoM+MsbB1wo/fPvv
X-Received: by 2002:a17:90b:53c7:b0:3aa:412f:b0a7 with SMTP id 98e67ed59e1d1-3ae89559e5emr3615666a91.5.1793521066862;
        Sun, 01 Nov 2026 01:17:46 -0700 (PDT)
X-Received: from LDYT9Q2G2M ([2a09:bac5:cad2:3228::4ff:28])
        by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3ae84596e3bsm10011455a91.9.2026.11.01.01.17.43
        (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256);
        Sun, 01 Nov 2026 01:17:46 -0700 (PDT)
X-Cc: linux-kernel@vger.kernel.org,
	kunit-dev@googlegroups.com,
	Pablo Neira Ayuso <pablo@netfilter.org>,
	Finke Lennart <lfinke@student.ethz.ch>,
	netfilter-devel@vger.kernel.org,
	Jamal Hadi Salim <jhs@mojatatu.com>,
	Jiri Pirko <jiri@resnulli.us>,
	netdev@vger.kernel.org,
	stable@vger.kernel.org
X-Mailer: git-send-email 2.54.0
X-In-Reply-To: <20261101081736.51803-1-zhiqizhangg@gmail.com>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list