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