match.c 20 KB

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