xref: /openbmc/linux/security/apparmor/match.c (revision d901d6a298dc6e9105b9dc091d65b043e9f8c9a6)
1e06f75a6SJohn Johansen /*
2e06f75a6SJohn Johansen  * AppArmor security module
3e06f75a6SJohn Johansen  *
4e06f75a6SJohn Johansen  * This file contains AppArmor dfa based regular expression matching engine
5e06f75a6SJohn Johansen  *
6e06f75a6SJohn Johansen  * Copyright (C) 1998-2008 Novell/SUSE
78e4ff109SJohn Johansen  * Copyright 2009-2012 Canonical Ltd.
8e06f75a6SJohn Johansen  *
9e06f75a6SJohn Johansen  * This program is free software; you can redistribute it and/or
10e06f75a6SJohn Johansen  * modify it under the terms of the GNU General Public License as
11e06f75a6SJohn Johansen  * published by the Free Software Foundation, version 2 of the
12e06f75a6SJohn Johansen  * License.
13e06f75a6SJohn Johansen  */
14e06f75a6SJohn Johansen 
15e06f75a6SJohn Johansen #include <linux/errno.h>
16e06f75a6SJohn Johansen #include <linux/kernel.h>
17e06f75a6SJohn Johansen #include <linux/mm.h>
18e06f75a6SJohn Johansen #include <linux/slab.h>
19e06f75a6SJohn Johansen #include <linux/vmalloc.h>
20e06f75a6SJohn Johansen #include <linux/err.h>
21e06f75a6SJohn Johansen #include <linux/kref.h>
22e06f75a6SJohn Johansen 
2312557dcbSJohn Johansen #include "include/lib.h"
24e06f75a6SJohn Johansen #include "include/match.h"
25e06f75a6SJohn Johansen 
26ed686308SJohn Johansen #define base_idx(X) ((X) & 0xffffff)
27ed686308SJohn Johansen 
2811c236b8SJohn Johansen static char nulldfa_src[] = {
2911c236b8SJohn Johansen 	#include "nulldfa.in"
3011c236b8SJohn Johansen };
3111c236b8SJohn Johansen struct aa_dfa *nulldfa;
3211c236b8SJohn Johansen 
336e0654d2SJohn Johansen static char stacksplitdfa_src[] = {
346e0654d2SJohn Johansen 	#include "stacksplitdfa.in"
356e0654d2SJohn Johansen };
366e0654d2SJohn Johansen struct aa_dfa *stacksplitdfa;
376e0654d2SJohn Johansen 
3811c236b8SJohn Johansen int aa_setup_dfa_engine(void)
3911c236b8SJohn Johansen {
4011c236b8SJohn Johansen 	int error;
4111c236b8SJohn Johansen 
4211c236b8SJohn Johansen 	nulldfa = aa_dfa_unpack(nulldfa_src, sizeof(nulldfa_src),
4311c236b8SJohn Johansen 				TO_ACCEPT1_FLAG(YYTD_DATA32) |
4411c236b8SJohn Johansen 				TO_ACCEPT2_FLAG(YYTD_DATA32));
456e0654d2SJohn Johansen 	if (IS_ERR(nulldfa)) {
4611c236b8SJohn Johansen 		error = PTR_ERR(nulldfa);
4711c236b8SJohn Johansen 		nulldfa = NULL;
4811c236b8SJohn Johansen 		return error;
4911c236b8SJohn Johansen 	}
5011c236b8SJohn Johansen 
516e0654d2SJohn Johansen 	stacksplitdfa = aa_dfa_unpack(stacksplitdfa_src,
526e0654d2SJohn Johansen 				      sizeof(stacksplitdfa_src),
536e0654d2SJohn Johansen 				      TO_ACCEPT1_FLAG(YYTD_DATA32) |
546e0654d2SJohn Johansen 				      TO_ACCEPT2_FLAG(YYTD_DATA32));
556e0654d2SJohn Johansen 	if (IS_ERR(stacksplitdfa)) {
566e0654d2SJohn Johansen 		aa_put_dfa(nulldfa);
576e0654d2SJohn Johansen 		nulldfa = NULL;
586e0654d2SJohn Johansen 		error = PTR_ERR(stacksplitdfa);
596e0654d2SJohn Johansen 		stacksplitdfa = NULL;
606e0654d2SJohn Johansen 		return error;
616e0654d2SJohn Johansen 	}
626e0654d2SJohn Johansen 
636e0654d2SJohn Johansen 	return 0;
646e0654d2SJohn Johansen }
656e0654d2SJohn Johansen 
6611c236b8SJohn Johansen void aa_teardown_dfa_engine(void)
6711c236b8SJohn Johansen {
686e0654d2SJohn Johansen 	aa_put_dfa(stacksplitdfa);
6911c236b8SJohn Johansen 	aa_put_dfa(nulldfa);
7011c236b8SJohn Johansen }
7111c236b8SJohn Johansen 
72e06f75a6SJohn Johansen /**
73e06f75a6SJohn Johansen  * unpack_table - unpack a dfa table (one of accept, default, base, next check)
74e06f75a6SJohn Johansen  * @blob: data to unpack (NOT NULL)
75e06f75a6SJohn Johansen  * @bsize: size of blob
76e06f75a6SJohn Johansen  *
77e06f75a6SJohn Johansen  * Returns: pointer to table else NULL on failure
78e06f75a6SJohn Johansen  *
790ca554b9SJohn Johansen  * NOTE: must be freed by kvfree (not kfree)
80e06f75a6SJohn Johansen  */
81e06f75a6SJohn Johansen static struct table_header *unpack_table(char *blob, size_t bsize)
82e06f75a6SJohn Johansen {
83e06f75a6SJohn Johansen 	struct table_header *table = NULL;
84e06f75a6SJohn Johansen 	struct table_header th;
85e06f75a6SJohn Johansen 	size_t tsize;
86e06f75a6SJohn Johansen 
87e06f75a6SJohn Johansen 	if (bsize < sizeof(struct table_header))
88e06f75a6SJohn Johansen 		goto out;
89e06f75a6SJohn Johansen 
90e06f75a6SJohn Johansen 	/* loaded td_id's start at 1, subtract 1 now to avoid doing
91e06f75a6SJohn Johansen 	 * it every time we use td_id as an index
92e06f75a6SJohn Johansen 	 */
93e6e8bf41SJohn Johansen 	th.td_id = be16_to_cpu(*(__be16 *) (blob)) - 1;
9415756178SJohn Johansen 	if (th.td_id > YYTD_ID_MAX)
9515756178SJohn Johansen 		goto out;
96e6e8bf41SJohn Johansen 	th.td_flags = be16_to_cpu(*(__be16 *) (blob + 2));
97e6e8bf41SJohn Johansen 	th.td_lolen = be32_to_cpu(*(__be32 *) (blob + 8));
98e06f75a6SJohn Johansen 	blob += sizeof(struct table_header);
99e06f75a6SJohn Johansen 
100e06f75a6SJohn Johansen 	if (!(th.td_flags == YYTD_DATA16 || th.td_flags == YYTD_DATA32 ||
101e06f75a6SJohn Johansen 	      th.td_flags == YYTD_DATA8))
102e06f75a6SJohn Johansen 		goto out;
103e06f75a6SJohn Johansen 
104e06f75a6SJohn Johansen 	tsize = table_size(th.td_lolen, th.td_flags);
105e06f75a6SJohn Johansen 	if (bsize < tsize)
106e06f75a6SJohn Johansen 		goto out;
107e06f75a6SJohn Johansen 
108a7c3e901SMichal Hocko 	table = kvzalloc(tsize, GFP_KERNEL);
109e06f75a6SJohn Johansen 	if (table) {
110f4ee2defSHeinrich Schuchardt 		table->td_id = th.td_id;
111f4ee2defSHeinrich Schuchardt 		table->td_flags = th.td_flags;
112f4ee2defSHeinrich Schuchardt 		table->td_lolen = th.td_lolen;
113e06f75a6SJohn Johansen 		if (th.td_flags == YYTD_DATA8)
114e06f75a6SJohn Johansen 			UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
115e6e8bf41SJohn Johansen 				     u8, u8, byte_to_byte);
116e06f75a6SJohn Johansen 		else if (th.td_flags == YYTD_DATA16)
117e06f75a6SJohn Johansen 			UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
118e6e8bf41SJohn Johansen 				     u16, __be16, be16_to_cpu);
119e06f75a6SJohn Johansen 		else if (th.td_flags == YYTD_DATA32)
120e06f75a6SJohn Johansen 			UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
121e6e8bf41SJohn Johansen 				     u32, __be32, be32_to_cpu);
122e06f75a6SJohn Johansen 		else
123e06f75a6SJohn Johansen 			goto fail;
124e06f75a6SJohn Johansen 		/* if table was vmalloced make sure the page tables are synced
125e06f75a6SJohn Johansen 		 * before it is used, as it goes live to all cpus.
126e06f75a6SJohn Johansen 		 */
127e06f75a6SJohn Johansen 		if (is_vmalloc_addr(table))
128e06f75a6SJohn Johansen 			vm_unmap_aliases();
1293197f5adSJohn Johansen 	}
1303197f5adSJohn Johansen 
1313197f5adSJohn Johansen out:
132e06f75a6SJohn Johansen 	return table;
133e06f75a6SJohn Johansen fail:
134e06f75a6SJohn Johansen 	kvfree(table);
135e06f75a6SJohn Johansen 	return NULL;
136e06f75a6SJohn Johansen }
137e06f75a6SJohn Johansen 
138e06f75a6SJohn Johansen /**
139*d901d6a2SJohn Johansen  * verify_table_headers - verify that the tables headers are as expected
140*d901d6a2SJohn Johansen  * @tables - array of dfa tables to check (NOT NULL)
141e06f75a6SJohn Johansen  * @flags: flags controlling what type of accept table are acceptable
142e06f75a6SJohn Johansen  *
143e06f75a6SJohn Johansen  * Assumes dfa has gone through the first pass verification done by unpacking
144e06f75a6SJohn Johansen  * NOTE: this does not valid accept table values
145e06f75a6SJohn Johansen  *
146e06f75a6SJohn Johansen  * Returns: %0 else error code on failure to verify
147e06f75a6SJohn Johansen  */
148*d901d6a2SJohn Johansen static int verify_table_headers(struct table_header **tables, int flags)
149e06f75a6SJohn Johansen {
150*d901d6a2SJohn Johansen 	size_t state_count, trans_count;
151e06f75a6SJohn Johansen 	int error = -EPROTO;
152e06f75a6SJohn Johansen 
153e06f75a6SJohn Johansen 	/* check that required tables exist */
154*d901d6a2SJohn Johansen 	if (!(tables[YYTD_ID_DEF] && tables[YYTD_ID_BASE] &&
155*d901d6a2SJohn Johansen 	      tables[YYTD_ID_NXT] && tables[YYTD_ID_CHK]))
156e06f75a6SJohn Johansen 		goto out;
157e06f75a6SJohn Johansen 
158e06f75a6SJohn Johansen 	/* accept.size == default.size == base.size */
159*d901d6a2SJohn Johansen 	state_count = tables[YYTD_ID_BASE]->td_lolen;
160e06f75a6SJohn Johansen 	if (ACCEPT1_FLAGS(flags)) {
161*d901d6a2SJohn Johansen 		if (!tables[YYTD_ID_ACCEPT])
162e06f75a6SJohn Johansen 			goto out;
163*d901d6a2SJohn Johansen 		if (state_count != tables[YYTD_ID_ACCEPT]->td_lolen)
164e06f75a6SJohn Johansen 			goto out;
165e06f75a6SJohn Johansen 	}
166e06f75a6SJohn Johansen 	if (ACCEPT2_FLAGS(flags)) {
167*d901d6a2SJohn Johansen 		if (!tables[YYTD_ID_ACCEPT2])
168e06f75a6SJohn Johansen 			goto out;
169*d901d6a2SJohn Johansen 		if (state_count != tables[YYTD_ID_ACCEPT2]->td_lolen)
170e06f75a6SJohn Johansen 			goto out;
171e06f75a6SJohn Johansen 	}
172*d901d6a2SJohn Johansen 	if (state_count != tables[YYTD_ID_DEF]->td_lolen)
173e06f75a6SJohn Johansen 		goto out;
174e06f75a6SJohn Johansen 
175e06f75a6SJohn Johansen 	/* next.size == chk.size */
176*d901d6a2SJohn Johansen 	trans_count = tables[YYTD_ID_NXT]->td_lolen;
177*d901d6a2SJohn Johansen 	if (trans_count != tables[YYTD_ID_CHK]->td_lolen)
178e06f75a6SJohn Johansen 		goto out;
179e06f75a6SJohn Johansen 
180e06f75a6SJohn Johansen 	/* if equivalence classes then its table size must be 256 */
181*d901d6a2SJohn Johansen 	if (tables[YYTD_ID_EC] && tables[YYTD_ID_EC]->td_lolen != 256)
182e06f75a6SJohn Johansen 		goto out;
183e06f75a6SJohn Johansen 
184*d901d6a2SJohn Johansen 	error = 0;
185*d901d6a2SJohn Johansen out:
186*d901d6a2SJohn Johansen 	return error;
187*d901d6a2SJohn Johansen }
188*d901d6a2SJohn Johansen 
189*d901d6a2SJohn Johansen /**
190*d901d6a2SJohn Johansen  * verify_dfa - verify that transitions and states in the tables are in bounds.
191*d901d6a2SJohn Johansen  * @dfa: dfa to test  (NOT NULL)
192*d901d6a2SJohn Johansen  *
193*d901d6a2SJohn Johansen  * Assumes dfa has gone through the first pass verification done by unpacking
194*d901d6a2SJohn Johansen  * NOTE: this does not valid accept table values
195*d901d6a2SJohn Johansen  *
196*d901d6a2SJohn Johansen  * Returns: %0 else error code on failure to verify
197*d901d6a2SJohn Johansen  */
198*d901d6a2SJohn Johansen static int verify_dfa(struct aa_dfa *dfa)
199*d901d6a2SJohn Johansen {
200*d901d6a2SJohn Johansen 	size_t i, state_count, trans_count;
201*d901d6a2SJohn Johansen 	int error = EPROTO;
202*d901d6a2SJohn Johansen 
203*d901d6a2SJohn Johansen 	state_count = dfa->tables[YYTD_ID_BASE]->td_lolen;
204*d901d6a2SJohn Johansen 	trans_count = dfa->tables[YYTD_ID_NXT]->td_lolen;
205e06f75a6SJohn Johansen 	for (i = 0; i < state_count; i++) {
206031dcc8fSJohn Johansen 		if (!(BASE_TABLE(dfa)[i] & MATCH_FLAG_DIFF_ENCODE) &&
207031dcc8fSJohn Johansen 		    (DEFAULT_TABLE(dfa)[i] >= state_count))
208e06f75a6SJohn Johansen 			goto out;
209ed686308SJohn Johansen 		if (base_idx(BASE_TABLE(dfa)[i]) + 255 >= trans_count) {
210*d901d6a2SJohn Johansen 			pr_err("AppArmor DFA next/check upper bounds error\n");
211e06f75a6SJohn Johansen 			goto out;
212e06f75a6SJohn Johansen 		}
213e06f75a6SJohn Johansen 	}
214e06f75a6SJohn Johansen 
215e06f75a6SJohn Johansen 	for (i = 0; i < trans_count; i++) {
216e06f75a6SJohn Johansen 		if (NEXT_TABLE(dfa)[i] >= state_count)
217e06f75a6SJohn Johansen 			goto out;
218e06f75a6SJohn Johansen 		if (CHECK_TABLE(dfa)[i] >= state_count)
219e06f75a6SJohn Johansen 			goto out;
220e06f75a6SJohn Johansen 	}
221e06f75a6SJohn Johansen 
222031dcc8fSJohn Johansen 	/* Now that all the other tables are verified, verify diffencoding */
223*d901d6a2SJohn Johansen 	for (i = 0; i < state_count; i++) {
224031dcc8fSJohn Johansen 		size_t j, k;
225031dcc8fSJohn Johansen 
226031dcc8fSJohn Johansen 		for (j = i;
227031dcc8fSJohn Johansen 		     (BASE_TABLE(dfa)[j] & MATCH_FLAG_DIFF_ENCODE) &&
228031dcc8fSJohn Johansen 		     !(BASE_TABLE(dfa)[j] & MARK_DIFF_ENCODE);
229031dcc8fSJohn Johansen 		     j = k) {
230031dcc8fSJohn Johansen 			k = DEFAULT_TABLE(dfa)[j];
231031dcc8fSJohn Johansen 			if (j == k)
232031dcc8fSJohn Johansen 				goto out;
233031dcc8fSJohn Johansen 			if (k < j)
234031dcc8fSJohn Johansen 				break;		/* already verified */
235031dcc8fSJohn Johansen 			BASE_TABLE(dfa)[j] |= MARK_DIFF_ENCODE;
236031dcc8fSJohn Johansen 		}
237031dcc8fSJohn Johansen 	}
238e06f75a6SJohn Johansen 	error = 0;
239*d901d6a2SJohn Johansen 
240e06f75a6SJohn Johansen out:
241e06f75a6SJohn Johansen 	return error;
242e06f75a6SJohn Johansen }
243e06f75a6SJohn Johansen 
244e06f75a6SJohn Johansen /**
245e06f75a6SJohn Johansen  * dfa_free - free a dfa allocated by aa_dfa_unpack
246e06f75a6SJohn Johansen  * @dfa: the dfa to free  (MAYBE NULL)
247e06f75a6SJohn Johansen  *
248e06f75a6SJohn Johansen  * Requires: reference count to dfa == 0
249e06f75a6SJohn Johansen  */
250e06f75a6SJohn Johansen static void dfa_free(struct aa_dfa *dfa)
251e06f75a6SJohn Johansen {
252e06f75a6SJohn Johansen 	if (dfa) {
253e06f75a6SJohn Johansen 		int i;
254e06f75a6SJohn Johansen 
255e06f75a6SJohn Johansen 		for (i = 0; i < ARRAY_SIZE(dfa->tables); i++) {
256e06f75a6SJohn Johansen 			kvfree(dfa->tables[i]);
257e06f75a6SJohn Johansen 			dfa->tables[i] = NULL;
258e06f75a6SJohn Johansen 		}
259e06f75a6SJohn Johansen 		kfree(dfa);
260e06f75a6SJohn Johansen 	}
261e06f75a6SJohn Johansen }
262e06f75a6SJohn Johansen 
263e06f75a6SJohn Johansen /**
264e06f75a6SJohn Johansen  * aa_dfa_free_kref - free aa_dfa by kref (called by aa_put_dfa)
265e06f75a6SJohn Johansen  * @kr: kref callback for freeing of a dfa  (NOT NULL)
266e06f75a6SJohn Johansen  */
267e06f75a6SJohn Johansen void aa_dfa_free_kref(struct kref *kref)
268e06f75a6SJohn Johansen {
269e06f75a6SJohn Johansen 	struct aa_dfa *dfa = container_of(kref, struct aa_dfa, count);
270e06f75a6SJohn Johansen 	dfa_free(dfa);
271e06f75a6SJohn Johansen }
272e06f75a6SJohn Johansen 
273e06f75a6SJohn Johansen /**
274e06f75a6SJohn Johansen  * aa_dfa_unpack - unpack the binary tables of a serialized dfa
275e06f75a6SJohn Johansen  * @blob: aligned serialized stream of data to unpack  (NOT NULL)
276e06f75a6SJohn Johansen  * @size: size of data to unpack
277e06f75a6SJohn Johansen  * @flags: flags controlling what type of accept tables are acceptable
278e06f75a6SJohn Johansen  *
279e06f75a6SJohn Johansen  * Unpack a dfa that has been serialized.  To find information on the dfa
28026fccd9eSKees Cook  * format look in Documentation/admin-guide/LSM/apparmor.rst
28125985edcSLucas De Marchi  * Assumes the dfa @blob stream has been aligned on a 8 byte boundary
282e06f75a6SJohn Johansen  *
283e06f75a6SJohn Johansen  * Returns: an unpacked dfa ready for matching or ERR_PTR on failure
284e06f75a6SJohn Johansen  */
285e06f75a6SJohn Johansen struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags)
286e06f75a6SJohn Johansen {
287e06f75a6SJohn Johansen 	int hsize;
288e06f75a6SJohn Johansen 	int error = -ENOMEM;
289e06f75a6SJohn Johansen 	char *data = blob;
290e06f75a6SJohn Johansen 	struct table_header *table = NULL;
291e06f75a6SJohn Johansen 	struct aa_dfa *dfa = kzalloc(sizeof(struct aa_dfa), GFP_KERNEL);
292e06f75a6SJohn Johansen 	if (!dfa)
293e06f75a6SJohn Johansen 		goto fail;
294e06f75a6SJohn Johansen 
295e06f75a6SJohn Johansen 	kref_init(&dfa->count);
296e06f75a6SJohn Johansen 
297e06f75a6SJohn Johansen 	error = -EPROTO;
298e06f75a6SJohn Johansen 
299e06f75a6SJohn Johansen 	/* get dfa table set header */
300e06f75a6SJohn Johansen 	if (size < sizeof(struct table_set_header))
301e06f75a6SJohn Johansen 		goto fail;
302e06f75a6SJohn Johansen 
303e6e8bf41SJohn Johansen 	if (ntohl(*(__be32 *) data) != YYTH_MAGIC)
304e06f75a6SJohn Johansen 		goto fail;
305e06f75a6SJohn Johansen 
306e6e8bf41SJohn Johansen 	hsize = ntohl(*(__be32 *) (data + 4));
307e06f75a6SJohn Johansen 	if (size < hsize)
308e06f75a6SJohn Johansen 		goto fail;
309e06f75a6SJohn Johansen 
310e6e8bf41SJohn Johansen 	dfa->flags = ntohs(*(__be16 *) (data + 12));
311031dcc8fSJohn Johansen 	if (dfa->flags != 0 && dfa->flags != YYTH_FLAG_DIFF_ENCODE)
312031dcc8fSJohn Johansen 		goto fail;
313031dcc8fSJohn Johansen 
314e06f75a6SJohn Johansen 	data += hsize;
315e06f75a6SJohn Johansen 	size -= hsize;
316e06f75a6SJohn Johansen 
317e06f75a6SJohn Johansen 	while (size > 0) {
318e06f75a6SJohn Johansen 		table = unpack_table(data, size);
319e06f75a6SJohn Johansen 		if (!table)
320e06f75a6SJohn Johansen 			goto fail;
321e06f75a6SJohn Johansen 
322e06f75a6SJohn Johansen 		switch (table->td_id) {
323e06f75a6SJohn Johansen 		case YYTD_ID_ACCEPT:
324e06f75a6SJohn Johansen 			if (!(table->td_flags & ACCEPT1_FLAGS(flags)))
325e06f75a6SJohn Johansen 				goto fail;
326e06f75a6SJohn Johansen 			break;
327e06f75a6SJohn Johansen 		case YYTD_ID_ACCEPT2:
328e06f75a6SJohn Johansen 			if (!(table->td_flags & ACCEPT2_FLAGS(flags)))
329e06f75a6SJohn Johansen 				goto fail;
330e06f75a6SJohn Johansen 			break;
331e06f75a6SJohn Johansen 		case YYTD_ID_BASE:
332e06f75a6SJohn Johansen 			if (table->td_flags != YYTD_DATA32)
333e06f75a6SJohn Johansen 				goto fail;
334e06f75a6SJohn Johansen 			break;
335e06f75a6SJohn Johansen 		case YYTD_ID_DEF:
336e06f75a6SJohn Johansen 		case YYTD_ID_NXT:
337e06f75a6SJohn Johansen 		case YYTD_ID_CHK:
338e06f75a6SJohn Johansen 			if (table->td_flags != YYTD_DATA16)
339e06f75a6SJohn Johansen 				goto fail;
340e06f75a6SJohn Johansen 			break;
341e06f75a6SJohn Johansen 		case YYTD_ID_EC:
342e06f75a6SJohn Johansen 			if (table->td_flags != YYTD_DATA8)
343e06f75a6SJohn Johansen 				goto fail;
344e06f75a6SJohn Johansen 			break;
345e06f75a6SJohn Johansen 		default:
346e06f75a6SJohn Johansen 			goto fail;
347e06f75a6SJohn Johansen 		}
348e06f75a6SJohn Johansen 		/* check for duplicate table entry */
349e06f75a6SJohn Johansen 		if (dfa->tables[table->td_id])
350e06f75a6SJohn Johansen 			goto fail;
351e06f75a6SJohn Johansen 		dfa->tables[table->td_id] = table;
352e06f75a6SJohn Johansen 		data += table_size(table->td_lolen, table->td_flags);
353e06f75a6SJohn Johansen 		size -= table_size(table->td_lolen, table->td_flags);
354e06f75a6SJohn Johansen 		table = NULL;
355e06f75a6SJohn Johansen 	}
356*d901d6a2SJohn Johansen 	error = verify_table_headers(dfa->tables, flags);
357e06f75a6SJohn Johansen 	if (error)
358e06f75a6SJohn Johansen 		goto fail;
359e06f75a6SJohn Johansen 
360*d901d6a2SJohn Johansen 	if (flags & DFA_FLAG_VERIFY_STATES) {
361*d901d6a2SJohn Johansen 		error = verify_dfa(dfa);
362*d901d6a2SJohn Johansen 		if (error)
363*d901d6a2SJohn Johansen 			goto fail;
364*d901d6a2SJohn Johansen 	}
365*d901d6a2SJohn Johansen 
366e06f75a6SJohn Johansen 	return dfa;
367e06f75a6SJohn Johansen 
368e06f75a6SJohn Johansen fail:
369e06f75a6SJohn Johansen 	kvfree(table);
370e06f75a6SJohn Johansen 	dfa_free(dfa);
371e06f75a6SJohn Johansen 	return ERR_PTR(error);
372e06f75a6SJohn Johansen }
373e06f75a6SJohn Johansen 
374074c1cd7SJohn Johansen #define match_char(state, def, base, next, check, C)	\
375074c1cd7SJohn Johansen do {							\
376074c1cd7SJohn Johansen 	u32 b = (base)[(state)];			\
377074c1cd7SJohn Johansen 	unsigned int pos = base_idx(b) + (C);		\
378074c1cd7SJohn Johansen 	if ((check)[pos] != (state)) {			\
379074c1cd7SJohn Johansen 		(state) = (def)[(state)];		\
380031dcc8fSJohn Johansen 		if (b & MATCH_FLAG_DIFF_ENCODE)		\
381031dcc8fSJohn Johansen 			continue;			\
382074c1cd7SJohn Johansen 		break;					\
383074c1cd7SJohn Johansen 	}						\
384074c1cd7SJohn Johansen 	(state) = (next)[pos];				\
385074c1cd7SJohn Johansen 	break;						\
386074c1cd7SJohn Johansen } while (1)
387074c1cd7SJohn Johansen 
388e06f75a6SJohn Johansen /**
389e06f75a6SJohn Johansen  * aa_dfa_match_len - traverse @dfa to find state @str stops at
390e06f75a6SJohn Johansen  * @dfa: the dfa to match @str against  (NOT NULL)
391e06f75a6SJohn Johansen  * @start: the state of the dfa to start matching in
392e06f75a6SJohn Johansen  * @str: the string of bytes to match against the dfa  (NOT NULL)
393e06f75a6SJohn Johansen  * @len: length of the string of bytes to match
394e06f75a6SJohn Johansen  *
395e06f75a6SJohn Johansen  * aa_dfa_match_len will match @str against the dfa and return the state it
396e06f75a6SJohn Johansen  * finished matching in. The final state can be used to look up the accepting
397e06f75a6SJohn Johansen  * label, or as the start state of a continuing match.
398e06f75a6SJohn Johansen  *
399e06f75a6SJohn Johansen  * This function will happily match again the 0 byte and only finishes
400e06f75a6SJohn Johansen  * when @len input is consumed.
401e06f75a6SJohn Johansen  *
402e06f75a6SJohn Johansen  * Returns: final state reached after input is consumed
403e06f75a6SJohn Johansen  */
404e06f75a6SJohn Johansen unsigned int aa_dfa_match_len(struct aa_dfa *dfa, unsigned int start,
405e06f75a6SJohn Johansen 			      const char *str, int len)
406e06f75a6SJohn Johansen {
407e06f75a6SJohn Johansen 	u16 *def = DEFAULT_TABLE(dfa);
408e06f75a6SJohn Johansen 	u32 *base = BASE_TABLE(dfa);
409e06f75a6SJohn Johansen 	u16 *next = NEXT_TABLE(dfa);
410e06f75a6SJohn Johansen 	u16 *check = CHECK_TABLE(dfa);
411074c1cd7SJohn Johansen 	unsigned int state = start;
412e06f75a6SJohn Johansen 
413e06f75a6SJohn Johansen 	if (state == 0)
414e06f75a6SJohn Johansen 		return 0;
415e06f75a6SJohn Johansen 
416e06f75a6SJohn Johansen 	/* current state is <state>, matching character *str */
417e06f75a6SJohn Johansen 	if (dfa->tables[YYTD_ID_EC]) {
418e06f75a6SJohn Johansen 		/* Equivalence class table defined */
419e06f75a6SJohn Johansen 		u8 *equiv = EQUIV_TABLE(dfa);
420074c1cd7SJohn Johansen 		for (; len; len--)
421074c1cd7SJohn Johansen 			match_char(state, def, base, next, check,
422074c1cd7SJohn Johansen 				   equiv[(u8) *str++]);
423e06f75a6SJohn Johansen 	} else {
424e06f75a6SJohn Johansen 		/* default is direct to next state */
425074c1cd7SJohn Johansen 		for (; len; len--)
426074c1cd7SJohn Johansen 			match_char(state, def, base, next, check, (u8) *str++);
427e06f75a6SJohn Johansen 	}
428e06f75a6SJohn Johansen 
429e06f75a6SJohn Johansen 	return state;
430e06f75a6SJohn Johansen }
431e06f75a6SJohn Johansen 
432e06f75a6SJohn Johansen /**
4330fe1212dSJohn Johansen  * aa_dfa_match - traverse @dfa to find state @str stops at
434e06f75a6SJohn Johansen  * @dfa: the dfa to match @str against  (NOT NULL)
435e06f75a6SJohn Johansen  * @start: the state of the dfa to start matching in
436e06f75a6SJohn Johansen  * @str: the null terminated string of bytes to match against the dfa (NOT NULL)
437e06f75a6SJohn Johansen  *
4380fe1212dSJohn Johansen  * aa_dfa_match will match @str against the dfa and return the state it
439e06f75a6SJohn Johansen  * finished matching in. The final state can be used to look up the accepting
440e06f75a6SJohn Johansen  * label, or as the start state of a continuing match.
441e06f75a6SJohn Johansen  *
442e06f75a6SJohn Johansen  * Returns: final state reached after input is consumed
443e06f75a6SJohn Johansen  */
444e06f75a6SJohn Johansen unsigned int aa_dfa_match(struct aa_dfa *dfa, unsigned int start,
445e06f75a6SJohn Johansen 			  const char *str)
446e06f75a6SJohn Johansen {
4470fe1212dSJohn Johansen 	u16 *def = DEFAULT_TABLE(dfa);
4480fe1212dSJohn Johansen 	u32 *base = BASE_TABLE(dfa);
4490fe1212dSJohn Johansen 	u16 *next = NEXT_TABLE(dfa);
4500fe1212dSJohn Johansen 	u16 *check = CHECK_TABLE(dfa);
451074c1cd7SJohn Johansen 	unsigned int state = start;
4520fe1212dSJohn Johansen 
4530fe1212dSJohn Johansen 	if (state == 0)
4540fe1212dSJohn Johansen 		return 0;
4550fe1212dSJohn Johansen 
4560fe1212dSJohn Johansen 	/* current state is <state>, matching character *str */
4570fe1212dSJohn Johansen 	if (dfa->tables[YYTD_ID_EC]) {
4580fe1212dSJohn Johansen 		/* Equivalence class table defined */
4590fe1212dSJohn Johansen 		u8 *equiv = EQUIV_TABLE(dfa);
4600fe1212dSJohn Johansen 		/* default is direct to next state */
461074c1cd7SJohn Johansen 		while (*str)
462074c1cd7SJohn Johansen 			match_char(state, def, base, next, check,
463074c1cd7SJohn Johansen 				   equiv[(u8) *str++]);
4640fe1212dSJohn Johansen 	} else {
4650fe1212dSJohn Johansen 		/* default is direct to next state */
466074c1cd7SJohn Johansen 		while (*str)
467074c1cd7SJohn Johansen 			match_char(state, def, base, next, check, (u8) *str++);
4680fe1212dSJohn Johansen 	}
4690fe1212dSJohn Johansen 
4700fe1212dSJohn Johansen 	return state;
4710fe1212dSJohn Johansen }
4720fe1212dSJohn Johansen 
4730fe1212dSJohn Johansen /**
4740fe1212dSJohn Johansen  * aa_dfa_next - step one character to the next state in the dfa
4750fe1212dSJohn Johansen  * @dfa: the dfa to tranverse (NOT NULL)
4760fe1212dSJohn Johansen  * @state: the state to start in
4770fe1212dSJohn Johansen  * @c: the input character to transition on
4780fe1212dSJohn Johansen  *
4790fe1212dSJohn Johansen  * aa_dfa_match will step through the dfa by one input character @c
4800fe1212dSJohn Johansen  *
4810fe1212dSJohn Johansen  * Returns: state reach after input @c
4820fe1212dSJohn Johansen  */
4830fe1212dSJohn Johansen unsigned int aa_dfa_next(struct aa_dfa *dfa, unsigned int state,
4840fe1212dSJohn Johansen 			  const char c)
4850fe1212dSJohn Johansen {
4860fe1212dSJohn Johansen 	u16 *def = DEFAULT_TABLE(dfa);
4870fe1212dSJohn Johansen 	u32 *base = BASE_TABLE(dfa);
4880fe1212dSJohn Johansen 	u16 *next = NEXT_TABLE(dfa);
4890fe1212dSJohn Johansen 	u16 *check = CHECK_TABLE(dfa);
4900fe1212dSJohn Johansen 
4910fe1212dSJohn Johansen 	/* current state is <state>, matching character *str */
4920fe1212dSJohn Johansen 	if (dfa->tables[YYTD_ID_EC]) {
4930fe1212dSJohn Johansen 		/* Equivalence class table defined */
4940fe1212dSJohn Johansen 		u8 *equiv = EQUIV_TABLE(dfa);
495074c1cd7SJohn Johansen 		match_char(state, def, base, next, check, equiv[(u8) c]);
496074c1cd7SJohn Johansen 	} else
497074c1cd7SJohn Johansen 		match_char(state, def, base, next, check, (u8) c);
4980fe1212dSJohn Johansen 
4990fe1212dSJohn Johansen 	return state;
500e06f75a6SJohn Johansen }
501cf65fabcSJohn Johansen 
502cf65fabcSJohn Johansen /**
503cf65fabcSJohn Johansen  * aa_dfa_match_until - traverse @dfa until accept state or end of input
504cf65fabcSJohn Johansen  * @dfa: the dfa to match @str against  (NOT NULL)
505cf65fabcSJohn Johansen  * @start: the state of the dfa to start matching in
506cf65fabcSJohn Johansen  * @str: the null terminated string of bytes to match against the dfa (NOT NULL)
507cf65fabcSJohn Johansen  * @retpos: first character in str after match OR end of string
508cf65fabcSJohn Johansen  *
509cf65fabcSJohn Johansen  * aa_dfa_match will match @str against the dfa and return the state it
510cf65fabcSJohn Johansen  * finished matching in. The final state can be used to look up the accepting
511cf65fabcSJohn Johansen  * label, or as the start state of a continuing match.
512cf65fabcSJohn Johansen  *
513cf65fabcSJohn Johansen  * Returns: final state reached after input is consumed
514cf65fabcSJohn Johansen  */
515cf65fabcSJohn Johansen unsigned int aa_dfa_match_until(struct aa_dfa *dfa, unsigned int start,
516cf65fabcSJohn Johansen 				const char *str, const char **retpos)
517cf65fabcSJohn Johansen {
518cf65fabcSJohn Johansen 	u16 *def = DEFAULT_TABLE(dfa);
519cf65fabcSJohn Johansen 	u32 *base = BASE_TABLE(dfa);
520cf65fabcSJohn Johansen 	u16 *next = NEXT_TABLE(dfa);
521cf65fabcSJohn Johansen 	u16 *check = CHECK_TABLE(dfa);
522cf65fabcSJohn Johansen 	u32 *accept = ACCEPT_TABLE(dfa);
523cf65fabcSJohn Johansen 	unsigned int state = start, pos;
524cf65fabcSJohn Johansen 
525cf65fabcSJohn Johansen 	if (state == 0)
526cf65fabcSJohn Johansen 		return 0;
527cf65fabcSJohn Johansen 
528cf65fabcSJohn Johansen 	/* current state is <state>, matching character *str */
529cf65fabcSJohn Johansen 	if (dfa->tables[YYTD_ID_EC]) {
530cf65fabcSJohn Johansen 		/* Equivalence class table defined */
531cf65fabcSJohn Johansen 		u8 *equiv = EQUIV_TABLE(dfa);
532cf65fabcSJohn Johansen 		/* default is direct to next state */
533cf65fabcSJohn Johansen 		while (*str) {
534cf65fabcSJohn Johansen 			pos = base_idx(base[state]) + equiv[(u8) *str++];
535cf65fabcSJohn Johansen 			if (check[pos] == state)
536cf65fabcSJohn Johansen 				state = next[pos];
537cf65fabcSJohn Johansen 			else
538cf65fabcSJohn Johansen 				state = def[state];
539cf65fabcSJohn Johansen 			if (accept[state])
540cf65fabcSJohn Johansen 				break;
541cf65fabcSJohn Johansen 		}
542cf65fabcSJohn Johansen 	} else {
543cf65fabcSJohn Johansen 		/* default is direct to next state */
544cf65fabcSJohn Johansen 		while (*str) {
545cf65fabcSJohn Johansen 			pos = base_idx(base[state]) + (u8) *str++;
546cf65fabcSJohn Johansen 			if (check[pos] == state)
547cf65fabcSJohn Johansen 				state = next[pos];
548cf65fabcSJohn Johansen 			else
549cf65fabcSJohn Johansen 				state = def[state];
550cf65fabcSJohn Johansen 			if (accept[state])
551cf65fabcSJohn Johansen 				break;
552cf65fabcSJohn Johansen 		}
553cf65fabcSJohn Johansen 	}
554cf65fabcSJohn Johansen 
555cf65fabcSJohn Johansen 	*retpos = str;
556cf65fabcSJohn Johansen 	return state;
557cf65fabcSJohn Johansen }
558cf65fabcSJohn Johansen 
559cf65fabcSJohn Johansen 
560cf65fabcSJohn Johansen /**
561cf65fabcSJohn Johansen  * aa_dfa_matchn_until - traverse @dfa until accept or @n bytes consumed
562cf65fabcSJohn Johansen  * @dfa: the dfa to match @str against  (NOT NULL)
563cf65fabcSJohn Johansen  * @start: the state of the dfa to start matching in
564cf65fabcSJohn Johansen  * @str: the string of bytes to match against the dfa  (NOT NULL)
565cf65fabcSJohn Johansen  * @n: length of the string of bytes to match
566cf65fabcSJohn Johansen  * @retpos: first character in str after match OR str + n
567cf65fabcSJohn Johansen  *
568cf65fabcSJohn Johansen  * aa_dfa_match_len will match @str against the dfa and return the state it
569cf65fabcSJohn Johansen  * finished matching in. The final state can be used to look up the accepting
570cf65fabcSJohn Johansen  * label, or as the start state of a continuing match.
571cf65fabcSJohn Johansen  *
572cf65fabcSJohn Johansen  * This function will happily match again the 0 byte and only finishes
573cf65fabcSJohn Johansen  * when @n input is consumed.
574cf65fabcSJohn Johansen  *
575cf65fabcSJohn Johansen  * Returns: final state reached after input is consumed
576cf65fabcSJohn Johansen  */
577cf65fabcSJohn Johansen unsigned int aa_dfa_matchn_until(struct aa_dfa *dfa, unsigned int start,
578cf65fabcSJohn Johansen 				 const char *str, int n, const char **retpos)
579cf65fabcSJohn Johansen {
580cf65fabcSJohn Johansen 	u16 *def = DEFAULT_TABLE(dfa);
581cf65fabcSJohn Johansen 	u32 *base = BASE_TABLE(dfa);
582cf65fabcSJohn Johansen 	u16 *next = NEXT_TABLE(dfa);
583cf65fabcSJohn Johansen 	u16 *check = CHECK_TABLE(dfa);
584cf65fabcSJohn Johansen 	u32 *accept = ACCEPT_TABLE(dfa);
585cf65fabcSJohn Johansen 	unsigned int state = start, pos;
586cf65fabcSJohn Johansen 
587cf65fabcSJohn Johansen 	*retpos = NULL;
588cf65fabcSJohn Johansen 	if (state == 0)
589cf65fabcSJohn Johansen 		return 0;
590cf65fabcSJohn Johansen 
591cf65fabcSJohn Johansen 	/* current state is <state>, matching character *str */
592cf65fabcSJohn Johansen 	if (dfa->tables[YYTD_ID_EC]) {
593cf65fabcSJohn Johansen 		/* Equivalence class table defined */
594cf65fabcSJohn Johansen 		u8 *equiv = EQUIV_TABLE(dfa);
595cf65fabcSJohn Johansen 		/* default is direct to next state */
596cf65fabcSJohn Johansen 		for (; n; n--) {
597cf65fabcSJohn Johansen 			pos = base_idx(base[state]) + equiv[(u8) *str++];
598cf65fabcSJohn Johansen 			if (check[pos] == state)
599cf65fabcSJohn Johansen 				state = next[pos];
600cf65fabcSJohn Johansen 			else
601cf65fabcSJohn Johansen 				state = def[state];
602cf65fabcSJohn Johansen 			if (accept[state])
603cf65fabcSJohn Johansen 				break;
604cf65fabcSJohn Johansen 		}
605cf65fabcSJohn Johansen 	} else {
606cf65fabcSJohn Johansen 		/* default is direct to next state */
607cf65fabcSJohn Johansen 		for (; n; n--) {
608cf65fabcSJohn Johansen 			pos = base_idx(base[state]) + (u8) *str++;
609cf65fabcSJohn Johansen 			if (check[pos] == state)
610cf65fabcSJohn Johansen 				state = next[pos];
611cf65fabcSJohn Johansen 			else
612cf65fabcSJohn Johansen 				state = def[state];
613cf65fabcSJohn Johansen 			if (accept[state])
614cf65fabcSJohn Johansen 				break;
615cf65fabcSJohn Johansen 		}
616cf65fabcSJohn Johansen 	}
617cf65fabcSJohn Johansen 
618cf65fabcSJohn Johansen 	*retpos = str;
619cf65fabcSJohn Johansen 	return state;
620cf65fabcSJohn Johansen }
621