match.c 19 KB

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