match.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. 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. return 0;
  36. error = PTR_ERR(nulldfa);
  37. nulldfa = NULL;
  38. return error;
  39. }
  40. void aa_teardown_dfa_engine(void)
  41. {
  42. aa_put_dfa(nulldfa);
  43. nulldfa = NULL;
  44. }
  45. /**
  46. * unpack_table - unpack a dfa table (one of accept, default, base, next check)
  47. * @blob: data to unpack (NOT NULL)
  48. * @bsize: size of blob
  49. *
  50. * Returns: pointer to table else NULL on failure
  51. *
  52. * NOTE: must be freed by kvfree (not kfree)
  53. */
  54. static struct table_header *unpack_table(char *blob, size_t bsize)
  55. {
  56. struct table_header *table = NULL;
  57. struct table_header th;
  58. size_t tsize;
  59. if (bsize < sizeof(struct table_header))
  60. goto out;
  61. /* loaded td_id's start at 1, subtract 1 now to avoid doing
  62. * it every time we use td_id as an index
  63. */
  64. th.td_id = be16_to_cpu(*(__be16 *) (blob)) - 1;
  65. if (th.td_id > YYTD_ID_MAX)
  66. goto out;
  67. th.td_flags = be16_to_cpu(*(__be16 *) (blob + 2));
  68. th.td_lolen = be32_to_cpu(*(__be32 *) (blob + 8));
  69. blob += sizeof(struct table_header);
  70. if (!(th.td_flags == YYTD_DATA16 || th.td_flags == YYTD_DATA32 ||
  71. th.td_flags == YYTD_DATA8))
  72. goto out;
  73. tsize = table_size(th.td_lolen, th.td_flags);
  74. if (bsize < tsize)
  75. goto out;
  76. table = kvzalloc(tsize, GFP_KERNEL);
  77. if (table) {
  78. table->td_id = th.td_id;
  79. table->td_flags = th.td_flags;
  80. table->td_lolen = th.td_lolen;
  81. if (th.td_flags == YYTD_DATA8)
  82. UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
  83. u8, u8, byte_to_byte);
  84. else if (th.td_flags == YYTD_DATA16)
  85. UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
  86. u16, __be16, be16_to_cpu);
  87. else if (th.td_flags == YYTD_DATA32)
  88. UNPACK_ARRAY(table->td_data, blob, th.td_lolen,
  89. u32, __be32, be32_to_cpu);
  90. else
  91. goto fail;
  92. /* if table was vmalloced make sure the page tables are synced
  93. * before it is used, as it goes live to all cpus.
  94. */
  95. if (is_vmalloc_addr(table))
  96. vm_unmap_aliases();
  97. }
  98. out:
  99. return table;
  100. fail:
  101. kvfree(table);
  102. return NULL;
  103. }
  104. /**
  105. * verify_dfa - verify that transitions and states in the tables are in bounds.
  106. * @dfa: dfa to test (NOT NULL)
  107. * @flags: flags controlling what type of accept table are acceptable
  108. *
  109. * Assumes dfa has gone through the first pass verification done by unpacking
  110. * NOTE: this does not valid accept table values
  111. *
  112. * Returns: %0 else error code on failure to verify
  113. */
  114. static int verify_dfa(struct aa_dfa *dfa, int flags)
  115. {
  116. size_t i, state_count, trans_count;
  117. int error = -EPROTO;
  118. /* check that required tables exist */
  119. if (!(dfa->tables[YYTD_ID_DEF] &&
  120. dfa->tables[YYTD_ID_BASE] &&
  121. dfa->tables[YYTD_ID_NXT] && dfa->tables[YYTD_ID_CHK]))
  122. goto out;
  123. /* accept.size == default.size == base.size */
  124. state_count = dfa->tables[YYTD_ID_BASE]->td_lolen;
  125. if (ACCEPT1_FLAGS(flags)) {
  126. if (!dfa->tables[YYTD_ID_ACCEPT])
  127. goto out;
  128. if (state_count != dfa->tables[YYTD_ID_ACCEPT]->td_lolen)
  129. goto out;
  130. }
  131. if (ACCEPT2_FLAGS(flags)) {
  132. if (!dfa->tables[YYTD_ID_ACCEPT2])
  133. goto out;
  134. if (state_count != dfa->tables[YYTD_ID_ACCEPT2]->td_lolen)
  135. goto out;
  136. }
  137. if (state_count != dfa->tables[YYTD_ID_DEF]->td_lolen)
  138. goto out;
  139. /* next.size == chk.size */
  140. trans_count = dfa->tables[YYTD_ID_NXT]->td_lolen;
  141. if (trans_count != dfa->tables[YYTD_ID_CHK]->td_lolen)
  142. goto out;
  143. /* if equivalence classes then its table size must be 256 */
  144. if (dfa->tables[YYTD_ID_EC] &&
  145. dfa->tables[YYTD_ID_EC]->td_lolen != 256)
  146. goto out;
  147. if (flags & DFA_FLAG_VERIFY_STATES) {
  148. for (i = 0; i < state_count; i++) {
  149. if (DEFAULT_TABLE(dfa)[i] >= state_count)
  150. goto out;
  151. if (base_idx(BASE_TABLE(dfa)[i]) + 255 >= trans_count) {
  152. printk(KERN_ERR "AppArmor DFA next/check upper "
  153. "bounds error\n");
  154. goto out;
  155. }
  156. }
  157. for (i = 0; i < trans_count; i++) {
  158. if (NEXT_TABLE(dfa)[i] >= state_count)
  159. goto out;
  160. if (CHECK_TABLE(dfa)[i] >= state_count)
  161. goto out;
  162. }
  163. }
  164. error = 0;
  165. out:
  166. return error;
  167. }
  168. /**
  169. * dfa_free - free a dfa allocated by aa_dfa_unpack
  170. * @dfa: the dfa to free (MAYBE NULL)
  171. *
  172. * Requires: reference count to dfa == 0
  173. */
  174. static void dfa_free(struct aa_dfa *dfa)
  175. {
  176. if (dfa) {
  177. int i;
  178. for (i = 0; i < ARRAY_SIZE(dfa->tables); i++) {
  179. kvfree(dfa->tables[i]);
  180. dfa->tables[i] = NULL;
  181. }
  182. kfree(dfa);
  183. }
  184. }
  185. /**
  186. * aa_dfa_free_kref - free aa_dfa by kref (called by aa_put_dfa)
  187. * @kr: kref callback for freeing of a dfa (NOT NULL)
  188. */
  189. void aa_dfa_free_kref(struct kref *kref)
  190. {
  191. struct aa_dfa *dfa = container_of(kref, struct aa_dfa, count);
  192. dfa_free(dfa);
  193. }
  194. /**
  195. * aa_dfa_unpack - unpack the binary tables of a serialized dfa
  196. * @blob: aligned serialized stream of data to unpack (NOT NULL)
  197. * @size: size of data to unpack
  198. * @flags: flags controlling what type of accept tables are acceptable
  199. *
  200. * Unpack a dfa that has been serialized. To find information on the dfa
  201. * format look in Documentation/admin-guide/LSM/apparmor.rst
  202. * Assumes the dfa @blob stream has been aligned on a 8 byte boundary
  203. *
  204. * Returns: an unpacked dfa ready for matching or ERR_PTR on failure
  205. */
  206. struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags)
  207. {
  208. int hsize;
  209. int error = -ENOMEM;
  210. char *data = blob;
  211. struct table_header *table = NULL;
  212. struct aa_dfa *dfa = kzalloc(sizeof(struct aa_dfa), GFP_KERNEL);
  213. if (!dfa)
  214. goto fail;
  215. kref_init(&dfa->count);
  216. error = -EPROTO;
  217. /* get dfa table set header */
  218. if (size < sizeof(struct table_set_header))
  219. goto fail;
  220. if (ntohl(*(__be32 *) data) != YYTH_MAGIC)
  221. goto fail;
  222. hsize = ntohl(*(__be32 *) (data + 4));
  223. if (size < hsize)
  224. goto fail;
  225. dfa->flags = ntohs(*(__be16 *) (data + 12));
  226. data += hsize;
  227. size -= hsize;
  228. while (size > 0) {
  229. table = unpack_table(data, size);
  230. if (!table)
  231. goto fail;
  232. switch (table->td_id) {
  233. case YYTD_ID_ACCEPT:
  234. if (!(table->td_flags & ACCEPT1_FLAGS(flags)))
  235. goto fail;
  236. break;
  237. case YYTD_ID_ACCEPT2:
  238. if (!(table->td_flags & ACCEPT2_FLAGS(flags)))
  239. goto fail;
  240. break;
  241. case YYTD_ID_BASE:
  242. if (table->td_flags != YYTD_DATA32)
  243. goto fail;
  244. break;
  245. case YYTD_ID_DEF:
  246. case YYTD_ID_NXT:
  247. case YYTD_ID_CHK:
  248. if (table->td_flags != YYTD_DATA16)
  249. goto fail;
  250. break;
  251. case YYTD_ID_EC:
  252. if (table->td_flags != YYTD_DATA8)
  253. goto fail;
  254. break;
  255. default:
  256. goto fail;
  257. }
  258. /* check for duplicate table entry */
  259. if (dfa->tables[table->td_id])
  260. goto fail;
  261. dfa->tables[table->td_id] = table;
  262. data += table_size(table->td_lolen, table->td_flags);
  263. size -= table_size(table->td_lolen, table->td_flags);
  264. table = NULL;
  265. }
  266. error = verify_dfa(dfa, flags);
  267. if (error)
  268. goto fail;
  269. return dfa;
  270. fail:
  271. kvfree(table);
  272. dfa_free(dfa);
  273. return ERR_PTR(error);
  274. }
  275. /**
  276. * aa_dfa_match_len - traverse @dfa to find state @str stops at
  277. * @dfa: the dfa to match @str against (NOT NULL)
  278. * @start: the state of the dfa to start matching in
  279. * @str: the string of bytes to match against the dfa (NOT NULL)
  280. * @len: length of the string of bytes to match
  281. *
  282. * aa_dfa_match_len will match @str against the dfa and return the state it
  283. * finished matching in. The final state can be used to look up the accepting
  284. * label, or as the start state of a continuing match.
  285. *
  286. * This function will happily match again the 0 byte and only finishes
  287. * when @len input is consumed.
  288. *
  289. * Returns: final state reached after input is consumed
  290. */
  291. unsigned int aa_dfa_match_len(struct aa_dfa *dfa, unsigned int start,
  292. const char *str, int len)
  293. {
  294. u16 *def = DEFAULT_TABLE(dfa);
  295. u32 *base = BASE_TABLE(dfa);
  296. u16 *next = NEXT_TABLE(dfa);
  297. u16 *check = CHECK_TABLE(dfa);
  298. unsigned int state = start, pos;
  299. if (state == 0)
  300. return 0;
  301. /* current state is <state>, matching character *str */
  302. if (dfa->tables[YYTD_ID_EC]) {
  303. /* Equivalence class table defined */
  304. u8 *equiv = EQUIV_TABLE(dfa);
  305. /* default is direct to next state */
  306. for (; len; len--) {
  307. pos = base_idx(base[state]) + equiv[(u8) *str++];
  308. if (check[pos] == state)
  309. state = next[pos];
  310. else
  311. state = def[state];
  312. }
  313. } else {
  314. /* default is direct to next state */
  315. for (; len; len--) {
  316. pos = base_idx(base[state]) + (u8) *str++;
  317. if (check[pos] == state)
  318. state = next[pos];
  319. else
  320. state = def[state];
  321. }
  322. }
  323. return state;
  324. }
  325. /**
  326. * aa_dfa_match - traverse @dfa to find state @str stops at
  327. * @dfa: the dfa to match @str against (NOT NULL)
  328. * @start: the state of the dfa to start matching in
  329. * @str: the null terminated string of bytes to match against the dfa (NOT NULL)
  330. *
  331. * aa_dfa_match will match @str against the dfa and return the state it
  332. * finished matching in. The final state can be used to look up the accepting
  333. * label, or as the start state of a continuing match.
  334. *
  335. * Returns: final state reached after input is consumed
  336. */
  337. unsigned int aa_dfa_match(struct aa_dfa *dfa, unsigned int start,
  338. const char *str)
  339. {
  340. u16 *def = DEFAULT_TABLE(dfa);
  341. u32 *base = BASE_TABLE(dfa);
  342. u16 *next = NEXT_TABLE(dfa);
  343. u16 *check = CHECK_TABLE(dfa);
  344. unsigned int state = start, pos;
  345. if (state == 0)
  346. return 0;
  347. /* current state is <state>, matching character *str */
  348. if (dfa->tables[YYTD_ID_EC]) {
  349. /* Equivalence class table defined */
  350. u8 *equiv = EQUIV_TABLE(dfa);
  351. /* default is direct to next state */
  352. while (*str) {
  353. pos = base_idx(base[state]) + equiv[(u8) *str++];
  354. if (check[pos] == state)
  355. state = next[pos];
  356. else
  357. state = def[state];
  358. }
  359. } else {
  360. /* default is direct to next state */
  361. while (*str) {
  362. pos = base_idx(base[state]) + (u8) *str++;
  363. if (check[pos] == state)
  364. state = next[pos];
  365. else
  366. state = def[state];
  367. }
  368. }
  369. return state;
  370. }
  371. /**
  372. * aa_dfa_next - step one character to the next state in the dfa
  373. * @dfa: the dfa to tranverse (NOT NULL)
  374. * @state: the state to start in
  375. * @c: the input character to transition on
  376. *
  377. * aa_dfa_match will step through the dfa by one input character @c
  378. *
  379. * Returns: state reach after input @c
  380. */
  381. unsigned int aa_dfa_next(struct aa_dfa *dfa, unsigned int state,
  382. const char c)
  383. {
  384. u16 *def = DEFAULT_TABLE(dfa);
  385. u32 *base = BASE_TABLE(dfa);
  386. u16 *next = NEXT_TABLE(dfa);
  387. u16 *check = CHECK_TABLE(dfa);
  388. unsigned int pos;
  389. /* current state is <state>, matching character *str */
  390. if (dfa->tables[YYTD_ID_EC]) {
  391. /* Equivalence class table defined */
  392. u8 *equiv = EQUIV_TABLE(dfa);
  393. /* default is direct to next state */
  394. pos = base_idx(base[state]) + equiv[(u8) c];
  395. if (check[pos] == state)
  396. state = next[pos];
  397. else
  398. state = def[state];
  399. } else {
  400. /* default is direct to next state */
  401. pos = base_idx(base[state]) + (u8) c;
  402. if (check[pos] == state)
  403. state = next[pos];
  404. else
  405. state = def[state];
  406. }
  407. return state;
  408. }