xref: /linux-6.15/security/apparmor/match.c (revision 074c1cd7)
1 /*
2  * AppArmor security module
3  *
4  * This file contains AppArmor dfa based regular expression matching engine
5  *
6  * Copyright (C) 1998-2008 Novell/SUSE
7  * Copyright 2009-2012 Canonical Ltd.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation, version 2 of the
12  * License.
13  */
14 
15 #include <linux/errno.h>
16 #include <linux/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/err.h>
21 #include <linux/kref.h>
22 
23 #include "include/lib.h"
24 #include "include/match.h"
25 
26 #define base_idx(X) ((X) & 0xffffff)
27 
28 static char nulldfa_src[] = {
29 	#include "nulldfa.in"
30 };
31 struct aa_dfa *nulldfa;
32 
33 static char stacksplitdfa_src[] = {
34 	#include "stacksplitdfa.in"
35 };
36 struct aa_dfa *stacksplitdfa;
37 
38 int aa_setup_dfa_engine(void)
39 {
40 	int error;
41 
42 	nulldfa = aa_dfa_unpack(nulldfa_src, sizeof(nulldfa_src),
43 				TO_ACCEPT1_FLAG(YYTD_DATA32) |
44 				TO_ACCEPT2_FLAG(YYTD_DATA32));
45 	if (IS_ERR(nulldfa)) {
46 		error = PTR_ERR(nulldfa);
47 		nulldfa = NULL;
48 		return error;
49 	}
50 
51 	stacksplitdfa = aa_dfa_unpack(stacksplitdfa_src,
52 				      sizeof(stacksplitdfa_src),
53 				      TO_ACCEPT1_FLAG(YYTD_DATA32) |
54 				      TO_ACCEPT2_FLAG(YYTD_DATA32));
55 	if (IS_ERR(stacksplitdfa)) {
56 		aa_put_dfa(nulldfa);
57 		nulldfa = NULL;
58 		error = PTR_ERR(stacksplitdfa);
59 		stacksplitdfa = NULL;
60 		return error;
61 	}
62 
63 	return 0;
64 }
65 
66 void aa_teardown_dfa_engine(void)
67 {
68 	aa_put_dfa(stacksplitdfa);
69 	aa_put_dfa(nulldfa);
70 }
71 
72 /**
73  * unpack_table - unpack a dfa table (one of accept, default, base, next check)
74  * @blob: data to unpack (NOT NULL)
75  * @bsize: size of blob
76  *
77  * Returns: pointer to table else NULL on failure
78  *
79  * NOTE: must be freed by kvfree (not kfree)
80  */
81 static struct table_header *unpack_table(char *blob, size_t bsize)
82 {
83 	struct table_header *table = NULL;
84 	struct table_header th;
85 	size_t tsize;
86 
87 	if (bsize < sizeof(struct table_header))
88 		goto out;
89 
90 	/* loaded td_id's start at 1, subtract 1 now to avoid doing
91 	 * it every time we use td_id as an index
92 	 */
93 	th.td_id = be16_to_cpu(*(__be16 *) (blob)) - 1;
94 	if (th.td_id > YYTD_ID_MAX)
95 		goto out;
96 	th.td_flags = be16_to_cpu(*(__be16 *) (blob + 2));
97 	th.td_lolen = be32_to_cpu(*(__be32 *) (blob + 8));
98 	blob += sizeof(struct table_header);
99 
100 	if (!(th.td_flags == YYTD_DATA16 || th.td_flags == YYTD_DATA32 ||
101 	      th.td_flags == YYTD_DATA8))
102 		goto out;
103 
104 	tsize = table_size(th.td_lolen, th.td_flags);
105 	if (bsize < tsize)
106 		goto out;
107 
108 	table = kvzalloc(tsize, GFP_KERNEL);
109 	if (table) {
110 		table->td_id = th.td_id;
111 		table->td_flags = th.td_flags;
112 		table->td_lolen = th.td_lolen;
113 		if (th.td_flags == YYTD_DATA8)
114 			UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
115 				     u8, u8, byte_to_byte);
116 		else if (th.td_flags == YYTD_DATA16)
117 			UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
118 				     u16, __be16, be16_to_cpu);
119 		else if (th.td_flags == YYTD_DATA32)
120 			UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
121 				     u32, __be32, be32_to_cpu);
122 		else
123 			goto fail;
124 		/* if table was vmalloced make sure the page tables are synced
125 		 * before it is used, as it goes live to all cpus.
126 		 */
127 		if (is_vmalloc_addr(table))
128 			vm_unmap_aliases();
129 	}
130 
131 out:
132 	return table;
133 fail:
134 	kvfree(table);
135 	return NULL;
136 }
137 
138 /**
139  * verify_dfa - verify that transitions and states in the tables are in bounds.
140  * @dfa: dfa to test  (NOT NULL)
141  * @flags: flags controlling what type of accept table are acceptable
142  *
143  * Assumes dfa has gone through the first pass verification done by unpacking
144  * NOTE: this does not valid accept table values
145  *
146  * Returns: %0 else error code on failure to verify
147  */
148 static int verify_dfa(struct aa_dfa *dfa, int flags)
149 {
150 	size_t i, state_count, trans_count;
151 	int error = -EPROTO;
152 
153 	/* check that required tables exist */
154 	if (!(dfa->tables[YYTD_ID_DEF] &&
155 	      dfa->tables[YYTD_ID_BASE] &&
156 	      dfa->tables[YYTD_ID_NXT] && dfa->tables[YYTD_ID_CHK]))
157 		goto out;
158 
159 	/* accept.size == default.size == base.size */
160 	state_count = dfa->tables[YYTD_ID_BASE]->td_lolen;
161 	if (ACCEPT1_FLAGS(flags)) {
162 		if (!dfa->tables[YYTD_ID_ACCEPT])
163 			goto out;
164 		if (state_count != dfa->tables[YYTD_ID_ACCEPT]->td_lolen)
165 			goto out;
166 	}
167 	if (ACCEPT2_FLAGS(flags)) {
168 		if (!dfa->tables[YYTD_ID_ACCEPT2])
169 			goto out;
170 		if (state_count != dfa->tables[YYTD_ID_ACCEPT2]->td_lolen)
171 			goto out;
172 	}
173 	if (state_count != dfa->tables[YYTD_ID_DEF]->td_lolen)
174 		goto out;
175 
176 	/* next.size == chk.size */
177 	trans_count = dfa->tables[YYTD_ID_NXT]->td_lolen;
178 	if (trans_count != dfa->tables[YYTD_ID_CHK]->td_lolen)
179 		goto out;
180 
181 	/* if equivalence classes then its table size must be 256 */
182 	if (dfa->tables[YYTD_ID_EC] &&
183 	    dfa->tables[YYTD_ID_EC]->td_lolen != 256)
184 		goto out;
185 
186 	if (flags & DFA_FLAG_VERIFY_STATES) {
187 		for (i = 0; i < state_count; i++) {
188 			if (DEFAULT_TABLE(dfa)[i] >= state_count)
189 				goto out;
190 			if (base_idx(BASE_TABLE(dfa)[i]) + 255 >= trans_count) {
191 				printk(KERN_ERR "AppArmor DFA next/check upper "
192 				       "bounds error\n");
193 				goto out;
194 			}
195 		}
196 
197 		for (i = 0; i < trans_count; i++) {
198 			if (NEXT_TABLE(dfa)[i] >= state_count)
199 				goto out;
200 			if (CHECK_TABLE(dfa)[i] >= state_count)
201 				goto out;
202 		}
203 	}
204 
205 	error = 0;
206 out:
207 	return error;
208 }
209 
210 /**
211  * dfa_free - free a dfa allocated by aa_dfa_unpack
212  * @dfa: the dfa to free  (MAYBE NULL)
213  *
214  * Requires: reference count to dfa == 0
215  */
216 static void dfa_free(struct aa_dfa *dfa)
217 {
218 	if (dfa) {
219 		int i;
220 
221 		for (i = 0; i < ARRAY_SIZE(dfa->tables); i++) {
222 			kvfree(dfa->tables[i]);
223 			dfa->tables[i] = NULL;
224 		}
225 		kfree(dfa);
226 	}
227 }
228 
229 /**
230  * aa_dfa_free_kref - free aa_dfa by kref (called by aa_put_dfa)
231  * @kr: kref callback for freeing of a dfa  (NOT NULL)
232  */
233 void aa_dfa_free_kref(struct kref *kref)
234 {
235 	struct aa_dfa *dfa = container_of(kref, struct aa_dfa, count);
236 	dfa_free(dfa);
237 }
238 
239 /**
240  * aa_dfa_unpack - unpack the binary tables of a serialized dfa
241  * @blob: aligned serialized stream of data to unpack  (NOT NULL)
242  * @size: size of data to unpack
243  * @flags: flags controlling what type of accept tables are acceptable
244  *
245  * Unpack a dfa that has been serialized.  To find information on the dfa
246  * format look in Documentation/admin-guide/LSM/apparmor.rst
247  * Assumes the dfa @blob stream has been aligned on a 8 byte boundary
248  *
249  * Returns: an unpacked dfa ready for matching or ERR_PTR on failure
250  */
251 struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags)
252 {
253 	int hsize;
254 	int error = -ENOMEM;
255 	char *data = blob;
256 	struct table_header *table = NULL;
257 	struct aa_dfa *dfa = kzalloc(sizeof(struct aa_dfa), GFP_KERNEL);
258 	if (!dfa)
259 		goto fail;
260 
261 	kref_init(&dfa->count);
262 
263 	error = -EPROTO;
264 
265 	/* get dfa table set header */
266 	if (size < sizeof(struct table_set_header))
267 		goto fail;
268 
269 	if (ntohl(*(__be32 *) data) != YYTH_MAGIC)
270 		goto fail;
271 
272 	hsize = ntohl(*(__be32 *) (data + 4));
273 	if (size < hsize)
274 		goto fail;
275 
276 	dfa->flags = ntohs(*(__be16 *) (data + 12));
277 	data += hsize;
278 	size -= hsize;
279 
280 	while (size > 0) {
281 		table = unpack_table(data, size);
282 		if (!table)
283 			goto fail;
284 
285 		switch (table->td_id) {
286 		case YYTD_ID_ACCEPT:
287 			if (!(table->td_flags & ACCEPT1_FLAGS(flags)))
288 				goto fail;
289 			break;
290 		case YYTD_ID_ACCEPT2:
291 			if (!(table->td_flags & ACCEPT2_FLAGS(flags)))
292 				goto fail;
293 			break;
294 		case YYTD_ID_BASE:
295 			if (table->td_flags != YYTD_DATA32)
296 				goto fail;
297 			break;
298 		case YYTD_ID_DEF:
299 		case YYTD_ID_NXT:
300 		case YYTD_ID_CHK:
301 			if (table->td_flags != YYTD_DATA16)
302 				goto fail;
303 			break;
304 		case YYTD_ID_EC:
305 			if (table->td_flags != YYTD_DATA8)
306 				goto fail;
307 			break;
308 		default:
309 			goto fail;
310 		}
311 		/* check for duplicate table entry */
312 		if (dfa->tables[table->td_id])
313 			goto fail;
314 		dfa->tables[table->td_id] = table;
315 		data += table_size(table->td_lolen, table->td_flags);
316 		size -= table_size(table->td_lolen, table->td_flags);
317 		table = NULL;
318 	}
319 
320 	error = verify_dfa(dfa, flags);
321 	if (error)
322 		goto fail;
323 
324 	return dfa;
325 
326 fail:
327 	kvfree(table);
328 	dfa_free(dfa);
329 	return ERR_PTR(error);
330 }
331 
332 #define match_char(state, def, base, next, check, C)	\
333 do {							\
334 	u32 b = (base)[(state)];			\
335 	unsigned int pos = base_idx(b) + (C);		\
336 	if ((check)[pos] != (state)) {			\
337 		(state) = (def)[(state)];		\
338 		break;					\
339 	}						\
340 	(state) = (next)[pos];				\
341 	break;						\
342 } while (1)
343 
344 /**
345  * aa_dfa_match_len - traverse @dfa to find state @str stops at
346  * @dfa: the dfa to match @str against  (NOT NULL)
347  * @start: the state of the dfa to start matching in
348  * @str: the string of bytes to match against the dfa  (NOT NULL)
349  * @len: length of the string of bytes to match
350  *
351  * aa_dfa_match_len will match @str against the dfa and return the state it
352  * finished matching in. The final state can be used to look up the accepting
353  * label, or as the start state of a continuing match.
354  *
355  * This function will happily match again the 0 byte and only finishes
356  * when @len input is consumed.
357  *
358  * Returns: final state reached after input is consumed
359  */
360 unsigned int aa_dfa_match_len(struct aa_dfa *dfa, unsigned int start,
361 			      const char *str, int len)
362 {
363 	u16 *def = DEFAULT_TABLE(dfa);
364 	u32 *base = BASE_TABLE(dfa);
365 	u16 *next = NEXT_TABLE(dfa);
366 	u16 *check = CHECK_TABLE(dfa);
367 	unsigned int state = start;
368 
369 	if (state == 0)
370 		return 0;
371 
372 	/* current state is <state>, matching character *str */
373 	if (dfa->tables[YYTD_ID_EC]) {
374 		/* Equivalence class table defined */
375 		u8 *equiv = EQUIV_TABLE(dfa);
376 		for (; len; len--)
377 			match_char(state, def, base, next, check,
378 				   equiv[(u8) *str++]);
379 	} else {
380 		/* default is direct to next state */
381 		for (; len; len--)
382 			match_char(state, def, base, next, check, (u8) *str++);
383 	}
384 
385 	return state;
386 }
387 
388 /**
389  * aa_dfa_match - traverse @dfa to find state @str stops at
390  * @dfa: the dfa to match @str against  (NOT NULL)
391  * @start: the state of the dfa to start matching in
392  * @str: the null terminated string of bytes to match against the dfa (NOT NULL)
393  *
394  * aa_dfa_match will match @str against the dfa and return the state it
395  * finished matching in. The final state can be used to look up the accepting
396  * label, or as the start state of a continuing match.
397  *
398  * Returns: final state reached after input is consumed
399  */
400 unsigned int aa_dfa_match(struct aa_dfa *dfa, unsigned int start,
401 			  const char *str)
402 {
403 	u16 *def = DEFAULT_TABLE(dfa);
404 	u32 *base = BASE_TABLE(dfa);
405 	u16 *next = NEXT_TABLE(dfa);
406 	u16 *check = CHECK_TABLE(dfa);
407 	unsigned int state = start;
408 
409 	if (state == 0)
410 		return 0;
411 
412 	/* current state is <state>, matching character *str */
413 	if (dfa->tables[YYTD_ID_EC]) {
414 		/* Equivalence class table defined */
415 		u8 *equiv = EQUIV_TABLE(dfa);
416 		/* default is direct to next state */
417 		while (*str)
418 			match_char(state, def, base, next, check,
419 				   equiv[(u8) *str++]);
420 	} else {
421 		/* default is direct to next state */
422 		while (*str)
423 			match_char(state, def, base, next, check, (u8) *str++);
424 	}
425 
426 	return state;
427 }
428 
429 /**
430  * aa_dfa_next - step one character to the next state in the dfa
431  * @dfa: the dfa to tranverse (NOT NULL)
432  * @state: the state to start in
433  * @c: the input character to transition on
434  *
435  * aa_dfa_match will step through the dfa by one input character @c
436  *
437  * Returns: state reach after input @c
438  */
439 unsigned int aa_dfa_next(struct aa_dfa *dfa, unsigned int state,
440 			  const char c)
441 {
442 	u16 *def = DEFAULT_TABLE(dfa);
443 	u32 *base = BASE_TABLE(dfa);
444 	u16 *next = NEXT_TABLE(dfa);
445 	u16 *check = CHECK_TABLE(dfa);
446 
447 	/* current state is <state>, matching character *str */
448 	if (dfa->tables[YYTD_ID_EC]) {
449 		/* Equivalence class table defined */
450 		u8 *equiv = EQUIV_TABLE(dfa);
451 		match_char(state, def, base, next, check, equiv[(u8) c]);
452 	} else
453 		match_char(state, def, base, next, check, (u8) c);
454 
455 	return state;
456 }
457 
458 /**
459  * aa_dfa_match_until - traverse @dfa until accept state or end of input
460  * @dfa: the dfa to match @str against  (NOT NULL)
461  * @start: the state of the dfa to start matching in
462  * @str: the null terminated string of bytes to match against the dfa (NOT NULL)
463  * @retpos: first character in str after match OR end of string
464  *
465  * aa_dfa_match will match @str against the dfa and return the state it
466  * finished matching in. The final state can be used to look up the accepting
467  * label, or as the start state of a continuing match.
468  *
469  * Returns: final state reached after input is consumed
470  */
471 unsigned int aa_dfa_match_until(struct aa_dfa *dfa, unsigned int start,
472 				const char *str, const char **retpos)
473 {
474 	u16 *def = DEFAULT_TABLE(dfa);
475 	u32 *base = BASE_TABLE(dfa);
476 	u16 *next = NEXT_TABLE(dfa);
477 	u16 *check = CHECK_TABLE(dfa);
478 	u32 *accept = ACCEPT_TABLE(dfa);
479 	unsigned int state = start, pos;
480 
481 	if (state == 0)
482 		return 0;
483 
484 	/* current state is <state>, matching character *str */
485 	if (dfa->tables[YYTD_ID_EC]) {
486 		/* Equivalence class table defined */
487 		u8 *equiv = EQUIV_TABLE(dfa);
488 		/* default is direct to next state */
489 		while (*str) {
490 			pos = base_idx(base[state]) + equiv[(u8) *str++];
491 			if (check[pos] == state)
492 				state = next[pos];
493 			else
494 				state = def[state];
495 			if (accept[state])
496 				break;
497 		}
498 	} else {
499 		/* default is direct to next state */
500 		while (*str) {
501 			pos = base_idx(base[state]) + (u8) *str++;
502 			if (check[pos] == state)
503 				state = next[pos];
504 			else
505 				state = def[state];
506 			if (accept[state])
507 				break;
508 		}
509 	}
510 
511 	*retpos = str;
512 	return state;
513 }
514 
515 
516 /**
517  * aa_dfa_matchn_until - traverse @dfa until accept or @n bytes consumed
518  * @dfa: the dfa to match @str against  (NOT NULL)
519  * @start: the state of the dfa to start matching in
520  * @str: the string of bytes to match against the dfa  (NOT NULL)
521  * @n: length of the string of bytes to match
522  * @retpos: first character in str after match OR str + n
523  *
524  * aa_dfa_match_len will match @str against the dfa and return the state it
525  * finished matching in. The final state can be used to look up the accepting
526  * label, or as the start state of a continuing match.
527  *
528  * This function will happily match again the 0 byte and only finishes
529  * when @n input is consumed.
530  *
531  * Returns: final state reached after input is consumed
532  */
533 unsigned int aa_dfa_matchn_until(struct aa_dfa *dfa, unsigned int start,
534 				 const char *str, int n, const char **retpos)
535 {
536 	u16 *def = DEFAULT_TABLE(dfa);
537 	u32 *base = BASE_TABLE(dfa);
538 	u16 *next = NEXT_TABLE(dfa);
539 	u16 *check = CHECK_TABLE(dfa);
540 	u32 *accept = ACCEPT_TABLE(dfa);
541 	unsigned int state = start, pos;
542 
543 	*retpos = NULL;
544 	if (state == 0)
545 		return 0;
546 
547 	/* current state is <state>, matching character *str */
548 	if (dfa->tables[YYTD_ID_EC]) {
549 		/* Equivalence class table defined */
550 		u8 *equiv = EQUIV_TABLE(dfa);
551 		/* default is direct to next state */
552 		for (; n; n--) {
553 			pos = base_idx(base[state]) + equiv[(u8) *str++];
554 			if (check[pos] == state)
555 				state = next[pos];
556 			else
557 				state = def[state];
558 			if (accept[state])
559 				break;
560 		}
561 	} else {
562 		/* default is direct to next state */
563 		for (; n; n--) {
564 			pos = base_idx(base[state]) + (u8) *str++;
565 			if (check[pos] == state)
566 				state = next[pos];
567 			else
568 				state = def[state];
569 			if (accept[state])
570 				break;
571 		}
572 	}
573 
574 	*retpos = str;
575 	return state;
576 }
577