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 23e06f75a6SJohn Johansen #include "include/apparmor.h" 24e06f75a6SJohn Johansen #include "include/match.h" 25e06f75a6SJohn Johansen 26ed686308SJohn Johansen #define base_idx(X) ((X) & 0xffffff) 27ed686308SJohn Johansen 28e06f75a6SJohn Johansen /** 29e06f75a6SJohn Johansen * unpack_table - unpack a dfa table (one of accept, default, base, next check) 30e06f75a6SJohn Johansen * @blob: data to unpack (NOT NULL) 31e06f75a6SJohn Johansen * @bsize: size of blob 32e06f75a6SJohn Johansen * 33e06f75a6SJohn Johansen * Returns: pointer to table else NULL on failure 34e06f75a6SJohn Johansen * 350ca554b9SJohn Johansen * NOTE: must be freed by kvfree (not kfree) 36e06f75a6SJohn Johansen */ 37e06f75a6SJohn Johansen static struct table_header *unpack_table(char *blob, size_t bsize) 38e06f75a6SJohn Johansen { 39e06f75a6SJohn Johansen struct table_header *table = NULL; 40e06f75a6SJohn Johansen struct table_header th; 41e06f75a6SJohn Johansen size_t tsize; 42e06f75a6SJohn Johansen 43e06f75a6SJohn Johansen if (bsize < sizeof(struct table_header)) 44e06f75a6SJohn Johansen goto out; 45e06f75a6SJohn Johansen 46e06f75a6SJohn Johansen /* loaded td_id's start at 1, subtract 1 now to avoid doing 47e06f75a6SJohn Johansen * it every time we use td_id as an index 48e06f75a6SJohn Johansen */ 49e06f75a6SJohn Johansen th.td_id = be16_to_cpu(*(u16 *) (blob)) - 1; 50*15756178SJohn Johansen if (th.td_id > YYTD_ID_MAX) 51*15756178SJohn Johansen goto out; 52e06f75a6SJohn Johansen th.td_flags = be16_to_cpu(*(u16 *) (blob + 2)); 53e06f75a6SJohn Johansen th.td_lolen = be32_to_cpu(*(u32 *) (blob + 8)); 54e06f75a6SJohn Johansen blob += sizeof(struct table_header); 55e06f75a6SJohn Johansen 56e06f75a6SJohn Johansen if (!(th.td_flags == YYTD_DATA16 || th.td_flags == YYTD_DATA32 || 57e06f75a6SJohn Johansen th.td_flags == YYTD_DATA8)) 58e06f75a6SJohn Johansen goto out; 59e06f75a6SJohn Johansen 60e06f75a6SJohn Johansen tsize = table_size(th.td_lolen, th.td_flags); 61e06f75a6SJohn Johansen if (bsize < tsize) 62e06f75a6SJohn Johansen goto out; 63e06f75a6SJohn Johansen 640ca554b9SJohn Johansen table = kvzalloc(tsize); 65e06f75a6SJohn Johansen if (table) { 66e06f75a6SJohn Johansen *table = th; 67e06f75a6SJohn Johansen if (th.td_flags == YYTD_DATA8) 68e06f75a6SJohn Johansen UNPACK_ARRAY(table->td_data, blob, th.td_lolen, 69e06f75a6SJohn Johansen u8, byte_to_byte); 70e06f75a6SJohn Johansen else if (th.td_flags == YYTD_DATA16) 71e06f75a6SJohn Johansen UNPACK_ARRAY(table->td_data, blob, th.td_lolen, 72e06f75a6SJohn Johansen u16, be16_to_cpu); 73e06f75a6SJohn Johansen else if (th.td_flags == YYTD_DATA32) 74e06f75a6SJohn Johansen UNPACK_ARRAY(table->td_data, blob, th.td_lolen, 75e06f75a6SJohn Johansen u32, be32_to_cpu); 76e06f75a6SJohn Johansen else 77e06f75a6SJohn Johansen goto fail; 78e06f75a6SJohn Johansen } 79e06f75a6SJohn Johansen 80e06f75a6SJohn Johansen out: 81e06f75a6SJohn Johansen /* if table was vmalloced make sure the page tables are synced 82e06f75a6SJohn Johansen * before it is used, as it goes live to all cpus. 83e06f75a6SJohn Johansen */ 84e06f75a6SJohn Johansen if (is_vmalloc_addr(table)) 85e06f75a6SJohn Johansen vm_unmap_aliases(); 86e06f75a6SJohn Johansen return table; 87e06f75a6SJohn Johansen fail: 88e06f75a6SJohn Johansen kvfree(table); 89e06f75a6SJohn Johansen return NULL; 90e06f75a6SJohn Johansen } 91e06f75a6SJohn Johansen 92e06f75a6SJohn Johansen /** 93e06f75a6SJohn Johansen * verify_dfa - verify that transitions and states in the tables are in bounds. 94e06f75a6SJohn Johansen * @dfa: dfa to test (NOT NULL) 95e06f75a6SJohn Johansen * @flags: flags controlling what type of accept table are acceptable 96e06f75a6SJohn Johansen * 97e06f75a6SJohn Johansen * Assumes dfa has gone through the first pass verification done by unpacking 98e06f75a6SJohn Johansen * NOTE: this does not valid accept table values 99e06f75a6SJohn Johansen * 100e06f75a6SJohn Johansen * Returns: %0 else error code on failure to verify 101e06f75a6SJohn Johansen */ 102e06f75a6SJohn Johansen static int verify_dfa(struct aa_dfa *dfa, int flags) 103e06f75a6SJohn Johansen { 104e06f75a6SJohn Johansen size_t i, state_count, trans_count; 105e06f75a6SJohn Johansen int error = -EPROTO; 106e06f75a6SJohn Johansen 107e06f75a6SJohn Johansen /* check that required tables exist */ 108e06f75a6SJohn Johansen if (!(dfa->tables[YYTD_ID_DEF] && 109e06f75a6SJohn Johansen dfa->tables[YYTD_ID_BASE] && 110e06f75a6SJohn Johansen dfa->tables[YYTD_ID_NXT] && dfa->tables[YYTD_ID_CHK])) 111e06f75a6SJohn Johansen goto out; 112e06f75a6SJohn Johansen 113e06f75a6SJohn Johansen /* accept.size == default.size == base.size */ 114e06f75a6SJohn Johansen state_count = dfa->tables[YYTD_ID_BASE]->td_lolen; 115e06f75a6SJohn Johansen if (ACCEPT1_FLAGS(flags)) { 116e06f75a6SJohn Johansen if (!dfa->tables[YYTD_ID_ACCEPT]) 117e06f75a6SJohn Johansen goto out; 118e06f75a6SJohn Johansen if (state_count != dfa->tables[YYTD_ID_ACCEPT]->td_lolen) 119e06f75a6SJohn Johansen goto out; 120e06f75a6SJohn Johansen } 121e06f75a6SJohn Johansen if (ACCEPT2_FLAGS(flags)) { 122e06f75a6SJohn Johansen if (!dfa->tables[YYTD_ID_ACCEPT2]) 123e06f75a6SJohn Johansen goto out; 124e06f75a6SJohn Johansen if (state_count != dfa->tables[YYTD_ID_ACCEPT2]->td_lolen) 125e06f75a6SJohn Johansen goto out; 126e06f75a6SJohn Johansen } 127e06f75a6SJohn Johansen if (state_count != dfa->tables[YYTD_ID_DEF]->td_lolen) 128e06f75a6SJohn Johansen goto out; 129e06f75a6SJohn Johansen 130e06f75a6SJohn Johansen /* next.size == chk.size */ 131e06f75a6SJohn Johansen trans_count = dfa->tables[YYTD_ID_NXT]->td_lolen; 132e06f75a6SJohn Johansen if (trans_count != dfa->tables[YYTD_ID_CHK]->td_lolen) 133e06f75a6SJohn Johansen goto out; 134e06f75a6SJohn Johansen 135e06f75a6SJohn Johansen /* if equivalence classes then its table size must be 256 */ 136e06f75a6SJohn Johansen if (dfa->tables[YYTD_ID_EC] && 137e06f75a6SJohn Johansen dfa->tables[YYTD_ID_EC]->td_lolen != 256) 138e06f75a6SJohn Johansen goto out; 139e06f75a6SJohn Johansen 140e06f75a6SJohn Johansen if (flags & DFA_FLAG_VERIFY_STATES) { 141e06f75a6SJohn Johansen for (i = 0; i < state_count; i++) { 142e06f75a6SJohn Johansen if (DEFAULT_TABLE(dfa)[i] >= state_count) 143e06f75a6SJohn Johansen goto out; 144ed686308SJohn Johansen if (base_idx(BASE_TABLE(dfa)[i]) + 255 >= trans_count) { 145e06f75a6SJohn Johansen printk(KERN_ERR "AppArmor DFA next/check upper " 146e06f75a6SJohn Johansen "bounds error\n"); 147e06f75a6SJohn Johansen goto out; 148e06f75a6SJohn Johansen } 149e06f75a6SJohn Johansen } 150e06f75a6SJohn Johansen 151e06f75a6SJohn Johansen for (i = 0; i < trans_count; i++) { 152e06f75a6SJohn Johansen if (NEXT_TABLE(dfa)[i] >= state_count) 153e06f75a6SJohn Johansen goto out; 154e06f75a6SJohn Johansen if (CHECK_TABLE(dfa)[i] >= state_count) 155e06f75a6SJohn Johansen goto out; 156e06f75a6SJohn Johansen } 157e06f75a6SJohn Johansen } 158e06f75a6SJohn Johansen 159e06f75a6SJohn Johansen error = 0; 160e06f75a6SJohn Johansen out: 161e06f75a6SJohn Johansen return error; 162e06f75a6SJohn Johansen } 163e06f75a6SJohn Johansen 164e06f75a6SJohn Johansen /** 165e06f75a6SJohn Johansen * dfa_free - free a dfa allocated by aa_dfa_unpack 166e06f75a6SJohn Johansen * @dfa: the dfa to free (MAYBE NULL) 167e06f75a6SJohn Johansen * 168e06f75a6SJohn Johansen * Requires: reference count to dfa == 0 169e06f75a6SJohn Johansen */ 170e06f75a6SJohn Johansen static void dfa_free(struct aa_dfa *dfa) 171e06f75a6SJohn Johansen { 172e06f75a6SJohn Johansen if (dfa) { 173e06f75a6SJohn Johansen int i; 174e06f75a6SJohn Johansen 175e06f75a6SJohn Johansen for (i = 0; i < ARRAY_SIZE(dfa->tables); i++) { 176e06f75a6SJohn Johansen kvfree(dfa->tables[i]); 177e06f75a6SJohn Johansen dfa->tables[i] = NULL; 178e06f75a6SJohn Johansen } 179e06f75a6SJohn Johansen kfree(dfa); 180e06f75a6SJohn Johansen } 181e06f75a6SJohn Johansen } 182e06f75a6SJohn Johansen 183e06f75a6SJohn Johansen /** 184e06f75a6SJohn Johansen * aa_dfa_free_kref - free aa_dfa by kref (called by aa_put_dfa) 185e06f75a6SJohn Johansen * @kr: kref callback for freeing of a dfa (NOT NULL) 186e06f75a6SJohn Johansen */ 187e06f75a6SJohn Johansen void aa_dfa_free_kref(struct kref *kref) 188e06f75a6SJohn Johansen { 189e06f75a6SJohn Johansen struct aa_dfa *dfa = container_of(kref, struct aa_dfa, count); 190e06f75a6SJohn Johansen dfa_free(dfa); 191e06f75a6SJohn Johansen } 192e06f75a6SJohn Johansen 193e06f75a6SJohn Johansen /** 194e06f75a6SJohn Johansen * aa_dfa_unpack - unpack the binary tables of a serialized dfa 195e06f75a6SJohn Johansen * @blob: aligned serialized stream of data to unpack (NOT NULL) 196e06f75a6SJohn Johansen * @size: size of data to unpack 197e06f75a6SJohn Johansen * @flags: flags controlling what type of accept tables are acceptable 198e06f75a6SJohn Johansen * 199e06f75a6SJohn Johansen * Unpack a dfa that has been serialized. To find information on the dfa 200d410fa4eSRandy Dunlap * format look in Documentation/security/apparmor.txt 20125985edcSLucas De Marchi * Assumes the dfa @blob stream has been aligned on a 8 byte boundary 202e06f75a6SJohn Johansen * 203e06f75a6SJohn Johansen * Returns: an unpacked dfa ready for matching or ERR_PTR on failure 204e06f75a6SJohn Johansen */ 205e06f75a6SJohn Johansen struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags) 206e06f75a6SJohn Johansen { 207e06f75a6SJohn Johansen int hsize; 208e06f75a6SJohn Johansen int error = -ENOMEM; 209e06f75a6SJohn Johansen char *data = blob; 210e06f75a6SJohn Johansen struct table_header *table = NULL; 211e06f75a6SJohn Johansen struct aa_dfa *dfa = kzalloc(sizeof(struct aa_dfa), GFP_KERNEL); 212e06f75a6SJohn Johansen if (!dfa) 213e06f75a6SJohn Johansen goto fail; 214e06f75a6SJohn Johansen 215e06f75a6SJohn Johansen kref_init(&dfa->count); 216e06f75a6SJohn Johansen 217e06f75a6SJohn Johansen error = -EPROTO; 218e06f75a6SJohn Johansen 219e06f75a6SJohn Johansen /* get dfa table set header */ 220e06f75a6SJohn Johansen if (size < sizeof(struct table_set_header)) 221e06f75a6SJohn Johansen goto fail; 222e06f75a6SJohn Johansen 223e06f75a6SJohn Johansen if (ntohl(*(u32 *) data) != YYTH_MAGIC) 224e06f75a6SJohn Johansen goto fail; 225e06f75a6SJohn Johansen 226e06f75a6SJohn Johansen hsize = ntohl(*(u32 *) (data + 4)); 227e06f75a6SJohn Johansen if (size < hsize) 228e06f75a6SJohn Johansen goto fail; 229e06f75a6SJohn Johansen 230e06f75a6SJohn Johansen dfa->flags = ntohs(*(u16 *) (data + 12)); 231e06f75a6SJohn Johansen data += hsize; 232e06f75a6SJohn Johansen size -= hsize; 233e06f75a6SJohn Johansen 234e06f75a6SJohn Johansen while (size > 0) { 235e06f75a6SJohn Johansen table = unpack_table(data, size); 236e06f75a6SJohn Johansen if (!table) 237e06f75a6SJohn Johansen goto fail; 238e06f75a6SJohn Johansen 239e06f75a6SJohn Johansen switch (table->td_id) { 240e06f75a6SJohn Johansen case YYTD_ID_ACCEPT: 241e06f75a6SJohn Johansen if (!(table->td_flags & ACCEPT1_FLAGS(flags))) 242e06f75a6SJohn Johansen goto fail; 243e06f75a6SJohn Johansen break; 244e06f75a6SJohn Johansen case YYTD_ID_ACCEPT2: 245e06f75a6SJohn Johansen if (!(table->td_flags & ACCEPT2_FLAGS(flags))) 246e06f75a6SJohn Johansen goto fail; 247e06f75a6SJohn Johansen break; 248e06f75a6SJohn Johansen case YYTD_ID_BASE: 249e06f75a6SJohn Johansen if (table->td_flags != YYTD_DATA32) 250e06f75a6SJohn Johansen goto fail; 251e06f75a6SJohn Johansen break; 252e06f75a6SJohn Johansen case YYTD_ID_DEF: 253e06f75a6SJohn Johansen case YYTD_ID_NXT: 254e06f75a6SJohn Johansen case YYTD_ID_CHK: 255e06f75a6SJohn Johansen if (table->td_flags != YYTD_DATA16) 256e06f75a6SJohn Johansen goto fail; 257e06f75a6SJohn Johansen break; 258e06f75a6SJohn Johansen case YYTD_ID_EC: 259e06f75a6SJohn Johansen if (table->td_flags != YYTD_DATA8) 260e06f75a6SJohn Johansen goto fail; 261e06f75a6SJohn Johansen break; 262e06f75a6SJohn Johansen default: 263e06f75a6SJohn Johansen goto fail; 264e06f75a6SJohn Johansen } 265e06f75a6SJohn Johansen /* check for duplicate table entry */ 266e06f75a6SJohn Johansen if (dfa->tables[table->td_id]) 267e06f75a6SJohn Johansen goto fail; 268e06f75a6SJohn Johansen dfa->tables[table->td_id] = table; 269e06f75a6SJohn Johansen data += table_size(table->td_lolen, table->td_flags); 270e06f75a6SJohn Johansen size -= table_size(table->td_lolen, table->td_flags); 271e06f75a6SJohn Johansen table = NULL; 272e06f75a6SJohn Johansen } 273e06f75a6SJohn Johansen 274e06f75a6SJohn Johansen error = verify_dfa(dfa, flags); 275e06f75a6SJohn Johansen if (error) 276e06f75a6SJohn Johansen goto fail; 277e06f75a6SJohn Johansen 278e06f75a6SJohn Johansen return dfa; 279e06f75a6SJohn Johansen 280e06f75a6SJohn Johansen fail: 281e06f75a6SJohn Johansen kvfree(table); 282e06f75a6SJohn Johansen dfa_free(dfa); 283e06f75a6SJohn Johansen return ERR_PTR(error); 284e06f75a6SJohn Johansen } 285e06f75a6SJohn Johansen 286e06f75a6SJohn Johansen /** 287e06f75a6SJohn Johansen * aa_dfa_match_len - traverse @dfa to find state @str stops at 288e06f75a6SJohn Johansen * @dfa: the dfa to match @str against (NOT NULL) 289e06f75a6SJohn Johansen * @start: the state of the dfa to start matching in 290e06f75a6SJohn Johansen * @str: the string of bytes to match against the dfa (NOT NULL) 291e06f75a6SJohn Johansen * @len: length of the string of bytes to match 292e06f75a6SJohn Johansen * 293e06f75a6SJohn Johansen * aa_dfa_match_len will match @str against the dfa and return the state it 294e06f75a6SJohn Johansen * finished matching in. The final state can be used to look up the accepting 295e06f75a6SJohn Johansen * label, or as the start state of a continuing match. 296e06f75a6SJohn Johansen * 297e06f75a6SJohn Johansen * This function will happily match again the 0 byte and only finishes 298e06f75a6SJohn Johansen * when @len input is consumed. 299e06f75a6SJohn Johansen * 300e06f75a6SJohn Johansen * Returns: final state reached after input is consumed 301e06f75a6SJohn Johansen */ 302e06f75a6SJohn Johansen unsigned int aa_dfa_match_len(struct aa_dfa *dfa, unsigned int start, 303e06f75a6SJohn Johansen const char *str, int len) 304e06f75a6SJohn Johansen { 305e06f75a6SJohn Johansen u16 *def = DEFAULT_TABLE(dfa); 306e06f75a6SJohn Johansen u32 *base = BASE_TABLE(dfa); 307e06f75a6SJohn Johansen u16 *next = NEXT_TABLE(dfa); 308e06f75a6SJohn Johansen u16 *check = CHECK_TABLE(dfa); 309e06f75a6SJohn Johansen unsigned int state = start, pos; 310e06f75a6SJohn Johansen 311e06f75a6SJohn Johansen if (state == 0) 312e06f75a6SJohn Johansen return 0; 313e06f75a6SJohn Johansen 314e06f75a6SJohn Johansen /* current state is <state>, matching character *str */ 315e06f75a6SJohn Johansen if (dfa->tables[YYTD_ID_EC]) { 316e06f75a6SJohn Johansen /* Equivalence class table defined */ 317e06f75a6SJohn Johansen u8 *equiv = EQUIV_TABLE(dfa); 318e06f75a6SJohn Johansen /* default is direct to next state */ 319e06f75a6SJohn Johansen for (; len; len--) { 320ed686308SJohn Johansen pos = base_idx(base[state]) + equiv[(u8) *str++]; 321e06f75a6SJohn Johansen if (check[pos] == state) 322e06f75a6SJohn Johansen state = next[pos]; 323e06f75a6SJohn Johansen else 324e06f75a6SJohn Johansen state = def[state]; 325e06f75a6SJohn Johansen } 326e06f75a6SJohn Johansen } else { 327e06f75a6SJohn Johansen /* default is direct to next state */ 328e06f75a6SJohn Johansen for (; len; len--) { 329ed686308SJohn Johansen pos = base_idx(base[state]) + (u8) *str++; 330e06f75a6SJohn Johansen if (check[pos] == state) 331e06f75a6SJohn Johansen state = next[pos]; 332e06f75a6SJohn Johansen else 333e06f75a6SJohn Johansen state = def[state]; 334e06f75a6SJohn Johansen } 335e06f75a6SJohn Johansen } 336e06f75a6SJohn Johansen 337e06f75a6SJohn Johansen return state; 338e06f75a6SJohn Johansen } 339e06f75a6SJohn Johansen 340e06f75a6SJohn Johansen /** 3410fe1212dSJohn Johansen * aa_dfa_match - traverse @dfa to find state @str stops at 342e06f75a6SJohn Johansen * @dfa: the dfa to match @str against (NOT NULL) 343e06f75a6SJohn Johansen * @start: the state of the dfa to start matching in 344e06f75a6SJohn Johansen * @str: the null terminated string of bytes to match against the dfa (NOT NULL) 345e06f75a6SJohn Johansen * 3460fe1212dSJohn Johansen * aa_dfa_match will match @str against the dfa and return the state it 347e06f75a6SJohn Johansen * finished matching in. The final state can be used to look up the accepting 348e06f75a6SJohn Johansen * label, or as the start state of a continuing match. 349e06f75a6SJohn Johansen * 350e06f75a6SJohn Johansen * Returns: final state reached after input is consumed 351e06f75a6SJohn Johansen */ 352e06f75a6SJohn Johansen unsigned int aa_dfa_match(struct aa_dfa *dfa, unsigned int start, 353e06f75a6SJohn Johansen const char *str) 354e06f75a6SJohn Johansen { 3550fe1212dSJohn Johansen u16 *def = DEFAULT_TABLE(dfa); 3560fe1212dSJohn Johansen u32 *base = BASE_TABLE(dfa); 3570fe1212dSJohn Johansen u16 *next = NEXT_TABLE(dfa); 3580fe1212dSJohn Johansen u16 *check = CHECK_TABLE(dfa); 3590fe1212dSJohn Johansen unsigned int state = start, pos; 3600fe1212dSJohn Johansen 3610fe1212dSJohn Johansen if (state == 0) 3620fe1212dSJohn Johansen return 0; 3630fe1212dSJohn Johansen 3640fe1212dSJohn Johansen /* current state is <state>, matching character *str */ 3650fe1212dSJohn Johansen if (dfa->tables[YYTD_ID_EC]) { 3660fe1212dSJohn Johansen /* Equivalence class table defined */ 3670fe1212dSJohn Johansen u8 *equiv = EQUIV_TABLE(dfa); 3680fe1212dSJohn Johansen /* default is direct to next state */ 3690fe1212dSJohn Johansen while (*str) { 370ed686308SJohn Johansen pos = base_idx(base[state]) + equiv[(u8) *str++]; 3710fe1212dSJohn Johansen if (check[pos] == state) 3720fe1212dSJohn Johansen state = next[pos]; 3730fe1212dSJohn Johansen else 3740fe1212dSJohn Johansen state = def[state]; 3750fe1212dSJohn Johansen } 3760fe1212dSJohn Johansen } else { 3770fe1212dSJohn Johansen /* default is direct to next state */ 3780fe1212dSJohn Johansen while (*str) { 379ed686308SJohn Johansen pos = base_idx(base[state]) + (u8) *str++; 3800fe1212dSJohn Johansen if (check[pos] == state) 3810fe1212dSJohn Johansen state = next[pos]; 3820fe1212dSJohn Johansen else 3830fe1212dSJohn Johansen state = def[state]; 3840fe1212dSJohn Johansen } 3850fe1212dSJohn Johansen } 3860fe1212dSJohn Johansen 3870fe1212dSJohn Johansen return state; 3880fe1212dSJohn Johansen } 3890fe1212dSJohn Johansen 3900fe1212dSJohn Johansen /** 3910fe1212dSJohn Johansen * aa_dfa_next - step one character to the next state in the dfa 3920fe1212dSJohn Johansen * @dfa: the dfa to tranverse (NOT NULL) 3930fe1212dSJohn Johansen * @state: the state to start in 3940fe1212dSJohn Johansen * @c: the input character to transition on 3950fe1212dSJohn Johansen * 3960fe1212dSJohn Johansen * aa_dfa_match will step through the dfa by one input character @c 3970fe1212dSJohn Johansen * 3980fe1212dSJohn Johansen * Returns: state reach after input @c 3990fe1212dSJohn Johansen */ 4000fe1212dSJohn Johansen unsigned int aa_dfa_next(struct aa_dfa *dfa, unsigned int state, 4010fe1212dSJohn Johansen const char c) 4020fe1212dSJohn Johansen { 4030fe1212dSJohn Johansen u16 *def = DEFAULT_TABLE(dfa); 4040fe1212dSJohn Johansen u32 *base = BASE_TABLE(dfa); 4050fe1212dSJohn Johansen u16 *next = NEXT_TABLE(dfa); 4060fe1212dSJohn Johansen u16 *check = CHECK_TABLE(dfa); 4070fe1212dSJohn Johansen unsigned int pos; 4080fe1212dSJohn Johansen 4090fe1212dSJohn Johansen /* current state is <state>, matching character *str */ 4100fe1212dSJohn Johansen if (dfa->tables[YYTD_ID_EC]) { 4110fe1212dSJohn Johansen /* Equivalence class table defined */ 4120fe1212dSJohn Johansen u8 *equiv = EQUIV_TABLE(dfa); 4130fe1212dSJohn Johansen /* default is direct to next state */ 4140fe1212dSJohn Johansen 415ed686308SJohn Johansen pos = base_idx(base[state]) + equiv[(u8) c]; 4160fe1212dSJohn Johansen if (check[pos] == state) 4170fe1212dSJohn Johansen state = next[pos]; 4180fe1212dSJohn Johansen else 4190fe1212dSJohn Johansen state = def[state]; 4200fe1212dSJohn Johansen } else { 4210fe1212dSJohn Johansen /* default is direct to next state */ 422ed686308SJohn Johansen pos = base_idx(base[state]) + (u8) c; 4230fe1212dSJohn Johansen if (check[pos] == state) 4240fe1212dSJohn Johansen state = next[pos]; 4250fe1212dSJohn Johansen else 4260fe1212dSJohn Johansen state = def[state]; 4270fe1212dSJohn Johansen } 4280fe1212dSJohn Johansen 4290fe1212dSJohn Johansen return state; 430e06f75a6SJohn Johansen } 431