1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * lib/ts_bm.c		Boyer-Moore text search implementation
4  *
5  * Authors:	Pablo Neira Ayuso <pablo@eurodev.net>
6  *
7  * ==========================================================================
8  *
9  *   Implements Boyer-Moore string matching algorithm:
10  *
11  *   [1] A Fast String Searching Algorithm, R.S. Boyer and Moore.
12  *       Communications of the Association for Computing Machinery,
13  *       20(10), 1977, pp. 762-772.
14  *       https://www.cs.utexas.edu/users/moore/publications/fstrpos.pdf
15  *
16  *   [2] Handbook of Exact String Matching Algorithms, Thierry Lecroq, 2004
17  *       http://www-igm.univ-mlv.fr/~lecroq/string/string.pdf
18  *
19  *   Note: Since Boyer-Moore (BM) performs searches for matchings from right
20  *   to left, it's still possible that a matching could be spread over
21  *   multiple blocks, in that case this algorithm won't find any coincidence.
22  *
23  *   If you're willing to ensure that such thing won't ever happen, use the
24  *   Knuth-Pratt-Morris (KMP) implementation instead. In conclusion, choose
25  *   the proper string search algorithm depending on your setting.
26  *
27  *   Say you're using the textsearch infrastructure for filtering, NIDS or
28  *   any similar security focused purpose, then go KMP. Otherwise, if you
29  *   really care about performance, say you're classifying packets to apply
30  *   Quality of Service (QoS) policies, and you don't mind about possible
31  *   matchings spread over multiple fragments, then go BM.
32  */
33 
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/types.h>
37 #include <linux/string.h>
38 #include <linux/ctype.h>
39 #include <linux/textsearch.h>
40 
41 /* Alphabet size, use ASCII */
42 #define ASIZE 256
43 
44 #if 0
45 #define DEBUGP printk
46 #else
47 #define DEBUGP(args, format...)
48 #endif
49 
50 struct ts_bm
51 {
52 	u8 *		pattern;
53 	unsigned int	patlen;
54 	unsigned int 	bad_shift[ASIZE];
55 	unsigned int	good_shift[];
56 };
57 
bm_find(struct ts_config * conf,struct ts_state * state)58 static unsigned int bm_find(struct ts_config *conf, struct ts_state *state)
59 {
60 	struct ts_bm *bm = ts_config_priv(conf);
61 	unsigned int i, text_len, consumed = state->offset;
62 	const u8 *text;
63 	int shift = bm->patlen - 1, bs;
64 	const u8 icase = conf->flags & TS_IGNORECASE;
65 
66 	for (;;) {
67 		text_len = conf->get_next_block(consumed, &text, conf, state);
68 
69 		if (unlikely(text_len == 0))
70 			break;
71 
72 		while (shift < text_len) {
73 			DEBUGP("Searching in position %d (%c)\n",
74 				shift, text[shift]);
75 			for (i = 0; i < bm->patlen; i++)
76 				if ((icase ? toupper(text[shift-i])
77 				    : text[shift-i])
78 					!= bm->pattern[bm->patlen-1-i])
79 				     goto next;
80 
81 			/* London calling... */
82 			DEBUGP("found!\n");
83 			return consumed += (shift-(bm->patlen-1));
84 
85 next:			bs = bm->bad_shift[text[shift-i]];
86 
87 			/* Now jumping to... */
88 			shift = max_t(int, shift-i+bs, shift+bm->good_shift[i]);
89 		}
90 		consumed += text_len;
91 	}
92 
93 	return UINT_MAX;
94 }
95 
subpattern(u8 * pattern,int i,int j,int g)96 static int subpattern(u8 *pattern, int i, int j, int g)
97 {
98 	int x = i+g-1, y = j+g-1, ret = 0;
99 
100 	while(pattern[x--] == pattern[y--]) {
101 		if (y < 0) {
102 			ret = 1;
103 			break;
104 		}
105 		if (--g == 0) {
106 			ret = pattern[i-1] != pattern[j-1];
107 			break;
108 		}
109 	}
110 
111 	return ret;
112 }
113 
compute_prefix_tbl(struct ts_bm * bm,int flags)114 static void compute_prefix_tbl(struct ts_bm *bm, int flags)
115 {
116 	int i, j, g;
117 
118 	for (i = 0; i < ASIZE; i++)
119 		bm->bad_shift[i] = bm->patlen;
120 	for (i = 0; i < bm->patlen - 1; i++) {
121 		bm->bad_shift[bm->pattern[i]] = bm->patlen - 1 - i;
122 		if (flags & TS_IGNORECASE)
123 			bm->bad_shift[tolower(bm->pattern[i])]
124 			    = bm->patlen - 1 - i;
125 	}
126 
127 	/* Compute the good shift array, used to match reocurrences
128 	 * of a subpattern */
129 	bm->good_shift[0] = 1;
130 	for (i = 1; i < bm->patlen; i++)
131 		bm->good_shift[i] = bm->patlen;
132         for (i = bm->patlen-1, g = 1; i > 0; g++, i--) {
133 		for (j = i-1; j >= 1-g ; j--)
134 			if (subpattern(bm->pattern, i, j, g)) {
135 				bm->good_shift[g] = bm->patlen-j-g;
136 				break;
137 			}
138 	}
139 }
140 
bm_init(const void * pattern,unsigned int len,gfp_t gfp_mask,int flags)141 static struct ts_config *bm_init(const void *pattern, unsigned int len,
142 				 gfp_t gfp_mask, int flags)
143 {
144 	struct ts_config *conf;
145 	struct ts_bm *bm;
146 	int i;
147 	unsigned int prefix_tbl_len = len * sizeof(unsigned int);
148 	size_t priv_size = sizeof(*bm) + len + prefix_tbl_len;
149 
150 	conf = alloc_ts_config(priv_size, gfp_mask);
151 	if (IS_ERR(conf))
152 		return conf;
153 
154 	conf->flags = flags;
155 	bm = ts_config_priv(conf);
156 	bm->patlen = len;
157 	bm->pattern = (u8 *) bm->good_shift + prefix_tbl_len;
158 	if (flags & TS_IGNORECASE)
159 		for (i = 0; i < len; i++)
160 			bm->pattern[i] = toupper(((u8 *)pattern)[i]);
161 	else
162 		memcpy(bm->pattern, pattern, len);
163 	compute_prefix_tbl(bm, flags);
164 
165 	return conf;
166 }
167 
bm_get_pattern(struct ts_config * conf)168 static void *bm_get_pattern(struct ts_config *conf)
169 {
170 	struct ts_bm *bm = ts_config_priv(conf);
171 	return bm->pattern;
172 }
173 
bm_get_pattern_len(struct ts_config * conf)174 static unsigned int bm_get_pattern_len(struct ts_config *conf)
175 {
176 	struct ts_bm *bm = ts_config_priv(conf);
177 	return bm->patlen;
178 }
179 
180 static struct ts_ops bm_ops = {
181 	.name		  = "bm",
182 	.find		  = bm_find,
183 	.init		  = bm_init,
184 	.get_pattern	  = bm_get_pattern,
185 	.get_pattern_len  = bm_get_pattern_len,
186 	.owner		  = THIS_MODULE,
187 	.list		  = LIST_HEAD_INIT(bm_ops.list)
188 };
189 
init_bm(void)190 static int __init init_bm(void)
191 {
192 	return textsearch_register(&bm_ops);
193 }
194 
exit_bm(void)195 static void __exit exit_bm(void)
196 {
197 	textsearch_unregister(&bm_ops);
198 }
199 
200 MODULE_LICENSE("GPL");
201 
202 module_init(init_bm);
203 module_exit(exit_bm);
204