1e06f75a6SJohn Johansen /* 2e06f75a6SJohn Johansen * AppArmor security module 3e06f75a6SJohn Johansen * 4e06f75a6SJohn Johansen * This file contains AppArmor dfa based regular expression matching engine 5e06f75a6SJohn Johansen * 6e06f75a6SJohn Johansen * Copyright (C) 1998-2008 Novell/SUSE 78e4ff109SJohn Johansen * Copyright 2009-2012 Canonical Ltd. 8e06f75a6SJohn Johansen * 9e06f75a6SJohn Johansen * This program is free software; you can redistribute it and/or 10e06f75a6SJohn Johansen * modify it under the terms of the GNU General Public License as 11e06f75a6SJohn Johansen * published by the Free Software Foundation, version 2 of the 12e06f75a6SJohn Johansen * License. 13e06f75a6SJohn Johansen */ 14e06f75a6SJohn Johansen 15e06f75a6SJohn Johansen #include <linux/errno.h> 16e06f75a6SJohn Johansen #include <linux/kernel.h> 17e06f75a6SJohn Johansen #include <linux/mm.h> 18e06f75a6SJohn Johansen #include <linux/slab.h> 19e06f75a6SJohn Johansen #include <linux/vmalloc.h> 20e06f75a6SJohn Johansen #include <linux/err.h> 21e06f75a6SJohn Johansen #include <linux/kref.h> 22e06f75a6SJohn Johansen 2312557dcbSJohn Johansen #include "include/lib.h" 24e06f75a6SJohn Johansen #include "include/match.h" 25e06f75a6SJohn Johansen 26ed686308SJohn Johansen #define base_idx(X) ((X) & 0xffffff) 27ed686308SJohn Johansen 2811c236b8SJohn Johansen static char nulldfa_src[] = { 2911c236b8SJohn Johansen #include "nulldfa.in" 3011c236b8SJohn Johansen }; 3111c236b8SJohn Johansen struct aa_dfa *nulldfa; 3211c236b8SJohn Johansen 33*6e0654d2SJohn Johansen static char stacksplitdfa_src[] = { 34*6e0654d2SJohn Johansen #include "stacksplitdfa.in" 35*6e0654d2SJohn Johansen }; 36*6e0654d2SJohn Johansen struct aa_dfa *stacksplitdfa; 37*6e0654d2SJohn Johansen 3811c236b8SJohn Johansen int aa_setup_dfa_engine(void) 3911c236b8SJohn Johansen { 4011c236b8SJohn Johansen int error; 4111c236b8SJohn Johansen 4211c236b8SJohn Johansen nulldfa = aa_dfa_unpack(nulldfa_src, sizeof(nulldfa_src), 4311c236b8SJohn Johansen TO_ACCEPT1_FLAG(YYTD_DATA32) | 4411c236b8SJohn Johansen TO_ACCEPT2_FLAG(YYTD_DATA32)); 45*6e0654d2SJohn Johansen if (IS_ERR(nulldfa)) { 4611c236b8SJohn Johansen error = PTR_ERR(nulldfa); 4711c236b8SJohn Johansen nulldfa = NULL; 4811c236b8SJohn Johansen return error; 4911c236b8SJohn Johansen } 5011c236b8SJohn Johansen 51*6e0654d2SJohn Johansen stacksplitdfa = aa_dfa_unpack(stacksplitdfa_src, 52*6e0654d2SJohn Johansen sizeof(stacksplitdfa_src), 53*6e0654d2SJohn Johansen TO_ACCEPT1_FLAG(YYTD_DATA32) | 54*6e0654d2SJohn Johansen TO_ACCEPT2_FLAG(YYTD_DATA32)); 55*6e0654d2SJohn Johansen if (IS_ERR(stacksplitdfa)) { 56*6e0654d2SJohn Johansen aa_put_dfa(nulldfa); 57*6e0654d2SJohn Johansen nulldfa = NULL; 58*6e0654d2SJohn Johansen error = PTR_ERR(stacksplitdfa); 59*6e0654d2SJohn Johansen stacksplitdfa = NULL; 60*6e0654d2SJohn Johansen return error; 61*6e0654d2SJohn Johansen } 62*6e0654d2SJohn Johansen 63*6e0654d2SJohn Johansen return 0; 64*6e0654d2SJohn Johansen } 65*6e0654d2SJohn Johansen 6611c236b8SJohn Johansen void aa_teardown_dfa_engine(void) 6711c236b8SJohn Johansen { 68*6e0654d2SJohn Johansen aa_put_dfa(stacksplitdfa); 6911c236b8SJohn Johansen aa_put_dfa(nulldfa); 7011c236b8SJohn Johansen } 7111c236b8SJohn Johansen 72e06f75a6SJohn Johansen /** 73e06f75a6SJohn Johansen * unpack_table - unpack a dfa table (one of accept, default, base, next check) 74e06f75a6SJohn Johansen * @blob: data to unpack (NOT NULL) 75e06f75a6SJohn Johansen * @bsize: size of blob 76e06f75a6SJohn Johansen * 77e06f75a6SJohn Johansen * Returns: pointer to table else NULL on failure 78e06f75a6SJohn Johansen * 790ca554b9SJohn Johansen * NOTE: must be freed by kvfree (not kfree) 80e06f75a6SJohn Johansen */ 81e06f75a6SJohn Johansen static struct table_header *unpack_table(char *blob, size_t bsize) 82e06f75a6SJohn Johansen { 83e06f75a6SJohn Johansen struct table_header *table = NULL; 84e06f75a6SJohn Johansen struct table_header th; 85e06f75a6SJohn Johansen size_t tsize; 86e06f75a6SJohn Johansen 87e06f75a6SJohn Johansen if (bsize < sizeof(struct table_header)) 88e06f75a6SJohn Johansen goto out; 89e06f75a6SJohn Johansen 90e06f75a6SJohn Johansen /* loaded td_id's start at 1, subtract 1 now to avoid doing 91e06f75a6SJohn Johansen * it every time we use td_id as an index 92e06f75a6SJohn Johansen */ 93e6e8bf41SJohn Johansen th.td_id = be16_to_cpu(*(__be16 *) (blob)) - 1; 9415756178SJohn Johansen if (th.td_id > YYTD_ID_MAX) 9515756178SJohn Johansen goto out; 96e6e8bf41SJohn Johansen th.td_flags = be16_to_cpu(*(__be16 *) (blob + 2)); 97e6e8bf41SJohn Johansen th.td_lolen = be32_to_cpu(*(__be32 *) (blob + 8)); 98e06f75a6SJohn Johansen blob += sizeof(struct table_header); 99e06f75a6SJohn Johansen 100e06f75a6SJohn Johansen if (!(th.td_flags == YYTD_DATA16 || th.td_flags == YYTD_DATA32 || 101e06f75a6SJohn Johansen th.td_flags == YYTD_DATA8)) 102e06f75a6SJohn Johansen goto out; 103e06f75a6SJohn Johansen 104e06f75a6SJohn Johansen tsize = table_size(th.td_lolen, th.td_flags); 105e06f75a6SJohn Johansen if (bsize < tsize) 106e06f75a6SJohn Johansen goto out; 107e06f75a6SJohn Johansen 108a7c3e901SMichal Hocko table = kvzalloc(tsize, GFP_KERNEL); 109e06f75a6SJohn Johansen if (table) { 110f4ee2defSHeinrich Schuchardt table->td_id = th.td_id; 111f4ee2defSHeinrich Schuchardt table->td_flags = th.td_flags; 112f4ee2defSHeinrich Schuchardt table->td_lolen = th.td_lolen; 113e06f75a6SJohn Johansen if (th.td_flags == YYTD_DATA8) 114e06f75a6SJohn Johansen UNPACK_ARRAY(table->td_data, blob, th.td_lolen, 115e6e8bf41SJohn Johansen u8, u8, byte_to_byte); 116e06f75a6SJohn Johansen else if (th.td_flags == YYTD_DATA16) 117e06f75a6SJohn Johansen UNPACK_ARRAY(table->td_data, blob, th.td_lolen, 118e6e8bf41SJohn Johansen u16, __be16, be16_to_cpu); 119e06f75a6SJohn Johansen else if (th.td_flags == YYTD_DATA32) 120e06f75a6SJohn Johansen UNPACK_ARRAY(table->td_data, blob, th.td_lolen, 121e6e8bf41SJohn Johansen u32, __be32, be32_to_cpu); 122e06f75a6SJohn Johansen else 123e06f75a6SJohn Johansen goto fail; 124e06f75a6SJohn Johansen /* if table was vmalloced make sure the page tables are synced 125e06f75a6SJohn Johansen * before it is used, as it goes live to all cpus. 126e06f75a6SJohn Johansen */ 127e06f75a6SJohn Johansen if (is_vmalloc_addr(table)) 128e06f75a6SJohn Johansen vm_unmap_aliases(); 1293197f5adSJohn Johansen } 1303197f5adSJohn Johansen 1313197f5adSJohn Johansen out: 132e06f75a6SJohn Johansen return table; 133e06f75a6SJohn Johansen fail: 134e06f75a6SJohn Johansen kvfree(table); 135e06f75a6SJohn Johansen return NULL; 136e06f75a6SJohn Johansen } 137e06f75a6SJohn Johansen 138e06f75a6SJohn Johansen /** 139e06f75a6SJohn Johansen * verify_dfa - verify that transitions and states in the tables are in bounds. 140e06f75a6SJohn Johansen * @dfa: dfa to test (NOT NULL) 141e06f75a6SJohn Johansen * @flags: flags controlling what type of accept table are acceptable 142e06f75a6SJohn Johansen * 143e06f75a6SJohn Johansen * Assumes dfa has gone through the first pass verification done by unpacking 144e06f75a6SJohn Johansen * NOTE: this does not valid accept table values 145e06f75a6SJohn Johansen * 146e06f75a6SJohn Johansen * Returns: %0 else error code on failure to verify 147e06f75a6SJohn Johansen */ 148e06f75a6SJohn Johansen static int verify_dfa(struct aa_dfa *dfa, int flags) 149e06f75a6SJohn Johansen { 150e06f75a6SJohn Johansen size_t i, state_count, trans_count; 151e06f75a6SJohn Johansen int error = -EPROTO; 152e06f75a6SJohn Johansen 153e06f75a6SJohn Johansen /* check that required tables exist */ 154e06f75a6SJohn Johansen if (!(dfa->tables[YYTD_ID_DEF] && 155e06f75a6SJohn Johansen dfa->tables[YYTD_ID_BASE] && 156e06f75a6SJohn Johansen dfa->tables[YYTD_ID_NXT] && dfa->tables[YYTD_ID_CHK])) 157e06f75a6SJohn Johansen goto out; 158e06f75a6SJohn Johansen 159e06f75a6SJohn Johansen /* accept.size == default.size == base.size */ 160e06f75a6SJohn Johansen state_count = dfa->tables[YYTD_ID_BASE]->td_lolen; 161e06f75a6SJohn Johansen if (ACCEPT1_FLAGS(flags)) { 162e06f75a6SJohn Johansen if (!dfa->tables[YYTD_ID_ACCEPT]) 163e06f75a6SJohn Johansen goto out; 164e06f75a6SJohn Johansen if (state_count != dfa->tables[YYTD_ID_ACCEPT]->td_lolen) 165e06f75a6SJohn Johansen goto out; 166e06f75a6SJohn Johansen } 167e06f75a6SJohn Johansen if (ACCEPT2_FLAGS(flags)) { 168e06f75a6SJohn Johansen if (!dfa->tables[YYTD_ID_ACCEPT2]) 169e06f75a6SJohn Johansen goto out; 170e06f75a6SJohn Johansen if (state_count != dfa->tables[YYTD_ID_ACCEPT2]->td_lolen) 171e06f75a6SJohn Johansen goto out; 172e06f75a6SJohn Johansen } 173e06f75a6SJohn Johansen if (state_count != dfa->tables[YYTD_ID_DEF]->td_lolen) 174e06f75a6SJohn Johansen goto out; 175e06f75a6SJohn Johansen 176e06f75a6SJohn Johansen /* next.size == chk.size */ 177e06f75a6SJohn Johansen trans_count = dfa->tables[YYTD_ID_NXT]->td_lolen; 178e06f75a6SJohn Johansen if (trans_count != dfa->tables[YYTD_ID_CHK]->td_lolen) 179e06f75a6SJohn Johansen goto out; 180e06f75a6SJohn Johansen 181e06f75a6SJohn Johansen /* if equivalence classes then its table size must be 256 */ 182e06f75a6SJohn Johansen if (dfa->tables[YYTD_ID_EC] && 183e06f75a6SJohn Johansen dfa->tables[YYTD_ID_EC]->td_lolen != 256) 184e06f75a6SJohn Johansen goto out; 185e06f75a6SJohn Johansen 186e06f75a6SJohn Johansen if (flags & DFA_FLAG_VERIFY_STATES) { 187e06f75a6SJohn Johansen for (i = 0; i < state_count; i++) { 188e06f75a6SJohn Johansen if (DEFAULT_TABLE(dfa)[i] >= state_count) 189e06f75a6SJohn Johansen goto out; 190ed686308SJohn Johansen if (base_idx(BASE_TABLE(dfa)[i]) + 255 >= trans_count) { 191e06f75a6SJohn Johansen printk(KERN_ERR "AppArmor DFA next/check upper " 192e06f75a6SJohn Johansen "bounds error\n"); 193e06f75a6SJohn Johansen goto out; 194e06f75a6SJohn Johansen } 195e06f75a6SJohn Johansen } 196e06f75a6SJohn Johansen 197e06f75a6SJohn Johansen for (i = 0; i < trans_count; i++) { 198e06f75a6SJohn Johansen if (NEXT_TABLE(dfa)[i] >= state_count) 199e06f75a6SJohn Johansen goto out; 200e06f75a6SJohn Johansen if (CHECK_TABLE(dfa)[i] >= state_count) 201e06f75a6SJohn Johansen goto out; 202e06f75a6SJohn Johansen } 203e06f75a6SJohn Johansen } 204e06f75a6SJohn Johansen 205e06f75a6SJohn Johansen error = 0; 206e06f75a6SJohn Johansen out: 207e06f75a6SJohn Johansen return error; 208e06f75a6SJohn Johansen } 209e06f75a6SJohn Johansen 210e06f75a6SJohn Johansen /** 211e06f75a6SJohn Johansen * dfa_free - free a dfa allocated by aa_dfa_unpack 212e06f75a6SJohn Johansen * @dfa: the dfa to free (MAYBE NULL) 213e06f75a6SJohn Johansen * 214e06f75a6SJohn Johansen * Requires: reference count to dfa == 0 215e06f75a6SJohn Johansen */ 216e06f75a6SJohn Johansen static void dfa_free(struct aa_dfa *dfa) 217e06f75a6SJohn Johansen { 218e06f75a6SJohn Johansen if (dfa) { 219e06f75a6SJohn Johansen int i; 220e06f75a6SJohn Johansen 221e06f75a6SJohn Johansen for (i = 0; i < ARRAY_SIZE(dfa->tables); i++) { 222e06f75a6SJohn Johansen kvfree(dfa->tables[i]); 223e06f75a6SJohn Johansen dfa->tables[i] = NULL; 224e06f75a6SJohn Johansen } 225e06f75a6SJohn Johansen kfree(dfa); 226e06f75a6SJohn Johansen } 227e06f75a6SJohn Johansen } 228e06f75a6SJohn Johansen 229e06f75a6SJohn Johansen /** 230e06f75a6SJohn Johansen * aa_dfa_free_kref - free aa_dfa by kref (called by aa_put_dfa) 231e06f75a6SJohn Johansen * @kr: kref callback for freeing of a dfa (NOT NULL) 232e06f75a6SJohn Johansen */ 233e06f75a6SJohn Johansen void aa_dfa_free_kref(struct kref *kref) 234e06f75a6SJohn Johansen { 235e06f75a6SJohn Johansen struct aa_dfa *dfa = container_of(kref, struct aa_dfa, count); 236e06f75a6SJohn Johansen dfa_free(dfa); 237e06f75a6SJohn Johansen } 238e06f75a6SJohn Johansen 239e06f75a6SJohn Johansen /** 240e06f75a6SJohn Johansen * aa_dfa_unpack - unpack the binary tables of a serialized dfa 241e06f75a6SJohn Johansen * @blob: aligned serialized stream of data to unpack (NOT NULL) 242e06f75a6SJohn Johansen * @size: size of data to unpack 243e06f75a6SJohn Johansen * @flags: flags controlling what type of accept tables are acceptable 244e06f75a6SJohn Johansen * 245e06f75a6SJohn Johansen * Unpack a dfa that has been serialized. To find information on the dfa 24626fccd9eSKees Cook * format look in Documentation/admin-guide/LSM/apparmor.rst 24725985edcSLucas De Marchi * Assumes the dfa @blob stream has been aligned on a 8 byte boundary 248e06f75a6SJohn Johansen * 249e06f75a6SJohn Johansen * Returns: an unpacked dfa ready for matching or ERR_PTR on failure 250e06f75a6SJohn Johansen */ 251e06f75a6SJohn Johansen struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags) 252e06f75a6SJohn Johansen { 253e06f75a6SJohn Johansen int hsize; 254e06f75a6SJohn Johansen int error = -ENOMEM; 255e06f75a6SJohn Johansen char *data = blob; 256e06f75a6SJohn Johansen struct table_header *table = NULL; 257e06f75a6SJohn Johansen struct aa_dfa *dfa = kzalloc(sizeof(struct aa_dfa), GFP_KERNEL); 258e06f75a6SJohn Johansen if (!dfa) 259e06f75a6SJohn Johansen goto fail; 260e06f75a6SJohn Johansen 261e06f75a6SJohn Johansen kref_init(&dfa->count); 262e06f75a6SJohn Johansen 263e06f75a6SJohn Johansen error = -EPROTO; 264e06f75a6SJohn Johansen 265e06f75a6SJohn Johansen /* get dfa table set header */ 266e06f75a6SJohn Johansen if (size < sizeof(struct table_set_header)) 267e06f75a6SJohn Johansen goto fail; 268e06f75a6SJohn Johansen 269e6e8bf41SJohn Johansen if (ntohl(*(__be32 *) data) != YYTH_MAGIC) 270e06f75a6SJohn Johansen goto fail; 271e06f75a6SJohn Johansen 272e6e8bf41SJohn Johansen hsize = ntohl(*(__be32 *) (data + 4)); 273e06f75a6SJohn Johansen if (size < hsize) 274e06f75a6SJohn Johansen goto fail; 275e06f75a6SJohn Johansen 276e6e8bf41SJohn Johansen dfa->flags = ntohs(*(__be16 *) (data + 12)); 277e06f75a6SJohn Johansen data += hsize; 278e06f75a6SJohn Johansen size -= hsize; 279e06f75a6SJohn Johansen 280e06f75a6SJohn Johansen while (size > 0) { 281e06f75a6SJohn Johansen table = unpack_table(data, size); 282e06f75a6SJohn Johansen if (!table) 283e06f75a6SJohn Johansen goto fail; 284e06f75a6SJohn Johansen 285e06f75a6SJohn Johansen switch (table->td_id) { 286e06f75a6SJohn Johansen case YYTD_ID_ACCEPT: 287e06f75a6SJohn Johansen if (!(table->td_flags & ACCEPT1_FLAGS(flags))) 288e06f75a6SJohn Johansen goto fail; 289e06f75a6SJohn Johansen break; 290e06f75a6SJohn Johansen case YYTD_ID_ACCEPT2: 291e06f75a6SJohn Johansen if (!(table->td_flags & ACCEPT2_FLAGS(flags))) 292e06f75a6SJohn Johansen goto fail; 293e06f75a6SJohn Johansen break; 294e06f75a6SJohn Johansen case YYTD_ID_BASE: 295e06f75a6SJohn Johansen if (table->td_flags != YYTD_DATA32) 296e06f75a6SJohn Johansen goto fail; 297e06f75a6SJohn Johansen break; 298e06f75a6SJohn Johansen case YYTD_ID_DEF: 299e06f75a6SJohn Johansen case YYTD_ID_NXT: 300e06f75a6SJohn Johansen case YYTD_ID_CHK: 301e06f75a6SJohn Johansen if (table->td_flags != YYTD_DATA16) 302e06f75a6SJohn Johansen goto fail; 303e06f75a6SJohn Johansen break; 304e06f75a6SJohn Johansen case YYTD_ID_EC: 305e06f75a6SJohn Johansen if (table->td_flags != YYTD_DATA8) 306e06f75a6SJohn Johansen goto fail; 307e06f75a6SJohn Johansen break; 308e06f75a6SJohn Johansen default: 309e06f75a6SJohn Johansen goto fail; 310e06f75a6SJohn Johansen } 311e06f75a6SJohn Johansen /* check for duplicate table entry */ 312e06f75a6SJohn Johansen if (dfa->tables[table->td_id]) 313e06f75a6SJohn Johansen goto fail; 314e06f75a6SJohn Johansen dfa->tables[table->td_id] = table; 315e06f75a6SJohn Johansen data += table_size(table->td_lolen, table->td_flags); 316e06f75a6SJohn Johansen size -= table_size(table->td_lolen, table->td_flags); 317e06f75a6SJohn Johansen table = NULL; 318e06f75a6SJohn Johansen } 319e06f75a6SJohn Johansen 320e06f75a6SJohn Johansen error = verify_dfa(dfa, flags); 321e06f75a6SJohn Johansen if (error) 322e06f75a6SJohn Johansen goto fail; 323e06f75a6SJohn Johansen 324e06f75a6SJohn Johansen return dfa; 325e06f75a6SJohn Johansen 326e06f75a6SJohn Johansen fail: 327e06f75a6SJohn Johansen kvfree(table); 328e06f75a6SJohn Johansen dfa_free(dfa); 329e06f75a6SJohn Johansen return ERR_PTR(error); 330e06f75a6SJohn Johansen } 331e06f75a6SJohn Johansen 332e06f75a6SJohn Johansen /** 333e06f75a6SJohn Johansen * aa_dfa_match_len - traverse @dfa to find state @str stops at 334e06f75a6SJohn Johansen * @dfa: the dfa to match @str against (NOT NULL) 335e06f75a6SJohn Johansen * @start: the state of the dfa to start matching in 336e06f75a6SJohn Johansen * @str: the string of bytes to match against the dfa (NOT NULL) 337e06f75a6SJohn Johansen * @len: length of the string of bytes to match 338e06f75a6SJohn Johansen * 339e06f75a6SJohn Johansen * aa_dfa_match_len will match @str against the dfa and return the state it 340e06f75a6SJohn Johansen * finished matching in. The final state can be used to look up the accepting 341e06f75a6SJohn Johansen * label, or as the start state of a continuing match. 342e06f75a6SJohn Johansen * 343e06f75a6SJohn Johansen * This function will happily match again the 0 byte and only finishes 344e06f75a6SJohn Johansen * when @len input is consumed. 345e06f75a6SJohn Johansen * 346e06f75a6SJohn Johansen * Returns: final state reached after input is consumed 347e06f75a6SJohn Johansen */ 348e06f75a6SJohn Johansen unsigned int aa_dfa_match_len(struct aa_dfa *dfa, unsigned int start, 349e06f75a6SJohn Johansen const char *str, int len) 350e06f75a6SJohn Johansen { 351e06f75a6SJohn Johansen u16 *def = DEFAULT_TABLE(dfa); 352e06f75a6SJohn Johansen u32 *base = BASE_TABLE(dfa); 353e06f75a6SJohn Johansen u16 *next = NEXT_TABLE(dfa); 354e06f75a6SJohn Johansen u16 *check = CHECK_TABLE(dfa); 355e06f75a6SJohn Johansen unsigned int state = start, pos; 356e06f75a6SJohn Johansen 357e06f75a6SJohn Johansen if (state == 0) 358e06f75a6SJohn Johansen return 0; 359e06f75a6SJohn Johansen 360e06f75a6SJohn Johansen /* current state is <state>, matching character *str */ 361e06f75a6SJohn Johansen if (dfa->tables[YYTD_ID_EC]) { 362e06f75a6SJohn Johansen /* Equivalence class table defined */ 363e06f75a6SJohn Johansen u8 *equiv = EQUIV_TABLE(dfa); 364e06f75a6SJohn Johansen /* default is direct to next state */ 365e06f75a6SJohn Johansen for (; len; len--) { 366ed686308SJohn Johansen pos = base_idx(base[state]) + equiv[(u8) *str++]; 367e06f75a6SJohn Johansen if (check[pos] == state) 368e06f75a6SJohn Johansen state = next[pos]; 369e06f75a6SJohn Johansen else 370e06f75a6SJohn Johansen state = def[state]; 371e06f75a6SJohn Johansen } 372e06f75a6SJohn Johansen } else { 373e06f75a6SJohn Johansen /* default is direct to next state */ 374e06f75a6SJohn Johansen for (; len; len--) { 375ed686308SJohn Johansen pos = base_idx(base[state]) + (u8) *str++; 376e06f75a6SJohn Johansen if (check[pos] == state) 377e06f75a6SJohn Johansen state = next[pos]; 378e06f75a6SJohn Johansen else 379e06f75a6SJohn Johansen state = def[state]; 380e06f75a6SJohn Johansen } 381e06f75a6SJohn Johansen } 382e06f75a6SJohn Johansen 383e06f75a6SJohn Johansen return state; 384e06f75a6SJohn Johansen } 385e06f75a6SJohn Johansen 386e06f75a6SJohn Johansen /** 3870fe1212dSJohn Johansen * aa_dfa_match - traverse @dfa to find state @str stops at 388e06f75a6SJohn Johansen * @dfa: the dfa to match @str against (NOT NULL) 389e06f75a6SJohn Johansen * @start: the state of the dfa to start matching in 390e06f75a6SJohn Johansen * @str: the null terminated string of bytes to match against the dfa (NOT NULL) 391e06f75a6SJohn Johansen * 3920fe1212dSJohn Johansen * aa_dfa_match will match @str against the dfa and return the state it 393e06f75a6SJohn Johansen * finished matching in. The final state can be used to look up the accepting 394e06f75a6SJohn Johansen * label, or as the start state of a continuing match. 395e06f75a6SJohn Johansen * 396e06f75a6SJohn Johansen * Returns: final state reached after input is consumed 397e06f75a6SJohn Johansen */ 398e06f75a6SJohn Johansen unsigned int aa_dfa_match(struct aa_dfa *dfa, unsigned int start, 399e06f75a6SJohn Johansen const char *str) 400e06f75a6SJohn Johansen { 4010fe1212dSJohn Johansen u16 *def = DEFAULT_TABLE(dfa); 4020fe1212dSJohn Johansen u32 *base = BASE_TABLE(dfa); 4030fe1212dSJohn Johansen u16 *next = NEXT_TABLE(dfa); 4040fe1212dSJohn Johansen u16 *check = CHECK_TABLE(dfa); 4050fe1212dSJohn Johansen unsigned int state = start, pos; 4060fe1212dSJohn Johansen 4070fe1212dSJohn Johansen if (state == 0) 4080fe1212dSJohn Johansen return 0; 4090fe1212dSJohn Johansen 4100fe1212dSJohn Johansen /* current state is <state>, matching character *str */ 4110fe1212dSJohn Johansen if (dfa->tables[YYTD_ID_EC]) { 4120fe1212dSJohn Johansen /* Equivalence class table defined */ 4130fe1212dSJohn Johansen u8 *equiv = EQUIV_TABLE(dfa); 4140fe1212dSJohn Johansen /* default is direct to next state */ 4150fe1212dSJohn Johansen while (*str) { 416ed686308SJohn Johansen pos = base_idx(base[state]) + equiv[(u8) *str++]; 4170fe1212dSJohn Johansen if (check[pos] == state) 4180fe1212dSJohn Johansen state = next[pos]; 4190fe1212dSJohn Johansen else 4200fe1212dSJohn Johansen state = def[state]; 4210fe1212dSJohn Johansen } 4220fe1212dSJohn Johansen } else { 4230fe1212dSJohn Johansen /* default is direct to next state */ 4240fe1212dSJohn Johansen while (*str) { 425ed686308SJohn Johansen pos = base_idx(base[state]) + (u8) *str++; 4260fe1212dSJohn Johansen if (check[pos] == state) 4270fe1212dSJohn Johansen state = next[pos]; 4280fe1212dSJohn Johansen else 4290fe1212dSJohn Johansen state = def[state]; 4300fe1212dSJohn Johansen } 4310fe1212dSJohn Johansen } 4320fe1212dSJohn Johansen 4330fe1212dSJohn Johansen return state; 4340fe1212dSJohn Johansen } 4350fe1212dSJohn Johansen 4360fe1212dSJohn Johansen /** 4370fe1212dSJohn Johansen * aa_dfa_next - step one character to the next state in the dfa 4380fe1212dSJohn Johansen * @dfa: the dfa to tranverse (NOT NULL) 4390fe1212dSJohn Johansen * @state: the state to start in 4400fe1212dSJohn Johansen * @c: the input character to transition on 4410fe1212dSJohn Johansen * 4420fe1212dSJohn Johansen * aa_dfa_match will step through the dfa by one input character @c 4430fe1212dSJohn Johansen * 4440fe1212dSJohn Johansen * Returns: state reach after input @c 4450fe1212dSJohn Johansen */ 4460fe1212dSJohn Johansen unsigned int aa_dfa_next(struct aa_dfa *dfa, unsigned int state, 4470fe1212dSJohn Johansen const char c) 4480fe1212dSJohn Johansen { 4490fe1212dSJohn Johansen u16 *def = DEFAULT_TABLE(dfa); 4500fe1212dSJohn Johansen u32 *base = BASE_TABLE(dfa); 4510fe1212dSJohn Johansen u16 *next = NEXT_TABLE(dfa); 4520fe1212dSJohn Johansen u16 *check = CHECK_TABLE(dfa); 4530fe1212dSJohn Johansen unsigned int pos; 4540fe1212dSJohn Johansen 4550fe1212dSJohn Johansen /* current state is <state>, matching character *str */ 4560fe1212dSJohn Johansen if (dfa->tables[YYTD_ID_EC]) { 4570fe1212dSJohn Johansen /* Equivalence class table defined */ 4580fe1212dSJohn Johansen u8 *equiv = EQUIV_TABLE(dfa); 4590fe1212dSJohn Johansen /* default is direct to next state */ 4600fe1212dSJohn Johansen 461ed686308SJohn Johansen pos = base_idx(base[state]) + equiv[(u8) c]; 4620fe1212dSJohn Johansen if (check[pos] == state) 4630fe1212dSJohn Johansen state = next[pos]; 4640fe1212dSJohn Johansen else 4650fe1212dSJohn Johansen state = def[state]; 4660fe1212dSJohn Johansen } else { 4670fe1212dSJohn Johansen /* default is direct to next state */ 468ed686308SJohn Johansen pos = base_idx(base[state]) + (u8) c; 4690fe1212dSJohn Johansen if (check[pos] == state) 4700fe1212dSJohn Johansen state = next[pos]; 4710fe1212dSJohn Johansen else 4720fe1212dSJohn Johansen state = def[state]; 4730fe1212dSJohn Johansen } 4740fe1212dSJohn Johansen 4750fe1212dSJohn Johansen return state; 476e06f75a6SJohn Johansen } 477cf65fabcSJohn Johansen 478cf65fabcSJohn Johansen /** 479cf65fabcSJohn Johansen * aa_dfa_match_until - traverse @dfa until accept state or end of input 480cf65fabcSJohn Johansen * @dfa: the dfa to match @str against (NOT NULL) 481cf65fabcSJohn Johansen * @start: the state of the dfa to start matching in 482cf65fabcSJohn Johansen * @str: the null terminated string of bytes to match against the dfa (NOT NULL) 483cf65fabcSJohn Johansen * @retpos: first character in str after match OR end of string 484cf65fabcSJohn Johansen * 485cf65fabcSJohn Johansen * aa_dfa_match will match @str against the dfa and return the state it 486cf65fabcSJohn Johansen * finished matching in. The final state can be used to look up the accepting 487cf65fabcSJohn Johansen * label, or as the start state of a continuing match. 488cf65fabcSJohn Johansen * 489cf65fabcSJohn Johansen * Returns: final state reached after input is consumed 490cf65fabcSJohn Johansen */ 491cf65fabcSJohn Johansen unsigned int aa_dfa_match_until(struct aa_dfa *dfa, unsigned int start, 492cf65fabcSJohn Johansen const char *str, const char **retpos) 493cf65fabcSJohn Johansen { 494cf65fabcSJohn Johansen u16 *def = DEFAULT_TABLE(dfa); 495cf65fabcSJohn Johansen u32 *base = BASE_TABLE(dfa); 496cf65fabcSJohn Johansen u16 *next = NEXT_TABLE(dfa); 497cf65fabcSJohn Johansen u16 *check = CHECK_TABLE(dfa); 498cf65fabcSJohn Johansen u32 *accept = ACCEPT_TABLE(dfa); 499cf65fabcSJohn Johansen unsigned int state = start, pos; 500cf65fabcSJohn Johansen 501cf65fabcSJohn Johansen if (state == 0) 502cf65fabcSJohn Johansen return 0; 503cf65fabcSJohn Johansen 504cf65fabcSJohn Johansen /* current state is <state>, matching character *str */ 505cf65fabcSJohn Johansen if (dfa->tables[YYTD_ID_EC]) { 506cf65fabcSJohn Johansen /* Equivalence class table defined */ 507cf65fabcSJohn Johansen u8 *equiv = EQUIV_TABLE(dfa); 508cf65fabcSJohn Johansen /* default is direct to next state */ 509cf65fabcSJohn Johansen while (*str) { 510cf65fabcSJohn Johansen pos = base_idx(base[state]) + equiv[(u8) *str++]; 511cf65fabcSJohn Johansen if (check[pos] == state) 512cf65fabcSJohn Johansen state = next[pos]; 513cf65fabcSJohn Johansen else 514cf65fabcSJohn Johansen state = def[state]; 515cf65fabcSJohn Johansen if (accept[state]) 516cf65fabcSJohn Johansen break; 517cf65fabcSJohn Johansen } 518cf65fabcSJohn Johansen } else { 519cf65fabcSJohn Johansen /* default is direct to next state */ 520cf65fabcSJohn Johansen while (*str) { 521cf65fabcSJohn Johansen pos = base_idx(base[state]) + (u8) *str++; 522cf65fabcSJohn Johansen if (check[pos] == state) 523cf65fabcSJohn Johansen state = next[pos]; 524cf65fabcSJohn Johansen else 525cf65fabcSJohn Johansen state = def[state]; 526cf65fabcSJohn Johansen if (accept[state]) 527cf65fabcSJohn Johansen break; 528cf65fabcSJohn Johansen } 529cf65fabcSJohn Johansen } 530cf65fabcSJohn Johansen 531cf65fabcSJohn Johansen *retpos = str; 532cf65fabcSJohn Johansen return state; 533cf65fabcSJohn Johansen } 534cf65fabcSJohn Johansen 535cf65fabcSJohn Johansen 536cf65fabcSJohn Johansen /** 537cf65fabcSJohn Johansen * aa_dfa_matchn_until - traverse @dfa until accept or @n bytes consumed 538cf65fabcSJohn Johansen * @dfa: the dfa to match @str against (NOT NULL) 539cf65fabcSJohn Johansen * @start: the state of the dfa to start matching in 540cf65fabcSJohn Johansen * @str: the string of bytes to match against the dfa (NOT NULL) 541cf65fabcSJohn Johansen * @n: length of the string of bytes to match 542cf65fabcSJohn Johansen * @retpos: first character in str after match OR str + n 543cf65fabcSJohn Johansen * 544cf65fabcSJohn Johansen * aa_dfa_match_len will match @str against the dfa and return the state it 545cf65fabcSJohn Johansen * finished matching in. The final state can be used to look up the accepting 546cf65fabcSJohn Johansen * label, or as the start state of a continuing match. 547cf65fabcSJohn Johansen * 548cf65fabcSJohn Johansen * This function will happily match again the 0 byte and only finishes 549cf65fabcSJohn Johansen * when @n input is consumed. 550cf65fabcSJohn Johansen * 551cf65fabcSJohn Johansen * Returns: final state reached after input is consumed 552cf65fabcSJohn Johansen */ 553cf65fabcSJohn Johansen unsigned int aa_dfa_matchn_until(struct aa_dfa *dfa, unsigned int start, 554cf65fabcSJohn Johansen const char *str, int n, const char **retpos) 555cf65fabcSJohn Johansen { 556cf65fabcSJohn Johansen u16 *def = DEFAULT_TABLE(dfa); 557cf65fabcSJohn Johansen u32 *base = BASE_TABLE(dfa); 558cf65fabcSJohn Johansen u16 *next = NEXT_TABLE(dfa); 559cf65fabcSJohn Johansen u16 *check = CHECK_TABLE(dfa); 560cf65fabcSJohn Johansen u32 *accept = ACCEPT_TABLE(dfa); 561cf65fabcSJohn Johansen unsigned int state = start, pos; 562cf65fabcSJohn Johansen 563cf65fabcSJohn Johansen *retpos = NULL; 564cf65fabcSJohn Johansen if (state == 0) 565cf65fabcSJohn Johansen return 0; 566cf65fabcSJohn Johansen 567cf65fabcSJohn Johansen /* current state is <state>, matching character *str */ 568cf65fabcSJohn Johansen if (dfa->tables[YYTD_ID_EC]) { 569cf65fabcSJohn Johansen /* Equivalence class table defined */ 570cf65fabcSJohn Johansen u8 *equiv = EQUIV_TABLE(dfa); 571cf65fabcSJohn Johansen /* default is direct to next state */ 572cf65fabcSJohn Johansen for (; n; n--) { 573cf65fabcSJohn Johansen pos = base_idx(base[state]) + equiv[(u8) *str++]; 574cf65fabcSJohn Johansen if (check[pos] == state) 575cf65fabcSJohn Johansen state = next[pos]; 576cf65fabcSJohn Johansen else 577cf65fabcSJohn Johansen state = def[state]; 578cf65fabcSJohn Johansen if (accept[state]) 579cf65fabcSJohn Johansen break; 580cf65fabcSJohn Johansen } 581cf65fabcSJohn Johansen } else { 582cf65fabcSJohn Johansen /* default is direct to next state */ 583cf65fabcSJohn Johansen for (; n; n--) { 584cf65fabcSJohn Johansen pos = base_idx(base[state]) + (u8) *str++; 585cf65fabcSJohn Johansen if (check[pos] == state) 586cf65fabcSJohn Johansen state = next[pos]; 587cf65fabcSJohn Johansen else 588cf65fabcSJohn Johansen state = def[state]; 589cf65fabcSJohn Johansen if (accept[state]) 590cf65fabcSJohn Johansen break; 591cf65fabcSJohn Johansen } 592cf65fabcSJohn Johansen } 593cf65fabcSJohn Johansen 594cf65fabcSJohn Johansen *retpos = str; 595cf65fabcSJohn Johansen return state; 596cf65fabcSJohn Johansen } 597