match.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor policy dfa matching engine definitions.
  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. #ifndef __AA_MATCH_H
  15. #define __AA_MATCH_H
  16. #include <linux/kref.h>
  17. #define DFA_NOMATCH 0
  18. #define DFA_START 1
  19. /**
  20. * The format used for transition tables is based on the GNU flex table
  21. * file format (--tables-file option; see Table File Format in the flex
  22. * info pages and the flex sources for documentation). The magic number
  23. * used in the header is 0x1B5E783D instead of 0xF13C57B1 though, because
  24. * new tables have been defined and others YY_ID_CHK (check) and YY_ID_DEF
  25. * (default) tables are used slightly differently (see the apparmor-parser
  26. * package).
  27. *
  28. *
  29. * The data in the packed dfa is stored in network byte order, and the tables
  30. * are arranged for flexibility. We convert the table data to host native
  31. * byte order.
  32. *
  33. * The dfa begins with a table set header, and is followed by the actual
  34. * tables.
  35. */
  36. #define YYTH_MAGIC 0x1B5E783D
  37. #define YYTH_FLAG_DIFF_ENCODE 1
  38. struct table_set_header {
  39. u32 th_magic; /* YYTH_MAGIC */
  40. u32 th_hsize;
  41. u32 th_ssize;
  42. u16 th_flags;
  43. char th_version[];
  44. };
  45. /* The YYTD_ID are one less than flex table mappings. The flex id
  46. * has 1 subtracted at table load time, this allows us to directly use the
  47. * ID's as indexes.
  48. */
  49. #define YYTD_ID_ACCEPT 0
  50. #define YYTD_ID_BASE 1
  51. #define YYTD_ID_CHK 2
  52. #define YYTD_ID_DEF 3
  53. #define YYTD_ID_EC 4
  54. #define YYTD_ID_META 5
  55. #define YYTD_ID_ACCEPT2 6
  56. #define YYTD_ID_NXT 7
  57. #define YYTD_ID_TSIZE 8
  58. #define YYTD_ID_MAX 8
  59. #define YYTD_DATA8 1
  60. #define YYTD_DATA16 2
  61. #define YYTD_DATA32 4
  62. #define YYTD_DATA64 8
  63. /* ACCEPT & ACCEPT2 tables gets 6 dedicated flags, YYTD_DATAX define the
  64. * first flags
  65. */
  66. #define ACCEPT1_FLAGS(X) ((X) & 0x3f)
  67. #define ACCEPT2_FLAGS(X) ACCEPT1_FLAGS((X) >> YYTD_ID_ACCEPT2)
  68. #define TO_ACCEPT1_FLAG(X) ACCEPT1_FLAGS(X)
  69. #define TO_ACCEPT2_FLAG(X) (ACCEPT1_FLAGS(X) << YYTD_ID_ACCEPT2)
  70. #define DFA_FLAG_VERIFY_STATES 0x1000
  71. struct table_header {
  72. u16 td_id;
  73. u16 td_flags;
  74. u32 td_hilen;
  75. u32 td_lolen;
  76. char td_data[];
  77. };
  78. #define DEFAULT_TABLE(DFA) ((u16 *)((DFA)->tables[YYTD_ID_DEF]->td_data))
  79. #define BASE_TABLE(DFA) ((u32 *)((DFA)->tables[YYTD_ID_BASE]->td_data))
  80. #define NEXT_TABLE(DFA) ((u16 *)((DFA)->tables[YYTD_ID_NXT]->td_data))
  81. #define CHECK_TABLE(DFA) ((u16 *)((DFA)->tables[YYTD_ID_CHK]->td_data))
  82. #define EQUIV_TABLE(DFA) ((u8 *)((DFA)->tables[YYTD_ID_EC]->td_data))
  83. #define ACCEPT_TABLE(DFA) ((u32 *)((DFA)->tables[YYTD_ID_ACCEPT]->td_data))
  84. #define ACCEPT_TABLE2(DFA) ((u32 *)((DFA)->tables[YYTD_ID_ACCEPT2]->td_data))
  85. struct aa_dfa {
  86. struct kref count;
  87. u16 flags;
  88. struct table_header *tables[YYTD_ID_TSIZE];
  89. };
  90. extern struct aa_dfa *nulldfa;
  91. extern struct aa_dfa *stacksplitdfa;
  92. #define byte_to_byte(X) (X)
  93. #define UNPACK_ARRAY(TABLE, BLOB, LEN, TTYPE, BTYPE, NTOHX) \
  94. do { \
  95. typeof(LEN) __i; \
  96. TTYPE *__t = (TTYPE *) TABLE; \
  97. BTYPE *__b = (BTYPE *) BLOB; \
  98. for (__i = 0; __i < LEN; __i++) { \
  99. __t[__i] = NTOHX(__b[__i]); \
  100. } \
  101. } while (0)
  102. static inline size_t table_size(size_t len, size_t el_size)
  103. {
  104. return ALIGN(sizeof(struct table_header) + len * el_size, 8);
  105. }
  106. int aa_setup_dfa_engine(void);
  107. void aa_teardown_dfa_engine(void);
  108. struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags);
  109. unsigned int aa_dfa_match_len(struct aa_dfa *dfa, unsigned int start,
  110. const char *str, int len);
  111. unsigned int aa_dfa_match(struct aa_dfa *dfa, unsigned int start,
  112. const char *str);
  113. unsigned int aa_dfa_next(struct aa_dfa *dfa, unsigned int state,
  114. const char c);
  115. unsigned int aa_dfa_match_until(struct aa_dfa *dfa, unsigned int start,
  116. const char *str, const char **retpos);
  117. unsigned int aa_dfa_matchn_until(struct aa_dfa *dfa, unsigned int start,
  118. const char *str, int n, const char **retpos);
  119. void aa_dfa_free_kref(struct kref *kref);
  120. #define WB_HISTORY_SIZE 8
  121. struct match_workbuf {
  122. unsigned int count;
  123. unsigned int pos;
  124. unsigned int len;
  125. unsigned int size; /* power of 2, same as history size */
  126. unsigned int history[WB_HISTORY_SIZE];
  127. };
  128. #define DEFINE_MATCH_WB(N) \
  129. struct match_workbuf N = { \
  130. .count = 0, \
  131. .pos = 0, \
  132. .len = 0, \
  133. .size = WB_HISTORY_SIZE, \
  134. }
  135. unsigned int aa_dfa_leftmatch(struct aa_dfa *dfa, unsigned int start,
  136. const char *str, unsigned int *count);
  137. /**
  138. * aa_get_dfa - increment refcount on dfa @p
  139. * @dfa: dfa (MAYBE NULL)
  140. *
  141. * Returns: pointer to @dfa if @dfa is NULL will return NULL
  142. * Requires: @dfa must be held with valid refcount when called
  143. */
  144. static inline struct aa_dfa *aa_get_dfa(struct aa_dfa *dfa)
  145. {
  146. if (dfa)
  147. kref_get(&(dfa->count));
  148. return dfa;
  149. }
  150. /**
  151. * aa_put_dfa - put a dfa refcount
  152. * @dfa: dfa to put refcount (MAYBE NULL)
  153. *
  154. * Requires: if @dfa != NULL that a valid refcount be held
  155. */
  156. static inline void aa_put_dfa(struct aa_dfa *dfa)
  157. {
  158. if (dfa)
  159. kref_put(&dfa->count, aa_dfa_free_kref);
  160. }
  161. #define MATCH_FLAG_DIFF_ENCODE 0x80000000
  162. #define MARK_DIFF_ENCODE 0x40000000
  163. #endif /* __AA_MATCH_H */