John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 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 |
John Johansen | 8e4ff10 | 2013-02-18 16:08:34 -0800 | [diff] [blame] | 7 | * Copyright 2009-2012 Canonical Ltd. |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 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 | |
John Johansen | 12557dc | 2017-01-16 00:42:13 -0800 | [diff] [blame] | 23 | #include "include/lib.h" |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 24 | #include "include/match.h" |
| 25 | |
John Johansen | ed686308 | 2013-02-18 16:12:34 -0800 | [diff] [blame] | 26 | #define base_idx(X) ((X) & 0xffffff) |
| 27 | |
John Johansen | 11c236b | 2017-01-16 00:42:42 -0800 | [diff] [blame] | 28 | static char nulldfa_src[] = { |
| 29 | #include "nulldfa.in" |
| 30 | }; |
| 31 | struct aa_dfa *nulldfa; |
| 32 | |
John Johansen | 6e0654d | 2017-09-06 14:57:59 -0700 | [diff] [blame] | 33 | static char stacksplitdfa_src[] = { |
| 34 | #include "stacksplitdfa.in" |
| 35 | }; |
| 36 | struct aa_dfa *stacksplitdfa; |
| 37 | |
John Johansen | 11c236b | 2017-01-16 00:42:42 -0800 | [diff] [blame] | 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)); |
John Johansen | 6e0654d | 2017-09-06 14:57:59 -0700 | [diff] [blame] | 45 | if (IS_ERR(nulldfa)) { |
| 46 | error = PTR_ERR(nulldfa); |
| 47 | nulldfa = NULL; |
| 48 | return error; |
| 49 | } |
John Johansen | 11c236b | 2017-01-16 00:42:42 -0800 | [diff] [blame] | 50 | |
John Johansen | 6e0654d | 2017-09-06 14:57:59 -0700 | [diff] [blame] | 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 | } |
John Johansen | 11c236b | 2017-01-16 00:42:42 -0800 | [diff] [blame] | 62 | |
John Johansen | 6e0654d | 2017-09-06 14:57:59 -0700 | [diff] [blame] | 63 | return 0; |
John Johansen | 11c236b | 2017-01-16 00:42:42 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void aa_teardown_dfa_engine(void) |
| 67 | { |
John Johansen | 6e0654d | 2017-09-06 14:57:59 -0700 | [diff] [blame] | 68 | aa_put_dfa(stacksplitdfa); |
John Johansen | 11c236b | 2017-01-16 00:42:42 -0800 | [diff] [blame] | 69 | aa_put_dfa(nulldfa); |
John Johansen | 11c236b | 2017-01-16 00:42:42 -0800 | [diff] [blame] | 70 | } |
| 71 | |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 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 | * |
John Johansen | 0ca554b | 2013-02-18 16:04:34 -0800 | [diff] [blame] | 79 | * NOTE: must be freed by kvfree (not kfree) |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 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 | */ |
John Johansen | e6e8bf4 | 2017-01-16 00:43:13 -0800 | [diff] [blame] | 93 | th.td_id = be16_to_cpu(*(__be16 *) (blob)) - 1; |
John Johansen | 1575617 | 2016-06-02 02:37:02 -0700 | [diff] [blame] | 94 | if (th.td_id > YYTD_ID_MAX) |
| 95 | goto out; |
John Johansen | e6e8bf4 | 2017-01-16 00:43:13 -0800 | [diff] [blame] | 96 | th.td_flags = be16_to_cpu(*(__be16 *) (blob + 2)); |
| 97 | th.td_lolen = be32_to_cpu(*(__be32 *) (blob + 8)); |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 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 | |
Michal Hocko | a7c3e90 | 2017-05-08 15:57:09 -0700 | [diff] [blame] | 108 | table = kvzalloc(tsize, GFP_KERNEL); |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 109 | if (table) { |
Heinrich Schuchardt | f4ee2de | 2016-06-10 23:34:26 +0200 | [diff] [blame] | 110 | table->td_id = th.td_id; |
| 111 | table->td_flags = th.td_flags; |
| 112 | table->td_lolen = th.td_lolen; |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 113 | if (th.td_flags == YYTD_DATA8) |
| 114 | UNPACK_ARRAY(table->td_data, blob, th.td_lolen, |
John Johansen | e6e8bf4 | 2017-01-16 00:43:13 -0800 | [diff] [blame] | 115 | u8, u8, byte_to_byte); |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 116 | else if (th.td_flags == YYTD_DATA16) |
| 117 | UNPACK_ARRAY(table->td_data, blob, th.td_lolen, |
John Johansen | e6e8bf4 | 2017-01-16 00:43:13 -0800 | [diff] [blame] | 118 | u16, __be16, be16_to_cpu); |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 119 | else if (th.td_flags == YYTD_DATA32) |
| 120 | UNPACK_ARRAY(table->td_data, blob, th.td_lolen, |
John Johansen | e6e8bf4 | 2017-01-16 00:43:13 -0800 | [diff] [blame] | 121 | u32, __be32, be32_to_cpu); |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 122 | else |
| 123 | goto fail; |
John Johansen | 3197f5a | 2016-06-15 09:57:55 +0300 | [diff] [blame] | 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(); |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | out: |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 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; |
John Johansen | ed686308 | 2013-02-18 16:12:34 -0800 | [diff] [blame] | 190 | if (base_idx(BASE_TABLE(dfa)[i]) + 255 >= trans_count) { |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 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 |
Kees Cook | 26fccd9 | 2017-05-13 04:51:45 -0700 | [diff] [blame] | 246 | * format look in Documentation/admin-guide/LSM/apparmor.rst |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 247 | * Assumes the dfa @blob stream has been aligned on a 8 byte boundary |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 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 | |
John Johansen | e6e8bf4 | 2017-01-16 00:43:13 -0800 | [diff] [blame] | 269 | if (ntohl(*(__be32 *) data) != YYTH_MAGIC) |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 270 | goto fail; |
| 271 | |
John Johansen | e6e8bf4 | 2017-01-16 00:43:13 -0800 | [diff] [blame] | 272 | hsize = ntohl(*(__be32 *) (data + 4)); |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 273 | if (size < hsize) |
| 274 | goto fail; |
| 275 | |
John Johansen | e6e8bf4 | 2017-01-16 00:43:13 -0800 | [diff] [blame] | 276 | dfa->flags = ntohs(*(__be16 *) (data + 12)); |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 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 | |
John Johansen | 074c1cd | 2017-08-08 11:58:33 -0700 | [diff] [blame^] | 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 | |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 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); |
John Johansen | 074c1cd | 2017-08-08 11:58:33 -0700 | [diff] [blame^] | 367 | unsigned int state = start; |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 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); |
John Johansen | 074c1cd | 2017-08-08 11:58:33 -0700 | [diff] [blame^] | 376 | for (; len; len--) |
| 377 | match_char(state, def, base, next, check, |
| 378 | equiv[(u8) *str++]); |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 379 | } else { |
| 380 | /* default is direct to next state */ |
John Johansen | 074c1cd | 2017-08-08 11:58:33 -0700 | [diff] [blame^] | 381 | for (; len; len--) |
| 382 | match_char(state, def, base, next, check, (u8) *str++); |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | return state; |
| 386 | } |
| 387 | |
| 388 | /** |
John Johansen | 0fe1212 | 2012-02-16 06:20:26 -0800 | [diff] [blame] | 389 | * aa_dfa_match - traverse @dfa to find state @str stops at |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 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 | * |
John Johansen | 0fe1212 | 2012-02-16 06:20:26 -0800 | [diff] [blame] | 394 | * aa_dfa_match will match @str against the dfa and return the state it |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 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 | { |
John Johansen | 0fe1212 | 2012-02-16 06:20:26 -0800 | [diff] [blame] | 403 | u16 *def = DEFAULT_TABLE(dfa); |
| 404 | u32 *base = BASE_TABLE(dfa); |
| 405 | u16 *next = NEXT_TABLE(dfa); |
| 406 | u16 *check = CHECK_TABLE(dfa); |
John Johansen | 074c1cd | 2017-08-08 11:58:33 -0700 | [diff] [blame^] | 407 | unsigned int state = start; |
John Johansen | 0fe1212 | 2012-02-16 06:20:26 -0800 | [diff] [blame] | 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 */ |
John Johansen | 074c1cd | 2017-08-08 11:58:33 -0700 | [diff] [blame^] | 417 | while (*str) |
| 418 | match_char(state, def, base, next, check, |
| 419 | equiv[(u8) *str++]); |
John Johansen | 0fe1212 | 2012-02-16 06:20:26 -0800 | [diff] [blame] | 420 | } else { |
| 421 | /* default is direct to next state */ |
John Johansen | 074c1cd | 2017-08-08 11:58:33 -0700 | [diff] [blame^] | 422 | while (*str) |
| 423 | match_char(state, def, base, next, check, (u8) *str++); |
John Johansen | 0fe1212 | 2012-02-16 06:20:26 -0800 | [diff] [blame] | 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); |
John Johansen | 0fe1212 | 2012-02-16 06:20:26 -0800 | [diff] [blame] | 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); |
John Johansen | 074c1cd | 2017-08-08 11:58:33 -0700 | [diff] [blame^] | 451 | match_char(state, def, base, next, check, equiv[(u8) c]); |
| 452 | } else |
| 453 | match_char(state, def, base, next, check, (u8) c); |
John Johansen | 0fe1212 | 2012-02-16 06:20:26 -0800 | [diff] [blame] | 454 | |
| 455 | return state; |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 456 | } |
John Johansen | cf65fab | 2017-09-06 02:53:15 -0700 | [diff] [blame] | 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 | } |