1*b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2e06f75a6SJohn Johansen /* 3e06f75a6SJohn Johansen * AppArmor security module 4e06f75a6SJohn Johansen * 5e06f75a6SJohn Johansen * This file contains AppArmor dfa based regular expression matching engine 6e06f75a6SJohn Johansen * 7e06f75a6SJohn Johansen * Copyright (C) 1998-2008 Novell/SUSE 88e4ff109SJohn Johansen * Copyright 2009-2012 Canonical Ltd. 9e06f75a6SJohn Johansen */ 10e06f75a6SJohn Johansen 11e06f75a6SJohn Johansen #include <linux/errno.h> 12e06f75a6SJohn Johansen #include <linux/kernel.h> 13e06f75a6SJohn Johansen #include <linux/mm.h> 14e06f75a6SJohn Johansen #include <linux/slab.h> 15e06f75a6SJohn Johansen #include <linux/vmalloc.h> 16e06f75a6SJohn Johansen #include <linux/err.h> 17e06f75a6SJohn Johansen #include <linux/kref.h> 18e06f75a6SJohn Johansen 1912557dcbSJohn Johansen #include "include/lib.h" 20e06f75a6SJohn Johansen #include "include/match.h" 21e06f75a6SJohn Johansen 22ed686308SJohn Johansen #define base_idx(X) ((X) & 0xffffff) 23ed686308SJohn Johansen 2411c236b8SJohn Johansen static char nulldfa_src[] = { 2511c236b8SJohn Johansen #include "nulldfa.in" 2611c236b8SJohn Johansen }; 2711c236b8SJohn Johansen struct aa_dfa *nulldfa; 2811c236b8SJohn Johansen 296e0654d2SJohn Johansen static char stacksplitdfa_src[] = { 306e0654d2SJohn Johansen #include "stacksplitdfa.in" 316e0654d2SJohn Johansen }; 326e0654d2SJohn Johansen struct aa_dfa *stacksplitdfa; 336e0654d2SJohn Johansen 3411c236b8SJohn Johansen int aa_setup_dfa_engine(void) 3511c236b8SJohn Johansen { 3611c236b8SJohn Johansen int error; 3711c236b8SJohn Johansen 3811c236b8SJohn Johansen nulldfa = aa_dfa_unpack(nulldfa_src, sizeof(nulldfa_src), 3911c236b8SJohn Johansen TO_ACCEPT1_FLAG(YYTD_DATA32) | 4011c236b8SJohn Johansen TO_ACCEPT2_FLAG(YYTD_DATA32)); 416e0654d2SJohn Johansen if (IS_ERR(nulldfa)) { 4211c236b8SJohn Johansen error = PTR_ERR(nulldfa); 4311c236b8SJohn Johansen nulldfa = NULL; 4411c236b8SJohn Johansen return error; 4511c236b8SJohn Johansen } 4611c236b8SJohn Johansen 476e0654d2SJohn Johansen stacksplitdfa = aa_dfa_unpack(stacksplitdfa_src, 486e0654d2SJohn Johansen sizeof(stacksplitdfa_src), 496e0654d2SJohn Johansen TO_ACCEPT1_FLAG(YYTD_DATA32) | 506e0654d2SJohn Johansen TO_ACCEPT2_FLAG(YYTD_DATA32)); 516e0654d2SJohn Johansen if (IS_ERR(stacksplitdfa)) { 526e0654d2SJohn Johansen aa_put_dfa(nulldfa); 536e0654d2SJohn Johansen nulldfa = NULL; 546e0654d2SJohn Johansen error = PTR_ERR(stacksplitdfa); 556e0654d2SJohn Johansen stacksplitdfa = NULL; 566e0654d2SJohn Johansen return error; 576e0654d2SJohn Johansen } 586e0654d2SJohn Johansen 596e0654d2SJohn Johansen return 0; 606e0654d2SJohn Johansen } 616e0654d2SJohn Johansen 6211c236b8SJohn Johansen void aa_teardown_dfa_engine(void) 6311c236b8SJohn Johansen { 646e0654d2SJohn Johansen aa_put_dfa(stacksplitdfa); 6511c236b8SJohn Johansen aa_put_dfa(nulldfa); 6611c236b8SJohn Johansen } 6711c236b8SJohn Johansen 68e06f75a6SJohn Johansen /** 69e06f75a6SJohn Johansen * unpack_table - unpack a dfa table (one of accept, default, base, next check) 70e06f75a6SJohn Johansen * @blob: data to unpack (NOT NULL) 71e06f75a6SJohn Johansen * @bsize: size of blob 72e06f75a6SJohn Johansen * 73e06f75a6SJohn Johansen * Returns: pointer to table else NULL on failure 74e06f75a6SJohn Johansen * 750ca554b9SJohn Johansen * NOTE: must be freed by kvfree (not kfree) 76e06f75a6SJohn Johansen */ 77e06f75a6SJohn Johansen static struct table_header *unpack_table(char *blob, size_t bsize) 78e06f75a6SJohn Johansen { 79e06f75a6SJohn Johansen struct table_header *table = NULL; 80e06f75a6SJohn Johansen struct table_header th; 81e06f75a6SJohn Johansen size_t tsize; 82e06f75a6SJohn Johansen 83e06f75a6SJohn Johansen if (bsize < sizeof(struct table_header)) 84e06f75a6SJohn Johansen goto out; 85e06f75a6SJohn Johansen 86e06f75a6SJohn Johansen /* loaded td_id's start at 1, subtract 1 now to avoid doing 87e06f75a6SJohn Johansen * it every time we use td_id as an index 88e06f75a6SJohn Johansen */ 89e6e8bf41SJohn Johansen th.td_id = be16_to_cpu(*(__be16 *) (blob)) - 1; 9015756178SJohn Johansen if (th.td_id > YYTD_ID_MAX) 9115756178SJohn Johansen goto out; 92e6e8bf41SJohn Johansen th.td_flags = be16_to_cpu(*(__be16 *) (blob + 2)); 93e6e8bf41SJohn Johansen th.td_lolen = be32_to_cpu(*(__be32 *) (blob + 8)); 94e06f75a6SJohn Johansen blob += sizeof(struct table_header); 95e06f75a6SJohn Johansen 96e06f75a6SJohn Johansen if (!(th.td_flags == YYTD_DATA16 || th.td_flags == YYTD_DATA32 || 97e06f75a6SJohn Johansen th.td_flags == YYTD_DATA8)) 98e06f75a6SJohn Johansen goto out; 99e06f75a6SJohn Johansen 100e06f75a6SJohn Johansen tsize = table_size(th.td_lolen, th.td_flags); 101e06f75a6SJohn Johansen if (bsize < tsize) 102e06f75a6SJohn Johansen goto out; 103e06f75a6SJohn Johansen 104a7c3e901SMichal Hocko table = kvzalloc(tsize, GFP_KERNEL); 105e06f75a6SJohn Johansen if (table) { 106f4ee2defSHeinrich Schuchardt table->td_id = th.td_id; 107f4ee2defSHeinrich Schuchardt table->td_flags = th.td_flags; 108f4ee2defSHeinrich Schuchardt table->td_lolen = th.td_lolen; 109e06f75a6SJohn Johansen if (th.td_flags == YYTD_DATA8) 110e06f75a6SJohn Johansen UNPACK_ARRAY(table->td_data, blob, th.td_lolen, 111e6e8bf41SJohn Johansen u8, u8, byte_to_byte); 112e06f75a6SJohn Johansen else if (th.td_flags == YYTD_DATA16) 113e06f75a6SJohn Johansen UNPACK_ARRAY(table->td_data, blob, th.td_lolen, 114e6e8bf41SJohn Johansen u16, __be16, be16_to_cpu); 115e06f75a6SJohn Johansen else if (th.td_flags == YYTD_DATA32) 116e06f75a6SJohn Johansen UNPACK_ARRAY(table->td_data, blob, th.td_lolen, 117e6e8bf41SJohn Johansen u32, __be32, be32_to_cpu); 118e06f75a6SJohn Johansen else 119e06f75a6SJohn Johansen goto fail; 120e06f75a6SJohn Johansen /* if table was vmalloced make sure the page tables are synced 121e06f75a6SJohn Johansen * before it is used, as it goes live to all cpus. 122e06f75a6SJohn Johansen */ 123e06f75a6SJohn Johansen if (is_vmalloc_addr(table)) 124e06f75a6SJohn Johansen vm_unmap_aliases(); 1253197f5adSJohn Johansen } 1263197f5adSJohn Johansen 1273197f5adSJohn Johansen out: 128e06f75a6SJohn Johansen return table; 129e06f75a6SJohn Johansen fail: 130e06f75a6SJohn Johansen kvfree(table); 131e06f75a6SJohn Johansen return NULL; 132e06f75a6SJohn Johansen } 133e06f75a6SJohn Johansen 134e06f75a6SJohn Johansen /** 135d901d6a2SJohn Johansen * verify_table_headers - verify that the tables headers are as expected 136d901d6a2SJohn Johansen * @tables - array of dfa tables to check (NOT NULL) 137e06f75a6SJohn Johansen * @flags: flags controlling what type of accept table are acceptable 138e06f75a6SJohn Johansen * 139e06f75a6SJohn Johansen * Assumes dfa has gone through the first pass verification done by unpacking 140e06f75a6SJohn Johansen * NOTE: this does not valid accept table values 141e06f75a6SJohn Johansen * 142e06f75a6SJohn Johansen * Returns: %0 else error code on failure to verify 143e06f75a6SJohn Johansen */ 144d901d6a2SJohn Johansen static int verify_table_headers(struct table_header **tables, int flags) 145e06f75a6SJohn Johansen { 146d901d6a2SJohn Johansen size_t state_count, trans_count; 147e06f75a6SJohn Johansen int error = -EPROTO; 148e06f75a6SJohn Johansen 149e06f75a6SJohn Johansen /* check that required tables exist */ 150d901d6a2SJohn Johansen if (!(tables[YYTD_ID_DEF] && tables[YYTD_ID_BASE] && 151d901d6a2SJohn Johansen tables[YYTD_ID_NXT] && tables[YYTD_ID_CHK])) 152e06f75a6SJohn Johansen goto out; 153e06f75a6SJohn Johansen 154e06f75a6SJohn Johansen /* accept.size == default.size == base.size */ 155d901d6a2SJohn Johansen state_count = tables[YYTD_ID_BASE]->td_lolen; 156e06f75a6SJohn Johansen if (ACCEPT1_FLAGS(flags)) { 157d901d6a2SJohn Johansen if (!tables[YYTD_ID_ACCEPT]) 158e06f75a6SJohn Johansen goto out; 159d901d6a2SJohn Johansen if (state_count != tables[YYTD_ID_ACCEPT]->td_lolen) 160e06f75a6SJohn Johansen goto out; 161e06f75a6SJohn Johansen } 162e06f75a6SJohn Johansen if (ACCEPT2_FLAGS(flags)) { 163d901d6a2SJohn Johansen if (!tables[YYTD_ID_ACCEPT2]) 164e06f75a6SJohn Johansen goto out; 165d901d6a2SJohn Johansen if (state_count != tables[YYTD_ID_ACCEPT2]->td_lolen) 166e06f75a6SJohn Johansen goto out; 167e06f75a6SJohn Johansen } 168d901d6a2SJohn Johansen if (state_count != tables[YYTD_ID_DEF]->td_lolen) 169e06f75a6SJohn Johansen goto out; 170e06f75a6SJohn Johansen 171e06f75a6SJohn Johansen /* next.size == chk.size */ 172d901d6a2SJohn Johansen trans_count = tables[YYTD_ID_NXT]->td_lolen; 173d901d6a2SJohn Johansen if (trans_count != tables[YYTD_ID_CHK]->td_lolen) 174e06f75a6SJohn Johansen goto out; 175e06f75a6SJohn Johansen 176e06f75a6SJohn Johansen /* if equivalence classes then its table size must be 256 */ 177d901d6a2SJohn Johansen if (tables[YYTD_ID_EC] && tables[YYTD_ID_EC]->td_lolen != 256) 178e06f75a6SJohn Johansen goto out; 179e06f75a6SJohn Johansen 180d901d6a2SJohn Johansen error = 0; 181d901d6a2SJohn Johansen out: 182d901d6a2SJohn Johansen return error; 183d901d6a2SJohn Johansen } 184d901d6a2SJohn Johansen 185d901d6a2SJohn Johansen /** 186d901d6a2SJohn Johansen * verify_dfa - verify that transitions and states in the tables are in bounds. 187d901d6a2SJohn Johansen * @dfa: dfa to test (NOT NULL) 188d901d6a2SJohn Johansen * 189d901d6a2SJohn Johansen * Assumes dfa has gone through the first pass verification done by unpacking 190d901d6a2SJohn Johansen * NOTE: this does not valid accept table values 191d901d6a2SJohn Johansen * 192d901d6a2SJohn Johansen * Returns: %0 else error code on failure to verify 193d901d6a2SJohn Johansen */ 194d901d6a2SJohn Johansen static int verify_dfa(struct aa_dfa *dfa) 195d901d6a2SJohn Johansen { 196d901d6a2SJohn Johansen size_t i, state_count, trans_count; 197d53c9f4dSDan Carpenter int error = -EPROTO; 198d901d6a2SJohn Johansen 199d901d6a2SJohn Johansen state_count = dfa->tables[YYTD_ID_BASE]->td_lolen; 200d901d6a2SJohn Johansen trans_count = dfa->tables[YYTD_ID_NXT]->td_lolen; 201e06f75a6SJohn Johansen for (i = 0; i < state_count; i++) { 202031dcc8fSJohn Johansen if (!(BASE_TABLE(dfa)[i] & MATCH_FLAG_DIFF_ENCODE) && 203031dcc8fSJohn Johansen (DEFAULT_TABLE(dfa)[i] >= state_count)) 204e06f75a6SJohn Johansen goto out; 205ed686308SJohn Johansen if (base_idx(BASE_TABLE(dfa)[i]) + 255 >= trans_count) { 206d901d6a2SJohn Johansen pr_err("AppArmor DFA next/check upper bounds error\n"); 207e06f75a6SJohn Johansen goto out; 208e06f75a6SJohn Johansen } 209e06f75a6SJohn Johansen } 210e06f75a6SJohn Johansen 211e06f75a6SJohn Johansen for (i = 0; i < trans_count; i++) { 212e06f75a6SJohn Johansen if (NEXT_TABLE(dfa)[i] >= state_count) 213e06f75a6SJohn Johansen goto out; 214e06f75a6SJohn Johansen if (CHECK_TABLE(dfa)[i] >= state_count) 215e06f75a6SJohn Johansen goto out; 216e06f75a6SJohn Johansen } 217e06f75a6SJohn Johansen 218031dcc8fSJohn Johansen /* Now that all the other tables are verified, verify diffencoding */ 219d901d6a2SJohn Johansen for (i = 0; i < state_count; i++) { 220031dcc8fSJohn Johansen size_t j, k; 221031dcc8fSJohn Johansen 222031dcc8fSJohn Johansen for (j = i; 223031dcc8fSJohn Johansen (BASE_TABLE(dfa)[j] & MATCH_FLAG_DIFF_ENCODE) && 224031dcc8fSJohn Johansen !(BASE_TABLE(dfa)[j] & MARK_DIFF_ENCODE); 225031dcc8fSJohn Johansen j = k) { 226031dcc8fSJohn Johansen k = DEFAULT_TABLE(dfa)[j]; 227031dcc8fSJohn Johansen if (j == k) 228031dcc8fSJohn Johansen goto out; 229031dcc8fSJohn Johansen if (k < j) 230031dcc8fSJohn Johansen break; /* already verified */ 231031dcc8fSJohn Johansen BASE_TABLE(dfa)[j] |= MARK_DIFF_ENCODE; 232031dcc8fSJohn Johansen } 233031dcc8fSJohn Johansen } 234e06f75a6SJohn Johansen error = 0; 235d901d6a2SJohn Johansen 236e06f75a6SJohn Johansen out: 237e06f75a6SJohn Johansen return error; 238e06f75a6SJohn Johansen } 239e06f75a6SJohn Johansen 240e06f75a6SJohn Johansen /** 241e06f75a6SJohn Johansen * dfa_free - free a dfa allocated by aa_dfa_unpack 242e06f75a6SJohn Johansen * @dfa: the dfa to free (MAYBE NULL) 243e06f75a6SJohn Johansen * 244e06f75a6SJohn Johansen * Requires: reference count to dfa == 0 245e06f75a6SJohn Johansen */ 246e06f75a6SJohn Johansen static void dfa_free(struct aa_dfa *dfa) 247e06f75a6SJohn Johansen { 248e06f75a6SJohn Johansen if (dfa) { 249e06f75a6SJohn Johansen int i; 250e06f75a6SJohn Johansen 251e06f75a6SJohn Johansen for (i = 0; i < ARRAY_SIZE(dfa->tables); i++) { 252e06f75a6SJohn Johansen kvfree(dfa->tables[i]); 253e06f75a6SJohn Johansen dfa->tables[i] = NULL; 254e06f75a6SJohn Johansen } 255e06f75a6SJohn Johansen kfree(dfa); 256e06f75a6SJohn Johansen } 257e06f75a6SJohn Johansen } 258e06f75a6SJohn Johansen 259e06f75a6SJohn Johansen /** 260e06f75a6SJohn Johansen * aa_dfa_free_kref - free aa_dfa by kref (called by aa_put_dfa) 261e06f75a6SJohn Johansen * @kr: kref callback for freeing of a dfa (NOT NULL) 262e06f75a6SJohn Johansen */ 263e06f75a6SJohn Johansen void aa_dfa_free_kref(struct kref *kref) 264e06f75a6SJohn Johansen { 265e06f75a6SJohn Johansen struct aa_dfa *dfa = container_of(kref, struct aa_dfa, count); 266e06f75a6SJohn Johansen dfa_free(dfa); 267e06f75a6SJohn Johansen } 268e06f75a6SJohn Johansen 269e06f75a6SJohn Johansen /** 270e06f75a6SJohn Johansen * aa_dfa_unpack - unpack the binary tables of a serialized dfa 271e06f75a6SJohn Johansen * @blob: aligned serialized stream of data to unpack (NOT NULL) 272e06f75a6SJohn Johansen * @size: size of data to unpack 273e06f75a6SJohn Johansen * @flags: flags controlling what type of accept tables are acceptable 274e06f75a6SJohn Johansen * 275e06f75a6SJohn Johansen * Unpack a dfa that has been serialized. To find information on the dfa 27626fccd9eSKees Cook * format look in Documentation/admin-guide/LSM/apparmor.rst 27725985edcSLucas De Marchi * Assumes the dfa @blob stream has been aligned on a 8 byte boundary 278e06f75a6SJohn Johansen * 279e06f75a6SJohn Johansen * Returns: an unpacked dfa ready for matching or ERR_PTR on failure 280e06f75a6SJohn Johansen */ 281e06f75a6SJohn Johansen struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags) 282e06f75a6SJohn Johansen { 283e06f75a6SJohn Johansen int hsize; 284e06f75a6SJohn Johansen int error = -ENOMEM; 285e06f75a6SJohn Johansen char *data = blob; 286e06f75a6SJohn Johansen struct table_header *table = NULL; 287e06f75a6SJohn Johansen struct aa_dfa *dfa = kzalloc(sizeof(struct aa_dfa), GFP_KERNEL); 288e06f75a6SJohn Johansen if (!dfa) 289e06f75a6SJohn Johansen goto fail; 290e06f75a6SJohn Johansen 291e06f75a6SJohn Johansen kref_init(&dfa->count); 292e06f75a6SJohn Johansen 293e06f75a6SJohn Johansen error = -EPROTO; 294e06f75a6SJohn Johansen 295e06f75a6SJohn Johansen /* get dfa table set header */ 296e06f75a6SJohn Johansen if (size < sizeof(struct table_set_header)) 297e06f75a6SJohn Johansen goto fail; 298e06f75a6SJohn Johansen 299e6e8bf41SJohn Johansen if (ntohl(*(__be32 *) data) != YYTH_MAGIC) 300e06f75a6SJohn Johansen goto fail; 301e06f75a6SJohn Johansen 302e6e8bf41SJohn Johansen hsize = ntohl(*(__be32 *) (data + 4)); 303e06f75a6SJohn Johansen if (size < hsize) 304e06f75a6SJohn Johansen goto fail; 305e06f75a6SJohn Johansen 306e6e8bf41SJohn Johansen dfa->flags = ntohs(*(__be16 *) (data + 12)); 307031dcc8fSJohn Johansen if (dfa->flags != 0 && dfa->flags != YYTH_FLAG_DIFF_ENCODE) 308031dcc8fSJohn Johansen goto fail; 309031dcc8fSJohn Johansen 310e06f75a6SJohn Johansen data += hsize; 311e06f75a6SJohn Johansen size -= hsize; 312e06f75a6SJohn Johansen 313e06f75a6SJohn Johansen while (size > 0) { 314e06f75a6SJohn Johansen table = unpack_table(data, size); 315e06f75a6SJohn Johansen if (!table) 316e06f75a6SJohn Johansen goto fail; 317e06f75a6SJohn Johansen 318e06f75a6SJohn Johansen switch (table->td_id) { 319e06f75a6SJohn Johansen case YYTD_ID_ACCEPT: 320e06f75a6SJohn Johansen if (!(table->td_flags & ACCEPT1_FLAGS(flags))) 321e06f75a6SJohn Johansen goto fail; 322e06f75a6SJohn Johansen break; 323e06f75a6SJohn Johansen case YYTD_ID_ACCEPT2: 324e06f75a6SJohn Johansen if (!(table->td_flags & ACCEPT2_FLAGS(flags))) 325e06f75a6SJohn Johansen goto fail; 326e06f75a6SJohn Johansen break; 327e06f75a6SJohn Johansen case YYTD_ID_BASE: 328e06f75a6SJohn Johansen if (table->td_flags != YYTD_DATA32) 329e06f75a6SJohn Johansen goto fail; 330e06f75a6SJohn Johansen break; 331e06f75a6SJohn Johansen case YYTD_ID_DEF: 332e06f75a6SJohn Johansen case YYTD_ID_NXT: 333e06f75a6SJohn Johansen case YYTD_ID_CHK: 334e06f75a6SJohn Johansen if (table->td_flags != YYTD_DATA16) 335e06f75a6SJohn Johansen goto fail; 336e06f75a6SJohn Johansen break; 337e06f75a6SJohn Johansen case YYTD_ID_EC: 338e06f75a6SJohn Johansen if (table->td_flags != YYTD_DATA8) 339e06f75a6SJohn Johansen goto fail; 340e06f75a6SJohn Johansen break; 341e06f75a6SJohn Johansen default: 342e06f75a6SJohn Johansen goto fail; 343e06f75a6SJohn Johansen } 344e06f75a6SJohn Johansen /* check for duplicate table entry */ 345e06f75a6SJohn Johansen if (dfa->tables[table->td_id]) 346e06f75a6SJohn Johansen goto fail; 347e06f75a6SJohn Johansen dfa->tables[table->td_id] = table; 348e06f75a6SJohn Johansen data += table_size(table->td_lolen, table->td_flags); 349e06f75a6SJohn Johansen size -= table_size(table->td_lolen, table->td_flags); 350e06f75a6SJohn Johansen table = NULL; 351e06f75a6SJohn Johansen } 352d901d6a2SJohn Johansen error = verify_table_headers(dfa->tables, flags); 353e06f75a6SJohn Johansen if (error) 354e06f75a6SJohn Johansen goto fail; 355e06f75a6SJohn Johansen 356d901d6a2SJohn Johansen if (flags & DFA_FLAG_VERIFY_STATES) { 357d901d6a2SJohn Johansen error = verify_dfa(dfa); 358d901d6a2SJohn Johansen if (error) 359d901d6a2SJohn Johansen goto fail; 360d901d6a2SJohn Johansen } 361d901d6a2SJohn Johansen 362e06f75a6SJohn Johansen return dfa; 363e06f75a6SJohn Johansen 364e06f75a6SJohn Johansen fail: 365e06f75a6SJohn Johansen kvfree(table); 366e06f75a6SJohn Johansen dfa_free(dfa); 367e06f75a6SJohn Johansen return ERR_PTR(error); 368e06f75a6SJohn Johansen } 369e06f75a6SJohn Johansen 370074c1cd7SJohn Johansen #define match_char(state, def, base, next, check, C) \ 371074c1cd7SJohn Johansen do { \ 372074c1cd7SJohn Johansen u32 b = (base)[(state)]; \ 373074c1cd7SJohn Johansen unsigned int pos = base_idx(b) + (C); \ 374074c1cd7SJohn Johansen if ((check)[pos] != (state)) { \ 375074c1cd7SJohn Johansen (state) = (def)[(state)]; \ 376031dcc8fSJohn Johansen if (b & MATCH_FLAG_DIFF_ENCODE) \ 377031dcc8fSJohn Johansen continue; \ 378074c1cd7SJohn Johansen break; \ 379074c1cd7SJohn Johansen } \ 380074c1cd7SJohn Johansen (state) = (next)[pos]; \ 381074c1cd7SJohn Johansen break; \ 382074c1cd7SJohn Johansen } while (1) 383074c1cd7SJohn Johansen 384e06f75a6SJohn Johansen /** 385e06f75a6SJohn Johansen * aa_dfa_match_len - traverse @dfa to find state @str stops at 386e06f75a6SJohn Johansen * @dfa: the dfa to match @str against (NOT NULL) 387e06f75a6SJohn Johansen * @start: the state of the dfa to start matching in 388e06f75a6SJohn Johansen * @str: the string of bytes to match against the dfa (NOT NULL) 389e06f75a6SJohn Johansen * @len: length of the string of bytes to match 390e06f75a6SJohn Johansen * 391e06f75a6SJohn Johansen * aa_dfa_match_len will match @str against the dfa and return the state it 392e06f75a6SJohn Johansen * finished matching in. The final state can be used to look up the accepting 393e06f75a6SJohn Johansen * label, or as the start state of a continuing match. 394e06f75a6SJohn Johansen * 395e06f75a6SJohn Johansen * This function will happily match again the 0 byte and only finishes 396e06f75a6SJohn Johansen * when @len input is consumed. 397e06f75a6SJohn Johansen * 398e06f75a6SJohn Johansen * Returns: final state reached after input is consumed 399e06f75a6SJohn Johansen */ 400e06f75a6SJohn Johansen unsigned int aa_dfa_match_len(struct aa_dfa *dfa, unsigned int start, 401e06f75a6SJohn Johansen const char *str, int len) 402e06f75a6SJohn Johansen { 403e06f75a6SJohn Johansen u16 *def = DEFAULT_TABLE(dfa); 404e06f75a6SJohn Johansen u32 *base = BASE_TABLE(dfa); 405e06f75a6SJohn Johansen u16 *next = NEXT_TABLE(dfa); 406e06f75a6SJohn Johansen u16 *check = CHECK_TABLE(dfa); 407074c1cd7SJohn Johansen unsigned int state = start; 408e06f75a6SJohn Johansen 409e06f75a6SJohn Johansen if (state == 0) 410e06f75a6SJohn Johansen return 0; 411e06f75a6SJohn Johansen 412e06f75a6SJohn Johansen /* current state is <state>, matching character *str */ 413e06f75a6SJohn Johansen if (dfa->tables[YYTD_ID_EC]) { 414e06f75a6SJohn Johansen /* Equivalence class table defined */ 415e06f75a6SJohn Johansen u8 *equiv = EQUIV_TABLE(dfa); 416074c1cd7SJohn Johansen for (; len; len--) 417074c1cd7SJohn Johansen match_char(state, def, base, next, check, 418074c1cd7SJohn Johansen equiv[(u8) *str++]); 419e06f75a6SJohn Johansen } else { 420e06f75a6SJohn Johansen /* default is direct to next state */ 421074c1cd7SJohn Johansen for (; len; len--) 422074c1cd7SJohn Johansen match_char(state, def, base, next, check, (u8) *str++); 423e06f75a6SJohn Johansen } 424e06f75a6SJohn Johansen 425e06f75a6SJohn Johansen return state; 426e06f75a6SJohn Johansen } 427e06f75a6SJohn Johansen 428e06f75a6SJohn Johansen /** 4290fe1212dSJohn Johansen * aa_dfa_match - traverse @dfa to find state @str stops at 430e06f75a6SJohn Johansen * @dfa: the dfa to match @str against (NOT NULL) 431e06f75a6SJohn Johansen * @start: the state of the dfa to start matching in 432e06f75a6SJohn Johansen * @str: the null terminated string of bytes to match against the dfa (NOT NULL) 433e06f75a6SJohn Johansen * 4340fe1212dSJohn Johansen * aa_dfa_match will match @str against the dfa and return the state it 435e06f75a6SJohn Johansen * finished matching in. The final state can be used to look up the accepting 436e06f75a6SJohn Johansen * label, or as the start state of a continuing match. 437e06f75a6SJohn Johansen * 438e06f75a6SJohn Johansen * Returns: final state reached after input is consumed 439e06f75a6SJohn Johansen */ 440e06f75a6SJohn Johansen unsigned int aa_dfa_match(struct aa_dfa *dfa, unsigned int start, 441e06f75a6SJohn Johansen const char *str) 442e06f75a6SJohn Johansen { 4430fe1212dSJohn Johansen u16 *def = DEFAULT_TABLE(dfa); 4440fe1212dSJohn Johansen u32 *base = BASE_TABLE(dfa); 4450fe1212dSJohn Johansen u16 *next = NEXT_TABLE(dfa); 4460fe1212dSJohn Johansen u16 *check = CHECK_TABLE(dfa); 447074c1cd7SJohn Johansen unsigned int state = start; 4480fe1212dSJohn Johansen 4490fe1212dSJohn Johansen if (state == 0) 4500fe1212dSJohn Johansen return 0; 4510fe1212dSJohn Johansen 4520fe1212dSJohn Johansen /* current state is <state>, matching character *str */ 4530fe1212dSJohn Johansen if (dfa->tables[YYTD_ID_EC]) { 4540fe1212dSJohn Johansen /* Equivalence class table defined */ 4550fe1212dSJohn Johansen u8 *equiv = EQUIV_TABLE(dfa); 4560fe1212dSJohn Johansen /* default is direct to next state */ 457074c1cd7SJohn Johansen while (*str) 458074c1cd7SJohn Johansen match_char(state, def, base, next, check, 459074c1cd7SJohn Johansen equiv[(u8) *str++]); 4600fe1212dSJohn Johansen } else { 4610fe1212dSJohn Johansen /* default is direct to next state */ 462074c1cd7SJohn Johansen while (*str) 463074c1cd7SJohn Johansen match_char(state, def, base, next, check, (u8) *str++); 4640fe1212dSJohn Johansen } 4650fe1212dSJohn Johansen 4660fe1212dSJohn Johansen return state; 4670fe1212dSJohn Johansen } 4680fe1212dSJohn Johansen 4690fe1212dSJohn Johansen /** 4700fe1212dSJohn Johansen * aa_dfa_next - step one character to the next state in the dfa 4715d2371e1SZygmunt Krynicki * @dfa: the dfa to traverse (NOT NULL) 4720fe1212dSJohn Johansen * @state: the state to start in 4730fe1212dSJohn Johansen * @c: the input character to transition on 4740fe1212dSJohn Johansen * 4750fe1212dSJohn Johansen * aa_dfa_match will step through the dfa by one input character @c 4760fe1212dSJohn Johansen * 4770fe1212dSJohn Johansen * Returns: state reach after input @c 4780fe1212dSJohn Johansen */ 4790fe1212dSJohn Johansen unsigned int aa_dfa_next(struct aa_dfa *dfa, unsigned int state, 4800fe1212dSJohn Johansen const char c) 4810fe1212dSJohn Johansen { 4820fe1212dSJohn Johansen u16 *def = DEFAULT_TABLE(dfa); 4830fe1212dSJohn Johansen u32 *base = BASE_TABLE(dfa); 4840fe1212dSJohn Johansen u16 *next = NEXT_TABLE(dfa); 4850fe1212dSJohn Johansen u16 *check = CHECK_TABLE(dfa); 4860fe1212dSJohn Johansen 4870fe1212dSJohn Johansen /* current state is <state>, matching character *str */ 4880fe1212dSJohn Johansen if (dfa->tables[YYTD_ID_EC]) { 4890fe1212dSJohn Johansen /* Equivalence class table defined */ 4900fe1212dSJohn Johansen u8 *equiv = EQUIV_TABLE(dfa); 491074c1cd7SJohn Johansen match_char(state, def, base, next, check, equiv[(u8) c]); 492074c1cd7SJohn Johansen } else 493074c1cd7SJohn Johansen match_char(state, def, base, next, check, (u8) c); 4940fe1212dSJohn Johansen 4950fe1212dSJohn Johansen return state; 496e06f75a6SJohn Johansen } 497cf65fabcSJohn Johansen 498cf65fabcSJohn Johansen /** 499cf65fabcSJohn Johansen * aa_dfa_match_until - traverse @dfa until accept state or end of input 500cf65fabcSJohn Johansen * @dfa: the dfa to match @str against (NOT NULL) 501cf65fabcSJohn Johansen * @start: the state of the dfa to start matching in 502cf65fabcSJohn Johansen * @str: the null terminated string of bytes to match against the dfa (NOT NULL) 503cf65fabcSJohn Johansen * @retpos: first character in str after match OR end of string 504cf65fabcSJohn Johansen * 505cf65fabcSJohn Johansen * aa_dfa_match will match @str against the dfa and return the state it 506cf65fabcSJohn Johansen * finished matching in. The final state can be used to look up the accepting 507cf65fabcSJohn Johansen * label, or as the start state of a continuing match. 508cf65fabcSJohn Johansen * 509cf65fabcSJohn Johansen * Returns: final state reached after input is consumed 510cf65fabcSJohn Johansen */ 511cf65fabcSJohn Johansen unsigned int aa_dfa_match_until(struct aa_dfa *dfa, unsigned int start, 512cf65fabcSJohn Johansen const char *str, const char **retpos) 513cf65fabcSJohn Johansen { 514cf65fabcSJohn Johansen u16 *def = DEFAULT_TABLE(dfa); 515cf65fabcSJohn Johansen u32 *base = BASE_TABLE(dfa); 516cf65fabcSJohn Johansen u16 *next = NEXT_TABLE(dfa); 517cf65fabcSJohn Johansen u16 *check = CHECK_TABLE(dfa); 518cf65fabcSJohn Johansen u32 *accept = ACCEPT_TABLE(dfa); 519cf65fabcSJohn Johansen unsigned int state = start, pos; 520cf65fabcSJohn Johansen 521cf65fabcSJohn Johansen if (state == 0) 522cf65fabcSJohn Johansen return 0; 523cf65fabcSJohn Johansen 524cf65fabcSJohn Johansen /* current state is <state>, matching character *str */ 525cf65fabcSJohn Johansen if (dfa->tables[YYTD_ID_EC]) { 526cf65fabcSJohn Johansen /* Equivalence class table defined */ 527cf65fabcSJohn Johansen u8 *equiv = EQUIV_TABLE(dfa); 528cf65fabcSJohn Johansen /* default is direct to next state */ 529cf65fabcSJohn Johansen while (*str) { 530cf65fabcSJohn Johansen pos = base_idx(base[state]) + equiv[(u8) *str++]; 531cf65fabcSJohn Johansen if (check[pos] == state) 532cf65fabcSJohn Johansen state = next[pos]; 533cf65fabcSJohn Johansen else 534cf65fabcSJohn Johansen state = def[state]; 535cf65fabcSJohn Johansen if (accept[state]) 536cf65fabcSJohn Johansen break; 537cf65fabcSJohn Johansen } 538cf65fabcSJohn Johansen } else { 539cf65fabcSJohn Johansen /* default is direct to next state */ 540cf65fabcSJohn Johansen while (*str) { 541cf65fabcSJohn Johansen pos = base_idx(base[state]) + (u8) *str++; 542cf65fabcSJohn Johansen if (check[pos] == state) 543cf65fabcSJohn Johansen state = next[pos]; 544cf65fabcSJohn Johansen else 545cf65fabcSJohn Johansen state = def[state]; 546cf65fabcSJohn Johansen if (accept[state]) 547cf65fabcSJohn Johansen break; 548cf65fabcSJohn Johansen } 549cf65fabcSJohn Johansen } 550cf65fabcSJohn Johansen 551cf65fabcSJohn Johansen *retpos = str; 552cf65fabcSJohn Johansen return state; 553cf65fabcSJohn Johansen } 554cf65fabcSJohn Johansen 555cf65fabcSJohn Johansen /** 556cf65fabcSJohn Johansen * aa_dfa_matchn_until - traverse @dfa until accept or @n bytes consumed 557cf65fabcSJohn Johansen * @dfa: the dfa to match @str against (NOT NULL) 558cf65fabcSJohn Johansen * @start: the state of the dfa to start matching in 559cf65fabcSJohn Johansen * @str: the string of bytes to match against the dfa (NOT NULL) 560cf65fabcSJohn Johansen * @n: length of the string of bytes to match 561cf65fabcSJohn Johansen * @retpos: first character in str after match OR str + n 562cf65fabcSJohn Johansen * 563cf65fabcSJohn Johansen * aa_dfa_match_len will match @str against the dfa and return the state it 564cf65fabcSJohn Johansen * finished matching in. The final state can be used to look up the accepting 565cf65fabcSJohn Johansen * label, or as the start state of a continuing match. 566cf65fabcSJohn Johansen * 567cf65fabcSJohn Johansen * This function will happily match again the 0 byte and only finishes 568cf65fabcSJohn Johansen * when @n input is consumed. 569cf65fabcSJohn Johansen * 570cf65fabcSJohn Johansen * Returns: final state reached after input is consumed 571cf65fabcSJohn Johansen */ 572cf65fabcSJohn Johansen unsigned int aa_dfa_matchn_until(struct aa_dfa *dfa, unsigned int start, 573cf65fabcSJohn Johansen const char *str, int n, const char **retpos) 574cf65fabcSJohn Johansen { 575cf65fabcSJohn Johansen u16 *def = DEFAULT_TABLE(dfa); 576cf65fabcSJohn Johansen u32 *base = BASE_TABLE(dfa); 577cf65fabcSJohn Johansen u16 *next = NEXT_TABLE(dfa); 578cf65fabcSJohn Johansen u16 *check = CHECK_TABLE(dfa); 579cf65fabcSJohn Johansen u32 *accept = ACCEPT_TABLE(dfa); 580cf65fabcSJohn Johansen unsigned int state = start, pos; 581cf65fabcSJohn Johansen 582cf65fabcSJohn Johansen *retpos = NULL; 583cf65fabcSJohn Johansen if (state == 0) 584cf65fabcSJohn Johansen return 0; 585cf65fabcSJohn Johansen 586cf65fabcSJohn Johansen /* current state is <state>, matching character *str */ 587cf65fabcSJohn Johansen if (dfa->tables[YYTD_ID_EC]) { 588cf65fabcSJohn Johansen /* Equivalence class table defined */ 589cf65fabcSJohn Johansen u8 *equiv = EQUIV_TABLE(dfa); 590cf65fabcSJohn Johansen /* default is direct to next state */ 591cf65fabcSJohn Johansen for (; n; n--) { 592cf65fabcSJohn Johansen pos = base_idx(base[state]) + equiv[(u8) *str++]; 593cf65fabcSJohn Johansen if (check[pos] == state) 594cf65fabcSJohn Johansen state = next[pos]; 595cf65fabcSJohn Johansen else 596cf65fabcSJohn Johansen state = def[state]; 597cf65fabcSJohn Johansen if (accept[state]) 598cf65fabcSJohn Johansen break; 599cf65fabcSJohn Johansen } 600cf65fabcSJohn Johansen } else { 601cf65fabcSJohn Johansen /* default is direct to next state */ 602cf65fabcSJohn Johansen for (; n; n--) { 603cf65fabcSJohn Johansen pos = base_idx(base[state]) + (u8) *str++; 604cf65fabcSJohn Johansen if (check[pos] == state) 605cf65fabcSJohn Johansen state = next[pos]; 606cf65fabcSJohn Johansen else 607cf65fabcSJohn Johansen state = def[state]; 608cf65fabcSJohn Johansen if (accept[state]) 609cf65fabcSJohn Johansen break; 610cf65fabcSJohn Johansen } 611cf65fabcSJohn Johansen } 612cf65fabcSJohn Johansen 613cf65fabcSJohn Johansen *retpos = str; 614cf65fabcSJohn Johansen return state; 615cf65fabcSJohn Johansen } 61621f60661SJohn Johansen 61721f60661SJohn Johansen #define inc_wb_pos(wb) \ 61821f60661SJohn Johansen do { \ 61921f60661SJohn Johansen wb->pos = (wb->pos + 1) & (wb->size - 1); \ 62021f60661SJohn Johansen wb->len = (wb->len + 1) & (wb->size - 1); \ 62121f60661SJohn Johansen } while (0) 62221f60661SJohn Johansen 62321f60661SJohn Johansen /* For DFAs that don't support extended tagging of states */ 62421f60661SJohn Johansen static bool is_loop(struct match_workbuf *wb, unsigned int state, 62521f60661SJohn Johansen unsigned int *adjust) 62621f60661SJohn Johansen { 62721f60661SJohn Johansen unsigned int pos = wb->pos; 62821f60661SJohn Johansen unsigned int i; 62921f60661SJohn Johansen 63021f60661SJohn Johansen if (wb->history[pos] < state) 63121f60661SJohn Johansen return false; 63221f60661SJohn Johansen 63321f60661SJohn Johansen for (i = 0; i <= wb->len; i++) { 63421f60661SJohn Johansen if (wb->history[pos] == state) { 63521f60661SJohn Johansen *adjust = i; 63621f60661SJohn Johansen return true; 63721f60661SJohn Johansen } 63821f60661SJohn Johansen if (pos == 0) 63921f60661SJohn Johansen pos = wb->size; 64021f60661SJohn Johansen pos--; 64121f60661SJohn Johansen } 64221f60661SJohn Johansen 64321f60661SJohn Johansen *adjust = i; 64421f60661SJohn Johansen return true; 64521f60661SJohn Johansen } 64621f60661SJohn Johansen 64721f60661SJohn Johansen static unsigned int leftmatch_fb(struct aa_dfa *dfa, unsigned int start, 64821f60661SJohn Johansen const char *str, struct match_workbuf *wb, 64921f60661SJohn Johansen unsigned int *count) 65021f60661SJohn Johansen { 65121f60661SJohn Johansen u16 *def = DEFAULT_TABLE(dfa); 65221f60661SJohn Johansen u32 *base = BASE_TABLE(dfa); 65321f60661SJohn Johansen u16 *next = NEXT_TABLE(dfa); 65421f60661SJohn Johansen u16 *check = CHECK_TABLE(dfa); 65521f60661SJohn Johansen unsigned int state = start, pos; 65621f60661SJohn Johansen 65721f60661SJohn Johansen AA_BUG(!dfa); 65821f60661SJohn Johansen AA_BUG(!str); 65921f60661SJohn Johansen AA_BUG(!wb); 66021f60661SJohn Johansen AA_BUG(!count); 66121f60661SJohn Johansen 66221f60661SJohn Johansen *count = 0; 66321f60661SJohn Johansen if (state == 0) 66421f60661SJohn Johansen return 0; 66521f60661SJohn Johansen 66621f60661SJohn Johansen /* current state is <state>, matching character *str */ 66721f60661SJohn Johansen if (dfa->tables[YYTD_ID_EC]) { 66821f60661SJohn Johansen /* Equivalence class table defined */ 66921f60661SJohn Johansen u8 *equiv = EQUIV_TABLE(dfa); 67021f60661SJohn Johansen /* default is direct to next state */ 67121f60661SJohn Johansen while (*str) { 67221f60661SJohn Johansen unsigned int adjust; 67321f60661SJohn Johansen 67421f60661SJohn Johansen wb->history[wb->pos] = state; 67521f60661SJohn Johansen pos = base_idx(base[state]) + equiv[(u8) *str++]; 67621f60661SJohn Johansen if (check[pos] == state) 67721f60661SJohn Johansen state = next[pos]; 67821f60661SJohn Johansen else 67921f60661SJohn Johansen state = def[state]; 68021f60661SJohn Johansen if (is_loop(wb, state, &adjust)) { 68121f60661SJohn Johansen state = aa_dfa_match(dfa, state, str); 68221f60661SJohn Johansen *count -= adjust; 68321f60661SJohn Johansen goto out; 68421f60661SJohn Johansen } 68521f60661SJohn Johansen inc_wb_pos(wb); 68621f60661SJohn Johansen (*count)++; 68721f60661SJohn Johansen } 68821f60661SJohn Johansen } else { 68921f60661SJohn Johansen /* default is direct to next state */ 69021f60661SJohn Johansen while (*str) { 69121f60661SJohn Johansen unsigned int adjust; 69221f60661SJohn Johansen 69321f60661SJohn Johansen wb->history[wb->pos] = state; 69421f60661SJohn Johansen pos = base_idx(base[state]) + (u8) *str++; 69521f60661SJohn Johansen if (check[pos] == state) 69621f60661SJohn Johansen state = next[pos]; 69721f60661SJohn Johansen else 69821f60661SJohn Johansen state = def[state]; 69921f60661SJohn Johansen if (is_loop(wb, state, &adjust)) { 70021f60661SJohn Johansen state = aa_dfa_match(dfa, state, str); 70121f60661SJohn Johansen *count -= adjust; 70221f60661SJohn Johansen goto out; 70321f60661SJohn Johansen } 70421f60661SJohn Johansen inc_wb_pos(wb); 70521f60661SJohn Johansen (*count)++; 70621f60661SJohn Johansen } 70721f60661SJohn Johansen } 70821f60661SJohn Johansen 70921f60661SJohn Johansen out: 71021f60661SJohn Johansen if (!state) 71121f60661SJohn Johansen *count = 0; 71221f60661SJohn Johansen return state; 71321f60661SJohn Johansen } 71421f60661SJohn Johansen 71521f60661SJohn Johansen /** 71621f60661SJohn Johansen * aa_dfa_leftmatch - traverse @dfa to find state @str stops at 71721f60661SJohn Johansen * @dfa: the dfa to match @str against (NOT NULL) 71821f60661SJohn Johansen * @start: the state of the dfa to start matching in 71921f60661SJohn Johansen * @str: the null terminated string of bytes to match against the dfa (NOT NULL) 72021f60661SJohn Johansen * @count: current count of longest left. 72121f60661SJohn Johansen * 72221f60661SJohn Johansen * aa_dfa_match will match @str against the dfa and return the state it 72321f60661SJohn Johansen * finished matching in. The final state can be used to look up the accepting 72421f60661SJohn Johansen * label, or as the start state of a continuing match. 72521f60661SJohn Johansen * 72621f60661SJohn Johansen * Returns: final state reached after input is consumed 72721f60661SJohn Johansen */ 72821f60661SJohn Johansen unsigned int aa_dfa_leftmatch(struct aa_dfa *dfa, unsigned int start, 72921f60661SJohn Johansen const char *str, unsigned int *count) 73021f60661SJohn Johansen { 73121f60661SJohn Johansen DEFINE_MATCH_WB(wb); 73221f60661SJohn Johansen 73321f60661SJohn Johansen /* TODO: match for extended state dfas */ 73421f60661SJohn Johansen 73521f60661SJohn Johansen return leftmatch_fb(dfa, start, str, &wb, count); 73621f60661SJohn Johansen } 737