match.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor dfa based regular expression matching engine
  5. *
  6. * Copyright (C) 1998-2008 Novell/SUSE
  7. * Copyright 2009-2012 Canonical Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation, version 2 of the
  12. * License.
  13. */
  14. #include <linux/errno.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/slab.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/err.h>
  20. #include <linux/kref.h>
  21. #include "include/lib.h"
  22. #include "include/match.h"
  23. #define base_idx(X) ((X) & 0xffffff)
  24. static char nulldfa_src[] = {
  25. #include "nulldfa.in"
  26. };
  27. struct aa_dfa *nulldfa;
  28. static char stacksplitdfa_src[] = {
  29. #include "stacksplitdfa.in"
  30. };
  31. struct aa_dfa *stacksplitdfa;
  32. int aa_setup_dfa_engine(void)
  33. {
  34. int error;
  35. nulldfa = aa_dfa_unpack(nulldfa_src, sizeof(nulldfa_src),
  36. TO_ACCEPT1_FLAG(YYTD_DATA32) |
  37. TO_ACCEPT2_FLAG(YYTD_DATA32));
  38. if (IS_ERR(nulldfa)) {
  39. error = PTR_ERR(nulldfa);
  40. nulldfa = NULL;
  41. return error;
  42. }
  43. stacksplitdfa = aa_dfa_unpack(stacksplitdfa_src,
  44. sizeof(stacksplitdfa_src),
  45. TO_ACCEPT1_FLAG(YYTD_DATA32) |
  46. TO_ACCEPT2_FLAG(YYTD_DATA32));
  47. if (IS_ERR(stacksplitdfa)) {
  48. aa_put_dfa(nulldfa);
  49. nulldfa = NULL;
  50. error = PTR_ERR(stacksplitdfa);
  51. stacksplitdfa = NULL;
  52. return error;
  53. }
  54. return 0;
  55. }
  56. void aa_teardown_dfa_engine(void)
  57. {
  58. aa_put_dfa(stacksplitdfa);
  59. aa_put_dfa(nulldfa);
  60. }
  61. /**
  62. * unpack_table - unpack a dfa table (one of accept, default, base, next check)
  63. * @blob: data to unpack (NOT NULL)
  64. * @bsize: size of blob
  65. *
  66. * Returns: pointer to table else NULL on failure
  67. *
  68. * NOTE: must be freed by kvfree (not kfree)
  69. */
  70. static struct table_header *unpack_table(char *blob, size_t bsize)
  71. {
  72. struct table_header *table = NULL;
  73. struct table_header th;
  74. size_t tsize;
  75. if (bsize < sizeof(struct table_header))
  76. goto out;
  77. /* loaded td_id's start at 1, subtract 1 now to avoid doing
  78. * it every time we use td_id as an index
  79. */
  80. th.td_id = be16_to_cpu(*(__be16 *) (blob)) - 1;
  81. if (th.td_id > YYTD_ID_MAX)
  82. goto out;
  83. th.td_flags = be16_to_cpu(*(__be16 *) (blob + 2));
  84. th.td_lolen = be32_to_cpu(*(__be32 *) (blob + 8));
  85. blob += sizeof(struct table_header);
  86. if (!(th.td_flags == YYTD_DATA16 || th.td_flags == YYTD_DATA32 ||
  87. th.td_flags == YYTD_DATA8))
  88. goto out;
  89. tsize = table_size(th.td_lolen, th.td_flags);
  90. if (bsize < tsize)
  91. goto out;
  92. table = kvzalloc(tsize, GFP_KERNEL);
  93. if (table) {
  94. table->td_id = th.td_id;
  95. table->td_flags = th.td_flags;
  96. table->td_lolen = th.td_lolen;
  97. if (th.td_flags == YYTD_DATA8)
  98. UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
  99. u8, u8, byte_to_byte);
  100. else if (th.td_flags == YYTD_DATA16)
  101. UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
  102. u16, __be16, be16_to_cpu);
  103. else if (th.td_flags == YYTD_DATA32)
  104. UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
  105. u32, __be32, be32_to_cpu);
  106. else
  107. goto fail;
  108. /* if table was vmalloced make sure the page tables are synced
  109. * before it is used, as it goes live to all cpus.
  110. */
  111. if (is_vmalloc_addr(table))
  112. vm_unmap_aliases();
  113. }
  114. out:
  115. return table;
  116. fail:
  117. kvfree(table);
  118. return NULL;
  119. }
  120. /**
  121. * verify_table_headers - verify that the tables headers are as expected
  122. * @tables - array of dfa tables to check (NOT NULL)
  123. * @flags: flags controlling what type of accept table are acceptable
  124. *
  125. * Assumes dfa has gone through the first pass verification done by unpacking
  126. * NOTE: this does not valid accept table values
  127. *
  128. * Returns: %0 else error code on failure to verify
  129. */
  130. static int verify_table_headers(struct table_header **tables, int flags)
  131. {
  132. size_t state_count, trans_count;
  133. int error = -EPROTO;
  134. /* check that required tables exist */
  135. if (!(tables[YYTD_ID_DEF] && tables[YYTD_ID_BASE] &&
  136. tables[YYTD_ID_NXT] && tables[YYTD_ID_CHK]))
  137. goto out;
  138. /* accept.size == default.size == base.size */
  139. state_count = tables[YYTD_ID_BASE]->td_lolen;
  140. if (ACCEPT1_FLAGS(flags)) {
  141. if (!tables[YYTD_ID_ACCEPT])
  142. goto out;
  143. if (state_count != tables[YYTD_ID_ACCEPT]->td_lolen)
  144. goto out;
  145. }
  146. if (ACCEPT2_FLAGS(flags)) {
  147. if (!tables[YYTD_ID_ACCEPT2])
  148. goto out;
  149. if (state_count != tables[YYTD_ID_ACCEPT2]->td_lolen)
  150. goto out;
  151. }
  152. if (state_count != tables[YYTD_ID_DEF]->td_lolen)
  153. goto out;
  154. /* next.size == chk.size */
  155. trans_count = tables[YYTD_ID_NXT]->td_lolen;
  156. if (trans_count != tables[YYTD_ID_CHK]->td_lolen)
  157. goto out;
  158. /* if equivalence classes then its table size must be 256 */
  159. if (tables[YYTD_ID_EC] && tables[YYTD_ID_EC]->td_lolen != 256)
  160. goto out;
  161. error = 0;
  162. out:
  163. return error;
  164. }
  165. /**
  166. * verify_dfa - verify that transitions and states in the tables are in bounds.
  167. * @dfa: dfa to test (NOT NULL)
  168. *
  169. * Assumes dfa has gone through the first pass verification done by unpacking
  170. * NOTE: this does not valid accept table values
  171. *
  172. * Returns: %0 else error code on failure to verify
  173. */
  174. static int verify_dfa(struct aa_dfa *dfa)
  175. {
  176. size_t i, state_count, trans_count;
  177. int error = -EPROTO;
  178. state_count = dfa->tables[YYTD_ID_BASE]->td_lolen;
  179. trans_count = dfa->tables[YYTD_ID_NXT]->td_lolen;
  180. for (i = 0; i < state_count; i++) {
  181. if (!(BASE_TABLE(dfa)[i] & MATCH_FLAG_DIFF_ENCODE) &&
  182. (DEFAULT_TABLE(dfa)[i] >= state_count))
  183. goto out;
  184. if (base_idx(BASE_TABLE(dfa)[i]) + 255 >= trans_count) {
  185. pr_err("AppArmor DFA next/check upper bounds error\n");
  186. goto out;
  187. }
  188. }
  189. for (i = 0; i < trans_count; i++) {
  190. if (NEXT_TABLE(dfa)[i] >= state_count)
  191. goto out;
  192. if (CHECK_TABLE(dfa)[i] >= state_count)
  193. goto out;
  194. }
  195. /* Now that all the other tables are verified, verify diffencoding */
  196. for (i = 0; i < state_count; i++) {
  197. size_t j, k;
  198. for (j = i;
  199. (BASE_TABLE(dfa)[j] & MATCH_FLAG_DIFF_ENCODE) &&
  200. !(BASE_TABLE(dfa)[j] & MARK_DIFF_ENCODE);
  201. j = k) {
  202. k = DEFAULT_TABLE(dfa)[j];
  203. if (j == k)
  204. goto out;
  205. if (k < j)
  206. break; /* already verified */
  207. BASE_TABLE(dfa)[j] |= MARK_DIFF_ENCODE;
  208. }
  209. }
  210. error = 0;
  211. out:
  212. return error;
  213. }
  214. /**
  215. * dfa_free - free a dfa allocated by aa_dfa_unpack
  216. * @dfa: the dfa to free (MAYBE NULL)
  217. *
  218. * Requires: reference count to dfa == 0
  219. */
  220. static void dfa_free(struct aa_dfa *dfa)
  221. {
  222. if (dfa) {
  223. int i;
  224. for (i = 0; i < ARRAY_SIZE(dfa->tables); i++) {
  225. kvfree(dfa->tables[i]);
  226. dfa->tables[i] = NULL;
  227. }
  228. kfree(dfa);
  229. }
  230. }
  231. /**
  232. * aa_dfa_free_kref - free aa_dfa by kref (called by aa_put_dfa)
  233. * @kr: kref callback for freeing of a dfa (NOT NULL)
  234. */
  235. void aa_dfa_free_kref(struct kref *kref)
  236. {
  237. struct aa_dfa *dfa = container_of(kref, struct aa_dfa, count);
  238. dfa_free(dfa);
  239. }
  240. /**
  241. * aa_dfa_unpack - unpack the binary tables of a serialized dfa
  242. * @blob: aligned serialized stream of data to unpack (NOT NULL)
  243. * @size: size of data to unpack
  244. * @flags: flags controlling what type of accept tables are acceptable
  245. *
  246. * Unpack a dfa that has been serialized. To find information on the dfa
  247. * format look in Documentation/admin-guide/LSM/apparmor.rst
  248. * Assumes the dfa @blob stream has been aligned on a 8 byte boundary
  249. *
  250. * Returns: an unpacked dfa ready for matching or ERR_PTR on failure
  251. */
  252. struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags)
  253. {
  254. int hsize;
  255. int error = -ENOMEM;
  256. char *data = blob;
  257. struct table_header *table = NULL;
  258. struct aa_dfa *dfa = kzalloc(sizeof(struct aa_dfa), GFP_KERNEL);
  259. if (!dfa)
  260. goto fail;
  261. kref_init(&dfa->count);
  262. error = -EPROTO;
  263. /* get dfa table set header */
  264. if (size < sizeof(struct table_set_header))
  265. goto fail;
  266. if (ntohl(*(__be32 *) data) != YYTH_MAGIC)
  267. goto fail;
  268. hsize = ntohl(*(__be32 *) (data + 4));
  269. if (size < hsize)
  270. goto fail;
  271. dfa->flags = ntohs(*(__be16 *) (data + 12));
  272. if (dfa->flags != 0 && dfa->flags != YYTH_FLAG_DIFF_ENCODE)
  273. goto fail;
  274. data += hsize;
  275. size -= hsize;
  276. while (size > 0) {
  277. table = unpack_table(data, size);
  278. if (!table)
  279. goto fail;
  280. switch (table->td_id) {
  281. case YYTD_ID_ACCEPT:
  282. if (!(table->td_flags & ACCEPT1_FLAGS(flags)))
  283. goto fail;
  284. break;
  285. case YYTD_ID_ACCEPT2:
  286. if (!(table->td_flags & ACCEPT2_FLAGS(flags)))
  287. goto fail;
  288. break;
  289. case YYTD_ID_BASE:
  290. if (table->td_flags != YYTD_DATA32)
  291. goto fail;
  292. break;
  293. case YYTD_ID_DEF:
  294. case YYTD_ID_NXT:
  295. case YYTD_ID_CHK:
  296. if (table->td_flags != YYTD_DATA16)
  297. goto fail;
  298. break;
  299. case YYTD_ID_EC:
  300. if (table->td_flags != YYTD_DATA8)
  301. goto fail;
  302. break;
  303. default:
  304. goto fail;
  305. }
  306. /* check for duplicate table entry */
  307. if (dfa->tables[table->td_id])
  308. goto fail;
  309. dfa->tables[table->td_id] = table;
  310. data += table_size(table->td_lolen, table->td_flags);
  311. size -= table_size(table->td_lolen, table->td_flags);
  312. table = NULL;
  313. }
  314. error = verify_table_headers(dfa->tables, flags);
  315. if (error)
  316. goto fail;
  317. if (flags & DFA_FLAG_VERIFY_STATES) {
  318. error = verify_dfa(dfa);
  319. if (error)
  320. goto fail;
  321. }
  322. return dfa;
  323. fail:
  324. kvfree(table);
  325. dfa_free(dfa);
  326. return ERR_PTR(error);
  327. }
  328. #define match_char(state, def, base, next, check, C) \
  329. do { \
  330. u32 b = (base)[(state)]; \
  331. unsigned int pos = base_idx(b) + (C); \
  332. if ((check)[pos] != (state)) { \
  333. (state) = (def)[(state)]; \
  334. if (b & MATCH_FLAG_DIFF_ENCODE) \
  335. continue; \
  336. break; \
  337. } \
  338. (state) = (next)[pos]; \
  339. break; \
  340. } while (1)
  341. /**
  342. * aa_dfa_match_len - traverse @dfa to find state @str stops at
  343. * @dfa: the dfa to match @str against (NOT NULL)
  344. * @start: the state of the dfa to start matching in
  345. * @str: the string of bytes to match against the dfa (NOT NULL)
  346. * @len: length of the string of bytes to match
  347. *
  348. * aa_dfa_match_len will match @str against the dfa and return the state it
  349. * finished matching in. The final state can be used to look up the accepting
  350. * label, or as the start state of a continuing match.
  351. *
  352. * This function will happily match again the 0 byte and only finishes
  353. * when @len input is consumed.
  354. *
  355. * Returns: final state reached after input is consumed
  356. */
  357. unsigned int aa_dfa_match_len(struct aa_dfa *dfa, unsigned int start,
  358. const char *str, int len)
  359. {
  360. u16 *def = DEFAULT_TABLE(dfa);
  361. u32 *base = BASE_TABLE(dfa);
  362. u16 *next = NEXT_TABLE(dfa);
  363. u16 *check = CHECK_TABLE(dfa);
  364. unsigned int state = start;
  365. if (state == 0)
  366. return 0;
  367. /* current state is <state>, matching character *str */
  368. if (dfa->tables[YYTD_ID_EC]) {
  369. /* Equivalence class table defined */
  370. u8 *equiv = EQUIV_TABLE(dfa);
  371. for (; len; len--)
  372. match_char(state, def, base, next, check,
  373. equiv[(u8) *str++]);
  374. } else {
  375. /* default is direct to next state */
  376. for (; len; len--)
  377. match_char(state, def, base, next, check, (u8) *str++);
  378. }
  379. return state;
  380. }
  381. /**
  382. * aa_dfa_match - traverse @dfa to find state @str stops at
  383. * @dfa: the dfa to match @str against (NOT NULL)
  384. * @start: the state of the dfa to start matching in
  385. * @str: the null terminated string of bytes to match against the dfa (NOT NULL)
  386. *
  387. * aa_dfa_match will match @str against the dfa and return the state it
  388. * finished matching in. The final state can be used to look up the accepting
  389. * label, or as the start state of a continuing match.
  390. *
  391. * Returns: final state reached after input is consumed
  392. */
  393. unsigned int aa_dfa_match(struct aa_dfa *dfa, unsigned int start,
  394. const char *str)
  395. {
  396. u16 *def = DEFAULT_TABLE(dfa);
  397. u32 *base = BASE_TABLE(dfa);
  398. u16 *next = NEXT_TABLE(dfa);
  399. u16 *check = CHECK_TABLE(dfa);
  400. unsigned int state = start;
  401. if (state == 0)
  402. return 0;
  403. /* current state is <state>, matching character *str */
  404. if (dfa->tables[YYTD_ID_EC]) {
  405. /* Equivalence class table defined */
  406. u8 *equiv = EQUIV_TABLE(dfa);
  407. /* default is direct to next state */
  408. while (*str)
  409. match_char(state, def, base, next, check,
  410. equiv[(u8) *str++]);
  411. } else {
  412. /* default is direct to next state */
  413. while (*str)
  414. match_char(state, def, base, next, check, (u8) *str++);
  415. }
  416. return state;
  417. }
  418. /**
  419. * aa_dfa_next - step one character to the next state in the dfa
  420. * @dfa: the dfa to traverse (NOT NULL)
  421. * @state: the state to start in
  422. * @c: the input character to transition on
  423. *
  424. * aa_dfa_match will step through the dfa by one input character @c
  425. *
  426. * Returns: state reach after input @c
  427. */
  428. unsigned int aa_dfa_next(struct aa_dfa *dfa, unsigned int state,
  429. const char c)
  430. {
  431. u16 *def = DEFAULT_TABLE(dfa);
  432. u32 *base = BASE_TABLE(dfa);
  433. u16 *next = NEXT_TABLE(dfa);
  434. u16 *check = CHECK_TABLE(dfa);
  435. /* current state is <state>, matching character *str */
  436. if (dfa->tables[YYTD_ID_EC]) {
  437. /* Equivalence class table defined */
  438. u8 *equiv = EQUIV_TABLE(dfa);
  439. match_char(state, def, base, next, check, equiv[(u8) c]);
  440. } else
  441. match_char(state, def, base, next, check, (u8) c);
  442. return state;
  443. }
  444. /**
  445. * aa_dfa_match_until - traverse @dfa until accept state or end of input
  446. * @dfa: the dfa to match @str against (NOT NULL)
  447. * @start: the state of the dfa to start matching in
  448. * @str: the null terminated string of bytes to match against the dfa (NOT NULL)
  449. * @retpos: first character in str after match OR end of string
  450. *
  451. * aa_dfa_match will match @str against the dfa and return the state it
  452. * finished matching in. The final state can be used to look up the accepting
  453. * label, or as the start state of a continuing match.
  454. *
  455. * Returns: final state reached after input is consumed
  456. */
  457. unsigned int aa_dfa_match_until(struct aa_dfa *dfa, unsigned int start,
  458. const char *str, const char **retpos)
  459. {
  460. u16 *def = DEFAULT_TABLE(dfa);
  461. u32 *base = BASE_TABLE(dfa);
  462. u16 *next = NEXT_TABLE(dfa);
  463. u16 *check = CHECK_TABLE(dfa);
  464. u32 *accept = ACCEPT_TABLE(dfa);
  465. unsigned int state = start, pos;
  466. if (state == 0)
  467. return 0;
  468. /* current state is <state>, matching character *str */
  469. if (dfa->tables[YYTD_ID_EC]) {
  470. /* Equivalence class table defined */
  471. u8 *equiv = EQUIV_TABLE(dfa);
  472. /* default is direct to next state */
  473. while (*str) {
  474. pos = base_idx(base[state]) + equiv[(u8) *str++];
  475. if (check[pos] == state)
  476. state = next[pos];
  477. else
  478. state = def[state];
  479. if (accept[state])
  480. break;
  481. }
  482. } else {
  483. /* default is direct to next state */
  484. while (*str) {
  485. pos = base_idx(base[state]) + (u8) *str++;
  486. if (check[pos] == state)
  487. state = next[pos];
  488. else
  489. state = def[state];
  490. if (accept[state])
  491. break;
  492. }
  493. }
  494. *retpos = str;
  495. return state;
  496. }
  497. /**
  498. * aa_dfa_matchn_until - traverse @dfa until accept or @n bytes consumed
  499. * @dfa: the dfa to match @str against (NOT NULL)
  500. * @start: the state of the dfa to start matching in
  501. * @str: the string of bytes to match against the dfa (NOT NULL)
  502. * @n: length of the string of bytes to match
  503. * @retpos: first character in str after match OR str + n
  504. *
  505. * aa_dfa_match_len will match @str against the dfa and return the state it
  506. * finished matching in. The final state can be used to look up the accepting
  507. * label, or as the start state of a continuing match.
  508. *
  509. * This function will happily match again the 0 byte and only finishes
  510. * when @n input is consumed.
  511. *
  512. * Returns: final state reached after input is consumed
  513. */
  514. unsigned int aa_dfa_matchn_until(struct aa_dfa *dfa, unsigned int start,
  515. const char *str, int n, const char **retpos)
  516. {
  517. u16 *def = DEFAULT_TABLE(dfa);
  518. u32 *base = BASE_TABLE(dfa);
  519. u16 *next = NEXT_TABLE(dfa);
  520. u16 *check = CHECK_TABLE(dfa);
  521. u32 *accept = ACCEPT_TABLE(dfa);
  522. unsigned int state = start, pos;
  523. *retpos = NULL;
  524. if (state == 0)
  525. return 0;
  526. /* current state is <state>, matching character *str */
  527. if (dfa->tables[YYTD_ID_EC]) {
  528. /* Equivalence class table defined */
  529. u8 *equiv = EQUIV_TABLE(dfa);
  530. /* default is direct to next state */
  531. for (; n; n--) {
  532. pos = base_idx(base[state]) + equiv[(u8) *str++];
  533. if (check[pos] == state)
  534. state = next[pos];
  535. else
  536. state = def[state];
  537. if (accept[state])
  538. break;
  539. }
  540. } else {
  541. /* default is direct to next state */
  542. for (; n; n--) {
  543. pos = base_idx(base[state]) + (u8) *str++;
  544. if (check[pos] == state)
  545. state = next[pos];
  546. else
  547. state = def[state];
  548. if (accept[state])
  549. break;
  550. }
  551. }
  552. *retpos = str;
  553. return state;
  554. }
  555. #define inc_wb_pos(wb) \
  556. do { \
  557. wb->pos = (wb->pos + 1) & (wb->size - 1); \
  558. wb->len = (wb->len + 1) & (wb->size - 1); \
  559. } while (0)
  560. /* For DFAs that don't support extended tagging of states */
  561. static bool is_loop(struct match_workbuf *wb, unsigned int state,
  562. unsigned int *adjust)
  563. {
  564. unsigned int pos = wb->pos;
  565. unsigned int i;
  566. if (wb->history[pos] < state)
  567. return false;
  568. for (i = 0; i <= wb->len; i++) {
  569. if (wb->history[pos] == state) {
  570. *adjust = i;
  571. return true;
  572. }
  573. if (pos == 0)
  574. pos = wb->size;
  575. pos--;
  576. }
  577. *adjust = i;
  578. return true;
  579. }
  580. static unsigned int leftmatch_fb(struct aa_dfa *dfa, unsigned int start,
  581. const char *str, struct match_workbuf *wb,
  582. unsigned int *count)
  583. {
  584. u16 *def = DEFAULT_TABLE(dfa);
  585. u32 *base = BASE_TABLE(dfa);
  586. u16 *next = NEXT_TABLE(dfa);
  587. u16 *check = CHECK_TABLE(dfa);
  588. unsigned int state = start, pos;
  589. AA_BUG(!dfa);
  590. AA_BUG(!str);
  591. AA_BUG(!wb);
  592. AA_BUG(!count);
  593. *count = 0;
  594. if (state == 0)
  595. return 0;
  596. /* current state is <state>, matching character *str */
  597. if (dfa->tables[YYTD_ID_EC]) {
  598. /* Equivalence class table defined */
  599. u8 *equiv = EQUIV_TABLE(dfa);
  600. /* default is direct to next state */
  601. while (*str) {
  602. unsigned int adjust;
  603. wb->history[wb->pos] = state;
  604. pos = base_idx(base[state]) + equiv[(u8) *str++];
  605. if (check[pos] == state)
  606. state = next[pos];
  607. else
  608. state = def[state];
  609. if (is_loop(wb, state, &adjust)) {
  610. state = aa_dfa_match(dfa, state, str);
  611. *count -= adjust;
  612. goto out;
  613. }
  614. inc_wb_pos(wb);
  615. (*count)++;
  616. }
  617. } else {
  618. /* default is direct to next state */
  619. while (*str) {
  620. unsigned int adjust;
  621. wb->history[wb->pos] = state;
  622. pos = base_idx(base[state]) + (u8) *str++;
  623. if (check[pos] == state)
  624. state = next[pos];
  625. else
  626. state = def[state];
  627. if (is_loop(wb, state, &adjust)) {
  628. state = aa_dfa_match(dfa, state, str);
  629. *count -= adjust;
  630. goto out;
  631. }
  632. inc_wb_pos(wb);
  633. (*count)++;
  634. }
  635. }
  636. out:
  637. if (!state)
  638. *count = 0;
  639. return state;
  640. }
  641. /**
  642. * aa_dfa_leftmatch - traverse @dfa to find state @str stops at
  643. * @dfa: the dfa to match @str against (NOT NULL)
  644. * @start: the state of the dfa to start matching in
  645. * @str: the null terminated string of bytes to match against the dfa (NOT NULL)
  646. * @count: current count of longest left.
  647. *
  648. * aa_dfa_match will match @str against the dfa and return the state it
  649. * finished matching in. The final state can be used to look up the accepting
  650. * label, or as the start state of a continuing match.
  651. *
  652. * Returns: final state reached after input is consumed
  653. */
  654. unsigned int aa_dfa_leftmatch(struct aa_dfa *dfa, unsigned int start,
  655. const char *str, unsigned int *count)
  656. {
  657. DEFINE_MATCH_WB(wb);
  658. /* TODO: match for extended state dfas */
  659. return leftmatch_fb(dfa, start, str, &wb, count);
  660. }