match.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor dfa based regular expression matching engine
  5. *
  6. * Copyright (C) 1998-2008 Novell/SUSE
  7. * Copyright 2009-2012 Canonical Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation, version 2 of the
  12. * License.
  13. */
  14. #include <linux/errno.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/slab.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/err.h>
  20. #include <linux/kref.h>
  21. #include "include/lib.h"
  22. #include "include/match.h"
  23. #define base_idx(X) ((X) & 0xffffff)
  24. static char nulldfa_src[] = {
  25. #include "nulldfa.in"
  26. };
  27. struct aa_dfa *nulldfa;
  28. static char stacksplitdfa_src[] = {
  29. #include "stacksplitdfa.in"
  30. };
  31. struct aa_dfa *stacksplitdfa;
  32. int aa_setup_dfa_engine(void)
  33. {
  34. int error;
  35. nulldfa = aa_dfa_unpack(nulldfa_src, sizeof(nulldfa_src),
  36. TO_ACCEPT1_FLAG(YYTD_DATA32) |
  37. TO_ACCEPT2_FLAG(YYTD_DATA32));
  38. if (IS_ERR(nulldfa)) {
  39. error = PTR_ERR(nulldfa);
  40. nulldfa = NULL;
  41. return error;
  42. }
  43. stacksplitdfa = aa_dfa_unpack(stacksplitdfa_src,
  44. sizeof(stacksplitdfa_src),
  45. TO_ACCEPT1_FLAG(YYTD_DATA32) |
  46. TO_ACCEPT2_FLAG(YYTD_DATA32));
  47. if (IS_ERR(stacksplitdfa)) {
  48. aa_put_dfa(nulldfa);
  49. nulldfa = NULL;
  50. error = PTR_ERR(stacksplitdfa);
  51. stacksplitdfa = NULL;
  52. return error;
  53. }
  54. return 0;
  55. }
  56. void aa_teardown_dfa_engine(void)
  57. {
  58. aa_put_dfa(stacksplitdfa);
  59. aa_put_dfa(nulldfa);
  60. }
  61. /**
  62. * unpack_table - unpack a dfa table (one of accept, default, base, next check)
  63. * @blob: data to unpack (NOT NULL)
  64. * @bsize: size of blob
  65. *
  66. * Returns: pointer to table else NULL on failure
  67. *
  68. * NOTE: must be freed by kvfree (not kfree)
  69. */
  70. static struct table_header *unpack_table(char *blob, size_t bsize)
  71. {
  72. struct table_header *table = NULL;
  73. struct table_header th;
  74. size_t tsize;
  75. if (bsize < sizeof(struct table_header))
  76. goto out;
  77. /* loaded td_id's start at 1, subtract 1 now to avoid doing
  78. * it every time we use td_id as an index
  79. */
  80. th.td_id = be16_to_cpu(*(__be16 *) (blob)) - 1;
  81. if (th.td_id > YYTD_ID_MAX)
  82. goto out;
  83. th.td_flags = be16_to_cpu(*(__be16 *) (blob + 2));
  84. th.td_lolen = be32_to_cpu(*(__be32 *) (blob + 8));
  85. blob += sizeof(struct table_header);
  86. if (!(th.td_flags == YYTD_DATA16 || th.td_flags == YYTD_DATA32 ||
  87. th.td_flags == YYTD_DATA8))
  88. goto out;
  89. tsize = table_size(th.td_lolen, th.td_flags);
  90. if (bsize < tsize)
  91. goto out;
  92. table = kvzalloc(tsize, GFP_KERNEL);
  93. if (table) {
  94. table->td_id = th.td_id;
  95. table->td_flags = th.td_flags;
  96. table->td_lolen = th.td_lolen;
  97. if (th.td_flags == YYTD_DATA8)
  98. UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
  99. u8, u8, byte_to_byte);
  100. else if (th.td_flags == YYTD_DATA16)
  101. UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
  102. u16, __be16, be16_to_cpu);
  103. else if (th.td_flags == YYTD_DATA32)
  104. UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
  105. u32, __be32, be32_to_cpu);
  106. else
  107. goto fail;
  108. /* if table was vmalloced make sure the page tables are synced
  109. * before it is used, as it goes live to all cpus.
  110. */
  111. if (is_vmalloc_addr(table))
  112. vm_unmap_aliases();
  113. }
  114. out:
  115. return table;
  116. fail:
  117. kvfree(table);
  118. return NULL;
  119. }
  120. /**
  121. * verify_dfa - verify that transitions and states in the tables are in bounds.
  122. * @dfa: dfa to test (NOT NULL)
  123. * @flags: flags controlling what type of accept table are acceptable
  124. *
  125. * Assumes dfa has gone through the first pass verification done by unpacking
  126. * NOTE: this does not valid accept table values
  127. *
  128. * Returns: %0 else error code on failure to verify
  129. */
  130. static int verify_dfa(struct aa_dfa *dfa, int flags)
  131. {
  132. size_t i, state_count, trans_count;
  133. int error = -EPROTO;
  134. /* check that required tables exist */
  135. if (!(dfa->tables[YYTD_ID_DEF] &&
  136. dfa->tables[YYTD_ID_BASE] &&
  137. dfa->tables[YYTD_ID_NXT] && dfa->tables[YYTD_ID_CHK]))
  138. goto out;
  139. /* accept.size == default.size == base.size */
  140. state_count = dfa->tables[YYTD_ID_BASE]->td_lolen;
  141. if (ACCEPT1_FLAGS(flags)) {
  142. if (!dfa->tables[YYTD_ID_ACCEPT])
  143. goto out;
  144. if (state_count != dfa->tables[YYTD_ID_ACCEPT]->td_lolen)
  145. goto out;
  146. }
  147. if (ACCEPT2_FLAGS(flags)) {
  148. if (!dfa->tables[YYTD_ID_ACCEPT2])
  149. goto out;
  150. if (state_count != dfa->tables[YYTD_ID_ACCEPT2]->td_lolen)
  151. goto out;
  152. }
  153. if (state_count != dfa->tables[YYTD_ID_DEF]->td_lolen)
  154. goto out;
  155. /* next.size == chk.size */
  156. trans_count = dfa->tables[YYTD_ID_NXT]->td_lolen;
  157. if (trans_count != dfa->tables[YYTD_ID_CHK]->td_lolen)
  158. goto out;
  159. /* if equivalence classes then its table size must be 256 */
  160. if (dfa->tables[YYTD_ID_EC] &&
  161. dfa->tables[YYTD_ID_EC]->td_lolen != 256)
  162. goto out;
  163. if (flags & DFA_FLAG_VERIFY_STATES) {
  164. for (i = 0; i < state_count; i++) {
  165. if (DEFAULT_TABLE(dfa)[i] >= state_count)
  166. goto out;
  167. if (base_idx(BASE_TABLE(dfa)[i]) + 255 >= trans_count) {
  168. printk(KERN_ERR "AppArmor DFA next/check upper "
  169. "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. }
  180. error = 0;
  181. out:
  182. return error;
  183. }
  184. /**
  185. * dfa_free - free a dfa allocated by aa_dfa_unpack
  186. * @dfa: the dfa to free (MAYBE NULL)
  187. *
  188. * Requires: reference count to dfa == 0
  189. */
  190. static void dfa_free(struct aa_dfa *dfa)
  191. {
  192. if (dfa) {
  193. int i;
  194. for (i = 0; i < ARRAY_SIZE(dfa->tables); i++) {
  195. kvfree(dfa->tables[i]);
  196. dfa->tables[i] = NULL;
  197. }
  198. kfree(dfa);
  199. }
  200. }
  201. /**
  202. * aa_dfa_free_kref - free aa_dfa by kref (called by aa_put_dfa)
  203. * @kr: kref callback for freeing of a dfa (NOT NULL)
  204. */
  205. void aa_dfa_free_kref(struct kref *kref)
  206. {
  207. struct aa_dfa *dfa = container_of(kref, struct aa_dfa, count);
  208. dfa_free(dfa);
  209. }
  210. /**
  211. * aa_dfa_unpack - unpack the binary tables of a serialized dfa
  212. * @blob: aligned serialized stream of data to unpack (NOT NULL)
  213. * @size: size of data to unpack
  214. * @flags: flags controlling what type of accept tables are acceptable
  215. *
  216. * Unpack a dfa that has been serialized. To find information on the dfa
  217. * format look in Documentation/admin-guide/LSM/apparmor.rst
  218. * Assumes the dfa @blob stream has been aligned on a 8 byte boundary
  219. *
  220. * Returns: an unpacked dfa ready for matching or ERR_PTR on failure
  221. */
  222. struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags)
  223. {
  224. int hsize;
  225. int error = -ENOMEM;
  226. char *data = blob;
  227. struct table_header *table = NULL;
  228. struct aa_dfa *dfa = kzalloc(sizeof(struct aa_dfa), GFP_KERNEL);
  229. if (!dfa)
  230. goto fail;
  231. kref_init(&dfa->count);
  232. error = -EPROTO;
  233. /* get dfa table set header */
  234. if (size < sizeof(struct table_set_header))
  235. goto fail;
  236. if (ntohl(*(__be32 *) data) != YYTH_MAGIC)
  237. goto fail;
  238. hsize = ntohl(*(__be32 *) (data + 4));
  239. if (size < hsize)
  240. goto fail;
  241. dfa->flags = ntohs(*(__be16 *) (data + 12));
  242. data += hsize;
  243. size -= hsize;
  244. while (size > 0) {
  245. table = unpack_table(data, size);
  246. if (!table)
  247. goto fail;
  248. switch (table->td_id) {
  249. case YYTD_ID_ACCEPT:
  250. if (!(table->td_flags & ACCEPT1_FLAGS(flags)))
  251. goto fail;
  252. break;
  253. case YYTD_ID_ACCEPT2:
  254. if (!(table->td_flags & ACCEPT2_FLAGS(flags)))
  255. goto fail;
  256. break;
  257. case YYTD_ID_BASE:
  258. if (table->td_flags != YYTD_DATA32)
  259. goto fail;
  260. break;
  261. case YYTD_ID_DEF:
  262. case YYTD_ID_NXT:
  263. case YYTD_ID_CHK:
  264. if (table->td_flags != YYTD_DATA16)
  265. goto fail;
  266. break;
  267. case YYTD_ID_EC:
  268. if (table->td_flags != YYTD_DATA8)
  269. goto fail;
  270. break;
  271. default:
  272. goto fail;
  273. }
  274. /* check for duplicate table entry */
  275. if (dfa->tables[table->td_id])
  276. goto fail;
  277. dfa->tables[table->td_id] = table;
  278. data += table_size(table->td_lolen, table->td_flags);
  279. size -= table_size(table->td_lolen, table->td_flags);
  280. table = NULL;
  281. }
  282. error = verify_dfa(dfa, flags);
  283. if (error)
  284. goto fail;
  285. return dfa;
  286. fail:
  287. kvfree(table);
  288. dfa_free(dfa);
  289. return ERR_PTR(error);
  290. }
  291. #define match_char(state, def, base, next, check, C) \
  292. do { \
  293. u32 b = (base)[(state)]; \
  294. unsigned int pos = base_idx(b) + (C); \
  295. if ((check)[pos] != (state)) { \
  296. (state) = (def)[(state)]; \
  297. break; \
  298. } \
  299. (state) = (next)[pos]; \
  300. break; \
  301. } while (1)
  302. /**
  303. * aa_dfa_match_len - traverse @dfa to find state @str stops at
  304. * @dfa: the dfa to match @str against (NOT NULL)
  305. * @start: the state of the dfa to start matching in
  306. * @str: the string of bytes to match against the dfa (NOT NULL)
  307. * @len: length of the string of bytes to match
  308. *
  309. * aa_dfa_match_len will match @str against the dfa and return the state it
  310. * finished matching in. The final state can be used to look up the accepting
  311. * label, or as the start state of a continuing match.
  312. *
  313. * This function will happily match again the 0 byte and only finishes
  314. * when @len input is consumed.
  315. *
  316. * Returns: final state reached after input is consumed
  317. */
  318. unsigned int aa_dfa_match_len(struct aa_dfa *dfa, unsigned int start,
  319. const char *str, int len)
  320. {
  321. u16 *def = DEFAULT_TABLE(dfa);
  322. u32 *base = BASE_TABLE(dfa);
  323. u16 *next = NEXT_TABLE(dfa);
  324. u16 *check = CHECK_TABLE(dfa);
  325. unsigned int state = start;
  326. if (state == 0)
  327. return 0;
  328. /* current state is <state>, matching character *str */
  329. if (dfa->tables[YYTD_ID_EC]) {
  330. /* Equivalence class table defined */
  331. u8 *equiv = EQUIV_TABLE(dfa);
  332. for (; len; len--)
  333. match_char(state, def, base, next, check,
  334. equiv[(u8) *str++]);
  335. } else {
  336. /* default is direct to next state */
  337. for (; len; len--)
  338. match_char(state, def, base, next, check, (u8) *str++);
  339. }
  340. return state;
  341. }
  342. /**
  343. * aa_dfa_match - traverse @dfa to find state @str stops at
  344. * @dfa: the dfa to match @str against (NOT NULL)
  345. * @start: the state of the dfa to start matching in
  346. * @str: the null terminated string of bytes to match against the dfa (NOT NULL)
  347. *
  348. * aa_dfa_match will match @str against the dfa and return the state it
  349. * finished matching in. The final state can be used to look up the accepting
  350. * label, or as the start state of a continuing match.
  351. *
  352. * Returns: final state reached after input is consumed
  353. */
  354. unsigned int aa_dfa_match(struct aa_dfa *dfa, unsigned int start,
  355. const char *str)
  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. unsigned int state = start;
  362. if (state == 0)
  363. return 0;
  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. /* default is direct to next state */
  369. while (*str)
  370. match_char(state, def, base, next, check,
  371. equiv[(u8) *str++]);
  372. } else {
  373. /* default is direct to next state */
  374. while (*str)
  375. match_char(state, def, base, next, check, (u8) *str++);
  376. }
  377. return state;
  378. }
  379. /**
  380. * aa_dfa_next - step one character to the next state in the dfa
  381. * @dfa: the dfa to tranverse (NOT NULL)
  382. * @state: the state to start in
  383. * @c: the input character to transition on
  384. *
  385. * aa_dfa_match will step through the dfa by one input character @c
  386. *
  387. * Returns: state reach after input @c
  388. */
  389. unsigned int aa_dfa_next(struct aa_dfa *dfa, unsigned int state,
  390. const char c)
  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. /* current state is <state>, matching character *str */
  397. if (dfa->tables[YYTD_ID_EC]) {
  398. /* Equivalence class table defined */
  399. u8 *equiv = EQUIV_TABLE(dfa);
  400. match_char(state, def, base, next, check, equiv[(u8) c]);
  401. } else
  402. match_char(state, def, base, next, check, (u8) c);
  403. return state;
  404. }
  405. /**
  406. * aa_dfa_match_until - traverse @dfa until accept state or end of input
  407. * @dfa: the dfa to match @str against (NOT NULL)
  408. * @start: the state of the dfa to start matching in
  409. * @str: the null terminated string of bytes to match against the dfa (NOT NULL)
  410. * @retpos: first character in str after match OR end of string
  411. *
  412. * aa_dfa_match will match @str against the dfa and return the state it
  413. * finished matching in. The final state can be used to look up the accepting
  414. * label, or as the start state of a continuing match.
  415. *
  416. * Returns: final state reached after input is consumed
  417. */
  418. unsigned int aa_dfa_match_until(struct aa_dfa *dfa, unsigned int start,
  419. const char *str, const char **retpos)
  420. {
  421. u16 *def = DEFAULT_TABLE(dfa);
  422. u32 *base = BASE_TABLE(dfa);
  423. u16 *next = NEXT_TABLE(dfa);
  424. u16 *check = CHECK_TABLE(dfa);
  425. u32 *accept = ACCEPT_TABLE(dfa);
  426. unsigned int state = start, pos;
  427. if (state == 0)
  428. return 0;
  429. /* current state is <state>, matching character *str */
  430. if (dfa->tables[YYTD_ID_EC]) {
  431. /* Equivalence class table defined */
  432. u8 *equiv = EQUIV_TABLE(dfa);
  433. /* default is direct to next state */
  434. while (*str) {
  435. pos = base_idx(base[state]) + equiv[(u8) *str++];
  436. if (check[pos] == state)
  437. state = next[pos];
  438. else
  439. state = def[state];
  440. if (accept[state])
  441. break;
  442. }
  443. } else {
  444. /* default is direct to next state */
  445. while (*str) {
  446. pos = base_idx(base[state]) + (u8) *str++;
  447. if (check[pos] == state)
  448. state = next[pos];
  449. else
  450. state = def[state];
  451. if (accept[state])
  452. break;
  453. }
  454. }
  455. *retpos = str;
  456. return state;
  457. }
  458. /**
  459. * aa_dfa_matchn_until - traverse @dfa until accept or @n bytes consumed
  460. * @dfa: the dfa to match @str against (NOT NULL)
  461. * @start: the state of the dfa to start matching in
  462. * @str: the string of bytes to match against the dfa (NOT NULL)
  463. * @n: length of the string of bytes to match
  464. * @retpos: first character in str after match OR str + n
  465. *
  466. * aa_dfa_match_len will match @str against the dfa and return the state it
  467. * finished matching in. The final state can be used to look up the accepting
  468. * label, or as the start state of a continuing match.
  469. *
  470. * This function will happily match again the 0 byte and only finishes
  471. * when @n input is consumed.
  472. *
  473. * Returns: final state reached after input is consumed
  474. */
  475. unsigned int aa_dfa_matchn_until(struct aa_dfa *dfa, unsigned int start,
  476. const char *str, int n, const char **retpos)
  477. {
  478. u16 *def = DEFAULT_TABLE(dfa);
  479. u32 *base = BASE_TABLE(dfa);
  480. u16 *next = NEXT_TABLE(dfa);
  481. u16 *check = CHECK_TABLE(dfa);
  482. u32 *accept = ACCEPT_TABLE(dfa);
  483. unsigned int state = start, pos;
  484. *retpos = NULL;
  485. if (state == 0)
  486. return 0;
  487. /* current state is <state>, matching character *str */
  488. if (dfa->tables[YYTD_ID_EC]) {
  489. /* Equivalence class table defined */
  490. u8 *equiv = EQUIV_TABLE(dfa);
  491. /* default is direct to next state */
  492. for (; n; n--) {
  493. pos = base_idx(base[state]) + equiv[(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. } else {
  502. /* default is direct to next state */
  503. for (; n; n--) {
  504. pos = base_idx(base[state]) + (u8) *str++;
  505. if (check[pos] == state)
  506. state = next[pos];
  507. else
  508. state = def[state];
  509. if (accept[state])
  510. break;
  511. }
  512. }
  513. *retpos = str;
  514. return state;
  515. }