match.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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 <linux/unaligned.h>
  18. #include "include/lib.h"
  19. #include "include/match.h"
  20. #define base_idx(X) ((X) & 0xffffff)
  21. /**
  22. * unpack_table - unpack a dfa table (one of accept, default, base, next check)
  23. * @blob: data to unpack (NOT NULL)
  24. * @bsize: size of blob
  25. *
  26. * Returns: pointer to table else NULL on failure
  27. *
  28. * NOTE: must be freed by kvfree (not kfree)
  29. */
  30. static struct table_header *unpack_table(char *blob, size_t bsize)
  31. {
  32. struct table_header *table = NULL;
  33. struct table_header th;
  34. size_t tsize;
  35. if (bsize < sizeof(struct table_header))
  36. goto out;
  37. /* loaded td_id's start at 1, subtract 1 now to avoid doing
  38. * it every time we use td_id as an index
  39. */
  40. th.td_id = get_unaligned_be16(blob) - 1;
  41. if (th.td_id > YYTD_ID_MAX)
  42. goto out;
  43. th.td_flags = get_unaligned_be16(blob + 2);
  44. th.td_lolen = get_unaligned_be32(blob + 8);
  45. blob += sizeof(struct table_header);
  46. if (!(th.td_flags == YYTD_DATA16 || th.td_flags == YYTD_DATA32 ||
  47. th.td_flags == YYTD_DATA8))
  48. goto out;
  49. /* if we have a table it must have some entries */
  50. if (th.td_lolen == 0)
  51. goto out;
  52. tsize = table_size(th.td_lolen, th.td_flags);
  53. if (bsize < tsize)
  54. goto out;
  55. table = kvzalloc(tsize, GFP_KERNEL);
  56. if (table) {
  57. table->td_id = th.td_id;
  58. table->td_flags = th.td_flags;
  59. table->td_lolen = th.td_lolen;
  60. if (th.td_flags == YYTD_DATA8)
  61. memcpy(table->td_data, blob, th.td_lolen);
  62. else if (th.td_flags == YYTD_DATA16)
  63. UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
  64. u16, __be16, get_unaligned_be16);
  65. else if (th.td_flags == YYTD_DATA32)
  66. UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
  67. u32, __be32, get_unaligned_be32);
  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 (DEFAULT_TABLE(dfa)[i] >= state_count) {
  146. pr_err("AppArmor DFA default state out of bounds");
  147. goto out;
  148. }
  149. if (BASE_TABLE(dfa)[i] & MATCH_FLAGS_INVALID) {
  150. pr_err("AppArmor DFA state with invalid match flags");
  151. goto out;
  152. }
  153. if ((BASE_TABLE(dfa)[i] & MATCH_FLAG_DIFF_ENCODE)) {
  154. if (!(dfa->flags & YYTH_FLAG_DIFF_ENCODE)) {
  155. pr_err("AppArmor DFA diff encoded transition state without header flag");
  156. goto out;
  157. }
  158. }
  159. if ((BASE_TABLE(dfa)[i] & MATCH_FLAG_OOB_TRANSITION)) {
  160. if (base_idx(BASE_TABLE(dfa)[i]) < dfa->max_oob) {
  161. pr_err("AppArmor DFA out of bad transition out of range");
  162. goto out;
  163. }
  164. if (!(dfa->flags & YYTH_FLAG_OOB_TRANS)) {
  165. pr_err("AppArmor DFA out of bad transition state without header flag");
  166. goto out;
  167. }
  168. }
  169. if (base_idx(BASE_TABLE(dfa)[i]) + 255 >= trans_count) {
  170. pr_err("AppArmor DFA next/check upper bounds error\n");
  171. goto out;
  172. }
  173. }
  174. for (i = 0; i < trans_count; i++) {
  175. if (NEXT_TABLE(dfa)[i] >= state_count)
  176. goto out;
  177. if (CHECK_TABLE(dfa)[i] >= state_count)
  178. goto out;
  179. }
  180. /* Now that all the other tables are verified, verify diffencoding */
  181. for (i = 0; i < state_count; i++) {
  182. size_t j, k;
  183. for (j = i;
  184. ((BASE_TABLE(dfa)[j] & MATCH_FLAG_DIFF_ENCODE) &&
  185. !(BASE_TABLE(dfa)[j] & MARK_DIFF_ENCODE_VERIFIED));
  186. j = k) {
  187. if (BASE_TABLE(dfa)[j] & MARK_DIFF_ENCODE)
  188. /* loop in current chain */
  189. goto out;
  190. k = DEFAULT_TABLE(dfa)[j];
  191. if (j == k)
  192. /* self loop */
  193. goto out;
  194. BASE_TABLE(dfa)[j] |= MARK_DIFF_ENCODE;
  195. }
  196. /* move mark to verified */
  197. for (j = i;
  198. (BASE_TABLE(dfa)[j] & MATCH_FLAG_DIFF_ENCODE);
  199. j = k) {
  200. k = DEFAULT_TABLE(dfa)[j];
  201. if (j < i)
  202. /* jumps to state/chain that has been
  203. * verified
  204. */
  205. break;
  206. BASE_TABLE(dfa)[j] &= ~MARK_DIFF_ENCODE;
  207. BASE_TABLE(dfa)[j] |= MARK_DIFF_ENCODE_VERIFIED;
  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. * @kref: 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. * remap_data16_to_data32 - remap u16 @old table to a u32 based table
  242. * @old: table to remap
  243. *
  244. * Returns: new table with u32 entries instead of u16.
  245. *
  246. * Note: will free @old so caller does not have to
  247. */
  248. static struct table_header *remap_data16_to_data32(struct table_header *old)
  249. {
  250. struct table_header *new;
  251. size_t tsize;
  252. u32 i;
  253. tsize = table_size(old->td_lolen, YYTD_DATA32);
  254. new = kvzalloc(tsize, GFP_KERNEL);
  255. if (!new) {
  256. kvfree(old);
  257. return NULL;
  258. }
  259. new->td_id = old->td_id;
  260. new->td_flags = YYTD_DATA32;
  261. new->td_lolen = old->td_lolen;
  262. for (i = 0; i < old->td_lolen; i++)
  263. TABLE_DATAU32(new)[i] = (u32) TABLE_DATAU16(old)[i];
  264. kvfree(old);
  265. if (is_vmalloc_addr(new))
  266. vm_unmap_aliases();
  267. return new;
  268. }
  269. /**
  270. * aa_dfa_unpack - unpack the binary tables of a serialized dfa
  271. * @blob: aligned serialized stream of data to unpack (NOT NULL)
  272. * @size: size of data to unpack
  273. * @flags: flags controlling what type of accept tables are acceptable
  274. *
  275. * Unpack a dfa that has been serialized. To find information on the dfa
  276. * format look in Documentation/admin-guide/LSM/apparmor.rst
  277. * Assumes the dfa @blob stream has been aligned on a 8 byte boundary
  278. *
  279. * Returns: an unpacked dfa ready for matching or ERR_PTR on failure
  280. */
  281. struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags)
  282. {
  283. int hsize;
  284. int error = -ENOMEM;
  285. char *data = blob;
  286. struct table_header *table = NULL;
  287. struct aa_dfa *dfa = kzalloc_obj(struct aa_dfa);
  288. if (!dfa)
  289. goto fail;
  290. kref_init(&dfa->count);
  291. error = -EPROTO;
  292. /* get dfa table set header */
  293. if (size < sizeof(struct table_set_header))
  294. goto fail;
  295. if (get_unaligned_be32(data) != YYTH_MAGIC)
  296. goto fail;
  297. hsize = get_unaligned_be32(data + 4);
  298. if (size < hsize)
  299. goto fail;
  300. dfa->flags = get_unaligned_be16(data + 12);
  301. if (dfa->flags & ~(YYTH_FLAGS))
  302. goto fail;
  303. /*
  304. * TODO: needed for dfa to support more than 1 oob
  305. * if (dfa->flags & YYTH_FLAGS_OOB_TRANS) {
  306. * if (hsize < 16 + 4)
  307. * goto fail;
  308. * dfa->max_oob = get_unaligned_be32(data + 16);
  309. * if (dfa->max <= MAX_OOB_SUPPORTED) {
  310. * pr_err("AppArmor DFA OOB greater than supported\n");
  311. * goto fail;
  312. * }
  313. * }
  314. */
  315. dfa->max_oob = 1;
  316. data += hsize;
  317. size -= hsize;
  318. while (size > 0) {
  319. table = unpack_table(data, size);
  320. if (!table)
  321. goto fail;
  322. switch (table->td_id) {
  323. case YYTD_ID_ACCEPT:
  324. if (!(table->td_flags & ACCEPT1_FLAGS(flags)))
  325. goto fail;
  326. break;
  327. case YYTD_ID_ACCEPT2:
  328. if (!(table->td_flags & ACCEPT2_FLAGS(flags)))
  329. goto fail;
  330. break;
  331. case YYTD_ID_BASE:
  332. if (table->td_flags != YYTD_DATA32)
  333. goto fail;
  334. break;
  335. case YYTD_ID_DEF:
  336. case YYTD_ID_NXT:
  337. case YYTD_ID_CHK:
  338. if (!(table->td_flags == YYTD_DATA16 ||
  339. table->td_flags == YYTD_DATA32)) {
  340. goto fail;
  341. }
  342. break;
  343. case YYTD_ID_EC:
  344. if (table->td_flags != YYTD_DATA8)
  345. goto fail;
  346. break;
  347. default:
  348. goto fail;
  349. }
  350. /* check for duplicate table entry */
  351. if (dfa->tables[table->td_id])
  352. goto fail;
  353. dfa->tables[table->td_id] = table;
  354. data += table_size(table->td_lolen, table->td_flags);
  355. size -= table_size(table->td_lolen, table->td_flags);
  356. /*
  357. * this remapping has to be done after incrementing data above
  358. * for now straight remap, later have dfa support both
  359. */
  360. switch (table->td_id) {
  361. case YYTD_ID_DEF:
  362. case YYTD_ID_NXT:
  363. case YYTD_ID_CHK:
  364. if (table->td_flags == YYTD_DATA16) {
  365. table = remap_data16_to_data32(table);
  366. if (!table)
  367. goto fail;
  368. }
  369. dfa->tables[table->td_id] = table;
  370. break;
  371. }
  372. table = NULL;
  373. }
  374. error = verify_table_headers(dfa->tables, flags);
  375. if (error)
  376. goto fail;
  377. if (flags & DFA_FLAG_VERIFY_STATES) {
  378. error = verify_dfa(dfa);
  379. if (error)
  380. goto fail;
  381. }
  382. return dfa;
  383. fail:
  384. kvfree(table);
  385. dfa_free(dfa);
  386. return ERR_PTR(error);
  387. }
  388. #define match_char(state, def, base, next, check, C) \
  389. do { \
  390. u32 b = (base)[(state)]; \
  391. unsigned int pos = base_idx(b) + (C); \
  392. if ((check)[pos] != (state)) { \
  393. (state) = (def)[(state)]; \
  394. if (b & MATCH_FLAG_DIFF_ENCODE) \
  395. continue; \
  396. break; \
  397. } \
  398. (state) = (next)[pos]; \
  399. break; \
  400. } while (1)
  401. /**
  402. * aa_dfa_match_len - traverse @dfa to find state @str stops at
  403. * @dfa: the dfa to match @str against (NOT NULL)
  404. * @start: the state of the dfa to start matching in
  405. * @str: the string of bytes to match against the dfa (NOT NULL)
  406. * @len: length of the string of bytes to match
  407. *
  408. * aa_dfa_match_len will match @str against the dfa and return the state it
  409. * finished matching in. The final state can be used to look up the accepting
  410. * label, or as the start state of a continuing match.
  411. *
  412. * This function will happily match again the 0 byte and only finishes
  413. * when @len input is consumed.
  414. *
  415. * Returns: final state reached after input is consumed
  416. */
  417. aa_state_t aa_dfa_match_len(struct aa_dfa *dfa, aa_state_t start,
  418. const char *str, int len)
  419. {
  420. u32 *def = DEFAULT_TABLE(dfa);
  421. u32 *base = BASE_TABLE(dfa);
  422. u32 *next = NEXT_TABLE(dfa);
  423. u32 *check = CHECK_TABLE(dfa);
  424. aa_state_t state = start;
  425. if (state == DFA_NOMATCH)
  426. return DFA_NOMATCH;
  427. /* current state is <state>, matching character *str */
  428. if (dfa->tables[YYTD_ID_EC]) {
  429. /* Equivalence class table defined */
  430. u8 *equiv = EQUIV_TABLE(dfa);
  431. for (; len; len--) {
  432. u8 c = equiv[(u8) *str];
  433. match_char(state, def, base, next, check, c);
  434. str++;
  435. }
  436. } else {
  437. /* default is direct to next state */
  438. for (; len; len--) {
  439. match_char(state, def, base, next, check, (u8) *str);
  440. str++;
  441. }
  442. }
  443. return state;
  444. }
  445. /**
  446. * aa_dfa_match - traverse @dfa to find state @str stops at
  447. * @dfa: the dfa to match @str against (NOT NULL)
  448. * @start: the state of the dfa to start matching in
  449. * @str: the null terminated string of bytes to match against the dfa (NOT NULL)
  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. aa_state_t aa_dfa_match(struct aa_dfa *dfa, aa_state_t start, const char *str)
  458. {
  459. u32 *def = DEFAULT_TABLE(dfa);
  460. u32 *base = BASE_TABLE(dfa);
  461. u32 *next = NEXT_TABLE(dfa);
  462. u32 *check = CHECK_TABLE(dfa);
  463. aa_state_t state = start;
  464. if (state == DFA_NOMATCH)
  465. return DFA_NOMATCH;
  466. /* current state is <state>, matching character *str */
  467. if (dfa->tables[YYTD_ID_EC]) {
  468. /* Equivalence class table defined */
  469. u8 *equiv = EQUIV_TABLE(dfa);
  470. /* default is direct to next state */
  471. while (*str) {
  472. u8 c = equiv[(u8) *str];
  473. match_char(state, def, base, next, check, c);
  474. str++;
  475. }
  476. } else {
  477. /* default is direct to next state */
  478. while (*str) {
  479. match_char(state, def, base, next, check, (u8) *str);
  480. str++;
  481. }
  482. }
  483. return state;
  484. }
  485. /**
  486. * aa_dfa_next - step one character to the next state in the dfa
  487. * @dfa: the dfa to traverse (NOT NULL)
  488. * @state: the state to start in
  489. * @c: the input character to transition on
  490. *
  491. * aa_dfa_match will step through the dfa by one input character @c
  492. *
  493. * Returns: state reach after input @c
  494. */
  495. aa_state_t aa_dfa_next(struct aa_dfa *dfa, aa_state_t state, const char c)
  496. {
  497. u32 *def = DEFAULT_TABLE(dfa);
  498. u32 *base = BASE_TABLE(dfa);
  499. u32 *next = NEXT_TABLE(dfa);
  500. u32 *check = CHECK_TABLE(dfa);
  501. /* current state is <state>, matching character *str */
  502. if (dfa->tables[YYTD_ID_EC]) {
  503. /* Equivalence class table defined */
  504. u8 *equiv = EQUIV_TABLE(dfa);
  505. match_char(state, def, base, next, check, equiv[(u8) c]);
  506. } else
  507. match_char(state, def, base, next, check, (u8) c);
  508. return state;
  509. }
  510. aa_state_t aa_dfa_outofband_transition(struct aa_dfa *dfa, aa_state_t state)
  511. {
  512. u32 *def = DEFAULT_TABLE(dfa);
  513. u32 *base = BASE_TABLE(dfa);
  514. u32 *next = NEXT_TABLE(dfa);
  515. u32 *check = CHECK_TABLE(dfa);
  516. u32 b = (base)[(state)];
  517. if (!(b & MATCH_FLAG_OOB_TRANSITION))
  518. return DFA_NOMATCH;
  519. /* No Equivalence class remapping for outofband transitions */
  520. match_char(state, def, base, next, check, -1);
  521. return state;
  522. }
  523. /**
  524. * aa_dfa_match_until - traverse @dfa until accept state or end of input
  525. * @dfa: the dfa to match @str against (NOT NULL)
  526. * @start: the state of the dfa to start matching in
  527. * @str: the null terminated string of bytes to match against the dfa (NOT NULL)
  528. * @retpos: first character in str after match OR end of string
  529. *
  530. * aa_dfa_match will match @str against the dfa and return the state it
  531. * finished matching in. The final state can be used to look up the accepting
  532. * label, or as the start state of a continuing match.
  533. *
  534. * Returns: final state reached after input is consumed
  535. */
  536. aa_state_t aa_dfa_match_until(struct aa_dfa *dfa, aa_state_t start,
  537. const char *str, const char **retpos)
  538. {
  539. u32 *def = DEFAULT_TABLE(dfa);
  540. u32 *base = BASE_TABLE(dfa);
  541. u32 *next = NEXT_TABLE(dfa);
  542. u32 *check = CHECK_TABLE(dfa);
  543. u32 *accept = ACCEPT_TABLE(dfa);
  544. aa_state_t state = start, pos;
  545. if (state == DFA_NOMATCH)
  546. return DFA_NOMATCH;
  547. /* current state is <state>, matching character *str */
  548. if (dfa->tables[YYTD_ID_EC]) {
  549. /* Equivalence class table defined */
  550. u8 *equiv = EQUIV_TABLE(dfa);
  551. /* default is direct to next state */
  552. while (*str) {
  553. pos = base_idx(base[state]) + equiv[(u8) *str++];
  554. if (check[pos] == state)
  555. state = next[pos];
  556. else
  557. state = def[state];
  558. if (accept[state])
  559. break;
  560. }
  561. } else {
  562. /* default is direct to next state */
  563. while (*str) {
  564. pos = base_idx(base[state]) + (u8) *str++;
  565. if (check[pos] == state)
  566. state = next[pos];
  567. else
  568. state = def[state];
  569. if (accept[state])
  570. break;
  571. }
  572. }
  573. *retpos = str;
  574. return state;
  575. }
  576. /**
  577. * aa_dfa_matchn_until - traverse @dfa until accept or @n bytes consumed
  578. * @dfa: the dfa to match @str against (NOT NULL)
  579. * @start: the state of the dfa to start matching in
  580. * @str: the string of bytes to match against the dfa (NOT NULL)
  581. * @n: length of the string of bytes to match
  582. * @retpos: first character in str after match OR str + n
  583. *
  584. * aa_dfa_match_len will match @str against the dfa and return the state it
  585. * finished matching in. The final state can be used to look up the accepting
  586. * label, or as the start state of a continuing match.
  587. *
  588. * This function will happily match again the 0 byte and only finishes
  589. * when @n input is consumed.
  590. *
  591. * Returns: final state reached after input is consumed
  592. */
  593. aa_state_t aa_dfa_matchn_until(struct aa_dfa *dfa, aa_state_t start,
  594. const char *str, int n, const char **retpos)
  595. {
  596. u32 *def = DEFAULT_TABLE(dfa);
  597. u32 *base = BASE_TABLE(dfa);
  598. u32 *next = NEXT_TABLE(dfa);
  599. u32 *check = CHECK_TABLE(dfa);
  600. u32 *accept = ACCEPT_TABLE(dfa);
  601. aa_state_t state = start, pos;
  602. *retpos = NULL;
  603. if (state == DFA_NOMATCH)
  604. return DFA_NOMATCH;
  605. /* current state is <state>, matching character *str */
  606. if (dfa->tables[YYTD_ID_EC]) {
  607. /* Equivalence class table defined */
  608. u8 *equiv = EQUIV_TABLE(dfa);
  609. /* default is direct to next state */
  610. for (; n; n--) {
  611. pos = base_idx(base[state]) + equiv[(u8) *str++];
  612. if (check[pos] == state)
  613. state = next[pos];
  614. else
  615. state = def[state];
  616. if (accept[state])
  617. break;
  618. }
  619. } else {
  620. /* default is direct to next state */
  621. for (; n; n--) {
  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 (accept[state])
  628. break;
  629. }
  630. }
  631. *retpos = str;
  632. return state;
  633. }
  634. #define inc_wb_pos(wb) \
  635. do { \
  636. BUILD_BUG_ON_NOT_POWER_OF_2(WB_HISTORY_SIZE); \
  637. wb->pos = (wb->pos + 1) & (WB_HISTORY_SIZE - 1); \
  638. wb->len = (wb->len + 1) > WB_HISTORY_SIZE ? WB_HISTORY_SIZE : \
  639. wb->len + 1; \
  640. } while (0)
  641. /* For DFAs that don't support extended tagging of states */
  642. /* adjust is only set if is_loop returns true */
  643. static bool is_loop(struct match_workbuf *wb, aa_state_t state,
  644. unsigned int *adjust)
  645. {
  646. int pos = wb->pos;
  647. int i;
  648. if (wb->history[pos] < state)
  649. return false;
  650. for (i = 0; i < wb->len; i++) {
  651. if (wb->history[pos] == state) {
  652. *adjust = i;
  653. return true;
  654. }
  655. /* -1 wraps to WB_HISTORY_SIZE - 1 */
  656. pos = (pos - 1) & (WB_HISTORY_SIZE - 1);
  657. }
  658. return false;
  659. }
  660. static aa_state_t leftmatch_fb(struct aa_dfa *dfa, aa_state_t start,
  661. const char *str, struct match_workbuf *wb,
  662. unsigned int *count)
  663. {
  664. u32 *def = DEFAULT_TABLE(dfa);
  665. u32 *base = BASE_TABLE(dfa);
  666. u32 *next = NEXT_TABLE(dfa);
  667. u32 *check = CHECK_TABLE(dfa);
  668. aa_state_t state = start, pos;
  669. AA_BUG(!dfa);
  670. AA_BUG(!str);
  671. AA_BUG(!wb);
  672. AA_BUG(!count);
  673. *count = 0;
  674. if (state == DFA_NOMATCH)
  675. return DFA_NOMATCH;
  676. /* current state is <state>, matching character *str */
  677. if (dfa->tables[YYTD_ID_EC]) {
  678. /* Equivalence class table defined */
  679. u8 *equiv = EQUIV_TABLE(dfa);
  680. /* default is direct to next state */
  681. while (*str) {
  682. unsigned int adjust;
  683. wb->history[wb->pos] = state;
  684. pos = base_idx(base[state]) + equiv[(u8) *str++];
  685. if (check[pos] == state)
  686. state = next[pos];
  687. else
  688. state = def[state];
  689. if (is_loop(wb, state, &adjust)) {
  690. state = aa_dfa_match(dfa, state, str);
  691. *count -= adjust;
  692. goto out;
  693. }
  694. inc_wb_pos(wb);
  695. (*count)++;
  696. }
  697. } else {
  698. /* default is direct to next state */
  699. while (*str) {
  700. unsigned int adjust;
  701. wb->history[wb->pos] = state;
  702. pos = base_idx(base[state]) + (u8) *str++;
  703. if (check[pos] == state)
  704. state = next[pos];
  705. else
  706. state = def[state];
  707. if (is_loop(wb, state, &adjust)) {
  708. state = aa_dfa_match(dfa, state, str);
  709. *count -= adjust;
  710. goto out;
  711. }
  712. inc_wb_pos(wb);
  713. (*count)++;
  714. }
  715. }
  716. out:
  717. if (!state)
  718. *count = 0;
  719. return state;
  720. }
  721. /**
  722. * aa_dfa_leftmatch - traverse @dfa to find state @str stops at
  723. * @dfa: the dfa to match @str against (NOT NULL)
  724. * @start: the state of the dfa to start matching in
  725. * @str: the null terminated string of bytes to match against the dfa (NOT NULL)
  726. * @count: current count of longest left.
  727. *
  728. * aa_dfa_match will match @str against the dfa and return the state it
  729. * finished matching in. The final state can be used to look up the accepting
  730. * label, or as the start state of a continuing match.
  731. *
  732. * Returns: final state reached after input is consumed
  733. */
  734. aa_state_t aa_dfa_leftmatch(struct aa_dfa *dfa, aa_state_t start,
  735. const char *str, unsigned int *count)
  736. {
  737. DEFINE_MATCH_WB(wb);
  738. /* TODO: match for extended state dfas */
  739. return leftmatch_fb(dfa, start, str, &wb, count);
  740. }