2 * AppArmor security module
4 * This file contains AppArmor dfa based regular expression matching engine
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
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
15 #include <linux/errno.h>
16 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/err.h>
21 #include <linux/kref.h>
23 #include "include/apparmor.h"
24 #include "include/match.h"
27 * unpack_table - unpack a dfa table (one of accept, default, base, next check)
28 * @blob: data to unpack (NOT NULL)
29 * @bsize: size of blob
31 * Returns: pointer to table else NULL on failure
33 * NOTE: must be freed by kvfree (not kmalloc)
35 static struct table_header *unpack_table(char *blob, size_t bsize)
37 struct table_header *table = NULL;
38 struct table_header th;
41 if (bsize < sizeof(struct table_header))
44 /* loaded td_id's start at 1, subtract 1 now to avoid doing
45 * it every time we use td_id as an index
47 th.td_id = be16_to_cpu(*(u16 *) (blob)) - 1;
48 th.td_flags = be16_to_cpu(*(u16 *) (blob + 2));
49 th.td_lolen = be32_to_cpu(*(u32 *) (blob + 8));
50 blob += sizeof(struct table_header);
52 if (!(th.td_flags == YYTD_DATA16 || th.td_flags == YYTD_DATA32 ||
53 th.td_flags == YYTD_DATA8))
56 tsize = table_size(th.td_lolen, th.td_flags);
60 table = kvmalloc(tsize);
63 if (th.td_flags == YYTD_DATA8)
64 UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
66 else if (th.td_flags == YYTD_DATA16)
67 UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
69 else if (th.td_flags == YYTD_DATA32)
70 UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
77 /* if table was vmalloced make sure the page tables are synced
78 * before it is used, as it goes live to all cpus.
80 if (is_vmalloc_addr(table))
89 * verify_dfa - verify that transitions and states in the tables are in bounds.
90 * @dfa: dfa to test (NOT NULL)
91 * @flags: flags controlling what type of accept table are acceptable
93 * Assumes dfa has gone through the first pass verification done by unpacking
94 * NOTE: this does not valid accept table values
96 * Returns: %0 else error code on failure to verify
98 static int verify_dfa(struct aa_dfa *dfa, int flags)
100 size_t i, state_count, trans_count;
103 /* check that required tables exist */
104 if (!(dfa->tables[YYTD_ID_DEF] &&
105 dfa->tables[YYTD_ID_BASE] &&
106 dfa->tables[YYTD_ID_NXT] && dfa->tables[YYTD_ID_CHK]))
109 /* accept.size == default.size == base.size */
110 state_count = dfa->tables[YYTD_ID_BASE]->td_lolen;
111 if (ACCEPT1_FLAGS(flags)) {
112 if (!dfa->tables[YYTD_ID_ACCEPT])
114 if (state_count != dfa->tables[YYTD_ID_ACCEPT]->td_lolen)
117 if (ACCEPT2_FLAGS(flags)) {
118 if (!dfa->tables[YYTD_ID_ACCEPT2])
120 if (state_count != dfa->tables[YYTD_ID_ACCEPT2]->td_lolen)
123 if (state_count != dfa->tables[YYTD_ID_DEF]->td_lolen)
126 /* next.size == chk.size */
127 trans_count = dfa->tables[YYTD_ID_NXT]->td_lolen;
128 if (trans_count != dfa->tables[YYTD_ID_CHK]->td_lolen)
131 /* if equivalence classes then its table size must be 256 */
132 if (dfa->tables[YYTD_ID_EC] &&
133 dfa->tables[YYTD_ID_EC]->td_lolen != 256)
136 if (flags & DFA_FLAG_VERIFY_STATES) {
137 for (i = 0; i < state_count; i++) {
138 if (DEFAULT_TABLE(dfa)[i] >= state_count)
140 /* TODO: do check that DEF state recursion terminates */
141 if (BASE_TABLE(dfa)[i] + 255 >= trans_count) {
142 printk(KERN_ERR "AppArmor DFA next/check upper "
148 for (i = 0; i < trans_count; i++) {
149 if (NEXT_TABLE(dfa)[i] >= state_count)
151 if (CHECK_TABLE(dfa)[i] >= state_count)
162 * dfa_free - free a dfa allocated by aa_dfa_unpack
163 * @dfa: the dfa to free (MAYBE NULL)
165 * Requires: reference count to dfa == 0
167 static void dfa_free(struct aa_dfa *dfa)
172 for (i = 0; i < ARRAY_SIZE(dfa->tables); i++) {
173 kvfree(dfa->tables[i]);
174 dfa->tables[i] = NULL;
181 * aa_dfa_free_kref - free aa_dfa by kref (called by aa_put_dfa)
182 * @kr: kref callback for freeing of a dfa (NOT NULL)
184 void aa_dfa_free_kref(struct kref *kref)
186 struct aa_dfa *dfa = container_of(kref, struct aa_dfa, count);
191 * aa_dfa_unpack - unpack the binary tables of a serialized dfa
192 * @blob: aligned serialized stream of data to unpack (NOT NULL)
193 * @size: size of data to unpack
194 * @flags: flags controlling what type of accept tables are acceptable
196 * Unpack a dfa that has been serialized. To find information on the dfa
197 * format look in Documentation/security/apparmor.txt
198 * Assumes the dfa @blob stream has been aligned on a 8 byte boundary
200 * Returns: an unpacked dfa ready for matching or ERR_PTR on failure
202 struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags)
207 struct table_header *table = NULL;
208 struct aa_dfa *dfa = kzalloc(sizeof(struct aa_dfa), GFP_KERNEL);
212 kref_init(&dfa->count);
216 /* get dfa table set header */
217 if (size < sizeof(struct table_set_header))
220 if (ntohl(*(u32 *) data) != YYTH_MAGIC)
223 hsize = ntohl(*(u32 *) (data + 4));
227 dfa->flags = ntohs(*(u16 *) (data + 12));
232 table = unpack_table(data, size);
236 switch (table->td_id) {
238 if (!(table->td_flags & ACCEPT1_FLAGS(flags)))
241 case YYTD_ID_ACCEPT2:
242 if (!(table->td_flags & ACCEPT2_FLAGS(flags)))
246 if (table->td_flags != YYTD_DATA32)
252 if (table->td_flags != YYTD_DATA16)
256 if (table->td_flags != YYTD_DATA8)
262 /* check for duplicate table entry */
263 if (dfa->tables[table->td_id])
265 dfa->tables[table->td_id] = table;
266 data += table_size(table->td_lolen, table->td_flags);
267 size -= table_size(table->td_lolen, table->td_flags);
271 error = verify_dfa(dfa, flags);
280 return ERR_PTR(error);
284 * aa_dfa_match_len - traverse @dfa to find state @str stops at
285 * @dfa: the dfa to match @str against (NOT NULL)
286 * @start: the state of the dfa to start matching in
287 * @str: the string of bytes to match against the dfa (NOT NULL)
288 * @len: length of the string of bytes to match
290 * aa_dfa_match_len will match @str against the dfa and return the state it
291 * finished matching in. The final state can be used to look up the accepting
292 * label, or as the start state of a continuing match.
294 * This function will happily match again the 0 byte and only finishes
295 * when @len input is consumed.
297 * Returns: final state reached after input is consumed
299 unsigned int aa_dfa_match_len(struct aa_dfa *dfa, unsigned int start,
300 const char *str, int len)
302 u16 *def = DEFAULT_TABLE(dfa);
303 u32 *base = BASE_TABLE(dfa);
304 u16 *next = NEXT_TABLE(dfa);
305 u16 *check = CHECK_TABLE(dfa);
306 unsigned int state = start, pos;
311 /* current state is <state>, matching character *str */
312 if (dfa->tables[YYTD_ID_EC]) {
313 /* Equivalence class table defined */
314 u8 *equiv = EQUIV_TABLE(dfa);
315 /* default is direct to next state */
317 pos = base[state] + equiv[(u8) *str++];
318 if (check[pos] == state)
324 /* default is direct to next state */
326 pos = base[state] + (u8) *str++;
327 if (check[pos] == state)
338 * aa_dfa_match - traverse @dfa to find state @str stops at
339 * @dfa: the dfa to match @str against (NOT NULL)
340 * @start: the state of the dfa to start matching in
341 * @str: the null terminated string of bytes to match against the dfa (NOT NULL)
343 * aa_dfa_match will match @str against the dfa and return the state it
344 * finished matching in. The final state can be used to look up the accepting
345 * label, or as the start state of a continuing match.
347 * Returns: final state reached after input is consumed
349 unsigned int aa_dfa_match(struct aa_dfa *dfa, unsigned int start,
352 u16 *def = DEFAULT_TABLE(dfa);
353 u32 *base = BASE_TABLE(dfa);
354 u16 *next = NEXT_TABLE(dfa);
355 u16 *check = CHECK_TABLE(dfa);
356 unsigned int state = start, pos;
361 /* current state is <state>, matching character *str */
362 if (dfa->tables[YYTD_ID_EC]) {
363 /* Equivalence class table defined */
364 u8 *equiv = EQUIV_TABLE(dfa);
365 /* default is direct to next state */
367 pos = base[state] + equiv[(u8) *str++];
368 if (check[pos] == state)
374 /* default is direct to next state */
376 pos = base[state] + (u8) *str++;
377 if (check[pos] == state)
388 * aa_dfa_next - step one character to the next state in the dfa
389 * @dfa: the dfa to tranverse (NOT NULL)
390 * @state: the state to start in
391 * @c: the input character to transition on
393 * aa_dfa_match will step through the dfa by one input character @c
395 * Returns: state reach after input @c
397 unsigned int aa_dfa_next(struct aa_dfa *dfa, unsigned int state,
400 u16 *def = DEFAULT_TABLE(dfa);
401 u32 *base = BASE_TABLE(dfa);
402 u16 *next = NEXT_TABLE(dfa);
403 u16 *check = CHECK_TABLE(dfa);
406 /* current state is <state>, matching character *str */
407 if (dfa->tables[YYTD_ID_EC]) {
408 /* Equivalence class table defined */
409 u8 *equiv = EQUIV_TABLE(dfa);
410 /* default is direct to next state */
412 pos = base[state] + equiv[(u8) c];
413 if (check[pos] == state)
418 /* default is direct to next state */
419 pos = base[state] + (u8) c;
420 if (check[pos] == state)