match.c 11 KB

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