blob: 1ff823031c73ba8a27a0a3abb4189b3f136dc14f [file] [log] [blame]
John Johansene06f75a2010-07-29 14:48:01 -07001/*
2 * AppArmor security module
3 *
4 * This file contains AppArmor dfa based regular expression matching engine
5 *
6 * Copyright (C) 1998-2008 Novell/SUSE
John Johansen8e4ff102013-02-18 16:08:34 -08007 * Copyright 2009-2012 Canonical Ltd.
John Johansene06f75a2010-07-29 14:48:01 -07008 *
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/apparmor.h"
24#include "include/match.h"
25
26/**
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
30 *
31 * Returns: pointer to table else NULL on failure
32 *
John Johansen0ca554b2013-02-18 16:04:34 -080033 * NOTE: must be freed by kvfree (not kfree)
John Johansene06f75a2010-07-29 14:48:01 -070034 */
35static struct table_header *unpack_table(char *blob, size_t bsize)
36{
37 struct table_header *table = NULL;
38 struct table_header th;
39 size_t tsize;
40
41 if (bsize < sizeof(struct table_header))
42 goto out;
43
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
46 */
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);
51
52 if (!(th.td_flags == YYTD_DATA16 || th.td_flags == YYTD_DATA32 ||
53 th.td_flags == YYTD_DATA8))
54 goto out;
55
56 tsize = table_size(th.td_lolen, th.td_flags);
57 if (bsize < tsize)
58 goto out;
59
John Johansen0ca554b2013-02-18 16:04:34 -080060 table = kvzalloc(tsize);
John Johansene06f75a2010-07-29 14:48:01 -070061 if (table) {
62 *table = th;
63 if (th.td_flags == YYTD_DATA8)
64 UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
65 u8, byte_to_byte);
66 else if (th.td_flags == YYTD_DATA16)
67 UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
68 u16, be16_to_cpu);
69 else if (th.td_flags == YYTD_DATA32)
70 UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
71 u32, be32_to_cpu);
72 else
73 goto fail;
74 }
75
76out:
77 /* if table was vmalloced make sure the page tables are synced
78 * before it is used, as it goes live to all cpus.
79 */
80 if (is_vmalloc_addr(table))
81 vm_unmap_aliases();
82 return table;
83fail:
84 kvfree(table);
85 return NULL;
86}
87
88/**
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
92 *
93 * Assumes dfa has gone through the first pass verification done by unpacking
94 * NOTE: this does not valid accept table values
95 *
96 * Returns: %0 else error code on failure to verify
97 */
98static int verify_dfa(struct aa_dfa *dfa, int flags)
99{
100 size_t i, state_count, trans_count;
101 int error = -EPROTO;
102
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]))
107 goto out;
108
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])
113 goto out;
114 if (state_count != dfa->tables[YYTD_ID_ACCEPT]->td_lolen)
115 goto out;
116 }
117 if (ACCEPT2_FLAGS(flags)) {
118 if (!dfa->tables[YYTD_ID_ACCEPT2])
119 goto out;
120 if (state_count != dfa->tables[YYTD_ID_ACCEPT2]->td_lolen)
121 goto out;
122 }
123 if (state_count != dfa->tables[YYTD_ID_DEF]->td_lolen)
124 goto out;
125
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)
129 goto out;
130
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)
134 goto out;
135
136 if (flags & DFA_FLAG_VERIFY_STATES) {
137 for (i = 0; i < state_count; i++) {
138 if (DEFAULT_TABLE(dfa)[i] >= state_count)
139 goto out;
John Johansene06f75a2010-07-29 14:48:01 -0700140 if (BASE_TABLE(dfa)[i] + 255 >= trans_count) {
141 printk(KERN_ERR "AppArmor DFA next/check upper "
142 "bounds error\n");
143 goto out;
144 }
145 }
146
147 for (i = 0; i < trans_count; i++) {
148 if (NEXT_TABLE(dfa)[i] >= state_count)
149 goto out;
150 if (CHECK_TABLE(dfa)[i] >= state_count)
151 goto out;
152 }
153 }
154
155 error = 0;
156out:
157 return error;
158}
159
160/**
161 * dfa_free - free a dfa allocated by aa_dfa_unpack
162 * @dfa: the dfa to free (MAYBE NULL)
163 *
164 * Requires: reference count to dfa == 0
165 */
166static void dfa_free(struct aa_dfa *dfa)
167{
168 if (dfa) {
169 int i;
170
171 for (i = 0; i < ARRAY_SIZE(dfa->tables); i++) {
172 kvfree(dfa->tables[i]);
173 dfa->tables[i] = NULL;
174 }
175 kfree(dfa);
176 }
177}
178
179/**
180 * aa_dfa_free_kref - free aa_dfa by kref (called by aa_put_dfa)
181 * @kr: kref callback for freeing of a dfa (NOT NULL)
182 */
183void aa_dfa_free_kref(struct kref *kref)
184{
185 struct aa_dfa *dfa = container_of(kref, struct aa_dfa, count);
186 dfa_free(dfa);
187}
188
189/**
190 * aa_dfa_unpack - unpack the binary tables of a serialized dfa
191 * @blob: aligned serialized stream of data to unpack (NOT NULL)
192 * @size: size of data to unpack
193 * @flags: flags controlling what type of accept tables are acceptable
194 *
195 * Unpack a dfa that has been serialized. To find information on the dfa
Randy Dunlapd410fa42011-05-19 15:59:38 -0700196 * format look in Documentation/security/apparmor.txt
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300197 * Assumes the dfa @blob stream has been aligned on a 8 byte boundary
John Johansene06f75a2010-07-29 14:48:01 -0700198 *
199 * Returns: an unpacked dfa ready for matching or ERR_PTR on failure
200 */
201struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags)
202{
203 int hsize;
204 int error = -ENOMEM;
205 char *data = blob;
206 struct table_header *table = NULL;
207 struct aa_dfa *dfa = kzalloc(sizeof(struct aa_dfa), GFP_KERNEL);
208 if (!dfa)
209 goto fail;
210
211 kref_init(&dfa->count);
212
213 error = -EPROTO;
214
215 /* get dfa table set header */
216 if (size < sizeof(struct table_set_header))
217 goto fail;
218
219 if (ntohl(*(u32 *) data) != YYTH_MAGIC)
220 goto fail;
221
222 hsize = ntohl(*(u32 *) (data + 4));
223 if (size < hsize)
224 goto fail;
225
226 dfa->flags = ntohs(*(u16 *) (data + 12));
227 data += hsize;
228 size -= hsize;
229
230 while (size > 0) {
231 table = unpack_table(data, size);
232 if (!table)
233 goto fail;
234
235 switch (table->td_id) {
236 case YYTD_ID_ACCEPT:
237 if (!(table->td_flags & ACCEPT1_FLAGS(flags)))
238 goto fail;
239 break;
240 case YYTD_ID_ACCEPT2:
241 if (!(table->td_flags & ACCEPT2_FLAGS(flags)))
242 goto fail;
243 break;
244 case YYTD_ID_BASE:
245 if (table->td_flags != YYTD_DATA32)
246 goto fail;
247 break;
248 case YYTD_ID_DEF:
249 case YYTD_ID_NXT:
250 case YYTD_ID_CHK:
251 if (table->td_flags != YYTD_DATA16)
252 goto fail;
253 break;
254 case YYTD_ID_EC:
255 if (table->td_flags != YYTD_DATA8)
256 goto fail;
257 break;
258 default:
259 goto fail;
260 }
261 /* check for duplicate table entry */
262 if (dfa->tables[table->td_id])
263 goto fail;
264 dfa->tables[table->td_id] = table;
265 data += table_size(table->td_lolen, table->td_flags);
266 size -= table_size(table->td_lolen, table->td_flags);
267 table = NULL;
268 }
269
270 error = verify_dfa(dfa, flags);
271 if (error)
272 goto fail;
273
274 return dfa;
275
276fail:
277 kvfree(table);
278 dfa_free(dfa);
279 return ERR_PTR(error);
280}
281
282/**
283 * aa_dfa_match_len - traverse @dfa to find state @str stops at
284 * @dfa: the dfa to match @str against (NOT NULL)
285 * @start: the state of the dfa to start matching in
286 * @str: the string of bytes to match against the dfa (NOT NULL)
287 * @len: length of the string of bytes to match
288 *
289 * aa_dfa_match_len will match @str against the dfa and return the state it
290 * finished matching in. The final state can be used to look up the accepting
291 * label, or as the start state of a continuing match.
292 *
293 * This function will happily match again the 0 byte and only finishes
294 * when @len input is consumed.
295 *
296 * Returns: final state reached after input is consumed
297 */
298unsigned int aa_dfa_match_len(struct aa_dfa *dfa, unsigned int start,
299 const char *str, int len)
300{
301 u16 *def = DEFAULT_TABLE(dfa);
302 u32 *base = BASE_TABLE(dfa);
303 u16 *next = NEXT_TABLE(dfa);
304 u16 *check = CHECK_TABLE(dfa);
305 unsigned int state = start, pos;
306
307 if (state == 0)
308 return 0;
309
310 /* current state is <state>, matching character *str */
311 if (dfa->tables[YYTD_ID_EC]) {
312 /* Equivalence class table defined */
313 u8 *equiv = EQUIV_TABLE(dfa);
314 /* default is direct to next state */
315 for (; len; len--) {
316 pos = base[state] + equiv[(u8) *str++];
317 if (check[pos] == state)
318 state = next[pos];
319 else
320 state = def[state];
321 }
322 } else {
323 /* default is direct to next state */
324 for (; len; len--) {
325 pos = base[state] + (u8) *str++;
326 if (check[pos] == state)
327 state = next[pos];
328 else
329 state = def[state];
330 }
331 }
332
333 return state;
334}
335
336/**
John Johansen0fe12122012-02-16 06:20:26 -0800337 * aa_dfa_match - traverse @dfa to find state @str stops at
John Johansene06f75a2010-07-29 14:48:01 -0700338 * @dfa: the dfa to match @str against (NOT NULL)
339 * @start: the state of the dfa to start matching in
340 * @str: the null terminated string of bytes to match against the dfa (NOT NULL)
341 *
John Johansen0fe12122012-02-16 06:20:26 -0800342 * aa_dfa_match will match @str against the dfa and return the state it
John Johansene06f75a2010-07-29 14:48:01 -0700343 * finished matching in. The final state can be used to look up the accepting
344 * label, or as the start state of a continuing match.
345 *
346 * Returns: final state reached after input is consumed
347 */
348unsigned int aa_dfa_match(struct aa_dfa *dfa, unsigned int start,
349 const char *str)
350{
John Johansen0fe12122012-02-16 06:20:26 -0800351 u16 *def = DEFAULT_TABLE(dfa);
352 u32 *base = BASE_TABLE(dfa);
353 u16 *next = NEXT_TABLE(dfa);
354 u16 *check = CHECK_TABLE(dfa);
355 unsigned int state = start, pos;
356
357 if (state == 0)
358 return 0;
359
360 /* current state is <state>, matching character *str */
361 if (dfa->tables[YYTD_ID_EC]) {
362 /* Equivalence class table defined */
363 u8 *equiv = EQUIV_TABLE(dfa);
364 /* default is direct to next state */
365 while (*str) {
366 pos = base[state] + equiv[(u8) *str++];
367 if (check[pos] == state)
368 state = next[pos];
369 else
370 state = def[state];
371 }
372 } else {
373 /* default is direct to next state */
374 while (*str) {
375 pos = base[state] + (u8) *str++;
376 if (check[pos] == state)
377 state = next[pos];
378 else
379 state = def[state];
380 }
381 }
382
383 return state;
384}
385
386/**
387 * aa_dfa_next - step one character to the next state in the dfa
388 * @dfa: the dfa to tranverse (NOT NULL)
389 * @state: the state to start in
390 * @c: the input character to transition on
391 *
392 * aa_dfa_match will step through the dfa by one input character @c
393 *
394 * Returns: state reach after input @c
395 */
396unsigned int aa_dfa_next(struct aa_dfa *dfa, unsigned int state,
397 const char c)
398{
399 u16 *def = DEFAULT_TABLE(dfa);
400 u32 *base = BASE_TABLE(dfa);
401 u16 *next = NEXT_TABLE(dfa);
402 u16 *check = CHECK_TABLE(dfa);
403 unsigned int pos;
404
405 /* current state is <state>, matching character *str */
406 if (dfa->tables[YYTD_ID_EC]) {
407 /* Equivalence class table defined */
408 u8 *equiv = EQUIV_TABLE(dfa);
409 /* default is direct to next state */
410
411 pos = base[state] + equiv[(u8) c];
412 if (check[pos] == state)
413 state = next[pos];
414 else
415 state = def[state];
416 } else {
417 /* default is direct to next state */
418 pos = base[state] + (u8) c;
419 if (check[pos] == state)
420 state = next[pos];
421 else
422 state = def[state];
423 }
424
425 return state;
John Johansene06f75a2010-07-29 14:48:01 -0700426}