match.c 11 KB

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