match.c 19 KB

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